From patchwork Sat Jun 20 00:02:06 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Maltsev X-Patchwork-Id: 486940 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id CA75C1401DE for ; Sat, 20 Jun 2015 10:02:26 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=cgrW+aLg; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:content-type; q=dns; s=default; b=alVpW/u/jc4h2RTLfChgpRqomUxtxZjo6r0xExhysqT rjQabRXpO3UFzxVx2MX4Su7BWpod2aMUILg+iYdGXmFGJ0TvHztNN1OQPuhNZWix VE1Va/0UvOlnV/hp6pLGA51g1xv/GwDDtfDkHef7etPtuIDJBw6Nw09tRYY5EWag = DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:content-type; s=default; bh=WGpzOfojX8UMdI+DhAARX8b0wg8=; b=cgrW+aLg8zfDCeb9T 3TX/vLGH3VoV374i1DmWLW9stuNGJfmtEUEILx8xxA/Q1lYokRGhxSbXzd1EWjty luXoT1+cGRgtMivlzkVcqQ/StXqhbe4TPZJmTGYCvAbA5ra+8Ge/eplVrgShKUU5 kA6Yoz/VhW6hseazIegM9yOXHU= Received: (qmail 101576 invoked by alias); 20 Jun 2015 00:02:20 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 101567 invoked by uid 89); 20 Jun 2015 00:02:19 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.4 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-HELO: fencepost.gnu.org Received: from fencepost.gnu.org (HELO fencepost.gnu.org) (208.118.235.10) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Sat, 20 Jun 2015 00:02:17 +0000 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39523) by fencepost.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1Z66Eh-00076g-Go for gcc-patches@gnu.org; Fri, 19 Jun 2015 20:02:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z66Ed-00070R-M2 for gcc-patches@gnu.org; Fri, 19 Jun 2015 20:02:14 -0400 Received: from mail-la0-x233.google.com ([2a00:1450:4010:c03::233]:34331) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z66Ed-00070K-8a for gcc-patches@gnu.org; Fri, 19 Jun 2015 20:02:11 -0400 Received: by labbc20 with SMTP id bc20so82726066lab.1 for ; Fri, 19 Jun 2015 17:02:09 -0700 (PDT) X-Received: by 10.112.50.66 with SMTP id a2mr11744258lbo.68.1434758529711; Fri, 19 Jun 2015 17:02:09 -0700 (PDT) Received: from [192.168.123.200] ([77.41.78.126]) by mx.google.com with ESMTPSA id c5sm2838851lbd.49.2015.06.19.17.02.08 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Jun 2015 17:02:08 -0700 (PDT) Message-ID: <5584AD7E.7040603@gmail.com> Date: Sat, 20 Jun 2015 03:02:06 +0300 From: Mikhail Maltsev User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: gcc-patches CC: Marek Polacek , Jason Merrill Subject: [C/C++ PATCH] PR c++/66572. Fix Wlogical-op false positive X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4010:c03::233 X-IsSubscribed: yes Hi. In current implementation we avoid giving "logical and/or of equal expressions" warning for literal constant operands. The attached patch fixes the check to make it treat const-qualified VAR_DECLs with constant initializers the same way. Bootstrapped and regtested on x86_64-linux. OK for trunk? diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index dc2bf00..38c7be9 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -1766,9 +1766,12 @@ warn_logical_operator (location_t location, enum tree_code code, tree type, return; } - /* We do not warn for constants because they are typical of macro - expansions that test for features. */ - if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right)) + /* We do not warn for literal constants because they are typical of macro + expansions that test for features. Likewise, we do not warn for + const-qualified and constexpr variables which are initialized by constant + expressions, because they can come from e.g. or similar user + code. */ + if (TREE_CONSTANT (op_left) || TREE_CONSTANT (op_right)) return; /* This warning only makes sense with logical operands. */ diff --git a/gcc/testsuite/c-c++-common/Wlogical-op-2.c b/gcc/testsuite/c-c++-common/Wlogical-op-2.c new file mode 100644 index 0000000..47f5c28 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wlogical-op-2.c @@ -0,0 +1,24 @@ +/* PR c++/66572 */ +/* { dg-do compile } */ +/* { dg-options "-Wlogical-op" } */ + +#ifndef __cplusplus +# define bool _Bool +# define true 1 +# define false 0 +#endif + +void +no_warn () +{ + const bool cst_a = true; + const bool cst_b = false; + + if (cst_a || cst_b) {} + if (cst_a && cst_b) {} + if (true && cst_a) {} + if (true || cst_a) {} + if (false && cst_a) {} + if (false || cst_a) {} +} + diff --git a/gcc/testsuite/g++.dg/warn/Wlogical-op-2.C b/gcc/testsuite/g++.dg/warn/Wlogical-op-2.C new file mode 100644 index 0000000..252592c6 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wlogical-op-2.C @@ -0,0 +1,59 @@ +// PR c++/66572 +// { dg-do compile } +// { dg-options "-Wlogical-op" } + +#if __cplusplus >= 201103L +# define CONSTEXPR constexpr +#else +# define CONSTEXPR const +#endif + +struct true_type +{ + static CONSTEXPR bool value = true; +}; + +struct false_type +{ + static CONSTEXPR bool value = true; +}; + +template +struct is_unsigned : false_type { }; + +template<> +struct is_unsigned : true_type { }; + +template +bool both_are_unsigned () +{ + return is_unsigned ::value && is_unsigned ::value; +} + +template +bool one_of_is_unsigned () +{ + return is_unsigned ::value || is_unsigned ::value; +} + +void +foo () +{ + both_are_unsigned (); + both_are_unsigned (); + both_are_unsigned (); + one_of_is_unsigned (); + one_of_is_unsigned (); + one_of_is_unsigned (); +} + +void +bar (const int parm_a) +{ + const bool a = parm_a; + if (a && a) {} /* { dg-warning "logical .and. of equal expressions" } */ + if (a || a) {} /* { dg-warning "logical .or. of equal expressions" } */ + if (parm_a && parm_a) {} /* { dg-warning "logical .and. of equal expressions" } */ + if (parm_a || parm_a) {} /* { dg-warning "logical .or. of equal expressions" } */ +} +