]> gcc.gnu.org Git - gcc.git/blame - gcc/diagnostic.h
Add diagnostic_metadata and CWE support
[gcc.git] / gcc / diagnostic.h
CommitLineData
345ed1fe 1/* Various declarations for language-independent diagnostics subroutines.
a5544970 2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
345ed1fe
GDR
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
4
1322177d 5This file is part of GCC.
345ed1fe 6
1322177d
LB
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
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
1322177d 10version.
345ed1fe 11
1322177d
LB
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.
345ed1fe
GDR
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
345ed1fe 20
88657302
RH
21#ifndef GCC_DIAGNOSTIC_H
22#define GCC_DIAGNOSTIC_H
345ed1fe 23
b6fe0bb8 24#include "pretty-print.h"
1da2ed5f 25#include "diagnostic-core.h"
d0e66dbb 26
478dd60d
DM
27/* Enum for overriding the standard output format. */
28
29enum diagnostics_output_format
30{
31 /* The default: textual output. */
32 DIAGNOSTICS_OUTPUT_FORMAT_TEXT,
33
34 /* JSON-based output. */
35 DIAGNOSTICS_OUTPUT_FORMAT_JSON
36};
37
47b69537
GDR
38/* A diagnostic is described by the MESSAGE to send, the FILE and LINE of
39 its context and its KIND (ice, error, warning, note, ...) See complete
40 list in diagnostic.def. */
84562394 41struct diagnostic_info
47b69537 42{
8a645150 43 /* Text to be formatted. */
47b69537 44 text_info message;
8a645150
DM
45
46 /* The location at which the diagnostic is to be reported. */
47 rich_location *richloc;
48
6d4a35ca
DM
49 /* An optional bundle of metadata associated with the diagnostic
50 (or NULL). */
51 const diagnostic_metadata *metadata;
52
cf835838
JM
53 /* Auxiliary data for client. */
54 void *x_data;
47b69537
GDR
55 /* The kind of diagnostic it is about. */
56 diagnostic_t kind;
ccf08a6e
DD
57 /* Which OPT_* directly controls this diagnostic. */
58 int option_index;
84562394 59};
47b69537 60
cd7fe53b
DD
61/* Each time a diagnostic's classification is changed with a pragma,
62 we record the change and the location of the change in an array of
63 these structs. */
84562394 64struct diagnostic_classification_change_t
cd7fe53b
DD
65{
66 location_t location;
67 int option;
68 diagnostic_t kind;
84562394 69};
cd7fe53b 70
47b69537 71/* Forward declarations. */
79a490a9
AJ
72typedef void (*diagnostic_starter_fn) (diagnostic_context *,
73 diagnostic_info *);
876217ae
DM
74
75typedef void (*diagnostic_start_span_fn) (diagnostic_context *,
76 expanded_location);
77
478dd60d
DM
78typedef void (*diagnostic_finalizer_fn) (diagnostic_context *,
79 diagnostic_info *,
80 diagnostic_t);
47b69537 81
717ebe91
DM
82class edit_context;
83
c4765d18 84/* This data structure bundles altogether any information relevant to
e2840263
GDR
85 the context of a diagnostic message. */
86struct diagnostic_context
87{
2b72593e 88 /* Where most of the diagnostic formatting work is done. */
b6fe0bb8 89 pretty_printer *printer;
f68fc4db 90
4fcc2065
GDR
91 /* The number of times we have issued diagnostics. */
92 int diagnostic_count[DK_LAST_DIAGNOSTIC_KIND];
93
7783b402
GDR
94 /* True if it has been requested that warnings be treated as errors. */
95 bool warning_as_error_requested;
13f0d49c 96
5f0f4a3b
JM
97 /* The number of option indexes that can be passed to warning() et
98 al. */
99 int n_opts;
100
101 /* For each option index that can be passed to warning() et al
102 (OPT_* from options.h when using this code with the core GCC
103 options), this array may contain a new kind that the diagnostic
104 should be changed to before reporting, or DK_UNSPECIFIED to leave
105 it as the reported kind, or DK_IGNORED to not report it at
106 all. */
107 diagnostic_t *classify_diagnostic;
79cf5994 108
cd7fe53b
DD
109 /* History of all changes to the classifications above. This list
110 is stored in location-order, so we can search it, either
111 binary-wise or end-to-front, to find the most recent
112 classification for a given diagnostic, given the location of the
113 diagnostic. */
114 diagnostic_classification_change_t *classification_history;
115
116 /* The size of the above array. */
117 int n_classification_history;
118
119 /* For pragma push/pop. */
120 int *push_list;
121 int n_push;
122
9fec0042
MLI
123 /* True if we should print the source line with a caret indicating
124 the location. */
125 bool show_caret;
126
127 /* Maximum width of the source line printed. */
128 int caret_max_width;
129
8a645150 130 /* Character used for caret diagnostics. */
b816477a 131 char caret_chars[rich_location::STATICALLY_ALLOCATED_RANGES];
fbecdc83 132
6d4a35ca
DM
133 /* True if we should print any CWE identifiers associated with
134 diagnostics. */
135 bool show_cwe;
136
2098fe9e
DD
137 /* True if we should print the command line option which controls
138 each diagnostic, if known. */
139 bool show_option_requested;
140
886e0865
GK
141 /* True if we should raise a SIGABRT on errors. */
142 bool abort_on_error;
143
243fbddd
JM
144 /* True if we should show the column number on diagnostics. */
145 bool show_column;
146
147 /* True if pedwarns are errors. */
148 bool pedantic_errors;
149
150 /* True if permerrors are warnings. */
151 bool permissive;
152
5f0f4a3b
JM
153 /* The index of the option to associate with turning permerrors into
154 warnings. */
155 int opt_permissive;
156
243fbddd
JM
157 /* True if errors are fatal. */
158 bool fatal_errors;
159
5f0f4a3b 160 /* True if all warnings should be disabled. */
e3339d0f 161 bool dc_inhibit_warnings;
5f0f4a3b
JM
162
163 /* True if warnings should be given in system headers. */
e3339d0f 164 bool dc_warn_system_headers;
5f0f4a3b 165
3a789837 166 /* Maximum number of errors to report. */
d0ea9f0a 167 int max_errors;
3a789837 168
e2840263 169 /* This function is called before any message is printed out. It is
c4765d18 170 responsible for preparing message prefix and such. For example, it
e2840263
GDR
171 might say:
172 In file included from "/usr/local/include/curses.h:5:
173 from "/home/gdr/src/nifty_printer.h:56:
174 ...
175 */
47b69537 176 diagnostic_starter_fn begin_diagnostic;
e2840263 177
876217ae
DM
178 /* This function is called by diagnostic_show_locus in between
179 disjoint spans of source code, so that the context can print
180 something to indicate that a new span of source code has begun. */
181 diagnostic_start_span_fn start_span;
182
eebedaa5 183 /* This function is called after the diagnostic message is printed. */
47b69537 184 diagnostic_finalizer_fn end_diagnostic;
e2840263 185
27e511e0 186 /* Client hook to report an internal error. */
a13812e2 187 void (*internal_error) (diagnostic_context *, const char *, va_list *);
27e511e0 188
5f0f4a3b
JM
189 /* Client hook to say whether the option controlling a diagnostic is
190 enabled. Returns nonzero if enabled, zero if disabled. */
fa5baeed 191 int (*option_enabled) (int, unsigned, void *);
46625112
JM
192
193 /* Client information to pass as second argument to
194 option_enabled. */
195 void *option_state;
5f0f4a3b
JM
196
197 /* Client hook to return the name of an option that controls a
198 diagnostic. Returns malloced memory. The first diagnostic_t
199 argument is the kind of diagnostic before any reclassification
200 (of warnings as errors, etc.); the second is the kind after any
201 reclassification. May return NULL if no name is to be printed.
202 May be passed 0 as well as the index of a particular option. */
203 char *(*option_name) (diagnostic_context *, int, diagnostic_t, diagnostic_t);
204
b4c7ca2e
DM
205 /* Client hook to return a URL describing the option that controls
206 a diagnostic. Returns malloced memory. May return NULL if no URL
207 is available. May be passed 0 as well as the index of a
208 particular option. */
209 char *(*get_option_url) (diagnostic_context *, int);
210
cf835838
JM
211 /* Auxiliary data for client. */
212 void *x_data;
47b69537 213
1b8b126f
MLI
214 /* Used to detect that the last caret was printed at the same location. */
215 location_t last_location;
216
966e8f4d
TT
217 /* Used to detect when the input file stack has changed since last
218 described. */
26d5ed6c 219 const line_map_ordinary *last_module;
47b69537
GDR
220
221 int lock;
0e94b750 222
fa5baeed
MS
223 /* A copy of lang_hooks.option_lang_mask (). */
224 unsigned lang_mask;
225
0e94b750 226 bool inhibit_notes_p;
8a645150
DM
227
228 /* When printing source code, should the characters at carets and ranges
229 be colorized? (assuming colorization is on at all).
230 This should be true for frontends that generate range information
231 (so that the ranges of code are colorized),
232 and false for frontends that merely specify points within the
233 source code (to avoid e.g. colorizing just the first character in
234 a token, which would look strange). */
235 bool colorize_source_p;
f3352cab 236
96e6ae57
DM
237 /* When printing source code, should labelled ranges be printed? */
238 bool show_labels_p;
239
56b61d7f
DM
240 /* When printing source code, should there be a left-hand margin
241 showing line numbers? */
242 bool show_line_numbers_p;
243
0141ab44
DM
244 /* If printing source code, what should the minimum width of the margin
245 be? Line numbers will be right-aligned, and padded to this width. */
246 int min_margin_width;
247
f3352cab
DM
248 /* Usable by plugins; if true, print a debugging ruler above the
249 source output. */
250 bool show_ruler_p;
a93eac6a
DM
251
252 /* If true, print fixits in machine-parseable form after the
253 rest of the diagnostic. */
254 bool parseable_fixits_p;
717ebe91
DM
255
256 /* If non-NULL, an edit_context to which fix-it hints should be
257 applied, for generating patches. */
258 edit_context *edit_context_ptr;
097f82ec
DM
259
260 /* How many diagnostic_group instances are currently alive. */
261 int diagnostic_group_nesting_depth;
262
263 /* How many diagnostics have been emitted since the bottommost
264 diagnostic_group was pushed. */
265 int diagnostic_group_emission_count;
266
267 /* Optional callbacks for handling diagnostic groups. */
268
269 /* If non-NULL, this will be called immediately before the first
270 time a diagnostic is emitted within a stack of groups. */
271 void (*begin_group_cb) (diagnostic_context * context);
272
273 /* If non-NULL, this will be called when a stack of groups is
274 popped if any diagnostics were emitted within that group. */
275 void (*end_group_cb) (diagnostic_context * context);
478dd60d
DM
276
277 /* Callback for final cleanup. */
278 void (*final_cb) (diagnostic_context *context);
e2840263
GDR
279};
280
0e94b750
MLI
281static inline void
282diagnostic_inhibit_notes (diagnostic_context * context)
283{
284 context->inhibit_notes_p = true;
285}
286
287
6b67c26e 288/* Client supplied function to announce a diagnostic. */
e2840263 289#define diagnostic_starter(DC) (DC)->begin_diagnostic
6b67c26e
GDR
290
291/* Client supplied function called after a diagnostic message is
292 displayed. */
e2840263 293#define diagnostic_finalizer(DC) (DC)->end_diagnostic
6b67c26e 294
cf835838
JM
295/* Extension hooks for client. */
296#define diagnostic_context_auxiliary_data(DC) (DC)->x_data
297#define diagnostic_info_auxiliary_data(DI) (DI)->x_data
6b67c26e 298
b6fe0bb8 299/* Same as pp_format_decoder. Works on 'diagnostic_context *'. */
e1a4dd13 300#define diagnostic_format_decoder(DC) ((DC)->printer->format_decoder)
6b67c26e 301
bd6bec6b 302/* Same as output_prefixing_rule. Works on 'diagnostic_context *'. */
39ce81c9 303#define diagnostic_prefixing_rule(DC) ((DC)->printer->wrapping.rule)
e2840263 304
886e0865
GK
305/* Raise SIGABRT on any diagnostic of severity DK_ERROR or higher. */
306#define diagnostic_abort_on_error(DC) \
307 (DC)->abort_on_error = true
308
4fcc2065 309/* This diagnostic_context is used by front-ends that directly output
764dbbf2 310 diagnostic messages without going through `error', `warning',
6b67c26e 311 and similar functions. */
f68fc4db 312extern diagnostic_context *global_dc;
764dbbf2 313
f622a56b
TC
314/* Returns whether the diagnostic framework has been intialized already and is
315 ready for use. */
316#define diagnostic_ready_p() (global_dc->printer != NULL)
317
886e0865 318/* The total count of a KIND of diagnostics emitted so far. */
4fcc2065 319#define diagnostic_kind_count(DC, DK) (DC)->diagnostic_count[(int) (DK)]
2a2b2d43
GDR
320
321/* The number of errors that have been issued so far. Ideally, these
4fcc2065 322 would take a diagnostic_context as an argument. */
6b67c26e 323#define errorcount diagnostic_kind_count (global_dc, DK_ERROR)
2a2b2d43 324/* Similarly, but for warnings. */
6b67c26e 325#define warningcount diagnostic_kind_count (global_dc, DK_WARNING)
37e99116
JJ
326/* Similarly, but for warnings promoted to errors. */
327#define werrorcount diagnostic_kind_count (global_dc, DK_WERROR)
2a2b2d43 328/* Similarly, but for sorrys. */
6b67c26e 329#define sorrycount diagnostic_kind_count (global_dc, DK_SORRY)
2a2b2d43 330
cc2902df 331/* Returns nonzero if warnings should be emitted. */
5f0f4a3b 332#define diagnostic_report_warnings_p(DC, LOC) \
e3339d0f
JM
333 (!(DC)->dc_inhibit_warnings \
334 && !(in_system_header_at (LOC) && !(DC)->dc_warn_system_headers))
2a2b2d43 335
87cf0651
SB
336/* Override the option index to be used for reporting a
337 diagnostic. */
62a77bdf
DM
338
339static inline void
340diagnostic_override_option_index (diagnostic_info *info, int optidx)
341{
342 info->option_index = optidx;
343}
87cf0651 344
95bd1dd7 345/* Diagnostic related functions. */
5f0f4a3b 346extern void diagnostic_initialize (diagnostic_context *, int);
97aa8bb6 347extern void diagnostic_color_init (diagnostic_context *, int value = -1);
d2608235 348extern void diagnostic_urls_init (diagnostic_context *, int value = -1);
d0b8780d 349extern void diagnostic_finish (diagnostic_context *);
07a0b324 350extern void diagnostic_report_current_module (diagnostic_context *, location_t);
cc015f3a
DM
351extern void diagnostic_show_locus (diagnostic_context *,
352 rich_location *richloc,
353 diagnostic_t diagnostic_kind);
79cf5994
DD
354
355/* Force diagnostics controlled by OPTIDX to be kind KIND. */
356extern diagnostic_t diagnostic_classify_diagnostic (diagnostic_context *,
357 int /* optidx */,
cd7fe53b
DD
358 diagnostic_t /* kind */,
359 location_t);
360extern void diagnostic_push_diagnostics (diagnostic_context *, location_t);
361extern void diagnostic_pop_diagnostics (diagnostic_context *, location_t);
71205d17 362extern bool diagnostic_report_diagnostic (diagnostic_context *,
79a490a9 363 diagnostic_info *);
db857e7d 364#ifdef ATTRIBUTE_GCC_DIAG
79a490a9 365extern void diagnostic_set_info (diagnostic_info *, const char *, va_list *,
8a645150 366 rich_location *, diagnostic_t) ATTRIBUTE_GCC_DIAG(2,0);
178b58b5 367extern void diagnostic_set_info_translated (diagnostic_info *, const char *,
8a645150 368 va_list *, rich_location *,
178b58b5
JM
369 diagnostic_t)
370 ATTRIBUTE_GCC_DIAG(2,0);
dfa32261
MLI
371extern void diagnostic_append_note (diagnostic_context *, location_t,
372 const char *, ...) ATTRIBUTE_GCC_DIAG(3,4);
db857e7d 373#endif
dfa32261 374extern char *diagnostic_build_prefix (diagnostic_context *, const diagnostic_info *);
4537ec0c 375void default_diagnostic_starter (diagnostic_context *, diagnostic_info *);
876217ae
DM
376void default_diagnostic_start_span_fn (diagnostic_context *,
377 expanded_location);
478dd60d
DM
378void default_diagnostic_finalizer (diagnostic_context *, diagnostic_info *,
379 diagnostic_t);
9fec0042 380void diagnostic_set_caret_max_width (diagnostic_context *context, int value);
c4100eae 381void diagnostic_action_after_output (diagnostic_context *, diagnostic_t);
d0ea9f0a 382void diagnostic_check_max_errors (diagnostic_context *, bool flush = false);
9fec0042 383
7ecc3eb9 384void diagnostic_file_cache_fini (void);
47b69537 385
c9db45aa
TB
386int get_terminal_width (void);
387
2a2703a2
MLI
388/* Return the location associated to this diagnostic. Parameter WHICH
389 specifies which location. By default, expand the first one. */
390
391static inline location_t
392diagnostic_location (const diagnostic_info * diagnostic, int which = 0)
393{
394 return diagnostic->message.get_location (which);
395}
396
8a645150
DM
397/* Return the number of locations to be printed in DIAGNOSTIC. */
398
399static inline unsigned int
400diagnostic_num_locations (const diagnostic_info * diagnostic)
401{
402 return diagnostic->message.m_richloc->get_num_locations ();
403}
404
2a2703a2
MLI
405/* Expand the location of this diagnostic. Use this function for
406 consistency. Parameter WHICH specifies which location. By default,
407 expand the first one. */
99abe958
MLI
408
409static inline expanded_location
2a2703a2 410diagnostic_expand_location (const diagnostic_info * diagnostic, int which = 0)
99abe958 411{
40499f81 412 return diagnostic->richloc->get_expanded_location (which);
99abe958
MLI
413}
414
2a2703a2
MLI
415/* This is somehow the right-side margin of a caret line, that is, we
416 print at least these many characters after the position pointed at
417 by the caret. */
ba82e6b5 418const int CARET_LINE_MARGIN = 10;
2a2703a2
MLI
419
420/* Return true if the two locations can be represented within the same
421 caret line. This is used to build a prefix and also to determine
422 whether to print one or two caret lines. */
423
424static inline bool
425diagnostic_same_line (const diagnostic_context *context,
426 expanded_location s1, expanded_location s2)
427{
428 return s2.column && s1.line == s2.line
429 && context->caret_max_width - CARET_LINE_MARGIN > abs (s1.column - s2.column);
430}
431
8a645150 432extern const char *diagnostic_get_color_for_kind (diagnostic_t kind);
2a2703a2 433
47b69537 434/* Pure text formatting support functions. */
e78e8a0b 435extern char *file_name_as_prefix (diagnostic_context *, const char *);
345ed1fe 436
8e54f6d3
MLI
437extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
438
478dd60d
DM
439extern void diagnostic_output_format_init (diagnostic_context *,
440 enum diagnostics_output_format);
8e54f6d3 441
bc65bad2
MG
442/* Compute the number of digits in the decimal representation of an integer. */
443extern int num_digits (int);
444
88657302 445#endif /* ! GCC_DIAGNOSTIC_H */
This page took 5.310622 seconds and 5 git commands to generate.