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 // We should treat code guarded by 'x & 0' and 'x * 0' as unreachable.
140 void test_mul_and_zero(int x
) {
141 if (x
& 0) calledFun(); // expected-warning {{will never be executed}}
142 if (0 & x
) calledFun(); // expected-warning {{will never be executed}}
143 if (x
* 0) calledFun(); // expected-warning {{will never be executed}}
144 if (0 * x
) calledFun(); // expected-warning {{will never be executed}}
147 void raze(void) __attribute__((noreturn
));
148 void warn_here(void);
150 int test_break_preceded_by_noreturn(int i
) {
154 break; // expected-warning {{'break' will never be executed}}
157 break; // expected-warning {{'break' will never be executed}}
158 warn_here(); // expected-warning {{will never be executed}}
161 break; // expected-warning {{will never be executed}}
164 break; // expected-warning {{will never be executed}}
169 // Don't warn about unreachable 'default' cases, as that is covered
170 // by -Wcovered-switch-default.
171 typedef enum { Value1
= 1 } MyEnum
;
172 void unreachable_default(MyEnum e
) {
177 case 2: // expected-warning {{case value not in enumerated type 'MyEnum'}}
181 calledFun(); // no-warning
185 void unreachable_in_default(MyEnum e
) {
189 calledFun(); // expected-warning {{will never be executed}}
194 // Don't warn about trivial dead returns.
195 int trivial_dead_return(void) {
197 return ((0)); // expected-warning {{'return' will never be executed}}
200 void trivial_dead_return_void(void) {
202 return; // expected-warning {{'return' will never be executed}}
205 MyEnum
trivial_dead_return_enum(void) {
207 return Value1
; // expected-warning {{'return' will never be executed}}
210 MyEnum
trivial_dead_return_enum_2(int x
) {
218 return 2; // expected-warning {{will never be executed}}
221 const char *trivial_dead_return_cstr(void) {
223 return ""; // expected-warning {{return' will never be executed}}
226 char trivial_dead_return_char(void) {
228 return ' '; // expected-warning {{return' will never be executed}}
231 MyEnum
nontrivial_dead_return_enum_2(int x
) {
239 return calledFun(); // expected-warning {{will never be executed}}
244 int covered_switch(enum X x
) {
250 return 4; // no-warning
253 // Test unreachable code depending on configuration values
254 #define CONFIG_CONSTANT 1
255 int test_config_constant(int x
) {
256 if (!CONFIG_CONSTANT
) {
257 calledFun(); // no-warning
260 if (!1) { // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
261 calledFun(); // expected-warning {{will never be executed}}
264 if (sizeof(int) > sizeof(char)) {
265 calledFun(); // no-warning
269 return CONFIG_CONSTANT
? calledFun() : calledFun(); // no-warning
271 return 1 ? // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
273 calledFun(); // expected-warning {{will never be executed}}
276 int sizeof_int(int x
, int y
) {
277 if (sizeof(long) == sizeof(int))
278 return 1; // no-warning
279 if (sizeof(long) != sizeof(int))
280 return 0; // no-warning
281 if (x
&& y
&& sizeof(long) < sizeof(char))
282 return 0; // no-warning
283 return 2; // no-warning
287 ME_A
= CONFIG_CONSTANT
,
291 int test_MyEnum(void) {
293 return 1; // no-warning
295 return 2; // no-warning
298 if (!ME_B
) // expected-warning {{will never be executed}}
299 return 4; // expected-warning {{will never be executed}}
303 // Test for idiomatic do..while.
304 int test_do_while(int x
) {
306 if (x
== calledFun())
311 while (0); // no-warning
315 int test_do_while_nontrivial_cond(int x
) {
317 if (x
== calledFun())
322 while (calledFun()); // expected-warning {{will never be executed}}
326 // Diagnostic control: -Wunreachable-code-return.
328 #pragma clang diagnostic push
329 #pragma clang diagnostic ignored "-Wunreachable-code-return"
331 void trivial_dead_return_void_SUPPRESSED(void) {
333 return; // no-warning
336 MyEnum
trivial_dead_return_enum_SUPPRESSED(void) {
338 return Value1
; // no-warning
341 #pragma clang diagnostic pop
343 // Diagnostic control: -Wunreachable-code-break.
345 #pragma clang diagnostic push
346 #pragma clang diagnostic ignored "-Wunreachable-code-break"
348 int test_break_preceded_by_noreturn_SUPPRESSED(int i
) {
356 warn_here(); // expected-warning {{will never be executed}}
367 #pragma clang diagnostic pop
369 // Test "silencing" with parentheses.
370 void test_with_paren_silencing(int x
) {
371 if (0) calledFun(); // expected-warning {{will never be executed}} expected-note {{silence by adding parentheses to mark code as explicitly dead}}
372 if ((0)) calledFun(); // no-warning
374 if (1) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
377 calledFun(); // expected-warning {{will never be executed}}
382 calledFun(); // no-warning
384 if (!1) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
385 calledFun(); // expected-warning {{code will never be executed}}
390 calledFun(); // no-warning
395 calledFun(); // no-warning
400 struct StructWithPointer
{
404 void emitJustOneWarningForOr(struct StructWithPointer
*s
) {
405 if (1 || !s
->p
) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
406 return; // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"/* DISABLES CODE */ ("
407 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:8-[[@LINE-2]]:8}:")"
408 emitJustOneWarningForOr(s
); // expected-warning {{code will never be executed}}
411 void emitJustOneWarningForOrSilenced(struct StructWithPointer
*s
) {
415 emitJustOneWarningForOrSilenced(s
); // no warning
418 void emitJustOneWarningForOr2(struct StructWithPointer
*s
) {
419 if (1 || !s
->p
) // expected-warning {{code will never be executed}}
420 return; // expected-note@-1 {{silence by adding parentheses to mark code as explicitly dead}}
421 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:7-[[@LINE-2]]:7}:"/* DISABLES CODE */ ("
422 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:8-[[@LINE-3]]:8}:")"
425 void wrapOneInFixit(struct StructWithPointer
*s
) {
426 if (!s
->p
|| 1) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
427 return; // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"/* DISABLES CODE */ ("
428 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:17-[[@LINE-2]]:17}:")"
429 wrapOneInFixit(s
); // expected-warning {{code will never be executed}}
432 void unaryOpNoFixit(void) {
434 return; // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
435 unaryOpNoFixit(); // expected-warning {{code will never be executed}}
438 void unaryOpStrictFixit(struct StructWithPointer
*s
) {
439 if (!(s
->p
&& 0)) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
440 return; // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:17-[[@LINE-1]]:17}:"/* DISABLES CODE */ ("
441 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:18-[[@LINE-2]]:18}:")"
442 unaryOpStrictFixit(s
); // expected-warning {{code will never be executed}}
445 void unaryOpFixitCastSubExpr(int x
) {
446 if (! (int)0) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
447 return; // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"/* DISABLES CODE */ ("
448 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:")"
449 unaryOpFixitCastSubExpr(x
); // expected-warning {{code will never be executed}}
455 void testTrueFalseMacros(void) {
456 if (false) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
457 testTrueFalseMacros(); // expected-warning {{code will never be executed}}
458 if (!true) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
459 testTrueFalseMacros(); // expected-warning {{code will never be executed}}
462 int pr13910_foo(int x
) {
467 __builtin_unreachable(); // expected no warning
468 __builtin_assume(0); // expected no warning
471 int pr13910_bar(int x
) {
476 pr13910_foo(x
); // expected-warning {{code will never be executed}}
479 int pr13910_bar2(int x
) {
484 pr13910_foo(x
); // expected-warning {{code will never be executed}}
485 __builtin_unreachable(); // expected no warning
486 __builtin_assume(0); // expected no warning
487 pr13910_foo(x
); // expected-warning {{code will never be executed}}
490 void pr13910_noreturn(void) {
492 __builtin_unreachable(); // expected no warning
493 __builtin_assume(0); // expected no warning
496 void pr13910_assert(void) {
497 myassert(0 && "unreachable");
499 __builtin_unreachable(); // expected no warning
500 __builtin_assume(0); // expected no warning