diff mbox series

c++: Move -Wdangling-reference to -Wextra

Message ID 20240801202038.3945271-1-polacek@redhat.com
State New
Headers show
Series c++: Move -Wdangling-reference to -Wextra | expand

Commit Message

Marek Polacek Aug. 1, 2024, 8:20 p.m. UTC
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
Despite a number of mitigations (don't warn for std::span-like classes,
lambdas, adding [[gnu::no_dangling]], etc.), the warning still seems to
cause some grief.  Let's move the warning to -Wextra, then.

gcc/c-family/ChangeLog:

	* c.opt (Wdangling-reference): Move from -Wall to -Wextra.

gcc/ChangeLog:

	* doc/invoke.texi: Document that -Wdangling-reference is
	enabled by -Wextra.
---
 gcc/c-family/c.opt  | 2 +-
 gcc/doc/invoke.texi | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)


base-commit: 32e678b2ed752154b2f96719e33f11a7c6417f20

Comments

Jason Merrill Aug. 1, 2024, 9:18 p.m. UTC | #1
On 8/1/24 4:20 PM, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> 
> -- >8 --
> Despite a number of mitigations (don't warn for std::span-like classes,
> lambdas, adding [[gnu::no_dangling]], etc.), the warning still seems to
> cause some grief.  Let's move the warning to -Wextra, then.

Any references to the grief?

The patch is OK.

> gcc/c-family/ChangeLog:
> 
> 	* c.opt (Wdangling-reference): Move from -Wall to -Wextra.
> 
> gcc/ChangeLog:
> 
> 	* doc/invoke.texi: Document that -Wdangling-reference is
> 	enabled by -Wextra.
> ---
>   gcc/c-family/c.opt  | 2 +-
>   gcc/doc/invoke.texi | 3 ++-
>   2 files changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> index a52682d835c..979f17a7e32 100644
> --- a/gcc/c-family/c.opt
> +++ b/gcc/c-family/c.opt
> @@ -604,7 +604,7 @@ C ObjC C++ ObjC++ Joined RejectNegative UInteger Var(warn_dangling_pointer) Warn
>   Warn for uses of pointers to auto variables whose lifetime has ended.
>   
>   Wdangling-reference
> -C++ ObjC++ Var(warn_dangling_reference) Warning LangEnabledBy(C++ ObjC++, Wall)
> +C++ ObjC++ Var(warn_dangling_reference) Warning LangEnabledBy(C++ ObjC++, Wextra)
>   Warn when a reference is bound to a temporary whose lifetime has ended.
>   
>   Wdate-time
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index ef2213b4e84..252b232e9f2 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -3962,7 +3962,7 @@ that has a pointer data member and a trivial destructor.
>   The warning can be disabled by using the @code{gnu::no_dangling} attribute
>   (@pxref{C++ Attributes}).
>   
> -This warning is enabled by @option{-Wall}.
> +This warning is enabled by @option{-Wextra}.
>   
>   @opindex Wdelete-non-virtual-dtor
>   @opindex Wno-delete-non-virtual-dtor
> @@ -6481,6 +6481,7 @@ name is still supported, but the newer name is more descriptive.)
>   -Wcalloc-transposed-args
>   -Wcast-function-type
>   -Wclobbered
> +-Wdangling-reference @r{(C++ only)}
>   -Wdeprecated-copy @r{(C++ and Objective-C++ only)}
>   -Wempty-body
>   -Wenum-conversion @r{(only for C/ObjC)}
> 
> base-commit: 32e678b2ed752154b2f96719e33f11a7c6417f20
Marek Polacek Aug. 2, 2024, 6:43 p.m. UTC | #2
On Thu, Aug 01, 2024 at 05:18:35PM -0400, Jason Merrill wrote:
> On 8/1/24 4:20 PM, Marek Polacek wrote:
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > -- >8 --
> > Despite a number of mitigations (don't warn for std::span-like classes,
> > lambdas, adding [[gnu::no_dangling]], etc.), the warning still seems to
> > cause some grief.  Let's move the warning to -Wextra, then.
> 
> Any references to the grief?

Two recent:
https://gcc.gnu.org/PR115361
https://gcc.gnu.org/PR115730

> The patch is OK.

Thanks.

Marek
Patrick Palka Aug. 2, 2024, 7:15 p.m. UTC | #3
On Fri, 2 Aug 2024, Marek Polacek wrote:

> On Thu, Aug 01, 2024 at 05:18:35PM -0400, Jason Merrill wrote:
> > On 8/1/24 4:20 PM, Marek Polacek wrote:
> > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > > 
> > > -- >8 --
> > > Despite a number of mitigations (don't warn for std::span-like classes,
> > > lambdas, adding [[gnu::no_dangling]], etc.), the warning still seems to
> > > cause some grief.  Let's move the warning to -Wextra, then.
> > 
> > Any references to the grief?
> 
> Two recent:
> https://gcc.gnu.org/PR115361
> https://gcc.gnu.org/PR115730

FWIW PR115361 suggested adding another mitigation:

  Since the heuristic is concerned only with dangling references to
  *members of the GetKey object itself*, could you maybe add yet another
  heuristic to suppress the warning whenever the GetKey object is_empty?

That seems sensible to me, and would also resolve PR115229.

> 
> > The patch is OK.
> 
> Thanks.
> 
> Marek
> 
>
diff mbox series

Patch

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index a52682d835c..979f17a7e32 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -604,7 +604,7 @@  C ObjC C++ ObjC++ Joined RejectNegative UInteger Var(warn_dangling_pointer) Warn
 Warn for uses of pointers to auto variables whose lifetime has ended.
 
 Wdangling-reference
-C++ ObjC++ Var(warn_dangling_reference) Warning LangEnabledBy(C++ ObjC++, Wall)
+C++ ObjC++ Var(warn_dangling_reference) Warning LangEnabledBy(C++ ObjC++, Wextra)
 Warn when a reference is bound to a temporary whose lifetime has ended.
 
 Wdate-time
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index ef2213b4e84..252b232e9f2 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -3962,7 +3962,7 @@  that has a pointer data member and a trivial destructor.
 The warning can be disabled by using the @code{gnu::no_dangling} attribute
 (@pxref{C++ Attributes}).
 
-This warning is enabled by @option{-Wall}.
+This warning is enabled by @option{-Wextra}.
 
 @opindex Wdelete-non-virtual-dtor
 @opindex Wno-delete-non-virtual-dtor
@@ -6481,6 +6481,7 @@  name is still supported, but the newer name is more descriptive.)
 -Wcalloc-transposed-args
 -Wcast-function-type
 -Wclobbered
+-Wdangling-reference @r{(C++ only)}
 -Wdeprecated-copy @r{(C++ and Objective-C++ only)}
 -Wempty-body
 -Wenum-conversion @r{(only for C/ObjC)}