From 92675ce825a2068d1f87e7ac921e35ca60b3c34b Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 4 Feb 2023 14:31:08 +0100
Subject: [PATCH] assert-h, verify: Fix conflict with standard C++ header files
 on macOS.
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

* 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.
---
 ChangeLog                   |  9 +++++++++
 lib/verify.h                | 17 ++++++++++++++++-
 tests/test-assert-h-c++.cc  |  6 +++++-
 tests/test-assert-h-c++2.cc |  6 +++++-
 4 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 7b0b9112a7..eea8ce0dc2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2023-02-04  Bruno Haible  <bruno@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.
+
 2023-02-03  Bruno Haible  <bruno@clisp.org>
 
 	relocatable-prog: Fix compiler warning.
diff --git a/lib/verify.h b/lib/verify.h
index f751da2a98..e4a8278e03 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 \
       && (!defined __cplusplus \
           || (__cpp_static_assert < 201411 \
diff --git a/tests/test-assert-h-c++.cc b/tests/test-assert-h-c++.cc
index 1fc452338d..6b76565633 100644
--- a/tests/test-assert-h-c++.cc
+++ b/tests/test-assert-h-c++.cc
@@ -1,5 +1,5 @@
 /* Test of <assert.h> substitute in C++ mode.
-   Copyright (C) 2019-2022 Free Software Foundation, Inc.
+   Copyright (C) 2019-2023 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -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 9df136cfff..ffd7c8cf92 100644
--- a/tests/test-assert-h-c++2.cc
+++ b/tests/test-assert-h-c++2.cc
@@ -1,5 +1,5 @@
 /* Test of <assert.h> substitute in C++ mode.
-   Copyright (C) 2019-2022 Free Software Foundation, Inc.
+   Copyright (C) 2019-2023 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -18,3 +18,7 @@
 #include <config.h>
 
 #include <cassert>
+
+/* Check against conflicts between <cassert> and other C++ header files.  */
+#include <stddef.h>
+#include <iostream>
-- 
2.39.5