c++: Tweaks for -Wredundant-move [PR107363]
[official-gcc.git] / gcc / diagnostic.h
blobae6f2dfb7f43e36224b767ade795aaa4fbfca5dc
1 /* Various declarations for language-independent diagnostics subroutines.
2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5 This file is part of GCC.
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.
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.
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/>. */
21 #ifndef GCC_DIAGNOSTIC_H
22 #define GCC_DIAGNOSTIC_H
24 #include "pretty-print.h"
25 #include "diagnostic-core.h"
27 /* An enum for controlling what units to use for the column number
28 when diagnostics are output, used by the -fdiagnostics-column-unit option.
29 Tabs will be expanded or not according to the value of -ftabstop. The origin
30 (default 1) is controlled by -fdiagnostics-column-origin. */
32 enum diagnostics_column_unit
34 /* The default from GCC 11 onwards: display columns. */
35 DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
37 /* The behavior in GCC 10 and earlier: simple bytes. */
38 DIAGNOSTICS_COLUMN_UNIT_BYTE
41 /* An enum for controlling how to print non-ASCII characters/bytes when
42 a diagnostic suggests escaping the source code on output. */
44 enum diagnostics_escape_format
46 /* Escape non-ASCII Unicode characters in the form <U+XXXX> and
47 non-UTF-8 bytes in the form <XX>. */
48 DIAGNOSTICS_ESCAPE_FORMAT_UNICODE,
50 /* Escape non-ASCII bytes in the form <XX> (thus showing the underlying
51 encoding of non-ASCII Unicode characters). */
52 DIAGNOSTICS_ESCAPE_FORMAT_BYTES
55 /* Enum for overriding the standard output format. */
57 enum diagnostics_output_format
59 /* The default: textual output. */
60 DIAGNOSTICS_OUTPUT_FORMAT_TEXT,
62 /* JSON-based output, to stderr. */
63 DIAGNOSTICS_OUTPUT_FORMAT_JSON_STDERR,
65 /* JSON-based output, to a file. */
66 DIAGNOSTICS_OUTPUT_FORMAT_JSON_FILE,
68 /* SARIF-based output, to stderr. */
69 DIAGNOSTICS_OUTPUT_FORMAT_SARIF_STDERR,
71 /* SARIF-based output, to a file. */
72 DIAGNOSTICS_OUTPUT_FORMAT_SARIF_FILE
75 /* An enum for controlling how diagnostic_paths should be printed. */
76 enum diagnostic_path_format
78 /* Don't print diagnostic_paths. */
79 DPF_NONE,
81 /* Print diagnostic_paths by emitting a separate "note" for every event
82 in the path. */
83 DPF_SEPARATE_EVENTS,
85 /* Print diagnostic_paths by consolidating events together where they
86 are close enough, and printing such runs of events with multiple
87 calls to diagnostic_show_locus, showing the individual events in
88 each run via labels in the source. */
89 DPF_INLINE_EVENTS
92 /* An enum for capturing values of GCC_EXTRA_DIAGNOSTIC_OUTPUT,
93 and for -fdiagnostics-parseable-fixits. */
95 enum diagnostics_extra_output_kind
97 /* No extra output, or an unrecognized value. */
98 EXTRA_DIAGNOSTIC_OUTPUT_none,
100 /* Emit fix-it hints using the "fixits-v1" format, equivalent to
101 -fdiagnostics-parseable-fixits. */
102 EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1,
104 /* Emit fix-it hints using the "fixits-v2" format. */
105 EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2
108 /* A diagnostic is described by the MESSAGE to send, the FILE and LINE of
109 its context and its KIND (ice, error, warning, note, ...) See complete
110 list in diagnostic.def. */
111 struct diagnostic_info
113 diagnostic_info ()
114 : message (), richloc (), metadata (), x_data (), kind (), option_index (),
115 m_iinfo ()
118 /* Text to be formatted. */
119 text_info message;
121 /* The location at which the diagnostic is to be reported. */
122 rich_location *richloc;
124 /* An optional bundle of metadata associated with the diagnostic
125 (or NULL). */
126 const diagnostic_metadata *metadata;
128 /* Auxiliary data for client. */
129 void *x_data;
130 /* The kind of diagnostic it is about. */
131 diagnostic_t kind;
132 /* Which OPT_* directly controls this diagnostic. */
133 int option_index;
135 /* Inlining context containing locations for each call site along
136 the inlining stack. */
137 struct inlining_info
139 /* Locations along the inlining stack. */
140 auto_vec<location_t, 8> m_ilocs;
141 /* The abstract origin of the location. */
142 void *m_ao;
143 /* Set if every M_ILOCS element is in a system header. */
144 bool m_allsyslocs;
145 } m_iinfo;
148 /* Each time a diagnostic's classification is changed with a pragma,
149 we record the change and the location of the change in an array of
150 these structs. */
151 struct diagnostic_classification_change_t
153 location_t location;
154 int option;
155 diagnostic_t kind;
158 /* Forward declarations. */
159 typedef void (*diagnostic_starter_fn) (diagnostic_context *,
160 diagnostic_info *);
162 typedef void (*diagnostic_start_span_fn) (diagnostic_context *,
163 expanded_location);
165 typedef void (*diagnostic_finalizer_fn) (diagnostic_context *,
166 diagnostic_info *,
167 diagnostic_t);
169 class edit_context;
170 namespace json { class value; }
171 class diagnostic_client_data_hooks;
172 class logical_location;
174 /* This data structure bundles altogether any information relevant to
175 the context of a diagnostic message. */
176 struct diagnostic_context
178 /* Where most of the diagnostic formatting work is done. */
179 pretty_printer *printer;
181 /* Cache of source code. */
182 file_cache *m_file_cache;
184 /* The number of times we have issued diagnostics. */
185 int diagnostic_count[DK_LAST_DIAGNOSTIC_KIND];
187 /* True if it has been requested that warnings be treated as errors. */
188 bool warning_as_error_requested;
190 /* The number of option indexes that can be passed to warning() et
191 al. */
192 int n_opts;
194 /* For each option index that can be passed to warning() et al
195 (OPT_* from options.h when using this code with the core GCC
196 options), this array may contain a new kind that the diagnostic
197 should be changed to before reporting, or DK_UNSPECIFIED to leave
198 it as the reported kind, or DK_IGNORED to not report it at
199 all. */
200 diagnostic_t *classify_diagnostic;
202 /* History of all changes to the classifications above. This list
203 is stored in location-order, so we can search it, either
204 binary-wise or end-to-front, to find the most recent
205 classification for a given diagnostic, given the location of the
206 diagnostic. */
207 diagnostic_classification_change_t *classification_history;
209 /* The size of the above array. */
210 int n_classification_history;
212 /* For pragma push/pop. */
213 int *push_list;
214 int n_push;
216 /* True if we should print the source line with a caret indicating
217 the location. */
218 bool show_caret;
220 /* Maximum width of the source line printed. */
221 int caret_max_width;
223 /* Character used for caret diagnostics. */
224 char caret_chars[rich_location::STATICALLY_ALLOCATED_RANGES];
226 /* True if we should print any CWE identifiers associated with
227 diagnostics. */
228 bool show_cwe;
230 /* True if we should print any rules associated with diagnostics. */
231 bool show_rules;
233 /* How should diagnostic_path objects be printed. */
234 enum diagnostic_path_format path_format;
236 /* True if we should print stack depths when printing diagnostic paths. */
237 bool show_path_depths;
239 /* True if we should print the command line option which controls
240 each diagnostic, if known. */
241 bool show_option_requested;
243 /* True if we should raise a SIGABRT on errors. */
244 bool abort_on_error;
246 /* True if we should show the column number on diagnostics. */
247 bool show_column;
249 /* True if pedwarns are errors. */
250 bool pedantic_errors;
252 /* True if permerrors are warnings. */
253 bool permissive;
255 /* The index of the option to associate with turning permerrors into
256 warnings. */
257 int opt_permissive;
259 /* True if errors are fatal. */
260 bool fatal_errors;
262 /* True if all warnings should be disabled. */
263 bool dc_inhibit_warnings;
265 /* True if warnings should be given in system headers. */
266 bool dc_warn_system_headers;
268 /* Maximum number of errors to report. */
269 int max_errors;
271 /* This function is called before any message is printed out. It is
272 responsible for preparing message prefix and such. For example, it
273 might say:
274 In file included from "/usr/local/include/curses.h:5:
275 from "/home/gdr/src/nifty_printer.h:56:
278 diagnostic_starter_fn begin_diagnostic;
280 /* This function is called by diagnostic_show_locus in between
281 disjoint spans of source code, so that the context can print
282 something to indicate that a new span of source code has begun. */
283 diagnostic_start_span_fn start_span;
285 /* This function is called after the diagnostic message is printed. */
286 diagnostic_finalizer_fn end_diagnostic;
288 /* Client hook to report an internal error. */
289 void (*internal_error) (diagnostic_context *, const char *, va_list *);
291 /* Client hook to say whether the option controlling a diagnostic is
292 enabled. Returns nonzero if enabled, zero if disabled. */
293 int (*option_enabled) (int, unsigned, void *);
295 /* Client information to pass as second argument to
296 option_enabled. */
297 void *option_state;
299 /* Client hook to return the name of an option that controls a
300 diagnostic. Returns malloced memory. The first diagnostic_t
301 argument is the kind of diagnostic before any reclassification
302 (of warnings as errors, etc.); the second is the kind after any
303 reclassification. May return NULL if no name is to be printed.
304 May be passed 0 as well as the index of a particular option. */
305 char *(*option_name) (diagnostic_context *, int, diagnostic_t, diagnostic_t);
307 /* Client hook to return a URL describing the option that controls
308 a diagnostic. Returns malloced memory. May return NULL if no URL
309 is available. May be passed 0 as well as the index of a
310 particular option. */
311 char *(*get_option_url) (diagnostic_context *, int);
313 void (*print_path) (diagnostic_context *, const diagnostic_path *);
314 json::value *(*make_json_for_path) (diagnostic_context *, const diagnostic_path *);
316 /* Auxiliary data for client. */
317 void *x_data;
319 /* Used to detect that the last caret was printed at the same location. */
320 location_t last_location;
322 /* Used to detect when the input file stack has changed since last
323 described. */
324 const line_map_ordinary *last_module;
326 int lock;
328 /* A copy of lang_hooks.option_lang_mask (). */
329 unsigned lang_mask;
331 bool inhibit_notes_p;
333 /* When printing source code, should the characters at carets and ranges
334 be colorized? (assuming colorization is on at all).
335 This should be true for frontends that generate range information
336 (so that the ranges of code are colorized),
337 and false for frontends that merely specify points within the
338 source code (to avoid e.g. colorizing just the first character in
339 a token, which would look strange). */
340 bool colorize_source_p;
342 /* When printing source code, should labelled ranges be printed? */
343 bool show_labels_p;
345 /* When printing source code, should there be a left-hand margin
346 showing line numbers? */
347 bool show_line_numbers_p;
349 /* If printing source code, what should the minimum width of the margin
350 be? Line numbers will be right-aligned, and padded to this width. */
351 int min_margin_width;
353 /* Usable by plugins; if true, print a debugging ruler above the
354 source output. */
355 bool show_ruler_p;
357 /* True if -freport-bug option is used. */
358 bool report_bug;
360 /* Used to specify additional diagnostic output to be emitted after the
361 rest of the diagnostic. This is for implementing
362 -fdiagnostics-parseable-fixits and GCC_EXTRA_DIAGNOSTIC_OUTPUT. */
363 enum diagnostics_extra_output_kind extra_output_kind;
365 /* What units to use when outputting the column number. */
366 enum diagnostics_column_unit column_unit;
368 /* The origin for the column number (1-based or 0-based typically). */
369 int column_origin;
371 /* The size of the tabstop for tab expansion. */
372 int tabstop;
374 /* How should non-ASCII/non-printable bytes be escaped when
375 a diagnostic suggests escaping the source code on output. */
376 enum diagnostics_escape_format escape_format;
378 /* If non-NULL, an edit_context to which fix-it hints should be
379 applied, for generating patches. */
380 edit_context *edit_context_ptr;
382 /* How many diagnostic_group instances are currently alive. */
383 int diagnostic_group_nesting_depth;
385 /* How many diagnostics have been emitted since the bottommost
386 diagnostic_group was pushed. */
387 int diagnostic_group_emission_count;
389 /* Optional callbacks for handling diagnostic groups. */
391 /* If non-NULL, this will be called immediately before the first
392 time a diagnostic is emitted within a stack of groups. */
393 void (*begin_group_cb) (diagnostic_context * context);
395 /* If non-NULL, this will be called when a stack of groups is
396 popped if any diagnostics were emitted within that group. */
397 void (*end_group_cb) (diagnostic_context * context);
399 /* Callback for final cleanup. */
400 void (*final_cb) (diagnostic_context *context);
402 /* Callback to set the locations of call sites along the inlining
403 stack corresponding to a diagnostic location. Needed to traverse
404 the BLOCK_SUPERCONTEXT() chain hanging off the LOCATION_BLOCK()
405 of a diagnostic's location. */
406 void (*set_locations_cb)(diagnostic_context *, diagnostic_info *);
408 /* Include files that diagnostic_report_current_module has already listed the
409 include path for. */
410 hash_set<location_t, false, location_hash> *includes_seen;
412 /* A bundle of hooks for providing data to the context about its client
413 e.g. version information, plugins, etc.
414 Used by SARIF output to give metadata about the client that's
415 producing diagnostics. */
416 diagnostic_client_data_hooks *m_client_data_hooks;
419 static inline void
420 diagnostic_inhibit_notes (diagnostic_context * context)
422 context->inhibit_notes_p = true;
426 /* Client supplied function to announce a diagnostic. */
427 #define diagnostic_starter(DC) (DC)->begin_diagnostic
429 /* Client supplied function called after a diagnostic message is
430 displayed. */
431 #define diagnostic_finalizer(DC) (DC)->end_diagnostic
433 /* Extension hooks for client. */
434 #define diagnostic_context_auxiliary_data(DC) (DC)->x_data
435 #define diagnostic_info_auxiliary_data(DI) (DI)->x_data
437 /* Same as pp_format_decoder. Works on 'diagnostic_context *'. */
438 #define diagnostic_format_decoder(DC) ((DC)->printer->format_decoder)
440 /* Same as output_prefixing_rule. Works on 'diagnostic_context *'. */
441 #define diagnostic_prefixing_rule(DC) ((DC)->printer->wrapping.rule)
443 /* Raise SIGABRT on any diagnostic of severity DK_ERROR or higher. */
444 #define diagnostic_abort_on_error(DC) \
445 (DC)->abort_on_error = true
447 /* This diagnostic_context is used by front-ends that directly output
448 diagnostic messages without going through `error', `warning',
449 and similar functions. */
450 extern diagnostic_context *global_dc;
452 /* Returns whether the diagnostic framework has been intialized already and is
453 ready for use. */
454 #define diagnostic_ready_p() (global_dc->printer != NULL)
456 /* The total count of a KIND of diagnostics emitted so far. */
457 #define diagnostic_kind_count(DC, DK) (DC)->diagnostic_count[(int) (DK)]
459 /* The number of errors that have been issued so far. Ideally, these
460 would take a diagnostic_context as an argument. */
461 #define errorcount diagnostic_kind_count (global_dc, DK_ERROR)
462 /* Similarly, but for warnings. */
463 #define warningcount diagnostic_kind_count (global_dc, DK_WARNING)
464 /* Similarly, but for warnings promoted to errors. */
465 #define werrorcount diagnostic_kind_count (global_dc, DK_WERROR)
466 /* Similarly, but for sorrys. */
467 #define sorrycount diagnostic_kind_count (global_dc, DK_SORRY)
469 /* Returns nonzero if warnings should be emitted. */
470 #define diagnostic_report_warnings_p(DC, LOC) \
471 (!(DC)->dc_inhibit_warnings \
472 && !(in_system_header_at (LOC) && !(DC)->dc_warn_system_headers))
474 /* Override the option index to be used for reporting a
475 diagnostic. */
477 static inline void
478 diagnostic_override_option_index (diagnostic_info *info, int optidx)
480 info->option_index = optidx;
483 /* Diagnostic related functions. */
484 extern void diagnostic_initialize (diagnostic_context *, int);
485 extern void diagnostic_color_init (diagnostic_context *, int value = -1);
486 extern void diagnostic_urls_init (diagnostic_context *, int value = -1);
487 extern void diagnostic_finish (diagnostic_context *);
488 extern void diagnostic_report_current_module (diagnostic_context *, location_t);
489 extern void diagnostic_show_locus (diagnostic_context *,
490 rich_location *richloc,
491 diagnostic_t diagnostic_kind);
492 extern void diagnostic_show_any_path (diagnostic_context *, diagnostic_info *);
494 /* Because we read source files a second time after the frontend did it the
495 first time, we need to know how the frontend handled things like character
496 set conversion and UTF-8 BOM stripping, in order to make everything
497 consistent. This function needs to be called by each frontend that requires
498 non-default behavior, to inform the diagnostics infrastructure how input is
499 to be processed. The default behavior is to do no conversion and not to
500 strip a UTF-8 BOM.
502 The callback should return the input charset to be used to convert the given
503 file's contents to UTF-8, or it should return NULL if no conversion is needed
504 for this file. SHOULD_SKIP_BOM only applies in case no conversion was
505 performed, and if true, it will cause a UTF-8 BOM to be skipped at the
506 beginning of the file. (In case a conversion was performed, the BOM is
507 rather skipped as part of the conversion process.) */
509 void diagnostic_initialize_input_context (diagnostic_context *context,
510 diagnostic_input_charset_callback ccb,
511 bool should_skip_bom);
513 /* Force diagnostics controlled by OPTIDX to be kind KIND. */
514 extern diagnostic_t diagnostic_classify_diagnostic (diagnostic_context *,
515 int /* optidx */,
516 diagnostic_t /* kind */,
517 location_t);
518 extern void diagnostic_push_diagnostics (diagnostic_context *, location_t);
519 extern void diagnostic_pop_diagnostics (diagnostic_context *, location_t);
520 extern bool diagnostic_report_diagnostic (diagnostic_context *,
521 diagnostic_info *);
522 #ifdef ATTRIBUTE_GCC_DIAG
523 extern void diagnostic_set_info (diagnostic_info *, const char *, va_list *,
524 rich_location *, diagnostic_t) ATTRIBUTE_GCC_DIAG(2,0);
525 extern void diagnostic_set_info_translated (diagnostic_info *, const char *,
526 va_list *, rich_location *,
527 diagnostic_t)
528 ATTRIBUTE_GCC_DIAG(2,0);
529 extern void diagnostic_append_note (diagnostic_context *, location_t,
530 const char *, ...) ATTRIBUTE_GCC_DIAG(3,4);
531 #endif
532 extern char *diagnostic_build_prefix (diagnostic_context *, const diagnostic_info *);
533 void default_diagnostic_starter (diagnostic_context *, diagnostic_info *);
534 void default_diagnostic_start_span_fn (diagnostic_context *,
535 expanded_location);
536 void default_diagnostic_finalizer (diagnostic_context *, diagnostic_info *,
537 diagnostic_t);
538 void diagnostic_set_caret_max_width (diagnostic_context *context, int value);
539 void diagnostic_action_after_output (diagnostic_context *, diagnostic_t);
540 void diagnostic_check_max_errors (diagnostic_context *, bool flush = false);
542 void diagnostic_file_cache_fini (void);
544 int get_terminal_width (void);
546 /* Return the location associated to this diagnostic. Parameter WHICH
547 specifies which location. By default, expand the first one. */
549 static inline location_t
550 diagnostic_location (const diagnostic_info * diagnostic, int which = 0)
552 return diagnostic->message.get_location (which);
555 /* Return the number of locations to be printed in DIAGNOSTIC. */
557 static inline unsigned int
558 diagnostic_num_locations (const diagnostic_info * diagnostic)
560 return diagnostic->message.m_richloc->get_num_locations ();
563 /* Expand the location of this diagnostic. Use this function for
564 consistency. Parameter WHICH specifies which location. By default,
565 expand the first one. */
567 static inline expanded_location
568 diagnostic_expand_location (const diagnostic_info * diagnostic, int which = 0)
570 return diagnostic->richloc->get_expanded_location (which);
573 /* This is somehow the right-side margin of a caret line, that is, we
574 print at least these many characters after the position pointed at
575 by the caret. */
576 const int CARET_LINE_MARGIN = 10;
578 /* Return true if the two locations can be represented within the same
579 caret line. This is used to build a prefix and also to determine
580 whether to print one or two caret lines. */
582 static inline bool
583 diagnostic_same_line (const diagnostic_context *context,
584 expanded_location s1, expanded_location s2)
586 return s2.column && s1.line == s2.line
587 && context->caret_max_width - CARET_LINE_MARGIN > abs (s1.column - s2.column);
590 extern const char *diagnostic_get_color_for_kind (diagnostic_t kind);
591 extern int diagnostic_converted_column (diagnostic_context *context,
592 expanded_location s);
594 /* Pure text formatting support functions. */
595 extern char *file_name_as_prefix (diagnostic_context *, const char *);
597 extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
599 extern void diagnostic_output_format_init (diagnostic_context *,
600 const char *base_file_name,
601 enum diagnostics_output_format);
602 extern void diagnostic_output_format_init_json_stderr (diagnostic_context *context);
603 extern void diagnostic_output_format_init_json_file (diagnostic_context *context,
604 const char *base_file_name);
605 extern void diagnostic_output_format_init_sarif_stderr (diagnostic_context *context);
606 extern void diagnostic_output_format_init_sarif_file (diagnostic_context *context,
607 const char *base_file_name);
609 /* Compute the number of digits in the decimal representation of an integer. */
610 extern int num_digits (int);
612 extern json::value *json_from_expanded_location (diagnostic_context *context,
613 location_t loc);
615 extern bool warning_enabled_at (location_t, int);
617 extern char *get_cwe_url (int cwe);
619 #endif /* ! GCC_DIAGNOSTIC_H */