Rebase.
[official-gcc.git] / gcc / testsuite / g++.dg / plugin / selfassign.c
blob508176ff21d6b910dd6d188938b5d86f30a5e333
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 "stringpool.h"
12 #include "toplev.h"
13 #include "basic-block.h"
14 #include "pointer-set.h"
15 #include "hash-table.h"
16 #include "vec.h"
17 #include "ggc.h"
18 #include "basic-block.h"
19 #include "tree-ssa-alias.h"
20 #include "internal-fn.h"
21 #include "gimple-fold.h"
22 #include "tree-eh.h"
23 #include "gimple-expr.h"
24 #include "is-a.h"
25 #include "gimple.h"
26 #include "gimple-iterator.h"
27 #include "tree.h"
28 #include "tree-pass.h"
29 #include "intl.h"
30 #include "plugin-version.h"
31 #include "diagnostic.h"
32 #include "context.h"
34 int plugin_is_GPL_compatible;
36 /* Indicate whether to check overloaded operator '=', which is performed by
37 default. To disable it, use -fplugin-arg-NAME-no-check-operator-eq. */
38 bool check_operator_eq = true;
40 /* Given a rhs EXPR of a gimple assign statement, if it is
41 - SSA_NAME : returns its var decl, or, if it is a temp variable,
42 returns the rhs of its SSA def statement.
43 - VAR_DECL, PARM_DECL, FIELD_DECL, or a reference expression :
44 returns EXPR itself.
45 - any other expression : returns NULL_TREE. */
47 static tree
48 get_real_ref_rhs (tree expr)
50 switch (TREE_CODE (expr))
52 case SSA_NAME:
54 /* Given a self-assign statement, say foo.x = foo.x,
55 the IR (after SSA) looks like:
57 D.1797_14 = foo.x;
58 foo.x ={v} D.1797_14;
60 So if the rhs EXPR is an SSA_NAME of a temp variable,
61 e.g. D.1797_14, we need to grab the rhs of its SSA def
62 statement (i.e. foo.x). */
63 tree vdecl = SSA_NAME_VAR (expr);
64 if ((!vdecl || DECL_ARTIFICIAL (vdecl))
65 && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
67 gimple def_stmt = SSA_NAME_DEF_STMT (expr);
68 /* We are only interested in an assignment with a single
69 rhs operand because if it is not, the original assignment
70 will not possibly be a self-assignment. */
71 if (gimple_assign_single_p (def_stmt))
72 return get_real_ref_rhs (gimple_assign_rhs1 (def_stmt));
73 else
74 return NULL_TREE;
76 else
77 return vdecl;
79 case VAR_DECL:
80 case PARM_DECL:
81 case FIELD_DECL:
82 case COMPONENT_REF:
83 case MEM_REF:
84 case ARRAY_REF:
85 return expr;
86 default:
87 return NULL_TREE;
91 /* Given an expression tree, EXPR, that may contains SSA names, returns an
92 equivalent tree with the SSA names converted to var/parm/field decls
93 so that it can be used with '%E' format modifier when emitting warning
94 messages.
96 This function currently only supports VAR/PARM/FIELD_DECL, reference
97 expressions (COMPONENT_REF, INDIRECT_REF, ARRAY_REF), integer constant,
98 and SSA_NAME. If EXPR contains any other tree nodes (e.g. an arithmetic
99 expression appears in array index), NULL_TREE is returned. */
101 static tree
102 get_non_ssa_expr (tree expr)
104 if (!expr)
105 return NULL_TREE;
106 switch (TREE_CODE (expr))
108 case VAR_DECL:
109 case PARM_DECL:
110 case FIELD_DECL:
112 if (DECL_NAME (expr))
113 return expr;
114 else
115 return NULL_TREE;
117 case COMPONENT_REF:
119 tree base, orig_base = TREE_OPERAND (expr, 0);
120 tree component, orig_component = TREE_OPERAND (expr, 1);
121 base = get_non_ssa_expr (orig_base);
122 if (!base)
123 return NULL_TREE;
124 component = get_non_ssa_expr (orig_component);
125 if (!component)
126 return NULL_TREE;
127 /* If either BASE or COMPONENT is converted, build a new
128 component reference tree. */
129 if (base != orig_base || component != orig_component)
130 return build3 (COMPONENT_REF, TREE_TYPE (component),
131 base, component, NULL_TREE);
132 else
133 return expr;
135 case MEM_REF:
137 tree orig_base = TREE_OPERAND (expr, 0);
138 if (TREE_CODE (orig_base) == SSA_NAME)
140 tree base = get_non_ssa_expr (orig_base);
141 if (!base)
142 return NULL_TREE;
143 return fold_build2 (MEM_REF, TREE_TYPE (expr),
144 base, TREE_OPERAND (expr, 1));
146 return expr;
148 case ARRAY_REF:
150 tree array, orig_array = TREE_OPERAND (expr, 0);
151 tree index, orig_index = TREE_OPERAND (expr, 1);
152 array = get_non_ssa_expr (orig_array);
153 if (!array)
154 return NULL_TREE;
155 index = get_non_ssa_expr (orig_index);
156 if (!index)
157 return NULL_TREE;
158 /* If either ARRAY or INDEX is converted, build a new array
159 reference tree. */
160 if (array != orig_array || index != orig_index)
161 return build4 (ARRAY_REF, TREE_TYPE (expr), array, index,
162 TREE_OPERAND (expr, 2), TREE_OPERAND (expr, 3));
163 else
164 return expr;
166 case SSA_NAME:
168 tree vdecl = SSA_NAME_VAR (expr);
169 if ((!vdecl || DECL_ARTIFICIAL (vdecl))
170 && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
172 gimple def_stmt = SSA_NAME_DEF_STMT (expr);
173 if (gimple_assign_single_p (def_stmt))
174 vdecl = gimple_assign_rhs1 (def_stmt);
176 return get_non_ssa_expr (vdecl);
178 case INTEGER_CST:
179 return expr;
180 default:
181 /* Return NULL_TREE for any other kind of tree nodes. */
182 return NULL_TREE;
186 /* Given the LHS and (real) RHS of a gimple assign statement, STMT, check if
187 they are the same. If so, print a warning message about self-assignment. */
189 static void
190 compare_and_warn (gimple stmt, tree lhs, tree rhs)
192 if (operand_equal_p (lhs, rhs, OEP_PURE_SAME))
194 location_t location;
195 location = (gimple_has_location (stmt)
196 ? gimple_location (stmt)
197 : (DECL_P (lhs)
198 ? DECL_SOURCE_LOCATION (lhs)
199 : input_location));
200 /* If LHS contains any tree node not currently supported by
201 get_non_ssa_expr, simply emit a generic warning without
202 specifying LHS in the message. */
203 lhs = get_non_ssa_expr (lhs);
204 if (lhs)
205 warning_at (location, 0, G_("%qE is assigned to itself"), lhs);
206 else
207 warning_at (location, 0, G_("self-assignment detected"));
211 /* Check and warn if STMT is a self-assign statement. */
213 static void
214 warn_self_assign (gimple stmt)
216 tree rhs, lhs;
218 /* Check assigment statement. */
219 if (gimple_assign_single_p (stmt))
221 rhs = get_real_ref_rhs (gimple_assign_rhs1 (stmt));
222 if (!rhs)
223 return;
225 lhs = gimple_assign_lhs (stmt);
226 if (TREE_CODE (lhs) == SSA_NAME)
228 lhs = SSA_NAME_VAR (lhs);
229 if (!lhs || DECL_ARTIFICIAL (lhs))
230 return;
233 compare_and_warn (stmt, lhs, rhs);
235 /* Check overloaded operator '=' (if enabled). */
236 else if (check_operator_eq && is_gimple_call (stmt))
238 tree fdecl = gimple_call_fndecl (stmt);
239 if (fdecl && (DECL_NAME (fdecl) == maybe_get_identifier ("operator=")))
241 /* If 'operator=' takes reference operands, the arguments will be
242 ADDR_EXPR trees. In this case, just remove the address-taken
243 operator before we compare the lhs and rhs. */
244 lhs = gimple_call_arg (stmt, 0);
245 if (TREE_CODE (lhs) == ADDR_EXPR)
246 lhs = TREE_OPERAND (lhs, 0);
247 rhs = gimple_call_arg (stmt, 1);
248 if (TREE_CODE (rhs) == ADDR_EXPR)
249 rhs = TREE_OPERAND (rhs, 0);
251 compare_and_warn (stmt, lhs, rhs);
256 namespace {
258 const pass_data pass_data_warn_self_assign =
260 GIMPLE_PASS, /* type */
261 "warn_self_assign", /* name */
262 OPTGROUP_NONE, /* optinfo_flags */
263 TV_NONE, /* tv_id */
264 PROP_ssa, /* properties_required */
265 0, /* properties_provided */
266 0, /* properties_destroyed */
267 0, /* todo_flags_start */
268 0, /* todo_flags_finish */
271 class pass_warn_self_assign : public gimple_opt_pass
273 public:
274 pass_warn_self_assign(gcc::context *ctxt)
275 : gimple_opt_pass(pass_data_warn_self_assign, ctxt)
278 /* opt_pass methods: */
279 bool gate (function *) { return true; }
280 virtual unsigned int execute (function *);
282 }; // class pass_warn_self_assign
284 unsigned int
285 pass_warn_self_assign::execute (function *fun)
287 gimple_stmt_iterator gsi;
288 basic_block bb;
290 FOR_EACH_BB_FN (bb, fun)
292 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
293 warn_self_assign (gsi_stmt (gsi));
296 return 0;
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;