Re-factor inclusion of tree.h.
[official-gcc.git] / gcc / testsuite / g++.dg / plugin / dumb_plugin.c
blobe197d667251f51af14cb1bfc5cd3c57318af8d03
1 /* A trivial (dumb) plugin example that shows how to use the GCC plugin
2 mechanism. */
4 #include "gcc-plugin.h"
5 #include <stdlib.h>
6 #include "config.h"
7 #include "system.h"
8 #include "coretypes.h"
9 #include "tree.h"
10 #include "tree-pass.h"
11 #include "intl.h"
12 #include "toplev.h"
13 #include "diagnostic.h"
14 #include "context.h"
16 int plugin_is_GPL_compatible;
18 /* Callback function to invoke after GCC finishes parsing a struct. */
20 void
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. */
30 void
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. */
40 void
41 handle_end_of_compilation_unit (void *event_data, void *data)
43 warning (0, G_("End of compilation unit"));
47 static unsigned int
48 execute_dumb_plugin_example (void)
50 warning (0, G_("Analyze function %s"),
51 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
52 return 0;
55 static bool
56 gate_dumb_plugin_example (void)
58 return true;
61 namespace {
63 const pass_data pass_data_dumb_plugin_example =
65 GIMPLE_PASS, /* type */
66 "dumb_plugin_example", /* name */
67 OPTGROUP_NONE, /* optinfo_flags */
68 true, /* has_gate */
69 true, /* has_execute */
70 TV_NONE, /* tv_id */
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
80 public:
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
91 } // anon namespace
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;
113 int i;
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"))
121 if (argv[i].value)
122 ref_pass_name = argv[i].value;
123 else
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"))
129 if (argv[i].value)
130 ref_instance_number = strtol (argv[i].value, NULL, 0);
131 else
132 warning (0, G_("option '-fplugin-arg-%s-ref-pass-instance-num'"
133 " requires an integer value"), plugin_name);
135 else
136 warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
137 plugin_name, argv[i].key);
140 if (!ref_pass_name)
142 error (G_("plugin %qs requires a reference pass name"), plugin_name);
143 return 1;
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);
160 return 0;