]> granicus.if.org Git - clang/commitdiff
When the condition of a switch() statement is semantically invalid,
authorDouglas Gregor <dgregor@apple.com>
Wed, 25 Nov 2009 06:20:02 +0000 (06:20 +0000)
committerDouglas Gregor <dgregor@apple.com>
Wed, 25 Nov 2009 06:20:02 +0000 (06:20 +0000)
still parse the body of the switch to try to avoid spurious
diagnostics. Fixes PR5606.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89847 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Parse/ParseStmt.cpp
lib/Sema/SemaStmt.cpp
test/Sema/switch.c

index 2022fa51cfde5a8df34ab47aa34a4ccfd3daf195..c87010e356a08241efe0d8ff577d9b8200f8616e 100644 (file)
@@ -715,9 +715,7 @@ Parser::OwningStmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
 
   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
index b54de3e171118e678d1be221956b195682871da3..e2b065bb9051b4d203ccf02aca794a1c3697e464 100644 (file)
@@ -288,12 +288,8 @@ Sema::ActOnStartOfSwitchStmt(FullExprArg cond, DeclPtrTy CondVar) {
     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 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch,
   SS->setBody(BodyStmt, SwitchLoc);
   getSwitchStack().pop_back();
 
+  if (SS->getCond() == 0) {
+    SS->Destroy(Context);
+    return StmtError();
+  }
+    
   Expr *CondExpr = SS->getCond();
   QualType CondTypeBeforePromotion =
       GetTypeBeforeIntegralPromotion(CondExpr);
index 122947e7ce5cdfecbb50864f4b0d06494f84fa54..3ee371202f91d87624776d4e6aff7dce1d1c51a4 100644 (file)
@@ -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 @@ void test6() {
       break;
   }
 }
+
+// PR5606
+int f0(int var) {
+  switch (va) { // expected-error{{use of undeclared identifier 'va'}}
+  case 1:
+    break;
+  case 2:
+    return 1;
+  }
+  return 2;
+}