Daily bump.
[official-gcc.git] / gcc / plugin.c
blobcfd6ef25036915e7b080cb8d4a50c70060c13d75
1 /* Support for GCC plugin mechanism.
2 Copyright (C) 2009-2017 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 "options.h"
27 #include "tree-pass.h"
28 #include "diagnostic-core.h"
29 #include "flags.h"
30 #include "intl.h"
31 #include "plugin.h"
33 #ifdef ENABLE_PLUGIN
34 #include "plugin-version.h"
35 #endif
37 #define GCC_PLUGIN_STRINGIFY0(X) #X
38 #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X)
40 /* Event names as strings. Keep in sync with enum plugin_event. */
41 static const char *plugin_event_name_init[] =
43 # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME),
44 # include "plugin.def"
45 # undef DEFEVENT
48 /* A printf format large enough for the largest event above. */
49 #define FMT_FOR_PLUGIN_EVENT "%-32s"
51 const char **plugin_event_name = plugin_event_name_init;
53 /* Event hashtable helpers. */
55 struct event_hasher : nofree_ptr_hash <const char *>
57 static inline hashval_t hash (const char **);
58 static inline bool equal (const char **, const char **);
61 /* Helper function for the event hash table that hashes the entry V. */
63 inline hashval_t
64 event_hasher::hash (const char **v)
66 return htab_hash_string (*v);
69 /* Helper function for the event hash table that compares the name of an
70 existing entry (S1) with the given string (S2). */
72 inline bool
73 event_hasher::equal (const char **s1, const char **s2)
75 return !strcmp (*s1, *s2);
78 /* A hash table to map event names to the position of the names in the
79 plugin_event_name table. */
80 static hash_table<event_hasher> *event_tab;
82 /* Keep track of the limit of allocated events and space ready for
83 allocating events. */
84 static int event_last = PLUGIN_EVENT_FIRST_DYNAMIC;
85 static int event_horizon = PLUGIN_EVENT_FIRST_DYNAMIC;
87 /* Hash table for the plugin_name_args objects created during command-line
88 parsing. */
89 static htab_t plugin_name_args_tab = NULL;
91 /* List node for keeping track of plugin-registered callback. */
92 struct callback_info
94 const char *plugin_name; /* Name of plugin that registers the callback. */
95 plugin_callback_func func; /* Callback to be called. */
96 void *user_data; /* plugin-specified data. */
97 struct callback_info *next;
100 /* An array of lists of 'callback_info' objects indexed by the event id. */
101 static struct callback_info *plugin_callbacks_init[PLUGIN_EVENT_FIRST_DYNAMIC];
102 static struct callback_info **plugin_callbacks = plugin_callbacks_init;
104 /* For invoke_plugin_callbacks(), see plugin.h. */
105 bool flag_plugin_added = false;
107 #ifdef ENABLE_PLUGIN
108 /* Each plugin should define an initialization function with exactly
109 this name. */
110 static const char *str_plugin_init_func_name = "plugin_init";
112 /* Each plugin should define this symbol to assert that it is
113 distributed under a GPL-compatible license. */
114 static const char *str_license = "plugin_is_GPL_compatible";
115 #endif
117 /* Helper function for hashing the base_name of the plugin_name_args
118 structure to be inserted into the hash table. */
120 static hashval_t
121 htab_hash_plugin (const PTR p)
123 const struct plugin_name_args *plugin = (const struct plugin_name_args *) p;
124 return htab_hash_string (plugin->base_name);
127 /* Helper function for the hash table that compares the base_name of the
128 existing entry (S1) with the given string (S2). */
130 static int
131 htab_str_eq (const void *s1, const void *s2)
133 const struct plugin_name_args *plugin = (const struct plugin_name_args *) s1;
134 return !strcmp (plugin->base_name, (const char *) s2);
138 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
139 return NAME. */
141 static char *
142 get_plugin_base_name (const char *full_name)
144 /* First get the base name part of the full-path name, i.e. NAME.so. */
145 char *base_name = xstrdup (lbasename (full_name));
147 /* Then get rid of '.so' part of the name. */
148 strip_off_ending (base_name, strlen (base_name));
150 return base_name;
154 /* Create a plugin_name_args object for the given plugin and insert it
155 to the hash table. This function is called when
156 -fplugin=/path/to/NAME.so or -fplugin=NAME option is processed. */
158 void
159 add_new_plugin (const char* plugin_name)
161 struct plugin_name_args *plugin;
162 void **slot;
163 char *base_name;
164 bool name_is_short;
165 const char *pc;
167 flag_plugin_added = true;
169 /* Replace short names by their full path when relevant. */
170 name_is_short = !IS_ABSOLUTE_PATH (plugin_name);
171 for (pc = plugin_name; name_is_short && *pc; pc++)
172 if (*pc == '.' || IS_DIR_SEPARATOR (*pc))
173 name_is_short = false;
175 if (name_is_short)
177 base_name = CONST_CAST (char*, plugin_name);
178 /* FIXME: the ".so" suffix is currently builtin, since plugins
179 only work on ELF host systems like e.g. Linux or Solaris.
180 When plugins shall be available on non ELF systems such as
181 Windows or MacOS, this code has to be greatly improved. */
182 plugin_name = concat (default_plugin_dir_name (), "/",
183 plugin_name, ".so", NULL);
184 if (access (plugin_name, R_OK))
185 fatal_error
186 (input_location,
187 "inaccessible plugin file %s expanded from short plugin name %s: %m",
188 plugin_name, base_name);
190 else
191 base_name = get_plugin_base_name (plugin_name);
193 /* If this is the first -fplugin= option we encounter, create
194 'plugin_name_args_tab' hash table. */
195 if (!plugin_name_args_tab)
196 plugin_name_args_tab = htab_create (10, htab_hash_plugin, htab_str_eq,
197 NULL);
199 slot = htab_find_slot_with_hash (plugin_name_args_tab, base_name,
200 htab_hash_string (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_with_hash (plugin_name_args_tab, name,
288 htab_hash_string (name), NO_INSERT))
289 != NULL))
291 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
293 key = XNEWVEC (char, key_len + 1);
294 strncpy (key, key_start, key_len);
295 key[key_len] = '\0';
296 if (value_start)
298 value = XNEWVEC (char, value_len + 1);
299 strncpy (value, value_start, value_len);
300 value[value_len] = '\0';
302 else
303 value = NULL;
305 /* Create a plugin_argument object for the parsed key-value pair.
306 If there are already arguments for this plugin, we will need to
307 adjust the argument array size by creating a new array and deleting
308 the old one. If the performance ever becomes an issue, we can
309 change the code by pre-allocating a larger array first. */
310 if (plugin->argc > 0)
312 struct plugin_argument *args = XNEWVEC (struct plugin_argument,
313 plugin->argc + 1);
314 memcpy (args, plugin->argv,
315 sizeof (struct plugin_argument) * plugin->argc);
316 XDELETEVEC (plugin->argv);
317 plugin->argv = args;
318 ++plugin->argc;
320 else
322 gcc_assert (plugin->argv == NULL);
323 plugin->argv = XNEWVEC (struct plugin_argument, 1);
324 plugin->argc = 1;
327 plugin->argv[plugin->argc - 1].key = key;
328 plugin->argv[plugin->argc - 1].value = value;
330 else
331 error ("plugin %s should be specified before -fplugin-arg-%s "
332 "in the command line", name, arg);
334 /* We don't need the plugin's name anymore. Just release it. */
335 XDELETEVEC (name);
338 /* Register additional plugin information. NAME is the name passed to
339 plugin_init. INFO is the information that should be registered. */
341 static void
342 register_plugin_info (const char* name, struct plugin_info *info)
344 void **slot = htab_find_slot_with_hash (plugin_name_args_tab, name,
345 htab_hash_string (name), NO_INSERT);
346 struct plugin_name_args *plugin;
348 if (slot == NULL)
350 error ("unable to register info for plugin %qs - plugin name not found",
351 name);
352 return;
354 plugin = (struct plugin_name_args *) *slot;
355 plugin->version = info->version;
356 plugin->help = info->help;
359 /* Look up the event id for NAME. If the name is not found, return -1
360 if INSERT is NO_INSERT. */
363 get_named_event_id (const char *name, enum insert_option insert)
365 const char ***slot;
367 if (!event_tab)
369 int i;
371 event_tab = new hash_table<event_hasher> (150);
372 for (i = 0; i < event_last; i++)
374 slot = event_tab->find_slot (&plugin_event_name[i], INSERT);
375 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
376 *slot = &plugin_event_name[i];
379 slot = event_tab->find_slot (&name, insert);
380 if (slot == NULL)
381 return -1;
382 if (*slot != HTAB_EMPTY_ENTRY)
383 return *slot - &plugin_event_name[0];
385 if (event_last >= event_horizon)
387 event_horizon = event_last * 2;
388 if (plugin_event_name == plugin_event_name_init)
390 plugin_event_name = XNEWVEC (const char *, event_horizon);
391 memcpy (plugin_event_name, plugin_event_name_init,
392 sizeof plugin_event_name_init);
393 plugin_callbacks = XNEWVEC (struct callback_info *, event_horizon);
394 memcpy (plugin_callbacks, plugin_callbacks_init,
395 sizeof plugin_callbacks_init);
397 else
399 plugin_event_name
400 = XRESIZEVEC (const char *, plugin_event_name, event_horizon);
401 plugin_callbacks = XRESIZEVEC (struct callback_info *,
402 plugin_callbacks, event_horizon);
404 /* All the pointers in the hash table will need to be updated. */
405 delete event_tab;
406 event_tab = NULL;
408 else
409 *slot = &plugin_event_name[event_last];
410 plugin_event_name[event_last] = name;
411 return event_last++;
414 /* Called from the plugin's initialization code. Register a single callback.
415 This function can be called multiple times.
417 PLUGIN_NAME - display name for this plugin
418 EVENT - which event the callback is for
419 CALLBACK - the callback to be called at the event
420 USER_DATA - plugin-provided data */
422 void
423 register_callback (const char *plugin_name,
424 int event,
425 plugin_callback_func callback,
426 void *user_data)
428 switch (event)
430 case PLUGIN_PASS_MANAGER_SETUP:
431 gcc_assert (!callback);
432 register_pass ((struct register_pass_info *) user_data);
433 break;
434 case PLUGIN_INFO:
435 gcc_assert (!callback);
436 register_plugin_info (plugin_name, (struct plugin_info *) user_data);
437 break;
438 case PLUGIN_REGISTER_GGC_ROOTS:
439 gcc_assert (!callback);
440 ggc_register_root_tab ((const struct ggc_root_tab*) user_data);
441 break;
442 case PLUGIN_EVENT_FIRST_DYNAMIC:
443 default:
444 if (event < PLUGIN_EVENT_FIRST_DYNAMIC || event >= event_last)
446 error ("unknown callback event registered by plugin %s",
447 plugin_name);
448 return;
450 /* Fall through. */
451 case PLUGIN_START_PARSE_FUNCTION:
452 case PLUGIN_FINISH_PARSE_FUNCTION:
453 case PLUGIN_FINISH_TYPE:
454 case PLUGIN_FINISH_DECL:
455 case PLUGIN_START_UNIT:
456 case PLUGIN_FINISH_UNIT:
457 case PLUGIN_PRE_GENERICIZE:
458 case PLUGIN_GGC_START:
459 case PLUGIN_GGC_MARKING:
460 case PLUGIN_GGC_END:
461 case PLUGIN_ATTRIBUTES:
462 case PLUGIN_PRAGMAS:
463 case PLUGIN_FINISH:
464 case PLUGIN_ALL_PASSES_START:
465 case PLUGIN_ALL_PASSES_END:
466 case PLUGIN_ALL_IPA_PASSES_START:
467 case PLUGIN_ALL_IPA_PASSES_END:
468 case PLUGIN_OVERRIDE_GATE:
469 case PLUGIN_PASS_EXECUTION:
470 case PLUGIN_EARLY_GIMPLE_PASSES_START:
471 case PLUGIN_EARLY_GIMPLE_PASSES_END:
472 case PLUGIN_NEW_PASS:
473 case PLUGIN_INCLUDE_FILE:
475 struct callback_info *new_callback;
476 if (!callback)
478 error ("plugin %s registered a null callback function "
479 "for event %s", plugin_name, plugin_event_name[event]);
480 return;
482 new_callback = XNEW (struct callback_info);
483 new_callback->plugin_name = plugin_name;
484 new_callback->func = callback;
485 new_callback->user_data = user_data;
486 new_callback->next = plugin_callbacks[event];
487 plugin_callbacks[event] = new_callback;
489 break;
493 /* Remove a callback for EVENT which has been registered with for a plugin
494 PLUGIN_NAME. Return PLUGEVT_SUCCESS if a matching callback was
495 found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching
496 callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid. */
498 unregister_callback (const char *plugin_name, int event)
500 struct callback_info *callback, **cbp;
502 if (event >= event_last)
503 return PLUGEVT_NO_SUCH_EVENT;
505 for (cbp = &plugin_callbacks[event]; (callback = *cbp); cbp = &callback->next)
506 if (strcmp (callback->plugin_name, plugin_name) == 0)
508 *cbp = callback->next;
509 return PLUGEVT_SUCCESS;
511 return PLUGEVT_NO_CALLBACK;
514 /* Invoke all plugin callbacks registered with the specified event,
515 called from invoke_plugin_callbacks(). */
518 invoke_plugin_callbacks_full (int event, void *gcc_data)
520 int retval = PLUGEVT_SUCCESS;
522 timevar_push (TV_PLUGIN_RUN);
524 switch (event)
526 case PLUGIN_EVENT_FIRST_DYNAMIC:
527 default:
528 gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
529 gcc_assert (event < event_last);
530 /* Fall through. */
531 case PLUGIN_START_PARSE_FUNCTION:
532 case PLUGIN_FINISH_PARSE_FUNCTION:
533 case PLUGIN_FINISH_TYPE:
534 case PLUGIN_FINISH_DECL:
535 case PLUGIN_START_UNIT:
536 case PLUGIN_FINISH_UNIT:
537 case PLUGIN_PRE_GENERICIZE:
538 case PLUGIN_ATTRIBUTES:
539 case PLUGIN_PRAGMAS:
540 case PLUGIN_FINISH:
541 case PLUGIN_GGC_START:
542 case PLUGIN_GGC_MARKING:
543 case PLUGIN_GGC_END:
544 case PLUGIN_ALL_PASSES_START:
545 case PLUGIN_ALL_PASSES_END:
546 case PLUGIN_ALL_IPA_PASSES_START:
547 case PLUGIN_ALL_IPA_PASSES_END:
548 case PLUGIN_OVERRIDE_GATE:
549 case PLUGIN_PASS_EXECUTION:
550 case PLUGIN_EARLY_GIMPLE_PASSES_START:
551 case PLUGIN_EARLY_GIMPLE_PASSES_END:
552 case PLUGIN_NEW_PASS:
553 case PLUGIN_INCLUDE_FILE:
555 /* Iterate over every callback registered with this event and
556 call it. */
557 struct callback_info *callback = plugin_callbacks[event];
559 if (!callback)
560 retval = PLUGEVT_NO_CALLBACK;
561 for ( ; callback; callback = callback->next)
562 (*callback->func) (gcc_data, callback->user_data);
564 break;
566 case PLUGIN_PASS_MANAGER_SETUP:
567 case PLUGIN_REGISTER_GGC_ROOTS:
568 gcc_assert (false);
571 timevar_pop (TV_PLUGIN_RUN);
572 return retval;
575 #ifdef ENABLE_PLUGIN
576 /* We need a union to cast dlsym return value to a function pointer
577 as ISO C forbids assignment between function pointer and 'void *'.
578 Use explicit union instead of __extension__(<union_cast>) for
579 portability. */
580 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
581 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
582 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
584 /* Try to initialize PLUGIN. Return true if successful. */
586 static bool
587 try_init_one_plugin (struct plugin_name_args *plugin)
589 void *dl_handle;
590 plugin_init_func plugin_init;
591 const char *err;
592 PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
594 /* We use RTLD_NOW to accelerate binding and detect any mismatch
595 between the API expected by the plugin and the GCC API; we use
596 RTLD_GLOBAL which is useful to plugins which themselves call
597 dlopen. */
598 dl_handle = dlopen (plugin->full_name, RTLD_NOW | RTLD_GLOBAL);
599 if (!dl_handle)
601 error ("cannot load plugin %s\n%s", plugin->full_name, dlerror ());
602 return false;
605 /* Clear any existing error. */
606 dlerror ();
608 /* Check the plugin license. */
609 if (dlsym (dl_handle, str_license) == NULL)
610 fatal_error (input_location,
611 "plugin %s is not licensed under a GPL-compatible license\n"
612 "%s", plugin->full_name, dlerror ());
614 PTR_UNION_AS_VOID_PTR (plugin_init_union) =
615 dlsym (dl_handle, str_plugin_init_func_name);
616 plugin_init = PTR_UNION_AS_CAST_PTR (plugin_init_union);
618 if ((err = dlerror ()) != NULL)
620 error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
621 plugin->full_name, err);
622 return false;
625 /* Call the plugin-provided initialization routine with the arguments. */
626 if ((*plugin_init) (plugin, &gcc_version))
628 error ("fail to initialize plugin %s", plugin->full_name);
629 return false;
632 return true;
636 /* Routine to dlopen and initialize one plugin. This function is passed to
637 (and called by) the hash table traverse routine. Return 1 for the
638 htab_traverse to continue scan, 0 to stop.
640 SLOT - slot of the hash table element
641 INFO - auxiliary pointer handed to hash table traverse routine
642 (unused in this function) */
644 static int
645 init_one_plugin (void **slot, void * ARG_UNUSED (info))
647 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
648 bool ok = try_init_one_plugin (plugin);
649 if (!ok)
651 htab_remove_elt_with_hash (plugin_name_args_tab, plugin->base_name,
652 htab_hash_string (plugin->base_name));
653 XDELETE (plugin);
655 return 1;
658 #endif /* ENABLE_PLUGIN */
660 /* Main plugin initialization function. Called from compile_file() in
661 toplev.c. */
663 void
664 initialize_plugins (void)
666 /* If no plugin was specified in the command-line, simply return. */
667 if (!plugin_name_args_tab)
668 return;
670 timevar_push (TV_PLUGIN_INIT);
672 #ifdef ENABLE_PLUGIN
673 /* Traverse and initialize each plugin specified in the command-line. */
674 htab_traverse_noresize (plugin_name_args_tab, init_one_plugin, NULL);
675 #endif
677 timevar_pop (TV_PLUGIN_INIT);
680 /* Release memory used by one plugin. */
682 static int
683 finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
685 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
686 XDELETE (plugin);
687 return 1;
690 /* Free memory allocated by the plugin system. */
692 void
693 finalize_plugins (void)
695 if (!plugin_name_args_tab)
696 return;
698 /* We can now delete the plugin_name_args object as it will no longer
699 be used. Note that base_name and argv fields (both of which were also
700 dynamically allocated) are not freed as they could still be used by
701 the plugin code. */
703 htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
705 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
706 htab_delete (plugin_name_args_tab);
707 plugin_name_args_tab = NULL;
710 /* Used to pass options to htab_traverse callbacks. */
712 struct print_options
714 FILE *file;
715 const char *indent;
718 /* Print the version of one plugin. */
720 static int
721 print_version_one_plugin (void **slot, void *data)
723 struct print_options *opt = (struct print_options *) data;
724 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
725 const char *version = plugin->version ? plugin->version : "Unknown version.";
727 fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
728 return 1;
731 /* Print the version of each plugin. */
733 void
734 print_plugins_versions (FILE *file, const char *indent)
736 struct print_options opt;
737 opt.file = file;
738 opt.indent = indent;
739 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
740 return;
742 fprintf (file, "%sVersions of loaded plugins:\n", indent);
743 htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
746 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
747 argument to htab_traverse_noresize. */
749 static int
750 print_help_one_plugin (void **slot, void *data)
752 struct print_options *opt = (struct print_options *) data;
753 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
754 const char *help = plugin->help ? plugin->help : "No help available .";
756 char *dup = xstrdup (help);
757 char *p, *nl;
758 fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
760 for (p = nl = dup; nl; p = nl)
762 nl = strchr (nl, '\n');
763 if (nl)
765 *nl = '\0';
766 nl++;
768 fprintf (opt->file, " %s %s\n", opt->indent, p);
771 free (dup);
772 return 1;
775 /* Print help for each plugin. The output goes to FILE and every line starts
776 with INDENT. */
778 void
779 print_plugins_help (FILE *file, const char *indent)
781 struct print_options opt;
782 opt.file = file;
783 opt.indent = indent;
784 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
785 return;
787 fprintf (file, "%sHelp for the loaded plugins:\n", indent);
788 htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
792 /* Return true if plugins have been loaded. */
794 bool
795 plugins_active_p (void)
797 int event;
799 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
800 if (plugin_callbacks[event])
801 return true;
803 return false;
807 /* Dump to FILE the names and associated events for all the active
808 plugins. */
810 DEBUG_FUNCTION void
811 dump_active_plugins (FILE *file)
813 int event;
815 if (!plugins_active_p ())
816 return;
818 fprintf (file, FMT_FOR_PLUGIN_EVENT " | %s\n", _("Event"), _("Plugins"));
819 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
820 if (plugin_callbacks[event])
822 struct callback_info *ci;
824 fprintf (file, FMT_FOR_PLUGIN_EVENT " |", plugin_event_name[event]);
826 for (ci = plugin_callbacks[event]; ci; ci = ci->next)
827 fprintf (file, " %s", ci->plugin_name);
829 putc ('\n', file);
834 /* Dump active plugins to stderr. */
836 DEBUG_FUNCTION void
837 debug_active_plugins (void)
839 dump_active_plugins (stderr);
842 /* Give a warning if plugins are present, before an ICE message asking
843 to submit a bug report. */
845 void
846 warn_if_plugins (void)
848 if (plugins_active_p ())
850 fnotice (stderr, "*** WARNING *** there are active plugins, do not report"
851 " this as a bug unless you can reproduce it without enabling"
852 " any plugins.\n");
853 dump_active_plugins (stderr);
858 /* Likewise, as a callback from the diagnostics code. */
860 void
861 plugins_internal_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
862 const char *msgid ATTRIBUTE_UNUSED,
863 va_list *ap ATTRIBUTE_UNUSED)
865 warn_if_plugins ();
868 /* The default version check. Compares every field in VERSION. */
870 bool
871 plugin_default_version_check (struct plugin_gcc_version *gcc_version,
872 struct plugin_gcc_version *plugin_version)
874 if (!gcc_version || !plugin_version)
875 return false;
877 if (strcmp (gcc_version->basever, plugin_version->basever))
878 return false;
879 if (strcmp (gcc_version->datestamp, plugin_version->datestamp))
880 return false;
881 if (strcmp (gcc_version->devphase, plugin_version->devphase))
882 return false;
883 if (strcmp (gcc_version->revision, plugin_version->revision))
884 return false;
885 if (strcmp (gcc_version->configuration_arguments,
886 plugin_version->configuration_arguments))
887 return false;
888 return true;
892 /* Return the current value of event_last, so that plugins which provide
893 additional functionality for events for the benefit of high-level plugins
894 know how many valid entries plugin_event_name holds. */
897 get_event_last (void)
899 return event_last;
903 /* Retrieve the default plugin directory. The gcc driver should have passed
904 it as -iplugindir <dir> to the cc1 program, and it is queriable through the
905 -print-file-name=plugin option to gcc. */
906 const char*
907 default_plugin_dir_name (void)
909 if (!plugindir_string)
910 fatal_error (input_location,
911 "-iplugindir <dir> option not passed from the gcc driver");
912 return plugindir_string;