]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
diagnostics: add class sink::extension
authorDavid Malcolm <dmalcolm@redhat.com>
Thu, 9 Oct 2025 15:38:50 +0000 (11:38 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Thu, 9 Oct 2025 15:38:50 +0000 (11:38 -0400)
This patch provides a way for plugins to add extra information
to a diagnostic sink, potentially capturing more information via
a "finalizer" hook.

gcc/c-family/ChangeLog:
* c-opts.cc: Define INCLUDE_VECTOR.

gcc/cp/ChangeLog:
* error.cc: Define INCLUDE_VECTOR.

gcc/ChangeLog:
* diagnostic-global-context.cc: Define INCLUDE_VECTOR.
* diagnostics/buffering.cc: Likewise.
* diagnostics/context.cc (context::finish): Call
finalize_extensions on each sink.
(sink::dump): Dump any extensions.
(sink::finalize_extensions): New.
* diagnostics/macro-unwinding.cc: Define INCLUDE_VECTOR.
* diagnostics/selftest-context.cc: Likewise.
* diagnostics/sink.h (class sink::extension): New.
(sink::add_extension): New.
(sink::finalize_extensions): New decl.
(sink::m_extensions): New member.
* gcc.cc: Define INCLUDE_VECTOR.
* langhooks.cc: Likewise.
* opts.cc: Likewise.
* tree-diagnostic-client-data-hooks.cc: Likewise.
* tree-diagnostic.cc: Likewise.

gcc/fortran/ChangeLog:
* error.cc: Define INCLUDE_VECTOR.

gcc/testsuite/ChangeLog:
* gcc.dg/plugin/diagnostic_group_plugin.cc: Define INCLUDE_VECTOR.
* gcc.dg/plugin/diagnostic_plugin_test_show_locus.cc: Likewise.
* gcc.dg/plugin/location_overflow_plugin.cc: Likewise.

libcc1/ChangeLog:
* context.cc: Define INCLUDE_VECTOR.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
18 files changed:
gcc/c-family/c-opts.cc
gcc/cp/error.cc
gcc/diagnostic-global-context.cc
gcc/diagnostics/buffering.cc
gcc/diagnostics/context.cc
gcc/diagnostics/macro-unwinding.cc
gcc/diagnostics/selftest-context.cc
gcc/diagnostics/sink.h
gcc/fortran/error.cc
gcc/gcc.cc
gcc/langhooks.cc
gcc/opts.cc
gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.cc
gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.cc
gcc/testsuite/gcc.dg/plugin/location_overflow_plugin.cc
gcc/tree-diagnostic-client-data-hooks.cc
gcc/tree-diagnostic.cc
libcc1/context.cc

index 54e397cda9a493efa3caf3000ad9b900c9dc40f5..7bec3f10599725520cf85b79100b84f1845d4cd7 100644 (file)
@@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+#define INCLUDE_VECTOR
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
index 16dfeafc07a0a1688df4eaa96a0187c35e650306..ae899ec9f770bbebe2b3b96441b6508d39ba6d94 100644 (file)
@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+#define INCLUDE_VECTOR
 #include "config.h"
 /* For use with name_hint.  */
 #include "system.h"
index 30fc1906790c757c0f3a27d4d7e53141d1836d94..94be0fed81e313b5642963fbc5b9caf11c8e5072 100644 (file)
@@ -22,6 +22,7 @@ along with GCC; see the file COPYING3.  If not see
 /* This file implements the parts of the language independent aspect
    of diagnostic messages that implicitly use global_dc.  */
 
+#define INCLUDE_VECTOR
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
index 019c9927c6d2ae005f8a1701adfdfddcdafcbfe1..420a9cfbeea32a36c8957f01e4951bed67de370e 100644 (file)
@@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+#define INCLUDE_VECTOR
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
index cd14977d542558053bb975d8f5ccb093305ea8bc..dd6bbdb29cd73a31992ff519df0c44826bea69c7 100644 (file)
@@ -361,6 +361,9 @@ context::finish ()
       dump (m_logger->get_stream (), m_logger->get_indent ());
     }
 
+  for (auto iter : m_sinks)
+    iter->finalize_extensions ();
+
   /* We might be handling a fatal error.
      Close any active diagnostic groups, which may trigger flushing
      sinks.  */
@@ -1860,6 +1863,20 @@ sink::dump (FILE *out, int indent) const
 {
   dumping::emit_heading (out, indent, "printer");
   m_printer->dump (out, indent + 2);
+
+  dumping::emit_heading (out, indent, "extensions");
+  if (m_extensions.empty ())
+    dumping::emit_none (out, indent + 2);
+  else
+    for (auto &ext : m_extensions)
+      ext->dump (out, indent + 2);
+}
+
+void
+sink::finalize_extensions ()
+{
+  for (auto &ext : m_extensions)
+    ext->finalize ();
 }
 
 void
index fb4ee65f42448392d6ec6f01ad39241f7fd87724..4d7133963ad9f66653277c4d8d138c394713389a 100644 (file)
@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+#define INCLUDE_VECTOR
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
index 2eced4d3cd85db2d623447313b25dbabb1e44be8..aafa90ac457def36747fd882b2d1fbe6915d3658 100644 (file)
@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+#define INCLUDE_VECTOR
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
index aaa6c50ab2148cc1a55bec3c2838a4aea5ef6522..a2094e9f5a56083730bc21144529a756f8d7327a 100644 (file)
@@ -34,6 +34,27 @@ class per_sink_buffer;
 class sink
 {
 public:
+  /* Abstract base class for adding additional functionality to a sink
+     (e.g. via a plugin).  */
+  class extension
+  {
+  public:
+    virtual ~extension () {}
+    virtual void dump (FILE *out, int indent) const = 0;
+    virtual void finalize () {}
+
+    sink &get_sink () const { return m_sink; }
+
+  protected:
+    extension (sink &sink_)
+    : m_sink (sink_)
+    {
+    }
+
+  private:
+    sink &m_sink;
+  };
+
   virtual ~sink () {}
 
   virtual text_sink *dyn_cast_text_sink () { return nullptr; }
@@ -92,6 +113,15 @@ public:
 
   logging::logger *get_logger () { return m_context.get_logger (); }
 
+  void
+  add_extension (std::unique_ptr<extension> sink_ext)
+  {
+    m_extensions.push_back (std::move (sink_ext));
+  }
+
+  void
+  finalize_extensions ();
+
 protected:
   sink (context &dc)
   : m_context (dc),
@@ -101,6 +131,9 @@ protected:
 protected:
   context &m_context;
   std::unique_ptr<pretty_printer> m_printer;
+
+private:
+  std::vector<std::unique_ptr<extension>> m_extensions;
 };
 
 extern void
index ebf9e6197d2252d827d2249c934e3d6a2b74c4a0..8fde46ed6afc871b611fa9966b87cc3e47415190 100644 (file)
@@ -24,6 +24,7 @@ along with GCC; see the file COPYING3.  If not see
    for possible use later.  If a line does not match a legal
    construction, then the saved error message is reported.  */
 
+#define INCLUDE_VECTOR
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
index 5dd33c2dfcbdfede45aaf9a8ccc7019189a69b36..eae7f07d9626b50e2a59b2696cfee7cc7a2b6964 100644 (file)
@@ -28,6 +28,7 @@ Once it knows which kind of compilation to perform, the procedure for
 compilation is specified by a string called a "spec".  */
 
 #define INCLUDE_STRING
+#define INCLUDE_VECTOR
 #include "config.h"
 #include "system.h"
 #ifdef HOST_HAS_PERSONALITY_ADDR_NO_RANDOMIZE
index 20d27a6d7fd017fb9cd71fe03d15deada52eec7d..6431d40af0480ef8be3a5a7c5fdadc3bbc233230 100644 (file)
@@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+#define INCLUDE_VECTOR
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
index 10ce2c3de336de1e21a1351395a399462dc48ca9..21ac6b566e0bd71cd25bf3239467306a5ac0885b 100644 (file)
@@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+#define INCLUDE_VECTOR
 #include "config.h"
 #include "system.h"
 #include "intl.h"
index 48f832579add83e909f2f6600ad57ba5bf36e3e7..2bead63eede812fefbd191d06892ae006a870eb7 100644 (file)
@@ -1,5 +1,6 @@
 /* { dg-options "-O" } */
 
+#define INCLUDE_VECTOR
 #include "gcc-plugin.h"
 #include "config.h"
 #include "system.h"
index 92839cd35b7f9603fb82c8bfacf9d40c3eff3f71..9ee3219370cb9ad0e5909c965ab83ba3e627dbb3 100644 (file)
@@ -32,6 +32,7 @@
    to ensure that further very long lines don't start a new linemap.
    This also means that we can't use macros in the test files.  */
 
+#define INCLUDE_VECTOR
 #include "gcc-plugin.h"
 #include "config.h"
 #include "system.h"
index 00ad8704477a44e826599313cb4a32cdbc9d8e1d..2c40b311165e2b023c802ce20de5b7c3d96679d6 100644 (file)
@@ -1,6 +1,7 @@
 /* Plugin for testing how gracefully we degrade in the face of very
    large source files.  */
 
+#define INCLUDE_VECTOR
 #include "config.h"
 #include "gcc-plugin.h"
 #include "system.h"
index 77eb292f787db3c2be11419dffb1647e3e73d74d..9ad608d17e09b73d8dd5b2f305f05798a0fd7f0f 100644 (file)
@@ -19,6 +19,7 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+#define INCLUDE_VECTOR
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
index 20183c8bceda1dde59a9f4d193692f37c2aa0389..4cf742d047d93bc7081135dc388e3ca4a5febf77 100644 (file)
@@ -19,6 +19,7 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+#define INCLUDE_VECTOR
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
index 38343a7c29ebbf08fba63f6145b5ffa5307e20c7..b392f774c7238c149c70f51ea72d23ec57626180 100644 (file)
@@ -31,6 +31,7 @@ along with GCC; see the file COPYING3.  If not see
 #undef PACKAGE_TARNAME
 #undef PACKAGE_VERSION
 
+#define INCLUDE_VECTOR
 #include "gcc-plugin.h"
 #include "system.h"
 #include "coretypes.h"