From 716c0ba6bf36efd36fff564f42f1b121720cc611 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Wed, 29 Apr 2015 18:13:44 +0000 Subject: [PATCH] re PR c/64610 (No -Wbool-compare warning on "(0 != a) >= 0") PR c/64610 * c-common.c (maybe_warn_bool_compare): Warn when comparing a boolean with 0/1. * c-c++-common/Wbool-compare-1.c (fn1): Remove a few lines. * c-c++-common/Wbool-compare-2.c: New test. From-SVN: r222587 --- gcc/c-family/ChangeLog | 6 ++ gcc/c-family/c-common.c | 16 ++- gcc/testsuite/ChangeLog | 6 ++ gcc/testsuite/c-c++-common/Wbool-compare-1.c | 5 - gcc/testsuite/c-c++-common/Wbool-compare-2.c | 100 +++++++++++++++++++ 5 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wbool-compare-2.c diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 8d10487d96c..44e21f3dde6 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,9 @@ +2015-04-29 Marek Polacek + + PR c/64610 + * c-common.c (maybe_warn_bool_compare): Warn when comparing a boolean + with 0/1. + 2015-04-29 Jakub Jelinek * c-common.h (omp_clause_mask): Unconditionally define as a class. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 30456c6a552..eecbe41d4f6 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -11910,8 +11910,8 @@ maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0, if (!integer_zerop (cst) && !integer_onep (cst)) { - int sign = (TREE_CODE (op0) == INTEGER_CST) - ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst); + int sign = (TREE_CODE (op0) == INTEGER_CST + ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst)); if (code == EQ_EXPR || ((code == GT_EXPR || code == GE_EXPR) && sign < 0) || ((code == LT_EXPR || code == LE_EXPR) && sign > 0)) @@ -11921,6 +11921,18 @@ maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0, warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " "with boolean expression is always true", cst); } + else if (integer_zerop (cst) || integer_onep (cst)) + { + /* Do some magic to get the right diagnostics. */ + bool flag = TREE_CODE (op0) == INTEGER_CST; + flag = integer_zerop (cst) ? flag : !flag; + if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag)) + warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " + "with boolean expression is always true", cst); + else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag)) + warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " + "with boolean expression is always false", cst); + } } /* The C and C++ parsers both use vectors to hold function arguments. diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 7ade07c7f1d..7ff95665c48 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2015-04-29 Marek Polacek + + PR c/64610 + * c-c++-common/Wbool-compare-1.c (fn1): Remove a few lines. + * c-c++-common/Wbool-compare-2.c: New test. + 2015-04-29 Alan Lawrence PR target/65770 diff --git a/gcc/testsuite/c-c++-common/Wbool-compare-1.c b/gcc/testsuite/c-c++-common/Wbool-compare-1.c index 5b03e068210..2f435f651e6 100644 --- a/gcc/testsuite/c-c++-common/Wbool-compare-1.c +++ b/gcc/testsuite/c-c++-common/Wbool-compare-1.c @@ -72,15 +72,10 @@ fn1 (bool b) r = b == true; r = b != true; - /* Some of these don't make much sense, but we don't warn. */ - r = b < false; - r = b >= false; r = b <= false; r = b > false; r = b < true; r = b >= true; - r = b <= true; - r = b > true; } void diff --git a/gcc/testsuite/c-c++-common/Wbool-compare-2.c b/gcc/testsuite/c-c++-common/Wbool-compare-2.c new file mode 100644 index 00000000000..633032229fa --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wbool-compare-2.c @@ -0,0 +1,100 @@ +/* PR c/64610 */ +/* { dg-do compile } */ +/* { dg-options "-Wbool-compare" } */ + +#ifndef __cplusplus +# define bool _Bool +# define true 1 +# define false 0 +#endif + +extern bool foo (void); + +enum { A, B }; + +int +fn1 (bool b) +{ + int r = 0; + + r += b >= 0; /* { dg-warning "with boolean expression is always true" } */ + r += b > 0; + r += b < 0; /* { dg-warning "with boolean expression is always false" } */ + r += b <= 0; + + r += b >= 1; + r += b > 1; /* { dg-warning "with boolean expression is always false" } */ + r += b < 1; + r += b <= 1; /* { dg-warning "with boolean expression is always true" } */ + + r += foo () >= 0; /* { dg-warning "with boolean expression is always true" } */ + r += foo () > 0; + r += foo () < 0; /* { dg-warning "with boolean expression is always false" } */ + r += foo () <= 0; + + r += foo () >= 1; + r += foo () > 1; /* { dg-warning "with boolean expression is always false" } */ + r += foo () < 1; + r += foo () <= 1; /* { dg-warning "with boolean expression is always true" } */ + + r += b >= A; /* { dg-warning "with boolean expression is always true" } */ + r += b > A; + r += b < A; /* { dg-warning "with boolean expression is always false" } */ + r += b <= A; + + r += b >= B; + r += b > B; /* { dg-warning "with boolean expression is always false" } */ + r += b < B; + r += b <= B; /* { dg-warning "with boolean expression is always true" } */ + + /* Swap LHS and RHS. */ + r += 0 >= b; + r += 0 > b; /* { dg-warning "with boolean expression is always false" } */ + r += 0 < b; + r += 0 <= b; /* { dg-warning "with boolean expression is always true" } */ + + r += 1 >= b; /* { dg-warning "with boolean expression is always true" } */ + r += 1 > b; + r += 1 < b; /* { dg-warning "with boolean expression is always false" } */ + r += 1 <= b; + + r += 0 >= foo (); + r += 0 > foo (); /* { dg-warning "with boolean expression is always false" } */ + r += 0 < foo (); + r += 0 <= foo (); /* { dg-warning "with boolean expression is always true" } */ + + r += 1 >= foo (); /* { dg-warning "with boolean expression is always true" } */ + r += 1 > foo (); + r += 1 < foo (); /* { dg-warning "with boolean expression is always false" } */ + r += 1 <= foo (); + + r += A >= b; + r += A > b; /* { dg-warning "with boolean expression is always false" } */ + r += A < b; + r += A <= b; /* { dg-warning "with boolean expression is always true" } */ + + r += B >= b; /* { dg-warning "with boolean expression is always true" } */ + r += B > b; + r += B < b; /* { dg-warning "with boolean expression is always false" } */ + r += B <= b; + + return r; +} + +int +fn2 (int i, int j) +{ + int r = 0; + + r += (i == j) >= 0; /* { dg-warning "with boolean expression is always true" } */ + r += (i == j) > 0; + r += (i == j) < 0; /* { dg-warning "with boolean expression is always false" } */ + r += (i == j) <= 0; + + r += (i == j) >= 1; + r += (i == j) > 1; /* { dg-warning "with boolean expression is always false" } */ + r += (i == j) < 1; + r += (i == j) <= 1; /* { dg-warning "with boolean expression is always true" } */ + + return r; +} -- 2.30.2