bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] verify: port better to C23


From: Paul Eggert
Subject: [PATCH] verify: port better to C23
Date: Sun, 11 Sep 2022 15:54:38 -0500

* lib/verify.h (_GL_VERIFY, static_assert):
If C23, use static_assert keyword; no macro.
This should simplify diagnostics and debugging.
---
 ChangeLog       |  7 +++++++
 doc/verify.texi | 13 +++++--------
 lib/verify.h    | 15 ++++++++-------
 3 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 38df593ab6..f4d9ce4139 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2022-09-11  Paul Eggert  <eggert@cs.ucla.edu>
+
+       verify: port better to C23
+       * lib/verify.h (_GL_VERIFY, static_assert):
+       If C23, use static_assert keyword; no macro.
+       This should simplify diagnostics and debugging.
+
 2022-09-10  Bruno Haible  <bruno@clisp.org>
 
        pipe-filter-gi: Fix test failure on native Windows.
diff --git a/doc/verify.texi b/doc/verify.texi
index dee6fa1cb1..e4037adda1 100644
--- a/doc/verify.texi
+++ b/doc/verify.texi
@@ -51,17 +51,14 @@ integer constant expression, then a compiler might reject a 
usage like
 @samp{verify (@var{V});} even when @var{V} is
 nonzero.
 
-Although the standard @code{assert} macro is a runtime test, C2x
-specifies a builtin @code{_Static_assert (@var{V})},
-its @file{assert.h} header has a similar macro
-named @code{static_assert}, and C++17 has a similar
-@code{static_assert} builtin.  These builtins and macros differ
-from @code{verify} in two major ways.  First, they can also be used
+Although the standard @code{assert} macro is a runtime test, C23 and C++17
+specify a builtin @code{static_assert (@var{V})}, which differs
+from @code{verify} in two major ways.  First, it can also be used
 within a @code{struct} or @code{union} specifier, in place of an
-ordinary member declaration.  Second, they allow the programmer to
+ordinary member declaration.  Second, it allows the programmer to
 specify, as an optional second argument, a compile-time diagnostic as
 a string literal.  If your program is not intended to be portable to
-compilers that lack C2x or C++17 @code{static_assert}, the only
+compilers that lack C23 or C++17 @code{static_assert}, the only
 advantage of @code{verify} is that its name is a bit shorter.
 
 The @file{verify.h} header defines one more macro, @code{assume
diff --git a/lib/verify.h b/lib/verify.h
index 47b6ee661b..0066d211b2 100644
--- a/lib/verify.h
+++ b/lib/verify.h
@@ -25,19 +25,19 @@
    works as per C11.  This is supported by GCC 4.6.0+ and by clang 4+.
 
    Define _GL_HAVE__STATIC_ASSERT1 to 1 if _Static_assert (R) works as
-   per C2x.  This is supported by GCC 9.1+.
+   per C23.  This is supported by GCC 9.1+.
 
    Support compilers claiming conformance to the relevant standard,
    and also support GCC when not pedantic.  If we were willing to slow
    'configure' down we could also use it with other compilers, but
    since this affects only the quality of diagnostics, why bother?  */
 #ifndef __cplusplus
-# if (201112L <= __STDC_VERSION__ \
+# if (201112 <= __STDC_VERSION__ \
       || (!defined __STRICT_ANSI__ \
           && (4 < __GNUC__ + (6 <= __GNUC_MINOR__) || 5 <= __clang_major__)))
 #  define _GL_HAVE__STATIC_ASSERT 1
 # endif
-# if (202000L <= __STDC_VERSION__ \
+# if (202000 <= __STDC_VERSION__ \
       || (!defined __STRICT_ANSI__ && 9 <= __GNUC__))
 #  define _GL_HAVE__STATIC_ASSERT1 1
 # endif
@@ -202,12 +202,12 @@ template <int w>
 
    This macro requires three or more arguments but uses at most the first
    two, so that the _Static_assert macro optionally defined below supports
-   both the C11 two-argument syntax and the C2x one-argument syntax.
+   both the C11 two-argument syntax and the C23 one-argument syntax.
 
    Unfortunately, unlike C11, this implementation must appear as an
    ordinary declaration, and cannot appear inside struct { ... }.  */
 
-#if 200410 <= __cpp_static_assert
+#if 202311 <= __STDC_VERSION__ || 200410 <= __cpp_static_assert
 # define _GL_VERIFY(R, DIAGNOSTIC, ...) static_assert (R, DIAGNOSTIC)
 #elif defined _GL_HAVE__STATIC_ASSERT
 # define _GL_VERIFY(R, DIAGNOSTIC, ...) _Static_assert (R, DIAGNOSTIC)
@@ -226,7 +226,8 @@ template <int w>
 #  define _Static_assert(...) \
      _GL_VERIFY (__VA_ARGS__, "static assertion failed", -)
 # endif
-# if __cpp_static_assert < 201411 && !defined static_assert
+# if (!defined static_assert \
+      && __STDC_VERSION__ < 202311 && __cpp_static_assert < 201411)
 #  define static_assert _Static_assert /* C11 requires this #define.  */
 # endif
 #endif
@@ -303,7 +304,7 @@ template <int w>
 # define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
 #elif 1200 <= _MSC_VER
 # define assume(R) __assume (R)
-#elif 202311L <= __STDC_VERSION__
+#elif 202311 <= __STDC_VERSION__
 # include <stddef.h>
 # define assume(R) ((R) ? (void) 0 : unreachable ())
 #elif (defined GCC_LINT || defined lint) && _GL_HAS_BUILTIN_TRAP
-- 
2.37.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]