[cfe-commits] r39508 - /cfe/cfe/trunk/Parse/ParseStmt.cpp

clattner at cs.uiuc.edu clattner at cs.uiuc.edu
Wed Jul 11 09:45:06 PDT 2007


Author: clattner
Date: Wed Jul 11 11:45:06 2007
New Revision: 39508

URL: http://llvm.org/viewvc/llvm-project?rev=39508&view=rev
Log:
Improve the parsers resilience to sematic errors.  This allows us to turn:

void foo() {
  if (0) break;
  abc:
  def:
  hij:
    break;

into:

void foo() {
  if ((0)')
    ;
abc:
def:
hij:
  ;

instead of dropping the if and labels (due to break not being in a loop).

Modified:
    cfe/cfe/trunk/Parse/ParseStmt.cpp

Modified: cfe/cfe/trunk/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/ParseStmt.cpp?rev=39508&r1=39507&r2=39508&view=diff

==============================================================================
--- cfe/cfe/trunk/Parse/ParseStmt.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseStmt.cpp Wed Jul 11 11:45:06 2007
@@ -192,7 +192,10 @@
       ParseAttributes();
 
     StmtResult SubStmt = ParseStatement();
-    if (SubStmt.isInvalid) return true;
+    
+    // Broken substmt shouldn't prevent the label from being added to the AST.
+    if (SubStmt.isInvalid)
+      SubStmt = Actions.ParseNullStmt(ColonLoc);
     
     // FIXME: Enter this label into the symbol table for the function.
     return Actions.ParseLabelStmt(IdentTok.getLocation(), 
@@ -299,8 +302,10 @@
   }
   
   StmtResult SubStmt = ParseStatement();
+
+  // Broken substmt shouldn't prevent the case from being added to the AST.
   if (SubStmt.isInvalid)
-    return true;
+    SubStmt = Actions.ParseNullStmt(ColonLoc);
   
   // TODO: look up enclosing switch stmt.
   return Actions.ParseCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
@@ -429,6 +434,11 @@
   
   // Read the if condition.
   StmtResult CondStmt = ParseStatement();
+
+  // Broken substmt shouldn't prevent the label from being added to the AST.
+  if (CondStmt.isInvalid)
+    CondStmt = Actions.ParseNullStmt(Tok.getLocation());
+  
   
   // If it has an else, parse it.
   SourceLocation ElseLoc;
@@ -436,11 +446,11 @@
   if (Tok.getKind() == tok::kw_else) {
     ElseLoc = ConsumeToken();
     ElseStmt = ParseStatement();
+    
+    if (ElseStmt.isInvalid)
+      ElseStmt = Actions.ParseNullStmt(ElseLoc);
   }
   
-  if (CondStmt.isInvalid || ElseStmt.isInvalid)
-    return true;
-  
   return Actions.ParseIfStmt(IfLoc, CondExp.Val, CondStmt.Val,
                              ElseLoc, ElseStmt.Val);
 }





More information about the cfe-commits mailing list