OSDN Git Service

Changlog libcpp
[pf3gnuchains/gcc-fork.git] / gcc / plugin.c
1 /* Support for GCC plugin mechanism.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 /* This file contains the support for GCC plugin mechanism based on the
21    APIs described in doc/plugin.texi.  */
22
23 #include "config.h"
24 #include "system.h"
25
26 /* If plugin support is not enabled, do not try to execute any code
27    that may reference libdl.  The generic code is still compiled in to
28    avoid including too many conditional compilation paths in the rest
29    of the compiler.  */
30 #ifdef ENABLE_PLUGIN
31 #include <dlfcn.h>
32 #endif
33
34 #include "coretypes.h"
35 #include "toplev.h"
36 #include "tree.h"
37 #include "tree-pass.h"
38 #include "intl.h"
39 #include "plugin.h"
40 #include "timevar.h"
41 #include "ggc.h"
42
43 #ifdef ENABLE_PLUGIN
44 #include "plugin-version.h"
45 #endif
46
47 /* Event names as strings.  Keep in sync with enum plugin_event.  */
48 const char *plugin_event_name[] =
49 {
50   "PLUGIN_PASS_MANAGER_SETUP",
51   "PLUGIN_FINISH_TYPE",
52   "PLUGIN_FINISH_UNIT",
53   "PLUGIN_CXX_CP_PRE_GENERICIZE",
54   "PLUGIN_FINISH",
55   "PLUGIN_INFO",
56   "PLUGIN_GGC_START",
57   "PLUGIN_GGC_MARKING",
58   "PLUGIN_GGC_END",
59   "PLUGIN_REGISTER_GGC_ROOTS",
60   "PLUGIN_START_UNIT", 
61   "PLUGIN_EVENT_LAST"
62 };
63
64 /* Hash table for the plugin_name_args objects created during command-line
65    parsing.  */
66 static htab_t plugin_name_args_tab = NULL;
67
68 /* List node for keeping track of plugin-registered callback.  */
69 struct callback_info
70 {
71   const char *plugin_name;   /* Name of plugin that registers the callback.  */
72   plugin_callback_func func; /* Callback to be called.  */
73   void *user_data;           /* plugin-specified data.  */
74   struct callback_info *next;
75 };
76
77 /* An array of lists of 'callback_info' objects indexed by the event id.  */
78 static struct callback_info *plugin_callbacks[PLUGIN_EVENT_LAST] = { NULL };
79
80 /* List node for an inserted pass instance. We need to keep track of all
81    the newly-added pass instances (with 'added_pass_nodes' defined below)
82    so that we can register their dump files after pass-positioning is finished.
83    Registering dumping files needs to be post-processed or the
84    static_pass_number of the opt_pass object would be modified and mess up
85    the dump file names of future pass instances to be added.  */
86 struct pass_list_node
87 {
88   struct opt_pass *pass;
89   struct pass_list_node *next;
90 };
91
92 static struct pass_list_node *added_pass_nodes = NULL;
93 static struct pass_list_node *prev_added_pass_node;
94
95 #ifdef ENABLE_PLUGIN
96 /* Each plugin should define an initialization function with exactly
97    this name.  */
98 static const char *str_plugin_init_func_name = "plugin_init";
99
100 /* Each plugin should define this symbol to assert that it is
101    distributed under a GPL-compatible license.  */
102 static const char *str_license = "plugin_is_GPL_compatible";
103 #endif
104
105 /* Helper function for the hash table that compares the base_name of the
106    existing entry (S1) with the given string (S2).  */
107
108 static int
109 htab_str_eq (const void *s1, const void *s2)
110 {
111   const struct plugin_name_args *plugin = (const struct plugin_name_args *) s1;
112   return !strcmp (plugin->base_name, (const char *) s2);
113 }
114
115
116 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
117    return NAME.  */
118
119 static char *
120 get_plugin_base_name (const char *full_name)
121 {
122   /* First get the base name part of the full-path name, i.e. NAME.so.  */
123   char *base_name = xstrdup (lbasename (full_name));
124
125   /* Then get rid of '.so' part of the name.  */
126   strip_off_ending (base_name, strlen (base_name));
127
128   return base_name;
129 }
130
131
132 /* Create a plugin_name_args object for the give plugin and insert it to
133    the hash table. This function is called when -fplugin=/path/to/NAME.so
134    option is processed.  */
135
136 void
137 add_new_plugin (const char* plugin_name)
138 {
139   struct plugin_name_args *plugin;
140   void **slot;
141   char *base_name = get_plugin_base_name (plugin_name);
142
143   /* If this is the first -fplugin= option we encounter, create 
144      'plugin_name_args_tab' hash table.  */
145   if (!plugin_name_args_tab)
146     plugin_name_args_tab = htab_create (10, htab_hash_string, htab_str_eq,
147                                         NULL);
148
149   slot = htab_find_slot (plugin_name_args_tab, base_name, INSERT);
150
151   /* If the same plugin (name) has been specified earlier, either emit an
152      error or a warning message depending on if they have identical full
153      (path) names.  */
154   if (*slot)
155     {
156       plugin = (struct plugin_name_args *) *slot;
157       if (strcmp (plugin->full_name, plugin_name))
158         error ("Plugin %s was specified with different paths:\n%s\n%s",
159                plugin->base_name, plugin->full_name, plugin_name);
160       return;
161     }
162
163   plugin = XCNEW (struct plugin_name_args);
164   plugin->base_name = base_name;
165   plugin->full_name = plugin_name;
166
167   *slot = plugin;
168 }
169
170
171 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
172    'plugin_argument' object for the parsed key-value pair. ARG is
173    the <name>-<key>[=<value>] part of the option.  */
174
175 void
176 parse_plugin_arg_opt (const char *arg)
177 {
178   size_t len = 0, name_len = 0, key_len = 0, value_len = 0;
179   const char *ptr, *name_start = arg, *key_start = NULL, *value_start = NULL;
180   char *name, *key, *value;
181   void **slot;
182   bool name_parsed = false, key_parsed = false;
183
184   /* Iterate over the ARG string and identify the starting character position
185      of 'name', 'key', and 'value' and their lengths.  */
186   for (ptr = arg; *ptr; ++ptr)
187     {
188       /* Only the first '-' encountered is considered a separator between
189          'name' and 'key'. All the subsequent '-'s are considered part of
190          'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
191          the plugin name is 'foo' and the key is 'bar-primary-key'.  */
192       if (*ptr == '-' && !name_parsed)
193         {
194           name_len = len;
195           len = 0;
196           key_start = ptr + 1;
197           name_parsed = true;
198           continue;
199         }
200       else if (*ptr == '=')
201         {
202           if (key_parsed)
203             {
204               error ("Malformed option -fplugin-arg-%s (multiple '=' signs)",
205                      arg);
206               return;
207             }
208           key_len = len;
209           len = 0;
210           value_start = ptr + 1;
211           key_parsed = true;
212           continue;
213         }
214       else
215         ++len;
216     }
217
218   if (!key_start)
219     {
220       error ("Malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
221              arg);
222       return;
223     }
224
225   /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
226      Otherwise, it is the VALUE_LEN.  */
227   if (!value_start)
228     key_len = len;
229   else
230     value_len = len;
231
232   name = XNEWVEC (char, name_len + 1);
233   strncpy (name, name_start, name_len);
234   name[name_len] = '\0';
235
236   /* Check if the named plugin has already been specified earlier in the
237      command-line.  */
238   if (plugin_name_args_tab
239       && ((slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT))
240           != NULL))
241     {
242       struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
243
244       key = XNEWVEC (char, key_len + 1);
245       strncpy (key, key_start, key_len);
246       key[key_len] = '\0';
247       if (value_start)
248         {
249           value = XNEWVEC (char, value_len + 1);
250           strncpy (value, value_start, value_len);
251           value[value_len] = '\0';
252         }
253       else
254         value = NULL;
255
256       /* Create a plugin_argument object for the parsed key-value pair.
257          If there are already arguments for this plugin, we will need to
258          adjust the argument array size by creating a new array and deleting
259          the old one. If the performance ever becomes an issue, we can
260          change the code by pre-allocating a larger array first.  */
261       if (plugin->argc > 0)
262         {
263           struct plugin_argument *args = XNEWVEC (struct plugin_argument,
264                                                   plugin->argc + 1);
265           memcpy (args, plugin->argv,
266                   sizeof (struct plugin_argument) * plugin->argc);
267           XDELETEVEC (plugin->argv);
268           plugin->argv = args;
269           ++plugin->argc;
270         }
271       else
272         {
273           gcc_assert (plugin->argv == NULL);
274           plugin->argv = XNEWVEC (struct plugin_argument, 1);
275           plugin->argc = 1;
276         }
277
278       plugin->argv[plugin->argc - 1].key = key;
279       plugin->argv[plugin->argc - 1].value = value;
280     }
281   else
282     error ("Plugin %s should be specified before -fplugin-arg-%s "
283            "in the command line", name, arg);
284
285   /* We don't need the plugin's name anymore. Just release it.  */
286   XDELETEVEC (name);
287 }
288
289
290 /* Insert the plugin pass at the proper position. Return true if the pass 
291    is successfully added.
292
293    PLUGIN_PASS_INFO - new pass to be inserted
294    PASS_LIST        - root of the pass list to insert the new pass to  */
295
296 static bool
297 position_pass (struct plugin_pass *plugin_pass_info,
298                struct opt_pass **pass_list)
299 {
300   struct opt_pass *pass = *pass_list, *prev_pass = NULL;
301   bool success = false;
302
303   for ( ; pass; prev_pass = pass, pass = pass->next)
304     {
305       /* Check if the current pass is of the same type as the new pass and
306          matches the name and the instance number of the reference pass.  */
307       if (pass->type == plugin_pass_info->pass->type
308           && pass->name
309           && !strcmp (pass->name, plugin_pass_info->reference_pass_name)
310           && ((plugin_pass_info->ref_pass_instance_number == 0)
311               || (plugin_pass_info->ref_pass_instance_number ==
312                   pass->static_pass_number)
313               || (plugin_pass_info->ref_pass_instance_number == 1
314                   && pass->todo_flags_start & TODO_mark_first_instance)))
315         {
316           struct opt_pass *new_pass = plugin_pass_info->pass;
317           struct pass_list_node *new_pass_node;
318
319           /* The following code (if-statement) is adopted from next_pass_1.  */
320           if (new_pass->static_pass_number)
321             {
322               new_pass = XNEW (struct opt_pass);
323               memcpy (new_pass, plugin_pass_info->pass, sizeof (*new_pass));
324               new_pass->next = NULL;
325
326               new_pass->todo_flags_start &= ~TODO_mark_first_instance;
327
328               plugin_pass_info->pass->static_pass_number -= 1;
329               new_pass->static_pass_number =
330                   -plugin_pass_info->pass->static_pass_number;
331             }
332           else
333             {
334               new_pass->todo_flags_start |= TODO_mark_first_instance;
335               new_pass->static_pass_number = -1;
336             }
337
338           /* Insert the new pass instance based on the positioning op.  */
339           switch (plugin_pass_info->pos_op)
340             {
341               case PASS_POS_INSERT_AFTER:
342                 new_pass->next = pass->next;
343                 pass->next = new_pass;
344
345                 /* Skip newly inserted pass to avoid repeated
346                    insertions in the case where the new pass and the
347                    existing one have the same name.  */
348                 pass = new_pass; 
349                 break;
350               case PASS_POS_INSERT_BEFORE:
351                 new_pass->next = pass;
352                 if (prev_pass)
353                   prev_pass->next = new_pass;
354                 else
355                   *pass_list = new_pass;
356                 break;
357               case PASS_POS_REPLACE:
358                 new_pass->next = pass->next;
359                 if (prev_pass)
360                   prev_pass->next = new_pass;
361                 else
362                   *pass_list = new_pass;
363                 new_pass->sub = pass->sub;
364                 new_pass->tv_id = pass->tv_id;
365                 pass = new_pass;
366                 break;
367               default:
368                 error ("Invalid pass positioning operation");
369                 return false;
370             }
371
372           /* Save the newly added pass (instance) in the added_pass_nodes
373              list so that we can register its dump file later. Note that
374              we cannot register the dump file now because doing so will modify
375              the static_pass_number of the opt_pass object and therefore
376              mess up the dump file name of future instances.  */
377           new_pass_node = XCNEW (struct pass_list_node);
378           new_pass_node->pass = new_pass;
379           if (!added_pass_nodes)
380             added_pass_nodes = new_pass_node;
381           else
382             prev_added_pass_node->next = new_pass_node;
383           prev_added_pass_node = new_pass_node;
384
385           success = true;
386         }
387
388       if (pass->sub && position_pass (plugin_pass_info, &pass->sub))
389         success = true;
390     }
391
392   return success;
393 }
394
395
396 /* Hook into the pass lists (trees) a new pass registered by a plugin.
397
398    PLUGIN_NAME - display name for the plugin
399    PASS_INFO   - plugin pass information that specifies the opt_pass object,
400                  reference pass, instance number, and how to position
401                  the pass  */
402
403 static void
404 register_pass (const char *plugin_name, struct plugin_pass *pass_info)
405 {
406   if (!pass_info->pass)
407     {
408       error ("No pass specified when registering a new pass in plugin %s",
409              plugin_name);
410       return;
411     }
412
413   if (!pass_info->reference_pass_name)
414     {
415       error ("No reference pass specified for positioning the pass "
416              " from plugin %s", plugin_name);
417       return;
418     }
419
420   /* Try to insert the new pass to the pass lists. We need to check all
421      three lists as the reference pass could be in one (or all) of them.  */
422   if (!position_pass (pass_info, &all_lowering_passes)
423       && !position_pass (pass_info, &all_ipa_passes)
424       && !position_pass (pass_info, &all_passes))
425     error ("Failed to position pass %s registered by plugin %s. "
426            "Cannot find the (specified instance of) reference pass %s",
427            pass_info->pass->name, plugin_name, pass_info->reference_pass_name);
428   else
429     {
430       /* OK, we have successfully inserted the new pass. We need to register
431          the dump files for the newly added pass and its duplicates (if any).
432          Because the registration of plugin passes happens after the
433          command-line options are parsed, the options that specify single
434          pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
435          plugin passes. Therefore we currently can only enable dumping of
436          new plugin passes when the 'dump-all' flags (e.g. -fdump-tree-all)
437          are specified. While doing so, we also delete the pass_list_node
438          objects created during pass positioning.  */
439       while (added_pass_nodes)
440         {
441           struct pass_list_node *next_node = added_pass_nodes->next;
442           enum tree_dump_index tdi;
443           register_one_dump_file (added_pass_nodes->pass);
444           if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
445               || added_pass_nodes->pass->type == IPA_PASS)
446             tdi = TDI_ipa_all;
447           else if (added_pass_nodes->pass->type == GIMPLE_PASS)
448             tdi = TDI_tree_all;
449           else
450             tdi = TDI_rtl_all;
451           /* Check if dump-all flag is specified.  */
452           if (get_dump_file_info (tdi)->state)
453             get_dump_file_info (added_pass_nodes->pass->static_pass_number)
454                 ->state = get_dump_file_info (tdi)->state;
455           XDELETE (added_pass_nodes);
456           added_pass_nodes = next_node;
457         }
458     }
459 }
460
461
462 /* Register additional plugin information. NAME is the name passed to
463    plugin_init. INFO is the information that should be registered. */
464
465 static void
466 register_plugin_info (const char* name, struct plugin_info *info)
467 {
468   void **slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT);
469   struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
470   plugin->version = info->version;
471   plugin->help = info->help;
472 }
473
474 /* Called from the plugin's initialization code. Register a single callback.
475    This function can be called multiple times.
476
477    PLUGIN_NAME - display name for this plugin
478    EVENT       - which event the callback is for
479    CALLBACK    - the callback to be called at the event
480    USER_DATA   - plugin-provided data   */
481
482 void
483 register_callback (const char *plugin_name,
484                    enum plugin_event event,
485                    plugin_callback_func callback,
486                    void *user_data)
487 {
488   switch (event)
489     {
490       case PLUGIN_PASS_MANAGER_SETUP:
491         gcc_assert (!callback);
492         register_pass (plugin_name, (struct plugin_pass *) user_data);
493         break;
494       case PLUGIN_INFO:
495         gcc_assert (!callback);
496         register_plugin_info (plugin_name, (struct plugin_info *) user_data);
497         break;
498       case PLUGIN_REGISTER_GGC_ROOTS:
499         gcc_assert (!callback);
500         ggc_register_root_tab ((const struct ggc_root_tab*) user_data);
501         break;
502       case PLUGIN_FINISH_TYPE:
503       case PLUGIN_START_UNIT:
504       case PLUGIN_FINISH_UNIT:
505       case PLUGIN_CXX_CP_PRE_GENERICIZE:
506       case PLUGIN_GGC_START:
507       case PLUGIN_GGC_MARKING:
508       case PLUGIN_GGC_END:
509       case PLUGIN_ATTRIBUTES:
510       case PLUGIN_FINISH:
511         {
512           struct callback_info *new_callback;
513           if (!callback)
514             {
515               error ("Plugin %s registered a null callback function "
516                      "for event %s", plugin_name, plugin_event_name[event]);
517               return;
518             }
519           new_callback = XNEW (struct callback_info);
520           new_callback->plugin_name = plugin_name;
521           new_callback->func = callback;
522           new_callback->user_data = user_data;
523           new_callback->next = plugin_callbacks[event];
524           plugin_callbacks[event] = new_callback;
525         }
526         break;
527       case PLUGIN_EVENT_LAST:
528       default:
529         error ("Unkown callback event registered by plugin %s",
530                plugin_name);
531     }
532 }
533
534
535 /* Called from inside GCC.  Invoke all plug-in callbacks registered with
536    the specified event.
537
538    EVENT    - the event identifier
539    GCC_DATA - event-specific data provided by the compiler  */
540
541 void
542 invoke_plugin_callbacks (enum plugin_event event, void *gcc_data)
543 {
544   timevar_push (TV_PLUGIN_RUN);
545
546   switch (event)
547     {
548       case PLUGIN_FINISH_TYPE:
549       case PLUGIN_START_UNIT:
550       case PLUGIN_FINISH_UNIT:
551       case PLUGIN_CXX_CP_PRE_GENERICIZE:
552       case PLUGIN_ATTRIBUTES:
553       case PLUGIN_FINISH:
554       case PLUGIN_GGC_START:
555       case PLUGIN_GGC_MARKING:
556       case PLUGIN_GGC_END:
557         {
558           /* Iterate over every callback registered with this event and
559              call it.  */
560           struct callback_info *callback = plugin_callbacks[event];
561           for ( ; callback; callback = callback->next)
562             (*callback->func) (gcc_data, callback->user_data);
563         }
564         break;
565
566       case PLUGIN_PASS_MANAGER_SETUP:
567       case PLUGIN_EVENT_LAST:
568       case PLUGIN_REGISTER_GGC_ROOTS:
569       default:
570         gcc_assert (false);
571     }
572
573   timevar_pop (TV_PLUGIN_RUN);
574 }
575
576 #ifdef ENABLE_PLUGIN
577 /* We need a union to cast dlsym return value to a function pointer
578    as ISO C forbids assignment between function pointer and 'void *'.
579    Use explicit union instead of __extension__(<union_cast>) for
580    portability.  */
581 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
582 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
583 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
584
585 /* Try to initialize PLUGIN. Return true if successful. */
586
587 static bool
588 try_init_one_plugin (struct plugin_name_args *plugin)
589 {
590   void *dl_handle;
591   plugin_init_func plugin_init;
592   char *err;
593   PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
594
595   /* We use RTLD_NOW to accelerate binding and detect any mismatch
596      between the API expected by the plugin and the GCC API; we use
597      RTLD_GLOBAL which is useful to plugins which themselves call
598      dlopen.  */
599   dl_handle = dlopen (plugin->full_name, RTLD_NOW | RTLD_GLOBAL);
600   if (!dl_handle)
601     {
602       error ("Cannot load plugin %s\n%s", plugin->full_name, dlerror ());
603       return false;
604     }
605
606   /* Clear any existing error.  */
607   dlerror ();
608
609   /* Check the plugin license.  */
610   if (dlsym (dl_handle, str_license) == NULL)
611     fatal_error ("plugin %s is not licensed under a GPL-compatible license\n"
612                  "%s", plugin->full_name, dlerror ());
613
614   PTR_UNION_AS_VOID_PTR (plugin_init_union) =
615       dlsym (dl_handle, str_plugin_init_func_name);
616   plugin_init = PTR_UNION_AS_CAST_PTR (plugin_init_union);
617
618   if ((err = dlerror ()) != NULL)
619     {
620       error ("Cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
621              plugin->full_name, err);
622       return false;
623     }
624
625   /* Call the plugin-provided initialization routine with the arguments.  */
626   if ((*plugin_init) (plugin, &gcc_version))
627     {
628       error ("Fail to initialize plugin %s", plugin->full_name);
629       return false;
630     }
631
632   return true;
633 }
634
635
636 /* Routine to dlopen and initialize one plugin. This function is passed to
637    (and called by) the hash table traverse routine. Return 1 for the
638    htab_traverse to continue scan, 0 to stop.
639
640    SLOT - slot of the hash table element
641    INFO - auxiliary pointer handed to hash table traverse routine
642           (unused in this function)  */
643
644 static int
645 init_one_plugin (void **slot, void * ARG_UNUSED (info))
646 {
647   struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
648   bool ok = try_init_one_plugin (plugin);
649   if (!ok)
650     {
651       htab_remove_elt (plugin_name_args_tab, plugin->base_name);
652       XDELETE (plugin);
653     }
654   return 1;
655 }
656
657 #endif  /* ENABLE_PLUGIN  */
658
659 /* Main plugin initialization function.  Called from compile_file() in
660    toplev.c.  */
661
662 void
663 initialize_plugins (void)
664 {
665   /* If no plugin was specified in the command-line, simply return.  */
666   if (!plugin_name_args_tab)
667     return;
668
669   timevar_push (TV_PLUGIN_INIT);
670  
671 #ifdef ENABLE_PLUGIN
672   /* Traverse and initialize each plugin specified in the command-line.  */
673   htab_traverse_noresize (plugin_name_args_tab, init_one_plugin, NULL);
674 #endif
675
676   timevar_pop (TV_PLUGIN_INIT);
677 }
678
679 /* Release memory used by one plugin. */
680
681 static int
682 finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
683 {
684   struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
685   XDELETE (plugin);
686   return 1;
687 }
688
689 /* Free memory allocated by the plugin system. */
690
691 void
692 finalize_plugins (void)
693 {
694   if (!plugin_name_args_tab)
695     return;
696
697   /* We can now delete the plugin_name_args object as it will no longer
698      be used. Note that base_name and argv fields (both of which were also
699      dynamically allocated) are not freed as they could still be used by
700      the plugin code.  */
701
702   htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
703
704   /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it.  */
705   htab_delete (plugin_name_args_tab);
706   plugin_name_args_tab = NULL;
707 }
708
709 /* Used to pass options to htab_traverse callbacks. */
710
711 struct print_options
712 {
713   FILE *file;
714   const char *indent;
715 };
716
717 /* Print the version of one plugin. */
718
719 static int
720 print_version_one_plugin (void **slot, void *data)
721 {
722   struct print_options *opt = (struct print_options *) data;
723   struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
724   const char *version = plugin->version ? plugin->version : "Unknown version.";
725
726   fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
727   return 1;
728 }
729
730 /* Print the version of each plugin. */
731
732 void
733 print_plugins_versions (FILE *file, const char *indent)
734 {
735   struct print_options opt;
736   opt.file = file;
737   opt.indent = indent;
738   if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
739     return;
740
741   fprintf (file, "%sVersions of loaded plugins:\n", indent);
742   htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
743 }
744
745 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
746    argument to htab_traverse_noresize. */
747
748 static int
749 print_help_one_plugin (void **slot, void *data)
750 {
751   struct print_options *opt = (struct print_options *) data;
752   struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
753   const char *help = plugin->help ? plugin->help : "No help available .";
754
755   char *dup = xstrdup (help);
756   char *p, *nl;
757   fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
758
759   for (p = nl = dup; nl; p = nl)
760     {
761       nl = strchr (nl, '\n');
762       if (nl)
763         {
764           *nl = '\0';
765           nl++;
766         }
767       fprintf (opt->file, "   %s %s\n", opt->indent, p);
768     }
769
770   free (dup);
771   return 1;
772 }
773
774 /* Print help for each plugin. The output goes to FILE and every line starts
775    with INDENT. */
776
777 void
778 print_plugins_help (FILE *file, const char *indent)
779 {
780   struct print_options opt;
781   opt.file = file;
782   opt.indent = indent;
783   if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
784     return;
785
786   fprintf (file, "%sHelp for the loaded plugins:\n", indent);
787   htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
788 }
789
790
791 /* Return true if plugins have been loaded.  */
792
793 bool
794 plugins_active_p (void)
795 {
796   int event;
797
798   for (event = PLUGIN_PASS_MANAGER_SETUP; event < PLUGIN_EVENT_LAST; event++)
799     if (plugin_callbacks[event])
800       return true;
801
802   return false;
803 }
804
805
806 /* Dump to FILE the names and associated events for all the active
807    plugins.  */
808
809 void
810 dump_active_plugins (FILE *file)
811 {
812   int event;
813
814   if (!plugins_active_p ())
815     return;
816
817   fprintf (stderr, "Event\t\t\tPlugins\n");
818   for (event = PLUGIN_PASS_MANAGER_SETUP; event < PLUGIN_EVENT_LAST; event++)
819     if (plugin_callbacks[event])
820       {
821         struct callback_info *ci;
822
823         fprintf (file, "%s\t", plugin_event_name[event]);
824
825         for (ci = plugin_callbacks[event]; ci; ci = ci->next)
826           fprintf (file, "%s ", ci->plugin_name);
827
828         fprintf (file, "\n");
829       }
830 }
831
832
833 /* Dump active plugins to stderr.  */
834
835 void
836 debug_active_plugins (void)
837 {
838   dump_active_plugins (stderr);
839 }
840
841 /* The default version check. Compares every field in VERSION. */
842
843 bool
844 plugin_default_version_check (struct plugin_gcc_version *gcc_version,
845                               struct plugin_gcc_version *plugin_version)
846 {
847   if (!gcc_version || !plugin_version)
848     return false;
849
850   if (strcmp (gcc_version->basever, plugin_version->basever))
851     return false;
852   if (strcmp (gcc_version->datestamp, plugin_version->datestamp))
853     return false;
854   if (strcmp (gcc_version->devphase, plugin_version->devphase))
855     return false;
856   if (strcmp (gcc_version->revision, plugin_version->revision))
857     return false;
858   if (strcmp (gcc_version->configuration_arguments,
859               plugin_version->configuration_arguments))
860     return false;
861   return true;
862 }