Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / destructor.cpp
blobbeac50e449e96d49a94175902a0082e2208c7a64
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 struct H {
89 ~H(void) { }
92 struct X {};
94 struct Y {
95 ~X(); // expected-error {{expected the class name after '~' to name the enclosing class}}
98 namespace PR6421 {
99 class T; // expected-note{{forward declaration}}
101 class QGenericArgument
103 template<typename U>
104 void foo(T t) // expected-error{{variable has incomplete type}}
107 void disconnect()
109 T* t;
110 bob<QGenericArgument>(t); // expected-error{{undeclared identifier 'bob'}}
115 namespace PR6709 {
116 #ifdef MSABI
117 // This bug, "Clang instantiates destructor for function argument" is intended
118 // behaviour in the Microsoft ABI because the callee needs to destruct the arguments.
119 // expected-error@+3 {{indirection requires pointer operand ('int' invalid)}}
120 // expected-note@+3 {{in instantiation of member function 'PR6709::X<int>::~X' requested here}}
121 #endif
122 template<class T> class X { T v; ~X() { ++*v; } };
123 void a(X<int> x) {}
126 struct X0 { virtual ~X0() throw(); };
127 struct X1 : public X0 { };
129 // Make sure we instantiate operator deletes when building a virtual
130 // destructor.
131 namespace test6 {
132 template <class T> class A {
133 public:
134 void *operator new(__SIZE_TYPE__);
135 void operator delete(void *p) {
136 T::deleteIt(p); // expected-error {{type 'int' cannot be used prior to '::'}}
139 #ifdef MSABI
140 // expected-note@+2 {{in instantiation of member function 'test6::A<int>::operator delete' requested here}}
141 #endif
142 virtual ~A() {}
145 #ifndef MSABI
146 // expected-note@+2 {{in instantiation of member function 'test6::A<int>::operator delete' requested here}}
147 #endif
148 class B : A<int> { B(); };
149 B::B() {}
152 // Make sure classes are marked invalid when they have invalid
153 // members. This avoids a crash-on-invalid.
154 namespace test7 {
155 struct A {
156 ~A() const; // expected-error {{'const' qualifier is not allowed on a destructor}}
158 struct B : A {};
160 void test() {
161 B *b;
162 b->~B();
166 namespace nonvirtualdtor {
167 struct S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
168 virtual void m();
171 struct S2 {
172 ~S2(); // expected-warning {{has virtual functions but non-virtual destructor}}
173 virtual void m();
176 struct S3 : public S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
177 virtual void m();
180 struct S4 : public S2 { // expected-warning {{has virtual functions but non-virtual destructor}}
181 virtual void m();
184 struct B {
185 virtual ~B();
186 virtual void m();
189 struct S5 : public B {
190 virtual void m();
193 struct S6 {
194 virtual void m();
195 private:
196 ~S6();
199 struct S7 {
200 virtual void m();
201 protected:
202 ~S7();
205 struct S8 {} s8;
207 UnknownType S8::~S8() { // expected-error {{unknown type name 'UnknownType'}}
208 s8.~S8();
211 template<class T> class TS : public B {
212 virtual void m();
215 TS<int> baz;
217 template<class T> class TS2 { // expected-warning {{'nonvirtualdtor::TS2<int>' has virtual functions but non-virtual destructor}}
218 virtual void m();
221 TS2<int> foo; // expected-note {{instantiation}}
224 namespace dnvd { // delete-non-virtual-dtor warning
225 struct NP {};
227 struct B { // expected-warning {{has virtual functions but non-virtual destructor}}
228 virtual void foo();
231 struct D: B {}; // expected-warning {{has virtual functions but non-virtual destructor}}
233 struct F final : B {};
235 struct VB {
236 virtual void foo();
237 virtual ~VB();
240 struct VD: VB {};
242 struct VF final: VB {};
244 template <typename T>
245 class simple_ptr2 {
246 public:
247 simple_ptr2(T* t): _ptr(t) {}
248 ~simple_ptr2() { delete _ptr; } // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}}
249 T& operator*() const { return *_ptr; }
250 private:
251 T* _ptr;
254 void use(B&);
255 void use(SystemB&);
256 void use(VB&);
258 void nowarnstack() {
259 B b; use(b);
260 D d; use(d);
261 F f; use(f);
262 VB vb; use(vb);
263 VD vd; use(vd);
264 VF vf; use(vf);
267 void nowarnnonpoly() {
269 NP* np = new NP();
270 delete np;
273 NP* np = new NP[4];
274 delete[] np;
278 // FIXME: Why are these supposed to not warn?
279 void nowarnarray() {
281 B* b = new B[4];
282 delete[] b;
285 D* d = new D[4];
286 delete[] d;
289 VB* vb = new VB[4];
290 delete[] vb;
293 VD* vd = new VD[4];
294 delete[] vd;
298 template <typename T>
299 void nowarntemplate() {
301 T* t = new T();
302 delete t;
305 T* t = new T[4];
306 delete[] t;
310 void nowarn0() {
312 F* f = new F();
313 delete f;
316 VB* vb = new VB();
317 delete vb;
320 VB* vb = new VD();
321 delete vb;
324 VD* vd = new VD();
325 delete vd;
328 VF* vf = new VF();
329 delete vf;
333 void nowarn0_explicit_dtor(F* f, VB* vb, VD* vd, VF* vf) {
334 f->~F();
335 f->~F();
336 vb->~VB();
337 vd->~VD();
338 vf->~VF();
341 void warn0() {
343 B* b = new B();
344 delete b; // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}}
347 B* b = new D();
348 delete b; // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}}
351 D* d = new D();
352 delete d; // expected-warning {{delete called on non-final 'dnvd::D' that has virtual functions but non-virtual destructor}}
356 // Taken from libc++, slightly simplified.
357 template <class>
358 struct __is_destructible_apply { typedef int type; };
359 struct __two {char __lx[2];};
360 template <typename _Tp>
361 struct __is_destructor_wellformed {
362 template <typename _Tp1>
363 static char __test(typename __is_destructible_apply<
364 decltype(_Tp1().~_Tp1())>::type);
365 template <typename _Tp1>
366 static __two __test (...);
368 static const bool value = sizeof(__test<_Tp>(12)) == sizeof(char);
371 void warn0_explicit_dtor(B* b, B& br, D* d) {
372 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}}
373 b->B::~B(); // No warning when the call isn't virtual.
375 // No warning in unevaluated contexts.
376 (void)__is_destructor_wellformed<B>::value;
378 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}}
379 br.B::~B();
381 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}}
382 d->D::~D();
385 void nowarn1() {
387 simple_ptr<F> f(new F());
388 use(*f);
391 simple_ptr<VB> vb(new VB());
392 use(*vb);
395 simple_ptr<VB> vb(new VD());
396 use(*vb);
399 simple_ptr<VD> vd(new VD());
400 use(*vd);
403 simple_ptr<VF> vf(new VF());
404 use(*vf);
407 simple_ptr<SystemB> sb(new SystemB());
408 use(*sb);
412 void warn1() {
414 simple_ptr<B> b(new B()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr<dnvd::B>::~simple_ptr' requested here}}
415 use(*b);
418 simple_ptr2<B> b(new D()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr2<dnvd::B>::~simple_ptr2' requested here}}
419 use(*b);
422 simple_ptr<D> d(new D()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr<dnvd::D>::~simple_ptr' requested here}}
423 use(*d);
428 namespace PR9238 {
429 class B { public: ~B(); };
430 class C : virtual B { public: ~C() { } };
433 namespace PR7900 {
434 struct A { // expected-note 2{{type 'PR7900::A' found by destructor name lookup}}
436 struct B : public A {
438 void foo() {
439 B b;
440 b.~B();
441 b.~A(); // expected-error{{destructor type 'PR7900::A' in object destruction expression does not match the type 'B' of the object being destroyed}}
442 (&b)->~A(); // expected-error{{destructor type 'PR7900::A' in object destruction expression does not match the type 'B' of the object being destroyed}}
446 namespace PR16892 {
447 auto p = &A::~A; // expected-error{{taking the address of a destructor}}
450 namespace PR20238 {
451 struct S {
452 volatile ~S() { } // expected-error{{destructor cannot have a return type}}
456 namespace PR22668 {
457 struct S {
459 void f(S s) {
460 (s.~S)();
462 void g(S s) {
463 (s.~S); // expected-error{{reference to destructor must be called}}
467 class Invalid {
468 ~Invalid();
469 UnknownType xx; // expected-error{{unknown type name}}
472 // The constructor definition should not have errors
473 Invalid::~Invalid() {}
475 namespace PR30361 {
476 template <typename T>
477 struct C1 {
478 ~C1() {}
479 operator C1<T>* () { return nullptr; }
480 void foo1();
483 template<typename T>
484 void C1<T>::foo1() {
485 C1::operator C1<T>*();
486 C1::~C1();
489 void foo1() {
490 C1<int> x;
491 x.foo1();
495 namespace DtorTypedef {
496 struct A { ~A(); };
497 using A = A;
498 DtorTypedef::A::~A() {}
500 // This is invalid, but compilers accept it.
501 struct B { ~B(); };
502 namespace N { using B = B; }
503 N::B::~B() {} // expected-error {{destructor cannot be declared using a type alias}}
505 #pragma clang diagnostic push
506 #pragma clang diagnostic ignored "-Wdtor-typedef"
507 struct C { ~C(); };
508 namespace N { using C = C; }
509 N::C::~C() {}
510 #pragma clang diagnostic pop
513 // Ignore ambiguity errors in destructor name lookup. This matches the observed
514 // behavior of ICC, and is compatible with the observed behavior of GCC (which
515 // appears to ignore lookups that result in ambiguity) and MSVC (which appears
516 // to perform the lookups in the opposite order from Clang).
517 namespace PR44978 {
518 // All compilers accept this despite it being clearly ill-formed per the
519 // current wording.
520 namespace n {
521 class Foo {}; // expected-note {{found}}
523 class Foo {}; // expected-note {{found}}
524 using namespace n;
525 static void func(n::Foo *p) { p->~Foo(); } // expected-warning {{ambiguous}}
527 // GCC rejects this case, ICC accepts, despite the class member lookup being
528 // ambiguous.
529 struct Z;
530 struct X { using T = Z; }; // expected-note {{found}}
531 struct Y { using T = int; }; // expected-note {{found}}
532 struct Z : X, Y {};
533 void f(Z *p) { p->~T(); } // expected-warning {{ambiguous}}
535 // GCC accepts this and ignores the ambiguous class member lookup.
537 // FIXME: We should warn on the ambiguity here too, but that requires us to
538 // keep doing lookups after we've already found the type we want.
539 using T = Z;
540 void g(Z *p) { p->~T(); }
542 // ICC accepts this and ignores the ambiguous unqualified lookup.
543 struct Q {};
544 namespace { using U = Q; } // expected-note {{candidate}} expected-note {{found}}
545 using U = int; // expected-note {{candidate}} expected-note {{found}}
546 void f(Q *p) { p->~U(); } // expected-warning {{ambiguous}}
548 // We still diagnose if the unqualified lookup is dependent, though.
549 template<typename T> void f(T *p) { p->~U(); } // expected-error {{ambiguous}}
552 namespace crash_on_invalid_base_dtor {
553 struct Test {
554 virtual ~Test();
556 struct Baz : public Test { // expected-warning {{non-virtual destructor}}
557 Baz() {}
558 ~Baz() = defaul; // expected-error {{undeclared identifier 'defaul'}} \
559 // expected-error {{initializer on function}} \
560 // expected-note {{overridden virtual function is here}}
562 struct Foo : public Baz { // expected-error {{cannot override a non-deleted function}} \
563 // expected-note {{destructor of 'Foo' is implicitly deleted}}
564 Foo() {}
568 #endif // BE_THE_HEADER