1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
18 #include "mozilla/Assertions.h"
27 * Abort the execution of the program if the expression evaluates to
30 * There is no status value returned from the macro.
32 * Note that the non-debug version of this macro does <b>not</b>
33 * evaluate the expression argument. Hence side effect statements
34 * as arguments to the macro will yield improper execution in a
35 * non-debug build. For example:
37 * NS_ABORT_IF_FALSE(0 == foo++, "yikes foo should be zero");
39 * Note also that the non-debug version of this macro does <b>not</b>
40 * evaluate the message argument.
42 #define NS_ABORT_IF_FALSE(_expr, _msg) \
45 NS_DebugBreak(NS_DEBUG_ABORT, _msg, #_expr, __FILE__, __LINE__); \
50 * Warn if a given condition is false.
52 * Program execution continues past the usage of this macro.
54 * Note also that the non-debug version of this macro does <b>not</b>
55 * evaluate the message argument.
57 #define NS_WARN_IF_FALSE(_expr,_msg) \
60 NS_DebugBreak(NS_DEBUG_WARNING, _msg, #_expr, __FILE__, __LINE__); \
65 * Test a precondition for truth. If the expression is not true then
66 * trigger a program failure.
68 #define NS_PRECONDITION(expr, str) \
71 NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \
76 * Test an assertion for truth. If the expression is not true then
77 * trigger a program failure.
79 #define NS_ASSERTION(expr, str) \
82 NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \
87 * Test a post-condition for truth. If the expression is not true then
88 * trigger a program failure.
90 #define NS_POSTCONDITION(expr, str) \
93 NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \
98 * This macros triggers a program failure if executed. It indicates that
99 * an attempt was made to execute some unimplemented functionality.
101 #define NS_NOTYETIMPLEMENTED(str) \
102 NS_DebugBreak(NS_DEBUG_ASSERTION, str, "NotYetImplemented", __FILE__, __LINE__)
105 * This macros triggers a program failure if executed. It indicates that
106 * an attempt was made to execute some unimplemented functionality.
108 #define NS_NOTREACHED(str) \
109 NS_DebugBreak(NS_DEBUG_ASSERTION, str, "Not Reached", __FILE__, __LINE__)
112 * Log an error message.
114 #define NS_ERROR(str) \
115 NS_DebugBreak(NS_DEBUG_ASSERTION, str, "Error", __FILE__, __LINE__)
118 * Log a warning message.
120 #define NS_WARNING(str) \
121 NS_DebugBreak(NS_DEBUG_WARNING, str, nullptr, __FILE__, __LINE__)
127 NS_DebugBreak(NS_DEBUG_ABORT, nullptr, nullptr, __FILE__, __LINE__)
133 NS_DebugBreak(NS_DEBUG_BREAK, nullptr, nullptr, __FILE__, __LINE__)
138 * The non-debug version of these macros do not evaluate the
139 * expression or the message arguments to the macro.
141 #define NS_ABORT_IF_FALSE(_expr, _msg) do { /* nothing */ } while(0)
142 #define NS_WARN_IF_FALSE(_expr, _msg) do { /* nothing */ } while(0)
143 #define NS_PRECONDITION(expr, str) do { /* nothing */ } while(0)
144 #define NS_ASSERTION(expr, str) do { /* nothing */ } while(0)
145 #define NS_POSTCONDITION(expr, str) do { /* nothing */ } while(0)
146 #define NS_NOTYETIMPLEMENTED(str) do { /* nothing */ } while(0)
147 #define NS_NOTREACHED(str) do { /* nothing */ } while(0)
148 #define NS_ERROR(str) do { /* nothing */ } while(0)
149 #define NS_WARNING(str) do { /* nothing */ } while(0)
150 #define NS_ABORT() do { /* nothing */ } while(0)
151 #define NS_BREAK() do { /* nothing */ } while(0)
155 /******************************************************************************
156 ** Macros for static assertions. These are used by the sixgill tool.
157 ** When the tool is not running these macros are no-ops.
158 ******************************************************************************/
160 /* Avoid name collision if included with other headers defining annotations. */
161 #ifndef HAVE_STATIC_ANNOTATIONS
162 #define HAVE_STATIC_ANNOTATIONS
166 #define STATIC_PRECONDITION(COND) __attribute__((precondition(#COND)))
167 #define STATIC_PRECONDITION_ASSUME(COND) __attribute__((precondition_assume(#COND)))
168 #define STATIC_POSTCONDITION(COND) __attribute__((postcondition(#COND)))
169 #define STATIC_POSTCONDITION_ASSUME(COND) __attribute__((postcondition_assume(#COND)))
170 #define STATIC_INVARIANT(COND) __attribute__((invariant(#COND)))
171 #define STATIC_INVARIANT_ASSUME(COND) __attribute__((invariant_assume(#COND)))
173 /* Used to make identifiers for assert/assume annotations in a function. */
174 #define STATIC_PASTE2(X,Y) X ## Y
175 #define STATIC_PASTE1(X,Y) STATIC_PASTE2(X,Y)
177 #define STATIC_ASSERT(COND) \
179 __attribute__((assert_static(#COND), unused)) \
180 int STATIC_PASTE1(assert_static_, __COUNTER__); \
183 #define STATIC_ASSUME(COND) \
185 __attribute__((assume_static(#COND), unused)) \
186 int STATIC_PASTE1(assume_static_, __COUNTER__); \
189 #define STATIC_ASSERT_RUNTIME(COND) \
191 __attribute__((assert_static_runtime(#COND), unused)) \
192 int STATIC_PASTE1(assert_static_runtime_, __COUNTER__); \
195 #else /* XGILL_PLUGIN */
197 #define STATIC_PRECONDITION(COND) /* nothing */
198 #define STATIC_PRECONDITION_ASSUME(COND) /* nothing */
199 #define STATIC_POSTCONDITION(COND) /* nothing */
200 #define STATIC_POSTCONDITION_ASSUME(COND) /* nothing */
201 #define STATIC_INVARIANT(COND) /* nothing */
202 #define STATIC_INVARIANT_ASSUME(COND) /* nothing */
204 #define STATIC_ASSERT(COND) do { /* nothing */ } while(0)
205 #define STATIC_ASSUME(COND) do { /* nothing */ } while(0)
206 #define STATIC_ASSERT_RUNTIME(COND) do { /* nothing */ } while(0)
208 #endif /* XGILL_PLUGIN */
210 #define STATIC_SKIP_INFERENCE STATIC_INVARIANT(skip_inference())
212 #endif /* HAVE_STATIC_ANNOTATIONS */
216 /* Redefine runtime assertion macros to perform static assertions, for both
217 * debug and release builds. Don't include the original runtime assertions;
218 * this ensures the tool will consider cases where the assertion fails. */
220 #undef NS_PRECONDITION
222 #undef NS_POSTCONDITION
224 #define NS_PRECONDITION(expr, str) STATIC_ASSERT_RUNTIME(expr)
225 #define NS_ASSERTION(expr, str) STATIC_ASSERT_RUNTIME(expr)
226 #define NS_POSTCONDITION(expr, str) STATIC_ASSERT_RUNTIME(expr)
228 #endif /* XGILL_PLUGIN */
230 /******************************************************************************
231 ** Macros for terminating execution when an unrecoverable condition is
232 ** reached. These need to be compiled regardless of the DEBUG flag.
233 ******************************************************************************/
236 * Terminate execution <i>immediately</i>, and if possible on the current
237 * platform, in such a way that execution can't be continued by other
238 * code (e.g., by intercepting a signal).
240 #define NS_RUNTIMEABORT(msg) \
241 NS_DebugBreak(NS_DEBUG_ABORT, msg, nullptr, __FILE__, __LINE__)
244 /* Macros for checking the trueness of an expression passed in within an
245 * interface implementation. These need to be compiled regardless of the */
247 ******************************************************************************/
249 #define NS_ENSURE_TRUE(x, ret) \
251 if (NS_UNLIKELY(!(x))) { \
252 NS_WARNING("NS_ENSURE_TRUE(" #x ") failed"); \
257 #define NS_ENSURE_FALSE(x, ret) \
258 NS_ENSURE_TRUE(!(x), ret)
260 #define NS_ENSURE_TRUE_VOID(x) \
262 if (NS_UNLIKELY(!(x))) { \
263 NS_WARNING("NS_ENSURE_TRUE(" #x ") failed"); \
268 #define NS_ENSURE_FALSE_VOID(x) \
269 NS_ENSURE_TRUE_VOID(!(x))
271 /******************************************************************************
272 ** Macros for checking results
273 ******************************************************************************/
275 #if defined(DEBUG) && !defined(XPCOM_GLUE_AVOID_NSPR)
277 #define NS_ENSURE_SUCCESS_BODY(res, ret) \
278 char *msg = PR_smprintf("NS_ENSURE_SUCCESS(%s, %s) failed with " \
279 "result 0x%X", #res, #ret, __rv); \
281 PR_smprintf_free(msg);
283 #define NS_ENSURE_SUCCESS_BODY_VOID(res) \
284 char *msg = PR_smprintf("NS_ENSURE_SUCCESS_VOID(%s) failed with " \
285 "result 0x%X", #res, __rv); \
287 PR_smprintf_free(msg);
291 #define NS_ENSURE_SUCCESS_BODY(res, ret) \
292 NS_WARNING("NS_ENSURE_SUCCESS(" #res ", " #ret ") failed");
294 #define NS_ENSURE_SUCCESS_BODY_VOID(res) \
295 NS_WARNING("NS_ENSURE_SUCCESS_VOID(" #res ") failed");
299 #define NS_ENSURE_SUCCESS(res, ret) \
301 nsresult __rv = res; /* Don't evaluate |res| more than once */ \
302 if (NS_FAILED(__rv)) { \
303 NS_ENSURE_SUCCESS_BODY(res, ret) \
308 #define NS_ENSURE_SUCCESS_VOID(res) \
310 nsresult __rv = res; \
311 if (NS_FAILED(__rv)) { \
312 NS_ENSURE_SUCCESS_BODY_VOID(res) \
317 /******************************************************************************
318 ** Macros for checking state and arguments upon entering interface boundaries
319 ******************************************************************************/
321 #define NS_ENSURE_ARG(arg) \
322 NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_ARG)
324 #define NS_ENSURE_ARG_POINTER(arg) \
325 NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_POINTER)
327 #define NS_ENSURE_ARG_MIN(arg, min) \
328 NS_ENSURE_TRUE((arg) >= min, NS_ERROR_INVALID_ARG)
330 #define NS_ENSURE_ARG_MAX(arg, max) \
331 NS_ENSURE_TRUE((arg) <= max, NS_ERROR_INVALID_ARG)
333 #define NS_ENSURE_ARG_RANGE(arg, min, max) \
334 NS_ENSURE_TRUE(((arg) >= min) && ((arg) <= max), NS_ERROR_INVALID_ARG)
336 #define NS_ENSURE_STATE(state) \
337 NS_ENSURE_TRUE(state, NS_ERROR_UNEXPECTED)
339 #define NS_ENSURE_NO_AGGREGATION(outer) \
340 NS_ENSURE_FALSE(outer, NS_ERROR_NO_AGGREGATION)
342 #define NS_ENSURE_PROPER_AGGREGATION(outer, iid) \
343 NS_ENSURE_FALSE(outer && !iid.Equals(NS_GET_IID(nsISupports)), NS_ERROR_INVALID_ARG)
345 /*****************************************************************************/
348 #define NS_CheckThreadSafe(owningThread, msg)
350 #define NS_CheckThreadSafe(owningThread, msg) \
351 MOZ_ASSERT(owningThread == PR_GetCurrentThread(), msg)
354 /* When compiling the XPCOM Glue on Windows, we pretend that it's going to
355 * be linked with a static CRT (-MT) even when it's not. This means that we
356 * cannot link to data exports from the CRT, only function exports. So,
357 * instead of referencing "stderr" directly, use fdopen.
364 printf_stderr(const char *fmt
, ...);
370 #endif /* nsDebug_h___ */