Skip to content

Commit 297ab67

Browse files
committed
add constexpr test
1 parent 2b571c3 commit 297ab67

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

stl/inc/optional

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct _Optional_destruct_base { // either contains a value of _Ty or is empty (
7777

7878
constexpr _Optional_destruct_base() noexcept : _Dummy{}, _Has_value{false} { // initialize an empty optional
7979
#ifdef _INSERT_OPTIONAL_ANNOTATION
80-
if (_Asan_optional_should_annotate && !_STD _Is_constant_evaluated()) {
80+
if (!_STD _Is_constant_evaluated() && _Asan_optional_should_annotate) {
8181
__asan_poison_memory_region(_STD addressof(_Value), sizeof(_Ty));
8282
}
8383
#endif
@@ -101,7 +101,7 @@ struct _Optional_destruct_base { // either contains a value of _Ty or is empty (
101101
_CONSTEXPR20 void reset() noexcept {
102102
_Has_value = false;
103103
#ifdef _INSERT_OPTIONAL_ANNOTATION
104-
if (_Asan_optional_should_annotate && !_STD _Is_constant_evaluated()) {
104+
if (!_STD _Is_constant_evaluated() && _Asan_optional_should_annotate) {
105105
__asan_poison_memory_region(_STD addressof(_Value), sizeof(_Ty));
106106
}
107107
#endif
@@ -121,7 +121,7 @@ struct _Optional_destruct_base<_Ty, false> { // either contains a value of _Ty o
121121
_Value.~_Ty();
122122

123123
#ifdef _INSERT_OPTIONAL_ANNOTATION
124-
if (_Asan_optional_should_annotate && !_STD _Is_constant_evaluated()) {
124+
if (!_STD _Is_constant_evaluated() && _Asan_optional_should_annotate) {
125125
__asan_poison_memory_region(_STD addressof(_Value), sizeof(_Ty));
126126
}
127127
#endif
@@ -137,7 +137,7 @@ struct _Optional_destruct_base<_Ty, false> { // either contains a value of _Ty o
137137

138138
constexpr _Optional_destruct_base() noexcept : _Dummy{}, _Has_value{false} { // initialize an empty optional
139139
#ifdef _INSERT_OPTIONAL_ANNOTATION
140-
if (_Asan_optional_should_annotate && !_STD _Is_constant_evaluated()) {
140+
if (!_STD _Is_constant_evaluated() && _Asan_optional_should_annotate) {
141141
__asan_poison_memory_region(_STD addressof(_Value), sizeof(_Ty));
142142
}
143143
#endif
@@ -165,7 +165,7 @@ struct _Optional_destruct_base<_Ty, false> { // either contains a value of _Ty o
165165
_Value.~_Ty();
166166

167167
#ifdef _INSERT_OPTIONAL_ANNOTATION
168-
if (_Asan_optional_should_annotate && !_STD _Is_constant_evaluated()) {
168+
if (!_STD _Is_constant_evaluated() && _Asan_optional_should_annotate) {
169169
__asan_poison_memory_region(_STD addressof(_Value), sizeof(_Ty));
170170
}
171171
#endif
@@ -186,7 +186,7 @@ struct _Optional_construct_base : _Optional_destruct_base<_Ty> {
186186
_STL_INTERNAL_CHECK(!this->_Has_value);
187187

188188
#ifdef _INSERT_OPTIONAL_ANNOTATION
189-
if (_Asan_optional_should_annotate && !_STD _Is_constant_evaluated()) {
189+
if (!_STD _Is_constant_evaluated() && _Asan_optional_should_annotate) {
190190
__asan_unpoison_memory_region(_STD addressof(this->_Value), sizeof(_Ty));
191191
}
192192
#endif

tests/std/tests/GH_005974_optional_asan/test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,29 @@ void test_repoison_after_reset() {
4444
ASAN_VERIFY_POISONED(reinterpret_cast<Payload*>(&opt));
4545
}
4646

47+
constexpr bool test_constexpr() {
48+
#if _HAS_CXX20
49+
bool res = true;
50+
std::optional<Payload> opt = std::nullopt;
51+
opt = Payload{};
52+
opt.reset();
53+
opt = Payload{86, 0, 0, 0};
54+
res = opt->x == 86;
55+
opt.emplace(42, 0, 0, 0);
56+
res = res && (opt->x == 42);
57+
return res;
58+
#else
59+
std::optional<Payload> opt{Payload{86, 0, 0, 0}};
60+
return opt->x == 86;
61+
#endif
62+
}
63+
4764
int main() {
4865
test_poison_on_empty_access();
4966
test_emplace_unpoisoning();
5067
test_assignment_unpoisoning();
5168
test_repoison_after_reset();
69+
static_assert(test_constexpr(), "constexpr test failed");
5270

5371
return 0;
5472
}

0 commit comments

Comments
 (0)