* [Bug c++/107488] [13 Regression] -Werror=dangling-reference false positives in cppunit
2022-11-01 8:02 [Bug c++/107488] New: [13 Regression] -Werror=dangling-reference false positives in cppunit slyfox at gcc dot gnu.org
@ 2022-11-01 15:56 ` pinskia at gcc dot gnu.org
2022-11-01 20:50 ` mpolacek at gcc dot gnu.org
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-01 15:56 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107488
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Target Milestone|--- |13.0
Keywords| |diagnostic
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/107488] [13 Regression] -Werror=dangling-reference false positives in cppunit
2022-11-01 8:02 [Bug c++/107488] New: [13 Regression] -Werror=dangling-reference false positives in cppunit slyfox at gcc dot gnu.org
2022-11-01 15:56 ` [Bug c++/107488] " pinskia at gcc dot gnu.org
@ 2022-11-01 20:50 ` mpolacek at gcc dot gnu.org
2022-11-03 19:46 ` cvs-commit at gcc dot gnu.org
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-11-01 20:50 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107488
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Last reconfirmed| |2022-11-01
CC| |mpolacek at gcc dot gnu.org
Assignee|unassigned at gcc dot gnu.org |mpolacek at gcc dot gnu.org
Ever confirmed|0 |1
Status|UNCONFIRMED |ASSIGNED
--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I figure this warning is a false positive since "const int &attribute" refers
to one of the int elements of the vector v, not to a temporary object.
I think the warning should probably be disabled for operator*, since that
returns a reference but doesn't return its parameter:
reference
operator*() const _GLIBCXX_NOEXCEPT
{ return *_M_current; }
Patch:
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -13467,7 +13467,11 @@ 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)))
+ /* XXX */
+ || (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
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/107488] [13 Regression] -Werror=dangling-reference false positives in cppunit
2022-11-01 8:02 [Bug c++/107488] New: [13 Regression] -Werror=dangling-reference false positives in cppunit slyfox at gcc dot gnu.org
2022-11-01 15:56 ` [Bug c++/107488] " pinskia at gcc dot gnu.org
2022-11-01 20:50 ` mpolacek at gcc dot gnu.org
@ 2022-11-03 19:46 ` cvs-commit at gcc dot gnu.org
2022-11-03 19:48 ` mpolacek at gcc dot gnu.org
2022-11-04 8:14 ` slyfox at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-11-03 19:46 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107488
--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:
https://gcc.gnu.org/g:32a06ce38a38bf37db468f0e6c83520fcc221534
commit r13-3642-g32a06ce38a38bf37db468f0e6c83520fcc221534
Author: Marek Polacek <polacek@redhat.com>
Date: Tue Nov 1 17:05:52 2022 -0400
c++: Quash -Wdangling-reference for member operator* [PR107488]
-Wdangling-reference complains here:
std::vector<int> v = ...;
std::vector<int>::const_iterator it = v.begin();
while (it != v.end()) {
const int &r = *it++; // warning
}
because it sees a call to
__gnu_cxx::__normal_iterator<const int*, std::vector<int> >::operator*
which returns a reference and its argument is a TARGET_EXPR representing
the result of
__gnu_cxx::__normal_iterator<const int*, std::vector<int> >::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.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/107488] [13 Regression] -Werror=dangling-reference false positives in cppunit
2022-11-01 8:02 [Bug c++/107488] New: [13 Regression] -Werror=dangling-reference false positives in cppunit slyfox at gcc dot gnu.org
` (2 preceding siblings ...)
2022-11-03 19:46 ` cvs-commit at gcc dot gnu.org
@ 2022-11-03 19:48 ` mpolacek at gcc dot gnu.org
2022-11-04 8:14 ` slyfox at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-11-03 19:48 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107488
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|ASSIGNED |RESOLVED
Resolution|--- |FIXED
--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Should be fixed, thanks for the report.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/107488] [13 Regression] -Werror=dangling-reference false positives in cppunit
2022-11-01 8:02 [Bug c++/107488] New: [13 Regression] -Werror=dangling-reference false positives in cppunit slyfox at gcc dot gnu.org
` (3 preceding siblings ...)
2022-11-03 19:48 ` mpolacek at gcc dot gnu.org
@ 2022-11-04 8:14 ` slyfox at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: slyfox at gcc dot gnu.org @ 2022-11-04 8:14 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107488
--- Comment #4 from Sergei Trofimovich <slyfox at gcc dot gnu.org> ---
Thank you! cppunit-1.15.1 now builds fine with your fix.
^ permalink raw reply [flat|nested] 6+ messages in thread