]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/diagnostic.h
libstdc++: fix C header include guards
[thirdparty/gcc.git] / gcc / diagnostic.h
1 /* Various declarations for language-independent diagnostics subroutines.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 #ifndef GCC_DIAGNOSTIC_H
22 #define GCC_DIAGNOSTIC_H
23
24 #include "rich-location.h"
25 #include "pretty-print.h"
26 #include "diagnostic-core.h"
27
28 namespace text_art
29 {
30 class theme;
31 } // namespace text_art
32
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. */
37
38 enum diagnostics_column_unit
39 {
40 /* The default from GCC 11 onwards: display columns. */
41 DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
42
43 /* The behavior in GCC 10 and earlier: simple bytes. */
44 DIAGNOSTICS_COLUMN_UNIT_BYTE
45 };
46
47 /* An enum for controlling how to print non-ASCII characters/bytes when
48 a diagnostic suggests escaping the source code on output. */
49
50 enum diagnostics_escape_format
51 {
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,
55
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
59 };
60
61 /* Enum for overriding the standard output format. */
62
63 enum diagnostics_output_format
64 {
65 /* The default: textual output. */
66 DIAGNOSTICS_OUTPUT_FORMAT_TEXT,
67
68 /* JSON-based output, to stderr. */
69 DIAGNOSTICS_OUTPUT_FORMAT_JSON_STDERR,
70
71 /* JSON-based output, to a file. */
72 DIAGNOSTICS_OUTPUT_FORMAT_JSON_FILE,
73
74 /* SARIF-based output, to stderr. */
75 DIAGNOSTICS_OUTPUT_FORMAT_SARIF_STDERR,
76
77 /* SARIF-based output, to a file. */
78 DIAGNOSTICS_OUTPUT_FORMAT_SARIF_FILE
79 };
80
81 /* An enum for controlling how diagnostic_paths should be printed. */
82 enum diagnostic_path_format
83 {
84 /* Don't print diagnostic_paths. */
85 DPF_NONE,
86
87 /* Print diagnostic_paths by emitting a separate "note" for every event
88 in the path. */
89 DPF_SEPARATE_EVENTS,
90
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
96 };
97
98 /* An enum for capturing values of GCC_EXTRA_DIAGNOSTIC_OUTPUT,
99 and for -fdiagnostics-parseable-fixits. */
100
101 enum diagnostics_extra_output_kind
102 {
103 /* No extra output, or an unrecognized value. */
104 EXTRA_DIAGNOSTIC_OUTPUT_none,
105
106 /* Emit fix-it hints using the "fixits-v1" format, equivalent to
107 -fdiagnostics-parseable-fixits. */
108 EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1,
109
110 /* Emit fix-it hints using the "fixits-v2" format. */
111 EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2
112 };
113
114 /* Values for -fdiagnostics-text-art-charset=. */
115
116 enum diagnostic_text_art_charset
117 {
118 /* No text art diagrams shall be emitted. */
119 DIAGNOSTICS_TEXT_ART_CHARSET_NONE,
120
121 /* Use pure ASCII for text art diagrams. */
122 DIAGNOSTICS_TEXT_ART_CHARSET_ASCII,
123
124 /* Use ASCII + conservative use of other unicode characters
125 in text art diagrams. */
126 DIAGNOSTICS_TEXT_ART_CHARSET_UNICODE,
127
128 /* Use Emoji. */
129 DIAGNOSTICS_TEXT_ART_CHARSET_EMOJI
130 };
131
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
136 {
137 diagnostic_info ()
138 : message (), richloc (), metadata (), x_data (), kind (), option_index (),
139 m_iinfo ()
140 { }
141
142 /* Text to be formatted. */
143 text_info message;
144
145 /* The location at which the diagnostic is to be reported. */
146 rich_location *richloc;
147
148 /* An optional bundle of metadata associated with the diagnostic
149 (or NULL). */
150 const diagnostic_metadata *metadata;
151
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;
158
159 /* Inlining context containing locations for each call site along
160 the inlining stack. */
161 struct inlining_info
162 {
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;
170 };
171
172 /* Forward declarations. */
173 typedef void (*diagnostic_starter_fn) (diagnostic_context *,
174 const diagnostic_info *);
175
176 typedef void (*diagnostic_start_span_fn) (diagnostic_context *,
177 expanded_location);
178
179 typedef void (*diagnostic_finalizer_fn) (diagnostic_context *,
180 const diagnostic_info *,
181 diagnostic_t);
182
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,
190 unsigned);
191
192 class edit_context;
193 namespace json { class value; }
194 class diagnostic_client_data_hooks;
195 class logical_location;
196 class diagnostic_diagram;
197 class diagnostic_source_effect_info;
198
199 /* Abstract base class for a particular output format for diagnostics;
200 each value of -fdiagnostics-output-format= will have its own
201 implementation. */
202
203 class diagnostic_output_format
204 {
205 public:
206 virtual ~diagnostic_output_format () {}
207
208 virtual void on_begin_group () = 0;
209 virtual void on_end_group () = 0;
210 virtual void on_begin_diagnostic (const diagnostic_info &) = 0;
211 virtual void on_end_diagnostic (const diagnostic_info &,
212 diagnostic_t orig_diag_kind) = 0;
213 virtual void on_diagram (const diagnostic_diagram &diagram) = 0;
214 virtual bool machine_readable_stderr_p () const = 0;
215
216 protected:
217 diagnostic_output_format (diagnostic_context &context)
218 : m_context (context)
219 {}
220
221 diagnostic_context &m_context;
222 };
223
224 /* Subclass of diagnostic_output_format for classic text-based output
225 to stderr.
226
227 Uses diagnostic_context.m_text_callbacks to provide client-specific
228 textual output (e.g. include paths, macro expansions, etc). */
229
230 class diagnostic_text_output_format : public diagnostic_output_format
231 {
232 public:
233 diagnostic_text_output_format (diagnostic_context &context)
234 : diagnostic_output_format (context)
235 {}
236 ~diagnostic_text_output_format ();
237 void on_begin_group () override {}
238 void on_end_group () override {}
239 void on_begin_diagnostic (const diagnostic_info &) override;
240 void on_end_diagnostic (const diagnostic_info &,
241 diagnostic_t orig_diag_kind) override;
242 void on_diagram (const diagnostic_diagram &diagram) override;
243 bool machine_readable_stderr_p () const final override
244 {
245 return false;
246 }
247 };
248
249 /* A stack of sets of classifications: each entry in the stack is
250 a mapping from option index to diagnostic severity that can be changed
251 via pragmas. The stack can be pushed and popped. */
252
253 class diagnostic_option_classifier
254 {
255 public:
256 void init (int n_opts);
257 void fini ();
258
259 /* Save all diagnostic classifications in a stack. */
260 void push ();
261
262 /* Restore the topmost classification set off the stack. If the stack
263 is empty, revert to the state based on command line parameters. */
264 void pop (location_t where);
265
266 bool option_unspecified_p (int opt) const
267 {
268 return get_current_override (opt) == DK_UNSPECIFIED;
269 }
270
271 diagnostic_t get_current_override (int opt) const
272 {
273 gcc_assert (opt < m_n_opts);
274 return m_classify_diagnostic[opt];
275 }
276
277 diagnostic_t
278 classify_diagnostic (const diagnostic_context *context,
279 int option_index,
280 diagnostic_t new_kind,
281 location_t where);
282
283 diagnostic_t
284 update_effective_level_from_pragmas (diagnostic_info *diagnostic) const;
285
286 private:
287 /* Each time a diagnostic's classification is changed with a pragma,
288 we record the change and the location of the change in an array of
289 these structs. */
290 struct diagnostic_classification_change_t
291 {
292 location_t location;
293 int option;
294 diagnostic_t kind;
295 };
296
297 int m_n_opts;
298
299 /* For each option index that can be passed to warning() et al
300 (OPT_* from options.h when using this code with the core GCC
301 options), this array may contain a new kind that the diagnostic
302 should be changed to before reporting, or DK_UNSPECIFIED to leave
303 it as the reported kind, or DK_IGNORED to not report it at
304 all. */
305 diagnostic_t *m_classify_diagnostic;
306
307 /* History of all changes to the classifications above. This list
308 is stored in location-order, so we can search it, either
309 binary-wise or end-to-front, to find the most recent
310 classification for a given diagnostic, given the location of the
311 diagnostic. */
312 diagnostic_classification_change_t *m_classification_history;
313
314 /* The size of the above array. */
315 int m_n_classification_history;
316
317 /* For pragma push/pop. */
318 int *m_push_list;
319 int m_n_push;
320 };
321
322 /* A bundle of options relating to printing the user's source code
323 (potentially with a margin, underlining, labels, etc). */
324
325 struct diagnostic_source_printing_options
326 {
327 /* True if we should print the source line with a caret indicating
328 the location.
329 Corresponds to -fdiagnostics-show-caret. */
330 bool enabled;
331
332 /* Maximum width of the source line printed. */
333 int max_width;
334
335 /* Character used at the caret when printing source locations. */
336 char caret_chars[rich_location::STATICALLY_ALLOCATED_RANGES];
337
338 /* When printing source code, should the characters at carets and ranges
339 be colorized? (assuming colorization is on at all).
340 This should be true for frontends that generate range information
341 (so that the ranges of code are colorized),
342 and false for frontends that merely specify points within the
343 source code (to avoid e.g. colorizing just the first character in
344 a token, which would look strange). */
345 bool colorize_source_p;
346
347 /* When printing source code, should labelled ranges be printed?
348 Corresponds to -fdiagnostics-show-labels. */
349 bool show_labels_p;
350
351 /* When printing source code, should there be a left-hand margin
352 showing line numbers?
353 Corresponds to -fdiagnostics-show-line-numbers. */
354 bool show_line_numbers_p;
355
356 /* If printing source code, what should the minimum width of the margin
357 be? Line numbers will be right-aligned, and padded to this width.
358 Corresponds to -fdiagnostics-minimum-margin-width=VALUE. */
359 int min_margin_width;
360
361 /* Usable by plugins; if true, print a debugging ruler above the
362 source output. */
363 bool show_ruler_p;
364
365 /* When printing events in an inline path, should we print lines
366 visualizing links between related events (e.g. for CFG paths)?
367 Corresponds to -fdiagnostics-show-event-links. */
368 bool show_event_links_p;
369 };
370
371 /* This data structure bundles altogether any information relevant to
372 the context of a diagnostic message. */
373 class diagnostic_context
374 {
375 public:
376 /* Give access to m_text_callbacks. */
377 friend diagnostic_starter_fn &
378 diagnostic_starter (diagnostic_context *context);
379 friend diagnostic_start_span_fn &
380 diagnostic_start_span (diagnostic_context *context);
381 friend diagnostic_finalizer_fn &
382 diagnostic_finalizer (diagnostic_context *context);
383
384 typedef void (*ice_handler_callback_t) (diagnostic_context *);
385 typedef void (*set_locations_callback_t) (diagnostic_context *,
386 diagnostic_info *);
387
388 void initialize (int n_opts);
389 void color_init (int value);
390 void urls_init (int value);
391
392 void finish ();
393
394 void set_set_locations_callback (set_locations_callback_t cb)
395 {
396 m_set_locations_cb = cb;
397 }
398
399 void
400 initialize_input_context (diagnostic_input_charset_callback ccb,
401 bool should_skip_bom);
402
403 void begin_group ();
404 void end_group ();
405
406 bool warning_enabled_at (location_t loc, int opt);
407
408 bool option_unspecified_p (int opt) const
409 {
410 return m_option_classifier.option_unspecified_p (opt);
411 }
412
413 bool report_diagnostic (diagnostic_info *);
414
415 void report_current_module (location_t where);
416
417 void check_max_errors (bool flush);
418 void action_after_output (diagnostic_t diag_kind);
419
420 diagnostic_t
421 classify_diagnostic (int option_index,
422 diagnostic_t new_kind,
423 location_t where)
424 {
425 return m_option_classifier.classify_diagnostic (this,
426 option_index,
427 new_kind,
428 where);
429 }
430
431 void push_diagnostics (location_t where ATTRIBUTE_UNUSED)
432 {
433 m_option_classifier.push ();
434 }
435 void pop_diagnostics (location_t where)
436 {
437 m_option_classifier.pop (where);
438 }
439
440 void maybe_show_locus (const rich_location &richloc,
441 diagnostic_t diagnostic_kind,
442 pretty_printer *pp,
443 diagnostic_source_effect_info *effect_info);
444
445 void emit_diagram (const diagnostic_diagram &diagram);
446
447 const diagnostic_output_format *get_output_format () const
448 {
449 return m_output_format;
450 }
451
452 /* Various setters for use by option-handling logic. */
453 void set_output_format (diagnostic_output_format *output_format);
454 void set_text_art_charset (enum diagnostic_text_art_charset charset);
455 void set_client_data_hooks (diagnostic_client_data_hooks *hooks);
456 void set_urlifier (urlifier *);
457 void create_edit_context ();
458 void set_warning_as_error_requested (bool val)
459 {
460 m_warning_as_error_requested = val;
461 }
462 void set_report_bug (bool val) { m_report_bug = val; }
463 void set_extra_output_kind (enum diagnostics_extra_output_kind kind)
464 {
465 m_extra_output_kind = kind;
466 }
467 void set_show_cwe (bool val) { m_show_cwe = val; }
468 void set_show_rules (bool val) { m_show_rules = val; }
469 void set_path_format (enum diagnostic_path_format val)
470 {
471 m_path_format = val;
472 }
473 void set_show_path_depths (bool val) { m_show_path_depths = val; }
474 void set_show_option_requested (bool val) { m_show_option_requested = val; }
475 void set_max_errors (int val) { m_max_errors = val; }
476 void set_escape_format (enum diagnostics_escape_format val)
477 {
478 m_escape_format = val;
479 }
480 void set_ice_handler_callback (ice_handler_callback_t cb)
481 {
482 m_ice_handler_cb = cb;
483 }
484
485 /* Various accessors. */
486 bool warning_as_error_requested_p () const
487 {
488 return m_warning_as_error_requested;
489 }
490 bool show_path_depths_p () const { return m_show_path_depths; }
491 enum diagnostic_path_format get_path_format () const { return m_path_format; }
492 enum diagnostics_escape_format get_escape_format () const
493 {
494 return m_escape_format;
495 }
496
497 file_cache &
498 get_file_cache () const
499 {
500 gcc_assert (m_file_cache);
501 return *m_file_cache;
502 }
503
504 edit_context *get_edit_context () const
505 {
506 return m_edit_context_ptr;
507 }
508 const diagnostic_client_data_hooks *get_client_data_hooks ()
509 {
510 return m_client_data_hooks;
511 }
512 text_art::theme *get_diagram_theme () const { return m_diagrams.m_theme; }
513
514 int converted_column (expanded_location s) const;
515
516 int &diagnostic_count (diagnostic_t kind)
517 {
518 return m_diagnostic_count[kind];
519 }
520
521 /* Option-related member functions. */
522 inline bool option_enabled_p (int option_index) const
523 {
524 if (!m_option_callbacks.m_option_enabled_cb)
525 return true;
526 return m_option_callbacks.m_option_enabled_cb
527 (option_index,
528 m_option_callbacks.m_lang_mask,
529 m_option_callbacks.m_option_state);
530 }
531
532 inline char *make_option_name (int option_index,
533 diagnostic_t orig_diag_kind,
534 diagnostic_t diag_kind) const
535 {
536 if (!m_option_callbacks.m_make_option_name_cb)
537 return nullptr;
538 return m_option_callbacks.m_make_option_name_cb (this, option_index,
539 orig_diag_kind,
540 diag_kind);
541 }
542
543 inline char *make_option_url (int option_index) const
544 {
545 if (!m_option_callbacks.m_make_option_url_cb)
546 return nullptr;
547 return m_option_callbacks.m_make_option_url_cb (this, option_index,
548 get_lang_mask ());
549 }
550
551 void
552 set_option_hooks (diagnostic_option_enabled_cb option_enabled_cb,
553 void *option_state,
554 diagnostic_make_option_name_cb make_option_name_cb,
555 diagnostic_make_option_url_cb make_option_url_cb,
556 unsigned lang_mask);
557
558 unsigned get_lang_mask () const
559 {
560 return m_option_callbacks.m_lang_mask;
561 }
562
563 label_text get_location_text (const expanded_location &s) const;
564
565 private:
566 bool includes_seen_p (const line_map_ordinary *map);
567
568 void print_any_cwe (const diagnostic_info &diagnostic);
569 void print_any_rules (const diagnostic_info &diagnostic);
570 void print_option_information (const diagnostic_info &diagnostic,
571 diagnostic_t orig_diag_kind);
572
573 void show_any_path (const diagnostic_info &diagnostic);
574
575 void error_recursion () ATTRIBUTE_NORETURN;
576
577 bool diagnostic_enabled (diagnostic_info *diagnostic);
578
579 void get_any_inlining_info (diagnostic_info *diagnostic);
580
581 void show_locus (const rich_location &richloc,
582 diagnostic_t diagnostic_kind,
583 pretty_printer *pp,
584 diagnostic_source_effect_info *effect_info);
585
586 /* Data members.
587 Ideally, all of these would be private and have "m_" prefixes. */
588
589 public:
590 /* Where most of the diagnostic formatting work is done. */
591 pretty_printer *printer;
592
593 private:
594 /* Cache of source code. */
595 file_cache *m_file_cache;
596
597 /* The number of times we have issued diagnostics. */
598 int m_diagnostic_count[DK_LAST_DIAGNOSTIC_KIND];
599
600 /* True if it has been requested that warnings be treated as errors. */
601 bool m_warning_as_error_requested;
602
603 /* The number of option indexes that can be passed to warning() et
604 al. */
605 int m_n_opts;
606
607 /* The stack of sets of overridden diagnostic option severities. */
608 diagnostic_option_classifier m_option_classifier;
609
610 /* True if we should print any CWE identifiers associated with
611 diagnostics. */
612 bool m_show_cwe;
613
614 /* True if we should print any rules associated with diagnostics. */
615 bool m_show_rules;
616
617 /* How should diagnostic_path objects be printed. */
618 enum diagnostic_path_format m_path_format;
619
620 /* True if we should print stack depths when printing diagnostic paths. */
621 bool m_show_path_depths;
622
623 /* True if we should print the command line option which controls
624 each diagnostic, if known. */
625 bool m_show_option_requested;
626
627 public:
628 /* True if we should raise a SIGABRT on errors. */
629 bool m_abort_on_error;
630
631 /* True if we should show the column number on diagnostics. */
632 bool m_show_column;
633
634 /* True if pedwarns are errors. */
635 bool m_pedantic_errors;
636
637 /* True if permerrors are warnings. */
638 bool m_permissive;
639
640 /* The index of the option to associate with turning permerrors into
641 warnings. */
642 int m_opt_permissive;
643
644 /* True if errors are fatal. */
645 bool m_fatal_errors;
646
647 /* True if all warnings should be disabled. */
648 bool m_inhibit_warnings;
649
650 /* True if warnings should be given in system headers. */
651 bool m_warn_system_headers;
652
653 private:
654 /* Maximum number of errors to report. */
655 int m_max_errors;
656
657 /* Client-supplied callbacks for use in text output. */
658 struct {
659 /* This function is called before any message is printed out. It is
660 responsible for preparing message prefix and such. For example, it
661 might say:
662 In file included from "/usr/local/include/curses.h:5:
663 from "/home/gdr/src/nifty_printer.h:56:
664 ...
665 */
666 diagnostic_starter_fn m_begin_diagnostic;
667
668 /* This function is called by diagnostic_show_locus in between
669 disjoint spans of source code, so that the context can print
670 something to indicate that a new span of source code has begun. */
671 diagnostic_start_span_fn m_start_span;
672
673 /* This function is called after the diagnostic message is printed. */
674 diagnostic_finalizer_fn m_end_diagnostic;
675 } m_text_callbacks;
676
677 public:
678 /* Client hook to report an internal error. */
679 void (*m_internal_error) (diagnostic_context *, const char *, va_list *);
680
681 private:
682 /* Client-supplied callbacks for working with options. */
683 struct {
684 /* Client hook to say whether the option controlling a diagnostic is
685 enabled. Returns nonzero if enabled, zero if disabled. */
686 diagnostic_option_enabled_cb m_option_enabled_cb;
687
688 /* Client information to pass as second argument to
689 m_option_enabled_cb. */
690 void *m_option_state;
691
692 /* Client hook to return the name of an option that controls a
693 diagnostic. Returns malloced memory. The first diagnostic_t
694 argument is the kind of diagnostic before any reclassification
695 (of warnings as errors, etc.); the second is the kind after any
696 reclassification. May return NULL if no name is to be printed.
697 May be passed 0 as well as the index of a particular option. */
698 diagnostic_make_option_name_cb m_make_option_name_cb;
699
700 /* Client hook to return a URL describing the option that controls
701 a diagnostic. Returns malloced memory. May return NULL if no URL
702 is available. May be passed 0 as well as the index of a
703 particular option. */
704 diagnostic_make_option_url_cb m_make_option_url_cb;
705
706 /* A copy of lang_hooks.option_lang_mask (). */
707 unsigned m_lang_mask;
708 } m_option_callbacks;
709
710 /* An optional hook for adding URLs to quoted text strings in
711 diagnostics. Only used for the main diagnostic message. */
712 urlifier *m_urlifier;
713
714 public:
715 void (*m_print_path) (diagnostic_context *, const diagnostic_path *);
716 json::value *(*m_make_json_for_path) (diagnostic_context *,
717 const diagnostic_path *);
718
719 /* Auxiliary data for client. */
720 void *m_client_aux_data;
721
722 /* Used to detect that the last caret was printed at the same location. */
723 location_t m_last_location;
724
725 private:
726 /* Used to detect when the input file stack has changed since last
727 described. */
728 const line_map_ordinary *m_last_module;
729
730 int m_lock;
731
732 public:
733 bool m_inhibit_notes_p;
734
735 diagnostic_source_printing_options m_source_printing;
736
737 private:
738 /* True if -freport-bug option is used. */
739 bool m_report_bug;
740
741 /* Used to specify additional diagnostic output to be emitted after the
742 rest of the diagnostic. This is for implementing
743 -fdiagnostics-parseable-fixits and GCC_EXTRA_DIAGNOSTIC_OUTPUT. */
744 enum diagnostics_extra_output_kind m_extra_output_kind;
745
746 public:
747 /* What units to use when outputting the column number. */
748 enum diagnostics_column_unit m_column_unit;
749
750 /* The origin for the column number (1-based or 0-based typically). */
751 int m_column_origin;
752
753 /* The size of the tabstop for tab expansion. */
754 int m_tabstop;
755
756 private:
757 /* How should non-ASCII/non-printable bytes be escaped when
758 a diagnostic suggests escaping the source code on output. */
759 enum diagnostics_escape_format m_escape_format;
760
761 /* If non-NULL, an edit_context to which fix-it hints should be
762 applied, for generating patches. */
763 edit_context *m_edit_context_ptr;
764
765 /* Fields relating to diagnostic groups. */
766 struct {
767 /* How many diagnostic_group instances are currently alive. */
768 int m_nesting_depth;
769
770 /* How many diagnostics have been emitted since the bottommost
771 diagnostic_group was pushed. */
772 int m_emission_count;
773 } m_diagnostic_groups;
774
775 /* How to output diagnostics (text vs a structured format such as JSON).
776 Must be non-NULL; owned by context. */
777 diagnostic_output_format *m_output_format;
778
779 /* Callback to set the locations of call sites along the inlining
780 stack corresponding to a diagnostic location. Needed to traverse
781 the BLOCK_SUPERCONTEXT() chain hanging off the LOCATION_BLOCK()
782 of a diagnostic's location. */
783 set_locations_callback_t m_set_locations_cb;
784
785 /* Optional callback for attempting to handle ICEs gracefully. */
786 ice_handler_callback_t m_ice_handler_cb;
787
788 /* Include files that diagnostic_report_current_module has already listed the
789 include path for. */
790 hash_set<location_t, false, location_hash> *m_includes_seen;
791
792 /* A bundle of hooks for providing data to the context about its client
793 e.g. version information, plugins, etc.
794 Used by SARIF output to give metadata about the client that's
795 producing diagnostics. */
796 diagnostic_client_data_hooks *m_client_data_hooks;
797
798 /* Support for diagrams. */
799 struct
800 {
801 /* Theme to use when generating diagrams.
802 Can be NULL (if text art is disabled). */
803 text_art::theme *m_theme;
804
805 } m_diagrams;
806 };
807
808 inline void
809 diagnostic_inhibit_notes (diagnostic_context * context)
810 {
811 context->m_inhibit_notes_p = true;
812 }
813
814
815 /* Client supplied function to announce a diagnostic
816 (for text-based diagnostic output). */
817 inline diagnostic_starter_fn &
818 diagnostic_starter (diagnostic_context *context)
819 {
820 return context->m_text_callbacks.m_begin_diagnostic;
821 }
822
823 /* Client supplied function called between disjoint spans of source code,
824 so that the context can print
825 something to indicate that a new span of source code has begun. */
826 inline diagnostic_start_span_fn &
827 diagnostic_start_span (diagnostic_context *context)
828 {
829 return context->m_text_callbacks.m_start_span;
830 }
831
832 /* Client supplied function called after a diagnostic message is
833 displayed (for text-based diagnostic output). */
834 inline diagnostic_finalizer_fn &
835 diagnostic_finalizer (diagnostic_context *context)
836 {
837 return context->m_text_callbacks.m_end_diagnostic;
838 }
839
840 /* Extension hooks for client. */
841 #define diagnostic_context_auxiliary_data(DC) (DC)->m_client_aux_data
842 #define diagnostic_info_auxiliary_data(DI) (DI)->x_data
843
844 /* Same as pp_format_decoder. Works on 'diagnostic_context *'. */
845 #define diagnostic_format_decoder(DC) ((DC)->printer->format_decoder)
846
847 /* Same as output_prefixing_rule. Works on 'diagnostic_context *'. */
848 #define diagnostic_prefixing_rule(DC) ((DC)->printer->wrapping.rule)
849
850 /* Raise SIGABRT on any diagnostic of severity DK_ERROR or higher. */
851 inline void
852 diagnostic_abort_on_error (diagnostic_context *context)
853 {
854 context->m_abort_on_error = true;
855 }
856
857 /* This diagnostic_context is used by front-ends that directly output
858 diagnostic messages without going through `error', `warning',
859 and similar functions. */
860 extern diagnostic_context *global_dc;
861
862 /* Returns whether the diagnostic framework has been intialized already and is
863 ready for use. */
864 inline bool
865 diagnostic_ready_p ()
866 {
867 return global_dc->printer != nullptr;
868 }
869
870 /* The number of errors that have been issued so far. Ideally, these
871 would take a diagnostic_context as an argument. */
872 #define errorcount global_dc->diagnostic_count (DK_ERROR)
873 /* Similarly, but for warnings. */
874 #define warningcount global_dc->diagnostic_count (DK_WARNING)
875 /* Similarly, but for warnings promoted to errors. */
876 #define werrorcount global_dc->diagnostic_count (DK_WERROR)
877 /* Similarly, but for sorrys. */
878 #define sorrycount global_dc->diagnostic_count (DK_SORRY)
879
880 /* Returns nonzero if warnings should be emitted. */
881 #define diagnostic_report_warnings_p(DC, LOC) \
882 (!(DC)->m_inhibit_warnings \
883 && !(in_system_header_at (LOC) && !(DC)->m_warn_system_headers))
884
885 /* Override the option index to be used for reporting a
886 diagnostic. */
887
888 inline void
889 diagnostic_override_option_index (diagnostic_info *info, int optidx)
890 {
891 info->option_index = optidx;
892 }
893
894 /* Diagnostic related functions. */
895
896 inline void
897 diagnostic_initialize (diagnostic_context *context, int n_opts)
898 {
899 context->initialize (n_opts);
900 }
901
902 inline void
903 diagnostic_color_init (diagnostic_context *context, int value = -1)
904 {
905 context->color_init (value);
906 }
907
908 inline void
909 diagnostic_urls_init (diagnostic_context *context, int value = -1)
910 {
911 context->urls_init (value);
912 }
913
914 inline void
915 diagnostic_finish (diagnostic_context *context)
916 {
917 context->finish ();
918 }
919
920 inline void
921 diagnostic_report_current_module (diagnostic_context *context,
922 location_t where)
923 {
924 context->report_current_module (where);
925 }
926
927 inline void
928 diagnostic_show_locus (diagnostic_context *context,
929 rich_location *richloc,
930 diagnostic_t diagnostic_kind,
931 pretty_printer *pp = nullptr,
932 diagnostic_source_effect_info *effect_info = nullptr)
933 {
934 gcc_assert (richloc);
935 context->maybe_show_locus (*richloc, diagnostic_kind, pp, effect_info);
936 }
937
938 /* Because we read source files a second time after the frontend did it the
939 first time, we need to know how the frontend handled things like character
940 set conversion and UTF-8 BOM stripping, in order to make everything
941 consistent. This function needs to be called by each frontend that requires
942 non-default behavior, to inform the diagnostics infrastructure how input is
943 to be processed. The default behavior is to do no conversion and not to
944 strip a UTF-8 BOM.
945
946 The callback should return the input charset to be used to convert the given
947 file's contents to UTF-8, or it should return NULL if no conversion is needed
948 for this file. SHOULD_SKIP_BOM only applies in case no conversion was
949 performed, and if true, it will cause a UTF-8 BOM to be skipped at the
950 beginning of the file. (In case a conversion was performed, the BOM is
951 rather skipped as part of the conversion process.) */
952
953 inline void
954 diagnostic_initialize_input_context (diagnostic_context *context,
955 diagnostic_input_charset_callback ccb,
956 bool should_skip_bom)
957 {
958 context->initialize_input_context (ccb, should_skip_bom);
959 }
960
961 /* Force diagnostics controlled by OPTIDX to be kind KIND. */
962 inline diagnostic_t
963 diagnostic_classify_diagnostic (diagnostic_context *context,
964 int optidx,
965 diagnostic_t kind,
966 location_t where)
967 {
968 return context->classify_diagnostic (optidx, kind, where);
969 }
970
971 inline void
972 diagnostic_push_diagnostics (diagnostic_context *context,
973 location_t where)
974 {
975 context->push_diagnostics (where);
976 }
977 inline void
978 diagnostic_pop_diagnostics (diagnostic_context *context,
979 location_t where)
980 {
981 context->pop_diagnostics (where);
982 }
983
984 /* Report a diagnostic message (an error or a warning) as specified by
985 DC. This function is *the* subroutine in terms of which front-ends
986 should implement their specific diagnostic handling modules. The
987 front-end independent format specifiers are exactly those described
988 in the documentation of output_format.
989 Return true if a diagnostic was printed, false otherwise. */
990
991 inline bool
992 diagnostic_report_diagnostic (diagnostic_context *context,
993 diagnostic_info *diagnostic)
994 {
995 return context->report_diagnostic (diagnostic);
996 }
997
998 #ifdef ATTRIBUTE_GCC_DIAG
999 extern void diagnostic_set_info (diagnostic_info *, const char *, va_list *,
1000 rich_location *, diagnostic_t) ATTRIBUTE_GCC_DIAG(2,0);
1001 extern void diagnostic_set_info_translated (diagnostic_info *, const char *,
1002 va_list *, rich_location *,
1003 diagnostic_t)
1004 ATTRIBUTE_GCC_DIAG(2,0);
1005 extern void diagnostic_append_note (diagnostic_context *, location_t,
1006 const char *, ...) ATTRIBUTE_GCC_DIAG(3,4);
1007 #endif
1008 extern char *diagnostic_build_prefix (diagnostic_context *, const diagnostic_info *);
1009 void default_diagnostic_starter (diagnostic_context *, const diagnostic_info *);
1010 void default_diagnostic_start_span_fn (diagnostic_context *,
1011 expanded_location);
1012 void default_diagnostic_finalizer (diagnostic_context *,
1013 const diagnostic_info *,
1014 diagnostic_t);
1015 void diagnostic_set_caret_max_width (diagnostic_context *context, int value);
1016
1017 inline void
1018 diagnostic_action_after_output (diagnostic_context *context,
1019 diagnostic_t diag_kind)
1020 {
1021 context->action_after_output (diag_kind);
1022 }
1023
1024 inline void
1025 diagnostic_check_max_errors (diagnostic_context *context, bool flush = false)
1026 {
1027 context->check_max_errors (flush);
1028 }
1029
1030 int get_terminal_width (void);
1031
1032 /* Return the location associated to this diagnostic. Parameter WHICH
1033 specifies which location. By default, expand the first one. */
1034
1035 inline location_t
1036 diagnostic_location (const diagnostic_info * diagnostic, int which = 0)
1037 {
1038 return diagnostic->message.get_location (which);
1039 }
1040
1041 /* Return the number of locations to be printed in DIAGNOSTIC. */
1042
1043 inline unsigned int
1044 diagnostic_num_locations (const diagnostic_info * diagnostic)
1045 {
1046 return diagnostic->message.m_richloc->get_num_locations ();
1047 }
1048
1049 /* Expand the location of this diagnostic. Use this function for
1050 consistency. Parameter WHICH specifies which location. By default,
1051 expand the first one. */
1052
1053 inline expanded_location
1054 diagnostic_expand_location (const diagnostic_info * diagnostic, int which = 0)
1055 {
1056 return diagnostic->richloc->get_expanded_location (which);
1057 }
1058
1059 /* This is somehow the right-side margin of a caret line, that is, we
1060 print at least these many characters after the position pointed at
1061 by the caret. */
1062 const int CARET_LINE_MARGIN = 10;
1063
1064 /* Return true if the two locations can be represented within the same
1065 caret line. This is used to build a prefix and also to determine
1066 whether to print one or two caret lines. */
1067
1068 inline bool
1069 diagnostic_same_line (const diagnostic_context *context,
1070 expanded_location s1, expanded_location s2)
1071 {
1072 return (s2.column && s1.line == s2.line
1073 && (context->m_source_printing.max_width - CARET_LINE_MARGIN
1074 > abs (s1.column - s2.column)));
1075 }
1076
1077 extern const char *diagnostic_get_color_for_kind (diagnostic_t kind);
1078
1079 /* Pure text formatting support functions. */
1080 extern char *file_name_as_prefix (diagnostic_context *, const char *);
1081
1082 extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
1083
1084 extern void diagnostic_output_format_init (diagnostic_context *,
1085 const char *base_file_name,
1086 enum diagnostics_output_format,
1087 bool json_formatting);
1088 extern void diagnostic_output_format_init_json_stderr (diagnostic_context *context,
1089 bool formatted);
1090 extern void diagnostic_output_format_init_json_file (diagnostic_context *context,
1091 bool formatted,
1092 const char *base_file_name);
1093 extern void diagnostic_output_format_init_sarif_stderr (diagnostic_context *context,
1094 bool formatted);
1095 extern void diagnostic_output_format_init_sarif_file (diagnostic_context *context,
1096 bool formatted,
1097 const char *base_file_name);
1098 extern void diagnostic_output_format_init_sarif_stream (diagnostic_context *context,
1099 bool formatted,
1100 FILE *stream);
1101
1102 /* Compute the number of digits in the decimal representation of an integer. */
1103 extern int num_digits (int);
1104
1105 extern json::value *json_from_expanded_location (diagnostic_context *context,
1106 location_t loc);
1107
1108 inline bool
1109 warning_enabled_at (location_t loc, int opt)
1110 {
1111 return global_dc->warning_enabled_at (loc, opt);
1112 }
1113
1114 inline bool
1115 option_unspecified_p (int opt)
1116 {
1117 return global_dc->option_unspecified_p (opt);
1118 }
1119
1120 extern char *get_cwe_url (int cwe);
1121
1122 #endif /* ! GCC_DIAGNOSTIC_H */