r224237 - Sema: Don't leave switch stack inconsistent when recovering

David Majnemer david.majnemer at gmail.com
Sun Dec 14 23:46:12 PST 2014


Author: majnemer
Date: Mon Dec 15 01:46:12 2014
New Revision: 224237

URL: http://llvm.org/viewvc/llvm-project?rev=224237&view=rev
Log:
Sema: Don't leave switch stack inconsistent when recovering

We would exit Sema::ActOnFinishSwitchStmt early if we didn't have a
body.  This would leave an extra SwitchStmt on the SwitchStack.

This fixes PR21841.

Modified:
    cfe/trunk/include/clang/AST/Stmt.h
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/Parser/switch-recovery.cpp

Modified: cfe/trunk/include/clang/AST/Stmt.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Stmt.h?rev=224237&r1=224236&r2=224237&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/trunk/include/clang/AST/Stmt.h Mon Dec 15 01:46:12 2014
@@ -1015,7 +1015,7 @@ public:
 
   SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
   SourceLocation getLocEnd() const LLVM_READONLY {
-    return SubExprs[BODY]->getLocEnd();
+    return SubExprs[BODY] ? SubExprs[BODY]->getLocEnd() : SubExprs[COND]->getLocEnd();
   }
 
   // Iterators

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=224237&r1=224236&r2=224237&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Mon Dec 15 01:46:12 2014
@@ -730,9 +730,10 @@ Sema::ActOnFinishSwitchStmt(SourceLocati
   assert(SS == getCurFunction()->SwitchStack.back() &&
          "switch stack missing push/pop!");
 
+  getCurFunction()->SwitchStack.pop_back();
+
   if (!BodyStmt) return StmtError();
   SS->setBody(BodyStmt, SwitchLoc);
-  getCurFunction()->SwitchStack.pop_back();
 
   Expr *CondExpr = SS->getCond();
   if (!CondExpr) return StmtError();

Modified: cfe/trunk/test/Parser/switch-recovery.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/switch-recovery.cpp?rev=224237&r1=224236&r2=224237&view=diff
==============================================================================
--- cfe/trunk/test/Parser/switch-recovery.cpp (original)
+++ cfe/trunk/test/Parser/switch-recovery.cpp Mon Dec 15 01:46:12 2014
@@ -220,3 +220,12 @@ bool bar0() {
   case bar5: ;  // expected-error{{use of undeclared identifier 'bar5'}}
 }
 }
+
+namespace pr21841 {
+void fn1() {
+  switch (0)
+    switch (0  // expected-note{{to match this '('}}
+    {  // expected-error{{expected ')'}}
+    }
+} // expected-error{{expected statement}}
+}





More information about the cfe-commits mailing list