1/* Declarations for managing different output formats for diagnostics.
2 Copyright (C) 2023-2026 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_DIAGNOSTICS_SINK_H
22#define GCC_DIAGNOSTICS_SINK_H
23
24#include "diagnostic.h"
25#include "diagnostics/logical-locations.h"
26
27namespace diagnostics {
28
29class per_sink_buffer;
30
31/* Abstract base class for a particular output format for diagnostics;
32 each value of -fdiagnostics-output-format= will have its own
33 implementation. */
34
35class sink
36{
37public:
38 /* Abstract base class for adding additional functionality to a sink
39 (e.g. via a plugin). */
40 class extension
41 {
42 public:
43 virtual ~extension () {}
44 virtual void dump (FILE *out, int indent) const = 0;
45 virtual void finalize () {}
46
47 sink &get_sink () const { return m_sink; }
48
49 protected:
50 extension (sink &sink_)
51 : m_sink (sink_)
52 {
53 }
54
55 private:
56 sink &m_sink;
57 };
58
59 virtual ~sink () {}
60
61 virtual text_sink *dyn_cast_text_sink () { return nullptr; }
62
63 virtual void dump_kind (FILE *out) const = 0;
64 virtual void dump (FILE *out, int indent) const;
65
66 /* Vfunc for notifying this format what the primary input file is,
67 e.g. for titles of HTML, for SARIF's artifact metadata. */
68 virtual void set_main_input_filename (const char *) {}
69
70 /* Vfunc for making an appropriate per_sink_buffer
71 subclass for this format. */
72 virtual std::unique_ptr<per_sink_buffer>
73 make_per_sink_buffer () = 0;
74
75 /* Vfunc to be called when call a diagnostics::buffer is set on
76 a diagnostics::context, to update this format. The per_sink_buffer
77 will be one created by make_per_sink_buffer above and thus be
78 of the correct subclass. */
79 virtual void set_buffer (per_sink_buffer *) = 0;
80
81 virtual void on_begin_group () = 0;
82 virtual void on_end_group () = 0;
83
84 /* Vfunc with responsibility for phase 3 of formatting the message
85 and "printing" the result. */
86 virtual void on_report_diagnostic (const diagnostic_info &,
87 enum kind orig_diag_kind) = 0;
88
89 virtual void on_report_verbatim (text_info &);
90
91 virtual void on_diagram (const diagram &diag) = 0;
92 virtual void after_diagnostic (const diagnostic_info &) = 0;
93 virtual bool machine_readable_stderr_p () const = 0;
94 virtual bool follows_reference_printer_p () const = 0;
95
96 /* Vfunc called when the diagnostics::context changes its
97 reference printer (either to a new subclass of pretty_printer
98 or when color/url options change).
99 Subclasses should update their m_printer accordingly. */
100 virtual void update_printer () = 0;
101
102 virtual void
103 report_global_digraph (const lazily_created<digraphs::digraph> &) = 0;
104
105 virtual void
106 report_digraph_for_logical_location (const lazily_created<digraphs::digraph> &,
107 logical_locations::key) = 0;
108
109 context &get_context () const { return m_context; }
110 pretty_printer *get_printer () const { return m_printer.get (); }
111
112 text_art::theme *get_diagram_theme () const
113 {
114 return m_context.get_diagram_theme ();
115 }
116
117 void DEBUG_FUNCTION dump () const { dump (stderr, indent: 0); }
118
119 logging::logger *get_logger () { return m_context.get_logger (); }
120
121 void
122 add_extension (std::unique_ptr<extension> sink_ext)
123 {
124 m_extensions.push_back (x: std::move (sink_ext));
125 }
126
127 void
128 finalize_extensions ();
129
130protected:
131 sink (context &dc)
132 : m_context (dc),
133 m_printer (dc.clone_printer ())
134 {}
135
136protected:
137 context &m_context;
138 std::unique_ptr<pretty_printer> m_printer;
139
140private:
141 std::vector<std::unique_ptr<extension>> m_extensions;
142};
143
144extern void
145output_format_init (context &,
146 const char *main_input_filename_,
147 const char *base_file_name,
148 enum diagnostics_output_format,
149 bool json_formatting);
150
151} // namespace diagnostics
152
153#endif /* ! GCC_DIAGNOSTICS_SINK_H */
154

source code of gcc/diagnostics/sink.h