warn-unreachable.cpp
9.26 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
// RUN: %clang_cc1 %s -fcxx-exceptions -fexceptions -fsyntax-only -verify -fblocks -std=c++11 -Wunreachable-code-aggressive -Wno-unused-value -Wno-tautological-compare
int &halt() __attribute__((noreturn));
int &live();
int dead();
int liveti() throw(int);
int (*livetip)() throw(int);
int test1() {
try {
live();
} catch (int i) {
live();
}
return 1;
}
void test2() {
try {
live();
} catch (int i) {
live();
}
try {
liveti();
} catch (int i) {
live();
}
try {
livetip();
} catch (int i) {
live();
}
throw 1;
dead(); // expected-warning {{will never be executed}}
}
void test3() {
halt()
--; // expected-warning {{will never be executed}}
// FIXME: The unreachable part is just the '?', but really all of this
// code is unreachable and shouldn't be separately reported.
halt() // expected-warning {{will never be executed}}
?
dead() : dead();
live(),
float
(halt()); // expected-warning {{will never be executed}}
}
namespace Test4 {
struct S {
int mem;
} s;
S &foor();
void test4() {
halt(), foor()// expected-warning {{will never be executed}}
.mem;
}
}
namespace Test5 {
struct S {
int mem;
} s;
S &foonr() __attribute__((noreturn));
void test5() {
foonr()
.mem; // expected-warning {{will never be executed}}
}
}
void test6() {
struct S {
~S() { }
S(int i) { }
};
live(),
S
(halt()); // expected-warning {{will never be executed}}
}
// Don't warn about unreachable code in template instantiations, as
// they may only be unreachable in that specific instantiation.
void isUnreachable();
template <typename T> void test_unreachable_templates() {
T::foo();
isUnreachable(); // no-warning
}
struct TestUnreachableA {
static void foo() __attribute__((noreturn));
};
struct TestUnreachableB {
static void foo();
};
void test_unreachable_templates_harness() {
test_unreachable_templates<TestUnreachableA>();
test_unreachable_templates<TestUnreachableB>();
}
// Do warn about explicit template specializations, as they represent
// actual concrete functions that somebody wrote.
template <typename T> void funcToSpecialize() {}
template <> void funcToSpecialize<int>() {
halt();
dead(); // expected-warning {{will never be executed}}
}
// Handle 'try' code dominating a dead return.
enum PR19040_test_return_t
{ PR19040_TEST_FAILURE };
namespace PR19040_libtest
{
class A {
public:
~A ();
};
}
PR19040_test_return_t PR19040_fn1 ()
{
try
{
throw PR19040_libtest::A ();
} catch (...)
{
return PR19040_TEST_FAILURE;
}
return PR19040_TEST_FAILURE; // expected-warning {{will never be executed}}
}
__attribute__((noreturn))
void raze();
namespace std {
template<typename T> struct basic_string {
basic_string(const T* x) {}
~basic_string() {};
};
typedef basic_string<char> string;
}
std::string testStr() {
raze();
return ""; // expected-warning {{'return' will never be executed}}
}
std::string testStrWarn(const char *s) {
raze();
return s; // expected-warning {{will never be executed}}
}
bool testBool() {
raze();
return true; // expected-warning {{'return' will never be executed}}
}
static const bool ConditionVar = 1;
int test_global_as_conditionVariable() {
if (ConditionVar)
return 1;
return 0; // no-warning
}
// Handle unreachable temporary destructors.
class A {
public:
A();
~A();
};
__attribute__((noreturn))
void raze(const A& x);
void test_with_unreachable_tmp_dtors(int x) {
raze(x ? A() : A()); // no-warning
}
// Test sizeof - sizeof in enum declaration.
enum { BrownCow = sizeof(long) - sizeof(char) };
enum { CowBrown = 8 - 1 };
int test_enum_sizeof_arithmetic() {
if (BrownCow)
return 1;
return 2;
}
int test_enum_arithmetic() {
if (CowBrown)
return 1;
return 2; // expected-warning {{never be executed}}
}
int test_arithmetic() {
if (8 -1)
return 1;
return 2; // expected-warning {{never be executed}}
}
int test_treat_const_bool_local_as_config_value() {
const bool controlValue = false;
if (!controlValue)
return 1;
test_treat_const_bool_local_as_config_value(); // no-warning
return 0;
}
int test_treat_non_const_bool_local_as_non_config_value() {
bool controlValue = false;
if (!controlValue)
return 1;
// There is no warning here because 'controlValue' isn't really
// a control value at all. The CFG will not treat this
// branch as unreachable.
test_treat_non_const_bool_local_as_non_config_value(); // no-warning
return 0;
}
void test_do_while(int x) {
// Handle trivial expressions with
// implicit casts to bool.
do {
break;
} while (0); // no-warning
}
class Frobozz {
public:
Frobozz(int x);
~Frobozz();
};
Frobozz test_return_object(int flag) {
return Frobozz(flag);
return Frobozz(42); // expected-warning {{'return' will never be executed}}
}
Frobozz test_return_object_control_flow(int flag) {
return Frobozz(flag);
return Frobozz(flag ? 42 : 24); // expected-warning {{code will never be executed}}
}
void somethingToCall();
static constexpr bool isConstExprConfigValue() { return true; }
int test_const_expr_config_value() {
if (isConstExprConfigValue()) {
somethingToCall();
return 0;
}
somethingToCall(); // no-warning
return 1;
}
int test_const_expr_config_value_2() {
if (!isConstExprConfigValue()) {
somethingToCall(); // no-warning
return 0;
}
somethingToCall();
return 1;
}
class Frodo {
public:
static const bool aHobbit = true;
};
void test_static_class_var() {
if (Frodo::aHobbit)
somethingToCall();
else
somethingToCall(); // no-warning
}
void test_static_class_var(Frodo &F) {
if (F.aHobbit)
somethingToCall();
else
somethingToCall(); // no-warning
}
void test_unreachable_for_null_increment() {
for (unsigned i = 0; i < 10 ; ) // no-warning
break;
}
void test_unreachable_forrange_increment() {
int x[10] = { 0 };
for (auto i : x) { // expected-warning {{loop will run at most once (loop increment never executed)}}
break;
}
}
void calledFun() {}
// Test "silencing" with parentheses.
void test_with_paren_silencing(int x) {
if (false) calledFun(); // expected-warning {{will never be executed}} expected-note {{silence by adding parentheses to mark code as explicitly dead}}
if ((false)) calledFun(); // no-warning
if (true) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
calledFun();
else
calledFun(); // expected-warning {{will never be executed}}
if ((true))
calledFun();
else
calledFun(); // no-warning
if (!true) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
calledFun(); // expected-warning {{code will never be executed}}
else
calledFun();
if ((!true))
calledFun(); // no-warning
else
calledFun();
if (!(true))
calledFun(); // no-warning
else
calledFun();
}
void test_with_paren_silencing_impcast(int x) {
if (0) calledFun(); // expected-warning {{will never be executed}} expected-note {{silence by adding parentheses to mark code as explicitly dead}}
if ((0)) calledFun(); // no-warning
if (1) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
calledFun();
else
calledFun(); // expected-warning {{will never be executed}}
if ((1))
calledFun();
else
calledFun(); // no-warning
if (!1) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
calledFun(); // expected-warning {{code will never be executed}}
else
calledFun();
if ((!1))
calledFun(); // no-warning
else
calledFun();
if (!(1))
calledFun(); // no-warning
else
calledFun();
}
void tautological_compare(bool x, int y) {
if (x > 10) // expected-note {{silence}}
calledFun(); // expected-warning {{will never be executed}}
if (10 < x) // expected-note {{silence}}
calledFun(); // expected-warning {{will never be executed}}
if (x == 10) // expected-note {{silence}}
calledFun(); // expected-warning {{will never be executed}}
if (x < 10) // expected-note {{silence}}
calledFun();
else
calledFun(); // expected-warning {{will never be executed}}
if (10 > x) // expected-note {{silence}}
calledFun();
else
calledFun(); // expected-warning {{will never be executed}}
if (x != 10) // expected-note {{silence}}
calledFun();
else
calledFun(); // expected-warning {{will never be executed}}
if (y != 5 && y == 5) // expected-note {{silence}}
calledFun(); // expected-warning {{will never be executed}}
if (y > 5 && y < 4) // expected-note {{silence}}
calledFun(); // expected-warning {{will never be executed}}
if (y < 10 || y > 5) // expected-note {{silence}}
calledFun();
else
calledFun(); // expected-warning {{will never be executed}}
if (y == -1 && y != -1) // expected-note {{silence}}
calledFun(); // expected-warning {{will never be executed}}
// TODO: Extend warning to the following code:
if (x < -1)
calledFun();
if (x == -1)
calledFun();
if (x != -1)
calledFun();
else
calledFun();
if (-1 > x)
calledFun();
else
calledFun();
}