1 | /* -*- mode: c++; c-basic-offset: 4 -*- */
|
---|
2 | /*
|
---|
3 | * Copyright (C) 2003, 2006 Apple Computer, 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 |
|
---|
40 | #include "Platform.h"
|
---|
41 |
|
---|
42 | #if !PLATFORM(WIN)
|
---|
43 | #include <inttypes.h>
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | #ifdef NDEBUG
|
---|
47 | #define ASSERTIONS_DISABLED_DEFAULT 1
|
---|
48 | #else
|
---|
49 | #define ASSERTIONS_DISABLED_DEFAULT 0
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #ifndef ASSERT_DISABLED
|
---|
53 | #define ASSERT_DISABLED ASSERTIONS_DISABLED_DEFAULT
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | #ifndef ASSERT_ARG_DISABLED
|
---|
57 | #define ASSERT_ARG_DISABLED ASSERTIONS_DISABLED_DEFAULT
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | #ifndef FATAL_DISABLED
|
---|
61 | #define FATAL_DISABLED ASSERTIONS_DISABLED_DEFAULT
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | #ifndef ERROR_DISABLED
|
---|
65 | #define ERROR_DISABLED ASSERTIONS_DISABLED_DEFAULT
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | #ifndef LOG_DISABLED
|
---|
69 | #define LOG_DISABLED ASSERTIONS_DISABLED_DEFAULT
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | #if COMPILER(GCC)
|
---|
73 | #define WTF_PRETTY_FUNCTION __PRETTY_FUNCTION__
|
---|
74 | #else
|
---|
75 | #define WTF_PRETTY_FUNCTION __FUNCTION__
|
---|
76 | #endif
|
---|
77 |
|
---|
78 | /* These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. */
|
---|
79 |
|
---|
80 | #ifdef __cplusplus
|
---|
81 | extern "C" {
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | typedef enum { WTFLogChannelOff, WTFLogChannelOn } WTFLogChannelState;
|
---|
85 |
|
---|
86 | typedef struct {
|
---|
87 | unsigned mask;
|
---|
88 | const char *defaultName;
|
---|
89 | WTFLogChannelState state;
|
---|
90 | } WTFLogChannel;
|
---|
91 |
|
---|
92 | void WTFReportAssertionFailure(const char *file, int line, const char *function, const char *assertion);
|
---|
93 | void WTFReportAssertionFailureWithMessage(const char *file, int line, const char *function, const char *assertion, const char *format, ...);
|
---|
94 | void WTFReportArgumentAssertionFailure(const char *file, int line, const char *function, const char *argName, const char *assertion);
|
---|
95 | void WTFReportFatalError(const char *file, int line, const char *function, const char *format, ...) ;
|
---|
96 | void WTFReportError(const char *file, int line, const char *function, const char *format, ...);
|
---|
97 | void WTFLog(const char *file, int line, const char *function, WTFLogChannel *channel, const char *format, ...);
|
---|
98 |
|
---|
99 | #ifdef __cplusplus
|
---|
100 | }
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | /* CRASH -- gets us into the debugger or the crash reporter -- signals are ignored by the crash reporter so we must do better */
|
---|
104 |
|
---|
105 | #define CRASH() *(int *)(uintptr_t)0xbbadbeef = 0
|
---|
106 |
|
---|
107 | /* ASSERT, ASSERT_WITH_MESSAGE, ASSERT_NOT_REACHED */
|
---|
108 |
|
---|
109 | #if PLATFORM(WIN_OS)
|
---|
110 | /* FIXME: Change to use something other than ASSERT to avoid this conflict with win32. */
|
---|
111 | #undef ASSERT
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | #if ASSERT_DISABLED
|
---|
115 |
|
---|
116 | #define ASSERT(assertion) ((void)0)
|
---|
117 | #define ASSERT_WITH_MESSAGE(assertion, ...) ((void)0)
|
---|
118 | #define ASSERT_NOT_REACHED() ((void)0)
|
---|
119 |
|
---|
120 | #else
|
---|
121 |
|
---|
122 | #define ASSERT(assertion) do \
|
---|
123 | if (!(assertion)) { \
|
---|
124 | WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion); \
|
---|
125 | CRASH(); \
|
---|
126 | } \
|
---|
127 | while (0)
|
---|
128 | #define ASSERT_WITH_MESSAGE(assertion, ...) do \
|
---|
129 | if (!(assertion)) { \
|
---|
130 | WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
|
---|
131 | CRASH(); \
|
---|
132 | } \
|
---|
133 | while (0)
|
---|
134 | #define ASSERT_NOT_REACHED() do { \
|
---|
135 | WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 0); \
|
---|
136 | CRASH(); \
|
---|
137 | } while (0)
|
---|
138 |
|
---|
139 | #endif
|
---|
140 |
|
---|
141 | /* ASSERT_ARG */
|
---|
142 |
|
---|
143 | #if ASSERT_ARG_DISABLED
|
---|
144 |
|
---|
145 | #define ASSERT_ARG(argName, assertion) ((void)0)
|
---|
146 |
|
---|
147 | #else
|
---|
148 |
|
---|
149 | #define ASSERT_ARG(argName, assertion) do \
|
---|
150 | if (!(assertion)) { \
|
---|
151 | WTFReportArgumentAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #argName, #assertion); \
|
---|
152 | CRASH(); \
|
---|
153 | } \
|
---|
154 | while (0)
|
---|
155 |
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | /* FATAL */
|
---|
159 |
|
---|
160 | #if FATAL_DISABLED
|
---|
161 | #define FATAL(...) ((void)0)
|
---|
162 | #else
|
---|
163 | #define FATAL(...) do { \
|
---|
164 | WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__); \
|
---|
165 | CRASH(); \
|
---|
166 | } while (0)
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | /* LOG_ERROR */
|
---|
170 |
|
---|
171 | #if ERROR_DISABLED
|
---|
172 | #define LOG_ERROR(...) ((void)0)
|
---|
173 | #else
|
---|
174 | #define LOG_ERROR(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__)
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | /* LOG */
|
---|
178 |
|
---|
179 | #if LOG_DISABLED
|
---|
180 | #define LOG(channel, ...) ((void)0)
|
---|
181 | #else
|
---|
182 | #define LOG(channel, ...) WTFLog(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, &JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
|
---|
183 | #define JOIN_LOG_CHANNEL_WITH_PREFIX(prefix, channel) JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel)
|
---|
184 | #define JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel) prefix ## channel
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | #endif // WTF_Assertions_h
|
---|