]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Redefine __glibcxx_assert to work in C++23 constexpr
authorJonathan Wakely <jwakely@redhat.com>
Tue, 5 Dec 2023 10:22:17 +0000 (10:22 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 5 Dec 2023 23:33:22 +0000 (23:33 +0000)
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:
libstdc++-v3/include/bits/c++config
libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/back_constexpr_neg.cc
libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/constexpr_neg.cc
libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_constexpr_neg.cc
libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/back_constexpr_neg.cc
libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr_neg.cc
libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/front_constexpr_neg.cc
libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_prefix/debug.cc
libstdc++-v3/testsuite/21_strings/basic_string_view/modifiers/remove_suffix/debug.cc
libstdc++-v3/testsuite/23_containers/span/back_neg.cc
libstdc++-v3/testsuite/23_containers/span/front_neg.cc
libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc
libstdc++-v3/testsuite/26_numerics/lcm/105844.cc

index 410c136e1b11c5bc620406f7c1bf83ae5124c31d..284d24d933f1d35b5c0bb225c953124dc8ae3cf9 100644 (file)
@@ -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
index ffbc3069b1ca72b352a95974ef39ef9365acb9f1..39410dde260a25a40df9f1b29bb8384aff8c07c2 100644 (file)
@@ -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 }
index 3e718bfc98d4774a4bfa3c8dd0779931e4ebd9fc..59c465b7452c5da2dab73ffcb3a94310462757df 100644 (file)
@@ -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 }
index f46fe40f929aa7423a004f11f60c09025248a591..257b043c9693332870fb2fdf520f0c51425aaaa5 100644 (file)
@@ -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 }
index c40b2f1cba6c77b220538eea2bd7eb3cca933662..3ad9cea260036f38192fe5b07b404e6beb55ece0 100644 (file)
@@ -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 }
index 46c56e7a956791b95c03d8c0f7922d3595e733a0..ec676d7d1f93d0818113b3459bfc02ea6e83869c 100644 (file)
@@ -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 }
index 89367e8144baba19c1fd9732e7bbea16d6bb2440..c3ee7ffbf40350eff3b34c6425cf8f7c368ddd9a 100644 (file)
@@ -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 }
index 37204583b716472f69bc7cb2463df7175a999d0a..87a9660072346f081c349e5ff91798762dbd65f0 100644 (file)
@@ -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;
 }
 
index a549e4c2471b8beab4fe1350bf3535b982fa397e..513aa8ce28a62bb3c5bc9a04093397f7d2880826 100644 (file)
@@ -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;
 }
 
index dce512aa1c83503b982d018d48911f244f79fa39..494964be13099c741544c4cfc1660bfb23dd97d6 100644 (file)
@@ -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" }
index b2c5e9cae320c8856c527497a2b05cddbc6e684f..29fe3f03c9a6efe93474318ed4ffe95283ae5eb9 100644 (file)
@@ -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" }
index a2cdb8a44c7dabfe5d66d98b9946c4a42fc9e59d..8a3e3e9b70d271ea273f0897ab25e730418c0b40 100644 (file)
@@ -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" }
index d853974f77e94e3df290d3c5b768a6f6c653876a..65a999c3e058f6b01116cd829d61769eb0d19bae 100644 (file)
@@ -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 }