re PR c++/77591 (decltype(auto) and ternary operator allow returning local reference...
authorJakub Jelinek <jakub@redhat.com>
Mon, 28 Nov 2016 15:21:53 +0000 (16:21 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 28 Nov 2016 15:21:53 +0000 (16:21 +0100)
PR c++/77591
* typeck.c (maybe_warn_about_returning_address_of_local): Optimize
whats_returned through fold_for_warn.

* g++.dg/cpp1y/pr77591.C: New test.

From-SVN: r242924

gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp1y/pr77591.C [new file with mode: 0644]

index c6bc06f..f442a7a 100644 (file)
@@ -1,3 +1,9 @@
+2016-11-28  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/77591
+       * typeck.c (maybe_warn_about_returning_address_of_local): Optimize
+       whats_returned through fold_for_warn.
+
 2016-11-27  Jason Merrill  <jason@redhat.com>
 
        PR c++/77907
index 6f9ad0e..68fe19e 100644 (file)
@@ -8612,7 +8612,7 @@ static bool
 maybe_warn_about_returning_address_of_local (tree retval)
 {
   tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
-  tree whats_returned = retval;
+  tree whats_returned = fold_for_warn (retval);
 
   for (;;)
     {
index b69d554..1444448 100644 (file)
@@ -1,3 +1,8 @@
+2016-11-28  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/77591
+       * g++.dg/cpp1y/pr77591.C: New test.
+
 2016-11-28  David Edelsohn  <dje.gcc@gmail.com>
 
        * gcc.dg/torture/pr78515.c: Ignore ABI extension warning.
diff --git a/gcc/testsuite/g++.dg/cpp1y/pr77591.C b/gcc/testsuite/g++.dg/cpp1y/pr77591.C
new file mode 100644 (file)
index 0000000..8f9e28c
--- /dev/null
@@ -0,0 +1,19 @@
+// PR c++/77591
+// { dg-do compile { target c++14 } }
+// { dg-options "-O0 -Wreturn-local-addr" }
+
+class A { };
+
+decltype(auto)
+foo ()
+{
+  A c;                 // { dg-warning "reference to local variable 'c' returned" }
+  return (c);
+}
+
+decltype(auto)
+bar ()
+{
+  A c;                 // { dg-warning "reference to local variable 'c' returned" }
+  return 1==1 ? c : c;
+}