GCC Middle and Back End API Reference
plugin.cc File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "options.h"
#include "tree-pass.h"
#include "diagnostic-core.h"
#include "flags.h"
#include "intl.h"
#include "plugin.h"
#include "plugin.def"
Include dependency graph for plugin.cc:

Data Structures

struct  event_hasher
struct  callback_info
struct  for_each_plugin_closure
struct  print_options


#define FMT_FOR_PLUGIN_EVENT   "%-32s"


static hashval_t htab_hash_plugin (const void *p)
static int htab_str_eq (const void *s1, const void *s2)
static char * get_plugin_base_name (const char *full_name)
void add_new_plugin (const char *plugin_name)
void parse_plugin_arg_opt (const char *arg)
static void register_plugin_info (const char *name, struct plugin_info *info)
int get_named_event_id (const char *name, enum insert_option insert)
void register_callback (const char *plugin_name, int event, plugin_callback_func callback, void *user_data)
int unregister_callback (const char *plugin_name, int event)
int invoke_plugin_callbacks_full (int event, void *gcc_data)
void initialize_plugins (void)
static int finalize_one_plugin (void **slot, void *ARG_UNUSED(info))
void finalize_plugins (void)
static int for_each_plugin_cb (void **slot, void *info)
void for_each_plugin (void(*cb)(const plugin_name_args *, void *user_data), void *user_data)
static int print_version_one_plugin (void **slot, void *data)
void print_plugins_versions (FILE *file, const char *indent)
static int print_help_one_plugin (void **slot, void *data)
void print_plugins_help (FILE *file, const char *indent)
bool plugins_active_p (void)
DEBUG_FUNCTION void dump_active_plugins (FILE *file)
DEBUG_FUNCTION void debug_active_plugins (void)
void warn_if_plugins (void)
bool plugin_default_version_check (struct plugin_gcc_version *gcc_version, struct plugin_gcc_version *plugin_version)
int get_event_last (void)
const char * default_plugin_dir_name (void)


static const char * plugin_event_name_init []
const char ** plugin_event_name = plugin_event_name_init
static hash_table< event_hasher > * event_tab
static int event_last = PLUGIN_EVENT_FIRST_DYNAMIC
static int event_horizon = PLUGIN_EVENT_FIRST_DYNAMIC
static htab_t plugin_name_args_tab = NULL
static struct callback_infoplugin_callbacks_init [PLUGIN_EVENT_FIRST_DYNAMIC]
static struct callback_info ** plugin_callbacks = plugin_callbacks_init
bool flag_plugin_added = false

Macro Definition Documentation




#define FMT_FOR_PLUGIN_EVENT   "%-32s"
A printf format large enough for the largest event above.   

Referenced by dump_active_plugins().


#define GCC_PLUGIN_STRINGIFY0 ( X)    #X
Support for GCC plugin mechanism.
   Copyright (C) 2009-2024 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
This file contains the support for GCC plugin mechanism based on the
APIs described in doc/plugin.texi.   



Function Documentation

◆ add_new_plugin()

void add_new_plugin ( const char * plugin_name)
Create a plugin_name_args object for the given plugin and insert it
to the hash table. This function is called when
-fplugin=/path/to/NAME.so or -fplugin=NAME option is processed.   

References plugin_name_args::base_name, CONST_CAST, default_plugin_dir_name(), error(), fatal_error(), flag_plugin_added, plugin_name_args::full_name, get_plugin_base_name(), htab_hash_plugin(), htab_str_eq(), input_location, NULL, plugin_name_args_tab, and R_OK.

Referenced by handle_common_deferred_options().

◆ debug_active_plugins()

DEBUG_FUNCTION void debug_active_plugins ( void )
Dump active plugins to stderr.   

References dump_active_plugins().

◆ default_plugin_dir_name()

const char * default_plugin_dir_name ( void )
Retrieve the default plugin directory.  The gcc driver should have passed
it as -iplugindir <dir> to the cc1 program, and it is queriable through the
-print-file-name=plugin option to gcc.   

References fatal_error(), and input_location.

Referenced by add_new_plugin().

◆ dump_active_plugins()

DEBUG_FUNCTION void dump_active_plugins ( FILE * file)
Dump to FILE the names and associated events for all the active

References _, event_last, print_options::file, FMT_FOR_PLUGIN_EVENT, callback_info::next, plugin_callbacks, plugin_event_name, callback_info::plugin_name, and plugins_active_p().

Referenced by debug_active_plugins(), and warn_if_plugins().

◆ finalize_one_plugin()

static int finalize_one_plugin ( void ** slot,
void * ARG_UNUSEDinfo )
Release memory used by one plugin.  

Referenced by finalize_plugins().

◆ finalize_plugins()

void finalize_plugins ( void )
Free memory allocated by the plugin system.  

References finalize_one_plugin(), NULL, and plugin_name_args_tab.

Referenced by toplev::main().

◆ for_each_plugin()

void for_each_plugin ( void(* cb )(const plugin_name_args *, void *user_data),
void * user_data )

◆ for_each_plugin_cb()

static int for_each_plugin_cb ( void ** slot,
void * info )
Implementation detail of for_each_plugin: callback for htab_traverse_noresize
that calls the user-provided callback.   

References for_each_plugin_closure::cb, and for_each_plugin_closure::user_data.

Referenced by for_each_plugin().

◆ get_event_last()

int get_event_last ( void )
Return the current value of event_last, so that plugins which provide
additional functionality for events for the benefit of high-level plugins
know how many valid entries plugin_event_name holds.   

References event_last.

◆ get_named_event_id()

int get_named_event_id ( const char * name,
enum insert_option insert )
Look up the event id for NAME.  If the name is not found, return -1

References event_horizon, event_last, event_tab, gcc_assert, i, insert(), NULL, plugin_callbacks, plugin_callbacks_init, plugin_event_name, and plugin_event_name_init.

◆ get_plugin_base_name()

static char * get_plugin_base_name ( const char * full_name)
Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
return NAME.   

References plugin_name_args::base_name, plugin_name_args::full_name, and strip_off_ending().

Referenced by add_new_plugin().

◆ htab_hash_plugin()

static hashval_t htab_hash_plugin ( const void * p)
Helper function for hashing the base_name of the plugin_name_args
structure to be inserted into the hash table.   

References plugin_name_args::base_name.

Referenced by add_new_plugin().

◆ htab_str_eq()

static int htab_str_eq ( const void * s1,
const void * s2 )
Helper function for the hash table that compares the base_name of the
existing entry (S1) with the given string (S2).   

References plugin_name_args::base_name.

Referenced by add_new_plugin().

◆ initialize_plugins()

void initialize_plugins ( void )
Main plugin initialization function.  Called from compile_file() in

References NULL, plugin_name_args_tab, timevar_pop(), and timevar_push().

Referenced by toplev::main().

◆ invoke_plugin_callbacks_full()

int invoke_plugin_callbacks_full ( int event,
void * gcc_data )
Invoke all plugin callbacks registered with the specified event,
called from invoke_plugin_callbacks().   

References event_last, callback_info::func, gcc_assert, callback_info::next, PLUGEVT_NO_CALLBACK, PLUGEVT_SUCCESS, plugin_callbacks, PLUGIN_EVENT_FIRST_DYNAMIC, timevar_pop(), timevar_push(), and callback_info::user_data.

Referenced by invoke_plugin_callbacks().

◆ parse_plugin_arg_opt()

void parse_plugin_arg_opt ( const char * arg)
Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
'plugin_argument' object for the parsed key-value pair. ARG is
the <name>-<key>[=<value>] part of the option.   

References plugin_name_args::argc, plugin_name_args::argv, error(), gcc_assert, plugin_argument::key, NULL, plugin_name_args_tab, and plugin_argument::value.

Referenced by handle_common_deferred_options().

◆ plugin_default_version_check()

bool plugin_default_version_check ( struct plugin_gcc_version * gcc_version,
struct plugin_gcc_version * plugin_version )

◆ plugins_active_p()

bool plugins_active_p ( void )
Return true if plugins have been loaded.   

References event_last, and plugin_callbacks.

Referenced by dump_active_plugins(), and warn_if_plugins().

◆ print_help_one_plugin()

static int print_help_one_plugin ( void ** slot,
void * data )
Print help for one plugin. SLOT is the hash table slot. DATA is the
argument to htab_traverse_noresize.  

References plugin_name_args::base_name, print_options::file, free(), plugin_name_args::help, and print_options::indent.

Referenced by print_plugins_help().

◆ print_plugins_help()

void print_plugins_help ( FILE * file,
const char * indent )
Print help for each plugin. The output goes to FILE and every line starts
with INDENT.  

References print_options::file, print_options::indent, plugin_name_args_tab, and print_help_one_plugin().

Referenced by toplev::main().

◆ print_plugins_versions()

void print_plugins_versions ( FILE * file,
const char * indent )
Print the version of each plugin.  

References print_options::file, print_options::indent, plugin_name_args_tab, and print_version_one_plugin().

Referenced by print_version().

◆ print_version_one_plugin()

static int print_version_one_plugin ( void ** slot,
void * data )

◆ register_callback()

void register_callback ( const char * plugin_name,
int event,
plugin_callback_func callback,
void * user_data )
Called from the plugin's initialization code. Register a single callback.
This function can be called multiple times.

PLUGIN_NAME - display name for this plugin
EVENT       - which event the callback is for
CALLBACK    - the callback to be called at the event
USER_DATA   - plugin-provided data    

References error(), event_last, callback_info::func, gcc_assert, ggc_register_root_tab(), callback_info::next, plugin_callbacks, PLUGIN_EVENT_FIRST_DYNAMIC, plugin_event_name, callback_info::plugin_name, register_pass(), register_plugin_info(), and callback_info::user_data.

◆ register_plugin_info()

static void register_plugin_info ( const char * name,
struct plugin_info * info )
Register additional plugin information. NAME is the name passed to
plugin_init. INFO is the information that should be registered.  

References error(), plugin_info::help, plugin_name_args::help, NULL, plugin_name_args_tab, plugin_info::version, and plugin_name_args::version.

Referenced by register_callback().

◆ unregister_callback()

int unregister_callback ( const char * plugin_name,
int event )
Remove a callback for EVENT which has been registered with for a plugin
PLUGIN_NAME.  Return PLUGEVT_SUCCESS if a matching callback was
found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching
callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid.   

References event_last, callback_info::next, PLUGEVT_NO_CALLBACK, PLUGEVT_NO_SUCH_EVENT, PLUGEVT_SUCCESS, plugin_callbacks, and callback_info::plugin_name.

◆ warn_if_plugins()

void warn_if_plugins ( void )
Give a warning if plugins are present, before an ICE message asking
to submit a bug report.   

References dump_active_plugins(), fnotice(), and plugins_active_p().

Referenced by internal_error_function().

Variable Documentation

◆ event_horizon

int event_horizon = PLUGIN_EVENT_FIRST_DYNAMIC

Referenced by get_named_event_id().

◆ event_last

Keep track of the limit of allocated events and space ready for
allocating events.   

Referenced by dump_active_plugins(), get_event_last(), get_named_event_id(), invoke_plugin_callbacks_full(), plugins_active_p(), register_callback(), and unregister_callback().

◆ event_tab

hash_table<event_hasher>* event_tab
A hash table to map event names to the position of the names in the
plugin_event_name table.   

Referenced by get_named_event_id().

◆ flag_plugin_added

bool flag_plugin_added = false
For invoke_plugin_callbacks(), see plugin.h.   

Referenced by add_new_plugin(), and invoke_plugin_callbacks().

◆ plugin_callbacks

◆ plugin_callbacks_init

struct callback_info* plugin_callbacks_init[PLUGIN_EVENT_FIRST_DYNAMIC]
An array of lists of 'callback_info' objects indexed by the event id.   

Referenced by get_named_event_id().

◆ plugin_event_name

const char** plugin_event_name = plugin_event_name_init
All globals declared here have C linkage to reduce link compatibility
issues with implementation language choice and mangling.   

Referenced by dump_active_plugins(), get_named_event_id(), and register_callback().

◆ plugin_event_name_init

const char* plugin_event_name_init[]
Initial value:
Event names as strings.  Keep in sync with enum plugin_event.   

Referenced by get_named_event_id().

◆ plugin_name_args_tab

htab_t plugin_name_args_tab = NULL
Hash table for the plugin_name_args objects created during command-line

Referenced by add_new_plugin(), finalize_plugins(), for_each_plugin(), initialize_plugins(), parse_plugin_arg_opt(), print_plugins_help(), print_plugins_versions(), and register_plugin_info().