Search code examples
c++gccgcc-pluginsgimple

Inserting function calls in the gimple


I'm having problems figuring out how to do the next thing.

I have the following code:

test.cpp

#include <stdio.h>

void
function(void) {printf("Hellow ");}

int main(void) {
    printf("World\n");
    return 0;
}

And I want to transform it into the next one:

#include <stdio.h>

void
function(void) {printf("Hellow ");}

int main(void) {
    function();
    printf("World\n");
    return 0;
}

with a gcc plugin.

The code that doesn't work in my plugin is this one:

...
tree function_fn;
tree function_fn_type;

function_fn_type=build_function_type_list(void_type_node, void_type_node, NULL_TREE);
function_fn = build_fn_decl ("function", function_fn_type);

gimple call = gimple_build_call (funcion_fn, 0);
gsi_insert_before (&gsi, call, GSI_NEW_STMT);
...

Then when I compile test.cpp with the plugin i have the next error message:

/tmp/cc2VRszt.o: In function main': test.cpp:(.text+0x60): Undefined reference tofunction'

Anyone can help me?


Solution

  • You're building a function declaration, and inserting a call to a function based on the declaration, but unless you've defined that function in another translation unit you link to, it will be unresolved. If you want a plugin to insert a definition in the same translation unit like in your example, this guide for front-end developers would be a good start:

    http://www.tldp.org/HOWTO/GCC-Frontend-HOWTO-7.html