diff options
| author | Alex Turbov <i.zaufi@gmail.com> | 2021-08-09 21:41:55 (GMT) |
|---|---|---|
| committer | Brad King <brad.king@kitware.com> | 2021-08-11 12:57:36 (GMT) |
| commit | d22f68d01998ff3cbbdebaec2f1071148b82d588 (patch) | |
| tree | 80cffc3310c41c2ef43efccae6f048f62a22ef4c | |
| parent | e97e714f0d59809fcefdc94feaadedb319d252c6 (diff) | |
| download | CMake-d22f68d01998ff3cbbdebaec2f1071148b82d588.zip CMake-d22f68d01998ff3cbbdebaec2f1071148b82d588.tar.gz CMake-d22f68d01998ff3cbbdebaec2f1071148b82d588.tar.bz2 | |
Refactor: Transform `while` loop into `for`
And reduce scope for some variables + use some more `auto`.
| -rw-r--r-- | Source/cmWhileCommand.cxx | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/Source/cmWhileCommand.cxx b/Source/cmWhileCommand.cxx index 8938663..1285baf 100644 --- a/Source/cmWhileCommand.cxx +++ b/Source/cmWhileCommand.cxx @@ -61,21 +61,31 @@ bool cmWhileFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff, bool cmWhileFunctionBlocker::Replay(std::vector<cmListFileFunction> functions, cmExecutionStatus& inStatus) { - cmMakefile& mf = inStatus.GetMakefile(); - std::string errorString; - - std::vector<cmExpandedCommandArgument> 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<cmExpandedCommandArgument> expandedArguments; + // At least same size expected for `expandedArguments` as `Args` + expandedArguments.reserve(this->Args.size()); + + auto expandArgs = [&mf](std::vector<cmListFileArgument> const& args, + std::vector<cmExpandedCommandArgument>& out) + -> std::vector<cmExpandedCommandArgument>& { + out.clear(); + mf.ExpandArguments(args, out); + return out; + }; + + std::string errorString; + MessageType messageType; + auto isTrue = true; - while (isTrue) { + 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<cmListFileFunction> 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<cmListFileArgument> const& args, // create a function blocker { - cmMakefile& makefile = status.GetMakefile(); + auto& makefile = status.GetMakefile(); auto fb = cm::make_unique<cmWhileFunctionBlocker>(&makefile); fb->Args = args; makefile.AddFunctionBlocker(std::move(fb)); |
