d: Merge upstream dmd, druntime 2bbf64907c, phobos b64bfbf91
[official-gcc.git] / gcc / diagnostic.h
blob80e53ec92b06a8caeb250e0d6b0298b97c2d2e4e
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 "rich-location.h"
25 #include "pretty-print.h"
26 #include "diagnostic-core.h"
28 namespace text_art
30 class theme;
31 } // namespace text_art
33 /* An enum for controlling what units to use for the column number
34 when diagnostics are output, used by the -fdiagnostics-column-unit option.
35 Tabs will be expanded or not according to the value of -ftabstop. The origin
36 (default 1) is controlled by -fdiagnostics-column-origin. */
38 enum diagnostics_column_unit
40 /* The default from GCC 11 onwards: display columns. */
41 DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
43 /* The behavior in GCC 10 and earlier: simple bytes. */
44 DIAGNOSTICS_COLUMN_UNIT_BYTE
47 /* An enum for controlling how to print non-ASCII characters/bytes when
48 a diagnostic suggests escaping the source code on output. */
50 enum diagnostics_escape_format
52 /* Escape non-ASCII Unicode characters in the form <U+XXXX> and
53 non-UTF-8 bytes in the form <XX>. */
54 DIAGNOSTICS_ESCAPE_FORMAT_UNICODE,
56 /* Escape non-ASCII bytes in the form <XX> (thus showing the underlying
57 encoding of non-ASCII Unicode characters). */
58 DIAGNOSTICS_ESCAPE_FORMAT_BYTES
61 /* Enum for overriding the standard output format. */
63 enum diagnostics_output_format
65 /* The default: textual output. */
66 DIAGNOSTICS_OUTPUT_FORMAT_TEXT,
68 /* JSON-based output, to stderr. */
69 DIAGNOSTICS_OUTPUT_FORMAT_JSON_STDERR,
71 /* JSON-based output, to a file. */
72 DIAGNOSTICS_OUTPUT_FORMAT_JSON_FILE,
74 /* SARIF-based output, to stderr. */
75 DIAGNOSTICS_OUTPUT_FORMAT_SARIF_STDERR,
77 /* SARIF-based output, to a file. */
78 DIAGNOSTICS_OUTPUT_FORMAT_SARIF_FILE
81 /* An enum for controlling how diagnostic_paths should be printed. */
82 enum diagnostic_path_format
84 /* Don't print diagnostic_paths. */
85 DPF_NONE,
87 /* Print diagnostic_paths by emitting a separate "note" for every event
88 in the path. */
89 DPF_SEPARATE_EVENTS,
91 /* Print diagnostic_paths by consolidating events together where they
92 are close enough, and printing such runs of events with multiple
93 calls to diagnostic_show_locus, showing the individual events in
94 each run via labels in the source. */
95 DPF_INLINE_EVENTS
98 /* An enum for capturing values of GCC_EXTRA_DIAGNOSTIC_OUTPUT,
99 and for -fdiagnostics-parseable-fixits. */
101 enum diagnostics_extra_output_kind
103 /* No extra output, or an unrecognized value. */
104 EXTRA_DIAGNOSTIC_OUTPUT_none,
106 /* Emit fix-it hints using the "fixits-v1" format, equivalent to
107 -fdiagnostics-parseable-fixits. */
108 EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1,
110 /* Emit fix-it hints using the "fixits-v2" format. */
111 EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2
114 /* Values for -fdiagnostics-text-art-charset=. */
116 enum diagnostic_text_art_charset
118 /* No text art diagrams shall be emitted. */
119 DIAGNOSTICS_TEXT_ART_CHARSET_NONE,
121 /* Use pure ASCII for text art diagrams. */
122 DIAGNOSTICS_TEXT_ART_CHARSET_ASCII,
124 /* Use ASCII + conservative use of other unicode characters
125 in text art diagrams. */
126 DIAGNOSTICS_TEXT_ART_CHARSET_UNICODE,
128 /* Use Emoji. */
129 DIAGNOSTICS_TEXT_ART_CHARSET_EMOJI
132 /* A diagnostic is described by the MESSAGE to send, the FILE and LINE of
133 its context and its KIND (ice, error, warning, note, ...) See complete
134 list in diagnostic.def. */
135 struct diagnostic_info
137 diagnostic_info ()
138 : message (), richloc (), metadata (), x_data (), kind (), option_index (),
139 m_iinfo ()
142 /* Text to be formatted. */
143 text_info message;
145 /* The location at which the diagnostic is to be reported. */
146 rich_location *richloc;
148 /* An optional bundle of metadata associated with the diagnostic
149 (or NULL). */
150 const diagnostic_metadata *metadata;
152 /* Auxiliary data for client. */
153 void *x_data;
154 /* The kind of diagnostic it is about. */
155 diagnostic_t kind;
156 /* Which OPT_* directly controls this diagnostic. */
157 int option_index;
159 /* Inlining context containing locations for each call site along
160 the inlining stack. */
161 struct inlining_info
163 /* Locations along the inlining stack. */
164 auto_vec<location_t, 8> m_ilocs;
165 /* The abstract origin of the location. */
166 void *m_ao;
167 /* Set if every M_ILOCS element is in a system header. */
168 bool m_allsyslocs;
169 } m_iinfo;
172 /* Forward declarations. */
173 typedef void (*diagnostic_starter_fn) (diagnostic_context *,
174 const diagnostic_info *);
176 typedef void (*diagnostic_start_span_fn) (diagnostic_context *,
177 expanded_location);
179 typedef void (*diagnostic_finalizer_fn) (diagnostic_context *,
180 const diagnostic_info *,
181 diagnostic_t);
183 typedef int (*diagnostic_option_enabled_cb) (int, unsigned, void *);
184 typedef char *(*diagnostic_make_option_name_cb) (const diagnostic_context *,
185 int,
186 diagnostic_t,
187 diagnostic_t);
188 typedef char *(*diagnostic_make_option_url_cb) (const diagnostic_context *,
189 int);
191 class edit_context;
192 namespace json { class value; }
193 class diagnostic_client_data_hooks;
194 class logical_location;
195 class diagnostic_diagram;
197 /* Abstract base class for a particular output format for diagnostics;
198 each value of -fdiagnostics-output-format= will have its own
199 implementation. */
201 class diagnostic_output_format
203 public:
204 virtual ~diagnostic_output_format () {}
206 virtual void on_begin_group () = 0;
207 virtual void on_end_group () = 0;
208 virtual void on_begin_diagnostic (const diagnostic_info &) = 0;
209 virtual void on_end_diagnostic (const diagnostic_info &,
210 diagnostic_t orig_diag_kind) = 0;
211 virtual void on_diagram (const diagnostic_diagram &diagram) = 0;
213 protected:
214 diagnostic_output_format (diagnostic_context &context)
215 : m_context (context)
218 diagnostic_context &m_context;
221 /* Subclass of diagnostic_output_format for classic text-based output
222 to stderr.
224 Uses diagnostic_context.m_text_callbacks to provide client-specific
225 textual output (e.g. include paths, macro expansions, etc). */
227 class diagnostic_text_output_format : public diagnostic_output_format
229 public:
230 diagnostic_text_output_format (diagnostic_context &context)
231 : diagnostic_output_format (context)
233 ~diagnostic_text_output_format ();
234 void on_begin_group () override {}
235 void on_end_group () override {}
236 void on_begin_diagnostic (const diagnostic_info &) override;
237 void on_end_diagnostic (const diagnostic_info &,
238 diagnostic_t orig_diag_kind) override;
239 void on_diagram (const diagnostic_diagram &diagram) override;
242 /* A stack of sets of classifications: each entry in the stack is
243 a mapping from option index to diagnostic severity that can be changed
244 via pragmas. The stack can be pushed and popped. */
246 class diagnostic_option_classifier
248 public:
249 void init (int n_opts);
250 void fini ();
252 /* Save all diagnostic classifications in a stack. */
253 void push ();
255 /* Restore the topmost classification set off the stack. If the stack
256 is empty, revert to the state based on command line parameters. */
257 void pop (location_t where);
259 bool option_unspecified_p (int opt) const
261 return get_current_override (opt) == DK_UNSPECIFIED;
264 diagnostic_t get_current_override (int opt) const
266 gcc_assert (opt < m_n_opts);
267 return m_classify_diagnostic[opt];
270 diagnostic_t
271 classify_diagnostic (const diagnostic_context *context,
272 int option_index,
273 diagnostic_t new_kind,
274 location_t where);
276 diagnostic_t
277 update_effective_level_from_pragmas (diagnostic_info *diagnostic) const;
279 private:
280 /* Each time a diagnostic's classification is changed with a pragma,
281 we record the change and the location of the change in an array of
282 these structs. */
283 struct diagnostic_classification_change_t
285 location_t location;
286 int option;
287 diagnostic_t kind;
290 int m_n_opts;
292 /* For each option index that can be passed to warning() et al
293 (OPT_* from options.h when using this code with the core GCC
294 options), this array may contain a new kind that the diagnostic
295 should be changed to before reporting, or DK_UNSPECIFIED to leave
296 it as the reported kind, or DK_IGNORED to not report it at
297 all. */
298 diagnostic_t *m_classify_diagnostic;
300 /* History of all changes to the classifications above. This list
301 is stored in location-order, so we can search it, either
302 binary-wise or end-to-front, to find the most recent
303 classification for a given diagnostic, given the location of the
304 diagnostic. */
305 diagnostic_classification_change_t *m_classification_history;
307 /* The size of the above array. */
308 int m_n_classification_history;
310 /* For pragma push/pop. */
311 int *m_push_list;
312 int m_n_push;
315 /* A bundle of options relating to printing the user's source code
316 (potentially with a margin, underlining, labels, etc). */
318 struct diagnostic_source_printing_options
320 /* True if we should print the source line with a caret indicating
321 the location.
322 Corresponds to -fdiagnostics-show-caret. */
323 bool enabled;
325 /* Maximum width of the source line printed. */
326 int max_width;
328 /* Character used at the caret when printing source locations. */
329 char caret_chars[rich_location::STATICALLY_ALLOCATED_RANGES];
331 /* When printing source code, should the characters at carets and ranges
332 be colorized? (assuming colorization is on at all).
333 This should be true for frontends that generate range information
334 (so that the ranges of code are colorized),
335 and false for frontends that merely specify points within the
336 source code (to avoid e.g. colorizing just the first character in
337 a token, which would look strange). */
338 bool colorize_source_p;
340 /* When printing source code, should labelled ranges be printed?
341 Corresponds to -fdiagnostics-show-labels. */
342 bool show_labels_p;
344 /* When printing source code, should there be a left-hand margin
345 showing line numbers?
346 Corresponds to -fdiagnostics-show-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 Corresponds to -fdiagnostics-minimum-margin-width=VALUE. */
352 int min_margin_width;
354 /* Usable by plugins; if true, print a debugging ruler above the
355 source output. */
356 bool show_ruler_p;
359 /* This data structure bundles altogether any information relevant to
360 the context of a diagnostic message. */
361 class diagnostic_context
363 public:
364 /* Give access to m_text_callbacks. */
365 friend diagnostic_starter_fn &
366 diagnostic_starter (diagnostic_context *context);
367 friend diagnostic_start_span_fn &
368 diagnostic_start_span (diagnostic_context *context);
369 friend diagnostic_finalizer_fn &
370 diagnostic_finalizer (diagnostic_context *context);
372 typedef void (*ice_handler_callback_t) (diagnostic_context *);
373 typedef void (*set_locations_callback_t) (diagnostic_context *,
374 diagnostic_info *);
376 void initialize (int n_opts);
377 void color_init (int value);
378 void urls_init (int value);
380 void finish ();
382 void set_set_locations_callback (set_locations_callback_t cb)
384 m_set_locations_cb = cb;
387 void
388 initialize_input_context (diagnostic_input_charset_callback ccb,
389 bool should_skip_bom);
391 void begin_group ();
392 void end_group ();
394 bool warning_enabled_at (location_t loc, int opt);
396 bool option_unspecified_p (int opt) const
398 return m_option_classifier.option_unspecified_p (opt);
401 bool report_diagnostic (diagnostic_info *);
403 void report_current_module (location_t where);
405 void check_max_errors (bool flush);
406 void action_after_output (diagnostic_t diag_kind);
408 diagnostic_t
409 classify_diagnostic (int option_index,
410 diagnostic_t new_kind,
411 location_t where)
413 return m_option_classifier.classify_diagnostic (this,
414 option_index,
415 new_kind,
416 where);
419 void push_diagnostics (location_t where ATTRIBUTE_UNUSED)
421 m_option_classifier.push ();
423 void pop_diagnostics (location_t where)
425 m_option_classifier.pop (where);
428 void maybe_show_locus (const rich_location &richloc,
429 diagnostic_t diagnostic_kind,
430 pretty_printer *pp);
432 void emit_diagram (const diagnostic_diagram &diagram);
434 /* Various setters for use by option-handling logic. */
435 void set_output_format (diagnostic_output_format *output_format);
436 void set_text_art_charset (enum diagnostic_text_art_charset charset);
437 void set_client_data_hooks (diagnostic_client_data_hooks *hooks);
438 void set_urlifier (urlifier *);
439 void create_edit_context ();
440 void set_warning_as_error_requested (bool val)
442 m_warning_as_error_requested = val;
444 void set_report_bug (bool val) { m_report_bug = val; }
445 void set_extra_output_kind (enum diagnostics_extra_output_kind kind)
447 m_extra_output_kind = kind;
449 void set_show_cwe (bool val) { m_show_cwe = val; }
450 void set_show_rules (bool val) { m_show_rules = val; }
451 void set_path_format (enum diagnostic_path_format val)
453 m_path_format = val;
455 void set_show_path_depths (bool val) { m_show_path_depths = val; }
456 void set_show_option_requested (bool val) { m_show_option_requested = val; }
457 void set_max_errors (int val) { m_max_errors = val; }
458 void set_escape_format (enum diagnostics_escape_format val)
460 m_escape_format = val;
462 void set_ice_handler_callback (ice_handler_callback_t cb)
464 m_ice_handler_cb = cb;
467 /* Various accessors. */
468 bool warning_as_error_requested_p () const
470 return m_warning_as_error_requested;
472 bool show_path_depths_p () const { return m_show_path_depths; }
473 enum diagnostic_path_format get_path_format () const { return m_path_format; }
474 enum diagnostics_escape_format get_escape_format () const
476 return m_escape_format;
479 file_cache &
480 get_file_cache () const
482 gcc_assert (m_file_cache);
483 return *m_file_cache;
486 edit_context *get_edit_context () const
488 return m_edit_context_ptr;
490 const diagnostic_client_data_hooks *get_client_data_hooks ()
492 return m_client_data_hooks;
494 text_art::theme *get_diagram_theme () const { return m_diagrams.m_theme; }
496 int converted_column (expanded_location s) const;
498 int &diagnostic_count (diagnostic_t kind)
500 return m_diagnostic_count[kind];
503 /* Option-related member functions. */
504 inline bool option_enabled_p (int option_index) const
506 if (!m_option_callbacks.m_option_enabled_cb)
507 return true;
508 return m_option_callbacks.m_option_enabled_cb
509 (option_index,
510 m_option_callbacks.m_lang_mask,
511 m_option_callbacks.m_option_state);
514 inline char *make_option_name (int option_index,
515 diagnostic_t orig_diag_kind,
516 diagnostic_t diag_kind) const
518 if (!m_option_callbacks.m_make_option_name_cb)
519 return nullptr;
520 return m_option_callbacks.m_make_option_name_cb (this, option_index,
521 orig_diag_kind,
522 diag_kind);
525 inline char *make_option_url (int option_index) const
527 if (!m_option_callbacks.m_make_option_url_cb)
528 return nullptr;
529 return m_option_callbacks.m_make_option_url_cb (this, option_index);
532 void
533 set_option_hooks (diagnostic_option_enabled_cb option_enabled_cb,
534 void *option_state,
535 diagnostic_make_option_name_cb make_option_name_cb,
536 diagnostic_make_option_url_cb make_option_url_cb,
537 unsigned lang_mask);
539 unsigned get_lang_mask () const
541 return m_option_callbacks.m_lang_mask;
544 label_text get_location_text (const expanded_location &s) const;
546 private:
547 bool includes_seen_p (const line_map_ordinary *map);
549 void print_any_cwe (const diagnostic_info &diagnostic);
550 void print_any_rules (const diagnostic_info &diagnostic);
551 void print_option_information (const diagnostic_info &diagnostic,
552 diagnostic_t orig_diag_kind);
554 void show_any_path (const diagnostic_info &diagnostic);
556 void error_recursion () ATTRIBUTE_NORETURN;
558 bool diagnostic_enabled (diagnostic_info *diagnostic);
560 void get_any_inlining_info (diagnostic_info *diagnostic);
562 void show_locus (const rich_location &richloc,
563 diagnostic_t diagnostic_kind,
564 pretty_printer *pp);
566 /* Data members.
567 Ideally, all of these would be private and have "m_" prefixes. */
569 public:
570 /* Where most of the diagnostic formatting work is done. */
571 pretty_printer *printer;
573 private:
574 /* Cache of source code. */
575 file_cache *m_file_cache;
577 /* The number of times we have issued diagnostics. */
578 int m_diagnostic_count[DK_LAST_DIAGNOSTIC_KIND];
580 /* True if it has been requested that warnings be treated as errors. */
581 bool m_warning_as_error_requested;
583 /* The number of option indexes that can be passed to warning() et
584 al. */
585 int m_n_opts;
587 /* The stack of sets of overridden diagnostic option severities. */
588 diagnostic_option_classifier m_option_classifier;
590 /* True if we should print any CWE identifiers associated with
591 diagnostics. */
592 bool m_show_cwe;
594 /* True if we should print any rules associated with diagnostics. */
595 bool m_show_rules;
597 /* How should diagnostic_path objects be printed. */
598 enum diagnostic_path_format m_path_format;
600 /* True if we should print stack depths when printing diagnostic paths. */
601 bool m_show_path_depths;
603 /* True if we should print the command line option which controls
604 each diagnostic, if known. */
605 bool m_show_option_requested;
607 public:
608 /* True if we should raise a SIGABRT on errors. */
609 bool m_abort_on_error;
611 /* True if we should show the column number on diagnostics. */
612 bool m_show_column;
614 /* True if pedwarns are errors. */
615 bool m_pedantic_errors;
617 /* True if permerrors are warnings. */
618 bool m_permissive;
620 /* The index of the option to associate with turning permerrors into
621 warnings. */
622 int m_opt_permissive;
624 /* True if errors are fatal. */
625 bool m_fatal_errors;
627 /* True if all warnings should be disabled. */
628 bool m_inhibit_warnings;
630 /* True if warnings should be given in system headers. */
631 bool m_warn_system_headers;
633 private:
634 /* Maximum number of errors to report. */
635 int m_max_errors;
637 /* Client-supplied callbacks for use in text output. */
638 struct {
639 /* This function is called before any message is printed out. It is
640 responsible for preparing message prefix and such. For example, it
641 might say:
642 In file included from "/usr/local/include/curses.h:5:
643 from "/home/gdr/src/nifty_printer.h:56:
646 diagnostic_starter_fn m_begin_diagnostic;
648 /* This function is called by diagnostic_show_locus in between
649 disjoint spans of source code, so that the context can print
650 something to indicate that a new span of source code has begun. */
651 diagnostic_start_span_fn m_start_span;
653 /* This function is called after the diagnostic message is printed. */
654 diagnostic_finalizer_fn m_end_diagnostic;
655 } m_text_callbacks;
657 public:
658 /* Client hook to report an internal error. */
659 void (*m_internal_error) (diagnostic_context *, const char *, va_list *);
661 private:
662 /* Client-supplied callbacks for working with options. */
663 struct {
664 /* Client hook to say whether the option controlling a diagnostic is
665 enabled. Returns nonzero if enabled, zero if disabled. */
666 diagnostic_option_enabled_cb m_option_enabled_cb;
668 /* Client information to pass as second argument to
669 m_option_enabled_cb. */
670 void *m_option_state;
672 /* Client hook to return the name of an option that controls a
673 diagnostic. Returns malloced memory. The first diagnostic_t
674 argument is the kind of diagnostic before any reclassification
675 (of warnings as errors, etc.); the second is the kind after any
676 reclassification. May return NULL if no name is to be printed.
677 May be passed 0 as well as the index of a particular option. */
678 diagnostic_make_option_name_cb m_make_option_name_cb;
680 /* Client hook to return a URL describing the option that controls
681 a diagnostic. Returns malloced memory. May return NULL if no URL
682 is available. May be passed 0 as well as the index of a
683 particular option. */
684 diagnostic_make_option_url_cb m_make_option_url_cb;
686 /* A copy of lang_hooks.option_lang_mask (). */
687 unsigned m_lang_mask;
688 } m_option_callbacks;
690 /* An optional hook for adding URLs to quoted text strings in
691 diagnostics. Only used for the main diagnostic message. */
692 urlifier *m_urlifier;
694 public:
695 void (*m_print_path) (diagnostic_context *, const diagnostic_path *);
696 json::value *(*m_make_json_for_path) (diagnostic_context *,
697 const diagnostic_path *);
699 /* Auxiliary data for client. */
700 void *m_client_aux_data;
702 /* Used to detect that the last caret was printed at the same location. */
703 location_t m_last_location;
705 private:
706 /* Used to detect when the input file stack has changed since last
707 described. */
708 const line_map_ordinary *m_last_module;
710 int m_lock;
712 public:
713 bool m_inhibit_notes_p;
715 diagnostic_source_printing_options m_source_printing;
717 private:
718 /* True if -freport-bug option is used. */
719 bool m_report_bug;
721 /* Used to specify additional diagnostic output to be emitted after the
722 rest of the diagnostic. This is for implementing
723 -fdiagnostics-parseable-fixits and GCC_EXTRA_DIAGNOSTIC_OUTPUT. */
724 enum diagnostics_extra_output_kind m_extra_output_kind;
726 public:
727 /* What units to use when outputting the column number. */
728 enum diagnostics_column_unit m_column_unit;
730 /* The origin for the column number (1-based or 0-based typically). */
731 int m_column_origin;
733 /* The size of the tabstop for tab expansion. */
734 int m_tabstop;
736 private:
737 /* How should non-ASCII/non-printable bytes be escaped when
738 a diagnostic suggests escaping the source code on output. */
739 enum diagnostics_escape_format m_escape_format;
741 /* If non-NULL, an edit_context to which fix-it hints should be
742 applied, for generating patches. */
743 edit_context *m_edit_context_ptr;
745 /* Fields relating to diagnostic groups. */
746 struct {
747 /* How many diagnostic_group instances are currently alive. */
748 int m_nesting_depth;
750 /* How many diagnostics have been emitted since the bottommost
751 diagnostic_group was pushed. */
752 int m_emission_count;
753 } m_diagnostic_groups;
755 /* How to output diagnostics (text vs a structured format such as JSON).
756 Must be non-NULL; owned by context. */
757 diagnostic_output_format *m_output_format;
759 /* Callback to set the locations of call sites along the inlining
760 stack corresponding to a diagnostic location. Needed to traverse
761 the BLOCK_SUPERCONTEXT() chain hanging off the LOCATION_BLOCK()
762 of a diagnostic's location. */
763 set_locations_callback_t m_set_locations_cb;
765 /* Optional callback for attempting to handle ICEs gracefully. */
766 ice_handler_callback_t m_ice_handler_cb;
768 /* Include files that diagnostic_report_current_module has already listed the
769 include path for. */
770 hash_set<location_t, false, location_hash> *m_includes_seen;
772 /* A bundle of hooks for providing data to the context about its client
773 e.g. version information, plugins, etc.
774 Used by SARIF output to give metadata about the client that's
775 producing diagnostics. */
776 diagnostic_client_data_hooks *m_client_data_hooks;
778 /* Support for diagrams. */
779 struct
781 /* Theme to use when generating diagrams.
782 Can be NULL (if text art is disabled). */
783 text_art::theme *m_theme;
785 } m_diagrams;
788 inline void
789 diagnostic_inhibit_notes (diagnostic_context * context)
791 context->m_inhibit_notes_p = true;
795 /* Client supplied function to announce a diagnostic
796 (for text-based diagnostic output). */
797 inline diagnostic_starter_fn &
798 diagnostic_starter (diagnostic_context *context)
800 return context->m_text_callbacks.m_begin_diagnostic;
803 /* Client supplied function called between disjoint spans of source code,
804 so that the context can print
805 something to indicate that a new span of source code has begun. */
806 inline diagnostic_start_span_fn &
807 diagnostic_start_span (diagnostic_context *context)
809 return context->m_text_callbacks.m_start_span;
812 /* Client supplied function called after a diagnostic message is
813 displayed (for text-based diagnostic output). */
814 inline diagnostic_finalizer_fn &
815 diagnostic_finalizer (diagnostic_context *context)
817 return context->m_text_callbacks.m_end_diagnostic;
820 /* Extension hooks for client. */
821 #define diagnostic_context_auxiliary_data(DC) (DC)->m_client_aux_data
822 #define diagnostic_info_auxiliary_data(DI) (DI)->x_data
824 /* Same as pp_format_decoder. Works on 'diagnostic_context *'. */
825 #define diagnostic_format_decoder(DC) ((DC)->printer->format_decoder)
827 /* Same as output_prefixing_rule. Works on 'diagnostic_context *'. */
828 #define diagnostic_prefixing_rule(DC) ((DC)->printer->wrapping.rule)
830 /* Raise SIGABRT on any diagnostic of severity DK_ERROR or higher. */
831 inline void
832 diagnostic_abort_on_error (diagnostic_context *context)
834 context->m_abort_on_error = true;
837 /* This diagnostic_context is used by front-ends that directly output
838 diagnostic messages without going through `error', `warning',
839 and similar functions. */
840 extern diagnostic_context *global_dc;
842 /* Returns whether the diagnostic framework has been intialized already and is
843 ready for use. */
844 inline bool
845 diagnostic_ready_p ()
847 return global_dc->printer != nullptr;
850 /* The number of errors that have been issued so far. Ideally, these
851 would take a diagnostic_context as an argument. */
852 #define errorcount global_dc->diagnostic_count (DK_ERROR)
853 /* Similarly, but for warnings. */
854 #define warningcount global_dc->diagnostic_count (DK_WARNING)
855 /* Similarly, but for warnings promoted to errors. */
856 #define werrorcount global_dc->diagnostic_count (DK_WERROR)
857 /* Similarly, but for sorrys. */
858 #define sorrycount global_dc->diagnostic_count (DK_SORRY)
860 /* Returns nonzero if warnings should be emitted. */
861 #define diagnostic_report_warnings_p(DC, LOC) \
862 (!(DC)->m_inhibit_warnings \
863 && !(in_system_header_at (LOC) && !(DC)->m_warn_system_headers))
865 /* Override the option index to be used for reporting a
866 diagnostic. */
868 inline void
869 diagnostic_override_option_index (diagnostic_info *info, int optidx)
871 info->option_index = optidx;
874 /* Diagnostic related functions. */
876 inline void
877 diagnostic_initialize (diagnostic_context *context, int n_opts)
879 context->initialize (n_opts);
882 inline void
883 diagnostic_color_init (diagnostic_context *context, int value = -1)
885 context->color_init (value);
888 inline void
889 diagnostic_urls_init (diagnostic_context *context, int value = -1)
891 context->urls_init (value);
894 inline void
895 diagnostic_finish (diagnostic_context *context)
897 context->finish ();
900 inline void
901 diagnostic_report_current_module (diagnostic_context *context,
902 location_t where)
904 context->report_current_module (where);
907 inline void
908 diagnostic_show_locus (diagnostic_context *context,
909 rich_location *richloc,
910 diagnostic_t diagnostic_kind,
911 pretty_printer *pp = nullptr)
913 gcc_assert (richloc);
914 context->maybe_show_locus (*richloc, diagnostic_kind, pp);
917 /* Because we read source files a second time after the frontend did it the
918 first time, we need to know how the frontend handled things like character
919 set conversion and UTF-8 BOM stripping, in order to make everything
920 consistent. This function needs to be called by each frontend that requires
921 non-default behavior, to inform the diagnostics infrastructure how input is
922 to be processed. The default behavior is to do no conversion and not to
923 strip a UTF-8 BOM.
925 The callback should return the input charset to be used to convert the given
926 file's contents to UTF-8, or it should return NULL if no conversion is needed
927 for this file. SHOULD_SKIP_BOM only applies in case no conversion was
928 performed, and if true, it will cause a UTF-8 BOM to be skipped at the
929 beginning of the file. (In case a conversion was performed, the BOM is
930 rather skipped as part of the conversion process.) */
932 inline void
933 diagnostic_initialize_input_context (diagnostic_context *context,
934 diagnostic_input_charset_callback ccb,
935 bool should_skip_bom)
937 context->initialize_input_context (ccb, should_skip_bom);
940 /* Force diagnostics controlled by OPTIDX to be kind KIND. */
941 inline diagnostic_t
942 diagnostic_classify_diagnostic (diagnostic_context *context,
943 int optidx,
944 diagnostic_t kind,
945 location_t where)
947 return context->classify_diagnostic (optidx, kind, where);
950 inline void
951 diagnostic_push_diagnostics (diagnostic_context *context,
952 location_t where)
954 context->push_diagnostics (where);
956 inline void
957 diagnostic_pop_diagnostics (diagnostic_context *context,
958 location_t where)
960 context->pop_diagnostics (where);
963 /* Report a diagnostic message (an error or a warning) as specified by
964 DC. This function is *the* subroutine in terms of which front-ends
965 should implement their specific diagnostic handling modules. The
966 front-end independent format specifiers are exactly those described
967 in the documentation of output_format.
968 Return true if a diagnostic was printed, false otherwise. */
970 inline bool
971 diagnostic_report_diagnostic (diagnostic_context *context,
972 diagnostic_info *diagnostic)
974 return context->report_diagnostic (diagnostic);
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 extern void diagnostic_output_format_init (diagnostic_context *,
1064 const char *base_file_name,
1065 enum diagnostics_output_format,
1066 bool json_formatting);
1067 extern void diagnostic_output_format_init_json_stderr (diagnostic_context *context,
1068 bool formatted);
1069 extern void diagnostic_output_format_init_json_file (diagnostic_context *context,
1070 bool formatted,
1071 const char *base_file_name);
1072 extern void diagnostic_output_format_init_sarif_stderr (diagnostic_context *context,
1073 bool formatted);
1074 extern void diagnostic_output_format_init_sarif_file (diagnostic_context *context,
1075 bool formatted,
1076 const char *base_file_name);
1077 extern void diagnostic_output_format_init_sarif_stream (diagnostic_context *context,
1078 bool formatted,
1079 FILE *stream);
1081 /* Compute the number of digits in the decimal representation of an integer. */
1082 extern int num_digits (int);
1084 extern json::value *json_from_expanded_location (diagnostic_context *context,
1085 location_t loc);
1087 inline bool
1088 warning_enabled_at (location_t loc, int opt)
1090 return global_dc->warning_enabled_at (loc, opt);
1093 inline bool
1094 option_unspecified_p (int opt)
1096 return global_dc->option_unspecified_p (opt);
1099 extern char *get_cwe_url (int cwe);
1101 #endif /* ! GCC_DIAGNOSTIC_H */