From 849fd28cf0f7bcd978d0ddbb52a9ee1a3073dfef Mon Sep 17 00:00:00 2001 From: David Bolvansky Date: Tue, 24 Sep 2019 09:14:33 +0000 Subject: [Diagnostics] Do not diagnose unsigned shifts in boolean context (-Wint-in-bool-context) I was looking at old GCC's patch. Current "trunk" version avoids warning for unsigned case, GCC warns only for signed shifts. llvm-svn: 372708 --- clang/lib/Sema/SemaChecking.cpp | 47 ++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 26 deletions(-) (limited to 'clang/lib/Sema/SemaChecking.cpp') diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 19c83d6bac2..59b875aed76 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -11296,44 +11296,39 @@ static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T, return true; } +static const IntegerLiteral *getIntegerLiteral(Expr *E) { + const auto *IL = dyn_cast(E); + if (!IL) { + if (auto *UO = dyn_cast(E)) { + if (UO->getOpcode() == UO_Minus) + return dyn_cast(UO->getSubExpr()); + } + } + + return IL; +} + static void DiagnoseIntInBoolContext(Sema &S, Expr *E) { E = E->IgnoreParenImpCasts(); SourceLocation ExprLoc = E->getExprLoc(); if (const auto *BO = dyn_cast(E)) { BinaryOperator::Opcode Opc = BO->getOpcode(); - if (Opc == BO_Shl) + // Do not diagnose unsigned shifts. + if (Opc == BO_Shl && E->getType()->isSignedIntegerType()) S.Diag(ExprLoc, diag::warn_left_shift_in_bool_context) << E; } if (const auto *CO = dyn_cast(E)) { - const auto *LHS = dyn_cast(CO->getTrueExpr()); - if (!LHS) { - if (auto *UO = dyn_cast(CO->getTrueExpr())) { - if (UO->getOpcode() == UO_Minus) - LHS = dyn_cast(UO->getSubExpr()); - if (!LHS) - return; - } else { - return; - } - } - - const auto *RHS = dyn_cast(CO->getFalseExpr()); - if (!RHS) { - if (auto *UO = dyn_cast(CO->getFalseExpr())) { - if (UO->getOpcode() == UO_Minus) - RHS = dyn_cast(UO->getSubExpr()); - if (!RHS) - return; - } else { - return; - } - } - + const auto *LHS = getIntegerLiteral(CO->getTrueExpr()); + if (!LHS) + return; + const auto *RHS = getIntegerLiteral(CO->getFalseExpr()); + if (!RHS) + return; if ((LHS->getValue() == 0 || LHS->getValue() == 1) && (RHS->getValue() == 0 || RHS->getValue() == 1)) - // Do not diagnose common idioms + // Do not diagnose common idioms. return; if (LHS->getValue() != 0 && LHS->getValue() != 0) S.Diag(ExprLoc, diag::warn_integer_constants_in_conditional_always_true); -- cgit v1.2.3