<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/132630>132630</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Incompatibility between clang's built-in `stdatomic.h` and libstdc++'s `bits/shared_ptr_atomic.h`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          HighCommander4
      </td>
    </tr>
</table>

<pre>
    **Steps to reproduce**

1. Create a file named `test.cpp` with the following contents:
    ```c++
    #include <stdatomic.h>
    #include <memory>
 ````
2. Use `libstdc++` as your standard library. Depending on your system, this might happen by default, or you may need to add `-stdlib=libstdc++` to the `clang` command below (and possibly `--gcc-toolchain` to point it to a specific version of `libstdc++`).
3. Run `clang++ -c test.cpp`

**Expected results**

Compiles successfully.

**Actual results**

```console
In file included from test.cpp:2:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/memory:81:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:34:
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/atomic_base.h:155:25: error: expected ')'
  155 |     kill_dependency(_Tp __y) noexcept
      | ^
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/atomic_base.h:155:5: note: to match this '('
  155 |     kill_dependency(_Tp __y) noexcept
 |     ^
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:77:28: note: expanded from macro 'kill_dependency'
   77 | #define kill_dependency(y) (y)
      |                            ^
In file included from test.cpp:2:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/memory:81:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:34:
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/atomic_base.h:155:29: error: expected ';' after top level declarator
  155 | kill_dependency(_Tp __y) noexcept
      |                             ^
In file included from test.cpp:2:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/memory:81:
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:105:49: error: too many arguments provided to function-like macro invocation
  105 | atomic_is_lock_free(const __shared_ptr<_Tp, _Lp>*)
      | ^
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:90:9: note: macro 'atomic_is_lock_free' defined here
   90 | #define atomic_is_lock_free(obj) __c11_atomic_is_lock_free(sizeof(*(obj)))
      |         ^
In file included from test.cpp:2:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/memory:81:
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:108:7: error: expected expression
  108 |       return __gthread_active_p() == 0;
 | ^
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:112:6: error: expected ';' after top level declarator
  112 |     }
      | ^
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:143:5: error: expected parameter declarator
  143 |     atomic_load(const shared_ptr<_Tp>* __p)
      | ^
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:144:55: note: expanded from macro 'atomic_load'
  144 | #define atomic_load(object) __c11_atomic_load(object, __ATOMIC_SEQ_CST)
      | ^
<built-in>:18:26: note: expanded from macro '__ATOMIC_SEQ_CST'
   18 | #define __ATOMIC_SEQ_CST 5
      | ^
In file included from test.cpp:2:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/memory:81:
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:143:5: error: expected ')'
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:144:55: note: expanded from macro 'atomic_load'
  144 | #define atomic_load(object) __c11_atomic_load(object, __ATOMIC_SEQ_CST)
      | ^
<built-in>:18:26: note: expanded from macro '__ATOMIC_SEQ_CST'
   18 | #define __ATOMIC_SEQ_CST 5
      | ^
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:143:5: note: to match this '('
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:144:46: note: expanded from macro 'atomic_load'
  144 | #define atomic_load(object) __c11_atomic_load(object, __ATOMIC_SEQ_CST)
      | ^
In file included from test.cpp:2:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/memory:81:
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:158:41: error: too many arguments provided to function-like macro invocation
  158 | atomic_load(const __shared_ptr<_Tp, _Lp>* __p)
      | ^
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:144:9: note: macro 'atomic_load' defined here
  144 | #define atomic_load(object) __c11_atomic_load(object, __ATOMIC_SEQ_CST)
      |         ^
In file included from test.cpp:2:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/memory:81:
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:159:7: error: expected expression
  159 |     { return std::atomic_load_explicit(__p, memory_order_seq_cst); }
      | ^
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:159:69: error: expected ';' after top level declarator
  159 |     { return std::atomic_load_explicit(__p, memory_order_seq_cst); }
      | ^
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:184:5: error: expected parameter declarator
  184 | atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
      | ^
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:141:75: note: expanded from macro 'atomic_store'
  141 | #define atomic_store(object, desired) __c11_atomic_store(object, desired, __ATOMIC_SEQ_CST)
      | ^
<built-in>:18:26: note: expanded from macro '__ATOMIC_SEQ_CST'
   18 | #define __ATOMIC_SEQ_CST 5
      |                          ^
In file included from test.cpp:2:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/memory:81:
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:184:5: error: expected ')'
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:141:75: note: expanded from macro 'atomic_store'
  141 | #define atomic_store(object, desired) __c11_atomic_store(object, desired, __ATOMIC_SEQ_CST)
      | ^
<built-in>:18:26: note: expanded from macro '__ATOMIC_SEQ_CST'
   18 | #define __ATOMIC_SEQ_CST 5
      | ^
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:184:5: note: to match this '('
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:141:57: note: expanded from macro 'atomic_store'
  141 | #define atomic_store(object, desired) __c11_atomic_store(object, desired, __ATOMIC_SEQ_CST)
      | ^
In file included from test.cpp:2:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/memory:81:
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:201:47: error: too many arguments provided to function-like macro invocation
  201 | atomic_store(__shared_ptr<_Tp, _Lp>* __p, __shared_ptr<_Tp, _Lp> __r)
 | ^
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:141:9: note: macro 'atomic_store' defined here
  141 | #define atomic_store(object, desired) __c11_atomic_store(object, desired, __ATOMIC_SEQ_CST)
      |         ^
In file included from test.cpp:2:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/memory:81:
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:201:5: error: variable has incomplete type 'void'
  201 | atomic_store(__shared_ptr<_Tp, _Lp>* __p, __shared_ptr<_Tp, _Lp> __r)
 | ^
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:202:34: error: use of undeclared identifier '__p'
  202 |     { std::atomic_store_explicit(__p, std::move(__r), memory_order_seq_cst); }
 | ^
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:202:49: error: use of undeclared identifier '__r'
  202 |     { std::atomic_store_explicit(__p, std::move(__r), memory_order_seq_cst); }
 | ^
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:202:79: error: expected ';' after top level declarator
  202 |     { std::atomic_store_explicit(__p, std::move(__r), memory_order_seq_cst); }
 | ^
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:226:5: error: expected parameter declarator
  226 | atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
      | ^
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:147:81: note: expanded from macro 'atomic_exchange'
  147 | #define atomic_exchange(object, desired) __c11_atomic_exchange(object, desired, __ATOMIC_SEQ_CST)
      | ^
<built-in>:18:26: note: expanded from macro '__ATOMIC_SEQ_CST'
   18 | #define __ATOMIC_SEQ_CST 5
      |                          ^
In file included from test.cpp:2:
In file included from gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/memory:81:
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:226:5: error: expected ')'
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:147:81: note: expanded from macro 'atomic_exchange'
  147 | #define atomic_exchange(object, desired) __c11_atomic_exchange(object, desired, __ATOMIC_SEQ_CST)
      | ^
<built-in>:18:26: note: expanded from macro '__ATOMIC_SEQ_CST'
   18 | #define __ATOMIC_SEQ_CST 5
      | ^
gcc-14.2/lib/gcc/x86_64-pc-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/shared_ptr_atomic.h:226:5: note: to match this '('
/usr/lib/llvm-20/lib/clang/20/include/stdatomic.h:147:63: note: expanded from macro 'atomic_exchange'
  147 | #define atomic_exchange(object, desired) __c11_atomic_exchange(object, desired, __ATOMIC_SEQ_CST)
      | ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
```

This particular output is with clang 20 and gcc 14's libstdc++, but I tested other combinations of recent versions and the result was the same.

I'm not sure how common it is to try to use these two headers together, but it trips up clangd pretty badly because it tries to synthesize a translation unit containing all standard library headers for the purpose of pre-indexing the standard library, and it runs into these errors.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsW19z2joW_zTKyxkYW8YYHnggpMx2Znd2dm_32SPLx1i3suSV5CTcT78j2QQCNE3bNEm7dDymyJJ8_vzOT-coiFkrNgpxQdJrkt5csc7V2iz-Jjb1SjcNUyWayVWhy-2C0CWhyz8cthacBoOt0WXHsW8nkb_iMawMMofAoBISQbEGSyDTyKF1Y962ZBrBnXA1uB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