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