Use RAII objects to ensure proper destruction of expression and statement AST nodes in the parser in most cases, even on error.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@60057 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Parse/ParseInit.cpp b/lib/Parse/ParseInit.cpp
index c3d2cd2..61c9b9c 100644
--- a/lib/Parse/ParseInit.cpp
+++ b/lib/Parse/ParseInit.cpp
@@ -13,6 +13,7 @@
 
 #include "clang/Parse/Designator.h"
 #include "clang/Parse/Parser.h"
+#include "AstGuard.h"
 #include "clang/Basic/Diagnostic.h"
 #include "llvm/ADT/SmallString.h"
 using namespace clang;
@@ -238,11 +239,11 @@
 ///
 Parser::ExprResult Parser::ParseBraceInitializer() {
   SourceLocation LBraceLoc = ConsumeBrace();
-  
+
   /// InitExprs - This is the actual list of expressions contained in the
   /// initializer.
-  llvm::SmallVector<ExprTy*, 8> InitExprs;
-  
+  ExprVector InitExprs(Actions);
+
   /// ExprDesignators - For each initializer, keep track of the designator that
   /// was specified for it, if any.
   InitListDesignations InitExprDesignations(Actions);
@@ -289,6 +290,9 @@
       // parsing the rest of the initializer.  This allows us to emit
       // diagnostics for later elements that we find.  If we don't see a comma,
       // assume there is a parse error, and just skip to recover.
+      // FIXME: This comment doesn't sound right. If there is a r_brace
+      // immediately, it can't be an error, since there is no other way of
+      // leaving this loop except through this if.
       if (Tok.isNot(tok::comma)) {
         SkipUntil(tok::r_brace, false, true);
         break;
@@ -305,13 +309,9 @@
     if (Tok.is(tok::r_brace)) break;
   }
   if (InitExprsOk && Tok.is(tok::r_brace))
-    return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(),
+    return Actions.ActOnInitList(LBraceLoc, InitExprs.take(), InitExprs.size(),
                                  InitExprDesignations, ConsumeBrace());
   
-  // On error, delete any parsed subexpressions.
-  for (unsigned i = 0, e = InitExprs.size(); i != e; ++i)
-    Actions.DeleteExpr(InitExprs[i]);
-  
   // Match the '}'.
   MatchRHSPunctuation(tok::r_brace, LBraceLoc);
   return ExprResult(true); // an error occurred.