public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Quash -Wdangling-reference for member operator* [PR107488]
@ 2022-11-01 22:06 Marek Polacek
  2022-11-03 18:54 ` Jason Merrill
  0 siblings, 1 reply; 3+ messages in thread
From: Marek Polacek @ 2022-11-01 22:06 UTC (permalink / raw)
  To: Jason Merrill, GCC Patches

-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.  :/

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

	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<int> v = ...;
+		std::vector<int>::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 <vector>
+
+int
+do_sum (std::vector<int>& v)
+{
+  int sum = 0;
+
+  std::vector<int>::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
-- 
2.38.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] c++: Quash -Wdangling-reference for member operator* [PR107488]
  2022-11-01 22:06 [PATCH] c++: Quash -Wdangling-reference for member operator* [PR107488] Marek Polacek
@ 2022-11-03 18:54 ` Jason Merrill
  2022-11-03 19:02   ` Marek Polacek
  0 siblings, 1 reply; 3+ messages in thread
From: Jason Merrill @ 2022-11-03 18:54 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 11/1/22 18:06, Marek Polacek wrote:
> -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.  :/

> 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<int> v = ...;
> +		std::vector<int>::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 <vector>
> +
> +int
> +do_sum (std::vector<int>& v)
> +{
> +  int sum = 0;
> +
> +  std::vector<int>::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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] c++: Quash -Wdangling-reference for member operator* [PR107488]
  2022-11-03 18:54 ` Jason Merrill
@ 2022-11-03 19:02   ` Marek Polacek
  0 siblings, 0 replies; 3+ messages in thread
From: Marek Polacek @ 2022-11-03 19:02 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Patches

On Thu, Nov 03, 2022 at 02:54:12PM -0400, Jason Merrill wrote:
> On 11/1/22 18:06, Marek Polacek wrote:
> > -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.  :/
> 
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> 
> OK.

Thanks.
 
> 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.

Yup; amusingly I noticed that too while working with the reduced version
of the testcase, which I deliberately didn't end up using, and which
reduced to 'int&' rather than 'const int&'.

> Maybe also if the function returns a non-const lvalue reference.

Ok.  Expect a patch soon.

Marek


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-11-03 19:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-01 22:06 [PATCH] c++: Quash -Wdangling-reference for member operator* [PR107488] Marek Polacek
2022-11-03 18:54 ` Jason Merrill
2022-11-03 19:02   ` Marek Polacek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).