On 16/03/2023 12:48, Bruno Haible wrote:
ISO C 23 § 7.21.1 requires that <stddef.h> implements 'unreachable'.

This patch does it.


2023-03-16  Bruno Haible  <br...@clisp.org>

        stddef: Define 'unreachable', for ISO C 23 compliance.
        * lib/verify.h (_GL_HAS_BUILTIN_UNREACHABLE): Don't define if already
        defined.
        * lib/stddef.in.h (_GL_HAS_BUILTIN_UNREACHABLE, unreachable): New
        macros.
        (abort): Declare if needed for unreachable.
        * m4/stddef_h.m4 (gl_STDDEF_H): Test for unreachable.
        * tests/test-stddef.c (test_unreachable_optimization,
        test_unreachable_noreturn): New functions, based on tests/test-verify.c.
        * doc/posix-headers/stddef.texi: Mention unreachable.

diff --git a/doc/posix-headers/stddef.texi b/doc/posix-headers/stddef.texi
index e240f93363..33ad48244c 100644
--- a/doc/posix-headers/stddef.texi
+++ b/doc/posix-headers/stddef.texi
@@ -7,6 +7,10 @@
Portability problems fixed by Gnulib:
  @itemize
+@item
+Some platforms fail to provide @code{unreachable}, which was added in C23:
+GCC 13, clang 15, AIX with xlc 12.1, Solaris with Sun C 5.15, and others.
+
  @item
  Some platforms fail to provide @code{max_align_t}, which was added in C11:
  NetBSD 8.0, Solaris 11.0, and others.
diff --git a/lib/stddef.in.h b/lib/stddef.in.h
index 6eadcc3d5a..9e9d4b7cc6 100644
--- a/lib/stddef.in.h
+++ b/lib/stddef.in.h
@@ -18,7 +18,7 @@
  /* Written by Eric Blake.  */
/*
- * POSIX 2008 <stddef.h> for platforms that have issues.
+ * POSIX 2008 and ISO C 23 <stddef.h> for platforms that have issues.
   * <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stddef.h.html>
   */
@@ -142,6 +142,43 @@ typedef union
  # endif
  #endif
+/* ISO C 23 § 7.21.1 The unreachable macro */
+#ifndef unreachable
+
+/* Code borrowed from verify.h.  */
+# ifndef _GL_HAS_BUILTIN_UNREACHABLE
+#  if defined __clang_major__ && __clang_major__ < 5
+#   define _GL_HAS_BUILTIN_UNREACHABLE 0
+#  elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
+#   define _GL_HAS_BUILTIN_UNREACHABLE 1
+#  elif defined __has_builtin
+#   define _GL_HAS_BUILTIN_UNREACHABLE __has_builtin (__builtin_unreachable)
+#  else
+#   define _GL_HAS_BUILTIN_UNREACHABLE 0
+#  endif
+# endif
+
+# if _GL_HAS_BUILTIN_UNREACHABLE
+#  define unreachable() __builtin_unreachable ()
+# elif 1200 <= _MSC_VER
+#  define unreachable() __assume (0)
+# else
+/* Declare abort(), without including <stdlib.h>.  */
+extern
+#  if defined __cplusplus
+"C"
+#  endif
+_Noreturn

The above _Noreturn is causing a build failure on Solaris 10 on Sparc.
From config.log:

  $ gcc --version
  $ gcc (GCC) 3.4.3 (csl-sol210-3_4-branch+sol_rpath)

+void abort (void)
+#  if defined __cplusplus && (__GLIBC__ >= 2)
+throw ()
+#  endif
+;
+#  define unreachable() abort ()
+# endif
+
+#endif
+
  #  endif /* _@GUARD_PREFIX@_STDDEF_H */
  # endif /* _@GUARD_PREFIX@_STDDEF_H */
  #endif /* __need_XXX */

Commenting out that _Noreturn allows the build to complete.



Reply via email to