From 0683a1418b28b289eca4cb602ac69780f9e0a609 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Tue, 31 May 2011 05:41:42 +0000 Subject: [PATCH] Expand the coverage of the warning for constants on the RHS of logical operands: return f() || -1; where the user meant to write '|'. This bootstraps without any additional warnings. Patch by Richard Trieu. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@132327 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaExpr.cpp | 14 ++++++++------ test/CodeGenCXX/static-init-2.cpp | 2 +- test/Sema/exprs.c | 18 ++++++++++++++++++ test/SemaCXX/expressions.cpp | 31 +++++++++++++++++++++++++++++++ test/SemaCXX/switch.cpp | 2 +- 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 0a74daa008..e75af910bd 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -7797,13 +7797,15 @@ inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] // If the RHS can be constant folded, and if it constant folds to something // that isn't 0 or 1 (which indicate a potential logical operation that // happened to fold to true/false) then warn. + // Parens on the RHS are ignored. Expr::EvalResult Result; - if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects && - Result.Val.getInt() != 0 && Result.Val.getInt() != 1) { - Diag(Loc, diag::warn_logical_instead_of_bitwise) - << rex.get()->getSourceRange() - << (Opc == BO_LAnd ? "&&" : "||") - << (Opc == BO_LAnd ? "&" : "|"); + if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects) + if ((getLangOptions().Bool && !rex.get()->getType()->isBooleanType()) || + (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) { + Diag(Loc, diag::warn_logical_instead_of_bitwise) + << rex.get()->getSourceRange() + << (Opc == BO_LAnd ? "&&" : "||") + << (Opc == BO_LAnd ? "&" : "|"); } } diff --git a/test/CodeGenCXX/static-init-2.cpp b/test/CodeGenCXX/static-init-2.cpp index 65ab3bb126..768e6de92c 100644 --- a/test/CodeGenCXX/static-init-2.cpp +++ b/test/CodeGenCXX/static-init-2.cpp @@ -3,4 +3,4 @@ // Make sure we don't crash generating y; its value is constant, but the // initializer has side effects, so EmitConstantExpr should fail. int x(); -int y = x() && 0; +int y = x() & 0; diff --git a/test/Sema/exprs.c b/test/Sema/exprs.c index e4eeaec05d..9ce1481f16 100644 --- a/test/Sema/exprs.c +++ b/test/Sema/exprs.c @@ -189,6 +189,24 @@ int test20(int x) { // no warning, this is an idiom for "true" in old C style. return x && (signed char)1; + + return x || 0; + return x || 1; + return x || -1; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + return x || 5; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + return x && 0; + return x && 1; + return x && -1; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + return x && 5; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + return x || (0); + return x || (1); + return x || (-1); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + return x || (5); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + return x && (0); + return x && (1); + return x && (-1); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + return x && (5); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + } struct Test21; // expected-note 2 {{forward declaration}} diff --git a/test/SemaCXX/expressions.cpp b/test/SemaCXX/expressions.cpp index c4e9dccbf6..95ece48e51 100644 --- a/test/SemaCXX/expressions.cpp +++ b/test/SemaCXX/expressions.cpp @@ -32,3 +32,34 @@ namespace test1 { bar(x += E_zero); // expected-error {{incompatible type}} } } + +int test2(int x) { + return x && 4; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + + return x && sizeof(int) == 4; // no warning, RHS is logical op. + return x && true; + return x && false; + return x || true; + return x || false; + + return x && (unsigned)0; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + + return x || (unsigned)1; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + + return x || 0; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + return x || 1; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + return x || -1; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + return x || 5; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + return x && 0; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + return x && 1; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + return x && -1; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + return x && 5; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + return x || (0); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + return x || (1); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + return x || (-1); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + return x || (5); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}} + return x && (0); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + return x && (1); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + return x && (-1); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} + return x && (5); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}} +} diff --git a/test/SemaCXX/switch.cpp b/test/SemaCXX/switch.cpp index 3882a1f952..8a8cf33049 100644 --- a/test/SemaCXX/switch.cpp +++ b/test/SemaCXX/switch.cpp @@ -8,7 +8,7 @@ void test() { } int n = 3; - switch (n && 1) { // expected-warning {{bool}} + switch (n && true) { // expected-warning {{bool}} case 1: break; } -- 2.40.0