| 1 |
/* Classic text-based output of diagnostics. |
| 2 |
Copyright (C) 2023-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by David Malcolm <[email protected]>. |
| 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_TEXT_H |
| 22 |
#define GCC_DIAGNOSTIC_FORMAT_TEXT_H |
| 23 |
|
| 24 |
#include "diagnostic-format.h" |
| 25 |
|
| 26 |
/* Subclass of diagnostic_output_format for classic text-based output |
| 27 |
to stderr. |
| 28 |
|
| 29 |
Uses diagnostic_context.m_text_callbacks to provide client-specific |
| 30 |
textual output (e.g. include paths, macro expansions, etc). */ |
| 31 |
|
| 32 |
class diagnostic_text_output_format : public diagnostic_output_format |
| 33 |
{ |
| 34 |
public: |
| 35 |
diagnostic_text_output_format (diagnostic_context &context, |
| 36 |
diagnostic_source_printing_options *source_printing = nullptr, |
| 37 |
bool follows_reference_printer = false) |
| 38 |
: diagnostic_output_format (context), |
| 39 |
m_saved_output_buffer (nullptr), |
| 40 |
m_column_policy (context), |
| 41 |
m_last_module (nullptr), |
| 42 |
m_includes_seen (nullptr), |
| 43 |
m_source_printing (source_printing |
| 44 |
? *source_printing |
| 45 |
: context.m_source_printing), |
| 46 |
m_follows_reference_printer (follows_reference_printer), |
| 47 |
m_show_nesting (false), |
| 48 |
m_show_nesting_levels (false) |
| 49 |
{} |
| 50 |
~diagnostic_text_output_format (); |
| 51 |
|
| 52 |
void dump (FILE *out, int indent) const override; |
| 53 |
|
| 54 |
std::unique_ptr<diagnostic_per_format_buffer> |
| 55 |
make_per_format_buffer () final override; |
| 56 |
void set_buffer (diagnostic_per_format_buffer *) final override; |
| 57 |
|
| 58 |
void on_begin_group () override {} |
| 59 |
void on_end_group () override {} |
| 60 |
void on_report_diagnostic (const diagnostic_info &, |
| 61 |
diagnostic_t orig_diag_kind) override; |
| 62 |
void on_report_verbatim (text_info &) final override; |
| 63 |
void on_diagram (const diagnostic_diagram &diagram) override; |
| 64 |
void after_diagnostic (const diagnostic_info &) override; |
| 65 |
bool machine_readable_stderr_p () const final override |
| 66 |
{ |
| 67 |
return false; |
| 68 |
} |
| 69 |
bool follows_reference_printer_p () const final override; |
| 70 |
|
| 71 |
void update_printer () override; |
| 72 |
|
| 73 |
/* Helpers for writing lang-specific starters/finalizers for text output. */ |
| 74 |
char *build_prefix (const diagnostic_info &) const; |
| 75 |
void report_current_module (location_t where); |
| 76 |
void append_note (location_t location, |
| 77 |
const char * gmsgid, ...) ATTRIBUTE_GCC_DIAG(3,4); |
| 78 |
|
| 79 |
|
| 80 |
char *file_name_as_prefix (const char *) const; |
| 81 |
|
| 82 |
char *build_indent_prefix (bool with_bullet) const; |
| 83 |
|
| 84 |
void print_path (const diagnostic_path &path); |
| 85 |
|
| 86 |
bool show_column_p () const { return get_context ().m_show_column; } |
| 87 |
|
| 88 |
const diagnostic_column_policy &get_column_policy () const |
| 89 |
{ |
| 90 |
return m_column_policy; |
| 91 |
} |
| 92 |
diagnostic_location_print_policy get_location_print_policy () const; |
| 93 |
|
| 94 |
bool show_nesting_p () const { return m_show_nesting; } |
| 95 |
bool show_locations_in_nesting_p () const |
| 96 |
{ |
| 97 |
return m_show_locations_in_nesting; |
| 98 |
} |
| 99 |
|
| 100 |
void set_show_nesting (bool show_nesting) { m_show_nesting = show_nesting; } |
| 101 |
void set_show_locations_in_nesting (bool val) |
| 102 |
{ |
| 103 |
m_show_locations_in_nesting = val; |
| 104 |
} |
| 105 |
void set_show_nesting_levels (bool show_nesting_levels) |
| 106 |
{ |
| 107 |
m_show_nesting_levels = show_nesting_levels; |
| 108 |
} |
| 109 |
|
| 110 |
label_text get_location_text (const expanded_location &s) const; |
| 111 |
|
| 112 |
diagnostic_source_printing_options &get_source_printing_options () |
| 113 |
{ |
| 114 |
return m_source_printing; |
| 115 |
} |
| 116 |
const diagnostic_source_printing_options &get_source_printing_options () const |
| 117 |
{ |
| 118 |
return m_source_printing; |
| 119 |
} |
| 120 |
|
| 121 |
protected: |
| 122 |
void print_any_cwe (const diagnostic_info &diagnostic); |
| 123 |
void print_any_rules (const diagnostic_info &diagnostic); |
| 124 |
void print_option_information (const diagnostic_info &diagnostic, |
| 125 |
diagnostic_t orig_diag_kind); |
| 126 |
|
| 127 |
bool includes_seen_p (const line_map_ordinary *map); |
| 128 |
|
| 129 |
/* For handling diagnostic_buffer. */ |
| 130 |
output_buffer *m_saved_output_buffer; |
| 131 |
|
| 132 |
diagnostic_column_policy m_column_policy; |
| 133 |
|
| 134 |
/* Used to detect when the input file stack has changed since last |
| 135 |
described. */ |
| 136 |
const line_map_ordinary *m_last_module; |
| 137 |
|
| 138 |
/* Include files that report_current_module has already listed the |
| 139 |
include path for. */ |
| 140 |
hash_set<location_t, false, location_hash> *m_includes_seen; |
| 141 |
|
| 142 |
diagnostic_source_printing_options &m_source_printing; |
| 143 |
|
| 144 |
/* If true, this is the initial default text output format created |
| 145 |
when the diagnostic_context was created, and, in particular, before |
| 146 |
initializations of color and m_url_format. Hence this should follow |
| 147 |
the dc's reference printer for these. |
| 148 |
If false, this text output was created after the dc was created, and |
| 149 |
thus tracks its own values for color and m_url_format. */ |
| 150 |
bool m_follows_reference_printer; |
| 151 |
|
| 152 |
/* If true, then use indentation to show the nesting structure of |
| 153 |
nested diagnostics, and print locations on separate lines after the |
| 154 |
diagnostic message, rather than as a prefix to the message. */ |
| 155 |
bool m_show_nesting; |
| 156 |
|
| 157 |
/* Set to false to suppress location-printing when showing nested |
| 158 |
diagnostics, for use in DejaGnu tests. */ |
| 159 |
bool m_show_locations_in_nesting; |
| 160 |
|
| 161 |
/* If true, then add "(level N):" when printing nested diagnostics. */ |
| 162 |
bool m_show_nesting_levels; |
| 163 |
}; |
| 164 |
|
| 165 |
#endif /* ! GCC_DIAGNOSTIC_FORMAT_TEXT_H */ |