[cfe-commits] r40438 - in /cfe/trunk: Sema/SemaStmt.cpp include/clang/Basic/DiagnosticKinds.def test/Sema/switch.c

Chris Lattner sabre at nondot.org
Mon Jul 23 10:05:24 PDT 2007


Author: lattner
Date: Mon Jul 23 12:05:23 2007
New Revision: 40438

URL: http://llvm.org/viewvc/llvm-project?rev=40438&view=rev
Log:
correctly verify that default and case are in a switchstmt,
this fixes test/Sema/switch.c.

Added:
    cfe/trunk/test/Sema/switch.c
Modified:
    cfe/trunk/Sema/SemaStmt.cpp
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def

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

==============================================================================
--- cfe/trunk/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/Sema/SemaStmt.cpp Mon Jul 23 12:05:23 2007
@@ -55,7 +55,7 @@
                     SourceLocation DotDotDotLoc, ExprTy *rhsval,
                     SourceLocation ColonLoc, StmtTy *subStmt) {
   Stmt *SubStmt = static_cast<Stmt*>(subStmt);
-  Expr *LHSVal = ((Expr *)lhsval);
+  Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
   assert((LHSVal != 0) && "missing expression in case statement");
   
   SourceLocation ExpLoc;
@@ -67,19 +67,19 @@
   }
 
   // GCC extension: The expression shall be an integer constant.
-  Expr *RHSVal = ((Expr *)rhsval);
   if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
     Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
          RHSVal->getSourceRange());
     return SubStmt;
   }
+  
+  if (SwitchStack.empty()) {
+    Diag(CaseLoc, diag::err_case_not_in_switch);
+    return SubStmt;
+  }
 
   CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt);
-  
-  assert(!SwitchStack.empty() && "missing push/pop in switch stack!");
-  SwitchStmt *SS = SwitchStack.back();
-  SS->addSwitchCase(CS);
- 
+  SwitchStack.back()->addSwitchCase(CS);
   return CS;
 }
 
@@ -87,18 +87,14 @@
 Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, 
                        StmtTy *subStmt, Scope *CurScope) {
   Stmt *SubStmt = static_cast<Stmt*>(subStmt);
-  Scope *S = CurScope->getBreakParent();
   
-  if (!S) {
+  if (SwitchStack.empty()) {
     Diag(DefaultLoc, diag::err_default_not_in_switch);
     return SubStmt;
   }
   
   DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
-
-  assert(!SwitchStack.empty() && "missing push/pop in switch stack!");
-  SwitchStmt *SS = SwitchStack.back();
-  SS->addSwitchCase(DS);
+  SwitchStack.back()->addSwitchCase(DS);
 
   return DS;
 }

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=40438&r1=40437&r2=40438&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Mon Jul 23 12:05:23 2007
@@ -655,6 +655,8 @@
      "'break' statement not in loop or switch statement")
 DIAG(err_default_not_in_switch, ERROR,
      "'default' statement not in switch statement")
+DIAG(err_case_not_in_switch, ERROR,
+     "'case' statement not in switch statement")
 DIAG(err_typecheck_return_incompatible, ERROR,
      "incompatible type returning '%1', expected '%0'")
 DIAG(ext_typecheck_return_pointer_int, EXTENSION,

Added: cfe/trunk/test/Sema/switch.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/switch.c?rev=40438&view=auto

==============================================================================
--- cfe/trunk/test/Sema/switch.c (added)
+++ cfe/trunk/test/Sema/switch.c Mon Jul 23 12:05:23 2007
@@ -0,0 +1,9 @@
+// RUN: clang -parse-ast-check %s
+
+
+void f (int z) { 
+  while (z) { 
+    default: z--;   // expected-error {{statement not in switch}}
+  } 
+}
+





More information about the cfe-commits mailing list