I am trying to write a simple GCC plugin that identifies all variables declarations (globals and locals) and their scope, I've gotten so far is to identify if only local/global.
What I would like to do is something like:
int a = 3; /* Scope 0. */
int main()
{
int a; /* Scope 1. */
a = 5;
{
int a; /* Scope 2 and so on... */
a = 9;
}
}
and my actual code looks like this:
static void handle_finish_type (void *gcc_data, void *user_data)
{
(void) user_data;
tree t = (tree) gcc_data;
/* Skip everything that is not var decl. */
if (TREE_CODE (t) == VAR_DECL)
printf("scope: %d\n", TREE_PUBLIC(t));
}
int plugin_init (struct plugin_name_args *plugin_info,
struct plugin_gcc_version *version)
{
(void) version;
const char *plugin_name = plugin_info->base_name;
struct plugin_info pi = { "0.1", "Variable scope test" };
register_callback (plugin_name, PLUGIN_INFO, NULL, &pi);
register_callback (plugin_name, PLUGIN_FINISH_DECL,
&handle_finish_type, NULL);
return 0;
}
I don't know if this is possible with plugins and if so what is the appropriate pass to look for and/or which function/macro should I use for this?