GCC Middle and Back End API Reference
text-sink.h
Go to the documentation of this file.
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
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_TEXT_SINK_H
22#define GCC_DIAGNOSTICS_TEXT_SINK_H
23
24#include "diagnostics/sink.h"
25
26namespace 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
34class text_sink : public sink
35{
36public:
38 source_printing_options *source_printing = nullptr,
39 bool follows_reference_printer = false)
40 : sink (dc),
41 m_saved_output_buffer (nullptr),
42 m_column_policy (dc),
43 m_last_module (nullptr),
44 m_includes_seen (nullptr),
45 m_source_printing (source_printing
46 ? *source_printing
48 m_follows_reference_printer (follows_reference_printer),
51 {}
52 ~text_sink ();
53
54 text_sink *dyn_cast_text_sink () final override { return this; }
55
56 void dump_kind (FILE *out) const override
57 {
58 fprintf (out, "text_sink");
59 }
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 void on_begin_group () override {}
67 void on_end_group () override {}
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 bool machine_readable_stderr_p () const final override
74 {
75 return false;
76 }
77 bool follows_reference_printer_p () const final override;
78
79 void update_printer () override;
80
81 void
83 final override
84 {
85 // no-op for text
86 }
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 bool show_column_p () const { return get_context ().m_show_column; }
102
104 {
105 return m_column_policy;
106 }
108
109 bool show_nesting_p () const { return m_show_nesting; }
111 {
113 }
114
115 void set_show_nesting (bool show_nesting) { m_show_nesting = show_nesting; }
117 {
119 }
120 void set_show_nesting_levels (bool show_nesting_levels)
121 {
122 m_show_nesting_levels = show_nesting_levels;
123 }
124
125 label_text get_location_text (const expanded_location &s) const;
126
135
136 static const char *maybe_line_and_column (int line, int col);
137
138protected:
142 enum kind orig_diag_kind);
143
144 bool includes_seen_p (const line_map_ordinary *map);
145
146 /* For handling diagnostics::buffer. */
148
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. */
158
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. */
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. */
173
174 /* Set to false to suppress location-printing when showing nested
175 diagnostics, for use in DejaGnu tests. */
177
178 /* If true, then add "(level N):" when printing nested diagnostics. */
180};
181
182} // namespace diagnostics
183
184#endif /* ! GCC_DIAGNOSTICS_TEXT_SINK_H */
Definition diagnostics/context.h:94
Definition diagnostics/context.h:253
bool m_show_column
Definition diagnostics/context.h:683
Definition diagram.h:35
Definition diagnostics/context.h:116
Definition buffering.h:98
context & get_context() const
Definition sink.h:83
sink(context &dc)
Definition sink.h:96
void DEBUG_FUNCTION dump() const
Definition sink.h:91
void on_begin_group() override
Definition text-sink.h:66
void on_report_verbatim(text_info &) final override
Definition text-sink.cc:262
void on_end_group() override
Definition text-sink.h:67
void print_option_information(const diagnostic_info &diagnostic, enum kind orig_diag_kind)
Definition text-sink.cc:553
text_sink(context &dc, source_printing_options *source_printing=nullptr, bool follows_reference_printer=false)
Definition text-sink.h:37
text_sink * dyn_cast_text_sink() final override
Definition text-sink.h:54
bool show_nesting_p() const
Definition text-sink.h:109
void char * file_name_as_prefix(const char *) const
Definition text-sink.cc:340
bool show_locations_in_nesting_p() const
Definition text-sink.h:110
void print_any_rules(const diagnostic_info &diagnostic)
Definition text-sink.cc:513
void dump_kind(FILE *out) const override
Definition text-sink.h:56
source_printing_options & m_source_printing
Definition text-sink.h:159
const line_map_ordinary * m_last_module
Definition text-sink.h:153
char * build_prefix(const diagnostic_info &) const
Definition text-sink.cc:300
void set_show_nesting(bool show_nesting)
Definition text-sink.h:115
column_policy m_column_policy
Definition text-sink.h:149
void set_show_locations_in_nesting(bool val)
Definition text-sink.h:116
bool m_follows_reference_printer
Definition text-sink.h:167
location_print_policy get_location_print_policy() const
static const char * maybe_line_and_column(int line, int col)
Definition text-sink.cc:623
void on_diagram(const diagram &d) override
Definition text-sink.cc:269
bool follows_reference_printer_p() const final override
Definition text-sink.cc:447
bool show_column_p() const
Definition text-sink.h:101
std::unique_ptr< per_sink_buffer > make_per_sink_buffer() final override
Definition text-sink.cc:197
void print_any_cwe(const diagnostic_info &diagnostic)
Definition text-sink.cc:478
const column_policy & get_column_policy() const
Definition text-sink.h:103
bool includes_seen_p(const line_map_ordinary *map)
Definition text-sink.cc:584
output_buffer * m_saved_output_buffer
Definition text-sink.h:147
void after_diagnostic(const diagnostic_info &) override
Definition text-sink.cc:286
char * build_indent_prefix(bool with_bullet) const
Definition text-sink.cc:392
hash_set< location_t, false, location_hash > * m_includes_seen
Definition text-sink.h:157
const source_printing_options & get_source_printing_options() const
Definition text-sink.h:131
~text_sink()
Definition text-sink.cc:134
void update_printer() override
Definition text-sink.cc:453
bool machine_readable_stderr_p() const final override
Definition text-sink.h:73
void set_show_nesting_levels(bool show_nesting_levels)
Definition text-sink.h:120
void set_buffer(per_sink_buffer *) final override
Definition text-sink.cc:177
bool m_show_nesting
Definition text-sink.h:172
label_text get_location_text(const expanded_location &s) const
Definition text-sink.cc:607
void append_note(location_t location, const char *gmsgid,...) ATTRIBUTE_GCC_DIAG(3
Definition text-sink.cc:417
void report_global_digraph(const lazily_created< digraphs::digraph > &) final override
Definition text-sink.h:82
source_printing_options & get_source_printing_options()
Definition text-sink.h:127
bool m_show_locations_in_nesting
Definition text-sink.h:176
void on_report_diagnostic(const diagnostic_info &, enum kind orig_diag_kind) override
Definition text-sink.cc:206
bool m_show_nesting_levels
Definition text-sink.h:179
void report_current_module(location_t where)
Definition text-sink.cc:640
Definition digraph.h:81
Definition hash-set.h:37
Definition lazily-created.h:31
Definition pretty-print.h:84
static struct path_prefix cpath path
Definition collect2.cc:514
static struct string2counter_map map[debug_counter_number_of_counters]
Definition dbgcnt.cc:39
#define ATTRIBUTE_GCC_DIAG(m, n)
Definition diagnostic-core.h:71
void final(rtx_insn *first, FILE *file, int optimize_p)
Definition final.cc:2009
static void print_path(const char *path)
Definition genextract.cc:312
Definition diagnostics/context.h:39
Definition event-id.h:64
Definition coretypes.h:167
kind
Definition kinds.h:27
Definition libgdiagnostics.cc:1249
Definition diagnostic-info.h:32
Definition source-printing-options.h:29
Definition pretty-print.h:34
#define false
Definition system.h:888