From 2e23d718592fe571f8fbd4dba9080b1892e3cd09 Mon Sep 17 00:00:00 2001 From: David Bolvansky Date: Tue, 24 Sep 2019 13:14:18 +0000 Subject: [PATCH] [Diagnostics] Handle tautological left shifts in boolean context git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@372749 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 4 ++++ lib/Sema/SemaChecking.cpp | 19 ++++++++++++++----- test/Sema/warn-int-in-bool-context.c | 19 ++++++++++++------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index e7dacc852b..08fe50c436 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -6155,6 +6155,10 @@ def warn_integer_constants_in_conditional_always_true : Warning< "converting the result of '?:' with integer constants to a boolean always " "evaluates to 'true'">, InGroup; +def warn_left_shift_always : Warning< + "converting the result of '<<' to a boolean always evaluates " + "to %select{false|true}0">, + InGroup; def warn_comparison_of_mixed_enum_types : Warning< "comparison of two values with different enumeration types" "%diff{ ($ and $)|}0,1">, diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 59b875aed7..02c0d489ae 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -11314,17 +11314,26 @@ static void DiagnoseIntInBoolContext(Sema &S, Expr *E) { if (const auto *BO = dyn_cast(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(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)) diff --git a/test/Sema/warn-int-in-bool-context.c b/test/Sema/warn-int-in-bool-context.c index ae35ca34fb..0c94ebb391 100644 --- a/test/Sema/warn-int-in-bool-context.c +++ b/test/Sema/warn-int-in-bool-context.c @@ -24,12 +24,17 @@ enum num { int test(int a, unsigned b, enum num n) { boolean r; - r = a << a; // expected-warning {{converting the result of '<<' to a boolean; did you mean '(a << a) != 0'?}} - r = MM; // expected-warning {{converting the result of '<<' to a boolean; did you mean '(a << a) != 0'?}} - r = (1 << 7); // expected-warning {{converting the result of '<<' to a boolean; did you mean '(1 << 7) != 0'?}} - r = 2UL << 2; - r = 2 << b; // expected-warning {{converting the result of '<<' to a boolean; did you mean '(2 << b) != 0'?}} - r = (unsigned)(2 << b); + r = a << a; // expected-warning {{converting the result of '<<' to a boolean; did you mean '(a << a) != 0'?}} + r = MM; // expected-warning {{converting the result of '<<' to a boolean; did you mean '(a << a) != 0'?}} + r = (1 << 7); // expected-warning {{converting the result of '<<' to a boolean always evaluates to true}} + r = 2UL << 2; // expected-warning {{converting the result of '<<' to a boolean always evaluates to true}} + r = 0 << a; // expected-warning {{converting the result of '<<' to a boolean always evaluates to false}} + r = 0 << 2; // expected-warning {{converting the result of '<<' to a boolean always evaluates to false}} + r = 1 << 0; // expected-warning {{converting the result of '<<' to a boolean always evaluates to true}} + r = 1 << 2; // expected-warning {{converting the result of '<<' to a boolean always evaluates to true}} + r = 1ULL << 2; // expected-warning {{converting the result of '<<' to a boolean always evaluates to true}} + r = 2 << b; // expected-warning {{converting the result of '<<' to a boolean; did you mean '(2 << b) != 0'?}} + r = (unsigned)(2 << b); r = b << 7; r = (1 << a); // expected-warning {{converting the result of '<<' to a boolean; did you mean '(1 << a) != 0'?}} r = TWO << a; // expected-warning {{converting the result of '<<' to a boolean; did you mean '(2 << a) != 0'?}} @@ -39,7 +44,7 @@ int test(int a, unsigned b, enum num n) { return a; for (a = 0; 1 << a; a++) // expected-warning {{converting the result of '<<' to a boolean; did you mean '(1 << a) != 0'?}} - ; + ; if (a << TWO) // expected-warning {{converting the result of '<<' to a boolean; did you mean '(a << 2) != 0'?}} return a; -- 2.50.1