From b85864c64758dec007208e56af933fc3f52044ee Mon Sep 17 00:00:00 2001 From: Derek Mauro Date: Thu, 21 Apr 2022 13:22:38 -0700 Subject: Eliminate the legacy GTEST_COMPILE_ASSERT_ macro PiperOrigin-RevId: 443462203 Change-Id: I0c43f981663a7531ff5da4d4be01fb3d6762273d --- googlemock/include/gmock/gmock-actions.h | 18 ++++++-------- googlemock/include/gmock/gmock-matchers.h | 31 ++++++++++++----------- googletest/include/gtest/internal/gtest-port.h | 11 --------- googletest/test/gtest_unittest.cc | 34 ++++++++++++-------------- 4 files changed, 39 insertions(+), 55 deletions(-) diff --git a/googlemock/include/gmock/gmock-actions.h b/googlemock/include/gmock/gmock-actions.h index 1038b02..e84670a 100644 --- a/googlemock/include/gmock/gmock-actions.h +++ b/googlemock/include/gmock/gmock-actions.h @@ -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::Result Result; - GTEST_COMPILE_ASSERT_( - !std::is_reference::value, - use_ReturnRef_instead_of_Return_to_return_a_reference); + static_assert(!std::is_reference::value, + "use ReturnRef instead of Return to return a reference"); static_assert(!std::is_void::value, "Can't use Return() on an action expected to return `void`."); return Action(new Impl(value_)); @@ -945,8 +944,8 @@ class ReturnAction { Result Perform(const ArgumentTuple&) override { return value_; } private: - GTEST_COMPILE_ASSERT_(!std::is_reference::value, - Result_cannot_be_a_reference_type); + static_assert(!std::is_reference::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::value, - use_Return_instead_of_ReturnRef_to_return_a_value); + static_assert(std::is_reference::value, + "use Return instead of ReturnRef to return a value"); return Action(new Impl(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::value, - use_Return_instead_of_ReturnRefOfCopy_to_return_a_value); + static_assert(std::is_reference::value, + "use Return instead of ReturnRefOfCopy to return a value"); return Action(new Impl(value_)); } diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index 9750d84..fb46715 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -533,19 +533,18 @@ inline Matcher SafeMatcherCast(const Matcher& 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::value || !std::is_reference::value, - cannot_convert_non_reference_arg_to_reference); + static_assert(std::is_reference::value || !std::is_reference::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::value), - conversion_of_arithmetic_types_must_be_lossless); + "conversion of arithmetic types must be lossless"); return MatcherCast(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::value == - std::tuple_size::value, - matcher_and_value_have_different_numbers_of_fields); + static_assert(std::tuple_size::value == + std::tuple_size::value, + "matcher and value have different numbers of fields"); return TuplePrefix::value>::Matches(matcher_tuple, value_tuple); } @@ -2570,9 +2569,9 @@ class WhenSortedByMatcher { // container and the RHS container respectively. template class PointwiseMatcher { - GTEST_COMPILE_ASSERT_( + static_assert( !IsHashTable::value, - use_UnorderedPointwise_with_hash_tables); + "use UnorderedPointwise with hash tables"); public: typedef internal::StlContainerView RhsView; @@ -2591,9 +2590,9 @@ class PointwiseMatcher { template operator Matcher() const { - GTEST_COMPILE_ASSERT_( + static_assert( !IsHashTable::value, - use_UnorderedPointwise_with_hash_tables); + "use UnorderedPointwise with hash tables"); return Matcher( new Impl(tuple_matcher_, rhs_)); @@ -3725,10 +3724,10 @@ class ElementsAreMatcher { template operator Matcher() const { - GTEST_COMPILE_ASSERT_( + static_assert( !IsHashTable::value || ::std::tuple_size::value < 2, - use_UnorderedElementsAre_with_hash_tables); + "use UnorderedElementsAre with hash tables"); typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; typedef typename internal::StlContainerView::type View; @@ -3776,9 +3775,9 @@ class ElementsAreArrayMatcher { template operator Matcher() const { - GTEST_COMPILE_ASSERT_( + static_assert( !IsHashTable::value, - use_UnorderedElementsAreArray_with_hash_tables); + "use UnorderedElementsAreArray with hash tables"); return Matcher(new ElementsAreMatcherImpl( matchers_.begin(), matchers_.end())); diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 574af24..922de92 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -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); diff --git a/googletest/test/gtest_unittest.cc b/googletest/test/gtest_unittest.cc index 8ed0921..8cff92d 100644 --- a/googletest/test/gtest_unittest.cc +++ b/googletest/test/gtest_unittest.cc @@ -7173,28 +7173,26 @@ struct IncompleteType; // Tests that HasDebugStringAndShortDebugString::value is a compile-time // constant. TEST(HasDebugStringAndShortDebugStringTest, ValueIsCompileTimeConstant) { - GTEST_COMPILE_ASSERT_( - HasDebugStringAndShortDebugString::value, - const_true); - GTEST_COMPILE_ASSERT_( + static_assert(HasDebugStringAndShortDebugString::value, + "const_true"); + static_assert( HasDebugStringAndShortDebugString::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::value, - const_false); - GTEST_COMPILE_ASSERT_( + "const_false"); + static_assert( !HasDebugStringAndShortDebugString::value, - const_false); - GTEST_COMPILE_ASSERT_( + "const_false"); + static_assert( !HasDebugStringAndShortDebugString::value, - const_false); - GTEST_COMPILE_ASSERT_( - !HasDebugStringAndShortDebugString::value, const_false); - GTEST_COMPILE_ASSERT_(!HasDebugStringAndShortDebugString::value, - const_false); + "const_false"); + static_assert(!HasDebugStringAndShortDebugString::value, + "const_false"); + static_assert(!HasDebugStringAndShortDebugString::value, "const_false"); } // Tests that HasDebugStringAndShortDebugString::value is true when T has -- cgit v0.12