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