BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsla_used.h
Go to the documentation of this file.
1/// @file bsla_used.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsla_used.h -*-C++-*-
8#ifndef INCLUDED_BSLA_USED
9#define INCLUDED_BSLA_USED
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsla_used bsla_used
15/// @brief Provide a macro to prevent elision of unused entities.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsla
19/// @{
20/// @addtogroup bsla_used
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsla_used-purpose"> Purpose</a>
25/// * <a href="#bsla_used-macros"> Macros </a>
26/// * <a href="#bsla_used-description"> Description </a>
27/// * <a href="#bsla_used-macro-reference"> Macro Reference </a>
28/// * <a href="#bsla_used-usage"> Usage </a>
29/// * <a href="#bsla_used-example-1-unused-variables"> Example 1: Unused Variables </a>
30/// * <a href="#bsla_used-example-2-unused-functions"> Example 2: Unused Functions </a>
31///
32/// # Purpose {#bsla_used-purpose}
33/// Provide a macro to prevent elision of unused entities.
34///
35/// # Macros {#bsla_used-macros}
36///
37/// - BSLA_USED: emit annotated entity even if not referenced
38/// - BSLA_USED_IS_ACTIVE: defined if `BSLA_USED` is active
39///
40/// @see bsla_annotations
41///
42/// # Description {#bsla_used-description}
43/// This component provides a preprocessor macro that will
44/// guarantee the emission of a local function, type, or variable whether it is
45/// used or not.
46///
47/// ## Macro Reference {#bsla_used-macro-reference}
48///
49///
50/// `BSLA_USED`:
51/// This annotation indicates that the so-annotated function, variable, or
52/// type must be emitted even if it appears that it is not referenced.
53///
54/// `BSLA_USED_IS_ACTIVE`:
55/// The macro `BSLA_USED_IS_ACTIVE` is defined if `BSLA_USED` expands to
56/// something with the desired effect; otherwise `BSLA_USED_IS_ACTIVE` is
57/// not defined and `BSLA_USED` expands to nothing.
58///
59/// ## Usage {#bsla_used-usage}
60///
61///
62/// This section illustrates intended use of this component.
63///
64/// ### Example 1: Unused Variables {#bsla_used-example-1-unused-variables}
65///
66///
67/// First, we declare two unused static variables, one marked `BSLA_UNUSED` and
68/// the other marked `BSLA_USED`:
69/// @code
70/// static
71/// int usage_UNUSED_variable_no_warning BSLA_UNUSED;
72///
73/// static
74/// int usage_USED_variable_no_warning BSLA_USED;
75/// @endcode
76/// Finally, if we compile with clang and go into the debugger and stop in
77/// `main`, which is in the same file and from which both variables are visible,
78/// we observe that the variable marked `BSLA_UNUSED` cannot be accessed, but
79/// the variable marked `BSLA_USED` can.
80///
81/// ### Example 2: Unused Functions {#bsla_used-example-2-unused-functions}
82///
83///
84/// First, declare two unused static functions, one marked `BSLA_UNUSED` and one
85/// marked `BSLA_USED`:
86/// @code
87/// /// Print the specified 'woof'.
88/// static
89/// void usage_UNUSED_function_no_warning(int woof) BSLA_UNUSED;
90///
91/// static
92/// void usage_UNUSED_function_no_warning(int woof)
93/// {
94/// printf("%d\n", woof);
95/// }
96///
97/// /// Print the specified 'woof'.
98/// static
99/// void usage_USED_function_no_warning(int woof) BSLA_USED;
100///
101/// static
102/// void usage_USED_function_no_warning(int woof)
103/// {
104/// printf("%d\n", woof);
105/// }
106/// @endcode
107/// Finally, if we compile with clang and go into the debugger, we find that we
108/// can put a breakpoint in the function marked `BSLA_USED`, but not in the
109/// function marked `BSLA_UNUSED`.
110/// @}
111/** @} */
112/** @} */
113
114/** @addtogroup bsl
115 * @{
116 */
117/** @addtogroup bsla
118 * @{
119 */
120/** @addtogroup bsla_used
121 * @{
122 */
123
124#include <bsls_platform.h>
125
126// Note that we could conceivably migrate this to use '[[maybe_unused]]' when
127// available, but that has more specific constraints over where it can be
128// syntactically placed than the older vendor annotations.
129
130 // =============================
131 // Checks for Pre-Defined macros
132 // =============================
133
134#if defined(BSLA_USED)
135#error BSLA_USED is already defined!
136#endif
137
138#if defined(BSLA_USED_IS_ACTIVE)
139#error BSLA_USED_IS_ACTIVE is already defined!
140#endif
141
142 // =========================
143 // Set macros as appropriate
144 // =========================
145
146#if defined(BSLS_PLATFORM_CMP_GNU) || defined(BSLS_PLATFORM_CMP_CLANG)
147 #define BSLA_USED __attribute__((__used__))
148
149 #define BSLA_USED_IS_ACTIVE 1
150#else
151 #define BSLA_USED
152#endif
153
154#endif
155
156// ----------------------------------------------------------------------------
157// Copyright 2019 Bloomberg Finance L.P.
158//
159// Licensed under the Apache License, Version 2.0 (the "License");
160// you may not use this file except in compliance with the License.
161// You may obtain a copy of the License at
162//
163// http://www.apache.org/licenses/LICENSE-2.0
164//
165// Unless required by applicable law or agreed to in writing, software
166// distributed under the License is distributed on an "AS IS" BASIS,
167// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
168// See the License for the specific language governing permissions and
169// limitations under the License.
170// ----------------------------- END-OF-FILE ----------------------------------
171
172/** @} */
173/** @} */
174/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195