From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 ; 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 ; 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 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v2] c++: suppress -Waddress warnings with *_cast [PR105569] Message-ID: References: <20220516170619.358111-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 25 May 2022 20:25:53 -0000 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(&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(&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(&ref); // { dg-warning "comparing address of reference .ref. and NULL" } + bool b4 = nullptr == static_cast(&ref); // { dg-warning "comparing address of reference .ref. and NULL" } + if (dynamic_cast(&ref)) // { dg-warning "comparing address of reference .ref. and NULL" } + { + } + if (static_cast(&ref)) // { dg-warning "comparing address of reference .ref. and NULL" } + { + } + + auto ptr = dynamic_cast(&ref); + bool b2 = ptr == nullptr; + + C* cobject = new C(); + C &cref = *cobject; + + bool b3 = nullptr == dynamic_cast(&cref); +} base-commit: 34970d08c6297e12f3f9117b6ac19fb2de522e24 -- 2.36.1