c++cgccgcc-plugins

Insert global variable declaration with a gcc plugin


I would like to know if it's possible to insert a global variable declaration with a gcc plugin. For example if I have the following code:

test.c:

int main(void) {
  return 0;
}

and I want to transform it into:

int fake_var;

int main(void) {
  return 0;
}

Is that possible? If it's possible, in which pass and how can I do it?


Solution

  • I think you'll want to take a look at varpool_add_new_variable() in varpool.c. You should be able to pass a declaration built with type VAR_DECL to it. Similarly, take a look at add_new_static_var(), but I think the former is what you want, as it was added specifically to allow declaring globals in the middle/back end.