]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
analyzer: port translation_unit_callbacks to pub/sub
authorDavid Malcolm <dmalcolm@redhat.com>
Fri, 9 Jan 2026 20:54:16 +0000 (15:54 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Fri, 9 Jan 2026 20:54:16 +0000 (15:54 -0500)
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 <dmalcolm@redhat.com>
gcc/analyzer/analyzer-language.cc
gcc/analyzer/analyzer-language.h
gcc/analyzer/common.h
gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.cc

index 48ffb9ccdbb00a0ebb30b4484bd08569a4f1d172..aa8125e065488e83f8b352d6df73d6cc40536b1c 100644 (file)
@@ -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.
index 765d28cd1c3fe85adadee079404a83a6a191969a..1c8bc4695d135f6c6dcff0de24f62692458e2676 100644 (file)
@@ -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);
index 8928cc771daf8a94a761a7fba506595f9d02b153..d66c1a86e6130406c0e1c87bc41046ea0f4bf004 100644 (file)
@@ -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
index ce8d41289d7abd430a566f9eabaef92781008e40..fc677c5ec5418eb21eb61548b6cd96c98d783896 100644 (file)
@@ -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