switch-implicit-fallthrough.cpp revision e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wimplicit-fallthrough %s
2
3
4int fallthrough(int n) {
5  switch (n / 10) {
6      if (n - 1) {
7        n = 100;
8      } else if (n - 2) {
9        n = 101;
10      } else if (n - 3) {
11        n = 102;
12      }
13    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}}
14      ;
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}}
16    }
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}}
18      n += 100         ;
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}}
20      if (n > 0)
21        n += 200;
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}}
23      if (n < 0)
24        ;
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}}
26      switch (n) {
27      case 111:
28        break;
29      case 112:
30        break;
31      case 113:
32        break    ;
33      }
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}}
35      n += 300;
36  }
37  switch (n / 20) {
38    case 7:
39      n += 400;
40      [[clang::fallthrough]];
41    case 9:  // no warning here, intended fall-through marked with an attribute
42      n += 800;
43      [[clang::fallthrough]];
44    default: { // no warning here, intended fall-through marked with an attribute
45      if (n % 2 == 0) {
46        return 1;
47      } else {
48        [[clang::fallthrough]];
49      }
50    }
51    case 10:  // no warning here, intended fall-through marked with an attribute
52      if (n % 3 == 0) {
53        n %= 3;
54      } else {
55        [[clang::fallthrough]];
56      }
57    case 110:  // expected-warning{{unannotated fall-through between switch labels}} but no fix-it hint as we have one fall-through annotation!
58      n += 800;
59  }
60  switch (n / 30) {
61    case 11:
62    case 12:  // no warning here, intended fall-through, no statement between labels
63      n += 1600;
64  }
65  switch (n / 40) {
66    case 13:
67      if (n % 2 == 0) {
68        return 1;
69      } else {
70        return 2;
71      }
72    case 15:  // no warning here, there's no fall-through
73      n += 3200;
74  }
75  switch (n / 50) {
76    case 17: {
77      if (n % 2 == 0) {
78        return 1;
79      } else {
80        return 2;
81      }
82    }
83    case 19: { // no warning here, there's no fall-through
84      n += 6400;
85      return 3;
86    }
87    case 21: { // no warning here, there's no fall-through
88      break;
89    }
90    case 23: // no warning here, there's no fall-through
91      n += 128000;
92      break;
93    case 25: // no warning here, there's no fall-through
94      break;
95  }
96
97  return n;
98}
99
100class ClassWithDtor {
101public:
102  ~ClassWithDtor() {}
103};
104
105void fallthrough2(int n) {
106  switch (n) {
107    case 0:
108    {
109      ClassWithDtor temp;
110      break;
111    }
112    default: // no warning here, there's no fall-through
113      break;
114  }
115}
116
117#define MY_SWITCH(X, Y, Z, U, V) switch (X) { case Y: Z; case U: V; }
118#define MY_SWITCH2(X, Y, Z) switch (X) { Y; Z; }
119#define MY_CASE(X, Y) case X: Y
120#define MY_CASE2(X, Y, U, V) case X: Y; case U: V
121
122int fallthrough_macro1(int n) {
123  MY_SWITCH(n, 13, n *= 2, 14, break)  // expected-warning{{unannotated fall-through between switch labels}}
124
125  switch (n + 1) {
126    MY_CASE(33, n += 2);
127    MY_CASE(44, break);  // expected-warning{{unannotated fall-through between switch labels}}
128    MY_CASE(55, n += 3);
129  }
130
131  switch (n + 3) {
132    MY_CASE(333, return 333);
133    MY_CASE2(444, n += 44, 4444, break);  // expected-warning{{unannotated fall-through between switch labels}}
134    MY_CASE(555, n += 33);
135  }
136
137  MY_SWITCH2(n + 4, MY_CASE(17, n *= 3), MY_CASE(19, break))  // expected-warning{{unannotated fall-through between switch labels}}
138
139  MY_SWITCH2(n + 5, MY_CASE(21, break), MY_CASE2(23, n *= 7, 25, break))  // expected-warning{{unannotated fall-through between switch labels}}
140
141  return n;
142}
143
144int fallthrough_position(int n) {
145  switch (n) {
146      [[clang::fallthrough]];  // expected-warning{{fallthrough annotation in unreachable code}}
147    case 221:
148      [[clang::fallthrough]]; // expected-warning{{fallthrough annotation does not directly precede switch label}}
149      return 1;
150      [[clang::fallthrough]];  // expected-warning{{fallthrough annotation in unreachable code}}
151    case 222:
152      [[clang::fallthrough]]; // expected-warning{{fallthrough annotation does not directly precede switch label}}
153      n += 400;
154    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}}
155      [[clang::fallthrough]]; // expected-warning{{fallthrough annotation does not directly precede switch label}}
156  }
157  return n;
158}
159
160int fallthrough_targets(int n) {
161  [[clang::fallthrough]]; // expected-error{{fallthrough annotation is outside switch statement}}
162
163  [[clang::fallthrough]]  // expected-error{{fallthrough attribute is only allowed on empty statements}}
164  switch (n) {
165    case 121:
166      n += 400;
167      [[clang::fallthrough]]; // no warning here, correct target
168    case 123:
169      [[clang::fallthrough]]  // expected-error{{fallthrough attribute is only allowed on empty statements}}
170      n += 800;
171      break;
172    [[clang::fallthrough]]    // expected-error{{fallthrough attribute is only allowed on empty statements}} expected-note{{did you forget ';'?}}
173    case 125:
174      n += 1600;
175  }
176  return n;
177}
178