From 47d197745a44b20ee65edd0e3223464a43b3ee55 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 3 Mar 2022 16:49:28 -0500 Subject: Tests: Simplify RunCMake.{if,while} unbalanced parenthesis cases --- Tests/RunCMake/if/unbalanced-parenthesis-stderr.txt | 2 +- Tests/RunCMake/if/unbalanced-parenthesis.cmake | 11 ++++------- Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt | 2 +- Tests/RunCMake/while/unbalanced-parenthesis.cmake | 12 +++++------- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/Tests/RunCMake/if/unbalanced-parenthesis-stderr.txt b/Tests/RunCMake/if/unbalanced-parenthesis-stderr.txt index 770ccb8..d2260a0 100644 --- a/Tests/RunCMake/if/unbalanced-parenthesis-stderr.txt +++ b/Tests/RunCMake/if/unbalanced-parenthesis-stderr.txt @@ -1,7 +1,7 @@ CMake Error at unbalanced-parenthesis\.cmake:[0-9]+ \(if\): if given arguments: - "NOT" "\(" "IN_LIST" "some_list" + "\(" mismatched parenthesis in condition Call Stack \(most recent call first\): diff --git a/Tests/RunCMake/if/unbalanced-parenthesis.cmake b/Tests/RunCMake/if/unbalanced-parenthesis.cmake index c51c755..45932f6 100644 --- a/Tests/RunCMake/if/unbalanced-parenthesis.cmake +++ b/Tests/RunCMake/if/unbalanced-parenthesis.cmake @@ -1,8 +1,5 @@ -set(var_with_paren "(") -set(some_list "") - -if(NOT ${var_with_paren} IN_LIST some_list) - message(STATUS "Never prints") -else() - message(STATUS "Never prints") +set(paren "(") +if(${paren}) + message(STATUS "Condition incorrectly true") endif() +message(STATUS "Code incorrectly accepted") diff --git a/Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt b/Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt index 9d4132c..ffa4fda 100644 --- a/Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt +++ b/Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt @@ -1,7 +1,7 @@ CMake Error at unbalanced-parenthesis.cmake:[0-9]+ \(while\): had incorrect arguments: - "NOT" "\(" "IN_LIST" "some_list" + "\(" mismatched parenthesis in condition Call Stack \(most recent call first\): diff --git a/Tests/RunCMake/while/unbalanced-parenthesis.cmake b/Tests/RunCMake/while/unbalanced-parenthesis.cmake index 7a12701..15322e0 100644 --- a/Tests/RunCMake/while/unbalanced-parenthesis.cmake +++ b/Tests/RunCMake/while/unbalanced-parenthesis.cmake @@ -1,8 +1,6 @@ -set(var_with_paren "(") -set(some_list "") - -while(NOT ${var_with_paren} IN_LIST some_list) - message(STATUS "Never prints") +set(paren "(") +while(${paren}) + message(STATUS "Condition incorrectly true") + break() endwhile() - -message(STATUS "Never prints") +message(STATUS "Code incorrectly accepted") -- cgit v0.12 From da2361ffb35799319abca6f7c3138c916685fb2d Mon Sep 17 00:00:00 2001 From: Alex Turbov Date: Thu, 3 Mar 2022 23:08:07 +0400 Subject: while: Restore tolerance of condition error Since commit 880ca66b51 (Fix: `while()` can silently ignore incorrect condition, 2021-08-09, v3.22.0-rc1~238^2~4) we correctly reject the code set(paren "(") while(${paren}) endwhile() However, rejecting it breaks compatibility with projects that used such code accidentally. In CMake 3.21 and below, any error in the condition was ignored because the `false` result exited the loop first. Restore tolerance of the error for now. A policy will be needed to make it an error later. Note that the same condition with `if` was always correctly rejected. Fixes: #22524 Issue: #23296 Co-authored-by: Brad King --- Source/cmWhileCommand.cxx | 11 ++++++++--- Tests/RunCMake/while/unbalanced-parenthesis-result.txt | 1 - Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt | 8 -------- Tests/RunCMake/while/unbalanced-parenthesis-stdout.txt | 1 + Tests/RunCMake/while/unbalanced-parenthesis.cmake | 1 + 5 files changed, 10 insertions(+), 12 deletions(-) delete mode 100644 Tests/RunCMake/while/unbalanced-parenthesis-result.txt delete mode 100644 Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt create mode 100644 Tests/RunCMake/while/unbalanced-parenthesis-stdout.txt diff --git a/Source/cmWhileCommand.cxx b/Source/cmWhileCommand.cxx index b8297ce..a93a81f 100644 --- a/Source/cmWhileCommand.cxx +++ b/Source/cmWhileCommand.cxx @@ -79,12 +79,17 @@ bool cmWhileFunctionBlocker::Replay(std::vector functions, return out; }; + // FIXME(#23296): For compatibility with older versions of CMake, we + // tolerate condition errors that evaluate to false. We should add + // a policy to enforce such errors. + bool enforceError = true; std::string errorString; MessageType messageType; for (cmConditionEvaluator conditionEvaluator(mf, whileBT); - conditionEvaluator.IsTrue(expandArgs(this->Args, expandedArguments), - errorString, messageType);) { + (enforceError = /* enforce condition errors that evaluate to true */ + 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); @@ -105,7 +110,7 @@ bool cmWhileFunctionBlocker::Replay(std::vector functions, } } - if (!errorString.empty()) { + if (!errorString.empty() && enforceError) { std::string err = "had incorrect arguments:\n "; for (auto const& i : expandedArguments) { err += " "; diff --git a/Tests/RunCMake/while/unbalanced-parenthesis-result.txt b/Tests/RunCMake/while/unbalanced-parenthesis-result.txt deleted file mode 100644 index d00491f..0000000 --- a/Tests/RunCMake/while/unbalanced-parenthesis-result.txt +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt b/Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt deleted file mode 100644 index ffa4fda..0000000 --- a/Tests/RunCMake/while/unbalanced-parenthesis-stderr.txt +++ /dev/null @@ -1,8 +0,0 @@ -CMake Error at unbalanced-parenthesis.cmake:[0-9]+ \(while\): - had incorrect arguments: - - "\(" - - mismatched parenthesis in condition -Call Stack \(most recent call first\): - CMakeLists\.txt:[0-9]+ \(include\) diff --git a/Tests/RunCMake/while/unbalanced-parenthesis-stdout.txt b/Tests/RunCMake/while/unbalanced-parenthesis-stdout.txt new file mode 100644 index 0000000..d45e194 --- /dev/null +++ b/Tests/RunCMake/while/unbalanced-parenthesis-stdout.txt @@ -0,0 +1 @@ +-- Code incorrectly accepted diff --git a/Tests/RunCMake/while/unbalanced-parenthesis.cmake b/Tests/RunCMake/while/unbalanced-parenthesis.cmake index 15322e0..39d736b 100644 --- a/Tests/RunCMake/while/unbalanced-parenthesis.cmake +++ b/Tests/RunCMake/while/unbalanced-parenthesis.cmake @@ -3,4 +3,5 @@ while(${paren}) message(STATUS "Condition incorrectly true") break() endwhile() +# FIXME(#23296): The above condition error is tolerated for compatibility. message(STATUS "Code incorrectly accepted") -- cgit v0.12