[libcxx-commits] [libcxx] [libc++][Android] Always redirect <stdatomic.h> to <atomic> (PR #143036)
Ryan Prichard via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jun 5 14:19:26 PDT 2025
https://github.com/rprichard created https://github.com/llvm/llvm-project/pull/143036
When targeting Android, enable the <stdatomic.h> to <atomic> redirection for C++ language dialects before C++23, because this redirection historically existed for Android's C++ toolchain. Currently, the Android toolchain achieves this redirection with a stdatomic.h in the sysroot (from Bionic) that defines the C API either directly or by aliasing std identifiers from <atomic>. The Android team's LLVM build scripts then copy Bionic's header over the one in the Clang resource directory.
Hopefully, by moving Android's redirection into LLVM itself, the Android stdatomic.h situation can be simplified eventually.
>From 1e0457b899e7d12ee80b53343fcc5964b7eb6669 Mon Sep 17 00:00:00 2001
From: Ryan Prichard <rprichard at google.com>
Date: Tue, 3 Jun 2025 17:18:53 -0700
Subject: [PATCH] [libc++][Android] Always redirect <stdatomic.h> to <atomic>
When targeting Android, enable the <stdatomic.h> to <atomic>
redirection for C++ language dialects before C++23, because this
redirection historically existed for Android's C++ toolchain.
Currently, the Android toolchain achieves this redirection with a
stdatomic.h in the sysroot (from Bionic) that defines the C API either
directly or by aliasing std identifiers from <atomic>. The Android
team's LLVM build scripts then copy Bionic's header over the one in the
Clang resource directory.
Hopefully, by moving Android's redirection into LLVM itself, the
Android stdatomic.h situation can be simplified eventually.
---
libcxx/include/atomic | 5 ++++-
libcxx/include/stdatomic.h | 5 ++++-
.../atomics.syn/compatible_with_stdatomic.compile.pass.cpp | 5 ++++-
.../atomics.syn/incompatible_with_stdatomic.verify.cpp | 3 +++
.../dont_hijack_header.cxx23.compile.pass.cpp | 3 +++
5 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/libcxx/include/atomic b/libcxx/include/atomic
index 75af5de33ca4c..fba7040f6900c 100644
--- a/libcxx/include/atomic
+++ b/libcxx/include/atomic
@@ -598,7 +598,10 @@ template <class T>
# define _LIBCPP_STDATOMIC_H_HAS_DEFINITELY_BEEN_INCLUDED 0
# endif
-# if _LIBCPP_STD_VER < 23 && _LIBCPP_STDATOMIC_H_HAS_DEFINITELY_BEEN_INCLUDED
+// The Android LLVM toolchain has historically allowed combining the <atomic>
+// and <stdatomic.h> headers in dialects before C++23, so for backwards
+// compatibility, preserve that ability when targeting Android.
+# if _LIBCPP_STD_VER < 23 && !defined(__ANDROID__) && _LIBCPP_STDATOMIC_H_HAS_DEFINITELY_BEEN_INCLUDED
# error <atomic> is incompatible with <stdatomic.h> before C++23. Please compile with -std=c++23.
# endif
diff --git a/libcxx/include/stdatomic.h b/libcxx/include/stdatomic.h
index 2991030eee456..dc1a955b69cb6 100644
--- a/libcxx/include/stdatomic.h
+++ b/libcxx/include/stdatomic.h
@@ -126,7 +126,10 @@ using std::atomic_signal_fence // see below
# pragma GCC system_header
# endif
-# if defined(__cplusplus) && _LIBCPP_STD_VER >= 23
+// The Android LLVM toolchain has historically allowed combining the <atomic>
+// and <stdatomic.h> headers in dialects before C++23, so for backwards
+// compatibility, preserve that ability when targeting Android.
+# if defined(__cplusplus) && (_LIBCPP_STD_VER >= 23 || defined(__ANDROID__))
# include <atomic>
# include <version>
diff --git a/libcxx/test/libcxx/atomics/atomics.syn/compatible_with_stdatomic.compile.pass.cpp b/libcxx/test/libcxx/atomics/atomics.syn/compatible_with_stdatomic.compile.pass.cpp
index 349dc51aaa0e6..c997e4b792459 100644
--- a/libcxx/test/libcxx/atomics/atomics.syn/compatible_with_stdatomic.compile.pass.cpp
+++ b/libcxx/test/libcxx/atomics/atomics.syn/compatible_with_stdatomic.compile.pass.cpp
@@ -7,7 +7,10 @@
//===----------------------------------------------------------------------===//
// UNSUPPORTED: no-threads
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// On Android, libc++'s <stdatomic.h> header always redirects to <atomic>, even before C++23.
+// UNSUPPORTED: c++03
+// UNSUPPORTED: (c++11 || c++14 || c++17 || c++20) && !android
// This test verifies that <stdatomic.h> redirects to <atomic>.
diff --git a/libcxx/test/libcxx/atomics/atomics.syn/incompatible_with_stdatomic.verify.cpp b/libcxx/test/libcxx/atomics/atomics.syn/incompatible_with_stdatomic.verify.cpp
index a788ea32dddc8..7d14fa308bf06 100644
--- a/libcxx/test/libcxx/atomics/atomics.syn/incompatible_with_stdatomic.verify.cpp
+++ b/libcxx/test/libcxx/atomics/atomics.syn/incompatible_with_stdatomic.verify.cpp
@@ -9,6 +9,9 @@
// UNSUPPORTED: no-threads
// REQUIRES: c++03 || c++11 || c++14 || c++17 || c++20
+// On Android, libc++'s <stdatomic.h> header always redirects to <atomic>, even before C++23.
+// XFAIL: android
+
// No diagnostic gets emitted when we build with modules.
// XFAIL: clang-modules-build
diff --git a/libcxx/test/libcxx/atomics/stdatomic.h.syn/dont_hijack_header.cxx23.compile.pass.cpp b/libcxx/test/libcxx/atomics/stdatomic.h.syn/dont_hijack_header.cxx23.compile.pass.cpp
index a8a99e6937f31..5c966c6bca6e2 100644
--- a/libcxx/test/libcxx/atomics/stdatomic.h.syn/dont_hijack_header.cxx23.compile.pass.cpp
+++ b/libcxx/test/libcxx/atomics/stdatomic.h.syn/dont_hijack_header.cxx23.compile.pass.cpp
@@ -21,6 +21,9 @@
// doesn't work at all if we don't use the <stdatomic.h> provided by libc++ in C++23 and above.
// XFAIL: (c++11 || c++14 || c++17 || c++20) && gcc
+// On Android, libc++'s <stdatomic.h> header always redirects to <atomic>, even before C++23.
+// XFAIL: android
+
#include <atomic>
#include <stdatomic.h>
#include <type_traits>
More information about the libcxx-commits
mailing list