[committed] libstdc++: Fix recent changes to __glibcxx_assert [PR112882]

Message ID 20231207205558.950070-1-jwakely@redhat.com
State Committed
Commit 1395c573c523762957bde8c2a08832c5f4350815
Series [committed] libstdc++: Fix recent changes to __glibcxx_assert [PR112882] |


Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 warning Patch is already merged
linaro-tcwg-bot/tcwg_gcc_build--master-arm warning Patch is already merged

Commit Message

Jonathan Wakely Dec. 7, 2023, 8:55 p.m. UTC
  Tested x86_64-linux. Pushed to trunk.

-- >8 --

The changes in r14-6198-g5e8a30d8b8f4d7 were broken, as I used
_GLIBCXX17_CONSTEXPR for the 'if _GLIBCXX17_CONSTEXPR (true)' condition,
forgetting that it would also be used for the is_constant_evaluated()
check. Using 'if constexpr (std::is_constant_evaluated())' is a bug.

Additionally, relying on __glibcxx_assert_fail to give a "not a constant
expression" error is a problem because at -O0 an undefined reference to
__glibcxx_assert_fail is present in the compiled code. This means you
can't use libstdc++ headers without also linking to libstdc++ for the
symbol definition.

This fix rewrites the __glibcxx_assert macro again. This still avoids
doing the duplicate checks, once for constexpr and once at runtime (if
_GLIBCXX_ASSERTIONS is defined). When _GLIBCXX_ASSERTIONS is defined we
still rely on __glibcxx_assert_fail to give a "not a constant
expression" error during constant evaluation (because when assertions
are defined it's not a problem to emit a reference to the symbol). But
when that macro is not defined, we use a new inline (but not constexpr)
overload of __glibcxx_assert_fail to cause compilation to fail. That
inline function doesn't cause an undefined reference to a symbol in the
library (and will be optimized away anyway).

We can also add always_inline to the __is_constant_evaluated function,
although this doesn't actually matter for -O0 and it's always inlined
with any optimization enabled.


	PR libstdc++/112882
	* include/bits/c++config (__is_constant_evaluated): Add
	always_inline attribute.
	(_GLIBCXX_DO_ASSERT): Remove macro.
	(__glibcxx_assert): Define separately for assertions-enabled and
	constexpr-only cases.
 libstdc++-v3/include/bits/c++config | 33 ++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 10 deletions(-)


diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config
index 284d24d933f..25d37428fc1 100644
--- a/libstdc++-v3/include/bits/c++config
+++ b/libstdc++-v3/include/bits/c++config
@@ -538,6 +538,7 @@  namespace std
   // This can be used without checking if the compiler supports the feature.
   // The macro _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED can be used to check if
   // the compiler support is present to make this function work as expected.
+  __attribute__((__always_inline__))
   _GLIBCXX_CONSTEXPR inline bool
   __is_constant_evaluated() _GLIBCXX_NOEXCEPT
@@ -598,19 +599,31 @@  namespace std
-# define _GLIBCXX_DO_ASSERT true
-# define _GLIBCXX_DO_ASSERT std::__is_constant_evaluated()
-# define _GLIBCXX_DO_ASSERT false
+// Enable runtime assertion checks, and also check in constant expressions.
 # define __glibcxx_assert(cond)						\
   do {									\
-      if (__builtin_expect(!bool(cond), false))				\
-	_GLIBCXX_ASSERT_FAIL(cond);					\
+    if (__builtin_expect(!bool(cond), false))				\
+      _GLIBCXX_ASSERT_FAIL(cond);					\
   } while (false)
+// Only check assertions during constant evaluation.
+namespace std
+  __attribute__((__always_inline__,__visibility__("default")))
+  inline void
+  __glibcxx_assert_fail()
+  { }
+# define __glibcxx_assert(cond)						\
+  do {									\
+    if (std::__is_constant_evaluated())					\
+      if (__builtin_expect(!bool(cond), false))				\
+	std::__glibcxx_assert_fail();					\
+  } while (false)
+// Don't check any assertions.
+# define __glibcxx_assert(cond)
 // Macro indicating that TSAN is in use.