From patchwork Thu Jan 19 01:13:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 63375 Return-Path: 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 9FA303858434 for ; Thu, 19 Jan 2023 01:14:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9FA303858434 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674090844; bh=goBDbduGASJtighvhqGYhna0XD4VNC294AUGt4JOZkc=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=gSfycScIAjpP04fqGcN+13axy9U1OjbPCx2U7zNEmx1EkUEZxvQ+DW6QbuDhp3ONW Zu3Ws86PVVFtqjeGjPO4trP0tpkhouqh9+P1CB6vegRa8AjHhuksMWuOd6CeH8sBt1 L+oIMdnNGnNlmFZdGVrETqvCiNiNM1eBJFOZpRTg= 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 ED5A93858D28 for ; Thu, 19 Jan 2023 01:13:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ED5A93858D28 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-275-8dNmaoz5PFORGUyqWmzBgQ-1; Wed, 18 Jan 2023 20:13:32 -0500 X-MC-Unique: 8dNmaoz5PFORGUyqWmzBgQ-1 Received: by mail-qk1-f198.google.com with SMTP id j10-20020a05620a288a00b0070630ecfd9bso507489qkp.20 for ; Wed, 18 Jan 2023 17:13:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=goBDbduGASJtighvhqGYhna0XD4VNC294AUGt4JOZkc=; b=lhin67caP2IEI6WGIe8NTfwr1vPulAeUer3G5j9CxB8XajXVv7Aq/1ra/yKcCSJQT7 spJKRo0utaCHoxIOuzskls3MHAwVA7F1D7ifQcCj4axEPB5gRN7Ke0n58gjI79JX50OM ovtAH+cp+QbZfVf0QbjoifERQRoTZwnr3ya38IcOrnFociG2z+fTyfILPWfqLNs2eVn1 cntO/wYamm4i2/iW4qw9Dln5wXI95CYJnVvZvda37xMuiMEn3s+PnTLwBkGN8VkfMy1u BfZP+ln+6YGSs+ra1K63tz0+QYRJIqKBwQohHZmSjcEUb95WX+uN+1e/RvJZRszL8RxZ aZqg== X-Gm-Message-State: AFqh2kp5yy9mFwFZdhvNq3JLtqTnR+STR1mIujPon1ki6Ksmf7BEYhRJ oZF0mX8KTUSwVK+P+pAqBVQgSvkkHLtX8KFicX8GMvzrGI2BCgAHV6LwH/xLBFLqqz2Y9mDxS3o b9l4Ag+PECtKq3U4fPg== X-Received: by 2002:ac8:470a:0:b0:3b6:4217:3db2 with SMTP id f10-20020ac8470a000000b003b642173db2mr11067969qtp.13.1674090811440; Wed, 18 Jan 2023 17:13:31 -0800 (PST) X-Google-Smtp-Source: AMrXdXvzG3gEojWZGfeM2m+crINThDl1zWLLtRUz4Y7WhbaUfrBF0eheb5jeil7WwrJuuRk5mEoZ1w== X-Received: by 2002:ac8:470a:0:b0:3b6:4217:3db2 with SMTP id f10-20020ac8470a000000b003b642173db2mr11067940qtp.13.1674090811121; Wed, 18 Jan 2023 17:13:31 -0800 (PST) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id t39-20020a05622a182700b003b63b20314esm3629834qtc.57.2023.01.18.17.13.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Jan 2023 17:13:30 -0800 (PST) Date: Wed, 18 Jan 2023 20:13:28 -0500 To: Jason Merrill Cc: GCC Patches Subject: [PATCH v2] c++: -Wdangling-reference with reference wrapper [PR107532] Message-ID: References: <20230118175200.365397-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.9 (2022-11-12) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP 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-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" On Wed, Jan 18, 2023 at 04:07:59PM -0500, Jason Merrill wrote: > On 1/18/23 12:52, Marek Polacek wrote: > > Here, -Wdangling-reference triggers where it probably shouldn't, causing > > some grief. The code in question uses a reference wrapper with a member > > function returning a reference to a subobject of a non-temporary object: > > > > const Plane & meta = fm.planes().inner(); > > > > I've tried a few approaches, e.g., checking that the member function's > > return type is the same as the type of the enclosing class (which is > > the case for member functions returning *this), but that then breaks > > Wdangling-reference4.C with std::optional. > > > > So I figured that perhaps we want to look at the object we're invoking > > the member function(s) on and see if that is a temporary, as in, don't > > warn about > > > > const Plane & meta = fm.planes().inner(); > > > > but do warn about > > > > const Plane & meta = FrameMetadata().planes().inner(); > > > > It's ugly, but better than asking users to add #pragmas into their code. > > Hmm, that doesn't seem right; the former is only OK because Ref is in fact a > reference-like type. If planes() returned a class that held data, we would > want to warn. Sure, it's always some kind of tradeoff with warnings :/. > In this case, we might recognize the reference-like class because it has a > reference member and a constructor taking the same reference type. That occurred to me too, but then I found out that std::reference_wrapper actually uses T*, not T&, as you say. But here's a patch to do that (I hope). > That wouldn't help with std::reference_wrapper or std::ref_view because they > have pointer members instead of references, but perhaps loosening the check > to include that case would make sense? Sorry, I don't understand what you mean by loosening the check. I could hardcode std::reference_wrapper and std::ref_view but I don't think that's what you meant. Surely I cannot _not_ warn for any class that contains a T*. Here's the patch so that we have some actual code to discuss... Thanks. -- >8 -- Here, -Wdangling-reference triggers where it probably shouldn't, causing some grief. The code in question uses a reference wrapper with a member function returning a reference to a subobject of a non-temporary object: const Plane & meta = fm.planes().inner(); I've tried a few approaches, e.g., checking that the member function's return type is the same as the type of the enclosing class (which is the case for member functions returning *this), but that then breaks Wdangling-reference4.C with std::optional. Perhaps we want to look at the member function's enclosing class to see if it's a reference wrapper class (meaning, has a reference member and a constructor taking the same reference type) and don't warn if so, supposing that the member function returns a reference to a non-temporary object. It's ugly, but better than asking users to add #pragmas into their code. PR c++/107532 gcc/cp/ChangeLog: * call.cc (do_warn_dangling_reference): Don't warn when the member function comes from a reference wrapper class. gcc/testsuite/ChangeLog: * g++.dg/warn/Wdangling-reference8.C: New test. --- gcc/cp/call.cc | 32 ++++++++ .../g++.dg/warn/Wdangling-reference8.C | 77 +++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference8.C base-commit: 8e2c6e7b426b6c9c13076208b2e176d4aa1432f1 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 0780b5840a3..b0670a21240 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -13832,6 +13832,38 @@ do_warn_dangling_reference (tree expr) if (!(TYPE_REF_OBJ_P (rettype) || std_pair_ref_ref_p (rettype))) return NULL_TREE; + /* An attempt to reduce the number of -Wdangling-reference + false positives concerning reference wrappers (c++/107532). + If the enclosing class is a reference-like class, that is, has + a reference member and a constructor taking the same reference type, + we suppose that the member function is returning a reference + to a non-temporary object. */ + if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) + && !DECL_OVERLOADED_OPERATOR_P (fndecl)) + { + tree ctx = CP_DECL_CONTEXT (fndecl); + for (tree fields = TYPE_FIELDS (ctx); + fields; + fields = DECL_CHAIN (fields)) + { + if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields)) + continue; + tree type = TREE_TYPE (fields); + if (!TYPE_REF_P (type)) + continue; + /* OK, the field is a reference member. Do we have + a constructor taking its type? */ + for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (ctx))) + { + tree args = FUNCTION_FIRST_USER_PARMTYPE (fn); + if (args + && same_type_p (TREE_VALUE (args), type) + && TREE_CHAIN (args) == void_list_node) + return NULL_TREE; + } + } + } + /* Here we're looking to see if any of the arguments is a temporary initializing a reference parameter. */ for (int i = 0; i < call_expr_nargs (expr); ++i) diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference8.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference8.C new file mode 100644 index 00000000000..4d585891fae --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference8.C @@ -0,0 +1,77 @@ +// PR c++/107532 +// { dg-do compile { target c++11 } } +// { dg-options "-Wdangling-reference" } + +struct Plane { unsigned int bytesused; }; + +// Passes a reference through. Does not change lifetime. +template +struct Ref { + const T& i_; + Ref(const T & i) : i_(i) {} + const T & inner(); +}; + +struct FrameMetadata { + Ref planes() const { return p_; } + + Plane p_; +}; + +void bar(const Plane & meta); +void foo(const FrameMetadata & fm) +{ + const Plane & meta = fm.planes().inner(); + bar(meta); + const Plane & meta2 = FrameMetadata().planes().inner(); + bar(meta2); +} + +struct S { + const S& self () { return *this; } +} s; + +const S& r1 = s.self(); +const S& r2 = S().self(); // { dg-warning "dangling reference" } + +struct D { +}; + +struct C { + D d; + Ref get() const { return d; } +}; + +struct B { + C c; + const C& get() const { return c; } + B(); +}; + +struct A { + B b; + const B& get() const { return b; } +}; + +void +g (const A& a) +{ + const auto& d1 = a.get().get().get().inner(); + (void) d1; + const auto& d2 = A().get().get().get().inner(); + (void) d2; + const auto& d3 = A().b.get().get().inner(); + (void) d3; + const auto& d4 = a.b.get().get().inner(); + (void) d4; + const auto& d5 = a.b.c.get().inner(); + (void) d5; + const auto& d6 = A().b.c.get().inner(); + (void) d6; + Plane p; + Ref r(p); + const auto& d7 = r.inner(); + (void) d7; + const auto& d8 = Ref(p).inner(); + (void) d8; +}