From d22f68d01998ff3cbbdebaec2f1071148b82d588 Mon Sep 17 00:00:00 2001 From: Alex Turbov Date: Tue, 10 Aug 2021 00:41:55 +0300 Subject: [PATCH] Refactor: Transform `while` loop into `for` And reduce scope for some variables + use some more `auto`. --- Source/cmWhileCommand.cxx | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/Source/cmWhileCommand.cxx b/Source/cmWhileCommand.cxx index 89386635ba..1285baf3b6 100644 --- a/Source/cmWhileCommand.cxx +++ b/Source/cmWhileCommand.cxx @@ -61,21 +61,31 @@ bool cmWhileFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff, bool cmWhileFunctionBlocker::Replay(std::vector functions, cmExecutionStatus& inStatus) { - cmMakefile& mf = inStatus.GetMakefile(); - std::string errorString; - - std::vector expandedArguments; - mf.ExpandArguments(this->Args, expandedArguments); - MessageType messageType; + auto& mf = inStatus.GetMakefile(); cmListFileBacktrace whileBT = mf.GetBacktrace().Push(this->GetStartingContext()); - cmConditionEvaluator conditionEvaluator(mf, whileBT); - bool isTrue = - conditionEvaluator.IsTrue(expandedArguments, errorString, messageType); + std::vector expandedArguments; + // At least same size expected for `expandedArguments` as `Args` + expandedArguments.reserve(this->Args.size()); - while (isTrue) { + auto expandArgs = [&mf](std::vector const& args, + std::vector& out) + -> std::vector& { + out.clear(); + mf.ExpandArguments(args, out); + return out; + }; + + std::string errorString; + MessageType messageType; + auto isTrue = true; + + for (cmConditionEvaluator conditionEvaluator(mf, whileBT); + (isTrue = + conditionEvaluator.IsTrue(expandArgs(this->Args, expandedArguments), + errorString, messageType));) { // Invoke all the functions that were collected in the block. for (cmListFileFunction const& fn : functions) { cmExecutionStatus status(mf); @@ -94,15 +104,11 @@ bool cmWhileFunctionBlocker::Replay(std::vector functions, return true; } } - expandedArguments.clear(); - mf.ExpandArguments(this->Args, expandedArguments); - isTrue = - conditionEvaluator.IsTrue(expandedArguments, errorString, messageType); } if (!isTrue && !errorString.empty()) { std::string err = "had incorrect arguments:\n "; - for (cmExpandedCommandArgument const& i : expandedArguments) { + for (auto const& i : expandedArguments) { err += " "; err += cmOutputConverter::EscapeForCMake(i.GetValue()); } @@ -127,7 +133,7 @@ bool cmWhileCommand(std::vector const& args, // create a function blocker { - cmMakefile& makefile = status.GetMakefile(); + auto& makefile = status.GetMakefile(); auto fb = cm::make_unique(&makefile); fb->Args = args; makefile.AddFunctionBlocker(std::move(fb));