From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id 27B283858401; Thu, 3 Nov 2022 19:46:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 27B283858401 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667504808; bh=Vv2VFQd0pGz+mUckmbSSxlFQGkbmc9kFpeqNpSHdCFU=; h=From:To:Subject:Date:From; b=wYMJ7JRYVuqQABr99IBLF5IMQcgSIAlaqIZOycvEiQTgAMtUG+A5Ax6SBXLraf02m ujn8nD1D6edvXZy/vz+TnL/MXJrekQZ3LyMq9N9HBEs1/qqs12367O9i7cVYidVJSf 5fr7cD5GySm4G2ns4Ih5fjabh2qHvuWhyICNGeMQ= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3642] c++: Quash -Wdangling-reference for member operator* [PR107488] X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 40c34beef620ed13c4113c893ed4335ccc1b8f92 X-Git-Newrev: 32a06ce38a38bf37db468f0e6c83520fcc221534 Message-Id: <20221103194648.27B283858401@sourceware.org> Date: Thu, 3 Nov 2022 19:46:48 +0000 (GMT) List-Id: https://gcc.gnu.org/g:32a06ce38a38bf37db468f0e6c83520fcc221534 commit r13-3642-g32a06ce38a38bf37db468f0e6c83520fcc221534 Author: Marek Polacek Date: Tue Nov 1 17:05:52 2022 -0400 c++: Quash -Wdangling-reference for member operator* [PR107488] -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. :/ 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. Diff: --- gcc/cp/call.cc | 12 +++++++++++- gcc/testsuite/g++.dg/warn/Wdangling-reference5.C | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) 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; +}