[cfe-commits] r49998 - in /cfe/trunk: lib/Parse/ParseInit.cpp test/Parser/recovery-2.c

Chris Lattner sabre at nondot.org
Sun Apr 20 12:07:57 PDT 2008


Author: lattner
Date: Sun Apr 20 14:07:56 2008
New Revision: 49998

URL: http://llvm.org/viewvc/llvm-project?rev=49998&view=rev
Log:
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.

Added:
    cfe/trunk/test/Parser/recovery-2.c
Modified:
    cfe/trunk/lib/Parse/ParseInit.cpp

Modified: cfe/trunk/lib/Parse/ParseInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseInit.cpp?rev=49998&r1=49997&r2=49998&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseInit.cpp (original)
+++ cfe/trunk/lib/Parse/ParseInit.cpp Sun Apr 20 14:07:56 2008
@@ -201,12 +201,21 @@
       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 @@
   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.

Added: cfe/trunk/test/Parser/recovery-2.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/recovery-2.c?rev=49998&view=auto

==============================================================================
--- cfe/trunk/test/Parser/recovery-2.c (added)
+++ cfe/trunk/test/Parser/recovery-2.c Sun Apr 20 14:07:56 2008
@@ -0,0 +1,8 @@
+// RUN: clang -fsyntax-only -verify -pedantic %s
+
+
+// PR2241
+float f[] = { 
+  1e,            // expected-error {{exponent}}
+  1ee0           // expected-error {{exponent}}
+};





More information about the cfe-commits mailing list