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"));
48 execute_dumb_plugin_example (void)
50 warning (0, G_("Analyze function %s"),
51 IDENTIFIER_POINTER (DECL_NAME (current_function_decl
)));
56 gate_dumb_plugin_example (void)
63 const pass_data pass_data_dumb_plugin_example
=
65 GIMPLE_PASS
, /* type */
66 "dumb_plugin_example", /* name */
67 OPTGROUP_NONE
, /* optinfo_flags */
69 true, /* has_execute */
71 PROP_cfg
, /* properties_required */
72 0, /* properties_provided */
73 0, /* properties_destroyed */
74 0, /* todo_flags_start */
75 0, /* todo_flags_finish */
78 class pass_dumb_plugin_example
: public gimple_opt_pass
81 pass_dumb_plugin_example(gcc::context
*ctxt
)
82 : gimple_opt_pass(pass_data_dumb_plugin_example
, ctxt
)
85 /* opt_pass methods: */
86 bool gate () { return gate_dumb_plugin_example (); }
87 unsigned int execute () { return execute_dumb_plugin_example (); }
89 }; // class pass_dumb_plugin_example
93 static gimple_opt_pass
*
94 make_pass_dumb_plugin_example (gcc::context
*ctxt
)
96 return new pass_dumb_plugin_example (ctxt
);
99 /* Initialization function that GCC calls. This plugin takes an argument
100 that specifies the name of the reference pass and an instance number,
101 both of which determine where the plugin pass should be inserted. */
104 plugin_init (struct plugin_name_args
*plugin_info
,
105 struct plugin_gcc_version
*version
)
107 struct register_pass_info pass_info
;
108 const char *plugin_name
= plugin_info
->base_name
;
109 int argc
= plugin_info
->argc
;
110 struct plugin_argument
*argv
= plugin_info
->argv
;
111 char *ref_pass_name
= NULL
;
112 int ref_instance_number
= 0;
115 /* Process the plugin arguments. This plugin takes the following arguments:
116 ref-pass-name=<PASS_NAME> and ref-pass-instance-num=<NUM>. */
117 for (i
= 0; i
< argc
; ++i
)
119 if (!strcmp (argv
[i
].key
, "ref-pass-name"))
122 ref_pass_name
= argv
[i
].value
;
124 warning (0, G_("option '-fplugin-arg-%s-ref-pass-name'"
125 " requires a pass name"), plugin_name
);
127 else if (!strcmp (argv
[i
].key
, "ref-pass-instance-num"))
130 ref_instance_number
= strtol (argv
[i
].value
, NULL
, 0);
132 warning (0, G_("option '-fplugin-arg-%s-ref-pass-instance-num'"
133 " requires an integer value"), plugin_name
);
136 warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
137 plugin_name
, argv
[i
].key
);
142 error (G_("plugin %qs requires a reference pass name"), plugin_name
);
146 pass_info
.pass
= make_pass_dumb_plugin_example (g
);
147 pass_info
.reference_pass_name
= ref_pass_name
;
148 pass_info
.ref_pass_instance_number
= ref_instance_number
;
149 pass_info
.pos_op
= PASS_POS_INSERT_AFTER
;
151 register_callback (plugin_name
, PLUGIN_PASS_MANAGER_SETUP
, NULL
, &pass_info
);
153 register_callback (plugin_name
, PLUGIN_FINISH_TYPE
, handle_struct
, NULL
);
155 register_callback (plugin_name
, PLUGIN_PRE_GENERICIZE
,
156 handle_pre_generic
, NULL
);
158 register_callback (plugin_name
, PLUGIN_FINISH_UNIT
,
159 handle_end_of_compilation_unit
, NULL
);