OSDN Git Service

2009-05-06 Le-Chun Wu <lcwu@google.com>
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.dg / plugin / selfassign.c
1 /* This plugin contains an analysis pass that detects and warns about
2    self-assignment statements.  */
3 /* { dg-options "-O" } */
4
5 #include "config.h"
6 #include "system.h"
7 #include "coretypes.h"
8 #include "tm.h"
9 #include "toplev.h"
10 #include "basic-block.h"
11 #include "gimple.h"
12 #include "tree.h"
13 #include "tree-pass.h"
14 #include "intl.h"
15 #include "gcc-plugin.h"
16
17
18 /* Indicate whether to check overloaded operator '=', which is performed by
19    default. To disable it, use -fplugin-arg-NAME-no-check-operator-eq.  */
20 bool check_operator_eq = true;
21
22 /* Given a rhs EXPR of a gimple assign statement, if it is
23    - SSA_NAME : returns its var decl, or, if it is a temp variable,
24                 returns the rhs of its SSA def statement.
25    - VAR_DECL, PARM_DECL, FIELD_DECL, or a reference expression :
26                 returns EXPR itself.
27    - any other expression : returns NULL_TREE.  */
28
29 static tree
30 get_real_ref_rhs (tree expr)
31 {
32   switch (TREE_CODE (expr))
33     {
34       case SSA_NAME:
35         {
36           /* Given a self-assign statement, say foo.x = foo.x,
37              the IR (after SSA) looks like:
38
39              D.1797_14 = foo.x;
40              foo.x ={v} D.1797_14;
41
42              So if the rhs EXPR is an SSA_NAME of a temp variable,
43              e.g. D.1797_14, we need to grab the rhs of its SSA def
44              statement (i.e. foo.x).  */
45           tree vdecl = SSA_NAME_VAR (expr);
46           if (DECL_ARTIFICIAL (vdecl)
47               && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
48             {
49               gimple def_stmt = SSA_NAME_DEF_STMT (expr);
50               /* We are only interested in an assignment with a single
51                  rhs operand because if it is not, the original assignment
52                  will not possibly be a self-assignment.  */
53               if (is_gimple_assign (def_stmt)
54                   && (get_gimple_rhs_class (gimple_assign_rhs_code (def_stmt))
55                       == GIMPLE_SINGLE_RHS))
56                 return get_real_ref_rhs (gimple_assign_rhs1 (def_stmt));
57               else
58                 return NULL_TREE;
59             }
60           else
61             return vdecl;
62         }
63       case VAR_DECL:
64       case PARM_DECL:
65       case FIELD_DECL:
66       case COMPONENT_REF:
67       case INDIRECT_REF:
68       case ARRAY_REF:
69         return expr;
70       default:
71         return NULL_TREE;
72     }
73 }
74
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.
79
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.  */
84
85 static tree
86 get_non_ssa_expr (tree expr)
87 {
88   switch (TREE_CODE (expr))
89     {
90       case VAR_DECL:
91       case PARM_DECL:
92       case FIELD_DECL:
93         {
94           if (DECL_NAME (expr))
95             return expr;
96           else
97             return NULL_TREE;
98         }
99       case COMPONENT_REF:
100         {
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;
116         }
117       case INDIRECT_REF:
118         {
119           tree orig_base = TREE_OPERAND (expr, 0);
120           tree base = get_non_ssa_expr (orig_base);
121           if (!base)
122             return NULL_TREE;
123           /* If BASE is converted, build a new indirect reference tree.  */
124           if (base != orig_base)
125             return build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (base)), base);
126           else
127             return expr;
128         }
129       case ARRAY_REF:
130         {
131           tree array, orig_array = TREE_OPERAND (expr, 0);
132           tree index, orig_index = TREE_OPERAND (expr, 1);
133           array = get_non_ssa_expr (orig_array);
134           if (!array)
135             return NULL_TREE;
136           index = get_non_ssa_expr (orig_index);
137           if (!index)
138             return NULL_TREE;
139           /* If either ARRAY or INDEX is converted, build a new array
140              reference tree.  */
141           if (array != orig_array || index != orig_index)
142             return build4 (ARRAY_REF, TREE_TYPE (expr), array, index,
143                            TREE_OPERAND (expr, 2), TREE_OPERAND (expr, 3));
144           else
145             return expr;
146         }
147       case SSA_NAME:
148         {
149           tree vdecl = SSA_NAME_VAR (expr);
150           if (DECL_ARTIFICIAL (vdecl)
151               && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
152             {
153               gimple def_stmt = SSA_NAME_DEF_STMT (expr);
154               if (is_gimple_assign (def_stmt)
155                   && (get_gimple_rhs_class (gimple_assign_rhs_code (def_stmt))
156                       == GIMPLE_SINGLE_RHS))
157                 vdecl = gimple_assign_rhs1 (def_stmt);
158             }
159           return get_non_ssa_expr (vdecl);
160         }
161       case INTEGER_CST:
162         return expr;
163       default:
164         /* Return NULL_TREE for any other kind of tree nodes.  */
165         return NULL_TREE;
166     }
167 }
168
169 /* Given the LHS and (real) RHS of a gimple assign statement, STMT, check if
170    they are the same. If so, print a warning message about self-assignment.  */
171
172 static void
173 compare_and_warn (gimple stmt, tree lhs, tree rhs)
174 {
175   if (operand_equal_p (lhs, rhs, OEP_PURE_SAME))
176     {
177       location_t location;
178       location = (gimple_has_location (stmt)
179                   ? gimple_location (stmt)
180                   : (DECL_P (lhs)
181                      ? DECL_SOURCE_LOCATION (lhs)
182                      : input_location));
183       /* If LHS contains any tree node not currently supported by
184          get_non_ssa_expr, simply emit a generic warning without
185          specifying LHS in the message.  */
186       lhs = get_non_ssa_expr (lhs);
187       if (lhs)
188         warning (0, G_("%H%qE is assigned to itself"), &location, lhs);
189       else
190         warning (0, G_("%Hself-assignment detected"), &location);
191     }
192 }
193
194 /* Check and warn if STMT is a self-assign statement.  */
195
196 static void
197 warn_self_assign (gimple stmt)
198 {
199   tree rhs, lhs;
200
201   /* Check assigment statement.  */
202   if (is_gimple_assign (stmt)
203       && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
204           == GIMPLE_SINGLE_RHS))
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 (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
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
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     gate_warn_self_assign,                /* gate */
272     execute_warn_self_assign,             /* execute */
273     NULL,                                 /* sub */
274     NULL,                                 /* next */
275     0,                                    /* static_pass_number */
276     0,                                    /* tv_id */
277     PROP_ssa,                             /* properties_required */
278     0,                                    /* properties_provided */
279     0,                                    /* properties_destroyed */
280     0,                                    /* todo_flags_start */
281     TODO_dump_func                        /* todo_flags_finish */
282   }
283 };
284
285 /* The initialization routine exposed to and called by GCC. The spec of this
286    function is defined in gcc/gcc-plugin.h.
287
288    PLUGIN_NAME - name of the plugin (useful for error reporting)
289    ARGC        - the size of the ARGV array
290    ARGV        - an array of key-value argument pair
291
292    Returns 0 if initialization finishes successfully.
293
294    Note that this function needs to be named exactly "plugin_init".  */
295
296 int
297 plugin_init (const char *plugin_name, struct plugin_gcc_version *version,
298              int argc, struct plugin_argument *argv)
299 {
300   struct plugin_pass pass_info;
301   bool enabled = true;
302   int i;
303
304   if (!plugin_default_version_check (version, version))
305     return 1;
306
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;
312
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)
317     {
318       if (!strcmp (argv[i].key, "check-operator-eq"))
319         {
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;
326         }
327       else if (!strcmp (argv[i].key, "no-check-operator-eq"))
328         {
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;
335         }
336       else if (!strcmp (argv[i].key, "enable"))
337         {
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;
344         }
345       else if (!strcmp (argv[i].key, "disable"))
346         {
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;
353         }
354       else
355         warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
356                  plugin_name, argv[i].key);
357     }
358
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);
363
364   return 0;
365 }