From 31d7aa5e3c586e4818a7debdc0bcdbdfd6c2b7b2 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 19 Jul 2022 14:19:43 -0400 Subject: cmCMakePathCommand: Use cm::optional for keyword argument presence --- Source/cmCMakePathCommand.cxx | 60 +++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/Source/cmCMakePathCommand.cxx b/Source/cmCMakePathCommand.cxx index adcffa9..e604ed1 100644 --- a/Source/cmCMakePathCommand.cxx +++ b/Source/cmCMakePathCommand.cxx @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -43,15 +44,14 @@ public: } template - Result Parse(std::vector const& args, - std::vector* keywordsMissingValue = nullptr, - std::vector* parsedKeywords = nullptr) const + Result Parse( + std::vector const& args, + std::vector* keywordsMissingValue = nullptr) const { this->Inputs.clear(); return this->cmArgumentParser::Parse( - cmMakeRange(args).advance(Advance), &this->Inputs, keywordsMissingValue, - parsedKeywords); + cmMakeRange(args).advance(Advance), &this->Inputs, keywordsMissingValue); } const std::vector& GetInputs() const { return this->Inputs; } @@ -83,20 +83,15 @@ public: Result Parse(std::vector const& args) const { this->KeywordsMissingValue.clear(); - this->ParsedKeywords.clear(); return this->CMakePathArgumentParser::template Parse( - args, &this->KeywordsMissingValue, &this->ParsedKeywords); + args, &this->KeywordsMissingValue); } const std::vector& GetKeywordsMissingValue() const { return this->KeywordsMissingValue; } - const std::vector& GetParsedKeywords() const - { - return this->ParsedKeywords; - } bool checkOutputVariable(const Result& arguments, cmExecutionStatus& status) const @@ -109,10 +104,7 @@ public: return false; } - if (std::find(this->GetParsedKeywords().begin(), - this->GetParsedKeywords().end(), - "OUTPUT_VARIABLE"_s) != this->GetParsedKeywords().end() && - arguments.Output.empty()) { + if (arguments.Output && arguments.Output->empty()) { status.SetError("Invalid name for output variable."); return false; } @@ -122,12 +114,11 @@ public: private: mutable std::vector KeywordsMissingValue; - mutable std::vector ParsedKeywords; }; struct OutputVariable { - std::string Output; + cm::optional Output; }; // Usable when OUTPUT_VARIABLE is the only option class OutputVariableParser @@ -307,7 +298,7 @@ bool HandleAppendCommand(std::vector const& args, } status.GetMakefile().AddDefinition( - arguments.Output.empty() ? args[1] : arguments.Output, path.String()); + arguments.Output ? *arguments.Output : args[1], path.String()); return true; } @@ -334,7 +325,7 @@ bool HandleAppendStringCommand(std::vector const& args, } status.GetMakefile().AddDefinition( - arguments.Output.empty() ? args[1] : arguments.Output, path.String()); + arguments.Output ? *arguments.Output : args[1], path.String()); return true; } @@ -364,7 +355,7 @@ bool HandleRemoveFilenameCommand(std::vector const& args, path.RemoveFileName(); status.GetMakefile().AddDefinition( - arguments.Output.empty() ? args[1] : arguments.Output, path.String()); + arguments.Output ? *arguments.Output : args[1], path.String()); return true; } @@ -395,7 +386,7 @@ bool HandleReplaceFilenameCommand(std::vector const& args, parser.GetInputs().empty() ? "" : parser.GetInputs().front()); status.GetMakefile().AddDefinition( - arguments.Output.empty() ? args[1] : arguments.Output, path.String()); + arguments.Output ? *arguments.Output : args[1], path.String()); return true; } @@ -405,7 +396,7 @@ bool HandleRemoveExtensionCommand(std::vector const& args, { struct Arguments { - std::string Output; + cm::optional Output; bool LastOnly = false; }; @@ -438,7 +429,7 @@ bool HandleRemoveExtensionCommand(std::vector const& args, } status.GetMakefile().AddDefinition( - arguments.Output.empty() ? args[1] : arguments.Output, path.String()); + arguments.Output ? *arguments.Output : args[1], path.String()); return true; } @@ -448,7 +439,7 @@ bool HandleReplaceExtensionCommand(std::vector const& args, { struct Arguments { - std::string Output; + cm::optional Output; bool LastOnly = false; }; @@ -483,7 +474,7 @@ bool HandleReplaceExtensionCommand(std::vector const& args, } status.GetMakefile().AddDefinition( - arguments.Output.empty() ? args[1] : arguments.Output, path.String()); + arguments.Output ? *arguments.Output : args[1], path.String()); return true; } @@ -512,7 +503,7 @@ bool HandleNormalPathCommand(std::vector const& args, auto path = cmCMakePath(inputPath).Normal(); status.GetMakefile().AddDefinition( - arguments.Output.empty() ? args[1] : arguments.Output, path.String()); + arguments.Output ? *arguments.Output : args[1], path.String()); return true; } @@ -525,8 +516,8 @@ bool HandleTransformPathCommand( { struct Arguments { - std::string Output; - std::string BaseDirectory; + cm::optional Output; + cm::optional BaseDirectory; bool Normalize = false; }; @@ -554,10 +545,11 @@ bool HandleTransformPathCommand( return false; } - if (std::find(parser.GetParsedKeywords().begin(), - parser.GetParsedKeywords().end(), - "BASE_DIRECTORY"_s) == parser.GetParsedKeywords().end()) { - arguments.BaseDirectory = status.GetMakefile().GetCurrentSourceDirectory(); + std::string baseDirectory; + if (arguments.BaseDirectory) { + baseDirectory = *arguments.BaseDirectory; + } else { + baseDirectory = status.GetMakefile().GetCurrentSourceDirectory(); } std::string inputPath; @@ -565,13 +557,13 @@ bool HandleTransformPathCommand( return false; } - auto path = transform(cmCMakePath(inputPath), arguments.BaseDirectory); + auto path = transform(cmCMakePath(inputPath), baseDirectory); if (arguments.Normalize) { path = path.Normal(); } status.GetMakefile().AddDefinition( - arguments.Output.empty() ? args[1] : arguments.Output, path.String()); + arguments.Output ? *arguments.Output : args[1], path.String()); return true; } -- cgit v0.12