Use [[maybe_unused]] when it is available to avoid

-Wused-but-marked-unused warnings

PiperOrigin-RevId: 625430612
Change-Id: Ia9d2e47984e1e6f91966afae8a6750119ae69446
This commit is contained in:
Derek Mauro 2024-04-16 13:15:51 -07:00 committed by Copybara-Service
parent 5197b1a8e6
commit 5a37b517ad
7 changed files with 150 additions and 124 deletions

View File

@ -2174,9 +2174,9 @@ template <typename F, typename Impl>
} }
#define GMOCK_INTERNAL_ARG_UNUSED(i, data, el) \ #define GMOCK_INTERNAL_ARG_UNUSED(i, data, el) \
, const arg##i##_type& arg##i GTEST_ATTRIBUTE_UNUSED_ , GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED const arg##i##_type& arg##i
#define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_ \ #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_ \
const args_type& args GTEST_ATTRIBUTE_UNUSED_ GMOCK_PP_REPEAT( \ GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED const args_type& args GMOCK_PP_REPEAT( \
GMOCK_INTERNAL_ARG_UNUSED, , 10) GMOCK_INTERNAL_ARG_UNUSED, , 10)
#define GMOCK_INTERNAL_ARG(i, data, el) , const arg##i##_type& arg##i #define GMOCK_INTERNAL_ARG(i, data, el) , const arg##i##_type& arg##i

View File

@ -5445,47 +5445,47 @@ PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> ThrowsMessage(
::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
// MATCHER* macros itself are listed below. // MATCHER* macros itself are listed below.
#define MATCHER(name, description) \ #define MATCHER(name, description) \
class name##Matcher \ class name##Matcher \
: public ::testing::internal::MatcherBaseImpl<name##Matcher> { \ : public ::testing::internal::MatcherBaseImpl<name##Matcher> { \
public: \ public: \
template <typename arg_type> \ template <typename arg_type> \
class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \ class gmock_Impl : public ::testing::MatcherInterface<const arg_type&> { \
public: \ public: \
gmock_Impl() {} \ gmock_Impl() {} \
bool MatchAndExplain( \ bool MatchAndExplain( \
const arg_type& arg, \ const arg_type& arg, \
::testing::MatchResultListener* result_listener) const override; \ ::testing::MatchResultListener* result_listener) const override; \
void DescribeTo(::std::ostream* gmock_os) const override { \ void DescribeTo(::std::ostream* gmock_os) const override { \
*gmock_os << FormatDescription(false); \ *gmock_os << FormatDescription(false); \
} \ } \
void DescribeNegationTo(::std::ostream* gmock_os) const override { \ void DescribeNegationTo(::std::ostream* gmock_os) const override { \
*gmock_os << FormatDescription(true); \ *gmock_os << FormatDescription(true); \
} \ } \
\ \
private: \ private: \
::std::string FormatDescription(bool negation) const { \ ::std::string FormatDescription(bool negation) const { \
/* NOLINTNEXTLINE readability-redundant-string-init */ \ /* NOLINTNEXTLINE readability-redundant-string-init */ \
::std::string gmock_description = (description); \ ::std::string gmock_description = (description); \
if (!gmock_description.empty()) { \ if (!gmock_description.empty()) { \
return gmock_description; \ return gmock_description; \
} \ } \
return ::testing::internal::FormatMatcherDescription(negation, #name, \ return ::testing::internal::FormatMatcherDescription(negation, #name, \
{}, {}); \ {}, {}); \
} \ } \
}; \ }; \
}; \ }; \
inline name##Matcher GMOCK_INTERNAL_WARNING_PUSH() \ inline name##Matcher GMOCK_INTERNAL_WARNING_PUSH() \
GMOCK_INTERNAL_WARNING_CLANG(ignored, "-Wunused-function") \ GMOCK_INTERNAL_WARNING_CLANG(ignored, "-Wunused-function") \
GMOCK_INTERNAL_WARNING_CLANG(ignored, "-Wunused-member-function") \ GMOCK_INTERNAL_WARNING_CLANG(ignored, "-Wunused-member-function") \
name GMOCK_INTERNAL_WARNING_POP()() { \ name GMOCK_INTERNAL_WARNING_POP()() { \
return {}; \ return {}; \
} \ } \
template <typename arg_type> \ template <typename arg_type> \
bool name##Matcher::gmock_Impl<arg_type>::MatchAndExplain( \ bool name##Matcher::gmock_Impl<arg_type>::MatchAndExplain( \
const arg_type& arg, \ const arg_type& arg, \
::testing::MatchResultListener* result_listener GTEST_ATTRIBUTE_UNUSED_) \ GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED ::testing::MatchResultListener* \
const result_listener) const
#define MATCHER_P(name, p0, description) \ #define MATCHER_P(name, p0, description) \
GMOCK_INTERNAL_MATCHER(name, name##MatcherP, description, (#p0), (p0)) GMOCK_INTERNAL_MATCHER(name, name##MatcherP, description, (#p0), (p0))
@ -5567,11 +5567,11 @@ PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> ThrowsMessage(
} \ } \
template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \ template <GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args)> \
template <typename arg_type> \ template <typename arg_type> \
bool full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>::gmock_Impl< \ bool full_name<GMOCK_INTERNAL_MATCHER_TYPE_PARAMS(args)>:: \
arg_type>::MatchAndExplain(const arg_type& arg, \ gmock_Impl<arg_type>::MatchAndExplain( \
::testing::MatchResultListener* \ const arg_type& arg, \
result_listener GTEST_ATTRIBUTE_UNUSED_) \ GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED ::testing:: \
const MatchResultListener* result_listener) const
#define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args) \ #define GMOCK_INTERNAL_MATCHER_TEMPLATE_PARAMS(args) \
GMOCK_PP_TAIL( \ GMOCK_PP_TAIL( \

View File

@ -469,7 +469,7 @@ internal::ParamConverterGenerator<T> ConvertGenerator(
::testing::internal::CodeLocation(__FILE__, __LINE__)); \ ::testing::internal::CodeLocation(__FILE__, __LINE__)); \
return 0; \ return 0; \
} \ } \
static int gtest_registering_dummy_ GTEST_ATTRIBUTE_UNUSED_; \ GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED static int gtest_registering_dummy_; \
}; \ }; \
int GTEST_TEST_CLASS_NAME_(test_suite_name, \ int GTEST_TEST_CLASS_NAME_(test_suite_name, \
test_name)::gtest_registering_dummy_ = \ test_name)::gtest_registering_dummy_ = \
@ -514,8 +514,8 @@ internal::ParamConverterGenerator<T> ConvertGenerator(
::testing::internal::DefaultParamName<test_suite_name::ParamType>, \ ::testing::internal::DefaultParamName<test_suite_name::ParamType>, \
DUMMY_PARAM_))))(info); \ DUMMY_PARAM_))))(info); \
} \ } \
static int gtest_##prefix##test_suite_name##_dummy_ \ GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED static int \
GTEST_ATTRIBUTE_UNUSED_ = \ gtest_##prefix##test_suite_name##_dummy_ = \
::testing::UnitTest::GetInstance() \ ::testing::UnitTest::GetInstance() \
->parameterized_test_registry() \ ->parameterized_test_registry() \
.GetTestSuitePatternHolder<test_suite_name>( \ .GetTestSuitePatternHolder<test_suite_name>( \

View File

@ -194,33 +194,34 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);
typedef ::testing::internal::NameGeneratorSelector<__VA_ARGS__>::type \ typedef ::testing::internal::NameGeneratorSelector<__VA_ARGS__>::type \
GTEST_NAME_GENERATOR_(CaseName) GTEST_NAME_GENERATOR_(CaseName)
#define TYPED_TEST(CaseName, TestName) \ #define TYPED_TEST(CaseName, TestName) \
static_assert(sizeof(GTEST_STRINGIFY_(TestName)) > 1, \ static_assert(sizeof(GTEST_STRINGIFY_(TestName)) > 1, \
"test-name must not be empty"); \ "test-name must not be empty"); \
template <typename gtest_TypeParam_> \ template <typename gtest_TypeParam_> \
class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \ class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \
: public CaseName<gtest_TypeParam_> { \ : public CaseName<gtest_TypeParam_> { \
private: \ private: \
typedef CaseName<gtest_TypeParam_> TestFixture; \ typedef CaseName<gtest_TypeParam_> TestFixture; \
typedef gtest_TypeParam_ TypeParam; \ typedef gtest_TypeParam_ TypeParam; \
void TestBody() override; \ void TestBody() override; \
}; \ }; \
static bool gtest_##CaseName##_##TestName##_registered_ \ GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED static bool \
GTEST_ATTRIBUTE_UNUSED_ = ::testing::internal::TypeParameterizedTest< \ gtest_##CaseName##_##TestName##_registered_ = \
CaseName, \ ::testing::internal::TypeParameterizedTest< \
::testing::internal::TemplateSel<GTEST_TEST_CLASS_NAME_(CaseName, \ CaseName, \
TestName)>, \ ::testing::internal::TemplateSel<GTEST_TEST_CLASS_NAME_( \
GTEST_TYPE_PARAMS_( \ CaseName, TestName)>, \
CaseName)>::Register("", \ GTEST_TYPE_PARAMS_( \
::testing::internal::CodeLocation( \ CaseName)>::Register("", \
__FILE__, __LINE__), \ ::testing::internal::CodeLocation( \
GTEST_STRINGIFY_(CaseName), \ __FILE__, __LINE__), \
GTEST_STRINGIFY_(TestName), 0, \ GTEST_STRINGIFY_(CaseName), \
::testing::internal::GenerateNames< \ GTEST_STRINGIFY_(TestName), 0, \
GTEST_NAME_GENERATOR_(CaseName), \ ::testing::internal::GenerateNames< \
GTEST_TYPE_PARAMS_(CaseName)>()); \ GTEST_NAME_GENERATOR_(CaseName), \
template <typename gtest_TypeParam_> \ GTEST_TYPE_PARAMS_(CaseName)>()); \
void GTEST_TEST_CLASS_NAME_(CaseName, \ template <typename gtest_TypeParam_> \
void GTEST_TEST_CLASS_NAME_(CaseName, \
TestName)<gtest_TypeParam_>::TestBody() TestName)<gtest_TypeParam_>::TestBody()
// Legacy API is deprecated but still available // Legacy API is deprecated but still available
@ -267,22 +268,23 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);
TYPED_TEST_SUITE_P TYPED_TEST_SUITE_P
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
#define TYPED_TEST_P(SuiteName, TestName) \ #define TYPED_TEST_P(SuiteName, TestName) \
namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \ namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \
template <typename gtest_TypeParam_> \ template <typename gtest_TypeParam_> \
class TestName : public SuiteName<gtest_TypeParam_> { \ class TestName : public SuiteName<gtest_TypeParam_> { \
private: \ private: \
typedef SuiteName<gtest_TypeParam_> TestFixture; \ typedef SuiteName<gtest_TypeParam_> TestFixture; \
typedef gtest_TypeParam_ TypeParam; \ typedef gtest_TypeParam_ TypeParam; \
void TestBody() override; \ void TestBody() override; \
}; \ }; \
static bool gtest_##TestName##_defined_ GTEST_ATTRIBUTE_UNUSED_ = \ GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED static bool \
GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).AddTestName( \ gtest_##TestName##_defined_ = \
__FILE__, __LINE__, GTEST_STRINGIFY_(SuiteName), \ GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).AddTestName( \
GTEST_STRINGIFY_(TestName)); \ __FILE__, __LINE__, GTEST_STRINGIFY_(SuiteName), \
} \ GTEST_STRINGIFY_(TestName)); \
template <typename gtest_TypeParam_> \ } \
void GTEST_SUITE_NAMESPACE_( \ template <typename gtest_TypeParam_> \
void GTEST_SUITE_NAMESPACE_( \
SuiteName)::TestName<gtest_TypeParam_>::TestBody() SuiteName)::TestName<gtest_TypeParam_>::TestBody()
// Note: this won't work correctly if the trailing arguments are macros. // Note: this won't work correctly if the trailing arguments are macros.
@ -290,8 +292,8 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);
namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \ namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \
typedef ::testing::internal::Templates<__VA_ARGS__> gtest_AllTests_; \ typedef ::testing::internal::Templates<__VA_ARGS__> gtest_AllTests_; \
} \ } \
static const char* const GTEST_REGISTERED_TEST_NAMES_( \ GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED static const char* const \
SuiteName) GTEST_ATTRIBUTE_UNUSED_ = \ GTEST_REGISTERED_TEST_NAMES_(SuiteName) = \
GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).VerifyRegisteredTestNames( \ GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).VerifyRegisteredTestNames( \
GTEST_STRINGIFY_(SuiteName), __FILE__, __LINE__, #__VA_ARGS__) GTEST_STRINGIFY_(SuiteName), __FILE__, __LINE__, #__VA_ARGS__)
@ -303,22 +305,24 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);
REGISTER_TYPED_TEST_SUITE_P REGISTER_TYPED_TEST_SUITE_P
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
#define INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteName, Types, ...) \ #define INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteName, Types, ...) \
static_assert(sizeof(GTEST_STRINGIFY_(Prefix)) > 1, \ static_assert(sizeof(GTEST_STRINGIFY_(Prefix)) > 1, \
"test-suit-prefix must not be empty"); \ "test-suit-prefix must not be empty"); \
static bool gtest_##Prefix##_##SuiteName GTEST_ATTRIBUTE_UNUSED_ = \ GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED static bool \
::testing::internal::TypeParameterizedTestSuite< \ gtest_##Prefix##_##SuiteName = \
SuiteName, GTEST_SUITE_NAMESPACE_(SuiteName)::gtest_AllTests_, \ ::testing::internal::TypeParameterizedTestSuite< \
::testing::internal::GenerateTypeList<Types>::type>:: \ SuiteName, GTEST_SUITE_NAMESPACE_(SuiteName)::gtest_AllTests_, \
Register(GTEST_STRINGIFY_(Prefix), \ ::testing::internal::GenerateTypeList<Types>::type>:: \
::testing::internal::CodeLocation(__FILE__, __LINE__), \ Register( \
&GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName), \ GTEST_STRINGIFY_(Prefix), \
GTEST_STRINGIFY_(SuiteName), \ ::testing::internal::CodeLocation(__FILE__, __LINE__), \
GTEST_REGISTERED_TEST_NAMES_(SuiteName), \ &GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName), \
::testing::internal::GenerateNames< \ GTEST_STRINGIFY_(SuiteName), \
::testing::internal::NameGeneratorSelector< \ GTEST_REGISTERED_TEST_NAMES_(SuiteName), \
__VA_ARGS__>::type, \ ::testing::internal::GenerateNames< \
::testing::internal::GenerateTypeList<Types>::type>()) ::testing::internal::NameGeneratorSelector< \
__VA_ARGS__>::type, \
::testing::internal::GenerateTypeList<Types>::type>())
// Legacy API is deprecated but still available // Legacy API is deprecated but still available
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_

View File

@ -1501,7 +1501,8 @@ class NeverThrown {
\ \
private: \ private: \
void TestBody() override; \ void TestBody() override; \
static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_; \ GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED static ::testing::TestInfo* const \
test_info_; \
}; \ }; \
\ \
::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_suite_name, \ ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_suite_name, \

View File

@ -194,8 +194,6 @@
// //
// Macros for basic C++ coding: // Macros for basic C++ coding:
// GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning. // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
// GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a
// variable don't have to be used.
// GTEST_MUST_USE_RESULT_ - declares that a function's result must be used. // GTEST_MUST_USE_RESULT_ - declares that a function's result must be used.
// GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is // GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is
// suppressed (constant conditional). // suppressed (constant conditional).
@ -749,6 +747,20 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#define GTEST_HAVE_ATTRIBUTE_(x) 0 #define GTEST_HAVE_ATTRIBUTE_(x) 0
#endif #endif
// GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE
//
// A function-like feature checking macro that accepts C++11 style attributes.
// It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6
// (https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't
// find `__has_cpp_attribute`, will evaluate to 0.
#if defined(__has_cpp_attribute)
// NOTE: requiring __cplusplus above should not be necessary, but
// works around https://bugs.llvm.org/show_bug.cgi?id=23435.
#define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
#else
#define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) 0
#endif
// GTEST_HAVE_FEATURE_ // GTEST_HAVE_FEATURE_
// //
// A function-like feature checking macro that is a wrapper around // A function-like feature checking macro that is a wrapper around
@ -760,14 +772,22 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#endif #endif
// Use this annotation after a variable or parameter declaration to tell the // Use this annotation after a variable or parameter declaration to tell the
// compiler the variable/parameter does not have to be used. // compiler the variable/parameter may be used.
// Example: // Example:
// //
// GTEST_ATTRIBUTE_UNUSED_ int foo = bar(); // GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED int foo = bar();
#if GTEST_HAVE_ATTRIBUTE_(unused) //
#define GTEST_ATTRIBUTE_UNUSED_ __attribute__((unused)) // This can be removed once we only support only C++17 or newer and
// [[maybe_unused]] is available on all supported platforms.
#if GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(maybe_unused)
#define GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED [[maybe_unused]]
#elif GTEST_HAVE_ATTRIBUTE_(unused)
// This is inferior to [[maybe_unused]] as it can produce a
// -Wused-but-marked-unused warning on optionally used symbols, but it is all we
// have.
#define GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED __attribute__((__unused__))
#else #else
#define GTEST_ATTRIBUTE_UNUSED_ #define GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED
#endif #endif
// Use this annotation before a function that takes a printf format string. // Use this annotation before a function that takes a printf format string.

View File

@ -2163,7 +2163,7 @@ class UnitTestRecordPropertyTestEnvironment : public Environment {
}; };
// This will test property recording outside of any test or test case. // This will test property recording outside of any test or test case.
static Environment* record_property_env GTEST_ATTRIBUTE_UNUSED_ = GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED static Environment* record_property_env =
AddGlobalTestEnvironment(new UnitTestRecordPropertyTestEnvironment); AddGlobalTestEnvironment(new UnitTestRecordPropertyTestEnvironment);
// This group of tests is for predicate assertions (ASSERT_PRED*, etc) // This group of tests is for predicate assertions (ASSERT_PRED*, etc)
@ -6705,8 +6705,9 @@ TEST(ColoredOutputTest, UsesColorsWhenTermSupportsColors) {
// Verifies that StaticAssertTypeEq works in a namespace scope. // Verifies that StaticAssertTypeEq works in a namespace scope.
static bool dummy1 GTEST_ATTRIBUTE_UNUSED_ = StaticAssertTypeEq<bool, bool>(); GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED static bool dummy1 =
static bool dummy2 GTEST_ATTRIBUTE_UNUSED_ = StaticAssertTypeEq<bool, bool>();
GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED static bool dummy2 =
StaticAssertTypeEq<const int, const int>(); StaticAssertTypeEq<const int, const int>();
// Verifies that StaticAssertTypeEq works in a class. // Verifies that StaticAssertTypeEq works in a class.