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