From: David Majnemer Date: Mon, 15 Dec 2014 07:46:12 +0000 (+0000) Subject: Sema: Don't leave switch stack inconsistent when recovering X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d8ea786550a237502f99ae943fec3cf9dce429cd;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@224237 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index 380c3b956e..59ca525d1f 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -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 diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 8f6c5c6fe3..0c3bfd50ac 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -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(); diff --git a/test/Parser/switch-recovery.cpp b/test/Parser/switch-recovery.cpp index 5345170182..4b06d55ba5 100644 --- a/test/Parser/switch-recovery.cpp +++ b/test/Parser/switch-recovery.cpp @@ -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}} +}