]> granicus.if.org Git - clang/commitdiff
When deciding whether an expression has the boolean nature, don't look through
authorJohn McCall <rjmccall@apple.com>
Sat, 12 Jun 2010 01:56:02 +0000 (01:56 +0000)
committerJohn McCall <rjmccall@apple.com>
Sat, 12 Jun 2010 01:56:02 +0000 (01:56 +0000)
explicit casts.  Fixes PR7359.

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

lib/AST/Expr.cpp
lib/Sema/SemaStmt.cpp
test/Sema/switch.c

index c38cec32c3b2cb4713a4b073abd894a8006bd533..68fcb35c79daffd309b9f451be27fcfd56fc62f6 100644 (file)
@@ -52,7 +52,9 @@ bool Expr::isKnownToHaveBooleanValue() const {
     }
   }
   
-  if (const CastExpr *CE = dyn_cast<CastExpr>(this))
+  // Only look through implicit casts.  If the user writes
+  // '(int) (a && b)' treat it as an arbitrary int.
+  if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(this))
     return CE->getSubExpr()->isKnownToHaveBooleanValue();
   
   if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
index 875b160d712f70e56c2f016b8a1629810327b446..0c510fb1dd02de36f10a309c21ffe537ef080b81 100644 (file)
@@ -400,9 +400,7 @@ static bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
 /// 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) {
+  if (const CastExpr *ImplicitCast = dyn_cast<ImplicitCastExpr>(expr)) {
     const Expr *ExprBeforePromotion = ImplicitCast->getSubExpr();
     QualType TypeBeforePromotion = ExprBeforePromotion->getType();
     if (TypeBeforePromotion->isIntegralType()) {
index 27ad06657e265935c125cc14708212df204e7532..bb4822916cc72ecc5f117ad8a50331f8a79af672 100644 (file)
@@ -277,3 +277,14 @@ void test16() {
   case '6': return;
   }
 }
+
+// PR7359
+void test17(int x) {
+  switch (x >= 17) { // expected-warning {{switch condition has boolean value}}
+  case 0: return;
+  }
+
+  switch ((int) (x <= 17)) {
+  case 0: return;
+  }
+}