public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-6198] libstdc++: Redefine __glibcxx_assert to work in C++23 constexpr
@ 2023-12-05 23:34 Jonathan Wakely
0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-12-05 23:34 UTC (permalink / raw)
To: gcc-cvs, libstdc++-cvs
https://gcc.gnu.org/g:5e8a30d8b8f4d7ea0a8340b46c1e0c865dbde781
commit r14-6198-g5e8a30d8b8f4d7ea0a8340b46c1e0c865dbde781
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Tue Dec 5 10:22:17 2023 +0000
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:
---
libstdc++-v3/include/bits/c++config | 47 ++++++++++------------
.../element_access/char/back_constexpr_neg.cc | 3 +-
.../element_access/char/constexpr_neg.cc | 3 +-
.../element_access/char/front_constexpr_neg.cc | 3 +-
.../element_access/wchar_t/back_constexpr_neg.cc | 3 +-
.../element_access/wchar_t/constexpr_neg.cc | 3 +-
.../element_access/wchar_t/front_constexpr_neg.cc | 3 +-
.../modifiers/remove_prefix/debug.cc | 2 +-
.../modifiers/remove_suffix/debug.cc | 2 +-
.../testsuite/23_containers/span/back_neg.cc | 4 +-
.../testsuite/23_containers/span/front_neg.cc | 4 +-
.../testsuite/23_containers/span/index_op_neg.cc | 4 +-
libstdc++-v3/testsuite/26_numerics/lcm/105844.cc | 2 +-
13 files changed, 36 insertions(+), 47 deletions(-)
diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config
index 410c136e1b1..284d24d933f 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 ffbc3069b1c..39410dde260 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 3e718bfc98d..59c465b7452 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 f46fe40f929..257b043c969 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 c40b2f1cba6..3ad9cea2600 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 46c56e7a956..ec676d7d1f9 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 89367e8144b..c3ee7ffbf40 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 37204583b71..87a96600723 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 a549e4c2471..513aa8ce28a 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 dce512aa1c8..494964be130 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 b2c5e9cae32..29fe3f03c9a 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 a2cdb8a44c7..8a3e3e9b70d 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 d853974f77e..65a999c3e05 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 }
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-12-05 23:34 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-05 23:34 [gcc r14-6198] libstdc++: Redefine __glibcxx_assert to work in C++23 constexpr Jonathan Wakely
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).