public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/107488] [13 Regression] -Werror=dangling-reference false positives in cppunit
Date: Thu, 03 Nov 2022 19:46:49 +0000	[thread overview]
Message-ID: <bug-107488-4-Bf87rJrAjg@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-107488-4@http.gcc.gnu.org/bugzilla/>

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.

  parent reply	other threads:[~2022-11-03 19:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-01  8:02 [Bug c++/107488] New: " 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 [this message]
2022-11-03 19:48 ` mpolacek at gcc dot gnu.org
2022-11-04  8:14 ` slyfox at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107488-4-Bf87rJrAjg@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).