OSDN Git Service

2009-05-06 Le-Chun Wu <lcwu@google.com>
[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_FINISH:
497         {
498           struct callback_info *new_callback;
499           if (!callback)
500             {
501               error ("Plugin %s registered a null callback function "
502                      "for event %s", plugin_name, plugin_event_name[event]);
503               return;
504             }
505           new_callback = XNEW (struct callback_info);
506           new_callback->plugin_name = plugin_name;
507           new_callback->func = callback;
508           new_callback->user_data = user_data;
509           new_callback->next = plugin_callbacks[event];
510           plugin_callbacks[event] = new_callback;
511         }
512         break;
513       case PLUGIN_EVENT_LAST:
514       default:
515         error ("Unkown callback event registered by plugin %s",
516                plugin_name);
517     }
518 }
519
520
521 /* Called from inside GCC.  Invoke all plug-in callbacks registered with
522    the specified event.
523
524    EVENT    - the event identifier
525    GCC_DATA - event-specific data provided by the compiler  */
526
527 void
528 invoke_plugin_callbacks (enum plugin_event event, void *gcc_data)
529 {
530   timevar_push (TV_PLUGIN_RUN);
531
532   switch (event)
533     {
534       case PLUGIN_FINISH_TYPE:
535       case PLUGIN_FINISH_UNIT:
536       case PLUGIN_CXX_CP_PRE_GENERICIZE:
537       case PLUGIN_FINISH:
538         {
539           /* Iterate over every callback registered with this event and
540              call it.  */
541           struct callback_info *callback = plugin_callbacks[event];
542           for ( ; callback; callback = callback->next)
543             (*callback->func) (gcc_data, callback->user_data);
544         }
545         break;
546
547       case PLUGIN_PASS_MANAGER_SETUP:
548       case PLUGIN_EVENT_LAST:
549       default:
550         gcc_assert (false);
551     }
552
553   timevar_pop (TV_PLUGIN_RUN);
554 }
555
556 #ifdef ENABLE_PLUGIN
557 /* We need a union to cast dlsym return value to a function pointer
558    as ISO C forbids assignment between function pointer and 'void *'.
559    Use explicit union instead of __extension__(<union_cast>) for
560    portability.  */
561 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
562 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
563 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
564
565 /* Try to initialize PLUGIN. Return true if successful. */
566
567 static bool
568 try_init_one_plugin (struct plugin_name_args *plugin)
569 {
570   void *dl_handle;
571   plugin_init_func plugin_init;
572   char *err;
573   PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
574
575   dl_handle = dlopen (plugin->full_name, RTLD_NOW);
576   if (!dl_handle)
577     {
578       error ("Cannot load plugin %s\n%s", plugin->full_name, dlerror ());
579       return false;
580     }
581
582   /* Clear any existing error.  */
583   dlerror ();
584
585   PTR_UNION_AS_VOID_PTR (plugin_init_union) =
586       dlsym (dl_handle, str_plugin_init_func_name);
587   plugin_init = PTR_UNION_AS_CAST_PTR (plugin_init_union);
588
589   if ((err = dlerror ()) != NULL)
590     {
591       error ("Cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
592              plugin->full_name, err);
593       return false;
594     }
595
596   /* Call the plugin-provided initialization routine with the arguments.  */
597   if ((*plugin_init) (plugin->base_name, &gcc_version, plugin->argc,
598                       plugin->argv))
599     {
600       error ("Fail to initialize plugin %s", plugin->full_name);
601       return false;
602     }
603
604   return true;
605 }
606
607
608 /* Routine to dlopen and initialize one plugin. This function is passed to
609    (and called by) the hash table traverse routine. Return 1 for the
610    htab_traverse to continue scan, 0 to stop.
611
612    SLOT - slot of the hash table element
613    INFO - auxiliary pointer handed to hash table traverse routine
614           (unused in this function)  */
615
616 static int
617 init_one_plugin (void **slot, void * ARG_UNUSED (info))
618 {
619   struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
620   bool ok = try_init_one_plugin (plugin);
621   if (!ok)
622     {
623       htab_remove_elt (plugin_name_args_tab, plugin->base_name);
624       XDELETE (plugin);
625     }
626   return 1;
627 }
628
629 #endif  /* ENABLE_PLUGIN  */
630
631 /* Main plugin initialization function.  Called from compile_file() in
632    toplev.c.  */
633
634 void
635 initialize_plugins (void)
636 {
637   /* If no plugin was specified in the command-line, simply return.  */
638   if (!plugin_name_args_tab)
639     return;
640
641   timevar_push (TV_PLUGIN_INIT);
642  
643 #ifdef ENABLE_PLUGIN
644   /* Traverse and initialize each plugin specified in the command-line.  */
645   htab_traverse_noresize (plugin_name_args_tab, init_one_plugin, NULL);
646 #endif
647
648   timevar_pop (TV_PLUGIN_INIT);
649 }
650
651 /* Release memory used by one plugin. */
652
653 static int
654 finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
655 {
656   struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
657   XDELETE (plugin);
658   return 1;
659 }
660
661 /* Free memory allocated by the plugin system. */
662
663 void
664 finalize_plugins (void)
665 {
666   if (!plugin_name_args_tab)
667     return;
668
669   /* We can now delete the plugin_name_args object as it will no longer
670      be used. Note that base_name and argv fields (both of which were also
671      dynamically allocated) are not freed as they could still be used by
672      the plugin code.  */
673
674   htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
675
676   /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it.  */
677   htab_delete (plugin_name_args_tab);
678   plugin_name_args_tab = NULL;
679 }
680
681 /* Used to pass options to htab_traverse callbacks. */
682
683 struct print_options
684 {
685   FILE *file;
686   const char *indent;
687 };
688
689 /* Print the version of one plugin. */
690
691 static int
692 print_version_one_plugin (void **slot, void *data)
693 {
694   struct print_options *opt = (struct print_options *) data;
695   struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
696   const char *version = plugin->version ? plugin->version : "Unknown version.";
697
698   fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
699   return 1;
700 }
701
702 /* Print the version of each plugin. */
703
704 void
705 print_plugins_versions (FILE *file, const char *indent)
706 {
707   struct print_options opt;
708   opt.file = file;
709   opt.indent = indent;
710   if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
711     return;
712
713   fprintf (file, "%sVersions of loaded plugins:\n", indent);
714   htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
715 }
716
717 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
718    argument to htab_traverse_noresize. */
719
720 static int
721 print_help_one_plugin (void **slot, void *data)
722 {
723   struct print_options *opt = (struct print_options *) data;
724   struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
725   const char *help = plugin->help ? plugin->help : "No help available .";
726
727   char *dup = xstrdup (help);
728   char *p, *nl;
729   fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
730
731   for (p = nl = dup; nl; p = nl)
732     {
733       nl = strchr (nl, '\n');
734       if (nl)
735         {
736           *nl = '\0';
737           nl++;
738         }
739       fprintf (opt->file, "   %s %s\n", opt->indent, p);
740     }
741
742   free (dup);
743   return 1;
744 }
745
746 /* Print help for each plugin. The output goes to FILE and every line starts
747    with INDENT. */
748
749 void
750 print_plugins_help (FILE *file, const char *indent)
751 {
752   struct print_options opt;
753   opt.file = file;
754   opt.indent = indent;
755   if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
756     return;
757
758   fprintf (file, "%sHelp for the loaded plugins:\n", indent);
759   htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
760 }
761
762
763 /* Return true if plugins have been loaded.  */
764
765 bool
766 plugins_active_p (void)
767 {
768   enum plugin_event event;
769
770   for (event = PLUGIN_PASS_MANAGER_SETUP; event < PLUGIN_EVENT_LAST; event++)
771     if (plugin_callbacks[event])
772       return true;
773
774   return false;
775 }
776
777
778 /* Dump to FILE the names and associated events for all the active
779    plugins.  */
780
781 void
782 dump_active_plugins (FILE *file)
783 {
784   enum plugin_event event;
785
786   if (!plugins_active_p ())
787     return;
788
789   fprintf (stderr, "Event\t\t\tPlugins\n");
790   for (event = PLUGIN_PASS_MANAGER_SETUP; event < PLUGIN_EVENT_LAST; event++)
791     if (plugin_callbacks[event])
792       {
793         struct callback_info *ci;
794
795         fprintf (file, "%s\t", plugin_event_name[event]);
796
797         for (ci = plugin_callbacks[event]; ci; ci = ci->next)
798           fprintf (file, "%s ", ci->plugin_name);
799
800         fprintf (file, "\n");
801       }
802 }
803
804
805 /* Dump active plugins to stderr.  */
806
807 void
808 debug_active_plugins (void)
809 {
810   dump_active_plugins (stderr);
811 }
812
813 /* The default version check. Compares every field in VERSION. */
814
815 bool
816 plugin_default_version_check (struct plugin_gcc_version *gcc_version,
817                               struct plugin_gcc_version *plugin_version)
818 {
819   if (!gcc_version || !plugin_version)
820     return false;
821
822   if (strcmp (gcc_version->basever, plugin_version->basever))
823     return false;
824   if (strcmp (gcc_version->datestamp, plugin_version->datestamp))
825     return false;
826   if (strcmp (gcc_version->devphase, plugin_version->devphase))
827     return false;
828   if (strcmp (gcc_version->revision, plugin_version->revision))
829     return false;
830   if (strcmp (gcc_version->configuration_arguments,
831               plugin_version->configuration_arguments))
832     return false;
833   return true;
834 }