detect empty functions
Florian Weimer
fweimer@redhat.com
Thu Feb 6 16:33:00 GMT 2014
On 02/06/2014 03:30 PM, Prathamesh Kulkarni wrote:
> I was trying to make -Wunused-parameter not warn, if the function is
> empty (since an empty function would most likely
> be a stub function).
> Does that sound reasonable ?
Yes, why not. You should add a test case and a changelog entry and post
it to gcc-patches for review.
>
> +bool
> +is_tree_empty_function (tree fn)
> +{
> + tree body, kid;
> +
> + body = DECL_SAVED_TREE (fn);
> + if (!body)
> + return false;
> + kid = TREE_OPERAND (body, 1); // body is bind_expr
> + gcc_assert (kid);
> + return TREE_CODE (kid) == STATEMENT_LIST && (STATEMENT_LIST_HEAD
> (kid) == NULL);
> +}
Not sure about the name and how general this is.
> /* Possibly warn about unused parameters. */
> void
> do_warn_unused_parameter (tree fn)
> {
> tree decl;
> -
> + bool empty_function;
> +
> + empty_function = is_tree_empty_function (fn);
> for (decl = DECL_ARGUMENTS (fn);
> decl; decl = DECL_CHAIN (decl))
> if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
> && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
> - && !TREE_NO_WARNING (decl))
> + && !TREE_NO_WARNING (decl) && !empty_function)
> warning (OPT_Wunused_parameter, "unused parameter %q+D", decl);
> }
You should call the function only if the preceding conditions aren't false.
--
Florian Weimer / Red Hat Product Security Team
More information about the Gcc-help
mailing list