[libcxx-commits] [libcxx] 05337a7 - [libc++] Rename *SAFE_STATIC to *CONSTINIT, and normalize its uses.

Arthur O'Dwyer via libcxx-commits libcxx-commits at lists.llvm.org
Tue Feb 15 07:41:22 PST 2022


Author: Arthur O'Dwyer
Date: 2022-02-15T10:39:41-05:00
New Revision: 05337a756c6606fdbf2c0ff3a8399da3e60f1591

URL: https://github.com/llvm/llvm-project/commit/05337a756c6606fdbf2c0ff3a8399da3e60f1591
DIFF: https://github.com/llvm/llvm-project/commit/05337a756c6606fdbf2c0ff3a8399da3e60f1591.diff

LOG: [libc++] Rename *SAFE_STATIC to *CONSTINIT, and normalize its uses.

In src/, most files can use `constinit` directly because they're always
compiled with C++20. But some files, like "libcxxabi/src/fallback_malloc.cpp",
can't, because they're `#include`d directly from test cases in libcxxabi/test/
and therefore must (currently) compile as C++03. We might consider refactoring
those offending tests, or at least marking them `UNSUPPORTED: c++03`.

Differential Revision: https://reviews.llvm.org/D119264

Added: 
    

Modified: 
    libcxx/include/__config
    libcxx/src/debug.cpp
    libcxx/src/experimental/memory_resource.cpp
    libcxx/src/experimental/memory_resource_init_helper.h
    libcxx/src/memory.cpp
    libcxx/src/mutex.cpp
    libcxx/src/random_shuffle.cpp
    libcxx/src/support/runtime/exception_fallback.ipp
    libcxx/src/support/runtime/new_handler_fallback.ipp
    libcxx/test/std/experimental/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_always.pass.cpp
    libcxx/test/std/experimental/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_never.pass.cpp
    libcxx/test/std/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_always.pass.cpp
    libcxx/test/std/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_never.pass.cpp
    libcxx/test/std/utilities/any/any.class/any.cons/default.pass.cpp
    libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/default.pass.cpp
    libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/nullptr.pass.cpp
    libcxx/test/support/test_macros.h
    libcxxabi/src/cxa_default_handlers.cpp
    libcxxabi/src/cxa_guard_impl.h
    libcxxabi/src/fallback_malloc.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__config b/libcxx/include/__config
index 979dbb59bf053..acedbdb91f212 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -1239,10 +1239,12 @@ extern "C" _LIBCPP_FUNC_VIS void __sanitizer_annotate_contiguous_container(
 #   define _LIBCPP_THREAD_SAFETY_ANNOTATION(x)
 #endif
 
-#if __has_attribute(require_constant_initialization)
-#  define _LIBCPP_SAFE_STATIC __attribute__((__require_constant_initialization__))
+#if _LIBCPP_STD_VER > 17
+#  define _LIBCPP_CONSTINIT constinit
+#elif __has_attribute(require_constant_initialization)
+#  define _LIBCPP_CONSTINIT __attribute__((__require_constant_initialization__))
 #else
-#  define _LIBCPP_SAFE_STATIC
+#  define _LIBCPP_CONSTINIT
 #endif
 
 #if __has_attribute(diagnose_if) && !defined(_LIBCPP_DISABLE_ADDITIONAL_DIAGNOSTICS)

diff  --git a/libcxx/src/debug.cpp b/libcxx/src/debug.cpp
index ae31c91d154f4..15d8fdb8816ba 100644
--- a/libcxx/src/debug.cpp
+++ b/libcxx/src/debug.cpp
@@ -35,8 +35,7 @@ _LIBCPP_NORETURN void __libcpp_abort_debug_function(__libcpp_debug_info const& i
     std::abort();
 }
 
-_LIBCPP_SAFE_STATIC __libcpp_debug_function_type
-    __libcpp_debug_function = __libcpp_abort_debug_function;
+constinit __libcpp_debug_function_type __libcpp_debug_function = __libcpp_abort_debug_function;
 
 bool __libcpp_set_debug_function(__libcpp_debug_function_type __func) {
   __libcpp_debug_function = __func;

diff  --git a/libcxx/src/experimental/memory_resource.cpp b/libcxx/src/experimental/memory_resource.cpp
index 018d0159281a6..af86079823b69 100644
--- a/libcxx/src/experimental/memory_resource.cpp
+++ b/libcxx/src/experimental/memory_resource.cpp
@@ -97,7 +97,7 @@ static memory_resource *
 __default_memory_resource(bool set = false, memory_resource * new_res = nullptr) noexcept
 {
 #ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
-    _LIBCPP_SAFE_STATIC static atomic<memory_resource*> __res{&res_init.resources.new_delete_res};
+    static constinit atomic<memory_resource*> __res{&res_init.resources.new_delete_res};
     if (set) {
         new_res = new_res ? new_res : new_delete_resource();
         // TODO: Can a weaker ordering be used?
@@ -109,7 +109,7 @@ __default_memory_resource(bool set = false, memory_resource * new_res = nullptr)
             &__res, memory_order_acquire);
     }
 #elif !defined(_LIBCPP_HAS_NO_THREADS)
-    _LIBCPP_SAFE_STATIC static memory_resource * res = &res_init.resources.new_delete_res;
+    static constinit memory_resource *res = &res_init.resources.new_delete_res;
     static mutex res_lock;
     if (set) {
         new_res = new_res ? new_res : new_delete_resource();
@@ -122,7 +122,7 @@ __default_memory_resource(bool set = false, memory_resource * new_res = nullptr)
         return res;
     }
 #else
-    _LIBCPP_SAFE_STATIC static memory_resource* res = &res_init.resources.new_delete_res;
+    static constinit memory_resource *res = &res_init.resources.new_delete_res;
     if (set) {
         new_res = new_res ? new_res : new_delete_resource();
         memory_resource * old_res = res;

diff  --git a/libcxx/src/experimental/memory_resource_init_helper.h b/libcxx/src/experimental/memory_resource_init_helper.h
index 56b9da685878a..032edc12fa278 100644
--- a/libcxx/src/experimental/memory_resource_init_helper.h
+++ b/libcxx/src/experimental/memory_resource_init_helper.h
@@ -1,2 +1,2 @@
 #pragma GCC system_header
-_LIBCPP_SAFE_STATIC ResourceInitHelper res_init _LIBCPP_INIT_PRIORITY_MAX;
+static constinit ResourceInitHelper res_init _LIBCPP_INIT_PRIORITY_MAX;

diff  --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp
index 4c9bf9f073c88..e998e97b65270 100644
--- a/libcxx/src/memory.cpp
+++ b/libcxx/src/memory.cpp
@@ -132,8 +132,8 @@ __shared_weak_count::__get_deleter(const type_info&) const noexcept
 
 #if !defined(_LIBCPP_HAS_NO_THREADS)
 
-_LIBCPP_SAFE_STATIC static const std::size_t __sp_mut_count = 16;
-_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut_back[__sp_mut_count] =
+static constexpr std::size_t __sp_mut_count = 16;
+static constinit __libcpp_mutex_t mut_back[__sp_mut_count] =
 {
     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
@@ -171,8 +171,7 @@ __sp_mut::unlock() noexcept
 __sp_mut&
 __get_sp_mut(const void* p)
 {
-    static __sp_mut muts[__sp_mut_count]
-    {
+    static constinit __sp_mut muts[__sp_mut_count] = {
         &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3],
         &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7],
         &mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11],

diff  --git a/libcxx/src/mutex.cpp b/libcxx/src/mutex.cpp
index d6758fb0ef955..6669e76981682 100644
--- a/libcxx/src/mutex.cpp
+++ b/libcxx/src/mutex.cpp
@@ -196,8 +196,8 @@ recursive_timed_mutex::unlock() noexcept
 // keep in sync with:  7741191.
 
 #ifndef _LIBCPP_HAS_NO_THREADS
-_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
-_LIBCPP_SAFE_STATIC static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
+static constinit __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
+static constinit __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
 #endif
 
 void __call_once(volatile once_flag::_State_type& flag, void* arg,

diff  --git a/libcxx/src/random_shuffle.cpp b/libcxx/src/random_shuffle.cpp
index df9b7d53c8475..753a15f91daa0 100644
--- a/libcxx/src/random_shuffle.cpp
+++ b/libcxx/src/random_shuffle.cpp
@@ -18,7 +18,7 @@
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 #ifndef _LIBCPP_HAS_NO_THREADS
-_LIBCPP_SAFE_STATIC static __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER;
+static constinit __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER;
 #endif
 unsigned __rs_default::__c_ = 0;
 

diff  --git a/libcxx/src/support/runtime/exception_fallback.ipp b/libcxx/src/support/runtime/exception_fallback.ipp
index 67ebde3061ad8..ade933567f236 100644
--- a/libcxx/src/support/runtime/exception_fallback.ipp
+++ b/libcxx/src/support/runtime/exception_fallback.ipp
@@ -11,9 +11,8 @@
 
 namespace std {
 
-_LIBCPP_SAFE_STATIC static std::terminate_handler  __terminate_handler;
-_LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler;
-
+static constinit std::terminate_handler  __terminate_handler = nullptr;
+static constinit std::unexpected_handler __unexpected_handler = nullptr;
 
 // libcxxrt provides implementations of these functions itself.
 unexpected_handler
@@ -26,7 +25,6 @@ unexpected_handler
 get_unexpected() noexcept
 {
   return __libcpp_atomic_load(&__unexpected_handler);
-
 }
 
 _LIBCPP_NORETURN

diff  --git a/libcxx/src/support/runtime/new_handler_fallback.ipp b/libcxx/src/support/runtime/new_handler_fallback.ipp
index a969b2a45d5bb..2ec408327d64d 100644
--- a/libcxx/src/support/runtime/new_handler_fallback.ipp
+++ b/libcxx/src/support/runtime/new_handler_fallback.ipp
@@ -9,7 +9,7 @@
 
 namespace std {
 
-_LIBCPP_SAFE_STATIC static std::new_handler __new_handler;
+static constinit std::new_handler __new_handler = nullptr;
 
 new_handler
 set_new_handler(new_handler handler) noexcept

diff  --git a/libcxx/test/std/experimental/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_always.pass.cpp b/libcxx/test/std/experimental/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_always.pass.cpp
index 6e7af82f31758..7e7eb610eea96 100644
--- a/libcxx/test/std/experimental/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_always.pass.cpp
+++ b/libcxx/test/std/experimental/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_always.pass.cpp
@@ -18,13 +18,10 @@ namespace coro = std::experimental;
 
 using SuspendT = std::experimental::coroutines_v1::suspend_always;
 
-TEST_SAFE_STATIC SuspendT safe_sa;
-constexpr SuspendT constexpr_sa;
-
 constexpr bool check_suspend_constexpr() {
-  SuspendT s{};
-  const SuspendT scopy(s); ((void)scopy);
-  SuspendT smove(std::move(s)); ((void)smove);
+  SuspendT s;
+  const SuspendT scopy(s); (void)scopy;
+  SuspendT smove(std::move(s)); (void)smove;
   s = scopy;
   s = std::move(smove);
   return true;
@@ -64,10 +61,6 @@ int main(int, char**)
     static_assert(std::is_trivially_copyable<S>::value, "");
     static_assert(check_suspend_constexpr(), "");
   }
-  {
-    // suppress unused warnings for the global constexpr test variable
-    ((void)constexpr_sa);
-  }
 
   return 0;
 }

diff  --git a/libcxx/test/std/experimental/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_never.pass.cpp b/libcxx/test/std/experimental/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_never.pass.cpp
index 8a31a31ad16f2..2acad5ed2523e 100644
--- a/libcxx/test/std/experimental/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_never.pass.cpp
+++ b/libcxx/test/std/experimental/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_never.pass.cpp
@@ -16,16 +16,12 @@
 
 namespace coro = std::experimental;
 
-// Test that the type is in the correct namespace
 using SuspendT = std::experimental::coroutines_v1::suspend_never;
 
-TEST_SAFE_STATIC SuspendT safe_sn;
-constexpr SuspendT constexpr_sn;
-
 constexpr bool check_suspend_constexpr() {
-  SuspendT s{};
-  const SuspendT scopy(s); ((void)scopy);
-  SuspendT smove(std::move(s)); ((void)smove);
+  SuspendT s;
+  const SuspendT scopy(s); (void)scopy;
+  SuspendT smove(std::move(s)); (void)smove;
   s = scopy;
   s = std::move(smove);
   return true;
@@ -66,10 +62,6 @@ int main(int, char**)
     static_assert(std::is_trivially_copyable<S>::value, "");
     static_assert(check_suspend_constexpr(), "");
   }
-  {
-    // suppress unused warnings for the global constexpr test variable
-    ((void)constexpr_sn);
-  }
 
   return 0;
 }

diff  --git a/libcxx/test/std/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_always.pass.cpp b/libcxx/test/std/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_always.pass.cpp
index 31b7e0ec7f79a..a3a65c3123234 100644
--- a/libcxx/test/std/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_always.pass.cpp
+++ b/libcxx/test/std/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_always.pass.cpp
@@ -16,8 +16,6 @@
 
 #include "test_macros.h"
 
-TEST_SAFE_STATIC std::suspend_always safe_sa;
-
 constexpr bool check_suspend_constexpr() {
   std::suspend_always s{};
   const std::suspend_always scopy(s);

diff  --git a/libcxx/test/std/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_never.pass.cpp b/libcxx/test/std/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_never.pass.cpp
index 83439299572f0..2b838b9736727 100644
--- a/libcxx/test/std/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_never.pass.cpp
+++ b/libcxx/test/std/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_never.pass.cpp
@@ -16,11 +16,6 @@
 
 #include "test_macros.h"
 
-// Test that the type 'std::suspend_never' is in the correct namespace
-
-TEST_SAFE_STATIC std::suspend_never safe_sn;
-constexpr std::suspend_never constexpr_sn;
-
 constexpr bool check_suspend_constexpr() {
   std::suspend_never s{};
   const std::suspend_never scopy(s); ((void)scopy);
@@ -76,10 +71,6 @@ int main(int, char**)
   {
     static_assert(test_trivial_awaitable_constexpr<std::suspend_never>(true));
   }
-  {
-    // suppress unused warnings for the global constexpr test variable
-    ((void)constexpr_sn);
-  }
 
   return 0;
 }

diff  --git a/libcxx/test/std/utilities/any/any.class/any.cons/default.pass.cpp b/libcxx/test/std/utilities/any/any.class/any.cons/default.pass.cpp
index 2c2715c6444c0..b5efe9e8b75b9 100644
--- a/libcxx/test/std/utilities/any/any.class/any.cons/default.pass.cpp
+++ b/libcxx/test/std/utilities/any/any.class/any.cons/default.pass.cpp
@@ -33,8 +33,8 @@ int main(int, char**)
         struct TestConstexpr : public std::any {
           constexpr TestConstexpr() : std::any() {}
         };
-        TEST_SAFE_STATIC static std::any a;
-        ((void)a);
+        static TEST_CONSTINIT std::any a;
+        (void)a;
     }
     {
         DisableAllocationGuard g; ((void)g);

diff  --git a/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/default.pass.cpp b/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/default.pass.cpp
index 3919d67ac4445..bfe9a632c2f42 100644
--- a/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/default.pass.cpp
+++ b/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/default.pass.cpp
@@ -31,8 +31,8 @@
 #include "unique_ptr_test_helper.h"
 
 #if TEST_STD_VER >= 11
-TEST_SAFE_STATIC std::unique_ptr<int> global_static_unique_ptr_single;
-TEST_SAFE_STATIC std::unique_ptr<int[]> global_static_unique_ptr_runtime;
+TEST_CONSTINIT std::unique_ptr<int> global_static_unique_ptr_single;
+TEST_CONSTINIT std::unique_ptr<int[]> global_static_unique_ptr_runtime;
 
 struct NonDefaultDeleter {
   NonDefaultDeleter() = delete;

diff  --git a/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/nullptr.pass.cpp b/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/nullptr.pass.cpp
index 82d1cb28892bf..95393b677d61c 100644
--- a/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/nullptr.pass.cpp
+++ b/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/nullptr.pass.cpp
@@ -20,8 +20,8 @@
 
 
 #if TEST_STD_VER >= 11
-TEST_SAFE_STATIC std::unique_ptr<int> global_static_unique_ptr_single(nullptr);
-TEST_SAFE_STATIC std::unique_ptr<int[]> global_static_unique_ptr_runtime(nullptr);
+TEST_CONSTINIT std::unique_ptr<int> global_static_unique_ptr_single(nullptr);
+TEST_CONSTINIT std::unique_ptr<int[]> global_static_unique_ptr_runtime(nullptr);
 
 struct NonDefaultDeleter {
   NonDefaultDeleter() = delete;

diff  --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h
index 864216eeca269..272231da326be 100644
--- a/libcxx/test/support/test_macros.h
+++ b/libcxx/test/support/test_macros.h
@@ -206,10 +206,12 @@
 #define TEST_HAS_NO_ALIGNED_ALLOCATION
 #endif
 
-#if defined(_LIBCPP_SAFE_STATIC)
-#define TEST_SAFE_STATIC _LIBCPP_SAFE_STATIC
+#if TEST_STD_VER > 17
+#define TEST_CONSTINIT constinit
+#elif defined(_LIBCPP_CONSTINIT)
+#define TEST_CONSTINIT _LIBCPP_CONSTINIT
 #else
-#define TEST_SAFE_STATIC
+#define TEST_CONSTINIT
 #endif
 
 #if !defined(__cpp_impl_three_way_comparison) \

diff  --git a/libcxxabi/src/cxa_default_handlers.cpp b/libcxxabi/src/cxa_default_handlers.cpp
index e0ccbe119585e..b6ce05853c50c 100644
--- a/libcxxabi/src/cxa_default_handlers.cpp
+++ b/libcxxabi/src/cxa_default_handlers.cpp
@@ -20,8 +20,7 @@
 
 #if !defined(LIBCXXABI_SILENT_TERMINATE)
 
-_LIBCPP_SAFE_STATIC
-static const char* cause = "uncaught";
+static constinit const char* cause = "uncaught";
 
 __attribute__((noreturn))
 static void demangling_terminate_handler()
@@ -100,13 +99,13 @@ static constexpr std::terminate_handler default_unexpected_handler = std::termin
 // Global variables that hold the pointers to the current handler
 //
 _LIBCXXABI_DATA_VIS
-_LIBCPP_SAFE_STATIC std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
+constinit std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
 
 _LIBCXXABI_DATA_VIS
-_LIBCPP_SAFE_STATIC std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
+constinit std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
 
 _LIBCXXABI_DATA_VIS
-_LIBCPP_SAFE_STATIC std::new_handler __cxa_new_handler = 0;
+constinit std::new_handler __cxa_new_handler = nullptr;
 
 namespace std
 {

diff  --git a/libcxxabi/src/cxa_guard_impl.h b/libcxxabi/src/cxa_guard_impl.h
index 5a7cbfd5cdb9d..72940cc7e869a 100644
--- a/libcxxabi/src/cxa_guard_impl.h
+++ b/libcxxabi/src/cxa_guard_impl.h
@@ -619,7 +619,7 @@ struct GlobalStatic {
   static T instance;
 };
 template <class T>
-_LIBCPP_SAFE_STATIC T GlobalStatic<T>::instance = {};
+_LIBCPP_CONSTINIT T GlobalStatic<T>::instance = {};
 
 enum class Implementation { NoThreads, GlobalMutex, Futex };
 

diff  --git a/libcxxabi/src/fallback_malloc.cpp b/libcxxabi/src/fallback_malloc.cpp
index 7e356d9fe47ba..1d6c3808b2174 100644
--- a/libcxxabi/src/fallback_malloc.cpp
+++ b/libcxxabi/src/fallback_malloc.cpp
@@ -33,10 +33,9 @@ namespace {
 
 // When POSIX threads are not available, make the mutex operations a nop
 #ifndef _LIBCXXABI_HAS_NO_THREADS
-_LIBCPP_SAFE_STATIC
-static std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER;
+static _LIBCPP_CONSTINIT std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER;
 #else
-static void* heap_mutex = 0;
+static _LIBCPP_CONSTINIT void* heap_mutex = 0;
 #endif
 
 class mutexor {


        


More information about the libcxx-commits mailing list