[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Sema / warn-unreachable.c
blob9e3979690ab25191faa32018deaf5e211b47dd46
1 // RUN: %clang_cc1 -fsyntax-only -verify -fblocks -Wunreachable-code-aggressive -Wno-unused-value -Wno-covered-switch-default -I %S/Inputs %s
2 // RUN: %clang_cc1 -fsyntax-only -fblocks -Wunreachable-code-aggressive -Wno-unused-value -Wno-covered-switch-default -fdiagnostics-parseable-fixits -I %S/Inputs %s 2>&1 | FileCheck %s
4 #include "warn-unreachable.h"
6 int halt(void) __attribute__((noreturn));
7 int live(void);
8 int dead(void);
10 void test1(void) {
11 goto c;
13 goto e; // expected-warning {{will never be executed}}
14 c: ;
15 int i;
16 return;
17 goto b; // expected-warning {{will never be executed}}
18 goto a; // expected-warning {{will never be executed}}
20 i = 1;
22 i = 2;
23 goto f;
25 goto d;
26 f: ;
29 void test2(void) {
30 int i;
31 switch (live()) {
32 case 1:
33 halt(),
34 dead(); // expected-warning {{will never be executed}}
36 case 2:
37 live(), halt(),
38 dead(); // expected-warning {{will never be executed}}
40 case 3:
41 live()
42 + // expected-warning {{will never be executed}}
43 halt();
44 dead();
46 case 4:
47 a4:
48 live(),
49 halt();
50 goto a4; // expected-warning {{will never be executed}}
52 case 5:
53 goto a5;
54 c5:
55 dead(); // expected-warning {{will never be executed}}
56 goto b5;
57 a5:
58 live(),
59 halt();
60 b5:
61 goto c5;
63 case 6:
64 if (live())
65 goto e6;
66 live(),
67 halt();
68 d6:
69 dead(); // expected-warning {{will never be executed}}
70 goto b6;
71 c6:
72 dead();
73 goto b6;
74 e6:
75 live(),
76 halt();
77 b6:
78 goto c6;
79 case 7:
80 halt()
82 dead(); // expected-warning {{will never be executed}}
83 - // expected-warning {{will never be executed}}
84 halt();
85 case 8:
87 += // expected-warning {{will never be executed}}
88 halt();
89 case 9:
90 halt()
91 ? // expected-warning {{will never be executed}}
92 dead() : dead();
93 case 10:
94 ( // expected-warning {{will never be executed}}
95 float)halt();
96 case 11: {
97 int a[5];
98 live(),
99 a[halt()
100 ]; // expected-warning {{will never be executed}}
105 enum Cases { C1, C2, C3 };
106 int test_enum_cases(enum Cases C) {
107 switch (C) {
108 case C1:
109 case C2:
110 case C3:
111 return 1;
112 default: {
113 int i = 0; // no-warning
114 ++i;
115 return i;
120 // Handle unreachable code triggered by macro expansions.
121 void __myassert_rtn(const char *, const char *, int, const char *) __attribute__((__noreturn__));
123 #define myassert(e) \
124 (__builtin_expect(!(e), 0) ? __myassert_rtn(__func__, __FILE__, __LINE__, #e) : (void)0)
126 void test_assert(void) {
127 myassert(0 && "unreachable");
128 return; // no-warning
131 // Test case for PR 9774. Tests that dead code in macros aren't warned about.
132 #define MY_MAX(a,b) ((a) >= (b) ? (a) : (b))
133 void PR9774(int *s) {
134 for (int i = 0; i < MY_MAX(2, 3); i++) // no-warning
135 s[i] = 0;
138 // Test case for <rdar://problem/11005770>. We should treat code guarded
139 // by 'x & 0' and 'x * 0' as unreachable.
140 int calledFun(void);
141 void test_mul_and_zero(int x) {
142 if (x & 0) calledFun(); // expected-warning {{will never be executed}}
143 if (0 & x) calledFun(); // expected-warning {{will never be executed}}
144 if (x * 0) calledFun(); // expected-warning {{will never be executed}}
145 if (0 * x) calledFun(); // expected-warning {{will never be executed}}
148 void raze(void) __attribute__((noreturn));
149 void warn_here(void);
151 int test_break_preceded_by_noreturn(int i) {
152 switch (i) {
153 case 1:
154 raze();
155 break; // expected-warning {{'break' will never be executed}}
156 case 2:
157 raze();
158 break; // expected-warning {{'break' will never be executed}}
159 warn_here(); // expected-warning {{will never be executed}}
160 case 3:
161 return 1;
162 break; // expected-warning {{will never be executed}}
163 default:
164 break;
165 break; // expected-warning {{will never be executed}}
167 return i;
170 // Don't warn about unreachable 'default' cases, as that is covered
171 // by -Wcovered-switch-default.
172 typedef enum { Value1 = 1 } MyEnum;
173 void unreachable_default(MyEnum e) {
174 switch (e) {
175 case Value1:
176 calledFun();
177 break;
178 case 2: // expected-warning {{case value not in enumerated type 'MyEnum'}}
179 calledFun();
180 break;
181 default:
182 calledFun(); // no-warning
183 break;
186 void unreachable_in_default(MyEnum e) {
187 switch (e) {
188 default:
189 raze();
190 calledFun(); // expected-warning {{will never be executed}}
191 break;
195 // Don't warn about trivial dead returns.
196 int trivial_dead_return(void) {
197 raze();
198 return ((0)); // expected-warning {{'return' will never be executed}}
201 void trivial_dead_return_void(void) {
202 raze();
203 return; // expected-warning {{'return' will never be executed}}
206 MyEnum trivial_dead_return_enum(void) {
207 raze();
208 return Value1; // expected-warning {{'return' will never be executed}}
211 MyEnum trivial_dead_return_enum_2(int x) {
212 switch (x) {
213 case 1: return 1;
214 case 2: return 2;
215 case 3: return 3;
216 default: return 4;
219 return 2; // expected-warning {{will never be executed}}
222 const char *trivial_dead_return_cstr(void) {
223 raze();
224 return ""; // expected-warning {{return' will never be executed}}
227 char trivial_dead_return_char(void) {
228 raze();
229 return ' '; // expected-warning {{return' will never be executed}}
232 MyEnum nontrivial_dead_return_enum_2(int x) {
233 switch (x) {
234 case 1: return 1;
235 case 2: return 2;
236 case 3: return 3;
237 default: return 4;
240 return calledFun(); // expected-warning {{will never be executed}}
243 enum X { A, B, C };
245 int covered_switch(enum X x) {
246 switch (x) {
247 case A: return 1;
248 case B: return 2;
249 case C: return 3;
251 return 4; // no-warning
254 // Test unreachable code depending on configuration values
255 #define CONFIG_CONSTANT 1
256 int test_config_constant(int x) {
257 if (!CONFIG_CONSTANT) {
258 calledFun(); // no-warning
259 return 1;
261 if (!1) { // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
262 calledFun(); // expected-warning {{will never be executed}}
263 return 1;
265 if (sizeof(int) > sizeof(char)) {
266 calledFun(); // no-warning
267 return 1;
269 if (x > 10)
270 return CONFIG_CONSTANT ? calledFun() : calledFun(); // no-warning
271 else
272 return 1 ? // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
273 calledFun() :
274 calledFun(); // expected-warning {{will never be executed}}
277 int sizeof_int(int x, int y) {
278 if (sizeof(long) == sizeof(int))
279 return 1; // no-warning
280 if (sizeof(long) != sizeof(int))
281 return 0; // no-warning
282 if (x && y && sizeof(long) < sizeof(char))
283 return 0; // no-warning
284 return 2; // no-warning
287 enum MyEnum2 {
288 ME_A = CONFIG_CONSTANT,
289 ME_B = 1
292 int test_MyEnum(void) {
293 if (!ME_A)
294 return 1; // no-warning
295 if (ME_A)
296 return 2; // no-warning
297 if (ME_B)
298 return 3;
299 if (!ME_B) // expected-warning {{will never be executed}}
300 return 4; // expected-warning {{will never be executed}}
301 return 5;
304 // Test for idiomatic do..while.
305 int test_do_while(int x) {
306 do {
307 if (x == calledFun())
308 break;
309 ++x;
310 break;
312 while (0); // no-warning
313 return x;
316 int test_do_while_nontrivial_cond(int x) {
317 do {
318 if (x == calledFun())
319 break;
320 ++x;
321 break;
323 while (calledFun()); // expected-warning {{will never be executed}}
324 return x;
327 // Diagnostic control: -Wunreachable-code-return.
329 #pragma clang diagnostic push
330 #pragma clang diagnostic ignored "-Wunreachable-code-return"
332 void trivial_dead_return_void_SUPPRESSED(void) {
333 raze();
334 return; // no-warning
337 MyEnum trivial_dead_return_enum_SUPPRESSED(void) {
338 raze();
339 return Value1; // no-warning
342 #pragma clang diagnostic pop
344 // Diagnostic control: -Wunreachable-code-break.
346 #pragma clang diagnostic push
347 #pragma clang diagnostic ignored "-Wunreachable-code-break"
349 int test_break_preceded_by_noreturn_SUPPRESSED(int i) {
350 switch (i) {
351 case 1:
352 raze();
353 break; // no-warning
354 case 2:
355 raze();
356 break; // no-warning
357 warn_here(); // expected-warning {{will never be executed}}
358 case 3:
359 return 1;
360 break; // no-warning
361 default:
362 break;
363 break; // no-warning
365 return i;
368 #pragma clang diagnostic pop
370 // Test "silencing" with parentheses.
371 void test_with_paren_silencing(int x) {
372 if (0) calledFun(); // expected-warning {{will never be executed}} expected-note {{silence by adding parentheses to mark code as explicitly dead}}
373 if ((0)) calledFun(); // no-warning
375 if (1) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
376 calledFun();
377 else
378 calledFun(); // expected-warning {{will never be executed}}
380 if ((1))
381 calledFun();
382 else
383 calledFun(); // no-warning
385 if (!1) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
386 calledFun(); // expected-warning {{code will never be executed}}
387 else
388 calledFun();
390 if ((!1))
391 calledFun(); // no-warning
392 else
393 calledFun();
395 if (!(1))
396 calledFun(); // no-warning
397 else
398 calledFun();
401 // rdar://24570531
403 struct StructWithPointer {
404 void *p;
407 void emitJustOneWarningForOr(struct StructWithPointer *s) {
408 if (1 || !s->p) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
409 return; // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"/* DISABLES CODE */ ("
410 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:8-[[@LINE-2]]:8}:")"
411 emitJustOneWarningForOr(s); // expected-warning {{code will never be executed}}
414 void emitJustOneWarningForOrSilenced(struct StructWithPointer *s) {
415 if ((1) || !s->p)
416 return;
418 emitJustOneWarningForOrSilenced(s); // no warning
421 void emitJustOneWarningForOr2(struct StructWithPointer *s) {
422 if (1 || !s->p) // expected-warning {{code will never be executed}}
423 return; // expected-note@-1 {{silence by adding parentheses to mark code as explicitly dead}}
424 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:7-[[@LINE-2]]:7}:"/* DISABLES CODE */ ("
425 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:8-[[@LINE-3]]:8}:")"
428 void wrapOneInFixit(struct StructWithPointer *s) {
429 if (!s->p || 1) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
430 return; // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"/* DISABLES CODE */ ("
431 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:17-[[@LINE-2]]:17}:")"
432 wrapOneInFixit(s); // expected-warning {{code will never be executed}}
435 void unaryOpNoFixit(void) {
436 if (~ 1)
437 return; // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
438 unaryOpNoFixit(); // expected-warning {{code will never be executed}}
441 void unaryOpStrictFixit(struct StructWithPointer *s) {
442 if (!(s->p && 0)) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
443 return; // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:17-[[@LINE-1]]:17}:"/* DISABLES CODE */ ("
444 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:18-[[@LINE-2]]:18}:")"
445 unaryOpStrictFixit(s); // expected-warning {{code will never be executed}}
448 void unaryOpFixitCastSubExpr(int x) {
449 if (! (int)0) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
450 return; // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"/* DISABLES CODE */ ("
451 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:")"
452 unaryOpFixitCastSubExpr(x); // expected-warning {{code will never be executed}}
455 #define false 0
456 #define true 1
458 void testTrueFalseMacros(void) {
459 if (false) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
460 testTrueFalseMacros(); // expected-warning {{code will never be executed}}
461 if (!true) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
462 testTrueFalseMacros(); // expected-warning {{code will never be executed}}
465 int pr13910_foo(int x) {
466 if (x == 1)
467 return 0;
468 else
469 return x;
470 __builtin_unreachable(); // expected no warning
471 __builtin_assume(0); // expected no warning
474 int pr13910_bar(int x) {
475 switch (x) {
476 default:
477 return x + 1;
479 pr13910_foo(x); // expected-warning {{code will never be executed}}
482 int pr13910_bar2(int x) {
483 if (x == 1)
484 return 0;
485 else
486 return x;
487 pr13910_foo(x); // expected-warning {{code will never be executed}}
488 __builtin_unreachable(); // expected no warning
489 __builtin_assume(0); // expected no warning
490 pr13910_foo(x); // expected-warning {{code will never be executed}}
493 void pr13910_noreturn(void) {
494 raze();
495 __builtin_unreachable(); // expected no warning
496 __builtin_assume(0); // expected no warning
499 void pr13910_assert(void) {
500 myassert(0 && "unreachable");
501 return;
502 __builtin_unreachable(); // expected no warning
503 __builtin_assume(0); // expected no warning