Debian Codegraph @

gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_nesting.cc

raw
1 /* This plugin exercises diagnostic nesting. */
2
3 #include "gcc-plugin.h"
4 #include "config.h"
5 #include "system.h"
6 #include "coretypes.h"
7 #include "tm.h"
8 #include "tree.h"
9 #include "stringpool.h"
10 #include "toplev.h"
11 #include "basic-block.h"
12 #include "hash-table.h"
13 #include "vec.h"
14 #include "ggc.h"
15 #include "basic-block.h"
16 #include "tree-ssa-alias.h"
17 #include "internal-fn.h"
18 #include "gimple.h"
19 #include "gimple-iterator.h"
20 #include "gimple-fold.h"
21 #include "tree-eh.h"
22 #include "gimple-expr.h"
23 #include "is-a.h"
24 #include "tree.h"
25 #include "tree-pass.h"
26 #include "intl.h"
27 #include "plugin-version.h"
28 #include "diagnostic.h"
29 #include "context.h"
30 #include "gcc-rich-location.h"
31
32 int plugin_is_GPL_compatible;
33
34 const pass_data pass_data_test_nesting =
35 {
36 GIMPLE_PASS, /* type */
37 "test_nesting", /* name */
38 OPTGROUP_NONE, /* optinfo_flags */
39 TV_NONE, /* tv_id */
40 PROP_ssa, /* properties_required */
41 0, /* properties_provided */
42 0, /* properties_destroyed */
43 0, /* todo_flags_start */
44 0, /* todo_flags_finish */
45 };
46
47 class pass_test_nesting : public gimple_opt_pass
48 {
49 public:
50 pass_test_nesting(gcc::context *ctxt)
51 : gimple_opt_pass(pass_data_test_nesting, ctxt)
52 {}
53
54 /* opt_pass methods: */
55 bool gate (function *) { return true; }
56 virtual unsigned int execute (function *);
57
58 }; // class pass_test_nesting
59
60 /* Determine if STMT is a call with NUM_ARGS arguments to a function
61 named FUNCNAME.
62 If so, return STMT as a gcall *. Otherwise return NULL. */
63
64 static gcall *
65 check_for_named_call (gimple *stmt,
66 const char *funcname, unsigned int num_args)
67 {
68 gcc_assert (funcname);
69
70 gcall *call = dyn_cast <gcall *> (stmt);
71 if (!call)
72 return NULL;
73
74 tree fndecl = gimple_call_fndecl (call);
75 if (!fndecl)
76 return NULL;
77
78 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), funcname))
79 return NULL;
80
81 if (gimple_call_num_args (call) != num_args)
82 {
83 error_at (stmt->location, "expected number of args: %i (got %i)",
84 num_args, gimple_call_num_args (call));
85 return NULL;
86 }
87
88 return call;
89 }
90
91 /* Exercise diagnostic_nesting. */
92
93 unsigned int
94 pass_test_nesting::execute (function *fun)
95 {
96 gimple_stmt_iterator gsi;
97 basic_block bb;
98
99 FOR_EACH_BB_FN (bb, fun)
100 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
101 {
102 gimple *stmt = gsi_stmt (gsi);
103
104 if (gcall *call = check_for_named_call (stmt, "foo", 0))
105 {
106 location_t loc = gimple_location (call);
107 auto_diagnostic_group g;
108 error_at (loc, "top-level error");
109 for (int i = 0; i < 3; i++)
110 {
111 auto_diagnostic_nesting_level sentinel;
112 inform (loc, "child %i", i);
113 for (int j = 0; j < 3; j++)
114 {
115 auto_diagnostic_nesting_level sentinel;
116 inform (loc, "grandchild %i %i", i, j);
117 }
118 }
119 }
120 }
121
122 return 0;
123 }
124
125 int
126 plugin_init (struct plugin_name_args *plugin_info,
127 struct plugin_gcc_version *version)
128 {
129 struct register_pass_info pass_info;
130 const char *plugin_name = plugin_info->base_name;
131 int argc = plugin_info->argc;
132 struct plugin_argument *argv = plugin_info->argv;
133
134 if (!plugin_default_version_check (version, &gcc_version))
135 return 1;
136
137 pass_info.pass = new pass_test_nesting (g);
138 pass_info.reference_pass_name = "ssa";
139 pass_info.ref_pass_instance_number = 1;
140 pass_info.pos_op = PASS_POS_INSERT_AFTER;
141 register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
142 &pass_info);
143
144 return 0;
145 }