From patchwork Tue Jul 25 16:12:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 793530 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-458946-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="b0LBGQk7"; dkim-atps=neutral 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 3xH3F96F4mz9s0Z for ; Wed, 26 Jul 2017 02:13:15 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=oOifY/e5r8e3iDQGY iQlUch92PrvnjO+9lDTVwx2MiEtpIoNuJ/LPN4bctug1jEEjYjgvs50NgSGSdtNK Vm+BrNeumm26zEr38Ycvy+/iScFNDmhueRZDfjCGYimVo1zhXbp8J9VVpqjoxZZr Is+fYSUwQsKxMUGg8+jHLZWRKo= 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:date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=default; bh=jvTnOBwf4caS1F1ZD5WvWps iDqc=; b=b0LBGQk72263yywljXcvg6gbU2GAup4VMBCYSxN+4ZmQ7KArNLdlPA3 RiILOmOoafarS6hmmTTq6wF1spwHl/2Ze7158f0D2gW8WcBVOonRzi8lnttFFh8Y b5QZnaZJ5PaXKNWFOqcI5SonasHRkZsJmf5VpaEbD+4v8m8W2F3U= Received: (qmail 17861 invoked by alias); 25 Jul 2017 16:13:01 -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 17794 invoked by uid 89); 25 Jul 2017 16:12:56 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-10.9 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:1196, cs 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 ESMTP; Tue, 25 Jul 2017 16:12:55 +0000 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CBDBEC056788; Tue, 25 Jul 2017 16:12:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com CBDBEC056788 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=polacek@redhat.com Received: from redhat.com (ovpn-204-38.brq.redhat.com [10.40.204.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6582A5D969; Tue, 25 Jul 2017 16:12:52 +0000 (UTC) Date: Tue, 25 Jul 2017 18:12:48 +0200 From: Marek Polacek To: Martin Sebor Cc: David Malcolm , Eric Gallager , GCC Patches , Joseph Myers Subject: Re: c-family PATCH to improve -Wsign-compare (PR c/81417) Message-ID: <20170725161248.GH3397@redhat.com> References: <20170713163349.GE2890@redhat.com> <1499976221.7593.6.camel@redhat.com> <1499979560.7593.9.camel@redhat.com> <20170714132721.GI2890@redhat.com> <20170725094553.GB3397@redhat.com> <344d6661-ed26-bfee-c21d-9b47a84e354e@gmail.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <344d6661-ed26-bfee-c21d-9b47a84e354e@gmail.com> User-Agent: Mutt/1.8.3 (2017-05-23) On Tue, Jul 25, 2017 at 08:50:24AM -0600, Martin Sebor wrote: > How hard would it be to also suppress the warning for benign > comparisons like C++ manages to do? E.g., C warns on this > code even though there's no problem with it: > > int foo (unsigned int b) > { > const int a = 1; > return (a < b); > } The C++ FE can fold const int to it's initial value, but C's fold cannot. But there's decl_constant_value which is able to do it, so this might be just about Marek --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -11783,9 +11783,15 @@ build_binary_op (location_t location, enum tree_code code, } if (warn_sign_compare) - warn_for_sign_compare (location, orig_op0_folded, - orig_op1_folded, op0, op1, - result_type, resultcode); + { + if (VAR_P (orig_op0_folded)) + orig_op0_folded = decl_constant_value (orig_op0_folded); + if (VAR_P (orig_op1_folded)) + orig_op1_folded = decl_constant_value (orig_op1_folded); + warn_for_sign_compare (location, orig_op0_folded, + orig_op1_folded, op0, op1, + result_type, resultcode); + } if (!in_late_binary_op && !int_operands) { if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)