[lld][WebAssembly] Reinstate mistakenly disabled test. NFC
[llvm-project.git] / clang / test / SemaCXX / destructor.cpp
blobbc0048621f7663cad1c37d62e0f0442cf5253627
1 // RUN: %clang_cc1 -std=c++11 -triple %itanium_abi_triple -fsyntax-only -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -fcxx-exceptions -verify %s -pedantic
2 // RUN: %clang_cc1 -std=c++11 -triple %ms_abi_triple -DMSABI -fsyntax-only -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -verify %s -pedantic
4 #if defined(BE_THE_HEADER)
6 // Wdelete-non-virtual-dtor should warn about the delete from smart pointer
7 // classes in system headers (std::unique_ptr...) too.
9 #pragma clang system_header
10 namespace dnvd {
12 struct SystemB {
13 virtual void foo();
16 template <typename T>
17 class simple_ptr {
18 public:
19 simple_ptr(T* t): _ptr(t) {}
20 ~simple_ptr() { delete _ptr; } // \
21 // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}} \
22 // expected-warning {{delete called on non-final 'dnvd::D' that has virtual functions but non-virtual destructor}}
23 T& operator*() const { return *_ptr; }
24 private:
25 T* _ptr;
29 #else
31 #define BE_THE_HEADER
32 #include __FILE__
34 class A {
35 public:
36 ~A();
39 class B {
40 public:
41 ~B() { }
44 class C {
45 public:
46 (~C)() { }
49 struct D {
50 static void ~D(int, ...) const { } // \
51 // expected-error{{static member function cannot have 'const' qualifier}} \
52 // expected-error{{destructor cannot be declared 'static'}} \
53 // expected-error{{destructor cannot have any parameters}} \
54 // expected-error{{destructor cannot be variadic}} \
55 // expected-error{{destructor cannot have a return type}} \
56 // expected-error{{'const' qualifier is not allowed on a destructor}}
59 struct D2 {
60 void ~D2() { } // \
61 // expected-error{{destructor cannot have a return type}}
65 struct E;
67 typedef E E_typedef;
68 struct E {
69 ~E_typedef(); // expected-error{{destructor cannot be declared using a typedef 'E_typedef' (aka 'E') of the class name}}
72 struct F {
73 (~F)(); // expected-note {{previous declaration is here}}
74 ~F(); // expected-error {{destructor cannot be redeclared}}
77 ~; // expected-error {{expected a class name after '~' to name a destructor}}
78 ~undef(); // expected-error {{undeclared identifier 'undef' in destructor name}}
79 ~operator+(int, int); // expected-error {{expected a class name after '~' to name a destructor}}
80 ~F(){} // expected-error {{destructor must be a non-static member function}}
82 struct G {
83 ~G();
86 G::~G() { }
88 // <rdar://problem/6841210>
89 struct H {
90 ~H(void) { }
93 struct X {};
95 struct Y {
96 ~X(); // expected-error {{expected the class name after '~' to name the enclosing class}}
99 namespace PR6421 {
100 class T; // expected-note{{forward declaration}}
102 class QGenericArgument
104 template<typename U>
105 void foo(T t) // expected-error{{variable has incomplete type}}
108 void disconnect()
110 T* t;
111 bob<QGenericArgument>(t); // expected-error{{undeclared identifier 'bob'}}
116 namespace PR6709 {
117 #ifdef MSABI
118 // This bug, "Clang instantiates destructor for function argument" is intended
119 // behaviour in the Microsoft ABI because the callee needs to destruct the arguments.
120 // expected-error@+3 {{indirection requires pointer operand ('int' invalid)}}
121 // expected-note@+3 {{in instantiation of member function 'PR6709::X<int>::~X' requested here}}
122 #endif
123 template<class T> class X { T v; ~X() { ++*v; } };
124 void a(X<int> x) {}
127 struct X0 { virtual ~X0() throw(); };
128 struct X1 : public X0 { };
130 // Make sure we instantiate operator deletes when building a virtual
131 // destructor.
132 namespace test6 {
133 template <class T> class A {
134 public:
135 void *operator new(__SIZE_TYPE__);
136 void operator delete(void *p) {
137 T::deleteIt(p); // expected-error {{type 'int' cannot be used prior to '::'}}
140 #ifdef MSABI
141 // expected-note@+2 {{in instantiation of member function 'test6::A<int>::operator delete' requested here}}
142 #endif
143 virtual ~A() {}
146 #ifndef MSABI
147 // expected-note@+2 {{in instantiation of member function 'test6::A<int>::operator delete' requested here}}
148 #endif
149 class B : A<int> { B(); };
150 B::B() {}
153 // Make sure classes are marked invalid when they have invalid
154 // members. This avoids a crash-on-invalid.
155 namespace test7 {
156 struct A {
157 ~A() const; // expected-error {{'const' qualifier is not allowed on a destructor}}
159 struct B : A {};
161 void test() {
162 B *b;
163 b->~B();
167 namespace nonvirtualdtor {
168 struct S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
169 virtual void m();
172 struct S2 {
173 ~S2(); // expected-warning {{has virtual functions but non-virtual destructor}}
174 virtual void m();
177 struct S3 : public S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
178 virtual void m();
181 struct S4 : public S2 { // expected-warning {{has virtual functions but non-virtual destructor}}
182 virtual void m();
185 struct B {
186 virtual ~B();
187 virtual void m();
190 struct S5 : public B {
191 virtual void m();
194 struct S6 {
195 virtual void m();
196 private:
197 ~S6();
200 struct S7 {
201 virtual void m();
202 protected:
203 ~S7();
206 struct S8 {} s8;
208 UnknownType S8::~S8() { // expected-error {{unknown type name 'UnknownType'}}
209 s8.~S8();
212 template<class T> class TS : public B {
213 virtual void m();
216 TS<int> baz;
218 template<class T> class TS2 { // expected-warning {{'nonvirtualdtor::TS2<int>' has virtual functions but non-virtual destructor}}
219 virtual void m();
222 TS2<int> foo; // expected-note {{instantiation}}
225 namespace dnvd { // delete-non-virtual-dtor warning
226 struct NP {};
228 struct B { // expected-warning {{has virtual functions but non-virtual destructor}}
229 virtual void foo();
232 struct D: B {}; // expected-warning {{has virtual functions but non-virtual destructor}}
234 struct F final : B {};
236 struct VB {
237 virtual void foo();
238 virtual ~VB();
241 struct VD: VB {};
243 struct VF final: VB {};
245 template <typename T>
246 class simple_ptr2 {
247 public:
248 simple_ptr2(T* t): _ptr(t) {}
249 ~simple_ptr2() { delete _ptr; } // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}}
250 T& operator*() const { return *_ptr; }
251 private:
252 T* _ptr;
255 void use(B&);
256 void use(SystemB&);
257 void use(VB&);
259 void nowarnstack() {
260 B b; use(b);
261 D d; use(d);
262 F f; use(f);
263 VB vb; use(vb);
264 VD vd; use(vd);
265 VF vf; use(vf);
268 void nowarnnonpoly() {
270 NP* np = new NP();
271 delete np;
274 NP* np = new NP[4];
275 delete[] np;
279 // FIXME: Why are these supposed to not warn?
280 void nowarnarray() {
282 B* b = new B[4];
283 delete[] b;
286 D* d = new D[4];
287 delete[] d;
290 VB* vb = new VB[4];
291 delete[] vb;
294 VD* vd = new VD[4];
295 delete[] vd;
299 template <typename T>
300 void nowarntemplate() {
302 T* t = new T();
303 delete t;
306 T* t = new T[4];
307 delete[] t;
311 void nowarn0() {
313 F* f = new F();
314 delete f;
317 VB* vb = new VB();
318 delete vb;
321 VB* vb = new VD();
322 delete vb;
325 VD* vd = new VD();
326 delete vd;
329 VF* vf = new VF();
330 delete vf;
334 void nowarn0_explicit_dtor(F* f, VB* vb, VD* vd, VF* vf) {
335 f->~F();
336 f->~F();
337 vb->~VB();
338 vd->~VD();
339 vf->~VF();
342 void warn0() {
344 B* b = new B();
345 delete b; // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}}
348 B* b = new D();
349 delete b; // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}}
352 D* d = new D();
353 delete d; // expected-warning {{delete called on non-final 'dnvd::D' that has virtual functions but non-virtual destructor}}
357 // Taken from libc++, slightly simplified.
358 template <class>
359 struct __is_destructible_apply { typedef int type; };
360 struct __two {char __lx[2];};
361 template <typename _Tp>
362 struct __is_destructor_wellformed {
363 template <typename _Tp1>
364 static char __test(typename __is_destructible_apply<
365 decltype(_Tp1().~_Tp1())>::type);
366 template <typename _Tp1>
367 static __two __test (...);
369 static const bool value = sizeof(__test<_Tp>(12)) == sizeof(char);
372 void warn0_explicit_dtor(B* b, B& br, D* d) {
373 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}}
374 b->B::~B(); // No warning when the call isn't virtual.
376 // No warning in unevaluated contexts.
377 (void)__is_destructor_wellformed<B>::value;
379 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}}
380 br.B::~B();
382 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}}
383 d->D::~D();
386 void nowarn1() {
388 simple_ptr<F> f(new F());
389 use(*f);
392 simple_ptr<VB> vb(new VB());
393 use(*vb);
396 simple_ptr<VB> vb(new VD());
397 use(*vb);
400 simple_ptr<VD> vd(new VD());
401 use(*vd);
404 simple_ptr<VF> vf(new VF());
405 use(*vf);
408 simple_ptr<SystemB> sb(new SystemB());
409 use(*sb);
413 void warn1() {
415 simple_ptr<B> b(new B()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr<dnvd::B>::~simple_ptr' requested here}}
416 use(*b);
419 simple_ptr2<B> b(new D()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr2<dnvd::B>::~simple_ptr2' requested here}}
420 use(*b);
423 simple_ptr<D> d(new D()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr<dnvd::D>::~simple_ptr' requested here}}
424 use(*d);
429 namespace PR9238 {
430 class B { public: ~B(); };
431 class C : virtual B { public: ~C() { } };
434 namespace PR7900 {
435 struct A { // expected-note 2{{type 'PR7900::A' found by destructor name lookup}}
437 struct B : public A {
439 void foo() {
440 B b;
441 b.~B();
442 b.~A(); // expected-error{{destructor type 'PR7900::A' in object destruction expression does not match the type 'PR7900::B' of the object being destroyed}}
443 (&b)->~A(); // expected-error{{destructor type 'PR7900::A' in object destruction expression does not match the type 'PR7900::B' of the object being destroyed}}
447 namespace PR16892 {
448 auto p = &A::~A; // expected-error{{taking the address of a destructor}}
451 namespace PR20238 {
452 struct S {
453 volatile ~S() { } // expected-error{{destructor cannot have a return type}}
457 namespace PR22668 {
458 struct S {
460 void f(S s) {
461 (s.~S)();
463 void g(S s) {
464 (s.~S); // expected-error{{reference to destructor must be called}}
468 class Invalid {
469 ~Invalid();
470 UnknownType xx; // expected-error{{unknown type name}}
473 // The constructor definition should not have errors
474 Invalid::~Invalid() {}
476 namespace PR30361 {
477 template <typename T>
478 struct C1 {
479 ~C1() {}
480 operator C1<T>* () { return nullptr; }
481 void foo1();
484 template<typename T>
485 void C1<T>::foo1() {
486 C1::operator C1<T>*();
487 C1::~C1();
490 void foo1() {
491 C1<int> x;
492 x.foo1();
496 namespace DtorTypedef {
497 struct A { ~A(); };
498 using A = A;
499 DtorTypedef::A::~A() {}
501 // This is invalid, but compilers accept it.
502 struct B { ~B(); };
503 namespace N { using B = B; }
504 N::B::~B() {} // expected-error {{destructor cannot be declared using a type alias}}
506 #pragma clang diagnostic push
507 #pragma clang diagnostic ignored "-Wdtor-typedef"
508 struct C { ~C(); };
509 namespace N { using C = C; }
510 N::C::~C() {}
511 #pragma clang diagnostic pop
514 // Ignore ambiguity errors in destructor name lookup. This matches the observed
515 // behavior of ICC, and is compatible with the observed behavior of GCC (which
516 // appears to ignore lookups that result in ambiguity) and MSVC (which appears
517 // to perform the lookups in the opposite order from Clang).
518 namespace PR44978 {
519 // All compilers accept this despite it being clearly ill-formed per the
520 // current wording.
521 namespace n {
522 class Foo {}; // expected-note {{found}}
524 class Foo {}; // expected-note {{found}}
525 using namespace n;
526 static void func(n::Foo *p) { p->~Foo(); } // expected-warning {{ambiguous}}
528 // GCC rejects this case, ICC accepts, despite the class member lookup being
529 // ambiguous.
530 struct Z;
531 struct X { using T = Z; }; // expected-note {{found}}
532 struct Y { using T = int; }; // expected-note {{found}}
533 struct Z : X, Y {};
534 void f(Z *p) { p->~T(); } // expected-warning {{ambiguous}}
536 // GCC accepts this and ignores the ambiguous class member lookup.
538 // FIXME: We should warn on the ambiguity here too, but that requires us to
539 // keep doing lookups after we've already found the type we want.
540 using T = Z;
541 void g(Z *p) { p->~T(); }
543 // ICC accepts this and ignores the ambiguous unqualified lookup.
544 struct Q {};
545 namespace { using U = Q; } // expected-note {{candidate}} expected-note {{found}}
546 using U = int; // expected-note {{candidate}} expected-note {{found}}
547 void f(Q *p) { p->~U(); } // expected-warning {{ambiguous}}
549 // We still diagnose if the unqualified lookup is dependent, though.
550 template<typename T> void f(T *p) { p->~U(); } // expected-error {{ambiguous}}
553 #endif // BE_THE_HEADER