blob: 608aa148064b9fe6ba9cb297e9c738d9c57666da [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
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000033#include "../include/v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034extern "C" void V8_Fatal(const char* file, int line, const char* format, ...);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
36// The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during
37// development, but they should not be relied on in the final product.
ager@chromium.org236ad962008-09-25 09:45:57 +000038#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039#define FATAL(msg) \
40 V8_Fatal(__FILE__, __LINE__, "%s", (msg))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041#define UNIMPLEMENTED() \
42 V8_Fatal(__FILE__, __LINE__, "unimplemented code")
ager@chromium.org236ad962008-09-25 09:45:57 +000043#define UNREACHABLE() \
44 V8_Fatal(__FILE__, __LINE__, "unreachable code")
45#else
46#define FATAL(msg) \
47 V8_Fatal("", 0, "%s", (msg))
48#define UNIMPLEMENTED() \
49 V8_Fatal("", 0, "unimplemented code")
50#define UNREACHABLE() ((void) 0)
51#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052
53
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054// The CHECK macro checks that the given condition is true; if not, it
55// prints a message to stderr and aborts.
danno@chromium.orgfa458e42012-02-01 10:48:36 +000056#define CHECK(condition) do { \
57 if (!(condition)) { \
58 V8_Fatal(__FILE__, __LINE__, "CHECK(%s) failed", #condition); \
59 } \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000060 } while (0)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061
62
63// Helper function used by the CHECK_EQ function when given int
64// arguments. Should not be called directly.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +000065inline void CheckEqualsHelper(const char* file, int line,
66 const char* expected_source, int expected,
67 const char* value_source, int value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000068 if (expected != value) {
69 V8_Fatal(file, line,
70 "CHECK_EQ(%s, %s) failed\n# Expected: %i\n# Found: %i",
71 expected_source, value_source, expected, value);
72 }
73}
74
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000075
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000076// Helper function used by the CHECK_EQ function when given int64_t
77// arguments. Should not be called directly.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +000078inline void CheckEqualsHelper(const char* file, int line,
79 const char* expected_source,
80 int64_t expected,
81 const char* value_source,
82 int64_t value) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000083 if (expected != value) {
84 // Print int64_t values in hex, as two int32s,
85 // to avoid platform-dependencies.
86 V8_Fatal(file, line,
87 "CHECK_EQ(%s, %s) failed\n#"
88 " Expected: 0x%08x%08x\n# Found: 0x%08x%08x",
89 expected_source, value_source,
90 static_cast<uint32_t>(expected >> 32),
91 static_cast<uint32_t>(expected),
92 static_cast<uint32_t>(value >> 32),
93 static_cast<uint32_t>(value));
94 }
95}
96
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097
98// Helper function used by the CHECK_NE function when given int
99// arguments. Should not be called directly.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000100inline void CheckNonEqualsHelper(const char* file,
101 int line,
102 const char* unexpected_source,
103 int unexpected,
104 const char* value_source,
105 int value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106 if (unexpected == value) {
107 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i",
108 unexpected_source, value_source, value);
109 }
110}
111
112
113// Helper function used by the CHECK function when given string
114// arguments. Should not be called directly.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000115inline void CheckEqualsHelper(const char* file,
116 int line,
117 const char* expected_source,
118 const char* expected,
119 const char* value_source,
120 const char* value) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000121 if ((expected == NULL && value != NULL) ||
122 (expected != NULL && value == NULL) ||
123 (expected != NULL && value != NULL && strcmp(expected, value) != 0)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124 V8_Fatal(file, line,
125 "CHECK_EQ(%s, %s) failed\n# Expected: %s\n# Found: %s",
126 expected_source, value_source, expected, value);
127 }
128}
129
130
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000131inline void CheckNonEqualsHelper(const char* file,
132 int line,
133 const char* expected_source,
134 const char* expected,
135 const char* value_source,
136 const char* value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 if (expected == value ||
138 (expected != NULL && value != NULL && strcmp(expected, value) == 0)) {
139 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %s",
140 expected_source, value_source, value);
141 }
142}
143
144
145// Helper function used by the CHECK function when given pointer
146// arguments. Should not be called directly.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000147inline void CheckEqualsHelper(const char* file,
148 int line,
149 const char* expected_source,
150 const void* expected,
151 const char* value_source,
152 const void* value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153 if (expected != value) {
154 V8_Fatal(file, line,
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000155 "CHECK_EQ(%s, %s) failed\n# Expected: %p\n# Found: %p",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 expected_source, value_source,
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000157 expected, value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 }
159}
160
161
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000162inline void CheckNonEqualsHelper(const char* file,
163 int line,
164 const char* expected_source,
165 const void* expected,
166 const char* value_source,
167 const void* value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168 if (expected == value) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000169 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p",
170 expected_source, value_source, value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 }
172}
173
174
175// Helper function used by the CHECK function when given floating
176// point arguments. Should not be called directly.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000177inline void CheckEqualsHelper(const char* file,
178 int line,
179 const char* expected_source,
180 double expected,
181 const char* value_source,
182 double value) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000183 // Force values to 64 bit memory to truncate 80 bit precision on IA32.
184 volatile double* exp = new double[1];
185 *exp = expected;
186 volatile double* val = new double[1];
187 *val = value;
188 if (*exp != *val) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189 V8_Fatal(file, line,
190 "CHECK_EQ(%s, %s) failed\n# Expected: %f\n# Found: %f",
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000191 expected_source, value_source, *exp, *val);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000193 delete[] exp;
194 delete[] val;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195}
196
197
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000198inline void CheckNonEqualsHelper(const char* file,
199 int line,
200 const char* expected_source,
201 double expected,
202 const char* value_source,
203 double value) {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000204 // Force values to 64 bit memory to truncate 80 bit precision on IA32.
205 volatile double* exp = new double[1];
206 *exp = expected;
207 volatile double* val = new double[1];
208 *val = value;
209 if (*exp == *val) {
210 V8_Fatal(file, line,
211 "CHECK_NE(%s, %s) failed\n# Value: %f",
212 expected_source, value_source, *val);
213 }
214 delete[] exp;
215 delete[] val;
216}
217
218
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219#define CHECK_EQ(expected, value) CheckEqualsHelper(__FILE__, __LINE__, \
220 #expected, expected, #value, value)
221
222
223#define CHECK_NE(unexpected, value) CheckNonEqualsHelper(__FILE__, __LINE__, \
224 #unexpected, unexpected, #value, value)
225
226
227#define CHECK_GT(a, b) CHECK((a) > (b))
228#define CHECK_GE(a, b) CHECK((a) >= (b))
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000229#define CHECK_LT(a, b) CHECK((a) < (b))
230#define CHECK_LE(a, b) CHECK((a) <= (b))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231
232
233// This is inspired by the static assertion facility in boost. This
234// is pretty magical. If it causes you trouble on a platform you may
235// find a fix in the boost code.
236template <bool> class StaticAssertion;
237template <> class StaticAssertion<true> { };
238// This macro joins two tokens. If one of the tokens is a macro the
239// helper call causes it to be resolved before joining.
240#define SEMI_STATIC_JOIN(a, b) SEMI_STATIC_JOIN_HELPER(a, b)
241#define SEMI_STATIC_JOIN_HELPER(a, b) a##b
242// Causes an error during compilation of the condition is not
243// statically known to be true. It is formulated as a typedef so that
244// it can be used wherever a typedef can be used. Beware that this
245// actually causes each use to introduce a new defined type with a
246// name depending on the source line.
247template <int> class StaticAssertionHelper { };
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000248#define STATIC_CHECK(test) \
249 typedef \
250 StaticAssertionHelper<sizeof(StaticAssertion<static_cast<bool>((test))>)> \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251 SEMI_STATIC_JOIN(__StaticAssertTypedef__, __LINE__)
252
253
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000254extern bool FLAG_enable_slow_asserts;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000255
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000256
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257// The ASSERT macro is equivalent to CHECK except that it only
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000258// generates code in debug builds.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259#ifdef DEBUG
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000260#define ASSERT_RESULT(expr) CHECK(expr)
261#define ASSERT(condition) CHECK(condition)
262#define ASSERT_EQ(v1, v2) CHECK_EQ(v1, v2)
263#define ASSERT_NE(v1, v2) CHECK_NE(v1, v2)
264#define ASSERT_GE(v1, v2) CHECK_GE(v1, v2)
265#define ASSERT_LT(v1, v2) CHECK_LT(v1, v2)
266#define ASSERT_LE(v1, v2) CHECK_LE(v1, v2)
267#define SLOW_ASSERT(condition) CHECK(!FLAG_enable_slow_asserts || (condition))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268#else
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000269#define ASSERT_RESULT(expr) (expr)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270#define ASSERT(condition) ((void) 0)
271#define ASSERT_EQ(v1, v2) ((void) 0)
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000272#define ASSERT_NE(v1, v2) ((void) 0)
273#define ASSERT_GE(v1, v2) ((void) 0)
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000274#define ASSERT_LT(v1, v2) ((void) 0)
275#define ASSERT_LE(v1, v2) ((void) 0)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276#define SLOW_ASSERT(condition) ((void) 0)
277#endif
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000278// Static asserts has no impact on runtime performance, so they can be
279// safely enabled in release mode. Moreover, the ((void) 0) expression
280// obeys different syntax rules than typedef's, e.g. it can't appear
281// inside class declaration, this leads to inconsistency between debug
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000282// and release compilation modes behavior.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000283#define STATIC_ASSERT(test) STATIC_CHECK(test)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000285#define ASSERT_NOT_NULL(p) ASSERT_NE(NULL, p)
286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287#endif // V8_CHECKS_H_