diff --git a/googlemock/include/gmock/gmock-function-mocker.h b/googlemock/include/gmock/gmock-function-mocker.h index b568aefe..73065493 100644 --- a/googlemock/include/gmock/gmock-function-mocker.h +++ b/googlemock/include/gmock/gmock-function-mocker.h @@ -108,8 +108,11 @@ constexpr bool ValidateSpec(const char (&spec)[N]) { using internal::FunctionMocker; } // namespace testing -#define MOCK_METHOD(...) \ - GMOCK_PP_VARIADIC_CALL(GMOCK_INTERNAL_MOCK_METHOD_ARG_, __VA_ARGS__) +#define MOCK_METHOD(...) \ + GMOCK_INTERNAL_WARNING_PUSH() \ + GMOCK_INTERNAL_WARNING_CLANG(ignored, "-Wunused-member-function") \ + GMOCK_PP_VARIADIC_CALL(GMOCK_INTERNAL_MOCK_METHOD_ARG_, __VA_ARGS__) \ + GMOCK_INTERNAL_WARNING_POP() #define GMOCK_INTERNAL_MOCK_METHOD_ARG_1(...) \ GMOCK_INTERNAL_WRONG_ARITY(__VA_ARGS__) diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index 5faf8dc9..9ade9273 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -5482,7 +5482,13 @@ PolymorphicMatcher> ThrowsMessage( } \ }; \ }; \ - GTEST_ATTRIBUTE_UNUSED_ inline name##Matcher name() { return {}; } \ + inline name##Matcher GMOCK_INTERNAL_WARNING_PUSH() \ + GMOCK_INTERNAL_WARNING_CLANG(ignored, "-Wunused-function") \ + GMOCK_INTERNAL_WARNING_CLANG(ignored, "-Wunused-member-function") \ + name \ + GMOCK_INTERNAL_WARNING_POP()() { \ + return {}; \ + } \ template \ bool name##Matcher::gmock_Impl::MatchAndExplain( \ const arg_type& arg, \ diff --git a/googlemock/include/gmock/internal/gmock-internal-utils.h b/googlemock/include/gmock/internal/gmock-internal-utils.h index 2678920b..36ab8e26 100644 --- a/googlemock/include/gmock/internal/gmock-internal-utils.h +++ b/googlemock/include/gmock/internal/gmock-internal-utils.h @@ -95,6 +95,24 @@ inline Element* GetRawPointer(Element* p) { return p; } +// Default definitions for all compilers. +// NOTE: If you implement support for other compilers, make sure to avoid +// unexpected overlaps. +// (e.g., Clang also processes #pragma GCC, and clang-cl also handles _MSC_VER.) +#define GMOCK_INTERNAL_WARNING_PUSH() +#define GMOCK_INTERNAL_WARNING_CLANG(Level, Name) +#define GMOCK_INTERNAL_WARNING_POP() + +#if defined(__clang__) +#undef GMOCK_INTERNAL_WARNING_PUSH +#define GMOCK_INTERNAL_WARNING_PUSH() _Pragma("clang diagnostic push") +#undef GMOCK_INTERNAL_WARNING_CLANG +#define GMOCK_INTERNAL_WARNING_CLANG(Level, Warning) \ + _Pragma(GMOCK_PP_INTERNAL_STRINGIZE(clang diagnostic Level Warning)) +#undef GMOCK_INTERNAL_WARNING_POP +#define GMOCK_INTERNAL_WARNING_POP() _Pragma("clang diagnostic pop") +#endif + // MSVC treats wchar_t as a native type usually, but treats it as the // same as unsigned short when the compiler option /Zc:wchar_t- is // specified. It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t diff --git a/googlemock/test/gmock-function-mocker_test.cc b/googlemock/test/gmock-function-mocker_test.cc index 286115fe..1d15a299 100644 --- a/googlemock/test/gmock-function-mocker_test.cc +++ b/googlemock/test/gmock-function-mocker_test.cc @@ -958,6 +958,21 @@ TEST(MockMethodMockFunctionTest, MockMethodSizeOverhead) { EXPECT_EQ(sizeof(LegacyMockMethodSizes0), sizeof(MockMethodSizes0)); } +TEST(MockMethodMockFunctionTest, EnsureNoUnusedMemberFunction) { +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic error "-Wunused-member-function" +#endif + // https://github.com/google/googletest/issues/4052 + struct Foo { + MOCK_METHOD(void, foo, ()); + }; + EXPECT_CALL(Foo(), foo()).Times(0); +#ifdef __clang__ +#pragma clang diagnostic pop +#endif +} + void hasTwoParams(int, int); void MaybeThrows(); void DoesntThrow() noexcept; diff --git a/googlemock/test/gmock-matchers-misc_test.cc b/googlemock/test/gmock-matchers-misc_test.cc index 53f4962b..42a0e44d 100644 --- a/googlemock/test/gmock-matchers-misc_test.cc +++ b/googlemock/test/gmock-matchers-misc_test.cc @@ -1615,6 +1615,20 @@ TEST(MatcherPMacroTest, WorksOnMoveOnlyType) { EXPECT_THAT(p, Not(UniquePointee(2))); } +MATCHER(EnsureNoUnusedButMarkedUnusedWarning, "") { return (arg % 2) == 0; } + +TEST(MockMethodMockFunctionTest, EnsureNoUnusedButMarkedUnusedWarning) { +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic error "-Wused-but-marked-unused" +#endif + // https://github.com/google/googletest/issues/4055 + EXPECT_THAT(0, EnsureNoUnusedButMarkedUnusedWarning()); +#ifdef __clang__ +#pragma clang diagnostic pop +#endif +} + #if GTEST_HAS_EXCEPTIONS // std::function is used below for compatibility with older copies of