https://gcc.gnu.org/g:0558c6028e47eb623365c3865577f40f9d1fa27c
commit r16-4330-g0558c6028e47eb623365c3865577f40f9d1fa27c Author: David Malcolm <[email protected]> Date: Thu Oct 9 11:38:50 2025 -0400 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 <[email protected]> Diff: --- gcc/c-family/c-opts.cc | 1 + gcc/cp/error.cc | 1 + gcc/diagnostic-global-context.cc | 1 + gcc/diagnostics/buffering.cc | 1 + gcc/diagnostics/context.cc | 17 +++++++++++ gcc/diagnostics/macro-unwinding.cc | 1 + gcc/diagnostics/selftest-context.cc | 1 + gcc/diagnostics/sink.h | 33 ++++++++++++++++++++++ gcc/fortran/error.cc | 1 + gcc/gcc.cc | 1 + gcc/langhooks.cc | 1 + gcc/opts.cc | 1 + .../gcc.dg/plugin/diagnostic_group_plugin.cc | 1 + .../plugin/diagnostic_plugin_test_show_locus.cc | 1 + .../gcc.dg/plugin/location_overflow_plugin.cc | 1 + gcc/tree-diagnostic-client-data-hooks.cc | 1 + gcc/tree-diagnostic.cc | 1 + libcc1/context.cc | 1 + 18 files changed, 66 insertions(+) diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc index 54e397cda9a4..7bec3f105997 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 <http://www.gnu.org/licenses/>. */ +#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 16dfeafc07a0..ae899ec9f770 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 <http://www.gnu.org/licenses/>. */ +#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 30fc1906790c..94be0fed81e3 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 019c9927c6d2..420a9cfbeea3 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 <http://www.gnu.org/licenses/>. */ +#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 cd14977d5425..dd6bbdb29cd7 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 fb4ee65f4244..4d7133963ad9 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 <http://www.gnu.org/licenses/>. */ +#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 2eced4d3cd85..aafa90ac457d 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 <http://www.gnu.org/licenses/>. */ +#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 aaa6c50ab214..a2094e9f5a56 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<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 diff --git a/gcc/fortran/error.cc b/gcc/fortran/error.cc index ebf9e6197d22..8fde46ed6afc 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 5dd33c2dfcbd..eae7f07d9626 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 20d27a6d7fd0..6431d40af048 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 <http://www.gnu.org/licenses/>. */ +#define INCLUDE_VECTOR #include "config.h" #include "system.h" #include "coretypes.h" diff --git a/gcc/opts.cc b/gcc/opts.cc index 10ce2c3de336..21ac6b566e0b 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 <http://www.gnu.org/licenses/>. */ +#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 48f832579add..2bead63eede8 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 92839cd35b7f..9ee3219370cb 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 00ad8704477a..2c40b311165e 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 77eb292f787d..9ad608d17e09 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 <http://www.gnu.org/licenses/>. */ +#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 20183c8bceda..4cf742d047d9 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 <http://www.gnu.org/licenses/>. */ +#define INCLUDE_VECTOR #include "config.h" #include "system.h" #include "coretypes.h" diff --git a/libcc1/context.cc b/libcc1/context.cc index 38343a7c29eb..b392f774c723 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"
