Branch data Line data Source code
1 : : /* Classic text-based output of 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_TEXT_SINK_H
22 : : #define GCC_DIAGNOSTICS_TEXT_SINK_H
23 : :
24 : : #include "diagnostics/sink.h"
25 : :
26 : : namespace diagnostics {
27 : :
28 : : /* Subclass of diagnostics::sink for classic text-based output
29 : : to stderr.
30 : :
31 : : Uses diagnostics::context.m_text_callbacks to provide client-specific
32 : : textual output (e.g. include paths, macro expansions, etc). */
33 : :
34 : : class text_sink : public sink
35 : : {
36 : : public:
37 : 712313 : text_sink (context &dc,
38 : : source_printing_options *source_printing = nullptr,
39 : : bool follows_reference_printer = false)
40 : 712313 : : sink (dc),
41 : 712313 : m_saved_output_buffer (nullptr),
42 : 712313 : m_column_policy (dc),
43 : 712313 : m_last_module (nullptr),
44 : 712313 : m_includes_seen (nullptr),
45 : 712313 : m_source_printing (source_printing
46 : 712313 : ? *source_printing
47 : 712313 : : dc.get_source_printing_options ()),
48 : 712313 : m_follows_reference_printer (follows_reference_printer),
49 : 712313 : m_show_nesting (false),
50 : 712313 : m_show_nesting_levels (false)
51 : 712313 : {}
52 : : ~text_sink ();
53 : :
54 : 1123563 : text_sink *dyn_cast_text_sink () final override { return this; }
55 : :
56 : 0 : void dump_kind (FILE *out) const override
57 : : {
58 : 0 : fprintf (out, "text_sink");
59 : 0 : }
60 : : void dump (FILE *out, int indent) const override;
61 : :
62 : : std::unique_ptr<per_sink_buffer>
63 : : make_per_sink_buffer () final override;
64 : : void set_buffer (per_sink_buffer *) final override;
65 : :
66 : 1435504 : void on_begin_group () override {}
67 : 1435503 : void on_end_group () override {}
68 : : void on_report_diagnostic (const diagnostic_info &,
69 : : enum kind orig_diag_kind) override;
70 : : void on_report_verbatim (text_info &) final override;
71 : : void on_diagram (const diagram &d) override;
72 : : void after_diagnostic (const diagnostic_info &) override;
73 : 18033 : bool machine_readable_stderr_p () const final override
74 : : {
75 : 18033 : return false;
76 : : }
77 : : bool follows_reference_printer_p () const final override;
78 : :
79 : : void update_printer () override;
80 : :
81 : : void
82 : 3 : report_global_digraph (const lazily_created<digraphs::digraph> &)
83 : : final override
84 : : {
85 : : // no-op for text
86 : 3 : }
87 : :
88 : : /* Helpers for writing lang-specific starters/finalizers for text output. */
89 : : char *build_prefix (const diagnostic_info &) const;
90 : : void report_current_module (location_t where);
91 : : void append_note (location_t location,
92 : : const char * gmsgid, ...) ATTRIBUTE_GCC_DIAG(3,4);
93 : :
94 : :
95 : : char *file_name_as_prefix (const char *) const;
96 : :
97 : : char *build_indent_prefix (bool with_bullet) const;
98 : :
99 : : void print_path (const paths::path &path);
100 : :
101 : 355650 : bool show_column_p () const { return get_context ().m_show_column; }
102 : :
103 : 966 : const column_policy &get_column_policy () const
104 : : {
105 : 966 : return m_column_policy;
106 : : }
107 : : location_print_policy get_location_print_policy () const;
108 : :
109 : 29058 : bool show_nesting_p () const { return m_show_nesting; }
110 : 57 : bool show_locations_in_nesting_p () const
111 : : {
112 : 57 : return m_show_locations_in_nesting;
113 : : }
114 : :
115 : 552197 : void set_show_nesting (bool show_nesting) { m_show_nesting = show_nesting; }
116 : 285701 : void set_show_locations_in_nesting (bool val)
117 : : {
118 : 285701 : m_show_locations_in_nesting = val;
119 : 285689 : }
120 : 285701 : void set_show_nesting_levels (bool show_nesting_levels)
121 : : {
122 : 285701 : m_show_nesting_levels = show_nesting_levels;
123 : 285689 : }
124 : :
125 : : label_text get_location_text (const expanded_location &s) const;
126 : :
127 : 367338 : source_printing_options &get_source_printing_options ()
128 : : {
129 : 367338 : return m_source_printing;
130 : : }
131 : : const source_printing_options &get_source_printing_options () const
132 : : {
133 : : return m_source_printing;
134 : : }
135 : :
136 : : static const char *maybe_line_and_column (int line, int col);
137 : :
138 : : protected:
139 : : void print_any_cwe (const diagnostic_info &diagnostic);
140 : : void print_any_rules (const diagnostic_info &diagnostic);
141 : : void print_option_information (const diagnostic_info &diagnostic,
142 : : enum kind orig_diag_kind);
143 : :
144 : : bool includes_seen_p (const line_map_ordinary *map);
145 : :
146 : : /* For handling diagnostics::buffer. */
147 : : output_buffer *m_saved_output_buffer;
148 : :
149 : : column_policy m_column_policy;
150 : :
151 : : /* Used to detect when the input file stack has changed since last
152 : : described. */
153 : : const line_map_ordinary *m_last_module;
154 : :
155 : : /* Include files that report_current_module has already listed the
156 : : include path for. */
157 : : hash_set<location_t, false, location_hash> *m_includes_seen;
158 : :
159 : : source_printing_options &m_source_printing;
160 : :
161 : : /* If true, this is the initial default text output format created
162 : : when the diagnostics::context was created, and, in particular, before
163 : : initializations of color and m_url_format. Hence this should follow
164 : : the dc's reference printer for these.
165 : : If false, this text output was created after the dc was created, and
166 : : thus tracks its own values for color and m_url_format. */
167 : : bool m_follows_reference_printer;
168 : :
169 : : /* If true, then use indentation to show the nesting structure of
170 : : nested diagnostics, and print locations on separate lines after the
171 : : diagnostic message, rather than as a prefix to the message. */
172 : : bool m_show_nesting;
173 : :
174 : : /* Set to false to suppress location-printing when showing nested
175 : : diagnostics, for use in DejaGnu tests. */
176 : : bool m_show_locations_in_nesting;
177 : :
178 : : /* If true, then add "(level N):" when printing nested diagnostics. */
179 : : bool m_show_nesting_levels;
180 : : };
181 : :
182 : : } // namespace diagnostics
183 : :
184 : : #endif /* ! GCC_DIAGNOSTICS_TEXT_SINK_H */
|