2012-01-04 14:45:29 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#ifndef V8_CHECKS_H_
|
|
|
|
#define V8_CHECKS_H_
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-08-23 07:32:25 +00:00
|
|
|
#include "../include/v8stdint.h"
|
2013-08-21 10:43:50 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
extern "C" void V8_Fatal(const char* file, int line, const char* format, ...);
|
|
|
|
|
|
|
|
// The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during
|
|
|
|
// development, but they should not be relied on in the final product.
|
2008-09-22 09:22:18 +00:00
|
|
|
#ifdef DEBUG
|
2008-07-03 15:10:15 +00:00
|
|
|
#define FATAL(msg) \
|
|
|
|
V8_Fatal(__FILE__, __LINE__, "%s", (msg))
|
2008-09-22 09:22:18 +00:00
|
|
|
#define UNIMPLEMENTED() \
|
|
|
|
V8_Fatal(__FILE__, __LINE__, "unimplemented code")
|
2008-07-03 15:10:15 +00:00
|
|
|
#define UNREACHABLE() \
|
|
|
|
V8_Fatal(__FILE__, __LINE__, "unreachable code")
|
2008-09-22 09:22:18 +00:00
|
|
|
#else
|
|
|
|
#define FATAL(msg) \
|
|
|
|
V8_Fatal("", 0, "%s", (msg))
|
2008-07-03 15:10:15 +00:00
|
|
|
#define UNIMPLEMENTED() \
|
2008-09-22 09:22:18 +00:00
|
|
|
V8_Fatal("", 0, "unimplemented code")
|
|
|
|
#define UNREACHABLE() ((void) 0)
|
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// The CHECK macro checks that the given condition is true; if not, it
|
|
|
|
// prints a message to stderr and aborts.
|
2012-01-30 13:02:48 +00:00
|
|
|
#define CHECK(condition) do { \
|
|
|
|
if (!(condition)) { \
|
|
|
|
V8_Fatal(__FILE__, __LINE__, "CHECK(%s) failed", #condition); \
|
|
|
|
} \
|
2011-10-25 09:30:52 +00:00
|
|
|
} while (0)
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Helper function used by the CHECK_EQ function when given int
|
|
|
|
// arguments. Should not be called directly.
|
2011-11-29 10:56:11 +00:00
|
|
|
inline void CheckEqualsHelper(const char* file, int line,
|
|
|
|
const char* expected_source, int expected,
|
|
|
|
const char* value_source, int value) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (expected != value) {
|
|
|
|
V8_Fatal(file, line,
|
|
|
|
"CHECK_EQ(%s, %s) failed\n# Expected: %i\n# Found: %i",
|
|
|
|
expected_source, value_source, expected, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-11 14:49:35 +00:00
|
|
|
|
2009-10-08 12:54:29 +00:00
|
|
|
// Helper function used by the CHECK_EQ function when given int64_t
|
|
|
|
// arguments. Should not be called directly.
|
2011-11-29 10:56:11 +00:00
|
|
|
inline void CheckEqualsHelper(const char* file, int line,
|
|
|
|
const char* expected_source,
|
|
|
|
int64_t expected,
|
|
|
|
const char* value_source,
|
|
|
|
int64_t value) {
|
2009-10-08 12:54:29 +00:00
|
|
|
if (expected != value) {
|
|
|
|
// Print int64_t values in hex, as two int32s,
|
|
|
|
// to avoid platform-dependencies.
|
|
|
|
V8_Fatal(file, line,
|
|
|
|
"CHECK_EQ(%s, %s) failed\n#"
|
|
|
|
" Expected: 0x%08x%08x\n# Found: 0x%08x%08x",
|
|
|
|
expected_source, value_source,
|
|
|
|
static_cast<uint32_t>(expected >> 32),
|
|
|
|
static_cast<uint32_t>(expected),
|
|
|
|
static_cast<uint32_t>(value >> 32),
|
|
|
|
static_cast<uint32_t>(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Helper function used by the CHECK_NE function when given int
|
|
|
|
// arguments. Should not be called directly.
|
2011-11-29 10:56:11 +00:00
|
|
|
inline void CheckNonEqualsHelper(const char* file,
|
|
|
|
int line,
|
|
|
|
const char* unexpected_source,
|
|
|
|
int unexpected,
|
|
|
|
const char* value_source,
|
|
|
|
int value) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (unexpected == value) {
|
|
|
|
V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i",
|
|
|
|
unexpected_source, value_source, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function used by the CHECK function when given string
|
|
|
|
// arguments. Should not be called directly.
|
2011-11-29 10:56:11 +00:00
|
|
|
inline void CheckEqualsHelper(const char* file,
|
|
|
|
int line,
|
|
|
|
const char* expected_source,
|
|
|
|
const char* expected,
|
|
|
|
const char* value_source,
|
|
|
|
const char* value) {
|
2010-02-17 13:23:46 +00:00
|
|
|
if ((expected == NULL && value != NULL) ||
|
|
|
|
(expected != NULL && value == NULL) ||
|
|
|
|
(expected != NULL && value != NULL && strcmp(expected, value) != 0)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
V8_Fatal(file, line,
|
|
|
|
"CHECK_EQ(%s, %s) failed\n# Expected: %s\n# Found: %s",
|
|
|
|
expected_source, value_source, expected, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 10:56:11 +00:00
|
|
|
inline void CheckNonEqualsHelper(const char* file,
|
|
|
|
int line,
|
|
|
|
const char* expected_source,
|
|
|
|
const char* expected,
|
|
|
|
const char* value_source,
|
|
|
|
const char* value) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (expected == value ||
|
|
|
|
(expected != NULL && value != NULL && strcmp(expected, value) == 0)) {
|
|
|
|
V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %s",
|
|
|
|
expected_source, value_source, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function used by the CHECK function when given pointer
|
|
|
|
// arguments. Should not be called directly.
|
2011-11-29 10:56:11 +00:00
|
|
|
inline void CheckEqualsHelper(const char* file,
|
|
|
|
int line,
|
|
|
|
const char* expected_source,
|
|
|
|
const void* expected,
|
|
|
|
const char* value_source,
|
|
|
|
const void* value) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (expected != value) {
|
|
|
|
V8_Fatal(file, line,
|
2009-01-22 14:00:23 +00:00
|
|
|
"CHECK_EQ(%s, %s) failed\n# Expected: %p\n# Found: %p",
|
2008-07-03 15:10:15 +00:00
|
|
|
expected_source, value_source,
|
2009-01-22 14:00:23 +00:00
|
|
|
expected, value);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 10:56:11 +00:00
|
|
|
inline void CheckNonEqualsHelper(const char* file,
|
|
|
|
int line,
|
|
|
|
const char* expected_source,
|
|
|
|
const void* expected,
|
|
|
|
const char* value_source,
|
|
|
|
const void* value) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (expected == value) {
|
2009-01-22 14:00:23 +00:00
|
|
|
V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p",
|
|
|
|
expected_source, value_source, value);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function used by the CHECK function when given floating
|
|
|
|
// point arguments. Should not be called directly.
|
2011-11-29 10:56:11 +00:00
|
|
|
inline void CheckEqualsHelper(const char* file,
|
|
|
|
int line,
|
|
|
|
const char* expected_source,
|
|
|
|
double expected,
|
|
|
|
const char* value_source,
|
|
|
|
double value) {
|
2008-09-26 23:14:43 +00:00
|
|
|
// Force values to 64 bit memory to truncate 80 bit precision on IA32.
|
|
|
|
volatile double* exp = new double[1];
|
|
|
|
*exp = expected;
|
|
|
|
volatile double* val = new double[1];
|
|
|
|
*val = value;
|
|
|
|
if (*exp != *val) {
|
2008-07-03 15:10:15 +00:00
|
|
|
V8_Fatal(file, line,
|
|
|
|
"CHECK_EQ(%s, %s) failed\n# Expected: %f\n# Found: %f",
|
2008-09-26 23:14:43 +00:00
|
|
|
expected_source, value_source, *exp, *val);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-09-26 23:14:43 +00:00
|
|
|
delete[] exp;
|
|
|
|
delete[] val;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-12 12:08:54 +00:00
|
|
|
inline void CheckNonEqualsHelper(const char* file,
|
|
|
|
int line,
|
|
|
|
const char* expected_source,
|
|
|
|
int64_t expected,
|
|
|
|
const char* value_source,
|
|
|
|
int64_t value) {
|
|
|
|
if (expected == value) {
|
|
|
|
V8_Fatal(file, line,
|
|
|
|
"CHECK_EQ(%s, %s) failed\n# Expected: %f\n# Found: %f",
|
|
|
|
expected_source, value_source, expected, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 10:56:11 +00:00
|
|
|
inline void CheckNonEqualsHelper(const char* file,
|
|
|
|
int line,
|
|
|
|
const char* expected_source,
|
|
|
|
double expected,
|
|
|
|
const char* value_source,
|
|
|
|
double value) {
|
2010-03-11 14:49:35 +00:00
|
|
|
// Force values to 64 bit memory to truncate 80 bit precision on IA32.
|
|
|
|
volatile double* exp = new double[1];
|
|
|
|
*exp = expected;
|
|
|
|
volatile double* val = new double[1];
|
|
|
|
*val = value;
|
|
|
|
if (*exp == *val) {
|
|
|
|
V8_Fatal(file, line,
|
|
|
|
"CHECK_NE(%s, %s) failed\n# Value: %f",
|
|
|
|
expected_source, value_source, *val);
|
|
|
|
}
|
|
|
|
delete[] exp;
|
|
|
|
delete[] val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#define CHECK_EQ(expected, value) CheckEqualsHelper(__FILE__, __LINE__, \
|
|
|
|
#expected, expected, #value, value)
|
|
|
|
|
|
|
|
|
|
|
|
#define CHECK_NE(unexpected, value) CheckNonEqualsHelper(__FILE__, __LINE__, \
|
|
|
|
#unexpected, unexpected, #value, value)
|
|
|
|
|
|
|
|
|
|
|
|
#define CHECK_GT(a, b) CHECK((a) > (b))
|
|
|
|
#define CHECK_GE(a, b) CHECK((a) >= (b))
|
2010-12-07 14:03:59 +00:00
|
|
|
#define CHECK_LT(a, b) CHECK((a) < (b))
|
|
|
|
#define CHECK_LE(a, b) CHECK((a) <= (b))
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2013-07-18 07:43:41 +00:00
|
|
|
// Use C++11 static_assert if possible, which gives error
|
|
|
|
// messages that are easier to understand on first sight.
|
2013-08-23 07:32:25 +00:00
|
|
|
#if V8_HAS_CXX11_STATIC_ASSERT
|
2013-07-18 07:43:41 +00:00
|
|
|
#define STATIC_CHECK(test) static_assert(test, #test)
|
|
|
|
#else
|
2008-07-03 15:10:15 +00:00
|
|
|
// This is inspired by the static assertion facility in boost. This
|
|
|
|
// is pretty magical. If it causes you trouble on a platform you may
|
|
|
|
// find a fix in the boost code.
|
|
|
|
template <bool> class StaticAssertion;
|
|
|
|
template <> class StaticAssertion<true> { };
|
|
|
|
// This macro joins two tokens. If one of the tokens is a macro the
|
|
|
|
// helper call causes it to be resolved before joining.
|
|
|
|
#define SEMI_STATIC_JOIN(a, b) SEMI_STATIC_JOIN_HELPER(a, b)
|
|
|
|
#define SEMI_STATIC_JOIN_HELPER(a, b) a##b
|
|
|
|
// Causes an error during compilation of the condition is not
|
|
|
|
// statically known to be true. It is formulated as a typedef so that
|
|
|
|
// it can be used wherever a typedef can be used. Beware that this
|
|
|
|
// actually causes each use to introduce a new defined type with a
|
|
|
|
// name depending on the source line.
|
|
|
|
template <int> class StaticAssertionHelper { };
|
2011-08-29 11:41:23 +00:00
|
|
|
#define STATIC_CHECK(test) \
|
|
|
|
typedef \
|
|
|
|
StaticAssertionHelper<sizeof(StaticAssertion<static_cast<bool>((test))>)> \
|
2013-11-12 12:09:38 +00:00
|
|
|
SEMI_STATIC_JOIN(__StaticAssertTypedef__, __LINE__) V8_UNUSED
|
2013-07-31 07:51:46 +00:00
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2013-10-25 11:10:28 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
#ifndef OPTIMIZED_DEBUG
|
|
|
|
#define ENABLE_SLOW_ASSERTS 1
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
#ifdef ENABLE_SLOW_ASSERTS
|
|
|
|
#define SLOW_ASSERT(condition) \
|
|
|
|
CHECK(!v8::internal::FLAG_enable_slow_asserts || (condition))
|
2011-10-13 11:54:19 +00:00
|
|
|
extern bool FLAG_enable_slow_asserts;
|
2013-10-25 11:10:28 +00:00
|
|
|
#else
|
|
|
|
#define SLOW_ASSERT(condition) ((void) 0)
|
|
|
|
const bool FLAG_enable_slow_asserts = false;
|
|
|
|
#endif
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2010-11-05 13:33:40 +00:00
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// The ASSERT macro is equivalent to CHECK except that it only
|
2010-08-10 09:22:49 +00:00
|
|
|
// generates code in debug builds.
|
2008-07-03 15:10:15 +00:00
|
|
|
#ifdef DEBUG
|
2012-01-04 14:45:29 +00:00
|
|
|
#define ASSERT_RESULT(expr) CHECK(expr)
|
|
|
|
#define ASSERT(condition) CHECK(condition)
|
|
|
|
#define ASSERT_EQ(v1, v2) CHECK_EQ(v1, v2)
|
|
|
|
#define ASSERT_NE(v1, v2) CHECK_NE(v1, v2)
|
|
|
|
#define ASSERT_GE(v1, v2) CHECK_GE(v1, v2)
|
|
|
|
#define ASSERT_LT(v1, v2) CHECK_LT(v1, v2)
|
|
|
|
#define ASSERT_LE(v1, v2) CHECK_LE(v1, v2)
|
2008-07-03 15:10:15 +00:00
|
|
|
#else
|
2012-01-04 14:45:29 +00:00
|
|
|
#define ASSERT_RESULT(expr) (expr)
|
2008-07-03 15:10:15 +00:00
|
|
|
#define ASSERT(condition) ((void) 0)
|
|
|
|
#define ASSERT_EQ(v1, v2) ((void) 0)
|
2010-06-08 12:04:49 +00:00
|
|
|
#define ASSERT_NE(v1, v2) ((void) 0)
|
|
|
|
#define ASSERT_GE(v1, v2) ((void) 0)
|
2011-04-01 11:54:04 +00:00
|
|
|
#define ASSERT_LT(v1, v2) ((void) 0)
|
|
|
|
#define ASSERT_LE(v1, v2) ((void) 0)
|
2008-07-03 15:10:15 +00:00
|
|
|
#endif
|
2010-08-10 09:22:49 +00:00
|
|
|
// Static asserts has no impact on runtime performance, so they can be
|
|
|
|
// safely enabled in release mode. Moreover, the ((void) 0) expression
|
|
|
|
// obeys different syntax rules than typedef's, e.g. it can't appear
|
|
|
|
// inside class declaration, this leads to inconsistency between debug
|
2010-12-07 11:31:57 +00:00
|
|
|
// and release compilation modes behavior.
|
2010-08-10 09:22:49 +00:00
|
|
|
#define STATIC_ASSERT(test) STATIC_CHECK(test)
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
#define ASSERT_NOT_NULL(p) ASSERT_NE(NULL, p)
|
|
|
|
|
2012-09-05 16:06:53 +00:00
|
|
|
// "Extra checks" are lightweight checks that are enabled in some release
|
|
|
|
// builds.
|
|
|
|
#ifdef ENABLE_EXTRA_CHECKS
|
|
|
|
#define EXTRA_CHECK(condition) CHECK(condition)
|
|
|
|
#else
|
|
|
|
#define EXTRA_CHECK(condition) ((void) 0)
|
|
|
|
#endif
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#endif // V8_CHECKS_H_
|