https://gcc.gnu.org/g:d36257a190dbe94a284f774fbf5c003abf03fc33
commit r16-6667-gd36257a190dbe94a284f774fbf5c003abf03fc33 Author: David Malcolm <[email protected]> Date: Fri Jan 9 15:54:16 2026 -0500 analyzer: port translation_unit_callbacks to pub/sub Simplification/consolidation of some callback logic in analyzer in favor of using the analyzer pub/sub channel. No functional change intended. gcc/analyzer/ChangeLog: * analyzer-language.cc: Include "context.h" and "channels.h". (finish_translation_unit_callbacks): Delete. (register_finish_translation_unit_callback): Delete. (run_callbacks): Delete. (on_finish_translation_unit): Port from run_callbacks to pub/sub. * analyzer-language.h (finish_translation_unit_callback): Delete typedef. (register_finish_translation_unit_callback): Delete decl. * common.h (class translation_unit): New forward decl. (struct analyzer_events::on_tu_finished): New. (analyzer_events::subscriber::on_message): Add vfunc for on_tu_finished messages. gcc/testsuite/ChangeLog: * gcc.dg/plugin/analyzer_cpython_plugin.cc (cpython_analyzer_events_subscriber::on_message): New. (plugin_init): Port stashing of named types and global vars to pub/sub framework. Signed-off-by: David Malcolm <[email protected]> Diff: --- gcc/analyzer/analyzer-language.cc | 29 ++++++---------------- gcc/analyzer/analyzer-language.h | 5 ---- gcc/analyzer/common.h | 15 ++++++++++- .../gcc.dg/plugin/analyzer_cpython_plugin.cc | 10 ++++++-- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/gcc/analyzer/analyzer-language.cc b/gcc/analyzer/analyzer-language.cc index 48ffb9ccdbb0..aa8125e06548 100644 --- a/gcc/analyzer/analyzer-language.cc +++ b/gcc/analyzer/analyzer-language.cc @@ -22,6 +22,8 @@ along with GCC; see the file COPYING3. If not see #include "diagnostic.h" #include "stringpool.h" +#include "context.h" +#include "channels.h" #include "analyzer/analyzer-language.h" #include "analyzer/analyzer-logging.h" @@ -32,26 +34,6 @@ static GTY (()) hash_map <tree, tree> *analyzer_stashed_constants; #if ENABLE_ANALYZER namespace ana { -static vec<finish_translation_unit_callback> - *finish_translation_unit_callbacks; - -void -register_finish_translation_unit_callback ( - finish_translation_unit_callback callback) -{ - if (!finish_translation_unit_callbacks) - vec_alloc (finish_translation_unit_callbacks, 1); - finish_translation_unit_callbacks->safe_push (callback); -} - -static void -run_callbacks (logger *logger, const translation_unit &tu) -{ - for (auto const &cb : finish_translation_unit_callbacks) - { - cb (logger, tu); - } -} /* Call into TU to try to find a value for NAME. If found, stash its value within analyzer_stashed_constants. */ @@ -120,7 +102,12 @@ on_finish_translation_unit (const translation_unit &tu) *global_dc->get_reference_printer ())); stash_named_constants (the_logger.get_logger (), tu); - run_callbacks (the_logger.get_logger (), tu); + if (auto chan = g->get_channels ().analyzer_events_channel.get_if_active ()) + { + gcc::topics::analyzer_events::on_tu_finished msg {the_logger.get_logger (), + tu}; + chan->publish (msg); + } } /* Lookup NAME in the named constants stashed when the frontend TU finished. diff --git a/gcc/analyzer/analyzer-language.h b/gcc/analyzer/analyzer-language.h index 765d28cd1c3f..1c8bc4695d13 100644 --- a/gcc/analyzer/analyzer-language.h +++ b/gcc/analyzer/analyzer-language.h @@ -41,11 +41,6 @@ class translation_unit virtual tree lookup_global_var_by_id (tree id) const = 0; }; -typedef void (*finish_translation_unit_callback) - (logger *, const translation_unit &); -void register_finish_translation_unit_callback ( - finish_translation_unit_callback callback); - /* Analyzer hook for frontends to call at the end of the TU. */ void on_finish_translation_unit (const translation_unit &tu); diff --git a/gcc/analyzer/common.h b/gcc/analyzer/common.h index 8928cc771daf..d66c1a86e613 100644 --- a/gcc/analyzer/common.h +++ b/gcc/analyzer/common.h @@ -146,6 +146,8 @@ class known_function; class builtin_known_function; class internal_known_function; +class translation_unit; + /* Forward decls of functions. */ extern void dump_tree (pretty_printer *pp, tree t); @@ -609,6 +611,16 @@ namespace topics { namespace analyzer_events { +/* A message published by the analyzer when the frontend finishes + parsing the TU, to allow it to look up pertinent items using the FE's + name-resolution logic. */ + +struct on_tu_finished +{ + ana::logger *m_logger; + const ana::translation_unit &m_tu; +}; + /* A message published by the analyzer as it starts up, intended for subsystems/plugins that want to register additional functionality within the analyzer. */ @@ -630,7 +642,8 @@ struct subscriber { virtual ~subscriber () = default; - virtual void on_message (const on_ana_init &) = 0; + virtual void on_message (const on_tu_finished &) {} + virtual void on_message (const on_ana_init &) {} }; } // namespace gcc::topics::analyzer_events diff --git a/gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.cc b/gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.cc index ce8d41289d7a..fc677c5ec541 100644 --- a/gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.cc +++ b/gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.cc @@ -1185,6 +1185,14 @@ namespace analyzer_events = ::gcc::topics::analyzer_events; class cpython_analyzer_events_subscriber : public analyzer_events::subscriber { public: + void + on_message (const analyzer_events::on_tu_finished &msg) final override + { + LOG_SCOPE (msg.m_logger); + stash_named_types (msg.m_logger, msg.m_tu); + stash_global_vars (msg.m_logger, msg.m_tu); + } + void on_message (const analyzer_events::on_ana_init &m) final override { @@ -1222,8 +1230,6 @@ plugin_init (struct plugin_name_args *plugin_info, const char *plugin_name = plugin_info->base_name; if (0) inform (input_location, "got here; %qs", plugin_name); - register_finish_translation_unit_callback (&stash_named_types); - register_finish_translation_unit_callback (&stash_global_vars); region_model::register_pop_frame_callback(pyobj_refcnt_checker); g->get_channels ().analyzer_events_channel.add_subscriber (ana::cpython_sub); #else
