From: David Malcolm Date: Thu, 9 Oct 2025 15:38:50 +0000 (-0400) Subject: diagnostics: add class sink::extension X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0558c6028e47eb623365c3865577f40f9d1fa27c;p=thirdparty%2Fgcc.git diagnostics: add class sink::extension 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 --- diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc index 54e397cda9a..7bec3f10599 100644 --- a/gcc/c-family/c-opts.cc +++ b/gcc/c-family/c-opts.cc @@ -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 . */ +#define INCLUDE_VECTOR #include "config.h" #include "system.h" #include "coretypes.h" diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc index 16dfeafc07a..ae899ec9f77 100644 --- a/gcc/cp/error.cc +++ b/gcc/cp/error.cc @@ -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 . */ +#define INCLUDE_VECTOR #include "config.h" /* For use with name_hint. */ #include "system.h" diff --git a/gcc/diagnostic-global-context.cc b/gcc/diagnostic-global-context.cc index 30fc1906790..94be0fed81e 100644 --- a/gcc/diagnostic-global-context.cc +++ b/gcc/diagnostic-global-context.cc @@ -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" diff --git a/gcc/diagnostics/buffering.cc b/gcc/diagnostics/buffering.cc index 019c9927c6d..420a9cfbeea 100644 --- a/gcc/diagnostics/buffering.cc +++ b/gcc/diagnostics/buffering.cc @@ -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 . */ +#define INCLUDE_VECTOR #include "config.h" #include "system.h" #include "coretypes.h" diff --git a/gcc/diagnostics/context.cc b/gcc/diagnostics/context.cc index cd14977d542..dd6bbdb29cd 100644 --- a/gcc/diagnostics/context.cc +++ b/gcc/diagnostics/context.cc @@ -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 diff --git a/gcc/diagnostics/macro-unwinding.cc b/gcc/diagnostics/macro-unwinding.cc index fb4ee65f424..4d7133963ad 100644 --- a/gcc/diagnostics/macro-unwinding.cc +++ b/gcc/diagnostics/macro-unwinding.cc @@ -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 . */ +#define INCLUDE_VECTOR #include "config.h" #include "system.h" #include "coretypes.h" diff --git a/gcc/diagnostics/selftest-context.cc b/gcc/diagnostics/selftest-context.cc index 2eced4d3cd8..aafa90ac457 100644 --- a/gcc/diagnostics/selftest-context.cc +++ b/gcc/diagnostics/selftest-context.cc @@ -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 . */ +#define INCLUDE_VECTOR #include "config.h" #include "system.h" #include "coretypes.h" diff --git a/gcc/diagnostics/sink.h b/gcc/diagnostics/sink.h index aaa6c50ab21..a2094e9f5a5 100644 --- a/gcc/diagnostics/sink.h +++ b/gcc/diagnostics/sink.h @@ -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 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 m_printer; + +private: + std::vector> m_extensions; }; extern void diff --git a/gcc/fortran/error.cc b/gcc/fortran/error.cc index ebf9e6197d2..8fde46ed6af 100644 --- a/gcc/fortran/error.cc +++ b/gcc/fortran/error.cc @@ -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" diff --git a/gcc/gcc.cc b/gcc/gcc.cc index 5dd33c2dfcb..eae7f07d962 100644 --- a/gcc/gcc.cc +++ b/gcc/gcc.cc @@ -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 diff --git a/gcc/langhooks.cc b/gcc/langhooks.cc index 20d27a6d7fd..6431d40af04 100644 --- a/gcc/langhooks.cc +++ b/gcc/langhooks.cc @@ -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 . */ +#define INCLUDE_VECTOR #include "config.h" #include "system.h" #include "coretypes.h" diff --git a/gcc/opts.cc b/gcc/opts.cc index 10ce2c3de33..21ac6b566e0 100644 --- a/gcc/opts.cc +++ b/gcc/opts.cc @@ -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 . */ +#define INCLUDE_VECTOR #include "config.h" #include "system.h" #include "intl.h" diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.cc b/gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.cc index 48f832579ad..2bead63eede 100644 --- a/gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.cc +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.cc @@ -1,5 +1,6 @@ /* { dg-options "-O" } */ +#define INCLUDE_VECTOR #include "gcc-plugin.h" #include "config.h" #include "system.h" diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.cc b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.cc index 92839cd35b7..9ee3219370c 100644 --- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.cc +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.cc @@ -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" diff --git a/gcc/testsuite/gcc.dg/plugin/location_overflow_plugin.cc b/gcc/testsuite/gcc.dg/plugin/location_overflow_plugin.cc index 00ad8704477..2c40b311165 100644 --- a/gcc/testsuite/gcc.dg/plugin/location_overflow_plugin.cc +++ b/gcc/testsuite/gcc.dg/plugin/location_overflow_plugin.cc @@ -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" diff --git a/gcc/tree-diagnostic-client-data-hooks.cc b/gcc/tree-diagnostic-client-data-hooks.cc index 77eb292f787..9ad608d17e0 100644 --- a/gcc/tree-diagnostic-client-data-hooks.cc +++ b/gcc/tree-diagnostic-client-data-hooks.cc @@ -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 . */ +#define INCLUDE_VECTOR #include "config.h" #include "system.h" #include "coretypes.h" diff --git a/gcc/tree-diagnostic.cc b/gcc/tree-diagnostic.cc index 20183c8bced..4cf742d047d 100644 --- a/gcc/tree-diagnostic.cc +++ b/gcc/tree-diagnostic.cc @@ -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 . */ +#define INCLUDE_VECTOR #include "config.h" #include "system.h" #include "coretypes.h" diff --git a/libcc1/context.cc b/libcc1/context.cc index 38343a7c29e..b392f774c72 100644 --- a/libcc1/context.cc +++ b/libcc1/context.cc @@ -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"