RISC-V: Add RISC-V into vect_cmdline_needed
[official-gcc.git] / gcc / diagnostic.h
blob58341cec30857b162619b793e7fe5fe99a6d078c
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 class edit_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
190 implementation. */
192 class diagnostic_output_format
194 public:
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;
204 protected:
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
213 to stderr.
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
220 public:
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
239 public:
240 void init (int n_opts);
241 void fini ();
243 /* Save all diagnostic classifications in a stack. */
244 void push ();
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];
261 diagnostic_t
262 classify_diagnostic (const diagnostic_context *context,
263 int option_index,
264 diagnostic_t new_kind,
265 location_t where);
267 diagnostic_t
268 update_effective_level_from_pragmas (diagnostic_info *diagnostic) const;
270 private:
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
273 these structs. */
274 struct diagnostic_classification_change_t
276 location_t location;
277 int option;
278 diagnostic_t kind;
281 int m_n_opts;
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
288 all. */
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
295 diagnostic. */
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. */
302 int *m_push_list;
303 int m_n_push;
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
312 the location.
313 Corresponds to -fdiagnostics-show-caret. */
314 bool enabled;
316 /* Maximum width of the source line printed. */
317 int max_width;
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. */
333 bool show_labels_p;
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
346 source output. */
347 bool show_ruler_p;
350 /* This data structure bundles altogether any information relevant to
351 the context of a diagnostic message. */
352 class diagnostic_context
354 public:
355 typedef void (*ice_handler_callback_t) (diagnostic_context *);
356 typedef void (*set_locations_callback_t) (diagnostic_context *,
357 diagnostic_info *);
359 void initialize (int n_opts);
360 void color_init (int value);
361 void urls_init (int value);
363 void file_cache_init ();
365 void finish ();
367 void set_set_locations_callback (set_locations_callback_t cb)
369 m_set_locations_cb = cb;
372 void
373 initialize_input_context (diagnostic_input_charset_callback ccb,
374 bool should_skip_bom);
376 void begin_group ();
377 void end_group ();
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);
393 diagnostic_t
394 classify_diagnostic (int option_index,
395 diagnostic_t new_kind,
396 location_t where)
398 return m_option_classifier.classify_diagnostic (this,
399 option_index,
400 new_kind,
401 where);
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)
434 m_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;
460 file_cache *
461 get_file_cache () const
463 return m_file_cache;
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];
483 private:
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);
499 /* Data members.
500 Ideally, all of these would be private and have "m_" prefixes. */
502 public:
503 /* Where most of the diagnostic formatting work is done. */
504 pretty_printer *printer;
506 private:
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
517 al. */
518 int m_n_opts;
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
524 diagnostics. */
525 bool m_show_cwe;
527 /* True if we should print any rules associated with diagnostics. */
528 bool m_show_rules;
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;
540 public:
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. */
545 bool m_show_column;
547 /* True if pedwarns are errors. */
548 bool m_pedantic_errors;
550 /* True if permerrors are warnings. */
551 bool m_permissive;
553 /* The index of the option to associate with turning permerrors into
554 warnings. */
555 int m_opt_permissive;
557 /* True if errors are fatal. */
558 bool m_fatal_errors;
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;
566 private:
567 /* Maximum number of errors to report. */
568 int m_max_errors;
570 public:
571 /* Client-supplied callbacks for use in text output. */
572 struct {
573 /* This function is called before any message is printed out. It is
574 responsible for preparing message prefix and such. For example, it
575 might say:
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;
589 } m_text_callbacks;
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
599 option_enabled. */
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 *,
609 int,
610 diagnostic_t,
611 diagnostic_t);
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);
619 private:
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;
624 public:
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;
635 private:
636 /* Used to detect when the input file stack has changed since last
637 described. */
638 const line_map_ordinary *m_last_module;
640 int m_lock;
642 public:
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;
650 private:
651 /* True if -freport-bug option is used. */
652 bool m_report_bug;
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;
659 public:
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). */
664 int m_column_origin;
666 /* The size of the tabstop for tab expansion. */
667 int m_tabstop;
669 private:
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. */
679 struct {
680 /* How many diagnostic_group instances are currently alive. */
681 int m_nesting_depth;
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
702 include path for. */
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. */
712 struct
714 /* Theme to use when generating diagrams.
715 Can be NULL (if text art is disabled). */
716 text_art::theme *m_theme;
718 } m_diagrams;
721 inline void
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. */
747 inline void
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
759 ready for use. */
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
778 diagnostic. */
780 inline void
781 diagnostic_override_option_index (diagnostic_info *info, int optidx)
783 info->option_index = optidx;
786 /* Diagnostic related functions. */
788 inline void
789 diagnostic_initialize (diagnostic_context *context, int n_opts)
791 context->initialize (n_opts);
794 inline void
795 diagnostic_color_init (diagnostic_context *context, int value = -1)
797 context->color_init (value);
800 inline void
801 diagnostic_urls_init (diagnostic_context *context, int value = -1)
803 context->urls_init (value);
806 inline void
807 diagnostic_finish (diagnostic_context *context)
809 context->finish ();
812 inline void
813 diagnostic_report_current_module (diagnostic_context *context,
814 location_t where)
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
830 strip a UTF-8 BOM.
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.) */
839 inline void
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. */
848 inline diagnostic_t
849 diagnostic_classify_diagnostic (diagnostic_context *context,
850 int optidx,
851 diagnostic_t kind,
852 location_t where)
854 return context->classify_diagnostic (optidx, kind, where);
857 inline void
858 diagnostic_push_diagnostics (diagnostic_context *context,
859 location_t where)
861 context->push_diagnostics (where);
863 inline void
864 diagnostic_pop_diagnostics (diagnostic_context *context,
865 location_t where)
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. */
877 inline bool
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 *,
889 diagnostic_t)
890 ATTRIBUTE_GCC_DIAG(2,0);
891 extern void diagnostic_append_note (diagnostic_context *, location_t,
892 const char *, ...) ATTRIBUTE_GCC_DIAG(3,4);
893 #endif
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 *,
897 expanded_location);
898 void default_diagnostic_finalizer (diagnostic_context *, diagnostic_info *,
899 diagnostic_t);
900 void diagnostic_set_caret_max_width (diagnostic_context *context, int value);
902 inline void
903 diagnostic_action_after_output (diagnostic_context *context,
904 diagnostic_t diag_kind)
906 context->action_after_output (diag_kind);
909 inline void
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. */
920 inline location_t
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. */
928 inline unsigned int
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
946 by the caret. */
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. */
953 inline bool
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,
979 FILE *stream);
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,
985 location_t loc);
987 inline bool
988 warning_enabled_at (location_t loc, int opt)
990 return global_dc->warning_enabled_at (loc, opt);
993 inline bool
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 */