Fix ICE with SLP and -fdbg-cnt
[official-gcc.git] / gcc / diagnostic.h
blobdb61b0e274771d68e27826bfb393914276df3e8c
1 /* Various declarations for language-independent diagnostics subroutines.
2 Copyright (C) 2000-2023 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 namespace text_art
29 class theme;
30 } // namespace text_art
32 /* An enum for controlling what units to use for the column number
33 when diagnostics are output, used by the -fdiagnostics-column-unit option.
34 Tabs will be expanded or not according to the value of -ftabstop. The origin
35 (default 1) is controlled by -fdiagnostics-column-origin. */
37 enum diagnostics_column_unit
39 /* The default from GCC 11 onwards: display columns. */
40 DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
42 /* The behavior in GCC 10 and earlier: simple bytes. */
43 DIAGNOSTICS_COLUMN_UNIT_BYTE
46 /* An enum for controlling how to print non-ASCII characters/bytes when
47 a diagnostic suggests escaping the source code on output. */
49 enum diagnostics_escape_format
51 /* Escape non-ASCII Unicode characters in the form <U+XXXX> and
52 non-UTF-8 bytes in the form <XX>. */
53 DIAGNOSTICS_ESCAPE_FORMAT_UNICODE,
55 /* Escape non-ASCII bytes in the form <XX> (thus showing the underlying
56 encoding of non-ASCII Unicode characters). */
57 DIAGNOSTICS_ESCAPE_FORMAT_BYTES
60 /* Enum for overriding the standard output format. */
62 enum diagnostics_output_format
64 /* The default: textual output. */
65 DIAGNOSTICS_OUTPUT_FORMAT_TEXT,
67 /* JSON-based output, to stderr. */
68 DIAGNOSTICS_OUTPUT_FORMAT_JSON_STDERR,
70 /* JSON-based output, to a file. */
71 DIAGNOSTICS_OUTPUT_FORMAT_JSON_FILE,
73 /* SARIF-based output, to stderr. */
74 DIAGNOSTICS_OUTPUT_FORMAT_SARIF_STDERR,
76 /* SARIF-based output, to a file. */
77 DIAGNOSTICS_OUTPUT_FORMAT_SARIF_FILE
80 /* An enum for controlling how diagnostic_paths should be printed. */
81 enum diagnostic_path_format
83 /* Don't print diagnostic_paths. */
84 DPF_NONE,
86 /* Print diagnostic_paths by emitting a separate "note" for every event
87 in the path. */
88 DPF_SEPARATE_EVENTS,
90 /* Print diagnostic_paths by consolidating events together where they
91 are close enough, and printing such runs of events with multiple
92 calls to diagnostic_show_locus, showing the individual events in
93 each run via labels in the source. */
94 DPF_INLINE_EVENTS
97 /* An enum for capturing values of GCC_EXTRA_DIAGNOSTIC_OUTPUT,
98 and for -fdiagnostics-parseable-fixits. */
100 enum diagnostics_extra_output_kind
102 /* No extra output, or an unrecognized value. */
103 EXTRA_DIAGNOSTIC_OUTPUT_none,
105 /* Emit fix-it hints using the "fixits-v1" format, equivalent to
106 -fdiagnostics-parseable-fixits. */
107 EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1,
109 /* Emit fix-it hints using the "fixits-v2" format. */
110 EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2
113 /* Values for -fdiagnostics-text-art-charset=. */
115 enum diagnostic_text_art_charset
117 /* No text art diagrams shall be emitted. */
118 DIAGNOSTICS_TEXT_ART_CHARSET_NONE,
120 /* Use pure ASCII for text art diagrams. */
121 DIAGNOSTICS_TEXT_ART_CHARSET_ASCII,
123 /* Use ASCII + conservative use of other unicode characters
124 in text art diagrams. */
125 DIAGNOSTICS_TEXT_ART_CHARSET_UNICODE,
127 /* Use Emoji. */
128 DIAGNOSTICS_TEXT_ART_CHARSET_EMOJI
131 /* A diagnostic is described by the MESSAGE to send, the FILE and LINE of
132 its context and its KIND (ice, error, warning, note, ...) See complete
133 list in diagnostic.def. */
134 struct diagnostic_info
136 diagnostic_info ()
137 : message (), richloc (), metadata (), x_data (), kind (), option_index (),
138 m_iinfo ()
141 /* Text to be formatted. */
142 text_info message;
144 /* The location at which the diagnostic is to be reported. */
145 rich_location *richloc;
147 /* An optional bundle of metadata associated with the diagnostic
148 (or NULL). */
149 const diagnostic_metadata *metadata;
151 /* Auxiliary data for client. */
152 void *x_data;
153 /* The kind of diagnostic it is about. */
154 diagnostic_t kind;
155 /* Which OPT_* directly controls this diagnostic. */
156 int option_index;
158 /* Inlining context containing locations for each call site along
159 the inlining stack. */
160 struct inlining_info
162 /* Locations along the inlining stack. */
163 auto_vec<location_t, 8> m_ilocs;
164 /* The abstract origin of the location. */
165 void *m_ao;
166 /* Set if every M_ILOCS element is in a system header. */
167 bool m_allsyslocs;
168 } m_iinfo;
171 /* Forward declarations. */
172 typedef void (*diagnostic_starter_fn) (diagnostic_context *,
173 diagnostic_info *);
175 typedef void (*diagnostic_start_span_fn) (diagnostic_context *,
176 expanded_location);
178 typedef void (*diagnostic_finalizer_fn) (diagnostic_context *,
179 diagnostic_info *,
180 diagnostic_t);
182 typedef int (*diagnostic_option_enabled_cb) (int, unsigned, void *);
183 typedef char *(*diagnostic_make_option_name_cb) (const diagnostic_context *,
184 int,
185 diagnostic_t,
186 diagnostic_t);
187 typedef char *(*diagnostic_make_option_url_cb) (const diagnostic_context *,
188 int);
190 class edit_context;
191 namespace json { class value; }
192 class diagnostic_client_data_hooks;
193 class logical_location;
194 class diagnostic_diagram;
196 /* Abstract base class for a particular output format for diagnostics;
197 each value of -fdiagnostics-output-format= will have its own
198 implementation. */
200 class diagnostic_output_format
202 public:
203 virtual ~diagnostic_output_format () {}
205 virtual void on_begin_group () = 0;
206 virtual void on_end_group () = 0;
207 virtual void on_begin_diagnostic (diagnostic_info *) = 0;
208 virtual void on_end_diagnostic (diagnostic_info *,
209 diagnostic_t orig_diag_kind) = 0;
210 virtual void on_diagram (const diagnostic_diagram &diagram) = 0;
212 protected:
213 diagnostic_output_format (diagnostic_context &context)
214 : m_context (context)
217 diagnostic_context &m_context;
220 /* Subclass of diagnostic_output_format for classic text-based output
221 to stderr.
223 Uses diagnostic_context.m_text_callbacks to provide client-specific
224 textual output (e.g. include paths, macro expansions, etc). */
226 class diagnostic_text_output_format : public diagnostic_output_format
228 public:
229 diagnostic_text_output_format (diagnostic_context &context)
230 : diagnostic_output_format (context)
232 ~diagnostic_text_output_format ();
233 void on_begin_group () override {}
234 void on_end_group () override {}
235 void on_begin_diagnostic (diagnostic_info *) override;
236 void on_end_diagnostic (diagnostic_info *,
237 diagnostic_t orig_diag_kind) override;
238 void on_diagram (const diagnostic_diagram &diagram) override;
241 /* A stack of sets of classifications: each entry in the stack is
242 a mapping from option index to diagnostic severity that can be changed
243 via pragmas. The stack can be pushed and popped. */
245 class diagnostic_option_classifier
247 public:
248 void init (int n_opts);
249 void fini ();
251 /* Save all diagnostic classifications in a stack. */
252 void push ();
254 /* Restore the topmost classification set off the stack. If the stack
255 is empty, revert to the state based on command line parameters. */
256 void pop (location_t where);
258 bool option_unspecified_p (int opt) const
260 return get_current_override (opt) == DK_UNSPECIFIED;
263 diagnostic_t get_current_override (int opt) const
265 gcc_assert (opt < m_n_opts);
266 return m_classify_diagnostic[opt];
269 diagnostic_t
270 classify_diagnostic (const diagnostic_context *context,
271 int option_index,
272 diagnostic_t new_kind,
273 location_t where);
275 diagnostic_t
276 update_effective_level_from_pragmas (diagnostic_info *diagnostic) const;
278 private:
279 /* Each time a diagnostic's classification is changed with a pragma,
280 we record the change and the location of the change in an array of
281 these structs. */
282 struct diagnostic_classification_change_t
284 location_t location;
285 int option;
286 diagnostic_t kind;
289 int m_n_opts;
291 /* For each option index that can be passed to warning() et al
292 (OPT_* from options.h when using this code with the core GCC
293 options), this array may contain a new kind that the diagnostic
294 should be changed to before reporting, or DK_UNSPECIFIED to leave
295 it as the reported kind, or DK_IGNORED to not report it at
296 all. */
297 diagnostic_t *m_classify_diagnostic;
299 /* History of all changes to the classifications above. This list
300 is stored in location-order, so we can search it, either
301 binary-wise or end-to-front, to find the most recent
302 classification for a given diagnostic, given the location of the
303 diagnostic. */
304 diagnostic_classification_change_t *m_classification_history;
306 /* The size of the above array. */
307 int m_n_classification_history;
309 /* For pragma push/pop. */
310 int *m_push_list;
311 int m_n_push;
314 /* A bundle of options relating to printing the user's source code
315 (potentially with a margin, underlining, labels, etc). */
317 struct diagnostic_source_printing_options
319 /* True if we should print the source line with a caret indicating
320 the location.
321 Corresponds to -fdiagnostics-show-caret. */
322 bool enabled;
324 /* Maximum width of the source line printed. */
325 int max_width;
327 /* Character used at the caret when printing source locations. */
328 char caret_chars[rich_location::STATICALLY_ALLOCATED_RANGES];
330 /* When printing source code, should the characters at carets and ranges
331 be colorized? (assuming colorization is on at all).
332 This should be true for frontends that generate range information
333 (so that the ranges of code are colorized),
334 and false for frontends that merely specify points within the
335 source code (to avoid e.g. colorizing just the first character in
336 a token, which would look strange). */
337 bool colorize_source_p;
339 /* When printing source code, should labelled ranges be printed?
340 Corresponds to -fdiagnostics-show-labels. */
341 bool show_labels_p;
343 /* When printing source code, should there be a left-hand margin
344 showing line numbers?
345 Corresponds to -fdiagnostics-show-line-numbers. */
346 bool show_line_numbers_p;
348 /* If printing source code, what should the minimum width of the margin
349 be? Line numbers will be right-aligned, and padded to this width.
350 Corresponds to -fdiagnostics-minimum-margin-width=VALUE. */
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;
358 /* This data structure bundles altogether any information relevant to
359 the context of a diagnostic message. */
360 class diagnostic_context
362 public:
363 /* Give access to m_text_callbacks. */
364 friend diagnostic_starter_fn &
365 diagnostic_starter (diagnostic_context *context);
366 friend diagnostic_start_span_fn &
367 diagnostic_start_span (diagnostic_context *context);
368 friend diagnostic_finalizer_fn &
369 diagnostic_finalizer (diagnostic_context *context);
371 typedef void (*ice_handler_callback_t) (diagnostic_context *);
372 typedef void (*set_locations_callback_t) (diagnostic_context *,
373 diagnostic_info *);
375 void initialize (int n_opts);
376 void color_init (int value);
377 void urls_init (int value);
379 void finish ();
381 void set_set_locations_callback (set_locations_callback_t cb)
383 m_set_locations_cb = cb;
386 void
387 initialize_input_context (diagnostic_input_charset_callback ccb,
388 bool should_skip_bom);
390 void begin_group ();
391 void end_group ();
393 bool warning_enabled_at (location_t loc, int opt);
395 bool option_unspecified_p (int opt) const
397 return m_option_classifier.option_unspecified_p (opt);
400 bool report_diagnostic (diagnostic_info *);
402 void report_current_module (location_t where);
404 void check_max_errors (bool flush);
405 void action_after_output (diagnostic_t diag_kind);
407 diagnostic_t
408 classify_diagnostic (int option_index,
409 diagnostic_t new_kind,
410 location_t where)
412 return m_option_classifier.classify_diagnostic (this,
413 option_index,
414 new_kind,
415 where);
418 void push_diagnostics (location_t where ATTRIBUTE_UNUSED)
420 m_option_classifier.push ();
422 void pop_diagnostics (location_t where)
424 m_option_classifier.pop (where);
427 void maybe_show_locus (const rich_location &richloc,
428 diagnostic_t diagnostic_kind,
429 pretty_printer *pp);
431 void emit_diagram (const diagnostic_diagram &diagram);
433 /* Various setters for use by option-handling logic. */
434 void set_output_format (diagnostic_output_format *output_format);
435 void set_text_art_charset (enum diagnostic_text_art_charset charset);
436 void set_client_data_hooks (diagnostic_client_data_hooks *hooks);
437 void set_urlifier (urlifier *);
438 void create_edit_context ();
439 void set_warning_as_error_requested (bool val)
441 m_warning_as_error_requested = val;
443 void set_report_bug (bool val) { m_report_bug = val; }
444 void set_extra_output_kind (enum diagnostics_extra_output_kind kind)
446 m_extra_output_kind = kind;
448 void set_show_cwe (bool val) { m_show_cwe = val; }
449 void set_show_rules (bool val) { m_show_rules = val; }
450 void set_path_format (enum diagnostic_path_format val)
452 m_path_format = val;
454 void set_show_path_depths (bool val) { m_show_path_depths = val; }
455 void set_show_option_requested (bool val) { m_show_option_requested = val; }
456 void set_max_errors (int val) { m_max_errors = val; }
457 void set_escape_format (enum diagnostics_escape_format val)
459 m_escape_format = val;
461 void set_ice_handler_callback (ice_handler_callback_t cb)
463 m_ice_handler_cb = cb;
466 /* Various accessors. */
467 bool warning_as_error_requested_p () const
469 return m_warning_as_error_requested;
471 bool show_path_depths_p () const { return m_show_path_depths; }
472 enum diagnostic_path_format get_path_format () const { return m_path_format; }
473 enum diagnostics_escape_format get_escape_format () const
475 return m_escape_format;
478 file_cache &
479 get_file_cache () const
481 gcc_assert (m_file_cache);
482 return *m_file_cache;
485 edit_context *get_edit_context () const
487 return m_edit_context_ptr;
489 const diagnostic_client_data_hooks *get_client_data_hooks ()
491 return m_client_data_hooks;
493 text_art::theme *get_diagram_theme () const { return m_diagrams.m_theme; }
495 int converted_column (expanded_location s) const;
497 int &diagnostic_count (diagnostic_t kind)
499 return m_diagnostic_count[kind];
502 /* Option-related member functions. */
503 inline bool option_enabled_p (int option_index) const
505 if (!m_option_callbacks.m_option_enabled_cb)
506 return true;
507 return m_option_callbacks.m_option_enabled_cb
508 (option_index,
509 m_lang_mask,
510 m_option_callbacks.m_option_state);
513 inline char *make_option_name (int option_index,
514 diagnostic_t orig_diag_kind,
515 diagnostic_t diag_kind) const
517 if (!m_option_callbacks.m_make_option_name_cb)
518 return nullptr;
519 return m_option_callbacks.m_make_option_name_cb (this, option_index,
520 orig_diag_kind,
521 diag_kind);
524 inline char *make_option_url (int option_index) const
526 if (!m_option_callbacks.m_make_option_url_cb)
527 return nullptr;
528 return m_option_callbacks.m_make_option_url_cb (this, option_index);
531 void
532 set_option_hooks (diagnostic_option_enabled_cb option_enabled_cb,
533 void *option_state,
534 diagnostic_make_option_name_cb make_option_name_cb,
535 diagnostic_make_option_url_cb make_option_url_cb);
537 private:
538 bool includes_seen_p (const line_map_ordinary *map);
540 void print_any_cwe (const diagnostic_info &diagnostic);
541 void print_any_rules (const diagnostic_info &diagnostic);
542 void print_option_information (const diagnostic_info &diagnostic,
543 diagnostic_t orig_diag_kind);
545 void show_any_path (const diagnostic_info &diagnostic);
547 void error_recursion () ATTRIBUTE_NORETURN;
549 bool diagnostic_enabled (diagnostic_info *diagnostic);
551 void get_any_inlining_info (diagnostic_info *diagnostic);
553 void show_locus (const rich_location &richloc,
554 diagnostic_t diagnostic_kind,
555 pretty_printer *pp);
557 /* Data members.
558 Ideally, all of these would be private and have "m_" prefixes. */
560 public:
561 /* Where most of the diagnostic formatting work is done. */
562 pretty_printer *printer;
564 private:
565 /* Cache of source code. */
566 file_cache *m_file_cache;
568 /* The number of times we have issued diagnostics. */
569 int m_diagnostic_count[DK_LAST_DIAGNOSTIC_KIND];
571 /* True if it has been requested that warnings be treated as errors. */
572 bool m_warning_as_error_requested;
574 /* The number of option indexes that can be passed to warning() et
575 al. */
576 int m_n_opts;
578 /* The stack of sets of overridden diagnostic option severities. */
579 diagnostic_option_classifier m_option_classifier;
581 /* True if we should print any CWE identifiers associated with
582 diagnostics. */
583 bool m_show_cwe;
585 /* True if we should print any rules associated with diagnostics. */
586 bool m_show_rules;
588 /* How should diagnostic_path objects be printed. */
589 enum diagnostic_path_format m_path_format;
591 /* True if we should print stack depths when printing diagnostic paths. */
592 bool m_show_path_depths;
594 /* True if we should print the command line option which controls
595 each diagnostic, if known. */
596 bool m_show_option_requested;
598 public:
599 /* True if we should raise a SIGABRT on errors. */
600 bool m_abort_on_error;
602 /* True if we should show the column number on diagnostics. */
603 bool m_show_column;
605 /* True if pedwarns are errors. */
606 bool m_pedantic_errors;
608 /* True if permerrors are warnings. */
609 bool m_permissive;
611 /* The index of the option to associate with turning permerrors into
612 warnings. */
613 int m_opt_permissive;
615 /* True if errors are fatal. */
616 bool m_fatal_errors;
618 /* True if all warnings should be disabled. */
619 bool m_inhibit_warnings;
621 /* True if warnings should be given in system headers. */
622 bool m_warn_system_headers;
624 private:
625 /* Maximum number of errors to report. */
626 int m_max_errors;
628 /* Client-supplied callbacks for use in text output. */
629 struct {
630 /* This function is called before any message is printed out. It is
631 responsible for preparing message prefix and such. For example, it
632 might say:
633 In file included from "/usr/local/include/curses.h:5:
634 from "/home/gdr/src/nifty_printer.h:56:
637 diagnostic_starter_fn m_begin_diagnostic;
639 /* This function is called by diagnostic_show_locus in between
640 disjoint spans of source code, so that the context can print
641 something to indicate that a new span of source code has begun. */
642 diagnostic_start_span_fn m_start_span;
644 /* This function is called after the diagnostic message is printed. */
645 diagnostic_finalizer_fn m_end_diagnostic;
646 } m_text_callbacks;
648 public:
649 /* Client hook to report an internal error. */
650 void (*m_internal_error) (diagnostic_context *, const char *, va_list *);
652 private:
653 /* Client-supplied callbacks for working with options. */
654 struct {
655 /* Client hook to say whether the option controlling a diagnostic is
656 enabled. Returns nonzero if enabled, zero if disabled. */
657 diagnostic_option_enabled_cb m_option_enabled_cb;
659 /* Client information to pass as second argument to
660 m_option_enabled_cb. */
661 void *m_option_state;
663 /* Client hook to return the name of an option that controls a
664 diagnostic. Returns malloced memory. The first diagnostic_t
665 argument is the kind of diagnostic before any reclassification
666 (of warnings as errors, etc.); the second is the kind after any
667 reclassification. May return NULL if no name is to be printed.
668 May be passed 0 as well as the index of a particular option. */
669 diagnostic_make_option_name_cb m_make_option_name_cb;
671 /* Client hook to return a URL describing the option that controls
672 a diagnostic. Returns malloced memory. May return NULL if no URL
673 is available. May be passed 0 as well as the index of a
674 particular option. */
675 diagnostic_make_option_url_cb m_make_option_url_cb;
676 } m_option_callbacks;
678 /* An optional hook for adding URLs to quoted text strings in
679 diagnostics. Only used for the main diagnostic message. */
680 urlifier *m_urlifier;
682 public:
683 void (*m_print_path) (diagnostic_context *, const diagnostic_path *);
684 json::value *(*m_make_json_for_path) (diagnostic_context *,
685 const diagnostic_path *);
687 /* Auxiliary data for client. */
688 void *m_client_aux_data;
690 /* Used to detect that the last caret was printed at the same location. */
691 location_t m_last_location;
693 private:
694 /* Used to detect when the input file stack has changed since last
695 described. */
696 const line_map_ordinary *m_last_module;
698 int m_lock;
700 public:
701 /* A copy of lang_hooks.option_lang_mask (). */
702 unsigned m_lang_mask;
704 bool m_inhibit_notes_p;
706 diagnostic_source_printing_options m_source_printing;
708 private:
709 /* True if -freport-bug option is used. */
710 bool m_report_bug;
712 /* Used to specify additional diagnostic output to be emitted after the
713 rest of the diagnostic. This is for implementing
714 -fdiagnostics-parseable-fixits and GCC_EXTRA_DIAGNOSTIC_OUTPUT. */
715 enum diagnostics_extra_output_kind m_extra_output_kind;
717 public:
718 /* What units to use when outputting the column number. */
719 enum diagnostics_column_unit m_column_unit;
721 /* The origin for the column number (1-based or 0-based typically). */
722 int m_column_origin;
724 /* The size of the tabstop for tab expansion. */
725 int m_tabstop;
727 private:
728 /* How should non-ASCII/non-printable bytes be escaped when
729 a diagnostic suggests escaping the source code on output. */
730 enum diagnostics_escape_format m_escape_format;
732 /* If non-NULL, an edit_context to which fix-it hints should be
733 applied, for generating patches. */
734 edit_context *m_edit_context_ptr;
736 /* Fields relating to diagnostic groups. */
737 struct {
738 /* How many diagnostic_group instances are currently alive. */
739 int m_nesting_depth;
741 /* How many diagnostics have been emitted since the bottommost
742 diagnostic_group was pushed. */
743 int m_emission_count;
744 } m_diagnostic_groups;
746 /* How to output diagnostics (text vs a structured format such as JSON).
747 Must be non-NULL; owned by context. */
748 diagnostic_output_format *m_output_format;
750 /* Callback to set the locations of call sites along the inlining
751 stack corresponding to a diagnostic location. Needed to traverse
752 the BLOCK_SUPERCONTEXT() chain hanging off the LOCATION_BLOCK()
753 of a diagnostic's location. */
754 set_locations_callback_t m_set_locations_cb;
756 /* Optional callback for attempting to handle ICEs gracefully. */
757 ice_handler_callback_t m_ice_handler_cb;
759 /* Include files that diagnostic_report_current_module has already listed the
760 include path for. */
761 hash_set<location_t, false, location_hash> *m_includes_seen;
763 /* A bundle of hooks for providing data to the context about its client
764 e.g. version information, plugins, etc.
765 Used by SARIF output to give metadata about the client that's
766 producing diagnostics. */
767 diagnostic_client_data_hooks *m_client_data_hooks;
769 /* Support for diagrams. */
770 struct
772 /* Theme to use when generating diagrams.
773 Can be NULL (if text art is disabled). */
774 text_art::theme *m_theme;
776 } m_diagrams;
779 inline void
780 diagnostic_inhibit_notes (diagnostic_context * context)
782 context->m_inhibit_notes_p = true;
786 /* Client supplied function to announce a diagnostic
787 (for text-based diagnostic output). */
788 inline diagnostic_starter_fn &
789 diagnostic_starter (diagnostic_context *context)
791 return context->m_text_callbacks.m_begin_diagnostic;
794 /* Client supplied function called between disjoint spans of source code,
795 so that the context can print
796 something to indicate that a new span of source code has begun. */
797 inline diagnostic_start_span_fn &
798 diagnostic_start_span (diagnostic_context *context)
800 return context->m_text_callbacks.m_start_span;
803 /* Client supplied function called after a diagnostic message is
804 displayed (for text-based diagnostic output). */
805 inline diagnostic_finalizer_fn &
806 diagnostic_finalizer (diagnostic_context *context)
808 return context->m_text_callbacks.m_end_diagnostic;
811 /* Extension hooks for client. */
812 #define diagnostic_context_auxiliary_data(DC) (DC)->m_client_aux_data
813 #define diagnostic_info_auxiliary_data(DI) (DI)->x_data
815 /* Same as pp_format_decoder. Works on 'diagnostic_context *'. */
816 #define diagnostic_format_decoder(DC) ((DC)->printer->format_decoder)
818 /* Same as output_prefixing_rule. Works on 'diagnostic_context *'. */
819 #define diagnostic_prefixing_rule(DC) ((DC)->printer->wrapping.rule)
821 /* Raise SIGABRT on any diagnostic of severity DK_ERROR or higher. */
822 inline void
823 diagnostic_abort_on_error (diagnostic_context *context)
825 context->m_abort_on_error = true;
828 /* This diagnostic_context is used by front-ends that directly output
829 diagnostic messages without going through `error', `warning',
830 and similar functions. */
831 extern diagnostic_context *global_dc;
833 /* Returns whether the diagnostic framework has been intialized already and is
834 ready for use. */
835 inline bool
836 diagnostic_ready_p ()
838 return global_dc->printer != nullptr;
841 /* The number of errors that have been issued so far. Ideally, these
842 would take a diagnostic_context as an argument. */
843 #define errorcount global_dc->diagnostic_count (DK_ERROR)
844 /* Similarly, but for warnings. */
845 #define warningcount global_dc->diagnostic_count (DK_WARNING)
846 /* Similarly, but for warnings promoted to errors. */
847 #define werrorcount global_dc->diagnostic_count (DK_WERROR)
848 /* Similarly, but for sorrys. */
849 #define sorrycount global_dc->diagnostic_count (DK_SORRY)
851 /* Returns nonzero if warnings should be emitted. */
852 #define diagnostic_report_warnings_p(DC, LOC) \
853 (!(DC)->m_inhibit_warnings \
854 && !(in_system_header_at (LOC) && !(DC)->m_warn_system_headers))
856 /* Override the option index to be used for reporting a
857 diagnostic. */
859 inline void
860 diagnostic_override_option_index (diagnostic_info *info, int optidx)
862 info->option_index = optidx;
865 /* Diagnostic related functions. */
867 inline void
868 diagnostic_initialize (diagnostic_context *context, int n_opts)
870 context->initialize (n_opts);
873 inline void
874 diagnostic_color_init (diagnostic_context *context, int value = -1)
876 context->color_init (value);
879 inline void
880 diagnostic_urls_init (diagnostic_context *context, int value = -1)
882 context->urls_init (value);
885 inline void
886 diagnostic_finish (diagnostic_context *context)
888 context->finish ();
891 inline void
892 diagnostic_report_current_module (diagnostic_context *context,
893 location_t where)
895 context->report_current_module (where);
898 inline void
899 diagnostic_show_locus (diagnostic_context *context,
900 rich_location *richloc,
901 diagnostic_t diagnostic_kind,
902 pretty_printer *pp = nullptr)
904 gcc_assert (richloc);
905 context->maybe_show_locus (*richloc, diagnostic_kind, pp);
908 /* Because we read source files a second time after the frontend did it the
909 first time, we need to know how the frontend handled things like character
910 set conversion and UTF-8 BOM stripping, in order to make everything
911 consistent. This function needs to be called by each frontend that requires
912 non-default behavior, to inform the diagnostics infrastructure how input is
913 to be processed. The default behavior is to do no conversion and not to
914 strip a UTF-8 BOM.
916 The callback should return the input charset to be used to convert the given
917 file's contents to UTF-8, or it should return NULL if no conversion is needed
918 for this file. SHOULD_SKIP_BOM only applies in case no conversion was
919 performed, and if true, it will cause a UTF-8 BOM to be skipped at the
920 beginning of the file. (In case a conversion was performed, the BOM is
921 rather skipped as part of the conversion process.) */
923 inline void
924 diagnostic_initialize_input_context (diagnostic_context *context,
925 diagnostic_input_charset_callback ccb,
926 bool should_skip_bom)
928 context->initialize_input_context (ccb, should_skip_bom);
931 /* Force diagnostics controlled by OPTIDX to be kind KIND. */
932 inline diagnostic_t
933 diagnostic_classify_diagnostic (diagnostic_context *context,
934 int optidx,
935 diagnostic_t kind,
936 location_t where)
938 return context->classify_diagnostic (optidx, kind, where);
941 inline void
942 diagnostic_push_diagnostics (diagnostic_context *context,
943 location_t where)
945 context->push_diagnostics (where);
947 inline void
948 diagnostic_pop_diagnostics (diagnostic_context *context,
949 location_t where)
951 context->pop_diagnostics (where);
954 /* Report a diagnostic message (an error or a warning) as specified by
955 DC. This function is *the* subroutine in terms of which front-ends
956 should implement their specific diagnostic handling modules. The
957 front-end independent format specifiers are exactly those described
958 in the documentation of output_format.
959 Return true if a diagnostic was printed, false otherwise. */
961 inline bool
962 diagnostic_report_diagnostic (diagnostic_context *context,
963 diagnostic_info *diagnostic)
965 return context->report_diagnostic (diagnostic);
968 #ifdef ATTRIBUTE_GCC_DIAG
969 extern void diagnostic_set_info (diagnostic_info *, const char *, va_list *,
970 rich_location *, diagnostic_t) ATTRIBUTE_GCC_DIAG(2,0);
971 extern void diagnostic_set_info_translated (diagnostic_info *, const char *,
972 va_list *, rich_location *,
973 diagnostic_t)
974 ATTRIBUTE_GCC_DIAG(2,0);
975 extern void diagnostic_append_note (diagnostic_context *, location_t,
976 const char *, ...) ATTRIBUTE_GCC_DIAG(3,4);
977 #endif
978 extern char *diagnostic_build_prefix (diagnostic_context *, const diagnostic_info *);
979 void default_diagnostic_starter (diagnostic_context *, diagnostic_info *);
980 void default_diagnostic_start_span_fn (diagnostic_context *,
981 expanded_location);
982 void default_diagnostic_finalizer (diagnostic_context *, diagnostic_info *,
983 diagnostic_t);
984 void diagnostic_set_caret_max_width (diagnostic_context *context, int value);
986 inline void
987 diagnostic_action_after_output (diagnostic_context *context,
988 diagnostic_t diag_kind)
990 context->action_after_output (diag_kind);
993 inline void
994 diagnostic_check_max_errors (diagnostic_context *context, bool flush = false)
996 context->check_max_errors (flush);
999 int get_terminal_width (void);
1001 /* Return the location associated to this diagnostic. Parameter WHICH
1002 specifies which location. By default, expand the first one. */
1004 inline location_t
1005 diagnostic_location (const diagnostic_info * diagnostic, int which = 0)
1007 return diagnostic->message.get_location (which);
1010 /* Return the number of locations to be printed in DIAGNOSTIC. */
1012 inline unsigned int
1013 diagnostic_num_locations (const diagnostic_info * diagnostic)
1015 return diagnostic->message.m_richloc->get_num_locations ();
1018 /* Expand the location of this diagnostic. Use this function for
1019 consistency. Parameter WHICH specifies which location. By default,
1020 expand the first one. */
1022 inline expanded_location
1023 diagnostic_expand_location (const diagnostic_info * diagnostic, int which = 0)
1025 return diagnostic->richloc->get_expanded_location (which);
1028 /* This is somehow the right-side margin of a caret line, that is, we
1029 print at least these many characters after the position pointed at
1030 by the caret. */
1031 const int CARET_LINE_MARGIN = 10;
1033 /* Return true if the two locations can be represented within the same
1034 caret line. This is used to build a prefix and also to determine
1035 whether to print one or two caret lines. */
1037 inline bool
1038 diagnostic_same_line (const diagnostic_context *context,
1039 expanded_location s1, expanded_location s2)
1041 return (s2.column && s1.line == s2.line
1042 && (context->m_source_printing.max_width - CARET_LINE_MARGIN
1043 > abs (s1.column - s2.column)));
1046 extern const char *diagnostic_get_color_for_kind (diagnostic_t kind);
1048 /* Pure text formatting support functions. */
1049 extern char *file_name_as_prefix (diagnostic_context *, const char *);
1051 extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
1053 extern void diagnostic_output_format_init (diagnostic_context *,
1054 const char *base_file_name,
1055 enum diagnostics_output_format);
1056 extern void diagnostic_output_format_init_json_stderr (diagnostic_context *context);
1057 extern void diagnostic_output_format_init_json_file (diagnostic_context *context,
1058 const char *base_file_name);
1059 extern void diagnostic_output_format_init_sarif_stderr (diagnostic_context *context);
1060 extern void diagnostic_output_format_init_sarif_file (diagnostic_context *context,
1061 const char *base_file_name);
1062 extern void diagnostic_output_format_init_sarif_stream (diagnostic_context *context,
1063 FILE *stream);
1065 /* Compute the number of digits in the decimal representation of an integer. */
1066 extern int num_digits (int);
1068 extern json::value *json_from_expanded_location (diagnostic_context *context,
1069 location_t loc);
1071 inline bool
1072 warning_enabled_at (location_t loc, int opt)
1074 return global_dc->warning_enabled_at (loc, opt);
1077 inline bool
1078 option_unspecified_p (int opt)
1080 return global_dc->option_unspecified_p (opt);
1083 extern char *get_cwe_url (int cwe);
1085 #endif /* ! GCC_DIAGNOSTIC_H */