xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/switch-implicit-fallthrough-macro.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wimplicit-fallthrough -DCOMMAND_LINE_FALLTHROUGH=[[clang::fallthrough]] %s
2*f4a2713aSLionel Sambuc 
fallthrough_compatibility_macro_from_command_line(int n)3*f4a2713aSLionel Sambuc int fallthrough_compatibility_macro_from_command_line(int n) {
4*f4a2713aSLionel Sambuc   switch (n) {
5*f4a2713aSLionel Sambuc     case 0:
6*f4a2713aSLionel Sambuc       n = n * 10;
7*f4a2713aSLionel Sambuc     case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'COMMAND_LINE_FALLTHROUGH;' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
8*f4a2713aSLionel Sambuc       ;
9*f4a2713aSLionel Sambuc   }
10*f4a2713aSLionel Sambuc   return n;
11*f4a2713aSLionel Sambuc }
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc #ifdef __clang__
14*f4a2713aSLionel Sambuc #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
15*f4a2713aSLionel Sambuc #define COMPATIBILITY_FALLTHROUGH   [ [ /* test */  clang /* test */ \
16*f4a2713aSLionel Sambuc     ::  fallthrough  ]  ]    // testing whitespace and comments in macro definition
17*f4a2713aSLionel Sambuc #endif
18*f4a2713aSLionel Sambuc #endif
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc #ifndef COMPATIBILITY_FALLTHROUGH
21*f4a2713aSLionel Sambuc #define COMPATIBILITY_FALLTHROUGH do { } while (0)
22*f4a2713aSLionel Sambuc #endif
23*f4a2713aSLionel Sambuc 
fallthrough_compatibility_macro_from_source(int n)24*f4a2713aSLionel Sambuc int fallthrough_compatibility_macro_from_source(int n) {
25*f4a2713aSLionel Sambuc   switch (n) {
26*f4a2713aSLionel Sambuc     case 0:
27*f4a2713aSLionel Sambuc       n = n * 20;
28*f4a2713aSLionel Sambuc     case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'COMPATIBILITY_FALLTHROUGH;' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
29*f4a2713aSLionel Sambuc       ;
30*f4a2713aSLionel Sambuc   }
31*f4a2713aSLionel Sambuc   return n;
32*f4a2713aSLionel Sambuc }
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc // Deeper macro substitution
35*f4a2713aSLionel Sambuc #define M1 [[clang::fallthrough]]
36*f4a2713aSLionel Sambuc #ifdef __clang__
37*f4a2713aSLionel Sambuc #define M2 M1
38*f4a2713aSLionel Sambuc #else
39*f4a2713aSLionel Sambuc #define M2
40*f4a2713aSLionel Sambuc #endif
41*f4a2713aSLionel Sambuc 
42*f4a2713aSLionel Sambuc #define WRONG_MACRO1 clang::fallthrough
43*f4a2713aSLionel Sambuc #define WRONG_MACRO2 [[clang::fallthrough]
44*f4a2713aSLionel Sambuc #define WRONG_MACRO3 [[clang::fall through]]
45*f4a2713aSLionel Sambuc #define WRONG_MACRO4 [[clang::fallthrough]]]
46*f4a2713aSLionel Sambuc 
fallthrough_compatibility_macro_in_macro(int n)47*f4a2713aSLionel Sambuc int fallthrough_compatibility_macro_in_macro(int n) {
48*f4a2713aSLionel Sambuc   switch (n) {
49*f4a2713aSLionel Sambuc     case 0:
50*f4a2713aSLionel Sambuc       n = n * 20;
51*f4a2713aSLionel Sambuc     case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'M1;' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
52*f4a2713aSLionel Sambuc                                                                           // there was an idea that this ^ should be M2
53*f4a2713aSLionel Sambuc       ;
54*f4a2713aSLionel Sambuc   }
55*f4a2713aSLionel Sambuc   return n;
56*f4a2713aSLionel Sambuc }
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc #undef M1
59*f4a2713aSLionel Sambuc #undef M2
60*f4a2713aSLionel Sambuc #undef COMPATIBILITY_FALLTHROUGH
61*f4a2713aSLionel Sambuc #undef COMMAND_LINE_FALLTHROUGH
62*f4a2713aSLionel Sambuc 
fallthrough_compatibility_macro_undefined(int n)63*f4a2713aSLionel Sambuc int fallthrough_compatibility_macro_undefined(int n) {
64*f4a2713aSLionel Sambuc   switch (n) {
65*f4a2713aSLionel Sambuc     case 0:
66*f4a2713aSLionel Sambuc       n = n * 20;
67*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}}
68*f4a2713aSLionel Sambuc       ;
69*f4a2713aSLionel Sambuc   }
70*f4a2713aSLionel Sambuc #define TOO_LATE [[clang::fallthrough]]
71*f4a2713aSLionel Sambuc   return n;
72*f4a2713aSLionel Sambuc }
73*f4a2713aSLionel Sambuc #undef TOO_LATE
74*f4a2713aSLionel Sambuc 
75*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY 11111111
76*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY
77*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY [[clang::fallthrough]]
78*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY
79*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY 2222222
80*f4a2713aSLionel Sambuc 
fallthrough_compatibility_macro_history(int n)81*f4a2713aSLionel Sambuc int fallthrough_compatibility_macro_history(int n) {
82*f4a2713aSLionel Sambuc   switch (n) {
83*f4a2713aSLionel Sambuc     case 0:
84*f4a2713aSLionel Sambuc       n = n * 20;
85*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY
86*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}}
87*f4a2713aSLionel Sambuc       ;
88*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY [[clang::fallthrough]]
89*f4a2713aSLionel Sambuc   }
90*f4a2713aSLionel Sambuc   return n;
91*f4a2713aSLionel Sambuc }
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY
94*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY 11111111
95*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY
96*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY [[clang::fallthrough]]
97*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY
98*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY 2222222
99*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY
100*f4a2713aSLionel Sambuc 
fallthrough_compatibility_macro_history2(int n)101*f4a2713aSLionel Sambuc int fallthrough_compatibility_macro_history2(int n) {
102*f4a2713aSLionel Sambuc   switch (n) {
103*f4a2713aSLionel Sambuc     case 0:
104*f4a2713aSLionel Sambuc       n = n * 20;
105*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY [[clang::fallthrough]]
106*f4a2713aSLionel Sambuc     case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'MACRO_WITH_HISTORY;' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
107*f4a2713aSLionel Sambuc       ;
108*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY
109*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY 3333333
110*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY
111*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY 4444444
112*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY
113*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY 5555555
114*f4a2713aSLionel Sambuc   }
115*f4a2713aSLionel Sambuc   return n;
116*f4a2713aSLionel Sambuc }
117*f4a2713aSLionel Sambuc 
118*f4a2713aSLionel Sambuc template<const int N>
fallthrough_compatibility_macro_history_template(int n)119*f4a2713aSLionel Sambuc int fallthrough_compatibility_macro_history_template(int n) {
120*f4a2713aSLionel Sambuc   switch (N * n) {
121*f4a2713aSLionel Sambuc     case 0:
122*f4a2713aSLionel Sambuc       n = n * 20;
123*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY2 [[clang::fallthrough]]
124*f4a2713aSLionel Sambuc     case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert 'MACRO_WITH_HISTORY2;' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
125*f4a2713aSLionel Sambuc       ;
126*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY2
127*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY2 3333333
128*f4a2713aSLionel Sambuc   }
129*f4a2713aSLionel Sambuc   return n;
130*f4a2713aSLionel Sambuc }
131*f4a2713aSLionel Sambuc 
132*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY2
133*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY2 4444444
134*f4a2713aSLionel Sambuc #undef MACRO_WITH_HISTORY2
135*f4a2713aSLionel Sambuc #define MACRO_WITH_HISTORY2 5555555
136*f4a2713aSLionel Sambuc 
f()137*f4a2713aSLionel Sambuc void f() {
138*f4a2713aSLionel Sambuc   fallthrough_compatibility_macro_history_template<1>(0); // expected-note{{in instantiation of function template specialization 'fallthrough_compatibility_macro_history_template<1>' requested here}}
139*f4a2713aSLionel Sambuc }
140