xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/switch-implicit-fallthrough-cxx98.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 -Wimplicit-fallthrough %s
2*f4a2713aSLionel Sambuc // XFAIL: *
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc // NOTE: This test is marked XFAIL until we come up with a good language design
5*f4a2713aSLionel Sambuc // for a worfklow to use this warning outside of C++11.
6*f4a2713aSLionel Sambuc 
fallthrough(int n)7*f4a2713aSLionel Sambuc int fallthrough(int n) {
8*f4a2713aSLionel Sambuc   switch (n / 10) {
9*f4a2713aSLionel Sambuc       if (n - 1) {
10*f4a2713aSLionel Sambuc         n = 100;
11*f4a2713aSLionel Sambuc       } else if (n - 2) {
12*f4a2713aSLionel Sambuc         n = 101;
13*f4a2713aSLionel Sambuc       } else if (n - 3) {
14*f4a2713aSLionel Sambuc         n = 102;
15*f4a2713aSLionel Sambuc       }
16*f4a2713aSLionel Sambuc     case -1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'break;' to avoid fall-through}}
17*f4a2713aSLionel Sambuc       ;
18*f4a2713aSLionel Sambuc     case 0: {// expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'break;' to avoid fall-through}}
19*f4a2713aSLionel Sambuc     }
20*f4a2713aSLionel Sambuc     case 1:  // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'break;' to avoid fall-through}}
21*f4a2713aSLionel Sambuc       n += 100         ;
22*f4a2713aSLionel Sambuc     case 3:  // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'break;' to avoid fall-through}}
23*f4a2713aSLionel Sambuc       if (n > 0)
24*f4a2713aSLionel Sambuc         n += 200;
25*f4a2713aSLionel Sambuc     case 4:  // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'break;' to avoid fall-through}}
26*f4a2713aSLionel Sambuc       if (n < 0)
27*f4a2713aSLionel Sambuc         ;
28*f4a2713aSLionel Sambuc     case 5:  // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'break;' to avoid fall-through}}
29*f4a2713aSLionel Sambuc       switch (n) {
30*f4a2713aSLionel Sambuc       case 111:
31*f4a2713aSLionel Sambuc         break;
32*f4a2713aSLionel Sambuc       case 112:
33*f4a2713aSLionel Sambuc         break;
34*f4a2713aSLionel Sambuc       case 113:
35*f4a2713aSLionel Sambuc         break    ;
36*f4a2713aSLionel Sambuc       }
37*f4a2713aSLionel Sambuc     case 6:  // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'break;' to avoid fall-through}}
38*f4a2713aSLionel Sambuc       n += 300;
39*f4a2713aSLionel Sambuc   }
40*f4a2713aSLionel Sambuc   switch (n / 30) {
41*f4a2713aSLionel Sambuc     case 11:
42*f4a2713aSLionel Sambuc     case 12:  // no warning here, intended fall-through, no statement between labels
43*f4a2713aSLionel Sambuc       n += 1600;
44*f4a2713aSLionel Sambuc   }
45*f4a2713aSLionel Sambuc   switch (n / 40) {
46*f4a2713aSLionel Sambuc     case 13:
47*f4a2713aSLionel Sambuc       if (n % 2 == 0) {
48*f4a2713aSLionel Sambuc         return 1;
49*f4a2713aSLionel Sambuc       } else {
50*f4a2713aSLionel Sambuc         return 2;
51*f4a2713aSLionel Sambuc       }
52*f4a2713aSLionel Sambuc     case 15:  // no warning here, there's no fall-through
53*f4a2713aSLionel Sambuc       n += 3200;
54*f4a2713aSLionel Sambuc   }
55*f4a2713aSLionel Sambuc   switch (n / 50) {
56*f4a2713aSLionel Sambuc     case 17: {
57*f4a2713aSLionel Sambuc       if (n % 2 == 0) {
58*f4a2713aSLionel Sambuc         return 1;
59*f4a2713aSLionel Sambuc       } else {
60*f4a2713aSLionel Sambuc         return 2;
61*f4a2713aSLionel Sambuc       }
62*f4a2713aSLionel Sambuc     }
63*f4a2713aSLionel Sambuc     case 19: { // no warning here, there's no fall-through
64*f4a2713aSLionel Sambuc       n += 6400;
65*f4a2713aSLionel Sambuc       return 3;
66*f4a2713aSLionel Sambuc     }
67*f4a2713aSLionel Sambuc     case 21: { // no warning here, there's no fall-through
68*f4a2713aSLionel Sambuc       break;
69*f4a2713aSLionel Sambuc     }
70*f4a2713aSLionel Sambuc     case 23: // no warning here, there's no fall-through
71*f4a2713aSLionel Sambuc       n += 128000;
72*f4a2713aSLionel Sambuc       break;
73*f4a2713aSLionel Sambuc     case 25: // no warning here, there's no fall-through
74*f4a2713aSLionel Sambuc       break;
75*f4a2713aSLionel Sambuc   }
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc   return n;
78*f4a2713aSLionel Sambuc }
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc class ClassWithDtor {
81*f4a2713aSLionel Sambuc public:
~ClassWithDtor()82*f4a2713aSLionel Sambuc   ~ClassWithDtor() {}
83*f4a2713aSLionel Sambuc };
84*f4a2713aSLionel Sambuc 
fallthrough2(int n)85*f4a2713aSLionel Sambuc void fallthrough2(int n) {
86*f4a2713aSLionel Sambuc   switch (n) {
87*f4a2713aSLionel Sambuc     case 0:
88*f4a2713aSLionel Sambuc     {
89*f4a2713aSLionel Sambuc       ClassWithDtor temp;
90*f4a2713aSLionel Sambuc       break;
91*f4a2713aSLionel Sambuc     }
92*f4a2713aSLionel Sambuc     default: // no warning here, there's no fall-through
93*f4a2713aSLionel Sambuc       break;
94*f4a2713aSLionel Sambuc   }
95*f4a2713aSLionel Sambuc }
96*f4a2713aSLionel Sambuc 
97*f4a2713aSLionel Sambuc #define MY_SWITCH(X, Y, Z, U, V) switch (X) { case Y: Z; case U: V; }
98*f4a2713aSLionel Sambuc #define MY_SWITCH2(X, Y, Z) switch (X) { Y; Z; }
99*f4a2713aSLionel Sambuc #define MY_CASE(X, Y) case X: Y
100*f4a2713aSLionel Sambuc #define MY_CASE2(X, Y, U, V) case X: Y; case U: V
101*f4a2713aSLionel Sambuc 
fallthrough_macro1(int n)102*f4a2713aSLionel Sambuc int fallthrough_macro1(int n) {
103*f4a2713aSLionel Sambuc   MY_SWITCH(n, 13, n *= 2, 14, break)  // expected-warning{{unannotated fall-through between switch labels}}
104*f4a2713aSLionel Sambuc 
105*f4a2713aSLionel Sambuc   switch (n + 1) {
106*f4a2713aSLionel Sambuc     MY_CASE(33, n += 2);
107*f4a2713aSLionel Sambuc     MY_CASE(44, break);  // expected-warning{{unannotated fall-through between switch labels}}
108*f4a2713aSLionel Sambuc     MY_CASE(55, n += 3);
109*f4a2713aSLionel Sambuc   }
110*f4a2713aSLionel Sambuc 
111*f4a2713aSLionel Sambuc   switch (n + 3) {
112*f4a2713aSLionel Sambuc     MY_CASE(333, return 333);
113*f4a2713aSLionel Sambuc     MY_CASE2(444, n += 44, 4444, break);  // expected-warning{{unannotated fall-through between switch labels}}
114*f4a2713aSLionel Sambuc     MY_CASE(555, n += 33);
115*f4a2713aSLionel Sambuc   }
116*f4a2713aSLionel Sambuc 
117*f4a2713aSLionel Sambuc   MY_SWITCH2(n + 4, MY_CASE(17, n *= 3), MY_CASE(19, break))  // expected-warning{{unannotated fall-through between switch labels}}
118*f4a2713aSLionel Sambuc 
119*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}}
120*f4a2713aSLionel Sambuc 
121*f4a2713aSLionel Sambuc   return n;
122*f4a2713aSLionel Sambuc }
123