OSDN Git Service

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