Skip to content

Commit b85864c

Browse files
derekmaurocopybara-github
authored andcommitted
Eliminate the legacy GTEST_COMPILE_ASSERT_ macro
PiperOrigin-RevId: 443462203 Change-Id: I0c43f981663a7531ff5da4d4be01fb3d6762273d
1 parent 8ccdb9d commit b85864c

File tree

4 files changed

+39
-55
lines changed

4 files changed

+39
-55
lines changed

googlemock/include/gmock/gmock-actions.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -915,9 +915,8 @@ class ReturnAction {
915915
// and put the typedef both here (for use in assert statement) and
916916
// in the Impl class. But both definitions must be the same.
917917
typedef typename Function<F>::Result Result;
918-
GTEST_COMPILE_ASSERT_(
919-
!std::is_reference<Result>::value,
920-
use_ReturnRef_instead_of_Return_to_return_a_reference);
918+
static_assert(!std::is_reference<Result>::value,
919+
"use ReturnRef instead of Return to return a reference");
921920
static_assert(!std::is_void<Result>::value,
922921
"Can't use Return() on an action expected to return `void`.");
923922
return Action<F>(new Impl<R, F>(value_));
@@ -945,8 +944,8 @@ class ReturnAction {
945944
Result Perform(const ArgumentTuple&) override { return value_; }
946945

947946
private:
948-
GTEST_COMPILE_ASSERT_(!std::is_reference<Result>::value,
949-
Result_cannot_be_a_reference_type);
947+
static_assert(!std::is_reference<Result>::value,
948+
"Result cannot be a reference type");
950949
// We save the value before casting just in case it is being cast to a
951950
// wrapper type.
952951
R value_before_cast_;
@@ -1020,8 +1019,8 @@ class ReturnRefAction {
10201019
// Asserts that the function return type is a reference. This
10211020
// catches the user error of using ReturnRef(x) when Return(x)
10221021
// should be used, and generates some helpful error message.
1023-
GTEST_COMPILE_ASSERT_(std::is_reference<Result>::value,
1024-
use_Return_instead_of_ReturnRef_to_return_a_value);
1022+
static_assert(std::is_reference<Result>::value,
1023+
"use Return instead of ReturnRef to return a value");
10251024
return Action<F>(new Impl<F>(ref_));
10261025
}
10271026

@@ -1062,9 +1061,8 @@ class ReturnRefOfCopyAction {
10621061
// Asserts that the function return type is a reference. This
10631062
// catches the user error of using ReturnRefOfCopy(x) when Return(x)
10641063
// should be used, and generates some helpful error message.
1065-
GTEST_COMPILE_ASSERT_(
1066-
std::is_reference<Result>::value,
1067-
use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
1064+
static_assert(std::is_reference<Result>::value,
1065+
"use Return instead of ReturnRefOfCopy to return a value");
10681066
return Action<F>(new Impl<F>(value_));
10691067
}
10701068

googlemock/include/gmock/gmock-matchers.h

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -533,19 +533,18 @@ inline Matcher<T> SafeMatcherCast(const Matcher<U>& matcher) {
533533
"T must be implicitly convertible to U");
534534
// Enforce that we are not converting a non-reference type T to a reference
535535
// type U.
536-
GTEST_COMPILE_ASSERT_(
537-
std::is_reference<T>::value || !std::is_reference<U>::value,
538-
cannot_convert_non_reference_arg_to_reference);
536+
static_assert(std::is_reference<T>::value || !std::is_reference<U>::value,
537+
"cannot convert non reference arg to reference");
539538
// In case both T and U are arithmetic types, enforce that the
540539
// conversion is not lossy.
541540
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT;
542541
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU;
543542
constexpr bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther;
544543
constexpr bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther;
545-
GTEST_COMPILE_ASSERT_(
544+
static_assert(
546545
kTIsOther || kUIsOther ||
547546
(internal::LosslessArithmeticConvertible<RawT, RawU>::value),
548-
conversion_of_arithmetic_types_must_be_lossless);
547+
"conversion of arithmetic types must be lossless");
549548
return MatcherCast<T>(matcher);
550549
}
551550

@@ -678,9 +677,9 @@ bool TupleMatches(const MatcherTuple& matcher_tuple,
678677
const ValueTuple& value_tuple) {
679678
// Makes sure that matcher_tuple and value_tuple have the same
680679
// number of fields.
681-
GTEST_COMPILE_ASSERT_(std::tuple_size<MatcherTuple>::value ==
682-
std::tuple_size<ValueTuple>::value,
683-
matcher_and_value_have_different_numbers_of_fields);
680+
static_assert(std::tuple_size<MatcherTuple>::value ==
681+
std::tuple_size<ValueTuple>::value,
682+
"matcher and value have different numbers of fields");
684683
return TuplePrefix<std::tuple_size<ValueTuple>::value>::Matches(matcher_tuple,
685684
value_tuple);
686685
}
@@ -2570,9 +2569,9 @@ class WhenSortedByMatcher {
25702569
// container and the RHS container respectively.
25712570
template <typename TupleMatcher, typename RhsContainer>
25722571
class PointwiseMatcher {
2573-
GTEST_COMPILE_ASSERT_(
2572+
static_assert(
25742573
!IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>::value,
2575-
use_UnorderedPointwise_with_hash_tables);
2574+
"use UnorderedPointwise with hash tables");
25762575

25772576
public:
25782577
typedef internal::StlContainerView<RhsContainer> RhsView;
@@ -2591,9 +2590,9 @@ class PointwiseMatcher {
25912590

25922591
template <typename LhsContainer>
25932592
operator Matcher<LhsContainer>() const {
2594-
GTEST_COMPILE_ASSERT_(
2593+
static_assert(
25952594
!IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)>::value,
2596-
use_UnorderedPointwise_with_hash_tables);
2595+
"use UnorderedPointwise with hash tables");
25972596

25982597
return Matcher<LhsContainer>(
25992598
new Impl<const LhsContainer&>(tuple_matcher_, rhs_));
@@ -3725,10 +3724,10 @@ class ElementsAreMatcher {
37253724

37263725
template <typename Container>
37273726
operator Matcher<Container>() const {
3728-
GTEST_COMPILE_ASSERT_(
3727+
static_assert(
37293728
!IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value ||
37303729
::std::tuple_size<MatcherTuple>::value < 2,
3731-
use_UnorderedElementsAre_with_hash_tables);
3730+
"use UnorderedElementsAre with hash tables");
37323731

37333732
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
37343733
typedef typename internal::StlContainerView<RawContainer>::type View;
@@ -3776,9 +3775,9 @@ class ElementsAreArrayMatcher {
37763775

37773776
template <typename Container>
37783777
operator Matcher<Container>() const {
3779-
GTEST_COMPILE_ASSERT_(
3778+
static_assert(
37803779
!IsHashTable<GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>::value,
3781-
use_UnorderedElementsAreArray_with_hash_tables);
3780+
"use UnorderedElementsAreArray with hash tables");
37823781

37833782
return Matcher<Container>(new ElementsAreMatcherImpl<const Container&>(
37843783
matchers_.begin(), matchers_.end()));

googletest/include/gtest/internal/gtest-port.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -878,17 +878,6 @@ namespace internal {
878878
// Secret object, which is what we want.
879879
class Secret;
880880

881-
// The GTEST_COMPILE_ASSERT_ is a legacy macro used to verify that a compile
882-
// time expression is true (in new code, use static_assert instead). For
883-
// example, you could use it to verify the size of a static array:
884-
//
885-
// GTEST_COMPILE_ASSERT_(GTEST_ARRAY_SIZE_(names) == NUM_NAMES,
886-
// names_incorrect_size);
887-
//
888-
// The second argument to the macro must be a valid C++ identifier. If the
889-
// expression is false, compiler will issue an error containing this identifier.
890-
#define GTEST_COMPILE_ASSERT_(expr, msg) static_assert(expr, #msg)
891-
892881
// A helper for suppressing warnings on constant condition. It just
893882
// returns 'condition'.
894883
GTEST_API_ bool IsTrue(bool condition);

googletest/test/gtest_unittest.cc

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7173,28 +7173,26 @@ struct IncompleteType;
71737173
// Tests that HasDebugStringAndShortDebugString<T>::value is a compile-time
71747174
// constant.
71757175
TEST(HasDebugStringAndShortDebugStringTest, ValueIsCompileTimeConstant) {
7176-
GTEST_COMPILE_ASSERT_(
7177-
HasDebugStringAndShortDebugString<HasDebugStringMethods>::value,
7178-
const_true);
7179-
GTEST_COMPILE_ASSERT_(
7176+
static_assert(HasDebugStringAndShortDebugString<HasDebugStringMethods>::value,
7177+
"const_true");
7178+
static_assert(
71807179
HasDebugStringAndShortDebugString<InheritsDebugStringMethods>::value,
7181-
const_true);
7182-
GTEST_COMPILE_ASSERT_(HasDebugStringAndShortDebugString<
7183-
const InheritsDebugStringMethods>::value,
7184-
const_true);
7185-
GTEST_COMPILE_ASSERT_(
7180+
"const_true");
7181+
static_assert(HasDebugStringAndShortDebugString<
7182+
const InheritsDebugStringMethods>::value,
7183+
"const_true");
7184+
static_assert(
71867185
!HasDebugStringAndShortDebugString<WrongTypeDebugStringMethod>::value,
7187-
const_false);
7188-
GTEST_COMPILE_ASSERT_(
7186+
"const_false");
7187+
static_assert(
71897188
!HasDebugStringAndShortDebugString<NotConstDebugStringMethod>::value,
7190-
const_false);
7191-
GTEST_COMPILE_ASSERT_(
7189+
"const_false");
7190+
static_assert(
71927191
!HasDebugStringAndShortDebugString<MissingDebugStringMethod>::value,
7193-
const_false);
7194-
GTEST_COMPILE_ASSERT_(
7195-
!HasDebugStringAndShortDebugString<IncompleteType>::value, const_false);
7196-
GTEST_COMPILE_ASSERT_(!HasDebugStringAndShortDebugString<int>::value,
7197-
const_false);
7192+
"const_false");
7193+
static_assert(!HasDebugStringAndShortDebugString<IncompleteType>::value,
7194+
"const_false");
7195+
static_assert(!HasDebugStringAndShortDebugString<int>::value, "const_false");
71987196
}
71997197

72007198
// Tests that HasDebugStringAndShortDebugString<T>::value is true when T has

0 commit comments

Comments
 (0)