Skip to content

Commit bcb9b9f

Browse files
nggtru
authored andcommitted
[libcxx] Make stdatomic.h work when included from a C source file
If a C source file includes the libc++ stdatomic.h, compilation will break because (a) the C++ standard check will fail (which is expected), and (b) `_LIBCPP_COMPILER_CLANG_BASED` won't be defined because the logic defining it in `__config` is guarded by a `__cplusplus` check, so we'll end up with a blank header. Move the detection logic outside of the `__cplusplus` check to make the second check pass even in a C context when you're using Clang. Note that `_LIBCPP_STD_VER` is not defined when in C mode, hence stdatomic.h needs to check if in C++ mode before using that macro to avoid a warning. In an ideal world, a C source file wouldn't be including the libc++ header directory in its search path, so we'd never have this issue. Unfortunately, certain build environments make this hard to guarantee, and in this case it's easy to tweak this header to make it work in a C context, so I'm hoping this is acceptable. Fixes #57710. Differential Revision: https://reviews.llvm.org/D134591 (cherry picked from commit afec0f0)
1 parent d3e48d9 commit bcb9b9f

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

libcxx/include/__config

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@
2222
# pragma GCC system_header
2323
#endif
2424

25+
#if defined(__apple_build_version__)
26+
# define _LIBCPP_COMPILER_CLANG_BASED
27+
# define _LIBCPP_APPLE_CLANG_VER (__apple_build_version__ / 10000)
28+
#elif defined(__clang__)
29+
# define _LIBCPP_COMPILER_CLANG_BASED
30+
# define _LIBCPP_CLANG_VER (__clang_major__ * 100 + __clang_minor__)
31+
#elif defined(__GNUC__)
32+
# define _LIBCPP_COMPILER_GCC
33+
#elif defined(_MSC_VER)
34+
# define _LIBCPP_COMPILER_MSVC
35+
#endif
36+
2537
#ifdef __cplusplus
2638

2739
# define _LIBCPP_VERSION 15002
@@ -198,18 +210,6 @@
198210
# define __has_include(...) 0
199211
# endif
200212

201-
# if defined(__apple_build_version__)
202-
# define _LIBCPP_COMPILER_CLANG_BASED
203-
# define _LIBCPP_APPLE_CLANG_VER (__apple_build_version__ / 10000)
204-
# elif defined(__clang__)
205-
# define _LIBCPP_COMPILER_CLANG_BASED
206-
# define _LIBCPP_CLANG_VER (__clang_major__ * 100 + __clang_minor__)
207-
# elif defined(__GNUC__)
208-
# define _LIBCPP_COMPILER_GCC
209-
# elif defined(_MSC_VER)
210-
# define _LIBCPP_COMPILER_MSVC
211-
# endif
212-
213213
# if !defined(_LIBCPP_COMPILER_CLANG_BASED) && __cplusplus < 201103L
214214
# error "libc++ only supports C++03 with Clang-based compilers. Please enable C++11"
215215
# endif

libcxx/include/stdatomic.h

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

124-
#if _LIBCPP_STD_VER > 20
124+
#if defined(__cplusplus) && _LIBCPP_STD_VER > 20
125125

126126
#include <atomic>
127127
#include <version>
@@ -230,6 +230,6 @@ using std::atomic_thread_fence _LIBCPP_USING_IF_EXISTS;
230230
# include_next <stdatomic.h>
231231
# endif
232232

233-
#endif // _LIBCPP_STD_VER > 20
233+
#endif // defined(__cplusplus) && _LIBCPP_STD_VER > 20
234234

235235
#endif // _LIBCPP_STDATOMIC_H

libcxx/test/libcxx/include_as_c.sh.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#endif
3535
#include <math.h>
3636
#include <setjmp.h>
37+
#include <stdatomic.h>
3738
#include <stdbool.h>
3839
#include <stddef.h>
3940
#include <stdint.h>

0 commit comments

Comments
 (0)