1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
* vim: set ts=8 sw=4 et tw=99 ft=cpp:
4
* ***** BEGIN LICENSE BLOCK *****
5
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
7
* The contents of this file are subject to the Mozilla Public License Version
8
* 1.1 (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at:
10
* http://www.mozilla.org/MPL/
12
* Software distributed under the License is distributed on an "AS IS" basis,
13
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14
* for the specific language governing rights and limitations under the
17
* The Original Code is Mozilla Code.
19
* The Initial Developer of the Original Code is
20
* The Mozilla Foundation
21
* Portions created by the Initial Developer are Copyright (C) 2011
22
* the Initial Developer. All Rights Reserved.
25
* Jeff Walden <jwalden+code@mit.edu>
27
* Alternatively, the contents of this file may be used under the terms of
28
* either the GNU General Public License Version 2 or later (the "GPL"), or
29
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30
* in which case the provisions of the GPL or the LGPL are applicable instead
31
* of those above. If you wish to allow use of your version of this file only
32
* under the terms of either the GPL or the LGPL, and not to allow others to
33
* use your version of this file under the terms of the MPL, indicate your
34
* decision by deleting the provisions above and replace them with the notice
35
* and other provisions required by the GPL or the LGPL. If you do not delete
36
* the provisions above, a recipient may use your version of this file under
37
* the terms of any one of the MPL, the GPL or the LGPL.
39
* ***** END LICENSE BLOCK ***** */
41
/* Implementations of runtime and static assertion macros for C and C++. */
43
#ifndef mozilla_Assertions_h_
44
#define mozilla_Assertions_h_
46
#include "mozilla/Attributes.h"
47
#include "mozilla/Types.h"
50
* MOZ_STATIC_ASSERT may be used to assert a condition *at compile time*. This
51
* can be useful when you make certain assumptions about what must hold for
52
* optimal, or even correct, behavior. For example, you might assert that the
53
* size of a struct is a multiple of the target architecture's word size:
56
* MOZ_STATIC_ASSERT(sizeof(S) % sizeof(size_t) == 0,
57
* "S should be a multiple of word size for efficiency");
59
* This macro can be used in any location where both an extern declaration and a
60
* typedef could be used.
62
* Be aware of the gcc 4.2 concerns noted further down when writing patches that
63
* use this macro, particularly if a patch only bounces on OS X.
66
# if defined(__clang__)
67
# ifndef __has_extension
68
# define __has_extension __has_feature /* compatibility, for older versions of clang */
70
# if __has_extension(cxx_static_assert)
71
# define MOZ_STATIC_ASSERT(cond, reason) static_assert((cond), reason)
73
# elif defined(__GNUC__)
74
# if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L) && \
75
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
76
# define MOZ_STATIC_ASSERT(cond, reason) static_assert((cond), reason)
78
# elif defined(_MSC_VER)
79
# if _MSC_VER >= 1600 /* MSVC 10 */
80
# define MOZ_STATIC_ASSERT(cond, reason) static_assert((cond), reason)
82
# elif defined(__HP_aCC)
83
# if __HP_aCC >= 62500 && defined(_HP_CXX0x_SOURCE)
84
# define MOZ_STATIC_ASSERT(cond, reason) static_assert((cond), reason)
88
#ifndef MOZ_STATIC_ASSERT
89
# define MOZ_STATIC_ASSERT_GLUE1(x, y) x##y
90
# define MOZ_STATIC_ASSERT_GLUE(x, y) MOZ_STATIC_ASSERT_GLUE1(x, y)
91
# if defined(__SUNPRO_CC)
93
* The Sun Studio C++ compiler is buggy when declaring, inside a function,
94
* another extern'd function with an array argument whose length contains a
95
* sizeof, triggering the error message "sizeof expression not accepted as
96
* size of array parameter". This bug (6688515, not public yet) would hit
97
* defining moz_static_assert as a function, so we always define an extern
98
* array for Sun Studio.
100
* We include the line number in the symbol name in a best-effort attempt
101
* to avoid conflicts (see below).
103
# define MOZ_STATIC_ASSERT(cond, reason) \
104
extern char MOZ_STATIC_ASSERT_GLUE(moz_static_assert, __LINE__)[(cond) ? 1 : -1]
105
# elif defined(__COUNTER__)
107
* If there was no preferred alternative, use a compiler-agnostic version.
109
* Note that the non-__COUNTER__ version has a bug in C++: it can't be used
110
* in both |extern "C"| and normal C++ in the same translation unit. (Alas
111
* |extern "C"| isn't allowed in a function.) The only affected compiler
112
* we really care about is gcc 4.2. For that compiler and others like it,
113
* we include the line number in the function name to do the best we can to
114
* avoid conflicts. These should be rare: a conflict would require use of
115
* MOZ_STATIC_ASSERT on the same line in separate files in the same
116
* translation unit, *and* the uses would have to be in code with
117
* different linkage, *and* the first observed use must be in C++-linkage
120
# define MOZ_STATIC_ASSERT(cond, reason) \
121
typedef int MOZ_STATIC_ASSERT_GLUE(moz_static_assert, __COUNTER__)[(cond) ? 1 : -1]
123
# define MOZ_STATIC_ASSERT(cond, reason) \
124
extern void MOZ_STATIC_ASSERT_GLUE(moz_static_assert, __LINE__)(int arg[(cond) ? 1 : -1])
128
#define MOZ_STATIC_ASSERT_IF(cond, expr, reason) MOZ_STATIC_ASSERT(!(cond) || (expr), reason)
134
extern MFBT_API(void)
137
extern MFBT_API(void)
138
MOZ_Assert(const char* s, const char* file, int ln);
145
* MOZ_ASSERT(expr [, explanation-string]) asserts that |expr| must be truthy in
146
* debug builds. If it is, execution continues. Otherwise, an error message
147
* including the expression and the explanation-string (if provided) is printed,
148
* an attempt is made to invoke any existing debugger, and execution halts.
149
* MOZ_ASSERT is fatal: no recovery is possible. Do not assert a condition
150
* which can correctly be falsy.
152
* The optional explanation-string, if provided, must be a string literal
153
* explaining the assertion. It is intended for use with assertions whose
154
* correctness or rationale is non-obvious, and for assertions where the "real"
155
* condition being tested is best described prosaically. Don't provide an
156
* explanation if it's not actually helpful.
158
* // No explanation needed: pointer arguments often must not be NULL.
161
* // An explanation can be helpful to explain exactly how we know an
162
* // assertion is valid.
163
* MOZ_ASSERT(state == WAITING_FOR_RESPONSE,
164
* "given that <thingA> and <thingB>, we must have...");
166
* // Or it might disambiguate multiple identical (save for their location)
167
* // assertions of the same expression.
168
* MOZ_ASSERT(getSlot(PRIMITIVE_THIS_SLOT).isUndefined(),
169
* "we already set [[PrimitiveThis]] for this Boolean object");
170
* MOZ_ASSERT(getSlot(PRIMITIVE_THIS_SLOT).isUndefined(),
171
* "we already set [[PrimitiveThis]] for this String object");
173
* MOZ_ASSERT has no effect in non-debug builds. It is designed to catch bugs
174
* *only* during debugging, not "in the field".
177
/* First the single-argument form. */
178
# define MOZ_ASSERT_HELPER1(expr) \
179
((expr) ? ((void)0) : MOZ_Assert(#expr, __FILE__, __LINE__))
180
/* Now the two-argument form. */
181
# define MOZ_ASSERT_HELPER2(expr, explain) \
182
((expr) ? ((void)0) : MOZ_Assert(#expr " (" explain ")", __FILE__, __LINE__))
183
/* And now, helper macrology up the wazoo. */
185
* Count the number of arguments passed to MOZ_ASSERT, very carefully
186
* tiptoeing around an MSVC bug where it improperly expands __VA_ARGS__ as a
187
* single token in argument lists. See these URLs for details:
189
* http://connect.microsoft.com/VisualStudio/feedback/details/380090/variadic-macro-replacement
190
* http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/#comment-644
192
# define MOZ_COUNT_ASSERT_ARGS_IMPL2(_1, _2, count, ...) \
194
# define MOZ_COUNT_ASSERT_ARGS_IMPL(args) \
195
MOZ_COUNT_ASSERT_ARGS_IMPL2 args
196
# define MOZ_COUNT_ASSERT_ARGS(...) \
197
MOZ_COUNT_ASSERT_ARGS_IMPL((__VA_ARGS__, 2, 1, 0))
198
/* Pick the right helper macro to invoke. */
199
# define MOZ_ASSERT_CHOOSE_HELPER2(count) MOZ_ASSERT_HELPER##count
200
# define MOZ_ASSERT_CHOOSE_HELPER1(count) MOZ_ASSERT_CHOOSE_HELPER2(count)
201
# define MOZ_ASSERT_CHOOSE_HELPER(count) MOZ_ASSERT_CHOOSE_HELPER1(count)
202
/* The actual macro. */
203
# define MOZ_ASSERT_GLUE(x, y) x y
204
# define MOZ_ASSERT(...) \
205
MOZ_ASSERT_GLUE(MOZ_ASSERT_CHOOSE_HELPER(MOZ_COUNT_ASSERT_ARGS(__VA_ARGS__)), \
208
# define MOZ_ASSERT(...) ((void)0)
212
* MOZ_ASSERT_IF(cond1, cond2) is equivalent to MOZ_ASSERT(cond2) if cond1 is
215
* MOZ_ASSERT_IF(isPrime(num), num == 2 || isOdd(num));
217
* As with MOZ_ASSERT, MOZ_ASSERT_IF has effect only in debug builds. It is
218
* designed to catch bugs during debugging, not "in the field".
221
# define MOZ_ASSERT_IF(cond, expr) ((cond) ? MOZ_ASSERT(expr) : ((void)0))
223
# define MOZ_ASSERT_IF(cond, expr) ((void)0)
226
/* MOZ_NOT_REACHED_MARKER() expands (in compilers which support it) to an
227
* expression which states that it is undefined behavior for the compiler to
228
* reach this point. Most code should probably use the higher level
229
* MOZ_NOT_REACHED (which expands to this when appropriate).
231
#if defined(__clang__)
232
# define MOZ_NOT_REACHED_MARKER() __builtin_unreachable()
233
#elif defined(__GNUC__)
234
# if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
235
# define MOZ_NOT_REACHED_MARKER() __builtin_unreachable()
237
#elif defined(_MSC_VER)
238
# define MOZ_NOT_REACHED_MARKER() __assume(0)
242
* MOZ_NOT_REACHED(reason) indicates that the given point can't be reached
243
* during execution: simply reaching that point in execution is a bug. It takes
244
* as an argument an error message indicating the reason why that point should
245
* not have been reachable.
247
* // ...in a language parser...
248
* void handle(BooleanLiteralNode node)
251
* handleTrueLiteral();
252
* else if (node.isFalse())
253
* handleFalseLiteral();
255
* MOZ_NOT_REACHED("boolean literal that's not true or false?");
258
#if defined(MOZ_NOT_REACHED_MARKER)
260
# define MOZ_NOT_REACHED(reason) do { \
261
MOZ_Assert(reason, __FILE__, __LINE__); \
262
MOZ_NOT_REACHED_MARKER(); \
265
# define MOZ_NOT_REACHED(reason) MOZ_NOT_REACHED_MARKER()
268
# if defined(__GNUC__)
270
* On older versions of gcc we need to call a noreturn function to mark the
271
* code as unreachable. Since what we want is an unreachable version of
272
* MOZ_Assert, we use an asm label
273
* (http://gcc.gnu.org/onlinedocs/gcc-4.6.2/gcc/Asm-Labels.html) to create
274
* a new declaration to the same symbol. MOZ_ASSERT_NR should only be
275
* used via this macro, as it is a very specific hack to older versions of
278
# define MOZ_GETASMPREFIX2(X) #X
279
# define MOZ_GETASMPREFIX(X) MOZ_GETASMPREFIX2(X)
280
# define MOZ_ASMPREFIX MOZ_GETASMPREFIX(__USER_LABEL_PREFIX__)
281
extern MOZ_NORETURN MFBT_API(void)
282
MOZ_ASSERT_NR(const char* s, const char* file, int ln) \
283
asm (MOZ_ASMPREFIX "MOZ_Assert");
285
# define MOZ_NOT_REACHED(reason) MOZ_ASSERT_NR(reason, __FILE__, __LINE__)
286
# elif defined(DEBUG)
287
# define MOZ_NOT_REACHED(reason) MOZ_Assert(reason, __FILE__, __LINE__)
289
# define MOZ_NOT_REACHED(reason) ((void)0)
294
* MOZ_ALWAYS_TRUE(expr) and MOZ_ALWAYS_FALSE(expr) always evaluate the provided
295
* expression, in debug builds and in release builds both. Then, in debug
296
* builds only, the value of the expression is asserted either true or false
300
# define MOZ_ALWAYS_TRUE(expr) MOZ_ASSERT((expr))
301
# define MOZ_ALWAYS_FALSE(expr) MOZ_ASSERT(!(expr))
303
# define MOZ_ALWAYS_TRUE(expr) ((void)(expr))
304
# define MOZ_ALWAYS_FALSE(expr) ((void)(expr))
307
#endif /* mozilla_Assertions_h_ */