2015-01-30 09:29:25 +00:00
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
[base] Define CHECK comparison for signed vs. unsigned
The current CHECK/DCHECK implementation fails statically if a signed
value is compared against an unsigned value. The common solution is to
cast on each caller, which is tedious and error-prone (might hide bugs).
This CL implements signed vs. unsigned comparisons by executing up to
two comparisons. For example, if i is int32_t and u is uint_32_t, a
DCHECK_LE(i, u) would create the check
i <= 0 || static_cast<uint32_t>(i) <= u.
For checks against constants, at least one of the checks can be removed
by compiler optimizations.
The tradeoff we have to make is to sometimes silently execute an
additional comparison. And we increase code complexity of course, even
though the usage is just as easy (or even easier) as before.
The compile time impact seems to be minimal:
I ran 3 full compilations for Optdebug on my local machine, one time on
the current ToT, one time with this CL plus http://crrev.com/2524093002.
Before: 143.72 +- 1.21 seconds
Now: 144.18 +- 0.67 seconds
In order to check that the new comparisons are working, I refactored
some DCHECKs in wasm to use the new magic, and added unit test cases.
R=ishell@chromium.org, titzer@chromium.org
CC=ahaas@chromium.org, bmeurer@chromium.org
Committed: https://crrev.com/5925074a9dab5a8577766545b91b62f2c531d3dc
Review-Url: https://codereview.chromium.org/2526783002
Cr-Original-Commit-Position: refs/heads/master@{#41275}
Cr-Commit-Position: refs/heads/master@{#41411}
2016-12-01 08:52:31 +00:00
|
|
|
#include <cstdint>
|
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
#include "src/base/logging.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects.h"
|
2018-11-03 00:13:22 +00:00
|
|
|
#include "src/objects/smi.h"
|
2015-01-30 09:29:25 +00:00
|
|
|
#include "testing/gtest-support.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace base {
|
2017-09-28 17:55:52 +00:00
|
|
|
namespace logging_unittest {
|
2015-01-30 09:29:25 +00:00
|
|
|
|
2016-11-29 15:02:16 +00:00
|
|
|
namespace {
|
[base] Define CHECK comparison for signed vs. unsigned
The current CHECK/DCHECK implementation fails statically if a signed
value is compared against an unsigned value. The common solution is to
cast on each caller, which is tedious and error-prone (might hide bugs).
This CL implements signed vs. unsigned comparisons by executing up to
two comparisons. For example, if i is int32_t and u is uint_32_t, a
DCHECK_LE(i, u) would create the check
i <= 0 || static_cast<uint32_t>(i) <= u.
For checks against constants, at least one of the checks can be removed
by compiler optimizations.
The tradeoff we have to make is to sometimes silently execute an
additional comparison. And we increase code complexity of course, even
though the usage is just as easy (or even easier) as before.
The compile time impact seems to be minimal:
I ran 3 full compilations for Optdebug on my local machine, one time on
the current ToT, one time with this CL plus http://crrev.com/2524093002.
Before: 143.72 +- 1.21 seconds
Now: 144.18 +- 0.67 seconds
In order to check that the new comparisons are working, I refactored
some DCHECKs in wasm to use the new magic, and added unit test cases.
R=ishell@chromium.org, titzer@chromium.org
CC=ahaas@chromium.org, bmeurer@chromium.org
Committed: https://crrev.com/5925074a9dab5a8577766545b91b62f2c531d3dc
Review-Url: https://codereview.chromium.org/2526783002
Cr-Original-Commit-Position: refs/heads/master@{#41275}
Cr-Commit-Position: refs/heads/master@{#41411}
2016-12-01 08:52:31 +00:00
|
|
|
|
|
|
|
#define CHECK_SUCCEED(NAME, lhs, rhs) \
|
|
|
|
{ \
|
|
|
|
std::string* error_message = \
|
|
|
|
Check##NAME##Impl<decltype(lhs), decltype(rhs)>((lhs), (rhs), ""); \
|
|
|
|
EXPECT_EQ(nullptr, error_message); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_FAIL(NAME, lhs, rhs) \
|
|
|
|
{ \
|
|
|
|
std::string* error_message = \
|
|
|
|
Check##NAME##Impl<decltype(lhs), decltype(rhs)>((lhs), (rhs), ""); \
|
|
|
|
EXPECT_NE(nullptr, error_message); \
|
2016-12-19 11:58:28 +00:00
|
|
|
delete error_message; \
|
[base] Define CHECK comparison for signed vs. unsigned
The current CHECK/DCHECK implementation fails statically if a signed
value is compared against an unsigned value. The common solution is to
cast on each caller, which is tedious and error-prone (might hide bugs).
This CL implements signed vs. unsigned comparisons by executing up to
two comparisons. For example, if i is int32_t and u is uint_32_t, a
DCHECK_LE(i, u) would create the check
i <= 0 || static_cast<uint32_t>(i) <= u.
For checks against constants, at least one of the checks can be removed
by compiler optimizations.
The tradeoff we have to make is to sometimes silently execute an
additional comparison. And we increase code complexity of course, even
though the usage is just as easy (or even easier) as before.
The compile time impact seems to be minimal:
I ran 3 full compilations for Optdebug on my local machine, one time on
the current ToT, one time with this CL plus http://crrev.com/2524093002.
Before: 143.72 +- 1.21 seconds
Now: 144.18 +- 0.67 seconds
In order to check that the new comparisons are working, I refactored
some DCHECKs in wasm to use the new magic, and added unit test cases.
R=ishell@chromium.org, titzer@chromium.org
CC=ahaas@chromium.org, bmeurer@chromium.org
Committed: https://crrev.com/5925074a9dab5a8577766545b91b62f2c531d3dc
Review-Url: https://codereview.chromium.org/2526783002
Cr-Original-Commit-Position: refs/heads/master@{#41275}
Cr-Commit-Position: refs/heads/master@{#41411}
2016-12-01 08:52:31 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 15:02:16 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
TEST(LoggingTest, CheckEQImpl) {
|
2017-12-02 00:30:37 +00:00
|
|
|
CHECK_SUCCEED(EQ, 0.0, 0.0);
|
|
|
|
CHECK_SUCCEED(EQ, 0.0, -0.0);
|
|
|
|
CHECK_SUCCEED(EQ, -0.0, 0.0);
|
|
|
|
CHECK_SUCCEED(EQ, -0.0, -0.0);
|
[base] Define CHECK comparison for signed vs. unsigned
The current CHECK/DCHECK implementation fails statically if a signed
value is compared against an unsigned value. The common solution is to
cast on each caller, which is tedious and error-prone (might hide bugs).
This CL implements signed vs. unsigned comparisons by executing up to
two comparisons. For example, if i is int32_t and u is uint_32_t, a
DCHECK_LE(i, u) would create the check
i <= 0 || static_cast<uint32_t>(i) <= u.
For checks against constants, at least one of the checks can be removed
by compiler optimizations.
The tradeoff we have to make is to sometimes silently execute an
additional comparison. And we increase code complexity of course, even
though the usage is just as easy (or even easier) as before.
The compile time impact seems to be minimal:
I ran 3 full compilations for Optdebug on my local machine, one time on
the current ToT, one time with this CL plus http://crrev.com/2524093002.
Before: 143.72 +- 1.21 seconds
Now: 144.18 +- 0.67 seconds
In order to check that the new comparisons are working, I refactored
some DCHECKs in wasm to use the new magic, and added unit test cases.
R=ishell@chromium.org, titzer@chromium.org
CC=ahaas@chromium.org, bmeurer@chromium.org
Committed: https://crrev.com/5925074a9dab5a8577766545b91b62f2c531d3dc
Review-Url: https://codereview.chromium.org/2526783002
Cr-Original-Commit-Position: refs/heads/master@{#41275}
Cr-Commit-Position: refs/heads/master@{#41411}
2016-12-01 08:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LoggingTest, CompareSignedMismatch) {
|
2017-12-02 00:30:37 +00:00
|
|
|
CHECK_SUCCEED(EQ, static_cast<int32_t>(14), static_cast<uint32_t>(14));
|
|
|
|
CHECK_FAIL(EQ, static_cast<int32_t>(14), static_cast<uint32_t>(15));
|
|
|
|
CHECK_FAIL(EQ, static_cast<int32_t>(-1), static_cast<uint32_t>(-1));
|
|
|
|
CHECK_SUCCEED(LT, static_cast<int32_t>(-1), static_cast<uint32_t>(0));
|
|
|
|
CHECK_SUCCEED(LT, static_cast<int32_t>(-1), static_cast<uint32_t>(-1));
|
|
|
|
CHECK_SUCCEED(LE, static_cast<int32_t>(-1), static_cast<uint32_t>(0));
|
|
|
|
CHECK_SUCCEED(LE, static_cast<int32_t>(55), static_cast<uint32_t>(55));
|
|
|
|
CHECK_SUCCEED(LT, static_cast<int32_t>(55),
|
|
|
|
static_cast<uint32_t>(0x7FFFFF00));
|
|
|
|
CHECK_SUCCEED(LE, static_cast<int32_t>(55),
|
|
|
|
static_cast<uint32_t>(0x7FFFFF00));
|
|
|
|
CHECK_SUCCEED(GE, static_cast<uint32_t>(0x7FFFFF00),
|
|
|
|
static_cast<int32_t>(55));
|
|
|
|
CHECK_SUCCEED(GT, static_cast<uint32_t>(0x7FFFFF00),
|
|
|
|
static_cast<int32_t>(55));
|
|
|
|
CHECK_SUCCEED(GT, static_cast<uint32_t>(-1), static_cast<int32_t>(-1));
|
|
|
|
CHECK_SUCCEED(GE, static_cast<uint32_t>(0), static_cast<int32_t>(-1));
|
|
|
|
CHECK_SUCCEED(LT, static_cast<int8_t>(-1), static_cast<uint32_t>(0));
|
|
|
|
CHECK_SUCCEED(GT, static_cast<uint64_t>(0x7F01010101010101), 0);
|
|
|
|
CHECK_SUCCEED(LE, static_cast<int64_t>(0xFF01010101010101),
|
|
|
|
static_cast<uint8_t>(13));
|
[base] Define CHECK comparison for signed vs. unsigned
The current CHECK/DCHECK implementation fails statically if a signed
value is compared against an unsigned value. The common solution is to
cast on each caller, which is tedious and error-prone (might hide bugs).
This CL implements signed vs. unsigned comparisons by executing up to
two comparisons. For example, if i is int32_t and u is uint_32_t, a
DCHECK_LE(i, u) would create the check
i <= 0 || static_cast<uint32_t>(i) <= u.
For checks against constants, at least one of the checks can be removed
by compiler optimizations.
The tradeoff we have to make is to sometimes silently execute an
additional comparison. And we increase code complexity of course, even
though the usage is just as easy (or even easier) as before.
The compile time impact seems to be minimal:
I ran 3 full compilations for Optdebug on my local machine, one time on
the current ToT, one time with this CL plus http://crrev.com/2524093002.
Before: 143.72 +- 1.21 seconds
Now: 144.18 +- 0.67 seconds
In order to check that the new comparisons are working, I refactored
some DCHECKs in wasm to use the new magic, and added unit test cases.
R=ishell@chromium.org, titzer@chromium.org
CC=ahaas@chromium.org, bmeurer@chromium.org
Committed: https://crrev.com/5925074a9dab5a8577766545b91b62f2c531d3dc
Review-Url: https://codereview.chromium.org/2526783002
Cr-Original-Commit-Position: refs/heads/master@{#41275}
Cr-Commit-Position: refs/heads/master@{#41411}
2016-12-01 08:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LoggingTest, CompareAgainstStaticConstPointer) {
|
|
|
|
// These used to produce link errors before http://crrev.com/2524093002.
|
2018-11-19 10:36:57 +00:00
|
|
|
CHECK_FAIL(EQ, v8::internal::Smi::zero(), v8::internal::Smi::FromInt(17));
|
[base] Define CHECK comparison for signed vs. unsigned
The current CHECK/DCHECK implementation fails statically if a signed
value is compared against an unsigned value. The common solution is to
cast on each caller, which is tedious and error-prone (might hide bugs).
This CL implements signed vs. unsigned comparisons by executing up to
two comparisons. For example, if i is int32_t and u is uint_32_t, a
DCHECK_LE(i, u) would create the check
i <= 0 || static_cast<uint32_t>(i) <= u.
For checks against constants, at least one of the checks can be removed
by compiler optimizations.
The tradeoff we have to make is to sometimes silently execute an
additional comparison. And we increase code complexity of course, even
though the usage is just as easy (or even easier) as before.
The compile time impact seems to be minimal:
I ran 3 full compilations for Optdebug on my local machine, one time on
the current ToT, one time with this CL plus http://crrev.com/2524093002.
Before: 143.72 +- 1.21 seconds
Now: 144.18 +- 0.67 seconds
In order to check that the new comparisons are working, I refactored
some DCHECKs in wasm to use the new magic, and added unit test cases.
R=ishell@chromium.org, titzer@chromium.org
CC=ahaas@chromium.org, bmeurer@chromium.org
Committed: https://crrev.com/5925074a9dab5a8577766545b91b62f2c531d3dc
Review-Url: https://codereview.chromium.org/2526783002
Cr-Original-Commit-Position: refs/heads/master@{#41275}
Cr-Commit-Position: refs/heads/master@{#41411}
2016-12-01 08:52:31 +00:00
|
|
|
CHECK_SUCCEED(GT, 0, v8::internal::Smi::kMinValue);
|
2015-01-30 09:29:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 15:28:54 +00:00
|
|
|
#define CHECK_BOTH(name, lhs, rhs) \
|
|
|
|
CHECK_##name(lhs, rhs); \
|
|
|
|
DCHECK_##name(lhs, rhs)
|
|
|
|
|
2017-09-21 12:58:36 +00:00
|
|
|
namespace {
|
2020-01-08 18:45:54 +00:00
|
|
|
std::string SanitizeRegexp(std::string msg) {
|
2017-09-21 12:58:36 +00:00
|
|
|
size_t last_pos = 0;
|
|
|
|
do {
|
2020-01-08 18:45:54 +00:00
|
|
|
size_t pos = msg.find_first_of("(){}+*", last_pos);
|
2017-09-21 12:58:36 +00:00
|
|
|
if (pos == std::string::npos) break;
|
2020-01-08 18:45:54 +00:00
|
|
|
msg.insert(pos, "\\");
|
2017-09-21 12:58:36 +00:00
|
|
|
last_pos = pos + 2;
|
|
|
|
} while (true);
|
2020-01-08 18:45:54 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FailureMessage(const char* msg, const char* lhs, const char* rhs) {
|
|
|
|
std::string full_msg(msg);
|
|
|
|
#ifdef DEBUG
|
|
|
|
full_msg.append(" (").append(lhs).append(" vs. ").append(rhs);
|
|
|
|
#endif
|
|
|
|
return SanitizeRegexp(std::move(full_msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LongFailureMessage(const char* msg, const char* lhs,
|
|
|
|
const char* rhs) {
|
|
|
|
std::string full_msg(msg);
|
|
|
|
#ifdef DEBUG
|
|
|
|
full_msg.append("\n ").append(lhs).append("\n vs.\n ").append(rhs);
|
|
|
|
#endif
|
|
|
|
return SanitizeRegexp(std::move(full_msg));
|
2017-09-21 12:58:36 +00:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2017-05-11 18:06:41 +00:00
|
|
|
TEST(LoggingTest, CompareWithDifferentSignedness) {
|
2017-01-20 15:28:54 +00:00
|
|
|
int32_t i32 = 10;
|
|
|
|
uint32_t u32 = 20;
|
|
|
|
int64_t i64 = 30;
|
|
|
|
uint64_t u64 = 40;
|
|
|
|
|
|
|
|
// All these checks should compile (!) and succeed.
|
|
|
|
CHECK_BOTH(EQ, i32 + 10, u32);
|
|
|
|
CHECK_BOTH(LT, i32, u64);
|
|
|
|
CHECK_BOTH(LE, u32, i64);
|
|
|
|
CHECK_BOTH(IMPLIES, i32, i64);
|
|
|
|
CHECK_BOTH(IMPLIES, u32, i64);
|
|
|
|
CHECK_BOTH(IMPLIES, !u32, !i64);
|
2017-09-21 12:58:36 +00:00
|
|
|
|
|
|
|
// Check that the values are output correctly on error.
|
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
([&] { CHECK_GT(i32, u64); })(),
|
2020-01-08 18:45:54 +00:00
|
|
|
FailureMessage("Check failed: i32 > u64", "10", "40"));
|
2017-01-20 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
2017-05-11 18:06:41 +00:00
|
|
|
TEST(LoggingTest, CompareWithReferenceType) {
|
|
|
|
int32_t i32 = 10;
|
|
|
|
uint32_t u32 = 20;
|
|
|
|
int64_t i64 = 30;
|
|
|
|
uint64_t u64 = 40;
|
|
|
|
|
|
|
|
// All these checks should compile (!) and succeed.
|
|
|
|
CHECK_BOTH(EQ, i32 + 10, *&u32);
|
|
|
|
CHECK_BOTH(LT, *&i32, u64);
|
|
|
|
CHECK_BOTH(IMPLIES, *&i32, i64);
|
|
|
|
CHECK_BOTH(IMPLIES, *&i32, u64);
|
2017-09-21 12:58:36 +00:00
|
|
|
|
|
|
|
// Check that the values are output correctly on error.
|
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
([&] { CHECK_GT(*&i32, u64); })(),
|
2020-01-08 18:45:54 +00:00
|
|
|
FailureMessage("Check failed: *&i32 > u64", "10", "40"));
|
2017-09-21 12:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum TestEnum1 { ONE, TWO };
|
|
|
|
enum TestEnum2 : uint16_t { FOO = 14, BAR = 5 };
|
|
|
|
enum class TestEnum3 { A, B };
|
|
|
|
enum class TestEnum4 : uint8_t { FIRST, SECOND };
|
|
|
|
|
|
|
|
TEST(LoggingTest, CompareEnumTypes) {
|
|
|
|
// All these checks should compile (!) and succeed.
|
|
|
|
CHECK_BOTH(EQ, ONE, ONE);
|
|
|
|
CHECK_BOTH(LT, ONE, TWO);
|
|
|
|
CHECK_BOTH(EQ, BAR, 5);
|
|
|
|
CHECK_BOTH(LT, BAR, FOO);
|
|
|
|
CHECK_BOTH(EQ, TestEnum3::A, TestEnum3::A);
|
|
|
|
CHECK_BOTH(LT, TestEnum3::A, TestEnum3::B);
|
|
|
|
CHECK_BOTH(EQ, TestEnum4::FIRST, TestEnum4::FIRST);
|
|
|
|
CHECK_BOTH(LT, TestEnum4::FIRST, TestEnum4::SECOND);
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestClass1 {
|
|
|
|
public:
|
|
|
|
bool operator==(const TestClass1&) const { return true; }
|
|
|
|
bool operator!=(const TestClass1&) const { return false; }
|
|
|
|
};
|
|
|
|
class TestClass2 {
|
|
|
|
public:
|
|
|
|
explicit TestClass2(int val) : val_(val) {}
|
|
|
|
bool operator<(const TestClass2& other) const { return val_ < other.val_; }
|
|
|
|
int val() const { return val_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int val_;
|
|
|
|
};
|
|
|
|
std::ostream& operator<<(std::ostream& str, const TestClass2& val) {
|
|
|
|
return str << "TestClass2(" << val.val() << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LoggingTest, CompareClassTypes) {
|
|
|
|
// All these checks should compile (!) and succeed.
|
|
|
|
CHECK_BOTH(EQ, TestClass1{}, TestClass1{});
|
|
|
|
CHECK_BOTH(LT, TestClass2{2}, TestClass2{7});
|
|
|
|
|
|
|
|
// Check that the values are output correctly on error.
|
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
([&] { CHECK_NE(TestClass1{}, TestClass1{}); })(),
|
|
|
|
FailureMessage("Check failed: TestClass1{} != TestClass1{}",
|
2020-01-08 18:45:54 +00:00
|
|
|
"<unprintable>", "<unprintable>"));
|
2017-09-21 12:58:36 +00:00
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
([&] { CHECK_LT(TestClass2{4}, TestClass2{3}); })(),
|
|
|
|
FailureMessage("Check failed: TestClass2{4} < TestClass2{3}",
|
2020-01-08 18:45:54 +00:00
|
|
|
"TestClass2(4)", "TestClass2(3)"));
|
2017-09-21 12:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LoggingDeathTest, OutputEnumValues) {
|
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
([&] { CHECK_EQ(ONE, TWO); })(),
|
2020-01-08 18:45:54 +00:00
|
|
|
FailureMessage("Check failed: ONE == TWO", "0", "1"));
|
2017-09-21 12:58:36 +00:00
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
([&] { CHECK_NE(BAR, 2 + 3); })(),
|
2020-01-08 18:45:54 +00:00
|
|
|
FailureMessage("Check failed: BAR != 2 + 3", "5", "5"));
|
2017-09-21 12:58:36 +00:00
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
([&] { CHECK_EQ(TestEnum3::A, TestEnum3::B); })(),
|
2020-01-08 18:45:54 +00:00
|
|
|
FailureMessage("Check failed: TestEnum3::A == TestEnum3::B", "0", "1"));
|
2017-09-21 12:58:36 +00:00
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
([&] { CHECK_GE(TestEnum4::FIRST, TestEnum4::SECOND); })(),
|
2020-01-08 18:45:54 +00:00
|
|
|
FailureMessage("Check failed: TestEnum4::FIRST >= TestEnum4::SECOND", "0",
|
|
|
|
"1"));
|
2017-09-21 12:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum TestEnum5 { TEST_A, TEST_B };
|
|
|
|
enum class TestEnum6 { TEST_C, TEST_D };
|
|
|
|
std::ostream& operator<<(std::ostream& str, TestEnum5 val) {
|
|
|
|
return str << (val == TEST_A ? "A" : "B");
|
|
|
|
}
|
|
|
|
void operator<<(std::ostream& str, TestEnum6 val) {
|
|
|
|
str << (val == TestEnum6::TEST_C ? "C" : "D");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LoggingDeathTest, OutputEnumWithOutputOperator) {
|
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
([&] { CHECK_EQ(TEST_A, TEST_B); })(),
|
2020-01-08 18:45:54 +00:00
|
|
|
FailureMessage("Check failed: TEST_A == TEST_B", "A", "B"));
|
2017-09-21 12:58:36 +00:00
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
([&] { CHECK_GE(TestEnum6::TEST_C, TestEnum6::TEST_D); })(),
|
|
|
|
FailureMessage("Check failed: TestEnum6::TEST_C >= TestEnum6::TEST_D",
|
2020-01-08 18:45:54 +00:00
|
|
|
"C", "D"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LoggingDeathTest, OutputLongValues) {
|
|
|
|
constexpr size_t kMaxInlineLength = 50; // see logging.h
|
|
|
|
std::string str1;
|
|
|
|
while (str1.length() < kMaxInlineLength) {
|
|
|
|
str1.push_back('a' + (str1.length() % 26));
|
|
|
|
}
|
|
|
|
std::string str2("abc");
|
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
([&] { CHECK_EQ(str1, str2); })(),
|
|
|
|
FailureMessage("Check failed: str1 == str2",
|
|
|
|
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx",
|
|
|
|
"abc"));
|
|
|
|
str1.push_back('X');
|
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
([&] { CHECK_EQ(str1, str2); })(),
|
|
|
|
LongFailureMessage("Check failed: str1 == str2",
|
|
|
|
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxX",
|
|
|
|
"abc"));
|
2017-05-11 18:06:41 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 18:24:23 +00:00
|
|
|
TEST(LoggingDeathTest, FatalKills) {
|
|
|
|
ASSERT_DEATH_IF_SUPPORTED(FATAL("Dread pirate"), "Dread pirate");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LoggingDeathTest, DcheckIsOnlyFatalInDebug) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
ASSERT_DEATH_IF_SUPPORTED(DCHECK(false && "Dread pirate"), "Dread pirate");
|
|
|
|
#else
|
|
|
|
// DCHECK should be non-fatal if DEBUG is undefined.
|
|
|
|
DCHECK(false && "I'm a benign teapot");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void DcheckOverrideFunction(const char*, int, const char*) {}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(LoggingDeathTest, V8_DcheckCanBeOverridden) {
|
|
|
|
// Default DCHECK state should be fatal.
|
|
|
|
ASSERT_DEATH_IF_SUPPORTED(V8_Dcheck(__FILE__, __LINE__, "Dread pirate"),
|
|
|
|
"Dread pirate");
|
|
|
|
|
|
|
|
ASSERT_DEATH_IF_SUPPORTED(
|
|
|
|
{
|
|
|
|
v8::base::SetDcheckFunction(&DcheckOverrideFunction);
|
|
|
|
// This should be non-fatal.
|
|
|
|
V8_Dcheck(__FILE__, __LINE__, "I'm a benign teapot.");
|
|
|
|
|
|
|
|
// Restore default behavior, and assert on lethality.
|
|
|
|
v8::base::SetDcheckFunction(nullptr);
|
|
|
|
V8_Dcheck(__FILE__, __LINE__, "Dread pirate");
|
|
|
|
},
|
|
|
|
"Dread pirate");
|
|
|
|
}
|
|
|
|
|
Make v8 build with -Wmicrosoft-cast under clang-cl.
gcc and clang (and the standard) don't allow implicit conversion of
function pointers to object pointers. MSVC does allow that, and since
system headers require this to work, clang-cl allows it too -- but
it emits a -Wmicrosoft-cast warning (which we currently suppress in
the Chromium build, but which we want to enable.)
As a side effect, when printing a function pointer to a stream, MSVC
(and clang-cl) will pick the operator<<(void*) overload, while gcc
and clang will pick operator<<(bool) since the best allowed conversion
they find is from function pointer to bool.
To prevent the clang-cl warning, we need to make sure that we never
directly print a function pointer to a stream. In v8, this requires
two changes:
1. Give PrintCheckOperand() an explicit specialization for function
pointers and explicitly cast to void* there. This ports
https://codereview.chromium.org/2515283002/ to V8, and also fixes a
bug on non-Windows where DCHECK() of function pointers would print
"(1 vs 1)" instead of the function's addresses.
(The bug remains with member function pointers,
where it's not clear what to print instead of the 1.)
2. has_output_operator<T> must not use operator<< on its argument
in an evaluated context if T is a function pointer. This patch
modifies has_output_operator<> to use an unevaluated context instead,
which is simpler than the current approach (and matches what Chromium's
base does), but changes behavior in minor (boring) ways
(see template-utils-unittest.cc), since operator<<() is now
called with a temporary and only operator<<() implementations callable
with a temporary are considered.
A more complicated but behavior-preserving alternative would be to
add an explicit specialization for function pointers. You can see
this variant in patch set 1 on gerrit.
Bug: chromium:550065
Change-Id: Idc2854d6c258b7fc0b959604006d8952a79eca3d
Reviewed-on: https://chromium-review.googlesource.com/940004
Commit-Queue: Nico Weber <thakis@chromium.org>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#51636}
2018-02-28 16:11:12 +00:00
|
|
|
#if defined(DEBUG)
|
|
|
|
namespace {
|
|
|
|
int g_log_sink_call_count = 0;
|
|
|
|
void DcheckCountFunction(const char* file, int line, const char* message) {
|
|
|
|
++g_log_sink_call_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DcheckEmptyFunction1() {
|
|
|
|
// Provide a body so that Release builds do not cause the compiler to
|
|
|
|
// optimize DcheckEmptyFunction1 and DcheckEmptyFunction2 as a single
|
|
|
|
// function, which breaks the Dcheck tests below.
|
|
|
|
// Note that this function is never actually called.
|
|
|
|
g_log_sink_call_count += 42;
|
|
|
|
}
|
|
|
|
void DcheckEmptyFunction2() {}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(LoggingTest, LogFunctionPointers) {
|
|
|
|
v8::base::SetDcheckFunction(&DcheckCountFunction);
|
|
|
|
g_log_sink_call_count = 0;
|
|
|
|
void (*fp1)() = DcheckEmptyFunction1;
|
|
|
|
void (*fp2)() = DcheckEmptyFunction2;
|
|
|
|
void (*fp3)() = DcheckEmptyFunction1;
|
|
|
|
DCHECK_EQ(fp1, DcheckEmptyFunction1);
|
|
|
|
DCHECK_EQ(fp1, fp3);
|
|
|
|
EXPECT_EQ(0, g_log_sink_call_count);
|
|
|
|
DCHECK_EQ(fp1, fp2);
|
|
|
|
EXPECT_EQ(1, g_log_sink_call_count);
|
|
|
|
std::string* error_message =
|
|
|
|
CheckEQImpl<decltype(fp1), decltype(fp2)>(fp1, fp2, "");
|
|
|
|
EXPECT_NE(*error_message, "(1 vs 1)");
|
|
|
|
delete error_message;
|
|
|
|
}
|
|
|
|
#endif // defined(DEBUG)
|
|
|
|
|
2017-09-28 17:55:52 +00:00
|
|
|
} // namespace logging_unittest
|
2015-01-30 09:29:25 +00:00
|
|
|
} // namespace base
|
|
|
|
} // namespace v8
|