re PR c++/82600 (Address of local variable returned [-Werror=return-local-addr] when...
authorJakub Jelinek <jakub@redhat.com>
Thu, 19 Oct 2017 14:24:39 +0000 (16:24 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Thu, 19 Oct 2017 14:24:39 +0000 (16:24 +0200)
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.

From-SVN: r253899

gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wreturn-local-addr-4.C [new file with mode: 0644]

index daf302fc34963ecd7727b43f27fd8ebbdf4e74ea..9592c83be26e3d17a2bc8f7b7289171f24389a32 100644 (file)
@@ -1,3 +1,9 @@
+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.
+
 2017-10-17  Nathan Sidwell  <nathan@acm.org>
 
        PR c++/82560
index 08b2ae555e635fce9695076abd9302b7284e683b..19fbe3c4a4ac682b2f08864f868f9b6b925dd426 100644 (file)
@@ -9228,7 +9228,8 @@ check_return_expr (tree retval, bool *no_warning)
               && 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)));
     }
index 1488f6fc03b8c9db6f31fb6035bc5e144727a44b..3b2f638dba7584bc110a7dd735011056c1db8be9 100644 (file)
@@ -1,3 +1,8 @@
+2017-10-19  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/82600
+       * g++.dg/warn/Wreturn-local-addr-4.C: New test.
+
 2017-10-19  Eric Botcazou  <ebotcazou@adacore.com>
 
        * gcc.dg/debug/dwarf2/sso.c: Rename into...
diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-4.C b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-4.C
new file mode 100644 (file)
index 0000000..492dcb9
--- /dev/null
@@ -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);
+}