PR target/6641
[official-gcc.git] / gcc / plugin.c
bloba770e6f766ca76fe3bdfd273548bd4a7d9a1c312
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 "vec.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "options.h"
34 #include "flags.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "tree-pass.h"
38 #include "intl.h"
39 #include "plugin.h"
40 #include "ggc.h"
42 #ifdef ENABLE_PLUGIN
43 #include "plugin-version.h"
44 #endif
46 #define GCC_PLUGIN_STRINGIFY0(X) #X
47 #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X)
49 /* Event names as strings. Keep in sync with enum plugin_event. */
50 static const char *plugin_event_name_init[] =
52 # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME),
53 # include "plugin.def"
54 # undef DEFEVENT
57 /* A printf format large enough for the largest event above. */
58 #define FMT_FOR_PLUGIN_EVENT "%-32s"
60 const char **plugin_event_name = plugin_event_name_init;
62 /* Event hashtable helpers. */
64 struct event_hasher : typed_noop_remove <const char *>
66 typedef const char **value_type;
67 typedef const char **compare_type;
68 static inline hashval_t hash (const char **);
69 static inline bool equal (const char **, const char **);
72 /* Helper function for the event hash table that hashes the entry V. */
74 inline hashval_t
75 event_hasher::hash (const char **v)
77 return htab_hash_string (*v);
80 /* Helper function for the event hash table that compares the name of an
81 existing entry (S1) with the given string (S2). */
83 inline bool
84 event_hasher::equal (const char **s1, const char **s2)
86 return !strcmp (*s1, *s2);
89 /* A hash table to map event names to the position of the names in the
90 plugin_event_name table. */
91 static hash_table<event_hasher> *event_tab;
93 /* Keep track of the limit of allocated events and space ready for
94 allocating events. */
95 static int event_last = PLUGIN_EVENT_FIRST_DYNAMIC;
96 static int event_horizon = PLUGIN_EVENT_FIRST_DYNAMIC;
98 /* Hash table for the plugin_name_args objects created during command-line
99 parsing. */
100 static htab_t plugin_name_args_tab = NULL;
102 /* List node for keeping track of plugin-registered callback. */
103 struct callback_info
105 const char *plugin_name; /* Name of plugin that registers the callback. */
106 plugin_callback_func func; /* Callback to be called. */
107 void *user_data; /* plugin-specified data. */
108 struct callback_info *next;
111 /* An array of lists of 'callback_info' objects indexed by the event id. */
112 static struct callback_info *plugin_callbacks_init[PLUGIN_EVENT_FIRST_DYNAMIC];
113 static struct callback_info **plugin_callbacks = plugin_callbacks_init;
115 /* For invoke_plugin_callbacks(), see plugin.h. */
116 bool flag_plugin_added = false;
118 #ifdef ENABLE_PLUGIN
119 /* Each plugin should define an initialization function with exactly
120 this name. */
121 static const char *str_plugin_init_func_name = "plugin_init";
123 /* Each plugin should define this symbol to assert that it is
124 distributed under a GPL-compatible license. */
125 static const char *str_license = "plugin_is_GPL_compatible";
126 #endif
128 /* Helper function for the hash table that compares the base_name of the
129 existing entry (S1) with the given string (S2). */
131 static int
132 htab_str_eq (const void *s1, const void *s2)
134 const struct plugin_name_args *plugin = (const struct plugin_name_args *) s1;
135 return !strcmp (plugin->base_name, (const char *) s2);
139 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
140 return NAME. */
142 static char *
143 get_plugin_base_name (const char *full_name)
145 /* First get the base name part of the full-path name, i.e. NAME.so. */
146 char *base_name = xstrdup (lbasename (full_name));
148 /* Then get rid of '.so' part of the name. */
149 strip_off_ending (base_name, strlen (base_name));
151 return base_name;
155 /* Create a plugin_name_args object for the given plugin and insert it
156 to the hash table. This function is called when
157 -fplugin=/path/to/NAME.so or -fplugin=NAME option is processed. */
159 void
160 add_new_plugin (const char* plugin_name)
162 struct plugin_name_args *plugin;
163 void **slot;
164 char *base_name;
165 bool name_is_short;
166 const char *pc;
168 flag_plugin_added = true;
170 /* Replace short names by their full path when relevant. */
171 name_is_short = !IS_ABSOLUTE_PATH (plugin_name);
172 for (pc = plugin_name; name_is_short && *pc; pc++)
173 if (*pc == '.' || IS_DIR_SEPARATOR (*pc))
174 name_is_short = false;
176 if (name_is_short)
178 base_name = CONST_CAST (char*, plugin_name);
179 /* FIXME: the ".so" suffix is currently builtin, since plugins
180 only work on ELF host systems like e.g. Linux or Solaris.
181 When plugins shall be available on non ELF systems such as
182 Windows or MacOS, this code has to be greatly improved. */
183 plugin_name = concat (default_plugin_dir_name (), "/",
184 plugin_name, ".so", NULL);
185 if (access (plugin_name, R_OK))
186 fatal_error
187 (input_location,
188 "inaccessible plugin file %s expanded from short plugin name %s: %m",
189 plugin_name, base_name);
191 else
192 base_name = get_plugin_base_name (plugin_name);
194 /* If this is the first -fplugin= option we encounter, create
195 'plugin_name_args_tab' hash table. */
196 if (!plugin_name_args_tab)
197 plugin_name_args_tab = htab_create (10, htab_hash_string, htab_str_eq,
198 NULL);
200 slot = htab_find_slot (plugin_name_args_tab, base_name, INSERT);
202 /* If the same plugin (name) has been specified earlier, either emit an
203 error or a warning message depending on if they have identical full
204 (path) names. */
205 if (*slot)
207 plugin = (struct plugin_name_args *) *slot;
208 if (strcmp (plugin->full_name, plugin_name))
209 error ("plugin %s was specified with different paths:\n%s\n%s",
210 plugin->base_name, plugin->full_name, plugin_name);
211 return;
214 plugin = XCNEW (struct plugin_name_args);
215 plugin->base_name = base_name;
216 plugin->full_name = plugin_name;
218 *slot = plugin;
222 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
223 'plugin_argument' object for the parsed key-value pair. ARG is
224 the <name>-<key>[=<value>] part of the option. */
226 void
227 parse_plugin_arg_opt (const char *arg)
229 size_t len = 0, name_len = 0, key_len = 0, value_len = 0;
230 const char *ptr, *name_start = arg, *key_start = NULL, *value_start = NULL;
231 char *name, *key, *value;
232 void **slot;
233 bool name_parsed = false, key_parsed = false;
235 /* Iterate over the ARG string and identify the starting character position
236 of 'name', 'key', and 'value' and their lengths. */
237 for (ptr = arg; *ptr; ++ptr)
239 /* Only the first '-' encountered is considered a separator between
240 'name' and 'key'. All the subsequent '-'s are considered part of
241 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
242 the plugin name is 'foo' and the key is 'bar-primary-key'. */
243 if (*ptr == '-' && !name_parsed)
245 name_len = len;
246 len = 0;
247 key_start = ptr + 1;
248 name_parsed = true;
249 continue;
251 else if (*ptr == '=')
253 if (!key_parsed)
255 key_len = len;
256 len = 0;
257 value_start = ptr + 1;
258 key_parsed = true;
260 continue;
262 else
263 ++len;
266 if (!key_start)
268 error ("malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
269 arg);
270 return;
273 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
274 Otherwise, it is the VALUE_LEN. */
275 if (!value_start)
276 key_len = len;
277 else
278 value_len = len;
280 name = XNEWVEC (char, name_len + 1);
281 strncpy (name, name_start, name_len);
282 name[name_len] = '\0';
284 /* Check if the named plugin has already been specified earlier in the
285 command-line. */
286 if (plugin_name_args_tab
287 && ((slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT))
288 != NULL))
290 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
292 key = XNEWVEC (char, key_len + 1);
293 strncpy (key, key_start, key_len);
294 key[key_len] = '\0';
295 if (value_start)
297 value = XNEWVEC (char, value_len + 1);
298 strncpy (value, value_start, value_len);
299 value[value_len] = '\0';
301 else
302 value = NULL;
304 /* Create a plugin_argument object for the parsed key-value pair.
305 If there are already arguments for this plugin, we will need to
306 adjust the argument array size by creating a new array and deleting
307 the old one. If the performance ever becomes an issue, we can
308 change the code by pre-allocating a larger array first. */
309 if (plugin->argc > 0)
311 struct plugin_argument *args = XNEWVEC (struct plugin_argument,
312 plugin->argc + 1);
313 memcpy (args, plugin->argv,
314 sizeof (struct plugin_argument) * plugin->argc);
315 XDELETEVEC (plugin->argv);
316 plugin->argv = args;
317 ++plugin->argc;
319 else
321 gcc_assert (plugin->argv == NULL);
322 plugin->argv = XNEWVEC (struct plugin_argument, 1);
323 plugin->argc = 1;
326 plugin->argv[plugin->argc - 1].key = key;
327 plugin->argv[plugin->argc - 1].value = value;
329 else
330 error ("plugin %s should be specified before -fplugin-arg-%s "
331 "in the command line", name, arg);
333 /* We don't need the plugin's name anymore. Just release it. */
334 XDELETEVEC (name);
337 /* Register additional plugin information. NAME is the name passed to
338 plugin_init. INFO is the information that should be registered. */
340 static void
341 register_plugin_info (const char* name, struct plugin_info *info)
343 void **slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT);
344 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
345 plugin->version = info->version;
346 plugin->help = info->help;
349 /* Look up the event id for NAME. If the name is not found, return -1
350 if INSERT is NO_INSERT. */
353 get_named_event_id (const char *name, enum insert_option insert)
355 const char ***slot;
357 if (!event_tab)
359 int i;
361 event_tab = new hash_table<event_hasher> (150);
362 for (i = 0; i < event_last; i++)
364 slot = event_tab->find_slot (&plugin_event_name[i], INSERT);
365 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
366 *slot = &plugin_event_name[i];
369 slot = event_tab->find_slot (&name, insert);
370 if (slot == NULL)
371 return -1;
372 if (*slot != HTAB_EMPTY_ENTRY)
373 return *slot - &plugin_event_name[0];
375 if (event_last >= event_horizon)
377 event_horizon = event_last * 2;
378 if (plugin_event_name == plugin_event_name_init)
380 plugin_event_name = XNEWVEC (const char *, event_horizon);
381 memcpy (plugin_event_name, plugin_event_name_init,
382 sizeof plugin_event_name_init);
383 plugin_callbacks = XNEWVEC (struct callback_info *, event_horizon);
384 memcpy (plugin_callbacks, plugin_callbacks_init,
385 sizeof plugin_callbacks_init);
387 else
389 plugin_event_name
390 = XRESIZEVEC (const char *, plugin_event_name, event_horizon);
391 plugin_callbacks = XRESIZEVEC (struct callback_info *,
392 plugin_callbacks, event_horizon);
394 /* All the pointers in the hash table will need to be updated. */
395 delete event_tab;
396 event_tab = NULL;
398 else
399 *slot = &plugin_event_name[event_last];
400 plugin_event_name[event_last] = name;
401 return event_last++;
404 /* Called from the plugin's initialization code. Register a single callback.
405 This function can be called multiple times.
407 PLUGIN_NAME - display name for this plugin
408 EVENT - which event the callback is for
409 CALLBACK - the callback to be called at the event
410 USER_DATA - plugin-provided data */
412 void
413 register_callback (const char *plugin_name,
414 int event,
415 plugin_callback_func callback,
416 void *user_data)
418 switch (event)
420 case PLUGIN_PASS_MANAGER_SETUP:
421 gcc_assert (!callback);
422 register_pass ((struct register_pass_info *) user_data);
423 break;
424 case PLUGIN_INFO:
425 gcc_assert (!callback);
426 register_plugin_info (plugin_name, (struct plugin_info *) user_data);
427 break;
428 case PLUGIN_REGISTER_GGC_ROOTS:
429 gcc_assert (!callback);
430 ggc_register_root_tab ((const struct ggc_root_tab*) user_data);
431 break;
432 case PLUGIN_EVENT_FIRST_DYNAMIC:
433 default:
434 if (event < PLUGIN_EVENT_FIRST_DYNAMIC || event >= event_last)
436 error ("unknown callback event registered by plugin %s",
437 plugin_name);
438 return;
440 /* Fall through. */
441 case PLUGIN_START_PARSE_FUNCTION:
442 case PLUGIN_FINISH_PARSE_FUNCTION:
443 case PLUGIN_FINISH_TYPE:
444 case PLUGIN_FINISH_DECL:
445 case PLUGIN_START_UNIT:
446 case PLUGIN_FINISH_UNIT:
447 case PLUGIN_PRE_GENERICIZE:
448 case PLUGIN_GGC_START:
449 case PLUGIN_GGC_MARKING:
450 case PLUGIN_GGC_END:
451 case PLUGIN_ATTRIBUTES:
452 case PLUGIN_PRAGMAS:
453 case PLUGIN_FINISH:
454 case PLUGIN_ALL_PASSES_START:
455 case PLUGIN_ALL_PASSES_END:
456 case PLUGIN_ALL_IPA_PASSES_START:
457 case PLUGIN_ALL_IPA_PASSES_END:
458 case PLUGIN_OVERRIDE_GATE:
459 case PLUGIN_PASS_EXECUTION:
460 case PLUGIN_EARLY_GIMPLE_PASSES_START:
461 case PLUGIN_EARLY_GIMPLE_PASSES_END:
462 case PLUGIN_NEW_PASS:
463 case PLUGIN_INCLUDE_FILE:
465 struct callback_info *new_callback;
466 if (!callback)
468 error ("plugin %s registered a null callback function "
469 "for event %s", plugin_name, plugin_event_name[event]);
470 return;
472 new_callback = XNEW (struct callback_info);
473 new_callback->plugin_name = plugin_name;
474 new_callback->func = callback;
475 new_callback->user_data = user_data;
476 new_callback->next = plugin_callbacks[event];
477 plugin_callbacks[event] = new_callback;
479 break;
483 /* Remove a callback for EVENT which has been registered with for a plugin
484 PLUGIN_NAME. Return PLUGEVT_SUCCESS if a matching callback was
485 found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching
486 callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid. */
488 unregister_callback (const char *plugin_name, int event)
490 struct callback_info *callback, **cbp;
492 if (event >= event_last)
493 return PLUGEVT_NO_SUCH_EVENT;
495 for (cbp = &plugin_callbacks[event]; (callback = *cbp); cbp = &callback->next)
496 if (strcmp (callback->plugin_name, plugin_name) == 0)
498 *cbp = callback->next;
499 return PLUGEVT_SUCCESS;
501 return PLUGEVT_NO_CALLBACK;
504 /* Invoke all plugin callbacks registered with the specified event,
505 called from invoke_plugin_callbacks(). */
508 invoke_plugin_callbacks_full (int event, void *gcc_data)
510 int retval = PLUGEVT_SUCCESS;
512 timevar_push (TV_PLUGIN_RUN);
514 switch (event)
516 case PLUGIN_EVENT_FIRST_DYNAMIC:
517 default:
518 gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
519 gcc_assert (event < event_last);
520 /* Fall through. */
521 case PLUGIN_START_PARSE_FUNCTION:
522 case PLUGIN_FINISH_PARSE_FUNCTION:
523 case PLUGIN_FINISH_TYPE:
524 case PLUGIN_FINISH_DECL:
525 case PLUGIN_START_UNIT:
526 case PLUGIN_FINISH_UNIT:
527 case PLUGIN_PRE_GENERICIZE:
528 case PLUGIN_ATTRIBUTES:
529 case PLUGIN_PRAGMAS:
530 case PLUGIN_FINISH:
531 case PLUGIN_GGC_START:
532 case PLUGIN_GGC_MARKING:
533 case PLUGIN_GGC_END:
534 case PLUGIN_ALL_PASSES_START:
535 case PLUGIN_ALL_PASSES_END:
536 case PLUGIN_ALL_IPA_PASSES_START:
537 case PLUGIN_ALL_IPA_PASSES_END:
538 case PLUGIN_OVERRIDE_GATE:
539 case PLUGIN_PASS_EXECUTION:
540 case PLUGIN_EARLY_GIMPLE_PASSES_START:
541 case PLUGIN_EARLY_GIMPLE_PASSES_END:
542 case PLUGIN_NEW_PASS:
543 case PLUGIN_INCLUDE_FILE:
545 /* Iterate over every callback registered with this event and
546 call it. */
547 struct callback_info *callback = plugin_callbacks[event];
549 if (!callback)
550 retval = PLUGEVT_NO_CALLBACK;
551 for ( ; callback; callback = callback->next)
552 (*callback->func) (gcc_data, callback->user_data);
554 break;
556 case PLUGIN_PASS_MANAGER_SETUP:
557 case PLUGIN_REGISTER_GGC_ROOTS:
558 gcc_assert (false);
561 timevar_pop (TV_PLUGIN_RUN);
562 return retval;
565 #ifdef ENABLE_PLUGIN
566 /* We need a union to cast dlsym return value to a function pointer
567 as ISO C forbids assignment between function pointer and 'void *'.
568 Use explicit union instead of __extension__(<union_cast>) for
569 portability. */
570 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
571 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
572 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
574 /* Try to initialize PLUGIN. Return true if successful. */
576 static bool
577 try_init_one_plugin (struct plugin_name_args *plugin)
579 void *dl_handle;
580 plugin_init_func plugin_init;
581 const char *err;
582 PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
584 /* We use RTLD_NOW to accelerate binding and detect any mismatch
585 between the API expected by the plugin and the GCC API; we use
586 RTLD_GLOBAL which is useful to plugins which themselves call
587 dlopen. */
588 dl_handle = dlopen (plugin->full_name, RTLD_NOW | RTLD_GLOBAL);
589 if (!dl_handle)
591 error ("cannot load plugin %s\n%s", plugin->full_name, dlerror ());
592 return false;
595 /* Clear any existing error. */
596 dlerror ();
598 /* Check the plugin license. */
599 if (dlsym (dl_handle, str_license) == NULL)
600 fatal_error (input_location,
601 "plugin %s is not licensed under a GPL-compatible license\n"
602 "%s", plugin->full_name, dlerror ());
604 PTR_UNION_AS_VOID_PTR (plugin_init_union) =
605 dlsym (dl_handle, str_plugin_init_func_name);
606 plugin_init = PTR_UNION_AS_CAST_PTR (plugin_init_union);
608 if ((err = dlerror ()) != NULL)
610 error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
611 plugin->full_name, err);
612 return false;
615 /* Call the plugin-provided initialization routine with the arguments. */
616 if ((*plugin_init) (plugin, &gcc_version))
618 error ("fail to initialize plugin %s", plugin->full_name);
619 return false;
622 return true;
626 /* Routine to dlopen and initialize one plugin. This function is passed to
627 (and called by) the hash table traverse routine. Return 1 for the
628 htab_traverse to continue scan, 0 to stop.
630 SLOT - slot of the hash table element
631 INFO - auxiliary pointer handed to hash table traverse routine
632 (unused in this function) */
634 static int
635 init_one_plugin (void **slot, void * ARG_UNUSED (info))
637 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
638 bool ok = try_init_one_plugin (plugin);
639 if (!ok)
641 htab_remove_elt (plugin_name_args_tab, plugin->base_name);
642 XDELETE (plugin);
644 return 1;
647 #endif /* ENABLE_PLUGIN */
649 /* Main plugin initialization function. Called from compile_file() in
650 toplev.c. */
652 void
653 initialize_plugins (void)
655 /* If no plugin was specified in the command-line, simply return. */
656 if (!plugin_name_args_tab)
657 return;
659 timevar_push (TV_PLUGIN_INIT);
661 #ifdef ENABLE_PLUGIN
662 /* Traverse and initialize each plugin specified in the command-line. */
663 htab_traverse_noresize (plugin_name_args_tab, init_one_plugin, NULL);
664 #endif
666 timevar_pop (TV_PLUGIN_INIT);
669 /* Release memory used by one plugin. */
671 static int
672 finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
674 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
675 XDELETE (plugin);
676 return 1;
679 /* Free memory allocated by the plugin system. */
681 void
682 finalize_plugins (void)
684 if (!plugin_name_args_tab)
685 return;
687 /* We can now delete the plugin_name_args object as it will no longer
688 be used. Note that base_name and argv fields (both of which were also
689 dynamically allocated) are not freed as they could still be used by
690 the plugin code. */
692 htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
694 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
695 htab_delete (plugin_name_args_tab);
696 plugin_name_args_tab = NULL;
699 /* Used to pass options to htab_traverse callbacks. */
701 struct print_options
703 FILE *file;
704 const char *indent;
707 /* Print the version of one plugin. */
709 static int
710 print_version_one_plugin (void **slot, void *data)
712 struct print_options *opt = (struct print_options *) data;
713 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
714 const char *version = plugin->version ? plugin->version : "Unknown version.";
716 fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
717 return 1;
720 /* Print the version of each plugin. */
722 void
723 print_plugins_versions (FILE *file, const char *indent)
725 struct print_options opt;
726 opt.file = file;
727 opt.indent = indent;
728 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
729 return;
731 fprintf (file, "%sVersions of loaded plugins:\n", indent);
732 htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
735 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
736 argument to htab_traverse_noresize. */
738 static int
739 print_help_one_plugin (void **slot, void *data)
741 struct print_options *opt = (struct print_options *) data;
742 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
743 const char *help = plugin->help ? plugin->help : "No help available .";
745 char *dup = xstrdup (help);
746 char *p, *nl;
747 fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
749 for (p = nl = dup; nl; p = nl)
751 nl = strchr (nl, '\n');
752 if (nl)
754 *nl = '\0';
755 nl++;
757 fprintf (opt->file, " %s %s\n", opt->indent, p);
760 free (dup);
761 return 1;
764 /* Print help for each plugin. The output goes to FILE and every line starts
765 with INDENT. */
767 void
768 print_plugins_help (FILE *file, const char *indent)
770 struct print_options opt;
771 opt.file = file;
772 opt.indent = indent;
773 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
774 return;
776 fprintf (file, "%sHelp for the loaded plugins:\n", indent);
777 htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
781 /* Return true if plugins have been loaded. */
783 bool
784 plugins_active_p (void)
786 int event;
788 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
789 if (plugin_callbacks[event])
790 return true;
792 return false;
796 /* Dump to FILE the names and associated events for all the active
797 plugins. */
799 DEBUG_FUNCTION void
800 dump_active_plugins (FILE *file)
802 int event;
804 if (!plugins_active_p ())
805 return;
807 fprintf (file, FMT_FOR_PLUGIN_EVENT " | %s\n", _("Event"), _("Plugins"));
808 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
809 if (plugin_callbacks[event])
811 struct callback_info *ci;
813 fprintf (file, FMT_FOR_PLUGIN_EVENT " |", plugin_event_name[event]);
815 for (ci = plugin_callbacks[event]; ci; ci = ci->next)
816 fprintf (file, " %s", ci->plugin_name);
818 putc ('\n', file);
823 /* Dump active plugins to stderr. */
825 DEBUG_FUNCTION void
826 debug_active_plugins (void)
828 dump_active_plugins (stderr);
831 /* Give a warning if plugins are present, before an ICE message asking
832 to submit a bug report. */
834 void
835 warn_if_plugins (void)
837 if (plugins_active_p ())
839 fnotice (stderr, "*** WARNING *** there are active plugins, do not report"
840 " this as a bug unless you can reproduce it without enabling"
841 " any plugins.\n");
842 dump_active_plugins (stderr);
847 /* Likewise, as a callback from the diagnostics code. */
849 void
850 plugins_internal_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
851 const char *msgid ATTRIBUTE_UNUSED,
852 va_list *ap ATTRIBUTE_UNUSED)
854 warn_if_plugins ();
857 /* The default version check. Compares every field in VERSION. */
859 bool
860 plugin_default_version_check (struct plugin_gcc_version *gcc_version,
861 struct plugin_gcc_version *plugin_version)
863 if (!gcc_version || !plugin_version)
864 return false;
866 if (strcmp (gcc_version->basever, plugin_version->basever))
867 return false;
868 if (strcmp (gcc_version->datestamp, plugin_version->datestamp))
869 return false;
870 if (strcmp (gcc_version->devphase, plugin_version->devphase))
871 return false;
872 if (strcmp (gcc_version->revision, plugin_version->revision))
873 return false;
874 if (strcmp (gcc_version->configuration_arguments,
875 plugin_version->configuration_arguments))
876 return false;
877 return true;
881 /* Return the current value of event_last, so that plugins which provide
882 additional functionality for events for the benefit of high-level plugins
883 know how many valid entries plugin_event_name holds. */
886 get_event_last (void)
888 return event_last;
892 /* Retrieve the default plugin directory. The gcc driver should have passed
893 it as -iplugindir <dir> to the cc1 program, and it is queriable through the
894 -print-file-name=plugin option to gcc. */
895 const char*
896 default_plugin_dir_name (void)
898 if (!plugindir_string)
899 fatal_error (input_location,
900 "-iplugindir <dir> option not passed from the gcc driver");
901 return plugindir_string;