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
));
13 goto e
; // expected-warning {{will never be executed}}
17 goto b
; // expected-warning {{will never be executed}}
18 goto a
; // expected-warning {{will never be executed}}
34 dead(); // expected-warning {{will never be executed}}
38 dead(); // expected-warning {{will never be executed}}
42 + // expected-warning {{will never be executed}}
50 goto a4
; // expected-warning {{will never be executed}}
55 dead(); // expected-warning {{will never be executed}}
69 dead(); // expected-warning {{will never be executed}}
82 dead(); // expected-warning {{will never be executed}}
83 - // expected-warning {{will never be executed}}
87 += // expected-warning {{will never be executed}}
91 ? // expected-warning {{will never be executed}}
94 ( // expected-warning {{will never be executed}}
100 ]; // expected-warning {{will never be executed}}
105 enum Cases
{ C1
, C2
, C3
};
106 int test_enum_cases(enum Cases C
) {
113 int i
= 0; // no-warning
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
138 // Test case for <rdar://problem/11005770>. We should treat code guarded
139 // by 'x & 0' and 'x * 0' as unreachable.
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
) {
155 break; // expected-warning {{'break' will never be executed}}
158 break; // expected-warning {{'break' will never be executed}}
159 warn_here(); // expected-warning {{will never be executed}}
162 break; // expected-warning {{will never be executed}}
165 break; // expected-warning {{will never be executed}}
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
) {
178 case 2: // expected-warning {{case value not in enumerated type 'MyEnum'}}
182 calledFun(); // no-warning
186 void unreachable_in_default(MyEnum e
) {
190 calledFun(); // expected-warning {{will never be executed}}
195 // Don't warn about trivial dead returns.
196 int trivial_dead_return(void) {
198 return ((0)); // expected-warning {{'return' will never be executed}}
201 void trivial_dead_return_void(void) {
203 return; // expected-warning {{'return' will never be executed}}
206 MyEnum
trivial_dead_return_enum(void) {
208 return Value1
; // expected-warning {{'return' will never be executed}}
211 MyEnum
trivial_dead_return_enum_2(int x
) {
219 return 2; // expected-warning {{will never be executed}}
222 const char *trivial_dead_return_cstr(void) {
224 return ""; // expected-warning {{return' will never be executed}}
227 char trivial_dead_return_char(void) {
229 return ' '; // expected-warning {{return' will never be executed}}
232 MyEnum
nontrivial_dead_return_enum_2(int x
) {
240 return calledFun(); // expected-warning {{will never be executed}}
245 int covered_switch(enum X x
) {
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
261 if (!1) { // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
262 calledFun(); // expected-warning {{will never be executed}}
265 if (sizeof(int) > sizeof(char)) {
266 calledFun(); // no-warning
270 return CONFIG_CONSTANT
? calledFun() : calledFun(); // no-warning
272 return 1 ? // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
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
288 ME_A
= CONFIG_CONSTANT
,
292 int test_MyEnum(void) {
294 return 1; // no-warning
296 return 2; // no-warning
299 if (!ME_B
) // expected-warning {{will never be executed}}
300 return 4; // expected-warning {{will never be executed}}
304 // Test for idiomatic do..while.
305 int test_do_while(int x
) {
307 if (x
== calledFun())
312 while (0); // no-warning
316 int test_do_while_nontrivial_cond(int x
) {
318 if (x
== calledFun())
323 while (calledFun()); // expected-warning {{will never be executed}}
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) {
334 return; // no-warning
337 MyEnum
trivial_dead_return_enum_SUPPRESSED(void) {
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
) {
357 warn_here(); // expected-warning {{will never be executed}}
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}}
378 calledFun(); // expected-warning {{will never be executed}}
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}}
391 calledFun(); // no-warning
396 calledFun(); // no-warning
403 struct StructWithPointer
{
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
) {
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) {
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}}
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
) {
470 __builtin_unreachable(); // expected no warning
471 __builtin_assume(0); // expected no warning
474 int pr13910_bar(int x
) {
479 pr13910_foo(x
); // expected-warning {{code will never be executed}}
482 int pr13910_bar2(int 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) {
495 __builtin_unreachable(); // expected no warning
496 __builtin_assume(0); // expected no warning
499 void pr13910_assert(void) {
500 myassert(0 && "unreachable");
502 __builtin_unreachable(); // expected no warning
503 __builtin_assume(0); // expected no warning