Index: ext/googletest/googlemock/include/gmock/gmock-spec-builders.h =================================================================== diff -u -N -r4a481bbe77043e0bda2435c6d62a02700b3e46c5 -r2e4eacb299f21d06196fe13140b4b0d095abdca9 --- ext/googletest/googlemock/include/gmock/gmock-spec-builders.h (.../gmock-spec-builders.h) (revision 4a481bbe77043e0bda2435c6d62a02700b3e46c5) +++ ext/googletest/googlemock/include/gmock/gmock-spec-builders.h (.../gmock-spec-builders.h) (revision 2e4eacb299f21d06196fe13140b4b0d095abdca9) @@ -26,9 +26,8 @@ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Author: wan@google.com (Zhanyong Wan) + // Google Mock - a framework for writing C++ mock classes. // // This file implements the ON_CALL() and EXPECT_CALL() macros. @@ -57,6 +56,8 @@ // where all clauses are optional, and .InSequence()/.After()/ // .WillOnce() can appear any number of times. +// GOOGLETEST_CM0002 DO NOT DELETE + #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ #define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ @@ -65,18 +66,20 @@ #include #include #include - -#if GTEST_HAS_EXCEPTIONS -# include // NOLINT -#endif - #include "gmock/gmock-actions.h" #include "gmock/gmock-cardinalities.h" #include "gmock/gmock-matchers.h" #include "gmock/internal/gmock-internal-utils.h" #include "gmock/internal/gmock-port.h" #include "gtest/gtest.h" +#if GTEST_HAS_EXCEPTIONS +# include // NOLINT +#endif + +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ +/* class A needs to have dll-interface to be used by clients of class B */) + namespace testing { // An abstract handle of an expectation. @@ -148,15 +151,13 @@ // action fails. // L = * virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction( - const void* untyped_args, - const string& call_description) const = 0; + void* untyped_args, const std::string& call_description) const = 0; // Performs the given action with the given arguments and returns // the action's result. // L = * virtual UntypedActionResultHolderBase* UntypedPerformAction( - const void* untyped_action, - const void* untyped_args) const = 0; + const void* untyped_action, void* untyped_args) const = 0; // Writes a message that the call is uninteresting (i.e. neither // explicitly expected nor explicitly unexpected) to the given @@ -186,7 +187,7 @@ // this information in the global mock registry. Will be called // whenever an EXPECT_CALL() or ON_CALL() is executed on this mock // method. - // TODO(wan@google.com): rename to SetAndRegisterOwner(). + // FIXME: rename to SetAndRegisterOwner(). void RegisterOwner(const void* mock_obj) GTEST_LOCK_EXCLUDED_(g_gmock_mutex); @@ -211,9 +212,8 @@ // arguments. This function can be safely called from multiple // threads concurrently. The caller is responsible for deleting the // result. - UntypedActionResultHolderBase* UntypedInvokeWith( - const void* untyped_args) - GTEST_LOCK_EXCLUDED_(g_gmock_mutex); + UntypedActionResultHolderBase* UntypedInvokeWith(void* untyped_args) + GTEST_LOCK_EXCLUDED_(g_gmock_mutex); protected: typedef std::vector UntypedOnCallSpecs; @@ -238,6 +238,14 @@ UntypedOnCallSpecs untyped_on_call_specs_; // All expectations for this function mocker. + // + // It's undefined behavior to interleave expectations (EXPECT_CALLs + // or ON_CALLs) and mock function calls. Also, the order of + // expectations is important. Therefore it's a logic race condition + // to read/write untyped_expectations_ concurrently. In order for + // tools like tsan to catch concurrent read/write accesses to + // untyped_expectations, we deliberately leave accesses to it + // unprotected. UntypedExpectations untyped_expectations_; }; // class UntypedFunctionMockerBase @@ -263,12 +271,14 @@ }; // Asserts that the ON_CALL() statement has a certain property. - void AssertSpecProperty(bool property, const string& failure_message) const { + void AssertSpecProperty(bool property, + const std::string& failure_message) const { Assert(property, file_, line_, failure_message); } // Expects that the ON_CALL() statement has a certain property. - void ExpectSpecProperty(bool property, const string& failure_message) const { + void ExpectSpecProperty(bool property, + const std::string& failure_message) const { Expect(property, file_, line_, failure_message); } @@ -362,7 +372,6 @@ kAllow, kWarn, kFail, - kDefault = kWarn // By default, warn about uninteresting calls. }; } // namespace internal @@ -690,7 +699,7 @@ class GTEST_API_ ExpectationBase { public: // source_text is the EXPECT_CALL(...) source that created this Expectation. - ExpectationBase(const char* file, int line, const string& source_text); + ExpectationBase(const char* file, int line, const std::string& source_text); virtual ~ExpectationBase(); @@ -738,12 +747,14 @@ virtual Expectation GetHandle() = 0; // Asserts that the EXPECT_CALL() statement has the given property. - void AssertSpecProperty(bool property, const string& failure_message) const { + void AssertSpecProperty(bool property, + const std::string& failure_message) const { Assert(property, file_, line_, failure_message); } // Expects that the EXPECT_CALL() statement has the given property. - void ExpectSpecProperty(bool property, const string& failure_message) const { + void ExpectSpecProperty(bool property, + const std::string& failure_message) const { Expect(property, file_, line_, failure_message); } @@ -845,7 +856,7 @@ // an EXPECT_CALL() statement finishes. const char* file_; // The file that contains the expectation. int line_; // The line number of the expectation. - const string source_text_; // The EXPECT_CALL(...) source text. + const std::string source_text_; // The EXPECT_CALL(...) source text. // True iff the cardinality is specified explicitly. bool cardinality_specified_; Cardinality cardinality_; // The cardinality of the expectation. @@ -880,8 +891,8 @@ typedef typename Function::ArgumentMatcherTuple ArgumentMatcherTuple; typedef typename Function::Result Result; - TypedExpectation(FunctionMockerBase* owner, - const char* a_file, int a_line, const string& a_source_text, + TypedExpectation(FunctionMockerBase* owner, const char* a_file, int a_line, + const std::string& a_source_text, const ArgumentMatcherTuple& m) : ExpectationBase(a_file, a_line, a_source_text), owner_(owner), @@ -1199,7 +1210,7 @@ mocker->DescribeDefaultActionTo(args, what); DescribeCallCountTo(why); - // TODO(wan@google.com): allow the user to control whether + // FIXME: allow the user to control whether // unexpected calls should fail immediately or continue using a // flag --gmock_unexpected_calls_are_fatal. return NULL; @@ -1240,7 +1251,7 @@ // Logs a message including file and line number information. GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, const char* file, int line, - const string& message); + const std::string& message); template class MockSpec { @@ -1251,36 +1262,41 @@ // Constructs a MockSpec object, given the function mocker object // that the spec is associated with. - explicit MockSpec(internal::FunctionMockerBase* function_mocker) - : function_mocker_(function_mocker) {} + MockSpec(internal::FunctionMockerBase* function_mocker, + const ArgumentMatcherTuple& matchers) + : function_mocker_(function_mocker), matchers_(matchers) {} // Adds a new default action spec to the function mocker and returns // the newly created spec. internal::OnCallSpec& InternalDefaultActionSetAt( const char* file, int line, const char* obj, const char* call) { LogWithLocation(internal::kInfo, file, line, - string("ON_CALL(") + obj + ", " + call + ") invoked"); + std::string("ON_CALL(") + obj + ", " + call + ") invoked"); return function_mocker_->AddNewOnCallSpec(file, line, matchers_); } // Adds a new expectation spec to the function mocker and returns // the newly created spec. internal::TypedExpectation& InternalExpectedAt( const char* file, int line, const char* obj, const char* call) { - const string source_text(string("EXPECT_CALL(") + obj + ", " + call + ")"); + const std::string source_text(std::string("EXPECT_CALL(") + obj + ", " + + call + ")"); LogWithLocation(internal::kInfo, file, line, source_text + " invoked"); return function_mocker_->AddNewExpectation( file, line, source_text, matchers_); } + // This operator overload is used to swallow the superfluous parameter list + // introduced by the ON/EXPECT_CALL macros. See the macro comments for more + // explanation. + MockSpec& operator()(const internal::WithoutMatchers&, void* const) { + return *this; + } + private: template friend class internal::FunctionMocker; - void SetMatchers(const ArgumentMatcherTuple& matchers) { - matchers_ = matchers; - } - // The function mocker that owns this spec. internal::FunctionMockerBase* const function_mocker_; // The argument matchers specified in the spec. @@ -1344,12 +1360,8 @@ // we need to temporarily disable the warning. We have to do it for // the entire class to suppress the warning, even though it's about // the constructor only. +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4355) -#ifdef _MSC_VER -# pragma warning(push) // Saves the current warning state. -# pragma warning(disable:4355) // Temporarily disables warning 4355. -#endif // _MSV_VER - // C++ treats the void type specially. For example, you cannot define // a void-typed variable or pass a void value to a function. // ActionResultHolder holds a value of type T, where T must be a @@ -1388,19 +1400,20 @@ template static ActionResultHolder* PerformDefaultAction( const FunctionMockerBase* func_mocker, - const typename Function::ArgumentTuple& args, - const string& call_description) { - return new ActionResultHolder(Wrapper( - func_mocker->PerformDefaultAction(args, call_description))); + typename RvalueRef::ArgumentTuple>::type args, + const std::string& call_description) { + return new ActionResultHolder(Wrapper(func_mocker->PerformDefaultAction( + internal::move(args), call_description))); } // Performs the given action and returns the result in a new-ed // ActionResultHolder. template - static ActionResultHolder* - PerformAction(const Action& action, - const typename Function::ArgumentTuple& args) { - return new ActionResultHolder(Wrapper(action.Perform(args))); + static ActionResultHolder* PerformAction( + const Action& action, + typename RvalueRef::ArgumentTuple>::type args) { + return new ActionResultHolder( + Wrapper(action.Perform(internal::move(args)))); } private: @@ -1428,9 +1441,9 @@ template static ActionResultHolder* PerformDefaultAction( const FunctionMockerBase* func_mocker, - const typename Function::ArgumentTuple& args, - const string& call_description) { - func_mocker->PerformDefaultAction(args, call_description); + typename RvalueRef::ArgumentTuple>::type args, + const std::string& call_description) { + func_mocker->PerformDefaultAction(internal::move(args), call_description); return new ActionResultHolder; } @@ -1439,8 +1452,8 @@ template static ActionResultHolder* PerformAction( const Action& action, - const typename Function::ArgumentTuple& args) { - action.Perform(args); + typename RvalueRef::ArgumentTuple>::type args) { + action.Perform(internal::move(args)); return new ActionResultHolder; } @@ -1459,7 +1472,7 @@ typedef typename Function::ArgumentTuple ArgumentTuple; typedef typename Function::ArgumentMatcherTuple ArgumentMatcherTuple; - FunctionMockerBase() : current_spec_(this) {} + FunctionMockerBase() {} // The destructor verifies that all expectations on this mock // function have been satisfied. If not, it will report Google Test @@ -1495,14 +1508,16 @@ // mutable state of this object, and thus can be called concurrently // without locking. // L = * - Result PerformDefaultAction(const ArgumentTuple& args, - const string& call_description) const { + Result PerformDefaultAction( + typename RvalueRef::ArgumentTuple>::type args, + const std::string& call_description) const { const OnCallSpec* const spec = this->FindOnCallSpec(args); if (spec != NULL) { - return spec->GetAction().Perform(args); + return spec->GetAction().Perform(internal::move(args)); } - const string message = call_description + + const std::string message = + call_description + "\n The mock function has no default action " "set, and its return type has no default value set."; #if GTEST_HAS_EXCEPTIONS @@ -1521,25 +1536,24 @@ // action fails. The caller is responsible for deleting the result. // L = * virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction( - const void* untyped_args, // must point to an ArgumentTuple - const string& call_description) const { - const ArgumentTuple& args = - *static_cast(untyped_args); - return ResultHolder::PerformDefaultAction(this, args, call_description); + void* untyped_args, // must point to an ArgumentTuple + const std::string& call_description) const { + ArgumentTuple* args = static_cast(untyped_args); + return ResultHolder::PerformDefaultAction(this, internal::move(*args), + call_description); } // Performs the given action with the given arguments and returns // the action's result. The caller is responsible for deleting the // result. // L = * virtual UntypedActionResultHolderBase* UntypedPerformAction( - const void* untyped_action, const void* untyped_args) const { + const void* untyped_action, void* untyped_args) const { // Make a copy of the action before performing it, in case the // action deletes the mock object (and thus deletes itself). const Action action = *static_cast*>(untyped_action); - const ArgumentTuple& args = - *static_cast(untyped_args); - return ResultHolder::PerformAction(action, args); + ArgumentTuple* args = static_cast(untyped_args); + return ResultHolder::PerformAction(action, internal::move(*args)); } // Implements UntypedFunctionMockerBase::ClearDefaultActionsLocked(): @@ -1579,10 +1593,14 @@ // Returns the result of invoking this mock function with the given // arguments. This function can be safely called from multiple // threads concurrently. - Result InvokeWith(const ArgumentTuple& args) - GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { + Result InvokeWith( + typename RvalueRef::ArgumentTuple>::type args) + GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { + // const_cast is required since in C++98 we still pass ArgumentTuple around + // by const& instead of rvalue reference. + void* untyped_args = const_cast(static_cast(&args)); scoped_ptr holder( - DownCast_(this->UntypedInvokeWith(&args))); + DownCast_(this->UntypedInvokeWith(untyped_args))); return holder->Unwrap(); } @@ -1598,16 +1616,16 @@ } // Adds and returns an expectation spec for this mock function. - TypedExpectation& AddNewExpectation( - const char* file, - int line, - const string& source_text, - const ArgumentMatcherTuple& m) - GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { + TypedExpectation& AddNewExpectation(const char* file, int line, + const std::string& source_text, + const ArgumentMatcherTuple& m) + GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line); TypedExpectation* const expectation = new TypedExpectation(this, file, line, source_text, m); const linked_ptr untyped_expectation(expectation); + // See the definition of untyped_expectations_ for why access to + // it is unprotected here. untyped_expectations_.push_back(untyped_expectation); // Adds this expectation into the implicit sequence if there is one. @@ -1619,10 +1637,6 @@ return *expectation; } - // The current spec (either default action spec or expectation spec) - // being described on this function mocker. - MockSpec& current_spec() { return current_spec_; } - private: template friend class TypedExpectation; @@ -1715,6 +1729,8 @@ const ArgumentTuple& args) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { g_gmock_mutex.AssertHeld(); + // See the definition of untyped_expectations_ for why access to + // it is unprotected here. for (typename UntypedExpectations::const_reverse_iterator it = untyped_expectations_.rbegin(); it != untyped_expectations_.rend(); ++it) { @@ -1765,14 +1781,10 @@ } } - // The current spec (either default action spec or expectation spec) - // being described on this function mocker. - MockSpec current_spec_; - // There is no generally useful and implementable semantics of // copying a mock object, so copying a mock is usually a user error. // Thus we disallow copying function mockers. If the user really - // wants to copy a mock object, he should implement his own copy + // wants to copy a mock object, they should implement their own copy // operation, for example: // // class MockFoo : public Foo { @@ -1784,9 +1796,7 @@ GTEST_DISALLOW_COPY_AND_ASSIGN_(FunctionMockerBase); }; // class FunctionMockerBase -#ifdef _MSC_VER -# pragma warning(pop) // Restores the warning state. -#endif // _MSV_VER +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4355 // Implements methods of FunctionMockerBase. @@ -1796,7 +1806,7 @@ // Reports an uninteresting call (whose description is in msg) in the // manner specified by 'reaction'. -void ReportUninterestingCall(CallReaction reaction, const string& msg); +void ReportUninterestingCall(CallReaction reaction, const std::string& msg); } // namespace internal @@ -1831,17 +1841,78 @@ } // namespace testing -// A separate macro is required to avoid compile errors when the name -// of the method used in call is a result of macro expansion. -// See CompilesWithMethodNameExpandedFromMacro tests in -// internal/gmock-spec-builders_test.cc for more details. -#define GMOCK_ON_CALL_IMPL_(obj, call) \ - ((obj).gmock_##call).InternalDefaultActionSetAt(__FILE__, __LINE__, \ - #obj, #call) -#define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call) +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 -#define GMOCK_EXPECT_CALL_IMPL_(obj, call) \ - ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call) -#define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call) +// Implementation for ON_CALL and EXPECT_CALL macros. A separate macro is +// required to avoid compile errors when the name of the method used in call is +// a result of macro expansion. See CompilesWithMethodNameExpandedFromMacro +// tests in internal/gmock-spec-builders_test.cc for more details. +// +// This macro supports statements both with and without parameter matchers. If +// the parameter list is omitted, gMock will accept any parameters, which allows +// tests to be written that don't need to encode the number of method +// parameter. This technique may only be used for non-overloaded methods. +// +// // These are the same: +// ON_CALL(mock, NoArgsMethod()).WillByDefault(...); +// ON_CALL(mock, NoArgsMethod).WillByDefault(...); +// +// // As are these: +// ON_CALL(mock, TwoArgsMethod(_, _)).WillByDefault(...); +// ON_CALL(mock, TwoArgsMethod).WillByDefault(...); +// +// // Can also specify args if you want, of course: +// ON_CALL(mock, TwoArgsMethod(_, 45)).WillByDefault(...); +// +// // Overloads work as long as you specify parameters: +// ON_CALL(mock, OverloadedMethod(_)).WillByDefault(...); +// ON_CALL(mock, OverloadedMethod(_, _)).WillByDefault(...); +// +// // Oops! Which overload did you want? +// ON_CALL(mock, OverloadedMethod).WillByDefault(...); +// => ERROR: call to member function 'gmock_OverloadedMethod' is ambiguous +// +// How this works: The mock class uses two overloads of the gmock_Method +// expectation setter method plus an operator() overload on the MockSpec object. +// In the matcher list form, the macro expands to: +// +// // This statement: +// ON_CALL(mock, TwoArgsMethod(_, 45))... +// +// // ...expands to: +// mock.gmock_TwoArgsMethod(_, 45)(WithoutMatchers(), nullptr)... +// |-------------v---------------||------------v-------------| +// invokes first overload swallowed by operator() +// +// // ...which is essentially: +// mock.gmock_TwoArgsMethod(_, 45)... +// +// Whereas the form without a matcher list: +// +// // This statement: +// ON_CALL(mock, TwoArgsMethod)... +// +// // ...expands to: +// mock.gmock_TwoArgsMethod(WithoutMatchers(), nullptr)... +// |-----------------------v--------------------------| +// invokes second overload +// +// // ...which is essentially: +// mock.gmock_TwoArgsMethod(_, _)... +// +// The WithoutMatchers() argument is used to disambiguate overloads and to +// block the caller from accidentally invoking the second overload directly. The +// second argument is an internal type derived from the method signature. The +// failure to disambiguate two overloads of this method in the ON_CALL statement +// is how we block callers from setting expectations on overloaded methods. +#define GMOCK_ON_CALL_IMPL_(mock_expr, Setter, call) \ + ((mock_expr).gmock_##call)(::testing::internal::GetWithoutMatchers(), NULL) \ + .Setter(__FILE__, __LINE__, #mock_expr, #call) +#define ON_CALL(obj, call) \ + GMOCK_ON_CALL_IMPL_(obj, InternalDefaultActionSetAt, call) + +#define EXPECT_CALL(obj, call) \ + GMOCK_ON_CALL_IMPL_(obj, InternalExpectedAt, call) + #endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_