diff mbox series

[C++] Avoid bogus -Wreturn-local-addr warnings in templates (PR c++/82600)

Message ID 20171019081527.GN14653@tucnak
State New
Headers show
Series [C++] Avoid bogus -Wreturn-local-addr warnings in templates (PR c++/82600) | expand

Commit Message

Jakub Jelinek Oct. 19, 2017, 8:15 a.m. UTC
Hi!

A recent change to check_return_expr resulted in
maybe_warn_about_returning_address_of_local being called also with
processing_template_decl.  The problem with that is that the function
relies on folding (fold_for_warn) which isn't performed at all when
processing_template_decl.  So, we have still ARRAY_REF for a local decl
that has pointer type, rather than the expected POINTER_PLUS_EXPR,
where ARRAY_REF would only remain if the variable was actually an array.

The following patch fixes that by not calling the function at all
when processing_template_decl like before.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2017-10-19  Jakub Jelinek  <jakub@redhat.com>

	PR c++/82600
	* typeck.c (check_return_expr): Don't call
	maybe_warn_about_returning_address_of_local in templates.

	* g++.dg/warn/Wreturn-local-addr-4.C: New test.


	Jakub

Comments

Nathan Sidwell Oct. 19, 2017, 2:15 p.m. UTC | #1
On 10/19/2017 04:15 AM, Jakub Jelinek wrote:
> Hi!
> 
> A recent change to check_return_expr resulted in
> maybe_warn_about_returning_address_of_local being called also with
> processing_template_decl.  The problem with that is that the function
> relies on folding (fold_for_warn) which isn't performed at all when
> processing_template_decl.  So, we have still ARRAY_REF for a local decl
> that has pointer type, rather than the expected POINTER_PLUS_EXPR,
> where ARRAY_REF would only remain if the variable was actually an array.
> 
> The following patch fixes that by not calling the function at all
> when processing_template_decl like before.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> 2017-10-19  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/82600
> 	* typeck.c (check_return_expr): Don't call
> 	maybe_warn_about_returning_address_of_local in templates.
> 

ok, thanks

nathan
diff mbox series

Patch

--- gcc/cp/typeck.c.jj	2017-10-10 22:04:05.000000000 +0200
+++ gcc/cp/typeck.c	2017-10-18 12:17:29.282963388 +0200
@@ -9228,7 +9228,8 @@  check_return_expr (tree retval, bool *no
 	       && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
 	retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
 			 TREE_OPERAND (retval, 0));
-      else if (maybe_warn_about_returning_address_of_local (retval))
+      else if (!processing_template_decl
+	       && maybe_warn_about_returning_address_of_local (retval))
 	retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
 			 build_zero_cst (TREE_TYPE (retval)));
     }
--- gcc/testsuite/g++.dg/warn/Wreturn-local-addr-4.C.jj	2017-10-18 12:21:08.569273452 +0200
+++ gcc/testsuite/g++.dg/warn/Wreturn-local-addr-4.C	2017-10-18 12:16:28.000000000 +0200
@@ -0,0 +1,18 @@ 
+// PR c++/82600
+// { dg-do compile }
+
+void *b[10];
+
+template <int N>
+void **
+foo (int x)
+{
+  void **a = b;		// { dg-bogus "address of local variable 'a' returned" }
+  return &a[x];
+}
+
+void **
+bar (int x)
+{
+  return foo <0> (x);
+}