lto-cgraph.c (get_alias_symbol): Remove weakref sanity check.
[gcc.git] / gcc / plugin.c
1 /* Support for GCC plugin mechanism.
2 Copyright (C) 2009-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 /* This file contains the support for GCC plugin mechanism based on the
21 APIs described in doc/plugin.texi. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "hash-table.h"
27 #include "diagnostic-core.h"
28 #include "tree.h"
29 #include "tree-pass.h"
30 #include "intl.h"
31 #include "plugin.h"
32 #include "ggc.h"
33
34 #ifdef ENABLE_PLUGIN
35 #include "plugin-version.h"
36 #endif
37
38 #define GCC_PLUGIN_STRINGIFY0(X) #X
39 #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X)
40
41 /* Event names as strings. Keep in sync with enum plugin_event. */
42 static const char *plugin_event_name_init[] =
43 {
44 # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME),
45 # include "plugin.def"
46 # undef DEFEVENT
47 };
48
49 /* A printf format large enough for the largest event above. */
50 #define FMT_FOR_PLUGIN_EVENT "%-32s"
51
52 const char **plugin_event_name = plugin_event_name_init;
53
54 /* Event hashtable helpers. */
55
56 struct event_hasher : typed_noop_remove <const char *>
57 {
58 typedef const char *value_type;
59 typedef const char *compare_type;
60 static inline hashval_t hash (const value_type *);
61 static inline bool equal (const value_type *, const compare_type *);
62 };
63
64 /* Helper function for the event hash table that hashes the entry V. */
65
66 inline hashval_t
67 event_hasher::hash (const value_type *v)
68 {
69 return htab_hash_string (*v);
70 }
71
72 /* Helper function for the event hash table that compares the name of an
73 existing entry (S1) with the given string (S2). */
74
75 inline bool
76 event_hasher::equal (const value_type *s1, const compare_type *s2)
77 {
78 return !strcmp (*s1, *s2);
79 }
80
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;
84
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;
89
90 /* Hash table for the plugin_name_args objects created during command-line
91 parsing. */
92 static htab_t plugin_name_args_tab = NULL;
93
94 /* List node for keeping track of plugin-registered callback. */
95 struct callback_info
96 {
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;
101 };
102
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;
106
107 /* For invoke_plugin_callbacks(), see plugin.h. */
108 bool flag_plugin_added = false;
109
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";
114
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
119
120 /* Helper function for the hash table that compares the base_name of the
121 existing entry (S1) with the given string (S2). */
122
123 static int
124 htab_str_eq (const void *s1, const void *s2)
125 {
126 const struct plugin_name_args *plugin = (const struct plugin_name_args *) s1;
127 return !strcmp (plugin->base_name, (const char *) s2);
128 }
129
130
131 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
132 return NAME. */
133
134 static char *
135 get_plugin_base_name (const char *full_name)
136 {
137 /* First get the base name part of the full-path name, i.e. NAME.so. */
138 char *base_name = xstrdup (lbasename (full_name));
139
140 /* Then get rid of '.so' part of the name. */
141 strip_off_ending (base_name, strlen (base_name));
142
143 return base_name;
144 }
145
146
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. */
150
151 void
152 add_new_plugin (const char* plugin_name)
153 {
154 struct plugin_name_args *plugin;
155 void **slot;
156 char *base_name;
157 bool name_is_short;
158 const char *pc;
159
160 flag_plugin_added = true;
161
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;
167
168 if (name_is_short)
169 {
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 ("inaccessible plugin file %s expanded from short plugin name %s: %m",
180 plugin_name, base_name);
181 }
182 else
183 base_name = get_plugin_base_name (plugin_name);
184
185 /* If this is the first -fplugin= option we encounter, create
186 'plugin_name_args_tab' hash table. */
187 if (!plugin_name_args_tab)
188 plugin_name_args_tab = htab_create (10, htab_hash_string, htab_str_eq,
189 NULL);
190
191 slot = htab_find_slot (plugin_name_args_tab, base_name, INSERT);
192
193 /* If the same plugin (name) has been specified earlier, either emit an
194 error or a warning message depending on if they have identical full
195 (path) names. */
196 if (*slot)
197 {
198 plugin = (struct plugin_name_args *) *slot;
199 if (strcmp (plugin->full_name, plugin_name))
200 error ("plugin %s was specified with different paths:\n%s\n%s",
201 plugin->base_name, plugin->full_name, plugin_name);
202 return;
203 }
204
205 plugin = XCNEW (struct plugin_name_args);
206 plugin->base_name = base_name;
207 plugin->full_name = plugin_name;
208
209 *slot = plugin;
210 }
211
212
213 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
214 'plugin_argument' object for the parsed key-value pair. ARG is
215 the <name>-<key>[=<value>] part of the option. */
216
217 void
218 parse_plugin_arg_opt (const char *arg)
219 {
220 size_t len = 0, name_len = 0, key_len = 0, value_len = 0;
221 const char *ptr, *name_start = arg, *key_start = NULL, *value_start = NULL;
222 char *name, *key, *value;
223 void **slot;
224 bool name_parsed = false, key_parsed = false;
225
226 /* Iterate over the ARG string and identify the starting character position
227 of 'name', 'key', and 'value' and their lengths. */
228 for (ptr = arg; *ptr; ++ptr)
229 {
230 /* Only the first '-' encountered is considered a separator between
231 'name' and 'key'. All the subsequent '-'s are considered part of
232 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
233 the plugin name is 'foo' and the key is 'bar-primary-key'. */
234 if (*ptr == '-' && !name_parsed)
235 {
236 name_len = len;
237 len = 0;
238 key_start = ptr + 1;
239 name_parsed = true;
240 continue;
241 }
242 else if (*ptr == '=')
243 {
244 if (key_parsed)
245 {
246 error ("malformed option -fplugin-arg-%s (multiple '=' signs)",
247 arg);
248 return;
249 }
250 key_len = len;
251 len = 0;
252 value_start = ptr + 1;
253 key_parsed = true;
254 continue;
255 }
256 else
257 ++len;
258 }
259
260 if (!key_start)
261 {
262 error ("malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
263 arg);
264 return;
265 }
266
267 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
268 Otherwise, it is the VALUE_LEN. */
269 if (!value_start)
270 key_len = len;
271 else
272 value_len = len;
273
274 name = XNEWVEC (char, name_len + 1);
275 strncpy (name, name_start, name_len);
276 name[name_len] = '\0';
277
278 /* Check if the named plugin has already been specified earlier in the
279 command-line. */
280 if (plugin_name_args_tab
281 && ((slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT))
282 != NULL))
283 {
284 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
285
286 key = XNEWVEC (char, key_len + 1);
287 strncpy (key, key_start, key_len);
288 key[key_len] = '\0';
289 if (value_start)
290 {
291 value = XNEWVEC (char, value_len + 1);
292 strncpy (value, value_start, value_len);
293 value[value_len] = '\0';
294 }
295 else
296 value = NULL;
297
298 /* Create a plugin_argument object for the parsed key-value pair.
299 If there are already arguments for this plugin, we will need to
300 adjust the argument array size by creating a new array and deleting
301 the old one. If the performance ever becomes an issue, we can
302 change the code by pre-allocating a larger array first. */
303 if (plugin->argc > 0)
304 {
305 struct plugin_argument *args = XNEWVEC (struct plugin_argument,
306 plugin->argc + 1);
307 memcpy (args, plugin->argv,
308 sizeof (struct plugin_argument) * plugin->argc);
309 XDELETEVEC (plugin->argv);
310 plugin->argv = args;
311 ++plugin->argc;
312 }
313 else
314 {
315 gcc_assert (plugin->argv == NULL);
316 plugin->argv = XNEWVEC (struct plugin_argument, 1);
317 plugin->argc = 1;
318 }
319
320 plugin->argv[plugin->argc - 1].key = key;
321 plugin->argv[plugin->argc - 1].value = value;
322 }
323 else
324 error ("plugin %s should be specified before -fplugin-arg-%s "
325 "in the command line", name, arg);
326
327 /* We don't need the plugin's name anymore. Just release it. */
328 XDELETEVEC (name);
329 }
330
331 /* Register additional plugin information. NAME is the name passed to
332 plugin_init. INFO is the information that should be registered. */
333
334 static void
335 register_plugin_info (const char* name, struct plugin_info *info)
336 {
337 void **slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT);
338 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
339 plugin->version = info->version;
340 plugin->help = info->help;
341 }
342
343 /* Look up the event id for NAME. If the name is not found, return -1
344 if INSERT is NO_INSERT. */
345
346 int
347 get_named_event_id (const char *name, enum insert_option insert)
348 {
349 const char ***slot;
350
351 if (!event_tab.is_created ())
352 {
353 int i;
354
355 event_tab.create (150);
356 for (i = 0; i < event_last; i++)
357 {
358 slot = event_tab.find_slot (&plugin_event_name[i], INSERT);
359 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
360 *slot = &plugin_event_name[i];
361 }
362 }
363 slot = event_tab.find_slot (&name, insert);
364 if (slot == NULL)
365 return -1;
366 if (*slot != HTAB_EMPTY_ENTRY)
367 return *slot - &plugin_event_name[0];
368
369 if (event_last >= event_horizon)
370 {
371 event_horizon = event_last * 2;
372 if (plugin_event_name == plugin_event_name_init)
373 {
374 plugin_event_name = XNEWVEC (const char *, event_horizon);
375 memcpy (plugin_event_name, plugin_event_name_init,
376 sizeof plugin_event_name_init);
377 plugin_callbacks = XNEWVEC (struct callback_info *, event_horizon);
378 memcpy (plugin_callbacks, plugin_callbacks_init,
379 sizeof plugin_callbacks_init);
380 }
381 else
382 {
383 plugin_event_name
384 = XRESIZEVEC (const char *, plugin_event_name, event_horizon);
385 plugin_callbacks = XRESIZEVEC (struct callback_info *,
386 plugin_callbacks, event_horizon);
387 }
388 /* All the pointers in the hash table will need to be updated. */
389 event_tab.dispose ();
390 }
391 else
392 *slot = &plugin_event_name[event_last];
393 plugin_event_name[event_last] = name;
394 return event_last++;
395 }
396
397 /* Called from the plugin's initialization code. Register a single callback.
398 This function can be called multiple times.
399
400 PLUGIN_NAME - display name for this plugin
401 EVENT - which event the callback is for
402 CALLBACK - the callback to be called at the event
403 USER_DATA - plugin-provided data */
404
405 void
406 register_callback (const char *plugin_name,
407 int event,
408 plugin_callback_func callback,
409 void *user_data)
410 {
411 switch (event)
412 {
413 case PLUGIN_PASS_MANAGER_SETUP:
414 gcc_assert (!callback);
415 register_pass ((struct register_pass_info *) user_data);
416 break;
417 case PLUGIN_INFO:
418 gcc_assert (!callback);
419 register_plugin_info (plugin_name, (struct plugin_info *) user_data);
420 break;
421 case PLUGIN_REGISTER_GGC_ROOTS:
422 gcc_assert (!callback);
423 ggc_register_root_tab ((const struct ggc_root_tab*) user_data);
424 break;
425 case PLUGIN_REGISTER_GGC_CACHES:
426 gcc_assert (!callback);
427 ggc_register_cache_tab ((const struct ggc_cache_tab*) user_data);
428 break;
429 case PLUGIN_EVENT_FIRST_DYNAMIC:
430 default:
431 if (event < PLUGIN_EVENT_FIRST_DYNAMIC || event >= event_last)
432 {
433 error ("unknown callback event registered by plugin %s",
434 plugin_name);
435 return;
436 }
437 /* Fall through. */
438 case PLUGIN_FINISH_TYPE:
439 case PLUGIN_FINISH_DECL:
440 case PLUGIN_START_UNIT:
441 case PLUGIN_FINISH_UNIT:
442 case PLUGIN_PRE_GENERICIZE:
443 case PLUGIN_GGC_START:
444 case PLUGIN_GGC_MARKING:
445 case PLUGIN_GGC_END:
446 case PLUGIN_ATTRIBUTES:
447 case PLUGIN_PRAGMAS:
448 case PLUGIN_FINISH:
449 case PLUGIN_ALL_PASSES_START:
450 case PLUGIN_ALL_PASSES_END:
451 case PLUGIN_ALL_IPA_PASSES_START:
452 case PLUGIN_ALL_IPA_PASSES_END:
453 case PLUGIN_OVERRIDE_GATE:
454 case PLUGIN_PASS_EXECUTION:
455 case PLUGIN_EARLY_GIMPLE_PASSES_START:
456 case PLUGIN_EARLY_GIMPLE_PASSES_END:
457 case PLUGIN_NEW_PASS:
458 {
459 struct callback_info *new_callback;
460 if (!callback)
461 {
462 error ("plugin %s registered a null callback function "
463 "for event %s", plugin_name, plugin_event_name[event]);
464 return;
465 }
466 new_callback = XNEW (struct callback_info);
467 new_callback->plugin_name = plugin_name;
468 new_callback->func = callback;
469 new_callback->user_data = user_data;
470 new_callback->next = plugin_callbacks[event];
471 plugin_callbacks[event] = new_callback;
472 }
473 break;
474 }
475 }
476
477 /* Remove a callback for EVENT which has been registered with for a plugin
478 PLUGIN_NAME. Return PLUGEVT_SUCCESS if a matching callback was
479 found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching
480 callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid. */
481 int
482 unregister_callback (const char *plugin_name, int event)
483 {
484 struct callback_info *callback, **cbp;
485
486 if (event >= event_last)
487 return PLUGEVT_NO_SUCH_EVENT;
488
489 for (cbp = &plugin_callbacks[event]; (callback = *cbp); cbp = &callback->next)
490 if (strcmp (callback->plugin_name, plugin_name) == 0)
491 {
492 *cbp = callback->next;
493 return PLUGEVT_SUCCESS;
494 }
495 return PLUGEVT_NO_CALLBACK;
496 }
497
498 /* Invoke all plugin callbacks registered with the specified event,
499 called from invoke_plugin_callbacks(). */
500
501 int
502 invoke_plugin_callbacks_full (int event, void *gcc_data)
503 {
504 int retval = PLUGEVT_SUCCESS;
505
506 timevar_push (TV_PLUGIN_RUN);
507
508 switch (event)
509 {
510 case PLUGIN_EVENT_FIRST_DYNAMIC:
511 default:
512 gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
513 gcc_assert (event < event_last);
514 /* Fall through. */
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 {
536 /* Iterate over every callback registered with this event and
537 call it. */
538 struct callback_info *callback = plugin_callbacks[event];
539
540 if (!callback)
541 retval = PLUGEVT_NO_CALLBACK;
542 for ( ; callback; callback = callback->next)
543 (*callback->func) (gcc_data, callback->user_data);
544 }
545 break;
546
547 case PLUGIN_PASS_MANAGER_SETUP:
548 case PLUGIN_REGISTER_GGC_ROOTS:
549 case PLUGIN_REGISTER_GGC_CACHES:
550 gcc_assert (false);
551 }
552
553 timevar_pop (TV_PLUGIN_RUN);
554 return retval;
555 }
556
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)
565
566 /* Try to initialize PLUGIN. Return true if successful. */
567
568 static bool
569 try_init_one_plugin (struct plugin_name_args *plugin)
570 {
571 void *dl_handle;
572 plugin_init_func plugin_init;
573 const char *err;
574 PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
575
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)
582 {
583 error ("cannot load plugin %s\n%s", plugin->full_name, dlerror ());
584 return false;
585 }
586
587 /* Clear any existing error. */
588 dlerror ();
589
590 /* Check the plugin license. */
591 if (dlsym (dl_handle, str_license) == NULL)
592 fatal_error ("plugin %s is not licensed under a GPL-compatible license\n"
593 "%s", plugin->full_name, dlerror ());
594
595 PTR_UNION_AS_VOID_PTR (plugin_init_union) =
596 dlsym (dl_handle, str_plugin_init_func_name);
597 plugin_init = PTR_UNION_AS_CAST_PTR (plugin_init_union);
598
599 if ((err = dlerror ()) != NULL)
600 {
601 error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
602 plugin->full_name, err);
603 return false;
604 }
605
606 /* Call the plugin-provided initialization routine with the arguments. */
607 if ((*plugin_init) (plugin, &gcc_version))
608 {
609 error ("fail to initialize plugin %s", plugin->full_name);
610 return false;
611 }
612
613 return true;
614 }
615
616
617 /* Routine to dlopen and initialize one plugin. This function is passed to
618 (and called by) the hash table traverse routine. Return 1 for the
619 htab_traverse to continue scan, 0 to stop.
620
621 SLOT - slot of the hash table element
622 INFO - auxiliary pointer handed to hash table traverse routine
623 (unused in this function) */
624
625 static int
626 init_one_plugin (void **slot, void * ARG_UNUSED (info))
627 {
628 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
629 bool ok = try_init_one_plugin (plugin);
630 if (!ok)
631 {
632 htab_remove_elt (plugin_name_args_tab, plugin->base_name);
633 XDELETE (plugin);
634 }
635 return 1;
636 }
637
638 #endif /* ENABLE_PLUGIN */
639
640 /* Main plugin initialization function. Called from compile_file() in
641 toplev.c. */
642
643 void
644 initialize_plugins (void)
645 {
646 /* If no plugin was specified in the command-line, simply return. */
647 if (!plugin_name_args_tab)
648 return;
649
650 timevar_push (TV_PLUGIN_INIT);
651
652 #ifdef ENABLE_PLUGIN
653 /* Traverse and initialize each plugin specified in the command-line. */
654 htab_traverse_noresize (plugin_name_args_tab, init_one_plugin, NULL);
655 #endif
656
657 timevar_pop (TV_PLUGIN_INIT);
658 }
659
660 /* Release memory used by one plugin. */
661
662 static int
663 finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
664 {
665 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
666 XDELETE (plugin);
667 return 1;
668 }
669
670 /* Free memory allocated by the plugin system. */
671
672 void
673 finalize_plugins (void)
674 {
675 if (!plugin_name_args_tab)
676 return;
677
678 /* We can now delete the plugin_name_args object as it will no longer
679 be used. Note that base_name and argv fields (both of which were also
680 dynamically allocated) are not freed as they could still be used by
681 the plugin code. */
682
683 htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
684
685 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
686 htab_delete (plugin_name_args_tab);
687 plugin_name_args_tab = NULL;
688 }
689
690 /* Used to pass options to htab_traverse callbacks. */
691
692 struct print_options
693 {
694 FILE *file;
695 const char *indent;
696 };
697
698 /* Print the version of one plugin. */
699
700 static int
701 print_version_one_plugin (void **slot, void *data)
702 {
703 struct print_options *opt = (struct print_options *) data;
704 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
705 const char *version = plugin->version ? plugin->version : "Unknown version.";
706
707 fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
708 return 1;
709 }
710
711 /* Print the version of each plugin. */
712
713 void
714 print_plugins_versions (FILE *file, const char *indent)
715 {
716 struct print_options opt;
717 opt.file = file;
718 opt.indent = indent;
719 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
720 return;
721
722 fprintf (file, "%sVersions of loaded plugins:\n", indent);
723 htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
724 }
725
726 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
727 argument to htab_traverse_noresize. */
728
729 static int
730 print_help_one_plugin (void **slot, void *data)
731 {
732 struct print_options *opt = (struct print_options *) data;
733 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
734 const char *help = plugin->help ? plugin->help : "No help available .";
735
736 char *dup = xstrdup (help);
737 char *p, *nl;
738 fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
739
740 for (p = nl = dup; nl; p = nl)
741 {
742 nl = strchr (nl, '\n');
743 if (nl)
744 {
745 *nl = '\0';
746 nl++;
747 }
748 fprintf (opt->file, " %s %s\n", opt->indent, p);
749 }
750
751 free (dup);
752 return 1;
753 }
754
755 /* Print help for each plugin. The output goes to FILE and every line starts
756 with INDENT. */
757
758 void
759 print_plugins_help (FILE *file, const char *indent)
760 {
761 struct print_options opt;
762 opt.file = file;
763 opt.indent = indent;
764 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
765 return;
766
767 fprintf (file, "%sHelp for the loaded plugins:\n", indent);
768 htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
769 }
770
771
772 /* Return true if plugins have been loaded. */
773
774 bool
775 plugins_active_p (void)
776 {
777 int event;
778
779 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
780 if (plugin_callbacks[event])
781 return true;
782
783 return false;
784 }
785
786
787 /* Dump to FILE the names and associated events for all the active
788 plugins. */
789
790 DEBUG_FUNCTION void
791 dump_active_plugins (FILE *file)
792 {
793 int event;
794
795 if (!plugins_active_p ())
796 return;
797
798 fprintf (file, FMT_FOR_PLUGIN_EVENT " | %s\n", _("Event"), _("Plugins"));
799 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
800 if (plugin_callbacks[event])
801 {
802 struct callback_info *ci;
803
804 fprintf (file, FMT_FOR_PLUGIN_EVENT " |", plugin_event_name[event]);
805
806 for (ci = plugin_callbacks[event]; ci; ci = ci->next)
807 fprintf (file, " %s", ci->plugin_name);
808
809 putc('\n', file);
810 }
811 }
812
813
814 /* Dump active plugins to stderr. */
815
816 DEBUG_FUNCTION void
817 debug_active_plugins (void)
818 {
819 dump_active_plugins (stderr);
820 }
821
822 /* Give a warning if plugins are present, before an ICE message asking
823 to submit a bug report. */
824
825 void
826 warn_if_plugins (void)
827 {
828 if (plugins_active_p ())
829 {
830 fnotice (stderr, "*** WARNING *** there are active plugins, do not report"
831 " this as a bug unless you can reproduce it without enabling"
832 " any plugins.\n");
833 dump_active_plugins (stderr);
834 }
835
836 }
837
838 /* Likewise, as a callback from the diagnostics code. */
839
840 void
841 plugins_internal_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
842 const char *msgid ATTRIBUTE_UNUSED,
843 va_list *ap ATTRIBUTE_UNUSED)
844 {
845 warn_if_plugins ();
846 }
847
848 /* The default version check. Compares every field in VERSION. */
849
850 bool
851 plugin_default_version_check (struct plugin_gcc_version *gcc_version,
852 struct plugin_gcc_version *plugin_version)
853 {
854 if (!gcc_version || !plugin_version)
855 return false;
856
857 if (strcmp (gcc_version->basever, plugin_version->basever))
858 return false;
859 if (strcmp (gcc_version->datestamp, plugin_version->datestamp))
860 return false;
861 if (strcmp (gcc_version->devphase, plugin_version->devphase))
862 return false;
863 if (strcmp (gcc_version->revision, plugin_version->revision))
864 return false;
865 if (strcmp (gcc_version->configuration_arguments,
866 plugin_version->configuration_arguments))
867 return false;
868 return true;
869 }
870
871
872 /* Return the current value of event_last, so that plugins which provide
873 additional functionality for events for the benefit of high-level plugins
874 know how many valid entries plugin_event_name holds. */
875
876 int
877 get_event_last (void)
878 {
879 return event_last;
880 }
881
882
883 /* Retrieve the default plugin directory. The gcc driver should have passed
884 it as -iplugindir <dir> to the cc1 program, and it is queriable through the
885 -print-file-name=plugin option to gcc. */
886 const char*
887 default_plugin_dir_name (void)
888 {
889 if (!plugindir_string)
890 fatal_error ("-iplugindir <dir> option not passed from the gcc driver");
891 return plugindir_string;
892 }