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

@@ -124,37 +124,6 @@ class IgnoredValue {
IgnoredValue(const T& /* ignored */) {} // NOLINT(runtime/explicit)
};
// The only type that should be convertible to Secret* is nullptr.
// The other null pointer constants are not of a type that is convertible to
// Secret*. Only the literal with the right value is.
template <typename T>
using TypeIsValidNullptrConstant = std::integral_constant<
bool, std::is_same<typename std::decay<T>::type, std::nullptr_t>::value ||
!std::is_convertible<T, Secret*>::value>;
// Two overloaded helpers for checking at compile time whether an
// expression is a null pointer literal (i.e. NULL or any 0-valued
// compile-time integral constant). These helpers have no
// implementations, as we only need their signatures.
//
// Given IsNullLiteralHelper(x), the compiler will pick the first
// version if x can be implicitly converted to Secret*, and pick the
// second version otherwise. Since Secret is a secret and incomplete
// type, the only expression a user can write that has type Secret* is
// a null pointer literal. Therefore, we know that x is a null
// pointer literal if and only if the first version is picked by the
// compiler.
std::true_type IsNullLiteralHelper(Secret*, std::true_type);
std::false_type IsNullLiteralHelper(IgnoredValue, std::false_type);
std::false_type IsNullLiteralHelper(IgnoredValue, std::true_type);
// A compile-time bool constant that is true if and only if x is a null pointer
// literal (i.e. nullptr, NULL or any 0-valued compile-time integral constant).
#define GTEST_IS_NULL_LITERAL_(x) \
decltype(::testing::internal::IsNullLiteralHelper( \
x, \
::testing::internal::TypeIsValidNullptrConstant<decltype(x)>()))::value
// Appends the user-supplied message to the Google-Test-generated message.
GTEST_API_ std::string AppendUserMessage(
const std::string& gtest_msg, const Message& user_msg);