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_DIAGNOSTIC_FORMAT_H
22 : : #define GCC_DIAGNOSTIC_FORMAT_H
23 : :
24 : : #include "diagnostic.h"
25 : :
26 : : class diagnostic_per_format_buffer;
27 : :
28 : : /* Abstract base class for a particular output format for diagnostics;
29 : : each value of -fdiagnostics-output-format= will have its own
30 : : implementation. */
31 : :
32 : : class diagnostic_output_format
33 : : {
34 : : public:
35 : 307196 : virtual ~diagnostic_output_format () {}
36 : :
37 : : virtual void dump (FILE *out, int indent) const;
38 : :
39 : : /* Vfunc for notifying this format what the primary input file is,
40 : : e.g. for titles of HTML, for SARIF's artifact metadata. */
41 : 56 : virtual void set_main_input_filename (const char *) {}
42 : :
43 : : /* Vfunc for making an appropriate diagnostic_per_format_buffer
44 : : subclass for this format. */
45 : : virtual std::unique_ptr<diagnostic_per_format_buffer>
46 : : make_per_format_buffer () = 0;
47 : :
48 : : /* Vfunc to be called when call a diagnostic_buffer is set on
49 : : a diagnostic_context, to update this format. The per_format_buffer
50 : : will be one created by make_per_format_buffer above and thus be
51 : : of the correct subclass. */
52 : : virtual void set_buffer (diagnostic_per_format_buffer *) = 0;
53 : :
54 : : virtual void on_begin_group () = 0;
55 : : virtual void on_end_group () = 0;
56 : :
57 : : /* Vfunc with responsibility for phase 3 of formatting the message
58 : : and "printing" the result. */
59 : : virtual void on_report_diagnostic (const diagnostic_info &,
60 : : diagnostic_t orig_diag_kind) = 0;
61 : :
62 : : virtual void on_report_verbatim (text_info &);
63 : :
64 : : virtual void on_diagram (const diagnostic_diagram &diagram) = 0;
65 : : virtual void after_diagnostic (const diagnostic_info &) = 0;
66 : : virtual bool machine_readable_stderr_p () const = 0;
67 : : virtual bool follows_reference_printer_p () const = 0;
68 : :
69 : : /* Vfunc called when the diagnostic_context changes its
70 : : reference printer (either to a new subclass of pretty_printer
71 : : or when color/url options change).
72 : : Subclasses should update their m_printer accordingly. */
73 : : virtual void update_printer () = 0;
74 : :
75 : 4209512 : diagnostic_context &get_context () const { return m_context; }
76 : 18977222 : pretty_printer *get_printer () const { return m_printer.get (); }
77 : :
78 : 2629 : text_art::theme *get_diagram_theme () const
79 : : {
80 : 2629 : return m_context.get_diagram_theme ();
81 : : }
82 : :
83 : 0 : void DEBUG_FUNCTION dump () const { dump (stderr, 0); }
84 : :
85 : : protected:
86 : 710129 : diagnostic_output_format (diagnostic_context &context)
87 : 710129 : : m_context (context),
88 : 710129 : m_printer (context.clone_printer ())
89 : : {}
90 : :
91 : : protected:
92 : : diagnostic_context &m_context;
93 : : std::unique_ptr<pretty_printer> m_printer;
94 : : };
95 : :
96 : : extern void
97 : : diagnostic_output_format_init (diagnostic_context &,
98 : : const char *main_input_filename_,
99 : : const char *base_file_name,
100 : : enum diagnostics_output_format,
101 : : bool json_formatting);
102 : :
103 : : extern diagnostic_output_format &
104 : : diagnostic_output_format_init_json_stderr (diagnostic_context &context,
105 : : bool formatted);
106 : : extern diagnostic_output_format &
107 : : diagnostic_output_format_init_json_file (diagnostic_context &context,
108 : : bool formatted,
109 : : const char *base_file_name);
110 : :
111 : : #endif /* ! GCC_DIAGNOSTIC_FORMAT_H */
|