xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/new-delete.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc #include <stddef.h>
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc struct S // expected-note {{candidate}}
6f4a2713aSLionel Sambuc {
7f4a2713aSLionel Sambuc   S(int, int, double); // expected-note {{candidate}}
8f4a2713aSLionel Sambuc   S(double, int); // expected-note 2 {{candidate}}
9f4a2713aSLionel Sambuc   S(float, int); // expected-note 2 {{candidate}}
10f4a2713aSLionel Sambuc };
11f4a2713aSLionel Sambuc struct T; // expected-note{{forward declaration of 'T'}}
12f4a2713aSLionel Sambuc struct U
13f4a2713aSLionel Sambuc {
14f4a2713aSLionel Sambuc   // A special new, to verify that the global version isn't used.
15f4a2713aSLionel Sambuc   void* operator new(size_t, S*); // expected-note {{candidate}}
16f4a2713aSLionel Sambuc };
17f4a2713aSLionel Sambuc struct V : U
18f4a2713aSLionel Sambuc {
19f4a2713aSLionel Sambuc };
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc inline void operator delete(void *); // expected-warning {{replacement function 'operator delete' cannot be declared 'inline'}}
22f4a2713aSLionel Sambuc 
23f4a2713aSLionel Sambuc __attribute__((used))
operator new(size_t)24f4a2713aSLionel Sambuc inline void *operator new(size_t) { // no warning, due to __attribute__((used))
25f4a2713aSLionel Sambuc   return 0;
26f4a2713aSLionel Sambuc }
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc // PR5823
29f4a2713aSLionel Sambuc void* operator new(const size_t); // expected-note 2 {{candidate}}
30f4a2713aSLionel Sambuc void* operator new(size_t, int*); // expected-note 3 {{candidate}}
31f4a2713aSLionel Sambuc void* operator new(size_t, float*); // expected-note 3 {{candidate}}
32f4a2713aSLionel Sambuc void* operator new(size_t, S); // expected-note 2 {{candidate}}
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc struct foo { };
35f4a2713aSLionel Sambuc 
good_news()36f4a2713aSLionel Sambuc void good_news()
37f4a2713aSLionel Sambuc {
38f4a2713aSLionel Sambuc   int *pi = new int;
39f4a2713aSLionel Sambuc   float *pf = new (pi) float();
40f4a2713aSLionel Sambuc   pi = new int(1);
41f4a2713aSLionel Sambuc   pi = new int('c');
42f4a2713aSLionel Sambuc   const int *pci = new const int();
43f4a2713aSLionel Sambuc   S *ps = new S(1, 2, 3.4);
44f4a2713aSLionel Sambuc   ps = new (pf) (S)(1, 2, 3.4);
45f4a2713aSLionel Sambuc   S *(*paps)[2] = new S*[*pi][2];
46f4a2713aSLionel Sambuc   typedef int ia4[4];
47f4a2713aSLionel Sambuc   ia4 *pai = new (int[3][4]);
48f4a2713aSLionel Sambuc   pi = ::new int;
49f4a2713aSLionel Sambuc   U *pu = new (ps) U;
50f4a2713aSLionel Sambuc   V *pv = new (ps) V;
51f4a2713aSLionel Sambuc 
52f4a2713aSLionel Sambuc   pi = new (S(1.0f, 2)) int;
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc   (void)new int[true];
55f4a2713aSLionel Sambuc 
56f4a2713aSLionel Sambuc   // PR7147
57f4a2713aSLionel Sambuc   typedef int a[2];
58f4a2713aSLionel Sambuc   foo* f1 = new foo;
59f4a2713aSLionel Sambuc   foo* f2 = new foo[2];
60f4a2713aSLionel Sambuc   typedef foo x[2];
61f4a2713aSLionel Sambuc   typedef foo y[2][2];
62f4a2713aSLionel Sambuc   x* f3 = new y;
63f4a2713aSLionel Sambuc }
64f4a2713aSLionel Sambuc 
65f4a2713aSLionel Sambuc struct abstract {
66f4a2713aSLionel Sambuc   virtual ~abstract() = 0;
67f4a2713aSLionel Sambuc };
68f4a2713aSLionel Sambuc 
bad_news(int * ip)69f4a2713aSLionel Sambuc void bad_news(int *ip)
70f4a2713aSLionel Sambuc {
71f4a2713aSLionel Sambuc   int i = 1; // expected-note 2{{here}}
72f4a2713aSLionel Sambuc   (void)new; // expected-error {{expected a type}}
73f4a2713aSLionel Sambuc   (void)new 4; // expected-error {{expected a type}}
74f4a2713aSLionel Sambuc   (void)new () int; // expected-error {{expected expression}}
75f4a2713aSLionel Sambuc   (void)new int[1.1]; // expected-error {{array size expression must have integral or enumeration type, not 'double'}}
76f4a2713aSLionel Sambuc   (void)new int[1][i]; // expected-error {{only the first dimension}} expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}
77f4a2713aSLionel Sambuc   (void)new (int[1][i]); // expected-error {{only the first dimension}} expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}
78f4a2713aSLionel Sambuc   (void)new (int[i]); // expected-warning {{when type is in parentheses}}
79f4a2713aSLionel Sambuc   (void)new int(*(S*)0); // expected-error {{no viable conversion from 'S' to 'int'}}
80f4a2713aSLionel Sambuc   (void)new int(1, 2); // expected-error {{excess elements in scalar initializer}}
81f4a2713aSLionel Sambuc   (void)new S(1); // expected-error {{no matching constructor}}
82f4a2713aSLionel Sambuc   (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
83f4a2713aSLionel Sambuc   (void)new const int; // expected-error {{default initialization of an object of const type 'const int'}}
84f4a2713aSLionel Sambuc   (void)new float*(ip); // expected-error {{cannot initialize a new value of type 'float *' with an lvalue of type 'int *'}}
85f4a2713aSLionel Sambuc   // Undefined, but clang should reject it directly.
86f4a2713aSLionel Sambuc   (void)new int[-1]; // expected-error {{array size is negative}}
87f4a2713aSLionel Sambuc   (void)new int[2000000000]; // expected-error {{array is too large}}
88f4a2713aSLionel Sambuc   (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumeration type, not 'S'}}
89f4a2713aSLionel Sambuc   (void)::S::new int; // expected-error {{expected unqualified-id}}
90f4a2713aSLionel Sambuc   (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
91f4a2713aSLionel Sambuc   (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}}
92f4a2713aSLionel Sambuc   // This must fail, because the member version shouldn't be found.
93f4a2713aSLionel Sambuc   (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}}
94f4a2713aSLionel Sambuc   // This must fail, because any member version hides all global versions.
95f4a2713aSLionel Sambuc   (void)new U; // expected-error {{no matching function for call to 'operator new'}}
96f4a2713aSLionel Sambuc   (void)new (int[]); // expected-error {{array size must be specified in new expressions}}
97f4a2713aSLionel Sambuc   (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}}
98f4a2713aSLionel Sambuc   // Some lacking cases due to lack of sema support.
99f4a2713aSLionel Sambuc }
100f4a2713aSLionel Sambuc 
good_deletes()101f4a2713aSLionel Sambuc void good_deletes()
102f4a2713aSLionel Sambuc {
103f4a2713aSLionel Sambuc   delete (int*)0;
104f4a2713aSLionel Sambuc   delete [](int*)0;
105f4a2713aSLionel Sambuc   delete (S*)0;
106f4a2713aSLionel Sambuc   ::delete (int*)0;
107f4a2713aSLionel Sambuc }
108f4a2713aSLionel Sambuc 
bad_deletes()109f4a2713aSLionel Sambuc void bad_deletes()
110f4a2713aSLionel Sambuc {
111f4a2713aSLionel Sambuc   delete 0; // expected-error {{cannot delete expression of type 'int'}}
112f4a2713aSLionel Sambuc   delete [0] (int*)0; // expected-error {{expected expression}}
113f4a2713aSLionel Sambuc   delete (void*)0; // expected-warning {{cannot delete expression with pointer-to-'void' type 'void *'}}
114f4a2713aSLionel Sambuc   delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
115f4a2713aSLionel Sambuc   ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
116f4a2713aSLionel Sambuc }
117f4a2713aSLionel Sambuc 
118f4a2713aSLionel Sambuc struct X0 { };
119f4a2713aSLionel Sambuc 
120f4a2713aSLionel Sambuc struct X1 {
121f4a2713aSLionel Sambuc   operator int*();
122f4a2713aSLionel Sambuc   operator float();
123f4a2713aSLionel Sambuc };
124f4a2713aSLionel Sambuc 
125f4a2713aSLionel Sambuc struct X2 {
126f4a2713aSLionel Sambuc   operator int*(); // expected-note {{conversion}}
127f4a2713aSLionel Sambuc   operator float*(); // expected-note {{conversion}}
128f4a2713aSLionel Sambuc };
129f4a2713aSLionel Sambuc 
test_delete_conv(X0 x0,X1 x1,X2 x2)130f4a2713aSLionel Sambuc void test_delete_conv(X0 x0, X1 x1, X2 x2) {
131f4a2713aSLionel Sambuc   delete x0; // expected-error{{cannot delete}}
132f4a2713aSLionel Sambuc   delete x1;
133f4a2713aSLionel Sambuc   delete x2; // expected-error{{ambiguous conversion of delete expression of type 'X2' to a pointer}}
134f4a2713aSLionel Sambuc }
135f4a2713aSLionel Sambuc 
136f4a2713aSLionel Sambuc // PR4782
137f4a2713aSLionel Sambuc class X3 {
138f4a2713aSLionel Sambuc public:
139f4a2713aSLionel Sambuc   static void operator delete(void * mem, size_t size);
140f4a2713aSLionel Sambuc };
141f4a2713aSLionel Sambuc 
142f4a2713aSLionel Sambuc class X4 {
143f4a2713aSLionel Sambuc public:
144f4a2713aSLionel Sambuc   static void release(X3 *x);
145f4a2713aSLionel Sambuc   static void operator delete(void * mem, size_t size);
146f4a2713aSLionel Sambuc };
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc 
release(X3 * x)149f4a2713aSLionel Sambuc void X4::release(X3 *x) {
150f4a2713aSLionel Sambuc   delete x;
151f4a2713aSLionel Sambuc }
152f4a2713aSLionel Sambuc 
153f4a2713aSLionel Sambuc class X5 {
154f4a2713aSLionel Sambuc public:
Destroy() const155f4a2713aSLionel Sambuc   void Destroy() const { delete this; }
156f4a2713aSLionel Sambuc };
157f4a2713aSLionel Sambuc 
158f4a2713aSLionel Sambuc class Base {
159f4a2713aSLionel Sambuc public:
160f4a2713aSLionel Sambuc   static void *operator new(signed char) throw(); // expected-error {{'operator new' takes type size_t}}
161f4a2713aSLionel Sambuc   static int operator new[] (size_t) throw(); // expected-error {{operator new[]' must return type 'void *'}}
162f4a2713aSLionel Sambuc };
163f4a2713aSLionel Sambuc 
164f4a2713aSLionel Sambuc class Tier {};
165f4a2713aSLionel Sambuc class Comp : public Tier {};
166f4a2713aSLionel Sambuc 
167f4a2713aSLionel Sambuc class Thai : public Base {
168f4a2713aSLionel Sambuc public:
169f4a2713aSLionel Sambuc   Thai(const Tier *adoptDictionary);
170f4a2713aSLionel Sambuc };
171f4a2713aSLionel Sambuc 
loadEngineFor()172f4a2713aSLionel Sambuc void loadEngineFor() {
173f4a2713aSLionel Sambuc   const Comp *dict;
174f4a2713aSLionel Sambuc   new Thai(dict);
175f4a2713aSLionel Sambuc }
176f4a2713aSLionel Sambuc 
177f4a2713aSLionel Sambuc template <class T> struct TBase {
178f4a2713aSLionel Sambuc   void* operator new(T size, int); // expected-error {{'operator new' cannot take a dependent type as first parameter; use size_t}}
179f4a2713aSLionel Sambuc };
180f4a2713aSLionel Sambuc 
181f4a2713aSLionel Sambuc TBase<int> t1;
182f4a2713aSLionel Sambuc 
183f4a2713aSLionel Sambuc class X6 {
184f4a2713aSLionel Sambuc public:
185f4a2713aSLionel Sambuc   static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
186f4a2713aSLionel Sambuc };
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc class X7 {
189f4a2713aSLionel Sambuc public:
190f4a2713aSLionel Sambuc   static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
191f4a2713aSLionel Sambuc };
192f4a2713aSLionel Sambuc 
193f4a2713aSLionel Sambuc class X8 : public X6, public X7 {
194f4a2713aSLionel Sambuc };
195f4a2713aSLionel Sambuc 
f(X8 * x8)196f4a2713aSLionel Sambuc void f(X8 *x8) {
197f4a2713aSLionel Sambuc   delete x8; // expected-error {{member 'operator delete' found in multiple base classes of different types}}
198f4a2713aSLionel Sambuc }
199f4a2713aSLionel Sambuc 
200f4a2713aSLionel Sambuc class X9 {
201f4a2713aSLionel Sambuc public:
202f4a2713aSLionel Sambuc   static void operator delete(void*, int); // expected-note {{'operator delete' declared here}}
203f4a2713aSLionel Sambuc   static void operator delete(void*, float); // expected-note {{'operator delete' declared here}}
204f4a2713aSLionel Sambuc };
205f4a2713aSLionel Sambuc 
f(X9 * x9)206f4a2713aSLionel Sambuc void f(X9 *x9) {
207f4a2713aSLionel Sambuc   delete x9; // expected-error {{no suitable member 'operator delete' in 'X9'}}
208f4a2713aSLionel Sambuc }
209f4a2713aSLionel Sambuc 
210f4a2713aSLionel Sambuc struct X10 {
211f4a2713aSLionel Sambuc   virtual ~X10();
212f4a2713aSLionel Sambuc };
213f4a2713aSLionel Sambuc 
214f4a2713aSLionel Sambuc struct X11 : X10 { // expected-error {{no suitable member 'operator delete' in 'X11'}}
215f4a2713aSLionel Sambuc   void operator delete(void*, int); // expected-note {{'operator delete' declared here}}
216f4a2713aSLionel Sambuc };
217f4a2713aSLionel Sambuc 
f()218f4a2713aSLionel Sambuc void f() {
219f4a2713aSLionel Sambuc   X11 x11; // expected-note {{implicit destructor for 'X11' first required here}}
220f4a2713aSLionel Sambuc }
221f4a2713aSLionel Sambuc 
222f4a2713aSLionel Sambuc struct X12 {
223f4a2713aSLionel Sambuc   void* operator new(size_t, void*);
224f4a2713aSLionel Sambuc };
225f4a2713aSLionel Sambuc 
226f4a2713aSLionel Sambuc struct X13 : X12 {
227f4a2713aSLionel Sambuc   using X12::operator new;
228f4a2713aSLionel Sambuc };
229f4a2713aSLionel Sambuc 
f(void * g)230f4a2713aSLionel Sambuc static void* f(void* g)
231f4a2713aSLionel Sambuc {
232f4a2713aSLionel Sambuc     return new (g) X13();
233f4a2713aSLionel Sambuc }
234f4a2713aSLionel Sambuc 
235f4a2713aSLionel Sambuc class X14 {
236f4a2713aSLionel Sambuc public:
237f4a2713aSLionel Sambuc   static void operator delete(void*, const size_t);
238f4a2713aSLionel Sambuc };
239f4a2713aSLionel Sambuc 
f(X14 * x14a,X14 * x14b)240f4a2713aSLionel Sambuc void f(X14 *x14a, X14 *x14b) {
241f4a2713aSLionel Sambuc   delete x14a;
242f4a2713aSLionel Sambuc }
243f4a2713aSLionel Sambuc 
244f4a2713aSLionel Sambuc class X15 {
245f4a2713aSLionel Sambuc private:
246f4a2713aSLionel Sambuc   X15(); // expected-note {{declared private here}}
247f4a2713aSLionel Sambuc   ~X15(); // expected-note {{declared private here}}
248f4a2713aSLionel Sambuc };
249f4a2713aSLionel Sambuc 
f(X15 * x)250f4a2713aSLionel Sambuc void f(X15* x) {
251f4a2713aSLionel Sambuc   new X15(); // expected-error {{calling a private constructor}}
252f4a2713aSLionel Sambuc   delete x; // expected-error {{calling a private destructor}}
253f4a2713aSLionel Sambuc }
254f4a2713aSLionel Sambuc 
255f4a2713aSLionel Sambuc namespace PR5918 { // Look for template operator new overloads.
256f4a2713aSLionel Sambuc   struct S { template<typename T> static void* operator new(size_t, T); };
test()257f4a2713aSLionel Sambuc   void test() {
258f4a2713aSLionel Sambuc     (void)new(0) S;
259f4a2713aSLionel Sambuc   }
260f4a2713aSLionel Sambuc }
261f4a2713aSLionel Sambuc 
262f4a2713aSLionel Sambuc namespace Test1 {
263f4a2713aSLionel Sambuc 
f()264f4a2713aSLionel Sambuc void f() {
265f4a2713aSLionel Sambuc   (void)new int[10](1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
266f4a2713aSLionel Sambuc 
267f4a2713aSLionel Sambuc   typedef int T[10];
268f4a2713aSLionel Sambuc   (void)new T(1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
269f4a2713aSLionel Sambuc }
270f4a2713aSLionel Sambuc 
271f4a2713aSLionel Sambuc template<typename T>
g(unsigned i)272f4a2713aSLionel Sambuc void g(unsigned i) {
273f4a2713aSLionel Sambuc   (void)new T[1](i); // expected-error {{array 'new' cannot have initialization arguments}}
274f4a2713aSLionel Sambuc }
275f4a2713aSLionel Sambuc 
276f4a2713aSLionel Sambuc template<typename T>
h(unsigned i)277f4a2713aSLionel Sambuc void h(unsigned i) {
278f4a2713aSLionel Sambuc   (void)new T(i); // expected-error {{array 'new' cannot have initialization arguments}}
279f4a2713aSLionel Sambuc }
280f4a2713aSLionel Sambuc template void h<unsigned>(unsigned);
281f4a2713aSLionel Sambuc template void h<unsigned[10]>(unsigned); // expected-note {{in instantiation of function template specialization 'Test1::h<unsigned int [10]>' requested here}}
282f4a2713aSLionel Sambuc 
283f4a2713aSLionel Sambuc }
284f4a2713aSLionel Sambuc 
285f4a2713aSLionel Sambuc // Don't diagnose access for overload candidates that aren't selected.
286f4a2713aSLionel Sambuc namespace PR7436 {
287f4a2713aSLionel Sambuc struct S1 {
288f4a2713aSLionel Sambuc   void* operator new(size_t);
289f4a2713aSLionel Sambuc   void operator delete(void* p);
290f4a2713aSLionel Sambuc 
291f4a2713aSLionel Sambuc private:
292f4a2713aSLionel Sambuc   void* operator new(size_t, void*); // expected-note {{declared private here}}
293f4a2713aSLionel Sambuc   void operator delete(void*, void*);
294f4a2713aSLionel Sambuc };
295f4a2713aSLionel Sambuc class S2 {
296f4a2713aSLionel Sambuc   void* operator new(size_t); // expected-note {{declared private here}}
297f4a2713aSLionel Sambuc   void operator delete(void* p); // expected-note {{declared private here}}
298f4a2713aSLionel Sambuc };
299f4a2713aSLionel Sambuc 
test(S1 * s1,S2 * s2)300f4a2713aSLionel Sambuc void test(S1* s1, S2* s2) {
301f4a2713aSLionel Sambuc   delete s1;
302f4a2713aSLionel Sambuc   delete s2; // expected-error {{is a private member}}
303f4a2713aSLionel Sambuc   (void)new S1();
304f4a2713aSLionel Sambuc   (void)new (0L) S1(); // expected-error {{is a private member}}
305f4a2713aSLionel Sambuc   (void)new S2(); // expected-error {{is a private member}}
306f4a2713aSLionel Sambuc }
307f4a2713aSLionel Sambuc }
308f4a2713aSLionel Sambuc 
309f4a2713aSLionel Sambuc namespace rdar8018245 {
310f4a2713aSLionel Sambuc   struct X0 {
311f4a2713aSLionel Sambuc     static const int value = 17;
312f4a2713aSLionel Sambuc   };
313f4a2713aSLionel Sambuc 
314f4a2713aSLionel Sambuc   const int X0::value;
315f4a2713aSLionel Sambuc 
316f4a2713aSLionel Sambuc   struct X1 {
317f4a2713aSLionel Sambuc     static int value;
318f4a2713aSLionel Sambuc   };
319f4a2713aSLionel Sambuc 
320f4a2713aSLionel Sambuc   int X1::value;
321f4a2713aSLionel Sambuc 
322f4a2713aSLionel Sambuc   template<typename T>
f()323f4a2713aSLionel Sambuc   int *f() {
324f4a2713aSLionel Sambuc     return new (int[T::value]); // expected-warning{{when type is in parentheses, array cannot have dynamic size}}
325f4a2713aSLionel Sambuc   }
326f4a2713aSLionel Sambuc 
327f4a2713aSLionel Sambuc   template int *f<X0>();
328f4a2713aSLionel Sambuc   template int *f<X1>(); // expected-note{{in instantiation of}}
329f4a2713aSLionel Sambuc 
330f4a2713aSLionel Sambuc }
331f4a2713aSLionel Sambuc 
332f4a2713aSLionel Sambuc // <rdar://problem/8248780>
333f4a2713aSLionel Sambuc namespace Instantiate {
334f4a2713aSLionel Sambuc   template<typename T> struct X {
335f4a2713aSLionel Sambuc     operator T*();
336f4a2713aSLionel Sambuc   };
337f4a2713aSLionel Sambuc 
f(X<int> & xi)338f4a2713aSLionel Sambuc   void f(X<int> &xi) {
339f4a2713aSLionel Sambuc     delete xi;
340f4a2713aSLionel Sambuc   }
341f4a2713aSLionel Sambuc }
342f4a2713aSLionel Sambuc 
343f4a2713aSLionel Sambuc namespace PR7810 {
344f4a2713aSLionel Sambuc   struct X {
345f4a2713aSLionel Sambuc     // cv is ignored in arguments
346f4a2713aSLionel Sambuc     static void operator delete(void *const);
347f4a2713aSLionel Sambuc   };
348f4a2713aSLionel Sambuc   struct Y {
349f4a2713aSLionel Sambuc     // cv is ignored in arguments
350f4a2713aSLionel Sambuc     static void operator delete(void *volatile);
351f4a2713aSLionel Sambuc   };
352f4a2713aSLionel Sambuc }
353f4a2713aSLionel Sambuc 
354f4a2713aSLionel Sambuc // Don't crash on template delete operators
355f4a2713aSLionel Sambuc namespace TemplateDestructors {
356f4a2713aSLionel Sambuc   struct S {
~STemplateDestructors::S357f4a2713aSLionel Sambuc     virtual ~S() {}
358f4a2713aSLionel Sambuc 
359f4a2713aSLionel Sambuc     void* operator new(const size_t size);
360f4a2713aSLionel Sambuc     template<class T> void* operator new(const size_t, const int, T*);
361f4a2713aSLionel Sambuc     void operator delete(void*, const size_t);
362f4a2713aSLionel Sambuc     template<class T> void operator delete(void*, const size_t, const int, T*);
363f4a2713aSLionel Sambuc   };
364f4a2713aSLionel Sambuc }
365f4a2713aSLionel Sambuc 
366f4a2713aSLionel Sambuc namespace DeleteParam {
367f4a2713aSLionel Sambuc   struct X {
368f4a2713aSLionel Sambuc     void operator delete(X*); // expected-error{{first parameter of 'operator delete' must have type 'void *'}}
369f4a2713aSLionel Sambuc   };
370f4a2713aSLionel Sambuc 
371f4a2713aSLionel Sambuc   struct Y {
372f4a2713aSLionel Sambuc     void operator delete(void* const);
373f4a2713aSLionel Sambuc   };
374f4a2713aSLionel Sambuc }
375f4a2713aSLionel Sambuc 
376f4a2713aSLionel Sambuc // <rdar://problem/8427878>
377f4a2713aSLionel Sambuc // Test that the correct 'operator delete' is selected to pair with
378f4a2713aSLionel Sambuc // the unexpected placement 'operator new'.
379f4a2713aSLionel Sambuc namespace PairedDelete {
380f4a2713aSLionel Sambuc   template <class T> struct A {
381f4a2713aSLionel Sambuc     A();
382f4a2713aSLionel Sambuc     void *operator new(size_t s, double d = 0);
383f4a2713aSLionel Sambuc     void operator delete(void *p, double d);
operator deletePairedDelete::A384f4a2713aSLionel Sambuc     void operator delete(void *p) {
385f4a2713aSLionel Sambuc       T::dealloc(p);
386f4a2713aSLionel Sambuc     }
387f4a2713aSLionel Sambuc   };
388f4a2713aSLionel Sambuc 
test()389f4a2713aSLionel Sambuc   A<int> *test() {
390f4a2713aSLionel Sambuc     return new A<int>();
391f4a2713aSLionel Sambuc   }
392f4a2713aSLionel Sambuc }
393f4a2713aSLionel Sambuc 
394f4a2713aSLionel Sambuc namespace PR7702 {
test1()395f4a2713aSLionel Sambuc   void test1() {
396f4a2713aSLionel Sambuc     new DoesNotExist; // expected-error {{unknown type name 'DoesNotExist'}}
397f4a2713aSLionel Sambuc   }
398f4a2713aSLionel Sambuc }
399f4a2713aSLionel Sambuc 
400f4a2713aSLionel Sambuc namespace ArrayNewNeedsDtor {
401f4a2713aSLionel Sambuc   struct A { A(); private: ~A(); }; // expected-note {{declared private here}}
402f4a2713aSLionel Sambuc   struct B { B(); A a; }; // expected-error {{field of type 'ArrayNewNeedsDtor::A' has private destructor}}
test9()403f4a2713aSLionel Sambuc   B *test9() {
404f4a2713aSLionel Sambuc     return new B[5]; // expected-note {{implicit destructor for 'ArrayNewNeedsDtor::B' first required here}}
405f4a2713aSLionel Sambuc   }
406f4a2713aSLionel Sambuc }
407f4a2713aSLionel Sambuc 
408f4a2713aSLionel Sambuc namespace DeleteIncompleteClass {
409f4a2713aSLionel Sambuc   struct A; // expected-note {{forward declaration}}
410f4a2713aSLionel Sambuc   extern A x;
f()411f4a2713aSLionel Sambuc   void f() { delete x; } // expected-error {{deleting incomplete class type}}
412f4a2713aSLionel Sambuc }
413f4a2713aSLionel Sambuc 
414f4a2713aSLionel Sambuc namespace DeleteIncompleteClassPointerError {
415f4a2713aSLionel Sambuc   struct A; // expected-note {{forward declaration}}
f(A * x)416f4a2713aSLionel Sambuc   void f(A *x) { 1+delete x; } // expected-warning {{deleting pointer to incomplete type}} \
417f4a2713aSLionel Sambuc                                // expected-error {{invalid operands to binary expression}}
418f4a2713aSLionel Sambuc }
419f4a2713aSLionel Sambuc 
420f4a2713aSLionel Sambuc namespace PR10504 {
421f4a2713aSLionel Sambuc   struct A {
422f4a2713aSLionel Sambuc     virtual void foo() = 0;
423f4a2713aSLionel Sambuc   };
f(A * x)424f4a2713aSLionel Sambuc   void f(A *x) { delete x; } // expected-warning {{delete called on 'PR10504::A' that is abstract but has non-virtual destructor}}
425f4a2713aSLionel Sambuc }
426f4a2713aSLionel Sambuc 
427f4a2713aSLionel Sambuc struct PlacementArg {};
operator new[](size_t,const PlacementArg &)428f4a2713aSLionel Sambuc inline void *operator new[](size_t, const PlacementArg &) throw () {
429f4a2713aSLionel Sambuc   return 0;
430f4a2713aSLionel Sambuc }
operator delete[](void *,const PlacementArg &)431f4a2713aSLionel Sambuc inline void operator delete[](void *, const PlacementArg &) throw () {
432f4a2713aSLionel Sambuc }
433f4a2713aSLionel Sambuc 
434f4a2713aSLionel Sambuc namespace r150682 {
435f4a2713aSLionel Sambuc 
436f4a2713aSLionel Sambuc   template <typename X>
437f4a2713aSLionel Sambuc   struct S {
438f4a2713aSLionel Sambuc     struct Inner {};
Sr150682::S439f4a2713aSLionel Sambuc     S() { new Inner[1]; }
440f4a2713aSLionel Sambuc   };
441f4a2713aSLionel Sambuc 
442f4a2713aSLionel Sambuc   struct T {
443f4a2713aSLionel Sambuc   };
444f4a2713aSLionel Sambuc 
445f4a2713aSLionel Sambuc   template<typename X>
tfn()446f4a2713aSLionel Sambuc   void tfn() {
447f4a2713aSLionel Sambuc     new (*(PlacementArg*)0) T[1];
448f4a2713aSLionel Sambuc   }
449f4a2713aSLionel Sambuc 
fn()450f4a2713aSLionel Sambuc   void fn() {
451f4a2713aSLionel Sambuc     tfn<int>();
452f4a2713aSLionel Sambuc   }
453f4a2713aSLionel Sambuc 
454f4a2713aSLionel Sambuc }
455f4a2713aSLionel Sambuc 
456f4a2713aSLionel Sambuc namespace P12023 {
457f4a2713aSLionel Sambuc   struct CopyCounter
458f4a2713aSLionel Sambuc   {
459f4a2713aSLionel Sambuc       CopyCounter();
460f4a2713aSLionel Sambuc       CopyCounter(const CopyCounter&);
461f4a2713aSLionel Sambuc   };
462f4a2713aSLionel Sambuc 
main()463f4a2713aSLionel Sambuc   int main()
464f4a2713aSLionel Sambuc   {
465f4a2713aSLionel Sambuc     CopyCounter* f = new CopyCounter[10](CopyCounter()); // expected-error {{cannot have initialization arguments}}
466f4a2713aSLionel Sambuc       return 0;
467f4a2713aSLionel Sambuc   }
468f4a2713aSLionel Sambuc }
469f4a2713aSLionel Sambuc 
470f4a2713aSLionel Sambuc namespace PR12061 {
471f4a2713aSLionel Sambuc   template <class C> struct scoped_array {
472f4a2713aSLionel Sambuc     scoped_array(C* p = __null);
473f4a2713aSLionel Sambuc   };
474f4a2713aSLionel Sambuc   template <class Payload> struct Foo {
FooPR12061::Foo475f4a2713aSLionel Sambuc     Foo() : a_(new scoped_array<int>[5]) { }
476f4a2713aSLionel Sambuc     scoped_array< scoped_array<int> > a_;
477f4a2713aSLionel Sambuc   };
478f4a2713aSLionel Sambuc   class Bar {};
479f4a2713aSLionel Sambuc   Foo<Bar> x;
480f4a2713aSLionel Sambuc 
481f4a2713aSLionel Sambuc   template <class C> struct scoped_array2 {
482f4a2713aSLionel Sambuc     scoped_array2(C* p = __null, C* q = __null);
483f4a2713aSLionel Sambuc   };
484f4a2713aSLionel Sambuc   template <class Payload> struct Foo2 {
Foo2PR12061::Foo2485f4a2713aSLionel Sambuc     Foo2() : a_(new scoped_array2<int>[5]) { }
486f4a2713aSLionel Sambuc     scoped_array2< scoped_array2<int> > a_;
487f4a2713aSLionel Sambuc   };
488f4a2713aSLionel Sambuc   class Bar2 {};
489f4a2713aSLionel Sambuc   Foo2<Bar2> x2;
490f4a2713aSLionel Sambuc 
491f4a2713aSLionel Sambuc   class MessageLoop {
492f4a2713aSLionel Sambuc   public:
493f4a2713aSLionel Sambuc     explicit MessageLoop(int type = 0);
494f4a2713aSLionel Sambuc   };
495f4a2713aSLionel Sambuc   template <class CookieStoreTestTraits>
496f4a2713aSLionel Sambuc   class CookieStoreTest {
497f4a2713aSLionel Sambuc   protected:
CookieStoreTest()498f4a2713aSLionel Sambuc     CookieStoreTest() {
499f4a2713aSLionel Sambuc       new MessageLoop;
500f4a2713aSLionel Sambuc     }
501f4a2713aSLionel Sambuc   };
502f4a2713aSLionel Sambuc   struct CookieMonsterTestTraits {
503f4a2713aSLionel Sambuc   };
504f4a2713aSLionel Sambuc   class DeferredCookieTaskTest : public CookieStoreTest<CookieMonsterTestTraits>
505f4a2713aSLionel Sambuc   {
DeferredCookieTaskTest()506f4a2713aSLionel Sambuc     DeferredCookieTaskTest() {}
507f4a2713aSLionel Sambuc   };
508f4a2713aSLionel Sambuc }
509f4a2713aSLionel Sambuc 
510f4a2713aSLionel Sambuc class DeletingPlaceholder {
f()511f4a2713aSLionel Sambuc   int* f() {
512f4a2713aSLionel Sambuc     delete f; // expected-error {{reference to non-static member function must be called; did you mean to call it with no arguments?}}
513f4a2713aSLionel Sambuc     return 0;
514f4a2713aSLionel Sambuc   }
g(int,int)515f4a2713aSLionel Sambuc   int* g(int, int) {
516f4a2713aSLionel Sambuc     delete g; // expected-error {{reference to non-static member function must be called}}
517f4a2713aSLionel Sambuc     return 0;
518f4a2713aSLionel Sambuc   }
519f4a2713aSLionel Sambuc };
520*0a6a1f1dSLionel Sambuc 
521*0a6a1f1dSLionel Sambuc namespace PR18544 {
522*0a6a1f1dSLionel Sambuc   inline void *operator new(size_t); // expected-error {{'operator new' cannot be declared inside a namespace}}
523*0a6a1f1dSLionel Sambuc }
524*0a6a1f1dSLionel Sambuc 
525*0a6a1f1dSLionel Sambuc // PR19968
526*0a6a1f1dSLionel Sambuc inline void* operator new(); // expected-error {{'operator new' must have at least one parameter}}
527