blob: 3647526ff0af715001a5c6e940c9dc1f72a057c7 [file] [log] [blame]
Richard Smith19ad5232019-10-03 00:39:33 +00001// 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"
4
5constexpr 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;
9}
10static_assert(alloc_from_user_code()); // expected-error {{constant expression}} expected-note {{in call}}
11
12namespace 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}}
18 }
19 constexpr void deallocate(void *p) {
20 DELETE(p); // expected-note 2{{'std::allocator<...>::deallocate' used to delete pointer to object allocated with 'new'}}
21 }
22 };
23}
24
25constexpr bool alloc_via_std_allocator() {
26 std::allocator<int> alloc;
27 int *p = alloc.allocate(1);
28 alloc.deallocate(p);
29 return true;
30}
31static_assert(alloc_via_std_allocator());
32
33template<> struct std::allocator<void()> {
34 constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of function type 'void ()'}}
35};
36constexpr void *fn = std::allocator<void()>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
37
38struct Incomplete;
39template<> struct std::allocator<Incomplete> {
40 constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of incomplete type 'Incomplete'}}
41};
42constexpr void *incomplete = std::allocator<Incomplete>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
43
44struct WrongSize { char x[5]; };
45static_assert(sizeof(WrongSize) == 5);
46template<> 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'}}
48};
49constexpr void *wrong_size = std::allocator<WrongSize>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
50
51constexpr 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;
63 }
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;
74 }
75 return true;
76}
77static_assert(mismatched(0, 2)); // expected-error {{constant expression}} expected-note {{in call}}
78static_assert(mismatched(1, 2)); // expected-error {{constant expression}} expected-note {{in call}}
79static_assert(mismatched(2, 0)); // expected-error {{constant expression}} expected-note {{in call}}
80static_assert(mismatched(2, 1)); // expected-error {{constant expression}} expected-note {{in call}}
81static_assert(mismatched(2, 2));
82
83constexpr int *escape = std::allocator<int>().allocate(3); // expected-error {{constant expression}} expected-note {{pointer to subobject of heap-allocated}}
84constexpr int leak = (std::allocator<int>().allocate(3), 0); // expected-error {{constant expression}}
85constexpr int no_lifetime_start = (*std::allocator<int>().allocate(1) = 1); // expected-error {{constant expression}} expected-note {{assignment to object outside its lifetime}}
Richard Smithb5426022019-10-03 00:39:35 +000086
87void *operator new(std::size_t, void *p) { return p; }
88constexpr bool no_placement_new_in_user_code() { // expected-error {{never produces a constant expression}}
89 int a;
90 new (&a) int(42); // expected-note {{call to placement 'operator new'}}
91 return a == 42;
92}
93
94namespace std {
95 constexpr bool placement_new_in_stdlib() {
96 int a;
97 new (&a) int(42);
98 return a == 42;
99 }
100}
101static_assert(std::placement_new_in_stdlib());
102
103namespace std {
104 template<typename T, typename ...Args>
105 constexpr void construct_at(void *p, Args &&...args) {
106 new (p) T((Args&&)args...); // #new
107 }
108}
109
110constexpr bool call_std_construct_at() {
111 int *p = std::allocator<int>().allocate(3);
112 std::construct_at<int>(p, 1);
113 std::construct_at<int>(p + 1, 2);
114 std::construct_at<int>(p + 2, 3);
115 bool good = p[0] + p[1] + p[2] == 6;
116 std::allocator<int>().deallocate(p);
117 return good;
118}
119static_assert(call_std_construct_at());
120
121constexpr bool bad_construct_at_type() {
122 int a;
123 // expected-note@#new {{placement new would change type of storage from 'int' to 'float'}}
124 std::construct_at<float>(&a, 1.0f); // expected-note {{in call}}
125 return true;
126}
127static_assert(bad_construct_at_type()); // expected-error{{}} expected-note {{in call}}
128
129constexpr bool bad_construct_at_subobject() {
130 struct X { int a, b; };
131 union A {
132 int a;
133 X x;
134 };
135 A a = {1};
136 // expected-note@#new {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}}
137 std::construct_at<int>(&a.x.a, 1); // expected-note {{in call}}
138 return true;
139}
140static_assert(bad_construct_at_subobject()); // expected-error{{}} expected-note {{in call}}
141
142constexpr bool change_union_member() {
143 union U {
144 int a;
145 int b;
146 };
147 U u = {.a = 1};
148 std::construct_at<int>(&u.b, 2);
149 return u.b == 2;
150}
151static_assert(change_union_member());
152
153int external;
154// expected-note@#new {{visible outside}}
155static_assert((std::construct_at<int>(&external, 1), true)); // expected-error{{}} expected-note {{in call}}
156
157constexpr int &&temporary = 0; // expected-note {{created here}}
158// expected-note@#new {{construction of temporary is not allowed in a constant expression outside the expression that created the temporary}}
159static_assert((std::construct_at<int>(&temporary, 1), true)); // expected-error{{}} expected-note {{in call}}
160
161constexpr bool construct_after_lifetime() {
162 int *p = new int;
163 delete p;
164 // expected-note@#new {{construction of heap allocated object that has been deleted}}
165 std::construct_at<int>(p); // expected-note {{in call}}
166 return true;
167}
168static_assert(construct_after_lifetime()); // expected-error {{}} expected-note {{in call}}
Richard Smithe381f332019-10-10 22:31:17 +0000169
170constexpr bool construct_after_lifetime_2() {
171 struct A { struct B {} b; };
172 A a;
173 a.~A();
174 std::construct_at<A::B>(&a.b); // expected-note {{in call}}
175 // expected-note@#new {{construction of subobject of object outside its lifetime is not allowed in a constant expression}}
176 return true;
177}
178static_assert(construct_after_lifetime_2()); // expected-error {{}} expected-note {{in call}}