[PATCH] D45470: Emit an error when mixing <stdatomic.h> and <atomic>

Volodymyr Sapsai via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 9 18:14:03 PDT 2018


vsapsai created this revision.
vsapsai added reviewers: rsmith, EricWF, mclow.lists.
Herald added subscribers: christof, jkorous-apple.

Atomics in C and C++ are incompatible at the moment and mixing the
headers can result in confusing error messages.

Emit an error explicitly telling about the incompatibility. Introduce
the macro `__ALLOW_STDC_ATOMICS_IN_CXX__` that allows to choose in C++
between C atomics and C++ atomics.

rdar://problem/27435938


https://reviews.llvm.org/D45470

Files:
  clang/lib/Headers/stdatomic.h
  clang/test/Headers/stdatomic.cpp
  libcxx/include/atomic
  libcxx/test/libcxx/atomics/c_compatibility.fail.cpp


Index: libcxx/test/libcxx/atomics/c_compatibility.fail.cpp
===================================================================
--- /dev/null
+++ libcxx/test/libcxx/atomics/c_compatibility.fail.cpp
@@ -0,0 +1,35 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// UNSUPPORTED: libcpp-has-no-threads
+//
+// <atomic>
+
+// Test that including <atomic> fails to compile when we want to use C atomics
+// in C++ and have corresponding macro defined.
+
+// XFAIL: with_system_cxx_lib=macosx10.13
+// XFAIL: with_system_cxx_lib=macosx10.12
+// XFAIL: with_system_cxx_lib=macosx10.11
+// XFAIL: with_system_cxx_lib=macosx10.10
+// XFAIL: with_system_cxx_lib=macosx10.9
+// XFAIL: with_system_cxx_lib=macosx10.8
+// XFAIL: with_system_cxx_lib=macosx10.7
+
+// MODULES_DEFINES: __ALLOW_STDC_ATOMICS_IN_CXX__
+#ifndef __ALLOW_STDC_ATOMICS_IN_CXX__
+#define __ALLOW_STDC_ATOMICS_IN_CXX__
+#endif
+
+#include <atomic>
+
+int main()
+{
+}
+
Index: libcxx/include/atomic
===================================================================
--- libcxx/include/atomic
+++ libcxx/include/atomic
@@ -555,6 +555,9 @@
 #if !defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_GCC_ATOMIC_IMP)
 #error <atomic> is not implemented
 #endif
+#ifdef __ALLOW_STDC_ATOMICS_IN_CXX__
+#error <stdatomic.h> is incompatible with the C++ standard library
+#endif
 
 #if _LIBCPP_STD_VER > 14
 # define __cpp_lib_atomic_is_always_lock_free 201603L
Index: clang/test/Headers/stdatomic.cpp
===================================================================
--- /dev/null
+++ clang/test/Headers/stdatomic.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 %s -verify
+// RUN: %clang_cc1 -D__ALLOW_STDC_ATOMICS_IN_CXX__ %s -verify
+
+#include <stdatomic.h>
+
+#ifndef __ALLOW_STDC_ATOMICS_IN_CXX__
+// expected-error at stdatomic.h:* {{<stdatomic.h> is incompatible with the C++ standard library}}
+#else
+// expected-no-diagnostics
+#endif
Index: clang/lib/Headers/stdatomic.h
===================================================================
--- clang/lib/Headers/stdatomic.h
+++ clang/lib/Headers/stdatomic.h
@@ -31,6 +31,10 @@
 # include_next <stdatomic.h>
 #else
 
+#if !defined(__ALLOW_STDC_ATOMICS_IN_CXX__) && defined(__cplusplus)
+#error "<stdatomic.h> is incompatible with the C++ standard library; define __ALLOW_STDC_ATOMICS_IN_CXX__ to proceed."
+#endif
+
 #include <stddef.h>
 #include <stdint.h>
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45470.141775.patch
Type: text/x-patch
Size: 2692 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180410/29f79b4e/attachment.bin>


More information about the cfe-commits mailing list