Clang Project

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