Change Parser::ParseCaseStatement to use an iterative approach to parsing
multiple sequential case statements instead of doing it with recursion.  This
fixes a problem where we run out of stack space parsing 100K directly nested
cases.

There are a couple other problems that prevent this from being useful in 
practice (right now the example only parses correctly with -disable-free and
doesn't work with -emit-llvm), but this is a start.

I'm not including a testcase because it is large and uninteresting for 
regtesting.

Sebastian, I would appreciate it if you could scrutinize the smart pointer 
gymnastics I do.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66011 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index ea9ba4c..fcc501c 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/lib/Sema/SemaStmt.cpp
@@ -61,10 +61,9 @@
     DeclGroupRef DG(*decls.begin());                      
     return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
   }
-  else {
-    DeclGroupRef DG(DeclGroup::Create(Context, decls.size(), &decls[0]));
-    return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
-  }
+
+  DeclGroupRef DG(DeclGroup::Create(Context, decls.size(), &decls[0]));
+  return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
 }
 
 Action::OwningStmtResult
@@ -114,16 +113,14 @@
 Action::OwningStmtResult
 Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprArg lhsval,
                     SourceLocation DotDotDotLoc, ExprArg rhsval,
-                    SourceLocation ColonLoc, StmtArg subStmt) {
-  Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
+                    SourceLocation ColonLoc) {
   assert((lhsval.get() != 0) && "missing expression in case statement");
 
   // C99 6.8.4.2p3: The expression shall be an integer constant.
   // However, GCC allows any evaluatable integer expression. 
-
   Expr *LHSVal = static_cast<Expr*>(lhsval.get());
   if (VerifyIntegerConstantExpression(LHSVal))
-    return Owned(SubStmt);
+    return StmtError();
 
   // GCC extension: The expression shall be an integer constant.
 
@@ -135,17 +132,24 @@
 
   if (SwitchStack.empty()) {
     Diag(CaseLoc, diag::err_case_not_in_switch);
-    return Owned(SubStmt);
+    return StmtError();
   }
 
   // Only now release the smart pointers.
   lhsval.release();
   rhsval.release();
-  CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
+  CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc);
   SwitchStack.back()->addSwitchCase(CS);
   return Owned(CS);
 }
 
+/// ActOnCaseStmtBody - This installs a statement as the body of a case.
+void Sema::ActOnCaseStmtBody(StmtTy *caseStmt, StmtArg subStmt) {
+  CaseStmt *CS = static_cast<CaseStmt*>(caseStmt);
+  Stmt *SubStmt = static_cast<Stmt*>(subStmt.release());
+  CS->setSubStmt(SubStmt);
+}
+
 Action::OwningStmtResult
 Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, 
                        StmtArg subStmt, Scope *CurScope) {