1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wimplicit-fallthrough %s
4 int fallthrough(int n) {
13 case -1: // no warning here, ignore fall-through from unreachable code
15 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}}
17 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}}
19 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}}
22 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}}
25 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}}
34 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}}
36 case 66: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'break;' to avoid fall-through}}
55 [[clang::fallthrough]];
56 case 9: // no warning here, intended fall-through marked with an attribute
58 [[clang::fallthrough]];
59 default: { // no warning here, intended fall-through marked with an attribute
63 [[clang::fallthrough]];
66 case 10: // no warning here, intended fall-through marked with an attribute
70 [[clang::fallthrough]];
72 case 110: // expected-warning{{unannotated fall-through between switch labels}} but no fix-it hint as we have one fall-through annotation!
77 case 12: // no warning here, intended fall-through, no statement between labels
87 case 15: // no warning here, there's no fall-through
98 case 19: { // no warning here, there's no fall-through
102 case 21: { // no warning here, there's no fall-through
105 case 23: // no warning here, there's no fall-through
108 case 25: // no warning here, there's no fall-through
115 class ClassWithDtor {
120 void fallthrough2(int n) {
127 default: // no warning here, there's no fall-through
132 void fallthrough3(int n) {
148 #define MY_SWITCH(X, Y, Z, U, V) switch (X) { case Y: Z; case U: V; }
149 #define MY_SWITCH2(X, Y, Z) switch (X) { Y; Z; }
150 #define MY_CASE(X, Y) case X: Y
151 #define MY_CASE2(X, Y, U, V) case X: Y; case U: V
153 int fallthrough_macro1(int n) {
154 MY_SWITCH(n, 13, n *= 2, 14, break) // expected-warning{{unannotated fall-through between switch labels}}
158 MY_CASE(44, break); // expected-warning{{unannotated fall-through between switch labels}}
163 MY_CASE(333, return 333);
164 MY_CASE2(444, n += 44, 4444, break); // expected-warning{{unannotated fall-through between switch labels}}
165 MY_CASE(555, n += 33);
168 MY_SWITCH2(n + 4, MY_CASE(17, n *= 3), MY_CASE(19, break)) // expected-warning{{unannotated fall-through between switch labels}}
170 MY_SWITCH2(n + 5, MY_CASE(21, break), MY_CASE2(23, n *= 7, 25, break)) // expected-warning{{unannotated fall-through between switch labels}}
175 void fallthrough_cfgblock_with_null_successor(int x) {
176 (x && "") ? (void)(0) : (void)(1);
180 int fallthrough_position(int n) {
183 [[clang::fallthrough]]; // expected-warning{{fallthrough annotation in unreachable code}}
186 [[clang::fallthrough]]; // expected-warning{{fallthrough annotation in unreachable code}}
189 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}}
193 long p = static_cast<long>(n) * n;
196 n += static_cast<int>(p >> 32);
197 [[clang::fallthrough]]; // no warning here
199 n += static_cast<int>(p);
200 [[clang::fallthrough]]; // no warning here
213 int fallthrough_covered_enums(Enum e) {
218 [[clang::fallthrough]]; // no warning here, this shouldn't be treated as unreachable code
229 // Fallthrough annotations in local classes used to generate "fallthrough
230 // annotation does not directly precede switch label" warning.
231 void fallthrough_in_local_class() {
237 [[clang::fallthrough]]; // no diagnostics
241 expected-warning{{unannotated fall-through between switch labels}} \
242 expected-note{{insert 'break;' to avoid fall-through}}
249 // Fallthrough annotations in lambdas used to generate "fallthrough
250 // annotation does not directly precede switch label" warning.
251 void fallthrough_in_lambda() {
257 [[clang::fallthrough]]; // no diagnostics
261 expected-warning{{unannotated fall-through between switch labels}} \
262 expected-note{{insert 'break;' to avoid fall-through}}
269 void fatal() __attribute__((noreturn));
275 // Don't issue a warning.
282 int fallthrough_placement_error(int n) {
284 [[clang::fallthrough]]; // expected-warning{{fallthrough annotation in unreachable code}}
287 [[clang::fallthrough]]; // expected-error{{fallthrough annotation does not directly precede switch label}}
290 [[clang::fallthrough]]; // expected-error{{fallthrough annotation does not directly precede switch label}}
292 [[clang::fallthrough]];
294 [[clang::fallthrough]]; // expected-error{{fallthrough annotation does not directly precede switch label}}
299 int fallthrough_targets(int n) {
300 [[clang::fallthrough]]; // expected-error{{fallthrough annotation is outside switch statement}}
302 [[clang::fallthrough]] // expected-error{{fallthrough attribute is only allowed on empty statements}}
306 [[clang::fallthrough]]; // no warning here, correct target
308 [[clang::fallthrough]] // expected-error{{fallthrough attribute is only allowed on empty statements}}
311 [[clang::fallthrough]] // expected-error{{fallthrough attribute is only allowed on empty statements}} expected-note{{did you forget ';'?}}