From patchwork Fri Mar 3 17:50:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 65984 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 74FF938582B7 for ; Fri, 3 Mar 2023 17:51:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 74FF938582B7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677865878; bh=jTJM0s0LhM/mxBx7/iXJpgUlYN+JOXfyWNtMW/CNiZ4=; 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=p9gFFvthaBpz+0uCXWv8r0j2hLVpssssL6OY4BdXRlK1ZGbn9ovpjveyPwTy1Xcmt dErUAYi3jgGQxH3cAvqtbRF3uaUROu/0XdLB0ujQ6U2FpEtGrOYIChppaljtNGmRkH jVb2sD828QhJkok6bj05SlyUU3yS9gAkPryfJoDk= 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 44F153858CDB for ; Fri, 3 Mar 2023 17:50:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 44F153858CDB Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-465-6WtAxHe7PgmIb_0FZME_Hg-1; Fri, 03 Mar 2023 12:50:14 -0500 X-MC-Unique: 6WtAxHe7PgmIb_0FZME_Hg-1 Received: by mail-qt1-f197.google.com with SMTP id t22-20020ac86a16000000b003bd1c0f74cfso1863926qtr.20 for ; Fri, 03 Mar 2023 09:50:14 -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=jTJM0s0LhM/mxBx7/iXJpgUlYN+JOXfyWNtMW/CNiZ4=; b=Wqh4JRUbVpsNXgV8RvpCauyP4U0EfEXvssJlRoCRO3KZyOXF24jcMI0O6GyKOqfAss 7rRCJ8P9ipuTDihmJ3Hz8cKcfC7K6maDtjTDsXQ0jf6i76rRopJ3Mj/qGpCyWu0tpAQi ToDep7RtvC7OsPC4Flsbi1+R82tLptdi965/47ktJwUt8QcQa7S60nxOm2SqkNBS8YWg wvAjhW+iH6Y/D1PYtLncVdY8Q6KyCLjQHOKUNV4NZSVbAtLmvuMGjz4JNtqtK0KhdqEm i4hE1ZbmDSUDTvjEzkyWcK1zCe4Fm7MsRhLmbJGleKgbaCPyp0cifERTQkuGRQ753wDQ 4GJA== X-Gm-Message-State: AO0yUKV2nZdhM1DqaxUAs0RlTBwBX0375tW9Tv2CGsXuy5Yq3UXbVNBi yJA1mNmHXnYq4zI+itb8T2Fc/uHAOd37qTNIIT5DGrYOhKkJxrfB0/RPch5NwIrmyOPl1DhHK9J IV46fGyOLnVFUJK7ceSdLjOg= X-Received: by 2002:ac8:5e0e:0:b0:3bf:a8cf:3096 with SMTP id h14-20020ac85e0e000000b003bfa8cf3096mr4971060qtx.33.1677865813698; Fri, 03 Mar 2023 09:50:13 -0800 (PST) X-Google-Smtp-Source: AK7set8E5A3dBEo79hBMN6EVX0tfepp7mrUtAp69blcjVPm3jsdkigixkq3in9KhwQGyHWoGubbgWw== X-Received: by 2002:ac8:5e0e:0:b0:3bf:a8cf:3096 with SMTP id h14-20020ac85e0e000000b003bfa8cf3096mr4971013qtx.33.1677865813272; Fri, 03 Mar 2023 09:50:13 -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 i188-20020a37b8c5000000b0073b45004754sm2162609qkf.34.2023.03.03.09.50.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 09:50:12 -0800 (PST) Date: Fri, 3 Mar 2023 12:50:11 -0500 To: Jason Merrill Cc: GCC Patches Subject: [PATCH v5] c++: -Wdangling-reference with reference wrapper [PR107532] Message-ID: References: <0024ebdb-ecf2-d841-0e73-7063e4dcb9fe@redhat.com> <61a408eb-d9a8-6c3b-4d7a-367fe0d3997e@redhat.com> <81d1c004-82c2-096f-d2f0-48e365dd954a@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.6 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 Fri, Mar 03, 2023 at 11:25:06AM -0500, Jason Merrill wrote: > On 3/2/23 16:24, Marek Polacek wrote: > > On Wed, Mar 01, 2023 at 04:53:23PM -0500, Jason Merrill wrote: > > > > @@ -13791,12 +13830,39 @@ std_pair_ref_ref_p (tree t) > > > > const int& y = (f(1), 42); // NULL_TREE > > > > const int& z = f(f(1)); // f(f(1)) > > > > - EXPR is the initializer. */ > > > > + EXPR is the initializer. If ARG_P is true, we're processing an argument > > > > + to a function; the point is to distinguish between, for example, > > > > + > > > > + Ref::inner (&TARGET_EXPR ) > > > > + > > > > + where we shouldn't warn, and > > > > + > > > > + Ref::inner (&TARGET_EXPR )>) > > > > + > > > > + where we should warn (Ref is a reference_like_class_p so we see through > > > > + it. */ > > > > static tree > > > > -do_warn_dangling_reference (tree expr) > > > > +do_warn_dangling_reference (tree expr, bool arg_p) > > > > { > > > > STRIP_NOPS (expr); > > > > + if (TREE_CODE (expr) == ADDR_EXPR) > > > > + expr = TREE_OPERAND (expr, 0); > > > > > > I think if we move this here, we also need to check that expr before > > > STRIP_NOPS had REFERENCE_TYPE. OK with that change. > > > > Sorry but I don't think I can do that. There can be CONVERT_EXPRs > > that need to be stripped, whether arg_p or !arg_p. For example, we can get > > (const int *) f ((const int &) &TARGET_EXPR >) > > for > > const int& r5 = (42, f(10)); > > I meant that we only want to strip ADDR_EXPR if 'expr' at the start of the > function had REFERENCE_TYPE, corresponding to > > > /* Check that this argument initializes a reference, except > > for > > the argument initializing the object of a member function. */ > > if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) > > && !TYPE_REF_P (TREE_TYPE (arg))) > > continue; > > above the code for stripping an ADDR_EXPR from an argument that your patch > removes. I see. > If the original expr is a pointer rather than a reference, we don't want to > complain about it pointing to a temporary. Ug, I can't make it work. When we recurse, I can no longer check fndecl. How about just moving the stripping back where it was? -- >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. This patch adjusts do_warn_dangling_reference so that we look through reference wrapper classes (meaning, has a reference member and a constructor taking the same reference type, or is std::reference_wrapper or std::ranges::ref_view) and don't warn for them, supposing that the member function returns a reference to a non-temporary object. PR c++/107532 gcc/cp/ChangeLog: * call.cc (reference_like_class_p): New. (do_warn_dangling_reference): Add new bool parameter. See through reference_like_class_p. gcc/testsuite/ChangeLog: * g++.dg/warn/Wdangling-reference8.C: New test. * g++.dg/warn/Wdangling-reference9.C: New test. --- gcc/cp/call.cc | 92 ++++++++++++++++--- .../g++.dg/warn/Wdangling-reference8.C | 77 ++++++++++++++++ .../g++.dg/warn/Wdangling-reference9.C | 21 +++++ 3 files changed, 176 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference8.C create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference9.C base-commit: ce1c99f1ccd7b1229a4f8531d6b6de6cf571a9ef diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 048b2b052f8..62536573633 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -13779,6 +13779,45 @@ std_pair_ref_ref_p (tree t) return true; } +/* Return true if a class CTYPE is either std::reference_wrapper or + std::ref_view, or a reference wrapper class. We consider a class + a reference wrapper class if it has a reference member and a + constructor taking the same reference type. */ + +static bool +reference_like_class_p (tree ctype) +{ + tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype)); + if (decl_in_std_namespace_p (tdecl)) + { + tree name = DECL_NAME (tdecl); + return (name + && (id_equal (name, "reference_wrapper") + || id_equal (name, "ref_view"))); + } + for (tree fields = TYPE_FIELDS (ctype); + 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 (ctype))) + { + tree args = FUNCTION_FIRST_USER_PARMTYPE (fn); + if (args + && same_type_p (TREE_VALUE (args), type) + && TREE_CHAIN (args) == void_list_node) + return true; + } + } + return false; +} + /* Helper for maybe_warn_dangling_reference to find a problematic CALL_EXPR that initializes the LHS (and at least one of its arguments represents a temporary, as outlined in maybe_warn_dangling_reference), or NULL_TREE @@ -13793,12 +13832,37 @@ std_pair_ref_ref_p (tree t) const int& y = (f(1), 42); // NULL_TREE const int& z = f(f(1)); // f(f(1)) - EXPR is the initializer. */ + EXPR is the initializer. If ARG_P is true, we're processing an argument + to a function; the point is to distinguish between, for example, + + Ref::inner (&TARGET_EXPR ) + + where we shouldn't warn, and + + Ref::inner (&TARGET_EXPR )>) + + where we should warn (Ref is a reference_like_class_p so we see through + it. */ static tree -do_warn_dangling_reference (tree expr) +do_warn_dangling_reference (tree expr, bool arg_p) { STRIP_NOPS (expr); + + if (arg_p && expr_represents_temporary_p (expr)) + { + /* An attempt to reduce the number of -Wdangling-reference + false positives concerning reference wrappers (c++/107532). + Here we suppose that a member function of such a reference + wrapper class returns a reference to a non-temporary object. */ + tree e = expr; + while (handled_component_p (e)) + e = TREE_OPERAND (e, 0); + e = TREE_TYPE (e); + if (!CLASS_TYPE_P (e) || !reference_like_class_p (e)) + return expr; + } + switch (TREE_CODE (expr)) { case CALL_EXPR: @@ -13831,7 +13895,8 @@ do_warn_dangling_reference (tree expr) std::pair v = std::minmax(1, 2); which also creates a dangling reference, because std::minmax returns std::pair(b, a). */ - if (!(TYPE_REF_OBJ_P (rettype) || std_pair_ref_ref_p (rettype))) + if (!arg_p + && (!(TYPE_REF_OBJ_P (rettype) || std_pair_ref_ref_p (rettype)))) return NULL_TREE; /* Here we're looking to see if any of the arguments is a temporary @@ -13844,14 +13909,13 @@ do_warn_dangling_reference (tree expr) if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) && !TYPE_REF_P (TREE_TYPE (arg))) continue; - /* It could also be another call taking a temporary and returning - it and initializing this reference parameter. */ - if (do_warn_dangling_reference (arg)) - return expr; STRIP_NOPS (arg); if (TREE_CODE (arg) == ADDR_EXPR) arg = TREE_OPERAND (arg, 0); - if (expr_represents_temporary_p (arg)) + /* Recurse to see if the argument is a temporary. It could also + be another call taking a temporary and returning it and + initializing this reference parameter. */ + if (do_warn_dangling_reference (arg, /*arg_p=*/true)) return expr; /* Don't warn about member function like: std::any a(...); @@ -13868,15 +13932,15 @@ do_warn_dangling_reference (tree expr) return NULL_TREE; } case COMPOUND_EXPR: - return do_warn_dangling_reference (TREE_OPERAND (expr, 1)); + return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p); case COND_EXPR: - if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1))) + if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p)) return t; - return do_warn_dangling_reference (TREE_OPERAND (expr, 2)); + return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p); case PAREN_EXPR: - return do_warn_dangling_reference (TREE_OPERAND (expr, 0)); + return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p); case TARGET_EXPR: - return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr)); + return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p); default: return NULL_TREE; } @@ -13919,7 +13983,7 @@ maybe_warn_dangling_reference (const_tree decl, tree init) = make_temp_override (global_dc->dc_warn_system_headers, (!in_system_header_at (DECL_SOURCE_LOCATION (decl)) || global_dc->dc_warn_system_headers)); - if (tree call = do_warn_dangling_reference (init)) + if (tree call = do_warn_dangling_reference (init, /*arg_p=*/false)) { auto_diagnostic_group d; if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference, 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..330de1fd05d --- /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(); // { dg-warning "dangling reference" } + 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(); // { dg-warning "dangling reference" } + (void) d2; + const auto& d3 = A().b.get().get().inner(); // { dg-warning "dangling reference" } + (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(); // { dg-warning "dangling reference" } + (void) d6; + Plane p; + Ref r(p); + const auto& d7 = r.inner(); + (void) d7; + const auto& d8 = Ref(p).inner(); + (void) d8; +} diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference9.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference9.C new file mode 100644 index 00000000000..9ad83f7365e --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference9.C @@ -0,0 +1,21 @@ +// PR c++/107532 +// { dg-do compile { target c++11 } } +// { dg-options "-Wdangling-reference" } + +#include + +struct X { int n; }; + +struct S { + std::reference_wrapper wrapit() const { return x; } + X x; +}; + +void +g (const S& s) +{ + const auto& a1 = s.wrapit().get(); + (void) a1; + const auto& a2 = S().wrapit().get(); // { dg-warning "dangling reference" } + (void) a2; +}