public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110358] New: requesting nicer suppression for Wdangling-reference
@ 2023-06-21 18:40 barry.revzin at gmail dot com
  2023-06-21 18:42 ` [Bug c++/110358] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: barry.revzin at gmail dot com @ 2023-06-21 18:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110358

            Bug ID: 110358
           Summary: requesting nicer suppression for Wdangling-reference
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

As everyone is already aware, Wdangling-reference gives false positives for
reference-semantic classes. The compiler has special cases for the ones it
knows about, but not for mine. So:

template <typename T>
struct Span {
    T* data_;
    int len_;

    [[nodiscard]] constexpr auto operator[](int n) const noexcept -> T& {
return data_[n]; }
    [[nodiscard]] constexpr auto front() const noexcept -> T& { return
data_[0]; }
    [[nodiscard]] constexpr auto back() const noexcept -> T& { return
data_[len_ - 1]; }
};

auto get() -> Span<int>;

auto f() -> int {
    int const& a = get().front(); // warning
    int const& b = get().back();  // warning
    int const& c = get()[0];      // warning

    return a + b + c;
}

The suppression for this is to #pragma around all my functions, which is a bit
of a tedious suppression, since this is two prefix lines and one postfix line
(but for libraries it's actually 4 prefix lines because we can't ignore
Wdangling-reference until gcc 13, so need to #ifdef that out).

It'd be nice if we could:

* add an attribute to a class to conditionally suppress the warning
* add an attribute to a function to conditionally suppress the warning
* both

Basically in this case to let me do something like:

template <typename T>
struct [[gnu::marek_is_awesome_but_this_warning_has_too_many_false_positives]]
Span {
   // ...
};

And I say conditional so that I can do this (because Optional<int&> wants to
suppress the warning, but Optional<int> definitely does not!)

template <typename T>
struct
[[gnu::marek_is_awesome_but_this_warning_has_too_many_false_positives(std::is_reference_v<T>)]]
Optional {
   // ...
};

Obviously I am not the best at naming.

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

* [Bug c++/110358] requesting nicer suppression for Wdangling-reference
  2023-06-21 18:40 [Bug c++/110358] New: requesting nicer suppression for Wdangling-reference barry.revzin at gmail dot com
@ 2023-06-21 18:42 ` pinskia at gcc dot gnu.org
  2023-06-21 18:43 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-21 18:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110358

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I thought there was already a bug recording that there should be a better way
of supressing Wdangling-reference ...

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

* [Bug c++/110358] requesting nicer suppression for Wdangling-reference
  2023-06-21 18:40 [Bug c++/110358] New: requesting nicer suppression for Wdangling-reference barry.revzin at gmail dot com
  2023-06-21 18:42 ` [Bug c++/110358] " pinskia at gcc dot gnu.org
@ 2023-06-21 18:43 ` pinskia at gcc dot gnu.org
  2023-06-21 19:14 ` mpolacek at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-21 18:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110358

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> I thought there was already a bug recording that there should be a better
> way of supressing Wdangling-reference ...

Yes PR 109642 .

*** This bug has been marked as a duplicate of bug 109642 ***

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

* [Bug c++/110358] requesting nicer suppression for Wdangling-reference
  2023-06-21 18:40 [Bug c++/110358] New: requesting nicer suppression for Wdangling-reference barry.revzin at gmail dot com
  2023-06-21 18:42 ` [Bug c++/110358] " pinskia at gcc dot gnu.org
  2023-06-21 18:43 ` pinskia at gcc dot gnu.org
@ 2023-06-21 19:14 ` mpolacek at gcc dot gnu.org
  2024-01-30 16:14 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2023-06-21 19:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110358

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
                 CC|                            |mpolacek at gcc dot gnu.org
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org
             Status|RESOLVED                    |ASSIGNED
   Last reconfirmed|                            |2023-06-21
         Resolution|DUPLICATE                   |---

--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Reopening, specializations of class/function templates may need special
handling -- that is, a dedicated attribute.

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

* [Bug c++/110358] requesting nicer suppression for Wdangling-reference
  2023-06-21 18:40 [Bug c++/110358] New: requesting nicer suppression for Wdangling-reference barry.revzin at gmail dot com
                   ` (2 preceding siblings ...)
  2023-06-21 19:14 ` mpolacek at gcc dot gnu.org
@ 2024-01-30 16:14 ` mpolacek at gcc dot gnu.org
  2024-01-30 18:24 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-01-30 16:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110358

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Patch for [[gnu::non_owning]] posted:
https://gcc.gnu.org/pipermail/gcc-patches/2024-January/643998.html

Not sure how important it is to accept the optional argument.

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

* [Bug c++/110358] requesting nicer suppression for Wdangling-reference
  2023-06-21 18:40 [Bug c++/110358] New: requesting nicer suppression for Wdangling-reference barry.revzin at gmail dot com
                   ` (3 preceding siblings ...)
  2024-01-30 16:14 ` mpolacek at gcc dot gnu.org
@ 2024-01-30 18:24 ` cvs-commit at gcc dot gnu.org
  2024-03-01 20:55 ` cvs-commit at gcc dot gnu.org
  2024-03-01 20:56 ` mpolacek at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-01-30 18:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110358

--- Comment #5 from GCC 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:f2061b2a9641c2228d4e2d86f19532ad7e93d627

commit r14-8636-gf2061b2a9641c2228d4e2d86f19532ad7e93d627
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Jan 25 12:08:14 2024 -0500

    c++: avoid -Wdangling-reference for std::span-like classes [PR110358]

    Real-world experience shows that -Wdangling-reference triggers for
    user-defined std::span-like classes a lot.  We can easily avoid that
    by considering classes like

        template<typename T>
        struct Span {
          T* data_;
          std::size len_;
        };

    to be std::span-like, and not warning for them.  Unlike the previous
    patch, this one considers a non-union class template that has a pointer
    data member and a trivial destructor as std::span-like.

            PR c++/110358
            PR c++/109640

    gcc/cp/ChangeLog:

            * call.cc (reference_like_class_p): Don't warn for std::span-like
            classes.

    gcc/ChangeLog:

            * doc/invoke.texi: Update -Wdangling-reference description.

    gcc/testsuite/ChangeLog:

            * g++.dg/warn/Wdangling-reference18.C: New test.
            * g++.dg/warn/Wdangling-reference19.C: New test.
            * g++.dg/warn/Wdangling-reference20.C: New test.

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

* [Bug c++/110358] requesting nicer suppression for Wdangling-reference
  2023-06-21 18:40 [Bug c++/110358] New: requesting nicer suppression for Wdangling-reference barry.revzin at gmail dot com
                   ` (4 preceding siblings ...)
  2024-01-30 18:24 ` cvs-commit at gcc dot gnu.org
@ 2024-03-01 20:55 ` cvs-commit at gcc dot gnu.org
  2024-03-01 20:56 ` mpolacek at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-03-01 20:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110358

--- Comment #6 from GCC 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:c7607c4cf18986025430ca8626abfe56bfe87106

commit r14-9263-gc7607c4cf18986025430ca8626abfe56bfe87106
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Jan 25 16:38:51 2024 -0500

    c++: implement [[gnu::no_dangling]] [PR110358]

    Since -Wdangling-reference has false positives that can't be
    prevented, we should offer an easy way to suppress the warning.
    Currently, that is only possible by using a #pragma, either around the
    enclosing class or around the call site.  But #pragma GCC diagnostic tend
    to be onerous.  A better solution would be to have an attribute.

    To that end, this patch adds a new attribute, [[gnu::no_dangling]].
    This attribute takes an optional bool argument to support cases like:

      template <typename T>
      struct [[gnu::no_dangling(std::is_reference_v<T>)]] S {
         // ...
      };

            PR c++/110358
            PR c++/109642

    gcc/cp/ChangeLog:

            * call.cc (no_dangling_p): New.
            (reference_like_class_p): Use it.
            (do_warn_dangling_reference): Use it.  Don't warn when the function
            or its enclosing class has attribute gnu::no_dangling.
            * tree.cc (cxx_gnu_attributes): Add gnu::no_dangling.
            (handle_no_dangling_attribute): New.

    gcc/ChangeLog:

            * doc/extend.texi: Document gnu::no_dangling.
            * doc/invoke.texi: Mention that gnu::no_dangling disables
            -Wdangling-reference.

    gcc/testsuite/ChangeLog:

            * g++.dg/ext/attr-no-dangling1.C: New test.
            * g++.dg/ext/attr-no-dangling2.C: New test.
            * g++.dg/ext/attr-no-dangling3.C: New test.
            * g++.dg/ext/attr-no-dangling4.C: New test.
            * g++.dg/ext/attr-no-dangling5.C: New test.
            * g++.dg/ext/attr-no-dangling6.C: New test.
            * g++.dg/ext/attr-no-dangling7.C: New test.
            * g++.dg/ext/attr-no-dangling8.C: New test.
            * g++.dg/ext/attr-no-dangling9.C: New test.

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

* [Bug c++/110358] requesting nicer suppression for Wdangling-reference
  2023-06-21 18:40 [Bug c++/110358] New: requesting nicer suppression for Wdangling-reference barry.revzin at gmail dot com
                   ` (5 preceding siblings ...)
  2024-03-01 20:55 ` cvs-commit at gcc dot gnu.org
@ 2024-03-01 20:56 ` mpolacek at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-03-01 20:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110358

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #7 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
[[gnu::no_dangling]] implemented in GCC 14.

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

end of thread, other threads:[~2024-03-01 20:56 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-21 18:40 [Bug c++/110358] New: requesting nicer suppression for Wdangling-reference barry.revzin at gmail dot com
2023-06-21 18:42 ` [Bug c++/110358] " pinskia at gcc dot gnu.org
2023-06-21 18:43 ` pinskia at gcc dot gnu.org
2023-06-21 19:14 ` mpolacek at gcc dot gnu.org
2024-01-30 16:14 ` mpolacek at gcc dot gnu.org
2024-01-30 18:24 ` cvs-commit at gcc dot gnu.org
2024-03-01 20:55 ` cvs-commit at gcc dot gnu.org
2024-03-01 20:56 ` mpolacek at gcc dot gnu.org

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