libstdc++: Simplify constexpr checks in std::char_traits [PR 91488]
This removes the helper functions added by r8-1294 to detect whether the char_traits member functions can be evaluated at compile time. Instead, we can just use __builtin_constant_evaluated directly, which is well supported by non-GCC compilers by now. As a result, there is a chance that those members will no longer be usable in constant expressions when using old versions of non-GCC compilers. Make the relevant feature test macros depend on the availability of __builtin_constant_evaluated, so they are defined only when the feature is actualyl available. The new testcase from the PR is added to the libitm testsuite, because that's where we can be sure it's OK to use the -fgnu-tm option. Signed-off-by: Jonathan Wakely <jwakely@redhat.com> PR libstdc++/91488 libstdc++-v3/ChangeLog: * include/bits/basic_string.h (__cpp_lib_constexpr_string): Only define when is_constant_evaluated is available. * include/bits/char_traits.h (__cpp_lib_constexpr_char_traits): Likewise. (__constant_string_p, __constant_array_p): Remove. (char_traits): Use is_constant_evaluated directly. * include/std/version (__cpp_lib_constexpr_char_traits) (__cpp_lib_constexpr_string): Only define when is_constant_evaluated is available. libitm/ChangeLog: * testsuite/libitm.c++/libstdc++-pr91488.C: New test.
This commit is contained in:
parent
331e20a69b
commit
b376b1ef38
9
libitm/testsuite/libitm.c++/libstdc++-pr91488.C
Normal file
9
libitm/testsuite/libitm.c++/libstdc++-pr91488.C
Normal file
@ -0,0 +1,9 @@
|
||||
// PR libstdc++/91488 "inlining failed in call to always_inline"
|
||||
// { dg-do run }
|
||||
// { dg-additional-options "-O1" }
|
||||
|
||||
#include <string>
|
||||
|
||||
int main() {
|
||||
return std::char_traits<char>::length("");
|
||||
}
|
@ -52,12 +52,13 @@ namespace std _GLIBCXX_VISIBILITY(default)
|
||||
{
|
||||
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
|
||||
#if __cplusplus == 201703L
|
||||
#ifdef __cpp_lib_is_constant_evaluated
|
||||
// Support P1032R1 in C++20 (but not P0980R1 yet).
|
||||
# define __cpp_lib_constexpr_string 201811L
|
||||
#elif __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
// Support P0426R1 changes to char_traits in C++17.
|
||||
# define __cpp_lib_constexpr_string 201611L
|
||||
#elif __cplusplus > 201703L
|
||||
// Also support P1032R1 in C++20 (but not P0980R1 yet).
|
||||
# define __cpp_lib_constexpr_string 201811L
|
||||
#endif
|
||||
|
||||
#if _GLIBCXX_USE_CXX11_ABI
|
||||
|
@ -235,62 +235,12 @@ namespace std _GLIBCXX_VISIBILITY(default)
|
||||
{
|
||||
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
|
||||
#if __cplusplus >= 201703L
|
||||
|
||||
#if __cplusplus == 201703L
|
||||
// Unofficial macro indicating P0426R1 support
|
||||
# define __cpp_lib_constexpr_char_traits 201611L
|
||||
#else
|
||||
// Also support P1032R1 in C++20
|
||||
#ifdef __cpp_lib_is_constant_evaluated
|
||||
// Unofficial macro indicating P1032R1 support in C++20
|
||||
# define __cpp_lib_constexpr_char_traits 201811L
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Determine whether the characters of a NULL-terminated
|
||||
* string are known at compile time.
|
||||
* @param __s The string.
|
||||
*
|
||||
* Assumes that _CharT is a built-in character type.
|
||||
*/
|
||||
template<typename _CharT>
|
||||
static _GLIBCXX_ALWAYS_INLINE constexpr bool
|
||||
__constant_string_p(const _CharT* __s)
|
||||
{
|
||||
#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
(void) __s;
|
||||
// In constexpr contexts all strings should be constant.
|
||||
return __builtin_is_constant_evaluated();
|
||||
#else
|
||||
while (__builtin_constant_p(*__s) && *__s)
|
||||
__s++;
|
||||
return __builtin_constant_p(*__s);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Determine whether the characters of a character array are
|
||||
* known at compile time.
|
||||
* @param __a The character array.
|
||||
* @param __n Number of characters.
|
||||
*
|
||||
* Assumes that _CharT is a built-in character type.
|
||||
*/
|
||||
template<typename _CharT>
|
||||
static _GLIBCXX_ALWAYS_INLINE constexpr bool
|
||||
__constant_char_array_p(const _CharT* __a, size_t __n)
|
||||
{
|
||||
#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
(void) __a;
|
||||
(void) __n;
|
||||
// In constexpr contexts all character arrays should be constant.
|
||||
return __builtin_is_constant_evaluated();
|
||||
#else
|
||||
size_t __i = 0;
|
||||
while (__i < __n && __builtin_constant_p(__a[__i]))
|
||||
__i++;
|
||||
return __i == __n;
|
||||
#endif
|
||||
}
|
||||
#elif __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
// Unofficial macro indicating P0426R1 support in C++17
|
||||
# define __cpp_lib_constexpr_char_traits 201611L
|
||||
#endif
|
||||
|
||||
// 21.1
|
||||
@ -345,10 +295,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
{
|
||||
if (__n == 0)
|
||||
return 0;
|
||||
#if __cplusplus >= 201703L
|
||||
if (__builtin_constant_p(__n)
|
||||
&& __constant_char_array_p(__s1, __n)
|
||||
&& __constant_char_array_p(__s2, __n))
|
||||
#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
if (__builtin_is_constant_evaluated())
|
||||
{
|
||||
for (size_t __i = 0; __i < __n; ++__i)
|
||||
if (lt(__s1[__i], __s2[__i]))
|
||||
@ -364,8 +312,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
static _GLIBCXX17_CONSTEXPR size_t
|
||||
length(const char_type* __s)
|
||||
{
|
||||
#if __cplusplus >= 201703L
|
||||
if (__constant_string_p(__s))
|
||||
#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
if (__builtin_is_constant_evaluated())
|
||||
return __gnu_cxx::char_traits<char_type>::length(__s);
|
||||
#endif
|
||||
return __builtin_strlen(__s);
|
||||
@ -376,10 +324,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
{
|
||||
if (__n == 0)
|
||||
return 0;
|
||||
#if __cplusplus >= 201703L
|
||||
if (__builtin_constant_p(__n)
|
||||
&& __builtin_constant_p(__a)
|
||||
&& __constant_char_array_p(__s, __n))
|
||||
#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
if (__builtin_is_constant_evaluated())
|
||||
return __gnu_cxx::char_traits<char_type>::find(__s, __n, __a);
|
||||
#endif
|
||||
return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n));
|
||||
@ -476,10 +422,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
{
|
||||
if (__n == 0)
|
||||
return 0;
|
||||
#if __cplusplus >= 201703L
|
||||
if (__builtin_constant_p(__n)
|
||||
&& __constant_char_array_p(__s1, __n)
|
||||
&& __constant_char_array_p(__s2, __n))
|
||||
#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
if (__builtin_is_constant_evaluated())
|
||||
return __gnu_cxx::char_traits<char_type>::compare(__s1, __s2, __n);
|
||||
#endif
|
||||
return wmemcmp(__s1, __s2, __n);
|
||||
@ -488,8 +432,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
static _GLIBCXX17_CONSTEXPR size_t
|
||||
length(const char_type* __s)
|
||||
{
|
||||
#if __cplusplus >= 201703L
|
||||
if (__constant_string_p(__s))
|
||||
#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
if (__builtin_is_constant_evaluated())
|
||||
return __gnu_cxx::char_traits<char_type>::length(__s);
|
||||
#endif
|
||||
return wcslen(__s);
|
||||
@ -500,10 +444,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
{
|
||||
if (__n == 0)
|
||||
return 0;
|
||||
#if __cplusplus >= 201703L
|
||||
if (__builtin_constant_p(__n)
|
||||
&& __builtin_constant_p(__a)
|
||||
&& __constant_char_array_p(__s, __n))
|
||||
#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
if (__builtin_is_constant_evaluated())
|
||||
return __gnu_cxx::char_traits<char_type>::find(__s, __n, __a);
|
||||
#endif
|
||||
return wmemchr(__s, __a, __n);
|
||||
@ -597,10 +539,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
{
|
||||
if (__n == 0)
|
||||
return 0;
|
||||
#if __cplusplus > 201402
|
||||
if (__builtin_constant_p(__n)
|
||||
&& __constant_char_array_p(__s1, __n)
|
||||
&& __constant_char_array_p(__s2, __n))
|
||||
#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
if (__builtin_is_constant_evaluated())
|
||||
return __gnu_cxx::char_traits<char_type>::compare(__s1, __s2, __n);
|
||||
#endif
|
||||
return __builtin_memcmp(__s1, __s2, __n);
|
||||
@ -609,8 +549,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
static _GLIBCXX17_CONSTEXPR size_t
|
||||
length(const char_type* __s)
|
||||
{
|
||||
#if __cplusplus > 201402
|
||||
if (__constant_string_p(__s))
|
||||
#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
if (__builtin_is_constant_evaluated())
|
||||
return __gnu_cxx::char_traits<char_type>::length(__s);
|
||||
#endif
|
||||
size_t __i = 0;
|
||||
@ -624,10 +564,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
{
|
||||
if (__n == 0)
|
||||
return 0;
|
||||
#if __cplusplus > 201402
|
||||
if (__builtin_constant_p(__n)
|
||||
&& __builtin_constant_p(__a)
|
||||
&& __constant_char_array_p(__s, __n))
|
||||
#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
if (__builtin_is_constant_evaluated())
|
||||
return __gnu_cxx::char_traits<char_type>::find(__s, __n, __a);
|
||||
#endif
|
||||
return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n));
|
||||
|
@ -130,8 +130,10 @@
|
||||
#define __cpp_lib_chrono 201611
|
||||
#define __cpp_lib_clamp 201603
|
||||
#if __cplusplus == 201703L // N.B. updated value in C++20
|
||||
# define __cpp_lib_constexpr_char_traits 201611L
|
||||
# define __cpp_lib_constexpr_string 201611L
|
||||
# if _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
|
||||
# define __cpp_lib_constexpr_char_traits 201611L
|
||||
# define __cpp_lib_constexpr_string 201611L
|
||||
# endif
|
||||
#endif
|
||||
#define __cpp_lib_enable_shared_from_this 201603
|
||||
#define __cpp_lib_execution 201902L // FIXME: should be 201603L
|
||||
@ -213,14 +215,18 @@
|
||||
// FIXME: #define __cpp_lib_execution 201902L
|
||||
#define __cpp_lib_integer_comparison_functions 202002L
|
||||
#define __cpp_lib_constexpr_algorithms 201806L
|
||||
#define __cpp_lib_constexpr_char_traits 201811L
|
||||
#ifdef __cpp_lib_is_constant_evaluated
|
||||
# define __cpp_lib_constexpr_char_traits 201811L
|
||||
#endif
|
||||
#define __cpp_lib_constexpr_complex 201711L
|
||||
#define __cpp_lib_constexpr_dynamic_alloc 201907L
|
||||
#define __cpp_lib_constexpr_functional 201907L
|
||||
#define __cpp_lib_constexpr_iterator 201811L
|
||||
#define __cpp_lib_constexpr_memory 201811L
|
||||
#define __cpp_lib_constexpr_numeric 201911L
|
||||
#define __cpp_lib_constexpr_string 201811L
|
||||
#ifdef __cpp_lib_is_constant_evaluated
|
||||
# define __cpp_lib_constexpr_string 201811L
|
||||
#endif
|
||||
#define __cpp_lib_constexpr_string_view 201811L
|
||||
#define __cpp_lib_constexpr_tuple 201811L
|
||||
#define __cpp_lib_constexpr_utility 201811L
|
||||
|
Loading…
Reference in New Issue
Block a user