2015-06-23 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / plugin.c
blob15756f3bd43ecb52786074b91e24a2fa764e4906
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 "alias.h"
28 #include "symtab.h"
29 #include "options.h"
30 #include "flags.h"
31 #include "tree.h"
32 #include "tree-pass.h"
33 #include "intl.h"
34 #include "plugin.h"
36 #ifdef ENABLE_PLUGIN
37 #include "plugin-version.h"
38 #endif
40 #define GCC_PLUGIN_STRINGIFY0(X) #X
41 #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X)
43 /* Event names as strings. Keep in sync with enum plugin_event. */
44 static const char *plugin_event_name_init[] =
46 # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME),
47 # include "plugin.def"
48 # undef DEFEVENT
51 /* A printf format large enough for the largest event above. */
52 #define FMT_FOR_PLUGIN_EVENT "%-32s"
54 const char **plugin_event_name = plugin_event_name_init;
56 /* Event hashtable helpers. */
58 struct event_hasher : typed_noop_remove <const char *>
60 typedef const char **value_type;
61 typedef const char **compare_type;
62 static inline hashval_t hash (const char **);
63 static inline bool equal (const char **, const char **);
66 /* Helper function for the event hash table that hashes the entry V. */
68 inline hashval_t
69 event_hasher::hash (const char **v)
71 return htab_hash_string (*v);
74 /* Helper function for the event hash table that compares the name of an
75 existing entry (S1) with the given string (S2). */
77 inline bool
78 event_hasher::equal (const char **s1, const char **s2)
80 return !strcmp (*s1, *s2);
83 /* A hash table to map event names to the position of the names in the
84 plugin_event_name table. */
85 static hash_table<event_hasher> *event_tab;
87 /* Keep track of the limit of allocated events and space ready for
88 allocating events. */
89 static int event_last = PLUGIN_EVENT_FIRST_DYNAMIC;
90 static int event_horizon = PLUGIN_EVENT_FIRST_DYNAMIC;
92 /* Hash table for the plugin_name_args objects created during command-line
93 parsing. */
94 static htab_t plugin_name_args_tab = NULL;
96 /* List node for keeping track of plugin-registered callback. */
97 struct callback_info
99 const char *plugin_name; /* Name of plugin that registers the callback. */
100 plugin_callback_func func; /* Callback to be called. */
101 void *user_data; /* plugin-specified data. */
102 struct callback_info *next;
105 /* An array of lists of 'callback_info' objects indexed by the event id. */
106 static struct callback_info *plugin_callbacks_init[PLUGIN_EVENT_FIRST_DYNAMIC];
107 static struct callback_info **plugin_callbacks = plugin_callbacks_init;
109 /* For invoke_plugin_callbacks(), see plugin.h. */
110 bool flag_plugin_added = false;
112 #ifdef ENABLE_PLUGIN
113 /* Each plugin should define an initialization function with exactly
114 this name. */
115 static const char *str_plugin_init_func_name = "plugin_init";
117 /* Each plugin should define this symbol to assert that it is
118 distributed under a GPL-compatible license. */
119 static const char *str_license = "plugin_is_GPL_compatible";
120 #endif
122 /* Helper function for the hash table that compares the base_name of the
123 existing entry (S1) with the given string (S2). */
125 static int
126 htab_str_eq (const void *s1, const void *s2)
128 const struct plugin_name_args *plugin = (const struct plugin_name_args *) s1;
129 return !strcmp (plugin->base_name, (const char *) s2);
133 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
134 return NAME. */
136 static char *
137 get_plugin_base_name (const char *full_name)
139 /* First get the base name part of the full-path name, i.e. NAME.so. */
140 char *base_name = xstrdup (lbasename (full_name));
142 /* Then get rid of '.so' part of the name. */
143 strip_off_ending (base_name, strlen (base_name));
145 return base_name;
149 /* Create a plugin_name_args object for the given plugin and insert it
150 to the hash table. This function is called when
151 -fplugin=/path/to/NAME.so or -fplugin=NAME option is processed. */
153 void
154 add_new_plugin (const char* plugin_name)
156 struct plugin_name_args *plugin;
157 void **slot;
158 char *base_name;
159 bool name_is_short;
160 const char *pc;
162 flag_plugin_added = true;
164 /* Replace short names by their full path when relevant. */
165 name_is_short = !IS_ABSOLUTE_PATH (plugin_name);
166 for (pc = plugin_name; name_is_short && *pc; pc++)
167 if (*pc == '.' || IS_DIR_SEPARATOR (*pc))
168 name_is_short = false;
170 if (name_is_short)
172 base_name = CONST_CAST (char*, plugin_name);
173 /* FIXME: the ".so" suffix is currently builtin, since plugins
174 only work on ELF host systems like e.g. Linux or Solaris.
175 When plugins shall be available on non ELF systems such as
176 Windows or MacOS, this code has to be greatly improved. */
177 plugin_name = concat (default_plugin_dir_name (), "/",
178 plugin_name, ".so", NULL);
179 if (access (plugin_name, R_OK))
180 fatal_error
181 (input_location,
182 "inaccessible plugin file %s expanded from short plugin name %s: %m",
183 plugin_name, base_name);
185 else
186 base_name = get_plugin_base_name (plugin_name);
188 /* If this is the first -fplugin= option we encounter, create
189 'plugin_name_args_tab' hash table. */
190 if (!plugin_name_args_tab)
191 plugin_name_args_tab = htab_create (10, htab_hash_string, htab_str_eq,
192 NULL);
194 slot = htab_find_slot (plugin_name_args_tab, base_name, INSERT);
196 /* If the same plugin (name) has been specified earlier, either emit an
197 error or a warning message depending on if they have identical full
198 (path) names. */
199 if (*slot)
201 plugin = (struct plugin_name_args *) *slot;
202 if (strcmp (plugin->full_name, plugin_name))
203 error ("plugin %s was specified with different paths:\n%s\n%s",
204 plugin->base_name, plugin->full_name, plugin_name);
205 return;
208 plugin = XCNEW (struct plugin_name_args);
209 plugin->base_name = base_name;
210 plugin->full_name = plugin_name;
212 *slot = plugin;
216 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
217 'plugin_argument' object for the parsed key-value pair. ARG is
218 the <name>-<key>[=<value>] part of the option. */
220 void
221 parse_plugin_arg_opt (const char *arg)
223 size_t len = 0, name_len = 0, key_len = 0, value_len = 0;
224 const char *ptr, *name_start = arg, *key_start = NULL, *value_start = NULL;
225 char *name, *key, *value;
226 void **slot;
227 bool name_parsed = false, key_parsed = false;
229 /* Iterate over the ARG string and identify the starting character position
230 of 'name', 'key', and 'value' and their lengths. */
231 for (ptr = arg; *ptr; ++ptr)
233 /* Only the first '-' encountered is considered a separator between
234 'name' and 'key'. All the subsequent '-'s are considered part of
235 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
236 the plugin name is 'foo' and the key is 'bar-primary-key'. */
237 if (*ptr == '-' && !name_parsed)
239 name_len = len;
240 len = 0;
241 key_start = ptr + 1;
242 name_parsed = true;
243 continue;
245 else if (*ptr == '=')
247 if (!key_parsed)
249 key_len = len;
250 len = 0;
251 value_start = ptr + 1;
252 key_parsed = true;
254 continue;
256 else
257 ++len;
260 if (!key_start)
262 error ("malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
263 arg);
264 return;
267 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
268 Otherwise, it is the VALUE_LEN. */
269 if (!value_start)
270 key_len = len;
271 else
272 value_len = len;
274 name = XNEWVEC (char, name_len + 1);
275 strncpy (name, name_start, name_len);
276 name[name_len] = '\0';
278 /* Check if the named plugin has already been specified earlier in the
279 command-line. */
280 if (plugin_name_args_tab
281 && ((slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT))
282 != NULL))
284 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
286 key = XNEWVEC (char, key_len + 1);
287 strncpy (key, key_start, key_len);
288 key[key_len] = '\0';
289 if (value_start)
291 value = XNEWVEC (char, value_len + 1);
292 strncpy (value, value_start, value_len);
293 value[value_len] = '\0';
295 else
296 value = NULL;
298 /* Create a plugin_argument object for the parsed key-value pair.
299 If there are already arguments for this plugin, we will need to
300 adjust the argument array size by creating a new array and deleting
301 the old one. If the performance ever becomes an issue, we can
302 change the code by pre-allocating a larger array first. */
303 if (plugin->argc > 0)
305 struct plugin_argument *args = XNEWVEC (struct plugin_argument,
306 plugin->argc + 1);
307 memcpy (args, plugin->argv,
308 sizeof (struct plugin_argument) * plugin->argc);
309 XDELETEVEC (plugin->argv);
310 plugin->argv = args;
311 ++plugin->argc;
313 else
315 gcc_assert (plugin->argv == NULL);
316 plugin->argv = XNEWVEC (struct plugin_argument, 1);
317 plugin->argc = 1;
320 plugin->argv[plugin->argc - 1].key = key;
321 plugin->argv[plugin->argc - 1].value = value;
323 else
324 error ("plugin %s should be specified before -fplugin-arg-%s "
325 "in the command line", name, arg);
327 /* We don't need the plugin's name anymore. Just release it. */
328 XDELETEVEC (name);
331 /* Register additional plugin information. NAME is the name passed to
332 plugin_init. INFO is the information that should be registered. */
334 static void
335 register_plugin_info (const char* name, struct plugin_info *info)
337 void **slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT);
338 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
339 plugin->version = info->version;
340 plugin->help = info->help;
343 /* Look up the event id for NAME. If the name is not found, return -1
344 if INSERT is NO_INSERT. */
347 get_named_event_id (const char *name, enum insert_option insert)
349 const char ***slot;
351 if (!event_tab)
353 int i;
355 event_tab = new hash_table<event_hasher> (150);
356 for (i = 0; i < event_last; i++)
358 slot = event_tab->find_slot (&plugin_event_name[i], INSERT);
359 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
360 *slot = &plugin_event_name[i];
363 slot = event_tab->find_slot (&name, insert);
364 if (slot == NULL)
365 return -1;
366 if (*slot != HTAB_EMPTY_ENTRY)
367 return *slot - &plugin_event_name[0];
369 if (event_last >= event_horizon)
371 event_horizon = event_last * 2;
372 if (plugin_event_name == plugin_event_name_init)
374 plugin_event_name = XNEWVEC (const char *, event_horizon);
375 memcpy (plugin_event_name, plugin_event_name_init,
376 sizeof plugin_event_name_init);
377 plugin_callbacks = XNEWVEC (struct callback_info *, event_horizon);
378 memcpy (plugin_callbacks, plugin_callbacks_init,
379 sizeof plugin_callbacks_init);
381 else
383 plugin_event_name
384 = XRESIZEVEC (const char *, plugin_event_name, event_horizon);
385 plugin_callbacks = XRESIZEVEC (struct callback_info *,
386 plugin_callbacks, event_horizon);
388 /* All the pointers in the hash table will need to be updated. */
389 delete event_tab;
390 event_tab = NULL;
392 else
393 *slot = &plugin_event_name[event_last];
394 plugin_event_name[event_last] = name;
395 return event_last++;
398 /* Called from the plugin's initialization code. Register a single callback.
399 This function can be called multiple times.
401 PLUGIN_NAME - display name for this plugin
402 EVENT - which event the callback is for
403 CALLBACK - the callback to be called at the event
404 USER_DATA - plugin-provided data */
406 void
407 register_callback (const char *plugin_name,
408 int event,
409 plugin_callback_func callback,
410 void *user_data)
412 switch (event)
414 case PLUGIN_PASS_MANAGER_SETUP:
415 gcc_assert (!callback);
416 register_pass ((struct register_pass_info *) user_data);
417 break;
418 case PLUGIN_INFO:
419 gcc_assert (!callback);
420 register_plugin_info (plugin_name, (struct plugin_info *) user_data);
421 break;
422 case PLUGIN_REGISTER_GGC_ROOTS:
423 gcc_assert (!callback);
424 ggc_register_root_tab ((const struct ggc_root_tab*) user_data);
425 break;
426 case PLUGIN_EVENT_FIRST_DYNAMIC:
427 default:
428 if (event < PLUGIN_EVENT_FIRST_DYNAMIC || event >= event_last)
430 error ("unknown callback event registered by plugin %s",
431 plugin_name);
432 return;
434 /* Fall through. */
435 case PLUGIN_START_PARSE_FUNCTION:
436 case PLUGIN_FINISH_PARSE_FUNCTION:
437 case PLUGIN_FINISH_TYPE:
438 case PLUGIN_FINISH_DECL:
439 case PLUGIN_START_UNIT:
440 case PLUGIN_FINISH_UNIT:
441 case PLUGIN_PRE_GENERICIZE:
442 case PLUGIN_GGC_START:
443 case PLUGIN_GGC_MARKING:
444 case PLUGIN_GGC_END:
445 case PLUGIN_ATTRIBUTES:
446 case PLUGIN_PRAGMAS:
447 case PLUGIN_FINISH:
448 case PLUGIN_ALL_PASSES_START:
449 case PLUGIN_ALL_PASSES_END:
450 case PLUGIN_ALL_IPA_PASSES_START:
451 case PLUGIN_ALL_IPA_PASSES_END:
452 case PLUGIN_OVERRIDE_GATE:
453 case PLUGIN_PASS_EXECUTION:
454 case PLUGIN_EARLY_GIMPLE_PASSES_START:
455 case PLUGIN_EARLY_GIMPLE_PASSES_END:
456 case PLUGIN_NEW_PASS:
457 case PLUGIN_INCLUDE_FILE:
459 struct callback_info *new_callback;
460 if (!callback)
462 error ("plugin %s registered a null callback function "
463 "for event %s", plugin_name, plugin_event_name[event]);
464 return;
466 new_callback = XNEW (struct callback_info);
467 new_callback->plugin_name = plugin_name;
468 new_callback->func = callback;
469 new_callback->user_data = user_data;
470 new_callback->next = plugin_callbacks[event];
471 plugin_callbacks[event] = new_callback;
473 break;
477 /* Remove a callback for EVENT which has been registered with for a plugin
478 PLUGIN_NAME. Return PLUGEVT_SUCCESS if a matching callback was
479 found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching
480 callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid. */
482 unregister_callback (const char *plugin_name, int event)
484 struct callback_info *callback, **cbp;
486 if (event >= event_last)
487 return PLUGEVT_NO_SUCH_EVENT;
489 for (cbp = &plugin_callbacks[event]; (callback = *cbp); cbp = &callback->next)
490 if (strcmp (callback->plugin_name, plugin_name) == 0)
492 *cbp = callback->next;
493 return PLUGEVT_SUCCESS;
495 return PLUGEVT_NO_CALLBACK;
498 /* Invoke all plugin callbacks registered with the specified event,
499 called from invoke_plugin_callbacks(). */
502 invoke_plugin_callbacks_full (int event, void *gcc_data)
504 int retval = PLUGEVT_SUCCESS;
506 timevar_push (TV_PLUGIN_RUN);
508 switch (event)
510 case PLUGIN_EVENT_FIRST_DYNAMIC:
511 default:
512 gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
513 gcc_assert (event < event_last);
514 /* Fall through. */
515 case PLUGIN_START_PARSE_FUNCTION:
516 case PLUGIN_FINISH_PARSE_FUNCTION:
517 case PLUGIN_FINISH_TYPE:
518 case PLUGIN_FINISH_DECL:
519 case PLUGIN_START_UNIT:
520 case PLUGIN_FINISH_UNIT:
521 case PLUGIN_PRE_GENERICIZE:
522 case PLUGIN_ATTRIBUTES:
523 case PLUGIN_PRAGMAS:
524 case PLUGIN_FINISH:
525 case PLUGIN_GGC_START:
526 case PLUGIN_GGC_MARKING:
527 case PLUGIN_GGC_END:
528 case PLUGIN_ALL_PASSES_START:
529 case PLUGIN_ALL_PASSES_END:
530 case PLUGIN_ALL_IPA_PASSES_START:
531 case PLUGIN_ALL_IPA_PASSES_END:
532 case PLUGIN_OVERRIDE_GATE:
533 case PLUGIN_PASS_EXECUTION:
534 case PLUGIN_EARLY_GIMPLE_PASSES_START:
535 case PLUGIN_EARLY_GIMPLE_PASSES_END:
536 case PLUGIN_NEW_PASS:
537 case PLUGIN_INCLUDE_FILE:
539 /* Iterate over every callback registered with this event and
540 call it. */
541 struct callback_info *callback = plugin_callbacks[event];
543 if (!callback)
544 retval = PLUGEVT_NO_CALLBACK;
545 for ( ; callback; callback = callback->next)
546 (*callback->func) (gcc_data, callback->user_data);
548 break;
550 case PLUGIN_PASS_MANAGER_SETUP:
551 case PLUGIN_REGISTER_GGC_ROOTS:
552 gcc_assert (false);
555 timevar_pop (TV_PLUGIN_RUN);
556 return retval;
559 #ifdef ENABLE_PLUGIN
560 /* We need a union to cast dlsym return value to a function pointer
561 as ISO C forbids assignment between function pointer and 'void *'.
562 Use explicit union instead of __extension__(<union_cast>) for
563 portability. */
564 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
565 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
566 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
568 /* Try to initialize PLUGIN. Return true if successful. */
570 static bool
571 try_init_one_plugin (struct plugin_name_args *plugin)
573 void *dl_handle;
574 plugin_init_func plugin_init;
575 const char *err;
576 PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
578 /* We use RTLD_NOW to accelerate binding and detect any mismatch
579 between the API expected by the plugin and the GCC API; we use
580 RTLD_GLOBAL which is useful to plugins which themselves call
581 dlopen. */
582 dl_handle = dlopen (plugin->full_name, RTLD_NOW | RTLD_GLOBAL);
583 if (!dl_handle)
585 error ("cannot load plugin %s\n%s", plugin->full_name, dlerror ());
586 return false;
589 /* Clear any existing error. */
590 dlerror ();
592 /* Check the plugin license. */
593 if (dlsym (dl_handle, str_license) == NULL)
594 fatal_error (input_location,
595 "plugin %s is not licensed under a GPL-compatible license\n"
596 "%s", plugin->full_name, dlerror ());
598 PTR_UNION_AS_VOID_PTR (plugin_init_union) =
599 dlsym (dl_handle, str_plugin_init_func_name);
600 plugin_init = PTR_UNION_AS_CAST_PTR (plugin_init_union);
602 if ((err = dlerror ()) != NULL)
604 error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
605 plugin->full_name, err);
606 return false;
609 /* Call the plugin-provided initialization routine with the arguments. */
610 if ((*plugin_init) (plugin, &gcc_version))
612 error ("fail to initialize plugin %s", plugin->full_name);
613 return false;
616 return true;
620 /* Routine to dlopen and initialize one plugin. This function is passed to
621 (and called by) the hash table traverse routine. Return 1 for the
622 htab_traverse to continue scan, 0 to stop.
624 SLOT - slot of the hash table element
625 INFO - auxiliary pointer handed to hash table traverse routine
626 (unused in this function) */
628 static int
629 init_one_plugin (void **slot, void * ARG_UNUSED (info))
631 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
632 bool ok = try_init_one_plugin (plugin);
633 if (!ok)
635 htab_remove_elt (plugin_name_args_tab, plugin->base_name);
636 XDELETE (plugin);
638 return 1;
641 #endif /* ENABLE_PLUGIN */
643 /* Main plugin initialization function. Called from compile_file() in
644 toplev.c. */
646 void
647 initialize_plugins (void)
649 /* If no plugin was specified in the command-line, simply return. */
650 if (!plugin_name_args_tab)
651 return;
653 timevar_push (TV_PLUGIN_INIT);
655 #ifdef ENABLE_PLUGIN
656 /* Traverse and initialize each plugin specified in the command-line. */
657 htab_traverse_noresize (plugin_name_args_tab, init_one_plugin, NULL);
658 #endif
660 timevar_pop (TV_PLUGIN_INIT);
663 /* Release memory used by one plugin. */
665 static int
666 finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
668 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
669 XDELETE (plugin);
670 return 1;
673 /* Free memory allocated by the plugin system. */
675 void
676 finalize_plugins (void)
678 if (!plugin_name_args_tab)
679 return;
681 /* We can now delete the plugin_name_args object as it will no longer
682 be used. Note that base_name and argv fields (both of which were also
683 dynamically allocated) are not freed as they could still be used by
684 the plugin code. */
686 htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
688 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
689 htab_delete (plugin_name_args_tab);
690 plugin_name_args_tab = NULL;
693 /* Used to pass options to htab_traverse callbacks. */
695 struct print_options
697 FILE *file;
698 const char *indent;
701 /* Print the version of one plugin. */
703 static int
704 print_version_one_plugin (void **slot, void *data)
706 struct print_options *opt = (struct print_options *) data;
707 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
708 const char *version = plugin->version ? plugin->version : "Unknown version.";
710 fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
711 return 1;
714 /* Print the version of each plugin. */
716 void
717 print_plugins_versions (FILE *file, const char *indent)
719 struct print_options opt;
720 opt.file = file;
721 opt.indent = indent;
722 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
723 return;
725 fprintf (file, "%sVersions of loaded plugins:\n", indent);
726 htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
729 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
730 argument to htab_traverse_noresize. */
732 static int
733 print_help_one_plugin (void **slot, void *data)
735 struct print_options *opt = (struct print_options *) data;
736 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
737 const char *help = plugin->help ? plugin->help : "No help available .";
739 char *dup = xstrdup (help);
740 char *p, *nl;
741 fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
743 for (p = nl = dup; nl; p = nl)
745 nl = strchr (nl, '\n');
746 if (nl)
748 *nl = '\0';
749 nl++;
751 fprintf (opt->file, " %s %s\n", opt->indent, p);
754 free (dup);
755 return 1;
758 /* Print help for each plugin. The output goes to FILE and every line starts
759 with INDENT. */
761 void
762 print_plugins_help (FILE *file, const char *indent)
764 struct print_options opt;
765 opt.file = file;
766 opt.indent = indent;
767 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
768 return;
770 fprintf (file, "%sHelp for the loaded plugins:\n", indent);
771 htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
775 /* Return true if plugins have been loaded. */
777 bool
778 plugins_active_p (void)
780 int event;
782 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
783 if (plugin_callbacks[event])
784 return true;
786 return false;
790 /* Dump to FILE the names and associated events for all the active
791 plugins. */
793 DEBUG_FUNCTION void
794 dump_active_plugins (FILE *file)
796 int event;
798 if (!plugins_active_p ())
799 return;
801 fprintf (file, FMT_FOR_PLUGIN_EVENT " | %s\n", _("Event"), _("Plugins"));
802 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
803 if (plugin_callbacks[event])
805 struct callback_info *ci;
807 fprintf (file, FMT_FOR_PLUGIN_EVENT " |", plugin_event_name[event]);
809 for (ci = plugin_callbacks[event]; ci; ci = ci->next)
810 fprintf (file, " %s", ci->plugin_name);
812 putc ('\n', file);
817 /* Dump active plugins to stderr. */
819 DEBUG_FUNCTION void
820 debug_active_plugins (void)
822 dump_active_plugins (stderr);
825 /* Give a warning if plugins are present, before an ICE message asking
826 to submit a bug report. */
828 void
829 warn_if_plugins (void)
831 if (plugins_active_p ())
833 fnotice (stderr, "*** WARNING *** there are active plugins, do not report"
834 " this as a bug unless you can reproduce it without enabling"
835 " any plugins.\n");
836 dump_active_plugins (stderr);
841 /* Likewise, as a callback from the diagnostics code. */
843 void
844 plugins_internal_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
845 const char *msgid ATTRIBUTE_UNUSED,
846 va_list *ap ATTRIBUTE_UNUSED)
848 warn_if_plugins ();
851 /* The default version check. Compares every field in VERSION. */
853 bool
854 plugin_default_version_check (struct plugin_gcc_version *gcc_version,
855 struct plugin_gcc_version *plugin_version)
857 if (!gcc_version || !plugin_version)
858 return false;
860 if (strcmp (gcc_version->basever, plugin_version->basever))
861 return false;
862 if (strcmp (gcc_version->datestamp, plugin_version->datestamp))
863 return false;
864 if (strcmp (gcc_version->devphase, plugin_version->devphase))
865 return false;
866 if (strcmp (gcc_version->revision, plugin_version->revision))
867 return false;
868 if (strcmp (gcc_version->configuration_arguments,
869 plugin_version->configuration_arguments))
870 return false;
871 return true;
875 /* Return the current value of event_last, so that plugins which provide
876 additional functionality for events for the benefit of high-level plugins
877 know how many valid entries plugin_event_name holds. */
880 get_event_last (void)
882 return event_last;
886 /* Retrieve the default plugin directory. The gcc driver should have passed
887 it as -iplugindir <dir> to the cc1 program, and it is queriable through the
888 -print-file-name=plugin option to gcc. */
889 const char*
890 default_plugin_dir_name (void)
892 if (!plugindir_string)
893 fatal_error (input_location,
894 "-iplugindir <dir> option not passed from the gcc driver");
895 return plugindir_string;