From 4bb49ed640e34e23187ad7ea689693ef9927033f Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 4 Oct 2018 18:28:05 -0400 Subject: Apply clang-tidy modernize-use-nullptr to googletest. Now that googletest has moved to C++11, it should no longer use NULL or 0 for the null pointer. This patch converts all such usages to nullptr using clang-tidy. This prevents LLVM from issuing -Wzero-as-null-pointer-constant warnings. PiperOrigin-RevId: 215814400 --- googlemock/include/gmock/gmock-actions.h | 29 ++++++++-------- googlemock/include/gmock/gmock-matchers.h | 48 ++++++++++++-------------- googlemock/include/gmock/gmock-spec-builders.h | 20 +++++------ 3 files changed, 47 insertions(+), 50 deletions(-) (limited to 'googlemock/include') diff --git a/googlemock/include/gmock/gmock-actions.h b/googlemock/include/gmock/gmock-actions.h index b82313d5..e3b3f094 100644 --- a/googlemock/include/gmock/gmock-actions.h +++ b/googlemock/include/gmock/gmock-actions.h @@ -137,7 +137,7 @@ template class BuiltInDefaultValue { public: static bool Exists() { return true; } - static T* Get() { return NULL; } + static T* Get() { return nullptr; } }; // The following specializations define the default values for @@ -220,11 +220,11 @@ class DefaultValue { // Unsets the default value for type T. static void Clear() { delete producer_; - producer_ = NULL; + producer_ = nullptr; } // Returns true iff the user has set the default value for type T. - static bool IsSet() { return producer_ != NULL; } + static bool IsSet() { return producer_ != nullptr; } // Returns true if T has a default return value set by the user or there // exists a built-in default value. @@ -236,8 +236,8 @@ class DefaultValue { // otherwise returns the built-in default value. Requires that Exists() // is true, which ensures that the return value is well-defined. static T Get() { - return producer_ == NULL ? - internal::BuiltInDefaultValue::Get() : producer_->Produce(); + return producer_ == nullptr ? internal::BuiltInDefaultValue::Get() + : producer_->Produce(); } private: @@ -282,12 +282,10 @@ class DefaultValue { } // Unsets the default value for type T&. - static void Clear() { - address_ = NULL; - } + static void Clear() { address_ = nullptr; } // Returns true iff the user has set the default value for type T&. - static bool IsSet() { return address_ != NULL; } + static bool IsSet() { return address_ != nullptr; } // Returns true if T has a default return value set by the user or there // exists a built-in default value. @@ -299,8 +297,8 @@ class DefaultValue { // otherwise returns the built-in default value if there is one; // otherwise aborts the process. static T& Get() { - return address_ == NULL ? - internal::BuiltInDefaultValue::Get() : *address_; + return address_ == nullptr ? internal::BuiltInDefaultValue::Get() + : *address_; } private: @@ -318,11 +316,11 @@ class DefaultValue { // Points to the user-set default value for type T. template -typename DefaultValue::ValueProducer* DefaultValue::producer_ = NULL; +typename DefaultValue::ValueProducer* DefaultValue::producer_ = nullptr; // Points to the user-set default value for type T&. template -T* DefaultValue::address_ = NULL; +T* DefaultValue::address_ = nullptr; // Implement this interface to define an action for function type F. template @@ -1108,8 +1106,9 @@ Action::Action(const Action& from) #if GTEST_LANG_CXX11 fun_(from.fun_), #endif - impl_(from.impl_ == NULL ? NULL - : new internal::ActionAdaptor(from)) { + impl_(from.impl_ == nullptr + ? nullptr + : new internal::ActionAdaptor(from)) { } // Creates an action that returns 'value'. 'value' is passed by value diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index b4961d44..0f7745ea 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -94,8 +94,7 @@ class MatchResultListener { // is NULL. template MatchResultListener& operator<<(const T& x) { - if (stream_ != NULL) - *stream_ << x; + if (stream_ != nullptr) *stream_ << x; return *this; } @@ -106,7 +105,7 @@ class MatchResultListener { // the match result. A matcher's MatchAndExplain() method can use // this information to avoid generating the explanation when no one // intends to hear it. - bool IsInterested() const { return stream_ != NULL; } + bool IsInterested() const { return stream_ != nullptr; } private: ::std::ostream* const stream_; @@ -261,7 +260,7 @@ struct AnyGe { // A match result listener that ignores the explanation. class DummyMatchResultListener : public MatchResultListener { public: - DummyMatchResultListener() : MatchResultListener(NULL) {} + DummyMatchResultListener() : MatchResultListener(nullptr) {} private: GTEST_DISALLOW_COPY_AND_ASSIGN_(DummyMatchResultListener); @@ -333,7 +332,7 @@ class MatcherBase { const MatcherInterface* impl, typename internal::EnableIf< !internal::IsSame::value>::type* = - NULL) + nullptr) : impl_(new internal::MatcherInterfaceAdapter(impl)) {} virtual ~MatcherBase() {} @@ -375,9 +374,11 @@ class Matcher : public internal::MatcherBase { : internal::MatcherBase(impl) {} template - explicit Matcher(const MatcherInterface* impl, - typename internal::EnableIf::value>::type* = NULL) + explicit Matcher( + const MatcherInterface* impl, + typename internal::EnableIf< + !internal::IsSame::value>::type* = + nullptr) : internal::MatcherBase(impl) {} // Implicit constructor here allows people to write @@ -850,7 +851,7 @@ namespace internal { // If the explanation is not empty, prints it to the ostream. inline void PrintIfNotEmpty(const std::string& explanation, ::std::ostream* os) { - if (explanation != "" && os != NULL) { + if (explanation != "" && os != nullptr) { *os << ", " << explanation; } } @@ -1321,7 +1322,7 @@ class StrEqualityMatcher { // wchar_t* template bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { - if (s == NULL) { + if (s == nullptr) { return !expect_eq_; } return MatchAndExplain(StringType(s), listener); @@ -1391,7 +1392,7 @@ class HasSubstrMatcher { // wchar_t* template bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { - return s != NULL && MatchAndExplain(StringType(s), listener); + return s != nullptr && MatchAndExplain(StringType(s), listener); } // Matches anything that can convert to StringType. @@ -1448,7 +1449,7 @@ class StartsWithMatcher { // wchar_t* template bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { - return s != NULL && MatchAndExplain(StringType(s), listener); + return s != nullptr && MatchAndExplain(StringType(s), listener); } // Matches anything that can convert to StringType. @@ -1504,7 +1505,7 @@ class EndsWithMatcher { // wchar_t* template bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { - return s != NULL && MatchAndExplain(StringType(s), listener); + return s != nullptr && MatchAndExplain(StringType(s), listener); } // Matches anything that can convert to StringType. @@ -1557,7 +1558,7 @@ class MatchesRegexMatcher { // wchar_t* template bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { - return s != NULL && MatchAndExplain(std::string(s), listener); + return s != nullptr && MatchAndExplain(std::string(s), listener); } // Matches anything that can convert to std::string. @@ -2347,8 +2348,7 @@ class PointeeMatcher { virtual bool MatchAndExplain(Pointer pointer, MatchResultListener* listener) const { - if (GetRawPointer(pointer) == NULL) - return false; + if (GetRawPointer(pointer) == nullptr) return false; *listener << "which points to "; return MatchPrintAndExplain(*pointer, matcher_, listener); @@ -2431,7 +2431,7 @@ class WhenDynamicCastToMatcher : public WhenDynamicCastToMatcherBase { bool MatchAndExplain(From& from, MatchResultListener* listener) const { // We don't want an std::bad_cast here, so do the cast with pointers. To* to = dynamic_cast(&from); - if (to == NULL) { + if (to == nullptr) { *listener << "which cannot be dynamic_cast to " << this->GetToName(); return false; } @@ -2485,8 +2485,7 @@ class FieldMatcher { bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p, MatchResultListener* listener) const { - if (p == NULL) - return false; + if (p == nullptr) return false; *listener << "which points to an object "; // Since *p has a field, it must be a class/struct/union type and @@ -2570,8 +2569,7 @@ class PropertyMatcher { bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p, MatchResultListener* listener) const { - if (p == NULL) - return false; + if (p == nullptr) return false; *listener << "which points to an object "; // Since *p has a property method, it must be a class/struct/union @@ -2615,7 +2613,7 @@ struct CallableTraits { typedef ResType(*StorageType)(ArgType); static void CheckIsValid(ResType(*f)(ArgType)) { - GTEST_CHECK_(f != NULL) + GTEST_CHECK_(f != nullptr) << "NULL function pointer is passed into ResultOf()."; } template @@ -2857,7 +2855,7 @@ class ContainerEqMatcher { return true; ::std::ostream* const os = listener->stream(); - if (os != NULL) { + if (os != nullptr) { // Something is different. Check for extra values first. bool printed_header = false; for (typename LhsStlContainer::const_iterator it = @@ -4136,11 +4134,11 @@ class AnyCastMatcher { ::testing::MatchResultListener* listener) const { if (!listener->IsInterested()) { const T* ptr = any_cast(&value); - return ptr != NULL && matcher_.Matches(*ptr); + return ptr != nullptr && matcher_.Matches(*ptr); } const T* elem = any_cast(&value); - if (elem == NULL) { + if (elem == nullptr) { *listener << "whose value is not of type '" << GetTypeName() << "'"; return false; } diff --git a/googlemock/include/gmock/gmock-spec-builders.h b/googlemock/include/gmock/gmock-spec-builders.h index 436e2d8b..c8e864cc 100644 --- a/googlemock/include/gmock/gmock-spec-builders.h +++ b/googlemock/include/gmock/gmock-spec-builders.h @@ -1213,7 +1213,7 @@ class TypedExpectation : public ExpectationBase { // FIXME: allow the user to control whether // unexpected calls should fail immediately or continue using a // flag --gmock_unexpected_calls_are_fatal. - return NULL; + return nullptr; } IncrementCallCount(); @@ -1498,7 +1498,7 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { return spec; } - return NULL; + return nullptr; } // Performs the default action of this mock function on the given @@ -1513,7 +1513,7 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { const std::string& call_description) const { const OnCallSpec* const spec = this->FindOnCallSpec(args); - if (spec != NULL) { + if (spec != nullptr) { return spec->GetAction().Perform(internal::move(args)); } const std::string message = @@ -1630,7 +1630,7 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { // Adds this expectation into the implicit sequence if there is one. Sequence* const implicit_sequence = g_gmock_implicit_sequence.get(); - if (implicit_sequence != NULL) { + if (implicit_sequence != nullptr) { implicit_sequence->AddExpectation(Expectation(untyped_expectation)); } @@ -1649,7 +1649,7 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { ::std::ostream* os) const { const OnCallSpec* const spec = FindOnCallSpec(args); - if (spec == NULL) { + if (spec == nullptr) { *os << (internal::type_equals::value ? "returning directly.\n" : "returning default value.\n"); @@ -1699,9 +1699,9 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { *static_cast(untyped_args); MutexLock l(&g_gmock_mutex); TypedExpectation* exp = this->FindMatchingExpectationLocked(args); - if (exp == NULL) { // A match wasn't found. + if (exp == nullptr) { // A match wasn't found. this->FormatUnexpectedCallMessageLocked(args, what, why); - return NULL; + return nullptr; } // This line must be done before calling GetActionForArguments(), @@ -1709,8 +1709,8 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { // its saturation status. *is_excessive = exp->IsSaturated(); const Action* action = exp->GetActionForArguments(this, args, what, why); - if (action != NULL && action->IsDoDefault()) - action = NULL; // Normalize "do default" to NULL. + if (action != nullptr && action->IsDoDefault()) + action = nullptr; // Normalize "do default" to NULL. *untyped_action = action; return exp; } @@ -1740,7 +1740,7 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { return exp; } } - return NULL; + return nullptr; } // Returns a message that the arguments don't match any expectation. -- cgit v1.2.3