]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
stddef-h: Fix compilation errors in C++ mode (regression 2025-05-27).
authorBruno Haible <bruno@clisp.org>
Mon, 2 Jun 2025 16:32:26 +0000 (18:32 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 2 Jun 2025 16:43:34 +0000 (18:43 +0200)
* m4/stddef_h.m4 (gl_STDDEF_H): Set and substitute HAVE_C_UNREACHABLE.
Don't test for unreachable in <stddef.h> in C++. Set
GL_GENERATE_STDDEF_H to true always.
* lib/stddef.in.h (gl_unreachable): Renamed from _gl_unreachable. Test
HAVE_C_UNREACHABLE.
(unreachable): Don't define in C++ mode. Don't define if
HAVE_C_UNREACHABLE is 1.
* modules/stddef-h (Makefile.am): Substitute HAVE_C_UNREACHABLE.
* tests/test-stddef-h.c (test_unreachable_optimization,
test_unreachable_noreturn): Don't define in C++ mode.
(test_gl_unreachable_optimization, test_gl_unreachable_noreturn): New
functions.
* tests/test-stddef-h-c++3.cc (test_cxx_unreachable_1): Test
gl_unreachable instead of unreachable.
* lib/error.in.h (__gl_error_call1): Use gl_unreachable instead of
unreachable.

ChangeLog
lib/error.in.h
lib/stddef.in.h
m4/stddef_h.m4
modules/stddef-h
tests/test-stddef-h-c++3.cc
tests/test-stddef-h.c

index 51a3822484c75f7d2691c847395cedddd12a7744..a1d88c839df1bd2755d541034efe33bd50884dca 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2025-06-02  Bruno Haible  <bruno@clisp.org>
+
+       stddef-h: Fix compilation errors in C++ mode (regression 2025-05-27).
+       * m4/stddef_h.m4 (gl_STDDEF_H): Set and substitute HAVE_C_UNREACHABLE.
+       Don't test for unreachable in <stddef.h> in C++. Set
+       GL_GENERATE_STDDEF_H to true always.
+       * lib/stddef.in.h (gl_unreachable): Renamed from _gl_unreachable. Test
+       HAVE_C_UNREACHABLE.
+       (unreachable): Don't define in C++ mode. Don't define if
+       HAVE_C_UNREACHABLE is 1.
+       * modules/stddef-h (Makefile.am): Substitute HAVE_C_UNREACHABLE.
+       * tests/test-stddef-h.c (test_unreachable_optimization,
+       test_unreachable_noreturn): Don't define in C++ mode.
+       (test_gl_unreachable_optimization, test_gl_unreachable_noreturn): New
+       functions.
+       * tests/test-stddef-h-c++3.cc (test_cxx_unreachable_1): Test
+       gl_unreachable instead of unreachable.
+       * lib/error.in.h (__gl_error_call1): Use gl_unreachable instead of
+       unreachable.
+
 2025-06-02  Bruno Haible  <bruno@clisp.org>
 
        open, openat: Fix test failure on native Windows.
index 24a619f4a4461dd1d0c7c982cd64e8f176f2c87a..6c512ec8e5505ed0320e1f49670d83eccfef23f9 100644 (file)
@@ -39,7 +39,7 @@
 /* Get va_list.  */
 #include <stdarg.h>
 
-/* Get 'unreachable'.  */
+/* Get 'gl_unreachable'.  */
 #include <stddef.h>
 
 /* Get _GL_ATTRIBUTE_SPEC_PRINTF_STANDARD, _GL_ATTRIBUTE_SPEC_PRINTF_SYSTEM.  */
    It evaluates its arguments only once.
    Test case: Compile copy-file.c with "gcc -Wimplicit-fallthrough".  */
 #if defined __GNUC__ || defined __clang__
-/* Use 'unreachable' to tell the compiler when the function call does not
+/* Use 'gl_unreachable' to tell the compiler when the function call does not
    return.  */
 # define __gl_error_call1(function, status, ...) \
     ((function) (status, __VA_ARGS__), \
-     (status) != 0 ? unreachable () : (void) 0)
+     (status) != 0 ? gl_unreachable () : (void) 0)
 /* If STATUS is a not a constant, the function call may or may not return;
    therefore -Wimplicit-fallthrough will produce a warning.  Use a compound
    statement in order to evaluate STATUS only once.
index 2754e881e7fd0ac6298dc988ac8bbf752c747f5e..e8c55ff1cdc4a3a1b8bb460517952c606307e846 100644 (file)
@@ -188,6 +188,17 @@ typedef union
 #endif
 
 /* ISO C 23 ยง 7.21.1 The unreachable macro  */
+/* This macro is only usable in C, not in C++.
+   There is no way to define it as a macro in C++, because that would break code
+   that does
+       #include <utility>
+       ... std::unreachable() ...
+   Similarly, there is no way to define it as an inline function in C++, because
+   that would break code that does
+       #include <utility>
+       using std::unreachable;
+   As a workaround, we define a macro gl_unreachable, that is like unreachable,
+   but is usable in both C and C++.  */
 
 /* Code borrowed from verify.h.  */
 #ifndef _GL_HAS_BUILTIN_UNREACHABLE
@@ -203,9 +214,11 @@ typedef union
 #endif
 
 #if _GL_HAS_BUILTIN_UNREACHABLE
-# define _gl_unreachable() __builtin_unreachable ()
+# define gl_unreachable() __builtin_unreachable ()
 #elif 1200 <= _MSC_VER
-# define _gl_unreachable() __assume (0)
+# define gl_unreachable() __assume (0)
+#elif !defined __cplusplus && @HAVE_C_UNREACHABLE@
+# define gl_unreachable() unreachable ()
 #else
 /* Declare abort(), without including <stdlib.h>.  */
 extern
@@ -218,36 +231,14 @@ void abort (void)
 _GL_ATTRIBUTE_NOTHROW
 # endif
 ;
-# define _gl_unreachable() abort ()
+# define gl_unreachable() abort ()
 #endif
 
-#ifndef __cplusplus
+#if !defined __cplusplus && !@HAVE_C_UNREACHABLE@
 /* In C, define unreachable as a macro.  */
 
 # ifndef unreachable
-#  define unreachable() _gl_unreachable ()
-# endif
-
-#else
-/* In C++, define unreachable as an inline function.  */
-
-/* With some versions of MSVC, the inclusion of <utility> here causes errors
-   when <cstddef> gets included:
-   type_traits(1164): error C2065: 'max_align_t': undeclared identifier  */
-# if !defined _MSC_VER
-extern "C++" { /* needed for Cygwin */
-#  include <utility>
-}
-# endif
-
-# if defined __cpp_lib_unreachable /* C++23 or newer */
-
-using std::unreachable;
-
-# else
-
-inline void unreachable () { _gl_unreachable (); }
-
+#  define unreachable() gl_unreachable ()
 # endif
 
 #endif
index bdcb428017829970cd6817209aeda46e19042214..127ec05b60d19b732788cd0a88030ce2ab16cad4 100644 (file)
@@ -1,5 +1,5 @@
 # stddef_h.m4
-# serial 22
+# serial 23
 dnl Copyright (C) 2009-2025 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -77,32 +77,13 @@ AC_DEFUN_ONCE([gl_STDDEF_H],
     ])
   if test $gl_cv_c_func_unreachable = no; then
     GL_GENERATE_STDDEF_H=true
+    HAVE_C_UNREACHABLE=0
+  else
+    HAVE_C_UNREACHABLE=1
   fi
-  if test "$CXX" != no; then
-    dnl C++ <utility> has std::unreachable,
-    dnl see <https://en.cppreference.com/w/cpp/utility/unreachable>,
-    dnl but we want an unreachable() that is available from <stddef.h>,
-    dnl like in ISO C 23.
-    AC_CACHE_CHECK([for unreachable in <stddef.h> in C++],
-      [gl_cv_cxx_func_unreachable],
-      [dnl We can't use AC_LANG_PUSH([C++]) and AC_LANG_POP([C++]) here, due to
-       dnl an autoconf bug <https://savannah.gnu.org/support/?110294>.
-       cat > conftest.cpp <<\EOF
-#include <stddef.h>
-int main (void) { unreachable (); }
-EOF
-       gl_command="$CXX $CXXFLAGS $CPPFLAGS -c conftest.cpp"
-       if AC_TRY_EVAL([gl_command]); then
-         gl_cv_cxx_func_unreachable=yes
-       else
-         gl_cv_cxx_func_unreachable=no
-       fi
-       rm -fr conftest*
-      ])
-    if test $gl_cv_cxx_func_unreachable = no; then
-      GL_GENERATE_STDDEF_H=true
-    fi
-  fi
+  AC_SUBST([HAVE_C_UNREACHABLE])
+  dnl Provide gl_unreachable() unconditionally.
+  GL_GENERATE_STDDEF_H=true
 
   dnl https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114869
   AC_CACHE_CHECK([whether nullptr_t needs <stddef.h>],
index 96eeeaf88bc3149ee3abc97b5666f1d7796a6b9a..7f4f0c0ef640535b147450aa657da593d8a10460 100644 (file)
@@ -34,6 +34,7 @@ stddef.h: stddef.in.h $(top_builddir)/config.status
              -e 's|@''STDDEF_NOT_IDEMPOTENT''@|$(STDDEF_NOT_IDEMPOTENT)|g' \
              -e 's|@''REPLACE_NULL''@|$(REPLACE_NULL)|g' \
              -e 's|@''HAVE_MAX_ALIGN_T''@|$(HAVE_MAX_ALIGN_T)|g' \
+             -e 's|@''HAVE_C_UNREACHABLE''@|$(HAVE_C_UNREACHABLE)|g' \
              $(srcdir)/stddef.in.h > $@-t
        $(AM_V_at)mv $@-t $@
 else
index 77242183c8eafc14d943f0831a661475f181518c..b3b320b6711d44ca411649297c86fafdaa196b71 100644 (file)
@@ -16,7 +16,7 @@
 
 #include <config.h>
 
-/* Define unreachable.  */
+/* Define gl_unreachable.  */
 #include <stddef.h>
 /* Define std::unreachable (in C++23 or newer).  */
 #include <utility>
@@ -25,7 +25,7 @@ void
 test_cxx_unreachable_1 ()
 {
   if (2 < 1)
-    unreachable ();
+    gl_unreachable ();
 #if defined __cpp_lib_unreachable
   if (3 < 1)
     std::unreachable ();
index f201b411abd44e8c9c01adc2596f7cdf6f76e8de..b80df4d4452bb19c34ddc48397c00109899d2cf3 100644 (file)
@@ -69,6 +69,11 @@ static_assert (__alignof__ (struct d) <= __alignof__ (max_align_t));
 # endif
 #endif
 
+
+#ifndef __cplusplus
+
+/* Test 'unreachable'.  */
+
 int test_unreachable_optimization (int x);
 _Noreturn void test_unreachable_noreturn (void);
 
@@ -93,6 +98,36 @@ test_unreachable_noreturn (void)
   unreachable ();
 }
 
+#endif
+
+
+/* Test 'gl_unreachable'.  */
+
+int test_gl_unreachable_optimization (int x);
+_Noreturn void test_gl_unreachable_noreturn (void);
+
+int
+test_gl_unreachable_optimization (int x)
+{
+  /* Check that the compiler uses 'gl_unreachable' for optimization.
+     This function, when compiled with optimization, should have code
+     equivalent to
+       return x + 3;
+     Use 'objdump --disassemble test-stddef.o' to verify this.  */
+  if (x < 4)
+    gl_unreachable ();
+  return (x > 1 ? x + 3 : 2 * x + 10);
+}
+
+_Noreturn void
+test_gl_unreachable_noreturn (void)
+{
+  /* Check that the compiler's data-flow analysis recognizes
+     'gl_unreachable ()'.  This function should not elicit a warning.  */
+  gl_unreachable ();
+}
+
+
 #include <limits.h> /* INT_MAX */
 
 /* offsetof promotes to an unsigned integer if and only if sizes do