]> Savannah Git Hosting - gnulib.git/commitdiff
verify: port better to C23
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 11 Sep 2022 20:39:48 +0000 (15:39 -0500)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 11 Sep 2022 20:54:16 +0000 (15:54 -0500)
* lib/verify.h (_GL_VERIFY, static_assert):
If C23, use static_assert keyword; no macro.
This should simplify diagnostics and debugging.

ChangeLog
doc/verify.texi
lib/verify.h

index 38df593ab6c380f12bf6086078e1a8013a291679..f4d9ce41390de5c12da7f59b9d319c07fc5c52fa 100644 (file)
--- 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.
index dee6fa1cb187ad1729458210a6ca37d89bb7289c..e4037adda1c7b6d1de40970cff2959e1ebd9214b 100644 (file)
@@ -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
index 47b6ee661b3fac0566fd2628b2eea5107b69e491..0066d211b2300cc5d790520e75ca8fc2980ece1d 100644 (file)
    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