1 // RUN: %clang_cc1 -std=c++11 -triple %itanium_abi_triple -fsyntax-only -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -fcxx-exceptions -verify %s 2 // RUN: %clang_cc1 -std=c++11 -triple %ms_abi_triple -DMSABI -fsyntax-only -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -verify %s 3 4 #if defined(BE_THE_HEADER) 5 6 // Wdelete-non-virtual-dtor should warn about the delete from smart pointer 7 // classes in system headers (std::unique_ptr...) too. 8 9 #pragma clang system_header 10 namespace dnvd { 11 template <typename T> 12 class simple_ptr { 13 public: 14 simple_ptr(T* t): _ptr(t) {} 15 ~simple_ptr() { delete _ptr; } // \ 16 // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}} \ 17 // expected-warning {{delete called on non-final 'dnvd::D' that has virtual functions but non-virtual destructor}} 18 T& operator*() const { return *_ptr; } 19 private: 20 T* _ptr; 21 }; 22 } 23 24 #else 25 26 #define BE_THE_HEADER 27 #include __FILE__ 28 29 class A { 30 public: 31 ~A(); 32 }; 33 34 class B { 35 public: 36 ~B() { } 37 }; 38 39 class C { 40 public: 41 (~C)() { } 42 }; 43 44 struct D { 45 static void ~D(int, ...) const { } // \ 46 // expected-error{{static member function cannot have 'const' qualifier}} \ 47 // expected-error{{destructor cannot be declared 'static'}} \ 48 // expected-error{{destructor cannot have any parameters}} \ 49 // expected-error{{destructor cannot be variadic}} \ 50 // expected-error{{destructor cannot have a return type}} \ 51 // expected-error{{'const' qualifier is not allowed on a destructor}} 52 }; 53 54 struct D2 { 55 void ~D2() { } // \ 56 // expected-error{{destructor cannot have a return type}} 57 }; 58 59 60 struct E; 61 62 typedef E E_typedef; 63 struct E { 64 ~E_typedef(); // expected-error{{destructor cannot be declared using a typedef 'E_typedef' (aka 'E') of the class name}} 65 }; 66 67 struct F { 68 (~F)(); // expected-note {{previous declaration is here}} 69 ~F(); // expected-error {{destructor cannot be redeclared}} 70 }; 71 72 ~; // expected-error {{expected a class name after '~' to name a destructor}} 73 ~undef(); // expected-error {{expected the class name after '~' to name a destructor}} 74 ~operator+(int, int); // expected-error {{expected a class name after '~' to name a destructor}} 75 ~F(){} // expected-error {{destructor must be a non-static member function}} 76 77 struct G { 78 ~G(); 79 }; 80 81 G::~G() { } 82 83 // <rdar://problem/6841210> 84 struct H { 85 ~H(void) { } 86 }; 87 88 struct X {}; 89 90 struct Y { 91 ~X(); // expected-error {{expected the class name after '~' to name the enclosing class}} 92 }; 93 94 namespace PR6421 { 95 class T; // expected-note{{forward declaration}} 96 97 class QGenericArgument // expected-note{{declared here}} 98 { 99 template<typename U> 100 void foo(T t) // expected-error{{variable has incomplete type}} 101 { } 102 103 void disconnect() 104 { 105 T* t; 106 bob<QGenericArgument>(t); // expected-error{{undeclared identifier 'bob'}} \ 107 // expected-error{{does not refer to a value}} 108 } 109 }; 110 } 111 112 namespace PR6709 { 113 #ifdef MSABI 114 // This bug, "Clang instantiates destructor for function argument" is intended 115 // behaviour in the Microsoft ABI because the callee needs to destruct the arguments. 116 // expected-error@+3 {{indirection requires pointer operand ('int' invalid)}} 117 // expected-note@+3 {{in instantiation of member function 'PR6709::X<int>::~X' requested here}} 118 #endif 119 template<class T> class X { T v; ~X() { ++*v; } }; 120 void a(X<int> x) {} 121 } 122 123 struct X0 { virtual ~X0() throw(); }; 124 struct X1 : public X0 { }; 125 126 // Make sure we instantiate operator deletes when building a virtual 127 // destructor. 128 namespace test6 { 129 template <class T> class A { 130 public: 131 void *operator new(__SIZE_TYPE__); 132 void operator delete(void *p) { 133 T::deleteIt(p); // expected-error {{type 'int' cannot be used prior to '::'}} 134 } 135 136 #ifdef MSABI 137 // expected-note@+2 {{in instantiation of member function 'test6::A<int>::operator delete' requested here}} 138 #endif 139 virtual ~A() {} 140 }; 141 142 #ifndef MSABI 143 // expected-note@+2 {{in instantiation of member function 'test6::A<int>::operator delete' requested here}} 144 #endif 145 class B : A<int> { B(); }; 146 B::B() {} 147 } 148 149 // Make sure classes are marked invalid when they have invalid 150 // members. This avoids a crash-on-invalid. 151 namespace test7 { 152 struct A { 153 ~A() const; // expected-error {{'const' qualifier is not allowed on a destructor}} 154 }; 155 struct B : A {}; 156 157 void test() { 158 B *b; 159 b->~B(); 160 } 161 } 162 163 namespace nonvirtualdtor { 164 struct S1 { // expected-warning {{has virtual functions but non-virtual destructor}} 165 virtual void m(); 166 }; 167 168 struct S2 { 169 ~S2(); // expected-warning {{has virtual functions but non-virtual destructor}} 170 virtual void m(); 171 }; 172 173 struct S3 : public S1 { // expected-warning {{has virtual functions but non-virtual destructor}} 174 virtual void m(); 175 }; 176 177 struct S4 : public S2 { // expected-warning {{has virtual functions but non-virtual destructor}} 178 virtual void m(); 179 }; 180 181 struct B { 182 virtual ~B(); 183 virtual void m(); 184 }; 185 186 struct S5 : public B { 187 virtual void m(); 188 }; 189 190 struct S6 { 191 virtual void m(); 192 private: 193 ~S6(); 194 }; 195 196 struct S7 { 197 virtual void m(); 198 protected: 199 ~S7(); 200 }; 201 202 struct S8 {} s8; 203 204 UnknownType S8::~S8() { // expected-error {{unknown type name 'UnknownType'}} 205 s8.~S8(); 206 } 207 208 template<class T> class TS : public B { 209 virtual void m(); 210 }; 211 212 TS<int> baz; 213 214 template<class T> class TS2 { // expected-warning {{'nonvirtualdtor::TS2<int>' has virtual functions but non-virtual destructor}} 215 virtual void m(); 216 }; 217 218 TS2<int> foo; // expected-note {{instantiation}} 219 } 220 221 namespace dnvd { // delete-non-virtual-dtor warning 222 struct NP {}; 223 224 struct B { // expected-warning {{has virtual functions but non-virtual destructor}} 225 virtual void foo(); 226 }; 227 228 struct D: B {}; // expected-warning {{has virtual functions but non-virtual destructor}} 229 230 struct F final : B {}; 231 232 struct VB { 233 virtual void foo(); 234 virtual ~VB(); 235 }; 236 237 struct VD: VB {}; 238 239 struct VF final: VB {}; 240 241 template <typename T> 242 class simple_ptr2 { 243 public: 244 simple_ptr2(T* t): _ptr(t) {} 245 ~simple_ptr2() { delete _ptr; } // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}} 246 T& operator*() const { return *_ptr; } 247 private: 248 T* _ptr; 249 }; 250 251 void use(B&); 252 void use(VB&); 253 254 void nowarnstack() { 255 B b; use(b); 256 D d; use(d); 257 F f; use(f); 258 VB vb; use(vb); 259 VD vd; use(vd); 260 VF vf; use(vf); 261 } 262 263 void nowarnnonpoly() { 264 { 265 NP* np = new NP(); 266 delete np; 267 } 268 { 269 NP* np = new NP[4]; 270 delete[] np; 271 } 272 } 273 274 // FIXME: Why are these supposed to not warn? 275 void nowarnarray() { 276 { 277 B* b = new B[4]; 278 delete[] b; 279 } 280 { 281 D* d = new D[4]; 282 delete[] d; 283 } 284 { 285 VB* vb = new VB[4]; 286 delete[] vb; 287 } 288 { 289 VD* vd = new VD[4]; 290 delete[] vd; 291 } 292 } 293 294 template <typename T> 295 void nowarntemplate() { 296 { 297 T* t = new T(); 298 delete t; 299 } 300 { 301 T* t = new T[4]; 302 delete[] t; 303 } 304 } 305 306 void nowarn0() { 307 { 308 F* f = new F(); 309 delete f; 310 } 311 { 312 VB* vb = new VB(); 313 delete vb; 314 } 315 { 316 VB* vb = new VD(); 317 delete vb; 318 } 319 { 320 VD* vd = new VD(); 321 delete vd; 322 } 323 { 324 VF* vf = new VF(); 325 delete vf; 326 } 327 } 328 329 void nowarn0_explicit_dtor(F* f, VB* vb, VD* vd, VF* vf) { 330 f->~F(); 331 f->~F(); 332 vb->~VB(); 333 vd->~VD(); 334 vf->~VF(); 335 } 336 337 void warn0() { 338 { 339 B* b = new B(); 340 delete b; // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}} 341 } 342 { 343 B* b = new D(); 344 delete b; // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}} 345 } 346 { 347 D* d = new D(); 348 delete d; // expected-warning {{delete called on non-final 'dnvd::D' that has virtual functions but non-virtual destructor}} 349 } 350 } 351 352 // Taken from libc++, slightly simplified. 353 template <class> 354 struct __is_destructible_apply { typedef int type; }; 355 struct __two {char __lx[2];}; 356 template <typename _Tp> 357 struct __is_destructor_wellformed { 358 template <typename _Tp1> 359 static char __test(typename __is_destructible_apply< 360 decltype(_Tp1().~_Tp1())>::type); 361 template <typename _Tp1> 362 static __two __test (...); 363 364 static const bool value = sizeof(__test<_Tp>(12)) == sizeof(char); 365 }; 366 367 void warn0_explicit_dtor(B* b, B& br, D* d) { 368 b->~B(); // expected-warning {{destructor called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}} expected-note{{qualify call to silence this warning}} 369 b->B::~B(); // No warning when the call isn't virtual. 370 371 // No warning in unevaluated contexts. 372 (void)__is_destructor_wellformed<B>::value; 373 374 br.~B(); // expected-warning {{destructor called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}} expected-note{{qualify call to silence this warning}} 375 br.B::~B(); 376 377 d->~D(); // expected-warning {{destructor called on non-final 'dnvd::D' that has virtual functions but non-virtual destructor}} expected-note{{qualify call to silence this warning}} 378 d->D::~D(); 379 } 380 381 void nowarn1() { 382 { 383 simple_ptr<F> f(new F()); 384 use(*f); 385 } 386 { 387 simple_ptr<VB> vb(new VB()); 388 use(*vb); 389 } 390 { 391 simple_ptr<VB> vb(new VD()); 392 use(*vb); 393 } 394 { 395 simple_ptr<VD> vd(new VD()); 396 use(*vd); 397 } 398 { 399 simple_ptr<VF> vf(new VF()); 400 use(*vf); 401 } 402 } 403 404 void warn1() { 405 { 406 simple_ptr<B> b(new B()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr<dnvd::B>::~simple_ptr' requested here}} 407 use(*b); 408 } 409 { 410 simple_ptr2<B> b(new D()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr2<dnvd::B>::~simple_ptr2' requested here}} 411 use(*b); 412 } 413 { 414 simple_ptr<D> d(new D()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr<dnvd::D>::~simple_ptr' requested here}} 415 use(*d); 416 } 417 } 418 } 419 420 namespace PR9238 { 421 class B { public: ~B(); }; 422 class C : virtual B { public: ~C() { } }; 423 } 424 425 namespace PR7900 { 426 struct A { // expected-note 2{{type 'PR7900::A' is declared here}} 427 }; 428 struct B : public A { 429 }; 430 void foo() { 431 B b; 432 b.~B(); 433 b.~A(); // expected-error{{destructor type 'PR7900::A' in object destruction expression does not match the type 'PR7900::B' of the object being destroyed}} 434 (&b)->~A(); // expected-error{{destructor type 'PR7900::A' in object destruction expression does not match the type 'PR7900::B' of the object being destroyed}} 435 } 436 } 437 438 namespace PR16892 { 439 auto p = &A::~A; // expected-error{{taking the address of a destructor}} 440 } 441 442 namespace PR20238 { 443 struct S { 444 volatile ~S() { } // expected-error{{destructor cannot have a return type}} 445 }; 446 } 447 448 namespace PR22668 { 449 struct S { 450 }; 451 void f(S s) { 452 (s.~S)(); 453 } 454 void g(S s) { 455 (s.~S); // expected-error{{reference to destructor must be called}} 456 } 457 } 458 459 class Invalid { 460 ~Invalid(); 461 UnknownType xx; // expected-error{{unknown type name}} 462 }; 463 464 // The constructor definition should not have errors 465 Invalid::~Invalid() {} 466 467 namespace PR30361 { 468 template <typename T> 469 struct C1 { 470 ~C1() {} 471 operator C1<T>* () { return nullptr; } 472 void foo1(); 473 }; 474 475 template<typename T> 476 void C1<T>::foo1() { 477 C1::operator C1<T>*(); 478 C1::~C1(); 479 } 480 481 void foo1() { 482 C1<int> x; 483 x.foo1(); 484 } 485 } 486 #endif // BE_THE_HEADER 487