[Question: AST modification:] insert a simple function call to a existing function decl

yin liu liuyin.iwork@gmail.com
Sun Mar 5 05:13:00 GMT 2017


Hello,

I'm trying to do a simple modification of AST, insert a function call
to a existing function during gcc compilation.

here is my code:

test code:
int test(){
---------------------//I want to insert a function call here, like:
"myfunction();"
return 1;
}


GCC plugin code:
static void modify_AST(void*, void*) {
    tree __func_type = build_function_type(integer_type_node, NULL_TREE);
    tree __func_decl = build_decl(UNKNOWN_LOCATION, FUNCTION_DECL,
get_identifier("mycall"), __func_type);

    tree __addr_expr = build1(ADDR_EXPR, build_pointer_type
(TREE_TYPE(__func_type)), __func_decl);
    tree __call_expr = build_call_nary(__func_type, __addr_expr, 0);

    tree fnBody = DECL_SAVED_TREE(current_function_decl);
    tree statements = BIND_EXPR_BODY(fnBody);

    append_to_statement_list(__call_expr, &statements);
}

int plugin_init(plugin_name_args* info, plugin_gcc_version* ver) {
    int r(0);
    register_callback(info->base_name, PLUGIN_OVERRIDE_GATE, &modify_AST, 0);
    return r;
}


After I run my plugin on the test code, and check the result, there is
no "call" inserted to the assembly code.

Could you please kindly help to check my code?
And How to generate and insert a simple function call (such as
"myfunction();") into the source code during compilation?

Thanks in advance!

Brs,
YL.



More information about the Gcc-help mailing list