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 714BD3858D39 for ; Thu, 3 Nov 2022 18:54:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 714BD3858D39 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=1667501657; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HkMncxOHk4Vle81RqKJxfe13lkchHmAbCTxHqa3xPGE=; b=KAse0iTws4R/aTELtuvKNfupAF4rbrIgc0JJhuc0uG1RzEvNDl4S8/9yK/0YznyINZUlNY XjTKzx9I/e637cdXeAUIRcgVzAtlz5TO5IbiKTc0GcIf9JbQiMXn4BeRnV0n5hKWPvjjhR CpOJ2A773MvDp6KuzKtqKTE86FxLiUU= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-529-rKvq7F-5MAeSXNyBd776Sg-1; Thu, 03 Nov 2022 14:54:16 -0400 X-MC-Unique: rKvq7F-5MAeSXNyBd776Sg-1 Received: by mail-qk1-f200.google.com with SMTP id x2-20020a05620a448200b006fa7dad5c1cso1457216qkp.10 for ; Thu, 03 Nov 2022 11:54:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references: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=HkMncxOHk4Vle81RqKJxfe13lkchHmAbCTxHqa3xPGE=; b=SRAOZIKXFw0Lo2M2+eXE+azWzS9dW6ubUjfYlfwQ0BlPFDbK/ke4SrC7qjVJ0iaB3T UyskPco1yebVP9qrGmbRP/fN+D2I7/ZfLIu+l5pbsrNRW/aeDcJqGSkbVm4jWzgpXYzT xHi7EFtuFU3gL03qQagnSYY9j2UEtTxYWpOkPrEMYrqUacIQDGBJsG6wpXHCZWaS/Myn b0DZy4F8u24V8ZQBxlWf9sh+tyZQ1dlRO4C0+4dtGjXbO5SOK/aUe4aNmxh+wnv1kjRz /ZGStSmiNQ1N+F7pkzUML/eCSTs9wBsQ793czs8udgYH0VlMuhcv47f5V3LABxdTNeeG UsdQ== X-Gm-Message-State: ACrzQf3J+yZSstIYMMToqjdOGyBWAZzZnGAxTmkW6Asus1PIFBL0u50t 5wFn/vnS7csrjwWY91X+LDXm2Sn1S8lWvdApfbuCEb1Ti+7zNogHOJbQS7wYvXWS23sTs3ix38T XWXgneDWWisaGxl1+3g== X-Received: by 2002:a05:622a:6011:b0:3a5:3388:40df with SMTP id he17-20020a05622a601100b003a5338840dfmr14585334qtb.603.1667501654080; Thu, 03 Nov 2022 11:54:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM75D0QNaleHARSPPEimiQNT54y5ijy2Hr0v5SSXmhG3/sr+nt7AjFe14TH/JGfEROfqGn72UQ== X-Received: by 2002:a05:622a:6011:b0:3a5:3388:40df with SMTP id he17-20020a05622a601100b003a5338840dfmr14585320qtb.603.1667501653774; Thu, 03 Nov 2022 11:54:13 -0700 (PDT) Received: from [192.168.1.101] (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 i6-20020a05620a248600b006ecfb2c86d3sm1208150qkn.130.2022.11.03.11.54.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Nov 2022 11:54:13 -0700 (PDT) Message-ID: <7db5d94d-f75e-9387-bb43-3ccec6f7fdb3@redhat.com> Date: Thu, 3 Nov 2022 14:54:12 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: [PATCH] c++: Quash -Wdangling-reference for member operator* [PR107488] To: Marek Polacek , GCC Patches References: <20221101220652.588178-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20221101220652.588178-1-polacek@redhat.com> 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=-13.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,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 11/1/22 18:06, Marek Polacek wrote: > -Wdangling-reference complains here: > > std::vector v = ...; > std::vector::const_iterator it = v.begin(); > while (it != v.end()) { > const int &r = *it++; // warning > } > > because it sees a call to > __gnu_cxx::__normal_iterator >::operator* > which returns a reference and its argument is a TARGET_EXPR representing > the result of > __gnu_cxx::__normal_iterator >::operator++ > But 'r' above refers to one of the int elements of the vector 'v', not > to a temporary object. Therefore the warning is a false positive. > > I suppose code like the above is relatively common (the warning broke > cppunit-1.15.1 and a few other projects), so presumably it makes sense > to suppress the warning when it comes to member operator*. In this case > it's defined as > > reference > operator*() const _GLIBCXX_NOEXCEPT > { return *_M_current; } > > and I'm guessing a lot of member operator* are like that, at least when > it comes to iterators. I've looked at _Fwd_list_iterator, > _Fwd_list_const_iterator, __shared_ptr_access, _Deque_iterator, > istream_iterator, etc, and they're all like that, so adding #pragmas > would be quite tedious. :/ > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK. It also occurred to me that we should avoid warning if the reference we're initializing is a non-const lvalue reference, which can't bind to a temporary. Maybe also if the function returns a non-const lvalue reference. > PR c++/107488 > > gcc/cp/ChangeLog: > > * call.cc (do_warn_dangling_reference): Quash -Wdangling-reference > for member operator*. > > gcc/testsuite/ChangeLog: > > * g++.dg/warn/Wdangling-reference5.C: New test. > --- > gcc/cp/call.cc | 12 +++++++++- > .../g++.dg/warn/Wdangling-reference5.C | 22 +++++++++++++++++++ > 2 files changed, 33 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference5.C > > diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc > index c7c7a122045..2c0fa37f53a 100644 > --- a/gcc/cp/call.cc > +++ b/gcc/cp/call.cc > @@ -13467,7 +13467,17 @@ do_warn_dangling_reference (tree expr) > can be e.g. > const int& z = std::min({1, 2, 3, 4, 5, 6, 7}); > which doesn't dangle: std::min here returns an int. */ > - || !TYPE_REF_OBJ_P (TREE_TYPE (TREE_TYPE (fndecl)))) > + || !TYPE_REF_OBJ_P (TREE_TYPE (TREE_TYPE (fndecl))) > + /* Don't emit a false positive for: > + std::vector v = ...; > + std::vector::const_iterator it = v.begin(); > + const int &r = *it++; > + because R refers to one of the int elements of V, not to > + a temporary object. Member operator* may return a reference > + but probably not to one of its arguments. */ > + || (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) > + && DECL_OVERLOADED_OPERATOR_P (fndecl) > + && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))) > return NULL_TREE; > > /* Here we're looking to see if any of the arguments is a temporary > diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference5.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference5.C > new file mode 100644 > index 00000000000..59b5538aee5 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference5.C > @@ -0,0 +1,22 @@ > +// PR c++/107488 > +// { dg-do compile } > +// { dg-options "-Wdangling-reference" } > + > +#include > + > +int > +do_sum (std::vector& v) > +{ > + int sum = 0; > + > + std::vector::const_iterator it = v.begin(); > + while (it != v.end()) > + { > + // R refers to one of the int elements of V, not to a temporary > + // object, so no dangling reference here. > + const int &r = *it++; // { dg-bogus "dangling reference" } > + sum += r; > + } > + > + return sum; > +} > > base-commit: 2b0e81d5cc2f7e1d773f6c502bd65b097f392675