Richard Smith | 0b458fd | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu |
Douglas Gregor | c83ed04 | 2008-11-25 04:08:05 +0000 | [diff] [blame] | 2 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 3 | #include <stddef.h> |
| 4 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5 | struct S // expected-note {{candidate}} |
| 6 | { |
| 7 | S(int, int, double); // expected-note {{candidate}} |
Sebastian Redl | 3cb0692 | 2009-02-07 19:52:04 +0000 | [diff] [blame] | 8 | S(double, int); // expected-note 2 {{candidate}} |
| 9 | S(float, int); // expected-note 2 {{candidate}} |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 10 | }; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 11 | struct T; // expected-note{{forward declaration of 'T'}} |
Sebastian Redl | 636a7c4 | 2008-12-04 17:24:46 +0000 | [diff] [blame] | 12 | struct U |
| 13 | { |
| 14 | // A special new, to verify that the global version isn't used. |
Sebastian Redl | 9afe130 | 2009-05-14 18:11:41 +0000 | [diff] [blame] | 15 | void* operator new(size_t, S*); // expected-note {{candidate}} |
Sebastian Redl | 636a7c4 | 2008-12-04 17:24:46 +0000 | [diff] [blame] | 16 | }; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 17 | struct V : U |
| 18 | { |
| 19 | }; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 20 | |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 21 | // PR5823 |
| 22 | void* operator new(const size_t); // expected-note 2 {{candidate}} |
Sebastian Redl | 3cb0692 | 2009-02-07 19:52:04 +0000 | [diff] [blame] | 23 | void* operator new(size_t, int*); // expected-note 3 {{candidate}} |
| 24 | void* operator new(size_t, float*); // expected-note 3 {{candidate}} |
Anders Carlsson | fc27d26 | 2009-05-31 19:49:47 +0000 | [diff] [blame] | 25 | void* operator new(size_t, S); // expected-note 2 {{candidate}} |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 26 | |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 27 | struct foo { }; |
| 28 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 29 | void 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 Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 37 | ps = new (pf) (S)(1, 2, 3.4); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 38 | S *(*paps)[2] = new S*[*pi][2]; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 39 | typedef int ia4[4]; |
| 40 | ia4 *pai = new (int[3][4]); |
Sebastian Redl | fb4ccd7 | 2008-12-02 16:35:44 +0000 | [diff] [blame] | 41 | pi = ::new int; |
Sebastian Redl | 636a7c4 | 2008-12-04 17:24:46 +0000 | [diff] [blame] | 42 | U *pu = new (ps) U; |
Douglas Gregor | 5d64e5b | 2009-09-30 00:03:47 +0000 | [diff] [blame] | 43 | V *pv = new (ps) V; |
Anders Carlsson | fc27d26 | 2009-05-31 19:49:47 +0000 | [diff] [blame] | 44 | |
| 45 | pi = new (S(1.0f, 2)) int; |
Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 46 | |
| 47 | (void)new int[true]; |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 48 | |
| 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 Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 58 | struct abstract { |
| 59 | virtual ~abstract() = 0; |
| 60 | }; |
| 61 | |
Douglas Gregor | c83ed04 | 2008-11-25 04:08:05 +0000 | [diff] [blame] | 62 | void bad_news(int *ip) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 63 | { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 64 | int i = 1; // expected-note 2{{here}} |
Nick Lewycky | 9fa8e56 | 2010-11-03 17:52:57 +0000 | [diff] [blame] | 65 | (void)new; // expected-error {{expected a type}} |
| 66 | (void)new 4; // expected-error {{expected a type}} |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 67 | (void)new () int; // expected-error {{expected expression}} |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 68 | (void)new int[1.1]; // expected-error {{array size expression must have integral or enumeration type, not 'double'}} |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 69 | (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}} |
| 70 | (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}} |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 71 | (void)new (int[i]); // expected-warning {{when type is in parentheses}} |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 72 | (void)new int(*(S*)0); // expected-error {{no viable conversion from 'S' to 'int'}} |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 73 | (void)new int(1, 2); // expected-error {{excess elements in scalar initializer}} |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 74 | (void)new S(1); // expected-error {{no matching constructor}} |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 75 | (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}} |
Chris Lattner | 58f9e13 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 76 | (void)new const int; // expected-error {{default initialization of an object of const type 'const int'}} |
Anders Carlsson | a729bbb | 2010-01-23 05:47:27 +0000 | [diff] [blame] | 77 | (void)new float*(ip); // expected-error {{cannot initialize a new value of type 'float *' with an lvalue of type 'int *'}} |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 78 | // Undefined, but clang should reject it directly. |
| 79 | (void)new int[-1]; // expected-error {{array size is negative}} |
Richard Smith | 0b458fd | 2012-02-04 05:35:53 +0000 | [diff] [blame] | 80 | (void)new int[2000000000]; // expected-error {{array is too large}} |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 81 | (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumeration type, not 'S'}} |
Sebastian Redl | fb4ccd7 | 2008-12-02 16:35:44 +0000 | [diff] [blame] | 82 | (void)::S::new int; // expected-error {{expected unqualified-id}} |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 83 | (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}} |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 84 | (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}} |
Sebastian Redl | 636a7c4 | 2008-12-04 17:24:46 +0000 | [diff] [blame] | 85 | // This must fail, because the member version shouldn't be found. |
| 86 | (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}} |
Sebastian Redl | 9afe130 | 2009-05-14 18:11:41 +0000 | [diff] [blame] | 87 | // This must fail, because any member version hides all global versions. |
| 88 | (void)new U; // expected-error {{no matching function for call to 'operator new'}} |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 89 | (void)new (int[]); // expected-error {{array size must be specified in new expressions}} |
| 90 | (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}} |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 91 | // Some lacking cases due to lack of sema support. |
| 92 | } |
| 93 | |
| 94 | void good_deletes() |
| 95 | { |
| 96 | delete (int*)0; |
| 97 | delete [](int*)0; |
| 98 | delete (S*)0; |
Sebastian Redl | fb4ccd7 | 2008-12-02 16:35:44 +0000 | [diff] [blame] | 99 | ::delete (int*)0; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void bad_deletes() |
| 103 | { |
| 104 | delete 0; // expected-error {{cannot delete expression of type 'int'}} |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 105 | delete [0] (int*)0; // expected-error {{expected expression}} |
Douglas Gregor | 94a6157 | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 106 | delete (void*)0; // expected-warning {{cannot delete expression with pointer-to-'void' type 'void *'}} |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 107 | delete (T*)0; // expected-warning {{deleting pointer to incomplete type}} |
Sebastian Redl | fb4ccd7 | 2008-12-02 16:35:44 +0000 | [diff] [blame] | 108 | ::S::delete (int*)0; // expected-error {{expected unqualified-id}} |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 109 | } |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 110 | |
| 111 | struct X0 { }; |
| 112 | |
| 113 | struct X1 { |
| 114 | operator int*(); |
| 115 | operator float(); |
| 116 | }; |
| 117 | |
| 118 | struct X2 { |
Fariborz Jahanian | f652793 | 2009-09-15 17:21:47 +0000 | [diff] [blame] | 119 | operator int*(); // expected-note {{candidate function}} |
| 120 | operator float*(); // expected-note {{candidate function}} |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | void test_delete_conv(X0 x0, X1 x1, X2 x2) { |
| 124 | delete x0; // expected-error{{cannot delete}} |
| 125 | delete x1; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 126 | delete x2; // expected-error{{ambiguous conversion of delete expression of type 'X2' to a pointer}} |
Fariborz Jahanian | f652793 | 2009-09-15 17:21:47 +0000 | [diff] [blame] | 127 | } |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 128 | |
| 129 | // PR4782 |
| 130 | class X3 { |
| 131 | public: |
Eli Friedman | 5e6214b | 2009-11-16 05:14:40 +0000 | [diff] [blame] | 132 | static void operator delete(void * mem, size_t size); |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | class X4 { |
| 136 | public: |
| 137 | static void release(X3 *x); |
Eli Friedman | 5e6214b | 2009-11-16 05:14:40 +0000 | [diff] [blame] | 138 | static void operator delete(void * mem, size_t size); |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | |
| 142 | void X4::release(X3 *x) { |
| 143 | delete x; |
| 144 | } |
Douglas Gregor | 1070c9f | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 145 | |
Douglas Gregor | 5d64e5b | 2009-09-30 00:03:47 +0000 | [diff] [blame] | 146 | class X5 { |
Douglas Gregor | 1070c9f | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 147 | public: |
| 148 | void Destroy() const { delete this; } |
| 149 | }; |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 150 | |
| 151 | class Base { |
| 152 | public: |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 153 | 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 Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | class Tier {}; |
| 158 | class Comp : public Tier {}; |
| 159 | |
| 160 | class Thai : public Base { |
| 161 | public: |
| 162 | Thai(const Tier *adoptDictionary); |
| 163 | }; |
| 164 | |
| 165 | void loadEngineFor() { |
| 166 | const Comp *dict; |
| 167 | new Thai(dict); |
| 168 | } |
| 169 | |
| 170 | template <class T> struct TBase { |
Douglas Gregor | 2fa9800 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 171 | void* operator new(T size, int); // expected-error {{'operator new' cannot take a dependent type as first parameter; use size_t}} |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
Douglas Gregor | 2fa9800 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 174 | TBase<int> t1; |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 175 | |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 176 | class X6 { |
| 177 | public: |
| 178 | static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}} |
| 179 | }; |
| 180 | |
| 181 | class X7 { |
| 182 | public: |
| 183 | static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}} |
| 184 | }; |
| 185 | |
| 186 | class X8 : public X6, public X7 { |
| 187 | }; |
| 188 | |
Anders Carlsson | 5072430 | 2009-11-15 16:43:15 +0000 | [diff] [blame] | 189 | void f(X8 *x8) { |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 190 | delete x8; // expected-error {{member 'operator delete' found in multiple base classes of different types}} |
| 191 | } |
| 192 | |
Anders Carlsson | 5072430 | 2009-11-15 16:43:15 +0000 | [diff] [blame] | 193 | class X9 { |
John McCall | 7002f4c | 2010-04-09 19:03:51 +0000 | [diff] [blame] | 194 | public: |
Anders Carlsson | 5072430 | 2009-11-15 16:43:15 +0000 | [diff] [blame] | 195 | 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 | |
| 199 | void f(X9 *x9) { |
| 200 | delete x9; // expected-error {{no suitable member 'operator delete' in 'X9'}} |
| 201 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 202 | |
| 203 | struct X10 { |
| 204 | virtual ~X10(); |
| 205 | }; |
| 206 | |
| 207 | struct 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 | |
| 211 | void f() { |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 212 | X11 x11; // expected-note {{implicit default destructor for 'X11' first required here}} |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 213 | } |
Anders Carlsson | eac8139 | 2009-12-09 07:39:44 +0000 | [diff] [blame] | 214 | |
| 215 | struct X12 { |
| 216 | void* operator new(size_t, void*); |
| 217 | }; |
| 218 | |
| 219 | struct X13 : X12 { |
| 220 | using X12::operator new; |
| 221 | }; |
| 222 | |
| 223 | static void* f(void* g) |
| 224 | { |
| 225 | return new (g) X13(); |
| 226 | } |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 227 | |
Chandler Carruth | e228ba9 | 2010-02-08 18:54:05 +0000 | [diff] [blame] | 228 | class X14 { |
John McCall | 7002f4c | 2010-04-09 19:03:51 +0000 | [diff] [blame] | 229 | public: |
Chandler Carruth | e228ba9 | 2010-02-08 18:54:05 +0000 | [diff] [blame] | 230 | static void operator delete(void*, const size_t); |
| 231 | }; |
| 232 | |
| 233 | void f(X14 *x14a, X14 *x14b) { |
| 234 | delete x14a; |
| 235 | } |
| 236 | |
Douglas Gregor | d880f52 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 237 | class X15 { |
| 238 | private: |
| 239 | X15(); // expected-note {{declared private here}} |
| 240 | ~X15(); // expected-note {{declared private here}} |
| 241 | }; |
| 242 | |
| 243 | void f(X15* x) { |
| 244 | new X15(); // expected-error {{calling a private constructor}} |
| 245 | delete x; // expected-error {{calling a private destructor}} |
| 246 | } |
| 247 | |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 248 | namespace 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 Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 254 | |
| 255 | namespace Test1 { |
| 256 | |
| 257 | void f() { |
| 258 | (void)new int[10](1, 2); // expected-error {{array 'new' cannot have initialization arguments}} |
Anders Carlsson | 55cbd6e | 2010-05-16 16:24:20 +0000 | [diff] [blame] | 259 | |
| 260 | typedef int T[10]; |
| 261 | (void)new T(1, 2); // expected-error {{array 'new' cannot have initialization arguments}} |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | template<typename T> |
| 265 | void g(unsigned i) { |
| 266 | (void)new T[1](i); // expected-error {{array 'new' cannot have initialization arguments}} |
| 267 | } |
| 268 | |
| 269 | template<typename T> |
| 270 | void h(unsigned i) { |
| 271 | (void)new T(i); // expected-error {{array 'new' cannot have initialization arguments}} |
| 272 | } |
| 273 | template void h<unsigned>(unsigned); |
| 274 | template void h<unsigned[10]>(unsigned); // expected-note {{in instantiation of function template specialization 'Test1::h<unsigned int [10]>' requested here}} |
| 275 | |
| 276 | } |
Chandler Carruth | 2389324 | 2010-06-28 00:30:51 +0000 | [diff] [blame] | 277 | |
| 278 | // Don't diagnose access for overload candidates that aren't selected. |
| 279 | namespace PR7436 { |
| 280 | struct S1 { |
| 281 | void* operator new(size_t); |
| 282 | void operator delete(void* p); |
| 283 | |
| 284 | private: |
| 285 | void* operator new(size_t, void*); // expected-note {{declared private here}} |
| 286 | void operator delete(void*, void*); |
| 287 | }; |
| 288 | class 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 | |
| 293 | void 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 Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 301 | |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 302 | namespace 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 Gregor | 254a942 | 2010-07-29 14:44:35 +0000 | [diff] [blame] | 324 | |
| 325 | // <rdar://problem/8248780> |
| 326 | namespace Instantiate { |
| 327 | template<typename T> struct X { |
| 328 | operator T*(); |
| 329 | }; |
| 330 | |
| 331 | void f(X<int> &xi) { |
| 332 | delete xi; |
| 333 | } |
| 334 | } |
Sebastian Redl | 58e5539 | 2010-08-05 00:45:34 +0000 | [diff] [blame] | 335 | |
| 336 | namespace 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 Carruth | 09556fd | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 346 | |
| 347 | // Don't crash on template delete operators |
| 348 | namespace 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 Gregor | 520441e | 2010-08-27 21:39:15 +0000 | [diff] [blame] | 358 | |
| 359 | namespace 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 McCall | edeb6c9 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 368 | |
| 369 | // <rdar://problem/8427878> |
| 370 | // Test that the correct 'operator delete' is selected to pair with |
| 371 | // the unexpected placement 'operator new'. |
| 372 | namespace 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 Lewycky | 9fa8e56 | 2010-11-03 17:52:57 +0000 | [diff] [blame] | 386 | |
| 387 | namespace PR7702 { |
| 388 | void test1() { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 389 | new DoesNotExist; // expected-error {{unknown type name 'DoesNotExist'}} |
Nick Lewycky | 9fa8e56 | 2010-11-03 17:52:57 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
John McCall | 84ff0fc | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 392 | |
| 393 | namespace 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 Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 400 | |
| 401 | namespace 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 | |
| 407 | namespace 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 Friedman | e33f643 | 2011-07-26 22:50:18 +0000 | [diff] [blame] | 412 | |
| 413 | namespace PR10504 { |
| 414 | struct A { |
| 415 | virtual void foo() = 0; |
| 416 | }; |
Eli Friedman | ef8c79c | 2011-07-26 23:27:24 +0000 | [diff] [blame] | 417 | void f(A *x) { delete x; } // expected-warning {{delete called on 'PR10504::A' that is abstract but has non-virtual destructor}} |
Eli Friedman | e33f643 | 2011-07-26 22:50:18 +0000 | [diff] [blame] | 418 | } |
Sebastian Redl | 2aed8b8 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 419 | |
| 420 | struct PlacementArg {}; |
| 421 | inline void *operator new[](size_t, const PlacementArg &) throw () { |
| 422 | return 0; |
| 423 | } |
| 424 | inline void operator delete[](void *, const PlacementArg &) throw () { |
| 425 | } |
| 426 | |
| 427 | namespace r150682 { |
| 428 | |
| 429 | template <typename X> |
| 430 | struct S { |
| 431 | struct Inner {}; |
| 432 | S() { new Inner[1]; } |
| 433 | }; |
| 434 | |
| 435 | struct T { |
| 436 | }; |
| 437 | |
| 438 | template<typename X> |
| 439 | void tfn() { |
| 440 | new (*(PlacementArg*)0) T[1]; |
| 441 | } |
| 442 | |
| 443 | void fn() { |
| 444 | tfn<int>(); |
| 445 | } |
| 446 | |
| 447 | } |
Sebastian Redl | 1f27805 | 2012-02-17 08:42:32 +0000 | [diff] [blame] | 448 | |
| 449 | namespace P12023 { |
| 450 | struct CopyCounter |
| 451 | { |
| 452 | CopyCounter(); |
| 453 | CopyCounter(const CopyCounter&); |
| 454 | }; |
| 455 | |
| 456 | int main() |
| 457 | { |
| 458 | CopyCounter* f = new CopyCounter[10](CopyCounter()); // expected-error {{cannot have initialization arguments}} |
| 459 | return 0; |
| 460 | } |
| 461 | } |
Sebastian Redl | 428c620 | 2012-02-22 09:07:21 +0000 | [diff] [blame] | 462 | |
| 463 | namespace PR12061 { |
| 464 | template <class C> struct scoped_array { |
| 465 | scoped_array(C* p = __null); |
| 466 | }; |
| 467 | template <class Payload> struct Foo { |
| 468 | Foo() : a_(new scoped_array<int>[5]) { } |
| 469 | scoped_array< scoped_array<int> > a_; |
| 470 | }; |
| 471 | class Bar {}; |
| 472 | Foo<Bar> x; |
| 473 | |
| 474 | template <class C> struct scoped_array2 { |
| 475 | scoped_array2(C* p = __null, C* q = __null); |
| 476 | }; |
| 477 | template <class Payload> struct Foo2 { |
| 478 | Foo2() : a_(new scoped_array2<int>[5]) { } |
| 479 | scoped_array2< scoped_array2<int> > a_; |
| 480 | }; |
| 481 | class Bar2 {}; |
| 482 | Foo2<Bar2> x2; |
| 483 | |
| 484 | class MessageLoop { |
| 485 | public: |
| 486 | explicit MessageLoop(int type = 0); |
| 487 | }; |
| 488 | template <class CookieStoreTestTraits> |
| 489 | class CookieStoreTest { |
| 490 | protected: |
| 491 | CookieStoreTest() { |
| 492 | new MessageLoop; |
| 493 | } |
| 494 | }; |
| 495 | struct CookieMonsterTestTraits { |
| 496 | }; |
| 497 | class DeferredCookieTaskTest : public CookieStoreTest<CookieMonsterTestTraits> |
| 498 | { |
| 499 | DeferredCookieTaskTest() {} |
| 500 | }; |
| 501 | } |
Eli Friedman | 206491d | 2012-12-13 00:37:17 +0000 | [diff] [blame] | 502 | |
| 503 | class DeletingPlaceholder { |
| 504 | int* f() { |
| 505 | delete f; // expected-error {{reference to non-static member function must be called; did you mean to call it with no arguments?}} |
| 506 | return 0; |
| 507 | } |
| 508 | int* g(int, int) { |
| 509 | delete g; // expected-error {{reference to non-static member function must be called}} |
| 510 | return 0; |
| 511 | } |
| 512 | }; |