Reverting merge from trunk
[official-gcc.git] / gcc / testsuite / g++.dg / plugin / selfassign.c
blob2498153a273fc6981b7451d00c447de30b0188e4
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 "gimple-iterator.h"
15 #include "tree.h"
16 #include "tree-pass.h"
17 #include "intl.h"
18 #include "plugin-version.h"
19 #include "diagnostic.h"
20 #include "context.h"
22 int plugin_is_GPL_compatible;
24 /* Indicate whether to check overloaded operator '=', which is performed by
25 default. To disable it, use -fplugin-arg-NAME-no-check-operator-eq. */
26 bool check_operator_eq = true;
28 /* Given a rhs EXPR of a gimple assign statement, if it is
29 - SSA_NAME : returns its var decl, or, if it is a temp variable,
30 returns the rhs of its SSA def statement.
31 - VAR_DECL, PARM_DECL, FIELD_DECL, or a reference expression :
32 returns EXPR itself.
33 - any other expression : returns NULL_TREE. */
35 static tree
36 get_real_ref_rhs (tree expr)
38 switch (TREE_CODE (expr))
40 case SSA_NAME:
42 /* Given a self-assign statement, say foo.x = foo.x,
43 the IR (after SSA) looks like:
45 D.1797_14 = foo.x;
46 foo.x ={v} D.1797_14;
48 So if the rhs EXPR is an SSA_NAME of a temp variable,
49 e.g. D.1797_14, we need to grab the rhs of its SSA def
50 statement (i.e. foo.x). */
51 tree vdecl = SSA_NAME_VAR (expr);
52 if ((!vdecl || DECL_ARTIFICIAL (vdecl))
53 && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
55 gimple def_stmt = SSA_NAME_DEF_STMT (expr);
56 /* We are only interested in an assignment with a single
57 rhs operand because if it is not, the original assignment
58 will not possibly be a self-assignment. */
59 if (gimple_assign_single_p (def_stmt))
60 return get_real_ref_rhs (gimple_assign_rhs1 (def_stmt));
61 else
62 return NULL_TREE;
64 else
65 return vdecl;
67 case VAR_DECL:
68 case PARM_DECL:
69 case FIELD_DECL:
70 case COMPONENT_REF:
71 case MEM_REF:
72 case ARRAY_REF:
73 return expr;
74 default:
75 return NULL_TREE;
79 /* Given an expression tree, EXPR, that may contains SSA names, returns an
80 equivalent tree with the SSA names converted to var/parm/field decls
81 so that it can be used with '%E' format modifier when emitting warning
82 messages.
84 This function currently only supports VAR/PARM/FIELD_DECL, reference
85 expressions (COMPONENT_REF, INDIRECT_REF, ARRAY_REF), integer constant,
86 and SSA_NAME. If EXPR contains any other tree nodes (e.g. an arithmetic
87 expression appears in array index), NULL_TREE is returned. */
89 static tree
90 get_non_ssa_expr (tree expr)
92 if (!expr)
93 return NULL_TREE;
94 switch (TREE_CODE (expr))
96 case VAR_DECL:
97 case PARM_DECL:
98 case FIELD_DECL:
100 if (DECL_NAME (expr))
101 return expr;
102 else
103 return NULL_TREE;
105 case COMPONENT_REF:
107 tree base, orig_base = TREE_OPERAND (expr, 0);
108 tree component, orig_component = TREE_OPERAND (expr, 1);
109 base = get_non_ssa_expr (orig_base);
110 if (!base)
111 return NULL_TREE;
112 component = get_non_ssa_expr (orig_component);
113 if (!component)
114 return NULL_TREE;
115 /* If either BASE or COMPONENT is converted, build a new
116 component reference tree. */
117 if (base != orig_base || component != orig_component)
118 return build3 (COMPONENT_REF, TREE_TYPE (component),
119 base, component, NULL_TREE);
120 else
121 return expr;
123 case MEM_REF:
125 tree orig_base = TREE_OPERAND (expr, 0);
126 if (TREE_CODE (orig_base) == SSA_NAME)
128 tree base = get_non_ssa_expr (orig_base);
129 if (!base)
130 return NULL_TREE;
131 return fold_build2 (MEM_REF, TREE_TYPE (expr),
132 base, TREE_OPERAND (expr, 1));
134 return expr;
136 case ARRAY_REF:
138 tree array, orig_array = TREE_OPERAND (expr, 0);
139 tree index, orig_index = TREE_OPERAND (expr, 1);
140 array = get_non_ssa_expr (orig_array);
141 if (!array)
142 return NULL_TREE;
143 index = get_non_ssa_expr (orig_index);
144 if (!index)
145 return NULL_TREE;
146 /* If either ARRAY or INDEX is converted, build a new array
147 reference tree. */
148 if (array != orig_array || index != orig_index)
149 return build4 (ARRAY_REF, TREE_TYPE (expr), array, index,
150 TREE_OPERAND (expr, 2), TREE_OPERAND (expr, 3));
151 else
152 return expr;
154 case SSA_NAME:
156 tree vdecl = SSA_NAME_VAR (expr);
157 if ((!vdecl || DECL_ARTIFICIAL (vdecl))
158 && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
160 gimple def_stmt = SSA_NAME_DEF_STMT (expr);
161 if (gimple_assign_single_p (def_stmt))
162 vdecl = gimple_assign_rhs1 (def_stmt);
164 return get_non_ssa_expr (vdecl);
166 case INTEGER_CST:
167 return expr;
168 default:
169 /* Return NULL_TREE for any other kind of tree nodes. */
170 return NULL_TREE;
174 /* Given the LHS and (real) RHS of a gimple assign statement, STMT, check if
175 they are the same. If so, print a warning message about self-assignment. */
177 static void
178 compare_and_warn (gimple stmt, tree lhs, tree rhs)
180 if (operand_equal_p (lhs, rhs, OEP_PURE_SAME))
182 location_t location;
183 location = (gimple_has_location (stmt)
184 ? gimple_location (stmt)
185 : (DECL_P (lhs)
186 ? DECL_SOURCE_LOCATION (lhs)
187 : input_location));
188 /* If LHS contains any tree node not currently supported by
189 get_non_ssa_expr, simply emit a generic warning without
190 specifying LHS in the message. */
191 lhs = get_non_ssa_expr (lhs);
192 if (lhs)
193 warning_at (location, 0, G_("%qE is assigned to itself"), lhs);
194 else
195 warning_at (location, 0, G_("self-assignment detected"));
199 /* Check and warn if STMT is a self-assign statement. */
201 static void
202 warn_self_assign (gimple stmt)
204 tree rhs, lhs;
206 /* Check assigment statement. */
207 if (gimple_assign_single_p (stmt))
209 rhs = get_real_ref_rhs (gimple_assign_rhs1 (stmt));
210 if (!rhs)
211 return;
213 lhs = gimple_assign_lhs (stmt);
214 if (TREE_CODE (lhs) == SSA_NAME)
216 lhs = SSA_NAME_VAR (lhs);
217 if (!lhs || DECL_ARTIFICIAL (lhs))
218 return;
221 compare_and_warn (stmt, lhs, rhs);
223 /* Check overloaded operator '=' (if enabled). */
224 else if (check_operator_eq && is_gimple_call (stmt))
226 tree fdecl = gimple_call_fndecl (stmt);
227 if (fdecl && (DECL_NAME (fdecl) == maybe_get_identifier ("operator=")))
229 /* If 'operator=' takes reference operands, the arguments will be
230 ADDR_EXPR trees. In this case, just remove the address-taken
231 operator before we compare the lhs and rhs. */
232 lhs = gimple_call_arg (stmt, 0);
233 if (TREE_CODE (lhs) == ADDR_EXPR)
234 lhs = TREE_OPERAND (lhs, 0);
235 rhs = gimple_call_arg (stmt, 1);
236 if (TREE_CODE (rhs) == ADDR_EXPR)
237 rhs = TREE_OPERAND (rhs, 0);
239 compare_and_warn (stmt, lhs, rhs);
244 /* Entry point for the self-assignment detection pass. */
246 static unsigned int
247 execute_warn_self_assign (void)
249 gimple_stmt_iterator gsi;
250 basic_block bb;
252 FOR_EACH_BB (bb)
254 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
255 warn_self_assign (gsi_stmt (gsi));
258 return 0;
261 /* Pass gate function. Currently always returns true. */
263 static bool
264 gate_warn_self_assign (void)
266 return true;
269 namespace {
271 const pass_data pass_data_warn_self_assign =
273 GIMPLE_PASS, /* type */
274 "warn_self_assign", /* name */
275 OPTGROUP_NONE, /* optinfo_flags */
276 true, /* has_gate */
277 true, /* has_execute */
278 TV_NONE, /* tv_id */
279 PROP_ssa, /* properties_required */
280 0, /* properties_provided */
281 0, /* properties_destroyed */
282 0, /* todo_flags_start */
283 0, /* todo_flags_finish */
286 class pass_warn_self_assign : public gimple_opt_pass
288 public:
289 pass_warn_self_assign(gcc::context *ctxt)
290 : gimple_opt_pass(pass_data_warn_self_assign, ctxt)
293 /* opt_pass methods: */
294 bool gate () { return gate_warn_self_assign (); }
295 unsigned int execute () { return execute_warn_self_assign (); }
297 }; // class pass_warn_self_assign
299 } // anon namespace
301 static gimple_opt_pass *
302 make_pass_warn_self_assign (gcc::context *ctxt)
304 return new pass_warn_self_assign (ctxt);
307 /* The initialization routine exposed to and called by GCC. The spec of this
308 function is defined in gcc/gcc-plugin.h.
310 PLUGIN_NAME - name of the plugin (useful for error reporting)
311 ARGC - the size of the ARGV array
312 ARGV - an array of key-value argument pair
314 Returns 0 if initialization finishes successfully.
316 Note that this function needs to be named exactly "plugin_init". */
319 plugin_init (struct plugin_name_args *plugin_info,
320 struct plugin_gcc_version *version)
322 struct register_pass_info pass_info;
323 const char *plugin_name = plugin_info->base_name;
324 int argc = plugin_info->argc;
325 struct plugin_argument *argv = plugin_info->argv;
326 bool enabled = true;
327 int i;
329 if (!plugin_default_version_check (version, &gcc_version))
330 return 1;
332 /* Self-assign detection should happen after SSA is constructed. */
333 pass_info.pass = make_pass_warn_self_assign (g);
334 pass_info.reference_pass_name = "ssa";
335 pass_info.ref_pass_instance_number = 1;
336 pass_info.pos_op = PASS_POS_INSERT_AFTER;
338 /* Process the plugin arguments. This plugin takes the following arguments:
339 check-operator-eq, no-check-operator-eq, enable, and disable.
340 By default, the analysis is enabled with 'operator=' checked. */
341 for (i = 0; i < argc; ++i)
343 if (!strcmp (argv[i].key, "check-operator-eq"))
345 if (argv[i].value)
346 warning (0, G_("option '-fplugin-arg-%s-check-operator-eq=%s'"
347 " ignored (superfluous '=%s')"),
348 plugin_name, argv[i].value, argv[i].value);
349 else
350 check_operator_eq = true;
352 else if (!strcmp (argv[i].key, "no-check-operator-eq"))
354 if (argv[i].value)
355 warning (0, G_("option '-fplugin-arg-%s-no-check-operator-eq=%s'"
356 " ignored (superfluous '=%s')"),
357 plugin_name, argv[i].value, argv[i].value);
358 else
359 check_operator_eq = false;
361 else if (!strcmp (argv[i].key, "enable"))
363 if (argv[i].value)
364 warning (0, G_("option '-fplugin-arg-%s-enable=%s' ignored"
365 " (superfluous '=%s')"),
366 plugin_name, argv[i].value, argv[i].value);
367 else
368 enabled = true;
370 else if (!strcmp (argv[i].key, "disable"))
372 if (argv[i].value)
373 warning (0, G_("option '-fplugin-arg-%s-disable=%s' ignored"
374 " (superfluous '=%s')"),
375 plugin_name, argv[i].value, argv[i].value);
376 else
377 enabled = false;
379 else
380 warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
381 plugin_name, argv[i].key);
384 /* Register this new pass with GCC if the analysis is enabled. */
385 if (enabled)
386 register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
387 &pass_info);
389 return 0;