1 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu 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 // PR5823 22 void* operator new(const size_t); // expected-note 2 {{candidate}} 23 void* operator new(size_t, int*); // expected-note 3 {{candidate}} 24 void* operator new(size_t, float*); // expected-note 3 {{candidate}} 25 void* operator new(size_t, S); // expected-note 2 {{candidate}} 26 27 struct foo { }; 28 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); 37 ps = new (pf) (S)(1, 2, 3.4); 38 S *(*paps)[2] = new S*[*pi][2]; 39 typedef int ia4[4]; 40 ia4 *pai = new (int[3][4]); 41 pi = ::new int; 42 U *pu = new (ps) U; 43 V *pv = new (ps) V; 44 45 pi = new (S(1.0f, 2)) int; 46 47 (void)new int[true]; 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; 56 } 57 58 struct abstract { 59 virtual ~abstract() = 0; 60 }; 61 62 void bad_news(int *ip) 63 { 64 int i = 1; // expected-note 2{{here}} 65 (void)new; // expected-error {{expected a type}} 66 (void)new 4; // expected-error {{expected a type}} 67 (void)new () int; // expected-error {{expected expression}} 68 (void)new int[1.1]; // expected-error {{array size expression must have integral or enumeration type, not 'double'}} 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}} 71 (void)new (int[i]); // expected-warning {{when type is in parentheses}} 72 (void)new int(*(S*)0); // expected-error {{no viable conversion from 'S' to 'int'}} 73 (void)new int(1, 2); // expected-error {{excess elements in scalar initializer}} 74 (void)new S(1); // expected-error {{no matching constructor}} 75 (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}} 76 (void)new const int; // expected-error {{default initialization of an object of const type 'const int'}} 77 (void)new float*(ip); // expected-error {{cannot initialize a new value of type 'float *' with an lvalue of type 'int *'}} 78 // Undefined, but clang should reject it directly. 79 (void)new int[-1]; // expected-error {{array size is negative}} 80 (void)new int[2000000000]; // expected-error {{array is too large}} 81 (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumeration type, not 'S'}} 82 (void)::S::new int; // expected-error {{expected unqualified-id}} 83 (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}} 84 (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}} 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'}} 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'}} 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}} 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; 99 ::delete (int*)0; 100 } 101 102 void bad_deletes() 103 { 104 delete 0; // expected-error {{cannot delete expression of type 'int'}} 105 delete [0] (int*)0; // expected-error {{expected expression}} 106 delete (void*)0; // expected-warning {{cannot delete expression with pointer-to-'void' type 'void *'}} 107 delete (T*)0; // expected-warning {{deleting pointer to incomplete type}} 108 ::S::delete (int*)0; // expected-error {{expected unqualified-id}} 109 } 110 111 struct X0 { }; 112 113 struct X1 { 114 operator int*(); 115 operator float(); 116 }; 117 118 struct X2 { 119 operator int*(); // expected-note {{candidate function}} 120 operator float*(); // expected-note {{candidate function}} 121 }; 122 123 void test_delete_conv(X0 x0, X1 x1, X2 x2) { 124 delete x0; // expected-error{{cannot delete}} 125 delete x1; 126 delete x2; // expected-error{{ambiguous conversion of delete expression of type 'X2' to a pointer}} 127 } 128 129 // PR4782 130 class X3 { 131 public: 132 static void operator delete(void * mem, size_t size); 133 }; 134 135 class X4 { 136 public: 137 static void release(X3 *x); 138 static void operator delete(void * mem, size_t size); 139 }; 140 141 142 void X4::release(X3 *x) { 143 delete x; 144 } 145 146 class X5 { 147 public: 148 void Destroy() const { delete this; } 149 }; 150 151 class Base { 152 public: 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 *'}} 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 { 171 void* operator new(T size, int); // expected-error {{'operator new' cannot take a dependent type as first parameter; use size_t}} 172 }; 173 174 TBase<int> t1; 175 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 189 void f(X8 *x8) { 190 delete x8; // expected-error {{member 'operator delete' found in multiple base classes of different types}} 191 } 192 193 class X9 { 194 public: 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 } 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() { 212 X11 x11; // expected-note {{implicit default destructor for 'X11' first required here}} 213 } 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 } 227 228 class X14 { 229 public: 230 static void operator delete(void*, const size_t); 231 }; 232 233 void f(X14 *x14a, X14 *x14b) { 234 delete x14a; 235 } 236 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 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 } 254 255 namespace Test1 { 256 257 void f() { 258 (void)new int[10](1, 2); // expected-error {{array 'new' cannot have initialization arguments}} 259 260 typedef int T[10]; 261 (void)new T(1, 2); // expected-error {{array 'new' cannot have initialization arguments}} 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 } 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 } 301 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 } 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 } 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 } 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 } 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 } 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 } 386 387 namespace PR7702 { 388 void test1() { 389 new DoesNotExist; // expected-error {{unknown type name 'DoesNotExist'}} 390 } 391 } 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 } 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 } 412 413 namespace PR10504 { 414 struct A { 415 virtual void foo() = 0; 416 }; 417 void f(A *x) { delete x; } // expected-warning {{delete called on 'PR10504::A' that is abstract but has non-virtual destructor}} 418 } 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 } 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 } 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 } 502