From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 66669 invoked by alias); 10 Jun 2015 02:39:40 -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 65843 invoked by uid 89); 10 Jun 2015 02:39:39 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.0 required=5.0 tests=AWL,BAYES_05,KAM_LAZY_DOMAIN_SECURITY,RCVD_IN_DNSWL_LOW autolearn=no version=3.3.2 X-HELO: mail-ob0-f179.google.com Received: from mail-ob0-f179.google.com (HELO mail-ob0-f179.google.com) (209.85.214.179) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Wed, 10 Jun 2015 02:39:38 +0000 Received: by obbqz1 with SMTP id qz1so25634448obb.3 for ; Tue, 09 Jun 2015 19:39:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=R7ZRllICQoBDliy6Sj2ozzUTZAcd/kpb5lBMUNXQVS8=; b=gLhDHFfDGCRM7KsClNUK+1uwLuJBp3/5TG+L+54mQCgcyKjMuV5y4L1ZDgTa1La6YC 7SoacAVtC5AwpRKkLc/F+o5eLQ6xKKmjyObQpAHKRnsLygubYqTXogypIVPTuHuKDXKk cRESEAmI8D1MrlfbstRjOATJO/Io8snRwp9KquidRSEFXmkgmgJCusEctlJTerBoqIgL b0Q/ANIskTWdhhjCRE1BzgDdd1HJ7LmMISLqNudQx0+MpuPcoJS3UjIv1/nqE42gyAAI lPwUz1khRhNZYguCmlQtN7sfiYtG/z7JSUg9PcgWWhBCCb83ilyoaoYWgpdEXD0Xfd2q Vxcg== X-Gm-Message-State: ALoCoQm3BsZ73hGiDzPXDKtC4QAK/drzrXib/6sQncnUFlEGBF0tv30qHiCLPkUS505MEEvwmDjM X-Received: by 10.182.81.229 with SMTP id d5mr663519oby.28.1433903975994; Tue, 09 Jun 2015 19:39:35 -0700 (PDT) MIME-Version: 1.0 Received: by 10.182.96.167 with HTTP; Tue, 9 Jun 2015 19:39:15 -0700 (PDT) In-Reply-To: References: <1430096197-29836-1-git-send-email-patrick@parcs.ath.cx> From: Patrick Palka Date: Wed, 10 Jun 2015 02:46:00 -0000 Message-ID: Subject: Re: [PATCH] Emit -Waddress warnings for comparing address of reference against NULL To: GCC Patches Cc: Jason Merrill , =?UTF-8?B?TWFudWVsIEzDs3Blei1JYsOhw7Fleg==?= , Patrick Palka Content-Type: text/plain; charset=UTF-8 X-SW-Source: 2015-06/txt/msg00721.txt.bz2 On Sun, May 3, 2015 at 5:29 PM, Patrick Palka wrote: > On Sun, Apr 26, 2015 at 8:56 PM, Patrick Palka wrote: >> Here is an updated version of the patch with, hopefully, all your >> suggestions made. I decided to add calls to STRIP_NOPS before emitting >> the warning so that we properly warn for cases where there's a cast in >> between the whole thing, e.g. >> >> if (!&(int &)a) >> >> I also added guards to emit the warnings only when the stripped operand >> is actually a decl so that we don't pass a non-decl argument to >> warning_at() which can happen in a case like >> >> if (!&(int &)*(int *)0) >> >> Does this look OK now after testing? >> >> gcc/c-family/ChangeLog: >> >> PR c++/65168 >> * c-common.c (c_common_truthvalue_conversion): Warn when >> converting an address of a reference to a truth value. >> >> gcc/cp/ChangeLog: >> >> PR c++/65168 >> * typeck.c (cp_build_binary_op): Warn when comparing an address >> of a reference against NULL. >> >> gcc/testsuite/ChangeLog: >> >> PR c++/65168 >> g++.dg/warn/Walways-true-3.C: New test. >> --- >> gcc/c-family/c-common.c | 14 ++++++++++ >> gcc/cp/typeck.c | 34 ++++++++++++++++++++++ >> gcc/testsuite/g++.dg/warn/Walways-true-3.C | 45 ++++++++++++++++++++++++++++++ >> 3 files changed, 93 insertions(+) >> create mode 100644 gcc/testsuite/g++.dg/warn/Walways-true-3.C >> >> diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c >> index 9797e17..c353c72 100644 >> --- a/gcc/c-family/c-common.c >> +++ b/gcc/c-family/c-common.c >> @@ -4806,6 +4806,20 @@ c_common_truthvalue_conversion (location_t location, tree expr) >> tree totype = TREE_TYPE (expr); >> tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0)); >> >> + if (POINTER_TYPE_P (totype) >> + && TREE_CODE (fromtype) == REFERENCE_TYPE) >> + { >> + tree inner = expr; >> + STRIP_NOPS (inner); >> + >> + if (DECL_P (inner)) >> + warning_at (location, >> + OPT_Waddress, >> + "the compiler can assume that the address of " >> + "%qD will always evaluate to %", >> + inner); >> + } >> + >> /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, >> since that affects how `default_conversion' will behave. */ >> if (TREE_CODE (totype) == REFERENCE_TYPE >> diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c >> index 91db32a..13fb401 100644 >> --- a/gcc/cp/typeck.c >> +++ b/gcc/cp/typeck.c >> @@ -4423,6 +4423,23 @@ cp_build_binary_op (location_t location, >> warning (OPT_Waddress, "the address of %qD will never be NULL", >> TREE_OPERAND (op0, 0)); >> } >> + >> + if (CONVERT_EXPR_P (op0) >> + && TREE_CODE (TREE_TYPE (TREE_OPERAND (op0, 0))) >> + == REFERENCE_TYPE) >> + { >> + tree inner_op0 = op0; >> + STRIP_NOPS (inner_op0); >> + >> + if ((complain & tf_warning) >> + && c_inhibit_evaluation_warnings == 0 >> + && !TREE_NO_WARNING (op0) >> + && DECL_P (inner_op0)) >> + warning (OPT_Waddress, >> + "the compiler can assume that the address of " >> + "%qD will never be NULL", >> + inner_op0); >> + } >> } >> else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1)) >> && null_ptr_cst_p (op0)) >> @@ -4445,6 +4462,23 @@ cp_build_binary_op (location_t location, >> warning (OPT_Waddress, "the address of %qD will never be NULL", >> TREE_OPERAND (op1, 0)); >> } >> + >> + if (CONVERT_EXPR_P (op1) >> + && TREE_CODE (TREE_TYPE (TREE_OPERAND (op1, 0))) >> + == REFERENCE_TYPE) >> + { >> + tree inner_op1 = op1; >> + STRIP_NOPS (inner_op1); >> + >> + if ((complain & tf_warning) >> + && c_inhibit_evaluation_warnings == 0 >> + && !TREE_NO_WARNING (op1) >> + && DECL_P (inner_op1)) >> + warning (OPT_Waddress, >> + "the compiler can assume that the address of " >> + "%qD will never be NULL", >> + inner_op1); >> + } >> } >> else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE) >> || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1))) >> diff --git a/gcc/testsuite/g++.dg/warn/Walways-true-3.C b/gcc/testsuite/g++.dg/warn/Walways-true-3.C >> new file mode 100644 >> index 0000000..0d13d3f >> --- /dev/null >> +++ b/gcc/testsuite/g++.dg/warn/Walways-true-3.C >> @@ -0,0 +1,45 @@ >> +// { dg-options "-Waddress" } >> + >> +void foo (void); >> + >> +int d; >> +int &c = d; >> + >> +void >> +bar (int &a) >> +{ >> + int &b = a; >> + >> + if ((int *)&a) // { dg-warning "address of" } >> + foo (); >> + >> + if (&b) // { dg-warning "address of" } >> + foo (); >> + >> + if (!&c) // { dg-warning "address of" } >> + foo (); >> + >> + if (!&(int &)(int &)a) // { dg-warning "address of" } >> + foo (); >> + >> + if (&a == 0) // { dg-warning "never be NULL" } >> + foo (); >> + >> + if (&b != 0) // { dg-warning "never be NULL" } >> + foo (); >> + >> + if (0 == &(int &)(int &)c) // { dg-warning "never be NULL" } >> + foo (); >> + >> + if (&a != (int *)0) // { dg-warning "never be NULL" } >> + foo (); >> +} >> + >> +bool >> +bar_1 (int &a) >> +{ >> + if (d == 5) >> + return &a; // { dg-warning "address of" } >> + else >> + return !&(int &)(int &)a; // { dg-warning "address of" } >> +} >> -- >> 2.4.0.rc2.31.g7c597ef >> > > Ping. Ping.