blob: ec437f7101e47b53c09ddec48552018132297feb [file] [log] [blame]
Argyrios Kyrtzidisdef4e2a2011-01-31 07:05:00 +00001// RUN: %clang_cc1 -fsyntax-only -Wnon-virtual-dtor -verify %s
Douglas Gregor42a552f2008-11-05 20:51:48 +00002class A {
3public:
4 ~A();
5};
6
7class B {
8public:
9 ~B() { }
10};
11
12class C {
13public:
14 (~C)() { }
15};
16
17struct D {
18 static void ~D(int, ...) const { } // \
19 // expected-error{{type qualifier is not allowed on this function}} \
20 // expected-error{{destructor cannot be declared 'static'}} \
Douglas Gregor42a552f2008-11-05 20:51:48 +000021 // expected-error{{destructor cannot have any parameters}} \
Douglas Gregord92ec472010-07-01 05:10:53 +000022 // expected-error{{destructor cannot be variadic}} \
23 // expected-error{{destructor cannot have a return type}} \
24 // expected-error{{'const' qualifier is not allowed on a destructor}}
Douglas Gregor42a552f2008-11-05 20:51:48 +000025};
26
Chris Lattner65401802009-04-25 08:28:21 +000027struct D2 {
28 void ~D2() { } // \
29 // expected-error{{destructor cannot have a return type}}
30};
31
Chris Lattnereaaebc72009-04-25 08:06:05 +000032
Douglas Gregor42a552f2008-11-05 20:51:48 +000033struct E;
34
35typedef E E_typedef;
36struct E {
John McCall7c2342d2010-03-10 11:27:22 +000037 ~E_typedef(); // expected-error{{destructor cannot be declared using a typedef 'E_typedef' (aka 'E') of the class name}}
Douglas Gregor42a552f2008-11-05 20:51:48 +000038};
39
40struct F {
Chris Lattner5f4a6822008-11-23 23:12:31 +000041 (~F)(); // expected-note {{previous declaration is here}}
42 ~F(); // expected-error {{destructor cannot be redeclared}}
Douglas Gregor42a552f2008-11-05 20:51:48 +000043};
44
Douglas Gregor124b8782010-02-16 19:09:40 +000045~; // expected-error {{expected a class name after '~' to name a destructor}}
Fariborz Jahanian76ed9cb2009-07-20 22:41:12 +000046~undef(); // expected-error {{expected the class name after '~' to name a destructor}}
Douglas Gregor124b8782010-02-16 19:09:40 +000047~operator+(int, int); // expected-error {{expected a class name after '~' to name a destructor}}
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +000048~F(){} // expected-error {{destructor must be a non-static member function}}
Douglas Gregor9d350972008-12-12 08:25:50 +000049
50struct G {
51 ~G();
52};
53
54G::~G() { }
55
Anders Carlsson7786d1c2009-04-30 23:18:11 +000056// <rdar://problem/6841210>
57struct H {
58 ~H(void) { }
59};
Fariborz Jahanianc19f9592009-07-21 15:28:50 +000060
61struct X {};
62
63struct Y {
64 ~X(); // expected-error {{expected the class name after '~' to name the enclosing class}}
65};
Douglas Gregor333de062010-02-25 18:11:54 +000066
67namespace PR6421 {
68 class T; // expected-note{{forward declaration}}
69
Douglas Gregor200b2922010-09-17 22:25:06 +000070 class QGenericArgument // expected-note{{declared here}}
Douglas Gregor333de062010-02-25 18:11:54 +000071 {
72 template<typename U>
73 void foo(T t) // expected-error{{variable has incomplete type}}
74 { }
75
76 void disconnect()
77 {
78 T* t;
Douglas Gregor200b2922010-09-17 22:25:06 +000079 bob<QGenericArgument>(t); // expected-error{{undeclared identifier 'bob'}} \
80 // expected-error{{does not refer to a value}}
Douglas Gregor333de062010-02-25 18:11:54 +000081 }
82 };
83}
Douglas Gregor9c127392010-03-26 06:57:13 +000084
85namespace PR6709 {
86 template<class T> class X { T v; ~X() { ++*v; } };
87 void a(X<int> x) {}
88}
Douglas Gregord92ec472010-07-01 05:10:53 +000089
90struct X0 { virtual ~X0() throw(); };
91struct X1 : public X0 { };
John McCall5efd91a2010-07-03 18:33:00 +000092
93// Make sure we instantiate operator deletes when building a virtual
94// destructor.
95namespace test6 {
96 template <class T> class A {
97 public:
Benjamin Kramer6b855122010-07-03 20:33:13 +000098 void *operator new(__SIZE_TYPE__);
John McCall5efd91a2010-07-03 18:33:00 +000099 void operator delete(void *p) {
100 T::deleteIt(p); // expected-error {{type 'int' cannot be used prior to '::'}}
101 }
102
Nick Lewycky2a5f99e2010-11-25 00:35:20 +0000103 virtual ~A() {}
John McCall5efd91a2010-07-03 18:33:00 +0000104 };
105
Nick Lewycky2a5f99e2010-11-25 00:35:20 +0000106 class B : A<int> { B(); }; // expected-note {{in instantiation of member function 'test6::A<int>::operator delete' requested here}}
107 B::B() {}
John McCall5efd91a2010-07-03 18:33:00 +0000108}
John McCallfcadea22010-08-12 00:57:17 +0000109
110// Make sure classes are marked invalid when they have invalid
111// members. This avoids a crash-on-invalid.
112namespace test7 {
113 struct A {
114 ~A() const; // expected-error {{'const' qualifier is not allowed on a destructor}}
115 };
116 struct B : A {};
117
118 void test() {
119 B *b;
120 b->~B();
121 }
122}
Argyrios Kyrtzidisdef4e2a2011-01-31 07:05:00 +0000123
124namespace nonvirtualdtor {
125struct S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
126 virtual void m();
127};
128
129struct S2 {
130 ~S2(); // expected-warning {{has virtual functions but non-virtual destructor}}
131 virtual void m();
132};
133
134struct S3 : public S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
135 virtual void m();
136};
137
138struct S4 : public S2 { // expected-warning {{has virtual functions but non-virtual destructor}}
139 virtual void m();
140};
141
142struct B {
143 virtual ~B();
144 virtual void m();
145};
146
147struct S5 : public B {
148 virtual void m();
149};
Argyrios Kyrtzidis9641fc82011-01-31 17:10:25 +0000150
151struct S6 {
152 virtual void m();
153private:
154 ~S6();
155};
156
157struct S7 {
158 virtual void m();
159protected:
160 ~S7();
161};
Argyrios Kyrtzidis668fdd82011-02-02 18:47:41 +0000162
163template<class T> class TS : public B {
164 virtual void m();
165};
166
167TS<int> baz;
168
169template<class T> class TS2 { // expected-warning {{'nonvirtualdtor::TS2<int>' has virtual functions but non-virtual destructor}}
170 virtual void m();
171};
172
173TS2<int> foo; // expected-note {{instantiation}}
Argyrios Kyrtzidisdef4e2a2011-01-31 07:05:00 +0000174}