libstdc++: Simplify std::any to fix -Wdeprecated-declarations warning
[official-gcc.git] / gcc / diagnostic.h
blob1d54e86dc0893f986156c7768e6a72c7415627c8
1 /* Various declarations for language-independent diagnostics subroutines.
2 Copyright (C) 2000-2024 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 "unique-argv.h"
25 #include "rich-location.h"
26 #include "pretty-print.h"
27 #include "diagnostic-core.h"
29 namespace text_art
31 class theme;
32 } // namespace text_art
34 /* An enum for controlling what units to use for the column number
35 when diagnostics are output, used by the -fdiagnostics-column-unit option.
36 Tabs will be expanded or not according to the value of -ftabstop. The origin
37 (default 1) is controlled by -fdiagnostics-column-origin. */
39 enum diagnostics_column_unit
41 /* The default from GCC 11 onwards: display columns. */
42 DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
44 /* The behavior in GCC 10 and earlier: simple bytes. */
45 DIAGNOSTICS_COLUMN_UNIT_BYTE
48 /* An enum for controlling how to print non-ASCII characters/bytes when
49 a diagnostic suggests escaping the source code on output. */
51 enum diagnostics_escape_format
53 /* Escape non-ASCII Unicode characters in the form <U+XXXX> and
54 non-UTF-8 bytes in the form <XX>. */
55 DIAGNOSTICS_ESCAPE_FORMAT_UNICODE,
57 /* Escape non-ASCII bytes in the form <XX> (thus showing the underlying
58 encoding of non-ASCII Unicode characters). */
59 DIAGNOSTICS_ESCAPE_FORMAT_BYTES
62 /* Enum for overriding the standard output format. */
64 enum diagnostics_output_format
66 /* The default: textual output. */
67 DIAGNOSTICS_OUTPUT_FORMAT_TEXT,
69 /* JSON-based output, to stderr. */
70 DIAGNOSTICS_OUTPUT_FORMAT_JSON_STDERR,
72 /* JSON-based output, to a file. */
73 DIAGNOSTICS_OUTPUT_FORMAT_JSON_FILE,
75 /* SARIF-based output, to stderr. */
76 DIAGNOSTICS_OUTPUT_FORMAT_SARIF_STDERR,
78 /* SARIF-based output, to a file. */
79 DIAGNOSTICS_OUTPUT_FORMAT_SARIF_FILE
82 /* An enum for controlling how diagnostic_paths should be printed. */
83 enum diagnostic_path_format
85 /* Don't print diagnostic_paths. */
86 DPF_NONE,
88 /* Print diagnostic_paths by emitting a separate "note" for every event
89 in the path. */
90 DPF_SEPARATE_EVENTS,
92 /* Print diagnostic_paths by consolidating events together where they
93 are close enough, and printing such runs of events with multiple
94 calls to diagnostic_show_locus, showing the individual events in
95 each run via labels in the source. */
96 DPF_INLINE_EVENTS
99 /* An enum for capturing values of GCC_EXTRA_DIAGNOSTIC_OUTPUT,
100 and for -fdiagnostics-parseable-fixits. */
102 enum diagnostics_extra_output_kind
104 /* No extra output, or an unrecognized value. */
105 EXTRA_DIAGNOSTIC_OUTPUT_none,
107 /* Emit fix-it hints using the "fixits-v1" format, equivalent to
108 -fdiagnostics-parseable-fixits. */
109 EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1,
111 /* Emit fix-it hints using the "fixits-v2" format. */
112 EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2
115 /* Values for -fdiagnostics-text-art-charset=. */
117 enum diagnostic_text_art_charset
119 /* No text art diagrams shall be emitted. */
120 DIAGNOSTICS_TEXT_ART_CHARSET_NONE,
122 /* Use pure ASCII for text art diagrams. */
123 DIAGNOSTICS_TEXT_ART_CHARSET_ASCII,
125 /* Use ASCII + conservative use of other unicode characters
126 in text art diagrams. */
127 DIAGNOSTICS_TEXT_ART_CHARSET_UNICODE,
129 /* Use Emoji. */
130 DIAGNOSTICS_TEXT_ART_CHARSET_EMOJI
133 /* A diagnostic is described by the MESSAGE to send, the FILE and LINE of
134 its context and its KIND (ice, error, warning, note, ...) See complete
135 list in diagnostic.def. */
136 struct diagnostic_info
138 diagnostic_info ()
139 : message (), richloc (), metadata (), x_data (), kind (), option_index (),
140 m_iinfo ()
143 /* Text to be formatted. */
144 text_info message;
146 /* The location at which the diagnostic is to be reported. */
147 rich_location *richloc;
149 /* An optional bundle of metadata associated with the diagnostic
150 (or NULL). */
151 const diagnostic_metadata *metadata;
153 /* Auxiliary data for client. */
154 void *x_data;
155 /* The kind of diagnostic it is about. */
156 diagnostic_t kind;
157 /* Which OPT_* directly controls this diagnostic. */
158 int option_index;
160 /* Inlining context containing locations for each call site along
161 the inlining stack. */
162 struct inlining_info
164 /* Locations along the inlining stack. */
165 auto_vec<location_t, 8> m_ilocs;
166 /* The abstract origin of the location. */
167 void *m_ao;
168 /* Set if every M_ILOCS element is in a system header. */
169 bool m_allsyslocs;
170 } m_iinfo;
173 /* Forward declarations. */
174 typedef void (*diagnostic_starter_fn) (diagnostic_context *,
175 const diagnostic_info *);
177 typedef void (*diagnostic_start_span_fn) (diagnostic_context *,
178 expanded_location);
180 typedef void (*diagnostic_finalizer_fn) (diagnostic_context *,
181 const diagnostic_info *,
182 diagnostic_t);
184 typedef int (*diagnostic_option_enabled_cb) (int, unsigned, void *);
185 typedef char *(*diagnostic_make_option_name_cb) (const diagnostic_context *,
186 int,
187 diagnostic_t,
188 diagnostic_t);
189 typedef char *(*diagnostic_make_option_url_cb) (const diagnostic_context *,
190 int,
191 unsigned);
193 class edit_context;
194 namespace json { class value; }
195 class diagnostic_client_data_hooks;
196 class logical_location;
197 class diagnostic_diagram;
198 class diagnostic_source_effect_info;
199 class diagnostic_output_format;
200 class diagnostic_text_output_format;
202 /* A stack of sets of classifications: each entry in the stack is
203 a mapping from option index to diagnostic severity that can be changed
204 via pragmas. The stack can be pushed and popped. */
206 class diagnostic_option_classifier
208 public:
209 void init (int n_opts);
210 void fini ();
212 /* Save all diagnostic classifications in a stack. */
213 void push ();
215 /* Restore the topmost classification set off the stack. If the stack
216 is empty, revert to the state based on command line parameters. */
217 void pop (location_t where);
219 bool option_unspecified_p (int opt) const
221 return get_current_override (opt) == DK_UNSPECIFIED;
224 diagnostic_t get_current_override (int opt) const
226 gcc_assert (opt < m_n_opts);
227 return m_classify_diagnostic[opt];
230 diagnostic_t
231 classify_diagnostic (const diagnostic_context *context,
232 int option_index,
233 diagnostic_t new_kind,
234 location_t where);
236 diagnostic_t
237 update_effective_level_from_pragmas (diagnostic_info *diagnostic) const;
239 private:
240 /* Each time a diagnostic's classification is changed with a pragma,
241 we record the change and the location of the change in an array of
242 these structs. */
243 struct diagnostic_classification_change_t
245 location_t location;
246 int option;
247 diagnostic_t kind;
250 int m_n_opts;
252 /* For each option index that can be passed to warning() et al
253 (OPT_* from options.h when using this code with the core GCC
254 options), this array may contain a new kind that the diagnostic
255 should be changed to before reporting, or DK_UNSPECIFIED to leave
256 it as the reported kind, or DK_IGNORED to not report it at
257 all. */
258 diagnostic_t *m_classify_diagnostic;
260 /* History of all changes to the classifications above. This list
261 is stored in location-order, so we can search it, either
262 binary-wise or end-to-front, to find the most recent
263 classification for a given diagnostic, given the location of the
264 diagnostic. */
265 diagnostic_classification_change_t *m_classification_history;
267 /* The size of the above array. */
268 int m_n_classification_history;
270 /* For pragma push/pop. */
271 int *m_push_list;
272 int m_n_push;
275 /* A bundle of options relating to printing the user's source code
276 (potentially with a margin, underlining, labels, etc). */
278 struct diagnostic_source_printing_options
280 /* True if we should print the source line with a caret indicating
281 the location.
282 Corresponds to -fdiagnostics-show-caret. */
283 bool enabled;
285 /* Maximum width of the source line printed. */
286 int max_width;
288 /* Character used at the caret when printing source locations. */
289 char caret_chars[rich_location::STATICALLY_ALLOCATED_RANGES];
291 /* When printing source code, should the characters at carets and ranges
292 be colorized? (assuming colorization is on at all).
293 This should be true for frontends that generate range information
294 (so that the ranges of code are colorized),
295 and false for frontends that merely specify points within the
296 source code (to avoid e.g. colorizing just the first character in
297 a token, which would look strange). */
298 bool colorize_source_p;
300 /* When printing source code, should labelled ranges be printed?
301 Corresponds to -fdiagnostics-show-labels. */
302 bool show_labels_p;
304 /* When printing source code, should there be a left-hand margin
305 showing line numbers?
306 Corresponds to -fdiagnostics-show-line-numbers. */
307 bool show_line_numbers_p;
309 /* If printing source code, what should the minimum width of the margin
310 be? Line numbers will be right-aligned, and padded to this width.
311 Corresponds to -fdiagnostics-minimum-margin-width=VALUE. */
312 int min_margin_width;
314 /* Usable by plugins; if true, print a debugging ruler above the
315 source output. */
316 bool show_ruler_p;
318 /* When printing events in an inline path, should we print lines
319 visualizing links between related events (e.g. for CFG paths)?
320 Corresponds to -fdiagnostics-show-event-links. */
321 bool show_event_links_p;
324 /* This data structure bundles altogether any information relevant to
325 the context of a diagnostic message. */
326 class diagnostic_context
328 public:
329 /* Give access to m_text_callbacks. */
330 friend diagnostic_starter_fn &
331 diagnostic_starter (diagnostic_context *context);
332 friend diagnostic_start_span_fn &
333 diagnostic_start_span (diagnostic_context *context);
334 friend diagnostic_finalizer_fn &
335 diagnostic_finalizer (diagnostic_context *context);
337 friend class diagnostic_text_output_format;
339 typedef void (*ice_handler_callback_t) (diagnostic_context *);
340 typedef void (*set_locations_callback_t) (diagnostic_context *,
341 diagnostic_info *);
343 void initialize (int n_opts);
344 void color_init (int value);
345 void urls_init (int value);
347 void finish ();
349 bool execution_failed_p () const;
351 void set_original_argv (unique_argv original_argv);
352 const char * const *get_original_argv ()
354 return const_cast<const char * const *> (m_original_argv);
357 void set_set_locations_callback (set_locations_callback_t cb)
359 m_set_locations_cb = cb;
362 void
363 initialize_input_context (diagnostic_input_charset_callback ccb,
364 bool should_skip_bom);
366 void begin_group ();
367 void end_group ();
369 bool warning_enabled_at (location_t loc, int opt);
371 bool option_unspecified_p (int opt) const
373 return m_option_classifier.option_unspecified_p (opt);
376 bool report_diagnostic (diagnostic_info *);
378 void report_current_module (location_t where);
380 void check_max_errors (bool flush);
381 void action_after_output (diagnostic_t diag_kind);
383 diagnostic_t
384 classify_diagnostic (int option_index,
385 diagnostic_t new_kind,
386 location_t where)
388 return m_option_classifier.classify_diagnostic (this,
389 option_index,
390 new_kind,
391 where);
394 void push_diagnostics (location_t where ATTRIBUTE_UNUSED)
396 m_option_classifier.push ();
398 void pop_diagnostics (location_t where)
400 m_option_classifier.pop (where);
403 void maybe_show_locus (const rich_location &richloc,
404 diagnostic_t diagnostic_kind,
405 pretty_printer *pp,
406 diagnostic_source_effect_info *effect_info);
408 void emit_diagram (const diagnostic_diagram &diagram);
410 const diagnostic_output_format *get_output_format () const
412 return m_output_format;
415 /* Various setters for use by option-handling logic. */
416 void set_output_format (diagnostic_output_format *output_format);
417 void set_text_art_charset (enum diagnostic_text_art_charset charset);
418 void set_client_data_hooks (diagnostic_client_data_hooks *hooks);
419 void set_urlifier (urlifier *);
420 void create_edit_context ();
421 void set_warning_as_error_requested (bool val)
423 m_warning_as_error_requested = val;
425 void set_report_bug (bool val) { m_report_bug = val; }
426 void set_extra_output_kind (enum diagnostics_extra_output_kind kind)
428 m_extra_output_kind = kind;
430 void set_show_cwe (bool val) { m_show_cwe = val; }
431 void set_show_rules (bool val) { m_show_rules = val; }
432 void set_show_highlight_colors (bool val)
434 pp_show_highlight_colors (printer) = val;
436 void set_path_format (enum diagnostic_path_format val)
438 m_path_format = val;
440 void set_show_path_depths (bool val) { m_show_path_depths = val; }
441 void set_show_option_requested (bool val) { m_show_option_requested = val; }
442 void set_max_errors (int val) { m_max_errors = val; }
443 void set_escape_format (enum diagnostics_escape_format val)
445 m_escape_format = val;
447 void set_ice_handler_callback (ice_handler_callback_t cb)
449 m_ice_handler_cb = cb;
452 /* Various accessors. */
453 bool warning_as_error_requested_p () const
455 return m_warning_as_error_requested;
457 bool show_path_depths_p () const { return m_show_path_depths; }
458 enum diagnostic_path_format get_path_format () const { return m_path_format; }
459 enum diagnostics_escape_format get_escape_format () const
461 return m_escape_format;
464 file_cache &
465 get_file_cache () const
467 gcc_assert (m_file_cache);
468 return *m_file_cache;
471 edit_context *get_edit_context () const
473 return m_edit_context_ptr;
475 const diagnostic_client_data_hooks *get_client_data_hooks ()
477 return m_client_data_hooks;
479 urlifier *get_urlifier () const { return m_urlifier; }
480 text_art::theme *get_diagram_theme () const { return m_diagrams.m_theme; }
482 int converted_column (expanded_location s) const;
484 int &diagnostic_count (diagnostic_t kind)
486 return m_diagnostic_count[kind];
489 /* Option-related member functions. */
490 inline bool option_enabled_p (int option_index) const
492 if (!m_option_callbacks.m_option_enabled_cb)
493 return true;
494 return m_option_callbacks.m_option_enabled_cb
495 (option_index,
496 m_option_callbacks.m_lang_mask,
497 m_option_callbacks.m_option_state);
500 inline char *make_option_name (int option_index,
501 diagnostic_t orig_diag_kind,
502 diagnostic_t diag_kind) const
504 if (!m_option_callbacks.m_make_option_name_cb)
505 return nullptr;
506 return m_option_callbacks.m_make_option_name_cb (this, option_index,
507 orig_diag_kind,
508 diag_kind);
511 inline char *make_option_url (int option_index) const
513 if (!m_option_callbacks.m_make_option_url_cb)
514 return nullptr;
515 return m_option_callbacks.m_make_option_url_cb (this, option_index,
516 get_lang_mask ());
519 void
520 set_option_hooks (diagnostic_option_enabled_cb option_enabled_cb,
521 void *option_state,
522 diagnostic_make_option_name_cb make_option_name_cb,
523 diagnostic_make_option_url_cb make_option_url_cb,
524 unsigned lang_mask);
526 unsigned get_lang_mask () const
528 return m_option_callbacks.m_lang_mask;
531 label_text get_location_text (const expanded_location &s) const;
533 bool diagnostic_impl (rich_location *, const diagnostic_metadata *,
534 int, const char *,
535 va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0);
536 bool diagnostic_n_impl (rich_location *, const diagnostic_metadata *,
537 int, unsigned HOST_WIDE_INT,
538 const char *, const char *, va_list *,
539 diagnostic_t) ATTRIBUTE_GCC_DIAG(7,0);
541 private:
542 bool includes_seen_p (const line_map_ordinary *map);
544 void show_any_path (const diagnostic_info &diagnostic);
546 void error_recursion () ATTRIBUTE_NORETURN;
548 bool diagnostic_enabled (diagnostic_info *diagnostic);
550 void get_any_inlining_info (diagnostic_info *diagnostic);
552 void show_locus (const rich_location &richloc,
553 diagnostic_t diagnostic_kind,
554 pretty_printer *pp,
555 diagnostic_source_effect_info *effect_info);
557 void print_path (const diagnostic_path &path);
559 /* Data members.
560 Ideally, all of these would be private and have "m_" prefixes. */
562 public:
563 /* Where most of the diagnostic formatting work is done. */
564 pretty_printer *printer;
566 private:
567 /* Cache of source code. */
568 file_cache *m_file_cache;
570 /* The number of times we have issued diagnostics. */
571 int m_diagnostic_count[DK_LAST_DIAGNOSTIC_KIND];
573 /* True if it has been requested that warnings be treated as errors. */
574 bool m_warning_as_error_requested;
576 /* The number of option indexes that can be passed to warning() et
577 al. */
578 int m_n_opts;
580 /* The stack of sets of overridden diagnostic option severities. */
581 diagnostic_option_classifier m_option_classifier;
583 /* True if we should print any CWE identifiers associated with
584 diagnostics. */
585 bool m_show_cwe;
587 /* True if we should print any rules associated with diagnostics. */
588 bool m_show_rules;
590 /* How should diagnostic_path objects be printed. */
591 enum diagnostic_path_format m_path_format;
593 /* True if we should print stack depths when printing diagnostic paths. */
594 bool m_show_path_depths;
596 /* True if we should print the command line option which controls
597 each diagnostic, if known. */
598 bool m_show_option_requested;
600 public:
601 /* True if we should raise a SIGABRT on errors. */
602 bool m_abort_on_error;
604 /* True if we should show the column number on diagnostics. */
605 bool m_show_column;
607 /* True if pedwarns are errors. */
608 bool m_pedantic_errors;
610 /* True if permerrors are warnings. */
611 bool m_permissive;
613 /* The index of the option to associate with turning permerrors into
614 warnings. */
615 int m_opt_permissive;
617 /* True if errors are fatal. */
618 bool m_fatal_errors;
620 /* True if all warnings should be disabled. */
621 bool m_inhibit_warnings;
623 /* True if warnings should be given in system headers. */
624 bool m_warn_system_headers;
626 private:
627 /* Maximum number of errors to report. */
628 int m_max_errors;
630 /* Client-supplied callbacks for use in text output. */
631 struct {
632 /* This function is called before any message is printed out. It is
633 responsible for preparing message prefix and such. For example, it
634 might say:
635 In file included from "/usr/local/include/curses.h:5:
636 from "/home/gdr/src/nifty_printer.h:56:
639 diagnostic_starter_fn m_begin_diagnostic;
641 /* This function is called by diagnostic_show_locus in between
642 disjoint spans of source code, so that the context can print
643 something to indicate that a new span of source code has begun. */
644 diagnostic_start_span_fn m_start_span;
646 /* This function is called after the diagnostic message is printed. */
647 diagnostic_finalizer_fn m_end_diagnostic;
648 } m_text_callbacks;
650 public:
651 /* Client hook to report an internal error. */
652 void (*m_internal_error) (diagnostic_context *, const char *, va_list *);
654 /* Client hook to adjust properties of the given diagnostic that we're
655 about to issue, such as its kind. */
656 void (*m_adjust_diagnostic_info)(diagnostic_context *, diagnostic_info *);
658 private:
659 /* Client-supplied callbacks for working with options. */
660 struct {
661 /* Client hook to say whether the option controlling a diagnostic is
662 enabled. Returns nonzero if enabled, zero if disabled. */
663 diagnostic_option_enabled_cb m_option_enabled_cb;
665 /* Client information to pass as second argument to
666 m_option_enabled_cb. */
667 void *m_option_state;
669 /* Client hook to return the name of an option that controls a
670 diagnostic. Returns malloced memory. The first diagnostic_t
671 argument is the kind of diagnostic before any reclassification
672 (of warnings as errors, etc.); the second is the kind after any
673 reclassification. May return NULL if no name is to be printed.
674 May be passed 0 as well as the index of a particular option. */
675 diagnostic_make_option_name_cb m_make_option_name_cb;
677 /* Client hook to return a URL describing the option that controls
678 a diagnostic. Returns malloced memory. May return NULL if no URL
679 is available. May be passed 0 as well as the index of a
680 particular option. */
681 diagnostic_make_option_url_cb m_make_option_url_cb;
683 /* A copy of lang_hooks.option_lang_mask (). */
684 unsigned m_lang_mask;
685 } m_option_callbacks;
687 /* An optional hook for adding URLs to quoted text strings in
688 diagnostics. Only used for the main diagnostic message. */
689 urlifier *m_urlifier;
691 public:
692 /* Auxiliary data for client. */
693 void *m_client_aux_data;
695 /* Used to detect that the last caret was printed at the same location. */
696 location_t m_last_location;
698 private:
699 /* Used to detect when the input file stack has changed since last
700 described. */
701 const line_map_ordinary *m_last_module;
703 int m_lock;
705 public:
706 bool m_inhibit_notes_p;
708 diagnostic_source_printing_options m_source_printing;
710 private:
711 /* True if -freport-bug option is used. */
712 bool m_report_bug;
714 /* Used to specify additional diagnostic output to be emitted after the
715 rest of the diagnostic. This is for implementing
716 -fdiagnostics-parseable-fixits and GCC_EXTRA_DIAGNOSTIC_OUTPUT. */
717 enum diagnostics_extra_output_kind m_extra_output_kind;
719 public:
720 /* What units to use when outputting the column number. */
721 enum diagnostics_column_unit m_column_unit;
723 /* The origin for the column number (1-based or 0-based typically). */
724 int m_column_origin;
726 /* The size of the tabstop for tab expansion. */
727 int m_tabstop;
729 private:
730 /* How should non-ASCII/non-printable bytes be escaped when
731 a diagnostic suggests escaping the source code on output. */
732 enum diagnostics_escape_format m_escape_format;
734 /* If non-NULL, an edit_context to which fix-it hints should be
735 applied, for generating patches. */
736 edit_context *m_edit_context_ptr;
738 /* Fields relating to diagnostic groups. */
739 struct {
740 /* How many diagnostic_group instances are currently alive. */
741 int m_nesting_depth;
743 /* How many diagnostics have been emitted since the bottommost
744 diagnostic_group was pushed. */
745 int m_emission_count;
746 } m_diagnostic_groups;
748 /* How to output diagnostics (text vs a structured format such as JSON).
749 Must be non-NULL; owned by context. */
750 diagnostic_output_format *m_output_format;
752 /* Callback to set the locations of call sites along the inlining
753 stack corresponding to a diagnostic location. Needed to traverse
754 the BLOCK_SUPERCONTEXT() chain hanging off the LOCATION_BLOCK()
755 of a diagnostic's location. */
756 set_locations_callback_t m_set_locations_cb;
758 /* Optional callback for attempting to handle ICEs gracefully. */
759 ice_handler_callback_t m_ice_handler_cb;
761 /* Include files that diagnostic_report_current_module has already listed the
762 include path for. */
763 hash_set<location_t, false, location_hash> *m_includes_seen;
765 /* A bundle of hooks for providing data to the context about its client
766 e.g. version information, plugins, etc.
767 Used by SARIF output to give metadata about the client that's
768 producing diagnostics. */
769 diagnostic_client_data_hooks *m_client_data_hooks;
771 /* Support for diagrams. */
772 struct
774 /* Theme to use when generating diagrams.
775 Can be NULL (if text art is disabled). */
776 text_art::theme *m_theme;
778 } m_diagrams;
780 /* Owned by the context. */
781 char **m_original_argv;
784 inline void
785 diagnostic_inhibit_notes (diagnostic_context * context)
787 context->m_inhibit_notes_p = true;
791 /* Client supplied function to announce a diagnostic
792 (for text-based diagnostic output). */
793 inline diagnostic_starter_fn &
794 diagnostic_starter (diagnostic_context *context)
796 return context->m_text_callbacks.m_begin_diagnostic;
799 /* Client supplied function called between disjoint spans of source code,
800 so that the context can print
801 something to indicate that a new span of source code has begun. */
802 inline diagnostic_start_span_fn &
803 diagnostic_start_span (diagnostic_context *context)
805 return context->m_text_callbacks.m_start_span;
808 /* Client supplied function called after a diagnostic message is
809 displayed (for text-based diagnostic output). */
810 inline diagnostic_finalizer_fn &
811 diagnostic_finalizer (diagnostic_context *context)
813 return context->m_text_callbacks.m_end_diagnostic;
816 /* Extension hooks for client. */
817 #define diagnostic_context_auxiliary_data(DC) (DC)->m_client_aux_data
818 #define diagnostic_info_auxiliary_data(DI) (DI)->x_data
820 /* Same as pp_format_decoder. Works on 'diagnostic_context *'. */
821 #define diagnostic_format_decoder(DC) pp_format_decoder ((DC)->printer)
823 /* Same as pp_prefixing_rule. Works on 'diagnostic_context *'. */
824 #define diagnostic_prefixing_rule(DC) pp_prefixing_rule ((DC)->printer)
826 /* Raise SIGABRT on any diagnostic of severity DK_ERROR or higher. */
827 inline void
828 diagnostic_abort_on_error (diagnostic_context *context)
830 context->m_abort_on_error = true;
833 /* This diagnostic_context is used by front-ends that directly output
834 diagnostic messages without going through `error', `warning',
835 and similar functions. */
836 extern diagnostic_context *global_dc;
838 /* Returns whether the diagnostic framework has been intialized already and is
839 ready for use. */
840 inline bool
841 diagnostic_ready_p ()
843 return global_dc->printer != nullptr;
846 /* The number of errors that have been issued so far. Ideally, these
847 would take a diagnostic_context as an argument. */
848 #define errorcount global_dc->diagnostic_count (DK_ERROR)
849 /* Similarly, but for warnings. */
850 #define warningcount global_dc->diagnostic_count (DK_WARNING)
851 /* Similarly, but for warnings promoted to errors. */
852 #define werrorcount global_dc->diagnostic_count (DK_WERROR)
853 /* Similarly, but for sorrys. */
854 #define sorrycount global_dc->diagnostic_count (DK_SORRY)
856 /* Returns nonzero if warnings should be emitted. */
857 #define diagnostic_report_warnings_p(DC, LOC) \
858 (!(DC)->m_inhibit_warnings \
859 && !(in_system_header_at (LOC) && !(DC)->m_warn_system_headers))
861 /* Override the option index to be used for reporting a
862 diagnostic. */
864 inline void
865 diagnostic_override_option_index (diagnostic_info *info, int optidx)
867 info->option_index = optidx;
870 /* Diagnostic related functions. */
872 inline void
873 diagnostic_initialize (diagnostic_context *context, int n_opts)
875 context->initialize (n_opts);
878 inline void
879 diagnostic_color_init (diagnostic_context *context, int value = -1)
881 context->color_init (value);
884 inline void
885 diagnostic_urls_init (diagnostic_context *context, int value = -1)
887 context->urls_init (value);
890 inline void
891 diagnostic_finish (diagnostic_context *context)
893 context->finish ();
896 inline void
897 diagnostic_report_current_module (diagnostic_context *context,
898 location_t where)
900 context->report_current_module (where);
903 inline void
904 diagnostic_show_locus (diagnostic_context *context,
905 rich_location *richloc,
906 diagnostic_t diagnostic_kind,
907 pretty_printer *pp = nullptr,
908 diagnostic_source_effect_info *effect_info = nullptr)
910 gcc_assert (richloc);
911 context->maybe_show_locus (*richloc, diagnostic_kind, pp, effect_info);
914 /* Because we read source files a second time after the frontend did it the
915 first time, we need to know how the frontend handled things like character
916 set conversion and UTF-8 BOM stripping, in order to make everything
917 consistent. This function needs to be called by each frontend that requires
918 non-default behavior, to inform the diagnostics infrastructure how input is
919 to be processed. The default behavior is to do no conversion and not to
920 strip a UTF-8 BOM.
922 The callback should return the input charset to be used to convert the given
923 file's contents to UTF-8, or it should return NULL if no conversion is needed
924 for this file. SHOULD_SKIP_BOM only applies in case no conversion was
925 performed, and if true, it will cause a UTF-8 BOM to be skipped at the
926 beginning of the file. (In case a conversion was performed, the BOM is
927 rather skipped as part of the conversion process.) */
929 inline void
930 diagnostic_initialize_input_context (diagnostic_context *context,
931 diagnostic_input_charset_callback ccb,
932 bool should_skip_bom)
934 context->initialize_input_context (ccb, should_skip_bom);
937 /* Force diagnostics controlled by OPTIDX to be kind KIND. */
938 inline diagnostic_t
939 diagnostic_classify_diagnostic (diagnostic_context *context,
940 int optidx,
941 diagnostic_t kind,
942 location_t where)
944 return context->classify_diagnostic (optidx, kind, where);
947 inline void
948 diagnostic_push_diagnostics (diagnostic_context *context,
949 location_t where)
951 context->push_diagnostics (where);
953 inline void
954 diagnostic_pop_diagnostics (diagnostic_context *context,
955 location_t where)
957 context->pop_diagnostics (where);
960 /* Report a diagnostic message (an error or a warning) as specified by
961 DC. This function is *the* subroutine in terms of which front-ends
962 should implement their specific diagnostic handling modules. The
963 front-end independent format specifiers are exactly those described
964 in the documentation of output_format.
965 Return true if a diagnostic was printed, false otherwise. */
967 inline bool
968 diagnostic_report_diagnostic (diagnostic_context *context,
969 diagnostic_info *diagnostic)
971 context->begin_group ();
972 bool warned = context->report_diagnostic (diagnostic);
973 context->end_group ();
974 return warned;
977 #ifdef ATTRIBUTE_GCC_DIAG
978 extern void diagnostic_set_info (diagnostic_info *, const char *, va_list *,
979 rich_location *, diagnostic_t) ATTRIBUTE_GCC_DIAG(2,0);
980 extern void diagnostic_set_info_translated (diagnostic_info *, const char *,
981 va_list *, rich_location *,
982 diagnostic_t)
983 ATTRIBUTE_GCC_DIAG(2,0);
984 extern void diagnostic_append_note (diagnostic_context *, location_t,
985 const char *, ...) ATTRIBUTE_GCC_DIAG(3,4);
986 #endif
987 extern char *diagnostic_build_prefix (diagnostic_context *, const diagnostic_info *);
988 void default_diagnostic_starter (diagnostic_context *, const diagnostic_info *);
989 void default_diagnostic_start_span_fn (diagnostic_context *,
990 expanded_location);
991 void default_diagnostic_finalizer (diagnostic_context *,
992 const diagnostic_info *,
993 diagnostic_t);
994 void diagnostic_set_caret_max_width (diagnostic_context *context, int value);
996 inline void
997 diagnostic_action_after_output (diagnostic_context *context,
998 diagnostic_t diag_kind)
1000 context->action_after_output (diag_kind);
1003 inline void
1004 diagnostic_check_max_errors (diagnostic_context *context, bool flush = false)
1006 context->check_max_errors (flush);
1009 int get_terminal_width (void);
1011 /* Return the location associated to this diagnostic. Parameter WHICH
1012 specifies which location. By default, expand the first one. */
1014 inline location_t
1015 diagnostic_location (const diagnostic_info * diagnostic, int which = 0)
1017 return diagnostic->message.get_location (which);
1020 /* Return the number of locations to be printed in DIAGNOSTIC. */
1022 inline unsigned int
1023 diagnostic_num_locations (const diagnostic_info * diagnostic)
1025 return diagnostic->message.m_richloc->get_num_locations ();
1028 /* Expand the location of this diagnostic. Use this function for
1029 consistency. Parameter WHICH specifies which location. By default,
1030 expand the first one. */
1032 inline expanded_location
1033 diagnostic_expand_location (const diagnostic_info * diagnostic, int which = 0)
1035 return diagnostic->richloc->get_expanded_location (which);
1038 /* This is somehow the right-side margin of a caret line, that is, we
1039 print at least these many characters after the position pointed at
1040 by the caret. */
1041 const int CARET_LINE_MARGIN = 10;
1043 /* Return true if the two locations can be represented within the same
1044 caret line. This is used to build a prefix and also to determine
1045 whether to print one or two caret lines. */
1047 inline bool
1048 diagnostic_same_line (const diagnostic_context *context,
1049 expanded_location s1, expanded_location s2)
1051 return (s2.column && s1.line == s2.line
1052 && (context->m_source_printing.max_width - CARET_LINE_MARGIN
1053 > abs (s1.column - s2.column)));
1056 extern const char *diagnostic_get_color_for_kind (diagnostic_t kind);
1058 /* Pure text formatting support functions. */
1059 extern char *file_name_as_prefix (diagnostic_context *, const char *);
1061 extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
1063 /* Compute the number of digits in the decimal representation of an integer. */
1064 extern int num_digits (int);
1066 inline bool
1067 warning_enabled_at (location_t loc, int opt)
1069 return global_dc->warning_enabled_at (loc, opt);
1072 inline bool
1073 option_unspecified_p (int opt)
1075 return global_dc->option_unspecified_p (opt);
1078 extern char *get_cwe_url (int cwe);
1080 extern const char *get_diagnostic_kind_text (diagnostic_t kind);
1082 #endif /* ! GCC_DIAGNOSTIC_H */