+2019-06-05 Martin Sebor <msebor@redhat.com>
+
+ PR c/90737
+ * c-typeck.c (c_finish_return): Only consider functions returning
+ pointers as candidates for -Wreturn-local-addr.
+
2019-06-05 Martin Sebor <msebor@redhat.com>
* c-decl.c (start_decl): Adjust quoting and hyphenation
if (DECL_P (inner)
&& !DECL_EXTERNAL (inner)
&& !TREE_STATIC (inner)
- && DECL_CONTEXT (inner) == current_function_decl)
+ && DECL_CONTEXT (inner) == current_function_decl
+ && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
{
if (TREE_CODE (inner) == LABEL_DECL)
warning_at (loc, OPT_Wreturn_local_addr,
+2019-06-05 Martin Sebor <msebor@redhat.com>
+
+ PR c/90737
+ * typeck.c (maybe_warn_about_returning_address_of_local): Only
+ consider functions returning pointers as candidates for
+ -Wreturn-local-addr.
+
2019-06-05 Paolo Carlini <paolo.carlini@oracle.com>
* decl.c (smallest_type_location): New.
"returning local %<initializer_list%> variable %qD "
"does not extend the lifetime of the underlying array",
whats_returned);
- else if (TREE_CODE (whats_returned) == LABEL_DECL)
+ else if (POINTER_TYPE_P (valtype)
+ && TREE_CODE (whats_returned) == LABEL_DECL)
w = warning_at (loc, OPT_Wreturn_local_addr,
"address of label %qD returned",
whats_returned);
- else
+ else if (POINTER_TYPE_P (valtype))
w = warning_at (loc, OPT_Wreturn_local_addr,
"address of local variable %qD returned",
whats_returned);
+2019-06-05 Martin Sebor <msebor@redhat.com>
+
+ PR c/90737
+ * c-c++-common/Wreturn-local-addr.c: New test.
+ * g++.dg/warn/Wreturn-local-addr-6.C: New test.
+
2019-06-05 Hongtao Liu <hongtao.liu@intel.com>
* gcc.target/i386/avx512dq-vfpclasspd-1.c: Adjust scan assember
--- /dev/null
+/* PR c/90737 - inconsistent address of a local converted to intptr_t
+ between callee and caller
+ { dg-do compile }
+ { dg-options "-O1 -Wall -Wreturn-local-addr -fdump-tree-optimized" } */
+
+typedef __INTPTR_TYPE__ intptr_t;
+
+static inline intptr_t
+return_addr_local_as_int (void)
+{
+ int i;
+ if ((intptr_t)&i == 0)
+ __builtin_abort ();
+
+ return (intptr_t)&i;
+}
+
+void get_addr_local_as_int (void)
+{
+ intptr_t i = return_addr_local_as_int ();
+ if (i == 0)
+ __builtin_abort ();
+}
+
+
+static inline intptr_t
+return_addr_label_as_int (void)
+{
+ label:
+ if ((intptr_t)&&label == 0)
+ __builtin_abort ();
+
+ return (intptr_t)&&label;
+}
+
+void get_addr_label_as_int (void)
+{
+ intptr_t i = return_addr_label_as_int ();
+ if (i == 0)
+ __builtin_abort ();
+}
+
+/* Verify that the functions that return the address of the label
+ or local variable have been optimized away and so have the calls
+ to abort.
+ { dg-final { scan-tree-dump-not "return_addr_" "optimized" } }
+ { dg-final { scan-tree-dump-not "abort" "optimized" } } */
--- /dev/null
+/* PR c/90737 - inconsistent address of a local converted to intptr_t
+ between callee and caller
+ { dg-do compile }
+ { dg-options "-O1 -Wall -Wreturn-local-addr -fdump-tree-optimized" } */
+
+typedef __INTPTR_TYPE__ intptr_t;
+
+const intptr_t&
+return_addr_label_as_intref (void)
+{
+ label:
+ if ((const intptr_t*)&&label == 0)
+ __builtin_exit (1);
+
+ return *(const intptr_t*)&&label; // { dg-warning "\\\[-Wreturn-local-addr]" } */
+}
+
+const intptr_t&
+return_addr_local_as_intref (void)
+{
+ int a[1];
+ if ((const intptr_t*)a == 0)
+ __builtin_exit (1);
+
+ return (const intptr_t&)a; // { dg-warning "\\\[-Wreturn-local-addr]" } */
+}
+
+/* Verify that the return value has been replaced with zero:
+ { dg-final { scan-tree-dump-times "return 0;" 2 "optimized" } } */