Commit 88a3dbc1 by Richard Kenner

(c_expand_return): Issue a warning if returning address of non-static

local object.

From-SVN: r6405
parent 3a19aabc
/* Build expressions with type checking for C compiler. /* Build expressions with type checking for C compiler.
Copyright (C) 1987, 1988, 1989, 1992, 1993 Free Software Foundation, Inc. Copyright (C) 1987, 88, 91, 92, 93, 1994 Free Software Foundation, Inc.
This file is part of GNU CC. This file is part of GNU CC.
...@@ -6202,12 +6202,36 @@ c_expand_return (retval) ...@@ -6202,12 +6202,36 @@ c_expand_return (retval)
tree t = convert_for_assignment (valtype, retval, "return", tree t = convert_for_assignment (valtype, retval, "return",
NULL_TREE, NULL_TREE, 0); NULL_TREE, NULL_TREE, 0);
tree res = DECL_RESULT (current_function_decl); tree res = DECL_RESULT (current_function_decl);
tree inner;
if (t == error_mark_node) if (t == error_mark_node)
return; return;
t = build (MODIFY_EXPR, TREE_TYPE (res), inner = t = convert (TREE_TYPE (res), t);
res, convert (TREE_TYPE (res), t));
/* Strip any conversions, additions, and subtractions, and see if
we are returning the address of a local variable. Warn if so. */
while (TREE_CODE (inner) == NOP_EXPR
|| TREE_CODE (inner) == NON_LVALUE_EXPR
|| TREE_CODE (inner) == CONVERT_EXPR
|| TREE_CODE (inner) == PLUS_EXPR
|| TREE_CODE (inner) == MINUS_EXPR)
inner = TREE_OPERAND (inner, 0);
if (TREE_CODE (inner) == ADDR_EXPR)
{
inner = TREE_OPERAND (inner, 0);
while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
inner = TREE_OPERAND (inner, 0);
if (TREE_CODE (inner) == VAR_DECL
&& ! TREE_STATIC (inner)
&& DECL_CONTEXT (inner) == current_function_decl)
warning ("function returns address of local variable");
}
t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
TREE_SIDE_EFFECTS (t) = 1; TREE_SIDE_EFFECTS (t) = 1;
expand_return (t); expand_return (t);
current_function_returns_value = 1; current_function_returns_value = 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment