From 6907fbe758d23e1aec4c0a67e7b633d1d855feb4 Mon Sep 17 00:00:00 2001 From: John McCall Date: Sat, 12 Jun 2010 01:56:02 +0000 Subject: [PATCH] When deciding whether an expression has the boolean nature, don't look through 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 | 4 +++- lib/Sema/SemaStmt.cpp | 4 +--- test/Sema/switch.c | 11 +++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index c38cec32c3..68fcb35c79 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -52,7 +52,9 @@ bool Expr::isKnownToHaveBooleanValue() const { } } - if (const CastExpr *CE = dyn_cast(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(this)) return CE->getSubExpr()->isKnownToHaveBooleanValue(); if (const BinaryOperator *BO = dyn_cast(this)) { diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 875b160d71..0c510fb1dd 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -400,9 +400,7 @@ static bool EqEnumVals(const std::pair& 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(expr); - if (ImplicitCast != NULL) { + if (const CastExpr *ImplicitCast = dyn_cast(expr)) { const Expr *ExprBeforePromotion = ImplicitCast->getSubExpr(); QualType TypeBeforePromotion = ExprBeforePromotion->getType(); if (TypeBeforePromotion->isIntegralType()) { diff --git a/test/Sema/switch.c b/test/Sema/switch.c index 27ad06657e..bb4822916c 100644 --- a/test/Sema/switch.c +++ b/test/Sema/switch.c @@ -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; + } +} -- 2.50.1