mirror of
https://github.com/Kitware/CMake.git
synced 2025-12-31 02:39:48 -06:00
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 <brad.king@kitware.com>
This commit is contained in:
@@ -79,12 +79,17 @@ bool cmWhileFunctionBlocker::Replay(std::vector<cmListFileFunction> 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<cmListFileFunction> functions,
|
||||
}
|
||||
}
|
||||
|
||||
if (!errorString.empty()) {
|
||||
if (!errorString.empty() && enforceError) {
|
||||
std::string err = "had incorrect arguments:\n ";
|
||||
for (auto const& i : expandedArguments) {
|
||||
err += " ";
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
1
|
||||
@@ -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\)
|
||||
1
Tests/RunCMake/while/unbalanced-parenthesis-stdout.txt
Normal file
1
Tests/RunCMake/while/unbalanced-parenthesis-stdout.txt
Normal file
@@ -0,0 +1 @@
|
||||
-- Code incorrectly accepted
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user