Minor fix
[official-gcc.git] / gcc / plugin.c
blobd924438c9404912d4124325220b287416050c8b5
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 "hash-table.h"
27 #include "diagnostic-core.h"
28 #include "hash-set.h"
29 #include "machmode.h"
30 #include "vec.h"
31 #include "double-int.h"
32 #include "input.h"
33 #include "alias.h"
34 #include "symtab.h"
35 #include "options.h"
36 #include "flags.h"
37 #include "wide-int.h"
38 #include "inchash.h"
39 #include "tree.h"
40 #include "tree-pass.h"
41 #include "intl.h"
42 #include "plugin.h"
43 #include "ggc.h"
45 #ifdef ENABLE_PLUGIN
46 #include "plugin-version.h"
47 #endif
49 #define GCC_PLUGIN_STRINGIFY0(X) #X
50 #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X)
52 /* Event names as strings. Keep in sync with enum plugin_event. */
53 static const char *plugin_event_name_init[] =
55 # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME),
56 # include "plugin.def"
57 # undef DEFEVENT
60 /* A printf format large enough for the largest event above. */
61 #define FMT_FOR_PLUGIN_EVENT "%-32s"
63 const char **plugin_event_name = plugin_event_name_init;
65 /* Event hashtable helpers. */
67 struct event_hasher : typed_noop_remove <const char *>
69 typedef const char *value_type;
70 typedef const char *compare_type;
71 static inline hashval_t hash (const value_type *);
72 static inline bool equal (const value_type *, const compare_type *);
75 /* Helper function for the event hash table that hashes the entry V. */
77 inline hashval_t
78 event_hasher::hash (const value_type *v)
80 return htab_hash_string (*v);
83 /* Helper function for the event hash table that compares the name of an
84 existing entry (S1) with the given string (S2). */
86 inline bool
87 event_hasher::equal (const value_type *s1, const compare_type *s2)
89 return !strcmp (*s1, *s2);
92 /* A hash table to map event names to the position of the names in the
93 plugin_event_name table. */
94 static hash_table<event_hasher> *event_tab;
96 /* Keep track of the limit of allocated events and space ready for
97 allocating events. */
98 static int event_last = PLUGIN_EVENT_FIRST_DYNAMIC;
99 static int event_horizon = PLUGIN_EVENT_FIRST_DYNAMIC;
101 /* Hash table for the plugin_name_args objects created during command-line
102 parsing. */
103 static htab_t plugin_name_args_tab = NULL;
105 /* List node for keeping track of plugin-registered callback. */
106 struct callback_info
108 const char *plugin_name; /* Name of plugin that registers the callback. */
109 plugin_callback_func func; /* Callback to be called. */
110 void *user_data; /* plugin-specified data. */
111 struct callback_info *next;
114 /* An array of lists of 'callback_info' objects indexed by the event id. */
115 static struct callback_info *plugin_callbacks_init[PLUGIN_EVENT_FIRST_DYNAMIC];
116 static struct callback_info **plugin_callbacks = plugin_callbacks_init;
118 /* For invoke_plugin_callbacks(), see plugin.h. */
119 bool flag_plugin_added = false;
121 #ifdef ENABLE_PLUGIN
122 /* Each plugin should define an initialization function with exactly
123 this name. */
124 static const char *str_plugin_init_func_name = "plugin_init";
126 /* Each plugin should define this symbol to assert that it is
127 distributed under a GPL-compatible license. */
128 static const char *str_license = "plugin_is_GPL_compatible";
129 #endif
131 /* Helper function for the hash table that compares the base_name of the
132 existing entry (S1) with the given string (S2). */
134 static int
135 htab_str_eq (const void *s1, const void *s2)
137 const struct plugin_name_args *plugin = (const struct plugin_name_args *) s1;
138 return !strcmp (plugin->base_name, (const char *) s2);
142 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
143 return NAME. */
145 static char *
146 get_plugin_base_name (const char *full_name)
148 /* First get the base name part of the full-path name, i.e. NAME.so. */
149 char *base_name = xstrdup (lbasename (full_name));
151 /* Then get rid of '.so' part of the name. */
152 strip_off_ending (base_name, strlen (base_name));
154 return base_name;
158 /* Create a plugin_name_args object for the given plugin and insert it
159 to the hash table. This function is called when
160 -fplugin=/path/to/NAME.so or -fplugin=NAME option is processed. */
162 void
163 add_new_plugin (const char* plugin_name)
165 struct plugin_name_args *plugin;
166 void **slot;
167 char *base_name;
168 bool name_is_short;
169 const char *pc;
171 flag_plugin_added = true;
173 /* Replace short names by their full path when relevant. */
174 name_is_short = !IS_ABSOLUTE_PATH (plugin_name);
175 for (pc = plugin_name; name_is_short && *pc; pc++)
176 if (*pc == '.' || IS_DIR_SEPARATOR (*pc))
177 name_is_short = false;
179 if (name_is_short)
181 base_name = CONST_CAST (char*, plugin_name);
182 /* FIXME: the ".so" suffix is currently builtin, since plugins
183 only work on ELF host systems like e.g. Linux or Solaris.
184 When plugins shall be available on non ELF systems such as
185 Windows or MacOS, this code has to be greatly improved. */
186 plugin_name = concat (default_plugin_dir_name (), "/",
187 plugin_name, ".so", NULL);
188 if (access (plugin_name, R_OK))
189 fatal_error
190 (input_location,
191 "inaccessible plugin file %s expanded from short plugin name %s: %m",
192 plugin_name, base_name);
194 else
195 base_name = get_plugin_base_name (plugin_name);
197 /* If this is the first -fplugin= option we encounter, create
198 'plugin_name_args_tab' hash table. */
199 if (!plugin_name_args_tab)
200 plugin_name_args_tab = htab_create (10, htab_hash_string, htab_str_eq,
201 NULL);
203 slot = htab_find_slot (plugin_name_args_tab, base_name, INSERT);
205 /* If the same plugin (name) has been specified earlier, either emit an
206 error or a warning message depending on if they have identical full
207 (path) names. */
208 if (*slot)
210 plugin = (struct plugin_name_args *) *slot;
211 if (strcmp (plugin->full_name, plugin_name))
212 error ("plugin %s was specified with different paths:\n%s\n%s",
213 plugin->base_name, plugin->full_name, plugin_name);
214 return;
217 plugin = XCNEW (struct plugin_name_args);
218 plugin->base_name = base_name;
219 plugin->full_name = plugin_name;
221 *slot = plugin;
225 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
226 'plugin_argument' object for the parsed key-value pair. ARG is
227 the <name>-<key>[=<value>] part of the option. */
229 void
230 parse_plugin_arg_opt (const char *arg)
232 size_t len = 0, name_len = 0, key_len = 0, value_len = 0;
233 const char *ptr, *name_start = arg, *key_start = NULL, *value_start = NULL;
234 char *name, *key, *value;
235 void **slot;
236 bool name_parsed = false, key_parsed = false;
238 /* Iterate over the ARG string and identify the starting character position
239 of 'name', 'key', and 'value' and their lengths. */
240 for (ptr = arg; *ptr; ++ptr)
242 /* Only the first '-' encountered is considered a separator between
243 'name' and 'key'. All the subsequent '-'s are considered part of
244 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
245 the plugin name is 'foo' and the key is 'bar-primary-key'. */
246 if (*ptr == '-' && !name_parsed)
248 name_len = len;
249 len = 0;
250 key_start = ptr + 1;
251 name_parsed = true;
252 continue;
254 else if (*ptr == '=')
256 if (!key_parsed)
258 key_len = len;
259 len = 0;
260 value_start = ptr + 1;
261 key_parsed = true;
263 continue;
265 else
266 ++len;
269 if (!key_start)
271 error ("malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
272 arg);
273 return;
276 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
277 Otherwise, it is the VALUE_LEN. */
278 if (!value_start)
279 key_len = len;
280 else
281 value_len = len;
283 name = XNEWVEC (char, name_len + 1);
284 strncpy (name, name_start, name_len);
285 name[name_len] = '\0';
287 /* Check if the named plugin has already been specified earlier in the
288 command-line. */
289 if (plugin_name_args_tab
290 && ((slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT))
291 != NULL))
293 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
295 key = XNEWVEC (char, key_len + 1);
296 strncpy (key, key_start, key_len);
297 key[key_len] = '\0';
298 if (value_start)
300 value = XNEWVEC (char, value_len + 1);
301 strncpy (value, value_start, value_len);
302 value[value_len] = '\0';
304 else
305 value = NULL;
307 /* Create a plugin_argument object for the parsed key-value pair.
308 If there are already arguments for this plugin, we will need to
309 adjust the argument array size by creating a new array and deleting
310 the old one. If the performance ever becomes an issue, we can
311 change the code by pre-allocating a larger array first. */
312 if (plugin->argc > 0)
314 struct plugin_argument *args = XNEWVEC (struct plugin_argument,
315 plugin->argc + 1);
316 memcpy (args, plugin->argv,
317 sizeof (struct plugin_argument) * plugin->argc);
318 XDELETEVEC (plugin->argv);
319 plugin->argv = args;
320 ++plugin->argc;
322 else
324 gcc_assert (plugin->argv == NULL);
325 plugin->argv = XNEWVEC (struct plugin_argument, 1);
326 plugin->argc = 1;
329 plugin->argv[plugin->argc - 1].key = key;
330 plugin->argv[plugin->argc - 1].value = value;
332 else
333 error ("plugin %s should be specified before -fplugin-arg-%s "
334 "in the command line", name, arg);
336 /* We don't need the plugin's name anymore. Just release it. */
337 XDELETEVEC (name);
340 /* Register additional plugin information. NAME is the name passed to
341 plugin_init. INFO is the information that should be registered. */
343 static void
344 register_plugin_info (const char* name, struct plugin_info *info)
346 void **slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT);
347 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
348 plugin->version = info->version;
349 plugin->help = info->help;
352 /* Look up the event id for NAME. If the name is not found, return -1
353 if INSERT is NO_INSERT. */
356 get_named_event_id (const char *name, enum insert_option insert)
358 const char ***slot;
360 if (!event_tab)
362 int i;
364 event_tab = new hash_table<event_hasher> (150);
365 for (i = 0; i < event_last; i++)
367 slot = event_tab->find_slot (&plugin_event_name[i], INSERT);
368 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
369 *slot = &plugin_event_name[i];
372 slot = event_tab->find_slot (&name, insert);
373 if (slot == NULL)
374 return -1;
375 if (*slot != HTAB_EMPTY_ENTRY)
376 return *slot - &plugin_event_name[0];
378 if (event_last >= event_horizon)
380 event_horizon = event_last * 2;
381 if (plugin_event_name == plugin_event_name_init)
383 plugin_event_name = XNEWVEC (const char *, event_horizon);
384 memcpy (plugin_event_name, plugin_event_name_init,
385 sizeof plugin_event_name_init);
386 plugin_callbacks = XNEWVEC (struct callback_info *, event_horizon);
387 memcpy (plugin_callbacks, plugin_callbacks_init,
388 sizeof plugin_callbacks_init);
390 else
392 plugin_event_name
393 = XRESIZEVEC (const char *, plugin_event_name, event_horizon);
394 plugin_callbacks = XRESIZEVEC (struct callback_info *,
395 plugin_callbacks, event_horizon);
397 /* All the pointers in the hash table will need to be updated. */
398 delete event_tab;
399 event_tab = NULL;
401 else
402 *slot = &plugin_event_name[event_last];
403 plugin_event_name[event_last] = name;
404 return event_last++;
407 /* Called from the plugin's initialization code. Register a single callback.
408 This function can be called multiple times.
410 PLUGIN_NAME - display name for this plugin
411 EVENT - which event the callback is for
412 CALLBACK - the callback to be called at the event
413 USER_DATA - plugin-provided data */
415 void
416 register_callback (const char *plugin_name,
417 int event,
418 plugin_callback_func callback,
419 void *user_data)
421 switch (event)
423 case PLUGIN_PASS_MANAGER_SETUP:
424 gcc_assert (!callback);
425 register_pass ((struct register_pass_info *) user_data);
426 break;
427 case PLUGIN_INFO:
428 gcc_assert (!callback);
429 register_plugin_info (plugin_name, (struct plugin_info *) user_data);
430 break;
431 case PLUGIN_REGISTER_GGC_ROOTS:
432 gcc_assert (!callback);
433 ggc_register_root_tab ((const struct ggc_root_tab*) user_data);
434 break;
435 case PLUGIN_EVENT_FIRST_DYNAMIC:
436 default:
437 if (event < PLUGIN_EVENT_FIRST_DYNAMIC || event >= event_last)
439 error ("unknown callback event registered by plugin %s",
440 plugin_name);
441 return;
443 /* Fall through. */
444 case PLUGIN_FINISH_TYPE:
445 case PLUGIN_FINISH_DECL:
446 case PLUGIN_START_UNIT:
447 case PLUGIN_FINISH_UNIT:
448 case PLUGIN_PRE_GENERICIZE:
449 case PLUGIN_GGC_START:
450 case PLUGIN_GGC_MARKING:
451 case PLUGIN_GGC_END:
452 case PLUGIN_ATTRIBUTES:
453 case PLUGIN_PRAGMAS:
454 case PLUGIN_FINISH:
455 case PLUGIN_ALL_PASSES_START:
456 case PLUGIN_ALL_PASSES_END:
457 case PLUGIN_ALL_IPA_PASSES_START:
458 case PLUGIN_ALL_IPA_PASSES_END:
459 case PLUGIN_OVERRIDE_GATE:
460 case PLUGIN_PASS_EXECUTION:
461 case PLUGIN_EARLY_GIMPLE_PASSES_START:
462 case PLUGIN_EARLY_GIMPLE_PASSES_END:
463 case PLUGIN_NEW_PASS:
464 case PLUGIN_INCLUDE_FILE:
466 struct callback_info *new_callback;
467 if (!callback)
469 error ("plugin %s registered a null callback function "
470 "for event %s", plugin_name, plugin_event_name[event]);
471 return;
473 new_callback = XNEW (struct callback_info);
474 new_callback->plugin_name = plugin_name;
475 new_callback->func = callback;
476 new_callback->user_data = user_data;
477 new_callback->next = plugin_callbacks[event];
478 plugin_callbacks[event] = new_callback;
480 break;
484 /* Remove a callback for EVENT which has been registered with for a plugin
485 PLUGIN_NAME. Return PLUGEVT_SUCCESS if a matching callback was
486 found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching
487 callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid. */
489 unregister_callback (const char *plugin_name, int event)
491 struct callback_info *callback, **cbp;
493 if (event >= event_last)
494 return PLUGEVT_NO_SUCH_EVENT;
496 for (cbp = &plugin_callbacks[event]; (callback = *cbp); cbp = &callback->next)
497 if (strcmp (callback->plugin_name, plugin_name) == 0)
499 *cbp = callback->next;
500 return PLUGEVT_SUCCESS;
502 return PLUGEVT_NO_CALLBACK;
505 /* Invoke all plugin callbacks registered with the specified event,
506 called from invoke_plugin_callbacks(). */
509 invoke_plugin_callbacks_full (int event, void *gcc_data)
511 int retval = PLUGEVT_SUCCESS;
513 timevar_push (TV_PLUGIN_RUN);
515 switch (event)
517 case PLUGIN_EVENT_FIRST_DYNAMIC:
518 default:
519 gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
520 gcc_assert (event < event_last);
521 /* Fall through. */
522 case PLUGIN_FINISH_TYPE:
523 case PLUGIN_FINISH_DECL:
524 case PLUGIN_START_UNIT:
525 case PLUGIN_FINISH_UNIT:
526 case PLUGIN_PRE_GENERICIZE:
527 case PLUGIN_ATTRIBUTES:
528 case PLUGIN_PRAGMAS:
529 case PLUGIN_FINISH:
530 case PLUGIN_GGC_START:
531 case PLUGIN_GGC_MARKING:
532 case PLUGIN_GGC_END:
533 case PLUGIN_ALL_PASSES_START:
534 case PLUGIN_ALL_PASSES_END:
535 case PLUGIN_ALL_IPA_PASSES_START:
536 case PLUGIN_ALL_IPA_PASSES_END:
537 case PLUGIN_OVERRIDE_GATE:
538 case PLUGIN_PASS_EXECUTION:
539 case PLUGIN_EARLY_GIMPLE_PASSES_START:
540 case PLUGIN_EARLY_GIMPLE_PASSES_END:
541 case PLUGIN_NEW_PASS:
542 case PLUGIN_INCLUDE_FILE:
544 /* Iterate over every callback registered with this event and
545 call it. */
546 struct callback_info *callback = plugin_callbacks[event];
548 if (!callback)
549 retval = PLUGEVT_NO_CALLBACK;
550 for ( ; callback; callback = callback->next)
551 (*callback->func) (gcc_data, callback->user_data);
553 break;
555 case PLUGIN_PASS_MANAGER_SETUP:
556 case PLUGIN_REGISTER_GGC_ROOTS:
557 gcc_assert (false);
560 timevar_pop (TV_PLUGIN_RUN);
561 return retval;
564 #ifdef ENABLE_PLUGIN
565 /* We need a union to cast dlsym return value to a function pointer
566 as ISO C forbids assignment between function pointer and 'void *'.
567 Use explicit union instead of __extension__(<union_cast>) for
568 portability. */
569 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
570 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
571 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
573 /* Try to initialize PLUGIN. Return true if successful. */
575 static bool
576 try_init_one_plugin (struct plugin_name_args *plugin)
578 void *dl_handle;
579 plugin_init_func plugin_init;
580 const char *err;
581 PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
583 /* We use RTLD_NOW to accelerate binding and detect any mismatch
584 between the API expected by the plugin and the GCC API; we use
585 RTLD_GLOBAL which is useful to plugins which themselves call
586 dlopen. */
587 dl_handle = dlopen (plugin->full_name, RTLD_NOW | RTLD_GLOBAL);
588 if (!dl_handle)
590 error ("cannot load plugin %s\n%s", plugin->full_name, dlerror ());
591 return false;
594 /* Clear any existing error. */
595 dlerror ();
597 /* Check the plugin license. */
598 if (dlsym (dl_handle, str_license) == NULL)
599 fatal_error (input_location,
600 "plugin %s is not licensed under a GPL-compatible license\n"
601 "%s", plugin->full_name, dlerror ());
603 PTR_UNION_AS_VOID_PTR (plugin_init_union) =
604 dlsym (dl_handle, str_plugin_init_func_name);
605 plugin_init = PTR_UNION_AS_CAST_PTR (plugin_init_union);
607 if ((err = dlerror ()) != NULL)
609 error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
610 plugin->full_name, err);
611 return false;
614 /* Call the plugin-provided initialization routine with the arguments. */
615 if ((*plugin_init) (plugin, &gcc_version))
617 error ("fail to initialize plugin %s", plugin->full_name);
618 return false;
621 return true;
625 /* Routine to dlopen and initialize one plugin. This function is passed to
626 (and called by) the hash table traverse routine. Return 1 for the
627 htab_traverse to continue scan, 0 to stop.
629 SLOT - slot of the hash table element
630 INFO - auxiliary pointer handed to hash table traverse routine
631 (unused in this function) */
633 static int
634 init_one_plugin (void **slot, void * ARG_UNUSED (info))
636 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
637 bool ok = try_init_one_plugin (plugin);
638 if (!ok)
640 htab_remove_elt (plugin_name_args_tab, plugin->base_name);
641 XDELETE (plugin);
643 return 1;
646 #endif /* ENABLE_PLUGIN */
648 /* Main plugin initialization function. Called from compile_file() in
649 toplev.c. */
651 void
652 initialize_plugins (void)
654 /* If no plugin was specified in the command-line, simply return. */
655 if (!plugin_name_args_tab)
656 return;
658 timevar_push (TV_PLUGIN_INIT);
660 #ifdef ENABLE_PLUGIN
661 /* Traverse and initialize each plugin specified in the command-line. */
662 htab_traverse_noresize (plugin_name_args_tab, init_one_plugin, NULL);
663 #endif
665 timevar_pop (TV_PLUGIN_INIT);
668 /* Release memory used by one plugin. */
670 static int
671 finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
673 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
674 XDELETE (plugin);
675 return 1;
678 /* Free memory allocated by the plugin system. */
680 void
681 finalize_plugins (void)
683 if (!plugin_name_args_tab)
684 return;
686 /* We can now delete the plugin_name_args object as it will no longer
687 be used. Note that base_name and argv fields (both of which were also
688 dynamically allocated) are not freed as they could still be used by
689 the plugin code. */
691 htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
693 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
694 htab_delete (plugin_name_args_tab);
695 plugin_name_args_tab = NULL;
698 /* Used to pass options to htab_traverse callbacks. */
700 struct print_options
702 FILE *file;
703 const char *indent;
706 /* Print the version of one plugin. */
708 static int
709 print_version_one_plugin (void **slot, void *data)
711 struct print_options *opt = (struct print_options *) data;
712 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
713 const char *version = plugin->version ? plugin->version : "Unknown version.";
715 fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
716 return 1;
719 /* Print the version of each plugin. */
721 void
722 print_plugins_versions (FILE *file, const char *indent)
724 struct print_options opt;
725 opt.file = file;
726 opt.indent = indent;
727 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
728 return;
730 fprintf (file, "%sVersions of loaded plugins:\n", indent);
731 htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
734 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
735 argument to htab_traverse_noresize. */
737 static int
738 print_help_one_plugin (void **slot, void *data)
740 struct print_options *opt = (struct print_options *) data;
741 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
742 const char *help = plugin->help ? plugin->help : "No help available .";
744 char *dup = xstrdup (help);
745 char *p, *nl;
746 fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
748 for (p = nl = dup; nl; p = nl)
750 nl = strchr (nl, '\n');
751 if (nl)
753 *nl = '\0';
754 nl++;
756 fprintf (opt->file, " %s %s\n", opt->indent, p);
759 free (dup);
760 return 1;
763 /* Print help for each plugin. The output goes to FILE and every line starts
764 with INDENT. */
766 void
767 print_plugins_help (FILE *file, const char *indent)
769 struct print_options opt;
770 opt.file = file;
771 opt.indent = indent;
772 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
773 return;
775 fprintf (file, "%sHelp for the loaded plugins:\n", indent);
776 htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
780 /* Return true if plugins have been loaded. */
782 bool
783 plugins_active_p (void)
785 int event;
787 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
788 if (plugin_callbacks[event])
789 return true;
791 return false;
795 /* Dump to FILE the names and associated events for all the active
796 plugins. */
798 DEBUG_FUNCTION void
799 dump_active_plugins (FILE *file)
801 int event;
803 if (!plugins_active_p ())
804 return;
806 fprintf (file, FMT_FOR_PLUGIN_EVENT " | %s\n", _("Event"), _("Plugins"));
807 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
808 if (plugin_callbacks[event])
810 struct callback_info *ci;
812 fprintf (file, FMT_FOR_PLUGIN_EVENT " |", plugin_event_name[event]);
814 for (ci = plugin_callbacks[event]; ci; ci = ci->next)
815 fprintf (file, " %s", ci->plugin_name);
817 putc ('\n', file);
822 /* Dump active plugins to stderr. */
824 DEBUG_FUNCTION void
825 debug_active_plugins (void)
827 dump_active_plugins (stderr);
830 /* Give a warning if plugins are present, before an ICE message asking
831 to submit a bug report. */
833 void
834 warn_if_plugins (void)
836 if (plugins_active_p ())
838 fnotice (stderr, "*** WARNING *** there are active plugins, do not report"
839 " this as a bug unless you can reproduce it without enabling"
840 " any plugins.\n");
841 dump_active_plugins (stderr);
846 /* Likewise, as a callback from the diagnostics code. */
848 void
849 plugins_internal_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
850 const char *msgid ATTRIBUTE_UNUSED,
851 va_list *ap ATTRIBUTE_UNUSED)
853 warn_if_plugins ();
856 /* The default version check. Compares every field in VERSION. */
858 bool
859 plugin_default_version_check (struct plugin_gcc_version *gcc_version,
860 struct plugin_gcc_version *plugin_version)
862 if (!gcc_version || !plugin_version)
863 return false;
865 if (strcmp (gcc_version->basever, plugin_version->basever))
866 return false;
867 if (strcmp (gcc_version->datestamp, plugin_version->datestamp))
868 return false;
869 if (strcmp (gcc_version->devphase, plugin_version->devphase))
870 return false;
871 if (strcmp (gcc_version->revision, plugin_version->revision))
872 return false;
873 if (strcmp (gcc_version->configuration_arguments,
874 plugin_version->configuration_arguments))
875 return false;
876 return true;
880 /* Return the current value of event_last, so that plugins which provide
881 additional functionality for events for the benefit of high-level plugins
882 know how many valid entries plugin_event_name holds. */
885 get_event_last (void)
887 return event_last;
891 /* Retrieve the default plugin directory. The gcc driver should have passed
892 it as -iplugindir <dir> to the cc1 program, and it is queriable through the
893 -print-file-name=plugin option to gcc. */
894 const char*
895 default_plugin_dir_name (void)
897 if (!plugindir_string)
898 fatal_error (input_location,
899 "-iplugindir <dir> option not passed from the gcc driver");
900 return plugindir_string;