Ver código fonte

Fix assertion failure on unmatched foreach in function (#15572)

The lexical scope counting added by commit v3.2.0-rc1~332^2~1 (Track
nested loop levels in CMake language with a stack of counters,
2014-11-18) forgot to account for scopes popped by error messages about
unclosed scopes.  Teach the error handler to pop the lexical scope it
reports as unclosed.  Re-order the lexical scope RAII object to be
inside the variable scope RAII object scope so that the lexical scope
is fully closed before we check assertions about variable scopes.

Extend the RunCMake.Syntax test with a case covering this.
Brad King 10 anos atrás
pai
commit
3a65606591

+ 1 - 1
Source/cmFunctionCommand.cxx

@@ -94,8 +94,8 @@ bool cmFunctionHelperCommand::InvokeInitialPass
     }
 
   // we push a scope on the makefile
-  cmMakefile::LexicalPushPop lexScope(this->Makefile);
   cmMakefile::ScopePushPop varScope(this->Makefile);
+  cmMakefile::LexicalPushPop lexScope(this->Makefile);
   static_cast<void>(varScope);
 
   // Push a weak policy scope which restores the policies recorded at

+ 1 - 0
Source/cmMakefile.cxx

@@ -3284,6 +3284,7 @@ void cmMakefile::PopFunctionBlockerBarrier(bool reportError)
     this->FunctionBlockerBarriers.back();
   while(this->FunctionBlockers.size() > barrier)
     {
+    cmMakefile::LoopBlockPop loopBlockPop(this);
     cmsys::auto_ptr<cmFunctionBlocker> fb(this->FunctionBlockers.back());
     this->FunctionBlockers.pop_back();
     if(reportError)

+ 1 - 0
Tests/RunCMake/Syntax/FunctionUnmatchedForeach-result.txt

@@ -0,0 +1 @@
+1

+ 8 - 0
Tests/RunCMake/Syntax/FunctionUnmatchedForeach-stderr.txt

@@ -0,0 +1,8 @@
+^CMake Error at FunctionUnmatchedForeach.cmake:[0-9]+ \(f\):
+  A logical block opening on the line
+
+    .*/Tests/RunCMake/Syntax/FunctionUnmatchedForeach.cmake:[0-9]+ \(foreach\)
+
+  is not closed.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:[0-9]+ \(include\)$

+ 5 - 0
Tests/RunCMake/Syntax/FunctionUnmatchedForeach.cmake

@@ -0,0 +1,5 @@
+function(f)
+  foreach(i 1)
+  #endforeach() # missing
+endfunction()
+f()

+ 1 - 0
Tests/RunCMake/Syntax/MacroUnmatchedForeach-result.txt

@@ -0,0 +1 @@
+1

+ 8 - 0
Tests/RunCMake/Syntax/MacroUnmatchedForeach-stderr.txt

@@ -0,0 +1,8 @@
+^CMake Error at MacroUnmatchedForeach.cmake:[0-9]+ \(m\):
+  A logical block opening on the line
+
+    .*/Tests/RunCMake/Syntax/MacroUnmatchedForeach.cmake:[0-9]+ \(foreach\)
+
+  is not closed.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:[0-9]+ \(include\)$

+ 5 - 0
Tests/RunCMake/Syntax/MacroUnmatchedForeach.cmake

@@ -0,0 +1,5 @@
+macro(m)
+  foreach(i 1)
+  #endforeach() # missing
+endmacro()
+m()

+ 4 - 0
Tests/RunCMake/Syntax/RunCMakeTest.cmake

@@ -108,3 +108,7 @@ run_cmake(CMP0053-NameWithNewlineQuoted)
 run_cmake(CMP0053-NameWithCarriageReturnQuoted)
 run_cmake(CMP0053-NameWithEscapedSpacesQuoted)
 run_cmake(CMP0053-NameWithEscapedTabsQuoted)
+
+# Function and macro tests.
+run_cmake(FunctionUnmatchedForeach)
+run_cmake(MacroUnmatchedForeach)