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