From 880ca66b51551d5ee732c7b81349c1a5f724d093 Mon Sep 17 00:00:00 2001 From: Alex Turbov Date: Mon, 9 Aug 2021 20:58:27 +0300 Subject: Fix: `while()` can silently ignore incorrect condition When `conditionEvaluator.IsTrue(...)` returns `false` it just didn't print the error occured. --- Source/cmWhileCommand.cxx | 36 +++++++++++----------- .../while/unbalanced-parenthesis-result.txt | 1 + .../while/unbalanced-parenthesis-stderr.txt | 5 +++ Tests/RunCMake/while/unbalanced-parenthesis.cmake | 2 +- 4 files changed, 25 insertions(+), 19 deletions(-) create mode 100644 Tests/RunCMake/while/unbalanced-parenthesis-result.txt create mode 100644 Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt diff --git a/Source/cmWhileCommand.cxx b/Source/cmWhileCommand.cxx index 327c1c7..1363386 100644 --- a/Source/cmWhileCommand.cxx +++ b/Source/cmWhileCommand.cxx @@ -75,24 +75,6 @@ bool cmWhileFunctionBlocker::Replay(std::vector functions, conditionEvaluator.IsTrue(expandedArguments, errorString, messageType); while (isTrue) { - if (!errorString.empty()) { - std::string err = "had incorrect arguments: "; - for (cmListFileArgument const& arg : this->Args) { - err += (arg.Delim ? "\"" : ""); - err += arg.Value; - err += (arg.Delim ? "\"" : ""); - err += " "; - } - err += "("; - err += errorString; - err += ")."; - mf.GetCMakeInstance()->IssueMessage(messageType, err, whileBT); - if (messageType == MessageType::FATAL_ERROR) { - cmSystemTools::SetFatalErrorOccured(); - return true; - } - } - // Invoke all the functions that were collected in the block. for (cmListFileFunction const& fn : functions) { cmExecutionStatus status(mf); @@ -116,6 +98,24 @@ bool cmWhileFunctionBlocker::Replay(std::vector functions, isTrue = conditionEvaluator.IsTrue(expandedArguments, errorString, messageType); } + + if (!isTrue && !errorString.empty()) { + std::string err = "had incorrect arguments: "; + for (cmListFileArgument const& arg : this->Args) { + err += (arg.Delim ? "\"" : ""); + err += arg.Value; + err += (arg.Delim ? "\"" : ""); + err += " "; + } + err += "("; + err += errorString; + err += ")."; + mf.GetCMakeInstance()->IssueMessage(messageType, err, whileBT); + if (messageType == MessageType::FATAL_ERROR) { + cmSystemTools::SetFatalErrorOccured(); + } + } + return true; } diff --git a/Tests/RunCMake/while/unbalanced-parenthesis-result.txt b/Tests/RunCMake/while/unbalanced-parenthesis-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/while/unbalanced-parenthesis-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt b/Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt new file mode 100644 index 0000000..33ac54a --- /dev/null +++ b/Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt @@ -0,0 +1,5 @@ +CMake Error at unbalanced-parenthesis.cmake:[0-9]+ \(while\): + had incorrect arguments: NOT \$\{var_with_paren\} IN_LIST some_list + \(mismatched parenthesis in condition\). +Call Stack \(most recent call first\): + CMakeLists\.txt:[0-9]+ \(include\) diff --git a/Tests/RunCMake/while/unbalanced-parenthesis.cmake b/Tests/RunCMake/while/unbalanced-parenthesis.cmake index 4b6a5cd..7a12701 100644 --- a/Tests/RunCMake/while/unbalanced-parenthesis.cmake +++ b/Tests/RunCMake/while/unbalanced-parenthesis.cmake @@ -5,4 +5,4 @@ while(NOT ${var_with_paren} IN_LIST some_list) message(STATUS "Never prints") endwhile() -message(STATUS "It prints but in fact `while()` have to fail") +message(STATUS "Never prints") -- cgit v0.12