]> git.saurik.com Git - apple/javascriptcore.git/blob - wtf/Assertions.h
JavaScriptCore-554.1.tar.gz
[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 #ifdef NDEBUG
56 #define ASSERTIONS_DISABLED_DEFAULT 1
57 #else
58 #define ASSERTIONS_DISABLED_DEFAULT 0
59 #endif
60
61 #ifndef ASSERT_DISABLED
62 #define ASSERT_DISABLED ASSERTIONS_DISABLED_DEFAULT
63 #endif
64
65 #ifndef ASSERT_ARG_DISABLED
66 #define ASSERT_ARG_DISABLED ASSERTIONS_DISABLED_DEFAULT
67 #endif
68
69 #ifndef FATAL_DISABLED
70 #define FATAL_DISABLED ASSERTIONS_DISABLED_DEFAULT
71 #endif
72
73 #ifndef ERROR_DISABLED
74 #define ERROR_DISABLED ASSERTIONS_DISABLED_DEFAULT
75 #endif
76
77 #ifndef LOG_DISABLED
78 #define LOG_DISABLED ASSERTIONS_DISABLED_DEFAULT
79 #endif
80
81 #if COMPILER(GCC)
82 #define WTF_PRETTY_FUNCTION __PRETTY_FUNCTION__
83 #else
84 #define WTF_PRETTY_FUNCTION __FUNCTION__
85 #endif
86
87 /* WTF logging functions can process %@ in the format string to log a NSObject* but the printf format attribute
88 emits a warning when %@ is used in the format string. Until <rdar://problem/5195437> is resolved we can't include
89 the attribute when being used from Objective-C code in case it decides to use %@. */
90 #if COMPILER(GCC) && !defined(__OBJC__)
91 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments) __attribute__((__format__(printf, formatStringArgument, extraArguments)))
92 #else
93 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments)
94 #endif
95
96 /* This macro is needed to prevent the clang static analyzer from generating false-positive reports in ASSERT() macros. */
97 #ifdef __clang__
98 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
99 #else
100 #define CLANG_ANALYZER_NORETURN
101 #endif
102
103 /* These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. */
104
105 #ifdef __cplusplus
106 extern "C" {
107 #endif
108
109 typedef enum { WTFLogChannelOff, WTFLogChannelOn } WTFLogChannelState;
110
111 typedef struct {
112 unsigned mask;
113 const char *defaultName;
114 WTFLogChannelState state;
115 } WTFLogChannel;
116
117 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion) CLANG_ANALYZER_NORETURN;
118 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) CLANG_ANALYZER_NORETURN WTF_ATTRIBUTE_PRINTF(5, 6);
119 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion) CLANG_ANALYZER_NORETURN;
120 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) CLANG_ANALYZER_NORETURN WTF_ATTRIBUTE_PRINTF(4, 5);
121 void WTFReportError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
122 void WTFLog(WTFLogChannel* channel, const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
123 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
124
125 #ifdef __cplusplus
126 }
127 #endif
128
129 /* CRASH -- gets us into the debugger or the crash reporter -- signals are ignored by the crash reporter so we must do better */
130
131 #ifndef CRASH
132 #define CRASH() do { \
133 *(int *)(uintptr_t)0xbbadbeef = 0; \
134 ((void(*)())0)(); /* More reliable, but doesn't say BBADBEEF */ \
135 } while(false)
136 #endif
137
138 /* ASSERT, ASSERT_WITH_MESSAGE, ASSERT_NOT_REACHED */
139
140 #if PLATFORM(WINCE) && !PLATFORM(TORCHMOBILE)
141 /* FIXME: We include this here only to avoid a conflict with the ASSERT macro. */
142 #include <windows.h>
143 #undef min
144 #undef max
145 #undef ERROR
146 #endif
147
148 #if PLATFORM(WIN_OS)
149 /* FIXME: Change to use something other than ASSERT to avoid this conflict with win32. */
150 #undef ASSERT
151 #endif
152
153 #if ASSERT_DISABLED
154
155 #define ASSERT(assertion) ((void)0)
156 #define ASSERT_WITH_MESSAGE(assertion, ...) ((void)0)
157 #define ASSERT_NOT_REACHED() ((void)0)
158 #define ASSERT_UNUSED(variable, assertion) ((void)variable)
159
160 #else
161
162 #define ASSERT(assertion) do \
163 if (!(assertion)) { \
164 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion); \
165 CRASH(); \
166 } \
167 while (0)
168 #if COMPILER(MSVC7)
169 #define ASSERT_WITH_MESSAGE(assertion) ((void)0)
170 #else
171 #define ASSERT_WITH_MESSAGE(assertion, ...) do \
172 if (!(assertion)) { \
173 WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
174 CRASH(); \
175 } \
176 while (0)
177 #endif /* COMPILER(MSVC7) */
178 #define ASSERT_NOT_REACHED() do { \
179 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 0); \
180 CRASH(); \
181 } while (0)
182
183 #define ASSERT_UNUSED(variable, assertion) ASSERT(assertion)
184
185 #endif
186
187 /* ASSERT_ARG */
188
189 #if ASSERT_ARG_DISABLED
190
191 #define ASSERT_ARG(argName, assertion) ((void)0)
192
193 #else
194
195 #define ASSERT_ARG(argName, assertion) do \
196 if (!(assertion)) { \
197 WTFReportArgumentAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #argName, #assertion); \
198 CRASH(); \
199 } \
200 while (0)
201
202 #endif
203
204 /* COMPILE_ASSERT */
205 #ifndef COMPILE_ASSERT
206 #define COMPILE_ASSERT(exp, name) typedef int dummy##name [(exp) ? 1 : -1]
207 #endif
208
209 /* FATAL */
210
211 #if FATAL_DISABLED
212 #define FATAL(...) ((void)0)
213 #elif COMPILER(MSVC7)
214 #define FATAL() ((void)0)
215 #else
216 #define FATAL(...) do { \
217 WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__); \
218 CRASH(); \
219 } while (0)
220 #endif
221
222 /* LOG_ERROR */
223
224 #if ERROR_DISABLED
225 #define LOG_ERROR(...) ((void)0)
226 #elif COMPILER(MSVC7)
227 #define LOG_ERROR() ((void)0)
228 #else
229 #define LOG_ERROR(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__)
230 #endif
231
232 /* LOG */
233
234 #if LOG_DISABLED
235 #define LOG(channel, ...) ((void)0)
236 #elif COMPILER(MSVC7)
237 #define LOG() ((void)0)
238 #else
239 #define LOG(channel, ...) WTFLog(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
240 #define JOIN_LOG_CHANNEL_WITH_PREFIX(prefix, channel) JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel)
241 #define JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel) prefix ## channel
242 #endif
243
244 /* LOG_VERBOSE */
245
246 #if LOG_DISABLED
247 #define LOG_VERBOSE(channel, ...) ((void)0)
248 #elif COMPILER(MSVC7)
249 #define LOG_VERBOSE(channel) ((void)0)
250 #else
251 #define LOG_VERBOSE(channel, ...) WTFLogVerbose(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, &JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
252 #endif
253
254 #endif /* WTF_Assertions_h */