GCC Middle and Back End API Reference
sink.h
Go to the documentation of this file.
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
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
26namespace diagnostics {
27
28class 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
34class sink
35{
36public:
37 /* Abstract base class for adding additional functionality to a sink
38 (e.g. via a plugin). */
40 {
41 public:
42 virtual ~extension () {}
43 virtual void dump (FILE *out, int indent) const = 0;
44 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:
56 };
57
58 virtual ~sink () {}
59
60 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 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>
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. */
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
103
104 context &get_context () const { return m_context; }
105 pretty_printer *get_printer () const { return m_printer.get (); }
106
108 {
109 return m_context.get_diagram_theme ();
110 }
111
112 void DEBUG_FUNCTION dump () const { dump (stderr, 0); }
113
114 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
124
125protected:
127 : m_context (dc),
128 m_printer (dc.clone_printer ())
129 {}
130
131protected:
133 std::unique_ptr<pretty_printer> m_printer;
134
135private:
136 std::vector<std::unique_ptr<extension>> m_extensions;
137};
138
139extern void
141 const char *main_input_filename_,
142 const char *base_file_name,
144 bool json_formatting);
145
146} // namespace diagnostics
147
148#endif /* ! GCC_DIAGNOSTICS_SINK_H */
Definition diagnostics/context.h:254
Definition diagram.h:35
Definition logging.h:38
Definition buffering.h:98
virtual ~extension()
Definition sink.h:42
virtual void dump(FILE *out, int indent) const =0
extension(sink &sink_)
Definition sink.h:49
virtual void finalize()
Definition sink.h:44
sink & m_sink
Definition sink.h:55
sink & get_sink() const
Definition sink.h:46
void add_extension(std::unique_ptr< extension > sink_ext)
Definition sink.h:117
std::unique_ptr< pretty_printer > m_printer
Definition sink.h:133
virtual void on_begin_group()=0
virtual text_sink * dyn_cast_text_sink()
Definition sink.h:60
void finalize_extensions()
Definition diagnostics/context.cc:1876
text_art::theme * get_diagram_theme() const
Definition sink.h:107
context & get_context() const
Definition sink.h:104
virtual void on_diagram(const diagram &diag)=0
virtual std::unique_ptr< per_sink_buffer > make_per_sink_buffer()=0
virtual void set_buffer(per_sink_buffer *)=0
virtual bool follows_reference_printer_p() const =0
virtual void dump_kind(FILE *out) const =0
pretty_printer * get_printer() const
Definition sink.h:105
virtual void after_diagnostic(const diagnostic_info &)=0
virtual void on_report_diagnostic(const diagnostic_info &, enum kind orig_diag_kind)=0
virtual void on_end_group()=0
context & m_context
Definition sink.h:132
sink(context &dc)
Definition sink.h:126
void DEBUG_FUNCTION dump() const
Definition sink.h:112
virtual bool machine_readable_stderr_p() const =0
std::vector< std::unique_ptr< extension > > m_extensions
Definition sink.h:136
virtual void report_global_digraph(const lazily_created< digraphs::digraph > &)=0
virtual void update_printer()=0
virtual void on_report_verbatim(text_info &)
Definition diagnostics/context.cc:1883
virtual ~sink()
Definition sink.h:58
virtual void set_main_input_filename(const char *)
Definition sink.h:67
logging::logger * get_logger()
Definition sink.h:114
Definition text-sink.h:35
Definition lazily-created.h:31
Definition pretty-print.h:241
Definition theme.h:30
diagnostics_output_format
Definition context-options.h:54
static const char * base_file_name(const char *file_name)
Definition genautomata.cc:9229
Definition coretypes.h:167
void output_format_init(context &dc, const char *main_input_filename_, const char *base_file_name, enum diagnostics_output_format format, bool json_formatting)
Definition diagnostics/context.cc:1892
kind
Definition kinds.h:27
Definition diagnostic-info.h:32
Definition pretty-print.h:34
#define DEBUG_FUNCTION
Definition system.h:1236