From af0adbaa363fc677da585d90e1002ebe207063a5 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Sat, 8 Nov 2025 18:59:29 -0500
Subject: [PATCH 64/98] FIXME: start adding RPC granularity

---
 gcc/diagnostics/sarif-sink.cc | 81 +++++++++++++++++++++++++++--------
 gcc/diagnostics/sarif-sink.h  | 10 +++++
 2 files changed, 74 insertions(+), 17 deletions(-)

diff --git a/gcc/diagnostics/sarif-sink.cc b/gcc/diagnostics/sarif-sink.cc
index 82c616dd5b20..4505af3d732a 100644
--- a/gcc/diagnostics/sarif-sink.cc
+++ b/gcc/diagnostics/sarif-sink.cc
@@ -4299,42 +4299,87 @@ public:
 		     const line_maps *line_maps,
 		     std::unique_ptr<sarif_serialization_format> serialization_format,
 		     const sarif_generation_options &sarif_gen_opts,
-		     unique_fd fd)
+		     unique_fd fd,
+		     enum sarif_rpc_granularity granularity)
   : sarif_sink (dc, line_maps,
 		std::move (serialization_format),
 		sarif_gen_opts),
-    m_fd (std::move (fd))
+    m_fd (std::move (fd)),
+    m_granularity (granularity)
   {
   }
+  ~sarif_socket_sink ()
+  {
+    switch (m_granularity)
+      {
+      default:
+	gcc_unreachable ();
+
+      case sarif_rpc_granularity::log:
+	{
+	  std::unique_ptr<sarif_log> top = m_builder.flush_to_object ();
+
+	  auto notification = std::make_unique<json::object> ();
+	  notification->set_string ("jsonrpc", "2.0");
+	  notification->set_string ("method", "OnSarifLog");
+	  {
+	    auto params = std::make_unique<json::object> ();
+	    params->set ("log", std::move (top));
+	    notification->set ("params", std::move (params));
+	  }
+
+	  send_rpc_notification (*notification);
+	}
+	break;
+
+      case sarif_rpc_granularity::result:
+	break;
+      }
+  }
   void dump_kind (FILE *out) const override
   {
     fprintf (out, "sarif_socket_sink: fd=%i", int (m_fd));
+    // FIXME: dump granularity here?
   }
   bool machine_readable_stderr_p () const final override
   {
     return false;
   }
 
-  /* Rather than appending it to the results array, instead
-     send it to the output socket as a JSON-RPC 2.0 notification.  */
   void on_end_group () final override
   {
     // TODO: what about buffering?
+    switch (m_granularity)
+      {
+      default:
+	gcc_unreachable ();
 
-    std::unique_ptr<sarif_result> result = m_builder.take_current_result ();
-    if (!result)
-      return;
+      case sarif_rpc_granularity::log:
+	sarif_sink::on_end_group ();
+	break;
 
-    auto notification = std::make_unique<json::object> ();
-    notification->set_string ("jsonrpc", "2.0");
-    notification->set_string ("method", "OnSarifResult");
-    {
-      auto params = std::make_unique<json::object> ();
-      params->set ("result", std::move (result));
-      notification->set ("params", std::move (params));
-    }
+      case sarif_rpc_granularity::result:
+	{
+	  /* Rather than appending it to the results array, instead
+	     send it to the output socket as a JSON-RPC 2.0 notification.  */
+	  std::unique_ptr<sarif_result> result
+	    = m_builder.take_current_result ();
+	  if (!result)
+	    return;
+
+	  auto notification = std::make_unique<json::object> ();
+	  notification->set_string ("jsonrpc", "2.0");
+	  notification->set_string ("method", "OnSarifResult");
+	  {
+	    auto params = std::make_unique<json::object> ();
+	    params->set ("result", std::move (result));
+	    notification->set ("params", std::move (params));
+	  }
 
-    send_rpc_notification (*notification);
+	  send_rpc_notification (*notification);
+	}
+	break;
+      }
   }
 
 private:
@@ -4373,6 +4418,7 @@ private:
   }
 
   unique_fd m_fd;
+  enum sarif_rpc_granularity m_granularity;
 };
 
 /* Print the start of an embedded link to PP, as per 3.11.6.  */
@@ -4736,7 +4782,8 @@ maybe_open_sarif_sink_for_socket (context &dc)
      line_table,
      std::make_unique <sarif_serialization_format_json> (true),
      sarif_gen_opts,
-     std::move (sfd));
+     std::move (sfd),
+     sarif_rpc_granularity::log); // result);
   sink_->update_printer ();
   dc.add_sink (std::move (sink_));
 }
diff --git a/gcc/diagnostics/sarif-sink.h b/gcc/diagnostics/sarif-sink.h
index 25522f491370..634019fe9f3a 100644
--- a/gcc/diagnostics/sarif-sink.h
+++ b/gcc/diagnostics/sarif-sink.h
@@ -194,6 +194,16 @@ extern std::unique_ptr<sarif_edge>
 make_sarif_edge (const digraphs::edge &e,
 		 sarif_builder *builder);
 
+// FIXME:
+enum class sarif_rpc_granularity
+{
+  // FIXME
+  log,
+
+  // FIXME:
+  result
+};
+
 extern void
 maybe_open_sarif_sink_for_socket (context &ctxt);
 
-- 
2.49.0

