From: Jonathan Wakely Date: Tue, 5 Dec 2023 10:22:17 +0000 (+0000) Subject: libstdc++: Redefine __glibcxx_assert to work in C++23 constexpr X-Git-Tag: basepoints/gcc-15~3941 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5e8a30d8b8f4d7ea0a8340b46c1e0c865dbde781;p=thirdparty%2Fgcc.git libstdc++: Redefine __glibcxx_assert to work in C++23 constexpr The changes in r14-5979 to support unknown references in constant expressions caused some test regressions. The way that __glibcxx_assert is defined for constant evaluation no longer works when _GLIBCXX_ASSERTIONS is defined. This change simplifies __glibcxx_assert so that there is only one check, rather than a constexpr one and a conditionally-enabled runtime one. The constexpr one does not need to use __builtin_unreachable to cause a compilation failure, because __glibcxx_assert_fail is not usable in constant expressions, so that will cause a failure too. As well as fixing the regressions, this makes the code for the assertions shorter and simpler, so should be quicker to compile, and might inline better too. libstdc++-v3/ChangeLog: * include/bits/c++config (__glibcxx_assert_fail): Declare even when assertions are not enabled. (__glibcxx_constexpr_assert): Remove macro. (__glibcxx_assert_impl): Remove macro. (_GLIBCXX_ASSERT_FAIL): New macro. (_GLIBCXX_DO_ASSERT): New macro. (__glibcxx_assert): Simplify to a single definition that works at runtime and during constant evaluation. * testsuite/21_strings/basic_string_view/element_access/char/back_constexpr_neg.cc: Adjust expected errors. * testsuite/21_strings/basic_string_view/element_access/char/constexpr_neg.cc: Likewise. * testsuite/21_strings/basic_string_view/element_access/char/front_constexpr_neg.cc: Likewise. * testsuite/21_strings/basic_string_view/element_access/wchar_t/back_constexpr_neg.cc: Likewise. * testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr_neg.cc: Likewise. * testsuite/21_strings/basic_string_view/element_access/wchar_t/front_constexpr_neg.cc: Likewise. * testsuite/21_strings/basic_string_view/modifiers/remove_prefix/debug.cc: Likewise. * testsuite/21_strings/basic_string_view/modifiers/remove_suffix/debug.cc: Likewise. * testsuite/23_containers/span/back_neg.cc: Likewise. * testsuite/23_containers/span/front_neg.cc: Likewise. * testsuite/23_containers/span/index_op_neg.cc: Likewise. * testsuite/26_numerics/lcm/105844.cc: Likewise. --- diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config index 410c136e1b11..284d24d933f1 100644 --- a/libstdc++-v3/include/bits/c++config +++ b/libstdc++-v3/include/bits/c++config @@ -577,46 +577,41 @@ namespace std #undef _GLIBCXX_VERBOSE_ASSERT // Assert. -#if defined(_GLIBCXX_ASSERTIONS) \ - || defined(_GLIBCXX_PARALLEL) || defined(_GLIBCXX_PARALLEL_ASSERTIONS) -# ifdef _GLIBCXX_VERBOSE_ASSERT +#ifdef _GLIBCXX_VERBOSE_ASSERT namespace std { #pragma GCC visibility push(default) - // Avoid the use of assert, because we're trying to keep the - // include out of the mix. + // Don't use because this should be unaffected by NDEBUG. extern "C++" _GLIBCXX_NORETURN void - __glibcxx_assert_fail(const char* __file, int __line, - const char* __function, const char* __condition) + __glibcxx_assert_fail /* Called when a precondition violation is detected. */ + (const char* __file, int __line, const char* __function, + const char* __condition) _GLIBCXX_NOEXCEPT; #pragma GCC visibility pop } -#define __glibcxx_assert_impl(_Condition) \ - if (__builtin_expect(!bool(_Condition), false)) \ - { \ - __glibcxx_constexpr_assert(false); \ - std::__glibcxx_assert_fail(__FILE__, __LINE__, __PRETTY_FUNCTION__, \ - #_Condition); \ - } -# else // ! VERBOSE_ASSERT -# define __glibcxx_assert_impl(_Condition) \ - if (__builtin_expect(!bool(_Condition), false)) \ - { \ - __glibcxx_constexpr_assert(false); \ - __builtin_abort(); \ - } -# endif +# define _GLIBCXX_ASSERT_FAIL(_Condition) \ + std::__glibcxx_assert_fail(__FILE__, __LINE__, __PRETTY_FUNCTION__, \ + #_Condition) +#else // ! VERBOSE_ASSERT +# define _GLIBCXX_ASSERT_FAIL(_Condition) __builtin_abort() #endif #if defined(_GLIBCXX_ASSERTIONS) -# define __glibcxx_assert(cond) \ - do { __glibcxx_assert_impl(cond); } while (false) +# define _GLIBCXX_DO_ASSERT true +#elif _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED +# define _GLIBCXX_DO_ASSERT std::__is_constant_evaluated() #else -# define __glibcxx_assert(cond) \ - do { __glibcxx_constexpr_assert(cond); } while (false) +# define _GLIBCXX_DO_ASSERT false #endif +# define __glibcxx_assert(cond) \ + do { \ + if _GLIBCXX17_CONSTEXPR (_GLIBCXX_DO_ASSERT) \ + if (__builtin_expect(!bool(cond), false)) \ + _GLIBCXX_ASSERT_FAIL(cond); \ + } while (false) + // Macro indicating that TSAN is in use. #if __SANITIZE_THREAD__ # define _GLIBCXX_TSAN 1 diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/back_constexpr_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/back_constexpr_neg.cc index ffbc3069b1ca..39410dde260a 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/back_constexpr_neg.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/back_constexpr_neg.cc @@ -31,5 +31,4 @@ back() static_assert(back() != 'a'); // { dg-error "non-constant condition" } -// { dg-prune-output "in 'constexpr' expansion" } -// { dg-prune-output "unreachable" } +// { dg-error "assert_fail" "" { target *-*-* } 0 } diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/constexpr_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/constexpr_neg.cc index 3e718bfc98d4..59c465b7452c 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/constexpr_neg.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/constexpr_neg.cc @@ -30,5 +30,4 @@ test() static_assert(test() == 0); // { dg-error "non-constant condition" } -// { dg-prune-output "in 'constexpr' expansion" } -// { dg-prune-output "unreachable" } +// { dg-error "assert_fail" "" { target *-*-* } 0 } diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_constexpr_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_constexpr_neg.cc index f46fe40f929a..257b043c9693 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_constexpr_neg.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_constexpr_neg.cc @@ -31,5 +31,4 @@ front() static_assert(front() != 'a'); // { dg-error "non-constant condition" } -// { dg-prune-output "in 'constexpr' expansion" } -// { dg-prune-output "unreachable" } +// { dg-error "assert_fail" "" { target *-*-* } 0 } diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/back_constexpr_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/back_constexpr_neg.cc index c40b2f1cba6c..3ad9cea26003 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/back_constexpr_neg.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/back_constexpr_neg.cc @@ -31,5 +31,4 @@ back() static_assert(back() != L'a'); // { dg-error "non-constant condition" } -// { dg-prune-output "in 'constexpr' expansion" } -// { dg-prune-output "unreachable" } +// { dg-error "assert_fail" "" { target *-*-* } 0 } diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr_neg.cc index 46c56e7a9567..ec676d7d1f93 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr_neg.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr_neg.cc @@ -30,5 +30,4 @@ test() static_assert(test() == 0); // { dg-error "non-constant condition" } -// { dg-prune-output "in 'constexpr' expansion" } -// { dg-prune-output "unreachable" } +// { dg-error "assert_fail" "" { target *-*-* } 0 } diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/front_constexpr_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/front_constexpr_neg.cc index 89367e8144ba..c3ee7ffbf403 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/front_constexpr_neg.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/front_constexpr_neg.cc @@ -31,5 +31,4 @@ front() static_assert(front() != L'a'); // { dg-error "non-constant condition" } -// { dg-prune-output "in 'constexpr' expansion" } -// { dg-prune-output "unreachable" } +// { dg-error "assert_fail" "" { target *-*-* } 0 } diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_prefix/debug.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_prefix/debug.cc index 37204583b716..87a966007234 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_prefix/debug.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_prefix/debug.cc @@ -7,7 +7,7 @@ check_remove_prefix() { std::string_view sv("123"); sv.remove_prefix(4); - // { dg-error "not a constant expression" "" { target *-*-* } 0 } + // { dg-error "assert_fail" "" { target *-*-* } 0 } return true; } diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_suffix/debug.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_suffix/debug.cc index a549e4c2471b..513aa8ce28a6 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_suffix/debug.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_suffix/debug.cc @@ -7,7 +7,7 @@ check_remove_suffix() { std::string_view sv("123"); sv.remove_suffix(4); - // { dg-error "not a constant expression" "" { target *-*-* } 0 } + // { dg-error "assert_fail" "" { target *-*-* } 0 } return true; } diff --git a/libstdc++-v3/testsuite/23_containers/span/back_neg.cc b/libstdc++-v3/testsuite/23_containers/span/back_neg.cc index dce512aa1c83..494964be1309 100644 --- a/libstdc++-v3/testsuite/23_containers/span/back_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/back_neg.cc @@ -30,5 +30,5 @@ test01(bool b) static_assert(test01(false)); static_assert(test01(true)); // { dg-error "non-constant" } -// { dg-error "unreachable" "" { target *-*-* } 0 } -// { dg-prune-output "in 'constexpr' expansion" } +// { dg-error "assert_fail" "" { target *-*-* } 0 } +// { dg-prune-output "called in a constant expression" } diff --git a/libstdc++-v3/testsuite/23_containers/span/front_neg.cc b/libstdc++-v3/testsuite/23_containers/span/front_neg.cc index b2c5e9cae320..29fe3f03c9a6 100644 --- a/libstdc++-v3/testsuite/23_containers/span/front_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/front_neg.cc @@ -30,5 +30,5 @@ test01(bool b) static_assert(test01(false)); static_assert(test01(true)); // { dg-error "non-constant" } -// { dg-error "unreachable" "" { target *-*-* } 0 } -// { dg-prune-output "in 'constexpr' expansion" } +// { dg-error "assert_fail" "" { target *-*-* } 0 } +// { dg-prune-output "called in a constant expression" } diff --git a/libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc b/libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc index a2cdb8a44c7d..8a3e3e9b70d2 100644 --- a/libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc @@ -30,5 +30,5 @@ test01(bool b) static_assert(test01(false)); static_assert(test01(true)); // { dg-error "non-constant" } -// { dg-error "unreachable" "" { target *-*-* } 0 } -// { dg-prune-output "in 'constexpr' expansion" } +// { dg-error "assert_fail" "" { target *-*-* } 0 } +// { dg-prune-output "called in a constant expression" } diff --git a/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc b/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc index d853974f77e9..65a999c3e058 100644 --- a/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc +++ b/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc @@ -21,4 +21,4 @@ constexpr int e = std::lcm(500000u, 499999); // { dg-error "in .constexpr." } constexpr int f = std::lcm(499999u, 500000); // { dg-error "in .constexpr." } // { dg-error "overflow" "" { target *-*-* } 0 } -// { dg-error "unreachable" "" { target *-*-* } 0 } +// { dg-error "assert_fail" "" { target *-*-* } 0 }