Message ID | Yo6Qyrklo3DwgmHY@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 05596384F02D for <patchwork@sourceware.org>; Wed, 25 May 2022 20:27:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 05596384F02D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1653510468; bh=+JtcK/RryqhNhIxqicTuaJMjBnRBbQYFG4gEWExN8uQ=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=CsA6C/rq9OOacVaJzSyO0Fj4rHkUyYlNt9/FD2r0kbl8yXSR7kpMXo7UBfAUN5ymi zM6PFyAgYxLYTyZMhYvF6+668YEIY+wuSwupLPuEZWWMG9n5wj8S282Kyke8pfv+DT i0xjB+QqaKkKi55MYXu5zXSMAn3pbsK+rEllI2IA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 815933830668 for <gcc-patches@gcc.gnu.org>; Wed, 25 May 2022 20:25:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 815933830668 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-195-3gXvqXtaOnGdegXNDFVxDw-1; Wed, 25 May 2022 16:25:49 -0400 X-MC-Unique: 3gXvqXtaOnGdegXNDFVxDw-1 Received: by mail-qt1-f198.google.com with SMTP id q13-20020a05622a04cd00b002f3c0e197afso17112837qtx.0 for <gcc-patches@gcc.gnu.org>; Wed, 25 May 2022 13:25:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=+JtcK/RryqhNhIxqicTuaJMjBnRBbQYFG4gEWExN8uQ=; b=qOX88+1jsdsTEKPuRtMZIULssXP3ePtK40ygTKMJNGsjAbPiRmjR8V/mXBTWJXWVwr eOThW5fTyj+/O75G9iN0S8eAXxmdXH1vYv2YoTlUg0TlO0+7kNT5HV9YGC3koKHi6AtB Ho3D6+/zQijdmdISK5VP+R2hSJ3+jUBNnImpX6QfbLjB7NCdO8les2l8mLVPXVl2yPJ/ /OmSaqwf+6LiJH1ceRPcYuQ8leAI45TSGsh0k9x17CsRXeXNef40hKTjZwamM31Tuxz6 +VjaPdkoH+Mu27In9wiXFLBJXA9CnhLMBrwynGQQwQY+paAI1+gAKTTNesKeHyuzfOAc FvLA== X-Gm-Message-State: AOAM533vxdVBEqHSaU5aRsx/BhVoEWdq68jC4rEhtkIIClELKdjfA11n bkMTYfqa9A4hnX70DpiuxITFly8Js1DleoybCt/3ofsyndzjpvtRagDEwn/JTh8axZJfFdiqeuo AF/VtwdG7hoyH0c/L+A== X-Received: by 2002:a05:6214:2401:b0:462:61dc:ed3d with SMTP id fv1-20020a056214240100b0046261dced3dmr4506674qvb.112.1653510348607; Wed, 25 May 2022 13:25:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBSaY0wBzUhpERwC0dkcgcnh3IFFswHsh1sfKl/TB9KfHQTD95YiAPxcw9VFRa2ronlMe8hQ== X-Received: by 2002:a05:6214:2401:b0:462:61dc:ed3d with SMTP id fv1-20020a056214240100b0046261dced3dmr4506657qvb.112.1653510348337; Wed, 25 May 2022 13:25:48 -0700 (PDT) Received: from redhat.com ([2601:184:4780:4310::9979]) by smtp.gmail.com with ESMTPSA id s187-20020a375ec4000000b0069fc13ce243sm1789394qkb.116.2022.05.25.13.25.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 May 2022 13:25:47 -0700 (PDT) Date: Wed, 25 May 2022 16:25:46 -0400 To: Jason Merrill <jason@redhat.com> Subject: [PATCH v2] c++: suppress -Waddress warnings with *_cast [PR105569] Message-ID: <Yo6Qyrklo3DwgmHY@redhat.com> References: <20220516170619.358111-1-polacek@redhat.com> <dbab82d3-7f00-309d-e2d0-6ba450a28a35@redhat.com> MIME-Version: 1.0 In-Reply-To: <dbab82d3-7f00-309d-e2d0-6ba450a28a35@redhat.com> User-Agent: Mutt/2.2.1 (2022-02-19) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Marek Polacek via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Marek Polacek <polacek@redhat.com> Cc: GCC Patches <gcc-patches@gcc.gnu.org> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
[v2] c++: suppress -Waddress warnings with *_cast [PR105569]
|
|
Commit Message
Marek Polacek
May 25, 2022, 8:25 p.m. UTC
On Wed, May 18, 2022 at 09:43:47AM -0400, Jason Merrill wrote: > On 5/16/22 13:06, Marek Polacek wrote: > > dynamic_cast can legally return nullptr, so I don't think it's helpful > > for -Waddress to warn for > > > > if (dynamic_cast<A*>(&ref)) > > // ... > > > > More generally, it's likely not useful to warn for the artificial > > POINTER_PLUS_EXPRs created by build_base_path. > > Normally the POINTER_PLUS_EXPR is guarded by if (nonnull). But > build_base_path isn't adding that guard in this case because the operand is > known to be a reference, which cannot be null > (http://eel.is/c++draft/dcl.ref#5). So a warning is indicated for this > testcase, though it would be good to give a more informative one ("comparing > address of reference to null"). Ah, got it. How about this patch instead? Thanks, -- >8 -- This patch improves the diagnostic for -Waddress when it warns for if (dynamic_cast<A*>(&ref)) // ... where 'ref' is a reference, which cannot be null. In particular, it changes warning: comparing the result of pointer addition '(((A*)ref) + ((sizetype)(*(long int*)((& ref)->B::_vptr.B + -24))))' and NULL to warning: comparing address of reference 'ref' to null PR c++/105569 gcc/cp/ChangeLog: * typeck.cc (warn_for_null_address): Improve the warning when the POINTER_PLUS_EXPR's base is of reference type. gcc/testsuite/ChangeLog: * g++.dg/warn/Waddress-9.C: New test. --- gcc/cp/typeck.cc | 12 ++++++++-- gcc/testsuite/g++.dg/warn/Waddress-9.C | 31 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Waddress-9.C base-commit: 34970d08c6297e12f3f9117b6ac19fb2de522e24
Comments
On 5/25/22 16:25, Marek Polacek wrote: > On Wed, May 18, 2022 at 09:43:47AM -0400, Jason Merrill wrote: >> On 5/16/22 13:06, Marek Polacek wrote: >>> dynamic_cast can legally return nullptr, so I don't think it's helpful >>> for -Waddress to warn for >>> >>> if (dynamic_cast<A*>(&ref)) >>> // ... >>> >>> More generally, it's likely not useful to warn for the artificial >>> POINTER_PLUS_EXPRs created by build_base_path. >> >> Normally the POINTER_PLUS_EXPR is guarded by if (nonnull). But >> build_base_path isn't adding that guard in this case because the operand is >> known to be a reference, which cannot be null >> (http://eel.is/c++draft/dcl.ref#5). So a warning is indicated for this >> testcase, though it would be good to give a more informative one ("comparing >> address of reference to null"). > > Ah, got it. How about this patch instead? Thanks, Do we also warn about plain &ref == nullptr? > -- >8 -- > This patch improves the diagnostic for -Waddress when it warns for > > if (dynamic_cast<A*>(&ref)) > // ... > > where 'ref' is a reference, which cannot be null. In particular, it > changes > warning: comparing the result of pointer addition '(((A*)ref) + ((sizetype)(*(long int*)((& ref)->B::_vptr.B + -24))))' and NULL > to > warning: comparing address of reference 'ref' to null > > PR c++/105569 > > gcc/cp/ChangeLog: > > * typeck.cc (warn_for_null_address): Improve the warning when > the POINTER_PLUS_EXPR's base is of reference type. > > gcc/testsuite/ChangeLog: > > * g++.dg/warn/Waddress-9.C: New test. > --- > gcc/cp/typeck.cc | 12 ++++++++-- > gcc/testsuite/g++.dg/warn/Waddress-9.C | 31 ++++++++++++++++++++++++++ > 2 files changed, 41 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/warn/Waddress-9.C > > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > index 385cdf4d733..0837f2484ba 100644 > --- a/gcc/cp/typeck.cc > +++ b/gcc/cp/typeck.cc > @@ -4757,8 +4757,16 @@ warn_for_null_address (location_t location, tree op, tsubst_flags_t complain) > tree off = TREE_OPERAND (cop, 1); > if (!integer_zerop (off) > && !warning_suppressed_p (cop, OPT_Waddress)) > - warning_at (location, OPT_Waddress, "comparing the result of pointer " > - "addition %qE and NULL", cop); > + { > + tree base = TREE_OPERAND (cop, 0); > + STRIP_NOPS (base); > + if (TYPE_REF_P (TREE_TYPE (base))) > + warning_at (location, OPT_Waddress, "comparing address of " > + "reference %qE and NULL", base); > + else > + warning_at (location, OPT_Waddress, "comparing the result of " > + "pointer addition %qE and NULL", cop); > + } > return; > } > else if (CONVERT_EXPR_P (op) > diff --git a/gcc/testsuite/g++.dg/warn/Waddress-9.C b/gcc/testsuite/g++.dg/warn/Waddress-9.C > new file mode 100644 > index 00000000000..a3654ff1f91 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Waddress-9.C > @@ -0,0 +1,31 @@ > +// PR c++/105569 > +// { dg-do compile { target c++11 } } > +// { dg-options -Waddress } > + > +class A {}; > + > +class B : public virtual A {}; > + > +class C : public A {}; > + > +int main() { > + B* object = new B(); > + B &ref = *object; > + > + bool b = nullptr == dynamic_cast<A*>(&ref); // { dg-warning "comparing address of reference .ref. and NULL" } > + bool b4 = nullptr == static_cast<A*>(&ref); // { dg-warning "comparing address of reference .ref. and NULL" } > + if (dynamic_cast<A*>(&ref)) // { dg-warning "comparing address of reference .ref. and NULL" } > + { > + } > + if (static_cast<A*>(&ref)) // { dg-warning "comparing address of reference .ref. and NULL" } > + { > + } > + > + auto ptr = dynamic_cast<A*>(&ref); > + bool b2 = ptr == nullptr; > + > + C* cobject = new C(); > + C &cref = *cobject; > + > + bool b3 = nullptr == dynamic_cast<A*>(&cref); > +} > > base-commit: 34970d08c6297e12f3f9117b6ac19fb2de522e24
On Thu, May 26, 2022 at 09:26:16AM -0400, Jason Merrill wrote: > On 5/25/22 16:25, Marek Polacek wrote: > > On Wed, May 18, 2022 at 09:43:47AM -0400, Jason Merrill wrote: > > > On 5/16/22 13:06, Marek Polacek wrote: > > > > dynamic_cast can legally return nullptr, so I don't think it's helpful > > > > for -Waddress to warn for > > > > > > > > if (dynamic_cast<A*>(&ref)) > > > > // ... > > > > > > > > More generally, it's likely not useful to warn for the artificial > > > > POINTER_PLUS_EXPRs created by build_base_path. > > > > > > Normally the POINTER_PLUS_EXPR is guarded by if (nonnull). But > > > build_base_path isn't adding that guard in this case because the operand is > > > known to be a reference, which cannot be null > > > (http://eel.is/c++draft/dcl.ref#5). So a warning is indicated for this > > > testcase, though it would be good to give a more informative one ("comparing > > > address of reference to null"). > > > > Ah, got it. How about this patch instead? Thanks, > > Do we also warn about plain &ref == nullptr? Yeah, with different wording: p.C:5:12: warning: the compiler can assume that the address of ‘ref’ will never be NULL [-Waddress] 5 | if (&ref == nullptr) | ~~~~~^~~~~~~~~~ warn/Walways-true-3.C tests this. > > -- >8 -- > > This patch improves the diagnostic for -Waddress when it warns for > > > > if (dynamic_cast<A*>(&ref)) > > // ... > > > > where 'ref' is a reference, which cannot be null. In particular, it > > changes > > warning: comparing the result of pointer addition '(((A*)ref) + ((sizetype)(*(long int*)((& ref)->B::_vptr.B + -24))))' and NULL > > to > > warning: comparing address of reference 'ref' to null > > > > PR c++/105569 > > > > gcc/cp/ChangeLog: > > > > * typeck.cc (warn_for_null_address): Improve the warning when > > the POINTER_PLUS_EXPR's base is of reference type. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/warn/Waddress-9.C: New test. > > --- > > gcc/cp/typeck.cc | 12 ++++++++-- > > gcc/testsuite/g++.dg/warn/Waddress-9.C | 31 ++++++++++++++++++++++++++ > > 2 files changed, 41 insertions(+), 2 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/warn/Waddress-9.C > > > > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > > index 385cdf4d733..0837f2484ba 100644 > > --- a/gcc/cp/typeck.cc > > +++ b/gcc/cp/typeck.cc > > @@ -4757,8 +4757,16 @@ warn_for_null_address (location_t location, tree op, tsubst_flags_t complain) > > tree off = TREE_OPERAND (cop, 1); > > if (!integer_zerop (off) > > && !warning_suppressed_p (cop, OPT_Waddress)) > > - warning_at (location, OPT_Waddress, "comparing the result of pointer " > > - "addition %qE and NULL", cop); > > + { > > + tree base = TREE_OPERAND (cop, 0); > > + STRIP_NOPS (base); > > + if (TYPE_REF_P (TREE_TYPE (base))) > > + warning_at (location, OPT_Waddress, "comparing address of " > > + "reference %qE and NULL", base); > > + else > > + warning_at (location, OPT_Waddress, "comparing the result of " > > + "pointer addition %qE and NULL", cop); > > + } > > return; > > } > > else if (CONVERT_EXPR_P (op) > > diff --git a/gcc/testsuite/g++.dg/warn/Waddress-9.C b/gcc/testsuite/g++.dg/warn/Waddress-9.C > > new file mode 100644 > > index 00000000000..a3654ff1f91 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/warn/Waddress-9.C > > @@ -0,0 +1,31 @@ > > +// PR c++/105569 > > +// { dg-do compile { target c++11 } } > > +// { dg-options -Waddress } > > + > > +class A {}; > > + > > +class B : public virtual A {}; > > + > > +class C : public A {}; > > + > > +int main() { > > + B* object = new B(); > > + B &ref = *object; > > + > > + bool b = nullptr == dynamic_cast<A*>(&ref); // { dg-warning "comparing address of reference .ref. and NULL" } > > + bool b4 = nullptr == static_cast<A*>(&ref); // { dg-warning "comparing address of reference .ref. and NULL" } > > + if (dynamic_cast<A*>(&ref)) // { dg-warning "comparing address of reference .ref. and NULL" } > > + { > > + } > > + if (static_cast<A*>(&ref)) // { dg-warning "comparing address of reference .ref. and NULL" } > > + { > > + } > > + > > + auto ptr = dynamic_cast<A*>(&ref); > > + bool b2 = ptr == nullptr; > > + > > + C* cobject = new C(); > > + C &cref = *cobject; > > + > > + bool b3 = nullptr == dynamic_cast<A*>(&cref); > > +} > > > > base-commit: 34970d08c6297e12f3f9117b6ac19fb2de522e24 > Marek
On 5/26/22 09:33, Marek Polacek wrote: > On Thu, May 26, 2022 at 09:26:16AM -0400, Jason Merrill wrote: >> On 5/25/22 16:25, Marek Polacek wrote: >>> On Wed, May 18, 2022 at 09:43:47AM -0400, Jason Merrill wrote: >>>> On 5/16/22 13:06, Marek Polacek wrote: >>>>> dynamic_cast can legally return nullptr, so I don't think it's helpful >>>>> for -Waddress to warn for >>>>> >>>>> if (dynamic_cast<A*>(&ref)) >>>>> // ... >>>>> >>>>> More generally, it's likely not useful to warn for the artificial >>>>> POINTER_PLUS_EXPRs created by build_base_path. >>>> >>>> Normally the POINTER_PLUS_EXPR is guarded by if (nonnull). But >>>> build_base_path isn't adding that guard in this case because the operand is >>>> known to be a reference, which cannot be null >>>> (http://eel.is/c++draft/dcl.ref#5). So a warning is indicated for this >>>> testcase, though it would be good to give a more informative one ("comparing >>>> address of reference to null"). >>> >>> Ah, got it. How about this patch instead? Thanks, >> >> Do we also warn about plain &ref == nullptr? > > Yeah, with different wording: > > p.C:5:12: warning: the compiler can assume that the address of ‘ref’ will never be NULL [-Waddress] > 5 | if (&ref == nullptr) > | ~~~~~^~~~~~~~~~ > > warn/Walways-true-3.C tests this. Let's use that wording in this case as well, then. OK with that change. >>> -- >8 -- >>> This patch improves the diagnostic for -Waddress when it warns for >>> >>> if (dynamic_cast<A*>(&ref)) >>> // ... >>> >>> where 'ref' is a reference, which cannot be null. In particular, it >>> changes >>> warning: comparing the result of pointer addition '(((A*)ref) + ((sizetype)(*(long int*)((& ref)->B::_vptr.B + -24))))' and NULL >>> to >>> warning: comparing address of reference 'ref' to null >>> >>> PR c++/105569 >>> >>> gcc/cp/ChangeLog: >>> >>> * typeck.cc (warn_for_null_address): Improve the warning when >>> the POINTER_PLUS_EXPR's base is of reference type. >>> >>> gcc/testsuite/ChangeLog: >>> >>> * g++.dg/warn/Waddress-9.C: New test. >>> --- >>> gcc/cp/typeck.cc | 12 ++++++++-- >>> gcc/testsuite/g++.dg/warn/Waddress-9.C | 31 ++++++++++++++++++++++++++ >>> 2 files changed, 41 insertions(+), 2 deletions(-) >>> create mode 100644 gcc/testsuite/g++.dg/warn/Waddress-9.C >>> >>> diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc >>> index 385cdf4d733..0837f2484ba 100644 >>> --- a/gcc/cp/typeck.cc >>> +++ b/gcc/cp/typeck.cc >>> @@ -4757,8 +4757,16 @@ warn_for_null_address (location_t location, tree op, tsubst_flags_t complain) >>> tree off = TREE_OPERAND (cop, 1); >>> if (!integer_zerop (off) >>> && !warning_suppressed_p (cop, OPT_Waddress)) >>> - warning_at (location, OPT_Waddress, "comparing the result of pointer " >>> - "addition %qE and NULL", cop); >>> + { >>> + tree base = TREE_OPERAND (cop, 0); >>> + STRIP_NOPS (base); >>> + if (TYPE_REF_P (TREE_TYPE (base))) >>> + warning_at (location, OPT_Waddress, "comparing address of " >>> + "reference %qE and NULL", base); >>> + else >>> + warning_at (location, OPT_Waddress, "comparing the result of " >>> + "pointer addition %qE and NULL", cop); >>> + } >>> return; >>> } >>> else if (CONVERT_EXPR_P (op) >>> diff --git a/gcc/testsuite/g++.dg/warn/Waddress-9.C b/gcc/testsuite/g++.dg/warn/Waddress-9.C >>> new file mode 100644 >>> index 00000000000..a3654ff1f91 >>> --- /dev/null >>> +++ b/gcc/testsuite/g++.dg/warn/Waddress-9.C >>> @@ -0,0 +1,31 @@ >>> +// PR c++/105569 >>> +// { dg-do compile { target c++11 } } >>> +// { dg-options -Waddress } >>> + >>> +class A {}; >>> + >>> +class B : public virtual A {}; >>> + >>> +class C : public A {}; >>> + >>> +int main() { >>> + B* object = new B(); >>> + B &ref = *object; >>> + >>> + bool b = nullptr == dynamic_cast<A*>(&ref); // { dg-warning "comparing address of reference .ref. and NULL" } >>> + bool b4 = nullptr == static_cast<A*>(&ref); // { dg-warning "comparing address of reference .ref. and NULL" } >>> + if (dynamic_cast<A*>(&ref)) // { dg-warning "comparing address of reference .ref. and NULL" } >>> + { >>> + } >>> + if (static_cast<A*>(&ref)) // { dg-warning "comparing address of reference .ref. and NULL" } >>> + { >>> + } >>> + >>> + auto ptr = dynamic_cast<A*>(&ref); >>> + bool b2 = ptr == nullptr; >>> + >>> + C* cobject = new C(); >>> + C &cref = *cobject; >>> + >>> + bool b3 = nullptr == dynamic_cast<A*>(&cref); >>> +} >>> >>> base-commit: 34970d08c6297e12f3f9117b6ac19fb2de522e24 >> > > Marek >
diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 385cdf4d733..0837f2484ba 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -4757,8 +4757,16 @@ warn_for_null_address (location_t location, tree op, tsubst_flags_t complain) tree off = TREE_OPERAND (cop, 1); if (!integer_zerop (off) && !warning_suppressed_p (cop, OPT_Waddress)) - warning_at (location, OPT_Waddress, "comparing the result of pointer " - "addition %qE and NULL", cop); + { + tree base = TREE_OPERAND (cop, 0); + STRIP_NOPS (base); + if (TYPE_REF_P (TREE_TYPE (base))) + warning_at (location, OPT_Waddress, "comparing address of " + "reference %qE and NULL", base); + else + warning_at (location, OPT_Waddress, "comparing the result of " + "pointer addition %qE and NULL", cop); + } return; } else if (CONVERT_EXPR_P (op) diff --git a/gcc/testsuite/g++.dg/warn/Waddress-9.C b/gcc/testsuite/g++.dg/warn/Waddress-9.C new file mode 100644 index 00000000000..a3654ff1f91 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Waddress-9.C @@ -0,0 +1,31 @@ +// PR c++/105569 +// { dg-do compile { target c++11 } } +// { dg-options -Waddress } + +class A {}; + +class B : public virtual A {}; + +class C : public A {}; + +int main() { + B* object = new B(); + B &ref = *object; + + bool b = nullptr == dynamic_cast<A*>(&ref); // { dg-warning "comparing address of reference .ref. and NULL" } + bool b4 = nullptr == static_cast<A*>(&ref); // { dg-warning "comparing address of reference .ref. and NULL" } + if (dynamic_cast<A*>(&ref)) // { dg-warning "comparing address of reference .ref. and NULL" } + { + } + if (static_cast<A*>(&ref)) // { dg-warning "comparing address of reference .ref. and NULL" } + { + } + + auto ptr = dynamic_cast<A*>(&ref); + bool b2 = ptr == nullptr; + + C* cobject = new C(); + C &cref = *cobject; + + bool b3 = nullptr == dynamic_cast<A*>(&cref); +}