Skip to content

Commit 05337a7

Browse files
committed
[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
1 parent db33373 commit 05337a7

File tree

20 files changed

+43
-71
lines changed

20 files changed

+43
-71
lines changed

libcxx/include/__config

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,10 +1239,12 @@ extern "C" _LIBCPP_FUNC_VIS void __sanitizer_annotate_contiguous_container(
12391239
# define _LIBCPP_THREAD_SAFETY_ANNOTATION(x)
12401240
#endif
12411241

1242-
#if __has_attribute(require_constant_initialization)
1243-
# define _LIBCPP_SAFE_STATIC __attribute__((__require_constant_initialization__))
1242+
#if _LIBCPP_STD_VER > 17
1243+
# define _LIBCPP_CONSTINIT constinit
1244+
#elif __has_attribute(require_constant_initialization)
1245+
# define _LIBCPP_CONSTINIT __attribute__((__require_constant_initialization__))
12441246
#else
1245-
# define _LIBCPP_SAFE_STATIC
1247+
# define _LIBCPP_CONSTINIT
12461248
#endif
12471249

12481250
#if __has_attribute(diagnose_if) && !defined(_LIBCPP_DISABLE_ADDITIONAL_DIAGNOSTICS)

libcxx/src/debug.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ _LIBCPP_NORETURN void __libcpp_abort_debug_function(__libcpp_debug_info const& i
3535
std::abort();
3636
}
3737

38-
_LIBCPP_SAFE_STATIC __libcpp_debug_function_type
39-
__libcpp_debug_function = __libcpp_abort_debug_function;
38+
constinit __libcpp_debug_function_type __libcpp_debug_function = __libcpp_abort_debug_function;
4039

4140
bool __libcpp_set_debug_function(__libcpp_debug_function_type __func) {
4241
__libcpp_debug_function = __func;

libcxx/src/experimental/memory_resource.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static memory_resource *
9797
__default_memory_resource(bool set = false, memory_resource * new_res = nullptr) noexcept
9898
{
9999
#ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
100-
_LIBCPP_SAFE_STATIC static atomic<memory_resource*> __res{&res_init.resources.new_delete_res};
100+
static constinit atomic<memory_resource*> __res{&res_init.resources.new_delete_res};
101101
if (set) {
102102
new_res = new_res ? new_res : new_delete_resource();
103103
// TODO: Can a weaker ordering be used?
@@ -109,7 +109,7 @@ __default_memory_resource(bool set = false, memory_resource * new_res = nullptr)
109109
&__res, memory_order_acquire);
110110
}
111111
#elif !defined(_LIBCPP_HAS_NO_THREADS)
112-
_LIBCPP_SAFE_STATIC static memory_resource * res = &res_init.resources.new_delete_res;
112+
static constinit memory_resource *res = &res_init.resources.new_delete_res;
113113
static mutex res_lock;
114114
if (set) {
115115
new_res = new_res ? new_res : new_delete_resource();
@@ -122,7 +122,7 @@ __default_memory_resource(bool set = false, memory_resource * new_res = nullptr)
122122
return res;
123123
}
124124
#else
125-
_LIBCPP_SAFE_STATIC static memory_resource* res = &res_init.resources.new_delete_res;
125+
static constinit memory_resource *res = &res_init.resources.new_delete_res;
126126
if (set) {
127127
new_res = new_res ? new_res : new_delete_resource();
128128
memory_resource * old_res = res;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#pragma GCC system_header
2-
_LIBCPP_SAFE_STATIC ResourceInitHelper res_init _LIBCPP_INIT_PRIORITY_MAX;
2+
static constinit ResourceInitHelper res_init _LIBCPP_INIT_PRIORITY_MAX;

libcxx/src/memory.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ __shared_weak_count::__get_deleter(const type_info&) const noexcept
132132

133133
#if !defined(_LIBCPP_HAS_NO_THREADS)
134134

135-
_LIBCPP_SAFE_STATIC static const std::size_t __sp_mut_count = 16;
136-
_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut_back[__sp_mut_count] =
135+
static constexpr std::size_t __sp_mut_count = 16;
136+
static constinit __libcpp_mutex_t mut_back[__sp_mut_count] =
137137
{
138138
_LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
139139
_LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
@@ -171,8 +171,7 @@ __sp_mut::unlock() noexcept
171171
__sp_mut&
172172
__get_sp_mut(const void* p)
173173
{
174-
static __sp_mut muts[__sp_mut_count]
175-
{
174+
static constinit __sp_mut muts[__sp_mut_count] = {
176175
&mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3],
177176
&mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7],
178177
&mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11],

libcxx/src/mutex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ recursive_timed_mutex::unlock() noexcept
196196
// keep in sync with: 7741191.
197197

198198
#ifndef _LIBCPP_HAS_NO_THREADS
199-
_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
200-
_LIBCPP_SAFE_STATIC static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
199+
static constinit __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
200+
static constinit __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
201201
#endif
202202

203203
void __call_once(volatile once_flag::_State_type& flag, void* arg,

libcxx/src/random_shuffle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
_LIBCPP_BEGIN_NAMESPACE_STD
1919

2020
#ifndef _LIBCPP_HAS_NO_THREADS
21-
_LIBCPP_SAFE_STATIC static __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER;
21+
static constinit __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER;
2222
#endif
2323
unsigned __rs_default::__c_ = 0;
2424

libcxx/src/support/runtime/exception_fallback.ipp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111

1212
namespace std {
1313

14-
_LIBCPP_SAFE_STATIC static std::terminate_handler __terminate_handler;
15-
_LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler;
16-
14+
static constinit std::terminate_handler __terminate_handler = nullptr;
15+
static constinit std::unexpected_handler __unexpected_handler = nullptr;
1716

1817
// libcxxrt provides implementations of these functions itself.
1918
unexpected_handler
@@ -26,7 +25,6 @@ unexpected_handler
2625
get_unexpected() noexcept
2726
{
2827
return __libcpp_atomic_load(&__unexpected_handler);
29-
3028
}
3129

3230
_LIBCPP_NORETURN

libcxx/src/support/runtime/new_handler_fallback.ipp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace std {
1111

12-
_LIBCPP_SAFE_STATIC static std::new_handler __new_handler;
12+
static constinit std::new_handler __new_handler = nullptr;
1313

1414
new_handler
1515
set_new_handler(new_handler handler) noexcept

libcxx/test/std/experimental/language.support/support.coroutines/coroutine.trivial.awaitables/suspend_always.pass.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ namespace coro = std::experimental;
1818

1919
using SuspendT = std::experimental::coroutines_v1::suspend_always;
2020

21-
TEST_SAFE_STATIC SuspendT safe_sa;
22-
constexpr SuspendT constexpr_sa;
23-
2421
constexpr bool check_suspend_constexpr() {
25-
SuspendT s{};
26-
const SuspendT scopy(s); ((void)scopy);
27-
SuspendT smove(std::move(s)); ((void)smove);
22+
SuspendT s;
23+
const SuspendT scopy(s); (void)scopy;
24+
SuspendT smove(std::move(s)); (void)smove;
2825
s = scopy;
2926
s = std::move(smove);
3027
return true;
@@ -64,10 +61,6 @@ int main(int, char**)
6461
static_assert(std::is_trivially_copyable<S>::value, "");
6562
static_assert(check_suspend_constexpr(), "");
6663
}
67-
{
68-
// suppress unused warnings for the global constexpr test variable
69-
((void)constexpr_sa);
70-
}
7164

7265
return 0;
7366
}

0 commit comments

Comments
 (0)