2015-01-12 Sandra Loosemore <sandra@codesourcery.com>
[official-gcc.git] / gcc / plugin.c
blob7e07b634d2f05af7ff426ec47cd7ec1e2ab03e0c
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 ("inaccessible plugin file %s expanded from short plugin name %s: %m",
191 plugin_name, base_name);
193 else
194 base_name = get_plugin_base_name (plugin_name);
196 /* If this is the first -fplugin= option we encounter, create
197 'plugin_name_args_tab' hash table. */
198 if (!plugin_name_args_tab)
199 plugin_name_args_tab = htab_create (10, htab_hash_string, htab_str_eq,
200 NULL);
202 slot = htab_find_slot (plugin_name_args_tab, base_name, INSERT);
204 /* If the same plugin (name) has been specified earlier, either emit an
205 error or a warning message depending on if they have identical full
206 (path) names. */
207 if (*slot)
209 plugin = (struct plugin_name_args *) *slot;
210 if (strcmp (plugin->full_name, plugin_name))
211 error ("plugin %s was specified with different paths:\n%s\n%s",
212 plugin->base_name, plugin->full_name, plugin_name);
213 return;
216 plugin = XCNEW (struct plugin_name_args);
217 plugin->base_name = base_name;
218 plugin->full_name = plugin_name;
220 *slot = plugin;
224 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
225 'plugin_argument' object for the parsed key-value pair. ARG is
226 the <name>-<key>[=<value>] part of the option. */
228 void
229 parse_plugin_arg_opt (const char *arg)
231 size_t len = 0, name_len = 0, key_len = 0, value_len = 0;
232 const char *ptr, *name_start = arg, *key_start = NULL, *value_start = NULL;
233 char *name, *key, *value;
234 void **slot;
235 bool name_parsed = false, key_parsed = false;
237 /* Iterate over the ARG string and identify the starting character position
238 of 'name', 'key', and 'value' and their lengths. */
239 for (ptr = arg; *ptr; ++ptr)
241 /* Only the first '-' encountered is considered a separator between
242 'name' and 'key'. All the subsequent '-'s are considered part of
243 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
244 the plugin name is 'foo' and the key is 'bar-primary-key'. */
245 if (*ptr == '-' && !name_parsed)
247 name_len = len;
248 len = 0;
249 key_start = ptr + 1;
250 name_parsed = true;
251 continue;
253 else if (*ptr == '=')
255 if (!key_parsed)
257 key_len = len;
258 len = 0;
259 value_start = ptr + 1;
260 key_parsed = true;
262 continue;
264 else
265 ++len;
268 if (!key_start)
270 error ("malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
271 arg);
272 return;
275 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
276 Otherwise, it is the VALUE_LEN. */
277 if (!value_start)
278 key_len = len;
279 else
280 value_len = len;
282 name = XNEWVEC (char, name_len + 1);
283 strncpy (name, name_start, name_len);
284 name[name_len] = '\0';
286 /* Check if the named plugin has already been specified earlier in the
287 command-line. */
288 if (plugin_name_args_tab
289 && ((slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT))
290 != NULL))
292 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
294 key = XNEWVEC (char, key_len + 1);
295 strncpy (key, key_start, key_len);
296 key[key_len] = '\0';
297 if (value_start)
299 value = XNEWVEC (char, value_len + 1);
300 strncpy (value, value_start, value_len);
301 value[value_len] = '\0';
303 else
304 value = NULL;
306 /* Create a plugin_argument object for the parsed key-value pair.
307 If there are already arguments for this plugin, we will need to
308 adjust the argument array size by creating a new array and deleting
309 the old one. If the performance ever becomes an issue, we can
310 change the code by pre-allocating a larger array first. */
311 if (plugin->argc > 0)
313 struct plugin_argument *args = XNEWVEC (struct plugin_argument,
314 plugin->argc + 1);
315 memcpy (args, plugin->argv,
316 sizeof (struct plugin_argument) * plugin->argc);
317 XDELETEVEC (plugin->argv);
318 plugin->argv = args;
319 ++plugin->argc;
321 else
323 gcc_assert (plugin->argv == NULL);
324 plugin->argv = XNEWVEC (struct plugin_argument, 1);
325 plugin->argc = 1;
328 plugin->argv[plugin->argc - 1].key = key;
329 plugin->argv[plugin->argc - 1].value = value;
331 else
332 error ("plugin %s should be specified before -fplugin-arg-%s "
333 "in the command line", name, arg);
335 /* We don't need the plugin's name anymore. Just release it. */
336 XDELETEVEC (name);
339 /* Register additional plugin information. NAME is the name passed to
340 plugin_init. INFO is the information that should be registered. */
342 static void
343 register_plugin_info (const char* name, struct plugin_info *info)
345 void **slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT);
346 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
347 plugin->version = info->version;
348 plugin->help = info->help;
351 /* Look up the event id for NAME. If the name is not found, return -1
352 if INSERT is NO_INSERT. */
355 get_named_event_id (const char *name, enum insert_option insert)
357 const char ***slot;
359 if (!event_tab)
361 int i;
363 event_tab = new hash_table<event_hasher> (150);
364 for (i = 0; i < event_last; i++)
366 slot = event_tab->find_slot (&plugin_event_name[i], INSERT);
367 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
368 *slot = &plugin_event_name[i];
371 slot = event_tab->find_slot (&name, insert);
372 if (slot == NULL)
373 return -1;
374 if (*slot != HTAB_EMPTY_ENTRY)
375 return *slot - &plugin_event_name[0];
377 if (event_last >= event_horizon)
379 event_horizon = event_last * 2;
380 if (plugin_event_name == plugin_event_name_init)
382 plugin_event_name = XNEWVEC (const char *, event_horizon);
383 memcpy (plugin_event_name, plugin_event_name_init,
384 sizeof plugin_event_name_init);
385 plugin_callbacks = XNEWVEC (struct callback_info *, event_horizon);
386 memcpy (plugin_callbacks, plugin_callbacks_init,
387 sizeof plugin_callbacks_init);
389 else
391 plugin_event_name
392 = XRESIZEVEC (const char *, plugin_event_name, event_horizon);
393 plugin_callbacks = XRESIZEVEC (struct callback_info *,
394 plugin_callbacks, event_horizon);
396 /* All the pointers in the hash table will need to be updated. */
397 delete event_tab;
398 event_tab = NULL;
400 else
401 *slot = &plugin_event_name[event_last];
402 plugin_event_name[event_last] = name;
403 return event_last++;
406 /* Called from the plugin's initialization code. Register a single callback.
407 This function can be called multiple times.
409 PLUGIN_NAME - display name for this plugin
410 EVENT - which event the callback is for
411 CALLBACK - the callback to be called at the event
412 USER_DATA - plugin-provided data */
414 void
415 register_callback (const char *plugin_name,
416 int event,
417 plugin_callback_func callback,
418 void *user_data)
420 switch (event)
422 case PLUGIN_PASS_MANAGER_SETUP:
423 gcc_assert (!callback);
424 register_pass ((struct register_pass_info *) user_data);
425 break;
426 case PLUGIN_INFO:
427 gcc_assert (!callback);
428 register_plugin_info (plugin_name, (struct plugin_info *) user_data);
429 break;
430 case PLUGIN_REGISTER_GGC_ROOTS:
431 gcc_assert (!callback);
432 ggc_register_root_tab ((const struct ggc_root_tab*) user_data);
433 break;
434 case PLUGIN_EVENT_FIRST_DYNAMIC:
435 default:
436 if (event < PLUGIN_EVENT_FIRST_DYNAMIC || event >= event_last)
438 error ("unknown callback event registered by plugin %s",
439 plugin_name);
440 return;
442 /* Fall through. */
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_FINISH_TYPE:
522 case PLUGIN_FINISH_DECL:
523 case PLUGIN_START_UNIT:
524 case PLUGIN_FINISH_UNIT:
525 case PLUGIN_PRE_GENERICIZE:
526 case PLUGIN_ATTRIBUTES:
527 case PLUGIN_PRAGMAS:
528 case PLUGIN_FINISH:
529 case PLUGIN_GGC_START:
530 case PLUGIN_GGC_MARKING:
531 case PLUGIN_GGC_END:
532 case PLUGIN_ALL_PASSES_START:
533 case PLUGIN_ALL_PASSES_END:
534 case PLUGIN_ALL_IPA_PASSES_START:
535 case PLUGIN_ALL_IPA_PASSES_END:
536 case PLUGIN_OVERRIDE_GATE:
537 case PLUGIN_PASS_EXECUTION:
538 case PLUGIN_EARLY_GIMPLE_PASSES_START:
539 case PLUGIN_EARLY_GIMPLE_PASSES_END:
540 case PLUGIN_NEW_PASS:
541 case PLUGIN_INCLUDE_FILE:
543 /* Iterate over every callback registered with this event and
544 call it. */
545 struct callback_info *callback = plugin_callbacks[event];
547 if (!callback)
548 retval = PLUGEVT_NO_CALLBACK;
549 for ( ; callback; callback = callback->next)
550 (*callback->func) (gcc_data, callback->user_data);
552 break;
554 case PLUGIN_PASS_MANAGER_SETUP:
555 case PLUGIN_REGISTER_GGC_ROOTS:
556 gcc_assert (false);
559 timevar_pop (TV_PLUGIN_RUN);
560 return retval;
563 #ifdef ENABLE_PLUGIN
564 /* We need a union to cast dlsym return value to a function pointer
565 as ISO C forbids assignment between function pointer and 'void *'.
566 Use explicit union instead of __extension__(<union_cast>) for
567 portability. */
568 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
569 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
570 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
572 /* Try to initialize PLUGIN. Return true if successful. */
574 static bool
575 try_init_one_plugin (struct plugin_name_args *plugin)
577 void *dl_handle;
578 plugin_init_func plugin_init;
579 const char *err;
580 PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
582 /* We use RTLD_NOW to accelerate binding and detect any mismatch
583 between the API expected by the plugin and the GCC API; we use
584 RTLD_GLOBAL which is useful to plugins which themselves call
585 dlopen. */
586 dl_handle = dlopen (plugin->full_name, RTLD_NOW | RTLD_GLOBAL);
587 if (!dl_handle)
589 error ("cannot load plugin %s\n%s", plugin->full_name, dlerror ());
590 return false;
593 /* Clear any existing error. */
594 dlerror ();
596 /* Check the plugin license. */
597 if (dlsym (dl_handle, str_license) == NULL)
598 fatal_error ("plugin %s is not licensed under a GPL-compatible license\n"
599 "%s", plugin->full_name, dlerror ());
601 PTR_UNION_AS_VOID_PTR (plugin_init_union) =
602 dlsym (dl_handle, str_plugin_init_func_name);
603 plugin_init = PTR_UNION_AS_CAST_PTR (plugin_init_union);
605 if ((err = dlerror ()) != NULL)
607 error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
608 plugin->full_name, err);
609 return false;
612 /* Call the plugin-provided initialization routine with the arguments. */
613 if ((*plugin_init) (plugin, &gcc_version))
615 error ("fail to initialize plugin %s", plugin->full_name);
616 return false;
619 return true;
623 /* Routine to dlopen and initialize one plugin. This function is passed to
624 (and called by) the hash table traverse routine. Return 1 for the
625 htab_traverse to continue scan, 0 to stop.
627 SLOT - slot of the hash table element
628 INFO - auxiliary pointer handed to hash table traverse routine
629 (unused in this function) */
631 static int
632 init_one_plugin (void **slot, void * ARG_UNUSED (info))
634 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
635 bool ok = try_init_one_plugin (plugin);
636 if (!ok)
638 htab_remove_elt (plugin_name_args_tab, plugin->base_name);
639 XDELETE (plugin);
641 return 1;
644 #endif /* ENABLE_PLUGIN */
646 /* Main plugin initialization function. Called from compile_file() in
647 toplev.c. */
649 void
650 initialize_plugins (void)
652 /* If no plugin was specified in the command-line, simply return. */
653 if (!plugin_name_args_tab)
654 return;
656 timevar_push (TV_PLUGIN_INIT);
658 #ifdef ENABLE_PLUGIN
659 /* Traverse and initialize each plugin specified in the command-line. */
660 htab_traverse_noresize (plugin_name_args_tab, init_one_plugin, NULL);
661 #endif
663 timevar_pop (TV_PLUGIN_INIT);
666 /* Release memory used by one plugin. */
668 static int
669 finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
671 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
672 XDELETE (plugin);
673 return 1;
676 /* Free memory allocated by the plugin system. */
678 void
679 finalize_plugins (void)
681 if (!plugin_name_args_tab)
682 return;
684 /* We can now delete the plugin_name_args object as it will no longer
685 be used. Note that base_name and argv fields (both of which were also
686 dynamically allocated) are not freed as they could still be used by
687 the plugin code. */
689 htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
691 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
692 htab_delete (plugin_name_args_tab);
693 plugin_name_args_tab = NULL;
696 /* Used to pass options to htab_traverse callbacks. */
698 struct print_options
700 FILE *file;
701 const char *indent;
704 /* Print the version of one plugin. */
706 static int
707 print_version_one_plugin (void **slot, void *data)
709 struct print_options *opt = (struct print_options *) data;
710 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
711 const char *version = plugin->version ? plugin->version : "Unknown version.";
713 fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
714 return 1;
717 /* Print the version of each plugin. */
719 void
720 print_plugins_versions (FILE *file, const char *indent)
722 struct print_options opt;
723 opt.file = file;
724 opt.indent = indent;
725 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
726 return;
728 fprintf (file, "%sVersions of loaded plugins:\n", indent);
729 htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
732 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
733 argument to htab_traverse_noresize. */
735 static int
736 print_help_one_plugin (void **slot, void *data)
738 struct print_options *opt = (struct print_options *) data;
739 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
740 const char *help = plugin->help ? plugin->help : "No help available .";
742 char *dup = xstrdup (help);
743 char *p, *nl;
744 fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
746 for (p = nl = dup; nl; p = nl)
748 nl = strchr (nl, '\n');
749 if (nl)
751 *nl = '\0';
752 nl++;
754 fprintf (opt->file, " %s %s\n", opt->indent, p);
757 free (dup);
758 return 1;
761 /* Print help for each plugin. The output goes to FILE and every line starts
762 with INDENT. */
764 void
765 print_plugins_help (FILE *file, const char *indent)
767 struct print_options opt;
768 opt.file = file;
769 opt.indent = indent;
770 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
771 return;
773 fprintf (file, "%sHelp for the loaded plugins:\n", indent);
774 htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
778 /* Return true if plugins have been loaded. */
780 bool
781 plugins_active_p (void)
783 int event;
785 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
786 if (plugin_callbacks[event])
787 return true;
789 return false;
793 /* Dump to FILE the names and associated events for all the active
794 plugins. */
796 DEBUG_FUNCTION void
797 dump_active_plugins (FILE *file)
799 int event;
801 if (!plugins_active_p ())
802 return;
804 fprintf (file, FMT_FOR_PLUGIN_EVENT " | %s\n", _("Event"), _("Plugins"));
805 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
806 if (plugin_callbacks[event])
808 struct callback_info *ci;
810 fprintf (file, FMT_FOR_PLUGIN_EVENT " |", plugin_event_name[event]);
812 for (ci = plugin_callbacks[event]; ci; ci = ci->next)
813 fprintf (file, " %s", ci->plugin_name);
815 putc ('\n', file);
820 /* Dump active plugins to stderr. */
822 DEBUG_FUNCTION void
823 debug_active_plugins (void)
825 dump_active_plugins (stderr);
828 /* Give a warning if plugins are present, before an ICE message asking
829 to submit a bug report. */
831 void
832 warn_if_plugins (void)
834 if (plugins_active_p ())
836 fnotice (stderr, "*** WARNING *** there are active plugins, do not report"
837 " this as a bug unless you can reproduce it without enabling"
838 " any plugins.\n");
839 dump_active_plugins (stderr);
844 /* Likewise, as a callback from the diagnostics code. */
846 void
847 plugins_internal_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
848 const char *msgid ATTRIBUTE_UNUSED,
849 va_list *ap ATTRIBUTE_UNUSED)
851 warn_if_plugins ();
854 /* The default version check. Compares every field in VERSION. */
856 bool
857 plugin_default_version_check (struct plugin_gcc_version *gcc_version,
858 struct plugin_gcc_version *plugin_version)
860 if (!gcc_version || !plugin_version)
861 return false;
863 if (strcmp (gcc_version->basever, plugin_version->basever))
864 return false;
865 if (strcmp (gcc_version->datestamp, plugin_version->datestamp))
866 return false;
867 if (strcmp (gcc_version->devphase, plugin_version->devphase))
868 return false;
869 if (strcmp (gcc_version->revision, plugin_version->revision))
870 return false;
871 if (strcmp (gcc_version->configuration_arguments,
872 plugin_version->configuration_arguments))
873 return false;
874 return true;
878 /* Return the current value of event_last, so that plugins which provide
879 additional functionality for events for the benefit of high-level plugins
880 know how many valid entries plugin_event_name holds. */
883 get_event_last (void)
885 return event_last;
889 /* Retrieve the default plugin directory. The gcc driver should have passed
890 it as -iplugindir <dir> to the cc1 program, and it is queriable through the
891 -print-file-name=plugin option to gcc. */
892 const char*
893 default_plugin_dir_name (void)
895 if (!plugindir_string)
896 fatal_error ("-iplugindir <dir> option not passed from the gcc driver");
897 return plugindir_string;