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