获取使用gcc插件编译的c ++文件的函数数量

时间:2015-04-02 15:12:58

标签: gcc plugins g++ gimple

我正在使用GCC插件创建一个传递,这是我的传递:

static const struct pass_data calls_printer_pass_data = {
                .type                   = GIMPLE_PASS,
                .name                   = "calls_printer",
                .optinfo_flags          = OPTGROUP_NONE,
                .has_gate               = false,
                .has_execute            = true,
                .tv_id                  = TV_NONE,
                .properties_required    = 0,
                .properties_provided    = 0,
                .properties_destroyed   = 0,
                .todo_flags_start       = 0,
                .todo_flags_finish      = 0
};

class calls_printer_pass : public gimple_opt_pass {
public:
        calls_printer_pass() : gimple_opt_pass(calls_printer_pass_data, g) {}
        unsigned int execute() { return toto(); }
};

int plugin_init (plugin_name_args* plugin_info,
             plugin_gcc_version* ver)
{
  cerr << "starting " << plugin_info->base_name << endl;
  const char * const plugin_name = plugin_info->base_name;
  const int argc = plugin_info->argc;
  const struct plugin_argument * const argv = plugin_info->argv;
  struct register_pass_info calls_printer_info;

  calls_printer_info.pass                         = new calls_printer_pass();
  calls_printer_info.reference_pass_name          = "ssa" ;
  calls_printer_info.ref_pass_instance_number     = 1;
  calls_printer_info.pos_op                       = PASS_POS_INSERT_AFTER;
  register_callback (plugin_name,
                     PLUGIN_PASS_MANAGER_SETUP,
                     NULL,
                     &calls_printer_info);
  return 0;
}

所以对于定义的每个函数执行toto(),如果没有执行toto(),是否可以获取所有函数的数量,如何只对整个文件执行一次传递,并循环通过使用FOR_EACH_FUNCTION()?

的所有函数

2 个答案:

答案 0 :(得分:2)

int toto_cnt = 0;

int toto (void)
{
  struct cgraph_node *node;

  if (!toto_cnt)
  {
    FOR_EACH_FUNCTION (node)
    {
      toto_cnt++;
    }
  }
}

class calls_printer_pass : public gimple_opt_pass {
public:
    calls_printer_pass() : gimple_opt_pass (calls_printer_pass_data, g)  {}
    unsigned int execute() { return toto(); }
};

void execute_finish_unit (void *gcc_data, void *user_data)
{
  printf ("%d\n", toto_cnt);
}

int plugin_init (plugin_name_args* plugin_info,
         plugin_gcc_version* ver)
{
  printf ("starting %s\n", plugin_info->base_name);
  const char * const plugin_name = plugin_info->base_name;
  const int argc = plugin_info->argc;
  const struct plugin_argument * const argv = plugin_info->argv;
  struct register_pass_info calls_printer_info;

  calls_printer_info.pass = new calls_printer_pass();
  calls_printer_info.reference_pass_name = "ssa" ;
  calls_printer_info.ref_pass_instance_number = 1;
  calls_printer_info.pos_op = PASS_POS_INSERT_AFTER;
  register_callback (plugin_name,
                 PLUGIN_PASS_MANAGER_SETUP,
                 NULL,
                 &calls_printer_info);

  register_callback (plugin_name, PLUGIN_FINISH_UNIT, &execute_finish_unit, NULL);

  return 0;
}

答案 1 :(得分:0)

您可以通过编写&#34; IPA&#34;来获得您想要的效果。通过。这些是程序间的&#34;传递,并有权访问调用图(和变量池)。