BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsla_maybeunused.h
Go to the documentation of this file.
1/// @file bsla_maybeunused.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsla_maybeunused.h -*-C++-*-
8#ifndef INCLUDED_BSLA_MAYBEUNUSED
9#define INCLUDED_BSLA_MAYBEUNUSED
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsla_maybeunused bsla_maybeunused
15/// @brief Provide a macro to suppress "unused" warnings.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsla
19/// @{
20/// @addtogroup bsla_maybeunused
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsla_maybeunused-purpose"> Purpose</a>
25/// * <a href="#bsla_maybeunused-macros"> Macros </a>
26/// * <a href="#bsla_maybeunused-description"> Description </a>
27/// * <a href="#bsla_maybeunused-macro-reference"> Macro Reference </a>
28/// * <a href="#bsla_maybeunused-usage"> Usage </a>
29/// * <a href="#bsla_maybeunused-example-1-unused-warnings"> Example 1: "Unused" Warnings </a>
30///
31/// # Purpose {#bsla_maybeunused-purpose}
32/// Provide a macro to suppress "unused" warnings.
33///
34/// # Macros {#bsla_maybeunused-macros}
35///
36/// - BSLA_MAYBE_UNUSED: suppress compiler warnings on unused entities
37/// - BSLA_MAYBE_UNUSED_IS_ACTIVE: defined if `BSLA_MAYBE_UNUSED` is active
38///
39/// @see bsla_annotations
40///
41/// # Description {#bsla_maybeunused-description}
42/// This component provides a preprocessor macro that will suppress
43/// "unused" warnings on a locally defined function, type, or variable that is
44/// not used.
45///
46/// ## Macro Reference {#bsla_maybeunused-macro-reference}
47///
48///
49/// `BSLA_MAYBE_UNUSED`:
50/// This annotation indicates that the so-annotated function, variable, or
51/// type is possibly unused and the compiler should not generate a warning
52/// for the unused identifier.
53///
54/// `BSLA_MAYBE_UNUSED_IS_ACTIVE`:
55/// The macro `BSLA_MAYBE_UNUSED_IS_ACTIVE` is defined if
56/// `BSLA_MAYBE_UNUSED` expands to something with the desired effect;
57/// otherwise `BSLA_MAYBE_UNUSED_IS_ACTIVE` is not defined and
58/// `BSLA_MAYBE_UNUSED` expands to nothing.
59///
60/// ## Usage {#bsla_maybeunused-usage}
61///
62///
63/// This section illustrates intended use of this component.
64///
65/// ### Example 1: "Unused" Warnings {#bsla_maybeunused-example-1-unused-warnings}
66///
67///
68/// First, we define a namespace, `warn`, within the unnamed namespace with a
69/// type, a function, and a variable in it. They are unused:
70/// @code
71/// namespace {
72/// namespace warn {
73///
74/// double x;
75///
76/// struct ResultRec {
77/// double d_x;
78/// double d_y;
79/// };
80///
81/// int quadratic(double *zeroA,
82/// double *zeroB,
83/// double *zeroC,
84/// double cubeFactor,
85/// double a,
86/// double b,
87/// double c)
88/// // Solve the quadratic function for the specified 'a', 'b', and 'c',
89/// // where '0 = a * x^2 + b * x + c'. If the quadratic has no solutions,
90/// // return a non-zero value, and set the specified 'zeroA' and 'zeroB'
91/// // to those solutions and return 0 otherwise. The specified
92/// // 'cubeFactor' and 'zeroC' are unused for now but will be used in
93/// // future expansion of the function to handle cubic polynomials.
94/// {
95/// typedef int UnusedTypedef;
96///
97/// const double discriminant = b * b - 4 * a * c;
98/// if (discriminant < 0 || 0.0 == a) {
99/// *zeroA = *zeroB = 0.0;
100/// return -1; // RETURN
101/// }
102///
103/// const double root = ::sqrt(discriminant);
104/// *zeroA = (-b + root) / (2 * a);
105/// *zeroB = (-b - root) / (2 * a);
106///
107/// return 0;
108/// }
109///
110/// } // close namespace warn
111/// } // close unnamed namespace
112/// @endcode
113/// Then, we observe the warnings:
114/// @code
115/// .../bsla_maybeunused.t.cpp:145:21: warning: typedef 'UnusedTypedef' locally
116/// defined but not used [-Wunused-local-typedefs]
117/// typedef int UnusedTypedef;
118/// ^~~~~~~~~~~~~
119/// .../bsla_maybeunused.t.cpp:133:27: warning: unused parameter 'zeroC'
120/// [-Wunused-parameter]
121/// double *zeroC,
122/// ^~~~~
123/// .../bsla_maybeunused.t.cpp:134:26: warning: unused parameter 'cubeFactor'
124/// [-Wunused-parameter]
125/// double cubeFactor,
126/// ^~~~~~~~~~
127/// .../bsla_maybeunused.t.cpp:131:9: warning: 'int {anonymous}::warn::
128/// quadratic(double*, double*, double*, double, double, double, double)'
129/// defined but not used [-Wunused-function]
130/// int quadratic(double *zeroA,
131/// ^~~~~~~~~
132/// .../bsla_maybeunused.t.cpp:124:12: warning: '{anonymous}::warn::x' defined
133/// but not used [-Wunused-variable]
134/// double x;
135/// ^
136/// @endcode
137/// Note that none of the compilers currently in use by the development team
138/// issue a warning on the unused `warn::ResultRec`, but some in the future
139/// might. In the meantime, `BSLA_MAYBE_UNUSED` is tolerated on type
140/// declarations without resulting in a syntax error.
141///
142/// Next, we define a namespace, `nowarn`, within the unused namespace with
143/// exactly the same unused entities, using the `BSLA_MAYBE_UNUSED` annotation
144/// to silence the warnings:
145/// @code
146/// namespace {
147/// namespace nowarn {
148///
149/// struct BSLA_MAYBE_UNUSED ResultRec {
150/// double d_x;
151/// double d_y;
152/// };
153///
154/// BSLA_MAYBE_UNUSED double x;
155///
156/// BSLA_MAYBE_UNUSED int quadratic(double *zeroA,
157/// double *zeroB,
158/// BSLA_MAYBE_UNUSED double *zeroC,
159/// BSLA_MAYBE_UNUSED double cubeFactor,
160/// double a,
161/// double b,
162/// double c);
163/// // Solve the quadratic function for the specified 'a', 'b', and 'c',
164/// // where '0 = a * x^2 + b * x + c'. If the quadratic has no solutions,
165/// // return a non-zero value, and set the specified 'zeroA' and 'zeroB'
166/// // to those solutions and return 0 otherwise. The specified
167/// // 'cubeFactor' and 'zeroC' are unused for now but will be used in
168/// // future expansion of the function to handle cubic polynomials.
169///
170/// int quadratic(double *zeroA,
171/// double *zeroB,
172/// BSLA_MAYBE_UNUSED double *zeroC,
173/// BSLA_MAYBE_UNUSED double cubeFactor,
174/// double a,
175/// double b,
176/// double c)
177/// {
178/// BSLA_MAYBE_UNUSED typedef int UnusedTypedef;
179///
180/// const double discriminant = b * b - 4 * a * c;
181/// if (discriminant < 0 || 0.0 == a) {
182/// *zeroA = *zeroB = 0.0;
183/// return -1; // RETURN
184/// }
185///
186/// const double root = ::sqrt(discriminant);
187/// *zeroA = (-b + root) / (2 * a);
188/// *zeroB = (-b - root) / (2 * a);
189///
190/// return 0;
191/// }
192///
193/// } // close namespace nowarn
194/// } // close unnamed namespace
195/// @endcode
196/// Finally, we observe that the warnings for the `nowarn` namespace are
197/// suppressed.
198/// @}
199/** @} */
200/** @} */
201
202/** @addtogroup bsl
203 * @{
204 */
205/** @addtogroup bsla
206 * @{
207 */
208/** @addtogroup bsla_maybeunused
209 * @{
210 */
211
212#include <bsls_compilerfeatures.h>
213
214 // =============================
215 // Checks for Pre-Defined macros
216 // =============================
217
218#if defined(BSLA_MAYBE_USED)
219#error BSLA_MAYBE_USED is already defined!
220#endif
221
222#if defined(BSLA_MAYBE_USED_IS_ACTIVE)
223#error BSLA_MAYBE_USED_IS_ACTIVE is already defined!
224#endif
225
226 // =========================
227 // Set macros as appropriate
228 // =========================
229
230#if defined(BSLS_COMPILERFEATURES_SUPPORT_ATTRIBUTE_MAYBE_UNUSED)
231 #define BSLA_MAYBE_UNUSED [[ maybe_unused ]]
232
233 #define BSLA_MAYBE_UNUSED_IS_ACTIVE 1
234#elif defined(BSLS_PLATFORM_CMP_GNU) || defined(BSLS_PLATFORM_CMP_CLANG)
235 #define BSLA_MAYBE_UNUSED __attribute__((__unused__))
236
237 #define BSLA_MAYBE_UNUSED_IS_ACTIVE 1
238#else
239 #define BSLA_MAYBE_UNUSED
240#endif
241
242#endif
243
244// ----------------------------------------------------------------------------
245// Copyright 2019 Bloomberg Finance L.P.
246//
247// Licensed under the Apache License, Version 2.0 (the "License");
248// you may not use this file except in compliance with the License.
249// You may obtain a copy of the License at
250//
251// http://www.apache.org/licenses/LICENSE-2.0
252//
253// Unless required by applicable law or agreed to in writing, software
254// distributed under the License is distributed on an "AS IS" BASIS,
255// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
256// See the License for the specific language governing permissions and
257// limitations under the License.
258// ----------------------------- END-OF-FILE ----------------------------------
259
260/** @} */
261/** @} */
262/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195