Skip to content

Commit cc10778

Browse files
committed
Add a test for qualifiers out of a sense of paranoia
1 parent 63cd2d6 commit cc10778

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s -DNO_TADD -std=c++17 -fexperimental-cxx-type-aware-allocators
2+
3+
namespace std {
4+
template <class T> struct type_identity {
5+
typedef T type;
6+
};
7+
enum class align_val_t : __SIZE_TYPE__ {};
8+
struct destroying_delete_t { explicit destroying_delete_t() = default; };
9+
}
10+
11+
using size_t = __SIZE_TYPE__;
12+
13+
14+
template <class Tp> struct is_const {
15+
static const bool value = false;
16+
};
17+
template <class Tp> struct is_const<Tp const> {
18+
static const bool value = true;
19+
};
20+
21+
template <class Tp> struct is_volatile {
22+
static const bool value = false;
23+
};
24+
template <class Tp> struct is_volatile<Tp volatile> {
25+
static const bool value = true;
26+
};
27+
28+
template <class T> static const bool is_const_v = is_const<T>::value;
29+
template <class T> static const bool is_volatile_v = is_volatile<T>::value;
30+
31+
template <bool expectConst, bool expectVolatile>
32+
struct VerifyQualifiers {
33+
template <typename T> void *operator new(std::type_identity<T>, size_t) throw() {
34+
static_assert(is_const_v<T> == expectConst); // #1
35+
static_assert(is_volatile_v<T> == expectVolatile); // #2
36+
return 0;
37+
}
38+
template <typename T> void operator delete(std::type_identity<T>, void*) {
39+
static_assert(is_const_v<T> == expectConst); // #3
40+
static_assert(is_volatile_v<T> == expectVolatile); // #4
41+
}
42+
};
43+
44+
template <bool expectConst, bool expectVolatile> void *operator new(std::type_identity<VerifyQualifiers<expectConst, expectVolatile> > type, size_t) throw() {
45+
static_assert(is_const_v<typename decltype(type)::type> == expectConst); // #5
46+
static_assert(is_volatile_v<typename decltype(type)::type> == expectVolatile); // #6
47+
return 0;
48+
}
49+
50+
template <bool expectConst, bool expectVolatile> void operator delete(std::type_identity<VerifyQualifiers<expectConst, expectVolatile> > type, void*) {
51+
static_assert(is_const_v<typename decltype(type)::type> == expectConst); // #7
52+
static_assert(is_volatile_v<typename decltype(type)::type> == expectVolatile); // #8
53+
}
54+
55+
// Success tests
56+
void test_member_allocators() {
57+
auto *unqualified_obj = new VerifyQualifiers<false, false>();
58+
delete unqualified_obj;
59+
auto *const_obj = new const VerifyQualifiers<true, false>();
60+
delete const_obj;
61+
auto *volatile_obj = new volatile VerifyQualifiers<false, true>();
62+
delete volatile_obj;
63+
auto *const_volatile_obj = new const volatile VerifyQualifiers<true, true>();
64+
delete const_volatile_obj;
65+
}
66+
67+
void test_global_allocators() {
68+
auto *unqualified_obj = ::new VerifyQualifiers<false, false>();
69+
::delete unqualified_obj;
70+
auto *const_obj = ::new const VerifyQualifiers<true, false>();
71+
::delete const_obj;
72+
auto *volatile_obj = ::new volatile VerifyQualifiers<false, true>();
73+
::delete volatile_obj;
74+
auto *const_volatile_obj = ::new const volatile VerifyQualifiers<true, true>();
75+
::delete const_volatile_obj;
76+
}
77+
78+
// Verify mismatches
79+
void test_incorrect_member_allocators() {
80+
VerifyQualifiers<true, false> *incorrect_const_obj = new VerifyQualifiers<true, false>();
81+
// expected-error@#1 {{static assertion failed due to requirement 'is_const_v<VerifyQualifiers<true, false>> == true'}}
82+
// expected-note@-2 {{in instantiation of function template specialization 'VerifyQualifiers<true, false>::operator new<VerifyQualifiers<true, false>>' requested here}}
83+
delete incorrect_const_obj;
84+
// expected-error@#3 {{static assertion failed due to requirement 'is_const_v<VerifyQualifiers<true, false>> == true'}}
85+
// expected-note@-2 {{in instantiation of function template specialization 'VerifyQualifiers<true, false>::operator delete<VerifyQualifiers<true, false>>' requested here}}
86+
87+
VerifyQualifiers<false, true> *incorrect_volatile_obj = new VerifyQualifiers<false, true>();
88+
// expected-error@#2 {{static assertion failed due to requirement 'is_volatile_v<VerifyQualifiers<false, true>> == true'}}
89+
// expected-note@-2 {{in instantiation of function template specialization 'VerifyQualifiers<false, true>::operator new<VerifyQualifiers<false, true>>' requested here}}
90+
delete incorrect_volatile_obj;
91+
// expected-error@#4 {{static assertion failed due to requirement 'is_volatile_v<VerifyQualifiers<false, true>> == true'}}
92+
// expected-note@-2 {{in instantiation of function template specialization 'VerifyQualifiers<false, true>::operator delete<VerifyQualifiers<false, true>>' requested here}}
93+
94+
VerifyQualifiers<true, true> *incorrect_const_volatile_obj = new VerifyQualifiers<true, true>();
95+
// expected-error@#1 {{static assertion failed due to requirement 'is_const_v<VerifyQualifiers<true, true>> == true'}}
96+
// expected-error@#2 {{static assertion failed due to requirement 'is_volatile_v<VerifyQualifiers<true, true>> == true'}}
97+
// expected-note@-3 {{in instantiation of function template specialization 'VerifyQualifiers<true, true>::operator new<VerifyQualifiers<true, true>>' requested here}}
98+
delete incorrect_const_volatile_obj;
99+
// expected-error@#3 {{static assertion failed due to requirement 'is_const_v<VerifyQualifiers<true, true>> == true'}}
100+
// expected-error@#4 {{static assertion failed due to requirement 'is_volatile_v<VerifyQualifiers<true, true>> == true'}}
101+
// expected-note@-3 {{in instantiation of function template specialization 'VerifyQualifiers<true, true>::operator delete<VerifyQualifiers<true, true>>' requested here}}
102+
}
103+
104+
105+
// Verify mismatches
106+
void test_incorrect_global_allocators() {
107+
VerifyQualifiers<true, false> *incorrect_const_obj = ::new VerifyQualifiers<true, false>();
108+
// expected-error@#5 {{static assertion failed due to requirement 'is_const_v<VerifyQualifiers<true, false>> == true'}}
109+
// expected-note@-2 {{in instantiation of function template specialization 'operator new<true, false>' requested here}}
110+
::delete incorrect_const_obj;
111+
// expected-error@#7 {{static assertion failed due to requirement 'is_const_v<VerifyQualifiers<true, false>> == true'}}
112+
// expected-note@-2 {{in instantiation of function template specialization 'operator delete<true, false>' requested here}}
113+
114+
VerifyQualifiers<false, true> *incorrect_volatile_obj = ::new VerifyQualifiers<false, true>();
115+
// expected-error@#6 {{static assertion failed due to requirement 'is_volatile_v<VerifyQualifiers<false, true>> == true'}}
116+
// expected-note@-2 {{in instantiation of function template specialization 'operator new<false, true>' requested here}}
117+
::delete incorrect_volatile_obj;
118+
// expected-error@#8 {{static assertion failed due to requirement 'is_volatile_v<VerifyQualifiers<false, true>> == true'}}
119+
// expected-note@-2 {{in instantiation of function template specialization 'operator delete<false, true>' requested here}}
120+
121+
VerifyQualifiers<true, true> *incorrect_const_volatile_obj = ::new VerifyQualifiers<true, true>();
122+
// expected-error@#5 {{static assertion failed due to requirement 'is_const_v<VerifyQualifiers<true, true>> == true'}}
123+
// expected-error@#6 {{static assertion failed due to requirement 'is_volatile_v<VerifyQualifiers<true, true>> == true'}}
124+
// expected-note@-3 {{in instantiation of function template specialization 'operator new<true, true>' requested here}}
125+
::delete incorrect_const_volatile_obj;
126+
// expected-error@#7 {{static assertion failed due to requirement 'is_const_v<VerifyQualifiers<true, true>> == true'}}
127+
// expected-error@#8 {{static assertion failed due to requirement 'is_volatile_v<VerifyQualifiers<true, true>> == true'}}
128+
// expected-note@-3 {{in instantiation of function template specialization 'operator delete<true, true>' requested here}}
129+
}

0 commit comments

Comments
 (0)