[cfe-commits] r89847 - in /cfe/trunk: lib/Parse/ParseStmt.cpp lib/Sema/SemaStmt.cpp test/Sema/switch.c

Douglas Gregor dgregor at apple.com
Tue Nov 24 22:20:02 PST 2009


Author: dgregor
Date: Wed Nov 25 00:20:02 2009
New Revision: 89847

URL: http://llvm.org/viewvc/llvm-project?rev=89847&view=rev
Log:
When the condition of a switch() statement is semantically invalid,
still parse the body of the switch to try to avoid spurious
diagnostics. Fixes PR5606.

Modified:
    cfe/trunk/lib/Parse/ParseStmt.cpp
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/Sema/switch.c

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

==============================================================================
--- cfe/trunk/lib/Parse/ParseStmt.cpp (original)
+++ cfe/trunk/lib/Parse/ParseStmt.cpp Wed Nov 25 00:20:02 2009
@@ -715,9 +715,7 @@
 
   FullExprArg FullCond(Actions.FullExpr(Cond));
   
-  OwningStmtResult Switch(Actions);
-  if (!Cond.isInvalid() || CondVar.get())
-    Switch = Actions.ActOnStartOfSwitchStmt(FullCond, CondVar);
+  OwningStmtResult Switch = Actions.ActOnStartOfSwitchStmt(FullCond, CondVar);
 
   // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
   // there is no compound stmt.  C90 does not have this clause.  We only do this

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=89847&r1=89846&r2=89847&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Wed Nov 25 00:20:02 2009
@@ -288,12 +288,8 @@
     if (CondResult.isInvalid())
       return StmtError();
   }
-  Expr *ConditionExpr = CondResult.takeAs<Expr>();
-  if (!ConditionExpr)
-    return StmtError();
-
-  CondResult.release();
-  SwitchStmt *SS = new (Context) SwitchStmt(ConditionVar, ConditionExpr);
+  SwitchStmt *SS = new (Context) SwitchStmt(ConditionVar, 
+                                            CondResult.takeAs<Expr>());
   getSwitchStack().push_back(SS);
   return Owned(SS);
 }
@@ -496,6 +492,11 @@
   SS->setBody(BodyStmt, SwitchLoc);
   getSwitchStack().pop_back();
 
+  if (SS->getCond() == 0) {
+    SS->Destroy(Context);
+    return StmtError();
+  }
+    
   Expr *CondExpr = SS->getCond();
   QualType CondTypeBeforePromotion =
       GetTypeBeforeIntegralPromotion(CondExpr);

Modified: cfe/trunk/test/Sema/switch.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/switch.c?rev=89847&r1=89846&r2=89847&view=diff

==============================================================================
--- cfe/trunk/test/Sema/switch.c (original)
+++ cfe/trunk/test/Sema/switch.c Wed Nov 25 00:20:02 2009
@@ -1,5 +1,4 @@
 // RUN: clang-cc -fsyntax-only -verify %s
-
 void f (int z) { 
   while (z) { 
     default: z--;            // expected-error {{statement not in switch}}
@@ -75,3 +74,14 @@
       break;
   }
 }
+
+// PR5606
+int f0(int var) {
+  switch (va) { // expected-error{{use of undeclared identifier 'va'}}
+  case 1:
+    break;
+  case 2:
+    return 1;
+  }
+  return 2;
+}





More information about the cfe-commits mailing list