Debian Codegraph @

gcc/diagnostic.h

raw
1 /* Various declarations for language-independent diagnostics subroutines.
2 Copyright (C) 2000-2025 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <[email protected]>
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 "unique-argv.h"
25 #include "rich-location.h"
26 #include "pretty-print.h"
27 #include "diagnostic-core.h"
28
29 namespace text_art
30 {
31 class theme;
32 } // namespace text_art
33
34 /* An enum for controlling what units to use for the column number
35 when diagnostics are output, used by the -fdiagnostics-column-unit option.
36 Tabs will be expanded or not according to the value of -ftabstop. The origin
37 (default 1) is controlled by -fdiagnostics-column-origin. */
38
39 enum diagnostics_column_unit
40 {
41 /* The default from GCC 11 onwards: display columns. */
42 DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
43
44 /* The behavior in GCC 10 and earlier: simple bytes. */
45 DIAGNOSTICS_COLUMN_UNIT_BYTE
46 };
47
48 /* An enum for controlling how to print non-ASCII characters/bytes when
49 a diagnostic suggests escaping the source code on output. */
50
51 enum diagnostics_escape_format
52 {
53 /* Escape non-ASCII Unicode characters in the form <U+XXXX> and
54 non-UTF-8 bytes in the form <XX>. */
55 DIAGNOSTICS_ESCAPE_FORMAT_UNICODE,
56
57 /* Escape non-ASCII bytes in the form <XX> (thus showing the underlying
58 encoding of non-ASCII Unicode characters). */
59 DIAGNOSTICS_ESCAPE_FORMAT_BYTES
60 };
61
62 /* Enum for overriding the standard output format. */
63
64 enum diagnostics_output_format
65 {
66 /* The default: textual output. */
67 DIAGNOSTICS_OUTPUT_FORMAT_TEXT,
68
69 /* JSON-based output, to stderr. */
70 DIAGNOSTICS_OUTPUT_FORMAT_JSON_STDERR,
71
72 /* JSON-based output, to a file. */
73 DIAGNOSTICS_OUTPUT_FORMAT_JSON_FILE,
74
75 /* SARIF-based output, to stderr. */
76 DIAGNOSTICS_OUTPUT_FORMAT_SARIF_STDERR,
77
78 /* SARIF-based output, to a file. */
79 DIAGNOSTICS_OUTPUT_FORMAT_SARIF_FILE
80 };
81
82 /* An enum for controlling how diagnostic_paths should be printed. */
83 enum diagnostic_path_format
84 {
85 /* Don't print diagnostic_paths. */
86 DPF_NONE,
87
88 /* Print diagnostic_paths by emitting a separate "note" for every event
89 in the path. */
90 DPF_SEPARATE_EVENTS,
91
92 /* Print diagnostic_paths by consolidating events together where they
93 are close enough, and printing such runs of events with multiple
94 calls to diagnostic_show_locus, showing the individual events in
95 each run via labels in the source. */
96 DPF_INLINE_EVENTS
97 };
98
99 /* An enum for capturing values of GCC_EXTRA_DIAGNOSTIC_OUTPUT,
100 and for -fdiagnostics-parseable-fixits. */
101
102 enum diagnostics_extra_output_kind
103 {
104 /* No extra output, or an unrecognized value. */
105 EXTRA_DIAGNOSTIC_OUTPUT_none,
106
107 /* Emit fix-it hints using the "fixits-v1" format, equivalent to
108 -fdiagnostics-parseable-fixits. */
109 EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1,
110
111 /* Emit fix-it hints using the "fixits-v2" format. */
112 EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2
113 };
114
115 /* Values for -fdiagnostics-text-art-charset=. */
116
117 enum diagnostic_text_art_charset
118 {
119 /* No text art diagrams shall be emitted. */
120 DIAGNOSTICS_TEXT_ART_CHARSET_NONE,
121
122 /* Use pure ASCII for text art diagrams. */
123 DIAGNOSTICS_TEXT_ART_CHARSET_ASCII,
124
125 /* Use ASCII + conservative use of other unicode characters
126 in text art diagrams. */
127 DIAGNOSTICS_TEXT_ART_CHARSET_UNICODE,
128
129 /* Use Emoji. */
130 DIAGNOSTICS_TEXT_ART_CHARSET_EMOJI
131 };
132
133 /* A diagnostic is described by the MESSAGE to send, the FILE and LINE of
134 its context and its KIND (ice, error, warning, note, ...) See complete
135 list in diagnostic.def. */
136 struct diagnostic_info
137 {
138 diagnostic_info ()
139 : message (), richloc (), metadata (), x_data (), kind (), option_id (),
140 m_iinfo ()
141 { }
142
143 /* Text to be formatted. */
144 text_info message;
145
146 /* The location at which the diagnostic is to be reported. */
147 rich_location *richloc;
148
149 /* An optional bundle of metadata associated with the diagnostic
150 (or NULL). */
151 const diagnostic_metadata *metadata;
152
153 /* Auxiliary data for client. */
154 void *x_data;
155 /* The kind of diagnostic it is about. */
156 diagnostic_t kind;
157 /* Which OPT_* directly controls this diagnostic. */
158 diagnostic_option_id option_id;
159
160 /* Inlining context containing locations for each call site along
161 the inlining stack. */
162 struct inlining_info
163 {
164 /* Locations along the inlining stack. */
165 auto_vec<location_t, 8> m_ilocs;
166 /* The abstract origin of the location. */
167 void *m_ao;
168 /* Set if every M_ILOCS element is in a system header. */
169 bool m_allsyslocs;
170 } m_iinfo;
171 };
172
173 /* Forward declarations. */
174 class diagnostic_location_print_policy;
175 class diagnostic_source_print_policy;
176
177 typedef void (*diagnostic_text_starter_fn) (diagnostic_text_output_format &,
178 const diagnostic_info *);
179
180 typedef void
181 (*diagnostic_start_span_fn) (const diagnostic_location_print_policy &,
182 pretty_printer *,
183 expanded_location);
184
185 typedef void (*diagnostic_text_finalizer_fn) (diagnostic_text_output_format &,
186 const diagnostic_info *,
187 diagnostic_t);
188
189 /* Abstract base class for the diagnostic subsystem to make queries
190 about command-line options. */
191
192 class diagnostic_option_manager
193 {
194 public:
195 virtual ~diagnostic_option_manager () {}
196
197 /* Return 1 if option OPTION_ID is enabled, 0 if it is disabled,
198 or -1 if it isn't a simple on-off switch
199 (or if the value is unknown, typically set later in target). */
200 virtual int option_enabled_p (diagnostic_option_id option_id) const = 0;
201
202 /* Return malloced memory for the name of the option OPTION_ID
203 which enabled a diagnostic, originally of type ORIG_DIAG_KIND but
204 possibly converted to DIAG_KIND by options such as -Werror.
205 May return NULL if no name is to be printed.
206 May be passed 0 as well as the index of a particular option. */
207 virtual char *make_option_name (diagnostic_option_id option_id,
208 diagnostic_t orig_diag_kind,
209 diagnostic_t diag_kind) const = 0;
210
211 /* Return malloced memory for a URL describing the option that controls
212 a diagnostic.
213 May return NULL if no URL is available.
214 May be passed 0 as well as the index of a particular option. */
215 virtual char *make_option_url (diagnostic_option_id option_id) const = 0;
216 };
217
218 class edit_context;
219 class diagnostic_client_data_hooks;
220 class logical_location;
221 class diagnostic_diagram;
222 class diagnostic_source_effect_info;
223 class diagnostic_output_format;
224 class diagnostic_text_output_format;
225 class diagnostic_buffer;
226
227 /* A stack of sets of classifications: each entry in the stack is
228 a mapping from option index to diagnostic severity that can be changed
229 via pragmas. The stack can be pushed and popped. */
230
231 class diagnostic_option_classifier
232 {
233 public:
234 void init (int n_opts);
235 void fini ();
236
237 /* Save all diagnostic classifications in a stack. */
238 void push ();
239
240 /* Restore the topmost classification set off the stack. If the stack
241 is empty, revert to the state based on command line parameters. */
242 void pop (location_t where);
243
244 bool option_unspecified_p (diagnostic_option_id option_id) const
245 {
246 return get_current_override (option_id) == DK_UNSPECIFIED;
247 }
248
249 diagnostic_t get_current_override (diagnostic_option_id option_id) const
250 {
251 gcc_assert (option_id.m_idx < m_n_opts);
252 return m_classify_diagnostic[option_id.m_idx];
253 }
254
255 diagnostic_t
256 classify_diagnostic (const diagnostic_context *context,
257 diagnostic_option_id option_id,
258 diagnostic_t new_kind,
259 location_t where);
260
261 diagnostic_t
262 update_effective_level_from_pragmas (diagnostic_info *diagnostic) const;
263
264 int pch_save (FILE *);
265 int pch_restore (FILE *);
266
267 private:
268 /* Each time a diagnostic's classification is changed with a pragma,
269 we record the change and the location of the change in an array of
270 these structs. */
271 struct diagnostic_classification_change_t
272 {
273 location_t location;
274
275 /* For DK_POP, this is the index of the corresponding push (as stored
276 in m_push_list).
277 Otherwise, this is an option index. */
278 int option;
279
280 diagnostic_t kind;
281 };
282
283 int m_n_opts;
284
285 /* For each option index that can be passed to warning() et al
286 (OPT_* from options.h when using this code with the core GCC
287 options), this array may contain a new kind that the diagnostic
288 should be changed to before reporting, or DK_UNSPECIFIED to leave
289 it as the reported kind, or DK_IGNORED to not report it at
290 all. */
291 diagnostic_t *m_classify_diagnostic;
292
293 /* History of all changes to the classifications above. This list
294 is stored in location-order, so we can search it, either
295 binary-wise or end-to-front, to find the most recent
296 classification for a given diagnostic, given the location of the
297 diagnostic. */
298 vec<diagnostic_classification_change_t> m_classification_history;
299
300 /* For pragma push/pop. */
301 vec<int> m_push_list;
302 };
303
304 /* A bundle of options relating to printing the user's source code
305 (potentially with a margin, underlining, labels, etc). */
306
307 struct diagnostic_source_printing_options
308 {
309 /* True if we should print the source line with a caret indicating
310 the location.
311 Corresponds to -fdiagnostics-show-caret. */
312 bool enabled;
313
314 /* Maximum width of the source line printed. */
315 int max_width;
316
317 /* Character used at the caret when printing source locations. */
318 char caret_chars[rich_location::STATICALLY_ALLOCATED_RANGES];
319
320 /* When printing source code, should the characters at carets and ranges
321 be colorized? (assuming colorization is on at all).
322 This should be true for frontends that generate range information
323 (so that the ranges of code are colorized),
324 and false for frontends that merely specify points within the
325 source code (to avoid e.g. colorizing just the first character in
326 a token, which would look strange). */
327 bool colorize_source_p;
328
329 /* When printing source code, should labelled ranges be printed?
330 Corresponds to -fdiagnostics-show-labels. */
331 bool show_labels_p;
332
333 /* When printing source code, should there be a left-hand margin
334 showing line numbers?
335 Corresponds to -fdiagnostics-show-line-numbers. */
336 bool show_line_numbers_p;
337
338 /* If printing source code, what should the minimum width of the margin
339 be? Line numbers will be right-aligned, and padded to this width.
340 Corresponds to -fdiagnostics-minimum-margin-width=VALUE. */
341 int min_margin_width;
342
343 /* Usable by plugins; if true, print a debugging ruler above the
344 source output. */
345 bool show_ruler_p;
346
347 /* When printing events in an inline path, should we print lines
348 visualizing links between related events (e.g. for CFG paths)?
349 Corresponds to -fdiagnostics-show-event-links. */
350 bool show_event_links_p;
351 };
352
353 /* A bundle of state for determining column numbers in diagnostics
354 (tab stops, whether to start at 0 or 1, etc).
355 Uses a file_cache to handle tabs. */
356
357 class diagnostic_column_policy
358 {
359 public:
360 diagnostic_column_policy (const diagnostic_context &dc);
361
362 int converted_column (expanded_location s) const;
363
364 label_text get_location_text (const expanded_location &s,
365 bool show_column,
366 bool colorize) const;
367
368 int get_tabstop () const { return m_tabstop; }
369
370 private:
371 file_cache &m_file_cache;
372 enum diagnostics_column_unit m_column_unit;
373 int m_column_origin;
374 int m_tabstop;
375 };
376
377 /* A bundle of state for printing locations within diagnostics
378 (e.g. "FILENAME:LINE:COLUMN"), to isolate the interactions between
379 diagnostic_context and the start_span callbacks. */
380
381 class diagnostic_location_print_policy
382 {
383 public:
384 diagnostic_location_print_policy (const diagnostic_context &dc);
385 diagnostic_location_print_policy (const diagnostic_text_output_format &);
386
387 bool show_column_p () const { return m_show_column; }
388
389 const diagnostic_column_policy &
390 get_column_policy () const { return m_column_policy; }
391
392 private:
393 diagnostic_column_policy m_column_policy;
394 bool m_show_column;
395 };
396
397 /* A bundle of state for printing source within a diagnostic,
398 to isolate the interactions between diagnostic_context and the
399 implementation of diagnostic_show_locus. */
400
401 class diagnostic_source_print_policy
402 {
403 public:
404 diagnostic_source_print_policy (const diagnostic_context &);
405 diagnostic_source_print_policy (const diagnostic_context &,
406 const diagnostic_source_printing_options &);
407
408 void
409 print (pretty_printer &pp,
410 const rich_location &richloc,
411 diagnostic_t diagnostic_kind,
412 diagnostic_source_effect_info *effect_info) const;
413
414 const diagnostic_source_printing_options &
415 get_options () const { return m_options; }
416
417 diagnostic_start_span_fn
418 get_start_span_fn () const { return m_start_span_cb; }
419
420 file_cache &
421 get_file_cache () const { return m_file_cache; }
422
423 enum diagnostics_escape_format
424 get_escape_format () const
425 {
426 return m_escape_format;
427 }
428
429 text_art::theme *
430 get_diagram_theme () const { return m_diagram_theme; }
431
432 const diagnostic_column_policy &get_column_policy () const
433 {
434 return m_location_policy.get_column_policy ();
435 }
436
437 const diagnostic_location_print_policy &get_location_policy () const
438 {
439 return m_location_policy;
440 }
441
442 private:
443 const diagnostic_source_printing_options &m_options;
444 class diagnostic_location_print_policy m_location_policy;
445 diagnostic_start_span_fn m_start_span_cb;
446 file_cache &m_file_cache;
447
448 /* Other data copied from diagnostic_context. */
449 text_art::theme *m_diagram_theme;
450 enum diagnostics_escape_format m_escape_format;
451 };
452
453 /* A collection of counters of diagnostics, per-kind
454 (e.g. "3 errors and 1 warning"), for use by both diagnostic_context
455 and by diagnostic_buffer. */
456
457 struct diagnostic_counters
458 {
459 diagnostic_counters ();
460
461 void dump (FILE *out, int indent) const;
462 void DEBUG_FUNCTION dump () const { dump (stderr, 0); }
463
464 int get_count (diagnostic_t kind) const { return m_count_for_kind[kind]; }
465
466 void move_to (diagnostic_counters &dest);
467 void clear ();
468
469 int m_count_for_kind[DK_LAST_DIAGNOSTIC_KIND];
470 };
471
472 /* This class encapsulates the state of the diagnostics subsystem
473 as a whole (either directly, or via owned objects of other classes, to
474 avoid global variables).
475
476 It has responsibility for:
477 - being a central place for clients to report diagnostics
478 - reporting those diagnostics to zero or more output sinks
479 (e.g. text vs SARIF)
480 - providing a "dump" member function for a debug dump of the state of
481 the diagnostics subsytem
482 - direct vs buffered diagnostics (see class diagnostic_buffer)
483 - tracking the original argv of the program (for SARIF output)
484 - crash-handling
485
486 It delegates responsibility to various other classes:
487 - the various output sinks (instances of diagnostic_output_format
488 subclasses)
489 - formatting of messages (class pretty_printer)
490 - an optional urlifier to inject URLs into formatted messages
491 - counting the number of diagnostics reported of each kind
492 (class diagnostic_counters)
493 - calling out to a diagnostic_option_manager to determine if
494 a particular warning is enabled or disabled
495 - tracking pragmas that enable/disable warnings in a range of
496 source code
497 - a cache for use when quoting the user's source code (class file_cache)
498 - a text_art::theme
499 - an edit_context for generating patches from fix-it hints
500 - diagnostic_client_data_hooks for metadata.
501
502 Try to avoid adding new responsibilities to this class itself, to avoid
503 the "blob" anti-pattern. */
504
505 class diagnostic_context
506 {
507 public:
508 /* Give access to m_text_callbacks. */
509 friend diagnostic_text_starter_fn &
510 diagnostic_text_starter (diagnostic_context *context);
511 friend diagnostic_start_span_fn &
512 diagnostic_start_span (diagnostic_context *context);
513 friend diagnostic_text_finalizer_fn &
514 diagnostic_text_finalizer (diagnostic_context *context);
515
516 friend class diagnostic_source_print_policy;
517 friend class diagnostic_text_output_format;
518 friend class diagnostic_buffer;
519
520 typedef void (*set_locations_callback_t) (diagnostic_context *,
521 diagnostic_info *);
522
523 void initialize (int n_opts);
524 void color_init (int value);
525 void urls_init (int value);
526 void set_pretty_printer (std::unique_ptr<pretty_printer> pp);
527 void refresh_output_sinks ();
528
529 void finish ();
530
531 void dump (FILE *out) const;
532 void DEBUG_FUNCTION dump () const { dump (stderr); }
533
534 bool execution_failed_p () const;
535
536 void set_original_argv (unique_argv original_argv);
537 const char * const *get_original_argv ()
538 {
539 return const_cast<const char * const *> (m_original_argv);
540 }
541
542 void set_set_locations_callback (set_locations_callback_t cb)
543 {
544 m_set_locations_cb = cb;
545 }
546
547 void
548 initialize_input_context (diagnostic_input_charset_callback ccb,
549 bool should_skip_bom);
550
551 void begin_group ();
552 void end_group ();
553
554 void push_nesting_level ();
555 void pop_nesting_level ();
556
557 bool warning_enabled_at (location_t loc, diagnostic_option_id option_id);
558
559 bool option_unspecified_p (diagnostic_option_id option_id) const
560 {
561 return m_option_classifier.option_unspecified_p (option_id);
562 }
563
564 bool emit_diagnostic_with_group (diagnostic_t kind,
565 rich_location &richloc,
566 const diagnostic_metadata *metadata,
567 diagnostic_option_id option_id,
568 const char *gmsgid, ...)
569 ATTRIBUTE_GCC_DIAG(6,7);
570 bool emit_diagnostic_with_group_va (diagnostic_t kind,
571 rich_location &richloc,
572 const diagnostic_metadata *metadata,
573 diagnostic_option_id option_id,
574 const char *gmsgid, va_list *ap)
575 ATTRIBUTE_GCC_DIAG(6,0);
576
577 bool report_diagnostic (diagnostic_info *);
578 void report_verbatim (text_info &);
579
580 diagnostic_t
581 classify_diagnostic (diagnostic_option_id option_id,
582 diagnostic_t new_kind,
583 location_t where)
584 {
585 return m_option_classifier.classify_diagnostic (this,
586 option_id,
587 new_kind,
588 where);
589 }
590
591 void push_diagnostics (location_t where ATTRIBUTE_UNUSED)
592 {
593 m_option_classifier.push ();
594 }
595 void pop_diagnostics (location_t where)
596 {
597 m_option_classifier.pop (where);
598 }
599
600 void maybe_show_locus (const rich_location &richloc,
601 const diagnostic_source_printing_options &opts,
602 diagnostic_t diagnostic_kind,
603 pretty_printer &pp,
604 diagnostic_source_effect_info *effect_info);
605
606 void emit_diagram (const diagnostic_diagram &diagram);
607
608 /* Various setters for use by option-handling logic. */
609 void set_output_format (std::unique_ptr<diagnostic_output_format> output_format);
610 void set_text_art_charset (enum diagnostic_text_art_charset charset);
611 void set_client_data_hooks (std::unique_ptr<diagnostic_client_data_hooks> hooks);
612
613 void push_owned_urlifier (std::unique_ptr<urlifier>);
614 void push_borrowed_urlifier (const urlifier &);
615 void pop_urlifier ();
616
617 void create_edit_context ();
618 void set_warning_as_error_requested (bool val)
619 {
620 m_warning_as_error_requested = val;
621 }
622 void set_report_bug (bool val) { m_report_bug = val; }
623 void set_extra_output_kind (enum diagnostics_extra_output_kind kind)
624 {
625 m_extra_output_kind = kind;
626 }
627 void set_show_cwe (bool val) { m_show_cwe = val; }
628 void set_show_rules (bool val) { m_show_rules = val; }
629 void set_show_highlight_colors (bool val);
630 void set_path_format (enum diagnostic_path_format val)
631 {
632 m_path_format = val;
633 }
634 void set_show_path_depths (bool val) { m_show_path_depths = val; }
635 void set_show_option_requested (bool val) { m_show_option_requested = val; }
636 void set_max_errors (int val) { m_max_errors = val; }
637 void set_escape_format (enum diagnostics_escape_format val)
638 {
639 m_escape_format = val;
640 }
641
642 void set_format_decoder (printer_fn format_decoder);
643 void set_prefixing_rule (diagnostic_prefixing_rule_t rule);
644
645 /* Various accessors. */
646 bool warning_as_error_requested_p () const
647 {
648 return m_warning_as_error_requested;
649 }
650 bool show_path_depths_p () const { return m_show_path_depths; }
651 diagnostic_output_format &get_output_format (size_t idx) const;
652 enum diagnostic_path_format get_path_format () const { return m_path_format; }
653 enum diagnostics_escape_format get_escape_format () const
654 {
655 return m_escape_format;
656 }
657
658 file_cache &
659 get_file_cache () const
660 {
661 gcc_assert (m_file_cache);
662 return *m_file_cache;
663 }
664
665 edit_context *get_edit_context () const
666 {
667 return m_edit_context_ptr;
668 }
669 const diagnostic_client_data_hooks *get_client_data_hooks () const
670 {
671 return m_client_data_hooks;
672 }
673
674 const urlifier *get_urlifier () const;
675
676 text_art::theme *get_diagram_theme () const { return m_diagrams.m_theme; }
677
678 int &diagnostic_count (diagnostic_t kind)
679 {
680 return m_diagnostic_counters.m_count_for_kind[kind];
681 }
682 int diagnostic_count (diagnostic_t kind) const
683 {
684 return m_diagnostic_counters.get_count (kind);
685 }
686
687 /* Option-related member functions. */
688 inline bool option_enabled_p (diagnostic_option_id option_id) const
689 {
690 if (!m_option_mgr)
691 return true;
692 return m_option_mgr->option_enabled_p (option_id);
693 }
694
695 inline char *make_option_name (diagnostic_option_id option_id,
696 diagnostic_t orig_diag_kind,
697 diagnostic_t diag_kind) const
698 {
699 if (!m_option_mgr)
700 return nullptr;
701 return m_option_mgr->make_option_name (option_id,
702 orig_diag_kind,
703 diag_kind);
704 }
705
706 inline char *make_option_url (diagnostic_option_id option_id) const
707 {
708 if (!m_option_mgr)
709 return nullptr;
710 return m_option_mgr->make_option_url (option_id);
711 }
712
713 void
714 set_option_manager (std::unique_ptr<diagnostic_option_manager> mgr,
715 unsigned lang_mask);
716
717 unsigned get_lang_mask () const
718 {
719 return m_lang_mask;
720 }
721
722 bool diagnostic_impl (rich_location *, const diagnostic_metadata *,
723 diagnostic_option_id, const char *,
724 va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0);
725 bool diagnostic_n_impl (rich_location *, const diagnostic_metadata *,
726 diagnostic_option_id, unsigned HOST_WIDE_INT,
727 const char *, const char *, va_list *,
728 diagnostic_t) ATTRIBUTE_GCC_DIAG(7,0);
729
730 int get_diagnostic_nesting_level () const
731 {
732 return m_diagnostic_groups.m_diagnostic_nesting_level;
733 }
734
735 char *build_indent_prefix () const;
736
737 int
738 pch_save (FILE *f)
739 {
740 return m_option_classifier.pch_save (f);
741 }
742
743 int
744 pch_restore (FILE *f)
745 {
746 return m_option_classifier.pch_restore (f);
747 }
748
749
750 void set_diagnostic_buffer (diagnostic_buffer *);
751 diagnostic_buffer *get_diagnostic_buffer () const
752 {
753 return m_diagnostic_buffer;
754 }
755 void clear_diagnostic_buffer (diagnostic_buffer &);
756 void flush_diagnostic_buffer (diagnostic_buffer &);
757
758 std::unique_ptr<pretty_printer> clone_printer () const
759 {
760 return m_reference_printer->clone ();
761 }
762
763 pretty_printer *get_reference_printer () const
764 {
765 return m_reference_printer;
766 }
767
768 void
769 add_sink (std::unique_ptr<diagnostic_output_format>);
770
771 void remove_all_output_sinks ();
772
773 bool supports_fnotice_on_stderr_p () const;
774
775 private:
776 void error_recursion () ATTRIBUTE_NORETURN;
777
778 bool diagnostic_enabled (diagnostic_info *diagnostic);
779
780 void get_any_inlining_info (diagnostic_info *diagnostic);
781
782 void check_max_errors (bool flush);
783 void action_after_output (diagnostic_t diag_kind);
784
785 /* Data members.
786 Ideally, all of these would be private. */
787
788 private:
789 /* A reference instance of pretty_printer created by the client
790 and owned by the context. Used for cloning when creating/adding
791 output formats.
792 Owned by the context; this would be a std::unique_ptr if
793 diagnostic_context had a proper ctor. */
794 pretty_printer *m_reference_printer;
795
796 /* Cache of source code.
797 Owned by the context; this would be a std::unique_ptr if
798 diagnostic_context had a proper ctor. */
799 file_cache *m_file_cache;
800
801 /* The number of times we have issued diagnostics. */
802 diagnostic_counters m_diagnostic_counters;
803
804 /* True if it has been requested that warnings be treated as errors. */
805 bool m_warning_as_error_requested;
806
807 /* The number of option indexes that can be passed to warning() et
808 al. */
809 int m_n_opts;
810
811 /* The stack of sets of overridden diagnostic option severities. */
812 diagnostic_option_classifier m_option_classifier;
813
814 /* True if we should print any CWE identifiers associated with
815 diagnostics. */
816 bool m_show_cwe;
817
818 /* True if we should print any rules associated with diagnostics. */
819 bool m_show_rules;
820
821 /* How should diagnostic_path objects be printed. */
822 enum diagnostic_path_format m_path_format;
823
824 /* True if we should print stack depths when printing diagnostic paths. */
825 bool m_show_path_depths;
826
827 /* True if we should print the command line option which controls
828 each diagnostic, if known. */
829 bool m_show_option_requested;
830
831 public:
832 /* True if we should raise a SIGABRT on errors. */
833 bool m_abort_on_error;
834
835 /* True if we should show the column number on diagnostics. */
836 bool m_show_column;
837
838 /* True if pedwarns are errors. */
839 bool m_pedantic_errors;
840
841 /* True if permerrors are warnings. */
842 bool m_permissive;
843
844 /* The index of the option to associate with turning permerrors into
845 warnings. */
846 int m_opt_permissive;
847
848 /* True if errors are fatal. */
849 bool m_fatal_errors;
850
851 /* True if all warnings should be disabled. */
852 bool m_inhibit_warnings;
853
854 /* True if warnings should be given in system headers. */
855 bool m_warn_system_headers;
856
857 private:
858 /* Maximum number of errors to report. */
859 int m_max_errors;
860
861 /* Client-supplied callbacks for use in text output. */
862 struct {
863 /* This function is called before any message is printed out. It is
864 responsible for preparing message prefix and such. For example, it
865 might say:
866 In file included from "/usr/local/include/curses.h:5:
867 from "/home/gdr/src/nifty_printer.h:56:
868 ...
869 */
870 diagnostic_text_starter_fn m_begin_diagnostic;
871
872 /* This function is called by diagnostic_show_locus in between
873 disjoint spans of source code, so that the context can print
874 something to indicate that a new span of source code has begun. */
875 diagnostic_start_span_fn m_start_span;
876
877 /* This function is called after the diagnostic message is printed. */
878 diagnostic_text_finalizer_fn m_end_diagnostic;
879 } m_text_callbacks;
880
881 public:
882 /* Client hook to report an internal error. */
883 void (*m_internal_error) (diagnostic_context *, const char *, va_list *);
884
885 /* Client hook to adjust properties of the given diagnostic that we're
886 about to issue, such as its kind. */
887 void (*m_adjust_diagnostic_info)(diagnostic_context *, diagnostic_info *);
888
889 private:
890 /* Owned by the context; this would be a std::unique_ptr if
891 diagnostic_context had a proper ctor. */
892 diagnostic_option_manager *m_option_mgr;
893 unsigned m_lang_mask;
894
895 /* A stack of optional hooks for adding URLs to quoted text strings in
896 diagnostics. Only used for the main diagnostic message.
897 Typically a single one owner by the context, but can be temporarily
898 overridden by a borrowed urlifier (e.g. on-stack). */
899 struct urlifier_stack_node
900 {
901 urlifier *m_urlifier;
902 bool m_owned;
903 };
904 auto_vec<urlifier_stack_node> *m_urlifier_stack;
905
906 public:
907 /* Auxiliary data for client. */
908 void *m_client_aux_data;
909
910 /* Used to detect that the last caret was printed at the same location. */
911 location_t m_last_location;
912
913 private:
914 int m_lock;
915
916 public:
917 bool m_inhibit_notes_p;
918
919 diagnostic_source_printing_options m_source_printing;
920
921 private:
922 /* True if -freport-bug option is used. */
923 bool m_report_bug;
924
925 /* Used to specify additional diagnostic output to be emitted after the
926 rest of the diagnostic. This is for implementing
927 -fdiagnostics-parseable-fixits and GCC_EXTRA_DIAGNOSTIC_OUTPUT. */
928 enum diagnostics_extra_output_kind m_extra_output_kind;
929
930 public:
931 /* What units to use when outputting the column number. */
932 enum diagnostics_column_unit m_column_unit;
933
934 /* The origin for the column number (1-based or 0-based typically). */
935 int m_column_origin;
936
937 /* The size of the tabstop for tab expansion. */
938 int m_tabstop;
939
940 private:
941 /* How should non-ASCII/non-printable bytes be escaped when
942 a diagnostic suggests escaping the source code on output. */
943 enum diagnostics_escape_format m_escape_format;
944
945 /* If non-NULL, an edit_context to which fix-it hints should be
946 applied, for generating patches.
947 Owned by the context; this would be a std::unique_ptr if
948 diagnostic_context had a proper ctor. */
949 edit_context *m_edit_context_ptr;
950
951 /* Fields relating to diagnostic groups. */
952 struct {
953 /* How many diagnostic_group instances are currently alive. */
954 int m_group_nesting_depth;
955
956 /* How many nesting levels have been pushed within this group. */
957 int m_diagnostic_nesting_level;
958
959 /* How many diagnostics have been emitted since the bottommost
960 diagnostic_group was pushed. */
961 int m_emission_count;
962 } m_diagnostic_groups;
963
964 /* The various sinks to which diagnostics are to be outputted
965 (text vs structured formats such as SARIF).
966 The sinks are owned by the context; this would be a
967 std::vector<std::unique_ptr> if diagnostic_context had a
968 proper ctor. */
969 auto_vec<diagnostic_output_format *> m_output_sinks;
970
971 /* Callback to set the locations of call sites along the inlining
972 stack corresponding to a diagnostic location. Needed to traverse
973 the BLOCK_SUPERCONTEXT() chain hanging off the LOCATION_BLOCK()
974 of a diagnostic's location. */
975 set_locations_callback_t m_set_locations_cb;
976
977 /* A bundle of hooks for providing data to the context about its client
978 e.g. version information, plugins, etc.
979 Used by SARIF output to give metadata about the client that's
980 producing diagnostics.
981 Owned by the context; this would be a std::unique_ptr if
982 diagnostic_context had a proper ctor. */
983 diagnostic_client_data_hooks *m_client_data_hooks;
984
985 /* Support for diagrams. */
986 struct
987 {
988 /* Theme to use when generating diagrams.
989 Can be NULL (if text art is disabled).
990 Owned by the context; this would be a std::unique_ptr if
991 diagnostic_context had a proper ctor. */
992 text_art::theme *m_theme;
993
994 } m_diagrams;
995
996 /* Owned by the context. */
997 char **m_original_argv;
998
999 /* Borrowed pointer to the active diagnostic_buffer, if any.
1000 If null (the default), then diagnostics that are reported to the
1001 context are immediately issued to the output format.
1002 If non-null, then diagnostics that are reported to the context
1003 are buffered in the buffer, and may be issued to the output format
1004 later (if the buffer is flushed), moved to other buffers, or
1005 discarded (if the buffer is cleared). */
1006 diagnostic_buffer *m_diagnostic_buffer;
1007 };
1008
1009 inline void
1010 diagnostic_inhibit_notes (diagnostic_context * context)
1011 {
1012 context->m_inhibit_notes_p = true;
1013 }
1014
1015
1016 /* Client supplied function to announce a diagnostic
1017 (for text-based diagnostic output). */
1018 inline diagnostic_text_starter_fn &
1019 diagnostic_text_starter (diagnostic_context *context)
1020 {
1021 return context->m_text_callbacks.m_begin_diagnostic;
1022 }
1023
1024 /* Client supplied function called between disjoint spans of source code,
1025 so that the context can print
1026 something to indicate that a new span of source code has begun. */
1027 inline diagnostic_start_span_fn &
1028 diagnostic_start_span (diagnostic_context *context)
1029 {
1030 return context->m_text_callbacks.m_start_span;
1031 }
1032
1033 /* Client supplied function called after a diagnostic message is
1034 displayed (for text-based diagnostic output). */
1035 inline diagnostic_text_finalizer_fn &
1036 diagnostic_text_finalizer (diagnostic_context *context)
1037 {
1038 return context->m_text_callbacks.m_end_diagnostic;
1039 }
1040
1041 /* Extension hooks for client. */
1042 #define diagnostic_context_auxiliary_data(DC) (DC)->m_client_aux_data
1043 #define diagnostic_info_auxiliary_data(DI) (DI)->x_data
1044
1045 /* Raise SIGABRT on any diagnostic of severity DK_ERROR or higher. */
1046 inline void
1047 diagnostic_abort_on_error (diagnostic_context *context)
1048 {
1049 context->m_abort_on_error = true;
1050 }
1051
1052 /* This diagnostic_context is used by front-ends that directly output
1053 diagnostic messages without going through `error', `warning',
1054 and similar functions. */
1055 extern diagnostic_context *global_dc;
1056
1057 /* The number of errors that have been issued so far. Ideally, these
1058 would take a diagnostic_context as an argument. */
1059 #define errorcount global_dc->diagnostic_count (DK_ERROR)
1060 /* Similarly, but for warnings. */
1061 #define warningcount global_dc->diagnostic_count (DK_WARNING)
1062 /* Similarly, but for warnings promoted to errors. */
1063 #define werrorcount global_dc->diagnostic_count (DK_WERROR)
1064 /* Similarly, but for sorrys. */
1065 #define sorrycount global_dc->diagnostic_count (DK_SORRY)
1066
1067 /* Returns nonzero if warnings should be emitted. */
1068 #define diagnostic_report_warnings_p(DC, LOC) \
1069 (!(DC)->m_inhibit_warnings \
1070 && !(in_system_header_at (LOC) && !(DC)->m_warn_system_headers))
1071
1072 /* Override the option index to be used for reporting a
1073 diagnostic. */
1074
1075 inline void
1076 diagnostic_set_option_id (diagnostic_info *info,
1077 diagnostic_option_id option_id)
1078 {
1079 info->option_id = option_id;
1080 }
1081
1082 /* Diagnostic related functions. */
1083
1084 inline void
1085 diagnostic_initialize (diagnostic_context *context, int n_opts)
1086 {
1087 context->initialize (n_opts);
1088 }
1089
1090 inline void
1091 diagnostic_color_init (diagnostic_context *context, int value = -1)
1092 {
1093 context->color_init (value);
1094 }
1095
1096 inline void
1097 diagnostic_urls_init (diagnostic_context *context, int value = -1)
1098 {
1099 context->urls_init (value);
1100 }
1101
1102 inline void
1103 diagnostic_finish (diagnostic_context *context)
1104 {
1105 context->finish ();
1106 }
1107
1108 inline void
1109 diagnostic_show_locus (diagnostic_context *context,
1110 const diagnostic_source_printing_options &opts,
1111 rich_location *richloc,
1112 diagnostic_t diagnostic_kind,
1113 pretty_printer *pp,
1114 diagnostic_source_effect_info *effect_info = nullptr)
1115 {
1116 gcc_assert (context);
1117 gcc_assert (richloc);
1118 gcc_assert (pp);
1119 context->maybe_show_locus (*richloc, opts, diagnostic_kind, *pp, effect_info);
1120 }
1121
1122 /* Because we read source files a second time after the frontend did it the
1123 first time, we need to know how the frontend handled things like character
1124 set conversion and UTF-8 BOM stripping, in order to make everything
1125 consistent. This function needs to be called by each frontend that requires
1126 non-default behavior, to inform the diagnostics infrastructure how input is
1127 to be processed. The default behavior is to do no conversion and not to
1128 strip a UTF-8 BOM.
1129
1130 The callback should return the input charset to be used to convert the given
1131 file's contents to UTF-8, or it should return NULL if no conversion is needed
1132 for this file. SHOULD_SKIP_BOM only applies in case no conversion was
1133 performed, and if true, it will cause a UTF-8 BOM to be skipped at the
1134 beginning of the file. (In case a conversion was performed, the BOM is
1135 rather skipped as part of the conversion process.) */
1136
1137 inline void
1138 diagnostic_initialize_input_context (diagnostic_context *context,
1139 diagnostic_input_charset_callback ccb,
1140 bool should_skip_bom)
1141 {
1142 context->initialize_input_context (ccb, should_skip_bom);
1143 }
1144
1145 /* Force diagnostics controlled by OPTIDX to be kind KIND. */
1146 inline diagnostic_t
1147 diagnostic_classify_diagnostic (diagnostic_context *context,
1148 diagnostic_option_id option_id,
1149 diagnostic_t kind,
1150 location_t where)
1151 {
1152 return context->classify_diagnostic (option_id, kind, where);
1153 }
1154
1155 inline void
1156 diagnostic_push_diagnostics (diagnostic_context *context,
1157 location_t where)
1158 {
1159 context->push_diagnostics (where);
1160 }
1161 inline void
1162 diagnostic_pop_diagnostics (diagnostic_context *context,
1163 location_t where)
1164 {
1165 context->pop_diagnostics (where);
1166 }
1167
1168 /* Report a diagnostic message (an error or a warning) as specified by
1169 DC. This function is *the* subroutine in terms of which front-ends
1170 should implement their specific diagnostic handling modules. The
1171 front-end independent format specifiers are exactly those described
1172 in the documentation of output_format.
1173 Return true if a diagnostic was printed, false otherwise. */
1174
1175 inline bool
1176 diagnostic_report_diagnostic (diagnostic_context *context,
1177 diagnostic_info *diagnostic)
1178 {
1179 context->begin_group ();
1180 bool warned = context->report_diagnostic (diagnostic);
1181 context->end_group ();
1182 return warned;
1183 }
1184
1185 #ifdef ATTRIBUTE_GCC_DIAG
1186 extern void diagnostic_set_info (diagnostic_info *, const char *, va_list *,
1187 rich_location *, diagnostic_t) ATTRIBUTE_GCC_DIAG(2,0);
1188 extern void diagnostic_set_info_translated (diagnostic_info *, const char *,
1189 va_list *, rich_location *,
1190 diagnostic_t)
1191 ATTRIBUTE_GCC_DIAG(2,0);
1192 #endif
1193 void default_diagnostic_text_starter (diagnostic_text_output_format &,
1194 const diagnostic_info *);
1195 void default_diagnostic_start_span_fn (const diagnostic_location_print_policy &,
1196 pretty_printer *,
1197 expanded_location);
1198 void default_diagnostic_text_finalizer (diagnostic_text_output_format &,
1199 const diagnostic_info *,
1200 diagnostic_t);
1201 void diagnostic_set_caret_max_width (diagnostic_context *context, int value);
1202
1203 int get_terminal_width (void);
1204
1205 /* Return the location associated to this diagnostic. Parameter WHICH
1206 specifies which location. By default, expand the first one. */
1207
1208 inline location_t
1209 diagnostic_location (const diagnostic_info * diagnostic, int which = 0)
1210 {
1211 return diagnostic->message.get_location (which);
1212 }
1213
1214 /* Return the number of locations to be printed in DIAGNOSTIC. */
1215
1216 inline unsigned int
1217 diagnostic_num_locations (const diagnostic_info * diagnostic)
1218 {
1219 return diagnostic->message.m_richloc->get_num_locations ();
1220 }
1221
1222 /* Expand the location of this diagnostic. Use this function for
1223 consistency. Parameter WHICH specifies which location. By default,
1224 expand the first one. */
1225
1226 inline expanded_location
1227 diagnostic_expand_location (const diagnostic_info * diagnostic, int which = 0)
1228 {
1229 return diagnostic->richloc->get_expanded_location (which);
1230 }
1231
1232 /* This is somehow the right-side margin of a caret line, that is, we
1233 print at least these many characters after the position pointed at
1234 by the caret. */
1235 const int CARET_LINE_MARGIN = 10;
1236
1237 /* Return true if the two locations can be represented within the same
1238 caret line. This is used to build a prefix and also to determine
1239 whether to print one or two caret lines. */
1240
1241 inline bool
1242 diagnostic_same_line (const diagnostic_context *context,
1243 expanded_location s1, expanded_location s2)
1244 {
1245 return (s2.column && s1.line == s2.line
1246 && (context->m_source_printing.max_width - CARET_LINE_MARGIN
1247 > abs (s1.column - s2.column)));
1248 }
1249
1250 extern const char *diagnostic_get_color_for_kind (diagnostic_t kind);
1251
1252 /* Pure text formatting support functions. */
1253
1254 extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
1255
1256 /* Compute the number of digits in the decimal representation of an integer. */
1257 extern int num_digits (uint64_t);
1258
1259 inline bool
1260 warning_enabled_at (location_t loc, diagnostic_option_id option_id)
1261 {
1262 return global_dc->warning_enabled_at (loc, option_id);
1263 }
1264
1265 inline bool
1266 option_unspecified_p (diagnostic_option_id option_id)
1267 {
1268 return global_dc->option_unspecified_p (option_id);
1269 }
1270
1271 extern char *get_cwe_url (int cwe);
1272
1273 extern const char *get_diagnostic_kind_text (diagnostic_t kind);
1274
1275 const char *maybe_line_and_column (int line, int col);
1276
1277 #endif /* ! GCC_DIAGNOSTIC_H */