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