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

Volodymyr Sapsai via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 24 15:00:58 PDT 2018


vsapsai updated this revision to Diff 143817.
vsapsai added a comment.

- Tighten up a test with expected-error per review comment.


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,36 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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>
+// expected-error at atomic:* {{<stdatomic.h> is incompatible with the C++ standard library}}
+
+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.143817.patch
Type: text/x-patch
Size: 2784 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180424/9b684d40/attachment.bin>


More information about the cfe-commits mailing list