OLD | NEW |
(Empty) | |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 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 |
| 33 #include "flags.h" |
| 34 |
| 35 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...); |
| 36 void API_Fatal(const char* location, const char* format, ...); |
| 37 |
| 38 // The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during |
| 39 // development, but they should not be relied on in the final product. |
| 40 #ifdef DEBUG |
| 41 #define FATAL(msg) \ |
| 42 V8_Fatal(__FILE__, __LINE__, "%s", (msg)) |
| 43 #define UNIMPLEMENTED() \ |
| 44 V8_Fatal(__FILE__, __LINE__, "unimplemented code") |
| 45 #define UNREACHABLE() \ |
| 46 V8_Fatal(__FILE__, __LINE__, "unreachable code") |
| 47 #else |
| 48 #define FATAL(msg) \ |
| 49 V8_Fatal("", 0, "%s", (msg)) |
| 50 #define UNIMPLEMENTED() \ |
| 51 V8_Fatal("", 0, "unimplemented code") |
| 52 #define UNREACHABLE() ((void) 0) |
| 53 #endif |
| 54 |
| 55 |
| 56 // Used by the CHECK macro -- should not be called directly. |
| 57 static inline void CheckHelper(const char* file, |
| 58 int line, |
| 59 const char* source, |
| 60 bool condition) { |
| 61 if (!condition) |
| 62 V8_Fatal(file, line, "CHECK(%s) failed", source); |
| 63 } |
| 64 |
| 65 |
| 66 // The CHECK macro checks that the given condition is true; if not, it |
| 67 // prints a message to stderr and aborts. |
| 68 #define CHECK(condition) CheckHelper(__FILE__, __LINE__, #condition, condition) |
| 69 |
| 70 |
| 71 // Helper function used by the CHECK_EQ function when given int |
| 72 // arguments. Should not be called directly. |
| 73 static inline void CheckEqualsHelper(const char* file, int line, |
| 74 const char* expected_source, int expected, |
| 75 const char* value_source, int value) { |
| 76 if (expected != value) { |
| 77 V8_Fatal(file, line, |
| 78 "CHECK_EQ(%s, %s) failed\n# Expected: %i\n# Found: %i", |
| 79 expected_source, value_source, expected, value); |
| 80 } |
| 81 } |
| 82 |
| 83 |
| 84 // Helper function used by the CHECK_EQ function when given int64_t |
| 85 // arguments. Should not be called directly. |
| 86 static inline void CheckEqualsHelper(const char* file, int line, |
| 87 const char* expected_source, |
| 88 int64_t expected, |
| 89 const char* value_source, |
| 90 int64_t value) { |
| 91 if (expected != value) { |
| 92 // Print int64_t values in hex, as two int32s, |
| 93 // to avoid platform-dependencies. |
| 94 V8_Fatal(file, line, |
| 95 "CHECK_EQ(%s, %s) failed\n#" |
| 96 " Expected: 0x%08x%08x\n# Found: 0x%08x%08x", |
| 97 expected_source, value_source, |
| 98 static_cast<uint32_t>(expected >> 32), |
| 99 static_cast<uint32_t>(expected), |
| 100 static_cast<uint32_t>(value >> 32), |
| 101 static_cast<uint32_t>(value)); |
| 102 } |
| 103 } |
| 104 |
| 105 |
| 106 // Helper function used by the CHECK_NE function when given int |
| 107 // arguments. Should not be called directly. |
| 108 static inline void CheckNonEqualsHelper(const char* file, |
| 109 int line, |
| 110 const char* unexpected_source, |
| 111 int unexpected, |
| 112 const char* value_source, |
| 113 int value) { |
| 114 if (unexpected == value) { |
| 115 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i", |
| 116 unexpected_source, value_source, value); |
| 117 } |
| 118 } |
| 119 |
| 120 |
| 121 // Helper function used by the CHECK function when given string |
| 122 // arguments. Should not be called directly. |
| 123 static inline void CheckEqualsHelper(const char* file, |
| 124 int line, |
| 125 const char* expected_source, |
| 126 const char* expected, |
| 127 const char* value_source, |
| 128 const char* value) { |
| 129 if ((expected == NULL && value != NULL) || |
| 130 (expected != NULL && value == NULL) || |
| 131 (expected != NULL && value != NULL && strcmp(expected, value) != 0)) { |
| 132 V8_Fatal(file, line, |
| 133 "CHECK_EQ(%s, %s) failed\n# Expected: %s\n# Found: %s", |
| 134 expected_source, value_source, expected, value); |
| 135 } |
| 136 } |
| 137 |
| 138 |
| 139 static inline void CheckNonEqualsHelper(const char* file, |
| 140 int line, |
| 141 const char* expected_source, |
| 142 const char* expected, |
| 143 const char* value_source, |
| 144 const char* value) { |
| 145 if (expected == value || |
| 146 (expected != NULL && value != NULL && strcmp(expected, value) == 0)) { |
| 147 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %s", |
| 148 expected_source, value_source, value); |
| 149 } |
| 150 } |
| 151 |
| 152 |
| 153 // Helper function used by the CHECK function when given pointer |
| 154 // arguments. Should not be called directly. |
| 155 static inline void CheckEqualsHelper(const char* file, |
| 156 int line, |
| 157 const char* expected_source, |
| 158 const void* expected, |
| 159 const char* value_source, |
| 160 const void* value) { |
| 161 if (expected != value) { |
| 162 V8_Fatal(file, line, |
| 163 "CHECK_EQ(%s, %s) failed\n# Expected: %p\n# Found: %p", |
| 164 expected_source, value_source, |
| 165 expected, value); |
| 166 } |
| 167 } |
| 168 |
| 169 |
| 170 static inline void CheckNonEqualsHelper(const char* file, |
| 171 int line, |
| 172 const char* expected_source, |
| 173 const void* expected, |
| 174 const char* value_source, |
| 175 const void* value) { |
| 176 if (expected == value) { |
| 177 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p", |
| 178 expected_source, value_source, value); |
| 179 } |
| 180 } |
| 181 |
| 182 |
| 183 // Helper function used by the CHECK function when given floating |
| 184 // point arguments. Should not be called directly. |
| 185 static inline void CheckEqualsHelper(const char* file, |
| 186 int line, |
| 187 const char* expected_source, |
| 188 double expected, |
| 189 const char* value_source, |
| 190 double value) { |
| 191 // Force values to 64 bit memory to truncate 80 bit precision on IA32. |
| 192 volatile double* exp = new double[1]; |
| 193 *exp = expected; |
| 194 volatile double* val = new double[1]; |
| 195 *val = value; |
| 196 if (*exp != *val) { |
| 197 V8_Fatal(file, line, |
| 198 "CHECK_EQ(%s, %s) failed\n# Expected: %f\n# Found: %f", |
| 199 expected_source, value_source, *exp, *val); |
| 200 } |
| 201 delete[] exp; |
| 202 delete[] val; |
| 203 } |
| 204 |
| 205 |
| 206 static inline void CheckNonEqualsHelper(const char* file, |
| 207 int line, |
| 208 const char* expected_source, |
| 209 double expected, |
| 210 const char* value_source, |
| 211 double value) { |
| 212 // Force values to 64 bit memory to truncate 80 bit precision on IA32. |
| 213 volatile double* exp = new double[1]; |
| 214 *exp = expected; |
| 215 volatile double* val = new double[1]; |
| 216 *val = value; |
| 217 if (*exp == *val) { |
| 218 V8_Fatal(file, line, |
| 219 "CHECK_NE(%s, %s) failed\n# Value: %f", |
| 220 expected_source, value_source, *val); |
| 221 } |
| 222 delete[] exp; |
| 223 delete[] val; |
| 224 } |
| 225 |
| 226 |
| 227 namespace v8 { |
| 228 class Value; |
| 229 template <class T> class Handle; |
| 230 } |
| 231 |
| 232 |
| 233 void CheckNonEqualsHelper(const char* file, |
| 234 int line, |
| 235 const char* unexpected_source, |
| 236 v8::Handle<v8::Value> unexpected, |
| 237 const char* value_source, |
| 238 v8::Handle<v8::Value> value); |
| 239 |
| 240 |
| 241 void CheckEqualsHelper(const char* file, |
| 242 int line, |
| 243 const char* expected_source, |
| 244 v8::Handle<v8::Value> expected, |
| 245 const char* value_source, |
| 246 v8::Handle<v8::Value> value); |
| 247 |
| 248 |
| 249 #define CHECK_EQ(expected, value) CheckEqualsHelper(__FILE__, __LINE__, \ |
| 250 #expected, expected, #value, value) |
| 251 |
| 252 |
| 253 #define CHECK_NE(unexpected, value) CheckNonEqualsHelper(__FILE__, __LINE__, \ |
| 254 #unexpected, unexpected, #value, value) |
| 255 |
| 256 |
| 257 #define CHECK_GT(a, b) CHECK((a) > (b)) |
| 258 #define CHECK_GE(a, b) CHECK((a) >= (b)) |
| 259 |
| 260 |
| 261 // This is inspired by the static assertion facility in boost. This |
| 262 // is pretty magical. If it causes you trouble on a platform you may |
| 263 // find a fix in the boost code. |
| 264 template <bool> class StaticAssertion; |
| 265 template <> class StaticAssertion<true> { }; |
| 266 // This macro joins two tokens. If one of the tokens is a macro the |
| 267 // helper call causes it to be resolved before joining. |
| 268 #define SEMI_STATIC_JOIN(a, b) SEMI_STATIC_JOIN_HELPER(a, b) |
| 269 #define SEMI_STATIC_JOIN_HELPER(a, b) a##b |
| 270 // Causes an error during compilation of the condition is not |
| 271 // statically known to be true. It is formulated as a typedef so that |
| 272 // it can be used wherever a typedef can be used. Beware that this |
| 273 // actually causes each use to introduce a new defined type with a |
| 274 // name depending on the source line. |
| 275 template <int> class StaticAssertionHelper { }; |
| 276 #define STATIC_CHECK(test) \ |
| 277 typedef \ |
| 278 StaticAssertionHelper<sizeof(StaticAssertion<static_cast<bool>(test)>)> \ |
| 279 SEMI_STATIC_JOIN(__StaticAssertTypedef__, __LINE__) |
| 280 |
| 281 |
| 282 // The ASSERT macro is equivalent to CHECK except that it only |
| 283 // generates code in debug builds. |
| 284 #ifdef DEBUG |
| 285 #define ASSERT_RESULT(expr) CHECK(expr) |
| 286 #define ASSERT(condition) CHECK(condition) |
| 287 #define ASSERT_EQ(v1, v2) CHECK_EQ(v1, v2) |
| 288 #define ASSERT_NE(v1, v2) CHECK_NE(v1, v2) |
| 289 #define ASSERT_GE(v1, v2) CHECK_GE(v1, v2) |
| 290 #define SLOW_ASSERT(condition) if (FLAG_enable_slow_asserts) CHECK(condition) |
| 291 #else |
| 292 #define ASSERT_RESULT(expr) (expr) |
| 293 #define ASSERT(condition) ((void) 0) |
| 294 #define ASSERT_EQ(v1, v2) ((void) 0) |
| 295 #define ASSERT_NE(v1, v2) ((void) 0) |
| 296 #define ASSERT_GE(v1, v2) ((void) 0) |
| 297 #define SLOW_ASSERT(condition) ((void) 0) |
| 298 #endif |
| 299 // 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 |
| 303 // and release compilation modes behaviour. |
| 304 #define STATIC_ASSERT(test) STATIC_CHECK(test) |
| 305 |
| 306 |
| 307 #define ASSERT_TAG_ALIGNED(address) \ |
| 308 ASSERT((reinterpret_cast<intptr_t>(address) & kHeapObjectTagMask) == 0) |
| 309 |
| 310 #define ASSERT_SIZE_TAG_ALIGNED(size) ASSERT((size & kHeapObjectTagMask) == 0) |
| 311 |
| 312 #define ASSERT_NOT_NULL(p) ASSERT_NE(NULL, p) |
| 313 |
| 314 #endif // V8_CHECKS_H_ |
OLD | NEW |