annotate clang/test/SemaCXX/destructor.cpp @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 0572611fdcc8
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
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
anatofuz
parents:
diff changeset
2 // RUN: %clang_cc1 -std=c++11 -triple %ms_abi_triple -DMSABI -fsyntax-only -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -verify %s -pedantic
anatofuz
parents:
diff changeset
3
anatofuz
parents:
diff changeset
4 #if defined(BE_THE_HEADER)
anatofuz
parents:
diff changeset
5
anatofuz
parents:
diff changeset
6 // Wdelete-non-virtual-dtor should warn about the delete from smart pointer
anatofuz
parents:
diff changeset
7 // classes in system headers (std::unique_ptr...) too.
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #pragma clang system_header
anatofuz
parents:
diff changeset
10 namespace dnvd {
anatofuz
parents:
diff changeset
11
anatofuz
parents:
diff changeset
12 struct SystemB {
anatofuz
parents:
diff changeset
13 virtual void foo();
anatofuz
parents:
diff changeset
14 };
anatofuz
parents:
diff changeset
15
anatofuz
parents:
diff changeset
16 template <typename T>
anatofuz
parents:
diff changeset
17 class simple_ptr {
anatofuz
parents:
diff changeset
18 public:
anatofuz
parents:
diff changeset
19 simple_ptr(T* t): _ptr(t) {}
anatofuz
parents:
diff changeset
20 ~simple_ptr() { delete _ptr; } // \
anatofuz
parents:
diff changeset
21 // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}} \
anatofuz
parents:
diff changeset
22 // expected-warning {{delete called on non-final 'dnvd::D' that has virtual functions but non-virtual destructor}}
anatofuz
parents:
diff changeset
23 T& operator*() const { return *_ptr; }
anatofuz
parents:
diff changeset
24 private:
anatofuz
parents:
diff changeset
25 T* _ptr;
anatofuz
parents:
diff changeset
26 };
anatofuz
parents:
diff changeset
27 }
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29 #else
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 #define BE_THE_HEADER
anatofuz
parents:
diff changeset
32 #include __FILE__
anatofuz
parents:
diff changeset
33
anatofuz
parents:
diff changeset
34 class A {
anatofuz
parents:
diff changeset
35 public:
anatofuz
parents:
diff changeset
36 ~A();
anatofuz
parents:
diff changeset
37 };
anatofuz
parents:
diff changeset
38
anatofuz
parents:
diff changeset
39 class B {
anatofuz
parents:
diff changeset
40 public:
anatofuz
parents:
diff changeset
41 ~B() { }
anatofuz
parents:
diff changeset
42 };
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 class C {
anatofuz
parents:
diff changeset
45 public:
anatofuz
parents:
diff changeset
46 (~C)() { }
anatofuz
parents:
diff changeset
47 };
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 struct D {
anatofuz
parents:
diff changeset
50 static void ~D(int, ...) const { } // \
anatofuz
parents:
diff changeset
51 // expected-error{{static member function cannot have 'const' qualifier}} \
anatofuz
parents:
diff changeset
52 // expected-error{{destructor cannot be declared 'static'}} \
anatofuz
parents:
diff changeset
53 // expected-error{{destructor cannot have any parameters}} \
anatofuz
parents:
diff changeset
54 // expected-error{{destructor cannot be variadic}} \
anatofuz
parents:
diff changeset
55 // expected-error{{destructor cannot have a return type}} \
anatofuz
parents:
diff changeset
56 // expected-error{{'const' qualifier is not allowed on a destructor}}
anatofuz
parents:
diff changeset
57 };
anatofuz
parents:
diff changeset
58
anatofuz
parents:
diff changeset
59 struct D2 {
anatofuz
parents:
diff changeset
60 void ~D2() { } // \
anatofuz
parents:
diff changeset
61 // expected-error{{destructor cannot have a return type}}
anatofuz
parents:
diff changeset
62 };
anatofuz
parents:
diff changeset
63
anatofuz
parents:
diff changeset
64
anatofuz
parents:
diff changeset
65 struct E;
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 typedef E E_typedef;
anatofuz
parents:
diff changeset
68 struct E {
anatofuz
parents:
diff changeset
69 ~E_typedef(); // expected-error{{destructor cannot be declared using a typedef 'E_typedef' (aka 'E') of the class name}}
anatofuz
parents:
diff changeset
70 };
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 struct F {
anatofuz
parents:
diff changeset
73 (~F)(); // expected-note {{previous declaration is here}}
anatofuz
parents:
diff changeset
74 ~F(); // expected-error {{destructor cannot be redeclared}}
anatofuz
parents:
diff changeset
75 };
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 ~; // expected-error {{expected a class name after '~' to name a destructor}}
anatofuz
parents:
diff changeset
78 ~undef(); // expected-error {{undeclared identifier 'undef' in destructor name}}
anatofuz
parents:
diff changeset
79 ~operator+(int, int); // expected-error {{expected a class name after '~' to name a destructor}}
anatofuz
parents:
diff changeset
80 ~F(){} // expected-error {{destructor must be a non-static member function}}
anatofuz
parents:
diff changeset
81
anatofuz
parents:
diff changeset
82 struct G {
anatofuz
parents:
diff changeset
83 ~G();
anatofuz
parents:
diff changeset
84 };
anatofuz
parents:
diff changeset
85
anatofuz
parents:
diff changeset
86 G::~G() { }
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 // <rdar://problem/6841210>
anatofuz
parents:
diff changeset
89 struct H {
anatofuz
parents:
diff changeset
90 ~H(void) { }
anatofuz
parents:
diff changeset
91 };
anatofuz
parents:
diff changeset
92
anatofuz
parents:
diff changeset
93 struct X {};
anatofuz
parents:
diff changeset
94
anatofuz
parents:
diff changeset
95 struct Y {
anatofuz
parents:
diff changeset
96 ~X(); // expected-error {{expected the class name after '~' to name the enclosing class}}
anatofuz
parents:
diff changeset
97 };
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 namespace PR6421 {
anatofuz
parents:
diff changeset
100 class T; // expected-note{{forward declaration}}
anatofuz
parents:
diff changeset
101
anatofuz
parents:
diff changeset
102 class QGenericArgument
anatofuz
parents:
diff changeset
103 {
anatofuz
parents:
diff changeset
104 template<typename U>
anatofuz
parents:
diff changeset
105 void foo(T t) // expected-error{{variable has incomplete type}}
anatofuz
parents:
diff changeset
106 { }
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 void disconnect()
anatofuz
parents:
diff changeset
109 {
anatofuz
parents:
diff changeset
110 T* t;
anatofuz
parents:
diff changeset
111 bob<QGenericArgument>(t); // expected-error{{undeclared identifier 'bob'}}
anatofuz
parents:
diff changeset
112 }
anatofuz
parents:
diff changeset
113 };
anatofuz
parents:
diff changeset
114 }
anatofuz
parents:
diff changeset
115
anatofuz
parents:
diff changeset
116 namespace PR6709 {
anatofuz
parents:
diff changeset
117 #ifdef MSABI
anatofuz
parents:
diff changeset
118 // This bug, "Clang instantiates destructor for function argument" is intended
anatofuz
parents:
diff changeset
119 // behaviour in the Microsoft ABI because the callee needs to destruct the arguments.
anatofuz
parents:
diff changeset
120 // expected-error@+3 {{indirection requires pointer operand ('int' invalid)}}
anatofuz
parents:
diff changeset
121 // expected-note@+3 {{in instantiation of member function 'PR6709::X<int>::~X' requested here}}
anatofuz
parents:
diff changeset
122 #endif
anatofuz
parents:
diff changeset
123 template<class T> class X { T v; ~X() { ++*v; } };
anatofuz
parents:
diff changeset
124 void a(X<int> x) {}
anatofuz
parents:
diff changeset
125 }
anatofuz
parents:
diff changeset
126
anatofuz
parents:
diff changeset
127 struct X0 { virtual ~X0() throw(); };
anatofuz
parents:
diff changeset
128 struct X1 : public X0 { };
anatofuz
parents:
diff changeset
129
anatofuz
parents:
diff changeset
130 // Make sure we instantiate operator deletes when building a virtual
anatofuz
parents:
diff changeset
131 // destructor.
anatofuz
parents:
diff changeset
132 namespace test6 {
anatofuz
parents:
diff changeset
133 template <class T> class A {
anatofuz
parents:
diff changeset
134 public:
anatofuz
parents:
diff changeset
135 void *operator new(__SIZE_TYPE__);
anatofuz
parents:
diff changeset
136 void operator delete(void *p) {
anatofuz
parents:
diff changeset
137 T::deleteIt(p); // expected-error {{type 'int' cannot be used prior to '::'}}
anatofuz
parents:
diff changeset
138 }
anatofuz
parents:
diff changeset
139
anatofuz
parents:
diff changeset
140 #ifdef MSABI
anatofuz
parents:
diff changeset
141 // expected-note@+2 {{in instantiation of member function 'test6::A<int>::operator delete' requested here}}
anatofuz
parents:
diff changeset
142 #endif
anatofuz
parents:
diff changeset
143 virtual ~A() {}
anatofuz
parents:
diff changeset
144 };
anatofuz
parents:
diff changeset
145
anatofuz
parents:
diff changeset
146 #ifndef MSABI
anatofuz
parents:
diff changeset
147 // expected-note@+2 {{in instantiation of member function 'test6::A<int>::operator delete' requested here}}
anatofuz
parents:
diff changeset
148 #endif
anatofuz
parents:
diff changeset
149 class B : A<int> { B(); };
anatofuz
parents:
diff changeset
150 B::B() {}
anatofuz
parents:
diff changeset
151 }
anatofuz
parents:
diff changeset
152
anatofuz
parents:
diff changeset
153 // Make sure classes are marked invalid when they have invalid
anatofuz
parents:
diff changeset
154 // members. This avoids a crash-on-invalid.
anatofuz
parents:
diff changeset
155 namespace test7 {
anatofuz
parents:
diff changeset
156 struct A {
anatofuz
parents:
diff changeset
157 ~A() const; // expected-error {{'const' qualifier is not allowed on a destructor}}
anatofuz
parents:
diff changeset
158 };
anatofuz
parents:
diff changeset
159 struct B : A {};
anatofuz
parents:
diff changeset
160
anatofuz
parents:
diff changeset
161 void test() {
anatofuz
parents:
diff changeset
162 B *b;
anatofuz
parents:
diff changeset
163 b->~B();
anatofuz
parents:
diff changeset
164 }
anatofuz
parents:
diff changeset
165 }
anatofuz
parents:
diff changeset
166
anatofuz
parents:
diff changeset
167 namespace nonvirtualdtor {
anatofuz
parents:
diff changeset
168 struct S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
anatofuz
parents:
diff changeset
169 virtual void m();
anatofuz
parents:
diff changeset
170 };
anatofuz
parents:
diff changeset
171
anatofuz
parents:
diff changeset
172 struct S2 {
anatofuz
parents:
diff changeset
173 ~S2(); // expected-warning {{has virtual functions but non-virtual destructor}}
anatofuz
parents:
diff changeset
174 virtual void m();
anatofuz
parents:
diff changeset
175 };
anatofuz
parents:
diff changeset
176
anatofuz
parents:
diff changeset
177 struct S3 : public S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
anatofuz
parents:
diff changeset
178 virtual void m();
anatofuz
parents:
diff changeset
179 };
anatofuz
parents:
diff changeset
180
anatofuz
parents:
diff changeset
181 struct S4 : public S2 { // expected-warning {{has virtual functions but non-virtual destructor}}
anatofuz
parents:
diff changeset
182 virtual void m();
anatofuz
parents:
diff changeset
183 };
anatofuz
parents:
diff changeset
184
anatofuz
parents:
diff changeset
185 struct B {
anatofuz
parents:
diff changeset
186 virtual ~B();
anatofuz
parents:
diff changeset
187 virtual void m();
anatofuz
parents:
diff changeset
188 };
anatofuz
parents:
diff changeset
189
anatofuz
parents:
diff changeset
190 struct S5 : public B {
anatofuz
parents:
diff changeset
191 virtual void m();
anatofuz
parents:
diff changeset
192 };
anatofuz
parents:
diff changeset
193
anatofuz
parents:
diff changeset
194 struct S6 {
anatofuz
parents:
diff changeset
195 virtual void m();
anatofuz
parents:
diff changeset
196 private:
anatofuz
parents:
diff changeset
197 ~S6();
anatofuz
parents:
diff changeset
198 };
anatofuz
parents:
diff changeset
199
anatofuz
parents:
diff changeset
200 struct S7 {
anatofuz
parents:
diff changeset
201 virtual void m();
anatofuz
parents:
diff changeset
202 protected:
anatofuz
parents:
diff changeset
203 ~S7();
anatofuz
parents:
diff changeset
204 };
anatofuz
parents:
diff changeset
205
anatofuz
parents:
diff changeset
206 struct S8 {} s8;
anatofuz
parents:
diff changeset
207
anatofuz
parents:
diff changeset
208 UnknownType S8::~S8() { // expected-error {{unknown type name 'UnknownType'}}
anatofuz
parents:
diff changeset
209 s8.~S8();
anatofuz
parents:
diff changeset
210 }
anatofuz
parents:
diff changeset
211
anatofuz
parents:
diff changeset
212 template<class T> class TS : public B {
anatofuz
parents:
diff changeset
213 virtual void m();
anatofuz
parents:
diff changeset
214 };
anatofuz
parents:
diff changeset
215
anatofuz
parents:
diff changeset
216 TS<int> baz;
anatofuz
parents:
diff changeset
217
anatofuz
parents:
diff changeset
218 template<class T> class TS2 { // expected-warning {{'nonvirtualdtor::TS2<int>' has virtual functions but non-virtual destructor}}
anatofuz
parents:
diff changeset
219 virtual void m();
anatofuz
parents:
diff changeset
220 };
anatofuz
parents:
diff changeset
221
anatofuz
parents:
diff changeset
222 TS2<int> foo; // expected-note {{instantiation}}
anatofuz
parents:
diff changeset
223 }
anatofuz
parents:
diff changeset
224
anatofuz
parents:
diff changeset
225 namespace dnvd { // delete-non-virtual-dtor warning
anatofuz
parents:
diff changeset
226 struct NP {};
anatofuz
parents:
diff changeset
227
anatofuz
parents:
diff changeset
228 struct B { // expected-warning {{has virtual functions but non-virtual destructor}}
anatofuz
parents:
diff changeset
229 virtual void foo();
anatofuz
parents:
diff changeset
230 };
anatofuz
parents:
diff changeset
231
anatofuz
parents:
diff changeset
232 struct D: B {}; // expected-warning {{has virtual functions but non-virtual destructor}}
anatofuz
parents:
diff changeset
233
anatofuz
parents:
diff changeset
234 struct F final : B {};
anatofuz
parents:
diff changeset
235
anatofuz
parents:
diff changeset
236 struct VB {
anatofuz
parents:
diff changeset
237 virtual void foo();
anatofuz
parents:
diff changeset
238 virtual ~VB();
anatofuz
parents:
diff changeset
239 };
anatofuz
parents:
diff changeset
240
anatofuz
parents:
diff changeset
241 struct VD: VB {};
anatofuz
parents:
diff changeset
242
anatofuz
parents:
diff changeset
243 struct VF final: VB {};
anatofuz
parents:
diff changeset
244
anatofuz
parents:
diff changeset
245 template <typename T>
anatofuz
parents:
diff changeset
246 class simple_ptr2 {
anatofuz
parents:
diff changeset
247 public:
anatofuz
parents:
diff changeset
248 simple_ptr2(T* t): _ptr(t) {}
anatofuz
parents:
diff changeset
249 ~simple_ptr2() { delete _ptr; } // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}}
anatofuz
parents:
diff changeset
250 T& operator*() const { return *_ptr; }
anatofuz
parents:
diff changeset
251 private:
anatofuz
parents:
diff changeset
252 T* _ptr;
anatofuz
parents:
diff changeset
253 };
anatofuz
parents:
diff changeset
254
anatofuz
parents:
diff changeset
255 void use(B&);
anatofuz
parents:
diff changeset
256 void use(SystemB&);
anatofuz
parents:
diff changeset
257 void use(VB&);
anatofuz
parents:
diff changeset
258
anatofuz
parents:
diff changeset
259 void nowarnstack() {
anatofuz
parents:
diff changeset
260 B b; use(b);
anatofuz
parents:
diff changeset
261 D d; use(d);
anatofuz
parents:
diff changeset
262 F f; use(f);
anatofuz
parents:
diff changeset
263 VB vb; use(vb);
anatofuz
parents:
diff changeset
264 VD vd; use(vd);
anatofuz
parents:
diff changeset
265 VF vf; use(vf);
anatofuz
parents:
diff changeset
266 }
anatofuz
parents:
diff changeset
267
anatofuz
parents:
diff changeset
268 void nowarnnonpoly() {
anatofuz
parents:
diff changeset
269 {
anatofuz
parents:
diff changeset
270 NP* np = new NP();
anatofuz
parents:
diff changeset
271 delete np;
anatofuz
parents:
diff changeset
272 }
anatofuz
parents:
diff changeset
273 {
anatofuz
parents:
diff changeset
274 NP* np = new NP[4];
anatofuz
parents:
diff changeset
275 delete[] np;
anatofuz
parents:
diff changeset
276 }
anatofuz
parents:
diff changeset
277 }
anatofuz
parents:
diff changeset
278
anatofuz
parents:
diff changeset
279 // FIXME: Why are these supposed to not warn?
anatofuz
parents:
diff changeset
280 void nowarnarray() {
anatofuz
parents:
diff changeset
281 {
anatofuz
parents:
diff changeset
282 B* b = new B[4];
anatofuz
parents:
diff changeset
283 delete[] b;
anatofuz
parents:
diff changeset
284 }
anatofuz
parents:
diff changeset
285 {
anatofuz
parents:
diff changeset
286 D* d = new D[4];
anatofuz
parents:
diff changeset
287 delete[] d;
anatofuz
parents:
diff changeset
288 }
anatofuz
parents:
diff changeset
289 {
anatofuz
parents:
diff changeset
290 VB* vb = new VB[4];
anatofuz
parents:
diff changeset
291 delete[] vb;
anatofuz
parents:
diff changeset
292 }
anatofuz
parents:
diff changeset
293 {
anatofuz
parents:
diff changeset
294 VD* vd = new VD[4];
anatofuz
parents:
diff changeset
295 delete[] vd;
anatofuz
parents:
diff changeset
296 }
anatofuz
parents:
diff changeset
297 }
anatofuz
parents:
diff changeset
298
anatofuz
parents:
diff changeset
299 template <typename T>
anatofuz
parents:
diff changeset
300 void nowarntemplate() {
anatofuz
parents:
diff changeset
301 {
anatofuz
parents:
diff changeset
302 T* t = new T();
anatofuz
parents:
diff changeset
303 delete t;
anatofuz
parents:
diff changeset
304 }
anatofuz
parents:
diff changeset
305 {
anatofuz
parents:
diff changeset
306 T* t = new T[4];
anatofuz
parents:
diff changeset
307 delete[] t;
anatofuz
parents:
diff changeset
308 }
anatofuz
parents:
diff changeset
309 }
anatofuz
parents:
diff changeset
310
anatofuz
parents:
diff changeset
311 void nowarn0() {
anatofuz
parents:
diff changeset
312 {
anatofuz
parents:
diff changeset
313 F* f = new F();
anatofuz
parents:
diff changeset
314 delete f;
anatofuz
parents:
diff changeset
315 }
anatofuz
parents:
diff changeset
316 {
anatofuz
parents:
diff changeset
317 VB* vb = new VB();
anatofuz
parents:
diff changeset
318 delete vb;
anatofuz
parents:
diff changeset
319 }
anatofuz
parents:
diff changeset
320 {
anatofuz
parents:
diff changeset
321 VB* vb = new VD();
anatofuz
parents:
diff changeset
322 delete vb;
anatofuz
parents:
diff changeset
323 }
anatofuz
parents:
diff changeset
324 {
anatofuz
parents:
diff changeset
325 VD* vd = new VD();
anatofuz
parents:
diff changeset
326 delete vd;
anatofuz
parents:
diff changeset
327 }
anatofuz
parents:
diff changeset
328 {
anatofuz
parents:
diff changeset
329 VF* vf = new VF();
anatofuz
parents:
diff changeset
330 delete vf;
anatofuz
parents:
diff changeset
331 }
anatofuz
parents:
diff changeset
332 }
anatofuz
parents:
diff changeset
333
anatofuz
parents:
diff changeset
334 void nowarn0_explicit_dtor(F* f, VB* vb, VD* vd, VF* vf) {
anatofuz
parents:
diff changeset
335 f->~F();
anatofuz
parents:
diff changeset
336 f->~F();
anatofuz
parents:
diff changeset
337 vb->~VB();
anatofuz
parents:
diff changeset
338 vd->~VD();
anatofuz
parents:
diff changeset
339 vf->~VF();
anatofuz
parents:
diff changeset
340 }
anatofuz
parents:
diff changeset
341
anatofuz
parents:
diff changeset
342 void warn0() {
anatofuz
parents:
diff changeset
343 {
anatofuz
parents:
diff changeset
344 B* b = new B();
anatofuz
parents:
diff changeset
345 delete b; // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}}
anatofuz
parents:
diff changeset
346 }
anatofuz
parents:
diff changeset
347 {
anatofuz
parents:
diff changeset
348 B* b = new D();
anatofuz
parents:
diff changeset
349 delete b; // expected-warning {{delete called on non-final 'dnvd::B' that has virtual functions but non-virtual destructor}}
anatofuz
parents:
diff changeset
350 }
anatofuz
parents:
diff changeset
351 {
anatofuz
parents:
diff changeset
352 D* d = new D();
anatofuz
parents:
diff changeset
353 delete d; // expected-warning {{delete called on non-final 'dnvd::D' that has virtual functions but non-virtual destructor}}
anatofuz
parents:
diff changeset
354 }
anatofuz
parents:
diff changeset
355 }
anatofuz
parents:
diff changeset
356
anatofuz
parents:
diff changeset
357 // Taken from libc++, slightly simplified.
anatofuz
parents:
diff changeset
358 template <class>
anatofuz
parents:
diff changeset
359 struct __is_destructible_apply { typedef int type; };
anatofuz
parents:
diff changeset
360 struct __two {char __lx[2];};
anatofuz
parents:
diff changeset
361 template <typename _Tp>
anatofuz
parents:
diff changeset
362 struct __is_destructor_wellformed {
anatofuz
parents:
diff changeset
363 template <typename _Tp1>
anatofuz
parents:
diff changeset
364 static char __test(typename __is_destructible_apply<
anatofuz
parents:
diff changeset
365 decltype(_Tp1().~_Tp1())>::type);
anatofuz
parents:
diff changeset
366 template <typename _Tp1>
anatofuz
parents:
diff changeset
367 static __two __test (...);
anatofuz
parents:
diff changeset
368
anatofuz
parents:
diff changeset
369 static const bool value = sizeof(__test<_Tp>(12)) == sizeof(char);
anatofuz
parents:
diff changeset
370 };
anatofuz
parents:
diff changeset
371
anatofuz
parents:
diff changeset
372 void warn0_explicit_dtor(B* b, B& br, D* d) {
anatofuz
parents:
diff changeset
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}}
anatofuz
parents:
diff changeset
374 b->B::~B(); // No warning when the call isn't virtual.
anatofuz
parents:
diff changeset
375
anatofuz
parents:
diff changeset
376 // No warning in unevaluated contexts.
anatofuz
parents:
diff changeset
377 (void)__is_destructor_wellformed<B>::value;
anatofuz
parents:
diff changeset
378
anatofuz
parents:
diff changeset
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}}
anatofuz
parents:
diff changeset
380 br.B::~B();
anatofuz
parents:
diff changeset
381
anatofuz
parents:
diff changeset
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}}
anatofuz
parents:
diff changeset
383 d->D::~D();
anatofuz
parents:
diff changeset
384 }
anatofuz
parents:
diff changeset
385
anatofuz
parents:
diff changeset
386 void nowarn1() {
anatofuz
parents:
diff changeset
387 {
anatofuz
parents:
diff changeset
388 simple_ptr<F> f(new F());
anatofuz
parents:
diff changeset
389 use(*f);
anatofuz
parents:
diff changeset
390 }
anatofuz
parents:
diff changeset
391 {
anatofuz
parents:
diff changeset
392 simple_ptr<VB> vb(new VB());
anatofuz
parents:
diff changeset
393 use(*vb);
anatofuz
parents:
diff changeset
394 }
anatofuz
parents:
diff changeset
395 {
anatofuz
parents:
diff changeset
396 simple_ptr<VB> vb(new VD());
anatofuz
parents:
diff changeset
397 use(*vb);
anatofuz
parents:
diff changeset
398 }
anatofuz
parents:
diff changeset
399 {
anatofuz
parents:
diff changeset
400 simple_ptr<VD> vd(new VD());
anatofuz
parents:
diff changeset
401 use(*vd);
anatofuz
parents:
diff changeset
402 }
anatofuz
parents:
diff changeset
403 {
anatofuz
parents:
diff changeset
404 simple_ptr<VF> vf(new VF());
anatofuz
parents:
diff changeset
405 use(*vf);
anatofuz
parents:
diff changeset
406 }
anatofuz
parents:
diff changeset
407 {
anatofuz
parents:
diff changeset
408 simple_ptr<SystemB> sb(new SystemB());
anatofuz
parents:
diff changeset
409 use(*sb);
anatofuz
parents:
diff changeset
410 }
anatofuz
parents:
diff changeset
411 }
anatofuz
parents:
diff changeset
412
anatofuz
parents:
diff changeset
413 void warn1() {
anatofuz
parents:
diff changeset
414 {
anatofuz
parents:
diff changeset
415 simple_ptr<B> b(new B()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr<dnvd::B>::~simple_ptr' requested here}}
anatofuz
parents:
diff changeset
416 use(*b);
anatofuz
parents:
diff changeset
417 }
anatofuz
parents:
diff changeset
418 {
anatofuz
parents:
diff changeset
419 simple_ptr2<B> b(new D()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr2<dnvd::B>::~simple_ptr2' requested here}}
anatofuz
parents:
diff changeset
420 use(*b);
anatofuz
parents:
diff changeset
421 }
anatofuz
parents:
diff changeset
422 {
anatofuz
parents:
diff changeset
423 simple_ptr<D> d(new D()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr<dnvd::D>::~simple_ptr' requested here}}
anatofuz
parents:
diff changeset
424 use(*d);
anatofuz
parents:
diff changeset
425 }
anatofuz
parents:
diff changeset
426 }
anatofuz
parents:
diff changeset
427 }
anatofuz
parents:
diff changeset
428
anatofuz
parents:
diff changeset
429 namespace PR9238 {
anatofuz
parents:
diff changeset
430 class B { public: ~B(); };
anatofuz
parents:
diff changeset
431 class C : virtual B { public: ~C() { } };
anatofuz
parents:
diff changeset
432 }
anatofuz
parents:
diff changeset
433
anatofuz
parents:
diff changeset
434 namespace PR7900 {
anatofuz
parents:
diff changeset
435 struct A { // expected-note 2{{type 'PR7900::A' found by destructor name lookup}}
anatofuz
parents:
diff changeset
436 };
anatofuz
parents:
diff changeset
437 struct B : public A {
anatofuz
parents:
diff changeset
438 };
anatofuz
parents:
diff changeset
439 void foo() {
anatofuz
parents:
diff changeset
440 B b;
anatofuz
parents:
diff changeset
441 b.~B();
anatofuz
parents:
diff changeset
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}}
anatofuz
parents:
diff changeset
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}}
anatofuz
parents:
diff changeset
444 }
anatofuz
parents:
diff changeset
445 }
anatofuz
parents:
diff changeset
446
anatofuz
parents:
diff changeset
447 namespace PR16892 {
anatofuz
parents:
diff changeset
448 auto p = &A::~A; // expected-error{{taking the address of a destructor}}
anatofuz
parents:
diff changeset
449 }
anatofuz
parents:
diff changeset
450
anatofuz
parents:
diff changeset
451 namespace PR20238 {
anatofuz
parents:
diff changeset
452 struct S {
anatofuz
parents:
diff changeset
453 volatile ~S() { } // expected-error{{destructor cannot have a return type}}
anatofuz
parents:
diff changeset
454 };
anatofuz
parents:
diff changeset
455 }
anatofuz
parents:
diff changeset
456
anatofuz
parents:
diff changeset
457 namespace PR22668 {
anatofuz
parents:
diff changeset
458 struct S {
anatofuz
parents:
diff changeset
459 };
anatofuz
parents:
diff changeset
460 void f(S s) {
anatofuz
parents:
diff changeset
461 (s.~S)();
anatofuz
parents:
diff changeset
462 }
anatofuz
parents:
diff changeset
463 void g(S s) {
anatofuz
parents:
diff changeset
464 (s.~S); // expected-error{{reference to destructor must be called}}
anatofuz
parents:
diff changeset
465 }
anatofuz
parents:
diff changeset
466 }
anatofuz
parents:
diff changeset
467
anatofuz
parents:
diff changeset
468 class Invalid {
anatofuz
parents:
diff changeset
469 ~Invalid();
anatofuz
parents:
diff changeset
470 UnknownType xx; // expected-error{{unknown type name}}
anatofuz
parents:
diff changeset
471 };
anatofuz
parents:
diff changeset
472
anatofuz
parents:
diff changeset
473 // The constructor definition should not have errors
anatofuz
parents:
diff changeset
474 Invalid::~Invalid() {}
anatofuz
parents:
diff changeset
475
anatofuz
parents:
diff changeset
476 namespace PR30361 {
anatofuz
parents:
diff changeset
477 template <typename T>
anatofuz
parents:
diff changeset
478 struct C1 {
anatofuz
parents:
diff changeset
479 ~C1() {}
anatofuz
parents:
diff changeset
480 operator C1<T>* () { return nullptr; }
anatofuz
parents:
diff changeset
481 void foo1();
anatofuz
parents:
diff changeset
482 };
anatofuz
parents:
diff changeset
483
anatofuz
parents:
diff changeset
484 template<typename T>
anatofuz
parents:
diff changeset
485 void C1<T>::foo1() {
anatofuz
parents:
diff changeset
486 C1::operator C1<T>*();
anatofuz
parents:
diff changeset
487 C1::~C1();
anatofuz
parents:
diff changeset
488 }
anatofuz
parents:
diff changeset
489
anatofuz
parents:
diff changeset
490 void foo1() {
anatofuz
parents:
diff changeset
491 C1<int> x;
anatofuz
parents:
diff changeset
492 x.foo1();
anatofuz
parents:
diff changeset
493 }
anatofuz
parents:
diff changeset
494 }
anatofuz
parents:
diff changeset
495
anatofuz
parents:
diff changeset
496 namespace DtorTypedef {
anatofuz
parents:
diff changeset
497 struct A { ~A(); };
anatofuz
parents:
diff changeset
498 using A = A;
anatofuz
parents:
diff changeset
499 DtorTypedef::A::~A() {}
anatofuz
parents:
diff changeset
500
anatofuz
parents:
diff changeset
501 // This is invalid, but compilers accept it.
anatofuz
parents:
diff changeset
502 struct B { ~B(); };
anatofuz
parents:
diff changeset
503 namespace N { using B = B; }
anatofuz
parents:
diff changeset
504 N::B::~B() {} // expected-error {{destructor cannot be declared using a type alias}}
anatofuz
parents:
diff changeset
505
anatofuz
parents:
diff changeset
506 #pragma clang diagnostic push
anatofuz
parents:
diff changeset
507 #pragma clang diagnostic ignored "-Wdtor-typedef"
anatofuz
parents:
diff changeset
508 struct C { ~C(); };
anatofuz
parents:
diff changeset
509 namespace N { using C = C; }
anatofuz
parents:
diff changeset
510 N::C::~C() {}
anatofuz
parents:
diff changeset
511 #pragma clang diagnostic pop
anatofuz
parents:
diff changeset
512 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
513
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
514 // Ignore ambiguity errors in destructor name lookup. This matches the observed
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
515 // behavior of ICC, and is compatible with the observed behavior of GCC (which
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
516 // appears to ignore lookups that result in ambiguity) and MSVC (which appears
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
517 // to perform the lookups in the opposite order from Clang).
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
518 namespace PR44978 {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
519 // All compilers accept this despite it being clearly ill-formed per the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
520 // current wording.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
521 namespace n {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
522 class Foo {}; // expected-note {{found}}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
523 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
524 class Foo {}; // expected-note {{found}}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
525 using namespace n;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
526 static void func(n::Foo *p) { p->~Foo(); } // expected-warning {{ambiguous}}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
527
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
528 // GCC rejects this case, ICC accepts, despite the class member lookup being
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
529 // ambiguous.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
530 struct Z;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
531 struct X { using T = Z; }; // expected-note {{found}}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
532 struct Y { using T = int; }; // expected-note {{found}}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
533 struct Z : X, Y {};
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
534 void f(Z *p) { p->~T(); } // expected-warning {{ambiguous}}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
535
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
536 // GCC accepts this and ignores the ambiguous class member lookup.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
537 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
538 // FIXME: We should warn on the ambiguity here too, but that requires us to
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
539 // keep doing lookups after we've already found the type we want.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
540 using T = Z;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
541 void g(Z *p) { p->~T(); }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
542
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
543 // ICC accepts this and ignores the ambiguous unqualified lookup.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
544 struct Q {};
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
545 namespace { using U = Q; } // expected-note {{candidate}} expected-note {{found}}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
546 using U = int; // expected-note {{candidate}} expected-note {{found}}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
547 void f(Q *p) { p->~U(); } // expected-warning {{ambiguous}}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
548
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
549 // We still diagnose if the unqualified lookup is dependent, though.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
550 template<typename T> void f(T *p) { p->~U(); } // expected-error {{ambiguous}}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
551 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
552
150
anatofuz
parents:
diff changeset
553 #endif // BE_THE_HEADER