new-delete.cpp revision 1f2fcee89677d597f2a10b54d6cefac0e79db504
1// RUN: clang-cc -fsyntax-only -verify %s 2 3#include <stddef.h> 4 5struct 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}; 11struct T; // expected-note{{forward declaration of 'struct T'}} 12struct 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}; 17struct V : U 18{ 19}; 20 21void* operator new(size_t); // expected-note 2 {{candidate}} 22void* operator new(size_t, int*); // expected-note 3 {{candidate}} 23void* operator new(size_t, float*); // expected-note 3 {{candidate}} 24void* operator new(size_t, S); // expected-note 2 {{candidate}} 25 26void good_news() 27{ 28 int *pi = new int; 29 float *pf = new (pi) float(); 30 pi = new int(1); 31 pi = new int('c'); 32 const int *pci = new const int(); 33 S *ps = new S(1, 2, 3.4); 34 ps = new (pf) (S)(1, 2, 3.4); 35 S *(*paps)[2] = new S*[*pi][2]; 36 ps = new (S[3])(1, 2, 3.4); 37 typedef int ia4[4]; 38 ia4 *pai = new (int[3][4]); 39 pi = ::new int; 40 U *pu = new (ps) U; 41 V *pv = new (ps) V; 42 43 pi = new (S(1.0f, 2)) int; 44 45 (void)new int[true]; 46} 47 48struct abstract { 49 virtual ~abstract() = 0; 50}; 51 52void bad_news(int *ip) 53{ 54 int i = 1; 55 (void)new; // expected-error {{missing type specifier}} 56 (void)new 4; // expected-error {{missing type specifier}} 57 (void)new () int; // expected-error {{expected expression}} 58 (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}} 59 (void)new int[1][i]; // expected-error {{only the first dimension}} 60 (void)new (int[1][i]); // expected-error {{only the first dimension}} 61 (void)new (int[i]); // expected-error {{when type is in parentheses}} 62 (void)new int(*(S*)0); // expected-error {{incompatible type initializing}} 63 (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}} 64 (void)new S(1); // expected-error {{no matching constructor}} 65 (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}} 66 (void)new const int; // expected-error {{must provide an initializer}} 67 (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}} 68 // Undefined, but clang should reject it directly. 69 (void)new int[-1]; // expected-error {{array size is negative}} 70 (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}} 71 (void)::S::new int; // expected-error {{expected unqualified-id}} 72 (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}} 73 (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}} 74 // This must fail, because the member version shouldn't be found. 75 (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}} 76 // This must fail, because any member version hides all global versions. 77 (void)new U; // expected-error {{no matching function for call to 'operator new'}} 78 (void)new (int[]); // expected-error {{array size must be specified in new expressions}} 79 (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}} 80 // Some lacking cases due to lack of sema support. 81} 82 83void good_deletes() 84{ 85 delete (int*)0; 86 delete [](int*)0; 87 delete (S*)0; 88 ::delete (int*)0; 89} 90 91void bad_deletes() 92{ 93 delete 0; // expected-error {{cannot delete expression of type 'int'}} 94 delete [0] (int*)0; // expected-error {{expected ']'}} \ 95 // expected-note {{to match this '['}} 96 delete (void*)0; // expected-error {{cannot delete expression}} 97 delete (T*)0; // expected-warning {{deleting pointer to incomplete type}} 98 ::S::delete (int*)0; // expected-error {{expected unqualified-id}} 99} 100 101struct X0 { }; 102 103struct X1 { 104 operator int*(); 105 operator float(); 106}; 107 108struct X2 { 109 operator int*(); // expected-note {{candidate function}} 110 operator float*(); // expected-note {{candidate function}} 111}; 112 113void test_delete_conv(X0 x0, X1 x1, X2 x2) { 114 delete x0; // expected-error{{cannot delete}} 115 delete x1; 116 delete x2; // expected-error{{ambiguous conversion of delete expression of type 'struct X2' to a pointer}} 117} 118 119// PR4782 120class X3 { 121public: 122 static void operator delete(void * mem, unsigned long size); 123}; 124 125class X4 { 126public: 127 static void release(X3 *x); 128 static void operator delete(void * mem, unsigned long size); 129}; 130 131 132void X4::release(X3 *x) { 133 delete x; 134} 135 136class X5 { 137public: 138 void Destroy() const { delete this; } 139}; 140 141class Base { 142public: 143 static int operator new(signed char) throw(); // expected-error {{'operator new' takes type size_t}} \ 144 // expected-error {{operator new' must return type 'void *'}} 145 static int operator new[] (signed char) throw(); // expected-error {{'operator new[]' takes type size_t}} \ 146 // expected-error {{operator new[]' must return type 'void *'}} 147}; 148 149class Tier {}; 150class Comp : public Tier {}; 151 152class Thai : public Base { 153public: 154 Thai(const Tier *adoptDictionary); 155}; 156 157void loadEngineFor() { 158 const Comp *dict; 159 new Thai(dict); 160} 161 162template <class T> struct TBase { 163 void* operator new(T size, int); // expected-error {{'operator new' takes type size_t}} 164}; 165 166TBase<int> t1; // expected-note {{in instantiation of template class 'struct TBase<int>' requested here}} 167 168