blob: f5c5f232bd5e3e12afb8b8c0ec0e9154477080dd [file] [log] [blame]
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_CHECKS_H_
29#define V8_CHECKS_H_
30
31#include <string.h>
32
rossberg@chromium.org92597162013-08-23 13:28:00 +000033#include "../include/v8stdint.h"
verwaest@chromium.org32cb9b22013-08-21 11:18:12 +000034
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035extern "C" void V8_Fatal(const char* file, int line, const char* format, ...);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
37// The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during
38// development, but they should not be relied on in the final product.
ager@chromium.org236ad962008-09-25 09:45:57 +000039#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040#define FATAL(msg) \
41 V8_Fatal(__FILE__, __LINE__, "%s", (msg))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042#define UNIMPLEMENTED() \
43 V8_Fatal(__FILE__, __LINE__, "unimplemented code")
ager@chromium.org236ad962008-09-25 09:45:57 +000044#define UNREACHABLE() \
45 V8_Fatal(__FILE__, __LINE__, "unreachable code")
46#else
47#define FATAL(msg) \
48 V8_Fatal("", 0, "%s", (msg))
49#define UNIMPLEMENTED() \
50 V8_Fatal("", 0, "unimplemented code")
51#define UNREACHABLE() ((void) 0)
52#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053
54
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055// The CHECK macro checks that the given condition is true; if not, it
56// prints a message to stderr and aborts.
danno@chromium.orgfa458e42012-02-01 10:48:36 +000057#define CHECK(condition) do { \
58 if (!(condition)) { \
59 V8_Fatal(__FILE__, __LINE__, "CHECK(%s) failed", #condition); \
60 } \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000061 } while (0)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000062
63
64// Helper function used by the CHECK_EQ function when given int
65// arguments. Should not be called directly.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +000066inline void CheckEqualsHelper(const char* file, int line,
67 const char* expected_source, int expected,
68 const char* value_source, int value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000069 if (expected != value) {
70 V8_Fatal(file, line,
71 "CHECK_EQ(%s, %s) failed\n# Expected: %i\n# Found: %i",
72 expected_source, value_source, expected, value);
73 }
74}
75
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000076
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000077// Helper function used by the CHECK_EQ function when given int64_t
78// arguments. Should not be called directly.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +000079inline void CheckEqualsHelper(const char* file, int line,
80 const char* expected_source,
81 int64_t expected,
82 const char* value_source,
83 int64_t value) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000084 if (expected != value) {
85 // Print int64_t values in hex, as two int32s,
86 // to avoid platform-dependencies.
87 V8_Fatal(file, line,
88 "CHECK_EQ(%s, %s) failed\n#"
89 " Expected: 0x%08x%08x\n# Found: 0x%08x%08x",
90 expected_source, value_source,
91 static_cast<uint32_t>(expected >> 32),
92 static_cast<uint32_t>(expected),
93 static_cast<uint32_t>(value >> 32),
94 static_cast<uint32_t>(value));
95 }
96}
97
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098
99// Helper function used by the CHECK_NE function when given int
100// arguments. Should not be called directly.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000101inline void CheckNonEqualsHelper(const char* file,
102 int line,
103 const char* unexpected_source,
104 int unexpected,
105 const char* value_source,
106 int value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107 if (unexpected == value) {
108 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i",
109 unexpected_source, value_source, value);
110 }
111}
112
113
114// Helper function used by the CHECK function when given string
115// arguments. Should not be called directly.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000116inline void CheckEqualsHelper(const char* file,
117 int line,
118 const char* expected_source,
119 const char* expected,
120 const char* value_source,
121 const char* value) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000122 if ((expected == NULL && value != NULL) ||
123 (expected != NULL && value == NULL) ||
124 (expected != NULL && value != NULL && strcmp(expected, value) != 0)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125 V8_Fatal(file, line,
126 "CHECK_EQ(%s, %s) failed\n# Expected: %s\n# Found: %s",
127 expected_source, value_source, expected, value);
128 }
129}
130
131
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000132inline void CheckNonEqualsHelper(const char* file,
133 int line,
134 const char* expected_source,
135 const char* expected,
136 const char* value_source,
137 const char* value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 if (expected == value ||
139 (expected != NULL && value != NULL && strcmp(expected, value) == 0)) {
140 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %s",
141 expected_source, value_source, value);
142 }
143}
144
145
146// Helper function used by the CHECK function when given pointer
147// arguments. Should not be called directly.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000148inline void CheckEqualsHelper(const char* file,
149 int line,
150 const char* expected_source,
151 const void* expected,
152 const char* value_source,
153 const void* value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154 if (expected != value) {
155 V8_Fatal(file, line,
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000156 "CHECK_EQ(%s, %s) failed\n# Expected: %p\n# Found: %p",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 expected_source, value_source,
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000158 expected, value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159 }
160}
161
162
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000163inline void CheckNonEqualsHelper(const char* file,
164 int line,
165 const char* expected_source,
166 const void* expected,
167 const char* value_source,
168 const void* value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169 if (expected == value) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000170 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p",
171 expected_source, value_source, value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 }
173}
174
175
176// Helper function used by the CHECK function when given floating
177// point arguments. Should not be called directly.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000178inline void CheckEqualsHelper(const char* file,
179 int line,
180 const char* expected_source,
181 double expected,
182 const char* value_source,
183 double value) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000184 // Force values to 64 bit memory to truncate 80 bit precision on IA32.
185 volatile double* exp = new double[1];
186 *exp = expected;
187 volatile double* val = new double[1];
188 *val = value;
189 if (*exp != *val) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 V8_Fatal(file, line,
191 "CHECK_EQ(%s, %s) failed\n# Expected: %f\n# Found: %f",
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000192 expected_source, value_source, *exp, *val);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000194 delete[] exp;
195 delete[] val;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196}
197
198
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000199inline void CheckNonEqualsHelper(const char* file,
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +0000200 int line,
201 const char* expected_source,
202 int64_t expected,
203 const char* value_source,
204 int64_t value) {
205 if (expected == value) {
206 V8_Fatal(file, line,
207 "CHECK_EQ(%s, %s) failed\n# Expected: %f\n# Found: %f",
208 expected_source, value_source, expected, value);
209 }
210}
211
212
213inline void CheckNonEqualsHelper(const char* file,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000214 int line,
215 const char* expected_source,
216 double expected,
217 const char* value_source,
218 double value) {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000219 // Force values to 64 bit memory to truncate 80 bit precision on IA32.
220 volatile double* exp = new double[1];
221 *exp = expected;
222 volatile double* val = new double[1];
223 *val = value;
224 if (*exp == *val) {
225 V8_Fatal(file, line,
226 "CHECK_NE(%s, %s) failed\n# Value: %f",
227 expected_source, value_source, *val);
228 }
229 delete[] exp;
230 delete[] val;
231}
232
233
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234#define CHECK_EQ(expected, value) CheckEqualsHelper(__FILE__, __LINE__, \
235 #expected, expected, #value, value)
236
237
238#define CHECK_NE(unexpected, value) CheckNonEqualsHelper(__FILE__, __LINE__, \
239 #unexpected, unexpected, #value, value)
240
241
242#define CHECK_GT(a, b) CHECK((a) > (b))
243#define CHECK_GE(a, b) CHECK((a) >= (b))
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000244#define CHECK_LT(a, b) CHECK((a) < (b))
245#define CHECK_LE(a, b) CHECK((a) <= (b))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246
247
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000248// Use C++11 static_assert if possible, which gives error
249// messages that are easier to understand on first sight.
rossberg@chromium.org92597162013-08-23 13:28:00 +0000250#if V8_HAS_CXX11_STATIC_ASSERT
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000251#define STATIC_CHECK(test) static_assert(test, #test)
252#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253// This is inspired by the static assertion facility in boost. This
254// is pretty magical. If it causes you trouble on a platform you may
255// find a fix in the boost code.
256template <bool> class StaticAssertion;
257template <> class StaticAssertion<true> { };
258// This macro joins two tokens. If one of the tokens is a macro the
259// helper call causes it to be resolved before joining.
260#define SEMI_STATIC_JOIN(a, b) SEMI_STATIC_JOIN_HELPER(a, b)
261#define SEMI_STATIC_JOIN_HELPER(a, b) a##b
262// Causes an error during compilation of the condition is not
263// statically known to be true. It is formulated as a typedef so that
264// it can be used wherever a typedef can be used. Beware that this
265// actually causes each use to introduce a new defined type with a
266// name depending on the source line.
267template <int> class StaticAssertionHelper { };
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000268#define STATIC_CHECK(test) \
269 typedef \
270 StaticAssertionHelper<sizeof(StaticAssertion<static_cast<bool>((test))>)> \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271 SEMI_STATIC_JOIN(__StaticAssertTypedef__, __LINE__)
danno@chromium.orgc22f2d82013-07-31 07:47:18 +0000272#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273
274
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000275extern bool FLAG_enable_slow_asserts;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000276
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000277
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278// The ASSERT macro is equivalent to CHECK except that it only
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000279// generates code in debug builds.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280#ifdef DEBUG
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000281#define ASSERT_RESULT(expr) CHECK(expr)
282#define ASSERT(condition) CHECK(condition)
283#define ASSERT_EQ(v1, v2) CHECK_EQ(v1, v2)
284#define ASSERT_NE(v1, v2) CHECK_NE(v1, v2)
285#define ASSERT_GE(v1, v2) CHECK_GE(v1, v2)
286#define ASSERT_LT(v1, v2) CHECK_LT(v1, v2)
287#define ASSERT_LE(v1, v2) CHECK_LE(v1, v2)
288#define SLOW_ASSERT(condition) CHECK(!FLAG_enable_slow_asserts || (condition))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289#else
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000290#define ASSERT_RESULT(expr) (expr)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291#define ASSERT(condition) ((void) 0)
292#define ASSERT_EQ(v1, v2) ((void) 0)
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000293#define ASSERT_NE(v1, v2) ((void) 0)
294#define ASSERT_GE(v1, v2) ((void) 0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000295#define ASSERT_LT(v1, v2) ((void) 0)
296#define ASSERT_LE(v1, v2) ((void) 0)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297#define SLOW_ASSERT(condition) ((void) 0)
298#endif
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000299// Static asserts has no impact on runtime performance, so they can be
300// safely enabled in release mode. Moreover, the ((void) 0) expression
301// obeys different syntax rules than typedef's, e.g. it can't appear
302// inside class declaration, this leads to inconsistency between debug
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000303// and release compilation modes behavior.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000304#define STATIC_ASSERT(test) STATIC_CHECK(test)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000306#define ASSERT_NOT_NULL(p) ASSERT_NE(NULL, p)
307
jkummerow@chromium.org67255be2012-09-05 16:44:50 +0000308// "Extra checks" are lightweight checks that are enabled in some release
309// builds.
310#ifdef ENABLE_EXTRA_CHECKS
311#define EXTRA_CHECK(condition) CHECK(condition)
312#else
313#define EXTRA_CHECK(condition) ((void) 0)
314#endif
315
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316#endif // V8_CHECKS_H_