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
12 struct SystemB {
13 virtual void foo();
14 };
15
16 template <typename T>
17 class simple_ptr {
18 public:
simple_ptr(T * t)19 simple_ptr(T* t): _ptr(t) {}
~simple_ptr()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}}
operator *() const23 T& operator*() const { return *_ptr; }
24 private:
25 T* _ptr;
26 };
27 }
28
29 #else
30
31 #define BE_THE_HEADER
32 #include __FILE__
33
34 class A {
35 public:
36 ~A();
37 };
38
39 class B {
40 public:
~B()41 ~B() { }
42 };
43
44 class C {
45 public:
46 (~C)() { }
47 };
48
49 struct D {
~DD50 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}}
57 };
58
59 struct D2 {
~D2D260 void ~D2() { } // \
61 // expected-error{{destructor cannot have a return type}}
62 };
63
64
65 struct E;
66
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}}
70 };
71
72 struct F {
73 (~F)(); // expected-note {{previous declaration is here}}
74 ~F(); // expected-error {{destructor cannot be redeclared}}
75 };
76
77 ~; // expected-error {{expected a class name after '~' to name a destructor}}
78 ~undef(); // expected-error {{expected the class name after '~' to name a destructor}}
79 ~operator+(int, int); // expected-error {{expected a class name after '~' to name a destructor}}
~F()80 ~F(){} // expected-error {{destructor must be a non-static member function}}
81
82 struct G {
83 ~G();
84 };
85
~G()86 G::~G() { }
87
88 // <rdar://problem/6841210>
89 struct H {
~HH90 ~H(void) { }
91 };
92
93 struct X {};
94
95 struct Y {
96 ~X(); // expected-error {{expected the class name after '~' to name the enclosing class}}
97 };
98
99 namespace PR6421 {
100 class T; // expected-note{{forward declaration}}
101
102 class QGenericArgument
103 {
104 template<typename U>
foo(T t)105 void foo(T t) // expected-error{{variable has incomplete type}}
106 { }
107
disconnect()108 void disconnect()
109 {
110 T* t;
111 bob<QGenericArgument>(t); // expected-error{{undeclared identifier 'bob'}}
112 }
113 };
114 }
115
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
~X()123 template<class T> class X { T v; ~X() { ++*v; } };
a(X<int> x)124 void a(X<int> x) {}
125 }
126
127 struct X0 { virtual ~X0() throw(); };
128 struct X1 : public X0 { };
129
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__);
operator delete(void * p)136 void operator delete(void *p) {
137 T::deleteIt(p); // expected-error {{type 'int' cannot be used prior to '::'}}
138 }
139
140 #ifdef MSABI
141 // expected-note@+2 {{in instantiation of member function 'test6::A<int>::operator delete' requested here}}
142 #endif
~A()143 virtual ~A() {}
144 };
145
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(); };
B()150 B::B() {}
151 }
152
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}}
158 };
159 struct B : A {};
160
test()161 void test() {
162 B *b;
163 b->~B();
164 }
165 }
166
167 namespace nonvirtualdtor {
168 struct S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
169 virtual void m();
170 };
171
172 struct S2 {
173 ~S2(); // expected-warning {{has virtual functions but non-virtual destructor}}
174 virtual void m();
175 };
176
177 struct S3 : public S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
178 virtual void m();
179 };
180
181 struct S4 : public S2 { // expected-warning {{has virtual functions but non-virtual destructor}}
182 virtual void m();
183 };
184
185 struct B {
186 virtual ~B();
187 virtual void m();
188 };
189
190 struct S5 : public B {
191 virtual void m();
192 };
193
194 struct S6 {
195 virtual void m();
196 private:
197 ~S6();
198 };
199
200 struct S7 {
201 virtual void m();
202 protected:
203 ~S7();
204 };
205
206 struct S8 {} s8;
207
~S8()208 UnknownType S8::~S8() { // expected-error {{unknown type name 'UnknownType'}}
209 s8.~S8();
210 }
211
212 template<class T> class TS : public B {
213 virtual void m();
214 };
215
216 TS<int> baz;
217
218 template<class T> class TS2 { // expected-warning {{'nonvirtualdtor::TS2<int>' has virtual functions but non-virtual destructor}}
219 virtual void m();
220 };
221
222 TS2<int> foo; // expected-note {{instantiation}}
223 }
224
225 namespace dnvd { // delete-non-virtual-dtor warning
226 struct NP {};
227
228 struct B { // expected-warning {{has virtual functions but non-virtual destructor}}
229 virtual void foo();
230 };
231
232 struct D: B {}; // expected-warning {{has virtual functions but non-virtual destructor}}
233
234 struct F final : B {};
235
236 struct VB {
237 virtual void foo();
238 virtual ~VB();
239 };
240
241 struct VD: VB {};
242
243 struct VF final: VB {};
244
245 template <typename T>
246 class simple_ptr2 {
247 public:
simple_ptr2(T * t)248 simple_ptr2(T* t): _ptr(t) {}
~simple_ptr2()249 ~simple_ptr2() { delete _ptr; } // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}}
operator *() const250 T& operator*() const { return *_ptr; }
251 private:
252 T* _ptr;
253 };
254
255 void use(B&);
256 void use(SystemB&);
257 void use(VB&);
258
nowarnstack()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);
266 }
267
nowarnnonpoly()268 void nowarnnonpoly() {
269 {
270 NP* np = new NP();
271 delete np;
272 }
273 {
274 NP* np = new NP[4];
275 delete[] np;
276 }
277 }
278
279 // FIXME: Why are these supposed to not warn?
nowarnarray()280 void nowarnarray() {
281 {
282 B* b = new B[4];
283 delete[] b;
284 }
285 {
286 D* d = new D[4];
287 delete[] d;
288 }
289 {
290 VB* vb = new VB[4];
291 delete[] vb;
292 }
293 {
294 VD* vd = new VD[4];
295 delete[] vd;
296 }
297 }
298
299 template <typename T>
nowarntemplate()300 void nowarntemplate() {
301 {
302 T* t = new T();
303 delete t;
304 }
305 {
306 T* t = new T[4];
307 delete[] t;
308 }
309 }
310
nowarn0()311 void nowarn0() {
312 {
313 F* f = new F();
314 delete f;
315 }
316 {
317 VB* vb = new VB();
318 delete vb;
319 }
320 {
321 VB* vb = new VD();
322 delete vb;
323 }
324 {
325 VD* vd = new VD();
326 delete vd;
327 }
328 {
329 VF* vf = new VF();
330 delete vf;
331 }
332 }
333
nowarn0_explicit_dtor(F * f,VB * vb,VD * vd,VF * 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();
340 }
341
warn0()342 void warn0() {
343 {
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}}
346 }
347 {
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}}
350 }
351 {
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}}
354 }
355 }
356
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 (...);
368
369 static const bool value = sizeof(__test<_Tp>(12)) == sizeof(char);
370 };
371
warn0_explicit_dtor(B * b,B & br,D * d)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.
375
376 // No warning in unevaluated contexts.
377 (void)__is_destructor_wellformed<B>::value;
378
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();
381
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();
384 }
385
nowarn1()386 void nowarn1() {
387 {
388 simple_ptr<F> f(new F());
389 use(*f);
390 }
391 {
392 simple_ptr<VB> vb(new VB());
393 use(*vb);
394 }
395 {
396 simple_ptr<VB> vb(new VD());
397 use(*vb);
398 }
399 {
400 simple_ptr<VD> vd(new VD());
401 use(*vd);
402 }
403 {
404 simple_ptr<VF> vf(new VF());
405 use(*vf);
406 }
407 {
408 simple_ptr<SystemB> sb(new SystemB());
409 use(*sb);
410 }
411 }
412
warn1()413 void warn1() {
414 {
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);
417 }
418 {
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);
421 }
422 {
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);
425 }
426 }
427 }
428
429 namespace PR9238 {
430 class B { public: ~B(); };
~C()431 class C : virtual B { public: ~C() { } };
432 }
433
434 namespace PR7900 {
435 struct A { // expected-note 2{{type 'PR7900::A' is declared here}}
436 };
437 struct B : public A {
438 };
foo()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}}
444 }
445 }
446
447 namespace PR16892 {
448 auto p = &A::~A; // expected-error{{taking the address of a destructor}}
449 }
450
451 namespace PR20238 {
452 struct S {
~SPR20238::S453 volatile ~S() { } // expected-error{{destructor cannot have a return type}}
454 };
455 }
456
457 namespace PR22668 {
458 struct S {
459 };
f(S s)460 void f(S s) {
461 (s.~S)();
462 }
g(S s)463 void g(S s) {
464 (s.~S); // expected-error{{reference to destructor must be called}}
465 }
466 }
467
468 class Invalid {
469 ~Invalid();
470 UnknownType xx; // expected-error{{unknown type name}}
471 };
472
473 // The constructor definition should not have errors
~Invalid()474 Invalid::~Invalid() {}
475
476 namespace PR30361 {
477 template <typename T>
478 struct C1 {
~C1PR30361::C1479 ~C1() {}
operator C1<T>*PR30361::C1480 operator C1<T>* () { return nullptr; }
481 void foo1();
482 };
483
484 template<typename T>
foo1()485 void C1<T>::foo1() {
486 C1::operator C1<T>*();
487 C1::~C1();
488 }
489
foo1()490 void foo1() {
491 C1<int> x;
492 x.foo1();
493 }
494 }
495 #endif // BE_THE_HEADER
496