1 /* A trivial (dumb) plugin example that shows how to use the GCC plugin
4 #include "gcc-plugin.h"
10 #include "tree-pass.h"
13 #include "diagnostic.h"
16 int plugin_is_GPL_compatible
;
18 /* Callback function to invoke after GCC finishes parsing a struct. */
21 handle_struct (void *event_data
, void *data
)
23 tree type
= (tree
) event_data
;
24 warning (0, G_("Process struct %s"),
25 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
))));
28 /* Callback function to invoke before the function body is genericized. */
31 handle_pre_generic (void *event_data
, void *data
)
33 tree fndecl
= (tree
) event_data
;
34 warning (0, G_("Before genericizing function %s"),
35 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
38 /* Callback function to invoke after GCC finishes the compilation unit. */
41 handle_end_of_compilation_unit (void *event_data
, void *data
)
43 warning (0, G_("End of compilation unit"));
49 const pass_data pass_data_dumb_plugin_example
=
51 GIMPLE_PASS
, /* type */
52 "dumb_plugin_example", /* name */
53 OPTGROUP_NONE
, /* optinfo_flags */
55 PROP_cfg
, /* properties_required */
56 0, /* properties_provided */
57 0, /* properties_destroyed */
58 0, /* todo_flags_start */
59 0, /* todo_flags_finish */
62 class pass_dumb_plugin_example
: public gimple_opt_pass
65 pass_dumb_plugin_example(gcc::context
*ctxt
)
66 : gimple_opt_pass(pass_data_dumb_plugin_example
, ctxt
)
69 /* opt_pass methods: */
70 virtual unsigned int execute (function
*);
72 }; // class pass_dumb_plugin_example
75 pass_dumb_plugin_example::execute (function
*)
77 warning (0, G_("Analyze function %s"),
78 IDENTIFIER_POINTER (DECL_NAME (current_function_decl
)));
84 static gimple_opt_pass
*
85 make_pass_dumb_plugin_example (gcc::context
*ctxt
)
87 return new pass_dumb_plugin_example (ctxt
);
90 /* Initialization function that GCC calls. This plugin takes an argument
91 that specifies the name of the reference pass and an instance number,
92 both of which determine where the plugin pass should be inserted. */
95 plugin_init (struct plugin_name_args
*plugin_info
,
96 struct plugin_gcc_version
*version
)
98 struct register_pass_info pass_info
;
99 const char *plugin_name
= plugin_info
->base_name
;
100 int argc
= plugin_info
->argc
;
101 struct plugin_argument
*argv
= plugin_info
->argv
;
102 char *ref_pass_name
= NULL
;
103 int ref_instance_number
= 0;
106 /* Process the plugin arguments. This plugin takes the following arguments:
107 ref-pass-name=<PASS_NAME> and ref-pass-instance-num=<NUM>. */
108 for (i
= 0; i
< argc
; ++i
)
110 if (!strcmp (argv
[i
].key
, "ref-pass-name"))
113 ref_pass_name
= argv
[i
].value
;
115 warning (0, G_("option '-fplugin-arg-%s-ref-pass-name'"
116 " requires a pass name"), plugin_name
);
118 else if (!strcmp (argv
[i
].key
, "ref-pass-instance-num"))
121 ref_instance_number
= strtol (argv
[i
].value
, NULL
, 0);
123 warning (0, G_("option '-fplugin-arg-%s-ref-pass-instance-num'"
124 " requires an integer value"), plugin_name
);
127 warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
128 plugin_name
, argv
[i
].key
);
133 error (G_("plugin %qs requires a reference pass name"), plugin_name
);
137 pass_info
.pass
= make_pass_dumb_plugin_example (g
);
138 pass_info
.reference_pass_name
= ref_pass_name
;
139 pass_info
.ref_pass_instance_number
= ref_instance_number
;
140 pass_info
.pos_op
= PASS_POS_INSERT_AFTER
;
142 register_callback (plugin_name
, PLUGIN_PASS_MANAGER_SETUP
, NULL
, &pass_info
);
144 register_callback (plugin_name
, PLUGIN_FINISH_TYPE
, handle_struct
, NULL
);
146 register_callback (plugin_name
, PLUGIN_PRE_GENERICIZE
,
147 handle_pre_generic
, NULL
);
149 register_callback (plugin_name
, PLUGIN_FINISH_UNIT
,
150 handle_end_of_compilation_unit
, NULL
);