PR target/37072
[official-gcc.git] / gcc / plugin.c
blob93e2e3a6a097cf6c3b50266b9c4b15e2d6fdf453
1 /* Support for GCC plugin mechanism.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains the support for GCC plugin mechanism based on the
21 APIs described in doc/plugin.texi. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "diagnostic-core.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "options.h"
30 #include "flags.h"
31 #include "tree.h"
32 #include "tree-pass.h"
33 #include "intl.h"
34 #include "plugin.h"
36 #ifdef ENABLE_PLUGIN
37 #include "plugin-version.h"
38 #endif
40 #define GCC_PLUGIN_STRINGIFY0(X) #X
41 #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X)
43 /* Event names as strings. Keep in sync with enum plugin_event. */
44 static const char *plugin_event_name_init[] =
46 # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME),
47 # include "plugin.def"
48 # undef DEFEVENT
51 /* A printf format large enough for the largest event above. */
52 #define FMT_FOR_PLUGIN_EVENT "%-32s"
54 const char **plugin_event_name = plugin_event_name_init;
56 /* Event hashtable helpers. */
58 struct event_hasher : nofree_ptr_hash <const char *>
60 static inline hashval_t hash (const char **);
61 static inline bool equal (const char **, const char **);
64 /* Helper function for the event hash table that hashes the entry V. */
66 inline hashval_t
67 event_hasher::hash (const char **v)
69 return htab_hash_string (*v);
72 /* Helper function for the event hash table that compares the name of an
73 existing entry (S1) with the given string (S2). */
75 inline bool
76 event_hasher::equal (const char **s1, const char **s2)
78 return !strcmp (*s1, *s2);
81 /* A hash table to map event names to the position of the names in the
82 plugin_event_name table. */
83 static hash_table<event_hasher> *event_tab;
85 /* Keep track of the limit of allocated events and space ready for
86 allocating events. */
87 static int event_last = PLUGIN_EVENT_FIRST_DYNAMIC;
88 static int event_horizon = PLUGIN_EVENT_FIRST_DYNAMIC;
90 /* Hash table for the plugin_name_args objects created during command-line
91 parsing. */
92 static htab_t plugin_name_args_tab = NULL;
94 /* List node for keeping track of plugin-registered callback. */
95 struct callback_info
97 const char *plugin_name; /* Name of plugin that registers the callback. */
98 plugin_callback_func func; /* Callback to be called. */
99 void *user_data; /* plugin-specified data. */
100 struct callback_info *next;
103 /* An array of lists of 'callback_info' objects indexed by the event id. */
104 static struct callback_info *plugin_callbacks_init[PLUGIN_EVENT_FIRST_DYNAMIC];
105 static struct callback_info **plugin_callbacks = plugin_callbacks_init;
107 /* For invoke_plugin_callbacks(), see plugin.h. */
108 bool flag_plugin_added = false;
110 #ifdef ENABLE_PLUGIN
111 /* Each plugin should define an initialization function with exactly
112 this name. */
113 static const char *str_plugin_init_func_name = "plugin_init";
115 /* Each plugin should define this symbol to assert that it is
116 distributed under a GPL-compatible license. */
117 static const char *str_license = "plugin_is_GPL_compatible";
118 #endif
120 /* Helper function for the hash table that compares the base_name of the
121 existing entry (S1) with the given string (S2). */
123 static int
124 htab_str_eq (const void *s1, const void *s2)
126 const struct plugin_name_args *plugin = (const struct plugin_name_args *) s1;
127 return !strcmp (plugin->base_name, (const char *) s2);
131 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
132 return NAME. */
134 static char *
135 get_plugin_base_name (const char *full_name)
137 /* First get the base name part of the full-path name, i.e. NAME.so. */
138 char *base_name = xstrdup (lbasename (full_name));
140 /* Then get rid of '.so' part of the name. */
141 strip_off_ending (base_name, strlen (base_name));
143 return base_name;
147 /* Create a plugin_name_args object for the given plugin and insert it
148 to the hash table. This function is called when
149 -fplugin=/path/to/NAME.so or -fplugin=NAME option is processed. */
151 void
152 add_new_plugin (const char* plugin_name)
154 struct plugin_name_args *plugin;
155 void **slot;
156 char *base_name;
157 bool name_is_short;
158 const char *pc;
160 flag_plugin_added = true;
162 /* Replace short names by their full path when relevant. */
163 name_is_short = !IS_ABSOLUTE_PATH (plugin_name);
164 for (pc = plugin_name; name_is_short && *pc; pc++)
165 if (*pc == '.' || IS_DIR_SEPARATOR (*pc))
166 name_is_short = false;
168 if (name_is_short)
170 base_name = CONST_CAST (char*, plugin_name);
171 /* FIXME: the ".so" suffix is currently builtin, since plugins
172 only work on ELF host systems like e.g. Linux or Solaris.
173 When plugins shall be available on non ELF systems such as
174 Windows or MacOS, this code has to be greatly improved. */
175 plugin_name = concat (default_plugin_dir_name (), "/",
176 plugin_name, ".so", NULL);
177 if (access (plugin_name, R_OK))
178 fatal_error
179 (input_location,
180 "inaccessible plugin file %s expanded from short plugin name %s: %m",
181 plugin_name, base_name);
183 else
184 base_name = get_plugin_base_name (plugin_name);
186 /* If this is the first -fplugin= option we encounter, create
187 'plugin_name_args_tab' hash table. */
188 if (!plugin_name_args_tab)
189 plugin_name_args_tab = htab_create (10, htab_hash_string, htab_str_eq,
190 NULL);
192 slot = htab_find_slot (plugin_name_args_tab, base_name, INSERT);
194 /* If the same plugin (name) has been specified earlier, either emit an
195 error or a warning message depending on if they have identical full
196 (path) names. */
197 if (*slot)
199 plugin = (struct plugin_name_args *) *slot;
200 if (strcmp (plugin->full_name, plugin_name))
201 error ("plugin %s was specified with different paths:\n%s\n%s",
202 plugin->base_name, plugin->full_name, plugin_name);
203 return;
206 plugin = XCNEW (struct plugin_name_args);
207 plugin->base_name = base_name;
208 plugin->full_name = plugin_name;
210 *slot = plugin;
214 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
215 'plugin_argument' object for the parsed key-value pair. ARG is
216 the <name>-<key>[=<value>] part of the option. */
218 void
219 parse_plugin_arg_opt (const char *arg)
221 size_t len = 0, name_len = 0, key_len = 0, value_len = 0;
222 const char *ptr, *name_start = arg, *key_start = NULL, *value_start = NULL;
223 char *name, *key, *value;
224 void **slot;
225 bool name_parsed = false, key_parsed = false;
227 /* Iterate over the ARG string and identify the starting character position
228 of 'name', 'key', and 'value' and their lengths. */
229 for (ptr = arg; *ptr; ++ptr)
231 /* Only the first '-' encountered is considered a separator between
232 'name' and 'key'. All the subsequent '-'s are considered part of
233 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
234 the plugin name is 'foo' and the key is 'bar-primary-key'. */
235 if (*ptr == '-' && !name_parsed)
237 name_len = len;
238 len = 0;
239 key_start = ptr + 1;
240 name_parsed = true;
241 continue;
243 else if (*ptr == '=')
245 if (!key_parsed)
247 key_len = len;
248 len = 0;
249 value_start = ptr + 1;
250 key_parsed = true;
252 continue;
254 else
255 ++len;
258 if (!key_start)
260 error ("malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
261 arg);
262 return;
265 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
266 Otherwise, it is the VALUE_LEN. */
267 if (!value_start)
268 key_len = len;
269 else
270 value_len = len;
272 name = XNEWVEC (char, name_len + 1);
273 strncpy (name, name_start, name_len);
274 name[name_len] = '\0';
276 /* Check if the named plugin has already been specified earlier in the
277 command-line. */
278 if (plugin_name_args_tab
279 && ((slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT))
280 != NULL))
282 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
284 key = XNEWVEC (char, key_len + 1);
285 strncpy (key, key_start, key_len);
286 key[key_len] = '\0';
287 if (value_start)
289 value = XNEWVEC (char, value_len + 1);
290 strncpy (value, value_start, value_len);
291 value[value_len] = '\0';
293 else
294 value = NULL;
296 /* Create a plugin_argument object for the parsed key-value pair.
297 If there are already arguments for this plugin, we will need to
298 adjust the argument array size by creating a new array and deleting
299 the old one. If the performance ever becomes an issue, we can
300 change the code by pre-allocating a larger array first. */
301 if (plugin->argc > 0)
303 struct plugin_argument *args = XNEWVEC (struct plugin_argument,
304 plugin->argc + 1);
305 memcpy (args, plugin->argv,
306 sizeof (struct plugin_argument) * plugin->argc);
307 XDELETEVEC (plugin->argv);
308 plugin->argv = args;
309 ++plugin->argc;
311 else
313 gcc_assert (plugin->argv == NULL);
314 plugin->argv = XNEWVEC (struct plugin_argument, 1);
315 plugin->argc = 1;
318 plugin->argv[plugin->argc - 1].key = key;
319 plugin->argv[plugin->argc - 1].value = value;
321 else
322 error ("plugin %s should be specified before -fplugin-arg-%s "
323 "in the command line", name, arg);
325 /* We don't need the plugin's name anymore. Just release it. */
326 XDELETEVEC (name);
329 /* Register additional plugin information. NAME is the name passed to
330 plugin_init. INFO is the information that should be registered. */
332 static void
333 register_plugin_info (const char* name, struct plugin_info *info)
335 void **slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT);
336 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
337 plugin->version = info->version;
338 plugin->help = info->help;
341 /* Look up the event id for NAME. If the name is not found, return -1
342 if INSERT is NO_INSERT. */
345 get_named_event_id (const char *name, enum insert_option insert)
347 const char ***slot;
349 if (!event_tab)
351 int i;
353 event_tab = new hash_table<event_hasher> (150);
354 for (i = 0; i < event_last; i++)
356 slot = event_tab->find_slot (&plugin_event_name[i], INSERT);
357 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
358 *slot = &plugin_event_name[i];
361 slot = event_tab->find_slot (&name, insert);
362 if (slot == NULL)
363 return -1;
364 if (*slot != HTAB_EMPTY_ENTRY)
365 return *slot - &plugin_event_name[0];
367 if (event_last >= event_horizon)
369 event_horizon = event_last * 2;
370 if (plugin_event_name == plugin_event_name_init)
372 plugin_event_name = XNEWVEC (const char *, event_horizon);
373 memcpy (plugin_event_name, plugin_event_name_init,
374 sizeof plugin_event_name_init);
375 plugin_callbacks = XNEWVEC (struct callback_info *, event_horizon);
376 memcpy (plugin_callbacks, plugin_callbacks_init,
377 sizeof plugin_callbacks_init);
379 else
381 plugin_event_name
382 = XRESIZEVEC (const char *, plugin_event_name, event_horizon);
383 plugin_callbacks = XRESIZEVEC (struct callback_info *,
384 plugin_callbacks, event_horizon);
386 /* All the pointers in the hash table will need to be updated. */
387 delete event_tab;
388 event_tab = NULL;
390 else
391 *slot = &plugin_event_name[event_last];
392 plugin_event_name[event_last] = name;
393 return event_last++;
396 /* Called from the plugin's initialization code. Register a single callback.
397 This function can be called multiple times.
399 PLUGIN_NAME - display name for this plugin
400 EVENT - which event the callback is for
401 CALLBACK - the callback to be called at the event
402 USER_DATA - plugin-provided data */
404 void
405 register_callback (const char *plugin_name,
406 int event,
407 plugin_callback_func callback,
408 void *user_data)
410 switch (event)
412 case PLUGIN_PASS_MANAGER_SETUP:
413 gcc_assert (!callback);
414 register_pass ((struct register_pass_info *) user_data);
415 break;
416 case PLUGIN_INFO:
417 gcc_assert (!callback);
418 register_plugin_info (plugin_name, (struct plugin_info *) user_data);
419 break;
420 case PLUGIN_REGISTER_GGC_ROOTS:
421 gcc_assert (!callback);
422 ggc_register_root_tab ((const struct ggc_root_tab*) user_data);
423 break;
424 case PLUGIN_EVENT_FIRST_DYNAMIC:
425 default:
426 if (event < PLUGIN_EVENT_FIRST_DYNAMIC || event >= event_last)
428 error ("unknown callback event registered by plugin %s",
429 plugin_name);
430 return;
432 /* Fall through. */
433 case PLUGIN_START_PARSE_FUNCTION:
434 case PLUGIN_FINISH_PARSE_FUNCTION:
435 case PLUGIN_FINISH_TYPE:
436 case PLUGIN_FINISH_DECL:
437 case PLUGIN_START_UNIT:
438 case PLUGIN_FINISH_UNIT:
439 case PLUGIN_PRE_GENERICIZE:
440 case PLUGIN_GGC_START:
441 case PLUGIN_GGC_MARKING:
442 case PLUGIN_GGC_END:
443 case PLUGIN_ATTRIBUTES:
444 case PLUGIN_PRAGMAS:
445 case PLUGIN_FINISH:
446 case PLUGIN_ALL_PASSES_START:
447 case PLUGIN_ALL_PASSES_END:
448 case PLUGIN_ALL_IPA_PASSES_START:
449 case PLUGIN_ALL_IPA_PASSES_END:
450 case PLUGIN_OVERRIDE_GATE:
451 case PLUGIN_PASS_EXECUTION:
452 case PLUGIN_EARLY_GIMPLE_PASSES_START:
453 case PLUGIN_EARLY_GIMPLE_PASSES_END:
454 case PLUGIN_NEW_PASS:
455 case PLUGIN_INCLUDE_FILE:
457 struct callback_info *new_callback;
458 if (!callback)
460 error ("plugin %s registered a null callback function "
461 "for event %s", plugin_name, plugin_event_name[event]);
462 return;
464 new_callback = XNEW (struct callback_info);
465 new_callback->plugin_name = plugin_name;
466 new_callback->func = callback;
467 new_callback->user_data = user_data;
468 new_callback->next = plugin_callbacks[event];
469 plugin_callbacks[event] = new_callback;
471 break;
475 /* Remove a callback for EVENT which has been registered with for a plugin
476 PLUGIN_NAME. Return PLUGEVT_SUCCESS if a matching callback was
477 found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching
478 callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid. */
480 unregister_callback (const char *plugin_name, int event)
482 struct callback_info *callback, **cbp;
484 if (event >= event_last)
485 return PLUGEVT_NO_SUCH_EVENT;
487 for (cbp = &plugin_callbacks[event]; (callback = *cbp); cbp = &callback->next)
488 if (strcmp (callback->plugin_name, plugin_name) == 0)
490 *cbp = callback->next;
491 return PLUGEVT_SUCCESS;
493 return PLUGEVT_NO_CALLBACK;
496 /* Invoke all plugin callbacks registered with the specified event,
497 called from invoke_plugin_callbacks(). */
500 invoke_plugin_callbacks_full (int event, void *gcc_data)
502 int retval = PLUGEVT_SUCCESS;
504 timevar_push (TV_PLUGIN_RUN);
506 switch (event)
508 case PLUGIN_EVENT_FIRST_DYNAMIC:
509 default:
510 gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
511 gcc_assert (event < event_last);
512 /* Fall through. */
513 case PLUGIN_START_PARSE_FUNCTION:
514 case PLUGIN_FINISH_PARSE_FUNCTION:
515 case PLUGIN_FINISH_TYPE:
516 case PLUGIN_FINISH_DECL:
517 case PLUGIN_START_UNIT:
518 case PLUGIN_FINISH_UNIT:
519 case PLUGIN_PRE_GENERICIZE:
520 case PLUGIN_ATTRIBUTES:
521 case PLUGIN_PRAGMAS:
522 case PLUGIN_FINISH:
523 case PLUGIN_GGC_START:
524 case PLUGIN_GGC_MARKING:
525 case PLUGIN_GGC_END:
526 case PLUGIN_ALL_PASSES_START:
527 case PLUGIN_ALL_PASSES_END:
528 case PLUGIN_ALL_IPA_PASSES_START:
529 case PLUGIN_ALL_IPA_PASSES_END:
530 case PLUGIN_OVERRIDE_GATE:
531 case PLUGIN_PASS_EXECUTION:
532 case PLUGIN_EARLY_GIMPLE_PASSES_START:
533 case PLUGIN_EARLY_GIMPLE_PASSES_END:
534 case PLUGIN_NEW_PASS:
535 case PLUGIN_INCLUDE_FILE:
537 /* Iterate over every callback registered with this event and
538 call it. */
539 struct callback_info *callback = plugin_callbacks[event];
541 if (!callback)
542 retval = PLUGEVT_NO_CALLBACK;
543 for ( ; callback; callback = callback->next)
544 (*callback->func) (gcc_data, callback->user_data);
546 break;
548 case PLUGIN_PASS_MANAGER_SETUP:
549 case PLUGIN_REGISTER_GGC_ROOTS:
550 gcc_assert (false);
553 timevar_pop (TV_PLUGIN_RUN);
554 return retval;
557 #ifdef ENABLE_PLUGIN
558 /* We need a union to cast dlsym return value to a function pointer
559 as ISO C forbids assignment between function pointer and 'void *'.
560 Use explicit union instead of __extension__(<union_cast>) for
561 portability. */
562 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
563 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
564 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
566 /* Try to initialize PLUGIN. Return true if successful. */
568 static bool
569 try_init_one_plugin (struct plugin_name_args *plugin)
571 void *dl_handle;
572 plugin_init_func plugin_init;
573 const char *err;
574 PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
576 /* We use RTLD_NOW to accelerate binding and detect any mismatch
577 between the API expected by the plugin and the GCC API; we use
578 RTLD_GLOBAL which is useful to plugins which themselves call
579 dlopen. */
580 dl_handle = dlopen (plugin->full_name, RTLD_NOW | RTLD_GLOBAL);
581 if (!dl_handle)
583 error ("cannot load plugin %s\n%s", plugin->full_name, dlerror ());
584 return false;
587 /* Clear any existing error. */
588 dlerror ();
590 /* Check the plugin license. */
591 if (dlsym (dl_handle, str_license) == NULL)
592 fatal_error (input_location,
593 "plugin %s is not licensed under a GPL-compatible license\n"
594 "%s", plugin->full_name, dlerror ());
596 PTR_UNION_AS_VOID_PTR (plugin_init_union) =
597 dlsym (dl_handle, str_plugin_init_func_name);
598 plugin_init = PTR_UNION_AS_CAST_PTR (plugin_init_union);
600 if ((err = dlerror ()) != NULL)
602 error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
603 plugin->full_name, err);
604 return false;
607 /* Call the plugin-provided initialization routine with the arguments. */
608 if ((*plugin_init) (plugin, &gcc_version))
610 error ("fail to initialize plugin %s", plugin->full_name);
611 return false;
614 return true;
618 /* Routine to dlopen and initialize one plugin. This function is passed to
619 (and called by) the hash table traverse routine. Return 1 for the
620 htab_traverse to continue scan, 0 to stop.
622 SLOT - slot of the hash table element
623 INFO - auxiliary pointer handed to hash table traverse routine
624 (unused in this function) */
626 static int
627 init_one_plugin (void **slot, void * ARG_UNUSED (info))
629 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
630 bool ok = try_init_one_plugin (plugin);
631 if (!ok)
633 htab_remove_elt (plugin_name_args_tab, plugin->base_name);
634 XDELETE (plugin);
636 return 1;
639 #endif /* ENABLE_PLUGIN */
641 /* Main plugin initialization function. Called from compile_file() in
642 toplev.c. */
644 void
645 initialize_plugins (void)
647 /* If no plugin was specified in the command-line, simply return. */
648 if (!plugin_name_args_tab)
649 return;
651 timevar_push (TV_PLUGIN_INIT);
653 #ifdef ENABLE_PLUGIN
654 /* Traverse and initialize each plugin specified in the command-line. */
655 htab_traverse_noresize (plugin_name_args_tab, init_one_plugin, NULL);
656 #endif
658 timevar_pop (TV_PLUGIN_INIT);
661 /* Release memory used by one plugin. */
663 static int
664 finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
666 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
667 XDELETE (plugin);
668 return 1;
671 /* Free memory allocated by the plugin system. */
673 void
674 finalize_plugins (void)
676 if (!plugin_name_args_tab)
677 return;
679 /* We can now delete the plugin_name_args object as it will no longer
680 be used. Note that base_name and argv fields (both of which were also
681 dynamically allocated) are not freed as they could still be used by
682 the plugin code. */
684 htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
686 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
687 htab_delete (plugin_name_args_tab);
688 plugin_name_args_tab = NULL;
691 /* Used to pass options to htab_traverse callbacks. */
693 struct print_options
695 FILE *file;
696 const char *indent;
699 /* Print the version of one plugin. */
701 static int
702 print_version_one_plugin (void **slot, void *data)
704 struct print_options *opt = (struct print_options *) data;
705 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
706 const char *version = plugin->version ? plugin->version : "Unknown version.";
708 fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
709 return 1;
712 /* Print the version of each plugin. */
714 void
715 print_plugins_versions (FILE *file, const char *indent)
717 struct print_options opt;
718 opt.file = file;
719 opt.indent = indent;
720 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
721 return;
723 fprintf (file, "%sVersions of loaded plugins:\n", indent);
724 htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
727 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
728 argument to htab_traverse_noresize. */
730 static int
731 print_help_one_plugin (void **slot, void *data)
733 struct print_options *opt = (struct print_options *) data;
734 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
735 const char *help = plugin->help ? plugin->help : "No help available .";
737 char *dup = xstrdup (help);
738 char *p, *nl;
739 fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
741 for (p = nl = dup; nl; p = nl)
743 nl = strchr (nl, '\n');
744 if (nl)
746 *nl = '\0';
747 nl++;
749 fprintf (opt->file, " %s %s\n", opt->indent, p);
752 free (dup);
753 return 1;
756 /* Print help for each plugin. The output goes to FILE and every line starts
757 with INDENT. */
759 void
760 print_plugins_help (FILE *file, const char *indent)
762 struct print_options opt;
763 opt.file = file;
764 opt.indent = indent;
765 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
766 return;
768 fprintf (file, "%sHelp for the loaded plugins:\n", indent);
769 htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
773 /* Return true if plugins have been loaded. */
775 bool
776 plugins_active_p (void)
778 int event;
780 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
781 if (plugin_callbacks[event])
782 return true;
784 return false;
788 /* Dump to FILE the names and associated events for all the active
789 plugins. */
791 DEBUG_FUNCTION void
792 dump_active_plugins (FILE *file)
794 int event;
796 if (!plugins_active_p ())
797 return;
799 fprintf (file, FMT_FOR_PLUGIN_EVENT " | %s\n", _("Event"), _("Plugins"));
800 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
801 if (plugin_callbacks[event])
803 struct callback_info *ci;
805 fprintf (file, FMT_FOR_PLUGIN_EVENT " |", plugin_event_name[event]);
807 for (ci = plugin_callbacks[event]; ci; ci = ci->next)
808 fprintf (file, " %s", ci->plugin_name);
810 putc ('\n', file);
815 /* Dump active plugins to stderr. */
817 DEBUG_FUNCTION void
818 debug_active_plugins (void)
820 dump_active_plugins (stderr);
823 /* Give a warning if plugins are present, before an ICE message asking
824 to submit a bug report. */
826 void
827 warn_if_plugins (void)
829 if (plugins_active_p ())
831 fnotice (stderr, "*** WARNING *** there are active plugins, do not report"
832 " this as a bug unless you can reproduce it without enabling"
833 " any plugins.\n");
834 dump_active_plugins (stderr);
839 /* Likewise, as a callback from the diagnostics code. */
841 void
842 plugins_internal_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
843 const char *msgid ATTRIBUTE_UNUSED,
844 va_list *ap ATTRIBUTE_UNUSED)
846 warn_if_plugins ();
849 /* The default version check. Compares every field in VERSION. */
851 bool
852 plugin_default_version_check (struct plugin_gcc_version *gcc_version,
853 struct plugin_gcc_version *plugin_version)
855 if (!gcc_version || !plugin_version)
856 return false;
858 if (strcmp (gcc_version->basever, plugin_version->basever))
859 return false;
860 if (strcmp (gcc_version->datestamp, plugin_version->datestamp))
861 return false;
862 if (strcmp (gcc_version->devphase, plugin_version->devphase))
863 return false;
864 if (strcmp (gcc_version->revision, plugin_version->revision))
865 return false;
866 if (strcmp (gcc_version->configuration_arguments,
867 plugin_version->configuration_arguments))
868 return false;
869 return true;
873 /* Return the current value of event_last, so that plugins which provide
874 additional functionality for events for the benefit of high-level plugins
875 know how many valid entries plugin_event_name holds. */
878 get_event_last (void)
880 return event_last;
884 /* Retrieve the default plugin directory. The gcc driver should have passed
885 it as -iplugindir <dir> to the cc1 program, and it is queriable through the
886 -print-file-name=plugin option to gcc. */
887 const char*
888 default_plugin_dir_name (void)
890 if (!plugindir_string)
891 fatal_error (input_location,
892 "-iplugindir <dir> option not passed from the gcc driver");
893 return plugindir_string;