[libcxx] Make stdatomic.h work when included from a C source file
authorGergely Nagy <ngg@ngg.hu>
Tue, 27 Sep 2022 11:59:55 +0000 (07:59 -0400)
committerLouis Dionne <ldionne.2@gmail.com>
Tue, 27 Sep 2022 17:13:11 +0000 (13:13 -0400)
commitafec0f0ec38a72bcc6a697c1cefb1dac0bbd02fb
treec2329a868edd5c2ab5091b337b7fc25c4505c2c3
parent4c6683cd68219d02fcd4c295924995db6b77516d
[libcxx] Make stdatomic.h work when included from a C source file

If a C source file includes the libc++ stdatomic.h, compilation will
break because (a) the C++ standard check will fail (which is expected),
and (b) `_LIBCPP_COMPILER_CLANG_BASED` won't be defined because the
logic defining it in `__config` is guarded by a `__cplusplus` check, so
we'll end up with a blank header. Move the detection logic outside of
the `__cplusplus` check to make the second check pass even in a C context
when you're using Clang. Note that `_LIBCPP_STD_VER` is not defined when
in C mode, hence stdatomic.h needs to check if in C++ mode before using
that macro to avoid a warning.

In an ideal world, a C source file wouldn't be including the libc++
header directory in its search path, so we'd never have this issue.
Unfortunately, certain build environments make this hard to guarantee,
and in this case it's easy to tweak this header to make it work in a C
context, so I'm hoping this is acceptable.

Fixes https://github.com/llvm/llvm-project/issues/57710.

Differential Revision: https://reviews.llvm.org/D134591
libcxx/include/__config
libcxx/include/stdatomic.h
libcxx/test/libcxx/include_as_c.sh.cpp