2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / plugin.c
blob1be60e7ca0e95c65a3338bd465431d3364ea24bd
1 /* Support for GCC plugin mechanism.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 /* This file contains the support for GCC plugin mechanism based on the
21 APIs described in doc/plugin.texi. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "diagnostic-core.h"
27 #include "input.h"
28 #include "alias.h"
29 #include "symtab.h"
30 #include "options.h"
31 #include "flags.h"
32 #include "tree.h"
33 #include "tree-pass.h"
34 #include "intl.h"
35 #include "plugin.h"
37 #ifdef ENABLE_PLUGIN
38 #include "plugin-version.h"
39 #endif
41 #define GCC_PLUGIN_STRINGIFY0(X) #X
42 #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X)
44 /* Event names as strings. Keep in sync with enum plugin_event. */
45 static const char *plugin_event_name_init[] =
47 # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME),
48 # include "plugin.def"
49 # undef DEFEVENT
52 /* A printf format large enough for the largest event above. */
53 #define FMT_FOR_PLUGIN_EVENT "%-32s"
55 const char **plugin_event_name = plugin_event_name_init;
57 /* Event hashtable helpers. */
59 struct event_hasher : typed_noop_remove <const char *>
61 typedef const char **value_type;
62 typedef const char **compare_type;
63 static inline hashval_t hash (const char **);
64 static inline bool equal (const char **, const char **);
67 /* Helper function for the event hash table that hashes the entry V. */
69 inline hashval_t
70 event_hasher::hash (const char **v)
72 return htab_hash_string (*v);
75 /* Helper function for the event hash table that compares the name of an
76 existing entry (S1) with the given string (S2). */
78 inline bool
79 event_hasher::equal (const char **s1, const char **s2)
81 return !strcmp (*s1, *s2);
84 /* A hash table to map event names to the position of the names in the
85 plugin_event_name table. */
86 static hash_table<event_hasher> *event_tab;
88 /* Keep track of the limit of allocated events and space ready for
89 allocating events. */
90 static int event_last = PLUGIN_EVENT_FIRST_DYNAMIC;
91 static int event_horizon = PLUGIN_EVENT_FIRST_DYNAMIC;
93 /* Hash table for the plugin_name_args objects created during command-line
94 parsing. */
95 static htab_t plugin_name_args_tab = NULL;
97 /* List node for keeping track of plugin-registered callback. */
98 struct callback_info
100 const char *plugin_name; /* Name of plugin that registers the callback. */
101 plugin_callback_func func; /* Callback to be called. */
102 void *user_data; /* plugin-specified data. */
103 struct callback_info *next;
106 /* An array of lists of 'callback_info' objects indexed by the event id. */
107 static struct callback_info *plugin_callbacks_init[PLUGIN_EVENT_FIRST_DYNAMIC];
108 static struct callback_info **plugin_callbacks = plugin_callbacks_init;
110 /* For invoke_plugin_callbacks(), see plugin.h. */
111 bool flag_plugin_added = false;
113 #ifdef ENABLE_PLUGIN
114 /* Each plugin should define an initialization function with exactly
115 this name. */
116 static const char *str_plugin_init_func_name = "plugin_init";
118 /* Each plugin should define this symbol to assert that it is
119 distributed under a GPL-compatible license. */
120 static const char *str_license = "plugin_is_GPL_compatible";
121 #endif
123 /* Helper function for the hash table that compares the base_name of the
124 existing entry (S1) with the given string (S2). */
126 static int
127 htab_str_eq (const void *s1, const void *s2)
129 const struct plugin_name_args *plugin = (const struct plugin_name_args *) s1;
130 return !strcmp (plugin->base_name, (const char *) s2);
134 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
135 return NAME. */
137 static char *
138 get_plugin_base_name (const char *full_name)
140 /* First get the base name part of the full-path name, i.e. NAME.so. */
141 char *base_name = xstrdup (lbasename (full_name));
143 /* Then get rid of '.so' part of the name. */
144 strip_off_ending (base_name, strlen (base_name));
146 return base_name;
150 /* Create a plugin_name_args object for the given plugin and insert it
151 to the hash table. This function is called when
152 -fplugin=/path/to/NAME.so or -fplugin=NAME option is processed. */
154 void
155 add_new_plugin (const char* plugin_name)
157 struct plugin_name_args *plugin;
158 void **slot;
159 char *base_name;
160 bool name_is_short;
161 const char *pc;
163 flag_plugin_added = true;
165 /* Replace short names by their full path when relevant. */
166 name_is_short = !IS_ABSOLUTE_PATH (plugin_name);
167 for (pc = plugin_name; name_is_short && *pc; pc++)
168 if (*pc == '.' || IS_DIR_SEPARATOR (*pc))
169 name_is_short = false;
171 if (name_is_short)
173 base_name = CONST_CAST (char*, plugin_name);
174 /* FIXME: the ".so" suffix is currently builtin, since plugins
175 only work on ELF host systems like e.g. Linux or Solaris.
176 When plugins shall be available on non ELF systems such as
177 Windows or MacOS, this code has to be greatly improved. */
178 plugin_name = concat (default_plugin_dir_name (), "/",
179 plugin_name, ".so", NULL);
180 if (access (plugin_name, R_OK))
181 fatal_error
182 (input_location,
183 "inaccessible plugin file %s expanded from short plugin name %s: %m",
184 plugin_name, base_name);
186 else
187 base_name = get_plugin_base_name (plugin_name);
189 /* If this is the first -fplugin= option we encounter, create
190 'plugin_name_args_tab' hash table. */
191 if (!plugin_name_args_tab)
192 plugin_name_args_tab = htab_create (10, htab_hash_string, htab_str_eq,
193 NULL);
195 slot = htab_find_slot (plugin_name_args_tab, base_name, INSERT);
197 /* If the same plugin (name) has been specified earlier, either emit an
198 error or a warning message depending on if they have identical full
199 (path) names. */
200 if (*slot)
202 plugin = (struct plugin_name_args *) *slot;
203 if (strcmp (plugin->full_name, plugin_name))
204 error ("plugin %s was specified with different paths:\n%s\n%s",
205 plugin->base_name, plugin->full_name, plugin_name);
206 return;
209 plugin = XCNEW (struct plugin_name_args);
210 plugin->base_name = base_name;
211 plugin->full_name = plugin_name;
213 *slot = plugin;
217 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
218 'plugin_argument' object for the parsed key-value pair. ARG is
219 the <name>-<key>[=<value>] part of the option. */
221 void
222 parse_plugin_arg_opt (const char *arg)
224 size_t len = 0, name_len = 0, key_len = 0, value_len = 0;
225 const char *ptr, *name_start = arg, *key_start = NULL, *value_start = NULL;
226 char *name, *key, *value;
227 void **slot;
228 bool name_parsed = false, key_parsed = false;
230 /* Iterate over the ARG string and identify the starting character position
231 of 'name', 'key', and 'value' and their lengths. */
232 for (ptr = arg; *ptr; ++ptr)
234 /* Only the first '-' encountered is considered a separator between
235 'name' and 'key'. All the subsequent '-'s are considered part of
236 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
237 the plugin name is 'foo' and the key is 'bar-primary-key'. */
238 if (*ptr == '-' && !name_parsed)
240 name_len = len;
241 len = 0;
242 key_start = ptr + 1;
243 name_parsed = true;
244 continue;
246 else if (*ptr == '=')
248 if (!key_parsed)
250 key_len = len;
251 len = 0;
252 value_start = ptr + 1;
253 key_parsed = true;
255 continue;
257 else
258 ++len;
261 if (!key_start)
263 error ("malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
264 arg);
265 return;
268 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
269 Otherwise, it is the VALUE_LEN. */
270 if (!value_start)
271 key_len = len;
272 else
273 value_len = len;
275 name = XNEWVEC (char, name_len + 1);
276 strncpy (name, name_start, name_len);
277 name[name_len] = '\0';
279 /* Check if the named plugin has already been specified earlier in the
280 command-line. */
281 if (plugin_name_args_tab
282 && ((slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT))
283 != NULL))
285 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
287 key = XNEWVEC (char, key_len + 1);
288 strncpy (key, key_start, key_len);
289 key[key_len] = '\0';
290 if (value_start)
292 value = XNEWVEC (char, value_len + 1);
293 strncpy (value, value_start, value_len);
294 value[value_len] = '\0';
296 else
297 value = NULL;
299 /* Create a plugin_argument object for the parsed key-value pair.
300 If there are already arguments for this plugin, we will need to
301 adjust the argument array size by creating a new array and deleting
302 the old one. If the performance ever becomes an issue, we can
303 change the code by pre-allocating a larger array first. */
304 if (plugin->argc > 0)
306 struct plugin_argument *args = XNEWVEC (struct plugin_argument,
307 plugin->argc + 1);
308 memcpy (args, plugin->argv,
309 sizeof (struct plugin_argument) * plugin->argc);
310 XDELETEVEC (plugin->argv);
311 plugin->argv = args;
312 ++plugin->argc;
314 else
316 gcc_assert (plugin->argv == NULL);
317 plugin->argv = XNEWVEC (struct plugin_argument, 1);
318 plugin->argc = 1;
321 plugin->argv[plugin->argc - 1].key = key;
322 plugin->argv[plugin->argc - 1].value = value;
324 else
325 error ("plugin %s should be specified before -fplugin-arg-%s "
326 "in the command line", name, arg);
328 /* We don't need the plugin's name anymore. Just release it. */
329 XDELETEVEC (name);
332 /* Register additional plugin information. NAME is the name passed to
333 plugin_init. INFO is the information that should be registered. */
335 static void
336 register_plugin_info (const char* name, struct plugin_info *info)
338 void **slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT);
339 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
340 plugin->version = info->version;
341 plugin->help = info->help;
344 /* Look up the event id for NAME. If the name is not found, return -1
345 if INSERT is NO_INSERT. */
348 get_named_event_id (const char *name, enum insert_option insert)
350 const char ***slot;
352 if (!event_tab)
354 int i;
356 event_tab = new hash_table<event_hasher> (150);
357 for (i = 0; i < event_last; i++)
359 slot = event_tab->find_slot (&plugin_event_name[i], INSERT);
360 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
361 *slot = &plugin_event_name[i];
364 slot = event_tab->find_slot (&name, insert);
365 if (slot == NULL)
366 return -1;
367 if (*slot != HTAB_EMPTY_ENTRY)
368 return *slot - &plugin_event_name[0];
370 if (event_last >= event_horizon)
372 event_horizon = event_last * 2;
373 if (plugin_event_name == plugin_event_name_init)
375 plugin_event_name = XNEWVEC (const char *, event_horizon);
376 memcpy (plugin_event_name, plugin_event_name_init,
377 sizeof plugin_event_name_init);
378 plugin_callbacks = XNEWVEC (struct callback_info *, event_horizon);
379 memcpy (plugin_callbacks, plugin_callbacks_init,
380 sizeof plugin_callbacks_init);
382 else
384 plugin_event_name
385 = XRESIZEVEC (const char *, plugin_event_name, event_horizon);
386 plugin_callbacks = XRESIZEVEC (struct callback_info *,
387 plugin_callbacks, event_horizon);
389 /* All the pointers in the hash table will need to be updated. */
390 delete event_tab;
391 event_tab = NULL;
393 else
394 *slot = &plugin_event_name[event_last];
395 plugin_event_name[event_last] = name;
396 return event_last++;
399 /* Called from the plugin's initialization code. Register a single callback.
400 This function can be called multiple times.
402 PLUGIN_NAME - display name for this plugin
403 EVENT - which event the callback is for
404 CALLBACK - the callback to be called at the event
405 USER_DATA - plugin-provided data */
407 void
408 register_callback (const char *plugin_name,
409 int event,
410 plugin_callback_func callback,
411 void *user_data)
413 switch (event)
415 case PLUGIN_PASS_MANAGER_SETUP:
416 gcc_assert (!callback);
417 register_pass ((struct register_pass_info *) user_data);
418 break;
419 case PLUGIN_INFO:
420 gcc_assert (!callback);
421 register_plugin_info (plugin_name, (struct plugin_info *) user_data);
422 break;
423 case PLUGIN_REGISTER_GGC_ROOTS:
424 gcc_assert (!callback);
425 ggc_register_root_tab ((const struct ggc_root_tab*) user_data);
426 break;
427 case PLUGIN_EVENT_FIRST_DYNAMIC:
428 default:
429 if (event < PLUGIN_EVENT_FIRST_DYNAMIC || event >= event_last)
431 error ("unknown callback event registered by plugin %s",
432 plugin_name);
433 return;
435 /* Fall through. */
436 case PLUGIN_START_PARSE_FUNCTION:
437 case PLUGIN_FINISH_PARSE_FUNCTION:
438 case PLUGIN_FINISH_TYPE:
439 case PLUGIN_FINISH_DECL:
440 case PLUGIN_START_UNIT:
441 case PLUGIN_FINISH_UNIT:
442 case PLUGIN_PRE_GENERICIZE:
443 case PLUGIN_GGC_START:
444 case PLUGIN_GGC_MARKING:
445 case PLUGIN_GGC_END:
446 case PLUGIN_ATTRIBUTES:
447 case PLUGIN_PRAGMAS:
448 case PLUGIN_FINISH:
449 case PLUGIN_ALL_PASSES_START:
450 case PLUGIN_ALL_PASSES_END:
451 case PLUGIN_ALL_IPA_PASSES_START:
452 case PLUGIN_ALL_IPA_PASSES_END:
453 case PLUGIN_OVERRIDE_GATE:
454 case PLUGIN_PASS_EXECUTION:
455 case PLUGIN_EARLY_GIMPLE_PASSES_START:
456 case PLUGIN_EARLY_GIMPLE_PASSES_END:
457 case PLUGIN_NEW_PASS:
458 case PLUGIN_INCLUDE_FILE:
460 struct callback_info *new_callback;
461 if (!callback)
463 error ("plugin %s registered a null callback function "
464 "for event %s", plugin_name, plugin_event_name[event]);
465 return;
467 new_callback = XNEW (struct callback_info);
468 new_callback->plugin_name = plugin_name;
469 new_callback->func = callback;
470 new_callback->user_data = user_data;
471 new_callback->next = plugin_callbacks[event];
472 plugin_callbacks[event] = new_callback;
474 break;
478 /* Remove a callback for EVENT which has been registered with for a plugin
479 PLUGIN_NAME. Return PLUGEVT_SUCCESS if a matching callback was
480 found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching
481 callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid. */
483 unregister_callback (const char *plugin_name, int event)
485 struct callback_info *callback, **cbp;
487 if (event >= event_last)
488 return PLUGEVT_NO_SUCH_EVENT;
490 for (cbp = &plugin_callbacks[event]; (callback = *cbp); cbp = &callback->next)
491 if (strcmp (callback->plugin_name, plugin_name) == 0)
493 *cbp = callback->next;
494 return PLUGEVT_SUCCESS;
496 return PLUGEVT_NO_CALLBACK;
499 /* Invoke all plugin callbacks registered with the specified event,
500 called from invoke_plugin_callbacks(). */
503 invoke_plugin_callbacks_full (int event, void *gcc_data)
505 int retval = PLUGEVT_SUCCESS;
507 timevar_push (TV_PLUGIN_RUN);
509 switch (event)
511 case PLUGIN_EVENT_FIRST_DYNAMIC:
512 default:
513 gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
514 gcc_assert (event < event_last);
515 /* Fall through. */
516 case PLUGIN_START_PARSE_FUNCTION:
517 case PLUGIN_FINISH_PARSE_FUNCTION:
518 case PLUGIN_FINISH_TYPE:
519 case PLUGIN_FINISH_DECL:
520 case PLUGIN_START_UNIT:
521 case PLUGIN_FINISH_UNIT:
522 case PLUGIN_PRE_GENERICIZE:
523 case PLUGIN_ATTRIBUTES:
524 case PLUGIN_PRAGMAS:
525 case PLUGIN_FINISH:
526 case PLUGIN_GGC_START:
527 case PLUGIN_GGC_MARKING:
528 case PLUGIN_GGC_END:
529 case PLUGIN_ALL_PASSES_START:
530 case PLUGIN_ALL_PASSES_END:
531 case PLUGIN_ALL_IPA_PASSES_START:
532 case PLUGIN_ALL_IPA_PASSES_END:
533 case PLUGIN_OVERRIDE_GATE:
534 case PLUGIN_PASS_EXECUTION:
535 case PLUGIN_EARLY_GIMPLE_PASSES_START:
536 case PLUGIN_EARLY_GIMPLE_PASSES_END:
537 case PLUGIN_NEW_PASS:
538 case PLUGIN_INCLUDE_FILE:
540 /* Iterate over every callback registered with this event and
541 call it. */
542 struct callback_info *callback = plugin_callbacks[event];
544 if (!callback)
545 retval = PLUGEVT_NO_CALLBACK;
546 for ( ; callback; callback = callback->next)
547 (*callback->func) (gcc_data, callback->user_data);
549 break;
551 case PLUGIN_PASS_MANAGER_SETUP:
552 case PLUGIN_REGISTER_GGC_ROOTS:
553 gcc_assert (false);
556 timevar_pop (TV_PLUGIN_RUN);
557 return retval;
560 #ifdef ENABLE_PLUGIN
561 /* We need a union to cast dlsym return value to a function pointer
562 as ISO C forbids assignment between function pointer and 'void *'.
563 Use explicit union instead of __extension__(<union_cast>) for
564 portability. */
565 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
566 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
567 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
569 /* Try to initialize PLUGIN. Return true if successful. */
571 static bool
572 try_init_one_plugin (struct plugin_name_args *plugin)
574 void *dl_handle;
575 plugin_init_func plugin_init;
576 const char *err;
577 PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
579 /* We use RTLD_NOW to accelerate binding and detect any mismatch
580 between the API expected by the plugin and the GCC API; we use
581 RTLD_GLOBAL which is useful to plugins which themselves call
582 dlopen. */
583 dl_handle = dlopen (plugin->full_name, RTLD_NOW | RTLD_GLOBAL);
584 if (!dl_handle)
586 error ("cannot load plugin %s\n%s", plugin->full_name, dlerror ());
587 return false;
590 /* Clear any existing error. */
591 dlerror ();
593 /* Check the plugin license. */
594 if (dlsym (dl_handle, str_license) == NULL)
595 fatal_error (input_location,
596 "plugin %s is not licensed under a GPL-compatible license\n"
597 "%s", plugin->full_name, dlerror ());
599 PTR_UNION_AS_VOID_PTR (plugin_init_union) =
600 dlsym (dl_handle, str_plugin_init_func_name);
601 plugin_init = PTR_UNION_AS_CAST_PTR (plugin_init_union);
603 if ((err = dlerror ()) != NULL)
605 error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
606 plugin->full_name, err);
607 return false;
610 /* Call the plugin-provided initialization routine with the arguments. */
611 if ((*plugin_init) (plugin, &gcc_version))
613 error ("fail to initialize plugin %s", plugin->full_name);
614 return false;
617 return true;
621 /* Routine to dlopen and initialize one plugin. This function is passed to
622 (and called by) the hash table traverse routine. Return 1 for the
623 htab_traverse to continue scan, 0 to stop.
625 SLOT - slot of the hash table element
626 INFO - auxiliary pointer handed to hash table traverse routine
627 (unused in this function) */
629 static int
630 init_one_plugin (void **slot, void * ARG_UNUSED (info))
632 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
633 bool ok = try_init_one_plugin (plugin);
634 if (!ok)
636 htab_remove_elt (plugin_name_args_tab, plugin->base_name);
637 XDELETE (plugin);
639 return 1;
642 #endif /* ENABLE_PLUGIN */
644 /* Main plugin initialization function. Called from compile_file() in
645 toplev.c. */
647 void
648 initialize_plugins (void)
650 /* If no plugin was specified in the command-line, simply return. */
651 if (!plugin_name_args_tab)
652 return;
654 timevar_push (TV_PLUGIN_INIT);
656 #ifdef ENABLE_PLUGIN
657 /* Traverse and initialize each plugin specified in the command-line. */
658 htab_traverse_noresize (plugin_name_args_tab, init_one_plugin, NULL);
659 #endif
661 timevar_pop (TV_PLUGIN_INIT);
664 /* Release memory used by one plugin. */
666 static int
667 finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
669 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
670 XDELETE (plugin);
671 return 1;
674 /* Free memory allocated by the plugin system. */
676 void
677 finalize_plugins (void)
679 if (!plugin_name_args_tab)
680 return;
682 /* We can now delete the plugin_name_args object as it will no longer
683 be used. Note that base_name and argv fields (both of which were also
684 dynamically allocated) are not freed as they could still be used by
685 the plugin code. */
687 htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
689 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
690 htab_delete (plugin_name_args_tab);
691 plugin_name_args_tab = NULL;
694 /* Used to pass options to htab_traverse callbacks. */
696 struct print_options
698 FILE *file;
699 const char *indent;
702 /* Print the version of one plugin. */
704 static int
705 print_version_one_plugin (void **slot, void *data)
707 struct print_options *opt = (struct print_options *) data;
708 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
709 const char *version = plugin->version ? plugin->version : "Unknown version.";
711 fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
712 return 1;
715 /* Print the version of each plugin. */
717 void
718 print_plugins_versions (FILE *file, const char *indent)
720 struct print_options opt;
721 opt.file = file;
722 opt.indent = indent;
723 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
724 return;
726 fprintf (file, "%sVersions of loaded plugins:\n", indent);
727 htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
730 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
731 argument to htab_traverse_noresize. */
733 static int
734 print_help_one_plugin (void **slot, void *data)
736 struct print_options *opt = (struct print_options *) data;
737 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
738 const char *help = plugin->help ? plugin->help : "No help available .";
740 char *dup = xstrdup (help);
741 char *p, *nl;
742 fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
744 for (p = nl = dup; nl; p = nl)
746 nl = strchr (nl, '\n');
747 if (nl)
749 *nl = '\0';
750 nl++;
752 fprintf (opt->file, " %s %s\n", opt->indent, p);
755 free (dup);
756 return 1;
759 /* Print help for each plugin. The output goes to FILE and every line starts
760 with INDENT. */
762 void
763 print_plugins_help (FILE *file, const char *indent)
765 struct print_options opt;
766 opt.file = file;
767 opt.indent = indent;
768 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
769 return;
771 fprintf (file, "%sHelp for the loaded plugins:\n", indent);
772 htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
776 /* Return true if plugins have been loaded. */
778 bool
779 plugins_active_p (void)
781 int event;
783 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
784 if (plugin_callbacks[event])
785 return true;
787 return false;
791 /* Dump to FILE the names and associated events for all the active
792 plugins. */
794 DEBUG_FUNCTION void
795 dump_active_plugins (FILE *file)
797 int event;
799 if (!plugins_active_p ())
800 return;
802 fprintf (file, FMT_FOR_PLUGIN_EVENT " | %s\n", _("Event"), _("Plugins"));
803 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
804 if (plugin_callbacks[event])
806 struct callback_info *ci;
808 fprintf (file, FMT_FOR_PLUGIN_EVENT " |", plugin_event_name[event]);
810 for (ci = plugin_callbacks[event]; ci; ci = ci->next)
811 fprintf (file, " %s", ci->plugin_name);
813 putc ('\n', file);
818 /* Dump active plugins to stderr. */
820 DEBUG_FUNCTION void
821 debug_active_plugins (void)
823 dump_active_plugins (stderr);
826 /* Give a warning if plugins are present, before an ICE message asking
827 to submit a bug report. */
829 void
830 warn_if_plugins (void)
832 if (plugins_active_p ())
834 fnotice (stderr, "*** WARNING *** there are active plugins, do not report"
835 " this as a bug unless you can reproduce it without enabling"
836 " any plugins.\n");
837 dump_active_plugins (stderr);
842 /* Likewise, as a callback from the diagnostics code. */
844 void
845 plugins_internal_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
846 const char *msgid ATTRIBUTE_UNUSED,
847 va_list *ap ATTRIBUTE_UNUSED)
849 warn_if_plugins ();
852 /* The default version check. Compares every field in VERSION. */
854 bool
855 plugin_default_version_check (struct plugin_gcc_version *gcc_version,
856 struct plugin_gcc_version *plugin_version)
858 if (!gcc_version || !plugin_version)
859 return false;
861 if (strcmp (gcc_version->basever, plugin_version->basever))
862 return false;
863 if (strcmp (gcc_version->datestamp, plugin_version->datestamp))
864 return false;
865 if (strcmp (gcc_version->devphase, plugin_version->devphase))
866 return false;
867 if (strcmp (gcc_version->revision, plugin_version->revision))
868 return false;
869 if (strcmp (gcc_version->configuration_arguments,
870 plugin_version->configuration_arguments))
871 return false;
872 return true;
876 /* Return the current value of event_last, so that plugins which provide
877 additional functionality for events for the benefit of high-level plugins
878 know how many valid entries plugin_event_name holds. */
881 get_event_last (void)
883 return event_last;
887 /* Retrieve the default plugin directory. The gcc driver should have passed
888 it as -iplugindir <dir> to the cc1 program, and it is queriable through the
889 -print-file-name=plugin option to gcc. */
890 const char*
891 default_plugin_dir_name (void)
893 if (!plugindir_string)
894 fatal_error (input_location,
895 "-iplugindir <dir> option not passed from the gcc driver");
896 return plugindir_string;