blob: ad66d067eb2f720f338d17bb0aa90c0d9c04f7b4 [file] [log] [blame]
darin7e7b97b2002-09-09 06:11:13 +00001/*
weinigcdc6bc92007-05-31 00:16:27 +00002 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
darin7e7b97b2002-09-09 06:11:13 +00003 *
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
mjs98cf6e02006-10-19 02:42:55 +000026#ifndef WTF_Assertions_h
27#define WTF_Assertions_h
mjscff5e5e2005-09-27 22:37:33 +000028
ap467ab6f2006-09-01 20:05:39 +000029/*
30 no namespaces because this file has to be includable from C and Objective-C
mjscff5e5e2005-09-27 22:37:33 +000031
ap467ab6f2006-09-01 20:05:39 +000032 Note, this file uses many GCC extensions, but it should be compatible with
33 C, Objective C, C++, and Objective C++.
darin7e7b97b2002-09-09 06:11:13 +000034
ap467ab6f2006-09-01 20:05:39 +000035 For non-debug builds, everything is disabled by default.
36 Defining any of the symbols explicitly prevents this from having any effect.
bdash010c0572007-10-14 11:55:02 +000037
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
ap467ab6f2006-09-01 20:05:39 +000043*/
darin7e7b97b2002-09-09 06:11:13 +000044
sfalkend180c0c2006-05-09 21:50:12 +000045#include "Platform.h"
eseidel0a779082006-10-02 10:57:23 +000046
ggarend789afc2007-03-11 23:57:11 +000047#if COMPILER(MSVC)
48#include <stddef.h>
49#else
eseidel2aed5562006-10-02 10:26:58 +000050#include <inttypes.h>
eseidel0a779082006-10-02 10:57:23 +000051#endif
sfalkend180c0c2006-05-09 21:50:12 +000052
darin7e7b97b2002-09-09 06:11:13 +000053#ifdef NDEBUG
mjscff5e5e2005-09-27 22:37:33 +000054#define ASSERTIONS_DISABLED_DEFAULT 1
darinfc91b7c2002-09-11 23:23:14 +000055#else
mjscff5e5e2005-09-27 22:37:33 +000056#define ASSERTIONS_DISABLED_DEFAULT 0
darinfc91b7c2002-09-11 23:23:14 +000057#endif
darin7e7b97b2002-09-09 06:11:13 +000058
59#ifndef ASSERT_DISABLED
mjscff5e5e2005-09-27 22:37:33 +000060#define ASSERT_DISABLED ASSERTIONS_DISABLED_DEFAULT
darin7e7b97b2002-09-09 06:11:13 +000061#endif
62
63#ifndef ASSERT_ARG_DISABLED
mjscff5e5e2005-09-27 22:37:33 +000064#define ASSERT_ARG_DISABLED ASSERTIONS_DISABLED_DEFAULT
darin7e7b97b2002-09-09 06:11:13 +000065#endif
66
67#ifndef FATAL_DISABLED
mjscff5e5e2005-09-27 22:37:33 +000068#define FATAL_DISABLED ASSERTIONS_DISABLED_DEFAULT
darin7e7b97b2002-09-09 06:11:13 +000069#endif
70
71#ifndef ERROR_DISABLED
mjscff5e5e2005-09-27 22:37:33 +000072#define ERROR_DISABLED ASSERTIONS_DISABLED_DEFAULT
darin7e7b97b2002-09-09 06:11:13 +000073#endif
74
75#ifndef LOG_DISABLED
mjscff5e5e2005-09-27 22:37:33 +000076#define LOG_DISABLED ASSERTIONS_DISABLED_DEFAULT
darin7e7b97b2002-09-09 06:11:13 +000077#endif
78
mjs3bfb61b2006-03-02 09:12:06 +000079#if COMPILER(GCC)
mjs98cf6e02006-10-19 02:42:55 +000080#define WTF_PRETTY_FUNCTION __PRETTY_FUNCTION__
hyatt6c974dd2006-01-06 22:43:44 +000081#else
mjs98cf6e02006-10-19 02:42:55 +000082#define WTF_PRETTY_FUNCTION __FUNCTION__
hyatt6c974dd2006-01-06 22:43:44 +000083#endif
84
christian@webkit.orgab7f01f2008-06-16 13:39:59 +000085/* WTF logging functions can process %@ in the format string to log a NSObject* but the printf format attribute
86 emits a warning when %@ is used in the format string. Until <rdar://problem/5195437> is resolved we can't include
87 the attribute when being used from Objective-C code in case it decides to use %@. */
mrowe@apple.com85aac952007-11-22 03:08:41 +000088#if COMPILER(GCC) && !defined(__OBJC__)
89#define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments) __attribute__((__format__(printf, formatStringArgument, extraArguments)))
90#else
91#define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments)
92#endif
93
ap467ab6f2006-09-01 20:05:39 +000094/* These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. */
darin7e7b97b2002-09-09 06:11:13 +000095
96#ifdef __cplusplus
97extern "C" {
98#endif
99
mjsbb863512006-05-09 09:27:55 +0000100typedef enum { WTFLogChannelOff, WTFLogChannelOn } WTFLogChannelState;
mjscff5e5e2005-09-27 22:37:33 +0000101
darin7e7b97b2002-09-09 06:11:13 +0000102typedef struct {
103 unsigned mask;
104 const char *defaultName;
mjsbb863512006-05-09 09:27:55 +0000105 WTFLogChannelState state;
106} WTFLogChannel;
hyatt6c974dd2006-01-06 22:43:44 +0000107
weinigcdc6bc92007-05-31 00:16:27 +0000108void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion);
mrowe@apple.com85aac952007-11-22 03:08:41 +0000109void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
weinigcdc6bc92007-05-31 00:16:27 +0000110void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion);
mrowe@apple.com85aac952007-11-22 03:08:41 +0000111void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
112void WTFReportError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
113void WTFLog(WTFLogChannel* channel, const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
114void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
darin7e7b97b2002-09-09 06:11:13 +0000115
116#ifdef __cplusplus
117}
118#endif
119
ap467ab6f2006-09-01 20:05:39 +0000120/* CRASH -- gets us into the debugger or the crash reporter -- signals are ignored by the crash reporter so we must do better */
darin7b077f22002-09-13 14:50:00 +0000121
bdashc4c84a22007-03-09 03:11:46 +0000122#ifndef CRASH
ap@webkit.org7665d322008-11-21 21:20:41 +0000123#define CRASH() do { \
124 *(int *)(uintptr_t)0xbbadbeef = 0; \
125 ((void(*)())0)(); /* More reliable, but doesn't say BBADBEEF */ \
126} while(false)
ap@webkit.org93a88e02008-11-21 19:07:01 +0000127#endif
darin7b077f22002-09-13 14:50:00 +0000128
ap467ab6f2006-09-01 20:05:39 +0000129/* ASSERT, ASSERT_WITH_MESSAGE, ASSERT_NOT_REACHED */
darin7e7b97b2002-09-09 06:11:13 +0000130
treat@webkit.org068b0692009-06-17 16:39:56 +0000131#if PLATFORM(WINCE) && !PLATFORM(TORCHMOBILE)
hausmann@webkit.org51cca422008-11-24 08:09:56 +0000132/* FIXME: We include this here only to avoid a conflict with the ASSERT macro. */
133#include <windows.h>
134#undef min
135#undef max
136#undef ERROR
137#endif
138
eseidelb7b8a032006-04-04 18:10:12 +0000139#if PLATFORM(WIN_OS)
ap467ab6f2006-09-01 20:05:39 +0000140/* FIXME: Change to use something other than ASSERT to avoid this conflict with win32. */
eseidelb7b8a032006-04-04 18:10:12 +0000141#undef ASSERT
142#endif
143
darin7e7b97b2002-09-09 06:11:13 +0000144#if ASSERT_DISABLED
145
146#define ASSERT(assertion) ((void)0)
apf46e0632006-01-07 10:22:45 +0000147#define ASSERT_WITH_MESSAGE(assertion, ...) ((void)0)
darin7e7b97b2002-09-09 06:11:13 +0000148#define ASSERT_NOT_REACHED() ((void)0)
ap@webkit.org5b68ea12009-01-05 17:21:13 +0000149#define ASSERT_UNUSED(variable, assertion) ((void)variable)
darin7e7b97b2002-09-09 06:11:13 +0000150
151#else
152
153#define ASSERT(assertion) do \
154 if (!(assertion)) { \
mjs98cf6e02006-10-19 02:42:55 +0000155 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion); \
darin7b077f22002-09-13 14:50:00 +0000156 CRASH(); \
darin7e7b97b2002-09-09 06:11:13 +0000157 } \
158while (0)
bdash010c0572007-10-14 11:55:02 +0000159#if COMPILER(MSVC7)
160#define ASSERT_WITH_MESSAGE(assertion) ((void)0)
161#else
apf46e0632006-01-07 10:22:45 +0000162#define ASSERT_WITH_MESSAGE(assertion, ...) do \
darin7e7b97b2002-09-09 06:11:13 +0000163 if (!(assertion)) { \
mjs98cf6e02006-10-19 02:42:55 +0000164 WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
darin7b077f22002-09-13 14:50:00 +0000165 CRASH(); \
darin7e7b97b2002-09-09 06:11:13 +0000166 } \
167while (0)
christian@webkit.orgab7f01f2008-06-16 13:39:59 +0000168#endif /* COMPILER(MSVC7) */
darin7e7b97b2002-09-09 06:11:13 +0000169#define ASSERT_NOT_REACHED() do { \
mjs98cf6e02006-10-19 02:42:55 +0000170 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 0); \
darin7b077f22002-09-13 14:50:00 +0000171 CRASH(); \
darin7e7b97b2002-09-09 06:11:13 +0000172} while (0)
173
ap@webkit.org5b68ea12009-01-05 17:21:13 +0000174#define ASSERT_UNUSED(variable, assertion) ASSERT(assertion)
175
darin7e7b97b2002-09-09 06:11:13 +0000176#endif
177
ap467ab6f2006-09-01 20:05:39 +0000178/* ASSERT_ARG */
darin7e7b97b2002-09-09 06:11:13 +0000179
180#if ASSERT_ARG_DISABLED
181
182#define ASSERT_ARG(argName, assertion) ((void)0)
183
184#else
185
186#define ASSERT_ARG(argName, assertion) do \
187 if (!(assertion)) { \
mjs98cf6e02006-10-19 02:42:55 +0000188 WTFReportArgumentAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #argName, #assertion); \
darin7b077f22002-09-13 14:50:00 +0000189 CRASH(); \
darin7e7b97b2002-09-09 06:11:13 +0000190 } \
191while (0)
192
193#endif
194
kjk67f5d5a2007-02-17 09:07:39 +0000195/* COMPILE_ASSERT */
196#ifndef COMPILE_ASSERT
levin@chromium.org2bacfc02009-03-20 23:45:58 +0000197#define COMPILE_ASSERT(exp, name) typedef int dummy##name [(exp) ? 1 : -1]
kjk67f5d5a2007-02-17 09:07:39 +0000198#endif
199
ap467ab6f2006-09-01 20:05:39 +0000200/* FATAL */
darin7e7b97b2002-09-09 06:11:13 +0000201
202#if FATAL_DISABLED
apf46e0632006-01-07 10:22:45 +0000203#define FATAL(...) ((void)0)
bdash010c0572007-10-14 11:55:02 +0000204#elif COMPILER(MSVC7)
205#define FATAL() ((void)0)
darin7e7b97b2002-09-09 06:11:13 +0000206#else
apf46e0632006-01-07 10:22:45 +0000207#define FATAL(...) do { \
mjs98cf6e02006-10-19 02:42:55 +0000208 WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__); \
darin7b077f22002-09-13 14:50:00 +0000209 CRASH(); \
darin7e7b97b2002-09-09 06:11:13 +0000210} while (0)
211#endif
212
ap467ab6f2006-09-01 20:05:39 +0000213/* LOG_ERROR */
darin7e7b97b2002-09-09 06:11:13 +0000214
215#if ERROR_DISABLED
darind91fb242006-02-19 17:20:40 +0000216#define LOG_ERROR(...) ((void)0)
bdash010c0572007-10-14 11:55:02 +0000217#elif COMPILER(MSVC7)
218#define LOG_ERROR() ((void)0)
darin7e7b97b2002-09-09 06:11:13 +0000219#else
mjs98cf6e02006-10-19 02:42:55 +0000220#define LOG_ERROR(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__)
darin7e7b97b2002-09-09 06:11:13 +0000221#endif
222
ap467ab6f2006-09-01 20:05:39 +0000223/* LOG */
darin7e7b97b2002-09-09 06:11:13 +0000224
225#if LOG_DISABLED
apf46e0632006-01-07 10:22:45 +0000226#define LOG(channel, ...) ((void)0)
bdash010c0572007-10-14 11:55:02 +0000227#elif COMPILER(MSVC7)
228#define LOG() ((void)0)
darin7e7b97b2002-09-09 06:11:13 +0000229#else
weinigcdc6bc92007-05-31 00:16:27 +0000230#define LOG(channel, ...) WTFLog(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
darin7e7b97b2002-09-09 06:11:13 +0000231#define JOIN_LOG_CHANNEL_WITH_PREFIX(prefix, channel) JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel)
232#define JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel) prefix ## channel
233#endif
mjscff5e5e2005-09-27 22:37:33 +0000234
weinigcdc6bc92007-05-31 00:16:27 +0000235/* LOG_VERBOSE */
236
237#if LOG_DISABLED
238#define LOG_VERBOSE(channel, ...) ((void)0)
bdash010c0572007-10-14 11:55:02 +0000239#elif COMPILER(MSVC7)
240#define LOG_VERBOSE(channel) ((void)0)
weinigcdc6bc92007-05-31 00:16:27 +0000241#else
242#define LOG_VERBOSE(channel, ...) WTFLogVerbose(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, &JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
243#endif
244
christian@webkit.orgab7f01f2008-06-16 13:39:59 +0000245#endif /* WTF_Assertions_h */