blob: 2fbe1c78eb21025659b2326bf4e2f90cc2ca8803 [file] [log] [blame]
David Blaikie2dd52e32012-01-24 05:34:08 +00001// RUN: %clang %s -fsyntax-only -Xclang -verify -fblocks -Wunreachable-code -Wno-unused-value -Wno-covered-switch-default
Mike Stump9a24dc12010-01-15 03:15:36 +00002
Anders Carlsson5d1d7ae2010-09-03 00:25:02 +00003int halt() __attribute__((noreturn));
Mike Stump8eae0902010-01-15 23:17:13 +00004int live();
5int dead();
6
Mike Stump9a24dc12010-01-15 03:15:36 +00007void test1() {
8 goto c;
9 d:
10 goto e; // expected-warning {{will never be executed}}
11 c: ;
12 int i;
13 return;
14 goto b; // expected-warning {{will never be executed}}
15 goto a; // expected-warning {{will never be executed}}
16 b:
17 i = 1;
18 a:
19 i = 2;
20 goto f;
21 e:
22 goto d;
23 f: ;
24}
Mike Stump8eae0902010-01-15 23:17:13 +000025
26void test2() {
Mike Stump45db90d2010-01-21 17:31:41 +000027 int i;
Mike Stump8eae0902010-01-15 23:17:13 +000028 switch (live()) {
29 case 1:
30 halt(),
31 dead(); // expected-warning {{will never be executed}}
32
33 case 2:
Mike Stump55f988e2010-01-21 17:21:23 +000034 live(), halt(),
Mike Stump8eae0902010-01-15 23:17:13 +000035 dead(); // expected-warning {{will never be executed}}
36
37 case 3:
Marcin Swiderskie1667192010-10-24 08:21:40 +000038 live()
39 + // expected-warning {{will never be executed}}
Mike Stump55f988e2010-01-21 17:21:23 +000040 halt();
41 dead();
Mike Stump8eae0902010-01-15 23:17:13 +000042
43 case 4:
44 a4:
45 live(),
46 halt();
47 goto a4; // expected-warning {{will never be executed}}
48
49 case 5:
50 goto a5;
51 c5:
52 dead(); // expected-warning {{will never be executed}}
53 goto b5;
54 a5:
55 live(),
56 halt();
57 b5:
58 goto c5;
59
60 case 6:
61 if (live())
62 goto e6;
63 live(),
64 halt();
65 d6:
66 dead(); // expected-warning {{will never be executed}}
67 goto b6;
68 c6:
69 dead();
70 goto b6;
71 e6:
72 live(),
73 halt();
74 b6:
75 goto c6;
Mike Stump55f988e2010-01-21 17:21:23 +000076 case 7:
77 halt()
Marcin Swiderskie1667192010-10-24 08:21:40 +000078 +
79 dead(); // expected-warning {{will never be executed}}
Mike Stump55f988e2010-01-21 17:21:23 +000080 - // expected-warning {{will never be executed}}
81 halt();
Mike Stump45db90d2010-01-21 17:31:41 +000082 case 8:
Ted Kremenek0c8e5a02011-07-19 14:18:48 +000083 i // expected-warning {{will never be executed}}
84 +=
Mike Stump45db90d2010-01-21 17:31:41 +000085 halt();
Mike Stumpe5fba702010-01-21 19:44:04 +000086 case 9:
87 halt()
88 ? // expected-warning {{will never be executed}}
89 dead() : dead();
Mike Stump44582302010-01-21 19:51:34 +000090 case 10:
91 ( // expected-warning {{will never be executed}}
92 float)halt();
Mike Stumpb5c77552010-01-21 23:15:53 +000093 case 11: {
94 int a[5];
95 live(),
Ted Kremenek0c8e5a02011-07-19 14:18:48 +000096 a[halt() // expected-warning {{will never be executed}}
97 ];
Mike Stumpb5c77552010-01-21 23:15:53 +000098 }
Mike Stump8eae0902010-01-15 23:17:13 +000099 }
100}
Ted Kremenek8caec842010-09-09 00:06:10 +0000101
102enum Cases { C1, C2, C3 };
103int test_enum_cases(enum Cases C) {
104 switch (C) {
105 case C1:
106 case C2:
107 case C3:
108 return 1;
109 default: {
110 int i = 0; // expected-warning{{will never be executed}}
111 ++i;
112 return i;
113 }
114 }
115}
116
Ted Kremeneke7a27642011-08-25 19:28:55 +0000117// Handle unreachable code triggered by macro expansions.
118void __myassert_rtn(const char *, const char *, int, const char *) __attribute__((__noreturn__));
119
120#define myassert(e) \
121 (__builtin_expect(!(e), 0) ? __myassert_rtn(__func__, __FILE__, __LINE__, #e) : (void)0)
122
123void test_assert() {
124 myassert(0 && "unreachable");
125 return; // no-warning
126}
127
Ted Kremenek952b35d2012-01-03 21:02:04 +0000128// Test case for PR 9774. Tests that dead code in macros aren't warned about.
129#define MY_MAX(a,b) ((a) >= (b) ? (a) : (b))
130void PR9774(int *s) {
131 for (int i = 0; i < MY_MAX(2, 3); i++) // no-warning
132 s[i] = 0;
133}
Ted Kremeneke7a27642011-08-25 19:28:55 +0000134
Ted Kremenek3d1125b2012-08-24 07:42:09 +0000135// Test case for <rdar://problem/11005770>. We should treat code guarded
136// by 'x & 0' and 'x * 0' as unreachable.
137void calledFun();
138void test_mul_and_zero(int x) {
139 if (x & 0) calledFun(); // expected-warning {{will never be executed}}
140 if (0 & x) calledFun(); // expected-warning {{will never be executed}}
141 if (x * 0) calledFun(); // expected-warning {{will never be executed}}
142 if (0 * x) calledFun(); // expected-warning {{will never be executed}}
143}