[libcxx-commits] [libcxx] 2d26fc8 - [libc++] Enable C++ stdatomic.h for all C++ versions (#95498)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Sep 19 06:43:58 PDT 2024


Author: Ryan Prichard
Date: 2024-09-19T09:43:54-04:00
New Revision: 2d26fc8c6c5015b77dc725695c1d957f412e4141

URL: https://github.com/llvm/llvm-project/commit/2d26fc8c6c5015b77dc725695c1d957f412e4141
DIFF: https://github.com/llvm/llvm-project/commit/2d26fc8c6c5015b77dc725695c1d957f412e4141.diff

LOG: [libc++] Enable C++ stdatomic.h for all C++ versions (#95498)

This extension is motivated by Android's use of libc++, where
<stdatomic.h> has redirected to <atomic> for many years, long before it
was standardized in C++23.

When libc++'s stdatomic.h is included in C translation units, delegate
to the next stdatomic.h, which could come from Clang or libc.

Added: 
    libcxx/test/libcxx/atomics/atomics.syn/compatible_with_stdatomic.compile.pass.cpp

Modified: 
    libcxx/include/atomic
    libcxx/include/stdatomic.h

Removed: 
    libcxx/test/libcxx/atomics/atomics.syn/incompatible_with_stdatomic.verify.cpp
    libcxx/test/libcxx/atomics/stdatomic.h.syn/dont_hijack_header.compile.pass.cpp


################################################################################
diff  --git a/libcxx/include/atomic b/libcxx/include/atomic
index 772ac998615a93..bf65750672bb0a 100644
--- a/libcxx/include/atomic
+++ b/libcxx/include/atomic
@@ -589,10 +589,6 @@ template <class T>
 
 #include <__config>
 
-#if _LIBCPP_STD_VER < 23 && defined(_LIBCPP_STDATOMIC_H)
-#  error <atomic> is incompatible with <stdatomic.h> before C++23. Please compile with -std=c++23.
-#endif
-
 #include <__atomic/aliases.h>
 #include <__atomic/atomic.h>
 #include <__atomic/atomic_base.h>

diff  --git a/libcxx/include/stdatomic.h b/libcxx/include/stdatomic.h
index 0ff14660e144a1..3206f7da8c8c45 100644
--- a/libcxx/include/stdatomic.h
+++ b/libcxx/include/stdatomic.h
@@ -123,7 +123,7 @@ using std::atomic_signal_fence                         // see below
 #  pragma GCC system_header
 #endif
 
-#if defined(__cplusplus) && _LIBCPP_STD_VER >= 23
+#if defined(__cplusplus)
 
 #  include <atomic>
 #  include <version>
@@ -156,10 +156,14 @@ using std::atomic_long _LIBCPP_USING_IF_EXISTS;
 using std::atomic_ulong _LIBCPP_USING_IF_EXISTS;
 using std::atomic_llong _LIBCPP_USING_IF_EXISTS;
 using std::atomic_ullong _LIBCPP_USING_IF_EXISTS;
+#  ifndef _LIBCPP_HAS_NO_CHAR8_T
 using std::atomic_char8_t _LIBCPP_USING_IF_EXISTS;
+#  endif
 using std::atomic_char16_t _LIBCPP_USING_IF_EXISTS;
 using std::atomic_char32_t _LIBCPP_USING_IF_EXISTS;
+#  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
 using std::atomic_wchar_t _LIBCPP_USING_IF_EXISTS;
+#  endif
 
 using std::atomic_int8_t _LIBCPP_USING_IF_EXISTS;
 using std::atomic_uint8_t _LIBCPP_USING_IF_EXISTS;
@@ -224,16 +228,12 @@ using std::atomic_store_explicit _LIBCPP_USING_IF_EXISTS;
 using std::atomic_signal_fence _LIBCPP_USING_IF_EXISTS;
 using std::atomic_thread_fence _LIBCPP_USING_IF_EXISTS;
 
-#elif defined(_LIBCPP_COMPILER_CLANG_BASED)
+#else
 
-// Before C++23, we include the next <stdatomic.h> on the path to avoid hijacking
-// the header. We do this because Clang has historically shipped a <stdatomic.h>
-// header that would be available in all Standard modes, and we don't want to
-// break that use case.
 #  if __has_include_next(<stdatomic.h>)
 #    include_next <stdatomic.h>
 #  endif
 
-#endif // defined(__cplusplus) && _LIBCPP_STD_VER >= 23
+#endif // defined(__cplusplus)
 
 #endif // _LIBCPP_STDATOMIC_H

diff  --git a/libcxx/test/libcxx/atomics/atomics.syn/compatible_with_stdatomic.compile.pass.cpp b/libcxx/test/libcxx/atomics/atomics.syn/compatible_with_stdatomic.compile.pass.cpp
new file mode 100644
index 00000000000000..a52459abb9e0a5
--- /dev/null
+++ b/libcxx/test/libcxx/atomics/atomics.syn/compatible_with_stdatomic.compile.pass.cpp
@@ -0,0 +1,22 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: no-threads
+
+// This test verifies that <stdatomic.h> redirects to <atomic>. As an extension,
+// libc++ enables this redirection even before C++23.
+
+// Ordinarily, <stdatomic.h> can be included after <atomic>, but including it
+// first doesn't work because its macros break <atomic>. Verify that
+// <stdatomic.h> can be included first.
+#include <stdatomic.h>
+#include <atomic>
+
+#include <type_traits>
+
+static_assert(std::is_same<atomic_int, std::atomic<int> >::value, "");

diff  --git a/libcxx/test/libcxx/atomics/atomics.syn/incompatible_with_stdatomic.verify.cpp b/libcxx/test/libcxx/atomics/atomics.syn/incompatible_with_stdatomic.verify.cpp
deleted file mode 100644
index ca092d9c602753..00000000000000
--- a/libcxx/test/libcxx/atomics/atomics.syn/incompatible_with_stdatomic.verify.cpp
+++ /dev/null
@@ -1,22 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: no-threads
-// REQUIRES: c++03 || c++11 || c++14 || c++17 || c++20
-
-// This test ensures that we issue a reasonable diagnostic when including <atomic> after
-// <stdatomic.h> has been included. Before C++23, this otherwise leads to obscure errors
-// because <atomic> may try to redefine things defined by <stdatomic.h>.
-
-// Ignore additional weird errors that happen when the two headers are mixed.
-// ADDITIONAL_COMPILE_FLAGS: -Xclang -verify-ignore-unexpected=error -Xclang -verify-ignore-unexpected=warning
-
-#include <stdatomic.h>
-#include <atomic>
-
-// expected-error@*:* {{<atomic> is incompatible with <stdatomic.h> before C++23.}}

diff  --git a/libcxx/test/libcxx/atomics/stdatomic.h.syn/dont_hijack_header.compile.pass.cpp b/libcxx/test/libcxx/atomics/stdatomic.h.syn/dont_hijack_header.compile.pass.cpp
deleted file mode 100644
index f35eddfb353354..00000000000000
--- a/libcxx/test/libcxx/atomics/stdatomic.h.syn/dont_hijack_header.compile.pass.cpp
+++ /dev/null
@@ -1,23 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: no-threads
-
-// This test ensures that we don't hijack the <stdatomic.h> header even when compiling
-// before C++23, since Clang used to provide that header before libc++ provided one.
-
-// On GCC, the compiler-provided <stdatomic.h> is not C++ friendly, so including <stdatomic.h>
-// doesn't work at all if we don't use the <stdatomic.h> provided by libc++ in C++23 and above.
-// XFAIL: (c++11 || c++14 || c++17 || c++20) && gcc
-
-#include <stdatomic.h>
-
-void f() {
-  atomic_int i; // just make sure the header isn't empty
-  (void)i;
-}


        


More information about the libcxx-commits mailing list