warn-throw-out-noexcept-func.cpp
13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// RUN: %clang_cc1 %s -fdelayed-template-parsing -fcxx-exceptions -fsyntax-only -Wexceptions -verify -fdeclspec -std=c++17
struct A_ShouldDiag {
~A_ShouldDiag(); // implicitly noexcept(true)
};
A_ShouldDiag::~A_ShouldDiag() { // expected-note {{destructor has a implicit non-throwing exception specification}}
throw 1; // expected-warning {{has a non-throwing exception specification but can still throw}}
}
struct B_ShouldDiag {
int i;
~B_ShouldDiag() noexcept(true) {} //no disg, no throw stmt
};
struct R_ShouldDiag : A_ShouldDiag {
B_ShouldDiag b;
~R_ShouldDiag() { // expected-note {{destructor has a implicit non-throwing exception specification}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
__attribute__((nothrow)) R_ShouldDiag() {// expected-note {{function declared non-throwing here}}
throw 1;// expected-warning {{has a non-throwing exception specification but}}
}
void __attribute__((nothrow)) SomeThrow() {// expected-note {{function declared non-throwing here}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
void __declspec(nothrow) SomeDeclspecThrow() {// expected-note {{function declared non-throwing here}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
};
struct M_ShouldNotDiag {
B_ShouldDiag b;
~M_ShouldNotDiag() noexcept(false);
};
M_ShouldNotDiag::~M_ShouldNotDiag() noexcept(false) {
throw 1;
}
struct N_ShouldDiag {
B_ShouldDiag b;
~N_ShouldDiag(); //implicitly noexcept(true)
};
N_ShouldDiag::~N_ShouldDiag() { // expected-note {{destructor has a implicit non-throwing exception specification}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
struct X_ShouldDiag {
B_ShouldDiag b;
~X_ShouldDiag() noexcept { // expected-note {{destructor has a non-throwing exception}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
};
struct Y_ShouldDiag : A_ShouldDiag {
~Y_ShouldDiag() noexcept(true) { // expected-note {{destructor has a non-throwing exception specification}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
};
struct C_ShouldNotDiag {
int i;
~C_ShouldNotDiag() noexcept(false) {}
};
struct D_ShouldNotDiag {
C_ShouldNotDiag c;
~D_ShouldNotDiag() { //implicitly noexcept(false)
throw 1;
}
};
struct E_ShouldNotDiag {
C_ShouldNotDiag c;
~E_ShouldNotDiag(); //implicitly noexcept(false)
};
E_ShouldNotDiag::~E_ShouldNotDiag() //implicitly noexcept(false)
{
throw 1;
}
template <typename T>
class A1_ShouldDiag {
T b;
public:
~A1_ShouldDiag() { // expected-note {{destructor has a implicit non-throwing exception specification}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
};
template <typename T>
struct B1_ShouldDiag {
T i;
~B1_ShouldDiag() noexcept(true) {}
};
template <typename T>
struct R1_ShouldDiag : A1_ShouldDiag<T> //expected-note {{in instantiation of member function}}
{
B1_ShouldDiag<T> b;
~R1_ShouldDiag() { // expected-note {{destructor has a implicit non-throwing exception specification}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
};
template <typename T>
struct S1_ShouldDiag : A1_ShouldDiag<T> {
B1_ShouldDiag<T> b;
~S1_ShouldDiag() noexcept { // expected-note {{destructor has a non-throwing exception specification}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
};
void operator delete(void *ptr) noexcept { // expected-note {{deallocator has a non-throwing exception specification}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
struct except_fun {
static const bool i = false;
};
struct noexcept_fun {
static const bool i = true;
};
template <typename T>
struct dependent_warn {
~dependent_warn() noexcept(T::i) {
throw 1;
}
};
template <typename T>
struct dependent_warn_noexcept {
~dependent_warn_noexcept() noexcept(T::i) { // expected-note {{destructor has a non-throwing exception specification}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
};
template <typename T>
struct dependent_warn_both {
~dependent_warn_both() noexcept(T::i) { // expected-note {{destructor has a non-throwing exception specification}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
};
void foo() noexcept { //expected-note {{function declared non-throwing here}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
struct Throws {
~Throws() noexcept(false);
};
struct ShouldDiagnose {
Throws T;
~ShouldDiagnose() noexcept { //expected-note {{destructor has a non-throwing exception specification}}
throw; // expected-warning {{has a non-throwing exception specification but}}
}
};
struct ShouldNotDiagnose {
Throws T;
~ShouldNotDiagnose() {
throw;
}
};
void bar_ShouldNotDiag() noexcept {
try {
throw 1;
} catch (...) {
}
}
void f_ShouldNotDiag() noexcept {
try {
throw 12;
} catch (int) {
}
}
void g_ShouldNotDiag() noexcept {
try {
throw 12;
} catch (...) {
}
}
void h_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
try {
throw 12; // expected-warning {{has a non-throwing exception specification but}}
} catch (const char *) {
}
}
void i_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
try {
throw 12;
} catch (int) {
throw; // expected-warning {{has a non-throwing exception specification but}}
}
}
void j_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
try {
throw 12;
} catch (int) {
throw "haha"; // expected-warning {{has a non-throwing exception specification but}}
}
}
void k_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
try {
throw 12;
} catch (...) {
throw; // expected-warning {{has a non-throwing exception specification but}}
}
}
void loo_ShouldDiag(int i) noexcept { //expected-note {{function declared non-throwing here}}
if (i)
try {
throw 12;
} catch (int) {
throw "haha"; //expected-warning {{has a non-throwing exception specification but}}
}
i = 10;
}
void loo1_ShouldNotDiag() noexcept {
if (0)
throw 12;
}
void loo2_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
if (1)
throw 12; // expected-warning {{has a non-throwing exception specification but}}
}
struct S {};
void l_ShouldDiag() noexcept { //expected-note {{function declared non-throwing here}}
try {
throw S{}; //expected-warning {{has a non-throwing exception specification but}}
} catch (S *s) {
}
}
void m_ShouldNotDiag() noexcept {
try {
const S &s = S{};
throw s;
} catch (S s) {
}
}
void n_ShouldNotDiag() noexcept {
try {
S s = S{};
throw s;
} catch (const S &s) {
}
}
// As seen in p34973, this should not throw the warning. If there is an active
// exception, catch(...) catches everything.
void o_ShouldNotDiag() noexcept {
try {
throw;
} catch (...) {
}
}
void p_ShouldNotDiag() noexcept {
// Don't warn here: it's possible that the user arranges to only call this
// when the active exception is of type 'int'.
try {
throw;
} catch (int){
}
}
void q_ShouldNotDiag() noexcept {
try {
throw;
} catch (int){
} catch (...){
}
}
#define NOEXCEPT noexcept
void with_macro() NOEXCEPT { //expected-note {{function declared non-throwing here}}
throw 1; // expected-warning {{has a non-throwing exception specification but}}
}
void with_try_block() try {
throw 2;
} catch (...) {
}
void with_try_block1() noexcept try { //expected-note {{function declared non-throwing here}}
throw 2; // expected-warning {{has a non-throwing exception specification but}}
} catch (char *) {
}
namespace derived {
struct B {};
struct D: B {};
void goodPlain() noexcept {
try {
throw D();
} catch (B) {}
}
void goodReference() noexcept {
try {
throw D();
} catch (B &) {}
}
void goodPointer() noexcept {
D d;
try {
throw &d;
} catch (B *) {}
}
void badPlain() noexcept { //expected-note {{function declared non-throwing here}}
try {
throw B(); // expected-warning {{'badPlain' has a non-throwing exception specification but can still throw}}
} catch (D) {}
}
void badReference() noexcept { //expected-note {{function declared non-throwing here}}
try {
throw B(); // expected-warning {{'badReference' has a non-throwing exception specification but can still throw}}
} catch (D &) {}
}
void badPointer() noexcept { //expected-note {{function declared non-throwing here}}
B b;
try {
throw &b; // expected-warning {{'badPointer' has a non-throwing exception specification but can still throw}}
} catch (D *) {}
}
}
int main() {
R1_ShouldDiag<int> o; //expected-note {{in instantiation of member function}}
S1_ShouldDiag<int> b; //expected-note {{in instantiation of member function}}
dependent_warn<except_fun> f;
dependent_warn_noexcept<noexcept_fun> f1; //expected-note {{in instantiation of member function}}
dependent_warn_both<except_fun> f2;
dependent_warn_both<noexcept_fun> f3; //expected-note {{in instantiation of member function}}
ShouldDiagnose obj;
ShouldNotDiagnose obj1;
}
namespace ExceptionInNamespace {
namespace N {
struct E {};
}
void run() throw() {
try {
throw N::E();
} catch (const N::E &e) {
}
}
}
namespace HandlerSpecialCases {
struct A {};
using CA = const A;
struct B : A {};
using CB = const B;
struct AmbigBase {};
struct AmbigMiddle : AmbigBase {};
struct AmbigDerived : AmbigBase, AmbigMiddle {}; // expected-warning {{inaccessible}}
struct PrivateBase {};
struct PrivateDerived : private PrivateBase { friend void bad3() throw(); };
void good() throw() {
try { throw CA(); } catch (volatile A&) {}
try { throw B(); } catch (A&) {}
try { throw B(); } catch (const volatile A&) {}
try { throw CB(); } catch (A&) {}
try { throw (int*)0; } catch (void* const volatile) {}
try { throw (int*)0; } catch (void* const &) {}
try { throw (B*)0; } catch (A*) {}
try { throw (B*)0; } catch (A* const &) {}
try { throw (void(*)() noexcept)0; } catch (void (*)()) {}
try { throw (void(*)() noexcept)0; } catch (void (*const &)()) {}
try { throw (int**)0; } catch (const int * const*) {}
try { throw (int**)0; } catch (const int * const* const&) {}
try { throw nullptr; } catch (int*) {}
try { throw nullptr; } catch (int* const&) {}
}
void bad1() throw() { // expected-note {{here}}
try { throw A(); } catch (const B&) {} // expected-warning {{still throw}}
}
void bad2() throw() { // expected-note {{here}}
try { throw AmbigDerived(); } catch (const AmbigBase&) {} // expected-warning {{still throw}}
}
void bad3() throw() { // expected-note {{here}}
try { throw PrivateDerived(); } catch (const PrivateBase&) {} // expected-warning {{still throw}}
}
void bad4() throw() { // expected-note {{here}}
try { throw (int*)0; } catch (void* &) {} // expected-warning {{still throw}}
}
void bad5() throw() { // expected-note {{here}}
try { throw (int*)0; } catch (void* const volatile &) {} // expected-warning {{still throw}}
}
void bad6() throw() { // expected-note {{here}}
try { throw (int* volatile)0; } catch (void* const volatile &) {} // expected-warning {{still throw}}
}
void bad7() throw() { // expected-note {{here}}
try { throw (AmbigDerived*)0; } catch (AmbigBase*) {} // expected-warning {{still throw}}
}
void bad8() throw() { // expected-note {{here}}
try { throw (PrivateDerived*)0; } catch (PrivateBase*) {} // expected-warning {{still throw}}
}
void bad9() throw() { // expected-note {{here}}
try { throw (B*)0; } catch (A* &) {} // expected-warning {{still throw}}
}
void bad10() throw() { // expected-note {{here}}
try { throw (void(*)())0; } catch (void (*)() noexcept) {} // expected-warning {{still throw}}
}
void bad11() throw() { // expected-note {{here}}
try { throw (int**)0; } catch (const int **) {} // expected-warning {{still throw}}
}
void bad12() throw() { // expected-note {{here}}
try { throw nullptr; } catch (int) {} // expected-warning {{still throw}}
}
}
namespace NestedTry {
void f() noexcept {
try {
try {
throw 0;
} catch (float) {}
} catch (int) {}
}
struct A { [[noreturn]] ~A(); };
void g() noexcept { // expected-note {{here}}
try {
try {
throw 0; // expected-warning {{still throw}}
} catch (float) {}
} catch (const char*) {}
}
void h() noexcept { // expected-note {{here}}
try {
try {
throw 0;
} catch (float) {}
} catch (int) {
throw; // expected-warning {{still throw}}
}
}
// FIXME: Ideally, this should still warn; we can track which types are
// potentially thrown by the rethrow.
void i() noexcept {
try {
try {
throw 0;
} catch (int) {
throw;
}
} catch (float) {}
}
// FIXME: Ideally, this should not warn: the second catch block is
// unreachable.
void j() noexcept { // expected-note {{here}}
try {
try {
throw 0;
} catch (int) {}
} catch (float) {
throw; // expected-warning {{still throw}}
}
}
}