BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsls_preconditions.h
Go to the documentation of this file.
1/// @file bsls_preconditions.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_preconditions.h -*-C++-*-
8#ifndef INCLUDED_BSLS_PRECONDITIONS
9#define INCLUDED_BSLS_PRECONDITIONS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14
15/// @defgroup bsls_preconditions bsls_preconditions
16/// @brief Provide macros for use in fuzz testing narrow contract functions.
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bsls
20/// @{
21/// @addtogroup bsls_preconditions
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bsls_preconditions-purpose"> Purpose</a>
26/// * <a href="#bsls_preconditions-classes"> Classes </a>
27/// * <a href="#bsls_preconditions-macros"> Macros </a>
28/// * <a href="#bsls_preconditions-description"> Description </a>
29/// * <a href="#bsls_preconditions-usage"> Usage </a>
30///
31/// # Purpose {#bsls_preconditions-purpose}
32/// Provide macros for use in fuzz testing narrow contract functions.
33///
34/// # Classes {#bsls_preconditions-classes}
35///
36/// - bsls::PreconditionsHandler: for begin/end callback management functions
37///
38/// # Macros {#bsls_preconditions-macros}
39///
40/// - BSLS_PRECONDITIONS_BEGIN: mark the start of function preconditions
41/// - BSLS_PRECONDITIONS_END: mark the end of function preconditions
42///
43/// @see bsls_fuzztest
44///
45/// # Description {#bsls_preconditions-description}
46/// This component provides macros, `BSLS_PRECONDITIONS_BEGIN` and
47/// `BSLS_PRECONDITIONS_END`, to facilitate fuzz testing narrow contract
48/// functions. When fuzz testing is not enabled, the macros expand to nothing.
49/// When fuzz testing is enabled, the macros invoke a dynamic handler function
50/// via `bsls::PreconditionsHandler`.
51///
52/// `BSLS_PRECONDITIONS_BEGIN` is used as a marker to identify where
53/// precondition checks are begun, while `BSLS_PRECONDITIONS_END` is used as a
54/// marker to identify where precondition checks are complete. These macros
55/// should always be used as a pair, and always at the very beginning of a
56/// function, surrounding the function preconditions.
57///
58/// ## Usage {#bsls_preconditions-usage}
59///
60///
61/// Since the macros contained in this component are intended to be used in
62/// conjunction with the macros defined in @ref bsls_fuzztest , this test driver
63/// contains only the simplest USAGE EXAMPLE. See the USAGE EXAMPLE in
64/// @ref bsls_fuzztest for a fuller treatment.
65///
66/// The following example shows the use of `BSLS_PRECONDITIONS_BEGIN` and
67/// `BSLS_PRECONDITIONS_END` in the definition of a narrow contract function.
68/// These macros are to be placed around the function precondition checks,
69/// immediately before and after.
70/// @code
71/// double mySqrt(double x)
72/// // Return the square root of the specified 'x'. The behavior is
73/// // undefined unless 'x >= 0'.
74/// {
75/// BSLS_PRECONDITIONS_BEGIN();
76/// BSLS_ASSERT(0 <= x);
77/// BSLS_PRECONDITIONS_END();
78/// return sqrt(x);
79/// }
80/// @endcode
81/// In a fuzz-enabled build, we would invoke this function inside the fuzz loop
82/// with `BSLS_FUZZTEST_EVALUATE`.
83/// @}
84/** @} */
85/** @} */
86
87/** @addtogroup bsl
88 * @{
89 */
90/** @addtogroup bsls
91 * @{
92 */
93/** @addtogroup bsls_preconditions
94 * @{
95 */
96
97#include <bsls_assert.h>
98#include <bsls_atomicoperations.h> // 'AtomicTypes'
99#include <bsls_consteval.h>
100#include <bsls_pointercastutil.h>
101
102 // =================
103 // Macro Definitions
104 // =================
105#define BSLS_PRECONDITIONS_BEGIN_IMP() do { \
106 bsls::PreconditionsHandler::invokeBeginHandler(); \
107 } while (false)
108
109#define BSLS_PRECONDITIONS_END_IMP() do { \
110 bsls::PreconditionsHandler::invokeEndHandler(); \
111 } while (false)
112
113#if defined(BDE_ACTIVATE_FUZZ_TESTING) && \
114 defined(BSLS_CONSTEVAL_IS_CONSTANT_EVALUATED_IS_ACTIVE)
115
116 #define BSLS_PRECONDITIONS_BEGIN() do { \
117 if (!BSLS_CONSTEVAL_IS_CONSTANT_EVALUATED) { \
118 BSLS_PRECONDITIONS_BEGIN_IMP(); \
119 } \
120 } while (false)
121
122 #define BSLS_PRECONDITIONS_END() do { \
123 if (!BSLS_CONSTEVAL_IS_CONSTANT_EVALUATED) { \
124 BSLS_PRECONDITIONS_END_IMP(); \
125 } \
126 } while (false)
127
128#else // fuzzing not enabled or 'consteval' not active
129
130#define BSLS_PRECONDITIONS_BEGIN() do {} while(false)
131#define BSLS_PRECONDITIONS_END() do {} while(false)
132
133#endif
134
135
136namespace bsls {
137
138 // ===========================
139 // class PreconditionsHandler
140 // ===========================
141
142/// This utility class maintains pointers containing the addresses of
143/// functions invoked by the `BSLS_PRECONDITIONS_BEGIN` and
144/// `BSLS_PRECONDITIONS_END` macros, and provides methods to
145/// manipulate and utilize those functions.
146///
147/// See @ref bsls_preconditions
149
150 private:
151 // CLASS DATA
152 static AtomicOperations::AtomicTypes::Pointer
153 s_beginHandler; // begin handler function
154 static AtomicOperations::AtomicTypes::Pointer
155 s_endHandler; // end handler function
156
157 public:
158 // TYPES
159
160 /// `PreconditionHandlerType` is an alias for a pointer to a function
161 /// returning `void` and taking no parameters.
162 typedef void (*PreconditionHandlerType)();
163
164 // CLASS METHODS
165
166 /// Return the previously installed `s_beginHandler`.
167 static
169
170 /// Return the previously installed `s_endHandler`.
171 static
173
174 /// Store the specified `beginHandler` and `endHandler` function
175 /// pointers to the `static` member variables.
176 static void installHandlers(PreconditionHandlerType beginHandler,
177 PreconditionHandlerType endHandler);
178
179 /// Invoke the previously installed `s_beginHandler` function.
180 static void invokeBeginHandler();
181
182 /// Invoke the previously installed `s_endHandler` function.
183 static void invokeEndHandler();
184
185 /// Do nothing.
186 static void noOpHandler();
187};
188
189} // close package namespace
190
191
192#endif
193
194// ----------------------------------------------------------------------------
195// Copyright 2021 Bloomberg Finance L.P.
196//
197// Licensed under the Apache License, Version 2.0 (the "License");
198// you may not use this file except in compliance with the License.
199// You may obtain a copy of the License at
200//
201// http://www.apache.org/licenses/LICENSE-2.0
202//
203// Unless required by applicable law or agreed to in writing, software
204// distributed under the License is distributed on an "AS IS" BASIS,
205// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
206// See the License for the specific language governing permissions and
207// limitations under the License.
208// ----------------------------- END-OF-FILE ----------------------------------
209
210/** @} */
211/** @} */
212/** @} */
Definition bsls_preconditions.h:148
static void invokeEndHandler()
Invoke the previously installed s_endHandler function.
static void invokeBeginHandler()
Invoke the previously installed s_beginHandler function.
void(* PreconditionHandlerType)()
Definition bsls_preconditions.h:162
static void installHandlers(PreconditionHandlerType beginHandler, PreconditionHandlerType endHandler)
static void noOpHandler()
Do nothing.
static PreconditionHandlerType getBeginHandler()
Return the previously installed s_beginHandler.
static PreconditionHandlerType getEndHandler()
Return the previously installed s_endHandler.
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlt_iso8601util.h:691