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
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
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"
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. */
86 /* Print diagnostic_paths by emitting a separate "note" for every event
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. */
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
,
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
137 : message (), richloc (), metadata (), x_data (), kind (), option_index (),
141 /* Text to be formatted. */
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
149 const diagnostic_metadata
*metadata
;
151 /* Auxiliary data for client. */
153 /* The kind of diagnostic it is about. */
155 /* Which OPT_* directly controls this diagnostic. */
158 /* Inlining context containing locations for each call site along
159 the inlining stack. */
162 /* Locations along the inlining stack. */
163 auto_vec
<location_t
, 8> m_ilocs
;
164 /* The abstract origin of the location. */
166 /* Set if every M_ILOCS element is in a system header. */
171 /* Forward declarations. */
172 typedef void (*diagnostic_starter_fn
) (diagnostic_context
*,
175 typedef void (*diagnostic_start_span_fn
) (diagnostic_context
*,
178 typedef void (*diagnostic_finalizer_fn
) (diagnostic_context
*,
183 namespace json
{ class value
; }
184 class diagnostic_client_data_hooks
;
185 class logical_location
;
186 class diagnostic_diagram
;
188 /* Abstract base class for a particular output format for diagnostics;
189 each value of -fdiagnostics-output-format= will have its own
192 class diagnostic_output_format
195 virtual ~diagnostic_output_format () {}
197 virtual void on_begin_group () = 0;
198 virtual void on_end_group () = 0;
199 virtual void on_begin_diagnostic (diagnostic_info
*) = 0;
200 virtual void on_end_diagnostic (diagnostic_info
*,
201 diagnostic_t orig_diag_kind
) = 0;
202 virtual void on_diagram (const diagnostic_diagram
&diagram
) = 0;
205 diagnostic_output_format (diagnostic_context
&context
)
206 : m_context (context
)
209 diagnostic_context
&m_context
;
212 /* Subclass of diagnostic_output_format for classic text-based output
215 Uses diagnostic_context.m_text_callbacks to provide client-specific
216 textual output (e.g. include paths, macro expansions, etc). */
218 class diagnostic_text_output_format
: public diagnostic_output_format
221 diagnostic_text_output_format (diagnostic_context
&context
)
222 : diagnostic_output_format (context
)
224 ~diagnostic_text_output_format ();
225 void on_begin_group () override
{}
226 void on_end_group () override
{}
227 void on_begin_diagnostic (diagnostic_info
*) override
;
228 void on_end_diagnostic (diagnostic_info
*,
229 diagnostic_t orig_diag_kind
) override
;
230 void on_diagram (const diagnostic_diagram
&diagram
) override
;
233 /* A stack of sets of classifications: each entry in the stack is
234 a mapping from option index to diagnostic severity that can be changed
235 via pragmas. The stack can be pushed and popped. */
237 class diagnostic_option_classifier
240 void init (int n_opts
);
243 /* Save all diagnostic classifications in a stack. */
246 /* Restore the topmost classification set off the stack. If the stack
247 is empty, revert to the state based on command line parameters. */
248 void pop (location_t where
);
250 bool option_unspecified_p (int opt
) const
252 return get_current_override (opt
) == DK_UNSPECIFIED
;
255 diagnostic_t
get_current_override (int opt
) const
257 gcc_assert (opt
< m_n_opts
);
258 return m_classify_diagnostic
[opt
];
262 classify_diagnostic (const diagnostic_context
*context
,
264 diagnostic_t new_kind
,
268 update_effective_level_from_pragmas (diagnostic_info
*diagnostic
) const;
271 /* Each time a diagnostic's classification is changed with a pragma,
272 we record the change and the location of the change in an array of
274 struct diagnostic_classification_change_t
283 /* For each option index that can be passed to warning() et al
284 (OPT_* from options.h when using this code with the core GCC
285 options), this array may contain a new kind that the diagnostic
286 should be changed to before reporting, or DK_UNSPECIFIED to leave
287 it as the reported kind, or DK_IGNORED to not report it at
289 diagnostic_t
*m_classify_diagnostic
;
291 /* History of all changes to the classifications above. This list
292 is stored in location-order, so we can search it, either
293 binary-wise or end-to-front, to find the most recent
294 classification for a given diagnostic, given the location of the
296 diagnostic_classification_change_t
*m_classification_history
;
298 /* The size of the above array. */
299 int m_n_classification_history
;
301 /* For pragma push/pop. */
306 /* A bundle of options relating to printing the user's source code
307 (potentially with a margin, underlining, labels, etc). */
309 struct diagnostic_source_printing_options
311 /* True if we should print the source line with a caret indicating
313 Corresponds to -fdiagnostics-show-caret. */
316 /* Maximum width of the source line printed. */
319 /* Character used at the caret when printing source locations. */
320 char caret_chars
[rich_location::STATICALLY_ALLOCATED_RANGES
];
322 /* When printing source code, should the characters at carets and ranges
323 be colorized? (assuming colorization is on at all).
324 This should be true for frontends that generate range information
325 (so that the ranges of code are colorized),
326 and false for frontends that merely specify points within the
327 source code (to avoid e.g. colorizing just the first character in
328 a token, which would look strange). */
329 bool colorize_source_p
;
331 /* When printing source code, should labelled ranges be printed?
332 Corresponds to -fdiagnostics-show-labels. */
335 /* When printing source code, should there be a left-hand margin
336 showing line numbers?
337 Corresponds to -fdiagnostics-show-line-numbers. */
338 bool show_line_numbers_p
;
340 /* If printing source code, what should the minimum width of the margin
341 be? Line numbers will be right-aligned, and padded to this width.
342 Corresponds to -fdiagnostics-minimum-margin-width=VALUE. */
343 int min_margin_width
;
345 /* Usable by plugins; if true, print a debugging ruler above the
350 /* This data structure bundles altogether any information relevant to
351 the context of a diagnostic message. */
352 class diagnostic_context
355 typedef void (*ice_handler_callback_t
) (diagnostic_context
*);
356 typedef void (*set_locations_callback_t
) (diagnostic_context
*,
359 void initialize (int n_opts
);
360 void color_init (int value
);
361 void urls_init (int value
);
363 void file_cache_init ();
367 void set_set_locations_callback (set_locations_callback_t cb
)
369 m_set_locations_cb
= cb
;
373 initialize_input_context (diagnostic_input_charset_callback ccb
,
374 bool should_skip_bom
);
379 bool warning_enabled_at (location_t loc
, int opt
);
381 bool option_unspecified_p (int opt
) const
383 return m_option_classifier
.option_unspecified_p (opt
);
386 bool report_diagnostic (diagnostic_info
*);
388 void report_current_module (location_t where
);
390 void check_max_errors (bool flush
);
391 void action_after_output (diagnostic_t diag_kind
);
394 classify_diagnostic (int option_index
,
395 diagnostic_t new_kind
,
398 return m_option_classifier
.classify_diagnostic (this,
404 void push_diagnostics (location_t where ATTRIBUTE_UNUSED
)
406 m_option_classifier
.push ();
408 void pop_diagnostics (location_t where
)
410 m_option_classifier
.pop (where
);
413 void emit_diagram (const diagnostic_diagram
&diagram
);
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_path_format (enum diagnostic_path_format val
)
436 void set_show_path_depths (bool val
) { m_show_path_depths
= val
; }
437 void set_show_option_requested (bool val
) { m_show_option_requested
= val
; }
438 void set_max_errors (int val
) { m_max_errors
= val
; }
439 void set_escape_format (enum diagnostics_escape_format val
)
441 m_escape_format
= val
;
443 void set_ice_handler_callback (ice_handler_callback_t cb
)
445 m_ice_handler_cb
= cb
;
448 /* Various accessors. */
449 bool warning_as_error_requested_p () const
451 return m_warning_as_error_requested
;
453 bool show_path_depths_p () const { return m_show_path_depths
; }
454 enum diagnostic_path_format
get_path_format () const { return m_path_format
; }
455 enum diagnostics_escape_format
get_escape_format () const
457 return m_escape_format
;
461 get_file_cache () const
466 edit_context
*get_edit_context () const
468 return m_edit_context_ptr
;
470 const diagnostic_client_data_hooks
*get_client_data_hooks ()
472 return m_client_data_hooks
;
474 text_art::theme
*get_diagram_theme () const { return m_diagrams
.m_theme
; }
476 int converted_column (expanded_location s
) const;
478 int &diagnostic_count (diagnostic_t kind
)
480 return m_diagnostic_count
[kind
];
484 bool includes_seen_p (const line_map_ordinary
*map
);
486 void print_any_cwe (const diagnostic_info
&diagnostic
);
487 void print_any_rules (const diagnostic_info
&diagnostic
);
488 void print_option_information (const diagnostic_info
&diagnostic
,
489 diagnostic_t orig_diag_kind
);
491 void show_any_path (const diagnostic_info
&diagnostic
);
493 void error_recursion () ATTRIBUTE_NORETURN
;
495 bool diagnostic_enabled (diagnostic_info
*diagnostic
);
497 void get_any_inlining_info (diagnostic_info
*diagnostic
);
500 Ideally, all of these would be private and have "m_" prefixes. */
503 /* Where most of the diagnostic formatting work is done. */
504 pretty_printer
*printer
;
507 /* Cache of source code. */
508 file_cache
*m_file_cache
;
510 /* The number of times we have issued diagnostics. */
511 int m_diagnostic_count
[DK_LAST_DIAGNOSTIC_KIND
];
513 /* True if it has been requested that warnings be treated as errors. */
514 bool m_warning_as_error_requested
;
516 /* The number of option indexes that can be passed to warning() et
520 /* The stack of sets of overridden diagnostic option severities. */
521 diagnostic_option_classifier m_option_classifier
;
523 /* True if we should print any CWE identifiers associated with
527 /* True if we should print any rules associated with diagnostics. */
530 /* How should diagnostic_path objects be printed. */
531 enum diagnostic_path_format m_path_format
;
533 /* True if we should print stack depths when printing diagnostic paths. */
534 bool m_show_path_depths
;
536 /* True if we should print the command line option which controls
537 each diagnostic, if known. */
538 bool m_show_option_requested
;
541 /* True if we should raise a SIGABRT on errors. */
542 bool m_abort_on_error
;
544 /* True if we should show the column number on diagnostics. */
547 /* True if pedwarns are errors. */
548 bool m_pedantic_errors
;
550 /* True if permerrors are warnings. */
553 /* The index of the option to associate with turning permerrors into
555 int m_opt_permissive
;
557 /* True if errors are fatal. */
560 /* True if all warnings should be disabled. */
561 bool m_inhibit_warnings
;
563 /* True if warnings should be given in system headers. */
564 bool m_warn_system_headers
;
567 /* Maximum number of errors to report. */
571 /* Client-supplied callbacks for use in text output. */
573 /* This function is called before any message is printed out. It is
574 responsible for preparing message prefix and such. For example, it
576 In file included from "/usr/local/include/curses.h:5:
577 from "/home/gdr/src/nifty_printer.h:56:
580 diagnostic_starter_fn begin_diagnostic
;
582 /* This function is called by diagnostic_show_locus in between
583 disjoint spans of source code, so that the context can print
584 something to indicate that a new span of source code has begun. */
585 diagnostic_start_span_fn start_span
;
587 /* This function is called after the diagnostic message is printed. */
588 diagnostic_finalizer_fn end_diagnostic
;
591 /* Client hook to report an internal error. */
592 void (*m_internal_error
) (diagnostic_context
*, const char *, va_list *);
594 /* Client hook to say whether the option controlling a diagnostic is
595 enabled. Returns nonzero if enabled, zero if disabled. */
596 int (*m_option_enabled
) (int, unsigned, void *);
598 /* Client information to pass as second argument to
600 void *m_option_state
;
602 /* Client hook to return the name of an option that controls a
603 diagnostic. Returns malloced memory. The first diagnostic_t
604 argument is the kind of diagnostic before any reclassification
605 (of warnings as errors, etc.); the second is the kind after any
606 reclassification. May return NULL if no name is to be printed.
607 May be passed 0 as well as the index of a particular option. */
608 char *(*m_option_name
) (diagnostic_context
*,
613 /* Client hook to return a URL describing the option that controls
614 a diagnostic. Returns malloced memory. May return NULL if no URL
615 is available. May be passed 0 as well as the index of a
616 particular option. */
617 char *(*m_get_option_url
) (diagnostic_context
*, int);
620 /* An optional hook for adding URLs to quoted text strings in
621 diagnostics. Only used for the main diagnostic message. */
622 urlifier
*m_urlifier
;
625 void (*m_print_path
) (diagnostic_context
*, const diagnostic_path
*);
626 json::value
*(*m_make_json_for_path
) (diagnostic_context
*,
627 const diagnostic_path
*);
629 /* Auxiliary data for client. */
630 void *m_client_aux_data
;
632 /* Used to detect that the last caret was printed at the same location. */
633 location_t m_last_location
;
636 /* Used to detect when the input file stack has changed since last
638 const line_map_ordinary
*m_last_module
;
643 /* A copy of lang_hooks.option_lang_mask (). */
644 unsigned m_lang_mask
;
646 bool m_inhibit_notes_p
;
648 diagnostic_source_printing_options m_source_printing
;
651 /* True if -freport-bug option is used. */
654 /* Used to specify additional diagnostic output to be emitted after the
655 rest of the diagnostic. This is for implementing
656 -fdiagnostics-parseable-fixits and GCC_EXTRA_DIAGNOSTIC_OUTPUT. */
657 enum diagnostics_extra_output_kind m_extra_output_kind
;
660 /* What units to use when outputting the column number. */
661 enum diagnostics_column_unit m_column_unit
;
663 /* The origin for the column number (1-based or 0-based typically). */
666 /* The size of the tabstop for tab expansion. */
670 /* How should non-ASCII/non-printable bytes be escaped when
671 a diagnostic suggests escaping the source code on output. */
672 enum diagnostics_escape_format m_escape_format
;
674 /* If non-NULL, an edit_context to which fix-it hints should be
675 applied, for generating patches. */
676 edit_context
*m_edit_context_ptr
;
678 /* Fields relating to diagnostic groups. */
680 /* How many diagnostic_group instances are currently alive. */
683 /* How many diagnostics have been emitted since the bottommost
684 diagnostic_group was pushed. */
685 int m_emission_count
;
686 } m_diagnostic_groups
;
688 /* How to output diagnostics (text vs a structured format such as JSON).
689 Must be non-NULL; owned by context. */
690 diagnostic_output_format
*m_output_format
;
692 /* Callback to set the locations of call sites along the inlining
693 stack corresponding to a diagnostic location. Needed to traverse
694 the BLOCK_SUPERCONTEXT() chain hanging off the LOCATION_BLOCK()
695 of a diagnostic's location. */
696 set_locations_callback_t m_set_locations_cb
;
698 /* Optional callback for attempting to handle ICEs gracefully. */
699 ice_handler_callback_t m_ice_handler_cb
;
701 /* Include files that diagnostic_report_current_module has already listed the
703 hash_set
<location_t
, false, location_hash
> *m_includes_seen
;
705 /* A bundle of hooks for providing data to the context about its client
706 e.g. version information, plugins, etc.
707 Used by SARIF output to give metadata about the client that's
708 producing diagnostics. */
709 diagnostic_client_data_hooks
*m_client_data_hooks
;
711 /* Support for diagrams. */
714 /* Theme to use when generating diagrams.
715 Can be NULL (if text art is disabled). */
716 text_art::theme
*m_theme
;
722 diagnostic_inhibit_notes (diagnostic_context
* context
)
724 context
->m_inhibit_notes_p
= true;
728 /* Client supplied function to announce a diagnostic
729 (for text-based diagnostic output). */
730 #define diagnostic_starter(DC) (DC)->m_text_callbacks.begin_diagnostic
732 /* Client supplied function called after a diagnostic message is
733 displayed (for text-based diagnostic output). */
734 #define diagnostic_finalizer(DC) (DC)->m_text_callbacks.end_diagnostic
736 /* Extension hooks for client. */
737 #define diagnostic_context_auxiliary_data(DC) (DC)->m_client_aux_data
738 #define diagnostic_info_auxiliary_data(DI) (DI)->x_data
740 /* Same as pp_format_decoder. Works on 'diagnostic_context *'. */
741 #define diagnostic_format_decoder(DC) ((DC)->printer->format_decoder)
743 /* Same as output_prefixing_rule. Works on 'diagnostic_context *'. */
744 #define diagnostic_prefixing_rule(DC) ((DC)->printer->wrapping.rule)
746 /* Raise SIGABRT on any diagnostic of severity DK_ERROR or higher. */
748 diagnostic_abort_on_error (diagnostic_context
*context
)
750 context
->m_abort_on_error
= true;
753 /* This diagnostic_context is used by front-ends that directly output
754 diagnostic messages without going through `error', `warning',
755 and similar functions. */
756 extern diagnostic_context
*global_dc
;
758 /* Returns whether the diagnostic framework has been intialized already and is
760 #define diagnostic_ready_p() (global_dc->printer != NULL)
762 /* The number of errors that have been issued so far. Ideally, these
763 would take a diagnostic_context as an argument. */
764 #define errorcount global_dc->diagnostic_count (DK_ERROR)
765 /* Similarly, but for warnings. */
766 #define warningcount global_dc->diagnostic_count (DK_WARNING)
767 /* Similarly, but for warnings promoted to errors. */
768 #define werrorcount global_dc->diagnostic_count (DK_WERROR)
769 /* Similarly, but for sorrys. */
770 #define sorrycount global_dc->diagnostic_count (DK_SORRY)
772 /* Returns nonzero if warnings should be emitted. */
773 #define diagnostic_report_warnings_p(DC, LOC) \
774 (!(DC)->m_inhibit_warnings \
775 && !(in_system_header_at (LOC) && !(DC)->m_warn_system_headers))
777 /* Override the option index to be used for reporting a
781 diagnostic_override_option_index (diagnostic_info
*info
, int optidx
)
783 info
->option_index
= optidx
;
786 /* Diagnostic related functions. */
789 diagnostic_initialize (diagnostic_context
*context
, int n_opts
)
791 context
->initialize (n_opts
);
795 diagnostic_color_init (diagnostic_context
*context
, int value
= -1)
797 context
->color_init (value
);
801 diagnostic_urls_init (diagnostic_context
*context
, int value
= -1)
803 context
->urls_init (value
);
807 diagnostic_finish (diagnostic_context
*context
)
813 diagnostic_report_current_module (diagnostic_context
*context
,
816 context
->report_current_module (where
);
819 extern void diagnostic_show_locus (diagnostic_context
*,
820 rich_location
*richloc
,
821 diagnostic_t diagnostic_kind
,
822 pretty_printer
*pp
= nullptr);
824 /* Because we read source files a second time after the frontend did it the
825 first time, we need to know how the frontend handled things like character
826 set conversion and UTF-8 BOM stripping, in order to make everything
827 consistent. This function needs to be called by each frontend that requires
828 non-default behavior, to inform the diagnostics infrastructure how input is
829 to be processed. The default behavior is to do no conversion and not to
832 The callback should return the input charset to be used to convert the given
833 file's contents to UTF-8, or it should return NULL if no conversion is needed
834 for this file. SHOULD_SKIP_BOM only applies in case no conversion was
835 performed, and if true, it will cause a UTF-8 BOM to be skipped at the
836 beginning of the file. (In case a conversion was performed, the BOM is
837 rather skipped as part of the conversion process.) */
840 diagnostic_initialize_input_context (diagnostic_context
*context
,
841 diagnostic_input_charset_callback ccb
,
842 bool should_skip_bom
)
844 context
->initialize_input_context (ccb
, should_skip_bom
);
847 /* Force diagnostics controlled by OPTIDX to be kind KIND. */
849 diagnostic_classify_diagnostic (diagnostic_context
*context
,
854 return context
->classify_diagnostic (optidx
, kind
, where
);
858 diagnostic_push_diagnostics (diagnostic_context
*context
,
861 context
->push_diagnostics (where
);
864 diagnostic_pop_diagnostics (diagnostic_context
*context
,
867 context
->pop_diagnostics (where
);
870 /* Report a diagnostic message (an error or a warning) as specified by
871 DC. This function is *the* subroutine in terms of which front-ends
872 should implement their specific diagnostic handling modules. The
873 front-end independent format specifiers are exactly those described
874 in the documentation of output_format.
875 Return true if a diagnostic was printed, false otherwise. */
878 diagnostic_report_diagnostic (diagnostic_context
*context
,
879 diagnostic_info
*diagnostic
)
881 return context
->report_diagnostic (diagnostic
);
884 #ifdef ATTRIBUTE_GCC_DIAG
885 extern void diagnostic_set_info (diagnostic_info
*, const char *, va_list *,
886 rich_location
*, diagnostic_t
) ATTRIBUTE_GCC_DIAG(2,0);
887 extern void diagnostic_set_info_translated (diagnostic_info
*, const char *,
888 va_list *, rich_location
*,
890 ATTRIBUTE_GCC_DIAG(2,0);
891 extern void diagnostic_append_note (diagnostic_context
*, location_t
,
892 const char *, ...) ATTRIBUTE_GCC_DIAG(3,4);
894 extern char *diagnostic_build_prefix (diagnostic_context
*, const diagnostic_info
*);
895 void default_diagnostic_starter (diagnostic_context
*, diagnostic_info
*);
896 void default_diagnostic_start_span_fn (diagnostic_context
*,
898 void default_diagnostic_finalizer (diagnostic_context
*, diagnostic_info
*,
900 void diagnostic_set_caret_max_width (diagnostic_context
*context
, int value
);
903 diagnostic_action_after_output (diagnostic_context
*context
,
904 diagnostic_t diag_kind
)
906 context
->action_after_output (diag_kind
);
910 diagnostic_check_max_errors (diagnostic_context
*context
, bool flush
= false)
912 context
->check_max_errors (flush
);
915 int get_terminal_width (void);
917 /* Return the location associated to this diagnostic. Parameter WHICH
918 specifies which location. By default, expand the first one. */
921 diagnostic_location (const diagnostic_info
* diagnostic
, int which
= 0)
923 return diagnostic
->message
.get_location (which
);
926 /* Return the number of locations to be printed in DIAGNOSTIC. */
929 diagnostic_num_locations (const diagnostic_info
* diagnostic
)
931 return diagnostic
->message
.m_richloc
->get_num_locations ();
934 /* Expand the location of this diagnostic. Use this function for
935 consistency. Parameter WHICH specifies which location. By default,
936 expand the first one. */
938 inline expanded_location
939 diagnostic_expand_location (const diagnostic_info
* diagnostic
, int which
= 0)
941 return diagnostic
->richloc
->get_expanded_location (which
);
944 /* This is somehow the right-side margin of a caret line, that is, we
945 print at least these many characters after the position pointed at
947 const int CARET_LINE_MARGIN
= 10;
949 /* Return true if the two locations can be represented within the same
950 caret line. This is used to build a prefix and also to determine
951 whether to print one or two caret lines. */
954 diagnostic_same_line (const diagnostic_context
*context
,
955 expanded_location s1
, expanded_location s2
)
957 return (s2
.column
&& s1
.line
== s2
.line
958 && (context
->m_source_printing
.max_width
- CARET_LINE_MARGIN
959 > abs (s1
.column
- s2
.column
)));
962 extern const char *diagnostic_get_color_for_kind (diagnostic_t kind
);
964 /* Pure text formatting support functions. */
965 extern char *file_name_as_prefix (diagnostic_context
*, const char *);
967 extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1
;
969 extern void diagnostic_output_format_init (diagnostic_context
*,
970 const char *base_file_name
,
971 enum diagnostics_output_format
);
972 extern void diagnostic_output_format_init_json_stderr (diagnostic_context
*context
);
973 extern void diagnostic_output_format_init_json_file (diagnostic_context
*context
,
974 const char *base_file_name
);
975 extern void diagnostic_output_format_init_sarif_stderr (diagnostic_context
*context
);
976 extern void diagnostic_output_format_init_sarif_file (diagnostic_context
*context
,
977 const char *base_file_name
);
978 extern void diagnostic_output_format_init_sarif_stream (diagnostic_context
*context
,
981 /* Compute the number of digits in the decimal representation of an integer. */
982 extern int num_digits (int);
984 extern json::value
*json_from_expanded_location (diagnostic_context
*context
,
988 warning_enabled_at (location_t loc
, int opt
)
990 return global_dc
->warning_enabled_at (loc
, opt
);
994 option_unspecified_p (int opt
)
996 return global_dc
->option_unspecified_p (opt
);
999 extern char *get_cwe_url (int cwe
);
1001 #endif /* ! GCC_DIAGNOSTIC_H */