1 /* Support for GCC plugin mechanism.
2 Copyright (C) 2009-2023 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)
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. */
25 #include "coretypes.h"
27 #include "tree-pass.h"
28 #include "diagnostic-core.h"
34 #include "plugin-version.h"
38 #ifndef WIN32_LEAN_AND_MEAN
39 #define WIN32_LEAN_AND_MEAN
44 #define WIN32_LEAN_AND_MEAN
48 #define GCC_PLUGIN_STRINGIFY0(X) #X
49 #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X)
51 /* Event names as strings. Keep in sync with enum plugin_event. */
52 static const char *plugin_event_name_init
[] =
54 # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME),
55 # include "plugin.def"
59 /* A printf format large enough for the largest event above. */
60 #define FMT_FOR_PLUGIN_EVENT "%-32s"
62 const char **plugin_event_name
= plugin_event_name_init
;
64 /* Event hashtable helpers. */
66 struct event_hasher
: nofree_ptr_hash
<const char *>
68 static inline hashval_t
hash (const char **);
69 static inline bool equal (const char **, const char **);
72 /* Helper function for the event hash table that hashes the entry V. */
75 event_hasher::hash (const char **v
)
77 return htab_hash_string (*v
);
80 /* Helper function for the event hash table that compares the name of an
81 existing entry (S1) with the given string (S2). */
84 event_hasher::equal (const char **s1
, const char **s2
)
86 return !strcmp (*s1
, *s2
);
89 /* A hash table to map event names to the position of the names in the
90 plugin_event_name table. */
91 static hash_table
<event_hasher
> *event_tab
;
93 /* Keep track of the limit of allocated events and space ready for
95 static int event_last
= PLUGIN_EVENT_FIRST_DYNAMIC
;
96 static int event_horizon
= PLUGIN_EVENT_FIRST_DYNAMIC
;
98 /* Hash table for the plugin_name_args objects created during command-line
100 static htab_t plugin_name_args_tab
= NULL
;
102 /* List node for keeping track of plugin-registered callback. */
105 const char *plugin_name
; /* Name of plugin that registers the callback. */
106 plugin_callback_func func
; /* Callback to be called. */
107 void *user_data
; /* plugin-specified data. */
108 struct callback_info
*next
;
111 /* An array of lists of 'callback_info' objects indexed by the event id. */
112 static struct callback_info
*plugin_callbacks_init
[PLUGIN_EVENT_FIRST_DYNAMIC
];
113 static struct callback_info
**plugin_callbacks
= plugin_callbacks_init
;
115 /* For invoke_plugin_callbacks(), see plugin.h. */
116 bool flag_plugin_added
= false;
119 /* Each plugin should define an initialization function with exactly
121 static const char *str_plugin_init_func_name
= "plugin_init";
123 /* Each plugin should define this symbol to assert that it is
124 distributed under a GPL-compatible license. */
125 static const char *str_license
= "plugin_is_GPL_compatible";
128 /* Helper function for hashing the base_name of the plugin_name_args
129 structure to be inserted into the hash table. */
132 htab_hash_plugin (const void *p
)
134 const struct plugin_name_args
*plugin
= (const struct plugin_name_args
*) p
;
135 return htab_hash_string (plugin
->base_name
);
138 /* Helper function for the hash table that compares the base_name of the
139 existing entry (S1) with the given string (S2). */
142 htab_str_eq (const void *s1
, const void *s2
)
144 const struct plugin_name_args
*plugin
= (const struct plugin_name_args
*) s1
;
145 return !strcmp (plugin
->base_name
, (const char *) s2
);
149 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
153 get_plugin_base_name (const char *full_name
)
155 /* First get the base name part of the full-path name, i.e. NAME.so. */
156 char *base_name
= xstrdup (lbasename (full_name
));
158 /* Then get rid of the extension in the name, e.g., .so. */
159 strip_off_ending (base_name
, strlen (base_name
));
165 /* Create a plugin_name_args object for the given plugin and insert it
166 to the hash table. This function is called when
167 -fplugin=/path/to/NAME.so or -fplugin=NAME option is processed. */
170 add_new_plugin (const char* plugin_name
)
172 struct plugin_name_args
*plugin
;
178 flag_plugin_added
= true;
180 /* Replace short names by their full path when relevant. */
181 name_is_short
= !IS_ABSOLUTE_PATH (plugin_name
);
182 for (pc
= plugin_name
; name_is_short
&& *pc
; pc
++)
183 if (*pc
== '.' || IS_DIR_SEPARATOR (*pc
))
184 name_is_short
= false;
188 base_name
= CONST_CAST (char*, plugin_name
);
190 #if defined(__MINGW32__)
191 static const char plugin_ext
[] = ".dll";
192 #elif defined(__APPLE__)
193 /* macOS has two types of libraries: dynamic libraries (.dylib) and
194 plugins (.bundle). Both can be used with dlopen()/dlsym() but the
195 former cannot be linked at build time (i.e., with the -lfoo linker
196 option). A GCC plugin is therefore probably a macOS plugin but their
197 use seems to be quite rare and the .bundle extension is more of a
198 recommendation rather than the rule. This raises the questions of how
199 well they are supported by tools (e.g., libtool). So to avoid
200 complications let's use the .dylib extension for now. In the future,
201 if this proves to be an issue, we can always check for both
203 static const char plugin_ext
[] = ".dylib";
205 static const char plugin_ext
[] = ".so";
208 plugin_name
= concat (default_plugin_dir_name (), "/",
209 plugin_name
, plugin_ext
, NULL
);
210 if (access (plugin_name
, R_OK
))
213 "inaccessible plugin file %s expanded from short plugin name %s: %m",
214 plugin_name
, base_name
);
217 base_name
= get_plugin_base_name (plugin_name
);
219 /* If this is the first -fplugin= option we encounter, create
220 'plugin_name_args_tab' hash table. */
221 if (!plugin_name_args_tab
)
222 plugin_name_args_tab
= htab_create (10, htab_hash_plugin
, htab_str_eq
,
225 slot
= htab_find_slot_with_hash (plugin_name_args_tab
, base_name
,
226 htab_hash_string (base_name
), INSERT
);
228 /* If the same plugin (name) has been specified earlier, either emit an
229 error or a warning message depending on if they have identical full
233 plugin
= (struct plugin_name_args
*) *slot
;
234 if (strcmp (plugin
->full_name
, plugin_name
))
235 error ("plugin %qs was specified with different paths: %qs and %qs",
236 plugin
->base_name
, plugin
->full_name
, plugin_name
);
240 plugin
= XCNEW (struct plugin_name_args
);
241 plugin
->base_name
= base_name
;
242 plugin
->full_name
= plugin_name
;
248 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
249 'plugin_argument' object for the parsed key-value pair. ARG is
250 the <name>-<key>[=<value>] part of the option. */
253 parse_plugin_arg_opt (const char *arg
)
255 size_t len
= 0, name_len
= 0, key_len
= 0, value_len
= 0;
256 const char *ptr
, *name_start
= arg
, *key_start
= NULL
, *value_start
= NULL
;
257 char *name
, *key
, *value
;
259 bool name_parsed
= false, key_parsed
= false;
261 /* Iterate over the ARG string and identify the starting character position
262 of 'name', 'key', and 'value' and their lengths. */
263 for (ptr
= arg
; *ptr
; ++ptr
)
265 /* Only the first '-' encountered is considered a separator between
266 'name' and 'key'. All the subsequent '-'s are considered part of
267 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
268 the plugin name is 'foo' and the key is 'bar-primary-key'. */
269 if (*ptr
== '-' && !name_parsed
)
277 else if (*ptr
== '=')
283 value_start
= ptr
+ 1;
294 error ("malformed option %<-fplugin-arg-%s%>: "
295 "missing %<-<key>[=<value>]%>",
300 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
301 Otherwise, it is the VALUE_LEN. */
307 name
= XNEWVEC (char, name_len
+ 1);
308 strncpy (name
, name_start
, name_len
);
309 name
[name_len
] = '\0';
311 /* Check if the named plugin has already been specified earlier in the
313 if (plugin_name_args_tab
314 && ((slot
= htab_find_slot_with_hash (plugin_name_args_tab
, name
,
315 htab_hash_string (name
), NO_INSERT
))
318 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
320 key
= XNEWVEC (char, key_len
+ 1);
321 strncpy (key
, key_start
, key_len
);
325 value
= XNEWVEC (char, value_len
+ 1);
326 strncpy (value
, value_start
, value_len
);
327 value
[value_len
] = '\0';
332 /* Create a plugin_argument object for the parsed key-value pair.
333 If there are already arguments for this plugin, we will need to
334 adjust the argument array size by creating a new array and deleting
335 the old one. If the performance ever becomes an issue, we can
336 change the code by pre-allocating a larger array first. */
337 if (plugin
->argc
> 0)
339 struct plugin_argument
*args
= XNEWVEC (struct plugin_argument
,
341 memcpy (args
, plugin
->argv
,
342 sizeof (struct plugin_argument
) * plugin
->argc
);
343 XDELETEVEC (plugin
->argv
);
349 gcc_assert (plugin
->argv
== NULL
);
350 plugin
->argv
= XNEWVEC (struct plugin_argument
, 1);
354 plugin
->argv
[plugin
->argc
- 1].key
= key
;
355 plugin
->argv
[plugin
->argc
- 1].value
= value
;
358 error ("plugin %s should be specified before %<-fplugin-arg-%s%> "
359 "in the command line", name
, arg
);
361 /* We don't need the plugin's name anymore. Just release it. */
365 /* Register additional plugin information. NAME is the name passed to
366 plugin_init. INFO is the information that should be registered. */
369 register_plugin_info (const char* name
, struct plugin_info
*info
)
371 void **slot
= htab_find_slot_with_hash (plugin_name_args_tab
, name
,
372 htab_hash_string (name
), NO_INSERT
);
373 struct plugin_name_args
*plugin
;
377 error ("unable to register info for plugin %qs - plugin name not found",
381 plugin
= (struct plugin_name_args
*) *slot
;
382 plugin
->version
= info
->version
;
383 plugin
->help
= info
->help
;
386 /* Look up the event id for NAME. If the name is not found, return -1
387 if INSERT is NO_INSERT. */
390 get_named_event_id (const char *name
, enum insert_option insert
)
398 event_tab
= new hash_table
<event_hasher
> (150);
399 for (i
= 0; i
< event_last
; i
++)
401 slot
= event_tab
->find_slot (&plugin_event_name
[i
], INSERT
);
402 gcc_assert (*slot
== HTAB_EMPTY_ENTRY
);
403 *slot
= &plugin_event_name
[i
];
406 slot
= event_tab
->find_slot (&name
, insert
);
409 if (*slot
!= HTAB_EMPTY_ENTRY
)
410 return *slot
- &plugin_event_name
[0];
412 if (event_last
>= event_horizon
)
414 event_horizon
= event_last
* 2;
415 if (plugin_event_name
== plugin_event_name_init
)
417 plugin_event_name
= XNEWVEC (const char *, event_horizon
);
418 memcpy (plugin_event_name
, plugin_event_name_init
,
419 sizeof plugin_event_name_init
);
420 plugin_callbacks
= XNEWVEC (struct callback_info
*, event_horizon
);
421 memcpy (plugin_callbacks
, plugin_callbacks_init
,
422 sizeof plugin_callbacks_init
);
427 = XRESIZEVEC (const char *, plugin_event_name
, event_horizon
);
428 plugin_callbacks
= XRESIZEVEC (struct callback_info
*,
429 plugin_callbacks
, event_horizon
);
431 /* All the pointers in the hash table will need to be updated. */
436 *slot
= &plugin_event_name
[event_last
];
437 plugin_event_name
[event_last
] = name
;
441 /* Called from the plugin's initialization code. Register a single callback.
442 This function can be called multiple times.
444 PLUGIN_NAME - display name for this plugin
445 EVENT - which event the callback is for
446 CALLBACK - the callback to be called at the event
447 USER_DATA - plugin-provided data */
450 register_callback (const char *plugin_name
,
452 plugin_callback_func callback
,
457 case PLUGIN_PASS_MANAGER_SETUP
:
458 gcc_assert (!callback
);
459 register_pass ((struct register_pass_info
*) user_data
);
462 gcc_assert (!callback
);
463 register_plugin_info (plugin_name
, (struct plugin_info
*) user_data
);
465 case PLUGIN_REGISTER_GGC_ROOTS
:
466 gcc_assert (!callback
);
467 ggc_register_root_tab ((const struct ggc_root_tab
*) user_data
);
469 case PLUGIN_EVENT_FIRST_DYNAMIC
:
471 if (event
< PLUGIN_EVENT_FIRST_DYNAMIC
|| event
>= event_last
)
473 error ("unknown callback event registered by plugin %s",
478 case PLUGIN_START_PARSE_FUNCTION
:
479 case PLUGIN_FINISH_PARSE_FUNCTION
:
480 case PLUGIN_FINISH_TYPE
:
481 case PLUGIN_FINISH_DECL
:
482 case PLUGIN_START_UNIT
:
483 case PLUGIN_FINISH_UNIT
:
484 case PLUGIN_PRE_GENERICIZE
:
485 case PLUGIN_GGC_START
:
486 case PLUGIN_GGC_MARKING
:
488 case PLUGIN_ATTRIBUTES
:
491 case PLUGIN_ALL_PASSES_START
:
492 case PLUGIN_ALL_PASSES_END
:
493 case PLUGIN_ALL_IPA_PASSES_START
:
494 case PLUGIN_ALL_IPA_PASSES_END
:
495 case PLUGIN_OVERRIDE_GATE
:
496 case PLUGIN_PASS_EXECUTION
:
497 case PLUGIN_EARLY_GIMPLE_PASSES_START
:
498 case PLUGIN_EARLY_GIMPLE_PASSES_END
:
499 case PLUGIN_NEW_PASS
:
500 case PLUGIN_INCLUDE_FILE
:
501 case PLUGIN_ANALYZER_INIT
:
503 struct callback_info
*new_callback
;
506 error ("plugin %s registered a null callback function "
507 "for event %s", plugin_name
, plugin_event_name
[event
]);
510 new_callback
= XNEW (struct callback_info
);
511 new_callback
->plugin_name
= plugin_name
;
512 new_callback
->func
= callback
;
513 new_callback
->user_data
= user_data
;
514 new_callback
->next
= plugin_callbacks
[event
];
515 plugin_callbacks
[event
] = new_callback
;
521 /* Remove a callback for EVENT which has been registered with for a plugin
522 PLUGIN_NAME. Return PLUGEVT_SUCCESS if a matching callback was
523 found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching
524 callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid. */
526 unregister_callback (const char *plugin_name
, int event
)
528 struct callback_info
*callback
, **cbp
;
530 if (event
>= event_last
)
531 return PLUGEVT_NO_SUCH_EVENT
;
533 for (cbp
= &plugin_callbacks
[event
]; (callback
= *cbp
); cbp
= &callback
->next
)
534 if (strcmp (callback
->plugin_name
, plugin_name
) == 0)
536 *cbp
= callback
->next
;
537 return PLUGEVT_SUCCESS
;
539 return PLUGEVT_NO_CALLBACK
;
542 /* Invoke all plugin callbacks registered with the specified event,
543 called from invoke_plugin_callbacks(). */
546 invoke_plugin_callbacks_full (int event
, void *gcc_data
)
548 int retval
= PLUGEVT_SUCCESS
;
550 timevar_push (TV_PLUGIN_RUN
);
554 case PLUGIN_EVENT_FIRST_DYNAMIC
:
556 gcc_assert (event
>= PLUGIN_EVENT_FIRST_DYNAMIC
);
557 gcc_assert (event
< event_last
);
559 case PLUGIN_START_PARSE_FUNCTION
:
560 case PLUGIN_FINISH_PARSE_FUNCTION
:
561 case PLUGIN_FINISH_TYPE
:
562 case PLUGIN_FINISH_DECL
:
563 case PLUGIN_START_UNIT
:
564 case PLUGIN_FINISH_UNIT
:
565 case PLUGIN_PRE_GENERICIZE
:
566 case PLUGIN_ATTRIBUTES
:
569 case PLUGIN_GGC_START
:
570 case PLUGIN_GGC_MARKING
:
572 case PLUGIN_ALL_PASSES_START
:
573 case PLUGIN_ALL_PASSES_END
:
574 case PLUGIN_ALL_IPA_PASSES_START
:
575 case PLUGIN_ALL_IPA_PASSES_END
:
576 case PLUGIN_OVERRIDE_GATE
:
577 case PLUGIN_PASS_EXECUTION
:
578 case PLUGIN_EARLY_GIMPLE_PASSES_START
:
579 case PLUGIN_EARLY_GIMPLE_PASSES_END
:
580 case PLUGIN_NEW_PASS
:
581 case PLUGIN_INCLUDE_FILE
:
582 case PLUGIN_ANALYZER_INIT
:
584 /* Iterate over every callback registered with this event and
586 struct callback_info
*callback
= plugin_callbacks
[event
];
589 retval
= PLUGEVT_NO_CALLBACK
;
590 for ( ; callback
; callback
= callback
->next
)
591 (*callback
->func
) (gcc_data
, callback
->user_data
);
595 case PLUGIN_PASS_MANAGER_SETUP
:
596 case PLUGIN_REGISTER_GGC_ROOTS
:
600 timevar_pop (TV_PLUGIN_RUN
);
606 /* Try to initialize PLUGIN. Return true if successful. */
610 // Return a message string for last error or NULL if unknown. Must be freed
616 return FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER
|
617 FORMAT_MESSAGE_FROM_SYSTEM
|
618 FORMAT_MESSAGE_IGNORE_INSERTS
|
619 FORMAT_MESSAGE_MAX_WIDTH_MASK
,
622 MAKELANGID (LANG_NEUTRAL
, SUBLANG_DEFAULT
),
631 try_init_one_plugin (struct plugin_name_args
*plugin
)
634 plugin_init_func plugin_init
;
636 dl_handle
= LoadLibrary (plugin
->full_name
);
639 char *err
= win32_error_msg ();
640 error ("cannot load plugin %s\n%s", plugin
->full_name
, err
);
645 /* Check the plugin license. Unlike the name suggests, GetProcAddress()
646 can be used for both functions and variables. */
647 if (GetProcAddress (dl_handle
, str_license
) == NULL
)
649 char *err
= win32_error_msg ();
650 fatal_error (input_location
,
651 "plugin %s is not licensed under a GPL-compatible license\n"
652 "%s", plugin
->full_name
, err
);
655 /* Unlike dlsym(), GetProcAddress() returns a pointer to a function so we
656 can cast directly without union tricks. */
657 plugin_init
= (plugin_init_func
)
658 GetProcAddress (dl_handle
, str_plugin_init_func_name
);
660 if (plugin_init
== NULL
)
662 char *err
= win32_error_msg ();
663 FreeLibrary (dl_handle
);
664 error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name
,
665 plugin
->full_name
, err
);
670 /* Call the plugin-provided initialization routine with the arguments. */
671 if ((*plugin_init
) (plugin
, &gcc_version
))
673 FreeLibrary (dl_handle
);
674 error ("fail to initialize plugin %s", plugin
->full_name
);
677 /* Leak dl_handle on purpose to ensure the plugin is loaded for the
678 entire run of the compiler. */
682 #else // POSIX-like with dlopen()/dlsym().
684 /* We need a union to cast dlsym return value to a function pointer
685 as ISO C forbids assignment between function pointer and 'void *'.
686 Use explicit union instead of __extension__(<union_cast>) for
688 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
689 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
690 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
693 try_init_one_plugin (struct plugin_name_args
*plugin
)
696 plugin_init_func plugin_init
;
698 PTR_UNION_TYPE (plugin_init_func
) plugin_init_union
;
700 /* We use RTLD_NOW to accelerate binding and detect any mismatch
701 between the API expected by the plugin and the GCC API; we use
702 RTLD_GLOBAL which is useful to plugins which themselves call
704 dl_handle
= dlopen (plugin
->full_name
, RTLD_NOW
| RTLD_GLOBAL
);
707 error ("cannot load plugin %s: %s", plugin
->full_name
, dlerror ());
711 /* Clear any existing error. */
714 /* Check the plugin license. */
715 if (dlsym (dl_handle
, str_license
) == NULL
)
716 fatal_error (input_location
,
717 "plugin %s is not licensed under a GPL-compatible license"
718 " %s", plugin
->full_name
, dlerror ());
720 PTR_UNION_AS_VOID_PTR (plugin_init_union
)
721 = dlsym (dl_handle
, str_plugin_init_func_name
);
722 plugin_init
= PTR_UNION_AS_CAST_PTR (plugin_init_union
);
724 if ((err
= dlerror ()) != NULL
)
727 error ("cannot find %s in plugin %s: %s", str_plugin_init_func_name
,
728 plugin
->full_name
, err
);
732 /* Call the plugin-provided initialization routine with the arguments. */
733 if ((*plugin_init
) (plugin
, &gcc_version
))
736 error ("failed to initialize plugin %s", plugin
->full_name
);
739 /* leak dl_handle on purpose to ensure the plugin is loaded for the
740 entire run of the compiler. */
745 /* Routine to dlopen and initialize one plugin. This function is passed to
746 (and called by) the hash table traverse routine. Return 1 for the
747 htab_traverse to continue scan, 0 to stop.
749 SLOT - slot of the hash table element
750 INFO - auxiliary pointer handed to hash table traverse routine
751 (unused in this function) */
754 init_one_plugin (void **slot
, void * ARG_UNUSED (info
))
756 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
757 bool ok
= try_init_one_plugin (plugin
);
760 htab_remove_elt_with_hash (plugin_name_args_tab
, plugin
->base_name
,
761 htab_hash_string (plugin
->base_name
));
767 #endif /* ENABLE_PLUGIN */
769 /* Main plugin initialization function. Called from compile_file() in
773 initialize_plugins (void)
775 /* If no plugin was specified in the command-line, simply return. */
776 if (!plugin_name_args_tab
)
779 timevar_push (TV_PLUGIN_INIT
);
782 /* Traverse and initialize each plugin specified in the command-line. */
783 htab_traverse_noresize (plugin_name_args_tab
, init_one_plugin
, NULL
);
786 timevar_pop (TV_PLUGIN_INIT
);
789 /* Release memory used by one plugin. */
792 finalize_one_plugin (void **slot
, void * ARG_UNUSED (info
))
794 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
799 /* Free memory allocated by the plugin system. */
802 finalize_plugins (void)
804 if (!plugin_name_args_tab
)
807 /* We can now delete the plugin_name_args object as it will no longer
808 be used. Note that base_name and argv fields (both of which were also
809 dynamically allocated) are not freed as they could still be used by
812 htab_traverse_noresize (plugin_name_args_tab
, finalize_one_plugin
, NULL
);
814 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
815 htab_delete (plugin_name_args_tab
);
816 plugin_name_args_tab
= NULL
;
819 /* Implementation detail of for_each_plugin. */
821 struct for_each_plugin_closure
823 void (*cb
) (const plugin_name_args
*,
828 /* Implementation detail of for_each_plugin: callback for htab_traverse_noresize
829 that calls the user-provided callback. */
832 for_each_plugin_cb (void **slot
, void *info
)
834 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
835 for_each_plugin_closure
*c
= (for_each_plugin_closure
*)info
;
836 c
->cb (plugin
, c
->user_data
);
840 /* Call CB with USER_DATA on each plugin. */
843 for_each_plugin (void (*cb
) (const plugin_name_args
*,
847 if (!plugin_name_args_tab
)
850 for_each_plugin_closure c
;
852 c
.user_data
= user_data
;
854 htab_traverse_noresize (plugin_name_args_tab
, for_each_plugin_cb
, &c
);
857 /* Used to pass options to htab_traverse callbacks. */
865 /* Print the version of one plugin. */
868 print_version_one_plugin (void **slot
, void *data
)
870 struct print_options
*opt
= (struct print_options
*) data
;
871 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
872 const char *version
= plugin
->version
? plugin
->version
: "Unknown version.";
874 fprintf (opt
->file
, " %s%s: %s\n", opt
->indent
, plugin
->base_name
, version
);
878 /* Print the version of each plugin. */
881 print_plugins_versions (FILE *file
, const char *indent
)
883 struct print_options opt
;
886 if (!plugin_name_args_tab
|| htab_elements (plugin_name_args_tab
) == 0)
889 fprintf (file
, "%sVersions of loaded plugins:\n", indent
);
890 htab_traverse_noresize (plugin_name_args_tab
, print_version_one_plugin
, &opt
);
893 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
894 argument to htab_traverse_noresize. */
897 print_help_one_plugin (void **slot
, void *data
)
899 struct print_options
*opt
= (struct print_options
*) data
;
900 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
901 const char *help
= plugin
->help
? plugin
->help
: "No help available .";
903 char *dup
= xstrdup (help
);
905 fprintf (opt
->file
, " %s%s:\n", opt
->indent
, plugin
->base_name
);
907 for (p
= nl
= dup
; nl
; p
= nl
)
909 nl
= strchr (nl
, '\n');
915 fprintf (opt
->file
, " %s %s\n", opt
->indent
, p
);
922 /* Print help for each plugin. The output goes to FILE and every line starts
926 print_plugins_help (FILE *file
, const char *indent
)
928 struct print_options opt
;
931 if (!plugin_name_args_tab
|| htab_elements (plugin_name_args_tab
) == 0)
934 fprintf (file
, "%sHelp for the loaded plugins:\n", indent
);
935 htab_traverse_noresize (plugin_name_args_tab
, print_help_one_plugin
, &opt
);
939 /* Return true if plugins have been loaded. */
942 plugins_active_p (void)
946 for (event
= PLUGIN_PASS_MANAGER_SETUP
; event
< event_last
; event
++)
947 if (plugin_callbacks
[event
])
954 /* Dump to FILE the names and associated events for all the active
958 dump_active_plugins (FILE *file
)
962 if (!plugins_active_p ())
965 fprintf (file
, FMT_FOR_PLUGIN_EVENT
" | %s\n", _("Event"), _("Plugins"));
966 for (event
= PLUGIN_PASS_MANAGER_SETUP
; event
< event_last
; event
++)
967 if (plugin_callbacks
[event
])
969 struct callback_info
*ci
;
971 fprintf (file
, FMT_FOR_PLUGIN_EVENT
" |", plugin_event_name
[event
]);
973 for (ci
= plugin_callbacks
[event
]; ci
; ci
= ci
->next
)
974 fprintf (file
, " %s", ci
->plugin_name
);
981 /* Dump active plugins to stderr. */
984 debug_active_plugins (void)
986 dump_active_plugins (stderr
);
989 /* Give a warning if plugins are present, before an ICE message asking
990 to submit a bug report. */
993 warn_if_plugins (void)
995 if (plugins_active_p ())
997 fnotice (stderr
, "*** WARNING *** there are active plugins, do not report"
998 " this as a bug unless you can reproduce it without enabling"
1000 dump_active_plugins (stderr
);
1005 /* The default version check. Compares every field in VERSION. */
1008 plugin_default_version_check (struct plugin_gcc_version
*gcc_version
,
1009 struct plugin_gcc_version
*plugin_version
)
1011 if (!gcc_version
|| !plugin_version
)
1014 if (strcmp (gcc_version
->basever
, plugin_version
->basever
))
1016 if (strcmp (gcc_version
->datestamp
, plugin_version
->datestamp
))
1018 if (strcmp (gcc_version
->devphase
, plugin_version
->devphase
))
1020 if (strcmp (gcc_version
->revision
, plugin_version
->revision
))
1022 if (strcmp (gcc_version
->configuration_arguments
,
1023 plugin_version
->configuration_arguments
))
1029 /* Return the current value of event_last, so that plugins which provide
1030 additional functionality for events for the benefit of high-level plugins
1031 know how many valid entries plugin_event_name holds. */
1034 get_event_last (void)
1040 /* Retrieve the default plugin directory. The gcc driver should have passed
1041 it as -iplugindir <dir> to the cc1 program, and it is queriable through the
1042 -print-file-name=plugin option to gcc. */
1044 default_plugin_dir_name (void)
1046 if (!plugindir_string
)
1047 fatal_error (input_location
,
1048 "%<-iplugindir%> option not passed from the gcc driver");
1049 return plugindir_string
;