VirtualBox

source: kStuff/trunk/include/k/kHlpAssert.h@ 75

Last change on this file since 75 was 70, checked in by bird, 9 years ago

Make gcc 5.x happy when in pedantic mode.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.0 KB
Line 
1/* $Id: kHlpAssert.h 70 2015-08-13 09:03:02Z bird $ */
2/** @file
3 * kHlpAssert - Assertion Macros.
4 */
5
6/*
7 * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
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
37extern "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#else
57# error "Port Me"
58#endif
59
60/** @def K_FUNCTION
61 * Undecorated function name macro expanded by the compiler.
62 */
63#if defined(__GNUC__)
64# define K_FUNCTION __func__
65#else
66# define K_FUNCTION __FUNCTION__
67#endif
68
69#ifdef K_STRICT
70
71# define kHlpAssert(expr) \
72 do { \
73 if (!(expr)) \
74 { \
75 kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
76 kHlpAssertBreakpoint(); \
77 } \
78 } while (0)
79
80# define kHlpAssertStmt(expr, stmt) \
81 do { \
82 if (!(expr)) \
83 { \
84 kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
85 kHlpAssertBreakpoint(); \
86 stmt; \
87 } \
88 } while (0)
89
90# define kHlpAssertReturn(expr, rcRet) \
91 do { \
92 if (!(expr)) \
93 { \
94 kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
95 kHlpAssertBreakpoint(); \
96 return (rcRet); \
97 } \
98 } while (0)
99
100# define kHlpAssertStmtReturn(expr, stmt, rcRet) \
101 do { \
102 if (!(expr)) \
103 { \
104 kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
105 kHlpAssertBreakpoint(); \
106 stmt; \
107 return (rcRet); \
108 } \
109 } while (0)
110
111# define kHlpAssertReturnVoid(expr) \
112 do { \
113 if (!(expr)) \
114 { \
115 kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
116 kHlpAssertBreakpoint(); \
117 return; \
118 } \
119 } while (0)
120
121# define kHlpAssertStmtReturnVoid(expr, stmt) \
122 do { \
123 if (!(expr)) \
124 { \
125 kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
126 kHlpAssertBreakpoint(); \
127 stmt; \
128 return; \
129 } \
130 } while (0)
131
132# define kHlpAssertMsg(expr, msg) \
133 do { \
134 if (!(expr)) \
135 { \
136 kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
137 kHlpAssertMsg2 msg; \
138 kHlpAssertBreakpoint(); \
139 } \
140 } while (0)
141
142# define kHlpAssertMsgStmt(expr, msg, stmt) \
143 do { \
144 if (!(expr)) \
145 { \
146 kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
147 kHlpAssertMsg2 msg; \
148 kHlpAssertBreakpoint(); \
149 stmt; \
150 } \
151 } while (0)
152
153# define kHlpAssertMsgReturn(expr, msg, rcRet) \
154 do { \
155 if (!(expr)) \
156 { \
157 kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
158 kHlpAssertMsg2 msg; \
159 kHlpAssertBreakpoint(); \
160 return (rcRet); \
161 } \
162 } while (0)
163
164# define kHlpAssertMsgStmtReturn(expr, msg, stmt, rcRet) \
165 do { \
166 if (!(expr)) \
167 { \
168 kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
169 kHlpAssertMsg2 msg; \
170 kHlpAssertBreakpoint(); \
171 stmt; \
172 return (rcRet); \
173 } \
174 } while (0)
175
176# define kHlpAssertMsgReturnVoid(expr, msg) \
177 do { \
178 if (!(expr)) \
179 { \
180 kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
181 kHlpAssertMsg2 msg; \
182 kHlpAssertBreakpoint(); \
183 return; \
184 } \
185 } while (0)
186
187# define kHlpAssertMsgStmtReturnVoid(expr, msg, stmt) \
188 do { \
189 if (!(expr)) \
190 { \
191 kHlpAssertMsg1(#expr, __FILE__, __LINE__, K_FUNCTION); \
192 kHlpAssertMsg2 msg; \
193 kHlpAssertBreakpoint(); \
194 stmt; \
195 return; \
196 } \
197 } while (0)
198
199#else /* !K_STRICT */
200# define kHlpAssert(expr) do { } while (0)
201# define kHlpAssertStmt(expr, stmt) do { if (!(expr)) { stmt; } } while (0)
202# define kHlpAssertReturn(expr, rcRet) do { if (!(expr)) return (rcRet); } while (0)
203# define kHlpAssertStmtReturn(expr, stmt, rcRet) do { if (!(expr)) { stmt; return (rcRet); } } while (0)
204# define kHlpAssertReturnVoid(expr) do { if (!(expr)) return; } while (0)
205# define kHlpAssertStmtReturnVoid(expr, stmt) do { if (!(expr)) { stmt; return; } } while (0)
206# define kHlpAssertMsg(expr, msg) do { } while (0)
207# define kHlpAssertMsgStmt(expr, msg, stmt) do { if (!(expr)) { stmt; } } while (0)
208# define kHlpAssertMsgReturn(expr, msg, rcRet) do { if (!(expr)) return (rcRet); } while (0)
209# define kHlpAssertMsgStmtReturn(expr, msg, stmt, rcRet) do { if (!(expr)) { stmt; return (rcRet); } } while (0)
210# define kHlpAssertMsgReturnVoid(expr, msg) do { if (!(expr)) return; } while (0)
211# define kHlpAssertMsgStmtReturnVoid(expr, msg, stmt) do { if (!(expr)) { stmt; return; } } while (0)
212#endif /* !K_STRICT */
213
214#define kHlpAssertPtr(ptr) kHlpAssertMsg(K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
215#define kHlpAssertPtrReturn(ptr, rcRet) kHlpAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
216#define kHlpAssertPtrReturn(ptr, rcRet) kHlpAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
217#define kHlpAssertPtrReturnVoid(ptr) kHlpAssertMsgReturnVoid(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
218#define kHlpAssertPtrNull(ptr) kHlpAssertMsg(!(ptr) || K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
219#define kHlpAssertPtrNullReturn(ptr, rcRet) kHlpAssertMsgReturn(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
220#define kHlpAssertPtrNullReturnVoid(ptr) kHlpAssertMsgReturnVoid(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
221#define kHlpAssertRC(rc) kHlpAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
222#define kHlpAssertRCReturn(rc, rcRet) kHlpAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
223#define kHlpAssertRCReturnVoid(rc) kHlpAssertMsgReturnVoid((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)))
224#define kHlpAssertFailed() kHlpAssert(0)
225#define kHlpAssertFailedReturn(rcRet) kHlpAssertReturn(0, (rcRet))
226#define kHlpAssertFailedReturnVoid() kHlpAssertReturnVoid(0)
227#define kHlpAssertMsgFailed(msg) kHlpAssertMsg(0, msg)
228#define kHlpAssertMsgFailedReturn(msg, rcRet) kHlpAssertMsgReturn(0, msg, (rcRet))
229#define kHlpAssertMsgFailedReturnVoid(msg) kHlpAssertMsgReturnVoid(0, msg))
230
231/**
232 * Helper function that displays the first part of the assertion message.
233 *
234 * @param pszExpr The expression.
235 * @param pszFile The file name.
236 * @param iLine The line number is the file.
237 * @param pszFunction The function name.
238 * @internal
239 */
240KHLP_DECL(void) kHlpAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
241
242/**
243 * Helper function that displays custom assert message.
244 *
245 * @param pszFormat Format string that get passed to vprintf.
246 * @param ... Format arguments.
247 * @internal
248 */
249KHLP_DECL(void) kHlpAssertMsg2(const char *pszFormat, ...);
250
251
252/** @} */
253
254#ifdef __cplusplus
255}
256#endif
257
258#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette