From de77d355ac1808164b7247290f45b8133ce1246b Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Sun, 7 Apr 2019 21:27:41 +0200 Subject: cmState: Add scripted commands by value --- Source/cmFunctionCommand.cxx | 74 +++++++++++++++-------------------------- Source/cmLoadCommandCommand.cxx | 3 +- Source/cmMacroCommand.cxx | 54 ++++++++++-------------------- Source/cmState.cxx | 5 ++- Source/cmState.h | 3 +- 5 files changed, 50 insertions(+), 89 deletions(-) diff --git a/Source/cmFunctionCommand.cxx b/Source/cmFunctionCommand.cxx index 4041d62..8b664ad 100644 --- a/Source/cmFunctionCommand.cxx +++ b/Source/cmFunctionCommand.cxx @@ -5,8 +5,6 @@ #include #include -#include "cm_memory.hxx" - #include "cmAlgorithms.h" #include "cmExecutionStatus.h" #include "cmMakefile.h" @@ -15,35 +13,15 @@ #include "cmState.h" // define the class for function commands -class cmFunctionHelperCommand : public cmCommand +class cmFunctionHelperCommand { public: /** - * This is a virtual constructor for the command. - */ - std::unique_ptr Clone() override - { - auto newC = cm::make_unique(); - // we must copy when we clone - newC->Args = this->Args; - newC->Functions = this->Functions; - newC->Policies = this->Policies; - newC->FilePath = this->FilePath; - return std::unique_ptr(std::move(newC)); - } - - /** * This is called when the command is first encountered in * the CMakeLists.txt file. */ - bool InvokeInitialPass(const std::vector& args, - cmExecutionStatus&) override; - - bool InitialPass(std::vector const&, - cmExecutionStatus&) override - { - return false; - } + bool operator()(std::vector const& args, + cmExecutionStatus& inStatus) const; std::vector Args; std::vector Functions; @@ -51,12 +29,15 @@ public: std::string FilePath; }; -bool cmFunctionHelperCommand::InvokeInitialPass( - const std::vector& args, cmExecutionStatus& inStatus) +bool cmFunctionHelperCommand::operator()( + std::vector const& args, + cmExecutionStatus& inStatus) const { + cmMakefile& makefile = inStatus.GetMakefile(); + // Expand the argument list to the function. std::vector expandedArgs; - this->Makefile->ExpandArguments(args, expandedArgs); + makefile.ExpandArguments(args, expandedArgs); // make sure the number of arguments passed is at least the number // required by the signature @@ -64,30 +45,30 @@ bool cmFunctionHelperCommand::InvokeInitialPass( std::string errorMsg = "Function invoked with incorrect arguments for function named: "; errorMsg += this->Args[0]; - this->SetError(errorMsg); + inStatus.SetError(errorMsg); return false; } - cmMakefile::FunctionPushPop functionScope(this->Makefile, this->FilePath, + cmMakefile::FunctionPushPop functionScope(&makefile, this->FilePath, this->Policies); // set the value of argc std::ostringstream strStream; strStream << expandedArgs.size(); - this->Makefile->AddDefinition("ARGC", strStream.str().c_str()); - this->Makefile->MarkVariableAsUsed("ARGC"); + makefile.AddDefinition("ARGC", strStream.str().c_str()); + makefile.MarkVariableAsUsed("ARGC"); // set the values for ARGV0 ARGV1 ... for (unsigned int t = 0; t < expandedArgs.size(); ++t) { std::ostringstream tmpStream; tmpStream << "ARGV" << t; - this->Makefile->AddDefinition(tmpStream.str(), expandedArgs[t].c_str()); - this->Makefile->MarkVariableAsUsed(tmpStream.str()); + makefile.AddDefinition(tmpStream.str(), expandedArgs[t].c_str()); + makefile.MarkVariableAsUsed(tmpStream.str()); } // define the formal arguments for (unsigned int j = 1; j < this->Args.size(); ++j) { - this->Makefile->AddDefinition(this->Args[j], expandedArgs[j - 1].c_str()); + makefile.AddDefinition(this->Args[j], expandedArgs[j - 1].c_str()); } // define ARGV and ARGN @@ -95,17 +76,16 @@ bool cmFunctionHelperCommand::InvokeInitialPass( std::vector::const_iterator eit = expandedArgs.begin() + (this->Args.size() - 1); std::string argnDef = cmJoin(cmMakeRange(eit, expandedArgs.end()), ";"); - this->Makefile->AddDefinition("ARGV", argvDef.c_str()); - this->Makefile->MarkVariableAsUsed("ARGV"); - this->Makefile->AddDefinition("ARGN", argnDef.c_str()); - this->Makefile->MarkVariableAsUsed("ARGN"); + makefile.AddDefinition("ARGV", argvDef.c_str()); + makefile.MarkVariableAsUsed("ARGV"); + makefile.AddDefinition("ARGN", argnDef.c_str()); + makefile.MarkVariableAsUsed("ARGN"); // Invoke all the functions that were collected in the block. // for each function for (cmListFileFunction const& func : this->Functions) { - cmExecutionStatus status(*this->GetMakefile()); - if (!this->Makefile->ExecuteCommand(func, status) || - status.GetNestedError()) { + cmExecutionStatus status(makefile); + if (!makefile.ExecuteCommand(func, status) || status.GetNestedError()) { // The error message should have already included the call stack // so we do not need to report an error here. functionScope.Quiet(); @@ -132,11 +112,11 @@ bool cmFunctionFunctionBlocker::IsFunctionBlocked( // if this is the endfunction for this function then execute if (!this->Depth) { // create a new command and add it to cmake - auto f = cm::make_unique(); - f->Args = this->Args; - f->Functions = this->Functions; - f->FilePath = this->GetStartingContext().FilePath; - mf.RecordPolicies(f->Policies); + cmFunctionHelperCommand f; + f.Args = this->Args; + f.Functions = this->Functions; + f.FilePath = this->GetStartingContext().FilePath; + mf.RecordPolicies(f.Policies); mf.GetState()->AddScriptedCommand(this->Args[0], std::move(f)); // remove the function blocker now that the function is defined mf.RemoveFunctionBlocker(this, lff); diff --git a/Source/cmLoadCommandCommand.cxx b/Source/cmLoadCommandCommand.cxx index f5da2ee..78f4f83 100644 --- a/Source/cmLoadCommandCommand.cxx +++ b/Source/cmLoadCommandCommand.cxx @@ -247,7 +247,8 @@ bool cmLoadCommandCommand::InitialPass(std::vector const& args, // function blocker if (initFunction) { this->Makefile->GetState()->AddScriptedCommand( - args[0], cm::make_unique(initFunction)); + args[0], + cmLegacyCommandWrapper(cm::make_unique(initFunction))); return true; } this->SetError("Attempt to load command failed. " diff --git a/Source/cmMacroCommand.cxx b/Source/cmMacroCommand.cxx index 11f4bf8..22748b4 100644 --- a/Source/cmMacroCommand.cxx +++ b/Source/cmMacroCommand.cxx @@ -17,35 +17,15 @@ #include "cmSystemTools.h" // define the class for macro commands -class cmMacroHelperCommand : public cmCommand +class cmMacroHelperCommand { public: /** - * This is a virtual constructor for the command. - */ - std::unique_ptr Clone() override - { - auto newC = cm::make_unique(); - // we must copy when we clone - newC->Args = this->Args; - newC->Functions = this->Functions; - newC->FilePath = this->FilePath; - newC->Policies = this->Policies; - return std::unique_ptr(std::move(newC)); - } - - /** * This is called when the command is first encountered in * the CMakeLists.txt file. */ - bool InvokeInitialPass(const std::vector& args, - cmExecutionStatus&) override; - - bool InitialPass(std::vector const&, - cmExecutionStatus&) override - { - return false; - } + bool operator()(std::vector const& args, + cmExecutionStatus& inStatus) const; std::vector Args; std::vector Functions; @@ -53,12 +33,15 @@ public: std::string FilePath; }; -bool cmMacroHelperCommand::InvokeInitialPass( - const std::vector& args, cmExecutionStatus& inStatus) +bool cmMacroHelperCommand::operator()( + std::vector const& args, + cmExecutionStatus& inStatus) const { + cmMakefile& makefile = inStatus.GetMakefile(); + // Expand the argument list to the macro. std::vector expandedArgs; - this->Makefile->ExpandArguments(args, expandedArgs); + makefile.ExpandArguments(args, expandedArgs); // make sure the number of arguments passed is at least the number // required by the signature @@ -66,11 +49,11 @@ bool cmMacroHelperCommand::InvokeInitialPass( std::string errorMsg = "Macro invoked with incorrect arguments for macro named: "; errorMsg += this->Args[0]; - this->SetError(errorMsg); + inStatus.SetError(errorMsg); return false; } - cmMakefile::MacroPushPop macroScope(this->Makefile, this->FilePath, + cmMakefile::MacroPushPop macroScope(&makefile, this->FilePath, this->Policies); // set the value of argc @@ -132,9 +115,8 @@ bool cmMacroHelperCommand::InvokeInitialPass( arg.Line = k.Line; newLFF.Arguments.push_back(std::move(arg)); } - cmExecutionStatus status(*this->GetMakefile()); - if (!this->Makefile->ExecuteCommand(newLFF, status) || - status.GetNestedError()) { + cmExecutionStatus status(makefile); + if (!makefile.ExecuteCommand(newLFF, status) || status.GetNestedError()) { // The error message should have already included the call stack // so we do not need to report an error here. macroScope.Quiet(); @@ -166,11 +148,11 @@ bool cmMacroFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff, if (!this->Depth) { mf.AppendProperty("MACROS", this->Args[0].c_str()); // create a new command and add it to cmake - auto f = cm::make_unique(); - f->Args = this->Args; - f->Functions = this->Functions; - f->FilePath = this->GetStartingContext().FilePath; - mf.RecordPolicies(f->Policies); + cmMacroHelperCommand f; + f.Args = this->Args; + f.Functions = this->Functions; + f.FilePath = this->GetStartingContext().FilePath; + mf.RecordPolicies(f.Policies); mf.GetState()->AddScriptedCommand(this->Args[0], std::move(f)); // remove the function blocker now that the macro is defined mf.RemoveFunctionBlocker(this, lff); diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 82b0a52..441f11c 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -458,8 +458,7 @@ void cmState::AddUnexpectedCommand(std::string const& name, const char* error) }); } -void cmState::AddScriptedCommand(std::string const& name, - std::unique_ptr command) +void cmState::AddScriptedCommand(std::string const& name, Command command) { std::string sName = cmSystemTools::LowerCase(name); @@ -468,7 +467,7 @@ void cmState::AddScriptedCommand(std::string const& name, this->ScriptedCommands["_" + sName] = oldCmd; } - this->ScriptedCommands[sName] = cmLegacyCommandWrapper(std::move(command)); + this->ScriptedCommands[sName] = std::move(command); } cmState::Command cmState::GetCommand(std::string const& name) const diff --git a/Source/cmState.h b/Source/cmState.h index a6e9386..07c1c9b 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -158,8 +158,7 @@ public: std::unique_ptr command, cmPolicies::PolicyID policy, const char* message); void AddUnexpectedCommand(std::string const& name, const char* error); - void AddScriptedCommand(std::string const& name, - std::unique_ptr command); + void AddScriptedCommand(std::string const& name, Command command); void RemoveBuiltinCommand(std::string const& name); void RemoveUserDefinedCommands(); std::vector GetCommandNames() const; -- cgit v0.12