Eliminate the legacy GTEST_COMPILE_ASSERT_ macro

PiperOrigin-RevId: 443462203
Change-Id: I0c43f981663a7531ff5da4d4be01fb3d6762273d
This commit is contained in:
Derek Mauro 2022-04-21 13:22:38 -07:00 committed by Copybara-Service
parent 8ccdb9d56d
commit b85864c647
4 changed files with 39 additions and 55 deletions

View File

@ -915,9 +915,8 @@ class ReturnAction {
// and put the typedef both here (for use in assert statement) and
// in the Impl class. But both definitions must be the same.
typedef typename Function<F>::Result Result;
GTEST_COMPILE_ASSERT_(
!std::is_reference<Result>::value,
use_ReturnRef_instead_of_Return_to_return_a_reference);
static_assert(!std::is_reference<Result>::value,
"use ReturnRef instead of Return to return a reference");
static_assert(!std::is_void<Result>::value,
"Can't use Return() on an action expected to return `void`.");
return Action<F>(new Impl<R, F>(value_));
@ -945,8 +944,8 @@ class ReturnAction {
Result Perform(const ArgumentTuple&) override { return value_; }
private:
GTEST_COMPILE_ASSERT_(!std::is_reference<Result>::value,
Result_cannot_be_a_reference_type);
static_assert(!std::is_reference<Result>::value,
"Result cannot be a reference type");
// We save the value before casting just in case it is being cast to a
// wrapper type.
R value_before_cast_;
@ -1020,8 +1019,8 @@ class ReturnRefAction {
// Asserts that the function return type is a reference. This
// catches the user error of using ReturnRef(x) when Return(x)
// should be used, and generates some helpful error message.
GTEST_COMPILE_ASSERT_(std::is_reference<Result>::value,
use_Return_instead_of_ReturnRef_to_return_a_value);
static_assert(std::is_reference<Result>::value,
"use Return instead of ReturnRef to return a value");
return Action<F>(new Impl<F>(ref_));
}
@ -1062,9 +1061,8 @@ class ReturnRefOfCopyAction {
// Asserts that the function return type is a reference. This
// catches the user error of using ReturnRefOfCopy(x) when Return(x)
// should be used, and generates some helpful error message.
GTEST_COMPILE_ASSERT_(
std::is_reference<Result>::value,
use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
static_assert(std::is_reference<Result>::value,
"use Return instead of ReturnRefOfCopy to return a value");
return Action<F>(new Impl<F>(value_));
}

View File

@ -533,19 +533,18 @@ inline Matcher<T> SafeMatcherCast(const Matcher<U>& matcher) {
"T must be implicitly convertible to U");
// Enforce that we are not converting a non-reference type T to a reference
// type U.
GTEST_COMPILE_ASSERT_(
std::is_reference<T>::value || !std::is_reference<U>::value,
cannot_convert_non_reference_arg_to_reference);
static_assert(std::is_reference<T>::value || !std::is_reference<U>::value,
"cannot convert non reference arg to reference");
// In case both T and U are arithmetic types, enforce that the
// conversion is not lossy.
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT;
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU;
constexpr bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther;
constexpr bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther;
GTEST_COMPILE_ASSERT_(
static_assert(
kTIsOther || kUIsOther ||
(internal::LosslessArithmeticConvertible<RawT, RawU>::value),
conversion_of_arithmetic_types_must_be_lossless);
"conversion of arithmetic types must be lossless");
return MatcherCast<T>(matcher);
}
@ -678,9 +677,9 @@ bool TupleMatches(const MatcherTuple& matcher_tuple,
const ValueTuple& value_tuple) {
// Makes sure that matcher_tuple and value_tuple have the same
// number of fields.
GTEST_COMPILE_ASSERT_(std::tuple_size<MatcherTuple>::value ==
std::tuple_size<ValueTuple>::value,
matcher_and_value_have_different_numbers_of_fields);
static_assert(std::tuple_size<MatcherTuple>::value ==
std::tuple_size<ValueTuple>::value,
"matcher and value have different numbers of fields");
return TuplePrefix<std::tuple_size<ValueTuple>::value>::Matches(matcher_tuple,
value_tuple);
}
@ -2570,9 +2569,9 @@ class WhenSortedByMatcher {
// container and the RHS container respectively.
template <typename TupleMatcher, typename RhsContainer>
class PointwiseMatcher {
GTEST_COMPILE_ASSERT_(
static_assert(
!IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>::value,
use_UnorderedPointwise_with_hash_tables);
"use UnorderedPointwise with hash tables");
public:
typedef internal::StlContainerView<RhsContainer> RhsView;
@ -2591,9 +2590,9 @@ class PointwiseMatcher {
template <typename LhsContainer>
operator Matcher<LhsContainer>() const {
GTEST_COMPILE_ASSERT_(
static_assert(
!IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)>::value,
use_UnorderedPointwise_with_hash_tables);
"use UnorderedPointwise with hash tables");
return Matcher<LhsContainer>(
new Impl<const LhsContainer&>(tuple_matcher_, rhs_));
@ -3725,10 +3724,10 @@ class ElementsAreMatcher {
template <typename Container>
operator Matcher<Container>() const {
GTEST_COMPILE_ASSERT_(
static_assert(
!IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value ||
::std::tuple_size<MatcherTuple>::value < 2,
use_UnorderedElementsAre_with_hash_tables);
"use UnorderedElementsAre with hash tables");
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
typedef typename internal::StlContainerView<RawContainer>::type View;
@ -3776,9 +3775,9 @@ class ElementsAreArrayMatcher {
template <typename Container>
operator Matcher<Container>() const {
GTEST_COMPILE_ASSERT_(
static_assert(
!IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value,
use_UnorderedElementsAreArray_with_hash_tables);
"use UnorderedElementsAreArray with hash tables");
return Matcher<Container>(new ElementsAreMatcherImpl<const Container&>(
matchers_.begin(), matchers_.end()));

View File

@ -878,17 +878,6 @@ namespace internal {
// Secret object, which is what we want.
class Secret;
// The GTEST_COMPILE_ASSERT_ is a legacy macro used to verify that a compile
// time expression is true (in new code, use static_assert instead). For
// example, you could use it to verify the size of a static array:
//
// GTEST_COMPILE_ASSERT_(GTEST_ARRAY_SIZE_(names) == NUM_NAMES,
// names_incorrect_size);
//
// The second argument to the macro must be a valid C++ identifier. If the
// expression is false, compiler will issue an error containing this identifier.
#define GTEST_COMPILE_ASSERT_(expr, msg) static_assert(expr, #msg)
// A helper for suppressing warnings on constant condition. It just
// returns 'condition'.
GTEST_API_ bool IsTrue(bool condition);

View File

@ -7173,28 +7173,26 @@ struct IncompleteType;
// Tests that HasDebugStringAndShortDebugString<T>::value is a compile-time
// constant.
TEST(HasDebugStringAndShortDebugStringTest, ValueIsCompileTimeConstant) {
GTEST_COMPILE_ASSERT_(
HasDebugStringAndShortDebugString<HasDebugStringMethods>::value,
const_true);
GTEST_COMPILE_ASSERT_(
static_assert(HasDebugStringAndShortDebugString<HasDebugStringMethods>::value,
"const_true");
static_assert(
HasDebugStringAndShortDebugString<InheritsDebugStringMethods>::value,
const_true);
GTEST_COMPILE_ASSERT_(HasDebugStringAndShortDebugString<
const InheritsDebugStringMethods>::value,
const_true);
GTEST_COMPILE_ASSERT_(
"const_true");
static_assert(HasDebugStringAndShortDebugString<
const InheritsDebugStringMethods>::value,
"const_true");
static_assert(
!HasDebugStringAndShortDebugString<WrongTypeDebugStringMethod>::value,
const_false);
GTEST_COMPILE_ASSERT_(
"const_false");
static_assert(
!HasDebugStringAndShortDebugString<NotConstDebugStringMethod>::value,
const_false);
GTEST_COMPILE_ASSERT_(
"const_false");
static_assert(
!HasDebugStringAndShortDebugString<MissingDebugStringMethod>::value,
const_false);
GTEST_COMPILE_ASSERT_(
!HasDebugStringAndShortDebugString<IncompleteType>::value, const_false);
GTEST_COMPILE_ASSERT_(!HasDebugStringAndShortDebugString<int>::value,
const_false);
"const_false");
static_assert(!HasDebugStringAndShortDebugString<IncompleteType>::value,
"const_false");
static_assert(!HasDebugStringAndShortDebugString<int>::value, "const_false");
}
// Tests that HasDebugStringAndShortDebugString<T>::value is true when T has