1 /* This plugin contains an analysis pass that detects and warns about
2    self-assignment statements.  */
3 /* { dg-options "-O" } */
4 
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"
17 #include "diagnostic.h"
18 
19 int plugin_is_GPL_compatible;
20 
21 /* Indicate whether to check overloaded operator '=', which is performed by
22    default. To disable it, use -fplugin-arg-NAME-no-check-operator-eq.  */
23 bool check_operator_eq = true;
24 
25 /* Given a rhs EXPR of a gimple assign statement, if it is
26    - SSA_NAME : returns its var decl, or, if it is a temp variable,
27                 returns the rhs of its SSA def statement.
28    - VAR_DECL, PARM_DECL, FIELD_DECL, or a reference expression :
29                 returns EXPR itself.
30    - any other expression : returns NULL_TREE.  */
31 
32 static tree
get_real_ref_rhs(tree expr)33 get_real_ref_rhs (tree expr)
34 {
35   switch (TREE_CODE (expr))
36     {
37       case SSA_NAME:
38         {
39           /* Given a self-assign statement, say foo.x = foo.x,
40              the IR (after SSA) looks like:
41 
42              D.1797_14 = foo.x;
43              foo.x ={v} D.1797_14;
44 
45              So if the rhs EXPR is an SSA_NAME of a temp variable,
46              e.g. D.1797_14, we need to grab the rhs of its SSA def
47              statement (i.e. foo.x).  */
48           tree vdecl = SSA_NAME_VAR (expr);
49           if ((!vdecl || DECL_ARTIFICIAL (vdecl))
50               && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
51             {
52               gimple def_stmt = SSA_NAME_DEF_STMT (expr);
53               /* We are only interested in an assignment with a single
54                  rhs operand because if it is not, the original assignment
55                  will not possibly be a self-assignment.  */
56               if (gimple_assign_single_p (def_stmt))
57                 return get_real_ref_rhs (gimple_assign_rhs1 (def_stmt));
58               else
59                 return NULL_TREE;
60             }
61           else
62             return vdecl;
63         }
64       case VAR_DECL:
65       case PARM_DECL:
66       case FIELD_DECL:
67       case COMPONENT_REF:
68       case MEM_REF:
69       case ARRAY_REF:
70         return expr;
71       default:
72         return NULL_TREE;
73     }
74 }
75 
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
79    messages.
80 
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.  */
85 
86 static tree
get_non_ssa_expr(tree expr)87 get_non_ssa_expr (tree expr)
88 {
89   if (!expr)
90     return NULL_TREE;
91   switch (TREE_CODE (expr))
92     {
93       case VAR_DECL:
94       case PARM_DECL:
95       case FIELD_DECL:
96         {
97           if (DECL_NAME (expr))
98             return expr;
99           else
100             return NULL_TREE;
101         }
102       case COMPONENT_REF:
103         {
104           tree base, orig_base = TREE_OPERAND (expr, 0);
105           tree component, orig_component = TREE_OPERAND (expr, 1);
106           base = get_non_ssa_expr (orig_base);
107           if (!base)
108             return NULL_TREE;
109           component = get_non_ssa_expr (orig_component);
110           if (!component)
111             return NULL_TREE;
112           /* If either BASE or COMPONENT is converted, build a new
113              component reference tree.  */
114           if (base != orig_base || component != orig_component)
115             return build3 (COMPONENT_REF, TREE_TYPE (component),
116                            base, component, NULL_TREE);
117           else
118             return expr;
119         }
120       case MEM_REF:
121         {
122           tree orig_base = TREE_OPERAND (expr, 0);
123 	  if (TREE_CODE (orig_base) == SSA_NAME)
124 	    {
125 	      tree base = get_non_ssa_expr (orig_base);
126 	      if (!base)
127 		return NULL_TREE;
128 	      return fold_build2 (MEM_REF, TREE_TYPE (expr),
129 				  base, TREE_OPERAND (expr, 1));
130 	    }
131 	  return expr;
132         }
133       case ARRAY_REF:
134         {
135           tree array, orig_array = TREE_OPERAND (expr, 0);
136           tree index, orig_index = TREE_OPERAND (expr, 1);
137           array = get_non_ssa_expr (orig_array);
138           if (!array)
139             return NULL_TREE;
140           index = get_non_ssa_expr (orig_index);
141           if (!index)
142             return NULL_TREE;
143           /* If either ARRAY or INDEX is converted, build a new array
144              reference tree.  */
145           if (array != orig_array || index != orig_index)
146             return build4 (ARRAY_REF, TREE_TYPE (expr), array, index,
147                            TREE_OPERAND (expr, 2), TREE_OPERAND (expr, 3));
148           else
149             return expr;
150         }
151       case SSA_NAME:
152         {
153           tree vdecl = SSA_NAME_VAR (expr);
154           if ((!vdecl || DECL_ARTIFICIAL (vdecl))
155               && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
156             {
157               gimple def_stmt = SSA_NAME_DEF_STMT (expr);
158               if (gimple_assign_single_p (def_stmt))
159                 vdecl = gimple_assign_rhs1 (def_stmt);
160             }
161           return get_non_ssa_expr (vdecl);
162         }
163       case INTEGER_CST:
164         return expr;
165       default:
166         /* Return NULL_TREE for any other kind of tree nodes.  */
167         return NULL_TREE;
168     }
169 }
170 
171 /* Given the LHS and (real) RHS of a gimple assign statement, STMT, check if
172    they are the same. If so, print a warning message about self-assignment.  */
173 
174 static void
compare_and_warn(gimple stmt,tree lhs,tree rhs)175 compare_and_warn (gimple stmt, tree lhs, tree rhs)
176 {
177   if (operand_equal_p (lhs, rhs, OEP_PURE_SAME))
178     {
179       location_t location;
180       location = (gimple_has_location (stmt)
181                   ? gimple_location (stmt)
182                   : (DECL_P (lhs)
183                      ? DECL_SOURCE_LOCATION (lhs)
184                      : input_location));
185       /* If LHS contains any tree node not currently supported by
186          get_non_ssa_expr, simply emit a generic warning without
187          specifying LHS in the message.  */
188       lhs = get_non_ssa_expr (lhs);
189       if (lhs)
190         warning_at (location, 0, G_("%qE is assigned to itself"), lhs);
191       else
192         warning_at (location, 0, G_("self-assignment detected"));
193     }
194 }
195 
196 /* Check and warn if STMT is a self-assign statement.  */
197 
198 static void
warn_self_assign(gimple stmt)199 warn_self_assign (gimple stmt)
200 {
201   tree rhs, lhs;
202 
203   /* Check assigment statement.  */
204   if (gimple_assign_single_p (stmt))
205     {
206       rhs = get_real_ref_rhs (gimple_assign_rhs1 (stmt));
207       if (!rhs)
208         return;
209 
210       lhs = gimple_assign_lhs (stmt);
211       if (TREE_CODE (lhs) == SSA_NAME)
212         {
213           lhs = SSA_NAME_VAR (lhs);
214           if (!lhs || DECL_ARTIFICIAL (lhs))
215             return;
216         }
217 
218       compare_and_warn (stmt, lhs, rhs);
219     }
220   /* Check overloaded operator '=' (if enabled).  */
221   else if (check_operator_eq && is_gimple_call (stmt))
222     {
223       tree fdecl = gimple_call_fndecl (stmt);
224       if (fdecl && (DECL_NAME (fdecl) == maybe_get_identifier ("operator=")))
225         {
226           /* If 'operator=' takes reference operands, the arguments will be
227              ADDR_EXPR trees. In this case, just remove the address-taken
228              operator before we compare the lhs and rhs.  */
229           lhs = gimple_call_arg (stmt, 0);
230           if (TREE_CODE (lhs) == ADDR_EXPR)
231             lhs = TREE_OPERAND (lhs, 0);
232           rhs = gimple_call_arg (stmt, 1);
233           if (TREE_CODE (rhs) == ADDR_EXPR)
234             rhs = TREE_OPERAND (rhs, 0);
235 
236           compare_and_warn (stmt, lhs, rhs);
237         }
238     }
239 }
240 
241 /* Entry point for the self-assignment detection pass.  */
242 
243 static unsigned int
execute_warn_self_assign(void)244 execute_warn_self_assign (void)
245 {
246   gimple_stmt_iterator gsi;
247   basic_block bb;
248 
249   FOR_EACH_BB (bb)
250     {
251       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
252         warn_self_assign (gsi_stmt (gsi));
253     }
254 
255   return 0;
256 }
257 
258 /* Pass gate function. Currently always returns true.  */
259 
260 static bool
gate_warn_self_assign(void)261 gate_warn_self_assign (void)
262 {
263   return true;
264 }
265 
266 static struct gimple_opt_pass pass_warn_self_assign =
267 {
268   {
269     GIMPLE_PASS,
270     "warn_self_assign",                   /* name */
271     OPTGROUP_NONE,                        /* optinfo_flags */
272     gate_warn_self_assign,                /* gate */
273     execute_warn_self_assign,             /* execute */
274     NULL,                                 /* sub */
275     NULL,                                 /* next */
276     0,                                    /* static_pass_number */
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 */
283   }
284 };
285 
286 /* The initialization routine exposed to and called by GCC. The spec of this
287    function is defined in gcc/gcc-plugin.h.
288 
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
292 
293    Returns 0 if initialization finishes successfully.
294 
295    Note that this function needs to be named exactly "plugin_init".  */
296 
297 int
plugin_init(struct plugin_name_args * plugin_info,struct plugin_gcc_version * version)298 plugin_init (struct plugin_name_args *plugin_info,
299              struct plugin_gcc_version *version)
300 {
301   struct register_pass_info 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;
305   bool enabled = true;
306   int i;
307 
308   if (!plugin_default_version_check (version, &gcc_version))
309     return 1;
310 
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;
316 
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)
321     {
322       if (!strcmp (argv[i].key, "check-operator-eq"))
323         {
324           if (argv[i].value)
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);
328           else
329             check_operator_eq = true;
330         }
331       else if (!strcmp (argv[i].key, "no-check-operator-eq"))
332         {
333           if (argv[i].value)
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);
337           else
338             check_operator_eq = false;
339         }
340       else if (!strcmp (argv[i].key, "enable"))
341         {
342           if (argv[i].value)
343             warning (0, G_("option '-fplugin-arg-%s-enable=%s' ignored"
344                            " (superfluous '=%s')"),
345                      plugin_name, argv[i].value, argv[i].value);
346           else
347             enabled = true;
348         }
349       else if (!strcmp (argv[i].key, "disable"))
350         {
351           if (argv[i].value)
352             warning (0, G_("option '-fplugin-arg-%s-disable=%s' ignored"
353                            " (superfluous '=%s')"),
354                      plugin_name, argv[i].value, argv[i].value);
355           else
356             enabled = false;
357         }
358       else
359         warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
360                  plugin_name, argv[i].key);
361     }
362 
363   /* Register this new pass with GCC if the analysis is enabled.  */
364   if (enabled)
365     register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
366                        &pass_info);
367 
368   return 0;
369 }
370