Skip to content

Commit

Permalink
Use <stdatomic.h> with MSVC and C++
Browse files Browse the repository at this point in the history
and use fallback only for C.

It fixes the isssue with clang-cl:

```
#include <stdatomic.h>
#include <stdbool.h>
#ifdef __cplusplus
#include <atomic>
using namespace std;
#endif

int main() {
    atomic_bool b = true;
}
```

```
$ clang-cl /TC main.cpp
# works
```
```
$ clang-cl /TP /std:c++20 main.cpp

stdatomic.h(70,6): error: conflicting types for 'atomic_thread_fence'
void atomic_thread_fence(memory_order);
     ^
atomic(166,24): note: previous definition is here
extern "C" inline void atomic_thread_fence(const memory_order _Order) noexcept {

...

fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
```
Many errors but
`<stdatomic.h>` has many macros to built-in functions.
```
#define atomic_thread_fence(order) __c11_atomic_thread_fence(order)
```
and MSVC `<atomic>` has real functions.
and the built-in functions are redefined.

Reviewed By: #libc, aaron.ballman, Mordante

Differential Revision: https://reviews.llvm.org/D130419
  • Loading branch information
fsb4000 authored and mordante committed Jul 25, 2022
1 parent 8068751 commit ba49d39
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ Bug Fixes
used in comparison operators. Fixes `Issue 56560 <https://github.com/llvm/llvm-project/issues/56560>`_.
- Fix that ``if consteval`` could evaluate to ``true`` at runtime because it was incorrectly
constant folded. Fixes `Issue 55638 <https://github.com/llvm/llvm-project/issues/55638>`_.
- Fixed incompatibility of Clang's ``<stdatomic.h>`` with MSVC ``<atomic>``.
Fixes `MSVC STL Issue 2862 <https://github.com/microsoft/STL/issues/2862>`_.

Improvements to Clang's diagnostics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Headers/stdatomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* explicitly disallows `stdatomic.h` in the C mode via an `#error`. Fallback
* to the clang resource header until that is fully supported.
*/
#if __STDC_HOSTED__ && __has_include_next(<stdatomic.h>) && !defined(_MSC_VER)
#if __STDC_HOSTED__ && \
__has_include_next(<stdatomic.h>) && !(defined(_MSC_VER) && !defined(__cplusplus))
# include_next <stdatomic.h>
#else

Expand Down

0 comments on commit ba49d39

Please sign in to comment.