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 c6bc06fc241cd62b052b40dc3099da218442568f..f442a7a83f04b9990889266d304c273bea58ce54 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 6f9ad0ed3cb66e7301e6b727c47d4dba1d416efb..68fe19eeadb93232ecebc42cc13bd5843628ff0e 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 b69d554f4ea3d62102fb266bb4af44ed5afcf15d..1444448c3bc8477f11d61184bc97d3be5c2586d6 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;
+}