]> granicus.if.org Git - clang/commitdiff
Sema: Don't leave switch stack inconsistent when recovering
authorDavid Majnemer <david.majnemer@gmail.com>
Mon, 15 Dec 2014 07:46:12 +0000 (07:46 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Mon, 15 Dec 2014 07:46:12 +0000 (07:46 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@224237 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 380c3b956e6d45a4e4848be91dce25cabe7688f4..59ca525d1f5f4bd294e824f077ef307612873367 100644 (file)
@@ -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
index 8f6c5c6fe3491931db1826ee9ac693d02316a9ea..0c3bfd50acf101385a01f8f46189364239968e1f 100644 (file)
@@ -730,9 +730,10 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
   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();
index 534517018267f403e28c79a99d3fb41a6485d4b3..4b06d55ba5944b8b9deeb49724322ba3042720fd 100644 (file)
@@ -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}}
+}