]> git.saurik.com Git - apple/javascriptcore.git/blob - wtf/Assertions.h
365903046f965a6bcb0f4e09dc5c13e812b9fc69
[apple/javascriptcore.git] / wtf / Assertions.h
1 /*
2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #ifndef WTF_Assertions_h
27 #define WTF_Assertions_h
28
29 /*
30 no namespaces because this file has to be includable from C and Objective-C
31
32 Note, this file uses many GCC extensions, but it should be compatible with
33 C, Objective C, C++, and Objective C++.
34
35 For non-debug builds, everything is disabled by default.
36 Defining any of the symbols explicitly prevents this from having any effect.
37
38 MSVC7 note: variadic macro support was added in MSVC8, so for now we disable
39 those macros in MSVC7. For more info, see the MSDN document on variadic
40 macros here:
41
42 http://msdn2.microsoft.com/en-us/library/ms177415(VS.80).aspx
43 */
44
45 #include "Platform.h"
46
47 #include <stdbool.h>
48
49 #if COMPILER(MSVC)
50 #include <stddef.h>
51 #else
52 #include <inttypes.h>
53 #endif
54
55 #if OS(SYMBIAN)
56 #include <e32def.h>
57 #include <e32debug.h>
58 #endif
59
60 #ifdef NDEBUG
61 #define ASSERTIONS_DISABLED_DEFAULT 1
62 #else
63 #define ASSERTIONS_DISABLED_DEFAULT 0
64 #endif
65
66 #if COMPILER(MSVC7) || COMPILER(WINSCW)
67 #define HAVE_VARIADIC_MACRO 0
68 #else
69 #define HAVE_VARIADIC_MACRO 1
70 #endif
71
72 #ifndef ASSERT_DISABLED
73 #define ASSERT_DISABLED ASSERTIONS_DISABLED_DEFAULT
74 #endif
75
76 #ifndef ASSERT_MSG_DISABLED
77 #if HAVE(VARIADIC_MACRO)
78 #define ASSERT_MSG_DISABLED ASSERTIONS_DISABLED_DEFAULT
79 #else
80 #define ASSERT_MSG_DISABLED 1
81 #endif
82 #endif
83
84 #ifndef ASSERT_ARG_DISABLED
85 #define ASSERT_ARG_DISABLED ASSERTIONS_DISABLED_DEFAULT
86 #endif
87
88 #ifndef FATAL_DISABLED
89 #if HAVE(VARIADIC_MACRO)
90 #define FATAL_DISABLED ASSERTIONS_DISABLED_DEFAULT
91 #else
92 #define FATAL_DISABLED 1
93 #endif
94 #endif
95
96 #ifndef ERROR_DISABLED
97 #if HAVE(VARIADIC_MACRO)
98 #define ERROR_DISABLED ASSERTIONS_DISABLED_DEFAULT
99 #else
100 #define ERROR_DISABLED 1
101 #endif
102 #endif
103
104 #ifndef LOG_DISABLED
105 #if HAVE(VARIADIC_MACRO)
106 #define LOG_DISABLED ASSERTIONS_DISABLED_DEFAULT
107 #else
108 #define LOG_DISABLED 1
109 #endif
110 #endif
111
112 #if COMPILER(GCC)
113 #define WTF_PRETTY_FUNCTION __PRETTY_FUNCTION__
114 #else
115 #define WTF_PRETTY_FUNCTION __FUNCTION__
116 #endif
117
118 /* WTF logging functions can process %@ in the format string to log a NSObject* but the printf format attribute
119 emits a warning when %@ is used in the format string. Until <rdar://problem/5195437> is resolved we can't include
120 the attribute when being used from Objective-C code in case it decides to use %@. */
121 #if COMPILER(GCC) && !defined(__OBJC__)
122 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments) __attribute__((__format__(printf, formatStringArgument, extraArguments)))
123 #else
124 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments)
125 #endif
126
127 /* This macro is needed to prevent the clang static analyzer from generating false-positive reports in ASSERT() macros. */
128 #ifdef __clang__
129 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
130 #else
131 #define CLANG_ANALYZER_NORETURN
132 #endif
133
134 /* These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. */
135
136 #ifdef __cplusplus
137 extern "C" {
138 #endif
139
140 typedef enum { WTFLogChannelOff, WTFLogChannelOn } WTFLogChannelState;
141
142 typedef struct {
143 unsigned mask;
144 const char *defaultName;
145 WTFLogChannelState state;
146 } WTFLogChannel;
147
148 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion) CLANG_ANALYZER_NORETURN;
149 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) CLANG_ANALYZER_NORETURN WTF_ATTRIBUTE_PRINTF(5, 6);
150 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion) CLANG_ANALYZER_NORETURN;
151 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) CLANG_ANALYZER_NORETURN WTF_ATTRIBUTE_PRINTF(4, 5);
152 void WTFReportError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
153 void WTFLog(WTFLogChannel* channel, const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
154 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
155
156 #ifdef __cplusplus
157 }
158 #endif
159
160 /* CRASH -- gets us into the debugger or the crash reporter -- signals are ignored by the crash reporter so we must do better */
161
162 #ifndef CRASH
163 #if OS(SYMBIAN)
164 #define CRASH() do { \
165 __DEBUGGER(); \
166 User::Panic(_L("Webkit CRASH"),0); \
167 } while(false)
168 #else
169 #define CRASH() do { \
170 *(int *)(uintptr_t)0xbbadbeef = 0; \
171 ((void(*)())0)(); /* More reliable, but doesn't say BBADBEEF */ \
172 } while(false)
173 #endif
174 #endif
175
176 /* ASSERT, ASSERT_NOT_REACHED, ASSERT_UNUSED */
177
178 #if OS(WINCE) && !PLATFORM(TORCHMOBILE)
179 /* FIXME: We include this here only to avoid a conflict with the ASSERT macro. */
180 #include <windows.h>
181 #undef min
182 #undef max
183 #undef ERROR
184 #endif
185
186 #if OS(WINDOWS) || OS(SYMBIAN)
187 /* FIXME: Change to use something other than ASSERT to avoid this conflict with the underlying platform */
188 #undef ASSERT
189 #endif
190
191 #if ASSERT_DISABLED
192
193 #define ASSERT(assertion) ((void)0)
194 #define ASSERT_NOT_REACHED() ((void)0)
195 #define ASSERT_UNUSED(variable, assertion) ((void)variable)
196
197 #else
198
199 #define ASSERT(assertion) do \
200 if (!(assertion)) { \
201 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion); \
202 CRASH(); \
203 } \
204 while (0)
205
206 #define ASSERT_NOT_REACHED() do { \
207 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 0); \
208 CRASH(); \
209 } while (0)
210
211 #define ASSERT_UNUSED(variable, assertion) ASSERT(assertion)
212
213 #endif
214
215 /* ASSERT_WITH_MESSAGE */
216
217 #if COMPILER(MSVC7)
218 #define ASSERT_WITH_MESSAGE(assertion) ((void)0)
219 #elif COMPILER(WINSCW)
220 #define ASSERT_WITH_MESSAGE(assertion, arg...) ((void)0)
221 #elif ASSERT_MSG_DISABLED
222 #define ASSERT_WITH_MESSAGE(assertion, ...) ((void)0)
223 #else
224 #define ASSERT_WITH_MESSAGE(assertion, ...) do \
225 if (!(assertion)) { \
226 WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
227 CRASH(); \
228 } \
229 while (0)
230 #endif
231
232
233 /* ASSERT_ARG */
234
235 #if ASSERT_ARG_DISABLED
236
237 #define ASSERT_ARG(argName, assertion) ((void)0)
238
239 #else
240
241 #define ASSERT_ARG(argName, assertion) do \
242 if (!(assertion)) { \
243 WTFReportArgumentAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #argName, #assertion); \
244 CRASH(); \
245 } \
246 while (0)
247
248 #endif
249
250 /* COMPILE_ASSERT */
251 #ifndef COMPILE_ASSERT
252 #define COMPILE_ASSERT(exp, name) typedef int dummy##name [(exp) ? 1 : -1]
253 #endif
254
255 /* FATAL */
256
257 #if COMPILER(MSVC7)
258 #define FATAL() ((void)0)
259 #elif COMPILER(WINSCW)
260 #define FATAL(arg...) ((void)0)
261 #elif FATAL_DISABLED
262 #define FATAL(...) ((void)0)
263 #else
264 #define FATAL(...) do { \
265 WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__); \
266 CRASH(); \
267 } while (0)
268 #endif
269
270 /* LOG_ERROR */
271
272 #if COMPILER(MSVC7)
273 #define LOG_ERROR() ((void)0)
274 #elif COMPILER(WINSCW)
275 #define LOG_ERROR(arg...) ((void)0)
276 #elif ERROR_DISABLED
277 #define LOG_ERROR(...) ((void)0)
278 #else
279 #define LOG_ERROR(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__)
280 #endif
281
282 /* LOG */
283
284 #if COMPILER(MSVC7)
285 #define LOG() ((void)0)
286 #elif COMPILER(WINSCW)
287 #define LOG(arg...) ((void)0)
288 #elif LOG_DISABLED
289 #define LOG(channel, ...) ((void)0)
290 #else
291 #define LOG(channel, ...) WTFLog(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
292 #define JOIN_LOG_CHANNEL_WITH_PREFIX(prefix, channel) JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel)
293 #define JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel) prefix ## channel
294 #endif
295
296 /* LOG_VERBOSE */
297
298 #if COMPILER(MSVC7)
299 #define LOG_VERBOSE(channel) ((void)0)
300 #elif COMPILER(WINSCW)
301 #define LOG_VERBOSE(channel, arg...) ((void)0)
302 #elif LOG_DISABLED
303 #define LOG_VERBOSE(channel, ...) ((void)0)
304 #else
305 #define LOG_VERBOSE(channel, ...) WTFLogVerbose(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, &JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
306 #endif
307
308 #endif /* WTF_Assertions_h */