[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / cxx2a-constexpr-dynalloc.cpp
blob6d9c0b607d8a67085b8baa4334a92a21a74e92f3
1 // RUN: %clang_cc1 -std=c++2a -verify=expected,cxx20 %s -DNEW=__builtin_operator_new -DDELETE=__builtin_operator_delete
2 // RUN: %clang_cc1 -std=c++2a -verify=expected,cxx20 %s "-DNEW=operator new" "-DDELETE=operator delete"
3 // RUN: %clang_cc1 -std=c++2a -verify=expected,cxx20 %s "-DNEW=::operator new" "-DDELETE=::operator delete"
4 // RUN: %clang_cc1 -std=c++2c -verify=expected,cxx26 %s "-DNEW=::operator new" "-DDELETE=::operator delete"
6 constexpr bool alloc_from_user_code() {
7 void *p = NEW(sizeof(int)); // expected-note {{cannot allocate untyped memory in a constant expression; use 'std::allocator<T>::allocate'}}
8 DELETE(p);
9 return true;
11 static_assert(alloc_from_user_code()); // expected-error {{constant expression}} expected-note {{in call}}
13 namespace std {
14 using size_t = decltype(sizeof(0));
15 // FIXME: It would be preferable to point these notes at the location of the call to allocator<...>::[de]allocate instead
16 template<typename T> struct allocator {
17 constexpr T *allocate(size_t N) {
18 return (T*)NEW(sizeof(T) * N); // expected-note 3{{heap allocation}} expected-note {{not deallocated}}
20 constexpr void deallocate(void *p) {
21 DELETE(p); // #dealloc expected-note 2{{'std::allocator<...>::deallocate' used to delete pointer to object allocated with 'new'}}
26 constexpr bool alloc_via_std_allocator() {
27 std::allocator<int> alloc;
28 int *p = alloc.allocate(1);
29 alloc.deallocate(p);
30 return true;
32 static_assert(alloc_via_std_allocator());
34 template<> struct std::allocator<void()> {
35 constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of function type 'void ()'}}
37 constexpr void *fn = std::allocator<void()>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
39 struct Incomplete;
40 template<> struct std::allocator<Incomplete> {
41 constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of incomplete type 'Incomplete'}}
43 constexpr void *incomplete = std::allocator<Incomplete>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
45 struct WrongSize { char x[5]; };
46 static_assert(sizeof(WrongSize) == 5);
47 template<> struct std::allocator<WrongSize> {
48 constexpr void *allocate() { return NEW(7); } // expected-note {{allocated size 7 is not a multiple of size 5 of element type 'WrongSize'}}
50 constexpr void *wrong_size = std::allocator<WrongSize>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
52 constexpr bool mismatched(int alloc_kind, int dealloc_kind) {
53 int *p;
54 switch (alloc_kind) {
55 case 0:
56 p = new int; // expected-note {{heap allocation}}
57 break;
58 case 1:
59 p = new int[1]; // expected-note {{heap allocation}}
60 break;
61 case 2:
62 p = std::allocator<int>().allocate(1);
63 break;
65 switch (dealloc_kind) {
66 case 0:
67 delete p; // expected-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
68 break;
69 case 1:
70 delete[] p; // expected-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
71 break;
72 case 2:
73 std::allocator<int>().deallocate(p); // expected-note 2{{in call}}
74 break;
76 return true;
78 static_assert(mismatched(0, 2)); // expected-error {{constant expression}} expected-note {{in call}}
79 static_assert(mismatched(1, 2)); // expected-error {{constant expression}} expected-note {{in call}}
80 static_assert(mismatched(2, 0)); // expected-error {{constant expression}} expected-note {{in call}}
81 static_assert(mismatched(2, 1)); // expected-error {{constant expression}} expected-note {{in call}}
82 static_assert(mismatched(2, 2));
84 constexpr int *escape = std::allocator<int>().allocate(3); // expected-error {{constant expression}} expected-note {{pointer to subobject of heap-allocated}}
85 constexpr int leak = (std::allocator<int>().allocate(3), 0); // expected-error {{constant expression}}
86 constexpr int no_lifetime_start = (*std::allocator<int>().allocate(1) = 1); // expected-error {{constant expression}} expected-note {{assignment to object outside its lifetime}}
87 constexpr int no_deallocate_nullptr = (std::allocator<int>().deallocate(nullptr), 1); // expected-error {{constant expression}} expected-note {{in call}}
88 // expected-note@#dealloc {{'std::allocator<...>::deallocate' used to delete a null pointer}}
89 constexpr int no_deallocate_nonalloc = (std::allocator<int>().deallocate((int*)&no_deallocate_nonalloc), 1); // expected-error {{constant expression}} expected-note {{in call}}
90 // expected-note@#dealloc {{delete of pointer '&no_deallocate_nonalloc' that does not point to a heap-allocated object}}
91 // expected-note@-2 {{declared here}}
93 void *operator new(std::size_t, void *p) { return p; }
94 void* operator new[] (std::size_t, void* p) {return p;}
95 constexpr bool no_placement_new_in_user_code() { // cxx20-error {{constexpr function never produces a constant expression}}
96 int a;
97 new (&a) int(42); // cxx20-note {{this placement new expression is not supported in constant expressions before C++2c}}
98 return a == 42;
101 namespace std {
102 constexpr bool placement_new_in_stdlib() {
103 int a;
104 new (&a) int(42);
105 return a == 42;
108 static_assert(std::placement_new_in_stdlib());
110 namespace std {
111 template<typename T, typename ...Args>
112 constexpr void construct_at(void *p, Args &&...args) {
113 new (p) T((Args&&)args...); // #new
117 constexpr bool call_std_construct_at() {
118 int *p = std::allocator<int>().allocate(3);
119 std::construct_at<int>(p, 1);
120 std::construct_at<int>(p + 1, 2);
121 std::construct_at<int>(p + 2, 3);
122 bool good = p[0] + p[1] + p[2] == 6;
123 std::allocator<int>().deallocate(p);
124 return good;
126 static_assert(call_std_construct_at());
128 constexpr bool bad_construct_at_type() {
129 int a;
130 // expected-note@#new {{placement new would change type of storage from 'int' to 'float'}}
131 std::construct_at<float>(&a, 1.0f); // expected-note {{in call}}
132 return true;
134 static_assert(bad_construct_at_type()); // expected-error{{}} expected-note {{in call}}
136 constexpr bool bad_construct_at_subobject() {
137 struct X { int a, b; };
138 union A {
139 int a;
140 X x;
142 A a = {1};
143 // expected-note@#new {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}}
144 std::construct_at<int>(&a.x.a, 1); // expected-note {{in call}}
145 return true;
147 static_assert(bad_construct_at_subobject()); // expected-error{{}} expected-note {{in call}}
149 constexpr bool change_union_member() {
150 union U {
151 int a;
152 int b;
154 U u = {.a = 1};
155 std::construct_at<int>(&u.b, 2);
156 return u.b == 2;
158 static_assert(change_union_member());
160 int external;
161 // expected-note@#new {{visible outside}}
162 static_assert((std::construct_at<int>(&external, 1), true)); // expected-error{{}} expected-note {{in call}}
164 constexpr int &&temporary = 0; // expected-note {{created here}}
165 // expected-note@#new {{construction of temporary is not allowed in a constant expression outside the expression that created the temporary}}
166 static_assert((std::construct_at<int>(&temporary, 1), true)); // expected-error{{}} expected-note {{in call}}
168 constexpr bool construct_after_lifetime() {
169 int *p = new int;
170 delete p;
171 // expected-note@#new {{construction of heap allocated object that has been deleted}}
172 std::construct_at<int>(p); // expected-note {{in call}}
173 return true;
175 static_assert(construct_after_lifetime()); // expected-error {{}} expected-note {{in call}}
177 constexpr bool construct_after_lifetime_2() {
178 struct A { struct B {} b; };
179 A a;
180 a.~A();
181 std::construct_at<A::B>(&a.b); // expected-note {{in call}}
182 // expected-note@#new {{construction of subobject of object outside its lifetime is not allowed in a constant expression}}
183 return true;
185 static_assert(construct_after_lifetime_2()); // expected-error {{}} expected-note {{in call}}
187 namespace PR48606 {
188 struct A { mutable int n = 0; };
190 constexpr bool f() {
191 A a;
192 A *p = &a;
193 p->~A();
194 std::construct_at<A>(p);
195 return true;
197 static_assert(f());
199 constexpr bool g() {
200 A *p = new A;
201 p->~A();
202 std::construct_at<A>(p);
203 delete p;
204 return true;
206 static_assert(g());
208 constexpr bool h() {
209 std::allocator<A> alloc;
210 A *p = alloc.allocate(1);
211 std::construct_at<A>(p);
212 p->~A();
213 std::construct_at<A>(p);
214 p->~A();
215 alloc.deallocate(p);
216 return true;
218 static_assert(h());
221 namespace GH62462 {
223 class string {
224 public:
225 char *mem;
226 constexpr string() {
227 this->mem = new char(1);
229 constexpr ~string() {
230 delete this->mem;
232 constexpr unsigned size() const { return 4; }
236 template <unsigned N>
237 void test() {};
239 void f() {
240 test<string().size()>();