Merged trunk at revision 161680 into branch.
[official-gcc.git] / gcc / testsuite / g++.dg / plugin / selfassign.c
blobeb8f24a45a8e301c432e94fb07a04ec8e675a38a
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 "toplev.h"
11 #include "basic-block.h"
12 #include "gimple.h"
13 #include "tree.h"
14 #include "tree-pass.h"
15 #include "intl.h"
16 #include "plugin-version.h"
18 int plugin_is_GPL_compatible;
20 /* Indicate whether to check overloaded operator '=', which is performed by
21 default. To disable it, use -fplugin-arg-NAME-no-check-operator-eq. */
22 bool check_operator_eq = true;
24 /* Given a rhs EXPR of a gimple assign statement, if it is
25 - SSA_NAME : returns its var decl, or, if it is a temp variable,
26 returns the rhs of its SSA def statement.
27 - VAR_DECL, PARM_DECL, FIELD_DECL, or a reference expression :
28 returns EXPR itself.
29 - any other expression : returns NULL_TREE. */
31 static tree
32 get_real_ref_rhs (tree expr)
34 switch (TREE_CODE (expr))
36 case SSA_NAME:
38 /* Given a self-assign statement, say foo.x = foo.x,
39 the IR (after SSA) looks like:
41 D.1797_14 = foo.x;
42 foo.x ={v} D.1797_14;
44 So if the rhs EXPR is an SSA_NAME of a temp variable,
45 e.g. D.1797_14, we need to grab the rhs of its SSA def
46 statement (i.e. foo.x). */
47 tree vdecl = SSA_NAME_VAR (expr);
48 if (DECL_ARTIFICIAL (vdecl)
49 && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
51 gimple def_stmt = SSA_NAME_DEF_STMT (expr);
52 /* We are only interested in an assignment with a single
53 rhs operand because if it is not, the original assignment
54 will not possibly be a self-assignment. */
55 if (gimple_assign_single_p (def_stmt))
56 return get_real_ref_rhs (gimple_assign_rhs1 (def_stmt));
57 else
58 return NULL_TREE;
60 else
61 return vdecl;
63 case VAR_DECL:
64 case PARM_DECL:
65 case FIELD_DECL:
66 case COMPONENT_REF:
67 case MEM_REF:
68 case ARRAY_REF:
69 return expr;
70 default:
71 return NULL_TREE;
75 /* Given an expression tree, EXPR, that may contains SSA names, returns an
76 equivalent tree with the SSA names converted to var/parm/field decls
77 so that it can be used with '%E' format modifier when emitting warning
78 messages.
80 This function currently only supports VAR/PARM/FIELD_DECL, reference
81 expressions (COMPONENT_REF, INDIRECT_REF, ARRAY_REF), integer constant,
82 and SSA_NAME. If EXPR contains any other tree nodes (e.g. an arithmetic
83 expression appears in array index), NULL_TREE is returned. */
85 static tree
86 get_non_ssa_expr (tree expr)
88 switch (TREE_CODE (expr))
90 case VAR_DECL:
91 case PARM_DECL:
92 case FIELD_DECL:
94 if (DECL_NAME (expr))
95 return expr;
96 else
97 return NULL_TREE;
99 case COMPONENT_REF:
101 tree base, orig_base = TREE_OPERAND (expr, 0);
102 tree component, orig_component = TREE_OPERAND (expr, 1);
103 base = get_non_ssa_expr (orig_base);
104 if (!base)
105 return NULL_TREE;
106 component = get_non_ssa_expr (orig_component);
107 if (!component)
108 return NULL_TREE;
109 /* If either BASE or COMPONENT is converted, build a new
110 component reference tree. */
111 if (base != orig_base || component != orig_component)
112 return build3 (COMPONENT_REF, TREE_TYPE (component),
113 base, component, NULL_TREE);
114 else
115 return expr;
117 case MEM_REF:
119 tree orig_base = TREE_OPERAND (expr, 0);
120 if (TREE_CODE (orig_base) == SSA_NAME)
122 tree base = get_non_ssa_expr (orig_base);
123 if (!base)
124 return NULL_TREE;
125 return fold_build2 (MEM_REF, TREE_TYPE (expr),
126 base, TREE_OPERAND (expr, 1));
128 return expr;
130 case ARRAY_REF:
132 tree array, orig_array = TREE_OPERAND (expr, 0);
133 tree index, orig_index = TREE_OPERAND (expr, 1);
134 array = get_non_ssa_expr (orig_array);
135 if (!array)
136 return NULL_TREE;
137 index = get_non_ssa_expr (orig_index);
138 if (!index)
139 return NULL_TREE;
140 /* If either ARRAY or INDEX is converted, build a new array
141 reference tree. */
142 if (array != orig_array || index != orig_index)
143 return build4 (ARRAY_REF, TREE_TYPE (expr), array, index,
144 TREE_OPERAND (expr, 2), TREE_OPERAND (expr, 3));
145 else
146 return expr;
148 case SSA_NAME:
150 tree vdecl = SSA_NAME_VAR (expr);
151 if (DECL_ARTIFICIAL (vdecl)
152 && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
154 gimple def_stmt = SSA_NAME_DEF_STMT (expr);
155 if (gimple_assign_single_p (def_stmt))
156 vdecl = gimple_assign_rhs1 (def_stmt);
158 return get_non_ssa_expr (vdecl);
160 case INTEGER_CST:
161 return expr;
162 default:
163 /* Return NULL_TREE for any other kind of tree nodes. */
164 return NULL_TREE;
168 /* Given the LHS and (real) RHS of a gimple assign statement, STMT, check if
169 they are the same. If so, print a warning message about self-assignment. */
171 static void
172 compare_and_warn (gimple stmt, tree lhs, tree rhs)
174 if (operand_equal_p (lhs, rhs, OEP_PURE_SAME))
176 location_t location;
177 location = (gimple_has_location (stmt)
178 ? gimple_location (stmt)
179 : (DECL_P (lhs)
180 ? DECL_SOURCE_LOCATION (lhs)
181 : input_location));
182 /* If LHS contains any tree node not currently supported by
183 get_non_ssa_expr, simply emit a generic warning without
184 specifying LHS in the message. */
185 lhs = get_non_ssa_expr (lhs);
186 if (lhs)
187 warning_at (location, 0, G_("%qE is assigned to itself"), lhs);
188 else
189 warning_at (location, 0, G_("self-assignment detected"));
193 /* Check and warn if STMT is a self-assign statement. */
195 static void
196 warn_self_assign (gimple stmt)
198 tree rhs, lhs;
200 /* Check assigment statement. */
201 if (gimple_assign_single_p (stmt))
203 rhs = get_real_ref_rhs (gimple_assign_rhs1 (stmt));
204 if (!rhs)
205 return;
207 lhs = gimple_assign_lhs (stmt);
208 if (TREE_CODE (lhs) == SSA_NAME)
210 lhs = SSA_NAME_VAR (lhs);
211 if (DECL_ARTIFICIAL (lhs))
212 return;
215 compare_and_warn (stmt, lhs, rhs);
217 /* Check overloaded operator '=' (if enabled). */
218 else if (check_operator_eq && is_gimple_call (stmt))
220 tree fdecl = gimple_call_fndecl (stmt);
221 if (fdecl && (DECL_NAME (fdecl) == maybe_get_identifier ("operator=")))
223 /* If 'operator=' takes reference operands, the arguments will be
224 ADDR_EXPR trees. In this case, just remove the address-taken
225 operator before we compare the lhs and rhs. */
226 lhs = gimple_call_arg (stmt, 0);
227 if (TREE_CODE (lhs) == ADDR_EXPR)
228 lhs = TREE_OPERAND (lhs, 0);
229 rhs = gimple_call_arg (stmt, 1);
230 if (TREE_CODE (rhs) == ADDR_EXPR)
231 rhs = TREE_OPERAND (rhs, 0);
233 compare_and_warn (stmt, lhs, rhs);
238 /* Entry point for the self-assignment detection pass. */
240 static unsigned int
241 execute_warn_self_assign (void)
243 gimple_stmt_iterator gsi;
244 basic_block bb;
246 FOR_EACH_BB (bb)
248 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
249 warn_self_assign (gsi_stmt (gsi));
252 return 0;
255 /* Pass gate function. Currently always returns true. */
257 static bool
258 gate_warn_self_assign (void)
260 return true;
263 static struct gimple_opt_pass pass_warn_self_assign =
266 GIMPLE_PASS,
267 "warn_self_assign", /* name */
268 gate_warn_self_assign, /* gate */
269 execute_warn_self_assign, /* execute */
270 NULL, /* sub */
271 NULL, /* next */
272 0, /* static_pass_number */
273 TV_NONE, /* tv_id */
274 PROP_ssa, /* properties_required */
275 0, /* properties_provided */
276 0, /* properties_destroyed */
277 0, /* todo_flags_start */
278 TODO_dump_func /* todo_flags_finish */
282 /* The initialization routine exposed to and called by GCC. The spec of this
283 function is defined in gcc/gcc-plugin.h.
285 PLUGIN_NAME - name of the plugin (useful for error reporting)
286 ARGC - the size of the ARGV array
287 ARGV - an array of key-value argument pair
289 Returns 0 if initialization finishes successfully.
291 Note that this function needs to be named exactly "plugin_init". */
294 plugin_init (struct plugin_name_args *plugin_info,
295 struct plugin_gcc_version *version)
297 struct register_pass_info pass_info;
298 const char *plugin_name = plugin_info->base_name;
299 int argc = plugin_info->argc;
300 struct plugin_argument *argv = plugin_info->argv;
301 bool enabled = true;
302 int i;
304 if (!plugin_default_version_check (version, &gcc_version))
305 return 1;
307 /* Self-assign detection should happen after SSA is constructed. */
308 pass_info.pass = &pass_warn_self_assign.pass;
309 pass_info.reference_pass_name = "ssa";
310 pass_info.ref_pass_instance_number = 1;
311 pass_info.pos_op = PASS_POS_INSERT_AFTER;
313 /* Process the plugin arguments. This plugin takes the following arguments:
314 check-operator-eq, no-check-operator-eq, enable, and disable.
315 By default, the analysis is enabled with 'operator=' checked. */
316 for (i = 0; i < argc; ++i)
318 if (!strcmp (argv[i].key, "check-operator-eq"))
320 if (argv[i].value)
321 warning (0, G_("option '-fplugin-arg-%s-check-operator-eq=%s'"
322 " ignored (superfluous '=%s')"),
323 plugin_name, argv[i].value, argv[i].value);
324 else
325 check_operator_eq = true;
327 else if (!strcmp (argv[i].key, "no-check-operator-eq"))
329 if (argv[i].value)
330 warning (0, G_("option '-fplugin-arg-%s-no-check-operator-eq=%s'"
331 " ignored (superfluous '=%s')"),
332 plugin_name, argv[i].value, argv[i].value);
333 else
334 check_operator_eq = false;
336 else if (!strcmp (argv[i].key, "enable"))
338 if (argv[i].value)
339 warning (0, G_("option '-fplugin-arg-%s-enable=%s' ignored"
340 " (superfluous '=%s')"),
341 plugin_name, argv[i].value, argv[i].value);
342 else
343 enabled = true;
345 else if (!strcmp (argv[i].key, "disable"))
347 if (argv[i].value)
348 warning (0, G_("option '-fplugin-arg-%s-disable=%s' ignored"
349 " (superfluous '=%s')"),
350 plugin_name, argv[i].value, argv[i].value);
351 else
352 enabled = false;
354 else
355 warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
356 plugin_name, argv[i].key);
359 /* Register this new pass with GCC if the analysis is enabled. */
360 if (enabled)
361 register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
362 &pass_info);
364 return 0;