]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/Assertions.h
f6187bbe359b0cdd531d7434d6e8fc8256caa537
2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef WTF_Assertions_h
27 #define WTF_Assertions_h
30 no namespaces because this file has to be includable from C and Objective-C
32 Note, this file uses many GCC extensions, but it should be compatible with
33 C, Objective C, C++, and Objective C++.
35 For non-debug builds, everything is disabled by default.
36 Defining any of the symbols explicitly prevents this from having any effect.
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
42 http://msdn2.microsoft.com/en-us/library/ms177415(VS.80).aspx
66 /* Disable ASSERT* macros in release mode. */
67 #define ASSERTIONS_DISABLED_DEFAULT 1
69 #define ASSERTIONS_DISABLED_DEFAULT 0
72 #if COMPILER(MSVC7_OR_LOWER) || COMPILER(WINSCW)
73 #define HAVE_VARIADIC_MACRO 0
75 #define HAVE_VARIADIC_MACRO 1
78 #ifndef BACKTRACE_DISABLED
79 #define BACKTRACE_DISABLED ASSERTIONS_DISABLED_DEFAULT
82 #ifndef ASSERT_DISABLED
83 #define ASSERT_DISABLED ASSERTIONS_DISABLED_DEFAULT
86 #ifndef ASSERT_MSG_DISABLED
87 #if HAVE(VARIADIC_MACRO)
88 #define ASSERT_MSG_DISABLED ASSERTIONS_DISABLED_DEFAULT
90 #define ASSERT_MSG_DISABLED 1
94 #ifndef ASSERT_ARG_DISABLED
95 #define ASSERT_ARG_DISABLED ASSERTIONS_DISABLED_DEFAULT
98 #ifndef FATAL_DISABLED
99 #if HAVE(VARIADIC_MACRO)
100 #define FATAL_DISABLED ASSERTIONS_DISABLED_DEFAULT
102 #define FATAL_DISABLED 1
106 #ifndef ERROR_DISABLED
107 #if HAVE(VARIADIC_MACRO)
108 #define ERROR_DISABLED ASSERTIONS_DISABLED_DEFAULT
110 #define ERROR_DISABLED 1
115 #if HAVE(VARIADIC_MACRO)
116 #define LOG_DISABLED ASSERTIONS_DISABLED_DEFAULT
118 #define LOG_DISABLED 1
123 #define WTF_PRETTY_FUNCTION __PRETTY_FUNCTION__
125 #define WTF_PRETTY_FUNCTION __FUNCTION__
128 /* WTF logging functions can process %@ in the format string to log a NSObject* but the printf format attribute
129 emits a warning when %@ is used in the format string. Until <rdar://problem/5195437> is resolved we can't include
130 the attribute when being used from Objective-C code in case it decides to use %@. */
131 #if COMPILER(GCC) && !defined(__OBJC__)
132 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments) __attribute__((__format__(printf, formatStringArgument, extraArguments)))
134 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments)
137 /* This macro is needed to prevent the clang static analyzer from generating false-positive reports in ASSERT() macros. */
139 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
141 #define CLANG_ANALYZER_NORETURN
143 /* For project uses WTF but has no config.h, we need to explicitly set the export defines here. */
144 #ifndef WTF_EXPORT_PRIVATE
145 #define WTF_EXPORT_PRIVATE
148 /* These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. */
154 typedef enum { WTFLogChannelOff
, WTFLogChannelOn
} WTFLogChannelState
;
158 const char *defaultName
;
159 WTFLogChannelState state
;
162 WTF_EXPORT_PRIVATE
void WTFReportAssertionFailure(const char* file
, int line
, const char* function
, const char* assertion
) CLANG_ANALYZER_NORETURN
;
163 WTF_EXPORT_PRIVATE
void WTFReportAssertionFailureWithMessage(const char* file
, int line
, const char* function
, const char* assertion
, const char* format
, ...) CLANG_ANALYZER_NORETURN
WTF_ATTRIBUTE_PRINTF(5, 6);
164 WTF_EXPORT_PRIVATE
void WTFReportArgumentAssertionFailure(const char* file
, int line
, const char* function
, const char* argName
, const char* assertion
) CLANG_ANALYZER_NORETURN
;
165 WTF_EXPORT_PRIVATE
void WTFReportBacktrace();
166 WTF_EXPORT_PRIVATE
void WTFReportFatalError(const char* file
, int line
, const char* function
, const char* format
, ...) CLANG_ANALYZER_NORETURN
WTF_ATTRIBUTE_PRINTF(4, 5);
167 WTF_EXPORT_PRIVATE
void WTFReportError(const char* file
, int line
, const char* function
, const char* format
, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
168 WTF_EXPORT_PRIVATE
void WTFLog(WTFLogChannel
* channel
, const char* format
, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
169 WTF_EXPORT_PRIVATE
void WTFLogVerbose(const char* file
, int line
, const char* function
, WTFLogChannel
* channel
, const char* format
, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
175 /* CRASH() - Raises a fatal error resulting in program termination and triggering either the debugger or the crash reporter.
177 Use CRASH() in response to known, unrecoverable errors like out-of-memory.
178 Macro is enabled in both debug and release mode.
179 To test for unknown errors and verify assumptions, use ASSERT instead, to avoid impacting performance in release builds.
181 Signals are ignored by the crash reporter on OS X so we must do better.
185 #define CRASH() do { \
187 User::Panic(_L("Webkit CRASH"),0); \
189 #elif PLATFORM(BREWMP)
190 #define CRASH() do { \
191 dbg_Message("WebKit CRASH", DBG_MSG_LEVEL_FATAL, __FILE__, __LINE__); \
192 *(int *)(uintptr_t)0xbbadbeef = 0; \
193 ((void(*)())0)(); /* More reliable, but doesn't say BBADBEEF */ \
196 #define CRASH() do { \
197 WTFReportBacktrace(); \
198 *(int *)(uintptr_t)0xbbadbeef = 0; \
199 ((void(*)())0)(); /* More reliable, but doesn't say BBADBEEF */ \
206 Print a backtrace to the same location as ASSERT messages.
209 #if BACKTRACE_DISABLED
211 #define BACKTRACE() ((void)0)
215 #define BACKTRACE() do { \
216 WTFReportBacktrace(); \
221 /* ASSERT, ASSERT_NOT_REACHED, ASSERT_UNUSED
223 These macros are compiled out of release builds.
224 Expressions inside them are evaluated in debug builds only.
227 #if OS(WINCE) && !PLATFORM(TORCHMOBILE)
228 /* FIXME: We include this here only to avoid a conflict with the ASSERT macro. */
235 #if OS(WINDOWS) || OS(SYMBIAN)
236 /* FIXME: Change to use something other than ASSERT to avoid this conflict with the underlying platform */
241 /* FIXME: We include this here only to avoid a conflict with the COMPILE_ASSERT macro. */
242 #include <AEEClassIDs.h>
244 /* FIXME: Change to use something other than COMPILE_ASSERT to avoid this conflict with the underlying platform */
245 #undef COMPILE_ASSERT
250 #define ASSERT(assertion) ((void)0)
251 #define ASSERT_NOT_REACHED() ((void)0)
253 #if COMPILER(INTEL) && !OS(WINDOWS) || COMPILER(RVCT)
255 inline void assertUnused(T
& x
) { (void)x
; }
256 #define ASSERT_UNUSED(variable, assertion) (assertUnused(variable))
258 #define ASSERT_UNUSED(variable, assertion) ((void)variable)
263 #define ASSERT(assertion) do \
264 if (!(assertion)) { \
265 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion); \
270 #define ASSERT_NOT_REACHED() do { \
271 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 0); \
275 #define ASSERT_UNUSED(variable, assertion) ASSERT(assertion)
279 /* ASSERT_WITH_MESSAGE */
281 #if COMPILER(MSVC7_OR_LOWER)
282 #define ASSERT_WITH_MESSAGE(assertion) ((void)0)
283 #elif COMPILER(WINSCW)
284 #define ASSERT_WITH_MESSAGE(assertion, arg...) ((void)0)
285 #elif ASSERT_MSG_DISABLED
286 #define ASSERT_WITH_MESSAGE(assertion, ...) ((void)0)
288 #define ASSERT_WITH_MESSAGE(assertion, ...) do \
289 if (!(assertion)) { \
290 WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
296 /* ASSERT_WITH_MESSAGE_UNUSED */
298 #if COMPILER(MSVC7_OR_LOWER)
299 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion) ((void)0)
300 #elif COMPILER(WINSCW)
301 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, arg...) ((void)0)
302 #elif ASSERT_MSG_DISABLED
303 #if COMPILER(INTEL) && !OS(WINDOWS) || COMPILER(RVCT)
305 inline void assertWithMessageUnused(T
& x
) { (void)x
; }
306 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) (assertWithMessageUnused(variable))
308 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) ((void)variable)
311 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) do \
312 if (!(assertion)) { \
313 WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
322 #if ASSERT_ARG_DISABLED
324 #define ASSERT_ARG(argName, assertion) ((void)0)
328 #define ASSERT_ARG(argName, assertion) do \
329 if (!(assertion)) { \
330 WTFReportArgumentAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #argName, #assertion); \
338 #ifndef COMPILE_ASSERT
339 #define COMPILE_ASSERT(exp, name) typedef int dummy##name [(exp) ? 1 : -1]
344 #if COMPILER(MSVC7_OR_LOWER)
345 #define FATAL() ((void)0)
346 #elif COMPILER(WINSCW)
347 #define FATAL(arg...) ((void)0)
349 #define FATAL(...) ((void)0)
351 #define FATAL(...) do { \
352 WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__); \
359 #if COMPILER(MSVC7_OR_LOWER)
360 #define LOG_ERROR() ((void)0)
361 #elif COMPILER(WINSCW)
362 #define LOG_ERROR(arg...) ((void)0)
364 #define LOG_ERROR(...) ((void)0)
366 #define LOG_ERROR(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__)
371 #if COMPILER(MSVC7_OR_LOWER)
372 #define LOG() ((void)0)
373 #elif COMPILER(WINSCW)
374 #define LOG(arg...) ((void)0)
376 #define LOG(channel, ...) ((void)0)
378 #define LOG(channel, ...) WTFLog(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
379 #define JOIN_LOG_CHANNEL_WITH_PREFIX(prefix, channel) JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel)
380 #define JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel) prefix ## channel
385 #if COMPILER(MSVC7_OR_LOWER)
386 #define LOG_VERBOSE(channel) ((void)0)
387 #elif COMPILER(WINSCW)
388 #define LOG_VERBOSE(channel, arg...) ((void)0)
390 #define LOG_VERBOSE(channel, ...) ((void)0)
392 #define LOG_VERBOSE(channel, ...) WTFLogVerbose(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, &JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
395 #if ENABLE(GC_VALIDATION)
396 #define ASSERT_GC_OBJECT_LOOKS_VALID(cell) do { \
399 if (cell->unvalidatedStructure()->unvalidatedStructure() != cell->unvalidatedStructure()->unvalidatedStructure()->unvalidatedStructure())\
403 #define ASSERT_GC_OBJECT_INHERITS(object, classInfo) do {\
404 ASSERT_GC_OBJECT_LOOKS_VALID(object); \
405 if (!object->inherits(classInfo)) \
410 #define ASSERT_GC_OBJECT_LOOKS_VALID(cell) do { (void)cell; } while (0)
411 #define ASSERT_GC_OBJECT_INHERITS(object, classInfo) do { (void)object; (void)classInfo; } while (0)
414 #endif /* WTF_Assertions_h */