BDE 4.39.x 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, except on Microsoft Visual C++ that does not support it:
89/// @code
90/// #if !defined(BSLS_PLATFORM_CMP_MSVC)
91/// BSLA_FALLTHROUGH;
92/// #endif
93/// }
94/// case 1:
95/// @endcode
96/// Then, we see this also works on `case`s that don't have a `{}` block:
97/// @code
98/// jj -= 6;
99/// printf("%d\n", jj);
100/// BSLA_FALLTHROUGH;
101/// case 2: {
102/// if (jj > 4) {
103/// printf("%d\n", jj + 10);
104/// @endcode
105/// Next, we see that a `BSLA_FALLTHROUGH;` works within an `if` block, provided
106/// that it's in the last statement in the flow of control before falling
107/// through:
108/// @code
109/// BSLA_FALLTHROUGH;
110/// }
111/// else {
112/// return 0; // RETURN
113/// }
114/// }
115/// case 3: {
116/// if (jj > 4) {
117/// continue;
118/// }
119/// else {
120/// printf("%d\n", ++jj);
121/// @endcode
122/// Now, we see that a `BSLA_FALLTHROUGH;` can also occur as the last statement
123/// in an `else` block:
124/// @code
125/// BSLA_FALLTHROUGH;
126/// }
127/// }
128/// default: {
129/// return 1; // RETURN
130/// } break;
131/// }
132/// }
133///
134/// return -7;
135/// }
136/// @endcode
137/// Finally, we see that if we compile when `BSLA_FALLTHROUGH_IS_ACTIVE` is set,
138/// the above compiles with no warnings.
139/// @}
140/** @} */
141/** @} */
142
143/** @addtogroup bsl
144 * @{
145 */
146/** @addtogroup bsla
147 * @{
148 */
149/** @addtogroup bsla_fallthrough
150 * @{
151 */
152
153#include <bsls_compilerfeatures.h>
154#include <bsls_platform.h>
155
156 // =============================
157 // Checks for Pre-Defined macros
158 // =============================
159
160#if defined(BSLA_FALLTHROUGH)
161#error BSLA_FALLTHROUGH is already defined!
162#endif
163
164#if defined(BSLA_FALLTHROUGH_IS_ACTIVE)
165#error BSLA_FALLTHROUGH_IS_ACTIVE is already defined!
166#endif
167
168 // =========================
169 // Set macros as appropriate
170 // =========================
171
172#if defined(BSLS_COMPILERFEATURES_SUPPORT_ATTRIBUTE_FALLTHROUGH)
173 #define BSLA_FALLTHROUGH [[ fallthrough ]]
174#elif defined(BSLS_PLATFORM_CMP_GNU) && BSLS_PLATFORM_CMP_VERSION >= 70000
175 #define BSLA_FALLTHROUGH __attribute__((fallthrough))
176#elif defined(BSLS_PLATFORM_CMP_CLANG)
177 #if __cplusplus >= 201103L && defined(__has_warning)
178 #if __has_feature(cxx_attributes) && \
179 __has_warning("-Wimplicit-fallthrough")
180 #define BSLA_FALLTHROUGH [[clang::fallthrough]]
181 #endif
182 #endif
183#endif
184
185#if defined(BSLA_FALLTHROUGH)
186 #define BSLA_FALLTHROUGH_IS_ACTIVE 1
187#else
188 #define BSLA_FALLTHROUGH
189#endif
190
191#endif
192
193// ----------------------------------------------------------------------------
194// Copyright 2019 Bloomberg Finance L.P.
195//
196// Licensed under the Apache License, Version 2.0 (the "License");
197// you may not use this file except in compliance with the License.
198// You may obtain a copy of the License at
199//
200// http://www.apache.org/licenses/LICENSE-2.0
201//
202// Unless required by applicable law or agreed to in writing, software
203// distributed under the License is distributed on an "AS IS" BASIS,
204// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
205// See the License for the specific language governing permissions and
206// limitations under the License.
207// ----------------------------- END-OF-FILE ----------------------------------
208
209/** @} */
210/** @} */
211/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238