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"
11 #include "basic-block.h"
14 #include "tree-pass.h"
17 int plugin_is_GPL_compatible;
19 /* Indicate whether to check overloaded operator '=', which is performed by
20 default. To disable it, use -fplugin-arg-NAME-no-check-operator-eq. */
21 bool check_operator_eq = true;
23 /* Given a rhs EXPR of a gimple assign statement, if it is
24 - SSA_NAME : returns its var decl, or, if it is a temp variable,
25 returns the rhs of its SSA def statement.
26 - VAR_DECL, PARM_DECL, FIELD_DECL, or a reference expression :
28 - any other expression : returns NULL_TREE. */
31 get_real_ref_rhs (tree expr)
33 switch (TREE_CODE (expr))
37 /* Given a self-assign statement, say foo.x = foo.x,
38 the IR (after SSA) looks like:
43 So if the rhs EXPR is an SSA_NAME of a temp variable,
44 e.g. D.1797_14, we need to grab the rhs of its SSA def
45 statement (i.e. foo.x). */
46 tree vdecl = SSA_NAME_VAR (expr);
47 if (DECL_ARTIFICIAL (vdecl)
48 && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
50 gimple def_stmt = SSA_NAME_DEF_STMT (expr);
51 /* We are only interested in an assignment with a single
52 rhs operand because if it is not, the original assignment
53 will not possibly be a self-assignment. */
54 if (is_gimple_assign (def_stmt)
55 && (get_gimple_rhs_class (gimple_assign_rhs_code (def_stmt))
56 == GIMPLE_SINGLE_RHS))
57 return get_real_ref_rhs (gimple_assign_rhs1 (def_stmt));
76 /* Given an expression tree, EXPR, that may contains SSA names, returns an
77 equivalent tree with the SSA names converted to var/parm/field decls
78 so that it can be used with '%E' format modifier when emitting warning
81 This function currently only supports VAR/PARM/FIELD_DECL, reference
82 expressions (COMPONENT_REF, INDIRECT_REF, ARRAY_REF), integer constant,
83 and SSA_NAME. If EXPR contains any other tree nodes (e.g. an arithmetic
84 expression appears in array index), NULL_TREE is returned. */
87 get_non_ssa_expr (tree expr)
89 switch (TREE_CODE (expr))
102 tree base, orig_base = TREE_OPERAND (expr, 0);
103 tree component, orig_component = TREE_OPERAND (expr, 1);
104 base = get_non_ssa_expr (orig_base);
107 component = get_non_ssa_expr (orig_component);
110 /* If either BASE or COMPONENT is converted, build a new
111 component reference tree. */
112 if (base != orig_base || component != orig_component)
113 return build3 (COMPONENT_REF, TREE_TYPE (component),
114 base, component, NULL_TREE);
120 tree orig_base = TREE_OPERAND (expr, 0);
121 tree base = get_non_ssa_expr (orig_base);
124 /* If BASE is converted, build a new indirect reference tree. */
125 if (base != orig_base)
126 return build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (base)), base);
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);
137 index = get_non_ssa_expr (orig_index);
140 /* If either ARRAY or INDEX is converted, build a new array
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));
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 (is_gimple_assign (def_stmt)
156 && (get_gimple_rhs_class (gimple_assign_rhs_code (def_stmt))
157 == GIMPLE_SINGLE_RHS))
158 vdecl = gimple_assign_rhs1 (def_stmt);
160 return get_non_ssa_expr (vdecl);
165 /* Return NULL_TREE for any other kind of tree nodes. */
170 /* Given the LHS and (real) RHS of a gimple assign statement, STMT, check if
171 they are the same. If so, print a warning message about self-assignment. */
174 compare_and_warn (gimple stmt, tree lhs, tree rhs)
176 if (operand_equal_p (lhs, rhs, OEP_PURE_SAME))
179 location = (gimple_has_location (stmt)
180 ? gimple_location (stmt)
182 ? DECL_SOURCE_LOCATION (lhs)
184 /* If LHS contains any tree node not currently supported by
185 get_non_ssa_expr, simply emit a generic warning without
186 specifying LHS in the message. */
187 lhs = get_non_ssa_expr (lhs);
189 warning_at (location, 0, G_("%qE is assigned to itself"), lhs);
191 warning_at (location, 0, G_("self-assignment detected"));
195 /* Check and warn if STMT is a self-assign statement. */
198 warn_self_assign (gimple stmt)
202 /* Check assigment statement. */
203 if (is_gimple_assign (stmt)
204 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
205 == GIMPLE_SINGLE_RHS))
207 rhs = get_real_ref_rhs (gimple_assign_rhs1 (stmt));
211 lhs = gimple_assign_lhs (stmt);
212 if (TREE_CODE (lhs) == SSA_NAME)
214 lhs = SSA_NAME_VAR (lhs);
215 if (DECL_ARTIFICIAL (lhs))
219 compare_and_warn (stmt, lhs, rhs);
221 /* Check overloaded operator '=' (if enabled). */
222 else if (check_operator_eq && is_gimple_call (stmt))
224 tree fdecl = gimple_call_fndecl (stmt);
225 if (fdecl && (DECL_NAME (fdecl) == maybe_get_identifier ("operator=")))
227 /* If 'operator=' takes reference operands, the arguments will be
228 ADDR_EXPR trees. In this case, just remove the address-taken
229 operator before we compare the lhs and rhs. */
230 lhs = gimple_call_arg (stmt, 0);
231 if (TREE_CODE (lhs) == ADDR_EXPR)
232 lhs = TREE_OPERAND (lhs, 0);
233 rhs = gimple_call_arg (stmt, 1);
234 if (TREE_CODE (rhs) == ADDR_EXPR)
235 rhs = TREE_OPERAND (rhs, 0);
237 compare_and_warn (stmt, lhs, rhs);
242 /* Entry point for the self-assignment detection pass. */
245 execute_warn_self_assign (void)
247 gimple_stmt_iterator gsi;
252 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
253 warn_self_assign (gsi_stmt (gsi));
259 /* Pass gate function. Currently always returns true. */
262 gate_warn_self_assign (void)
267 static struct gimple_opt_pass pass_warn_self_assign =
271 "warn_self_assign", /* name */
272 gate_warn_self_assign, /* gate */
273 execute_warn_self_assign, /* execute */
276 0, /* static_pass_number */
278 PROP_ssa, /* properties_required */
279 0, /* properties_provided */
280 0, /* properties_destroyed */
281 0, /* todo_flags_start */
282 TODO_dump_func /* todo_flags_finish */
286 /* The initialization routine exposed to and called by GCC. The spec of this
287 function is defined in gcc/gcc-plugin.h.
289 PLUGIN_NAME - name of the plugin (useful for error reporting)
290 ARGC - the size of the ARGV array
291 ARGV - an array of key-value argument pair
293 Returns 0 if initialization finishes successfully.
295 Note that this function needs to be named exactly "plugin_init". */
298 plugin_init (struct plugin_name_args *plugin_info,
299 struct plugin_gcc_version *version)
301 struct plugin_pass pass_info;
302 const char *plugin_name = plugin_info->base_name;
303 int argc = plugin_info->argc;
304 struct plugin_argument *argv = plugin_info->argv;
308 if (!plugin_default_version_check (version, version))
311 /* Self-assign detection should happen after SSA is constructed. */
312 pass_info.pass = &pass_warn_self_assign.pass;
313 pass_info.reference_pass_name = "ssa";
314 pass_info.ref_pass_instance_number = 1;
315 pass_info.pos_op = PASS_POS_INSERT_AFTER;
317 /* Process the plugin arguments. This plugin takes the following arguments:
318 check-operator-eq, no-check-operator-eq, enable, and disable.
319 By default, the analysis is enabled with 'operator=' checked. */
320 for (i = 0; i < argc; ++i)
322 if (!strcmp (argv[i].key, "check-operator-eq"))
325 warning (0, G_("option '-fplugin-arg-%s-check-operator-eq=%s'"
326 " ignored (superfluous '=%s')"),
327 plugin_name, argv[i].value, argv[i].value);
329 check_operator_eq = true;
331 else if (!strcmp (argv[i].key, "no-check-operator-eq"))
334 warning (0, G_("option '-fplugin-arg-%s-no-check-operator-eq=%s'"
335 " ignored (superfluous '=%s')"),
336 plugin_name, argv[i].value, argv[i].value);
338 check_operator_eq = false;
340 else if (!strcmp (argv[i].key, "enable"))
343 warning (0, G_("option '-fplugin-arg-%s-enable=%s' ignored"
344 " (superfluous '=%s')"),
345 plugin_name, argv[i].value, argv[i].value);
349 else if (!strcmp (argv[i].key, "disable"))
352 warning (0, G_("option '-fplugin-arg-%s-disable=%s' ignored"
353 " (superfluous '=%s')"),
354 plugin_name, argv[i].value, argv[i].value);
359 warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
360 plugin_name, argv[i].key);
363 /* Register this new pass with GCC if the analysis is enabled. */
365 register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,