public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: -Wdangling-reference not suppressed in template [PR109774]
@ 2023-05-16 19:13 Marek Polacek
  2023-05-16 19:35 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2023-05-16 19:13 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill

In check_return_expr, we suppress the -Wdangling-reference warning when
we're sure it would be a false positive.  It wasn't working in a
template, though, because the suppress_warning call was never reached.

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

	PR c++/109774

gcc/cp/ChangeLog:

	* typeck.cc (check_return_expr): In a template, return only after
	suppressing -Wdangling-reference.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wdangling-reference13.C: New test.
---
 gcc/cp/typeck.cc                              |  6 ++---
 .../g++.dg/warn/Wdangling-reference13.C       | 23 +++++++++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference13.C

diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index 53ac925a092..c225c4e2423 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -11236,9 +11236,6 @@ check_return_expr (tree retval, bool *no_warning)
 			 build_zero_cst (TREE_TYPE (retval)));
     }
 
-  if (processing_template_decl)
-    return saved_retval;
-
   /* A naive attempt to reduce the number of -Wdangling-reference false
      positives: if we know that this function can return a variable with
      static storage duration rather than one of its parameters, suppress
@@ -11250,6 +11247,9 @@ check_return_expr (tree retval, bool *no_warning)
       && TREE_STATIC (bare_retval))
     suppress_warning (current_function_decl, OPT_Wdangling_reference);
 
+  if (processing_template_decl)
+    return saved_retval;
+
   /* Actually copy the value returned into the appropriate location.  */
   if (retval && retval != result)
     {
diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference13.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference13.C
new file mode 100644
index 00000000000..bc09fbae22b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference13.C
@@ -0,0 +1,23 @@
+// PR c++/109774
+// { dg-do compile }
+// { dg-options "-Wdangling-reference" }
+
+int y;
+
+template<typename T>
+int& get(const char& )
+{
+    return y;
+}
+
+int& get2(const char&)
+{
+    return y;
+}
+
+int stuff(void)
+{
+    const int &h = get<void>(0); // { dg-bogus "dangling reference" }
+    const int &k = get2(0); // { dg-bogus "dangling reference" }
+    return h+k;
+}

base-commit: 94a311abf783de754f0f1b2d4c1f00a9788e795b
-- 
2.40.1


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

* Re: [PATCH] c++: -Wdangling-reference not suppressed in template [PR109774]
  2023-05-16 19:13 [PATCH] c++: -Wdangling-reference not suppressed in template [PR109774] Marek Polacek
@ 2023-05-16 19:35 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2023-05-16 19:35 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 5/16/23 15:13, Marek Polacek wrote:
> In check_return_expr, we suppress the -Wdangling-reference warning when
> we're sure it would be a false positive.  It wasn't working in a
> template, though, because the suppress_warning call was never reached.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk and 13.2?

OK.

> 	PR c++/109774
> 
> gcc/cp/ChangeLog:
> 
> 	* typeck.cc (check_return_expr): In a template, return only after
> 	suppressing -Wdangling-reference.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/warn/Wdangling-reference13.C: New test.
> ---
>   gcc/cp/typeck.cc                              |  6 ++---
>   .../g++.dg/warn/Wdangling-reference13.C       | 23 +++++++++++++++++++
>   2 files changed, 26 insertions(+), 3 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference13.C
> 
> diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
> index 53ac925a092..c225c4e2423 100644
> --- a/gcc/cp/typeck.cc
> +++ b/gcc/cp/typeck.cc
> @@ -11236,9 +11236,6 @@ check_return_expr (tree retval, bool *no_warning)
>   			 build_zero_cst (TREE_TYPE (retval)));
>       }
>   
> -  if (processing_template_decl)
> -    return saved_retval;
> -
>     /* A naive attempt to reduce the number of -Wdangling-reference false
>        positives: if we know that this function can return a variable with
>        static storage duration rather than one of its parameters, suppress
> @@ -11250,6 +11247,9 @@ check_return_expr (tree retval, bool *no_warning)
>         && TREE_STATIC (bare_retval))
>       suppress_warning (current_function_decl, OPT_Wdangling_reference);
>   
> +  if (processing_template_decl)
> +    return saved_retval;
> +
>     /* Actually copy the value returned into the appropriate location.  */
>     if (retval && retval != result)
>       {
> diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference13.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference13.C
> new file mode 100644
> index 00000000000..bc09fbae22b
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference13.C
> @@ -0,0 +1,23 @@
> +// PR c++/109774
> +// { dg-do compile }
> +// { dg-options "-Wdangling-reference" }
> +
> +int y;
> +
> +template<typename T>
> +int& get(const char& )
> +{
> +    return y;
> +}
> +
> +int& get2(const char&)
> +{
> +    return y;
> +}
> +
> +int stuff(void)
> +{
> +    const int &h = get<void>(0); // { dg-bogus "dangling reference" }
> +    const int &k = get2(0); // { dg-bogus "dangling reference" }
> +    return h+k;
> +}
> 
> base-commit: 94a311abf783de754f0f1b2d4c1f00a9788e795b


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

end of thread, other threads:[~2023-05-16 19:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-16 19:13 [PATCH] c++: -Wdangling-reference not suppressed in template [PR109774] Marek Polacek
2023-05-16 19:35 ` Jason Merrill

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