rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // 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.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 33 | #include "../include/v8stdint.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | extern "C" void V8_Fatal(const char* file, int line, const char* format, ...); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
| 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.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 38 | #ifdef DEBUG |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | #define FATAL(msg) \ |
| 40 | V8_Fatal(__FILE__, __LINE__, "%s", (msg)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | #define UNIMPLEMENTED() \ |
| 42 | V8_Fatal(__FILE__, __LINE__, "unimplemented code") |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 43 | #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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | |
| 53 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | // The CHECK macro checks that the given condition is true; if not, it |
| 55 | // prints a message to stderr and aborts. |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 56 | #define CHECK(condition) do { \ |
| 57 | if (!(condition)) { \ |
| 58 | V8_Fatal(__FILE__, __LINE__, "CHECK(%s) failed", #condition); \ |
| 59 | } \ |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 60 | } while (0) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | // Helper function used by the CHECK_EQ function when given int |
| 64 | // arguments. Should not be called directly. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 65 | inline void CheckEqualsHelper(const char* file, int line, |
| 66 | const char* expected_source, int expected, |
| 67 | const char* value_source, int value) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | 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.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 75 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 76 | // Helper function used by the CHECK_EQ function when given int64_t |
| 77 | // arguments. Should not be called directly. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 78 | inline 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.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 83 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 97 | |
| 98 | // Helper function used by the CHECK_NE function when given int |
| 99 | // arguments. Should not be called directly. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 100 | inline 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | 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.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 115 | inline 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.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 121 | if ((expected == NULL && value != NULL) || |
| 122 | (expected != NULL && value == NULL) || |
| 123 | (expected != NULL && value != NULL && strcmp(expected, value) != 0)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | 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.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 131 | inline 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | 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.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 147 | inline 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | if (expected != value) { |
| 154 | V8_Fatal(file, line, |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 155 | "CHECK_EQ(%s, %s) failed\n# Expected: %p\n# Found: %p", |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 156 | expected_source, value_source, |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 157 | expected, value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 162 | inline 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | if (expected == value) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 169 | V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p", |
| 170 | expected_source, value_source, value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | |
| 175 | // Helper function used by the CHECK function when given floating |
| 176 | // point arguments. Should not be called directly. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 177 | inline 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.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 183 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 189 | V8_Fatal(file, line, |
| 190 | "CHECK_EQ(%s, %s) failed\n# Expected: %f\n# Found: %f", |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 191 | expected_source, value_source, *exp, *val); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 193 | delete[] exp; |
| 194 | delete[] val; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 198 | inline 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.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 204 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 219 | #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.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 229 | #define CHECK_LT(a, b) CHECK((a) < (b)) |
| 230 | #define CHECK_LE(a, b) CHECK((a) <= (b)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 231 | |
| 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. |
| 236 | template <bool> class StaticAssertion; |
| 237 | template <> 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. |
| 247 | template <int> class StaticAssertionHelper { }; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 248 | #define STATIC_CHECK(test) \ |
| 249 | typedef \ |
| 250 | StaticAssertionHelper<sizeof(StaticAssertion<static_cast<bool>((test))>)> \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 251 | SEMI_STATIC_JOIN(__StaticAssertTypedef__, __LINE__) |
| 252 | |
| 253 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 254 | extern bool FLAG_enable_slow_asserts; |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 255 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 256 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 257 | // The ASSERT macro is equivalent to CHECK except that it only |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 258 | // generates code in debug builds. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 259 | #ifdef DEBUG |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 260 | #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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 268 | #else |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 269 | #define ASSERT_RESULT(expr) (expr) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 270 | #define ASSERT(condition) ((void) 0) |
| 271 | #define ASSERT_EQ(v1, v2) ((void) 0) |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 272 | #define ASSERT_NE(v1, v2) ((void) 0) |
| 273 | #define ASSERT_GE(v1, v2) ((void) 0) |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 274 | #define ASSERT_LT(v1, v2) ((void) 0) |
| 275 | #define ASSERT_LE(v1, v2) ((void) 0) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 276 | #define SLOW_ASSERT(condition) ((void) 0) |
| 277 | #endif |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 278 | // 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.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 282 | // and release compilation modes behavior. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 283 | #define STATIC_ASSERT(test) STATIC_CHECK(test) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 285 | #define ASSERT_NOT_NULL(p) ASSERT_NE(NULL, p) |
| 286 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 287 | #endif // V8_CHECKS_H_ |