From 1eebc2956321c2e7da00a5d35e207bedb899c804 Mon Sep 17 00:00:00 2001 From: Daniel Pfeifer Date: Mon, 26 Dec 2016 10:38:36 +0100 Subject: cmCommand: deprecate functions GetMakefile and SetError Replace the members for the Makefile and the Error with a cmExecutionStatus. Re-implement GetMakefile and SetError based on that. Both functions should be called directly on the cmExecutionStatus that is passed to InitialPass. This will help us make all Commands immutable and remove the need for cloning. --- Source/CTest/cmCTestHandlerCommand.cxx | 17 ++++++++--------- Source/cmCPluginAPI.cxx | 2 +- Source/cmCommand.cxx | 18 ++++++++---------- Source/cmCommand.h | 11 ++++------- Source/cmDisallowedCommand.cxx | 6 ++---- Source/cmExecutionStatus.h | 19 +++++++++++++++++++ Source/cmFileCopier.cxx | 7 ++----- Source/cmForEachCommand.cxx | 2 +- Source/cmFunctionCommand.cxx | 2 +- Source/cmIfCommand.cxx | 2 +- Source/cmMacroCommand.cxx | 2 +- Source/cmMakefile.cxx | 6 +++--- Source/cmVariableWatchCommand.cxx | 2 +- Source/cmWhileCommand.cxx | 2 +- 14 files changed, 53 insertions(+), 45 deletions(-) diff --git a/Source/CTest/cmCTestHandlerCommand.cxx b/Source/CTest/cmCTestHandlerCommand.cxx index adf9553..2b73d40 100644 --- a/Source/CTest/cmCTestHandlerCommand.cxx +++ b/Source/CTest/cmCTestHandlerCommand.cxx @@ -4,6 +4,7 @@ #include "cmCTest.h" #include "cmCTestGenericHandler.h" +#include "cmExecutionStatus.h" #include "cmMakefile.h" #include "cmMessageType.h" #include "cmSystemTools.h" @@ -13,8 +14,6 @@ #include #include -class cmExecutionStatus; - cmCTestHandlerCommand::cmCTestHandlerCommand() { const size_t INIT_SIZE = 100; @@ -86,7 +85,7 @@ private: } bool cmCTestHandlerCommand::InitialPass(std::vector const& args, - cmExecutionStatus& /*unused*/) + cmExecutionStatus& status) { // save error state and restore it if needed SaveRestoreErrorState errorState; @@ -126,7 +125,7 @@ bool cmCTestHandlerCommand::InitialPass(std::vector const& args, if (capureCMakeError) { this->Makefile->AddDefinition(this->Values[ct_CAPTURE_CMAKE_ERROR], "-1"); - std::string const err = this->GetName() + " " + this->GetError(); + std::string const err = this->GetName() + " " + status.GetError(); if (!cmSystemTools::FindLastString(err.c_str(), "unknown error.")) { cmCTestLog(this->CTest, ERROR_MESSAGE, err << " error from command\n"); } @@ -195,8 +194,8 @@ bool cmCTestHandlerCommand::InitialPass(std::vector const& args, if (capureCMakeError) { this->Makefile->AddDefinition(this->Values[ct_CAPTURE_CMAKE_ERROR], "-1"); - const char* err = this->GetError(); - if (err && !cmSystemTools::FindLastString(err, "unknown error.")) { + std::string const& err = status.GetError(); + if (!cmSystemTools::FindLastString(err.c_str(), "unknown error.")) { cmCTestLog(this->CTest, ERROR_MESSAGE, err << " error from command\n"); } return true; @@ -220,7 +219,7 @@ bool cmCTestHandlerCommand::InitialPass(std::vector const& args, this->Makefile->AddDefinition(this->Values[ct_CAPTURE_CMAKE_ERROR], "-1"); cmCTestLog(this->CTest, ERROR_MESSAGE, - this->GetName() << " " << this->GetError() << "\n"); + this->GetName() << " " << status.GetError() << "\n"); // return success because failure is recorded in CAPTURE_CMAKE_ERROR return true; } @@ -240,10 +239,10 @@ bool cmCTestHandlerCommand::InitialPass(std::vector const& args, const char* returnString = "0"; if (cmSystemTools::GetErrorOccuredFlag()) { returnString = "-1"; - const char* err = this->GetError(); + std::string const& err = status.GetError(); // print out the error if it is not "unknown error" which means // there was no message - if (err && !cmSystemTools::FindLastString(err, "unknown error.")) { + if (!cmSystemTools::FindLastString(err.c_str(), "unknown error.")) { cmCTestLog(this->CTest, ERROR_MESSAGE, err); } } diff --git a/Source/cmCPluginAPI.cxx b/Source/cmCPluginAPI.cxx index 255a8e6..8c2d987 100644 --- a/Source/cmCPluginAPI.cxx +++ b/Source/cmCPluginAPI.cxx @@ -421,7 +421,7 @@ int CCONV cmExecuteCommand(void* arg, const char* name, int numArgs, // Assume all arguments are quoted. lff.Arguments.emplace_back(args[i], cmListFileArgument::Quoted, 0); } - cmExecutionStatus status; + cmExecutionStatus status(*mf); return mf->ExecuteCommand(lff, status); } diff --git a/Source/cmCommand.cxx b/Source/cmCommand.cxx index d349c91..99bdd1e 100644 --- a/Source/cmCommand.cxx +++ b/Source/cmCommand.cxx @@ -2,11 +2,17 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmCommand.h" +#include "cmExecutionStatus.h" #include "cmMakefile.h" -class cmExecutionStatus; struct cmListFileArgument; +void cmCommand::SetExecutionStatus(cmExecutionStatus* status) +{ + this->Status = status; + this->Makefile = &status->GetMakefile(); +} + bool cmCommand::InvokeInitialPass(const std::vector& args, cmExecutionStatus& status) { @@ -19,15 +25,7 @@ bool cmCommand::InvokeInitialPass(const std::vector& args, return this->InitialPass(expandedArguments, status); } -const char* cmCommand::GetError() -{ - if (this->Error.empty()) { - return "unknown error."; - } - return this->Error.c_str(); -} - void cmCommand::SetError(const std::string& e) { - this->Error = e; + this->Status->SetError(e); } diff --git a/Source/cmCommand.h b/Source/cmCommand.h index b210f27..6d3a5fa 100644 --- a/Source/cmCommand.h +++ b/Source/cmCommand.h @@ -42,9 +42,11 @@ public: /** * Specify the makefile. */ - void SetMakefile(cmMakefile* m) { this->Makefile = m; } cmMakefile* GetMakefile() { return this->Makefile; } + void SetExecutionStatus(cmExecutionStatus* s); + cmExecutionStatus* GetExecutionStatus() { return this->Status; }; + /** * This is called by the cmMakefile when the command is first * encountered in the CMakeLists.txt file. It expands the command's @@ -66,11 +68,6 @@ public: virtual std::unique_ptr Clone() = 0; /** - * Return the last error string. - */ - const char* GetError(); - - /** * Set the error message */ void SetError(const std::string& e); @@ -79,7 +76,7 @@ protected: cmMakefile* Makefile = nullptr; private: - std::string Error; + cmExecutionStatus* Status = nullptr; }; #endif diff --git a/Source/cmDisallowedCommand.cxx b/Source/cmDisallowedCommand.cxx index 418d98c..aa1f90b 100644 --- a/Source/cmDisallowedCommand.cxx +++ b/Source/cmDisallowedCommand.cxx @@ -24,8 +24,6 @@ bool cmDisallowedCommand::InitialPass(std::vector const& args, return true; } - this->Command->SetMakefile(this->GetMakefile()); - bool const ret = this->Command->InitialPass(args, status); - this->SetError(this->Command->GetError()); - return ret; + this->Command->SetExecutionStatus(this->GetExecutionStatus()); + return this->Command->InitialPass(args, status); } diff --git a/Source/cmExecutionStatus.h b/Source/cmExecutionStatus.h index 56199dd..bcacc2f 100644 --- a/Source/cmExecutionStatus.h +++ b/Source/cmExecutionStatus.h @@ -3,6 +3,11 @@ #ifndef cmExecutionStatus_h #define cmExecutionStatus_h +#include // IWYU pragma: keep +#include + +class cmMakefile; + /** \class cmExecutionStatus * \brief Superclass for all command status classes * @@ -11,14 +16,26 @@ class cmExecutionStatus { public: + cmExecutionStatus(cmMakefile& makefile) + : Makefile(makefile) + , Error("unknown error.") + { + } + void Clear() { + this->Error = "unknown error."; this->ReturnInvoked = false; this->BreakInvoked = false; this->ContinueInvoked = false; this->NestedError = false; } + cmMakefile& GetMakefile() { return this->Makefile; } + + void SetError(std::string const& e) { this->Error = e; } + std::string const& GetError() const { return this->Error; } + void SetReturnInvoked() { this->ReturnInvoked = true; } bool GetReturnInvoked() const { return this->ReturnInvoked; } @@ -32,6 +49,8 @@ public: bool GetNestedError() const { return this->NestedError; } private: + cmMakefile& Makefile; + std::string Error; bool ReturnInvoked = false; bool BreakInvoked = false; bool ContinueInvoked = false; diff --git a/Source/cmFileCopier.cxx b/Source/cmFileCopier.cxx index 49e8cd5..4f1a158 100644 --- a/Source/cmFileCopier.cxx +++ b/Source/cmFileCopier.cxx @@ -174,11 +174,8 @@ bool cmFileCopier::GetDefaultDirectoryPermissions(mode_t** mode) cmSystemTools::ExpandListArgument(default_dir_install_permissions, items); for (const auto& arg : items) { if (!this->CheckPermissions(arg, **mode)) { - std::ostringstream e; - e << this->FileCommand->GetError() - << " Set with CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS " - "variable."; - this->FileCommand->SetError(e.str()); + this->FileCommand->SetError( + " Set with CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS variable."); return false; } } diff --git a/Source/cmForEachCommand.cxx b/Source/cmForEachCommand.cxx index a30ebe1..e3918b5 100644 --- a/Source/cmForEachCommand.cxx +++ b/Source/cmForEachCommand.cxx @@ -55,7 +55,7 @@ bool cmForEachFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff, // set the variable to the loop value mf.AddDefinition(this->Args[0], arg.c_str()); // Invoke all the functions that were collected in the block. - cmExecutionStatus status; + cmExecutionStatus status(mf); for (cmListFileFunction const& func : this->Functions) { status.Clear(); mf.ExecuteCommand(func, status); diff --git a/Source/cmFunctionCommand.cxx b/Source/cmFunctionCommand.cxx index 6d06531..4041d62 100644 --- a/Source/cmFunctionCommand.cxx +++ b/Source/cmFunctionCommand.cxx @@ -103,7 +103,7 @@ bool cmFunctionHelperCommand::InvokeInitialPass( // Invoke all the functions that were collected in the block. // for each function for (cmListFileFunction const& func : this->Functions) { - cmExecutionStatus status; + cmExecutionStatus status(*this->GetMakefile()); if (!this->Makefile->ExecuteCommand(func, status) || status.GetNestedError()) { // The error message should have already included the call stack diff --git a/Source/cmIfCommand.cxx b/Source/cmIfCommand.cxx index 625dd45..4edea17 100644 --- a/Source/cmIfCommand.cxx +++ b/Source/cmIfCommand.cxx @@ -47,7 +47,7 @@ bool cmIfFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff, } // execute the functions for the true parts of the if statement - cmExecutionStatus status; + cmExecutionStatus status(mf); int scopeDepth = 0; for (cmListFileFunction const& func : this->Functions) { // keep track of scope depth diff --git a/Source/cmMacroCommand.cxx b/Source/cmMacroCommand.cxx index 6e65c6b..11f4bf8 100644 --- a/Source/cmMacroCommand.cxx +++ b/Source/cmMacroCommand.cxx @@ -132,7 +132,7 @@ bool cmMacroHelperCommand::InvokeInitialPass( arg.Line = k.Line; newLFF.Arguments.push_back(std::move(arg)); } - cmExecutionStatus status; + cmExecutionStatus status(*this->GetMakefile()); if (!this->Makefile->ExecuteCommand(newLFF, status) || status.GetNestedError()) { // The error message should have already included the call stack diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 1723c5a..cb7e94b 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -392,7 +392,7 @@ bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff, this->GetState()->GetCommandByExactName(lff.Name.Lower)) { // Clone the prototype. std::unique_ptr pcmd(proto->Clone()); - pcmd->SetMakefile(this); + pcmd->SetExecutionStatus(&status); // Decide whether to invoke the command. if (!cmSystemTools::GetFatalErrorOccured()) { @@ -407,7 +407,7 @@ bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff, if (!hadNestedError) { // The command invocation requested that we report an error. std::string const error = - std::string(lff.Name.Original) + " " + pcmd->GetError(); + std::string(lff.Name.Original) + " " + status.GetError(); this->IssueMessage(MessageType::FATAL_ERROR, error); } result = false; @@ -657,7 +657,7 @@ void cmMakefile::ReadListFile(cmListFile const& listFile, // Run the parsed commands. const size_t numberFunctions = listFile.Functions.size(); for (size_t i = 0; i < numberFunctions; ++i) { - cmExecutionStatus status; + cmExecutionStatus status(*this); this->ExecuteCommand(listFile.Functions[i], status); if (cmSystemTools::GetFatalErrorOccured()) { break; diff --git a/Source/cmVariableWatchCommand.cxx b/Source/cmVariableWatchCommand.cxx index afc0b76..83a774d 100644 --- a/Source/cmVariableWatchCommand.cxx +++ b/Source/cmVariableWatchCommand.cxx @@ -55,7 +55,7 @@ static void cmVariableWatchCommandVariableAccessed(const std::string& variable, newLFF.Arguments.emplace_back(stack, cmListFileArgument::Quoted, 9999); newLFF.Name = data->Command; newLFF.Line = 9999; - cmExecutionStatus status; + cmExecutionStatus status(*makefile); if (!makefile->ExecuteCommand(newLFF, status)) { std::ostringstream error; error << "Error in cmake code at\nUnknown:0:\n" diff --git a/Source/cmWhileCommand.cxx b/Source/cmWhileCommand.cxx index a902964..434c298 100644 --- a/Source/cmWhileCommand.cxx +++ b/Source/cmWhileCommand.cxx @@ -82,7 +82,7 @@ bool cmWhileFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff, // Invoke all the functions that were collected in the block. for (cmListFileFunction const& fn : this->Functions) { - cmExecutionStatus status; + cmExecutionStatus status(mf); mf.ExecuteCommand(fn, status); if (status.GetReturnInvoked()) { inStatus.SetReturnInvoked(); -- cgit v0.12 From 015001aaf138119f4825e3c84c0845c5127f9088 Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Sun, 7 Apr 2019 20:18:32 +0200 Subject: cmState: Hold commands by value --- Source/cmCommand.cxx | 28 ++++++++++++++++++++++++++++ Source/cmCommand.h | 18 ++++++++++++++++++ Source/cmConditionEvaluator.cxx | 4 ++-- Source/cmMakefile.cxx | 9 ++------- Source/cmState.cxx | 36 ++++++++++-------------------------- Source/cmState.h | 13 +++++++++---- 6 files changed, 69 insertions(+), 39 deletions(-) diff --git a/Source/cmCommand.cxx b/Source/cmCommand.cxx index 99bdd1e..0c2734e 100644 --- a/Source/cmCommand.cxx +++ b/Source/cmCommand.cxx @@ -2,6 +2,8 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmCommand.h" +#include + #include "cmExecutionStatus.h" #include "cmMakefile.h" @@ -29,3 +31,29 @@ void cmCommand::SetError(const std::string& e) { this->Status->SetError(e); } + +cmLegacyCommandWrapper::cmLegacyCommandWrapper(std::unique_ptr cmd) + : Command(std::move(cmd)) +{ +} + +cmLegacyCommandWrapper::cmLegacyCommandWrapper( + cmLegacyCommandWrapper const& other) + : Command(other.Command->Clone()) +{ +} + +cmLegacyCommandWrapper& cmLegacyCommandWrapper::operator=( + cmLegacyCommandWrapper const& other) +{ + this->Command = other.Command->Clone(); + return *this; +} + +bool cmLegacyCommandWrapper::operator()( + std::vector const& args, cmExecutionStatus& status) const +{ + auto cmd = this->Command->Clone(); + cmd->SetExecutionStatus(&status); + return cmd->InvokeInitialPass(args, status); +} diff --git a/Source/cmCommand.h b/Source/cmCommand.h index 6d3a5fa..9e978b3 100644 --- a/Source/cmCommand.h +++ b/Source/cmCommand.h @@ -79,4 +79,22 @@ private: cmExecutionStatus* Status = nullptr; }; +class cmLegacyCommandWrapper +{ +public: + explicit cmLegacyCommandWrapper(std::unique_ptr cmd); + + cmLegacyCommandWrapper(cmLegacyCommandWrapper const& other); + cmLegacyCommandWrapper& operator=(cmLegacyCommandWrapper const& other); + + cmLegacyCommandWrapper(cmLegacyCommandWrapper&&) = default; + cmLegacyCommandWrapper& operator=(cmLegacyCommandWrapper&&) = default; + + bool operator()(std::vector const& args, + cmExecutionStatus& status) const; + +private: + std::unique_ptr Command; +}; + #endif diff --git a/Source/cmConditionEvaluator.cxx b/Source/cmConditionEvaluator.cxx index e7e91c1..2907f4a 100644 --- a/Source/cmConditionEvaluator.cxx +++ b/Source/cmConditionEvaluator.cxx @@ -4,6 +4,7 @@ #include "cmsys/RegularExpression.hxx" #include +#include #include #include #include @@ -17,7 +18,6 @@ #include "cmSystemTools.h" #include "cmake.h" -class cmCommand; class cmTest; static std::string const keyAND = "AND"; @@ -452,7 +452,7 @@ bool cmConditionEvaluator::HandleLevel1(cmArgumentList& newArgs, std::string&, } // does a command exist if (this->IsKeyword(keyCOMMAND, *arg) && argP1 != newArgs.end()) { - cmCommand* command = + cmState::Command command = this->Makefile.GetState()->GetCommand(argP1->c_str()); this->HandlePredicate(command != nullptr, reducible, arg, newArgs, argP1, argP2); diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index cb7e94b..3177adc 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -16,7 +16,6 @@ #include #include "cmAlgorithms.h" -#include "cmCommand.h" #include "cmCommandArgumentParserHelper.h" #include "cmCustomCommand.h" #include "cmCustomCommandLines.h" @@ -388,12 +387,8 @@ bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff, } // Lookup the command prototype. - if (cmCommand* proto = + if (cmState::Command command = this->GetState()->GetCommandByExactName(lff.Name.Lower)) { - // Clone the prototype. - std::unique_ptr pcmd(proto->Clone()); - pcmd->SetExecutionStatus(&status); - // Decide whether to invoke the command. if (!cmSystemTools::GetFatalErrorOccured()) { // if trace is enabled, print out invoke information @@ -401,7 +396,7 @@ bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff, this->PrintCommandTrace(lff); } // Try invoking the command. - bool invokeSucceeded = pcmd->InvokeInitialPass(lff.Arguments, status); + bool invokeSucceeded = command(lff.Arguments, status); bool hadNestedError = status.GetNestedError(); if (!invokeSucceeded || hadNestedError) { if (!hadNestedError) { diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 9748cf5..57ae324 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -6,7 +6,6 @@ #include #include #include -#include #include #include "cm_memory.hxx" @@ -423,9 +422,8 @@ void cmState::AddBuiltinCommand(std::string const& name, { assert(name == cmSystemTools::LowerCase(name)); assert(this->BuiltinCommands.find(name) == this->BuiltinCommands.end()); - this->BuiltinCommands.insert( - std::map>::value_type( - name, std::move(command))); + this->BuiltinCommands.emplace(name, + cmLegacyCommandWrapper(std::move(command))); } void cmState::AddDisallowedCommand(std::string const& name, @@ -450,39 +448,27 @@ void cmState::AddScriptedCommand(std::string const& name, std::string sName = cmSystemTools::LowerCase(name); // if the command already exists, give a new name to the old command. - if (cmCommand* oldCmd = this->GetCommand(sName)) { - std::string const newName = "_" + sName; - auto pos = this->ScriptedCommands.find(newName); - if (pos != this->ScriptedCommands.end()) { - this->ScriptedCommands.erase(pos); - } - this->ScriptedCommands.insert(std::make_pair(newName, oldCmd->Clone())); + if (Command oldCmd = this->GetCommand(sName)) { + this->ScriptedCommands["_" + sName] = oldCmd; } - // if the command already exists, free the old one - auto pos = this->ScriptedCommands.find(sName); - if (pos != this->ScriptedCommands.end()) { - this->ScriptedCommands.erase(pos); - } - this->ScriptedCommands.insert( - std::map>::value_type( - sName, std::move(command))); + this->ScriptedCommands[sName] = cmLegacyCommandWrapper(std::move(command)); } -cmCommand* cmState::GetCommand(std::string const& name) const +cmState::Command cmState::GetCommand(std::string const& name) const { return GetCommandByExactName(cmSystemTools::LowerCase(name)); } -cmCommand* cmState::GetCommandByExactName(std::string const& name) const +cmState::Command cmState::GetCommandByExactName(std::string const& name) const { auto pos = this->ScriptedCommands.find(name); if (pos != this->ScriptedCommands.end()) { - return pos->second.get(); + return pos->second; } pos = this->BuiltinCommands.find(name); if (pos != this->BuiltinCommands.end()) { - return pos->second.get(); + return pos->second; } return nullptr; } @@ -507,9 +493,7 @@ std::vector cmState::GetCommandNames() const void cmState::RemoveBuiltinCommand(std::string const& name) { assert(name == cmSystemTools::LowerCase(name)); - auto i = this->BuiltinCommands.find(name); - assert(i != this->BuiltinCommands.end()); - this->BuiltinCommands.erase(i); + this->BuiltinCommands.erase(name); } void cmState::RemoveUserDefinedCommands() diff --git a/Source/cmState.h b/Source/cmState.h index 7e88030..ed70bd6 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -5,6 +5,7 @@ #include "cmConfigure.h" // IWYU pragma: keep +#include #include #include #include @@ -27,6 +28,7 @@ class cmGlobVerificationManager; class cmPropertyDefinition; class cmStateSnapshot; class cmMessenger; +class cmExecutionStatus; class cmState { @@ -141,10 +143,13 @@ public: bool GetIsGeneratorMultiConfig() const; void SetIsGeneratorMultiConfig(bool b); + using Command = std::function const&, + cmExecutionStatus&)>; + // Returns a command from its name, case insensitive, or nullptr - cmCommand* GetCommand(std::string const& name) const; + Command GetCommand(std::string const& name) const; // Returns a command from its name, or nullptr - cmCommand* GetCommandByExactName(std::string const& name) const; + Command GetCommandByExactName(std::string const& name) const; void AddBuiltinCommand(std::string const& name, std::unique_ptr command); @@ -212,8 +217,8 @@ private: std::map PropertyDefinitions; std::vector EnabledLanguages; - std::map> BuiltinCommands; - std::map> ScriptedCommands; + std::map BuiltinCommands; + std::map ScriptedCommands; cmPropertyMap GlobalProperties; std::unique_ptr CacheManager; std::unique_ptr GlobVerificationManager; -- cgit v0.12 From 0101ace1319b5c97472649ec809b67299e2a46ce Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Sun, 7 Apr 2019 20:40:59 +0200 Subject: cmUnexpectedCommand: Replace with lambda expression --- Source/CMakeLists.txt | 2 -- Source/cmState.cxx | 26 +++++++++++++++++++++----- Source/cmState.h | 1 + Source/cmUnexpectedCommand.cxx | 22 ---------------------- Source/cmUnexpectedCommand.h | 40 ---------------------------------------- bootstrap | 1 - 6 files changed, 22 insertions(+), 70 deletions(-) delete mode 100644 Source/cmUnexpectedCommand.cxx delete mode 100644 Source/cmUnexpectedCommand.h diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 0316532..8117916 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -662,8 +662,6 @@ set(SRCS cmTryCompileCommand.h cmTryRunCommand.cxx cmTryRunCommand.h - cmUnexpectedCommand.cxx - cmUnexpectedCommand.h cmUnsetCommand.cxx cmUnsetCommand.h cmUseMangledMesaCommand.cxx diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 57ae324..82b0a52 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -5,6 +5,7 @@ #include "cmsys/RegularExpression.hxx" #include #include +#include #include #include @@ -15,12 +16,13 @@ #include "cmCommand.h" #include "cmDefinitions.h" #include "cmDisallowedCommand.h" +#include "cmExecutionStatus.h" #include "cmGlobVerificationManager.h" #include "cmListFileCache.h" +#include "cmMakefile.h" #include "cmStatePrivate.h" #include "cmStateSnapshot.h" #include "cmSystemTools.h" -#include "cmUnexpectedCommand.h" #include "cmake.h" cmState::cmState() @@ -420,10 +422,14 @@ void cmState::SetIsGeneratorMultiConfig(bool b) void cmState::AddBuiltinCommand(std::string const& name, std::unique_ptr command) { + this->AddBuiltinCommand(name, cmLegacyCommandWrapper(std::move(command))); +} + +void cmState::AddBuiltinCommand(std::string const& name, Command command) +{ assert(name == cmSystemTools::LowerCase(name)); assert(this->BuiltinCommands.find(name) == this->BuiltinCommands.end()); - this->BuiltinCommands.emplace(name, - cmLegacyCommandWrapper(std::move(command))); + this->BuiltinCommands.emplace(name, std::move(command)); } void cmState::AddDisallowedCommand(std::string const& name, @@ -438,8 +444,18 @@ void cmState::AddDisallowedCommand(std::string const& name, void cmState::AddUnexpectedCommand(std::string const& name, const char* error) { - this->AddBuiltinCommand(name, - cm::make_unique(name, error)); + this->AddBuiltinCommand( + name, + [name, error](std::vector const&, + cmExecutionStatus& status) -> bool { + const char* versionValue = + status.GetMakefile().GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION"); + if (name == "endif" && (!versionValue || atof(versionValue) <= 1.4)) { + return true; + } + status.SetError(error); + return false; + }); } void cmState::AddScriptedCommand(std::string const& name, diff --git a/Source/cmState.h b/Source/cmState.h index ed70bd6..a6e9386 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -153,6 +153,7 @@ public: void AddBuiltinCommand(std::string const& name, std::unique_ptr command); + void AddBuiltinCommand(std::string const& name, Command command); void AddDisallowedCommand(std::string const& name, std::unique_ptr command, cmPolicies::PolicyID policy, const char* message); diff --git a/Source/cmUnexpectedCommand.cxx b/Source/cmUnexpectedCommand.cxx deleted file mode 100644 index a8de9e6..0000000 --- a/Source/cmUnexpectedCommand.cxx +++ /dev/null @@ -1,22 +0,0 @@ -/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying - file Copyright.txt or https://cmake.org/licensing for details. */ -#include "cmUnexpectedCommand.h" - -#include - -#include "cmMakefile.h" - -class cmExecutionStatus; - -bool cmUnexpectedCommand::InitialPass(std::vector const&, - cmExecutionStatus&) -{ - const char* versionValue = - this->Makefile->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION"); - if (this->Name == "endif" && (!versionValue || atof(versionValue) <= 1.4)) { - return true; - } - - this->SetError(this->Error); - return false; -} diff --git a/Source/cmUnexpectedCommand.h b/Source/cmUnexpectedCommand.h deleted file mode 100644 index 6e4cee5..0000000 --- a/Source/cmUnexpectedCommand.h +++ /dev/null @@ -1,40 +0,0 @@ -/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying - file Copyright.txt or https://cmake.org/licensing for details. */ -#ifndef cmUnexpectedCommand_h -#define cmUnexpectedCommand_h - -#include "cmConfigure.h" // IWYU pragma: keep - -#include -#include -#include - -#include "cm_memory.hxx" - -#include "cmCommand.h" - -class cmExecutionStatus; - -class cmUnexpectedCommand : public cmCommand -{ -public: - cmUnexpectedCommand(std::string name, const char* error) - : Name(std::move(name)) - , Error(error) - { - } - - std::unique_ptr Clone() override - { - return cm::make_unique(this->Name, this->Error); - } - - bool InitialPass(std::vector const& args, - cmExecutionStatus& status) override; - -private: - std::string Name; - const char* Error; -}; - -#endif diff --git a/bootstrap b/bootstrap index c2194fe..bb5229b 100755 --- a/bootstrap +++ b/bootstrap @@ -440,7 +440,6 @@ CMAKE_CXX_SOURCES="\ cmTimestamp \ cmTryCompileCommand \ cmTryRunCommand \ - cmUnexpectedCommand \ cmUnsetCommand \ cmUVHandlePtr \ cmUVProcessChain \ -- cgit v0.12 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 From 28f2d12a055e025aa0ddeb9842f204f29181eaff Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Sun, 7 Apr 2019 21:46:46 +0200 Subject: cmCommand: De-virtualize function InvokeInitialPass --- Source/cmCommand.h | 4 ++-- Source/cmCommands.cxx | 4 ++-- Source/cmIfCommand.cxx | 17 +++++++++-------- Source/cmIfCommand.h | 39 ++------------------------------------- Source/cmWhileCommand.cxx | 12 +++++++----- Source/cmWhileCommand.h | 34 ++-------------------------------- 6 files changed, 24 insertions(+), 86 deletions(-) diff --git a/Source/cmCommand.h b/Source/cmCommand.h index 9e978b3..bcb178d 100644 --- a/Source/cmCommand.h +++ b/Source/cmCommand.h @@ -52,8 +52,8 @@ public: * encountered in the CMakeLists.txt file. It expands the command's * arguments and then invokes the InitialPass. */ - virtual bool InvokeInitialPass(const std::vector& args, - cmExecutionStatus& status); + bool InvokeInitialPass(const std::vector& args, + cmExecutionStatus& status); /** * This is called when the command is first encountered in diff --git a/Source/cmCommands.cxx b/Source/cmCommands.cxx index 96c7105..9ae2f71 100644 --- a/Source/cmCommands.cxx +++ b/Source/cmCommands.cxx @@ -147,7 +147,7 @@ void GetScriptingCommands(cmState* state) cm::make_unique()); state->AddBuiltinCommand("get_property", cm::make_unique()); - state->AddBuiltinCommand("if", cm::make_unique()); + state->AddBuiltinCommand("if", cmIfCommand); state->AddBuiltinCommand("include", cm::make_unique()); state->AddBuiltinCommand("include_guard", cm::make_unique()); @@ -173,7 +173,7 @@ void GetScriptingCommands(cmState* state) state->AddBuiltinCommand("site_name", cm::make_unique()); state->AddBuiltinCommand("string", cm::make_unique()); state->AddBuiltinCommand("unset", cm::make_unique()); - state->AddBuiltinCommand("while", cm::make_unique()); + state->AddBuiltinCommand("while", cmWhileCommand); state->AddUnexpectedCommand( "else", diff --git a/Source/cmIfCommand.cxx b/Source/cmIfCommand.cxx index 4edea17..385022c 100644 --- a/Source/cmIfCommand.cxx +++ b/Source/cmIfCommand.cxx @@ -13,6 +13,7 @@ #include "cmSystemTools.h" #include "cmake.h" +#include #include static std::string cmIfCommandError( @@ -176,19 +177,19 @@ bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff, } //========================================================================= -bool cmIfCommand::InvokeInitialPass( - const std::vector& args, cmExecutionStatus&) +bool cmIfCommand(std::vector const& args, + cmExecutionStatus& inStatus) { + cmMakefile& makefile = inStatus.GetMakefile(); std::string errorString; std::vector expandedArguments; - this->Makefile->ExpandArguments(args, expandedArguments); + makefile.ExpandArguments(args, expandedArguments); MessageType status; cmConditionEvaluator conditionEvaluator( - *(this->Makefile), this->Makefile->GetExecutionContext(), - this->Makefile->GetBacktrace()); + makefile, makefile.GetExecutionContext(), makefile.GetBacktrace()); bool isTrue = conditionEvaluator.IsTrue(expandedArguments, errorString, status); @@ -197,11 +198,11 @@ bool cmIfCommand::InvokeInitialPass( std::string err = "if " + cmIfCommandError(expandedArguments); err += errorString; if (status == MessageType::FATAL_ERROR) { - this->Makefile->IssueMessage(MessageType::FATAL_ERROR, err); + makefile.IssueMessage(MessageType::FATAL_ERROR, err); cmSystemTools::SetFatalErrorOccured(); return true; } - this->Makefile->IssueMessage(status, err); + makefile.IssueMessage(status, err); } { @@ -213,7 +214,7 @@ bool cmIfCommand::InvokeInitialPass( fb->HasRun = true; } fb->Args = args; - this->Makefile->AddFunctionBlocker(std::move(fb)); + makefile.AddFunctionBlocker(std::move(fb)); } return true; diff --git a/Source/cmIfCommand.h b/Source/cmIfCommand.h index 4a67760..775e609 100644 --- a/Source/cmIfCommand.h +++ b/Source/cmIfCommand.h @@ -5,17 +5,12 @@ #include "cmConfigure.h" // IWYU pragma: keep -#include #include -#include "cm_memory.hxx" - -#include "cmCommand.h" #include "cmFunctionBlocker.h" #include "cmListFileCache.h" class cmExecutionStatus; -class cmExpandedCommandArgument; class cmMakefile; class cmIfFunctionBlocker : public cmFunctionBlocker @@ -34,37 +29,7 @@ public: }; /// Starts an if block -class cmIfCommand : public cmCommand -{ -public: - /** - * This is a virtual constructor for the command. - */ - std::unique_ptr Clone() override - { - return cm::make_unique(); - } - - /** - * This overrides the default InvokeInitialPass implementation. - * It records the arguments before expansion. - */ - bool InvokeInitialPass(const std::vector& args, - cmExecutionStatus&) override; - - /** - * This is called when the command is first encountered in - * the CMakeLists.txt file. - */ - bool InitialPass(std::vector const&, - cmExecutionStatus&) override - { - return false; - } - - // Filter the given variable definition based on policy CMP0054. - static const char* GetDefinitionIfUnquoted( - const cmMakefile* mf, cmExpandedCommandArgument const& argument); -}; +bool cmIfCommand(std::vector const& args, + cmExecutionStatus& status); #endif diff --git a/Source/cmWhileCommand.cxx b/Source/cmWhileCommand.cxx index 434c298..37d1c74 100644 --- a/Source/cmWhileCommand.cxx +++ b/Source/cmWhileCommand.cxx @@ -11,6 +11,7 @@ #include "cmMessageType.h" #include "cmSystemTools.h" +#include #include cmWhileFunctionBlocker::cmWhileFunctionBlocker(cmMakefile* mf) @@ -129,19 +130,20 @@ bool cmWhileFunctionBlocker::ShouldRemove(const cmListFileFunction& lff, return false; } -bool cmWhileCommand::InvokeInitialPass( - const std::vector& args, cmExecutionStatus&) +bool cmWhileCommand(std::vector const& args, + cmExecutionStatus& status) { if (args.empty()) { - this->SetError("called with incorrect number of arguments"); + status.SetError("called with incorrect number of arguments"); return false; } // create a function blocker { - auto fb = cm::make_unique(this->Makefile); + cmMakefile& makefile = status.GetMakefile(); + auto fb = cm::make_unique(&makefile); fb->Args = args; - this->Makefile->AddFunctionBlocker(std::move(fb)); + makefile.AddFunctionBlocker(std::move(fb)); } return true; } diff --git a/Source/cmWhileCommand.h b/Source/cmWhileCommand.h index 857d1c8..2257799 100644 --- a/Source/cmWhileCommand.h +++ b/Source/cmWhileCommand.h @@ -5,12 +5,8 @@ #include "cmConfigure.h" // IWYU pragma: keep -#include #include -#include "cm_memory.hxx" - -#include "cmCommand.h" #include "cmFunctionBlocker.h" #include "cmListFileCache.h" @@ -35,33 +31,7 @@ private: }; /// \brief Starts a while loop -class cmWhileCommand : public cmCommand -{ -public: - /** - * This is a virtual constructor for the command. - */ - std::unique_ptr Clone() override - { - return cm::make_unique(); - } - - /** - * This overrides the default InvokeInitialPass implementation. - * It records the arguments before expansion. - */ - bool InvokeInitialPass(const std::vector& args, - cmExecutionStatus&) override; - - /** - * This is called when the command is first encountered in - * the CMakeLists.txt file. - */ - bool InitialPass(std::vector const&, - cmExecutionStatus&) override - { - return false; - } -}; +bool cmWhileCommand(std::vector const& args, + cmExecutionStatus& status); #endif -- cgit v0.12 From dfaa87f1b3335b2e80f68726fd5e1b3bbd87d667 Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Mon, 8 Apr 2019 09:55:52 +0200 Subject: cmState: Support BuiltinCommands as free functions --- Source/cmCommands.cxx | 5 ++--- Source/cmEnableTestingCommand.cxx | 11 ++++------- Source/cmEnableTestingCommand.h | 26 +++----------------------- Source/cmReturnCommand.cxx | 4 ++-- Source/cmReturnCommand.h | 30 +++--------------------------- Source/cmState.cxx | 17 +++++++++++++++++ Source/cmState.h | 3 +++ 7 files changed, 34 insertions(+), 62 deletions(-) diff --git a/Source/cmCommands.cxx b/Source/cmCommands.cxx index 9ae2f71..f351ff8 100644 --- a/Source/cmCommands.cxx +++ b/Source/cmCommands.cxx @@ -162,7 +162,7 @@ void GetScriptingCommands(cmState* state) state->AddBuiltinCommand("option", cm::make_unique()); state->AddBuiltinCommand("cmake_parse_arguments", cm::make_unique()); - state->AddBuiltinCommand("return", cm::make_unique()); + state->AddBuiltinCommand("return", cmReturnCommand); state->AddBuiltinCommand("separate_arguments", cm::make_unique()); state->AddBuiltinCommand("set", cm::make_unique()); @@ -255,8 +255,7 @@ void GetProjectCommands(cmState* state) cm::make_unique()); state->AddBuiltinCommand("enable_language", cm::make_unique()); - state->AddBuiltinCommand("enable_testing", - cm::make_unique()); + state->AddBuiltinCommand("enable_testing", cmEnableTestingCommand); state->AddBuiltinCommand("get_source_file_property", cm::make_unique()); state->AddBuiltinCommand("get_target_property", diff --git a/Source/cmEnableTestingCommand.cxx b/Source/cmEnableTestingCommand.cxx index 6a64450..89212c8 100644 --- a/Source/cmEnableTestingCommand.cxx +++ b/Source/cmEnableTestingCommand.cxx @@ -2,15 +2,12 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmEnableTestingCommand.h" +#include "cmExecutionStatus.h" #include "cmMakefile.h" -class cmExecutionStatus; - -// we do this in the final pass so that we now the subdirs have all -// been defined -bool cmEnableTestingCommand::InitialPass(std::vector const&, - cmExecutionStatus&) +bool cmEnableTestingCommand(std::vector const&, + cmExecutionStatus& status) { - this->Makefile->AddDefinition("CMAKE_TESTING_ENABLED", "1"); + status.GetMakefile().AddDefinition("CMAKE_TESTING_ENABLED", "1"); return true; } diff --git a/Source/cmEnableTestingCommand.h b/Source/cmEnableTestingCommand.h index fd50ebc..e4593f2 100644 --- a/Source/cmEnableTestingCommand.h +++ b/Source/cmEnableTestingCommand.h @@ -8,13 +8,9 @@ #include #include -#include "cm_memory.hxx" - -#include "cmCommand.h" - class cmExecutionStatus; -/** \class cmEnableTestingCommand +/** * \brief Enable testing for this directory and below. * * Produce the output testfile. This produces a file in the build directory @@ -27,23 +23,7 @@ class cmExecutionStatus; * Note that CTest expects to find this file in the build directory root; * therefore, this command should be in the source directory root too. */ -class cmEnableTestingCommand : public cmCommand -{ -public: - /** - * This is a virtual constructor for the command. - */ - std::unique_ptr Clone() override - { - return cm::make_unique(); - } - - /** - * This is called when the command is first encountered in - * the CMakeLists.txt file. - */ - bool InitialPass(std::vector const&, - cmExecutionStatus&) override; -}; +bool cmEnableTestingCommand(std::vector const&, + cmExecutionStatus&); #endif diff --git a/Source/cmReturnCommand.cxx b/Source/cmReturnCommand.cxx index ceea8b4..5905669 100644 --- a/Source/cmReturnCommand.cxx +++ b/Source/cmReturnCommand.cxx @@ -5,8 +5,8 @@ #include "cmExecutionStatus.h" // cmReturnCommand -bool cmReturnCommand::InitialPass(std::vector const&, - cmExecutionStatus& status) +bool cmReturnCommand(std::vector const&, + cmExecutionStatus& status) { status.SetReturnInvoked(); return true; diff --git a/Source/cmReturnCommand.h b/Source/cmReturnCommand.h index e9264d2..2404a36 100644 --- a/Source/cmReturnCommand.h +++ b/Source/cmReturnCommand.h @@ -8,34 +8,10 @@ #include #include -#include "cm_memory.hxx" - -#include "cmCommand.h" - class cmExecutionStatus; -/** \class cmReturnCommand - * \brief Return from a directory or function - * - * cmReturnCommand returns from a directory or function - */ -class cmReturnCommand : public cmCommand -{ -public: - /** - * This is a virtual constructor for the command. - */ - std::unique_ptr Clone() override - { - return cm::make_unique(); - } - - /** - * This is called when the command is first encountered in - * the CMakeLists.txt file. - */ - bool InitialPass(std::vector const& args, - cmExecutionStatus& status) override; -}; +/// Return from a directory or function +bool cmReturnCommand(std::vector const& args, + cmExecutionStatus& status); #endif diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 441f11c..0b12a65 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -432,6 +432,23 @@ void cmState::AddBuiltinCommand(std::string const& name, Command command) this->BuiltinCommands.emplace(name, std::move(command)); } +void cmState::AddBuiltinCommand(std::string const& name, + BuiltinCommand command) +{ + this->AddBuiltinCommand( + name, + [command](const std::vector& args, + cmExecutionStatus& status) -> bool { + std::vector expandedArguments; + if (!status.GetMakefile().ExpandArguments(args, expandedArguments)) { + // There was an error expanding arguments. It was already + // reported, so we can skip this command without error. + return true; + } + return command(expandedArguments, status); + }); +} + void cmState::AddDisallowedCommand(std::string const& name, std::unique_ptr command, cmPolicies::PolicyID policy, diff --git a/Source/cmState.h b/Source/cmState.h index 07c1c9b..8847f3b 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -145,6 +145,8 @@ public: using Command = std::function const&, cmExecutionStatus&)>; + using BuiltinCommand = bool (*)(std::vector const&, + cmExecutionStatus&); // Returns a command from its name, case insensitive, or nullptr Command GetCommand(std::string const& name) const; @@ -154,6 +156,7 @@ public: void AddBuiltinCommand(std::string const& name, std::unique_ptr command); void AddBuiltinCommand(std::string const& name, Command command); + void AddBuiltinCommand(std::string const& name, BuiltinCommand command); void AddDisallowedCommand(std::string const& name, std::unique_ptr command, cmPolicies::PolicyID policy, const char* message); -- cgit v0.12