Skip to content

Commit 2d26fc8

Browse files
authored
[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.
1 parent 4d459df commit 2d26fc8

File tree

5 files changed

+29
-56
lines changed

5 files changed

+29
-56
lines changed

libcxx/include/atomic

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,10 +589,6 @@ template <class T>
589589

590590
#include <__config>
591591

592-
#if _LIBCPP_STD_VER < 23 && defined(_LIBCPP_STDATOMIC_H)
593-
# error <atomic> is incompatible with <stdatomic.h> before C++23. Please compile with -std=c++23.
594-
#endif
595-
596592
#include <__atomic/aliases.h>
597593
#include <__atomic/atomic.h>
598594
#include <__atomic/atomic_base.h>

libcxx/include/stdatomic.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ using std::atomic_signal_fence // see below
123123
# pragma GCC system_header
124124
#endif
125125

126-
#if defined(__cplusplus) && _LIBCPP_STD_VER >= 23
126+
#if defined(__cplusplus)
127127

128128
# include <atomic>
129129
# include <version>
@@ -156,10 +156,14 @@ using std::atomic_long _LIBCPP_USING_IF_EXISTS;
156156
using std::atomic_ulong _LIBCPP_USING_IF_EXISTS;
157157
using std::atomic_llong _LIBCPP_USING_IF_EXISTS;
158158
using std::atomic_ullong _LIBCPP_USING_IF_EXISTS;
159+
# ifndef _LIBCPP_HAS_NO_CHAR8_T
159160
using std::atomic_char8_t _LIBCPP_USING_IF_EXISTS;
161+
# endif
160162
using std::atomic_char16_t _LIBCPP_USING_IF_EXISTS;
161163
using std::atomic_char32_t _LIBCPP_USING_IF_EXISTS;
164+
# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
162165
using std::atomic_wchar_t _LIBCPP_USING_IF_EXISTS;
166+
# endif
163167

164168
using std::atomic_int8_t _LIBCPP_USING_IF_EXISTS;
165169
using std::atomic_uint8_t _LIBCPP_USING_IF_EXISTS;
@@ -224,16 +228,12 @@ using std::atomic_store_explicit _LIBCPP_USING_IF_EXISTS;
224228
using std::atomic_signal_fence _LIBCPP_USING_IF_EXISTS;
225229
using std::atomic_thread_fence _LIBCPP_USING_IF_EXISTS;
226230

227-
#elif defined(_LIBCPP_COMPILER_CLANG_BASED)
231+
#else
228232

229-
// Before C++23, we include the next <stdatomic.h> on the path to avoid hijacking
230-
// the header. We do this because Clang has historically shipped a <stdatomic.h>
231-
// header that would be available in all Standard modes, and we don't want to
232-
// break that use case.
233233
# if __has_include_next(<stdatomic.h>)
234234
# include_next <stdatomic.h>
235235
# endif
236236

237-
#endif // defined(__cplusplus) && _LIBCPP_STD_VER >= 23
237+
#endif // defined(__cplusplus)
238238

239239
#endif // _LIBCPP_STDATOMIC_H
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: no-threads
10+
11+
// This test verifies that <stdatomic.h> redirects to <atomic>. As an extension,
12+
// libc++ enables this redirection even before C++23.
13+
14+
// Ordinarily, <stdatomic.h> can be included after <atomic>, but including it
15+
// first doesn't work because its macros break <atomic>. Verify that
16+
// <stdatomic.h> can be included first.
17+
#include <stdatomic.h>
18+
#include <atomic>
19+
20+
#include <type_traits>
21+
22+
static_assert(std::is_same<atomic_int, std::atomic<int> >::value, "");

libcxx/test/libcxx/atomics/atomics.syn/incompatible_with_stdatomic.verify.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

libcxx/test/libcxx/atomics/stdatomic.h.syn/dont_hijack_header.compile.pass.cpp

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)