1 | /* $Id: kHlpAssert.h 118 2020-12-23 19:51:15Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kHlpAssert - Assertion Macros.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #ifndef ___kHlpAssert_h___
|
---|
32 | #define ___kHlpAssert_h___
|
---|
33 |
|
---|
34 | #include <k/kHlpDefs.h>
|
---|
35 |
|
---|
36 | #ifdef __cplusplus
|
---|
37 | extern "C" {
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | /** @defgroup grp_kHlpAssert - Assertion Macros
|
---|
41 | * @addtogroup grp_kHlp
|
---|
42 | * @{ */
|
---|
43 |
|
---|
44 | /** @def K_STRICT
|
---|
45 | * Assertions are enabled when K_STRICT is \#defined. */
|
---|
46 |
|
---|
47 | /** @def kHlpAssertBreakpoint
|
---|
48 | * Emits a breakpoint instruction or somehow triggers a debugger breakpoint.
|
---|
49 | */
|
---|
50 | #ifdef _MSC_VER
|
---|
51 | # define kHlpAssertBreakpoint() do { __debugbreak(); } while (0)
|
---|
52 | #elif defined(__GNUC__) && K_OS == K_OS_SOLARIS && (K_ARCH == K_ARCH_AMD64 || K_ARCH == K_ARCH_X86_32)
|
---|
53 | # define kHlpAssertBreakpoint() do { __asm__ __volatile__ ("int $3"); } while (0)
|
---|
54 | #elif defined(__GNUC__) && (K_ARCH == K_ARCH_AMD64 || K_ARCH == K_ARCH_X86_32 || K_ARCH == K_ARCH_X86_16)
|
---|
55 | # define kHlpAssertBreakpoint() do { __asm__ __volatile__ ("int3"); } while (0)
|
---|
56 | #elif defined(__GNUC__) && (K_ARCH == K_ARCH_ARM_64 || K_ARCH == K_ARCH_ARM_32) /* probably not supported by older ARM CPUs */
|
---|
57 | # define kHlpAssertBreakpoint() do { __asm__ __volatile__ ("brk #0x1"); } while (0)
|
---|
58 | #else
|
---|
59 | # error "Port Me"
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | /** @def K_FUNCTION
|
---|
63 | * Undecorated function name macro expanded by the compiler.
|
---|
64 | */
|
---|
65 | #if defined(__GNUC__)
|
---|
66 | # define K_FUNCTION __func__
|
---|
67 | #else
|
---|
68 | # define K_FUNCTION __FUNCTION__
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | #ifdef K_STRICT
|
---|
72 |
|
---|
73 | # define kHlpAssert(expr) \
|
---|
74 | do { \
|
---|
75 | if (!(expr)) \
|
---|
76 | { \
|
---|
77 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
|
---|
78 | kHlpAssertBreakpoint(); \
|
---|
79 | } \
|
---|
80 | } while (0)
|
---|
81 |
|
---|
82 | # define kHlpAssertStmt(expr, stmt) \
|
---|
83 | do { \
|
---|
84 | if (!(expr)) \
|
---|
85 | { \
|
---|
86 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
|
---|
87 | kHlpAssertBreakpoint(); \
|
---|
88 | stmt; \
|
---|
89 | } \
|
---|
90 | } while (0)
|
---|
91 |
|
---|
92 | # define kHlpAssertReturn(expr, rcRet) \
|
---|
93 | do { \
|
---|
94 | if (!(expr)) \
|
---|
95 | { \
|
---|
96 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
|
---|
97 | kHlpAssertBreakpoint(); \
|
---|
98 | return (rcRet); \
|
---|
99 | } \
|
---|
100 | } while (0)
|
---|
101 |
|
---|
102 | # define kHlpAssertStmtReturn(expr, stmt, rcRet) \
|
---|
103 | do { \
|
---|
104 | if (!(expr)) \
|
---|
105 | { \
|
---|
106 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
|
---|
107 | kHlpAssertBreakpoint(); \
|
---|
108 | stmt; \
|
---|
109 | return (rcRet); \
|
---|
110 | } \
|
---|
111 | } while (0)
|
---|
112 |
|
---|
113 | # define kHlpAssertReturnVoid(expr) \
|
---|
114 | do { \
|
---|
115 | if (!(expr)) \
|
---|
116 | { \
|
---|
117 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
|
---|
118 | kHlpAssertBreakpoint(); \
|
---|
119 | return; \
|
---|
120 | } \
|
---|
121 | } while (0)
|
---|
122 |
|
---|
123 | # define kHlpAssertStmtReturnVoid(expr, stmt) \
|
---|
124 | do { \
|
---|
125 | if (!(expr)) \
|
---|
126 | { \
|
---|
127 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
|
---|
128 | kHlpAssertBreakpoint(); \
|
---|
129 | stmt; \
|
---|
130 | return; \
|
---|
131 | } \
|
---|
132 | } while (0)
|
---|
133 |
|
---|
134 | # define kHlpAssertMsg(expr, msg) \
|
---|
135 | do { \
|
---|
136 | if (!(expr)) \
|
---|
137 | { \
|
---|
138 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
|
---|
139 | kHlpAssertMsg2 msg; \
|
---|
140 | kHlpAssertBreakpoint(); \
|
---|
141 | } \
|
---|
142 | } while (0)
|
---|
143 |
|
---|
144 | # define kHlpAssertMsgStmt(expr, msg, stmt) \
|
---|
145 | do { \
|
---|
146 | if (!(expr)) \
|
---|
147 | { \
|
---|
148 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
|
---|
149 | kHlpAssertMsg2 msg; \
|
---|
150 | kHlpAssertBreakpoint(); \
|
---|
151 | stmt; \
|
---|
152 | } \
|
---|
153 | } while (0)
|
---|
154 |
|
---|
155 | # define kHlpAssertMsgReturn(expr, msg, rcRet) \
|
---|
156 | do { \
|
---|
157 | if (!(expr)) \
|
---|
158 | { \
|
---|
159 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
|
---|
160 | kHlpAssertMsg2 msg; \
|
---|
161 | kHlpAssertBreakpoint(); \
|
---|
162 | return (rcRet); \
|
---|
163 | } \
|
---|
164 | } while (0)
|
---|
165 |
|
---|
166 | # define kHlpAssertMsgStmtReturn(expr, msg, stmt, rcRet) \
|
---|
167 | do { \
|
---|
168 | if (!(expr)) \
|
---|
169 | { \
|
---|
170 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
|
---|
171 | kHlpAssertMsg2 msg; \
|
---|
172 | kHlpAssertBreakpoint(); \
|
---|
173 | stmt; \
|
---|
174 | return (rcRet); \
|
---|
175 | } \
|
---|
176 | } while (0)
|
---|
177 |
|
---|
178 | # define kHlpAssertMsgReturnVoid(expr, msg) \
|
---|
179 | do { \
|
---|
180 | if (!(expr)) \
|
---|
181 | { \
|
---|
182 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
|
---|
183 | kHlpAssertMsg2 msg; \
|
---|
184 | kHlpAssertBreakpoint(); \
|
---|
185 | return; \
|
---|
186 | } \
|
---|
187 | } while (0)
|
---|
188 |
|
---|
189 | # define kHlpAssertMsgStmtReturnVoid(expr, msg, stmt) \
|
---|
190 | do { \
|
---|
191 | if (!(expr)) \
|
---|
192 | { \
|
---|
193 | kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
|
---|
194 | kHlpAssertMsg2 msg; \
|
---|
195 | kHlpAssertBreakpoint(); \
|
---|
196 | stmt; \
|
---|
197 | return; \
|
---|
198 | } \
|
---|
199 | } while (0)
|
---|
200 |
|
---|
201 | /* Same as above, only no expression. */
|
---|
202 |
|
---|
203 | # define kHlpAssertFailed() \
|
---|
204 | do { \
|
---|
205 | kHlpAssertMsg1("failed", __FILE__, __LINE__, K_FUNCTION); \
|
---|
206 | kHlpAssertBreakpoint(); \
|
---|
207 | } while (0)
|
---|
208 |
|
---|
209 | # define kHlpAssertFailedStmt(stmt) \
|
---|
210 | do { \
|
---|
211 | kHlpAssertMsg1("failed", __FILE__, __LINE__, K_FUNCTION); \
|
---|
212 | kHlpAssertBreakpoint(); \
|
---|
213 | stmt; \
|
---|
214 | } while (0)
|
---|
215 |
|
---|
216 | # define kHlpAssertFailedReturn(rcRet) \
|
---|
217 | do { \
|
---|
218 | kHlpAssertMsg1("failed", __FILE__, __LINE__, K_FUNCTION); \
|
---|
219 | kHlpAssertBreakpoint(); \
|
---|
220 | return (rcRet); \
|
---|
221 | } while (0)
|
---|
222 |
|
---|
223 | # define kHlpAssertFailedStmtReturn(stmt, rcRet) \
|
---|
224 | do { \
|
---|
225 | kHlpAssertMsg1("failed", __FILE__, __LINE__, K_FUNCTION); \
|
---|
226 | kHlpAssertBreakpoint(); \
|
---|
227 | stmt; \
|
---|
228 | return (rcRet); \
|
---|
229 | } while (0)
|
---|
230 |
|
---|
231 | # define kHlpAssertFailedReturnVoid() \
|
---|
232 | do { \
|
---|
233 | kHlpAssertMsg1("failed", __FILE__, __LINE__, K_FUNCTION); \
|
---|
234 | kHlpAssertBreakpoint(); \
|
---|
235 | return; \
|
---|
236 | } while (0)
|
---|
237 |
|
---|
238 | # define kHlpAssertFailedStmtReturnVoid(stmt) \
|
---|
239 | do { \
|
---|
240 | kHlpAssertMsg1("failed", __FILE__, __LINE__, K_FUNCTION); \
|
---|
241 | kHlpAssertBreakpoint(); \
|
---|
242 | stmt; \
|
---|
243 | return; \
|
---|
244 | } while (0)
|
---|
245 |
|
---|
246 | # define kHlpAssertMsgFailed(msg) \
|
---|
247 | do { \
|
---|
248 | kHlpAssertMsg1("failed", __FILE__, __LINE__, K_FUNCTION); \
|
---|
249 | kHlpAssertMsg2 msg; \
|
---|
250 | kHlpAssertBreakpoint(); \
|
---|
251 | } while (0)
|
---|
252 |
|
---|
253 | # define kHlpAssertMsgFailedStmt(msg, stmt) \
|
---|
254 | do { \
|
---|
255 | kHlpAssertMsg1("failed", __FILE__, __LINE__, K_FUNCTION); \
|
---|
256 | kHlpAssertMsg2 msg; \
|
---|
257 | kHlpAssertBreakpoint(); \
|
---|
258 | stmt; \
|
---|
259 | } while (0)
|
---|
260 |
|
---|
261 | # define kHlpAssertMsgFailedReturn(msg, rcRet) \
|
---|
262 | do { \
|
---|
263 | kHlpAssertMsg1("failed", __FILE__, __LINE__, K_FUNCTION); \
|
---|
264 | kHlpAssertMsg2 msg; \
|
---|
265 | kHlpAssertBreakpoint(); \
|
---|
266 | return (rcRet); \
|
---|
267 | } while (0)
|
---|
268 |
|
---|
269 | # define kHlpAssertMsgFailedStmtReturn(msg, stmt, rcRet) \
|
---|
270 | do { \
|
---|
271 | kHlpAssertMsg1("failed", __FILE__, __LINE__, K_FUNCTION); \
|
---|
272 | kHlpAssertMsg2 msg; \
|
---|
273 | kHlpAssertBreakpoint(); \
|
---|
274 | stmt; \
|
---|
275 | return (rcRet); \
|
---|
276 | } while (0)
|
---|
277 |
|
---|
278 | # define kHlpAssertMsgFailedReturnVoid(msg) \
|
---|
279 | do { \
|
---|
280 | kHlpAssertMsg1("failed", __FILE__, __LINE__, K_FUNCTION); \
|
---|
281 | kHlpAssertMsg2 msg; \
|
---|
282 | kHlpAssertBreakpoint(); \
|
---|
283 | return; \
|
---|
284 | } while (0)
|
---|
285 |
|
---|
286 | # define kHlpAssertMsgFailedStmtReturnVoid(msg, stmt) \
|
---|
287 | do { \
|
---|
288 | kHlpAssertMsg1("failed", __FILE__, __LINE__, K_FUNCTION); \
|
---|
289 | kHlpAssertMsg2 msg; \
|
---|
290 | kHlpAssertBreakpoint(); \
|
---|
291 | stmt; \
|
---|
292 | return; \
|
---|
293 | } while (0)
|
---|
294 |
|
---|
295 |
|
---|
296 | #else /* !K_STRICT */
|
---|
297 |
|
---|
298 | # define kHlpAssert(expr) do { } while (0)
|
---|
299 | # define kHlpAssertStmt(expr, stmt) do { if (!(expr)) { stmt; } } while (0)
|
---|
300 | # define kHlpAssertReturn(expr, rcRet) do { if (!(expr)) return (rcRet); } while (0)
|
---|
301 | # define kHlpAssertStmtReturn(expr, stmt, rcRet) do { if (!(expr)) { stmt; return (rcRet); } } while (0)
|
---|
302 | # define kHlpAssertReturnVoid(expr) do { if (!(expr)) return; } while (0)
|
---|
303 | # define kHlpAssertStmtReturnVoid(expr, stmt) do { if (!(expr)) { stmt; return; } } while (0)
|
---|
304 | # define kHlpAssertMsg(expr, msg) do { } while (0)
|
---|
305 | # define kHlpAssertMsgStmt(expr, msg, stmt) do { if (!(expr)) { stmt; } } while (0)
|
---|
306 | # define kHlpAssertMsgReturn(expr, msg, rcRet) do { if (!(expr)) return (rcRet); } while (0)
|
---|
307 | # define kHlpAssertMsgStmtReturn(expr, msg, stmt, rcRet) do { if (!(expr)) { stmt; return (rcRet); } } while (0)
|
---|
308 | # define kHlpAssertMsgReturnVoid(expr, msg) do { if (!(expr)) return; } while (0)
|
---|
309 | # define kHlpAssertMsgStmtReturnVoid(expr, msg, stmt) do { if (!(expr)) { stmt; return; } } while (0)
|
---|
310 | /* Same as above, only no expression: */
|
---|
311 | # define kHlpAssertFailed() do { } while (0)
|
---|
312 | # define kHlpAssertFailedStmt(stmt) do { stmt; } while (0)
|
---|
313 | # define kHlpAssertFailedReturn(rcRet) do { return (rcRet); } while (0)
|
---|
314 | # define kHlpAssertFailedStmtReturn(stmt, rcRet) do { stmt; return (rcRet); } while (0)
|
---|
315 | # define kHlpAssertFailedReturnVoid() do { return; } while (0)
|
---|
316 | # define kHlpAssertFailedStmtReturnVoid(stmt) do { stmt; return; } while (0)
|
---|
317 | # define kHlpAssertMsgFailed(msg) do { } while (0)
|
---|
318 | # define kHlpAssertMsgFailedStmt(msg, stmt) do { stmt; } while (0)
|
---|
319 | # define kHlpAssertMsgFailedReturn(msg, rcRet) do { return (rcRet); } while (0)
|
---|
320 | # define kHlpAssertMsgFailedStmtReturn(msg, stmt, rcRet) do { { stmt; return (rcRet); } } while (0)
|
---|
321 | # define kHlpAssertMsgFailedReturnVoid(msg) do { return; } while (0)
|
---|
322 | # define kHlpAssertMsgFailedStmtReturnVoid(msg, stmt) do { stmt; return; } while (0)
|
---|
323 |
|
---|
324 | #endif /* !K_STRICT */
|
---|
325 |
|
---|
326 | #define kHlpAssertPtr(ptr) kHlpAssertMsg(K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
|
---|
327 | #define kHlpAssertPtrReturn(ptr, rcRet) kHlpAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
|
---|
328 | #define kHlpAssertPtrReturn(ptr, rcRet) kHlpAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
|
---|
329 | #define kHlpAssertPtrReturnVoid(ptr) kHlpAssertMsgReturnVoid(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
|
---|
330 | #define kHlpAssertPtrNull(ptr) kHlpAssertMsg(!(ptr) || K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
|
---|
331 | #define kHlpAssertPtrNullReturn(ptr, rcRet) kHlpAssertMsgReturn(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
|
---|
332 | #define kHlpAssertPtrNullReturnVoid(ptr) kHlpAssertMsgReturnVoid(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
|
---|
333 | #define kHlpAssertRC(rc) kHlpAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
|
---|
334 | #define kHlpAssertRCReturn(rc, rcRet) kHlpAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
|
---|
335 | #define kHlpAssertRCReturnVoid(rc) kHlpAssertMsgReturnVoid((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)))
|
---|
336 |
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Helper function that displays the first part of the assertion message.
|
---|
340 | *
|
---|
341 | * @param pszExpr The expression.
|
---|
342 | * @param pszFile The file name.
|
---|
343 | * @param iLine The line number is the file.
|
---|
344 | * @param pszFunction The function name.
|
---|
345 | * @internal
|
---|
346 | */
|
---|
347 | KHLP_DECL(void) kHlpAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Helper function that displays custom assert message.
|
---|
351 | *
|
---|
352 | * @param pszFormat Format string that get passed to vprintf.
|
---|
353 | * @param ... Format arguments.
|
---|
354 | * @internal
|
---|
355 | */
|
---|
356 | KHLP_DECL(void) kHlpAssertMsg2(const char *pszFormat, ...);
|
---|
357 |
|
---|
358 |
|
---|
359 | /** @} */
|
---|
360 |
|
---|
361 | #ifdef __cplusplus
|
---|
362 | }
|
---|
363 | #endif
|
---|
364 |
|
---|
365 | #endif
|
---|