BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsls_assertimputil.h
Go to the documentation of this file.
1/// @file bsls_assertimputil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_assertimputil.h -*-C++-*-
8#ifndef INCLUDED_BSLS_ASSERTIMPUTIL
9#define INCLUDED_BSLS_ASSERTIMPUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_assertimputil bsls_assertimputil
15/// @brief Provide utilities to implement `bsls_assert` and `bsls_review`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_assertimputil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_assertimputil-purpose"> Purpose</a>
25/// * <a href="#bsls_assertimputil-classes"> Classes </a>
26/// * <a href="#bsls_assertimputil-description"> Description </a>
27/// * <a href="#bsls_assertimputil-bsls_assertimputil_avoid_string_constants"> BSLS_ASSERTIMPUTIL_AVOID_STRING_CONSTANTS </a>
28/// * <a href="#bsls_assertimputil-usage"> Usage </a>
29/// * <a href="#bsls_assertimputil-example-1-aborting-the-current-process"> Example 1: Aborting the Current Process </a>
30/// * <a href="#bsls_assertimputil-example-2-sleeping-forever"> Example 2: Sleeping Forever </a>
31///
32/// # Purpose {#bsls_assertimputil-purpose}
33/// Provide utilities to implement @ref bsls_assert and @ref bsls_review .
34///
35/// # Classes {#bsls_assertimputil-classes}
36///
37/// - bsls::AssertImpUtil: namespace for shared assert and review functions
38///
39/// # Description {#bsls_assertimputil-description}
40/// This component defines a `struct`, `bsls::AssertImpUtil`, that
41/// serves as a namespace for shared functions used by the various handlers
42/// provided by @ref bsls_assert and @ref bsls_review .
43///
44/// ## BSLS_ASSERTIMPUTIL_AVOID_STRING_CONSTANTS {#bsls_assertimputil-bsls_assertimputil_avoid_string_constants}
45///
46///
47/// On some platforms the string constants used to pass filenames to the assert
48/// macro invocations are not coalesced, so each inline function use containing
49/// such a macro puts an extra copy of the filename string into the resulting
50/// executable. For these platforms, it is possible to locally alter the
51/// filename that assert macros will use by altering the definition of
52/// `BSLS_ASSERTIMPUTIL_FILE`.
53///
54/// At the start of your component header, after all other include directives,
55/// place the following block of code to detect if this is a platform where the
56/// workaround is needed and apply it:
57/// @code
58/// // my_component.h
59/// // ...
60/// // 'BSLS_ASSERT' filename fix -- See {bsls_assertimputil}
61/// #ifdef BSLS_ASSERTIMPUTIL_AVOID_STRING_CONSTANTS
62/// extern const char s_my_component_h[];
63/// #undef BSLS_ASSERTIMPUTIL_FILE
64/// #define BSLS_ASSERTIMPUTIL_FILE BloombergLP::s_my_component_h
65/// #endif
66/// @endcode
67/// Then, at the end of your header revert the definition of the filename macro
68/// to its default:
69/// @code
70/// // Undo 'BSLS_ASSERT' filename fix -- See {bsls_assertimputil}
71/// #ifdef BSLS_ASSERTIMPUTIL_AVOID_STRING_CONSTANTS
72/// #undef BSLS_ASSERTIMPUTIL_FILE
73/// #define BSLS_ASSERTIMPUTIL_FILE BSLS_ASSERTIMPUTIL_DEFAULTFILE
74/// #endif
75/// @endcode
76/// Finally, in the `.cpp` file add the following:
77/// @code
78/// // 'BSLS_ASSERT' filename fix -- See {bsls_assertimputil}
79/// #ifdef BSLS_ASSERTIMPUTIL_AVOID_STRING_CONSTANTS
80/// extern const char s_my_component_h[] = "my_component.h";
81/// #endif
82/// @endcode
83/// Note that these constants should all be in an appropriate namespace and
84/// should have names and contents that match your actual component name.
85///
86/// ## Usage {#bsls_assertimputil-usage}
87///
88///
89/// This section illustrates the intended use of this component.
90///
91/// ### Example 1: Aborting the Current Process {#bsls_assertimputil-example-1-aborting-the-current-process}
92///
93///
94/// Suppose you are implementing an assertion handler that should cause a
95/// process to terminate when invoked. In order to stop the process
96/// immediately, you would call `failByAbort` like this:
97/// @code
98/// void myAbort()
99/// {
100/// bsls::AssertImpUtil::failByAbort();
101/// // This code should never be reached.
102/// }
103/// @endcode
104/// This function would then abort the current process.
105///
106/// ### Example 2: Sleeping Forever {#bsls_assertimputil-example-2-sleeping-forever}
107///
108///
109/// Suppose you want a process to no longer continue doing anything, but you
110/// want to leave it running in order to attach a debugger to it and diagnose
111/// the full state of your system. In order to have your process sleep forever,
112/// you might call `failBySleep` like this:
113/// @code
114/// void mySleep()
115/// {
116/// bsls::AssertImpUtil::failBySleep();
117/// // This code should never be reached.
118/// }
119/// @endcode
120/// This function would then sleep forever and never return.
121/// @}
122/** @} */
123/** @} */
124
125/** @addtogroup bsl
126 * @{
127 */
128/** @addtogroup bsls
129 * @{
130 */
131/** @addtogroup bsls_assertimputil
132 * @{
133 */
134
135#include <bsls_annotation.h>
137#include <bsls_linkcoercion.h>
138#include <bsls_platform.h>
139
140 // =========================================
141 // BSLS_ASSERTIMPUTIL_AVOID_STRING_CONSTANTS
142 // =========================================
143
144#ifdef BSLS_PLATFORM_CMP_SUN
145// On sun, string constants are not coalesced, so uses of 'BSLS_ASSERT' and
146// 'BSLS_REVIEW' macros in inlined functions lead to massive executable size
147// bloat. This flag indicates that a workaround should be enabled to minimize
148// references to strings literals in these macros. That workaround should
149// involve redefining 'BSLS_ASSERTIMPUTIL_FILE' to reference a static constant
150// that is initialized in the component's implementation file.
151
152#define BSLS_ASSERTIMPUTIL_AVOID_STRING_CONSTANTS
153
154#endif
155
156
157namespace bsls {
158 // ===========================
159 // Assert Macro Support Macros
160 // ===========================
161
162// Assertion Filename
163#define BSLS_ASSERTIMPUTIL_DEFAULTFILE __FILE__
164#define BSLS_ASSERTIMPUTIL_FILE BSLS_ASSERTIMPUTIL_DEFAULTFILE
165
166#define BSLS_ASSERTIMPUTIL_LINE __LINE__
167
168#ifdef BSLS_PLATFORM_CMP_SUN
169// On sun, if possible headers that use @ref bsls_review or @ref bsls_assert macros
170// in inline functions should attempt to specify their filenames as an extern
171// string defined in just the '.cpp' file of the component instead of just
172// using '__FILE__'.
173
174#define BSLS_ASSERTIMPUTIL_AVOID_STRING_CONSTANTS
175
176#endif
177
178 // =========================
179 // BSLS_ASSERT_USE_CONTRACTS
180 // =========================
181// Language-level contracts can be turned on with the external control macro
182// 'BSLS_ASSERT_USE_CONTRACTS' when running on a compiler that supports this.
183// Note that mixing builds where language contracts are supported and those
184// where they are not is not ABI-compatible, so we enforce that this does not
185// happen with a link coercion. Note also that this is likely not to be
186// ABI-compatible between distinct flavours of language level contracts in the
187// future, so we will coerce the use of distinct names to protect against mixed
188// builds.
189
190#ifdef BSLS_ASSERT_USE_CONTRACTS
191
192// The Lock3 branch of GCC supports a contract implementation sufficient to
193// build @ref bsls_assert and @ref bsls_review on. Full documentation for the
194// extensions it makes available can be found at
195// 'https://github.com/lock3/gcc/wiki/contracts'.
196
197#if !defined(__cpp_contracts_literal_semantics)
198#error BSLS_ASSERT_USE_CONTRACTS requires compiler contract support \
199 (__cpp_contracts_literal_semantics)
200#endif
201
202#ifndef BSLS_ASSERT_NORETURN_INVOKE_HANDLER
203// @ref bsls_assert with language-level contracts will use a non-continuing
204// contract mode, so we can no longer support a returning violation handler.
205#define BSLS_ASSERT_NORETURN_INVOKE_HANDLER
206#endif
207
208struct AssertImpUtil_UseContractsCpp20 {
209 static const int s_isAssertUseContracts;
210};
211typedef AssertImpUtil_UseContractsCpp20 AssertImpUtil_UseContracts;
212
213#else
214
219
220#endif
221
223 const int,
224 bsls_assertimputil_coerce_use_contracts,
226
227 // ====================
228 // struct AssertImpUtil
229 // ====================
230
231/// This "implementation utility" `struct` provides static functions with
232/// shared functionality that is made use of by both @ref bsls_assert and
233/// @ref bsls_review .
234struct AssertImpUtil {
235
236 public:
237 // CLASS METHODS
238
239 /// Unconditionally abort the current application. It is up to the
240 /// caller to first output a useful message describing the location of
241 /// the failure.
243 static void failByAbort();
244
245 /// Spin in an infinite loop. It is up to the caller to first output a
246 /// useful message describing the location of the failure.
248 static void failBySleep();
249};
250
251} // close package namespace
252
253
254 // ========================================================
255 // UNDEFINE THE LOCALLY-SCOPED IMPLEMENTATION DETAIL MACROS
256 // ========================================================
257
258#endif
259
260// ----------------------------------------------------------------------------
261// Copyright 2018 Bloomberg Finance L.P.
262//
263// Licensed under the Apache License, Version 2.0 (the "License");
264// you may not use this file except in compliance with the License.
265// You may obtain a copy of the License at
266//
267// http://www.apache.org/licenses/LICENSE-2.0
268//
269// Unless required by applicable law or agreed to in writing, software
270// distributed under the License is distributed on an "AS IS" BASIS,
271// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
272// See the License for the specific language governing permissions and
273// limitations under the License.
274// ----------------------------- END-OF-FILE ----------------------------------
275
276/** @} */
277/** @} */
278/** @} */
#define BSLS_ANNOTATION_NORETURN
Definition bsls_annotation.h:383
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_LINKCOERCION_FORCE_SYMBOL_DEPENDENCY(type, refName, referredSymbol)
Definition bsls_linkcoercion.h:194
Definition bdlt_iso8601util.h:691
AssertImpUtil_UseContractsNo AssertImpUtil_UseContracts
Definition bsls_assertimputil.h:218
Definition bsls_assertimputil.h:215
static const int s_isAssertUseContracts
Definition bsls_assertimputil.h:216