OSDN Git Service

* gcc.dg/simulate-thread/simulate-thread.gdb: Call
[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 "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
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 (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
87 get_non_ssa_expr (tree expr)
88 {
89   switch (TREE_CODE (expr))
90     {
91       case VAR_DECL:
92       case PARM_DECL:
93       case FIELD_DECL:
94         {
95           if (DECL_NAME (expr))
96             return expr;
97           else
98             return NULL_TREE;
99         }
100       case COMPONENT_REF:
101         {
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);
105           if (!base)
106             return NULL_TREE;
107           component = get_non_ssa_expr (orig_component);
108           if (!component)
109             return NULL_TREE;
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);
115           else
116             return expr;
117         }
118       case MEM_REF:
119         {
120           tree orig_base = TREE_OPERAND (expr, 0);
121           if (TREE_CODE (orig_base) == SSA_NAME)
122             {
123               tree base = get_non_ssa_expr (orig_base);
124               if (!base)
125                 return NULL_TREE;
126               return fold_build2 (MEM_REF, TREE_TYPE (expr),
127                                   base, TREE_OPERAND (expr, 1));
128             }
129           return expr;
130         }
131       case ARRAY_REF:
132         {
133           tree array, orig_array = TREE_OPERAND (expr, 0);
134           tree index, orig_index = TREE_OPERAND (expr, 1);
135           array = get_non_ssa_expr (orig_array);
136           if (!array)
137             return NULL_TREE;
138           index = get_non_ssa_expr (orig_index);
139           if (!index)
140             return NULL_TREE;
141           /* If either ARRAY or INDEX is converted, build a new array
142              reference tree.  */
143           if (array != orig_array || index != orig_index)
144             return build4 (ARRAY_REF, TREE_TYPE (expr), array, index,
145                            TREE_OPERAND (expr, 2), TREE_OPERAND (expr, 3));
146           else
147             return expr;
148         }
149       case SSA_NAME:
150         {
151           tree vdecl = SSA_NAME_VAR (expr);
152           if (DECL_ARTIFICIAL (vdecl)
153               && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
154             {
155               gimple def_stmt = SSA_NAME_DEF_STMT (expr);
156               if (gimple_assign_single_p (def_stmt))
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_at (location, 0, G_("%qE is assigned to itself"), lhs);
189       else
190         warning_at (location, 0, G_("self-assignment detected"));
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 (gimple_assign_single_p (stmt))
203     {
204       rhs = get_real_ref_rhs (gimple_assign_rhs1 (stmt));
205       if (!rhs)
206         return;
207
208       lhs = gimple_assign_lhs (stmt);
209       if (TREE_CODE (lhs) == SSA_NAME)
210         {
211           lhs = SSA_NAME_VAR (lhs);
212           if (DECL_ARTIFICIAL (lhs))
213             return;
214         }
215
216       compare_and_warn (stmt, lhs, rhs);
217     }
218   /* Check overloaded operator '=' (if enabled).  */
219   else if (check_operator_eq && is_gimple_call (stmt))
220     {
221       tree fdecl = gimple_call_fndecl (stmt);
222       if (fdecl && (DECL_NAME (fdecl) == maybe_get_identifier ("operator=")))
223         {
224           /* If 'operator=' takes reference operands, the arguments will be 
225              ADDR_EXPR trees. In this case, just remove the address-taken
226              operator before we compare the lhs and rhs.  */
227           lhs = gimple_call_arg (stmt, 0);
228           if (TREE_CODE (lhs) == ADDR_EXPR)
229             lhs = TREE_OPERAND (lhs, 0);
230           rhs = gimple_call_arg (stmt, 1);
231           if (TREE_CODE (rhs) == ADDR_EXPR)
232             rhs = TREE_OPERAND (rhs, 0);
233
234           compare_and_warn (stmt, lhs, rhs);
235         }
236     }
237 }
238
239 /* Entry point for the self-assignment detection pass.  */
240
241 static unsigned int
242 execute_warn_self_assign (void)
243 {
244   gimple_stmt_iterator gsi;
245   basic_block bb;
246
247   FOR_EACH_BB (bb)
248     {
249       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
250         warn_self_assign (gsi_stmt (gsi));
251     }
252
253   return 0;
254 }
255
256 /* Pass gate function. Currently always returns true.  */
257
258 static bool
259 gate_warn_self_assign (void)
260 {
261   return true;
262 }
263
264 static struct gimple_opt_pass pass_warn_self_assign =
265 {
266   {
267     GIMPLE_PASS,
268     "warn_self_assign",                   /* name */
269     gate_warn_self_assign,                /* gate */
270     execute_warn_self_assign,             /* execute */
271     NULL,                                 /* sub */
272     NULL,                                 /* next */
273     0,                                    /* static_pass_number */
274     TV_NONE,                              /* tv_id */
275     PROP_ssa,                             /* properties_required */
276     0,                                    /* properties_provided */
277     0,                                    /* properties_destroyed */
278     0,                                    /* todo_flags_start */
279     TODO_dump_func                        /* todo_flags_finish */
280   }
281 };
282
283 /* The initialization routine exposed to and called by GCC. The spec of this
284    function is defined in gcc/gcc-plugin.h.
285
286    PLUGIN_NAME - name of the plugin (useful for error reporting)
287    ARGC        - the size of the ARGV array
288    ARGV        - an array of key-value argument pair
289
290    Returns 0 if initialization finishes successfully.
291
292    Note that this function needs to be named exactly "plugin_init".  */
293
294 int
295 plugin_init (struct plugin_name_args *plugin_info,
296              struct plugin_gcc_version *version)
297 {
298   struct register_pass_info pass_info;
299   const char *plugin_name = plugin_info->base_name;
300   int argc = plugin_info->argc;
301   struct plugin_argument *argv = plugin_info->argv;
302   bool enabled = true;
303   int i;
304
305   if (!plugin_default_version_check (version, &gcc_version))
306     return 1;
307
308   /* Self-assign detection should happen after SSA is constructed.  */
309   pass_info.pass = &pass_warn_self_assign.pass;
310   pass_info.reference_pass_name = "ssa";
311   pass_info.ref_pass_instance_number = 1;
312   pass_info.pos_op = PASS_POS_INSERT_AFTER;
313
314   /* Process the plugin arguments. This plugin takes the following arguments:
315      check-operator-eq, no-check-operator-eq, enable, and disable.
316      By default, the analysis is enabled with 'operator=' checked.  */
317   for (i = 0; i < argc; ++i)
318     {
319       if (!strcmp (argv[i].key, "check-operator-eq"))
320         {
321           if (argv[i].value)
322             warning (0, G_("option '-fplugin-arg-%s-check-operator-eq=%s'"
323                            " ignored (superfluous '=%s')"),
324                      plugin_name, argv[i].value, argv[i].value);
325           else
326             check_operator_eq = true;
327         }
328       else if (!strcmp (argv[i].key, "no-check-operator-eq"))
329         {
330           if (argv[i].value)
331             warning (0, G_("option '-fplugin-arg-%s-no-check-operator-eq=%s'"
332                            " ignored (superfluous '=%s')"),
333                      plugin_name, argv[i].value, argv[i].value);
334           else
335             check_operator_eq = false;
336         }
337       else if (!strcmp (argv[i].key, "enable"))
338         {
339           if (argv[i].value)
340             warning (0, G_("option '-fplugin-arg-%s-enable=%s' ignored"
341                            " (superfluous '=%s')"),
342                      plugin_name, argv[i].value, argv[i].value);
343           else
344             enabled = true;
345         }
346       else if (!strcmp (argv[i].key, "disable"))
347         {
348           if (argv[i].value)
349             warning (0, G_("option '-fplugin-arg-%s-disable=%s' ignored"
350                            " (superfluous '=%s')"),
351                      plugin_name, argv[i].value, argv[i].value);
352           else
353             enabled = false;
354         }
355       else
356         warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
357                  plugin_name, argv[i].key);
358     }
359
360   /* Register this new pass with GCC if the analysis is enabled.  */
361   if (enabled)
362     register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
363                        &pass_info);
364
365   return 0;
366 }