This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Inserting global variable daclaration


Hi, I was trying to insert a global variable declaration using gcc plugins.
I tried to make simple plugin example that inserts a global int variable called
_fake_var_.
The plugin code is this:

---------------- plugin.cpp ----------------------

int plugin_is_GPL_compatible;
static tree fake_var = NULL;

static void start_unit_callback (void *gcc_data, void *user_data)
{
printf("\n------------------Start Unit------------------\n");
  fake_var = build_decl (UNKNOWN_LOCATION, VAR_DECL,
                                     get_identifier ("_fake_var_"),
                                     integer_type_node);
  TREE_PUBLIC (fake_var) = 1;
  DECL_ARTIFICIAL (fake_var) = 1;
  TREE_USED(fake_var) = 1;
}

int plugin_init (struct plugin_name_args *plugin_info,
                 struct plugin_gcc_version *version)
{
  register_callback ("start_unit", PLUGIN_START_UNIT,
&start_unit_callback, NULL);
  return 0;
}
------------------------------------------------------
Then i compiled the next code(test.c) using that plugin:

---------------- test.c ------------------------------

int main(void) {
    _fake_var_=2;
    return 0;
}
-------------------------------------------------------

But i get this message:
error: â_fake_var_â was not declared in this scope _fake_var_=2;

The question is that if I'm missing something here and if there is
something wrong, because i didn't find further documentation about how
to do this.

AndrÃs.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]