Debian Codegraph @

clang/test/AST/ByteCode/placement-new.cpp

raw
1 // RUN: %clang_cc1 -std=c++2c -fcxx-exceptions -fexperimental-new-constant-interpreter -verify=expected,both %s -DBYTECODE
2 // RUN: %clang_cc1 -std=c++2c -fcxx-exceptions -verify=ref,both %s
3
4 typedef __INT64_TYPE__ int64_t;
5 namespace std {
6 using size_t = decltype(sizeof(0));
7 template<typename T> struct allocator {
8 constexpr T *allocate(size_t N) {
9 return (T*)operator new(sizeof(T) * N);
10 }
11 constexpr void deallocate(void *p) {
12 operator delete(p);
13 }
14 };
15 template<typename T, typename ...Args>
16 constexpr void construct_at(void *p, Args &&...args) {
17 new (p) T((Args&&)args...); // both-note {{in call to}} \
18 // both-note {{placement new would change type of storage from 'int' to 'float'}} \
19 // both-note {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}} \
20 // both-note {{construction of temporary is not allowed}} \
21 // both-note {{construction of heap allocated object that has been deleted}} \
22 // both-note {{construction of subobject of object outside its lifetime is not allowed in a constant expression}}
23 }
24 }
25
26 void *operator new(std::size_t, void *p) { return p; }
27 void* operator new[] (std::size_t, void* p) {return p;}
28
29 constexpr int no_lifetime_start = (*std::allocator<int>().allocate(1) = 1); // both-error {{constant expression}} \
30 // both-note {{assignment to object outside its lifetime}}
31
32 consteval auto ok1() {
33 bool b;
34 new (&b) bool(true);
35 return b;
36 }
37 static_assert(ok1());
38
39 consteval auto ok2() {
40 int b;
41 new (&b) int(12);
42 return b;
43 }
44 static_assert(ok2() == 12);
45
46
47 consteval auto ok3() {
48 float b;
49 new (&b) float(12.0);
50 return b;
51 }
52 static_assert(ok3() == 12.0);
53
54
55 consteval auto ok4() {
56 _BitInt(11) b;
57 new (&b) _BitInt(11)(37);
58 return b;
59 }
60 static_assert(ok4() == 37);
61
62 consteval int ok5() {
63 int i;
64 new (&i) int[1]{1};
65
66 struct S {
67 int a; int b;
68 } s;
69 new (&s) S[1]{{12, 13}};
70
71 /// FIXME: Broken in the current interpreter.
72 #if BYTECODE
73 return s.a + s.b;
74 #else
75 return 25;
76 #endif
77 }
78 static_assert(ok5() == 25);
79
80 consteval int ok6() {
81 int i[2];
82 new (i) int(100);
83 new (i + 1) int(200);
84 return i[0] + i[1];
85 }
86 static_assert(ok6() == 300);
87
88 /// FIXME: Broken in the current interpreter.
89 #if BYTECODE
90 consteval int ok7() {
91 int i;
92 new (&i) int[1]{1};
93 return i;
94 }
95 static_assert(ok7() == 1);
96
97 consteval int ok8() {
98 int i[2];
99 new (&i) int(100);
100 return i[0];
101 }
102 static_assert(ok8() == 100);
103 #endif
104
105 consteval auto fail1() {
106 int b;
107 new (&b) float(1.0); // both-note {{placement new would change type of storage from 'int' to 'float'}}
108 return b;
109 }
110 static_assert(fail1() == 0); // both-error {{not an integral constant expression}} \
111 // both-note {{in call to}}
112
113 consteval int fail2() {
114 int i;
115 new (static_cast<void*>(&i)) float(0); // both-note {{placement new would change type of storage from 'int' to 'float'}}
116 return 0;
117 }
118 static_assert(fail2() == 0); // both-error {{not an integral constant expression}} \
119 // both-note {{in call to}}
120
121 consteval int indeterminate() {
122 int * indeterminate;
123 new (indeterminate) int(0); // both-note {{read of uninitialized object is not allowed in a constant expression}}
124 return 0;
125 }
126 static_assert(indeterminate() == 0); // both-error {{not an integral constant expression}} \
127 // both-note {{in call to}}
128
129 consteval int array1() {
130 int i[2];
131 new (&i) int[]{1,2};
132 return i[0] + i[1];
133 }
134 static_assert(array1() == 3);
135
136 consteval int array2() {
137 int i[2];
138 new (static_cast<void*>(&i)) int[]{1,2};
139 return i[0] + i[1];
140 }
141 static_assert(array2() == 3);
142
143 consteval int array3() {
144 int i[1];
145 new (&i) int[2]; // both-note {{placement new would change type of storage from 'int[1]' to 'int[2]'}}
146 return 0;
147 }
148 static_assert(array3() == 0); // both-error {{not an integral constant expression}} \
149 // both-note {{in call to}}
150
151 consteval int array4() {
152 int i[2];
153 new (&i) int[]{12};
154 return i[0];
155 }
156 static_assert(array4() == 12);
157
158 constexpr int *intptr() {
159 return new int;
160 }
161 constexpr bool yay() {
162 int *ptr = new (intptr()) int(42);
163 bool ret = *ptr == 42;
164 delete ptr;
165 return ret;
166 }
167 static_assert(yay());
168
169
170 constexpr bool blah() {
171 int *ptr = new (intptr()) int[3]{ 1, 2, 3 }; // both-note {{placement new would change type of storage from 'int' to 'int[3]'}}
172 bool ret = ptr[0] == 1 && ptr[1] == 2 && ptr[2] == 3;
173 delete [] ptr;
174 return ret;
175 }
176 static_assert(blah()); // both-error {{not an integral constant expression}} \
177 // both-note {{in call to 'blah()'}}
178
179
180 constexpr int *get_indeterminate() {
181 int *evil;
182 return evil; // both-note {{read of uninitialized object is not allowed in a constant expression}}
183 }
184
185 constexpr bool bleh() {
186 int *ptr = new (get_indeterminate()) int; // both-note {{in call to 'get_indeterminate()'}}
187 return true;
188 }
189 static_assert(bleh()); // both-error {{not an integral constant expression}} \
190 // both-note {{in call to 'bleh()'}}
191
192 namespace records {
193 class S {
194 public:
195 float f;
196 };
197
198 constexpr bool record1() {
199 S s(13);
200 new (&s) S(42);
201 return s.f == 42;
202 }
203 static_assert(record1());
204
205 S GlobalS;
206 constexpr bool record2() {
207 new (&GlobalS) S(42); // both-note {{a constant expression cannot modify an object that is visible outside that expression}}
208 return GlobalS.f == 42;
209 }
210 static_assert(record2()); // both-error {{not an integral constant expression}} \
211 // both-note {{in call to}}
212
213
214 constexpr bool record3() {
215 S ss[3];
216
217 new (&ss) S[]{{1}, {2}, {3}};
218
219 return ss[0].f == 1 && ss[1].f == 2 && ss[2].f == 3;
220 }
221 static_assert(record3());
222
223 struct F {
224 float f;
225 };
226 struct R {
227 F f;
228 int a;
229 };
230 constexpr bool record4() {
231 R r;
232 new (&r.f) F{42.0};
233 new (&r.a) int(12);
234
235 return r.f.f == 42.0 && r.a == 12;
236 }
237 static_assert(record4());
238
239 /// Destructor is NOT called.
240 struct A {
241 bool b;
242 constexpr ~A() { if (b) throw; }
243 };
244
245 constexpr int foo() {
246 A a;
247 new (&a) A(true);
248 new (&a) A(false);
249 return 0;
250 }
251 static_assert(foo() == 0);
252 }
253
254 namespace ConstructAt {
255 struct S {
256 int a = 10;
257 float b = 1.0;
258 };
259
260 constexpr bool ok1() {
261 S s;
262
263 std::construct_at<S>(&s);
264 return s.a == 10 && s.b == 1.0;
265 }
266 static_assert(ok1());
267
268 struct S2 {
269 constexpr S2() {
270 (void)(1/0); // both-note {{division by zero}} \
271 // both-warning {{division by zero is undefined}}
272 }
273 };
274
275 constexpr bool ctorFail() { //
276 S2 *s = std::allocator<S2>().allocate(1);
277 std::construct_at<S2>(s); // both-note {{in call to}}
278
279 return true;
280 }
281 static_assert(ctorFail()); // both-error {{not an integral constant expression}} \
282 // both-note {{in call to 'ctorFail()'}}
283
284
285 constexpr bool bad_construct_at_type() {
286 int a;
287 std::construct_at<float>(&a, 1.0f); // both-note {{in call to}}
288 return true;
289 }
290 static_assert(bad_construct_at_type()); // both-error {{not an integral constant expression}} \
291 // both-note {{in call}}
292
293 constexpr bool bad_construct_at_subobject() {
294 struct X { int a, b; };
295 union A {
296 int a;
297 X x;
298 };
299 A a = {1};
300 std::construct_at<int>(&a.x.a, 1); // both-note {{in call}}
301 return true;
302 }
303 static_assert(bad_construct_at_subobject()); // both-error{{not an integral constant expression}} \
304 // both-note {{in call}}
305 }
306
307 namespace UsedToCrash {
308 struct S {
309 int* i;
310 constexpr S() : i(new int(42)) {} // #no-deallocation
311 constexpr ~S() {delete i;}
312 };
313 consteval void alloc() {
314 S* s = new S();
315 s->~S();
316 new (s) S();
317 delete s;
318 }
319 int alloc1 = (alloc(), 0);
320 }
321
322 constexpr bool change_union_member() {
323 union U {
324 int a;
325 int b;
326 };
327 U u = {.a = 1};
328 std::construct_at<int>(&u.b, 2);
329 return u.b == 2;
330 }
331 static_assert(change_union_member());
332
333 namespace PR48606 {
334 struct A { mutable int n = 0; };
335
336 constexpr bool f() {
337 A a;
338 A *p = &a;
339 p->~A();
340 std::construct_at<A>(p);
341 return true;
342 }
343 static_assert(f());
344 }
345
346 /// This used to crash because of an assertion in the implementation
347 /// of the This instruction.
348 namespace ExplicitThisOnArrayElement {
349 struct S {
350 int a = 12;
351 constexpr S(int a) {
352 this->a = a;
353 }
354 };
355
356 template <class _Tp, class... _Args>
357 constexpr void construct_at(_Tp *__location, _Args &&...__args) {
358 new (__location) _Tp(__args...);
359 }
360
361 constexpr bool foo() {
362 auto *M = std::allocator<S>().allocate(13); // both-note {{allocation performed here was not deallocated}}
363 construct_at(M, 12);
364 return true;
365 }
366
367 static_assert(foo()); // both-error {{not an integral constant expression}}
368 }
369
370 #ifdef BYTECODE
371 constexpr int N = [] // expected-error {{must be initialized by a constant expression}} \
372 // expected-note {{assignment to dereferenced one-past-the-end pointer is not allowed in a constant expression}} \
373 // expected-note {{in call to}}
374 {
375 struct S {
376 int a[1];
377 };
378 S s;
379 ::new (s.a) int[1][2][3][4]();
380 return s.a[0];
381 }();
382 #endif
383
384 namespace MemMove {
385 constexpr int foo() {
386 int *a = std::allocator<int>{}.allocate(1);
387 new(a) int{123};
388
389 int b;
390 __builtin_memmove(&b, a, sizeof(int));
391
392 std::allocator<int>{}.deallocate(a);
393 return b;
394 }
395
396 static_assert(foo() == 123);
397 }
398
399 namespace Temp {
400 constexpr int &&temporary = 0; // both-note {{created here}}
401 static_assert((std::construct_at<int>(&temporary, 1), true)); // both-error{{not an integral constant expression}} \
402 // both-note {{in call}}
403 }
404
405 namespace PlacementNewAfterDelete {
406 constexpr bool construct_after_lifetime() {
407 int *p = new int;
408 delete p;
409 std::construct_at<int>(p); // both-note {{in call}}
410 return true;
411 }
412 static_assert(construct_after_lifetime()); // both-error {{}} \
413 // both-note {{in call}}
414 }
415
416 namespace SubObj {
417 constexpr bool construct_after_lifetime_2() {
418 struct A { struct B {} b; };
419 A a;
420 a.~A();
421 std::construct_at<A::B>(&a.b); // both-note {{in call}}
422 return true;
423 }
424 static_assert(construct_after_lifetime_2()); // both-error {{}} both-note {{in call}}
425 }
426
427 namespace RecursiveLifetimeStart {
428 struct B {
429 int b;
430 };
431
432 struct A {
433 B b;
434 int a;
435 };
436
437 constexpr int foo() {
438 A a;
439 a.~A();
440
441 new (&a) A();
442 a.a = 10;
443 a.b.b = 12;
444 return a.a;
445 }
446 static_assert(foo() == 10);
447 }
448
449 namespace ArrayRoot {
450 struct S {
451 int a;
452 };
453 constexpr int foo() {
454 S* ss = std::allocator<S>().allocate(2);
455 new (ss) S{};
456 new (ss + 1) S{};
457
458 S* ps = &ss[2];
459 ps = ss;
460 ps->~S();
461
462 std::allocator<S>().deallocate(ss);
463 return 0;
464 }
465
466 static_assert(foo() == 0);
467 }
468
469 namespace bitcast {
470 template <typename F, typename T>
471 constexpr T bit_cast(const F &f) {
472 return __builtin_bit_cast(T, f);
473 }
474 constexpr int foo() {
475 double *d = std::allocator<double>{}.allocate(2);
476 std::construct_at<double>(d, 0);
477
478 double &dd = *d;
479
480 int64_t i = bit_cast<double, int64_t>(*d);
481
482
483 std::allocator<double>{}.deallocate(d);
484 return i;
485 }
486 static_assert(foo() == 0);
487 }
488
489 constexpr int modify_const_variable() {
490 const int a = 10;
491 new ((int *)&a) int(12); // both-note {{modification of object of const-qualified type 'const int' is not allowed in a constant expression}}
492 return a;
493 }
494 static_assert(modify_const_variable()); // both-error {{not an integral constant expression}} \
495 // both-note {{in call to}}
496
497 constexpr int nullDest() {
498 new (nullptr) int{12}; // both-note {{construction of dereferenced null pointer}}
499 return 0;
500 }
501 static_assert(nullDest() == 0); // both-error {{not an integral constant expression}} \
502 // both-note {{in call to}}
503
504 constexpr int nullArrayDest() {
505 new (nullptr) int{12}; // both-note {{construction of dereferenced null pointer}}
506 return 0;
507 }
508 static_assert(nullArrayDest() == 0); // both-error {{not an integral constant expression}} \
509 // both-note {{in call to}}
510
511 constexpr int intDest() {
512 new ((void*)2) int{3}; // both-note {{cast that performs the conversions of a reinterpret_cast}}
513 return 0;
514 }
515 static_assert(intDest() == 0); // both-error {{not an integral constant expression}} \
516 // both-note {{in call to}}
517
518 constexpr int intDestArray() {
519 new ((void*)2) int[4]; // both-note {{cast that performs the conversions of a reinterpret_cast}}
520 return 0;
521 }
522 static_assert(intDestArray() == 0); // both-error {{not an integral constant expression}} \
523 // both-note {{in call to}}
524