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
45 #include <wtf/Platform.h>
54 /* Disable ASSERT* macros in release mode. */
55 #define ASSERTIONS_DISABLED_DEFAULT 1
57 #define ASSERTIONS_DISABLED_DEFAULT 0
60 #if COMPILER(MSVC7_OR_LOWER)
61 #define HAVE_VARIADIC_MACRO 0
63 #define HAVE_VARIADIC_MACRO 1
66 #ifndef BACKTRACE_DISABLED
67 #define BACKTRACE_DISABLED ASSERTIONS_DISABLED_DEFAULT
70 #ifndef ASSERT_DISABLED
71 #define ASSERT_DISABLED ASSERTIONS_DISABLED_DEFAULT
74 #ifndef ASSERT_MSG_DISABLED
75 #if HAVE(VARIADIC_MACRO)
76 #define ASSERT_MSG_DISABLED ASSERTIONS_DISABLED_DEFAULT
78 #define ASSERT_MSG_DISABLED 1
82 #ifndef ASSERT_ARG_DISABLED
83 #define ASSERT_ARG_DISABLED ASSERTIONS_DISABLED_DEFAULT
86 #ifndef FATAL_DISABLED
87 #if HAVE(VARIADIC_MACRO)
88 #define FATAL_DISABLED ASSERTIONS_DISABLED_DEFAULT
90 #define FATAL_DISABLED 1
94 #ifndef ERROR_DISABLED
95 #if HAVE(VARIADIC_MACRO)
96 #define ERROR_DISABLED ASSERTIONS_DISABLED_DEFAULT
98 #define ERROR_DISABLED 1
103 #if HAVE(VARIADIC_MACRO)
104 #define LOG_DISABLED ASSERTIONS_DISABLED_DEFAULT
106 #define LOG_DISABLED 1
111 #define WTF_PRETTY_FUNCTION __PRETTY_FUNCTION__
113 #define WTF_PRETTY_FUNCTION __FUNCTION__
116 /* WTF logging functions can process %@ in the format string to log a NSObject* but the printf format attribute
117 emits a warning when %@ is used in the format string. Until <rdar://problem/5195437> is resolved we can't include
118 the attribute when being used from Objective-C code in case it decides to use %@. */
119 #if COMPILER(GCC) && !defined(__OBJC__)
120 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments) __attribute__((__format__(printf, formatStringArgument, extraArguments)))
122 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments)
125 /* These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. */
131 typedef enum { WTFLogChannelOff, WTFLogChannelOn } WTFLogChannelState;
135 const char *defaultName;
136 WTFLogChannelState state;
139 WTF_EXPORT_PRIVATE void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion);
140 WTF_EXPORT_PRIVATE void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
141 WTF_EXPORT_PRIVATE void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion);
142 WTF_EXPORT_PRIVATE void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
143 WTF_EXPORT_PRIVATE void WTFReportError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
144 WTF_EXPORT_PRIVATE void WTFLog(WTFLogChannel*, const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
145 WTF_EXPORT_PRIVATE void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel*, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
146 WTF_EXPORT_PRIVATE void WTFLogAlways(const char* format, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
148 WTF_EXPORT_PRIVATE void WTFGetBacktrace(void** stack, int* size);
149 WTF_EXPORT_PRIVATE void WTFReportBacktrace();
150 WTF_EXPORT_PRIVATE void WTFPrintBacktrace(void** stack, int size);
152 typedef void (*WTFCrashHookFunction)();
153 WTF_EXPORT_PRIVATE void WTFSetCrashHook(WTFCrashHookFunction);
154 WTF_EXPORT_PRIVATE void WTFInvokeCrashHook();
156 #if ENABLE(TIZEN_DLOG_SUPPORT)
158 TIZEN_DLOG_PRIORITY_ERROR,
159 TIZEN_DLOG_PRIORITY_INFO
162 WTF_EXPORT_PRIVATE void TizenLog(TizenDlogPriority priority, bool secureLog, int line, const char* function, const char* format, ...);
169 /* CRASH() - Raises a fatal error resulting in program termination and triggering either the debugger or the crash reporter.
171 Use CRASH() in response to known, unrecoverable errors like out-of-memory.
172 Macro is enabled in both debug and release mode.
173 To test for unknown errors and verify assumptions, use ASSERT instead, to avoid impacting performance in release builds.
175 Signals are ignored by the crash reporter on OS X so we must do better.
180 (WTFReportBacktrace(), \
181 WTFInvokeCrashHook(), \
182 (*(int *)(uintptr_t)0xbbadbeef = 0), \
186 (WTFReportBacktrace(), \
187 WTFInvokeCrashHook(), \
188 (*(int *)(uintptr_t)0xbbadbeef = 0), \
189 ((void(*)())0)() /* More reliable, but doesn't say BBADBEEF */ \
195 #define NO_RETURN_DUE_TO_CRASH NO_RETURN
197 #define NO_RETURN_DUE_TO_CRASH
203 Print a backtrace to the same location as ASSERT messages.
206 #if BACKTRACE_DISABLED
208 #define BACKTRACE() ((void)0)
212 #define BACKTRACE() do { \
213 WTFReportBacktrace(); \
218 /* ASSERT, ASSERT_NOT_REACHED, ASSERT_UNUSED
220 These macros are compiled out of release builds.
221 Expressions inside them are evaluated in debug builds only.
224 #if OS(WINCE) && !PLATFORM(TORCHMOBILE)
225 /* FIXME: We include this here only to avoid a conflict with the ASSERT macro. */
233 /* FIXME: Change to use something other than ASSERT to avoid this conflict with the underlying platform */
239 #define ASSERT(assertion) ((void)0)
240 #define ASSERT_AT(assertion, file, line, function) ((void)0)
241 #define ASSERT_NOT_REACHED() ((void)0)
242 #define NO_RETURN_DUE_TO_ASSERT
244 #if COMPILER(INTEL) && !OS(WINDOWS) || COMPILER(RVCT)
246 inline void assertUnused(T& x) { (void)x; }
247 #define ASSERT_UNUSED(variable, assertion) (assertUnused(variable))
249 #define ASSERT_UNUSED(variable, assertion) ((void)variable)
254 #define ASSERT(assertion) \
256 (WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion), \
260 #define ASSERT_AT(assertion, file, line, function) \
262 (WTFReportAssertionFailure(file, line, function, #assertion), \
266 #define ASSERT_NOT_REACHED() do { \
267 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 0); \
271 #define ASSERT_UNUSED(variable, assertion) ASSERT(assertion)
273 #define NO_RETURN_DUE_TO_ASSERT NO_RETURN_DUE_TO_CRASH
277 /* ASSERT_WITH_MESSAGE */
279 #if COMPILER(MSVC7_OR_LOWER)
280 #define ASSERT_WITH_MESSAGE(assertion) ((void)0)
281 #elif ASSERT_MSG_DISABLED
282 #define ASSERT_WITH_MESSAGE(assertion, ...) ((void)0)
284 #define ASSERT_WITH_MESSAGE(assertion, ...) do \
285 if (!(assertion)) { \
286 WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
292 /* ASSERT_WITH_MESSAGE_UNUSED */
294 #if COMPILER(MSVC7_OR_LOWER)
295 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion) ((void)0)
296 #elif ASSERT_MSG_DISABLED
297 #if COMPILER(INTEL) && !OS(WINDOWS) || COMPILER(RVCT)
299 inline void assertWithMessageUnused(T& x) { (void)x; }
300 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) (assertWithMessageUnused(variable))
302 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) ((void)variable)
305 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) do \
306 if (!(assertion)) { \
307 WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
316 #if ASSERT_ARG_DISABLED
318 #define ASSERT_ARG(argName, assertion) ((void)0)
322 #define ASSERT_ARG(argName, assertion) do \
323 if (!(assertion)) { \
324 WTFReportArgumentAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #argName, #assertion); \
332 #ifndef COMPILE_ASSERT
333 #if COMPILER_SUPPORTS(C_STATIC_ASSERT)
334 #define COMPILE_ASSERT(exp, name) _Static_assert((exp), #name)
336 #define COMPILE_ASSERT(exp, name) typedef int dummy##name [(exp) ? 1 : -1]
342 #if COMPILER(MSVC7_OR_LOWER)
343 #define FATAL() ((void)0)
345 #define FATAL(...) ((void)0)
347 #define FATAL(...) do { \
348 WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__); \
355 #if COMPILER(MSVC7_OR_LOWER)
356 #define LOG_ERROR() ((void)0)
358 #define LOG_ERROR(...) ((void)0)
360 #define LOG_ERROR(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__)
365 #if COMPILER(MSVC7_OR_LOWER)
366 #define LOG() ((void)0)
368 #define LOG(channel, ...) ((void)0)
370 #if ENABLE(TIZEN_DLOG_SUPPORT)
371 // Use below macros to print out WebKit debug log.
372 #define TIZEN_LOGE(...) TizenLog(TIZEN_DLOG_PRIORITY_ERROR, false, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__);
373 #define TIZEN_LOGI(...) TizenLog(TIZEN_DLOG_PRIORITY_INFO, false, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__);
375 // Use below macros to print out secure log.
376 #define TIZEN_SECURE_LOGE(...) TizenLog(TIZEN_DLOG_PRIORITY_ERROR, true, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__);
377 #define TIZEN_SECURE_LOGI(...) TizenLog(TIZEN_DLOG_PRIORITY_INFO, true, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__);
379 #define LOG(channel, ...) LOG_VERBOSE(channel, __VA_ARGS__)
381 #define LOG(channel, ...) WTFLog(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
383 #define JOIN_LOG_CHANNEL_WITH_PREFIX(prefix, channel) JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel)
384 #define JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel) prefix ## channel
389 #if COMPILER(MSVC7_OR_LOWER)
390 #define LOG_VERBOSE(channel) ((void)0)
392 #define LOG_VERBOSE(channel, ...) ((void)0)
394 #define LOG_VERBOSE(channel, ...) WTFLogVerbose(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, &JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
397 /* UNREACHABLE_FOR_PLATFORM */
400 // This would be a macro except that its use of #pragma works best around
401 // a function. Hence it uses macro naming convention.
402 #pragma clang diagnostic push
403 #pragma clang diagnostic ignored "-Wmissing-noreturn"
404 static inline void UNREACHABLE_FOR_PLATFORM()
406 ASSERT_NOT_REACHED();
408 #pragma clang diagnostic pop
410 #define UNREACHABLE_FOR_PLATFORM() ASSERT_NOT_REACHED()
413 #endif /* WTF_Assertions_h */