From: Antony Polukhin <antoshkka@gmail.com>
To: "libstdc++" <libstdc++@gcc.gnu.org>,
gcc-patches List <gcc-patches@gcc.gnu.org>
Subject: [PATCH] PR libstdc++/89728 diagnose some missuses of [locale.convenience] functions
Date: Tue, 11 May 2021 21:27:41 +0300 [thread overview]
Message-ID: <CAKqmYPYf-Cp3Q68DuJwUzLNGZwS5b=tuzYoZk=dQLp=wFc5ESg@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 366 bytes --]
This patch provides compile time diagnostics for common misuse of
[locale.convenience] functions with std::string as a character type.
2021-05-11 Antony Polukhin <antoshkka@gmail.com>
PR libstdc++/89728
* include/bits/locale_facets.h (ctype) Add static assert.
* testsuite/22_locale/ctype/is/string/89728_neg.cc New test.
--
Best regards,
Antony Polukhin
[-- Attachment #2: ctype_compile_time_diag.txt --]
[-- Type: text/plain, Size: 4861 bytes --]
diff --git a/libstdc++-v3/include/bits/locale_facets.h b/libstdc++-v3/include/bits/locale_facets.h
index 03724cf..012857f 100644
--- a/libstdc++-v3/include/bits/locale_facets.h
+++ b/libstdc++-v3/include/bits/locale_facets.h
@@ -136,6 +136,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return __s;
}
+ template<typename>
+ struct __is_string
+ {
+ enum _Value { _value = 0 };
+ };
+
+ template<typename _CharT, typename _Traits, typename _Alloc>
+ struct __is_string<basic_string<_CharT, _Traits, _Alloc> >
+ {
+ enum _Value { _value = 1 };
+ };
// 22.2.1.1 Template class ctype
// Include host and configuration specific ctype enums for ctype_base.
@@ -614,6 +625,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _CharT>
class ctype : public __ctype_abstract_base<_CharT>
{
+#if __cplusplus >= 201103L
+ static_assert(!__is_string<_CharT>::_value,
+ "std::basic_string used as a character type");
+#endif
public:
// Types:
typedef _CharT char_type;
diff --git a/libstdc++-v3/testsuite/22_locale/ctype/is/string/89728_neg.cc b/libstdc++-v3/testsuite/22_locale/ctype/is/string/89728_neg.cc
new file mode 100644
index 0000000..987fa8e
--- /dev/null
+++ b/libstdc++-v3/testsuite/22_locale/ctype/is/string/89728_neg.cc
@@ -0,0 +1,73 @@
+// { dg-do compile { target c++11 } }
+
+// Copyright (C) 2021 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-error "used as a character type" "" { target *-*-* } 0 }
+
+#include <locale>
+
+template <class Char, int I>
+struct trait: std::char_traits<Char> {};
+
+template <class Char, int I>
+auto make_str()
+{
+ return std::basic_string<Char, trait<Char, I>>{};
+}
+
+void test01()
+{
+ const auto& loc = std::locale::classic();
+
+ std::isspace(std::string{}, loc); // { dg-error "required from here" }
+ std::isprint(make_str<char, 0>(), loc); // { dg-error "required from here" }
+ std::iscntrl(make_str<char, 1>(), loc); // { dg-error "required from here" }
+ std::isupper(make_str<char, 2>(), loc); // { dg-error "required from here" }
+ std::islower(make_str<char, 3>(), loc); // { dg-error "required from here" }
+ std::isalpha(make_str<char, 4>(), loc); // { dg-error "required from here" }
+ std::isdigit(make_str<char, 5>(), loc); // { dg-error "required from here" }
+ std::ispunct(make_str<char, 6>(), loc); // { dg-error "required from here" }
+ std::isxdigit(make_str<char, 7>(), loc); // { dg-error "required from here" }
+ std::isalnum(make_str<char, 8>(), loc); // { dg-error "required from here" }
+ std::isgraph(make_str<char, 9>(), loc); // { dg-error "required from here" }
+ std::isblank(make_str<char, 10>(), loc); // { dg-error "required from here" }
+ std::toupper(make_str<char, 11>(), loc); // { dg-error "required from here" }
+ std::tolower(make_str<char, 12>(), loc); // { dg-error "required from here" }
+}
+
+#ifdef _GLIBCXX_USE_WCHAR_T
+void test02()
+{
+ const auto& loc = std::locale::classic();
+
+ std::isspace(std::wstring{}, loc); // { dg-error "required from here" }
+ std::isprint(make_str<wchar_t, 0>(), loc); // { dg-error "required from here" }
+ std::iscntrl(make_str<wchar_t, 1>(), loc); // { dg-error "required from here" }
+ std::isupper(make_str<wchar_t, 2>(), loc); // { dg-error "required from here" }
+ std::islower(make_str<wchar_t, 3>(), loc); // { dg-error "required from here" }
+ std::isalpha(make_str<wchar_t, 4>(), loc); // { dg-error "required from here" }
+ std::isdigit(make_str<wchar_t, 5>(), loc); // { dg-error "required from here" }
+ std::ispunct(make_str<wchar_t, 6>(), loc); // { dg-error "required from here" }
+ std::isxdigit(make_str<wchar_t, 7>(), loc); // { dg-error "required from here" }
+ std::isalnum(make_str<wchar_t, 8>(), loc); // { dg-error "required from here" }
+ std::isgraph(make_str<wchar_t, 9>(), loc); // { dg-error "required from here" }
+ std::isblank(make_str<wchar_t, 10>(), loc); // { dg-error "required from here" }
+ std::toupper(make_str<wchar_t, 11>(), loc); // { dg-error "required from here" }
+ std::tolower(make_str<wchar_t, 12>(), loc); // { dg-error "required from here" }
+}
+#endif
next reply other threads:[~2021-05-11 18:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-11 18:27 Antony Polukhin [this message]
2021-05-11 20:00 ` Jonathan Wakely
2021-05-12 9:18 ` Jonathan Wakely
2021-05-12 9:58 ` Antony Polukhin
2021-05-12 12:35 ` Antony Polukhin
2021-05-12 14:44 ` Jonathan Wakely
2021-05-12 15:38 ` Antony Polukhin
2021-05-12 15:51 ` Antony Polukhin
2021-05-12 16:16 ` Jonathan Wakely
2021-05-17 17:14 ` Jonathan Wakely
2021-06-01 18:04 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAKqmYPYf-Cp3Q68DuJwUzLNGZwS5b=tuzYoZk=dQLp=wFc5ESg@mail.gmail.com' \
--to=antoshkka@gmail.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).