diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2023-12-05 10:22:17 +0000 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2023-12-05 23:33:22 +0000 |
commit | 5e8a30d8b8f4d7ea0a8340b46c1e0c865dbde781 (patch) | |
tree | 39f8e3ed38235051a45cfef65eda4d27db042433 | |
parent | 2e7abd09621a4401d44f4513adf126bce4b4828b (diff) | |
download | gcc-5e8a30d8b8f4d7ea0a8340b46c1e0c865dbde781.zip gcc-5e8a30d8b8f4d7ea0a8340b46c1e0c865dbde781.tar.gz gcc-5e8a30d8b8f4d7ea0a8340b46c1e0c865dbde781.tar.bz2 |
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.
13 files changed, 36 insertions, 47 deletions
diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config index 410c136..284d24d 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 <cassert> - // include out of the mix. + // Don't use <cassert> 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 ffbc306..39410dd 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 3e718bf..59c465b 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 f46fe40..257b043 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 c40b2f1..3ad9cea 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 46c56e7..ec676d7 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 89367e8..c3ee7ff 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 3720458..87a9660 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 a549e4c..513aa8c 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 dce512a..494964b 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 b2c5e9c..29fe3f0 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 a2cdb8a4..8a3e3e9 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 d853974..65a999c 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 } |