After strengthening the C++ tests of <assert.h>, I get compilation errors on macOS 12.5, such as:
In file included from ../../gltests/test-assert-h-c++.cc:26: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream:37: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:214: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:15: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/string:520: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__functional_base:26: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/utility:221: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:411:5: error: storage class specified for a member declaration static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::pair<T1, T2>>"); ^ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__config:896:29: note: expanded from macro 'static_assert' # define static_assert(...) _Static_assert(__VA_ARGS__) ^ ../gllib/assert.h:256:7: note: expanded from macro '_Static_assert' _GL_STATIC_ASSERT (__VA_ARGS__, "static assertion failed", -) ^ ../gllib/assert.h:244:5: note: expanded from macro '_GL_STATIC_ASSERT' extern int (*_GL_GENSYM (_gl_static_assert_function) (void)) \ ^ The problem is that _GL_STATIC_ASSERT expands to something that starts with 'extern', and that is apparently forbidden in C++ in member-declaration position. This patch fixes it. 2023-02-04 Bruno Haible <br...@clisp.org> assert-h, verify: Fix conflict with standard C++ header files on macOS. * lib/verify.h (_Static_assert): Don't redefine with clang ≥ 3.8.0 in C++ mode. * tests/test-assert-h-c++.cc: Also check against conflict with the standard C++ header files. * tests/test-assert-h-c++2.cc: Likewise. diff --git a/lib/verify.h b/lib/verify.h index b63cb26432..8f786af7f5 100644 --- a/lib/verify.h +++ b/lib/verify.h @@ -222,7 +222,21 @@ template <int w> /* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */ #ifdef _GL_STATIC_ASSERT_H -# if !defined _GL_HAVE__STATIC_ASSERT1 && !defined _Static_assert +/* Define _Static_assert if needed. */ +/* With clang ≥ 3.8.0 in C++ mode, _Static_assert already works and accepts + 1 or 2 arguments. We better don't override it, because clang's standard + C++ library uses static_assert inside classes in several places, and our + replacement via _GL_VERIFY does not work in these contexts. */ +# if (defined __cplusplus && defined __clang__ \ + && (4 <= __clang_major__ + (8 <= __clang_minor__))) +# if 5 <= __clang_major__ +/* Avoid "warning: 'static_assert' with no message is a C++17 extension". */ +# pragma clang diagnostic ignored "-Wc++17-extensions" +# else +/* Avoid "warning: static_assert with no message is a C++1z extension". */ +# pragma clang diagnostic ignored "-Wc++1z-extensions" +# endif +# elif !defined _GL_HAVE__STATIC_ASSERT1 && !defined _Static_assert # if !defined _MSC_VER || defined __clang__ # define _Static_assert(...) \ _GL_VERIFY (__VA_ARGS__, "static assertion failed", -) @@ -233,6 +247,7 @@ template <int w> _GL_VERIFY ((R), "static assertion failed", -) # endif # endif +/* Define static_assert if needed. */ # if (!defined static_assert \ && __STDC_VERSION__ < 202311 \ && (!defined __cplusplus \ diff --git a/tests/test-assert-h-c++.cc b/tests/test-assert-h-c++.cc index 4fa23bfeec..6b76565633 100644 --- a/tests/test-assert-h-c++.cc +++ b/tests/test-assert-h-c++.cc @@ -21,6 +21,10 @@ #include <assert.h> +/* Check against conflicts between <assert.h> and the C++ header files. */ +#include <stddef.h> +#include <iostream> + int main () diff --git a/tests/test-assert-h-c++2.cc b/tests/test-assert-h-c++2.cc index 3c19de325c..ffd7c8cf92 100644 --- a/tests/test-assert-h-c++2.cc +++ b/tests/test-assert-h-c++2.cc @@ -18,3 +18,7 @@ #include <config.h> #include <cassert> + +/* Check against conflicts between <cassert> and other C++ header files. */ +#include <stddef.h> +#include <iostream>