From patchwork Thu Feb 11 16:25:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 581961 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 39E7E140BAF for ; Fri, 12 Feb 2016 03:26:06 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=Wqy8e1TU; 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:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=fbAjZiU+KKmC2s2tSqVPi10sE5f2U S9tAI/eoXDghQhcd34XwrGAnJvumWztVZ+6wNB5dJk4a5q+XTlIjgvFx+roSe3rH 3+zpIA6KQKqSFs+0mRt8B9yfnONRUcc8aE6dcXNy9wOer/7mqSTCL4vphuAL0gt0 KHa+1miKdHZTO8= 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:reply-to:mime-version :content-type; s=default; bh=fXzCbHwUZxdt7/FlnuBuHixIu4A=; b=Wqy 8e1TUqAJegK7X9h12Zoz5enFIZSFeg+C2RUUnYaNQrGuixlLyXQtlGhriit0QO4A qLCDdMglwFOF5Jqecs6lsX/zYJFQ5Z6UxROK81QV1vfJjSnJrdznBQkQmqwKADbm sZ5xGV3lAfrXyhBN0Br/ddEoEj6HlsbwLGztuR/E= Received: (qmail 82125 invoked by alias); 11 Feb 2016 16:25:37 -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 82036 invoked by uid 89); 11 Feb 2016 16:25:36 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= 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; Thu, 11 Feb 2016 16:25:34 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 784A8219F; Thu, 11 Feb 2016 16:25:32 +0000 (UTC) Received: from tucnak.zalov.cz ([10.3.113.11]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u1BGPUp0022864 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 11 Feb 2016 11:25:32 -0500 Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id u1BGPSHB007548; Thu, 11 Feb 2016 17:25:29 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id u1BGPQiS007546; Thu, 11 Feb 2016 17:25:26 +0100 Date: Thu, 11 Feb 2016 17:25:26 +0100 From: Jakub Jelinek To: "Joseph S. Myers" , Marek Polacek , Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [C/C++ PATCH] Fix a -Waddress regression (PR c/69768) Message-ID: <20160211162526.GD3017@tucnak.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-IsSubscribed: yes Hi! Until recently, integer_zerop would STRIP_NOPS, so that change regressed some cases in the -Waddress warning that affect some real-world code. The following patch re-adds stripping of nops for that case (but doesn't try to fold it further). With this patch, for C and C++98 we get the same behavior as in 5.x, for C++11 and C++14 we warn even about the weirdo cases where (10 - 10) or (&e - &e) is cast to a pointer (but, e.g. for C we have been and are warning about the latter only). I'd say warning for such weird cases is fine. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2016-02-11 Jakub Jelinek PR c/69768 * c-typeck.c (parser_build_binary_op): Strip nops from integer_zerop arguments for -Waddress warning. * typeck.c (cp_build_binary_op): Strip nops from integer_zerop arguments for -Waddress warning. Fix up formatting. * c-c++-common/Waddress-1.c: New test. Jakub --- gcc/c/c-typeck.c.jj 2016-01-27 20:30:18.000000000 +0100 +++ gcc/c/c-typeck.c 2016-02-11 17:17:07.470222811 +0100 @@ -3597,8 +3597,10 @@ parser_build_binary_op (location_t locat of testing for equality or inequality of a string literal with NULL. */ if (code == EQ_EXPR || code == NE_EXPR) { - if ((code1 == STRING_CST && !integer_zerop (arg2.value)) - || (code2 == STRING_CST && !integer_zerop (arg1.value))) + if ((code1 == STRING_CST + && !integer_zerop (tree_strip_nop_conversions (arg2.value))) + || (code2 == STRING_CST + && !integer_zerop (tree_strip_nop_conversions (arg1.value)))) warning_at (location, OPT_Waddress, "comparison with string literal results in unspecified behavior"); } --- gcc/cp/typeck.c.jj 2016-02-11 10:50:58.000000000 +0100 +++ gcc/cp/typeck.c 2016-02-11 17:17:07.473222771 +0100 @@ -4487,9 +4487,12 @@ cp_build_binary_op (location_t location, warning (OPT_Wfloat_equal, "comparing floating point with == or != is unsafe"); if ((complain & tf_warning) - && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1)) - || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0)))) - warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour"); + && ((TREE_CODE (orig_op0) == STRING_CST + && !integer_zerop (tree_strip_nop_conversions (op1))) + || (TREE_CODE (orig_op1) == STRING_CST + && !integer_zerop (tree_strip_nop_conversions (op0))))) + warning (OPT_Waddress, "comparison with string literal results " + "in unspecified behaviour"); build_type = boolean_type_node; if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE --- gcc/testsuite/c-c++-common/Waddress-1.c.jj 2016-02-11 14:30:30.677090429 +0100 +++ gcc/testsuite/c-c++-common/Waddress-1.c 2016-02-11 17:18:13.970329341 +0100 @@ -0,0 +1,15 @@ +/* PR c/69768 */ +/* { dg-do compile } */ +/* { dg-options "-Waddress" } */ + +static int e; + +int +foo () +{ + return "foo1" != (void *) 0 /* { dg-bogus "comparison with string literal results in unspecified behaviou?r" } */ + && "foo2" != (const char *) ((void *) 0) /* { dg-bogus "comparison with string literal results in unspecified behaviou?r" } */ + && "foo3" != (const char *) ((void *) (10 - 10)) /* { dg-warning "comparison with string literal results in unspecified behaviou?r" "" { target c++11 } } */ + && "foo4" != (const char *) ((void *) (&e - &e)) /* { dg-warning "comparison with string literal results in unspecified behaviou?r" "" { target { c || c++11 } } } */ + && "foo5" != "foo6"; /* { dg-warning "comparison with string literal results in unspecified behaviou?r" } */ +}