BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsla_fallthrough.h
Go to the documentation of this file.
1/// @file bsla_fallthrough.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsla_fallthrough.h -*-C++-*-
8#ifndef INCLUDED_BSLA_FALLTHROUGH
9#define INCLUDED_BSLA_FALLTHROUGH
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsla_fallthrough bsla_fallthrough
15/// @brief Provide a macro to suppress warnings on `switch` fall-throughs.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsla
19/// @{
20/// @addtogroup bsla_fallthrough
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsla_fallthrough-purpose"> Purpose</a>
25/// * <a href="#bsla_fallthrough-macros"> Macros </a>
26/// * <a href="#bsla_fallthrough-description"> Description </a>
27/// * <a href="#bsla_fallthrough-macro-reference"> Macro Reference </a>
28/// * <a href="#bsla_fallthrough-usage"> Usage </a>
29/// * <a href="#bsla_fallthrough-example-1-suppressing-fall-through-warnings-in-a-switch-statement"> Example 1: Suppressing Fall-Through Warnings in a switch Statement </a>
30///
31/// # Purpose {#bsla_fallthrough-purpose}
32/// Provide a macro to suppress warnings on `switch` fall-throughs.
33///
34/// # Macros {#bsla_fallthrough-macros}
35///
36/// - BSLA_FALLTHROUGH: do not warn if `switch` `case` falls through
37/// - BSLA_FALLTHROUGH_IS_ACTIVE: defined if `BSLA_FALLTHROUGH` is active
38///
39/// @see bsla_annotations
40///
41/// # Description {#bsla_fallthrough-description}
42/// This component provides a preprocessor macro that suppresses
43/// compiler warnings about flow of control fall-through from one `case` or
44/// `default` of a `switch` statement to another. On compilers where the
45/// appropriate attribute is not supported, the macro expands to nothing.
46///
47/// ## Macro Reference {#bsla_fallthrough-macro-reference}
48///
49///
50/// `BSLA_FALLTHROUGH`:
51/// This annotation should be placed in a `case` clause as the last
52/// statement within a flow of control that is expected to allow control to
53/// fall through instead of ending with a `break`, `continue`, or `return`.
54/// This will prevent compilers from warning about fall-through. The
55/// `BSLA_FALLTHROUGH` must be followed by a semicolon and may be nested
56/// within blocks, `if`s, or `else`s.
57///
58/// `BSLA_FALLTHROUGH_IS_ACTIVE`:
59/// The macro `BSLA_FALLTHROUGH_IS_ACTIVE` is defined if `BSLA_FALLTHROUGH`
60/// expands to something with the desired effect; otherwise
61/// `BSLA_FALLTHROUGH_IS_ACTIVE` is not defined and `BSLA_FALLTHROUGH`
62/// expands to nothing.
63///
64/// ## Usage {#bsla_fallthrough-usage}
65///
66///
67/// This section illustrates intended use of this component.
68///
69/// ### Example 1: Suppressing Fall-Through Warnings in a switch Statement {#bsla_fallthrough-example-1-suppressing-fall-through-warnings-in-a-switch-statement}
70///
71///
72/// First, we define a function:
73/// @code
74/// int usageFunction(int jj)
75/// // Demonstrate the usage of 'BSLA_FALLTHROUGH', read the specified
76/// // 'jj'.
77/// {
78/// for (int ii = 0; ii < 5; ++ii) {
79/// @endcode
80/// Then, we have a `switch` in the function:
81/// @code
82/// switch (ii) {
83/// case 0: {
84/// printf("%d\n", jj - 3);
85/// @endcode
86/// Next, we see that `BSLA_FALLTHROUGH;` as the last statement in a `case`
87/// block before falling through silences the fall-through warning from the
88/// compiler:
89/// @code
90/// BSLA_FALLTHROUGH;
91/// }
92/// case 1:
93/// @endcode
94/// Then, we see this also works on `case`s that don't have a `{}` block:
95/// @code
96/// jj -= 6;
97/// printf("%d\n", jj);
98/// BSLA_FALLTHROUGH;
99/// case 2: {
100/// if (jj > 4) {
101/// printf("%d\n", jj + 10);
102/// @endcode
103/// Next, we see that a `BSLA_FALLTHROUGH;` works within an `if` block, provided
104/// that it's in the last statement in the flow of control before falling
105/// through:
106/// @code
107/// BSLA_FALLTHROUGH;
108/// }
109/// else {
110/// return 0; // RETURN
111/// }
112/// }
113/// case 3: {
114/// if (jj > 4) {
115/// continue;
116/// }
117/// else {
118/// printf("%d\n", ++jj);
119/// @endcode
120/// Now, we see that a `BSLA_FALLTHROUGH;` can also occur as the last statement
121/// in an `else` block:
122/// @code
123/// BSLA_FALLTHROUGH;
124/// }
125/// }
126/// default: {
127/// return 1; // RETURN
128/// } break;
129/// }
130/// }
131///
132/// return -7;
133/// }
134/// @endcode
135/// Finally, we see that if we compile when `BSLA_FALLTHROUGH_IS_ACTIVE` is set,
136/// the above compiles with no warnings.
137/// @}
138/** @} */
139/** @} */
140
141/** @addtogroup bsl
142 * @{
143 */
144/** @addtogroup bsla
145 * @{
146 */
147/** @addtogroup bsla_fallthrough
148 * @{
149 */
150
151#include <bsls_compilerfeatures.h>
152#include <bsls_platform.h>
153
154 // =============================
155 // Checks for Pre-Defined macros
156 // =============================
157
158#if defined(BSLA_FALLTHROUGH)
159#error BSLA_FALLTHROUGH is already defined!
160#endif
161
162#if defined(BSLA_FALLTHROUGH_IS_ACTIVE)
163#error BSLA_FALLTHROUGH_IS_ACTIVE is already defined!
164#endif
165
166 // =========================
167 // Set macros as appropriate
168 // =========================
169
170#if defined(BSLS_COMPILERFEATURES_SUPPORT_ATTRIBUTE_FALLTHROUGH)
171 #define BSLA_FALLTHROUGH [[ fallthrough ]]
172#elif defined(BSLS_PLATFORM_CMP_GNU) && BSLS_PLATFORM_CMP_VERSION >= 70000
173 #define BSLA_FALLTHROUGH __attribute__((fallthrough))
174#elif defined(BSLS_PLATFORM_CMP_CLANG)
175 #if __cplusplus >= 201103L && defined(__has_warning)
176 #if __has_feature(cxx_attributes) && \
177 __has_warning("-Wimplicit-fallthrough")
178 #define BSLA_FALLTHROUGH [[clang::fallthrough]]
179 #endif
180 #endif
181#endif
182
183#if defined(BSLA_FALLTHROUGH)
184 #define BSLA_FALLTHROUGH_IS_ACTIVE 1
185#else
186 #define BSLA_FALLTHROUGH
187#endif
188
189#endif
190
191// ----------------------------------------------------------------------------
192// Copyright 2019 Bloomberg Finance L.P.
193//
194// Licensed under the Apache License, Version 2.0 (the "License");
195// you may not use this file except in compliance with the License.
196// You may obtain a copy of the License at
197//
198// http://www.apache.org/licenses/LICENSE-2.0
199//
200// Unless required by applicable law or agreed to in writing, software
201// distributed under the License is distributed on an "AS IS" BASIS,
202// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
203// See the License for the specific language governing permissions and
204// limitations under the License.
205// ----------------------------- END-OF-FILE ----------------------------------
206
207/** @} */
208/** @} */
209/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195