1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15
16 #ifdef __cplusplus
17 #include <type_traits>
18 #else
19 #include <stddef.h>
20 #endif // __cplusplus
21
22 // Note: This file depends on the backend header already being included.
23
24 #include "pw_assert/config.h"
25 #include "pw_preprocessor/compiler.h"
26
27 // PW_CRASH - Crash the system, with a message.
28 #define PW_CRASH PW_HANDLE_CRASH
29
30 // PW_CHECK - If condition evaluates to false, crash. Message optional.
31 #define PW_CHECK(condition, ...) \
32 do { \
33 if (!(condition)) { \
34 _pw_assert_ConditionCannotContainThePercentCharacter( \
35 #condition); /* cannot use '%' in PW_CHECK conditions */ \
36 PW_HANDLE_ASSERT_FAILURE(#condition, "" __VA_ARGS__); \
37 } \
38 } while (0)
39
40 #define PW_DCHECK(...) \
41 do { \
42 if (PW_ASSERT_ENABLE_DEBUG) { \
43 PW_CHECK(__VA_ARGS__); \
44 } \
45 } while (0)
46
47 // PW_D?CHECK_<type>_<comparison> macros - Binary comparison asserts.
48 //
49 // The below blocks are structured in table form, violating the 80-column
50 // Pigweed style, in order to make it clearer what is common and what isn't
51 // between the multitude of assert macro instantiations. To best view this
52 // section, turn off editor wrapping or make your editor wide.
53 //
54 // clang-format off
55
56 // Checks for int: LE, LT, GE, GT, EQ.
57 #define PW_CHECK_INT_LE(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, <=, argb, int, "%d", __VA_ARGS__)
58 #define PW_CHECK_INT_LT(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, < , argb, int, "%d", __VA_ARGS__)
59 #define PW_CHECK_INT_GE(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, >=, argb, int, "%d", __VA_ARGS__)
60 #define PW_CHECK_INT_GT(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, > , argb, int, "%d", __VA_ARGS__)
61 #define PW_CHECK_INT_EQ(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, ==, argb, int, "%d", __VA_ARGS__)
62 #define PW_CHECK_INT_NE(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, !=, argb, int, "%d", __VA_ARGS__)
63
64 // Debug checks for int: LE, LT, GE, GT, EQ.
65 #define PW_DCHECK_INT_LE(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_INT_LE(__VA_ARGS__)
66 #define PW_DCHECK_INT_LT(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_INT_LT(__VA_ARGS__)
67 #define PW_DCHECK_INT_GE(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_INT_GE(__VA_ARGS__)
68 #define PW_DCHECK_INT_GT(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_INT_GT(__VA_ARGS__)
69 #define PW_DCHECK_INT_EQ(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_INT_EQ(__VA_ARGS__)
70 #define PW_DCHECK_INT_NE(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_INT_NE(__VA_ARGS__)
71
72 // Checks for unsigned int: LE, LT, GE, GT, EQ.
73 #define PW_CHECK_UINT_LE(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, <=, argb, unsigned int, "%u", __VA_ARGS__)
74 #define PW_CHECK_UINT_LT(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, < , argb, unsigned int, "%u", __VA_ARGS__)
75 #define PW_CHECK_UINT_GE(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, >=, argb, unsigned int, "%u", __VA_ARGS__)
76 #define PW_CHECK_UINT_GT(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, > , argb, unsigned int, "%u", __VA_ARGS__)
77 #define PW_CHECK_UINT_EQ(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, ==, argb, unsigned int, "%u", __VA_ARGS__)
78 #define PW_CHECK_UINT_NE(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, !=, argb, unsigned int, "%u", __VA_ARGS__)
79
80 // Debug checks for unsigned int: LE, LT, GE, GT, EQ.
81 #define PW_DCHECK_UINT_LE(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_UINT_LE(__VA_ARGS__)
82 #define PW_DCHECK_UINT_LT(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_UINT_LT(__VA_ARGS__)
83 #define PW_DCHECK_UINT_GE(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_UINT_GE(__VA_ARGS__)
84 #define PW_DCHECK_UINT_GT(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_UINT_GT(__VA_ARGS__)
85 #define PW_DCHECK_UINT_EQ(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_UINT_EQ(__VA_ARGS__)
86 #define PW_DCHECK_UINT_NE(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_UINT_NE(__VA_ARGS__)
87
88 // Checks for pointer: LE, LT, GE, GT, EQ, NE.
89 #define PW_CHECK_PTR_LE(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, <=, argb, const void*, "%p", __VA_ARGS__)
90 #define PW_CHECK_PTR_LT(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, < , argb, const void*, "%p", __VA_ARGS__)
91 #define PW_CHECK_PTR_GE(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, >=, argb, const void*, "%p", __VA_ARGS__)
92 #define PW_CHECK_PTR_GT(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, > , argb, const void*, "%p", __VA_ARGS__)
93 #define PW_CHECK_PTR_EQ(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, ==, argb, const void*, "%p", __VA_ARGS__)
94 #define PW_CHECK_PTR_NE(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, !=, argb, const void*, "%p", __VA_ARGS__)
95
96 // Check for pointer: NOTNULL. Use "nullptr" in C++, "NULL" in C.
97 #ifdef __cplusplus
98 #define PW_CHECK_NOTNULL(arga, ...) \
99 _PW_CHECK_BINARY_CMP_IMPL(arga, !=, nullptr, const void*, "%p", __VA_ARGS__)
100 #else // __cplusplus
101 #define PW_CHECK_NOTNULL(arga, ...) \
102 _PW_CHECK_BINARY_CMP_IMPL(arga, !=, NULL, const void*, "%p", __VA_ARGS__)
103 #endif // __cplusplus
104
105 // Debug checks for pointer: LE, LT, GE, GT, EQ, NE, and NOTNULL.
106 #define PW_DCHECK_PTR_LE(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_PTR_LE(__VA_ARGS__)
107 #define PW_DCHECK_PTR_LT(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_PTR_LT(__VA_ARGS__)
108 #define PW_DCHECK_PTR_GE(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_PTR_GE(__VA_ARGS__)
109 #define PW_DCHECK_PTR_GT(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_PTR_GT(__VA_ARGS__)
110 #define PW_DCHECK_PTR_EQ(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_PTR_EQ(__VA_ARGS__)
111 #define PW_DCHECK_PTR_NE(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_PTR_NE(__VA_ARGS__)
112 #define PW_DCHECK_NOTNULL(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_NOTNULL(__VA_ARGS__)
113
114 // Checks for float: EXACT_LE, EXACT_LT, EXACT_GE, EXACT_GT, EXACT_EQ, EXACT_NE,
115 // NEAR.
116 #define PW_CHECK_FLOAT_NEAR(arga, argb, abs_tolerance, ...) \
117 _PW_CHECK_FLOAT_NEAR(arga, argb, abs_tolerance, __VA_ARGS__)
118 #define PW_CHECK_FLOAT_EXACT_LE(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, <=, argb, float, "%f", __VA_ARGS__)
119 #define PW_CHECK_FLOAT_EXACT_LT(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, < , argb, float, "%f", __VA_ARGS__)
120 #define PW_CHECK_FLOAT_EXACT_GE(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, >=, argb, float, "%f", __VA_ARGS__)
121 #define PW_CHECK_FLOAT_EXACT_GT(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, > , argb, float, "%f", __VA_ARGS__)
122 #define PW_CHECK_FLOAT_EXACT_EQ(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, ==, argb, float, "%f", __VA_ARGS__)
123 #define PW_CHECK_FLOAT_EXACT_NE(arga, argb, ...) _PW_CHECK_BINARY_CMP_IMPL(arga, !=, argb, float, "%f", __VA_ARGS__)
124
125 // Debug checks for float: NEAR, EXACT_LE, EXACT_LT, EXACT_GE, EXACT_GT,
126 // EXACT_EQ.
127 #define PW_DCHECK_FLOAT_NEAR(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_NEAR(__VA_ARGS__)
128 #define PW_DCHECK_FLOAT_EXACT_LE(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_EXACT_LE(__VA_ARGS__)
129 #define PW_DCHECK_FLOAT_EXACT_LT(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_EXACT_LT(__VA_ARGS__)
130 #define PW_DCHECK_FLOAT_EXACT_GE(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_EXACT_GE(__VA_ARGS__)
131 #define PW_DCHECK_FLOAT_EXACT_GT(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_EXACT_GT(__VA_ARGS__)
132 #define PW_DCHECK_FLOAT_EXACT_EQ(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_EXACT_EQ(__VA_ARGS__)
133 #define PW_DCHECK_FLOAT_EXACT_NE(...) if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_EXACT_NE(__VA_ARGS__)
134
135 // Debug checks for integer overflows: ADD, SUB, MUL.
136 #define PW_CHECK_ADD(a, b, out, ...) PW_CHECK(!PW_ADD_OVERFLOW(a, b, out), __VA_ARGS__)
137 #define PW_CHECK_SUB(a, b, out, ...) PW_CHECK(!PW_SUB_OVERFLOW(a, b, out), __VA_ARGS__)
138 #define PW_CHECK_MUL(a, b, out, ...) PW_CHECK(!PW_MUL_OVERFLOW(a, b, out), __VA_ARGS__)
139
140 // Debug checks for integer overflows: ADD, SUB, MUL.
141 #define PW_DCHECK_ADD(a, b, out, ...) PW_DCHECK(!PW_ADD_OVERFLOW(a, b, out), __VA_ARGS__)
142 #define PW_DCHECK_SUB(a, b, out, ...) PW_DCHECK(!PW_SUB_OVERFLOW(a, b, out), __VA_ARGS__)
143 #define PW_DCHECK_MUL(a, b, out, ...) PW_DCHECK(!PW_MUL_OVERFLOW(a, b, out), __VA_ARGS__)
144
145 // clang-format on
146
147 // PW_CHECK_OK - If condition does not evaluate to PW_STATUS_OK, crash. Message
148 // optional.
149 #define PW_CHECK_OK(expression, ...) \
150 do { \
151 const _PW_CHECK_OK_STATUS _pw_assert_check_ok_status = (expression); \
152 if (_pw_assert_check_ok_status != PW_STATUS_OK) { \
153 _PW_CHECK_BINARY_ARG_HANDLER( \
154 #expression, \
155 pw_StatusString(_pw_assert_check_ok_status), \
156 "==", \
157 "OkStatus()", \
158 "OK", \
159 "%s", \
160 "" __VA_ARGS__); \
161 } \
162 } while (0)
163
164 #ifdef __cplusplus
165 #define _PW_CHECK_OK_STATUS ::pw::Status
166 #else
167 #define _PW_CHECK_OK_STATUS pw_Status
168 #endif // __cplusplus
169
170 #define PW_DCHECK_OK(...) \
171 if (!(PW_ASSERT_ENABLE_DEBUG)) { \
172 } else \
173 PW_CHECK_OK(__VA_ARGS__)
174
175 // Use a static_cast in C++ to avoid accidental comparisons between e.g. an
176 // integer and the CHECK message const char*.
177 #if defined(__cplusplus) && __cplusplus >= 201703L
178
179 namespace pw::assert::internal {
180
181 template <typename T, typename U>
ConvertToType(U * value)182 constexpr const void* ConvertToType(U* value) {
183 if constexpr (std::is_function<U>()) {
184 return reinterpret_cast<const void*>(value);
185 } else {
186 return static_cast<const void*>(value);
187 }
188 }
189
190 template <typename T, typename U>
ConvertToType(const U & value)191 constexpr T ConvertToType(const U& value) {
192 return static_cast<T>(value);
193 }
194
195 } // namespace pw::assert::internal
196
197 #define _PW_CHECK_CONVERT(type, name, arg) \
198 type name = ::pw::assert::internal::ConvertToType<type>(arg)
199 #else
200 #define _PW_CHECK_CONVERT(type, name, arg) type name = (type)(arg)
201 #endif // __cplusplus
202
203 // For the binary assertions, this private macro is re-used for almost all of
204 // the variants. Due to limitations of C formatting, it is necessary to have
205 // separate macros for the types.
206 //
207 // The macro avoids evaluating the arguments multiple times at the cost of some
208 // macro complexity.
209 #define _PW_CHECK_BINARY_CMP_IMPL( \
210 arg_a, comparison_op, arg_b, type_decl, type_fmt, ...) \
211 do { \
212 _PW_CHECK_CONVERT(type_decl, evaluated_argument_a, arg_a); \
213 _PW_CHECK_CONVERT(type_decl, evaluated_argument_b, arg_b); \
214 if (!(evaluated_argument_a comparison_op evaluated_argument_b)) { \
215 _PW_CHECK_BINARY_ARG_HANDLER(#arg_a, \
216 evaluated_argument_a, \
217 #comparison_op, \
218 #arg_b, \
219 evaluated_argument_b, \
220 type_fmt, \
221 "" __VA_ARGS__); \
222 } \
223 } while (0)
224
225 // All binary comparison CHECK macros are directed to this handler before
226 // hitting the CHECK backend. This controls whether evaluated values are
227 // captured.
228 #if PW_ASSERT_CAPTURE_VALUES
229 #define _PW_CHECK_BINARY_ARG_HANDLER(arg_a_str, \
230 arg_a_val, \
231 comparison_op_str, \
232 arg_b_str, \
233 arg_b_val, \
234 type_fmt, \
235 ...) \
236 \
237 _pw_assert_ConditionCannotContainThePercentCharacter( \
238 arg_a_str arg_b_str); /* cannot use '%' in PW_CHECK conditions */ \
239 PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE(arg_a_str, \
240 arg_a_val, \
241 comparison_op_str, \
242 arg_b_str, \
243 arg_b_val, \
244 type_fmt, \
245 __VA_ARGS__)
246 #else
247 #define _PW_CHECK_BINARY_ARG_HANDLER(arg_a_str, \
248 arg_a_val, \
249 comparison_op_str, \
250 arg_b_str, \
251 arg_b_val, \
252 type_fmt, \
253 ...) \
254 _pw_assert_ConditionCannotContainThePercentCharacter( \
255 arg_a_str arg_b_str); /* cannot use '%' in PW_CHECK conditions */ \
256 PW_HANDLE_ASSERT_FAILURE(arg_a_str " " comparison_op_str " " arg_b_str, \
257 __VA_ARGS__)
258 #endif // PW_ASSERT_CAPTURE_VALUES
259
260 // Custom implementation for FLOAT_NEAR which is implemented through two
261 // underlying checks which are not trivially replaced through the use of
262 // FLOAT_EXACT_LE & FLOAT_EXACT_GE.
263 #define _PW_CHECK_FLOAT_NEAR(argument_a, argument_b, abs_tolerance, ...) \
264 do { \
265 PW_CHECK_FLOAT_EXACT_GE(abs_tolerance, 0.0f); \
266 float evaluated_argument_a = (float)(argument_a); \
267 float evaluated_argument_b_min = (float)(argument_b) - abs_tolerance; \
268 float evaluated_argument_b_max = (float)(argument_b) + abs_tolerance; \
269 if (!(evaluated_argument_a >= evaluated_argument_b_min)) { \
270 _PW_CHECK_BINARY_ARG_HANDLER(#argument_a, \
271 evaluated_argument_a, \
272 ">=", \
273 #argument_b " - abs_tolerance", \
274 evaluated_argument_b_min, \
275 "%f", \
276 "" __VA_ARGS__); \
277 } else if (!(evaluated_argument_a <= evaluated_argument_b_max)) { \
278 _PW_CHECK_BINARY_ARG_HANDLER(#argument_a, \
279 evaluated_argument_a, \
280 "<=", \
281 #argument_b " + abs_tolerance", \
282 evaluated_argument_b_max, \
283 "%f", \
284 "" __VA_ARGS__); \
285 } \
286 } while (0)
287
288 // This empty function allows the compiler to verify that the condition contains
289 // no % characters (modulus operator). Backends (pw_assert-tokenized in
290 // particular) may include the condition in the format string as a size
291 // optimization. Unintentionally introducing an extra argument could lead to
292 // problems. Checking the condition here ensures that the behavior is consistent
293 // for all backends.
294 //
295 // TODO: b/235149326 - Remove this restriction when pw_assert macros no longer
296 // accept arbitrary arguments.
297 static inline void _pw_assert_ConditionCannotContainThePercentCharacter(
298 const char* format, ...) PW_PRINTF_FORMAT(1, 2);
299
_pw_assert_ConditionCannotContainThePercentCharacter(const char * format,...)300 static inline void _pw_assert_ConditionCannotContainThePercentCharacter(
301 const char* format, ...) {
302 (void)format;
303 }
304