From 86bf1eef7587b837057f098927fb6e7e10149c66 Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Tue, 13 Aug 2019 13:42:59 +0200 Subject: cmState: Support free function disallowed commands --- Source/cmState.cxx | 47 +++++++++++++++++++++++++++++++++++++++++------ Source/cmState.h | 2 ++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 1ea72e1..4f694e2 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -432,6 +432,20 @@ void cmState::AddBuiltinCommand(std::string const& name, Command command) this->BuiltinCommands.emplace(name, std::move(command)); } +static bool InvokeBuiltinCommand(cmState::BuiltinCommand command, + std::vector const& args, + cmExecutionStatus& status) +{ + cmMakefile& mf = status.GetMakefile(); + std::vector expandedArguments; + if (!mf.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::AddBuiltinCommand(std::string const& name, BuiltinCommand command) { @@ -439,13 +453,34 @@ void cmState::AddBuiltinCommand(std::string const& name, 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 InvokeBuiltinCommand(command, args, status); + }); +} + +void cmState::AddDisallowedCommand(std::string const& name, + BuiltinCommand command, + cmPolicies::PolicyID policy, + const char* message) +{ + this->AddBuiltinCommand( + name, + [command, policy, message](const std::vector& args, + cmExecutionStatus& status) -> bool { + cmMakefile& mf = status.GetMakefile(); + switch (mf.GetPolicyStatus(policy)) { + case cmPolicies::WARN: + mf.IssueMessage(MessageType::AUTHOR_WARNING, + cmPolicies::GetPolicyWarning(policy)); + break; + case cmPolicies::OLD: + break; + case cmPolicies::REQUIRED_IF_USED: + case cmPolicies::REQUIRED_ALWAYS: + case cmPolicies::NEW: + mf.IssueMessage(MessageType::FATAL_ERROR, message); + return true; } - return command(expandedArguments, status); + return InvokeBuiltinCommand(command, args, status); }); } diff --git a/Source/cmState.h b/Source/cmState.h index 8847f3b..937ab61 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -157,6 +157,8 @@ public: 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, BuiltinCommand command, + cmPolicies::PolicyID policy, const char* message); void AddDisallowedCommand(std::string const& name, std::unique_ptr command, cmPolicies::PolicyID policy, const char* message); -- cgit v0.12 From c55fb044a9cf20a449f9d5ebc5d88c6de279786d Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Tue, 13 Aug 2019 13:43:47 +0200 Subject: cmBuildNameCommand: Implement as free function --- Source/cmBuildNameCommand.cxx | 25 ++++++++++++------------- Source/cmBuildNameCommand.h | 16 ++-------------- Source/cmCommands.cxx | 2 +- Source/cmState.cxx | 1 + 4 files changed, 16 insertions(+), 28 deletions(-) diff --git a/Source/cmBuildNameCommand.cxx b/Source/cmBuildNameCommand.cxx index ddff686..df94f1d 100644 --- a/Source/cmBuildNameCommand.cxx +++ b/Source/cmBuildNameCommand.cxx @@ -5,21 +5,20 @@ #include "cmsys/RegularExpression.hxx" #include +#include "cmExecutionStatus.h" #include "cmMakefile.h" #include "cmStateTypes.h" #include "cmSystemTools.h" -class cmExecutionStatus; - -// cmBuildNameCommand -bool cmBuildNameCommand::InitialPass(std::vector const& args, - cmExecutionStatus&) +bool cmBuildNameCommand(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; } - const char* cacheValue = this->Makefile->GetDefinition(args[0]); + cmMakefile& mf = status.GetMakefile(); + const char* cacheValue = mf.GetDefinition(args[0]); if (cacheValue) { // do we need to correct the value? cmsys::RegularExpression reg("[()/]"); @@ -28,14 +27,14 @@ bool cmBuildNameCommand::InitialPass(std::vector const& args, std::replace(cv.begin(), cv.end(), '/', '_'); std::replace(cv.begin(), cv.end(), '(', '_'); std::replace(cv.begin(), cv.end(), ')', '_'); - this->Makefile->AddCacheDefinition(args[0], cv.c_str(), "Name of build.", - cmStateEnums::STRING); + mf.AddCacheDefinition(args[0], cv.c_str(), "Name of build.", + cmStateEnums::STRING); } return true; } std::string buildname = "WinNT"; - if (this->Makefile->GetDefinition("UNIX")) { + if (mf.GetDefinition("UNIX")) { buildname.clear(); cmSystemTools::RunSingleCommand("uname -a", &buildname, &buildname); if (!buildname.empty()) { @@ -47,14 +46,14 @@ bool cmBuildNameCommand::InitialPass(std::vector const& args, } } std::string compiler = "${CMAKE_CXX_COMPILER}"; - this->Makefile->ExpandVariablesInString(compiler); + mf.ExpandVariablesInString(compiler); buildname += "-"; buildname += cmSystemTools::GetFilenameName(compiler); std::replace(buildname.begin(), buildname.end(), '/', '_'); std::replace(buildname.begin(), buildname.end(), '(', '_'); std::replace(buildname.begin(), buildname.end(), ')', '_'); - this->Makefile->AddCacheDefinition(args[0], buildname.c_str(), - "Name of build.", cmStateEnums::STRING); + mf.AddCacheDefinition(args[0], buildname.c_str(), "Name of build.", + cmStateEnums::STRING); return true; } diff --git a/Source/cmBuildNameCommand.h b/Source/cmBuildNameCommand.h index bd2d146..37a7268 100644 --- a/Source/cmBuildNameCommand.h +++ b/Source/cmBuildNameCommand.h @@ -8,21 +8,9 @@ #include #include -#include "cm_memory.hxx" - -#include "cmCommand.h" - class cmExecutionStatus; -class cmBuildNameCommand : public cmCommand -{ -public: - std::unique_ptr Clone() override - { - return cm::make_unique(); - } - bool InitialPass(std::vector const& args, - cmExecutionStatus& status) override; -}; +bool cmBuildNameCommand(std::vector const& args, + cmExecutionStatus& status); #endif diff --git a/Source/cmCommands.cxx b/Source/cmCommands.cxx index a9be445..1989427 100644 --- a/Source/cmCommands.cxx +++ b/Source/cmCommands.cxx @@ -221,7 +221,7 @@ void GetScriptingCommands(cmState* state) cm::make_unique()); state->AddDisallowedCommand( - "build_name", cm::make_unique(), cmPolicies::CMP0036, + "build_name", cmBuildNameCommand, cmPolicies::CMP0036, "The build_name command should not be called; see CMP0036."); state->AddDisallowedCommand( "use_mangled_mesa", cm::make_unique(), diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 4f694e2..8cc960c 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -19,6 +19,7 @@ #include "cmGlobVerificationManager.h" #include "cmListFileCache.h" #include "cmMakefile.h" +#include "cmMessageType.h" #include "cmStatePrivate.h" #include "cmStateSnapshot.h" #include "cmStringAlgorithms.h" -- cgit v0.12