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