blob: 748ce77700976043449f2334d449c3877ba27986 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Douglas Gregorc83ed042008-11-25 04:08:05 +00002
Sebastian Redlb5a57a62008-12-03 20:26:15 +00003#include <stddef.h>
4
Sebastian Redl4c5d3202008-11-21 19:14:01 +00005struct S // expected-note {{candidate}}
6{
7 S(int, int, double); // expected-note {{candidate}}
Sebastian Redl3cb06922009-02-07 19:52:04 +00008 S(double, int); // expected-note 2 {{candidate}}
9 S(float, int); // expected-note 2 {{candidate}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000010};
John McCall7c2342d2010-03-10 11:27:22 +000011struct T; // expected-note{{forward declaration of 'T'}}
Sebastian Redl636a7c42008-12-04 17:24:46 +000012struct U
13{
14 // A special new, to verify that the global version isn't used.
Sebastian Redl9afe1302009-05-14 18:11:41 +000015 void* operator new(size_t, S*); // expected-note {{candidate}}
Sebastian Redl636a7c42008-12-04 17:24:46 +000016};
Sebastian Redl7f662392008-12-04 22:20:51 +000017struct V : U
18{
19};
Sebastian Redl4c5d3202008-11-21 19:14:01 +000020
Douglas Gregor6e790ab2009-12-22 23:42:49 +000021// PR5823
22void* operator new(const size_t); // expected-note 2 {{candidate}}
Sebastian Redl3cb06922009-02-07 19:52:04 +000023void* operator new(size_t, int*); // expected-note 3 {{candidate}}
24void* operator new(size_t, float*); // expected-note 3 {{candidate}}
Anders Carlssonfc27d262009-05-31 19:49:47 +000025void* operator new(size_t, S); // expected-note 2 {{candidate}}
Sebastian Redlb5a57a62008-12-03 20:26:15 +000026
Douglas Gregor3caf04e2010-05-16 16:01:03 +000027struct foo { };
28
Sebastian Redl4c5d3202008-11-21 19:14:01 +000029void good_news()
30{
31 int *pi = new int;
32 float *pf = new (pi) float();
33 pi = new int(1);
34 pi = new int('c');
35 const int *pci = new const int();
36 S *ps = new S(1, 2, 3.4);
Sebastian Redlcee63fb2008-12-02 14:43:59 +000037 ps = new (pf) (S)(1, 2, 3.4);
Sebastian Redl4c5d3202008-11-21 19:14:01 +000038 S *(*paps)[2] = new S*[*pi][2];
Sebastian Redl4c5d3202008-11-21 19:14:01 +000039 typedef int ia4[4];
40 ia4 *pai = new (int[3][4]);
Sebastian Redlfb4ccd72008-12-02 16:35:44 +000041 pi = ::new int;
Sebastian Redl636a7c42008-12-04 17:24:46 +000042 U *pu = new (ps) U;
Douglas Gregor5d64e5b2009-09-30 00:03:47 +000043 V *pv = new (ps) V;
Anders Carlssonfc27d262009-05-31 19:49:47 +000044
45 pi = new (S(1.0f, 2)) int;
Anders Carlssonac18b2e2009-09-23 00:37:25 +000046
47 (void)new int[true];
Douglas Gregor3caf04e2010-05-16 16:01:03 +000048
49 // PR7147
50 typedef int a[2];
51 foo* f1 = new foo;
52 foo* f2 = new foo[2];
53 typedef foo x[2];
54 typedef foo y[2][2];
55 x* f3 = new y;
Sebastian Redl4c5d3202008-11-21 19:14:01 +000056}
57
Douglas Gregore7450f52009-03-24 19:52:54 +000058struct abstract {
59 virtual ~abstract() = 0;
60};
61
Douglas Gregorc83ed042008-11-25 04:08:05 +000062void bad_news(int *ip)
Sebastian Redl4c5d3202008-11-21 19:14:01 +000063{
64 int i = 1;
Nick Lewycky9fa8e562010-11-03 17:52:57 +000065 (void)new; // expected-error {{expected a type}}
66 (void)new 4; // expected-error {{expected a type}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000067 (void)new () int; // expected-error {{expected expression}}
Sebastian Redlcee63fb2008-12-02 14:43:59 +000068 (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000069 (void)new int[1][i]; // expected-error {{only the first dimension}}
70 (void)new (int[1][i]); // expected-error {{only the first dimension}}
Douglas Gregor4bd40312010-07-13 15:54:32 +000071 (void)new (int[i]); // expected-warning {{when type is in parentheses}}
John McCall7c2342d2010-03-10 11:27:22 +000072 (void)new int(*(S*)0); // expected-error {{no viable conversion from 'S' to 'int'}}
Douglas Gregor99a2e602009-12-16 01:38:02 +000073 (void)new int(1, 2); // expected-error {{excess elements in scalar initializer}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000074 (void)new S(1); // expected-error {{no matching constructor}}
John McCall7c2342d2010-03-10 11:27:22 +000075 (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
Chris Lattner58f9e132010-09-05 00:04:01 +000076 (void)new const int; // expected-error {{default initialization of an object of const type 'const int'}}
Anders Carlssona729bbb2010-01-23 05:47:27 +000077 (void)new float*(ip); // expected-error {{cannot initialize a new value of type 'float *' with an lvalue of type 'int *'}}
Sebastian Redlcee63fb2008-12-02 14:43:59 +000078 // Undefined, but clang should reject it directly.
79 (void)new int[-1]; // expected-error {{array size is negative}}
John McCall7c2342d2010-03-10 11:27:22 +000080 (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'S'}}
Sebastian Redlfb4ccd72008-12-02 16:35:44 +000081 (void)::S::new int; // expected-error {{expected unqualified-id}}
Sebastian Redlb5a57a62008-12-03 20:26:15 +000082 (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
Sebastian Redl7f662392008-12-04 22:20:51 +000083 (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}}
Sebastian Redl636a7c42008-12-04 17:24:46 +000084 // This must fail, because the member version shouldn't be found.
85 (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}}
Sebastian Redl9afe1302009-05-14 18:11:41 +000086 // This must fail, because any member version hides all global versions.
87 (void)new U; // expected-error {{no matching function for call to 'operator new'}}
Sebastian Redl00e68e22009-02-09 18:24:27 +000088 (void)new (int[]); // expected-error {{array size must be specified in new expressions}}
89 (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +000090 // Some lacking cases due to lack of sema support.
91}
92
93void good_deletes()
94{
95 delete (int*)0;
96 delete [](int*)0;
97 delete (S*)0;
Sebastian Redlfb4ccd72008-12-02 16:35:44 +000098 ::delete (int*)0;
Sebastian Redl4c5d3202008-11-21 19:14:01 +000099}
100
101void bad_deletes()
102{
103 delete 0; // expected-error {{cannot delete expression of type 'int'}}
104 delete [0] (int*)0; // expected-error {{expected ']'}} \
Chris Lattner28eb7e92008-11-23 23:17:07 +0000105 // expected-note {{to match this '['}}
Douglas Gregor94a61572010-05-24 17:01:56 +0000106 delete (void*)0; // expected-warning {{cannot delete expression with pointer-to-'void' type 'void *'}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000107 delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000108 ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000109}
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +0000110
111struct X0 { };
112
113struct X1 {
114 operator int*();
115 operator float();
116};
117
118struct X2 {
Fariborz Jahanianf6527932009-09-15 17:21:47 +0000119 operator int*(); // expected-note {{candidate function}}
120 operator float*(); // expected-note {{candidate function}}
Douglas Gregor9cd9f3f2009-09-09 23:39:55 +0000121};
122
123void test_delete_conv(X0 x0, X1 x1, X2 x2) {
124 delete x0; // expected-error{{cannot delete}}
125 delete x1;
John McCall7c2342d2010-03-10 11:27:22 +0000126 delete x2; // expected-error{{ambiguous conversion of delete expression of type 'X2' to a pointer}}
Fariborz Jahanianf6527932009-09-15 17:21:47 +0000127}
Douglas Gregor90916562009-09-29 18:16:17 +0000128
129// PR4782
130class X3 {
131public:
Eli Friedman5e6214b2009-11-16 05:14:40 +0000132 static void operator delete(void * mem, size_t size);
Douglas Gregor90916562009-09-29 18:16:17 +0000133};
134
135class X4 {
136public:
137 static void release(X3 *x);
Eli Friedman5e6214b2009-11-16 05:14:40 +0000138 static void operator delete(void * mem, size_t size);
Douglas Gregor90916562009-09-29 18:16:17 +0000139};
140
141
142void X4::release(X3 *x) {
143 delete x;
144}
Douglas Gregor1070c9f2009-09-29 21:38:53 +0000145
Douglas Gregor5d64e5b2009-09-30 00:03:47 +0000146class X5 {
Douglas Gregor1070c9f2009-09-29 21:38:53 +0000147public:
148 void Destroy() const { delete this; }
149};
Fariborz Jahanianb03bfa52009-11-10 23:47:18 +0000150
151class Base {
152public:
Anders Carlsson156c78e2009-12-13 17:53:43 +0000153 static void *operator new(signed char) throw(); // expected-error {{'operator new' takes type size_t}}
154 static int operator new[] (size_t) throw(); // expected-error {{operator new[]' must return type 'void *'}}
Fariborz Jahanianb03bfa52009-11-10 23:47:18 +0000155};
156
157class Tier {};
158class Comp : public Tier {};
159
160class Thai : public Base {
161public:
162 Thai(const Tier *adoptDictionary);
163};
164
165void loadEngineFor() {
166 const Comp *dict;
167 new Thai(dict);
168}
169
170template <class T> struct TBase {
Douglas Gregor2fa98002010-02-16 19:28:15 +0000171 void* operator new(T size, int); // expected-error {{'operator new' cannot take a dependent type as first parameter; use size_t}}
Fariborz Jahanianb03bfa52009-11-10 23:47:18 +0000172};
173
Douglas Gregor2fa98002010-02-16 19:28:15 +0000174TBase<int> t1;
Fariborz Jahanianb03bfa52009-11-10 23:47:18 +0000175
Anders Carlsson0ba63ea2009-11-14 03:17:38 +0000176class X6 {
177public:
178 static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
179};
180
181class X7 {
182public:
183 static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
184};
185
186class X8 : public X6, public X7 {
187};
188
Anders Carlsson50724302009-11-15 16:43:15 +0000189void f(X8 *x8) {
Anders Carlsson0ba63ea2009-11-14 03:17:38 +0000190 delete x8; // expected-error {{member 'operator delete' found in multiple base classes of different types}}
191}
192
Anders Carlsson50724302009-11-15 16:43:15 +0000193class X9 {
John McCall7002f4c2010-04-09 19:03:51 +0000194public:
Anders Carlsson50724302009-11-15 16:43:15 +0000195 static void operator delete(void*, int); // expected-note {{'operator delete' declared here}}
196 static void operator delete(void*, float); // expected-note {{'operator delete' declared here}}
197};
198
199void f(X9 *x9) {
200 delete x9; // expected-error {{no suitable member 'operator delete' in 'X9'}}
201}
Anders Carlsson37909802009-11-30 21:24:50 +0000202
203struct X10 {
204 virtual ~X10();
205};
206
207struct X11 : X10 { // expected-error {{no suitable member 'operator delete' in 'X11'}}
208 void operator delete(void*, int); // expected-note {{'operator delete' declared here}}
209};
210
211void f() {
John McCall7c2342d2010-03-10 11:27:22 +0000212 X11 x11; // expected-note {{implicit default destructor for 'X11' first required here}}
Anders Carlsson37909802009-11-30 21:24:50 +0000213}
Anders Carlssoneac81392009-12-09 07:39:44 +0000214
215struct X12 {
216 void* operator new(size_t, void*);
217};
218
219struct X13 : X12 {
220 using X12::operator new;
221};
222
223static void* f(void* g)
224{
225 return new (g) X13();
226}
Chandler Carruth4a73ea92010-02-03 11:02:14 +0000227
Chandler Carruthe228ba92010-02-08 18:54:05 +0000228class X14 {
John McCall7002f4c2010-04-09 19:03:51 +0000229public:
Chandler Carruthe228ba92010-02-08 18:54:05 +0000230 static void operator delete(void*, const size_t);
231};
232
233void f(X14 *x14a, X14 *x14b) {
234 delete x14a;
235}
236
Douglas Gregord880f522011-02-01 15:50:11 +0000237class X15 {
238private:
239 X15(); // expected-note {{declared private here}}
240 ~X15(); // expected-note {{declared private here}}
241};
242
243void f(X15* x) {
244 new X15(); // expected-error {{calling a private constructor}}
245 delete x; // expected-error {{calling a private destructor}}
246}
247
Chandler Carruth4a73ea92010-02-03 11:02:14 +0000248namespace PR5918 { // Look for template operator new overloads.
249 struct S { template<typename T> static void* operator new(size_t, T); };
250 void test() {
251 (void)new(0) S;
252 }
253}
Anders Carlsson48c95012010-05-03 15:45:23 +0000254
255namespace Test1 {
256
257void f() {
258 (void)new int[10](1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
Anders Carlsson55cbd6e2010-05-16 16:24:20 +0000259
260 typedef int T[10];
261 (void)new T(1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
Anders Carlsson48c95012010-05-03 15:45:23 +0000262}
263
264template<typename T>
265void g(unsigned i) {
266 (void)new T[1](i); // expected-error {{array 'new' cannot have initialization arguments}}
267}
268
269template<typename T>
270void h(unsigned i) {
271 (void)new T(i); // expected-error {{array 'new' cannot have initialization arguments}}
272}
273template void h<unsigned>(unsigned);
274template void h<unsigned[10]>(unsigned); // expected-note {{in instantiation of function template specialization 'Test1::h<unsigned int [10]>' requested here}}
275
276}
Chandler Carruth23893242010-06-28 00:30:51 +0000277
278// Don't diagnose access for overload candidates that aren't selected.
279namespace PR7436 {
280struct S1 {
281 void* operator new(size_t);
282 void operator delete(void* p);
283
284private:
285 void* operator new(size_t, void*); // expected-note {{declared private here}}
286 void operator delete(void*, void*);
287};
288class S2 {
289 void* operator new(size_t); // expected-note {{declared private here}}
290 void operator delete(void* p); // expected-note {{declared private here}}
291};
292
293void test(S1* s1, S2* s2) {
294 delete s1;
295 delete s2; // expected-error {{is a private member}}
296 (void)new S1();
297 (void)new (0L) S1(); // expected-error {{is a private member}}
298 (void)new S2(); // expected-error {{is a private member}}
299}
300}
Douglas Gregor6bc574d2010-06-30 00:20:43 +0000301
Douglas Gregor4bd40312010-07-13 15:54:32 +0000302namespace rdar8018245 {
303 struct X0 {
304 static const int value = 17;
305 };
306
307 const int X0::value;
308
309 struct X1 {
310 static int value;
311 };
312
313 int X1::value;
314
315 template<typename T>
316 int *f() {
317 return new (int[T::value]); // expected-warning{{when type is in parentheses, array cannot have dynamic size}}
318 }
319
320 template int *f<X0>();
321 template int *f<X1>(); // expected-note{{in instantiation of}}
322
323}
Douglas Gregor254a9422010-07-29 14:44:35 +0000324
325// <rdar://problem/8248780>
326namespace Instantiate {
327 template<typename T> struct X {
328 operator T*();
329 };
330
331 void f(X<int> &xi) {
332 delete xi;
333 }
334}
Sebastian Redl58e55392010-08-05 00:45:34 +0000335
336namespace PR7810 {
337 struct X {
338 // cv is ignored in arguments
339 static void operator delete(void *const);
340 };
341 struct Y {
342 // cv is ignored in arguments
343 static void operator delete(void *volatile);
344 };
345}
Chandler Carruth09556fd2010-08-08 07:04:00 +0000346
347// Don't crash on template delete operators
348namespace TemplateDestructors {
349 struct S {
350 virtual ~S() {}
351
352 void* operator new(const size_t size);
353 template<class T> void* operator new(const size_t, const int, T*);
354 void operator delete(void*, const size_t);
355 template<class T> void operator delete(void*, const size_t, const int, T*);
356 };
357}
Douglas Gregor520441e2010-08-27 21:39:15 +0000358
359namespace DeleteParam {
360 struct X {
361 void operator delete(X*); // expected-error{{first parameter of 'operator delete' must have type 'void *'}}
362 };
363
364 struct Y {
365 void operator delete(void* const);
366 };
367}
John McCalledeb6c92010-09-14 21:34:24 +0000368
369// <rdar://problem/8427878>
370// Test that the correct 'operator delete' is selected to pair with
371// the unexpected placement 'operator new'.
372namespace PairedDelete {
373 template <class T> struct A {
374 A();
375 void *operator new(size_t s, double d = 0);
376 void operator delete(void *p, double d);
377 void operator delete(void *p) {
378 T::dealloc(p);
379 }
380 };
381
382 A<int> *test() {
383 return new A<int>();
384 }
385}
Nick Lewycky9fa8e562010-11-03 17:52:57 +0000386
387namespace PR7702 {
388 void test1() {
389 new DoesNotExist; // expected-error {{expected a type}}
390 }
391}
John McCall84ff0fc2011-07-13 20:12:57 +0000392
393namespace ArrayNewNeedsDtor {
394 struct A { A(); private: ~A(); }; // expected-note {{declared private here}}
395 struct B { B(); A a; }; // expected-error {{field of type 'ArrayNewNeedsDtor::A' has private destructor}}
396 B *test9() {
397 return new B[5]; // expected-note {{implicit default destructor for 'ArrayNewNeedsDtor::B' first required here}}
398 }
399}
Eli Friedmane52c9142011-07-26 22:25:31 +0000400
401namespace DeleteIncompleteClass {
402 struct A; // expected-note {{forward declaration}}
403 extern A x;
404 void f() { delete x; } // expected-error {{deleting incomplete class type}}
405}
406
407namespace DeleteIncompleteClassPointerError {
408 struct A; // expected-note {{forward declaration}}
409 void f(A *x) { 1+delete x; } // expected-warning {{deleting pointer to incomplete type}} \
410 // expected-error {{invalid operands to binary expression}}
411}
Eli Friedmane33f6432011-07-26 22:50:18 +0000412
413namespace PR10504 {
414 struct A {
415 virtual void foo() = 0;
416 };
Eli Friedmanef8c79c2011-07-26 23:27:24 +0000417 void f(A *x) { delete x; } // expected-warning {{delete called on 'PR10504::A' that is abstract but has non-virtual destructor}}
Eli Friedmane33f6432011-07-26 22:50:18 +0000418}