From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27940 invoked by alias); 23 Apr 2015 15:34:35 -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 27922 invoked by uid 89); 23 Apr 2015 15:34:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-wi0-f178.google.com Received: from mail-wi0-f178.google.com (HELO mail-wi0-f178.google.com) (209.85.212.178) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Thu, 23 Apr 2015 15:34:33 +0000 Received: by wicmx19 with SMTP id mx19so15285013wic.1 for ; Thu, 23 Apr 2015 08:34:30 -0700 (PDT) X-Received: by 10.194.239.99 with SMTP id vr3mr6524688wjc.151.1429803270854; Thu, 23 Apr 2015 08:34:30 -0700 (PDT) Received: from [192.168.1.3] (ip-213-49-236-25.dsl.scarlet.be. [213.49.236.25]) by mx.google.com with ESMTPSA id hy7sm12639012wjb.1.2015.04.23.08.34.29 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Apr 2015 08:34:30 -0700 (PDT) Message-ID: <55391105.90804@gmail.com> Date: Thu, 23 Apr 2015 15:34:00 -0000 From: =?UTF-8?B?TWFudWVsIEzDs3Blei1JYsOhw7Fleg==?= User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: Jason Merrill <"jason"@redhat.com\">, Patrick Palka , gcc-patches@gcc.gnu.org Subject: Re: Re: [PATCH] Emit -Waddress warnings for comparing address of reference against NULL References: <1429583806-29631-1-git-send-email-patrick@parcs.ath.cx> <55390BC4.9000403@redhat.com> In-Reply-To: <55390BC4.9000403@redhat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-SW-Source: 2015-04/txt/msg01427.txt.bz2 On 04/23/2015 05:12 PM, Jason Merrill wrote: > On 04/20/2015 10:36 PM, Patrick Palka wrote: > Implementation is pretty straightforward. The only catch is that the > middle-end doesn't actually assume that REFERENCE_TYPEs are non-NULL so > code like > > int &a = *(int *)0; > if (&a != 0) > > will warn that &a will never be NULL yet the middle-end will fold the > conditional to false instead of true anyway. But I guess that's not a > big deal. Is this actually correct? Is it because of undefined behavior? It seems also weird we do not warn directly for '*(int *)0' in the C/C++ FE. >> + if (decl_with_nonnull_addr_p (inner)) > > Using decl_with_nonnull_addr_p doesn't make sense for reference variables, > since we're using their pointer value rather than their address. Is an extra check needed at all (can &reference ever be false)? > >> + warning_at (location, >> + OPT_Waddress, >> + "the address of reference %qD may be assumed to " >> + "always evaluate to %", >> + inner); > > Perhaps "the compiler can assume that the address of reference %qD will always > evaluate to %"? The discussion (and perhaps the patch) at https://gcc.gnu.org/bugzilla/PR65168 may be relevant. Jonathan suggests to match what we say for: /home/manuel/test.c:3:21: warning: the address of ‘i’ will always evaluate as ‘true’ [-Waddress] int i; bool b = !&i; ^ I think this case requires a slightly different text because the address may not evaluate to 'true' and also because it is not actually the address of the reference but the object bounded to the reference. Clang says: warning: reference cannot be bound to dereferenced null pointer in well-defined C++ code; pointer may be assumed to always convert to true [-Wundefined-bool-conversion] which is in my opinion even less clear. The testcases: int fii(int *p) { int &r=*p; return !&r; } int fii(int p) { int &r=p; return !&r; } should also generate the same warning in my opinion. Cheers, Manuel.