[llvm-branch-commits] [libcxx] bcb9b9f - [libcxx] Make stdatomic.h work when included from a C source file

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Sep 29 23:20:52 PDT 2022


Author: Gergely Nagy
Date: 2022-09-30T08:20:26+02:00
New Revision: bcb9b9fc44e4d58877873b5b4b310b0c235017fe

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

LOG: [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 https://github.com/llvm/llvm-project/issues/57710.

Differential Revision: https://reviews.llvm.org/D134591

(cherry picked from commit afec0f0ec38a72bcc6a697c1cefb1dac0bbd02fb)

Added: 
    

Modified: 
    libcxx/include/__config
    libcxx/include/stdatomic.h
    libcxx/test/libcxx/include_as_c.sh.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__config b/libcxx/include/__config
index cfad605e84fb9..01377a9617ea4 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -22,6 +22,18 @@
 #  pragma GCC system_header
 #endif
 
+#if defined(__apple_build_version__)
+#  define _LIBCPP_COMPILER_CLANG_BASED
+#  define _LIBCPP_APPLE_CLANG_VER (__apple_build_version__ / 10000)
+#elif defined(__clang__)
+#  define _LIBCPP_COMPILER_CLANG_BASED
+#  define _LIBCPP_CLANG_VER (__clang_major__ * 100 + __clang_minor__)
+#elif defined(__GNUC__)
+#  define _LIBCPP_COMPILER_GCC
+#elif defined(_MSC_VER)
+#  define _LIBCPP_COMPILER_MSVC
+#endif
+
 #ifdef __cplusplus
 
 #  define _LIBCPP_VERSION 15002
@@ -198,18 +210,6 @@
 #    define __has_include(...) 0
 #  endif
 
-#  if defined(__apple_build_version__)
-#    define _LIBCPP_COMPILER_CLANG_BASED
-#    define _LIBCPP_APPLE_CLANG_VER (__apple_build_version__ / 10000)
-#  elif defined(__clang__)
-#    define _LIBCPP_COMPILER_CLANG_BASED
-#    define _LIBCPP_CLANG_VER (__clang_major__ * 100 + __clang_minor__)
-#  elif defined(__GNUC__)
-#    define _LIBCPP_COMPILER_GCC
-#  elif defined(_MSC_VER)
-#    define _LIBCPP_COMPILER_MSVC
-#  endif
-
 #  if !defined(_LIBCPP_COMPILER_CLANG_BASED) && __cplusplus < 201103L
 #    error "libc++ only supports C++03 with Clang-based compilers. Please enable C++11"
 #  endif

diff  --git a/libcxx/include/stdatomic.h b/libcxx/include/stdatomic.h
index d9550c44061c5..ff2a5682f5bb6 100644
--- a/libcxx/include/stdatomic.h
+++ b/libcxx/include/stdatomic.h
@@ -121,7 +121,7 @@ using std::atomic_signal_fence                         // see below
 #  pragma GCC system_header
 #endif
 
-#if _LIBCPP_STD_VER > 20
+#if defined(__cplusplus) && _LIBCPP_STD_VER > 20
 
 #include <atomic>
 #include <version>
@@ -230,6 +230,6 @@ using std::atomic_thread_fence _LIBCPP_USING_IF_EXISTS;
 #   include_next <stdatomic.h>
 # endif
 
-#endif // _LIBCPP_STD_VER > 20
+#endif // defined(__cplusplus) && _LIBCPP_STD_VER > 20
 
 #endif // _LIBCPP_STDATOMIC_H

diff  --git a/libcxx/test/libcxx/include_as_c.sh.cpp b/libcxx/test/libcxx/include_as_c.sh.cpp
index a3e5d533040d4..f649c9c54f1be 100644
--- a/libcxx/test/libcxx/include_as_c.sh.cpp
+++ b/libcxx/test/libcxx/include_as_c.sh.cpp
@@ -34,6 +34,7 @@
 #endif
 #include <math.h>
 #include <setjmp.h>
+#include <stdatomic.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>


        


More information about the llvm-branch-commits mailing list