1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wimplicit-fallthrough %s
2f4a2713aSLionel Sambuc
3f4a2713aSLionel Sambuc
fallthrough(int n)4f4a2713aSLionel Sambuc int fallthrough(int n) {
5f4a2713aSLionel Sambuc switch (n / 10) {
6f4a2713aSLionel Sambuc if (n - 1) {
7f4a2713aSLionel Sambuc n = 100;
8f4a2713aSLionel Sambuc } else if (n - 2) {
9f4a2713aSLionel Sambuc n = 101;
10f4a2713aSLionel Sambuc } else if (n - 3) {
11f4a2713aSLionel Sambuc n = 102;
12f4a2713aSLionel Sambuc }
13f4a2713aSLionel Sambuc case -1: // no warning here, ignore fall-through from unreachable code
14f4a2713aSLionel Sambuc ;
15f4a2713aSLionel Sambuc case 0: {// expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
16f4a2713aSLionel Sambuc }
17f4a2713aSLionel Sambuc case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
18f4a2713aSLionel Sambuc n += 100 ;
19f4a2713aSLionel Sambuc case 3: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
20f4a2713aSLionel Sambuc if (n > 0)
21f4a2713aSLionel Sambuc n += 200;
22f4a2713aSLionel Sambuc case 4: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
23f4a2713aSLionel Sambuc if (n < 0)
24f4a2713aSLionel Sambuc ;
25f4a2713aSLionel Sambuc case 5: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
26f4a2713aSLionel Sambuc switch (n) {
27f4a2713aSLionel Sambuc case 111:
28f4a2713aSLionel Sambuc break;
29f4a2713aSLionel Sambuc case 112:
30f4a2713aSLionel Sambuc break;
31f4a2713aSLionel Sambuc case 113:
32f4a2713aSLionel Sambuc break ;
33f4a2713aSLionel Sambuc }
34f4a2713aSLionel Sambuc case 6: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
35f4a2713aSLionel Sambuc n += 300;
36f4a2713aSLionel Sambuc case 66: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'break;' to avoid fall-through}}
37f4a2713aSLionel Sambuc case 67:
38f4a2713aSLionel Sambuc case 68:
39f4a2713aSLionel Sambuc break;
40f4a2713aSLionel Sambuc }
41f4a2713aSLionel Sambuc switch (n / 15) {
42f4a2713aSLionel Sambuc label_default:
43f4a2713aSLionel Sambuc default:
44f4a2713aSLionel Sambuc n += 333;
45f4a2713aSLionel Sambuc if (n % 10)
46f4a2713aSLionel Sambuc goto label_default;
47f4a2713aSLionel Sambuc break;
48f4a2713aSLionel Sambuc case 70:
49f4a2713aSLionel Sambuc n += 335;
50f4a2713aSLionel Sambuc break;
51f4a2713aSLionel Sambuc }
52f4a2713aSLionel Sambuc switch (n / 20) {
53f4a2713aSLionel Sambuc case 7:
54f4a2713aSLionel Sambuc n += 400;
55f4a2713aSLionel Sambuc [[clang::fallthrough]];
56f4a2713aSLionel Sambuc case 9: // no warning here, intended fall-through marked with an attribute
57f4a2713aSLionel Sambuc n += 800;
58f4a2713aSLionel Sambuc [[clang::fallthrough]];
59f4a2713aSLionel Sambuc default: { // no warning here, intended fall-through marked with an attribute
60f4a2713aSLionel Sambuc if (n % 2 == 0) {
61f4a2713aSLionel Sambuc return 1;
62f4a2713aSLionel Sambuc } else {
63f4a2713aSLionel Sambuc [[clang::fallthrough]];
64f4a2713aSLionel Sambuc }
65f4a2713aSLionel Sambuc }
66f4a2713aSLionel Sambuc case 10: // no warning here, intended fall-through marked with an attribute
67f4a2713aSLionel Sambuc if (n % 3 == 0) {
68f4a2713aSLionel Sambuc n %= 3;
69f4a2713aSLionel Sambuc } else {
70f4a2713aSLionel Sambuc [[clang::fallthrough]];
71f4a2713aSLionel Sambuc }
72f4a2713aSLionel Sambuc case 110: // expected-warning{{unannotated fall-through between switch labels}} but no fix-it hint as we have one fall-through annotation!
73f4a2713aSLionel Sambuc n += 800;
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc switch (n / 30) {
76f4a2713aSLionel Sambuc case 11:
77f4a2713aSLionel Sambuc case 12: // no warning here, intended fall-through, no statement between labels
78f4a2713aSLionel Sambuc n += 1600;
79f4a2713aSLionel Sambuc }
80f4a2713aSLionel Sambuc switch (n / 40) {
81f4a2713aSLionel Sambuc case 13:
82f4a2713aSLionel Sambuc if (n % 2 == 0) {
83f4a2713aSLionel Sambuc return 1;
84f4a2713aSLionel Sambuc } else {
85f4a2713aSLionel Sambuc return 2;
86f4a2713aSLionel Sambuc }
87f4a2713aSLionel Sambuc case 15: // no warning here, there's no fall-through
88f4a2713aSLionel Sambuc n += 3200;
89f4a2713aSLionel Sambuc }
90f4a2713aSLionel Sambuc switch (n / 50) {
91f4a2713aSLionel Sambuc case 17: {
92f4a2713aSLionel Sambuc if (n % 2 == 0) {
93f4a2713aSLionel Sambuc return 1;
94f4a2713aSLionel Sambuc } else {
95f4a2713aSLionel Sambuc return 2;
96f4a2713aSLionel Sambuc }
97f4a2713aSLionel Sambuc }
98f4a2713aSLionel Sambuc case 19: { // no warning here, there's no fall-through
99f4a2713aSLionel Sambuc n += 6400;
100f4a2713aSLionel Sambuc return 3;
101f4a2713aSLionel Sambuc }
102f4a2713aSLionel Sambuc case 21: { // no warning here, there's no fall-through
103f4a2713aSLionel Sambuc break;
104f4a2713aSLionel Sambuc }
105f4a2713aSLionel Sambuc case 23: // no warning here, there's no fall-through
106f4a2713aSLionel Sambuc n += 128000;
107f4a2713aSLionel Sambuc break;
108f4a2713aSLionel Sambuc case 25: // no warning here, there's no fall-through
109f4a2713aSLionel Sambuc break;
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc
112f4a2713aSLionel Sambuc return n;
113f4a2713aSLionel Sambuc }
114f4a2713aSLionel Sambuc
115f4a2713aSLionel Sambuc class ClassWithDtor {
116f4a2713aSLionel Sambuc public:
~ClassWithDtor()117f4a2713aSLionel Sambuc ~ClassWithDtor() {}
118f4a2713aSLionel Sambuc };
119f4a2713aSLionel Sambuc
fallthrough2(int n)120f4a2713aSLionel Sambuc void fallthrough2(int n) {
121f4a2713aSLionel Sambuc switch (n) {
122f4a2713aSLionel Sambuc case 0:
123f4a2713aSLionel Sambuc {
124f4a2713aSLionel Sambuc ClassWithDtor temp;
125f4a2713aSLionel Sambuc break;
126f4a2713aSLionel Sambuc }
127f4a2713aSLionel Sambuc default: // no warning here, there's no fall-through
128f4a2713aSLionel Sambuc break;
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc }
131f4a2713aSLionel Sambuc
fallthrough3(int n)132f4a2713aSLionel Sambuc void fallthrough3(int n) {
133f4a2713aSLionel Sambuc switch (n) {
134f4a2713aSLionel Sambuc case 1:
135f4a2713aSLionel Sambuc do {
136f4a2713aSLionel Sambuc return;
137f4a2713aSLionel Sambuc } while (0);
138f4a2713aSLionel Sambuc case 2:
139f4a2713aSLionel Sambuc do {
140f4a2713aSLionel Sambuc ClassWithDtor temp;
141f4a2713aSLionel Sambuc return;
142f4a2713aSLionel Sambuc } while (0);
143f4a2713aSLionel Sambuc case 3:
144f4a2713aSLionel Sambuc break;
145f4a2713aSLionel Sambuc }
146f4a2713aSLionel Sambuc }
147f4a2713aSLionel Sambuc
148f4a2713aSLionel Sambuc #define MY_SWITCH(X, Y, Z, U, V) switch (X) { case Y: Z; case U: V; }
149f4a2713aSLionel Sambuc #define MY_SWITCH2(X, Y, Z) switch (X) { Y; Z; }
150f4a2713aSLionel Sambuc #define MY_CASE(X, Y) case X: Y
151f4a2713aSLionel Sambuc #define MY_CASE2(X, Y, U, V) case X: Y; case U: V
152f4a2713aSLionel Sambuc
fallthrough_macro1(int n)153f4a2713aSLionel Sambuc int fallthrough_macro1(int n) {
154f4a2713aSLionel Sambuc MY_SWITCH(n, 13, n *= 2, 14, break) // expected-warning{{unannotated fall-through between switch labels}}
155f4a2713aSLionel Sambuc
156f4a2713aSLionel Sambuc switch (n + 1) {
157f4a2713aSLionel Sambuc MY_CASE(33, n += 2);
158f4a2713aSLionel Sambuc MY_CASE(44, break); // expected-warning{{unannotated fall-through between switch labels}}
159f4a2713aSLionel Sambuc MY_CASE(55, n += 3);
160f4a2713aSLionel Sambuc }
161f4a2713aSLionel Sambuc
162f4a2713aSLionel Sambuc switch (n + 3) {
163f4a2713aSLionel Sambuc MY_CASE(333, return 333);
164f4a2713aSLionel Sambuc MY_CASE2(444, n += 44, 4444, break); // expected-warning{{unannotated fall-through between switch labels}}
165f4a2713aSLionel Sambuc MY_CASE(555, n += 33);
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc
168f4a2713aSLionel Sambuc MY_SWITCH2(n + 4, MY_CASE(17, n *= 3), MY_CASE(19, break)) // expected-warning{{unannotated fall-through between switch labels}}
169f4a2713aSLionel Sambuc
170f4a2713aSLionel Sambuc MY_SWITCH2(n + 5, MY_CASE(21, break), MY_CASE2(23, n *= 7, 25, break)) // expected-warning{{unannotated fall-through between switch labels}}
171f4a2713aSLionel Sambuc
172f4a2713aSLionel Sambuc return n;
173f4a2713aSLionel Sambuc }
174f4a2713aSLionel Sambuc
fallthrough_cfgblock_with_null_successor(int x)175f4a2713aSLionel Sambuc void fallthrough_cfgblock_with_null_successor(int x) {
176f4a2713aSLionel Sambuc (x && "") ? (void)(0) : (void)(1);
177f4a2713aSLionel Sambuc switch (x) {}
178f4a2713aSLionel Sambuc }
179f4a2713aSLionel Sambuc
fallthrough_position(int n)180f4a2713aSLionel Sambuc int fallthrough_position(int n) {
181f4a2713aSLionel Sambuc switch (n) {
182f4a2713aSLionel Sambuc [[clang::fallthrough]]; // expected-warning{{fallthrough annotation does not directly precede switch label}}
183f4a2713aSLionel Sambuc n += 300;
184f4a2713aSLionel Sambuc [[clang::fallthrough]]; // expected-warning{{fallthrough annotation in unreachable code}}
185f4a2713aSLionel Sambuc case 221:
186f4a2713aSLionel Sambuc [[clang::fallthrough]]; // expected-warning{{fallthrough annotation does not directly precede switch label}}
187f4a2713aSLionel Sambuc return 1;
188f4a2713aSLionel Sambuc [[clang::fallthrough]]; // expected-warning{{fallthrough annotation in unreachable code}}
189f4a2713aSLionel Sambuc case 222:
190f4a2713aSLionel Sambuc [[clang::fallthrough]]; // expected-warning{{fallthrough annotation does not directly precede switch label}}
191f4a2713aSLionel Sambuc n += 400;
192f4a2713aSLionel Sambuc case 223: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
193f4a2713aSLionel Sambuc [[clang::fallthrough]]; // expected-warning{{fallthrough annotation does not directly precede switch label}}
194f4a2713aSLionel Sambuc }
195f4a2713aSLionel Sambuc
196f4a2713aSLionel Sambuc long p = static_cast<long>(n) * n;
197f4a2713aSLionel Sambuc switch (sizeof(p)) {
198f4a2713aSLionel Sambuc case 9:
199f4a2713aSLionel Sambuc n += static_cast<int>(p >> 32);
200f4a2713aSLionel Sambuc [[clang::fallthrough]]; // no warning here
201f4a2713aSLionel Sambuc case 5:
202f4a2713aSLionel Sambuc n += static_cast<int>(p);
203f4a2713aSLionel Sambuc [[clang::fallthrough]]; // no warning here
204f4a2713aSLionel Sambuc default:
205f4a2713aSLionel Sambuc n += 1;
206f4a2713aSLionel Sambuc break;
207f4a2713aSLionel Sambuc }
208f4a2713aSLionel Sambuc
209f4a2713aSLionel Sambuc return n;
210f4a2713aSLionel Sambuc }
211f4a2713aSLionel Sambuc
212f4a2713aSLionel Sambuc enum Enum {
213f4a2713aSLionel Sambuc Value1, Value2
214f4a2713aSLionel Sambuc };
215f4a2713aSLionel Sambuc
fallthrough_covered_enums(Enum e)216f4a2713aSLionel Sambuc int fallthrough_covered_enums(Enum e) {
217f4a2713aSLionel Sambuc int n = 0;
218f4a2713aSLionel Sambuc switch (e) {
219f4a2713aSLionel Sambuc default:
220f4a2713aSLionel Sambuc n += 17;
221f4a2713aSLionel Sambuc [[clang::fallthrough]]; // no warning here, this shouldn't be treated as unreachable code
222f4a2713aSLionel Sambuc case Value1:
223f4a2713aSLionel Sambuc n += 19;
224f4a2713aSLionel Sambuc break;
225f4a2713aSLionel Sambuc case Value2:
226f4a2713aSLionel Sambuc n += 21;
227f4a2713aSLionel Sambuc break;
228f4a2713aSLionel Sambuc }
229f4a2713aSLionel Sambuc return n;
230f4a2713aSLionel Sambuc }
231f4a2713aSLionel Sambuc
232*0a6a1f1dSLionel Sambuc // Fallthrough annotations in local classes used to generate "fallthrough
233*0a6a1f1dSLionel Sambuc // annotation does not directly precede switch label" warning.
fallthrough_in_local_class()234*0a6a1f1dSLionel Sambuc void fallthrough_in_local_class() {
235*0a6a1f1dSLionel Sambuc class C {
236*0a6a1f1dSLionel Sambuc void f(int x) {
237*0a6a1f1dSLionel Sambuc switch (x) {
238*0a6a1f1dSLionel Sambuc case 0:
239*0a6a1f1dSLionel Sambuc x++;
240*0a6a1f1dSLionel Sambuc [[clang::fallthrough]]; // no diagnostics
241*0a6a1f1dSLionel Sambuc case 1:
242*0a6a1f1dSLionel Sambuc x++;
243*0a6a1f1dSLionel Sambuc default: // \
244*0a6a1f1dSLionel Sambuc expected-warning{{unannotated fall-through between switch labels}} \
245*0a6a1f1dSLionel Sambuc expected-note{{insert 'break;' to avoid fall-through}}
246*0a6a1f1dSLionel Sambuc break;
247*0a6a1f1dSLionel Sambuc }
248*0a6a1f1dSLionel Sambuc }
249*0a6a1f1dSLionel Sambuc };
250*0a6a1f1dSLionel Sambuc }
251*0a6a1f1dSLionel Sambuc
252*0a6a1f1dSLionel Sambuc // Fallthrough annotations in lambdas used to generate "fallthrough
253*0a6a1f1dSLionel Sambuc // annotation does not directly precede switch label" warning.
fallthrough_in_lambda()254*0a6a1f1dSLionel Sambuc void fallthrough_in_lambda() {
255*0a6a1f1dSLionel Sambuc (void)[] {
256*0a6a1f1dSLionel Sambuc int x = 0;
257*0a6a1f1dSLionel Sambuc switch (x) {
258*0a6a1f1dSLionel Sambuc case 0:
259*0a6a1f1dSLionel Sambuc x++;
260*0a6a1f1dSLionel Sambuc [[clang::fallthrough]]; // no diagnostics
261*0a6a1f1dSLionel Sambuc case 1:
262*0a6a1f1dSLionel Sambuc x++;
263*0a6a1f1dSLionel Sambuc default: // \
264*0a6a1f1dSLionel Sambuc expected-warning{{unannotated fall-through between switch labels}} \
265*0a6a1f1dSLionel Sambuc expected-note{{insert 'break;' to avoid fall-through}}
266*0a6a1f1dSLionel Sambuc break;
267*0a6a1f1dSLionel Sambuc }
268*0a6a1f1dSLionel Sambuc };
269*0a6a1f1dSLionel Sambuc }
270*0a6a1f1dSLionel Sambuc
271*0a6a1f1dSLionel Sambuc namespace PR18983 {
272*0a6a1f1dSLionel Sambuc void fatal() __attribute__((noreturn));
273*0a6a1f1dSLionel Sambuc int num();
test()274*0a6a1f1dSLionel Sambuc void test() {
275*0a6a1f1dSLionel Sambuc switch (num()) {
276*0a6a1f1dSLionel Sambuc case 1:
277*0a6a1f1dSLionel Sambuc fatal();
278*0a6a1f1dSLionel Sambuc // Don't issue a warning.
279*0a6a1f1dSLionel Sambuc case 2:
280*0a6a1f1dSLionel Sambuc break;
281*0a6a1f1dSLionel Sambuc }
282*0a6a1f1dSLionel Sambuc }
283*0a6a1f1dSLionel Sambuc }
284*0a6a1f1dSLionel Sambuc
fallthrough_targets(int n)285f4a2713aSLionel Sambuc int fallthrough_targets(int n) {
286f4a2713aSLionel Sambuc [[clang::fallthrough]]; // expected-error{{fallthrough annotation is outside switch statement}}
287f4a2713aSLionel Sambuc
288f4a2713aSLionel Sambuc [[clang::fallthrough]] // expected-error{{fallthrough attribute is only allowed on empty statements}}
289f4a2713aSLionel Sambuc switch (n) {
290f4a2713aSLionel Sambuc case 121:
291f4a2713aSLionel Sambuc n += 400;
292f4a2713aSLionel Sambuc [[clang::fallthrough]]; // no warning here, correct target
293f4a2713aSLionel Sambuc case 123:
294f4a2713aSLionel Sambuc [[clang::fallthrough]] // expected-error{{fallthrough attribute is only allowed on empty statements}}
295f4a2713aSLionel Sambuc n += 800;
296f4a2713aSLionel Sambuc break;
297f4a2713aSLionel Sambuc [[clang::fallthrough]] // expected-error{{fallthrough attribute is only allowed on empty statements}} expected-note{{did you forget ';'?}}
298f4a2713aSLionel Sambuc case 125:
299f4a2713aSLionel Sambuc n += 1600;
300f4a2713aSLionel Sambuc }
301f4a2713aSLionel Sambuc return n;
302f4a2713aSLionel Sambuc }
303