re PR testsuite/79026 (The tests changed by revision r244006 now fail on darwin)
[gcc.git] / gcc / plugin.c
1 /* Support for GCC plugin mechanism.
2 Copyright (C) 2009-2017 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 "options.h"
27 #include "tree-pass.h"
28 #include "diagnostic-core.h"
29 #include "flags.h"
30 #include "intl.h"
31 #include "plugin.h"
32
33 #ifdef ENABLE_PLUGIN
34 #include "plugin-version.h"
35 #endif
36
37 #define GCC_PLUGIN_STRINGIFY0(X) #X
38 #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X)
39
40 /* Event names as strings. Keep in sync with enum plugin_event. */
41 static const char *plugin_event_name_init[] =
42 {
43 # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME),
44 # include "plugin.def"
45 # undef DEFEVENT
46 };
47
48 /* A printf format large enough for the largest event above. */
49 #define FMT_FOR_PLUGIN_EVENT "%-32s"
50
51 const char **plugin_event_name = plugin_event_name_init;
52
53 /* Event hashtable helpers. */
54
55 struct event_hasher : nofree_ptr_hash <const char *>
56 {
57 static inline hashval_t hash (const char **);
58 static inline bool equal (const char **, const char **);
59 };
60
61 /* Helper function for the event hash table that hashes the entry V. */
62
63 inline hashval_t
64 event_hasher::hash (const char **v)
65 {
66 return htab_hash_string (*v);
67 }
68
69 /* Helper function for the event hash table that compares the name of an
70 existing entry (S1) with the given string (S2). */
71
72 inline bool
73 event_hasher::equal (const char **s1, const char **s2)
74 {
75 return !strcmp (*s1, *s2);
76 }
77
78 /* A hash table to map event names to the position of the names in the
79 plugin_event_name table. */
80 static hash_table<event_hasher> *event_tab;
81
82 /* Keep track of the limit of allocated events and space ready for
83 allocating events. */
84 static int event_last = PLUGIN_EVENT_FIRST_DYNAMIC;
85 static int event_horizon = PLUGIN_EVENT_FIRST_DYNAMIC;
86
87 /* Hash table for the plugin_name_args objects created during command-line
88 parsing. */
89 static htab_t plugin_name_args_tab = NULL;
90
91 /* List node for keeping track of plugin-registered callback. */
92 struct callback_info
93 {
94 const char *plugin_name; /* Name of plugin that registers the callback. */
95 plugin_callback_func func; /* Callback to be called. */
96 void *user_data; /* plugin-specified data. */
97 struct callback_info *next;
98 };
99
100 /* An array of lists of 'callback_info' objects indexed by the event id. */
101 static struct callback_info *plugin_callbacks_init[PLUGIN_EVENT_FIRST_DYNAMIC];
102 static struct callback_info **plugin_callbacks = plugin_callbacks_init;
103
104 /* For invoke_plugin_callbacks(), see plugin.h. */
105 bool flag_plugin_added = false;
106
107 #ifdef ENABLE_PLUGIN
108 /* Each plugin should define an initialization function with exactly
109 this name. */
110 static const char *str_plugin_init_func_name = "plugin_init";
111
112 /* Each plugin should define this symbol to assert that it is
113 distributed under a GPL-compatible license. */
114 static const char *str_license = "plugin_is_GPL_compatible";
115 #endif
116
117 /* Helper function for the hash table that compares the base_name of the
118 existing entry (S1) with the given string (S2). */
119
120 static int
121 htab_str_eq (const void *s1, const void *s2)
122 {
123 const struct plugin_name_args *plugin = (const struct plugin_name_args *) s1;
124 return !strcmp (plugin->base_name, (const char *) s2);
125 }
126
127
128 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
129 return NAME. */
130
131 static char *
132 get_plugin_base_name (const char *full_name)
133 {
134 /* First get the base name part of the full-path name, i.e. NAME.so. */
135 char *base_name = xstrdup (lbasename (full_name));
136
137 /* Then get rid of '.so' part of the name. */
138 strip_off_ending (base_name, strlen (base_name));
139
140 return base_name;
141 }
142
143
144 /* Create a plugin_name_args object for the given plugin and insert it
145 to the hash table. This function is called when
146 -fplugin=/path/to/NAME.so or -fplugin=NAME option is processed. */
147
148 void
149 add_new_plugin (const char* plugin_name)
150 {
151 struct plugin_name_args *plugin;
152 void **slot;
153 char *base_name;
154 bool name_is_short;
155 const char *pc;
156
157 flag_plugin_added = true;
158
159 /* Replace short names by their full path when relevant. */
160 name_is_short = !IS_ABSOLUTE_PATH (plugin_name);
161 for (pc = plugin_name; name_is_short && *pc; pc++)
162 if (*pc == '.' || IS_DIR_SEPARATOR (*pc))
163 name_is_short = false;
164
165 if (name_is_short)
166 {
167 base_name = CONST_CAST (char*, plugin_name);
168 /* FIXME: the ".so" suffix is currently builtin, since plugins
169 only work on ELF host systems like e.g. Linux or Solaris.
170 When plugins shall be available on non ELF systems such as
171 Windows or MacOS, this code has to be greatly improved. */
172 plugin_name = concat (default_plugin_dir_name (), "/",
173 plugin_name, ".so", NULL);
174 if (access (plugin_name, R_OK))
175 fatal_error
176 (input_location,
177 "inaccessible plugin file %s expanded from short plugin name %s: %m",
178 plugin_name, base_name);
179 }
180 else
181 base_name = get_plugin_base_name (plugin_name);
182
183 /* If this is the first -fplugin= option we encounter, create
184 'plugin_name_args_tab' hash table. */
185 if (!plugin_name_args_tab)
186 plugin_name_args_tab = htab_create (10, htab_hash_string, htab_str_eq,
187 NULL);
188
189 slot = htab_find_slot (plugin_name_args_tab, base_name, INSERT);
190
191 /* If the same plugin (name) has been specified earlier, either emit an
192 error or a warning message depending on if they have identical full
193 (path) names. */
194 if (*slot)
195 {
196 plugin = (struct plugin_name_args *) *slot;
197 if (strcmp (plugin->full_name, plugin_name))
198 error ("plugin %s was specified with different paths:\n%s\n%s",
199 plugin->base_name, plugin->full_name, plugin_name);
200 return;
201 }
202
203 plugin = XCNEW (struct plugin_name_args);
204 plugin->base_name = base_name;
205 plugin->full_name = plugin_name;
206
207 *slot = plugin;
208 }
209
210
211 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
212 'plugin_argument' object for the parsed key-value pair. ARG is
213 the <name>-<key>[=<value>] part of the option. */
214
215 void
216 parse_plugin_arg_opt (const char *arg)
217 {
218 size_t len = 0, name_len = 0, key_len = 0, value_len = 0;
219 const char *ptr, *name_start = arg, *key_start = NULL, *value_start = NULL;
220 char *name, *key, *value;
221 void **slot;
222 bool name_parsed = false, key_parsed = false;
223
224 /* Iterate over the ARG string and identify the starting character position
225 of 'name', 'key', and 'value' and their lengths. */
226 for (ptr = arg; *ptr; ++ptr)
227 {
228 /* Only the first '-' encountered is considered a separator between
229 'name' and 'key'. All the subsequent '-'s are considered part of
230 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
231 the plugin name is 'foo' and the key is 'bar-primary-key'. */
232 if (*ptr == '-' && !name_parsed)
233 {
234 name_len = len;
235 len = 0;
236 key_start = ptr + 1;
237 name_parsed = true;
238 continue;
239 }
240 else if (*ptr == '=')
241 {
242 if (!key_parsed)
243 {
244 key_len = len;
245 len = 0;
246 value_start = ptr + 1;
247 key_parsed = true;
248 }
249 continue;
250 }
251 else
252 ++len;
253 }
254
255 if (!key_start)
256 {
257 error ("malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
258 arg);
259 return;
260 }
261
262 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
263 Otherwise, it is the VALUE_LEN. */
264 if (!value_start)
265 key_len = len;
266 else
267 value_len = len;
268
269 name = XNEWVEC (char, name_len + 1);
270 strncpy (name, name_start, name_len);
271 name[name_len] = '\0';
272
273 /* Check if the named plugin has already been specified earlier in the
274 command-line. */
275 if (plugin_name_args_tab
276 && ((slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT))
277 != NULL))
278 {
279 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
280
281 key = XNEWVEC (char, key_len + 1);
282 strncpy (key, key_start, key_len);
283 key[key_len] = '\0';
284 if (value_start)
285 {
286 value = XNEWVEC (char, value_len + 1);
287 strncpy (value, value_start, value_len);
288 value[value_len] = '\0';
289 }
290 else
291 value = NULL;
292
293 /* Create a plugin_argument object for the parsed key-value pair.
294 If there are already arguments for this plugin, we will need to
295 adjust the argument array size by creating a new array and deleting
296 the old one. If the performance ever becomes an issue, we can
297 change the code by pre-allocating a larger array first. */
298 if (plugin->argc > 0)
299 {
300 struct plugin_argument *args = XNEWVEC (struct plugin_argument,
301 plugin->argc + 1);
302 memcpy (args, plugin->argv,
303 sizeof (struct plugin_argument) * plugin->argc);
304 XDELETEVEC (plugin->argv);
305 plugin->argv = args;
306 ++plugin->argc;
307 }
308 else
309 {
310 gcc_assert (plugin->argv == NULL);
311 plugin->argv = XNEWVEC (struct plugin_argument, 1);
312 plugin->argc = 1;
313 }
314
315 plugin->argv[plugin->argc - 1].key = key;
316 plugin->argv[plugin->argc - 1].value = value;
317 }
318 else
319 error ("plugin %s should be specified before -fplugin-arg-%s "
320 "in the command line", name, arg);
321
322 /* We don't need the plugin's name anymore. Just release it. */
323 XDELETEVEC (name);
324 }
325
326 /* Register additional plugin information. NAME is the name passed to
327 plugin_init. INFO is the information that should be registered. */
328
329 static void
330 register_plugin_info (const char* name, struct plugin_info *info)
331 {
332 void **slot = htab_find_slot (plugin_name_args_tab, name, NO_INSERT);
333 struct plugin_name_args *plugin;
334
335 if (slot == NULL)
336 {
337 error ("unable to register info for plugin '%s' - plugin name not found",
338 name);
339 return;
340 }
341 plugin = (struct plugin_name_args *) *slot;
342 plugin->version = info->version;
343 plugin->help = info->help;
344 }
345
346 /* Look up the event id for NAME. If the name is not found, return -1
347 if INSERT is NO_INSERT. */
348
349 int
350 get_named_event_id (const char *name, enum insert_option insert)
351 {
352 const char ***slot;
353
354 if (!event_tab)
355 {
356 int i;
357
358 event_tab = new hash_table<event_hasher> (150);
359 for (i = 0; i < event_last; i++)
360 {
361 slot = event_tab->find_slot (&plugin_event_name[i], INSERT);
362 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
363 *slot = &plugin_event_name[i];
364 }
365 }
366 slot = event_tab->find_slot (&name, insert);
367 if (slot == NULL)
368 return -1;
369 if (*slot != HTAB_EMPTY_ENTRY)
370 return *slot - &plugin_event_name[0];
371
372 if (event_last >= event_horizon)
373 {
374 event_horizon = event_last * 2;
375 if (plugin_event_name == plugin_event_name_init)
376 {
377 plugin_event_name = XNEWVEC (const char *, event_horizon);
378 memcpy (plugin_event_name, plugin_event_name_init,
379 sizeof plugin_event_name_init);
380 plugin_callbacks = XNEWVEC (struct callback_info *, event_horizon);
381 memcpy (plugin_callbacks, plugin_callbacks_init,
382 sizeof plugin_callbacks_init);
383 }
384 else
385 {
386 plugin_event_name
387 = XRESIZEVEC (const char *, plugin_event_name, event_horizon);
388 plugin_callbacks = XRESIZEVEC (struct callback_info *,
389 plugin_callbacks, event_horizon);
390 }
391 /* All the pointers in the hash table will need to be updated. */
392 delete event_tab;
393 event_tab = NULL;
394 }
395 else
396 *slot = &plugin_event_name[event_last];
397 plugin_event_name[event_last] = name;
398 return event_last++;
399 }
400
401 /* Called from the plugin's initialization code. Register a single callback.
402 This function can be called multiple times.
403
404 PLUGIN_NAME - display name for this plugin
405 EVENT - which event the callback is for
406 CALLBACK - the callback to be called at the event
407 USER_DATA - plugin-provided data */
408
409 void
410 register_callback (const char *plugin_name,
411 int event,
412 plugin_callback_func callback,
413 void *user_data)
414 {
415 switch (event)
416 {
417 case PLUGIN_PASS_MANAGER_SETUP:
418 gcc_assert (!callback);
419 register_pass ((struct register_pass_info *) user_data);
420 break;
421 case PLUGIN_INFO:
422 gcc_assert (!callback);
423 register_plugin_info (plugin_name, (struct plugin_info *) user_data);
424 break;
425 case PLUGIN_REGISTER_GGC_ROOTS:
426 gcc_assert (!callback);
427 ggc_register_root_tab ((const struct ggc_root_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_START_PARSE_FUNCTION:
439 case PLUGIN_FINISH_PARSE_FUNCTION:
440 case PLUGIN_FINISH_TYPE:
441 case PLUGIN_FINISH_DECL:
442 case PLUGIN_START_UNIT:
443 case PLUGIN_FINISH_UNIT:
444 case PLUGIN_PRE_GENERICIZE:
445 case PLUGIN_GGC_START:
446 case PLUGIN_GGC_MARKING:
447 case PLUGIN_GGC_END:
448 case PLUGIN_ATTRIBUTES:
449 case PLUGIN_PRAGMAS:
450 case PLUGIN_FINISH:
451 case PLUGIN_ALL_PASSES_START:
452 case PLUGIN_ALL_PASSES_END:
453 case PLUGIN_ALL_IPA_PASSES_START:
454 case PLUGIN_ALL_IPA_PASSES_END:
455 case PLUGIN_OVERRIDE_GATE:
456 case PLUGIN_PASS_EXECUTION:
457 case PLUGIN_EARLY_GIMPLE_PASSES_START:
458 case PLUGIN_EARLY_GIMPLE_PASSES_END:
459 case PLUGIN_NEW_PASS:
460 case PLUGIN_INCLUDE_FILE:
461 {
462 struct callback_info *new_callback;
463 if (!callback)
464 {
465 error ("plugin %s registered a null callback function "
466 "for event %s", plugin_name, plugin_event_name[event]);
467 return;
468 }
469 new_callback = XNEW (struct callback_info);
470 new_callback->plugin_name = plugin_name;
471 new_callback->func = callback;
472 new_callback->user_data = user_data;
473 new_callback->next = plugin_callbacks[event];
474 plugin_callbacks[event] = new_callback;
475 }
476 break;
477 }
478 }
479
480 /* Remove a callback for EVENT which has been registered with for a plugin
481 PLUGIN_NAME. Return PLUGEVT_SUCCESS if a matching callback was
482 found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching
483 callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid. */
484 int
485 unregister_callback (const char *plugin_name, int event)
486 {
487 struct callback_info *callback, **cbp;
488
489 if (event >= event_last)
490 return PLUGEVT_NO_SUCH_EVENT;
491
492 for (cbp = &plugin_callbacks[event]; (callback = *cbp); cbp = &callback->next)
493 if (strcmp (callback->plugin_name, plugin_name) == 0)
494 {
495 *cbp = callback->next;
496 return PLUGEVT_SUCCESS;
497 }
498 return PLUGEVT_NO_CALLBACK;
499 }
500
501 /* Invoke all plugin callbacks registered with the specified event,
502 called from invoke_plugin_callbacks(). */
503
504 int
505 invoke_plugin_callbacks_full (int event, void *gcc_data)
506 {
507 int retval = PLUGEVT_SUCCESS;
508
509 timevar_push (TV_PLUGIN_RUN);
510
511 switch (event)
512 {
513 case PLUGIN_EVENT_FIRST_DYNAMIC:
514 default:
515 gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
516 gcc_assert (event < event_last);
517 /* Fall through. */
518 case PLUGIN_START_PARSE_FUNCTION:
519 case PLUGIN_FINISH_PARSE_FUNCTION:
520 case PLUGIN_FINISH_TYPE:
521 case PLUGIN_FINISH_DECL:
522 case PLUGIN_START_UNIT:
523 case PLUGIN_FINISH_UNIT:
524 case PLUGIN_PRE_GENERICIZE:
525 case PLUGIN_ATTRIBUTES:
526 case PLUGIN_PRAGMAS:
527 case PLUGIN_FINISH:
528 case PLUGIN_GGC_START:
529 case PLUGIN_GGC_MARKING:
530 case PLUGIN_GGC_END:
531 case PLUGIN_ALL_PASSES_START:
532 case PLUGIN_ALL_PASSES_END:
533 case PLUGIN_ALL_IPA_PASSES_START:
534 case PLUGIN_ALL_IPA_PASSES_END:
535 case PLUGIN_OVERRIDE_GATE:
536 case PLUGIN_PASS_EXECUTION:
537 case PLUGIN_EARLY_GIMPLE_PASSES_START:
538 case PLUGIN_EARLY_GIMPLE_PASSES_END:
539 case PLUGIN_NEW_PASS:
540 case PLUGIN_INCLUDE_FILE:
541 {
542 /* Iterate over every callback registered with this event and
543 call it. */
544 struct callback_info *callback = plugin_callbacks[event];
545
546 if (!callback)
547 retval = PLUGEVT_NO_CALLBACK;
548 for ( ; callback; callback = callback->next)
549 (*callback->func) (gcc_data, callback->user_data);
550 }
551 break;
552
553 case PLUGIN_PASS_MANAGER_SETUP:
554 case PLUGIN_REGISTER_GGC_ROOTS:
555 gcc_assert (false);
556 }
557
558 timevar_pop (TV_PLUGIN_RUN);
559 return retval;
560 }
561
562 #ifdef ENABLE_PLUGIN
563 /* We need a union to cast dlsym return value to a function pointer
564 as ISO C forbids assignment between function pointer and 'void *'.
565 Use explicit union instead of __extension__(<union_cast>) for
566 portability. */
567 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
568 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
569 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
570
571 /* Try to initialize PLUGIN. Return true if successful. */
572
573 static bool
574 try_init_one_plugin (struct plugin_name_args *plugin)
575 {
576 void *dl_handle;
577 plugin_init_func plugin_init;
578 const char *err;
579 PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
580
581 /* We use RTLD_NOW to accelerate binding and detect any mismatch
582 between the API expected by the plugin and the GCC API; we use
583 RTLD_GLOBAL which is useful to plugins which themselves call
584 dlopen. */
585 dl_handle = dlopen (plugin->full_name, RTLD_NOW | RTLD_GLOBAL);
586 if (!dl_handle)
587 {
588 error ("cannot load plugin %s\n%s", plugin->full_name, dlerror ());
589 return false;
590 }
591
592 /* Clear any existing error. */
593 dlerror ();
594
595 /* Check the plugin license. */
596 if (dlsym (dl_handle, str_license) == NULL)
597 fatal_error (input_location,
598 "plugin %s is not licensed under a GPL-compatible license\n"
599 "%s", plugin->full_name, dlerror ());
600
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);
604
605 if ((err = dlerror ()) != NULL)
606 {
607 error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
608 plugin->full_name, err);
609 return false;
610 }
611
612 /* Call the plugin-provided initialization routine with the arguments. */
613 if ((*plugin_init) (plugin, &gcc_version))
614 {
615 error ("fail to initialize plugin %s", plugin->full_name);
616 return false;
617 }
618
619 return true;
620 }
621
622
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.
626
627 SLOT - slot of the hash table element
628 INFO - auxiliary pointer handed to hash table traverse routine
629 (unused in this function) */
630
631 static int
632 init_one_plugin (void **slot, void * ARG_UNUSED (info))
633 {
634 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
635 bool ok = try_init_one_plugin (plugin);
636 if (!ok)
637 {
638 htab_remove_elt (plugin_name_args_tab, plugin->base_name);
639 XDELETE (plugin);
640 }
641 return 1;
642 }
643
644 #endif /* ENABLE_PLUGIN */
645
646 /* Main plugin initialization function. Called from compile_file() in
647 toplev.c. */
648
649 void
650 initialize_plugins (void)
651 {
652 /* If no plugin was specified in the command-line, simply return. */
653 if (!plugin_name_args_tab)
654 return;
655
656 timevar_push (TV_PLUGIN_INIT);
657
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
662
663 timevar_pop (TV_PLUGIN_INIT);
664 }
665
666 /* Release memory used by one plugin. */
667
668 static int
669 finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
670 {
671 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
672 XDELETE (plugin);
673 return 1;
674 }
675
676 /* Free memory allocated by the plugin system. */
677
678 void
679 finalize_plugins (void)
680 {
681 if (!plugin_name_args_tab)
682 return;
683
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. */
688
689 htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
690
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;
694 }
695
696 /* Used to pass options to htab_traverse callbacks. */
697
698 struct print_options
699 {
700 FILE *file;
701 const char *indent;
702 };
703
704 /* Print the version of one plugin. */
705
706 static int
707 print_version_one_plugin (void **slot, void *data)
708 {
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.";
712
713 fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
714 return 1;
715 }
716
717 /* Print the version of each plugin. */
718
719 void
720 print_plugins_versions (FILE *file, const char *indent)
721 {
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;
727
728 fprintf (file, "%sVersions of loaded plugins:\n", indent);
729 htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
730 }
731
732 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
733 argument to htab_traverse_noresize. */
734
735 static int
736 print_help_one_plugin (void **slot, void *data)
737 {
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 .";
741
742 char *dup = xstrdup (help);
743 char *p, *nl;
744 fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
745
746 for (p = nl = dup; nl; p = nl)
747 {
748 nl = strchr (nl, '\n');
749 if (nl)
750 {
751 *nl = '\0';
752 nl++;
753 }
754 fprintf (opt->file, " %s %s\n", opt->indent, p);
755 }
756
757 free (dup);
758 return 1;
759 }
760
761 /* Print help for each plugin. The output goes to FILE and every line starts
762 with INDENT. */
763
764 void
765 print_plugins_help (FILE *file, const char *indent)
766 {
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;
772
773 fprintf (file, "%sHelp for the loaded plugins:\n", indent);
774 htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
775 }
776
777
778 /* Return true if plugins have been loaded. */
779
780 bool
781 plugins_active_p (void)
782 {
783 int event;
784
785 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
786 if (plugin_callbacks[event])
787 return true;
788
789 return false;
790 }
791
792
793 /* Dump to FILE the names and associated events for all the active
794 plugins. */
795
796 DEBUG_FUNCTION void
797 dump_active_plugins (FILE *file)
798 {
799 int event;
800
801 if (!plugins_active_p ())
802 return;
803
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])
807 {
808 struct callback_info *ci;
809
810 fprintf (file, FMT_FOR_PLUGIN_EVENT " |", plugin_event_name[event]);
811
812 for (ci = plugin_callbacks[event]; ci; ci = ci->next)
813 fprintf (file, " %s", ci->plugin_name);
814
815 putc ('\n', file);
816 }
817 }
818
819
820 /* Dump active plugins to stderr. */
821
822 DEBUG_FUNCTION void
823 debug_active_plugins (void)
824 {
825 dump_active_plugins (stderr);
826 }
827
828 /* Give a warning if plugins are present, before an ICE message asking
829 to submit a bug report. */
830
831 void
832 warn_if_plugins (void)
833 {
834 if (plugins_active_p ())
835 {
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);
840 }
841
842 }
843
844 /* Likewise, as a callback from the diagnostics code. */
845
846 void
847 plugins_internal_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
848 const char *msgid ATTRIBUTE_UNUSED,
849 va_list *ap ATTRIBUTE_UNUSED)
850 {
851 warn_if_plugins ();
852 }
853
854 /* The default version check. Compares every field in VERSION. */
855
856 bool
857 plugin_default_version_check (struct plugin_gcc_version *gcc_version,
858 struct plugin_gcc_version *plugin_version)
859 {
860 if (!gcc_version || !plugin_version)
861 return false;
862
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;
875 }
876
877
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. */
881
882 int
883 get_event_last (void)
884 {
885 return event_last;
886 }
887
888
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)
894 {
895 if (!plugindir_string)
896 fatal_error (input_location,
897 "-iplugindir <dir> option not passed from the gcc driver");
898 return plugindir_string;
899 }