|
| 1 | +/*============================================================================ |
| 2 | +* Design By Contract (DBC) for embedded C and C++ |
| 3 | +* GitHub: https://github.com/QuantumLeaps/DBC-for-embedded-C |
| 4 | +* |
| 5 | +* Q u a n t u m L e a P s |
| 6 | +* ------------------------ |
| 7 | +* Modern Embedded Software |
| 8 | +* |
| 9 | +* Copyright (C) 2005 Quantum Leaps, <state-machine.com>. |
| 10 | +* |
| 11 | +* SPDX-License-Identifier: MIT |
| 12 | +* |
| 13 | +* Permission is hereby granted, free of charge, to any person obtaining a |
| 14 | +* copy of this software and associated documentation files (the "Software"), |
| 15 | +* to deal in the Software without restriction, including without limitation |
| 16 | +* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 17 | +* and/or sell copies of the Software, and to permit persons to whom the |
| 18 | +* Software is furnished to do so, subject to the following conditions: |
| 19 | +* |
| 20 | +* The above copyright notice and this permission notice shall be included in |
| 21 | +* all copies or substantial portions of the Software. |
| 22 | +* |
| 23 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 26 | +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 28 | +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 29 | +* DEALINGS IN THE SOFTWARE. |
| 30 | +============================================================================*/ |
| 31 | +#ifndef DBC_ASSERT_H_ |
| 32 | +#define DBC_ASSERT_H_ |
| 33 | + |
| 34 | +/*! @file |
| 35 | +* @brief Memory-efficient Design by Contract (DBC) for embedded C and C++. |
| 36 | +* |
| 37 | +* @note |
| 38 | +* The runtime checking of the DBC assertions can be disabled by defining |
| 39 | +* the macro #DBC_DISABLE. However, it is generally **not** advisable to |
| 40 | +* disable assertions, *especially* in the production code. Instead, the |
| 41 | +* assertion fault handler DBC_fault_handler() should be very carefully |
| 42 | +* designed and tested under all fault conditions. |
| 43 | +*/ |
| 44 | + |
| 45 | +/* Active DbC macros -------------------------------------------------------*/ |
| 46 | +#ifndef DBC_DISABLE |
| 47 | + |
| 48 | +/*! Define the user-specified module name for assertions in this file. |
| 49 | +* |
| 50 | +* @details |
| 51 | +* Macro to be placed at the top of each C/C++ module to define the |
| 52 | +* single instance of the module name string to be used in reporting |
| 53 | +* assertions in this module. This macro takes the user-supplied parameter |
| 54 | +* `name_`. |
| 55 | +* |
| 56 | +* @param[in] name_ string constant representing the module name |
| 57 | +* |
| 58 | +* @note |
| 59 | +* This macro should **not** be terminated by a semicolon. |
| 60 | +*/ |
| 61 | +#define DBC_MODULE_NAME(name_) \ |
| 62 | + static char const DBC_module_name_[] = name_; |
| 63 | + |
| 64 | +/*! General purpose assertion with user-specified ID number. |
| 65 | +* |
| 66 | +* @details |
| 67 | +* Makes sure the `expr_` parameter is TRUE. Calls the DBC_fault_handler() |
| 68 | +* callback if the `expr_` evaluates to FALSE. This assertion takes the |
| 69 | +* user-supplied parameter `label_` to identify the location of this |
| 70 | +* assertion within the module. This avoids the volatility of using line |
| 71 | +* numbers, which change whenever a line of code is added or removed |
| 72 | +* upstream from the assertion. |
| 73 | +* |
| 74 | +* @param[in] label_ numeric label of the assertion (unique within the module) |
| 75 | +* @param[in] expr_ Boolean expression to check |
| 76 | +* |
| 77 | +* @note |
| 78 | +* The `expr_` expression is **not** evaluated if assertions are |
| 79 | +* disabled with the ::DBC_DISABLE switch. |
| 80 | +*/ |
| 81 | +#define DBC_ASSERT(label_, expr_) ((expr_) \ |
| 82 | + ? ((void)0) : DBC_fault_handler(&DBC_module_name_[0], (label_))) |
| 83 | + |
| 84 | +/*! General purpose assertion with user-specified ID number that |
| 85 | +* evaluates the `expr_` expression even when assertions are disabled. |
| 86 | +* |
| 87 | +* @details |
| 88 | +* Like the DBC_ASSERT() macro, except it **always** evaluates the |
| 89 | +* `expr_` expression even when DBC assertions are disabled with the |
| 90 | +* #DBC_DISABLE macro. |
| 91 | +* |
| 92 | +* @param[in] label_ numeric label of the assertion (unique within the module) |
| 93 | +* @param[in] expr_ Boolean expression to check |
| 94 | +*/ |
| 95 | +#define DBC_ALLEGE(label_, expr_) DBC_ASSERT(label_, expr_) |
| 96 | + |
| 97 | +/*! Assertion for a wrong path through the code |
| 98 | +* |
| 99 | +* @details |
| 100 | +* Calls the DBC_fault_handler() callback if ever executed. This assertion |
| 101 | +* takes the user-supplied parameter `id_` to identify the location of |
| 102 | +* this assertion within the file. This avoids the volatility of using |
| 103 | +* line numbers, which change whenever a line of code is added or removed |
| 104 | +* upstream from the assertion. |
| 105 | +* |
| 106 | +* @param[in] label_ numeric label of the assertion (unique within the module) |
| 107 | +*/ |
| 108 | +#define DBC_ERROR(label_) DBC_fault_handler(&DBC_module_name_[0], (label_)) |
| 109 | + |
| 110 | +/*! Assertion for checking preconditions. |
| 111 | +* |
| 112 | +* @details |
| 113 | +* Equivalent to DBC_ASSERT(), except the name provides a better |
| 114 | +* documentation of the intention of this assertion. |
| 115 | +* |
| 116 | +* @param[in] label_ numeric label of the assertion (unique within the module) |
| 117 | +* @param[in] expr_ Boolean expression to check |
| 118 | +* |
| 119 | +* @note |
| 120 | +* The `expr_` expression is **not** evaluated if assertions are |
| 121 | +* disabled with the ::DBC_DISABLE switch. |
| 122 | +*/ |
| 123 | +#define DBC_REQUIRE(label_, expr_) DBC_ASSERT((label_), (expr_)) |
| 124 | + |
| 125 | +/*! Assertion for checking postconditions. |
| 126 | +* |
| 127 | +* @details |
| 128 | +* Equivalent to DBC_ASSERT(), except the name provides a better |
| 129 | +* documentation of the intention of this assertion. |
| 130 | +* |
| 131 | +* @param[in] label_ numeric label of the assertion (unique within the module) |
| 132 | +* @param[in] expr_ Boolean expression to check |
| 133 | +* |
| 134 | +* @note |
| 135 | +* The `expr_` expression is **not** evaluated if assertions are |
| 136 | +* disabled with the ::DBC_DISABLE switch. |
| 137 | +*/ |
| 138 | +#define DBC_ENSURE(label_, expr_) DBC_ASSERT((label_), (expr_)) |
| 139 | + |
| 140 | +/*! Assertion for checking invariants. |
| 141 | +* |
| 142 | +* @details |
| 143 | +* Equivalent to DBC_ASSERT(), except the name provides a better |
| 144 | +* documentation of the intention of this assertion. |
| 145 | +* |
| 146 | +* @param[in] label_ numeric label of the assertion (unique within the module) |
| 147 | +* @param[in] expr_ Boolean expression to check |
| 148 | +* |
| 149 | +* @note |
| 150 | +* The `expr_` expression is **not** evaluated if assertions are |
| 151 | +* disabled with the ::DBC_DISABLE switch. |
| 152 | +*/ |
| 153 | +#define DBC_INVARIANT(label_, expr_) DBC_ASSERT((label_), (expr_)) |
| 154 | + |
| 155 | +#ifndef DBC_NORETURN |
| 156 | +#define DBC_NORETURN |
| 157 | +#endif |
| 158 | + |
| 159 | +#ifdef __cplusplus |
| 160 | +extern "C" { |
| 161 | +#endif |
| 162 | + |
| 163 | +/*! DBC assertion fault handler. |
| 164 | +* |
| 165 | +* @details |
| 166 | +* This is an application-specific callback function needs to be defined in |
| 167 | +* the application to perform the clean system shutdown and perhaps a reset. |
| 168 | +* The DBC_fault_handler() function is the last line of defense after the |
| 169 | +* system failure and its implementation should be very **carefully** |
| 170 | +* designed and **tested** under various fault conditions, including but |
| 171 | +* not limited to: stack overflow, stack corruption, or calling |
| 172 | +* DBC_fault_handler() from ISRs. |
| 173 | +
|
| 174 | +* @param[in] module name of the file/module in which the assertion failed |
| 175 | +* (constant, zero-terminated C string) |
| 176 | +* @param[in] label unique label of the assertion within the module. |
| 177 | +* This could be a line number or a user-defined label. |
| 178 | +* |
| 179 | +* @returns |
| 180 | +* This callback function should **not return** (see #NORETURN), |
| 181 | +* as continuation after an assertion failure does not make sense. |
| 182 | +* |
| 183 | +* @note |
| 184 | +* It is typically a **bad idea** to implement DBC_fault_handler() as an |
| 185 | +* endless loop that ties up the CPU. During debugging, DBC_fault_handler() |
| 186 | +* is an ideal place to put a breakpoint. |
| 187 | +*/ |
| 188 | +DBC_NORETURN void DBC_fault_handler(char const * module, int label); |
| 189 | + |
| 190 | +#ifdef __cplusplus |
| 191 | +} |
| 192 | +#endif |
| 193 | + |
| 194 | +/* Inactive DbC macros -----------------------------------------------------*/ |
| 195 | +#else |
| 196 | + |
| 197 | +#define DBC_MODULE_NAME(dummy_) |
| 198 | +#define DBC_ASSERT(label_, expr_) ((void)0) |
| 199 | +#define DBC_ERROR(label_) ((void)0) |
| 200 | +#define DBC_REQUIRE(label_, expr_) ((void)0) |
| 201 | +#define DBC_ENSURE(label_, expr_) ((void)0) |
| 202 | +#define DBC_INVARIANT(label_, expr_) ((void)0) |
| 203 | +#define DBC_ALLEGE(label_, expr_) ((void)(expr_)) |
| 204 | + |
| 205 | +#endif /* Inactive DBC macros */ |
| 206 | + |
| 207 | +#endif /* DBC_ASSERT_ */ |
0 commit comments