diff options
author | David Bolvansky <david.bolvansky@gmail.com> | 2019-09-24 13:14:18 +0000 |
---|---|---|
committer | David Bolvansky <david.bolvansky@gmail.com> | 2019-09-24 13:14:18 +0000 |
commit | 275e4df11548959f2c645f6ab1b7cd27223682a9 (patch) | |
tree | 987a16e4cfa84c087e00473ebfdc352cad42238e /clang/lib/Sema/SemaChecking.cpp | |
parent | 355764e3888578ba789ca4dd2c6da345dc6b725d (diff) | |
download | bcm5719-llvm-275e4df11548959f2c645f6ab1b7cd27223682a9.tar.gz bcm5719-llvm-275e4df11548959f2c645f6ab1b7cd27223682a9.zip |
[Diagnostics] Handle tautological left shifts in boolean context
llvm-svn: 372749
Diffstat (limited to 'clang/lib/Sema/SemaChecking.cpp')
-rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 59b875aed76..02c0d489ae0 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -11314,17 +11314,26 @@ static void DiagnoseIntInBoolContext(Sema &S, Expr *E) { if (const auto *BO = dyn_cast<BinaryOperator>(E)) { BinaryOperator::Opcode Opc = BO->getOpcode(); + Expr::EvalResult Result; // Do not diagnose unsigned shifts. - if (Opc == BO_Shl && E->getType()->isSignedIntegerType()) - S.Diag(ExprLoc, diag::warn_left_shift_in_bool_context) << E; + if (Opc == BO_Shl) { + const auto *LHS = getIntegerLiteral(BO->getLHS()); + const auto *RHS = getIntegerLiteral(BO->getRHS()); + if (LHS && LHS->getValue() == 0) + S.Diag(ExprLoc, diag::warn_left_shift_always) << 0; + else if (RHS && RHS->getValue().isNonNegative() && + E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects)) + S.Diag(ExprLoc, diag::warn_left_shift_always) + << (Result.Val.getInt() != 0); + else if (E->getType()->isSignedIntegerType()) + S.Diag(ExprLoc, diag::warn_left_shift_in_bool_context) << E; + } } if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { const auto *LHS = getIntegerLiteral(CO->getTrueExpr()); - if (!LHS) - return; const auto *RHS = getIntegerLiteral(CO->getFalseExpr()); - if (!RHS) + if (!LHS || !RHS) return; if ((LHS->getValue() == 0 || LHS->getValue() == 1) && (RHS->getValue() == 0 || RHS->getValue() == 1)) |