]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/Assertions.h
JavaScriptCore-721.26.tar.gz
[apple/javascriptcore.git] / wtf / Assertions.h
CommitLineData
b37bf2e1
A
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
ba379fdc
A
47#include <stdbool.h>
48
b37bf2e1
A
49#if COMPILER(MSVC)
50#include <stddef.h>
51#else
52#include <inttypes.h>
53#endif
54
f9bf01c6
A
55#if OS(SYMBIAN)
56#include <e32def.h>
57#include <e32debug.h>
58#endif
59
b37bf2e1 60#ifdef NDEBUG
4e4e5a6f 61/* Disable ASSERT* macros in release mode. */
b37bf2e1
A
62#define ASSERTIONS_DISABLED_DEFAULT 1
63#else
64#define ASSERTIONS_DISABLED_DEFAULT 0
65#endif
66
4e4e5a6f 67#if COMPILER(MSVC7_OR_LOWER) || COMPILER(WINSCW)
f9bf01c6
A
68#define HAVE_VARIADIC_MACRO 0
69#else
70#define HAVE_VARIADIC_MACRO 1
71#endif
72
b37bf2e1
A
73#ifndef ASSERT_DISABLED
74#define ASSERT_DISABLED ASSERTIONS_DISABLED_DEFAULT
75#endif
76
f9bf01c6
A
77#ifndef ASSERT_MSG_DISABLED
78#if HAVE(VARIADIC_MACRO)
79#define ASSERT_MSG_DISABLED ASSERTIONS_DISABLED_DEFAULT
80#else
81#define ASSERT_MSG_DISABLED 1
82#endif
83#endif
84
b37bf2e1
A
85#ifndef ASSERT_ARG_DISABLED
86#define ASSERT_ARG_DISABLED ASSERTIONS_DISABLED_DEFAULT
87#endif
88
89#ifndef FATAL_DISABLED
f9bf01c6 90#if HAVE(VARIADIC_MACRO)
b37bf2e1 91#define FATAL_DISABLED ASSERTIONS_DISABLED_DEFAULT
f9bf01c6
A
92#else
93#define FATAL_DISABLED 1
94#endif
b37bf2e1
A
95#endif
96
97#ifndef ERROR_DISABLED
f9bf01c6 98#if HAVE(VARIADIC_MACRO)
b37bf2e1 99#define ERROR_DISABLED ASSERTIONS_DISABLED_DEFAULT
f9bf01c6
A
100#else
101#define ERROR_DISABLED 1
102#endif
b37bf2e1
A
103#endif
104
105#ifndef LOG_DISABLED
f9bf01c6 106#if HAVE(VARIADIC_MACRO)
b37bf2e1 107#define LOG_DISABLED ASSERTIONS_DISABLED_DEFAULT
f9bf01c6
A
108#else
109#define LOG_DISABLED 1
110#endif
b37bf2e1
A
111#endif
112
113#if COMPILER(GCC)
114#define WTF_PRETTY_FUNCTION __PRETTY_FUNCTION__
115#else
116#define WTF_PRETTY_FUNCTION __FUNCTION__
117#endif
118
9dae56ea
A
119/* WTF logging functions can process %@ in the format string to log a NSObject* but the printf format attribute
120 emits a warning when %@ is used in the format string. Until <rdar://problem/5195437> is resolved we can't include
121 the attribute when being used from Objective-C code in case it decides to use %@. */
b37bf2e1
A
122#if COMPILER(GCC) && !defined(__OBJC__)
123#define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments) __attribute__((__format__(printf, formatStringArgument, extraArguments)))
124#else
125#define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments)
126#endif
127
ba379fdc
A
128/* This macro is needed to prevent the clang static analyzer from generating false-positive reports in ASSERT() macros. */
129#ifdef __clang__
130#define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
131#else
132#define CLANG_ANALYZER_NORETURN
133#endif
134
b37bf2e1
A
135/* These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. */
136
137#ifdef __cplusplus
138extern "C" {
139#endif
140
141typedef enum { WTFLogChannelOff, WTFLogChannelOn } WTFLogChannelState;
142
143typedef struct {
144 unsigned mask;
145 const char *defaultName;
146 WTFLogChannelState state;
147} WTFLogChannel;
148
ba379fdc
A
149void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion) CLANG_ANALYZER_NORETURN;
150void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) CLANG_ANALYZER_NORETURN WTF_ATTRIBUTE_PRINTF(5, 6);
151void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion) CLANG_ANALYZER_NORETURN;
152void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) CLANG_ANALYZER_NORETURN WTF_ATTRIBUTE_PRINTF(4, 5);
b37bf2e1
A
153void WTFReportError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
154void WTFLog(WTFLogChannel* channel, const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
155void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
156
157#ifdef __cplusplus
158}
159#endif
160
4e4e5a6f 161/* CRASH() - Raises a fatal error resulting in program termination and triggering either the debugger or the crash reporter.
b37bf2e1 162
4e4e5a6f
A
163 Use CRASH() in response to known, unrecoverable errors like out-of-memory.
164 Macro is enabled in both debug and release mode.
165 To test for unknown errors and verify assumptions, use ASSERT instead, to avoid impacting performance in release builds.
166
167 Signals are ignored by the crash reporter on OS X so we must do better.
168*/
b37bf2e1 169#ifndef CRASH
f9bf01c6
A
170#if OS(SYMBIAN)
171#define CRASH() do { \
172 __DEBUGGER(); \
173 User::Panic(_L("Webkit CRASH"),0); \
174 } while(false)
175#else
9dae56ea
A
176#define CRASH() do { \
177 *(int *)(uintptr_t)0xbbadbeef = 0; \
178 ((void(*)())0)(); /* More reliable, but doesn't say BBADBEEF */ \
179} while(false)
b37bf2e1 180#endif
f9bf01c6 181#endif
b37bf2e1 182
4e4e5a6f
A
183/* ASSERT, ASSERT_NOT_REACHED, ASSERT_UNUSED
184
185 These macros are compiled out of release builds.
186 Expressions inside them are evaluated in debug builds only.
187*/
b37bf2e1 188
f9bf01c6 189#if OS(WINCE) && !PLATFORM(TORCHMOBILE)
9dae56ea
A
190/* FIXME: We include this here only to avoid a conflict with the ASSERT macro. */
191#include <windows.h>
192#undef min
193#undef max
194#undef ERROR
195#endif
196
f9bf01c6
A
197#if OS(WINDOWS) || OS(SYMBIAN)
198/* FIXME: Change to use something other than ASSERT to avoid this conflict with the underlying platform */
b37bf2e1
A
199#undef ASSERT
200#endif
201
4e4e5a6f
A
202#if PLATFORM(BREWMP)
203/* FIXME: We include this here only to avoid a conflict with the COMPILE_ASSERT macro. */
204#include <AEEClassIDs.h>
205
206/* FIXME: Change to use something other than COMPILE_ASSERT to avoid this conflict with the underlying platform */
207#undef COMPILE_ASSERT
208#endif
209
b37bf2e1
A
210#if ASSERT_DISABLED
211
212#define ASSERT(assertion) ((void)0)
b37bf2e1 213#define ASSERT_NOT_REACHED() ((void)0)
9dae56ea 214#define ASSERT_UNUSED(variable, assertion) ((void)variable)
b37bf2e1
A
215
216#else
217
218#define ASSERT(assertion) do \
219 if (!(assertion)) { \
220 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion); \
221 CRASH(); \
222 } \
223while (0)
f9bf01c6
A
224
225#define ASSERT_NOT_REACHED() do { \
226 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 0); \
227 CRASH(); \
228} while (0)
229
230#define ASSERT_UNUSED(variable, assertion) ASSERT(assertion)
231
232#endif
233
234/* ASSERT_WITH_MESSAGE */
235
4e4e5a6f 236#if COMPILER(MSVC7_OR_LOWER)
b37bf2e1 237#define ASSERT_WITH_MESSAGE(assertion) ((void)0)
f9bf01c6
A
238#elif COMPILER(WINSCW)
239#define ASSERT_WITH_MESSAGE(assertion, arg...) ((void)0)
240#elif ASSERT_MSG_DISABLED
241#define ASSERT_WITH_MESSAGE(assertion, ...) ((void)0)
b37bf2e1
A
242#else
243#define ASSERT_WITH_MESSAGE(assertion, ...) do \
244 if (!(assertion)) { \
245 WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
246 CRASH(); \
247 } \
248while (0)
b37bf2e1 249#endif
f9bf01c6
A
250
251
b37bf2e1
A
252/* ASSERT_ARG */
253
254#if ASSERT_ARG_DISABLED
255
256#define ASSERT_ARG(argName, assertion) ((void)0)
257
258#else
259
260#define ASSERT_ARG(argName, assertion) do \
261 if (!(assertion)) { \
262 WTFReportArgumentAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #argName, #assertion); \
263 CRASH(); \
264 } \
265while (0)
266
267#endif
268
269/* COMPILE_ASSERT */
270#ifndef COMPILE_ASSERT
ba379fdc 271#define COMPILE_ASSERT(exp, name) typedef int dummy##name [(exp) ? 1 : -1]
b37bf2e1
A
272#endif
273
274/* FATAL */
275
4e4e5a6f 276#if COMPILER(MSVC7_OR_LOWER)
b37bf2e1 277#define FATAL() ((void)0)
f9bf01c6
A
278#elif COMPILER(WINSCW)
279#define FATAL(arg...) ((void)0)
280#elif FATAL_DISABLED
281#define FATAL(...) ((void)0)
b37bf2e1
A
282#else
283#define FATAL(...) do { \
284 WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__); \
285 CRASH(); \
286} while (0)
287#endif
288
289/* LOG_ERROR */
290
4e4e5a6f 291#if COMPILER(MSVC7_OR_LOWER)
b37bf2e1 292#define LOG_ERROR() ((void)0)
f9bf01c6
A
293#elif COMPILER(WINSCW)
294#define LOG_ERROR(arg...) ((void)0)
295#elif ERROR_DISABLED
296#define LOG_ERROR(...) ((void)0)
b37bf2e1
A
297#else
298#define LOG_ERROR(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__)
299#endif
300
301/* LOG */
302
4e4e5a6f 303#if COMPILER(MSVC7_OR_LOWER)
b37bf2e1 304#define LOG() ((void)0)
f9bf01c6
A
305#elif COMPILER(WINSCW)
306#define LOG(arg...) ((void)0)
307#elif LOG_DISABLED
308#define LOG(channel, ...) ((void)0)
b37bf2e1
A
309#else
310#define LOG(channel, ...) WTFLog(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
311#define JOIN_LOG_CHANNEL_WITH_PREFIX(prefix, channel) JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel)
312#define JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel) prefix ## channel
313#endif
314
315/* LOG_VERBOSE */
316
4e4e5a6f 317#if COMPILER(MSVC7_OR_LOWER)
b37bf2e1 318#define LOG_VERBOSE(channel) ((void)0)
f9bf01c6
A
319#elif COMPILER(WINSCW)
320#define LOG_VERBOSE(channel, arg...) ((void)0)
321#elif LOG_DISABLED
322#define LOG_VERBOSE(channel, ...) ((void)0)
b37bf2e1
A
323#else
324#define LOG_VERBOSE(channel, ...) WTFLogVerbose(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, &JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
325#endif
326
9dae56ea 327#endif /* WTF_Assertions_h */