1 // RUN: %clang_cc1 %s  -fdelayed-template-parsing -fcxx-exceptions -fsyntax-only -Wexceptions -verify -std=c++11
2 struct A_ShouldDiag {
3   ~A_ShouldDiag(); // implicitly noexcept(true)
4 };
5 A_ShouldDiag::~A_ShouldDiag() { // expected-note {{destructor has a implicit non-throwing exception specification}}
6   throw 1; // expected-warning {{has a non-throwing exception specification but can still throw}}
7 }
8 struct B_ShouldDiag {
9   int i;
10   ~B_ShouldDiag() noexcept(true) {} //no disg, no throw stmt
11 };
12 struct R_ShouldDiag : A_ShouldDiag {
13   B_ShouldDiag b;
14   ~R_ShouldDiag() { // expected-note  {{destructor has a implicit non-throwing exception specification}}
15     throw 1; // expected-warning {{has a non-throwing exception specification but}}
16   }
17 };
18 
19 struct M_ShouldNotDiag {
20   B_ShouldDiag b;
21   ~M_ShouldNotDiag() noexcept(false);
22 };
23 
24 M_ShouldNotDiag::~M_ShouldNotDiag() noexcept(false) {
25   throw 1;
26 }
27 
28 struct N_ShouldDiag {
29   B_ShouldDiag b;
30   ~N_ShouldDiag(); //implicitly noexcept(true)
31 };
32 
33 N_ShouldDiag::~N_ShouldDiag() { // expected-note  {{destructor has a implicit non-throwing exception specification}}
34   throw 1; // expected-warning {{has a non-throwing exception specification but}}
35 }
36 struct X_ShouldDiag {
37   B_ShouldDiag b;
38   ~X_ShouldDiag() noexcept { // expected-note  {{destructor has a non-throwing exception}}
39     throw 1; // expected-warning {{has a non-throwing exception specification but}}
40   }
41 };
42 struct Y_ShouldDiag : A_ShouldDiag {
43   ~Y_ShouldDiag() noexcept(true) { // expected-note  {{destructor has a non-throwing exception specification}}
44     throw 1; // expected-warning {{has a non-throwing exception specification but}}
45   }
46 };
47 struct C_ShouldNotDiag {
48   int i;
49   ~C_ShouldNotDiag() noexcept(false) {}
50 };
51 struct D_ShouldNotDiag {
52   C_ShouldNotDiag c;
53   ~D_ShouldNotDiag() { //implicitly noexcept(false)
54     throw 1;
55   }
56 };
57 struct E_ShouldNotDiag {
58   C_ShouldNotDiag c;
59   ~E_ShouldNotDiag(); //implicitly noexcept(false)
60 };
61 E_ShouldNotDiag::~E_ShouldNotDiag() //implicitly noexcept(false)
62 {
63   throw 1;
64 }
65 
66 template <typename T>
67 class A1_ShouldDiag {
68   T b;
69 
70 public:
71   ~A1_ShouldDiag() { // expected-note  {{destructor has a implicit non-throwing exception specification}}
72     throw 1; // expected-warning {{has a non-throwing exception specification but}}
73   }
74 };
75 template <typename T>
76 struct B1_ShouldDiag {
77   T i;
78   ~B1_ShouldDiag() noexcept(true) {}
79 };
80 template <typename T>
81 struct R1_ShouldDiag : A1_ShouldDiag<T> //expected-note {{in instantiation of member function}}
82 {
83   B1_ShouldDiag<T> b;
84   ~R1_ShouldDiag() { // expected-note  {{destructor has a implicit non-throwing exception specification}}
85     throw 1; // expected-warning {{has a non-throwing exception specification but}}
86   }
87 };
88 template <typename T>
89 struct S1_ShouldDiag : A1_ShouldDiag<T> {
90   B1_ShouldDiag<T> b;
91   ~S1_ShouldDiag() noexcept { // expected-note  {{destructor has a non-throwing exception specification}}
92     throw 1; // expected-warning {{has a non-throwing exception specification but}}
93   }
94 };
95 void operator delete(void *ptr) noexcept { // expected-note  {{deallocator has a non-throwing exception specification}}
96   throw 1; // expected-warning {{has a non-throwing exception specification but}}
97 }
98 struct except_fun {
99   static const bool i = false;
100 };
101 struct noexcept_fun {
102   static const bool i = true;
103 };
104 template <typename T>
105 struct dependent_warn {
106   ~dependent_warn() noexcept(T::i) {
107     throw 1;
108   }
109 };
110 template <typename T>
111 struct dependent_warn_noexcept {
112   ~dependent_warn_noexcept() noexcept(T::i) { // expected-note  {{destructor has a non-throwing exception specification}}
113     throw 1; // expected-warning {{has a non-throwing exception specification but}}
114   }
115 };
116 template <typename T>
117 struct dependent_warn_both {
118   ~dependent_warn_both() noexcept(T::i) { // expected-note  {{destructor has a non-throwing exception specification}}
119     throw 1; // expected-warning {{has a non-throwing exception specification but}}
120   }
121 };
122 void foo() noexcept { //expected-note {{function declared non-throwing here}}
123   throw 1; // expected-warning {{has a non-throwing exception specification but}}
124 }
125 struct Throws {
126   ~Throws() noexcept(false);
127 };
128 
129 struct ShouldDiagnose {
130   Throws T;
131   ~ShouldDiagnose() noexcept { //expected-note {{destructor has a non-throwing exception specification}}
132     throw; // expected-warning {{has a non-throwing exception specification but}}
133   }
134 };
135 struct ShouldNotDiagnose {
136   Throws T;
137   ~ShouldNotDiagnose() {
138     throw;
139   }
140 };
141 
142 void bar_ShouldNotDiag() noexcept {
143   try {
144     throw 1;
145   } catch (...) {
146   }
147 }
148 void f_ShouldNotDiag() noexcept {
149   try {
150     throw 12;
151   } catch (int) {
152   }
153 }
154 void g_ShouldNotDiag() noexcept {
155   try {
156     throw 12;
157   } catch (...) {
158   }
159 }
160 
161 void h_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
162   try {
163     throw 12; // expected-warning {{has a non-throwing exception specification but}}
164   } catch (const char *) {
165   }
166 }
167 
168 void i_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
169   try {
170     throw 12;
171   } catch (int) {
172     throw; // expected-warning {{has a non-throwing exception specification but}}
173   }
174 }
175 void j_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
176   try {
177     throw 12;
178   } catch (int) {
179     throw "haha"; // expected-warning {{has a non-throwing exception specification but}}
180   }
181 }
182 
183 void k_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
184   try {
185     throw 12;
186   } catch (...) {
187     throw; // expected-warning {{has a non-throwing exception specification but}}
188   }
189 }
190 
191 void loo_ShouldDiag(int i) noexcept { //expected-note {{function declared non-throwing here}}
192   if (i)
193     try {
194       throw 12;
195     } catch (int) {
196       throw "haha"; //expected-warning {{has a non-throwing exception specification but}}
197     }
198   i = 10;
199 }
200 
201 void loo1_ShouldNotDiag() noexcept {
202   if (0)
203     throw 12;
204 }
205 
206 void loo2_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
207   if (1)
208     throw 12; // expected-warning {{has a non-throwing exception specification but}}
209 }
210 struct S {};
211 
212 void l_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
213   try {
214     throw S{}; //expected-warning {{has a non-throwing exception specification but}}
215   } catch (S *s) {
216   }
217 }
218 
219 void m_ShouldNotDiag() noexcept {
220   try {
221     const S &s = S{};
222     throw s;
223   } catch (S s) {
224   }
225 }
226 void n_ShouldNotDiag() noexcept {
227   try {
228     S s = S{};
229     throw s;
230   } catch (const S &s) {
231   }
232 }
233 void o_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
234   try {
235     throw; //expected-warning {{has a non-throwing exception specification but}}
236   } catch (...) {
237   }
238 }
239 
240 #define NOEXCEPT noexcept
241 void with_macro() NOEXCEPT { //expected-note {{function declared non-throwing here}}
242   throw 1; // expected-warning {{has a non-throwing exception specification but}}
243 }
244 
245 void with_try_block() try {
246   throw 2;
247 } catch (...) {
248 }
249 
250 void with_try_block1() noexcept try { //expected-note {{function declared non-throwing here}}
251   throw 2; // expected-warning {{has a non-throwing exception specification but}}
252 } catch (char *) {
253 }
254 
255 namespace derived {
256 struct B {};
257 struct D: B {};
258 void goodPlain() noexcept {
259   try {
260     throw D();
261   } catch (B) {}
262 }
263 void goodReference() noexcept {
264   try {
265     throw D();
266   } catch (B &) {}
267 }
268 void goodPointer() noexcept {
269   D d;
270   try {
271     throw &d;
272   } catch (B *) {}
273 }
274 void badPlain() noexcept { //expected-note {{function declared non-throwing here}}
275   try {
276     throw B(); // expected-warning {{'badPlain' has a non-throwing exception specification but can still throw}}
277   } catch (D) {}
278 }
279 void badReference() noexcept { //expected-note {{function declared non-throwing here}}
280   try {
281     throw B(); // expected-warning {{'badReference' has a non-throwing exception specification but can still throw}}
282   } catch (D &) {}
283 }
284 void badPointer() noexcept { //expected-note {{function declared non-throwing here}}
285   B b;
286   try {
287     throw &b; // expected-warning {{'badPointer' has a non-throwing exception specification but can still throw}}
288   } catch (D *) {}
289 }
290 }
291 
292 int main() {
293   R1_ShouldDiag<int> o; //expected-note {{in instantiation of member function}}
294   S1_ShouldDiag<int> b; //expected-note {{in instantiation of member function}}
295   dependent_warn<except_fun> f;
296   dependent_warn_noexcept<noexcept_fun> f1; //expected-note {{in instantiation of member function}}
297   dependent_warn_both<except_fun> f2;
298   dependent_warn_both<noexcept_fun> f3; //expected-note {{in instantiation of member function}}
299   ShouldDiagnose obj;
300   ShouldNotDiagnose obj1;
301 }
302