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 CF5D7385B519 for ; Tue, 21 Mar 2023 14:31:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CF5D7385B519 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679409066; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4Hw+qcpbDG3Fkto1QGa1hWVvB+/7x8upVCFDhtspupI=; b=MZX1AqolCUwsbnRn2BGEOSOf1N2J/e9L2vrVZT9oVVSBIatMGyhkknM1ylATZtNUVVAOLS 3/rysKmYQeEjIJuoBsEV0IXvcqPx2Lh8ENsoiw87G+MQ5EQX5am+yU3GYKLkp6HeeA8wE+ h08dfWp8IvUpuKCkD6kkpk6nPpWWc0g= 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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-269-CWDW9g7UMHeqZd6uJox_Ag-1; Tue, 21 Mar 2023 10:31:05 -0400 X-MC-Unique: CWDW9g7UMHeqZd6uJox_Ag-1 Received: by mail-qt1-f198.google.com with SMTP id x5-20020a05622a000500b003e259c363f9so3220539qtw.22 for ; Tue, 21 Mar 2023 07:31:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679409064; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4Hw+qcpbDG3Fkto1QGa1hWVvB+/7x8upVCFDhtspupI=; b=vYJtohY0wBs+vJ3+NTd+I2hE0lqFG2pC9dkoU7UuxsQjo1oElpGbRPiipoNS+KdJ6T 6+R3XrRi2YNZFAKzStU+nQ6273pxpLCNYChdZHiCwZU7YnCDa5SFyjMJPgaeKyExQS6Z 5vfyf0V1tCYvNwKNSLLo+e89c+yqgS45/7sBLgGmwzvKEsfk/oEvXEft2QazYlErT7eD KYkTDbfUb15zLZU/477kbG2xPkfV1zuOl2LgxBcwGuue5rwmeMProeJjqH2xkarxDy9T Gdx9RmeG/O9ypHwgaNDOek0cErABNP64bdmL8kzrN4/9ei7zjGmBbmFDPJ0MWz1P9qFQ 450A== X-Gm-Message-State: AO0yUKUzbhuvokxZ6jYoIkjwWCWuTxwLZFz5EsbBMkj+4wvOiWCd1Ltf uBUj0E+DSkdFjypQEbT4yY17z+BdJAVtx1l/YZfVVgG8RXf+Vee6lMs8ZCNH7khw8KrsIHaO1Di Vgpv4Ojp+v+duCC7k13hx3q3bcA== X-Received: by 2002:ac8:5a4a:0:b0:3dd:a703:f7df with SMTP id o10-20020ac85a4a000000b003dda703f7dfmr182513qta.2.1679409063766; Tue, 21 Mar 2023 07:31:03 -0700 (PDT) X-Google-Smtp-Source: AK7set94sV4LsmXNW+AQztWz5SXe4aE0UbaqUOFvyunK7vcuCAsE+F/0NbshfKEc1/tad6UYe+pAfQ== X-Received: by 2002:ac8:5a4a:0:b0:3dd:a703:f7df with SMTP id o10-20020ac85a4a000000b003dda703f7dfmr182472qta.2.1679409063349; Tue, 21 Mar 2023 07:31:03 -0700 (PDT) Received: from [192.168.1.108] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id x23-20020ac87317000000b003b63b8df24asm1689574qto.36.2023.03.21.07.31.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Mar 2023 07:31:02 -0700 (PDT) Message-ID: <7ee531fc-fef5-df24-4b46-ce163c2ea66e@redhat.com> Date: Tue, 21 Mar 2023 10:31:01 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [PATCH v2] c++: further -Wdangling-reference refinement [PR107532] To: Marek Polacek Cc: GCC Patches References: <20230317202908.42800-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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,KAM_SHORT,NICE_REPLY_A,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 List-Id: On 3/20/23 18:06, Marek Polacek wrote: > On Sat, Mar 18, 2023 at 08:35:36AM -0400, Jason Merrill wrote: >> On 3/17/23 16:29, Marek Polacek wrote: >>> Based on , >>> it seems like we should treat *any* class with a reference member >>> as a reference wrapper. This simplifies the code so I'm happy to >>> make that change. >>> >>> The patch, however, does not suppress the warning in >>> >>> int i = 42; >>> auto const& v = std::get<0>(std::tuple(i)); >> >> Why not? tuple has an int& member, doesn't it? Do we need to look >> into bases as well? > > Indeed. I don't know why I didn't do it right away; it's really not that > complicated: > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK. > -- >8 -- > Based on , > it seems like we should treat *any* class with a reference member > as a reference wrapper. To suppress the warning in > > int i = 42; > auto const& v = std::get<0>(std::tuple(i)); > > we have to look into base classes as well. For std::tuple, this means > that we have to check the _Head_base subobject, which is a non-direct > base class of std::tuple. So I've employed a DFS walk. > > PR c++/107532 > > gcc/cp/ChangeLog: > > * call.cc (class_has_reference_member_p): New. > (class_has_reference_member_p_r): New. > (reference_like_class_p): Don't look for a specific constructor. > Use a DFS walk with class_has_reference_member_p_r. > > gcc/testsuite/ChangeLog: > > * g++.dg/warn/Wdangling-reference11.C: New test. > * g++.dg/warn/Wdangling-reference12.C: New test. > --- > gcc/cp/call.cc | 63 +++++++++++-------- > .../g++.dg/warn/Wdangling-reference11.C | 23 +++++++ > .../g++.dg/warn/Wdangling-reference12.C | 12 ++++ > 3 files changed, 72 insertions(+), 26 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference11.C > create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference12.C > > diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc > index c52a09b9be2..429170e43ea 100644 > --- a/gcc/cp/call.cc > +++ b/gcc/cp/call.cc > @@ -13783,8 +13783,31 @@ std_pair_ref_ref_p (tree t) > > /* 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. */ > + a reference wrapper class if it has a reference member. We no > + longer check that it has a constructor taking the same reference type > + since that approach still generated too many false positives. */ > + > +static bool > +class_has_reference_member_p (tree t) > +{ > + for (tree fields = TYPE_FIELDS (t); > + fields; > + fields = DECL_CHAIN (fields)) > + if (TREE_CODE (fields) == FIELD_DECL > + && !DECL_ARTIFICIAL (fields) > + && TYPE_REF_P (TREE_TYPE (fields))) > + return true; > + return false; > +} > + > +/* A wrapper for the above suitable as a callback for dfs_walk_once. */ > + > +static tree > +class_has_reference_member_p_r (tree binfo, void *) > +{ > + return (class_has_reference_member_p (BINFO_TYPE (binfo)) > + ? integer_one_node : NULL_TREE); > +} > > static bool > reference_like_class_p (tree ctype) > @@ -13800,31 +13823,19 @@ reference_like_class_p (tree ctype) > if (decl_in_std_namespace_p (tdecl)) > { > tree name = DECL_NAME (tdecl); > - return (name > - && (id_equal (name, "reference_wrapper") > - || id_equal (name, "span") > - || 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; > - } > + if (name > + && (id_equal (name, "reference_wrapper") > + || id_equal (name, "span") > + || id_equal (name, "ref_view"))) > + return true; > } > + > + /* Some classes, such as std::tuple, have the reference member in its > + (non-direct) base class. */ > + if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r, > + nullptr, nullptr)) > + return true; > + > return false; > } > > diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference11.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference11.C > new file mode 100644 > index 00000000000..667618e7196 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference11.C > @@ -0,0 +1,23 @@ > +// PR c++/107532 > +// { dg-do compile { target c++11 } } > +// { dg-options "-Wdangling-reference" } > + > +struct R > +{ > + int& r; > + int& get() { return r; } > + int&& rget() { return static_cast(r); } > +}; > + > +int main() > +{ > + int i = 42; > + int& l = R{i}.get(); // { dg-bogus "dangling reference" } > + int const& cl = R{i}.get(); // { dg-bogus "dangling reference" } > + int&& r = R{i}.rget(); // { dg-bogus "dangling reference" } > + int const&& cr = R{i}.rget(); // { dg-bogus "dangling reference" } > + (void) l; > + (void) r; > + (void) cr; > + (void) cl; > +} > diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference12.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference12.C > new file mode 100644 > index 00000000000..85e01f01a50 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference12.C > @@ -0,0 +1,12 @@ > +// PR c++/107532 > +// { dg-do compile { target c++11 } } > +// { dg-options "-Wdangling-reference" } > + > +#include > + > +int main() > +{ > + int i = 42; > + auto const& v = std::get<0>(std::tuple(i)); // { dg-bogus "dangling reference" } > + (void) v; > +} > > base-commit: a226590fefb35ed66adf73d85cefe49048a78ab8