aboutsummaryrefslogtreecommitdiff
path: root/lib/Parse/ParseInit.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-04-20 19:07:56 +0000
committerChris Lattner <sabre@nondot.org>2008-04-20 19:07:56 +0000
commit65bb89c3825406bdeeab6b2d8408b72deac8bbb2 (patch)
treefd10d096710a7470642fb31a92e9337091a62533 /lib/Parse/ParseInit.cpp
parent70f66ab053f36ab3df7a778d09bcb2b4b0fec1f8 (diff)
Two improvements to initializer parsing:
1. If we hit a semantic error, try harder to recover to emit diagnostics for later initializer errors (PR2241). 2. Don't leak parsed initializers on an error. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@49998 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Parse/ParseInit.cpp')
-rw-r--r--lib/Parse/ParseInit.cpp24
1 files changed, 19 insertions, 5 deletions
diff --git a/lib/Parse/ParseInit.cpp b/lib/Parse/ParseInit.cpp
index 45cf86e5b4..c40fe88c21 100644
--- a/lib/Parse/ParseInit.cpp
+++ b/lib/Parse/ParseInit.cpp
@@ -201,12 +201,21 @@ Parser::ExprResult Parser::ParseInitializer() {
SubElt = ParseInitializerWithPotentialDesignator();
// If we couldn't parse the subelement, bail out.
- if (SubElt.isInvalid) {
- InitExprsOk = false;
- SkipUntil(tok::r_brace, false, true);
- break;
- } else
+ if (!SubElt.isInvalid) {
InitExprs.push_back(SubElt.Val);
+ } else {
+ InitExprsOk = false;
+
+ // We have two ways to try to recover from this error: if the code looks
+ // gramatically ok (i.e. we have a comma comming up) try to continue
+ // 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.
+ if (Tok.isNot(tok::comma)) {
+ SkipUntil(tok::r_brace, false, true);
+ break;
+ }
+ }
// If we don't have a comma continued list, we're done.
if (Tok.isNot(tok::comma)) break;
@@ -220,6 +229,11 @@ Parser::ExprResult Parser::ParseInitializer() {
if (InitExprsOk && Tok.is(tok::r_brace))
return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(),
ConsumeBrace());
+
+ // 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.