Googletest export

Fix emission of -Wzero-as-null-pointer-constant when comparing integers.

The following code fails to compile:

#pragma clang diagnostic error "-Wzero-as-null-pointer-constant"
void foo() {
  EXPECT_EQ(0, 0);
}

This happens because gtest checks the first argument to EXPECT_EQ and
ASSERT_EQ is a null pointer constant. The magic it does to do this causes the
warning to be emitted.

This patch removes that check. It replaces the explicit check with a Compare
overload that can only be selected when 0 or nullptr is passed on the LHS
with a pointer on the right.

This patch does not suppress -Wzero-as-null-pointer-constant when users
are actually using it as NULL.

PiperOrigin-RevId: 236654634
This commit is contained in:
Abseil Team
2019-03-04 11:10:22 -05:00
committed by Gennadiy Civil
parent a1dd07786b
commit 3dd2e841c3
3 changed files with 65 additions and 97 deletions

View File

@@ -515,22 +515,23 @@ TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsEpochStart) {
# pragma option push -w-ccc -w-rch
# endif
// Tests that GTEST_IS_NULL_LITERAL_(x) is true when x is a null
// pointer literal.
TEST(NullLiteralTest, IsTrueForNullLiterals) {
EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(NULL)); // NOLINT
EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0)); // NOLINT
EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0u)); // NOLINT
EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(nullptr));
}
// Tests that the LHS of EXPECT_EQ or ASSERT_EQ can be used as a null literal
// when the RHS is a pointer type.
TEST(NullLiteralTest, LHSAllowsNullLiterals) {
EXPECT_EQ(0, static_cast<void*>(nullptr)); // NOLINT
ASSERT_EQ(0, static_cast<void*>(nullptr)); // NOLINT
EXPECT_EQ(NULL, static_cast<void*>(nullptr)); // NOLINT
ASSERT_EQ(NULL, static_cast<void*>(nullptr)); // NOLINT
EXPECT_EQ(nullptr, static_cast<void*>(nullptr));
ASSERT_EQ(nullptr, static_cast<void*>(nullptr));
// Tests that GTEST_IS_NULL_LITERAL_(x) is false when x is not a null
// pointer literal.
TEST(NullLiteralTest, IsFalseForNonNullLiterals) {
EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(1));
EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(0.0));
EXPECT_FALSE(GTEST_IS_NULL_LITERAL_('a'));
EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(static_cast<void*>(nullptr)));
const int* const p = nullptr;
EXPECT_EQ(0, p); // NOLINT
ASSERT_EQ(0, p); // NOLINT
EXPECT_EQ(NULL, p); // NOLINT
ASSERT_EQ(NULL, p); // NOLINT
EXPECT_EQ(nullptr, p);
ASSERT_EQ(nullptr, p);
}
struct ConvertToAll {
@@ -540,6 +541,13 @@ struct ConvertToAll {
}
};
struct ConvertToPointer {
template <class T>
operator T*() const { // NOLINT
return nullptr;
}
};
struct ConvertToAllButNoPointers {
template <typename T,
typename std::enable_if<!std::is_pointer<T>::value, int>::type = 0>
@@ -548,11 +556,37 @@ struct ConvertToAllButNoPointers {
}
};
struct MyType {};
inline bool operator==(MyType const&, MyType const&) { return true; }
TEST(NullLiteralTest, ImplicitConversion) {
EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(ConvertToAll{}));
EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(ConvertToAllButNoPointers{}));
EXPECT_EQ(ConvertToPointer{}, static_cast<void*>(nullptr));
#if !defined(__GNUC__) || defined(__clang__)
// Disabled due to GCC bug gcc.gnu.org/PR89580
EXPECT_EQ(ConvertToAll{}, static_cast<void*>(nullptr));
#endif
EXPECT_EQ(ConvertToAll{}, MyType{});
EXPECT_EQ(ConvertToAllButNoPointers{}, MyType{});
}
#ifdef __clang__
#pragma clang diagnostic push
#if __has_warning("-Wzero-as-null-pointer-constant")
#pragma clang diagnostic error "-Wzero-as-null-pointer-constant"
#endif
#endif
TEST(NullLiteralTest, NoConversionNoWarning) {
// Test that gtests detection and handling of null pointer constants
// doesn't trigger a warning when '0' isn't actually used as null.
EXPECT_EQ(0, 0);
ASSERT_EQ(0, 0);
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
# ifdef __BORLANDC__
// Restores warnings after previous "#pragma option push" suppressed them.
# pragma option pop