Index: clang/lib/Headers/stdatomic.h =================================================================== --- clang/lib/Headers/stdatomic.h +++ clang/lib/Headers/stdatomic.h @@ -31,6 +31,10 @@ # include_next #else +#if !defined(__ALLOW_STDC_ATOMICS_IN_CXX__) && defined(__cplusplus) +#error " is incompatible with the C++ standard library; define __ALLOW_STDC_ATOMICS_IN_CXX__ to proceed." +#endif + #include #include 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 + +#ifndef __ALLOW_STDC_ATOMICS_IN_CXX__ +// expected-error@stdatomic.h:* {{ is incompatible with the C++ standard library}} +#else +// expected-no-diagnostics +#endif 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 is not implemented #endif +#ifdef __ALLOW_STDC_ATOMICS_IN_CXX__ +#error is incompatible with the C++ standard library +#endif #if _LIBCPP_STD_VER > 14 # define __cpp_lib_atomic_is_always_lock_free 201603L 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 +// +// + +// Test that including 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 + +int main() +{ +} +