From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 51005 invoked by alias); 28 Apr 2015 10:49:39 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 50993 invoked by uid 89); 28 Apr 2015 10:49:38 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,SPF_HELO_PASS,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Tue, 28 Apr 2015 10:49:37 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id AABE53C223 for ; Tue, 28 Apr 2015 10:49:36 +0000 (UTC) Received: from redhat.com (ovpn-204-27.brq.redhat.com [10.40.204.27]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t3SAnXkG026127 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Tue, 28 Apr 2015 06:49:36 -0400 Date: Tue, 28 Apr 2015 12:06:00 -0000 From: Marek Polacek To: GCC Patches Subject: c-family PATCH to improve -Wbool-compare (PR c/64610) Message-ID: <20150428104932.GD11448@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-SW-Source: 2015-04/txt/msg01721.txt.bz2 This improves -Wbool-compare a bit: boolean >= 0 is always true, on the other hand boolean < 0 is always false, and similar. One feature/bug is that in C++ we don't warn for e.g. bool >= false, since the warning cares about CMP or CMP . I can fix that in a follow-up if anyone wants me to. Bootstrapped (-> GCC itself is clean)/regtested on x86_64-linux, ok for trunk? 2015-04-28 Marek Polacek 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. diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c index 9797e17..afd6edf2 100644 --- gcc/c-family/c-common.c +++ gcc/c-family/c-common.c @@ -11906,8 +11906,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)) @@ -11917,6 +11917,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 gcc/testsuite/c-c++-common/Wbool-compare-1.c gcc/testsuite/c-c++-common/Wbool-compare-1.c index 5b03e06..2f435f6 100644 --- gcc/testsuite/c-c++-common/Wbool-compare-1.c +++ 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 gcc/testsuite/c-c++-common/Wbool-compare-2.c gcc/testsuite/c-c++-common/Wbool-compare-2.c index e69de29..6330322 100644 --- gcc/testsuite/c-c++-common/Wbool-compare-2.c +++ 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; +} Marek