Re-factor inclusion of tree.h.
[official-gcc.git] / gcc / testsuite / g++.dg / plugin / selfassign.c
blob5331f792cb2b5b83ccd6d5fd3c154d41c309c00d
1 /* This plugin contains an analysis pass that detects and warns about
2 self-assignment statements. */
3 /* { dg-options "-O" } */
5 #include "gcc-plugin.h"
6 #include "config.h"
7 #include "system.h"
8 #include "coretypes.h"
9 #include "tm.h"
10 #include "tree.h"
11 #include "toplev.h"
12 #include "basic-block.h"
13 #include "gimple.h"
14 #include "tree.h"
15 #include "tree-pass.h"
16 #include "intl.h"
17 #include "plugin-version.h"
18 #include "diagnostic.h"
19 #include "context.h"
21 int plugin_is_GPL_compatible;
23 /* Indicate whether to check overloaded operator '=', which is performed by
24 default. To disable it, use -fplugin-arg-NAME-no-check-operator-eq. */
25 bool check_operator_eq = true;
27 /* Given a rhs EXPR of a gimple assign statement, if it is
28 - SSA_NAME : returns its var decl, or, if it is a temp variable,
29 returns the rhs of its SSA def statement.
30 - VAR_DECL, PARM_DECL, FIELD_DECL, or a reference expression :
31 returns EXPR itself.
32 - any other expression : returns NULL_TREE. */
34 static tree
35 get_real_ref_rhs (tree expr)
37 switch (TREE_CODE (expr))
39 case SSA_NAME:
41 /* Given a self-assign statement, say foo.x = foo.x,
42 the IR (after SSA) looks like:
44 D.1797_14 = foo.x;
45 foo.x ={v} D.1797_14;
47 So if the rhs EXPR is an SSA_NAME of a temp variable,
48 e.g. D.1797_14, we need to grab the rhs of its SSA def
49 statement (i.e. foo.x). */
50 tree vdecl = SSA_NAME_VAR (expr);
51 if ((!vdecl || DECL_ARTIFICIAL (vdecl))
52 && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
54 gimple def_stmt = SSA_NAME_DEF_STMT (expr);
55 /* We are only interested in an assignment with a single
56 rhs operand because if it is not, the original assignment
57 will not possibly be a self-assignment. */
58 if (gimple_assign_single_p (def_stmt))
59 return get_real_ref_rhs (gimple_assign_rhs1 (def_stmt));
60 else
61 return NULL_TREE;
63 else
64 return vdecl;
66 case VAR_DECL:
67 case PARM_DECL:
68 case FIELD_DECL:
69 case COMPONENT_REF:
70 case MEM_REF:
71 case ARRAY_REF:
72 return expr;
73 default:
74 return NULL_TREE;
78 /* Given an expression tree, EXPR, that may contains SSA names, returns an
79 equivalent tree with the SSA names converted to var/parm/field decls
80 so that it can be used with '%E' format modifier when emitting warning
81 messages.
83 This function currently only supports VAR/PARM/FIELD_DECL, reference
84 expressions (COMPONENT_REF, INDIRECT_REF, ARRAY_REF), integer constant,
85 and SSA_NAME. If EXPR contains any other tree nodes (e.g. an arithmetic
86 expression appears in array index), NULL_TREE is returned. */
88 static tree
89 get_non_ssa_expr (tree expr)
91 if (!expr)
92 return NULL_TREE;
93 switch (TREE_CODE (expr))
95 case VAR_DECL:
96 case PARM_DECL:
97 case FIELD_DECL:
99 if (DECL_NAME (expr))
100 return expr;
101 else
102 return NULL_TREE;
104 case COMPONENT_REF:
106 tree base, orig_base = TREE_OPERAND (expr, 0);
107 tree component, orig_component = TREE_OPERAND (expr, 1);
108 base = get_non_ssa_expr (orig_base);
109 if (!base)
110 return NULL_TREE;
111 component = get_non_ssa_expr (orig_component);
112 if (!component)
113 return NULL_TREE;
114 /* If either BASE or COMPONENT is converted, build a new
115 component reference tree. */
116 if (base != orig_base || component != orig_component)
117 return build3 (COMPONENT_REF, TREE_TYPE (component),
118 base, component, NULL_TREE);
119 else
120 return expr;
122 case MEM_REF:
124 tree orig_base = TREE_OPERAND (expr, 0);
125 if (TREE_CODE (orig_base) == SSA_NAME)
127 tree base = get_non_ssa_expr (orig_base);
128 if (!base)
129 return NULL_TREE;
130 return fold_build2 (MEM_REF, TREE_TYPE (expr),
131 base, TREE_OPERAND (expr, 1));
133 return expr;
135 case ARRAY_REF:
137 tree array, orig_array = TREE_OPERAND (expr, 0);
138 tree index, orig_index = TREE_OPERAND (expr, 1);
139 array = get_non_ssa_expr (orig_array);
140 if (!array)
141 return NULL_TREE;
142 index = get_non_ssa_expr (orig_index);
143 if (!index)
144 return NULL_TREE;
145 /* If either ARRAY or INDEX is converted, build a new array
146 reference tree. */
147 if (array != orig_array || index != orig_index)
148 return build4 (ARRAY_REF, TREE_TYPE (expr), array, index,
149 TREE_OPERAND (expr, 2), TREE_OPERAND (expr, 3));
150 else
151 return expr;
153 case SSA_NAME:
155 tree vdecl = SSA_NAME_VAR (expr);
156 if ((!vdecl || DECL_ARTIFICIAL (vdecl))
157 && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
159 gimple def_stmt = SSA_NAME_DEF_STMT (expr);
160 if (gimple_assign_single_p (def_stmt))
161 vdecl = gimple_assign_rhs1 (def_stmt);
163 return get_non_ssa_expr (vdecl);
165 case INTEGER_CST:
166 return expr;
167 default:
168 /* Return NULL_TREE for any other kind of tree nodes. */
169 return NULL_TREE;
173 /* Given the LHS and (real) RHS of a gimple assign statement, STMT, check if
174 they are the same. If so, print a warning message about self-assignment. */
176 static void
177 compare_and_warn (gimple stmt, tree lhs, tree rhs)
179 if (operand_equal_p (lhs, rhs, OEP_PURE_SAME))
181 location_t location;
182 location = (gimple_has_location (stmt)
183 ? gimple_location (stmt)
184 : (DECL_P (lhs)
185 ? DECL_SOURCE_LOCATION (lhs)
186 : input_location));
187 /* If LHS contains any tree node not currently supported by
188 get_non_ssa_expr, simply emit a generic warning without
189 specifying LHS in the message. */
190 lhs = get_non_ssa_expr (lhs);
191 if (lhs)
192 warning_at (location, 0, G_("%qE is assigned to itself"), lhs);
193 else
194 warning_at (location, 0, G_("self-assignment detected"));
198 /* Check and warn if STMT is a self-assign statement. */
200 static void
201 warn_self_assign (gimple stmt)
203 tree rhs, lhs;
205 /* Check assigment statement. */
206 if (gimple_assign_single_p (stmt))
208 rhs = get_real_ref_rhs (gimple_assign_rhs1 (stmt));
209 if (!rhs)
210 return;
212 lhs = gimple_assign_lhs (stmt);
213 if (TREE_CODE (lhs) == SSA_NAME)
215 lhs = SSA_NAME_VAR (lhs);
216 if (!lhs || DECL_ARTIFICIAL (lhs))
217 return;
220 compare_and_warn (stmt, lhs, rhs);
222 /* Check overloaded operator '=' (if enabled). */
223 else if (check_operator_eq && is_gimple_call (stmt))
225 tree fdecl = gimple_call_fndecl (stmt);
226 if (fdecl && (DECL_NAME (fdecl) == maybe_get_identifier ("operator=")))
228 /* If 'operator=' takes reference operands, the arguments will be
229 ADDR_EXPR trees. In this case, just remove the address-taken
230 operator before we compare the lhs and rhs. */
231 lhs = gimple_call_arg (stmt, 0);
232 if (TREE_CODE (lhs) == ADDR_EXPR)
233 lhs = TREE_OPERAND (lhs, 0);
234 rhs = gimple_call_arg (stmt, 1);
235 if (TREE_CODE (rhs) == ADDR_EXPR)
236 rhs = TREE_OPERAND (rhs, 0);
238 compare_and_warn (stmt, lhs, rhs);
243 /* Entry point for the self-assignment detection pass. */
245 static unsigned int
246 execute_warn_self_assign (void)
248 gimple_stmt_iterator gsi;
249 basic_block bb;
251 FOR_EACH_BB (bb)
253 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
254 warn_self_assign (gsi_stmt (gsi));
257 return 0;
260 /* Pass gate function. Currently always returns true. */
262 static bool
263 gate_warn_self_assign (void)
265 return true;
268 namespace {
270 const pass_data pass_data_warn_self_assign =
272 GIMPLE_PASS, /* type */
273 "warn_self_assign", /* name */
274 OPTGROUP_NONE, /* optinfo_flags */
275 true, /* has_gate */
276 true, /* has_execute */
277 TV_NONE, /* tv_id */
278 PROP_ssa, /* properties_required */
279 0, /* properties_provided */
280 0, /* properties_destroyed */
281 0, /* todo_flags_start */
282 0, /* todo_flags_finish */
285 class pass_warn_self_assign : public gimple_opt_pass
287 public:
288 pass_warn_self_assign(gcc::context *ctxt)
289 : gimple_opt_pass(pass_data_warn_self_assign, ctxt)
292 /* opt_pass methods: */
293 bool gate () { return gate_warn_self_assign (); }
294 unsigned int execute () { return execute_warn_self_assign (); }
296 }; // class pass_warn_self_assign
298 } // anon namespace
300 static gimple_opt_pass *
301 make_pass_warn_self_assign (gcc::context *ctxt)
303 return new pass_warn_self_assign (ctxt);
306 /* The initialization routine exposed to and called by GCC. The spec of this
307 function is defined in gcc/gcc-plugin.h.
309 PLUGIN_NAME - name of the plugin (useful for error reporting)
310 ARGC - the size of the ARGV array
311 ARGV - an array of key-value argument pair
313 Returns 0 if initialization finishes successfully.
315 Note that this function needs to be named exactly "plugin_init". */
318 plugin_init (struct plugin_name_args *plugin_info,
319 struct plugin_gcc_version *version)
321 struct register_pass_info pass_info;
322 const char *plugin_name = plugin_info->base_name;
323 int argc = plugin_info->argc;
324 struct plugin_argument *argv = plugin_info->argv;
325 bool enabled = true;
326 int i;
328 if (!plugin_default_version_check (version, &gcc_version))
329 return 1;
331 /* Self-assign detection should happen after SSA is constructed. */
332 pass_info.pass = make_pass_warn_self_assign (g);
333 pass_info.reference_pass_name = "ssa";
334 pass_info.ref_pass_instance_number = 1;
335 pass_info.pos_op = PASS_POS_INSERT_AFTER;
337 /* Process the plugin arguments. This plugin takes the following arguments:
338 check-operator-eq, no-check-operator-eq, enable, and disable.
339 By default, the analysis is enabled with 'operator=' checked. */
340 for (i = 0; i < argc; ++i)
342 if (!strcmp (argv[i].key, "check-operator-eq"))
344 if (argv[i].value)
345 warning (0, G_("option '-fplugin-arg-%s-check-operator-eq=%s'"
346 " ignored (superfluous '=%s')"),
347 plugin_name, argv[i].value, argv[i].value);
348 else
349 check_operator_eq = true;
351 else if (!strcmp (argv[i].key, "no-check-operator-eq"))
353 if (argv[i].value)
354 warning (0, G_("option '-fplugin-arg-%s-no-check-operator-eq=%s'"
355 " ignored (superfluous '=%s')"),
356 plugin_name, argv[i].value, argv[i].value);
357 else
358 check_operator_eq = false;
360 else if (!strcmp (argv[i].key, "enable"))
362 if (argv[i].value)
363 warning (0, G_("option '-fplugin-arg-%s-enable=%s' ignored"
364 " (superfluous '=%s')"),
365 plugin_name, argv[i].value, argv[i].value);
366 else
367 enabled = true;
369 else if (!strcmp (argv[i].key, "disable"))
371 if (argv[i].value)
372 warning (0, G_("option '-fplugin-arg-%s-disable=%s' ignored"
373 " (superfluous '=%s')"),
374 plugin_name, argv[i].value, argv[i].value);
375 else
376 enabled = false;
378 else
379 warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
380 plugin_name, argv[i].key);
383 /* Register this new pass with GCC if the analysis is enabled. */
384 if (enabled)
385 register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
386 &pass_info);
388 return 0;