On Thu, Feb 6, 2014 at 7:48 PM, Florian Weimer <fweimer@xxxxxxxxxx> wrote: > On 02/06/2014 03:15 PM, Prathamesh Kulkarni wrote: >> >> On Thu, Feb 6, 2014 at 5:34 PM, Florian Weimer <fweimer@xxxxxxxxxx> wrote: >>> >>> On 02/05/2014 03:30 PM, Prathamesh Kulkarni wrote: >>>> >>>> >>>> I wanted to ask, is there a way to detect if a function has empty body >>>> using GCC API ? >>> >>> >>> >>> Yes, you can write a plug-in and iterate over all the basic blocks in a >>> function and check if they are empty. > > >> I was looking for a way to detect empty function at GENERIC level. >> Is the following correct way to do it ? > > > I expect it's not fully reliable because the parser will remove some > constructs and add others. It depends on what you're trying to achieve. 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 ? +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); +} + /* 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); } > > > -- > Florian Weimer / Red Hat Product Security Team