GCC Middle and Back End API Reference
diagnostic.h
Go to the documentation of this file.
1/* Various declarations for language-independent diagnostics subroutines.
2 Copyright (C) 2000-2025 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.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_DIAGNOSTIC_H
22#define GCC_DIAGNOSTIC_H
23
24#include "rich-location.h"
25#include "pretty-print.h"
26#include "diagnostic-core.h"
27
29#include "diagnostics/context.h"
30
31/* Extension hooks for client. */
32#define diagnostic_context_auxiliary_data(DC) (DC)->m_client_aux_data
33#define diagnostic_info_auxiliary_data(DI) (DI)->m_x_data
34
35/* This diagnostics::context is used by front-ends that directly output
36 diagnostic messages without going through `error', `warning',
37 and similar functions. */
39
40/* The number of errors that have been issued so far. Ideally, these
41 would take a diagnostics::context as an argument. */
42#define errorcount global_dc->diagnostic_count (diagnostics::kind::error)
43/* Similarly, but for warnings. */
44#define warningcount global_dc->diagnostic_count (diagnostics::kind::warning)
45/* Similarly, but for warnings promoted to errors. */
46#define werrorcount global_dc->diagnostic_count (diagnostics::kind::werror)
47/* Similarly, but for sorrys. */
48#define sorrycount global_dc->diagnostic_count (diagnostics::kind::sorry)
49
50/* Returns nonzero if warnings should be emitted. */
51#define diagnostic_report_warnings_p(DC, LOC) \
52 (!(DC)->m_inhibit_warnings \
53 && !(in_system_header_at (LOC) && !(DC)->m_warn_system_headers))
54
55/* Override the option index to be used for reporting a
56 diagnostic. */
57
58inline void
64
65/* Diagnostic related functions. */
66
67inline void
69{
70 context->initialize (n_opts);
71}
72
73inline void
75{
76 context->color_init (value);
77}
78
79inline void
81{
82 context->urls_init (value);
83}
84
85inline void
87{
88 context->finish ();
89}
90
91inline void
94 rich_location *richloc,
95 enum diagnostics::kind diagnostic_kind,
97 diagnostics::source_effect_info *effect_info = nullptr)
98{
99 gcc_assert (context);
100 gcc_assert (richloc);
101 gcc_assert (pp);
102 context->maybe_show_locus (*richloc, opts, diagnostic_kind, *pp, effect_info);
103}
104
105inline void
108 rich_location *richloc,
109 enum diagnostics::kind diagnostic_kind,
110 xml::printer &xp,
111 diagnostics::source_effect_info *effect_info = nullptr,
112 diagnostics::html_label_writer *label_writer = nullptr)
113{
114 gcc_assert (context);
115 gcc_assert (richloc);
116 context->maybe_show_locus_as_html (*richloc, opts, diagnostic_kind, xp,
117 effect_info, label_writer);
118}
119
120/* Because we read source files a second time after the frontend did it the
121 first time, we need to know how the frontend handled things like character
122 set conversion and UTF-8 BOM stripping, in order to make everything
123 consistent. This function needs to be called by each frontend that requires
124 non-default behavior, to inform the diagnostics infrastructure how input is
125 to be processed. The default behavior is to do no conversion and not to
126 strip a UTF-8 BOM.
127
128 The callback should return the input charset to be used to convert the given
129 file's contents to UTF-8, or it should return NULL if no conversion is needed
130 for this file. SHOULD_SKIP_BOM only applies in case no conversion was
131 performed, and if true, it will cause a UTF-8 BOM to be skipped at the
132 beginning of the file. (In case a conversion was performed, the BOM is
133 rather skipped as part of the conversion process.) */
134
135inline void
138 bool should_skip_bom)
139{
140 context->initialize_input_context (ccb, should_skip_bom);
141}
142
143/* Force diagnostics controlled by OPTIDX to be kind KIND. */
147 enum diagnostics::kind kind,
148 location_t where)
149{
150 return context->classify_diagnostic (opt_id, kind, where);
151}
152
153inline void
155 location_t where)
156{
157 context->push_diagnostics (where);
158}
159inline void
161 location_t where)
162{
163 context->pop_diagnostics (where);
164}
165
166/* Report a diagnostic message (an error or a warning) as specified by
167 DC. This function is *the* subroutine in terms of which front-ends
168 should implement their specific diagnostic handling modules. The
169 front-end independent format specifiers are exactly those described
170 in the documentation of output_format.
171 Return true if a diagnostic was printed, false otherwise. */
172
173inline bool
176{
177 context->begin_group ();
178 bool warned = context->report_diagnostic (diagnostic);
179 context->end_group ();
180 return warned;
181}
182
183#ifdef ATTRIBUTE_GCC_DIAG
185 const char *, va_list *,
186 rich_location *,
190 const char *, va_list *,
191 rich_location *,
194#endif
195
196namespace diagnostics {
197
200template <typename TextOrHtml>
202 TextOrHtml &text_or_html,
203 expanded_location);
206 enum diagnostics::kind);
207} // namespace diagnostics
208
209int get_terminal_width (void);
210
211/* Return the location associated to this diagnostic. Parameter WHICH
212 specifies which location. By default, expand the first one. */
213
214inline location_t
216 int which = 0)
217{
218 return diagnostic->m_message.get_location (which);
219}
220
221/* Return the number of locations to be printed in DIAGNOSTIC. */
222
223inline unsigned int
225{
226 return diagnostic->m_message.m_richloc->get_num_locations ();
227}
228
229/* Expand the location of this diagnostic. Use this function for
230 consistency. Parameter WHICH specifies which location. By default,
231 expand the first one. */
232
233inline expanded_location
235 int which = 0)
236{
237 return diagnostic->m_richloc->get_expanded_location (which);
238}
239
240/* This is somehow the right-side margin of a caret line, that is, we
241 print at least these many characters after the position pointed at
242 by the caret. */
243const int CARET_LINE_MARGIN = 10;
244
245/* Return true if the two locations can be represented within the same
246 caret line. This is used to build a prefix and also to determine
247 whether to print one or two caret lines. */
248
249inline bool
251 expanded_location s1, expanded_location s2)
252{
253 return (s2.column && s1.line == s2.line
256 > abs (s1.column - s2.column)));
257}
258
259/* Pure text formatting support functions. */
260
261extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
262
263inline bool
264warning_enabled_at (location_t loc, diagnostics::option_id opt_id)
265{
266 return global_dc->warning_enabled_at (loc, opt_id);
267}
268
269inline bool
271{
272 return global_dc->option_unspecified_p (opt_id);
273}
274
275namespace diagnostics {
276
277/* Compute the number of digits in the decimal representation of an integer. */
278extern int num_digits (int);
279
280} // namespace diagnostics
281
282#endif /* ! GCC_DIAGNOSTIC_H */
Definition diagnostics/context.h:254
void maybe_show_locus(const rich_location &richloc, const source_printing_options &opts, enum kind diagnostic_kind, pretty_printer &pp, source_effect_info *effect_info)
Definition source-printing.cc:3859
void finish()
Definition diagnostics/context.cc:353
void push_diagnostics(location_t where)
Definition diagnostics/context.h:355
void initialize_input_context(diagnostic_input_charset_callback ccb, bool should_skip_bom)
Definition diagnostics/context.cc:344
void end_group()
Definition diagnostics/context.cc:1817
void color_init(int value)
Definition diagnostics/context.cc:255
source_printing_options & get_source_printing_options()
Definition diagnostics/context.h:608
bool bool bool report_diagnostic(diagnostic_info *)
Definition diagnostics/context.cc:1347
void initialize(int n_opts)
Definition diagnostics/context.cc:138
enum kind classify_diagnostic(option_id opt_id, enum kind new_kind, location_t where)
Definition diagnostics/context.h:338
void pop_diagnostics(location_t where)
Definition diagnostics/context.h:364
void begin_group()
Definition diagnostics/context.cc:1811
void maybe_show_locus_as_html(const rich_location &richloc, const source_printing_options &opts, enum kind diagnostic_kind, xml::printer &xp, source_effect_info *effect_info, html_label_writer *label_writer)
Definition source-printing.cc:3892
void urls_init(int value)
Definition diagnostics/context.cc:286
Definition diagnostics/context.h:146
Definition diagnostics/context.h:117
Definition source-printing-effects.h:43
Definition text-sink.h:35
Definition pretty-print.h:241
Definition xml-printer.h:33
const char *(* diagnostic_input_charset_callback)(const char *)
Definition coretypes.h:173
#define ATTRIBUTE_GCC_DIAG(m, n)
Definition diagnostic-core.h:71
diagnostics::context * global_dc
Definition diagnostic-global-context.cc:35
void void diagnostic_set_info_translated(diagnostics::diagnostic_info *, const char *, va_list *, rich_location *, enum diagnostics::kind) ATTRIBUTE_GCC_DIAG(2
const int CARET_LINE_MARGIN
Definition diagnostic.h:243
bool diagnostic_report_diagnostic(diagnostics::context *context, diagnostics::diagnostic_info *diagnostic)
Definition diagnostic.h:174
void diagnostic_urls_init(diagnostics::context *context, int value=-1)
Definition diagnostic.h:80
void diagnostic_initialize_input_context(diagnostics::context *context, diagnostic_input_charset_callback ccb, bool should_skip_bom)
Definition diagnostic.h:136
location_t diagnostic_location(const diagnostics::diagnostic_info *diagnostic, int which=0)
Definition diagnostic.h:215
int get_terminal_width(void)
Definition diagnostics/context.cc:98
diagnostics::kind diagnostic_classify_diagnostic(diagnostics::context *context, diagnostics::option_id opt_id, enum diagnostics::kind kind, location_t where)
Definition diagnostic.h:145
void diagnostic_push_diagnostics(diagnostics::context *context, location_t where)
Definition diagnostic.h:154
void diagnostic_initialize(diagnostics::context *context, int n_opts)
Definition diagnostic.h:68
unsigned int diagnostic_num_locations(const diagnostics::diagnostic_info *diagnostic)
Definition diagnostic.h:224
bool diagnostic_same_line(const diagnostics::context *context, expanded_location s1, expanded_location s2)
Definition diagnostic.h:250
void diagnostic_show_locus(diagnostics::context *context, const diagnostics::source_printing_options &opts, rich_location *richloc, enum diagnostics::kind diagnostic_kind, pretty_printer *pp, diagnostics::source_effect_info *effect_info=nullptr)
Definition diagnostic.h:92
void diagnostic_color_init(diagnostics::context *context, int value=-1)
Definition diagnostic.h:74
void diagnostic_finish(diagnostics::context *context)
Definition diagnostic.h:86
char * build_message_string(const char *,...) ATTRIBUTE_PRINTF_1
Definition diagnostics/context.cc:81
void diagnostic_set_option_id(diagnostics::diagnostic_info *info, diagnostics::option_id opt_id)
Definition diagnostic.h:59
bool option_unspecified_p(diagnostics::option_id opt_id)
Definition diagnostic.h:270
bool warning_enabled_at(location_t loc, diagnostics::option_id opt_id)
Definition diagnostic.h:264
void diagnostic_show_locus_as_html(diagnostics::context *context, const diagnostics::source_printing_options &opts, rich_location *richloc, enum diagnostics::kind diagnostic_kind, xml::printer &xp, diagnostics::source_effect_info *effect_info=nullptr, diagnostics::html_label_writer *label_writer=nullptr)
Definition diagnostic.h:106
expanded_location diagnostic_expand_location(const diagnostics::diagnostic_info *diagnostic, int which=0)
Definition diagnostic.h:234
void diagnostic_pop_diagnostics(diagnostics::context *context, location_t where)
Definition diagnostic.h:160
void diagnostic_set_info(diagnostics::diagnostic_info *, const char *, va_list *, rich_location *, enum diagnostics::kind) ATTRIBUTE_GCC_DIAG(2
Definition coretypes.h:167
void default_start_span_fn(const diagnostics::location_print_policy &, TextOrHtml &text_or_html, expanded_location)
void default_text_finalizer(diagnostics::text_sink &, const diagnostics::diagnostic_info *, enum diagnostics::kind)
void default_text_starter(diagnostics::text_sink &, const diagnostics::diagnostic_info *)
Definition text-sink.cc:707
kind
Definition kinds.h:27
int num_digits(int)
Definition diagnostics/context.cc:1592
Definition libgdiagnostics.cc:1256
Definition diagnostic-info.h:32
option_id m_option_id
Definition diagnostic-info.h:58
Definition option-id.h:32
Definition source-printing-options.h:31
int max_width
Definition source-printing-options.h:38
#define gcc_assert(EXPR)
Definition system.h:814