1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wimplicit-fallthrough -Wunreachable-code-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}}
54 [[clang::fallthrough
]];
57 [[clang::fallthrough
]];
58 case 9: // no warning here, intended fall-through marked with an attribute
60 [[clang::fallthrough
]];
61 default: { // no warning here, intended fall-through marked with an attribute
65 [[clang::fallthrough
]];
68 case 10: // no warning here, intended fall-through marked with an attribute
72 [[clang::fallthrough
]];
74 case 110: // expected-warning{{unannotated fall-through between switch labels}} but no fix-it hint as we have one fall-through annotation!
79 [[unlikely
, clang::fallthrough
]];
81 case 12: // no warning here, intended fall-through, no statement between labels
91 case 15: // no warning here, there's no fall-through
102 case 19: { // no warning here, there's no fall-through
106 case 21: { // no warning here, there's no fall-through
109 case 23: // no warning here, there's no fall-through
112 case 25: // no warning here, there's no fall-through
119 class ClassWithDtor
{
124 void fallthrough2(int n
) {
131 default: // no warning here, there's no fall-through
136 void fallthrough3(int n
) {
152 #define MY_SWITCH(X, Y, Z, U, V) switch (X) { case Y: Z; case U: V; }
153 #define MY_SWITCH2(X, Y, Z) switch (X) { Y; Z; }
154 #define MY_CASE(X, Y) case X: Y
155 #define MY_CASE2(X, Y, U, V) case X: Y; case U: V
157 int fallthrough_macro1(int n
) {
158 MY_SWITCH(n
, 13, n
*= 2, 14, break) // expected-warning{{unannotated fall-through between switch labels}}
162 MY_CASE(44, break); // expected-warning{{unannotated fall-through between switch labels}}
167 MY_CASE(333, return 333);
168 MY_CASE2(444, n
+= 44, 4444, break); // expected-warning{{unannotated fall-through between switch labels}}
169 MY_CASE(555, n
+= 33);
172 MY_SWITCH2(n
+ 4, MY_CASE(17, n
*= 3), MY_CASE(19, break)) // expected-warning{{unannotated fall-through between switch labels}}
174 MY_SWITCH2(n
+ 5, MY_CASE(21, break), MY_CASE2(23, n
*= 7, 25, break)) // expected-warning{{unannotated fall-through between switch labels}}
179 void fallthrough_cfgblock_with_null_successor(int x
) {
180 (x
&& "") ? (void)(0) : (void)(1);
184 int fallthrough_position(int n
) {
187 [[clang::fallthrough
]]; // expected-warning{{fallthrough annotation in unreachable code}}
190 [[clang::fallthrough
]]; // expected-warning{{fallthrough annotation in unreachable code}}
193 __attribute__((fallthrough
)); // expected-warning{{fallthrough annotation in unreachable code}}
196 case 224: // 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}}
200 #pragma clang diagnostic push
201 #pragma clang diagnostic ignored "-Wunreachable-code-fallthrough"
204 [[clang::fallthrough
]]; // no warning here
207 [[clang::fallthrough
]]; // no warning here
210 __attribute__((fallthrough
)); // no warning here
214 __attribute__((fallthrough
)); // no warning here
218 #pragma clang diagnostic pop
220 long p
= static_cast<long>(n
) * n
;
223 n
+= static_cast<int>(p
>> 32);
224 [[clang::fallthrough
]]; // no warning here
226 n
+= static_cast<int>(p
);
227 [[clang::fallthrough
]]; // no warning here
240 int fallthrough_covered_enums(Enum e
) {
245 [[clang::fallthrough
]]; // no warning here, this shouldn't be treated as unreachable code
256 // Fallthrough annotations in local classes used to generate "fallthrough
257 // annotation does not directly precede switch label" warning.
258 void fallthrough_in_local_class() {
264 [[clang::fallthrough
]]; // no diagnostics
268 expected
-warning
{{unannotated fall
-through between
switch labels
}} \
269 expected
-note
{{insert
'break;' to avoid fall
-through
}}
276 // Fallthrough annotations in lambdas used to generate "fallthrough
277 // annotation does not directly precede switch label" warning.
278 void fallthrough_in_lambda() {
284 [[clang::fallthrough
]]; // no diagnostics
288 expected
-warning
{{unannotated fall
-through between
switch labels
}} \
289 expected
-note
{{insert
'break;' to avoid fall
-through
}}
296 void fatal() __attribute__((noreturn
));
302 // Don't issue a warning.
309 int fallthrough_placement_error(int n
) {
311 [[clang::fallthrough
]]; // expected-warning{{fallthrough annotation in unreachable code}}
314 [[clang::fallthrough
]]; // expected-error{{fallthrough annotation does not directly precede switch label}}
317 [[clang::fallthrough
]]; // expected-error{{fallthrough annotation does not directly precede switch label}}
319 [[clang::fallthrough
]];
321 [[clang::fallthrough
]]; // expected-error{{fallthrough annotation does not directly precede switch label}}
326 int fallthrough_targets(int n
) {
327 [[clang::fallthrough
]]; // expected-error{{fallthrough annotation is outside switch statement}}
329 [[clang::fallthrough
]] // expected-error{{'fallthrough' attribute only applies to empty statements}}
333 [[clang::fallthrough
]]; // no warning here, correct target
335 [[clang::fallthrough
]] // expected-error{{'fallthrough' attribute only applies to empty statements}}
338 [[clang::fallthrough
]] // expected-error{{'fallthrough' attribute is only allowed on empty statements}} expected-note{{did you forget ';'?}}
345 int fallthrough_alt_spelling(int n
) {
349 [[clang::fallthrough
]];
352 [[clang::__fallthrough__
]];
360 int fallthrough_attribute_spelling(int n
) {
364 __attribute__((fallthrough
));