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

Chris Lattner sabre at nondot.org
Fri Oct 16 09:45:22 PDT 2009


Author: lattner
Date: Fri Oct 16 11:45:22 2009
New Revision: 84259

URL: http://llvm.org/viewvc/llvm-project?rev=84259&view=rev
Log:
Implement PR4407 - missing warnings on case value overflow,
patch by Zhanyong Wan!

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

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Fri Oct 16 11:45:22 2009
@@ -368,6 +368,21 @@
   return false;
 }
 
+/// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of
+/// potentially integral-promoted expression @p expr.
+static QualType GetTypeBeforeIntegralPromotion(const Expr* expr) {
+  const ImplicitCastExpr *ImplicitCast =
+      dyn_cast_or_null<ImplicitCastExpr>(expr);
+  if (ImplicitCast != NULL) {
+    const Expr *ExprBeforePromotion = ImplicitCast->getSubExpr();
+    QualType TypeBeforePromotion = ExprBeforePromotion->getType();
+    if (TypeBeforePromotion->isIntegralType()) {
+      return TypeBeforePromotion;
+    }
+  }
+  return expr->getType();
+}
+
 Action::OwningStmtResult
 Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch,
                             StmtArg Body) {
@@ -382,6 +397,16 @@
   Expr *CondExpr = SS->getCond();
   QualType CondType = CondExpr->getType();
 
+  // C++ 6.4.2.p2:
+  // Integral promotions are performed (on the switch condition).
+  //
+  // A case value unrepresentable by the original switch condition
+  // type (before the promotion) doesn't make sense, even when it can
+  // be represented by the promoted type.  Therefore we need to find
+  // the pre-promotion type of the switch condition.
+  QualType CondTypeBeforePromotion =
+      GetTypeBeforeIntegralPromotion(CondExpr);
+
   if (!CondExpr->isTypeDependent() &&
       !CondType->isIntegerType()) { // C99 6.8.4.2p1
     Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
@@ -395,8 +420,8 @@
     = CondExpr->isTypeDependent() || CondExpr->isValueDependent();
   unsigned CondWidth
     = HasDependentValue? 0
-                       : static_cast<unsigned>(Context.getTypeSize(CondType));
-  bool CondIsSigned = CondType->isSignedIntegerType();
+      : static_cast<unsigned>(Context.getTypeSize(CondTypeBeforePromotion));
+  bool CondIsSigned = CondTypeBeforePromotion->isSignedIntegerType();
 
   // Accumulate all of the case values in a vector so that we can sort them
   // and detect duplicates.  This vector contains the APInt for the case after

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

==============================================================================
--- cfe/trunk/test/Sema/switch.c (original)
+++ cfe/trunk/test/Sema/switch.c Fri Oct 16 11:45:22 2009
@@ -68,3 +68,10 @@
   }
 } 
 
+void test6() {
+  const char ch = 'a';
+  switch(ch) {
+    case 1234:  // expected-warning {{overflow converting case value}}
+      break;
+  }
+}





More information about the cfe-commits mailing list