re PR target/65697 (__atomic memory barriers not strong enough for __sync builtins)
[gcc.git] / gcc / plugin.c
1 /* Support for GCC plugin mechanism.
2 Copyright (C) 2009-2015 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 "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"
35
36 #ifdef ENABLE_PLUGIN
37 #include "plugin-version.h"
38 #endif
39
40 #define GCC_PLUGIN_STRINGIFY0(X) #X
41 #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X)
42
43 /* Event names as strings. Keep in sync with enum plugin_event. */
44 static const char *plugin_event_name_init[] =
45 {
46 # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME),
47 # include "plugin.def"
48 # undef DEFEVENT
49 };
50
51 /* A printf format large enough for the largest event above. */
52 #define FMT_FOR_PLUGIN_EVENT "%-32s"
53
54 const char **plugin_event_name = plugin_event_name_init;
55
56 /* Event hashtable helpers. */
57
58 struct event_hasher : nofree_ptr_hash <const char *>
59 {
60 static inline hashval_t hash (const char **);
61 static inline bool equal (const char **, const char **);
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 char **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 char **s1, const char **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 (input_location,
180 "inaccessible plugin file %s expanded from short plugin name %s: %m",
181 plugin_name, base_name);
182 }
183 else
184 base_name = get_plugin_base_name (plugin_name);
185
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);
191
192 slot = htab_find_slot (plugin_name_args_tab, base_name, INSERT);
193
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)
198 {
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;
204 }
205
206 plugin = XCNEW (struct plugin_name_args);
207 plugin->base_name = base_name;
208 plugin->full_name = plugin_name;
209
210 *slot = plugin;
211 }
212
213
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. */
217
218 void
219 parse_plugin_arg_opt (const char *arg)
220 {
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;
226
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)
230 {
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)
236 {
237 name_len = len;
238 len = 0;
239 key_start = ptr + 1;
240 name_parsed = true;
241 continue;
242 }
243 else if (*ptr == '=')
244 {
245 if (!key_parsed)
246 {
247 key_len = len;
248 len = 0;
249 value_start = ptr + 1;
250 key_parsed = true;
251 }
252 continue;
253 }
254 else
255 ++len;
256 }
257
258 if (!key_start)
259 {
260 error ("malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
261 arg);
262 return;
263 }
264
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;
271
272 name = XNEWVEC (char, name_len + 1);
273 strncpy (name, name_start, name_len);
274 name[name_len] = '\0';
275
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))
281 {
282 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
283
284 key = XNEWVEC (char, key_len + 1);
285 strncpy (key, key_start, key_len);
286 key[key_len] = '\0';
287 if (value_start)
288 {
289 value = XNEWVEC (char, value_len + 1);
290 strncpy (value, value_start, value_len);
291 value[value_len] = '\0';
292 }
293 else
294 value = NULL;
295
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)
302 {
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;
310 }
311 else
312 {
313 gcc_assert (plugin->argv == NULL);
314 plugin->argv = XNEWVEC (struct plugin_argument, 1);
315 plugin->argc = 1;
316 }
317
318 plugin->argv[plugin->argc - 1].key = key;
319 plugin->argv[plugin->argc - 1].value = value;
320 }
321 else
322 error ("plugin %s should be specified before -fplugin-arg-%s "
323 "in the command line", name, arg);
324
325 /* We don't need the plugin's name anymore. Just release it. */
326 XDELETEVEC (name);
327 }
328
329 /* Register additional plugin information. NAME is the name passed to
330 plugin_init. INFO is the information that should be registered. */
331
332 static void
333 register_plugin_info (const char* name, struct plugin_info *info)
334 {
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;
339 }
340
341 /* Look up the event id for NAME. If the name is not found, return -1
342 if INSERT is NO_INSERT. */
343
344 int
345 get_named_event_id (const char *name, enum insert_option insert)
346 {
347 const char ***slot;
348
349 if (!event_tab)
350 {
351 int i;
352
353 event_tab = new hash_table<event_hasher> (150);
354 for (i = 0; i < event_last; i++)
355 {
356 slot = event_tab->find_slot (&plugin_event_name[i], INSERT);
357 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
358 *slot = &plugin_event_name[i];
359 }
360 }
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];
366
367 if (event_last >= event_horizon)
368 {
369 event_horizon = event_last * 2;
370 if (plugin_event_name == plugin_event_name_init)
371 {
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);
378 }
379 else
380 {
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);
385 }
386 /* All the pointers in the hash table will need to be updated. */
387 delete event_tab;
388 event_tab = NULL;
389 }
390 else
391 *slot = &plugin_event_name[event_last];
392 plugin_event_name[event_last] = name;
393 return event_last++;
394 }
395
396 /* Called from the plugin's initialization code. Register a single callback.
397 This function can be called multiple times.
398
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 */
403
404 void
405 register_callback (const char *plugin_name,
406 int event,
407 plugin_callback_func callback,
408 void *user_data)
409 {
410 switch (event)
411 {
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)
427 {
428 error ("unknown callback event registered by plugin %s",
429 plugin_name);
430 return;
431 }
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:
456 {
457 struct callback_info *new_callback;
458 if (!callback)
459 {
460 error ("plugin %s registered a null callback function "
461 "for event %s", plugin_name, plugin_event_name[event]);
462 return;
463 }
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;
470 }
471 break;
472 }
473 }
474
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. */
479 int
480 unregister_callback (const char *plugin_name, int event)
481 {
482 struct callback_info *callback, **cbp;
483
484 if (event >= event_last)
485 return PLUGEVT_NO_SUCH_EVENT;
486
487 for (cbp = &plugin_callbacks[event]; (callback = *cbp); cbp = &callback->next)
488 if (strcmp (callback->plugin_name, plugin_name) == 0)
489 {
490 *cbp = callback->next;
491 return PLUGEVT_SUCCESS;
492 }
493 return PLUGEVT_NO_CALLBACK;
494 }
495
496 /* Invoke all plugin callbacks registered with the specified event,
497 called from invoke_plugin_callbacks(). */
498
499 int
500 invoke_plugin_callbacks_full (int event, void *gcc_data)
501 {
502 int retval = PLUGEVT_SUCCESS;
503
504 timevar_push (TV_PLUGIN_RUN);
505
506 switch (event)
507 {
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:
536 {
537 /* Iterate over every callback registered with this event and
538 call it. */
539 struct callback_info *callback = plugin_callbacks[event];
540
541 if (!callback)
542 retval = PLUGEVT_NO_CALLBACK;
543 for ( ; callback; callback = callback->next)
544 (*callback->func) (gcc_data, callback->user_data);
545 }
546 break;
547
548 case PLUGIN_PASS_MANAGER_SETUP:
549 case PLUGIN_REGISTER_GGC_ROOTS:
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 (input_location,
593 "plugin %s is not licensed under a GPL-compatible license\n"
594 "%s", plugin->full_name, dlerror ());
595
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);
599
600 if ((err = dlerror ()) != NULL)
601 {
602 error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
603 plugin->full_name, err);
604 return false;
605 }
606
607 /* Call the plugin-provided initialization routine with the arguments. */
608 if ((*plugin_init) (plugin, &gcc_version))
609 {
610 error ("fail to initialize plugin %s", plugin->full_name);
611 return false;
612 }
613
614 return true;
615 }
616
617
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.
621
622 SLOT - slot of the hash table element
623 INFO - auxiliary pointer handed to hash table traverse routine
624 (unused in this function) */
625
626 static int
627 init_one_plugin (void **slot, void * ARG_UNUSED (info))
628 {
629 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
630 bool ok = try_init_one_plugin (plugin);
631 if (!ok)
632 {
633 htab_remove_elt (plugin_name_args_tab, plugin->base_name);
634 XDELETE (plugin);
635 }
636 return 1;
637 }
638
639 #endif /* ENABLE_PLUGIN */
640
641 /* Main plugin initialization function. Called from compile_file() in
642 toplev.c. */
643
644 void
645 initialize_plugins (void)
646 {
647 /* If no plugin was specified in the command-line, simply return. */
648 if (!plugin_name_args_tab)
649 return;
650
651 timevar_push (TV_PLUGIN_INIT);
652
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
657
658 timevar_pop (TV_PLUGIN_INIT);
659 }
660
661 /* Release memory used by one plugin. */
662
663 static int
664 finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
665 {
666 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
667 XDELETE (plugin);
668 return 1;
669 }
670
671 /* Free memory allocated by the plugin system. */
672
673 void
674 finalize_plugins (void)
675 {
676 if (!plugin_name_args_tab)
677 return;
678
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. */
683
684 htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
685
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;
689 }
690
691 /* Used to pass options to htab_traverse callbacks. */
692
693 struct print_options
694 {
695 FILE *file;
696 const char *indent;
697 };
698
699 /* Print the version of one plugin. */
700
701 static int
702 print_version_one_plugin (void **slot, void *data)
703 {
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.";
707
708 fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
709 return 1;
710 }
711
712 /* Print the version of each plugin. */
713
714 void
715 print_plugins_versions (FILE *file, const char *indent)
716 {
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;
722
723 fprintf (file, "%sVersions of loaded plugins:\n", indent);
724 htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
725 }
726
727 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
728 argument to htab_traverse_noresize. */
729
730 static int
731 print_help_one_plugin (void **slot, void *data)
732 {
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 .";
736
737 char *dup = xstrdup (help);
738 char *p, *nl;
739 fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
740
741 for (p = nl = dup; nl; p = nl)
742 {
743 nl = strchr (nl, '\n');
744 if (nl)
745 {
746 *nl = '\0';
747 nl++;
748 }
749 fprintf (opt->file, " %s %s\n", opt->indent, p);
750 }
751
752 free (dup);
753 return 1;
754 }
755
756 /* Print help for each plugin. The output goes to FILE and every line starts
757 with INDENT. */
758
759 void
760 print_plugins_help (FILE *file, const char *indent)
761 {
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;
767
768 fprintf (file, "%sHelp for the loaded plugins:\n", indent);
769 htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
770 }
771
772
773 /* Return true if plugins have been loaded. */
774
775 bool
776 plugins_active_p (void)
777 {
778 int event;
779
780 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
781 if (plugin_callbacks[event])
782 return true;
783
784 return false;
785 }
786
787
788 /* Dump to FILE the names and associated events for all the active
789 plugins. */
790
791 DEBUG_FUNCTION void
792 dump_active_plugins (FILE *file)
793 {
794 int event;
795
796 if (!plugins_active_p ())
797 return;
798
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])
802 {
803 struct callback_info *ci;
804
805 fprintf (file, FMT_FOR_PLUGIN_EVENT " |", plugin_event_name[event]);
806
807 for (ci = plugin_callbacks[event]; ci; ci = ci->next)
808 fprintf (file, " %s", ci->plugin_name);
809
810 putc ('\n', file);
811 }
812 }
813
814
815 /* Dump active plugins to stderr. */
816
817 DEBUG_FUNCTION void
818 debug_active_plugins (void)
819 {
820 dump_active_plugins (stderr);
821 }
822
823 /* Give a warning if plugins are present, before an ICE message asking
824 to submit a bug report. */
825
826 void
827 warn_if_plugins (void)
828 {
829 if (plugins_active_p ())
830 {
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);
835 }
836
837 }
838
839 /* Likewise, as a callback from the diagnostics code. */
840
841 void
842 plugins_internal_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
843 const char *msgid ATTRIBUTE_UNUSED,
844 va_list *ap ATTRIBUTE_UNUSED)
845 {
846 warn_if_plugins ();
847 }
848
849 /* The default version check. Compares every field in VERSION. */
850
851 bool
852 plugin_default_version_check (struct plugin_gcc_version *gcc_version,
853 struct plugin_gcc_version *plugin_version)
854 {
855 if (!gcc_version || !plugin_version)
856 return false;
857
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;
870 }
871
872
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. */
876
877 int
878 get_event_last (void)
879 {
880 return event_last;
881 }
882
883
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)
889 {
890 if (!plugindir_string)
891 fatal_error (input_location,
892 "-iplugindir <dir> option not passed from the gcc driver");
893 return plugindir_string;
894 }