[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / cxx2a-constexpr-dynalloc.cpp
blobf37ebc9f6365588459f7f5e8bb04c3dc5f1addbd
1 // RUN: %clang_cc1 -std=c++2a -verify %s -DNEW=__builtin_operator_new -DDELETE=__builtin_operator_delete
2 // RUN: %clang_cc1 -std=c++2a -verify %s "-DNEW=operator new" "-DDELETE=operator delete"
3 // RUN: %clang_cc1 -std=c++2a -verify %s "-DNEW=::operator new" "-DDELETE=::operator delete"
5 constexpr bool alloc_from_user_code() {
6 void *p = NEW(sizeof(int)); // expected-note {{cannot allocate untyped memory in a constant expression; use 'std::allocator<T>::allocate'}}
7 DELETE(p);
8 return true;
10 static_assert(alloc_from_user_code()); // expected-error {{constant expression}} expected-note {{in call}}
12 namespace std {
13 using size_t = decltype(sizeof(0));
14 // FIXME: It would be preferable to point these notes at the location of the call to allocator<...>::[de]allocate instead
15 template<typename T> struct allocator {
16 constexpr T *allocate(size_t N) {
17 return (T*)NEW(sizeof(T) * N); // expected-note 3{{heap allocation}} expected-note {{not deallocated}}
19 constexpr void deallocate(void *p) {
20 DELETE(p); // #dealloc expected-note 2{{'std::allocator<...>::deallocate' used to delete pointer to object allocated with 'new'}}
25 constexpr bool alloc_via_std_allocator() {
26 std::allocator<int> alloc;
27 int *p = alloc.allocate(1);
28 alloc.deallocate(p);
29 return true;
31 static_assert(alloc_via_std_allocator());
33 template<> struct std::allocator<void()> {
34 constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of function type 'void ()'}}
36 constexpr void *fn = std::allocator<void()>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
38 struct Incomplete;
39 template<> struct std::allocator<Incomplete> {
40 constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of incomplete type 'Incomplete'}}
42 constexpr void *incomplete = std::allocator<Incomplete>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
44 struct WrongSize { char x[5]; };
45 static_assert(sizeof(WrongSize) == 5);
46 template<> struct std::allocator<WrongSize> {
47 constexpr void *allocate() { return NEW(7); } // expected-note {{allocated size 7 is not a multiple of size 5 of element type 'WrongSize'}}
49 constexpr void *wrong_size = std::allocator<WrongSize>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
51 constexpr bool mismatched(int alloc_kind, int dealloc_kind) {
52 int *p;
53 switch (alloc_kind) {
54 case 0:
55 p = new int; // expected-note {{heap allocation}}
56 break;
57 case 1:
58 p = new int[1]; // expected-note {{heap allocation}}
59 break;
60 case 2:
61 p = std::allocator<int>().allocate(1);
62 break;
64 switch (dealloc_kind) {
65 case 0:
66 delete p; // expected-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
67 break;
68 case 1:
69 delete[] p; // expected-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
70 break;
71 case 2:
72 std::allocator<int>().deallocate(p); // expected-note 2{{in call}}
73 break;
75 return true;
77 static_assert(mismatched(0, 2)); // expected-error {{constant expression}} expected-note {{in call}}
78 static_assert(mismatched(1, 2)); // expected-error {{constant expression}} expected-note {{in call}}
79 static_assert(mismatched(2, 0)); // expected-error {{constant expression}} expected-note {{in call}}
80 static_assert(mismatched(2, 1)); // expected-error {{constant expression}} expected-note {{in call}}
81 static_assert(mismatched(2, 2));
83 constexpr int *escape = std::allocator<int>().allocate(3); // expected-error {{constant expression}} expected-note {{pointer to subobject of heap-allocated}}
84 constexpr int leak = (std::allocator<int>().allocate(3), 0); // expected-error {{constant expression}}
85 constexpr int no_lifetime_start = (*std::allocator<int>().allocate(1) = 1); // expected-error {{constant expression}} expected-note {{assignment to object outside its lifetime}}
86 constexpr int no_deallocate_nullptr = (std::allocator<int>().deallocate(nullptr), 1); // expected-error {{constant expression}} expected-note {{in call}}
87 // expected-note@#dealloc {{'std::allocator<...>::deallocate' used to delete a null pointer}}
88 constexpr int no_deallocate_nonalloc = (std::allocator<int>().deallocate((int*)&no_deallocate_nonalloc), 1); // expected-error {{constant expression}} expected-note {{in call}}
89 // expected-note@#dealloc {{delete of pointer '&no_deallocate_nonalloc' that does not point to a heap-allocated object}}
90 // expected-note@-2 {{declared here}}
92 void *operator new(std::size_t, void *p) { return p; }
93 constexpr bool no_placement_new_in_user_code() { // expected-error {{never produces a constant expression}}
94 int a;
95 new (&a) int(42); // expected-note {{call to placement 'operator new'}}
96 return a == 42;
99 namespace std {
100 constexpr bool placement_new_in_stdlib() {
101 int a;
102 new (&a) int(42);
103 return a == 42;
106 static_assert(std::placement_new_in_stdlib());
108 namespace std {
109 template<typename T, typename ...Args>
110 constexpr void construct_at(void *p, Args &&...args) {
111 new (p) T((Args&&)args...); // #new
115 constexpr bool call_std_construct_at() {
116 int *p = std::allocator<int>().allocate(3);
117 std::construct_at<int>(p, 1);
118 std::construct_at<int>(p + 1, 2);
119 std::construct_at<int>(p + 2, 3);
120 bool good = p[0] + p[1] + p[2] == 6;
121 std::allocator<int>().deallocate(p);
122 return good;
124 static_assert(call_std_construct_at());
126 constexpr bool bad_construct_at_type() {
127 int a;
128 // expected-note@#new {{placement new would change type of storage from 'int' to 'float'}}
129 std::construct_at<float>(&a, 1.0f); // expected-note {{in call}}
130 return true;
132 static_assert(bad_construct_at_type()); // expected-error{{}} expected-note {{in call}}
134 constexpr bool bad_construct_at_subobject() {
135 struct X { int a, b; };
136 union A {
137 int a;
138 X x;
140 A a = {1};
141 // expected-note@#new {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}}
142 std::construct_at<int>(&a.x.a, 1); // expected-note {{in call}}
143 return true;
145 static_assert(bad_construct_at_subobject()); // expected-error{{}} expected-note {{in call}}
147 constexpr bool change_union_member() {
148 union U {
149 int a;
150 int b;
152 U u = {.a = 1};
153 std::construct_at<int>(&u.b, 2);
154 return u.b == 2;
156 static_assert(change_union_member());
158 int external;
159 // expected-note@#new {{visible outside}}
160 static_assert((std::construct_at<int>(&external, 1), true)); // expected-error{{}} expected-note {{in call}}
162 constexpr int &&temporary = 0; // expected-note {{created here}}
163 // expected-note@#new {{construction of temporary is not allowed in a constant expression outside the expression that created the temporary}}
164 static_assert((std::construct_at<int>(&temporary, 1), true)); // expected-error{{}} expected-note {{in call}}
166 constexpr bool construct_after_lifetime() {
167 int *p = new int;
168 delete p;
169 // expected-note@#new {{construction of heap allocated object that has been deleted}}
170 std::construct_at<int>(p); // expected-note {{in call}}
171 return true;
173 static_assert(construct_after_lifetime()); // expected-error {{}} expected-note {{in call}}
175 constexpr bool construct_after_lifetime_2() {
176 struct A { struct B {} b; };
177 A a;
178 a.~A();
179 std::construct_at<A::B>(&a.b); // expected-note {{in call}}
180 // expected-note@#new {{construction of subobject of object outside its lifetime is not allowed in a constant expression}}
181 return true;
183 static_assert(construct_after_lifetime_2()); // expected-error {{}} expected-note {{in call}}
185 namespace PR48606 {
186 struct A { mutable int n = 0; };
188 constexpr bool f() {
189 A a;
190 A *p = &a;
191 p->~A();
192 std::construct_at<A>(p);
193 return true;
195 static_assert(f());
197 constexpr bool g() {
198 A *p = new A;
199 p->~A();
200 std::construct_at<A>(p);
201 delete p;
202 return true;
204 static_assert(g());
206 constexpr bool h() {
207 std::allocator<A> alloc;
208 A *p = alloc.allocate(1);
209 std::construct_at<A>(p);
210 p->~A();
211 std::construct_at<A>(p);
212 p->~A();
213 alloc.deallocate(p);
214 return true;
216 static_assert(h());