Branch data Line data Source code
1 : : /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 : : Copyright (C) 1999-2025 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 : :
22 : : /* This file implements the language independent aspect of diagnostic
23 : : message module. */
24 : :
25 : : #include "config.h"
26 : : #define INCLUDE_VECTOR
27 : : #include "system.h"
28 : : #include "coretypes.h"
29 : : #include "version.h"
30 : : #include "demangle.h"
31 : : #include "intl.h"
32 : : #include "backtrace.h"
33 : : #include "diagnostic.h"
34 : : #include "diagnostics/color.h"
35 : : #include "diagnostics/url.h"
36 : : #include "diagnostics/metadata.h"
37 : : #include "diagnostics/paths.h"
38 : : #include "diagnostics/client-data-hooks.h"
39 : : #include "diagnostics/diagram.h"
40 : : #include "diagnostics/sink.h"
41 : : #include "diagnostics/sarif-sink.h"
42 : : #include "diagnostics/text-sink.h"
43 : : #include "diagnostics/changes.h"
44 : : #include "selftest.h"
45 : : #include "diagnostics/selftest-context.h"
46 : : #include "opts.h"
47 : : #include "cpplib.h"
48 : : #include "text-art/theme.h"
49 : : #include "pretty-print-urlifier.h"
50 : : #include "diagnostics/logical-locations.h"
51 : : #include "diagnostics/buffering.h"
52 : : #include "diagnostics/file-cache.h"
53 : : #include "diagnostics/dumping.h"
54 : : #include "diagnostics/logging.h"
55 : :
56 : : #ifdef HAVE_TERMIOS_H
57 : : # include <termios.h>
58 : : #endif
59 : :
60 : : #ifdef GWINSZ_IN_SYS_IOCTL
61 : : # include <sys/ioctl.h>
62 : : #endif
63 : :
64 : : /* Disable warnings about quoting issues in the pp_xxx calls below
65 : : that (intentionally) don't follow GCC diagnostic conventions. */
66 : : #if __GNUC__ >= 10
67 : : # pragma GCC diagnostic push
68 : : # pragma GCC diagnostic ignored "-Wformat-diag"
69 : : #endif
70 : :
71 : : static void real_abort (void) ATTRIBUTE_NORETURN;
72 : :
73 : : /* Name of program invoked, sans directories. */
74 : :
75 : : const char *progname;
76 : :
77 : :
78 : : /* Return a malloc'd string containing MSG formatted a la printf. The
79 : : caller is responsible for freeing the memory. */
80 : : char *
81 : 3149740 : build_message_string (const char *msg, ...)
82 : : {
83 : 3149740 : char *str;
84 : 3149740 : va_list ap;
85 : :
86 : 3149740 : va_start (ap, msg);
87 : 3149740 : str = xvasprintf (msg, ap);
88 : 3149740 : va_end (ap);
89 : :
90 : 3149740 : return str;
91 : : }
92 : :
93 : :
94 : : /* Return the value of the getenv("COLUMNS") as an integer. If the
95 : : value is not set to a positive integer, use ioctl to get the
96 : : terminal width. If it fails, return INT_MAX. */
97 : : int
98 : 772160 : get_terminal_width (void)
99 : : {
100 : 772160 : const char * s = getenv ("COLUMNS");
101 : 772160 : if (s != nullptr) {
102 : 126 : int n = atoi (s);
103 : 126 : if (n > 0)
104 : : return n;
105 : : }
106 : :
107 : : #ifdef TIOCGWINSZ
108 : 772034 : struct winsize w;
109 : 772034 : w.ws_col = 0;
110 : 772034 : if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
111 : 0 : return w.ws_col;
112 : : #endif
113 : :
114 : : return INT_MAX;
115 : : }
116 : :
117 : : namespace diagnostics {
118 : :
119 : : /* Set caret_max_width to value. */
120 : :
121 : : void
122 : 818465 : context::set_caret_max_width (int value)
123 : : {
124 : : /* One minus to account for the leading empty space. */
125 : 1590531 : value = value ? value - 1
126 : 818465 : : (isatty (fileno (pp_buffer (get_reference_printer ())->m_stream))
127 : 818465 : ? get_terminal_width () - 1 : INT_MAX);
128 : :
129 : 772066 : if (value <= 0)
130 : 46399 : value = INT_MAX;
131 : :
132 : 818465 : m_source_printing.max_width = value;
133 : 818465 : }
134 : :
135 : : /* Initialize the diagnostic message outputting machinery. */
136 : :
137 : : void
138 : 715302 : context::initialize (int n_opts)
139 : : {
140 : : /* Allocate a basic pretty-printer. Clients will replace this a
141 : : much more elaborated pretty-printer if they wish. */
142 : 715302 : m_reference_printer = std::make_unique<pretty_printer> ().release ();
143 : :
144 : 715302 : m_file_cache = new file_cache ();
145 : 715302 : m_diagnostic_counters.clear ();
146 : 715302 : m_warning_as_error_requested = false;
147 : 715302 : m_n_opts = n_opts;
148 : 715302 : m_option_classifier.init (n_opts);
149 : 715302 : m_source_printing.enabled = false;
150 : 715302 : set_caret_max_width (pp_line_cutoff (get_reference_printer ()));
151 : 2861208 : for (int i = 0; i < rich_location::STATICALLY_ALLOCATED_RANGES; i++)
152 : 2145906 : m_source_printing.caret_chars[i] = '^';
153 : 715302 : m_show_cwe = false;
154 : 715302 : m_show_rules = false;
155 : 715302 : m_path_format = DPF_NONE;
156 : 715302 : m_show_path_depths = false;
157 : 715302 : m_show_option_requested = false;
158 : 715302 : m_abort_on_error = false;
159 : 715302 : m_show_column = false;
160 : 715302 : m_pedantic_errors = false;
161 : 715302 : m_permissive = false;
162 : 715302 : m_opt_permissive = 0;
163 : 715302 : m_fatal_errors = false;
164 : 715302 : m_inhibit_warnings = false;
165 : 715302 : m_warn_system_headers = false;
166 : 715302 : m_max_errors = 0;
167 : 715302 : m_internal_error = nullptr;
168 : 715302 : m_adjust_diagnostic_info = nullptr;
169 : 715302 : m_text_callbacks.m_begin_diagnostic = default_text_starter;
170 : 715302 : m_text_callbacks.m_text_start_span
171 : 715302 : = default_start_span_fn<to_text>;
172 : 715302 : m_text_callbacks.m_html_start_span
173 : 715302 : = default_start_span_fn<to_html>;
174 : 715302 : m_text_callbacks.m_end_diagnostic = default_text_finalizer;
175 : 715302 : m_option_id_mgr = nullptr;
176 : 715302 : m_urlifier_stack = new auto_vec<urlifier_stack_node> ();
177 : 715302 : m_last_location = UNKNOWN_LOCATION;
178 : 715302 : m_client_aux_data = nullptr;
179 : 715302 : m_lock = 0;
180 : 715302 : m_inhibit_notes_p = false;
181 : :
182 : 715302 : m_source_printing.colorize_source_p = false;
183 : 715302 : m_source_printing.show_labels_p = false;
184 : 715302 : m_source_printing.show_line_numbers_p = false;
185 : 715302 : m_source_printing.min_margin_width = 0;
186 : 715302 : m_source_printing.show_ruler_p = false;
187 : 715302 : m_source_printing.show_event_links_p = false;
188 : :
189 : 715302 : m_column_options.m_column_unit = DIAGNOSTICS_COLUMN_UNIT_DISPLAY;
190 : 715302 : m_column_options.m_column_origin = 1;
191 : 715302 : m_column_options.m_tabstop = 8;
192 : :
193 : 715302 : m_report_bug = false;
194 : 715302 : m_extra_output_kind = EXTRA_DIAGNOSTIC_OUTPUT_none;
195 : 715302 : if (const char *var = getenv ("GCC_EXTRA_DIAGNOSTIC_OUTPUT"))
196 : : {
197 : 4 : if (!strcmp (var, "fixits-v1"))
198 : 2 : m_extra_output_kind = EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1;
199 : 2 : else if (!strcmp (var, "fixits-v2"))
200 : 2 : m_extra_output_kind = EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2;
201 : : /* Silently ignore unrecognized values. */
202 : : }
203 : 715302 : m_escape_format = DIAGNOSTICS_ESCAPE_FORMAT_UNICODE;
204 : 715302 : m_fixits_change_set = nullptr;
205 : 715302 : m_diagnostic_groups.m_group_nesting_depth = 0;
206 : 715302 : m_diagnostic_groups.m_diagnostic_nesting_level = 0;
207 : 715302 : m_diagnostic_groups.m_emission_count = 0;
208 : 715302 : m_diagnostic_groups.m_inhibiting_notes_from = 0;
209 : 715302 : m_sinks.safe_push (new text_sink (*this, nullptr, true));
210 : 715302 : m_set_locations_cb = nullptr;
211 : 715302 : m_client_data_hooks = nullptr;
212 : 715302 : m_diagrams.m_theme = nullptr;
213 : 715302 : m_original_argv = nullptr;
214 : 715302 : m_diagnostic_buffer = nullptr;
215 : 715302 : m_logger = nullptr;
216 : :
217 : 715302 : enum diagnostic_text_art_charset text_art_charset
218 : : = DIAGNOSTICS_TEXT_ART_CHARSET_EMOJI;
219 : 715302 : if (const char *lang = getenv ("LANG"))
220 : : {
221 : : /* For LANG=C, don't assume the terminal supports anything
222 : : other than ASCII. */
223 : 713030 : if (!strcmp (lang, "C"))
224 : 715302 : text_art_charset = DIAGNOSTICS_TEXT_ART_CHARSET_ASCII;
225 : : }
226 : 715302 : set_text_art_charset (text_art_charset);
227 : :
228 : 715302 : if (const char *name = getenv ("GCC_DIAGNOSTICS_LOG"))
229 : : {
230 : 0 : if (name[0] != '\0')
231 : : {
232 : : /* Try to write a log to the named path. */
233 : 0 : if (FILE *outfile = fopen (name, "w"))
234 : 0 : m_logger = new logging::logger
235 : 0 : (output_file (outfile, true,
236 : 0 : label_text::take (xstrdup (name))));
237 : : }
238 : : else
239 : : /* Write a log to stderr. */
240 : 0 : m_logger = new logging::logger
241 : 0 : (output_file
242 : : (stderr, false,
243 : 0 : label_text::borrow ("stderr")));
244 : : }
245 : :
246 : 715302 : if (m_logger)
247 : 0 : m_logger->log_printf ("diagnostics::context::initialize");
248 : 715302 : }
249 : :
250 : : /* Maybe initialize the color support. We require clients to do this
251 : : explicitly, since most clients don't want color. When called
252 : : without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
253 : :
254 : : void
255 : 1422840 : context::color_init (int value)
256 : : {
257 : : /* value == -1 is the default value. */
258 : 1422840 : if (value < 0)
259 : : {
260 : : /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
261 : : -fdiagnostics-color=auto if GCC_COLORS is in the environment,
262 : : otherwise default to -fdiagnostics-color=never, for other
263 : : values default to that
264 : : -fdiagnostics-color={never,auto,always}. */
265 : 600905 : if (DIAGNOSTICS_COLOR_DEFAULT == -1)
266 : : {
267 : : if (!getenv ("GCC_COLORS"))
268 : : return;
269 : : value = DIAGNOSTICS_COLOR_AUTO;
270 : : }
271 : : else
272 : 600905 : value = DIAGNOSTICS_COLOR_DEFAULT;
273 : : }
274 : 2845680 : pp_show_color (m_reference_printer)
275 : 1422840 : = colorize_init ((diagnostic_color_rule_t) value);
276 : 5691360 : for (auto sink_ : m_sinks)
277 : 1422840 : if (sink_->follows_reference_printer_p ())
278 : 1422840 : pp_show_color (sink_->get_printer ())
279 : 1422840 : = pp_show_color (m_reference_printer);
280 : : }
281 : :
282 : : /* Initialize URL support within this context based on VALUE,
283 : : handling "auto". */
284 : :
285 : : void
286 : 1411738 : context::urls_init (int value)
287 : : {
288 : : /* value == -1 is the default value. */
289 : 1411738 : if (value < 0)
290 : : {
291 : : /* If DIAGNOSTICS_URLS_DEFAULT is -1, default to
292 : : -fdiagnostics-urls=auto if GCC_URLS or TERM_URLS is in the
293 : : environment, otherwise default to -fdiagnostics-urls=never,
294 : : for other values default to that
295 : : -fdiagnostics-urls={never,auto,always}. */
296 : 600905 : if (DIAGNOSTICS_URLS_DEFAULT == -1)
297 : : {
298 : : if (!getenv ("GCC_URLS") && !getenv ("TERM_URLS"))
299 : : return;
300 : : value = DIAGNOSTICS_URL_AUTO;
301 : : }
302 : : else
303 : 600905 : value = DIAGNOSTICS_URLS_DEFAULT;
304 : : }
305 : :
306 : 1411738 : m_reference_printer->set_url_format
307 : 1411738 : (determine_url_format ((diagnostic_url_rule_t) value));
308 : 5646952 : for (auto sink_ : m_sinks)
309 : 1411738 : if (sink_->follows_reference_printer_p ())
310 : 1411738 : sink_->get_printer ()->set_url_format
311 : 1411738 : (m_reference_printer->get_url_format ());
312 : : }
313 : :
314 : : void
315 : 559366 : context::set_show_nesting (bool val)
316 : : {
317 : 2237464 : for (auto sink_ : m_sinks)
318 : 559366 : if (sink_->follows_reference_printer_p ())
319 : 559366 : if (auto text_sink_ = sink_->dyn_cast_text_sink ())
320 : 559366 : text_sink_->set_show_nesting (val);
321 : 559366 : }
322 : :
323 : : void
324 : 289302 : context::set_show_nesting_locations (bool val)
325 : : {
326 : 1157208 : for (auto sink_ : m_sinks)
327 : 289302 : if (sink_->follows_reference_printer_p ())
328 : 289302 : if (auto text_sink_ = sink_->dyn_cast_text_sink ())
329 : 289302 : text_sink_->set_show_locations_in_nesting (val);
330 : 289302 : }
331 : :
332 : : void
333 : 289302 : context::set_show_nesting_levels (bool val)
334 : : {
335 : 1157208 : for (auto sink_ : m_sinks)
336 : 289302 : if (sink_->follows_reference_printer_p ())
337 : 289302 : if (auto text_sink_ = sink_->dyn_cast_text_sink ())
338 : 289302 : text_sink_->set_show_nesting_levels (val);
339 : 289302 : }
340 : :
341 : : /* Create the file_cache, if not already created, and tell it how to
342 : : translate files on input. */
343 : : void
344 : 211250 : context::initialize_input_context (diagnostic_input_charset_callback ccb,
345 : : bool should_skip_bom)
346 : : {
347 : 211250 : m_file_cache->initialize_input_context (ccb, should_skip_bom);
348 : 211250 : }
349 : :
350 : : /* Do any cleaning up required after the last diagnostic is emitted. */
351 : :
352 : : void
353 : 308411 : context::finish ()
354 : : {
355 : 308411 : if (m_logger)
356 : : {
357 : 0 : m_logger->log_printf ("diagnostics::context::finish");
358 : : /* We're cleaning up the logger before this function exits,
359 : : so we can't use auto_inc_depth here. */
360 : 0 : m_logger->inc_depth ();
361 : 0 : dump (m_logger->get_stream (), m_logger->get_indent ());
362 : : }
363 : :
364 : 1233677 : for (auto iter : m_sinks)
365 : 308444 : iter->finalize_extensions ();
366 : :
367 : : /* We might be handling a fatal error.
368 : : Close any active diagnostic groups, which may trigger flushing
369 : : sinks. */
370 : 309165 : while (m_diagnostic_groups.m_group_nesting_depth > 0)
371 : 754 : end_group ();
372 : :
373 : 308411 : set_diagnostic_buffer (nullptr);
374 : :
375 : : /* Clean ups. */
376 : :
377 : 925266 : while (!m_sinks.is_empty ())
378 : 308444 : delete m_sinks.pop ();
379 : :
380 : 308411 : if (m_diagrams.m_theme)
381 : : {
382 : 39948 : delete m_diagrams.m_theme;
383 : 39948 : m_diagrams.m_theme = nullptr;
384 : : }
385 : :
386 : 308411 : delete m_file_cache;
387 : 308411 : m_file_cache = nullptr;
388 : :
389 : 308411 : m_option_classifier.fini ();
390 : :
391 : 308411 : delete m_reference_printer;
392 : 308411 : m_reference_printer = nullptr;
393 : :
394 : 308411 : if (m_fixits_change_set)
395 : : {
396 : 17 : delete m_fixits_change_set;
397 : 17 : m_fixits_change_set = nullptr;
398 : : }
399 : :
400 : 308411 : if (m_client_data_hooks)
401 : : {
402 : 287767 : delete m_client_data_hooks;
403 : 287767 : m_client_data_hooks = nullptr;
404 : : }
405 : :
406 : 308411 : delete m_option_id_mgr;
407 : 308411 : m_option_id_mgr = nullptr;
408 : :
409 : 308411 : if (m_urlifier_stack)
410 : : {
411 : 597518 : while (!m_urlifier_stack->is_empty ())
412 : 289107 : pop_urlifier ();
413 : 308411 : delete m_urlifier_stack;
414 : 308411 : m_urlifier_stack = nullptr;
415 : : }
416 : :
417 : 308411 : freeargv (m_original_argv);
418 : 308411 : m_original_argv = nullptr;
419 : :
420 : 308411 : delete m_logger;
421 : 308411 : m_logger = nullptr;
422 : 308411 : }
423 : :
424 : : /* Dump state of this diagnostics::context to OUT, for debugging. */
425 : :
426 : : void
427 : 0 : context::dump (FILE *outfile, int indent) const
428 : : {
429 : 0 : dumping::emit_heading (outfile, indent, "diagnostics::context");
430 : 0 : m_diagnostic_counters.dump (outfile, indent + 2);
431 : 0 : dumping::emit_heading (outfile, indent + 2, "reference printer");
432 : 0 : if (m_reference_printer)
433 : 0 : m_reference_printer->dump (outfile, indent + 4);
434 : : else
435 : 0 : dumping::emit_none (outfile, indent + 4);
436 : 0 : dumping::emit_heading (outfile, indent + 2, "output sinks");
437 : 0 : if (m_sinks.length () > 0)
438 : : {
439 : 0 : for (unsigned i = 0; i < m_sinks.length (); ++i)
440 : : {
441 : 0 : dumping::emit_indent (outfile, indent + 4);
442 : 0 : const sink *s = m_sinks[i];
443 : 0 : fprintf (outfile, "sink %i (", i);
444 : 0 : s->dump_kind (outfile);
445 : 0 : fprintf (outfile, "):\n");
446 : 0 : s->dump (outfile, indent + 6);
447 : : }
448 : : }
449 : : else
450 : 0 : dumping::emit_none (outfile, indent + 4);
451 : 0 : dumping::emit_heading (outfile, indent + 2, "diagnostic buffer");
452 : 0 : if (m_diagnostic_buffer)
453 : 0 : m_diagnostic_buffer->dump (outfile, indent + 4);
454 : : else
455 : 0 : dumping::emit_none (outfile, indent + 4);
456 : 0 : dumping::emit_heading (outfile, indent + 2, "file cache");
457 : 0 : if (m_file_cache)
458 : 0 : m_file_cache->dump (outfile, indent + 4);
459 : : else
460 : 0 : dumping::emit_none (outfile, indent + 4);
461 : 0 : dumping::emit_heading (outfile, indent + 2, "client data hooks");
462 : 0 : if (m_client_data_hooks)
463 : 0 : m_client_data_hooks->dump (outfile, indent + 4);
464 : : else
465 : 0 : dumping::emit_none (outfile, indent + 4);
466 : 0 : }
467 : :
468 : : /* Return true if sufficiently severe diagnostics have been seen that
469 : : we ought to exit with a non-zero exit code. */
470 : :
471 : : bool
472 : 287560 : context::execution_failed_p () const
473 : : {
474 : 287560 : return (diagnostic_count (kind::fatal)
475 : 287559 : || diagnostic_count (kind::error)
476 : 260597 : || diagnostic_count (kind::sorry)
477 : 547984 : || diagnostic_count (kind::werror));
478 : : }
479 : :
480 : : void
481 : 1615 : context::remove_all_output_sinks ()
482 : : {
483 : 4845 : while (!m_sinks.is_empty ())
484 : 1615 : delete m_sinks.pop ();
485 : 1615 : }
486 : :
487 : : void
488 : 1615 : context::set_sink (std::unique_ptr<sink> sink_)
489 : : {
490 : 1615 : DIAGNOSTICS_LOG_SCOPE_PRINTF0 (m_logger, "diagnostics::context::set_sink");
491 : 1615 : if (m_logger)
492 : 0 : sink_->dump (m_logger->get_stream (), m_logger->get_indent ());
493 : 1615 : remove_all_output_sinks ();
494 : 1615 : m_sinks.safe_push (sink_.release ());
495 : 1615 : }
496 : :
497 : : sink &
498 : 18845 : context::get_sink (size_t idx) const
499 : : {
500 : 18845 : gcc_assert (idx < m_sinks.length ());
501 : 18845 : gcc_assert (m_sinks[idx]);
502 : 18845 : return *m_sinks[idx];
503 : : }
504 : :
505 : : void
506 : 33 : context::add_sink (std::unique_ptr<sink> sink_)
507 : : {
508 : 33 : DIAGNOSTICS_LOG_SCOPE_PRINTF0 (m_logger, "diagnostics::context::add_sink");
509 : 33 : if (m_logger)
510 : 0 : sink_->dump (m_logger->get_stream (), m_logger->get_indent ());
511 : 33 : m_sinks.safe_push (sink_.release ());
512 : 33 : }
513 : :
514 : : /* Return true if there are no machine-readable formats writing to stderr. */
515 : :
516 : : bool
517 : 18557 : context::supports_fnotice_on_stderr_p () const
518 : : {
519 : 74182 : for (auto sink_ : m_sinks)
520 : 18511 : if (sink_->machine_readable_stderr_p ())
521 : : return false;
522 : : return true;
523 : : }
524 : :
525 : : void
526 : 0 : context::set_main_input_filename (const char *filename)
527 : : {
528 : 0 : for (auto sink_ : m_sinks)
529 : 0 : sink_->set_main_input_filename (filename);
530 : 0 : }
531 : :
532 : : void
533 : 344459 : context::set_client_data_hooks (std::unique_ptr<client_data_hooks> hooks)
534 : : {
535 : 344459 : delete m_client_data_hooks;
536 : : /* Ideally the field would be a std::unique_ptr here. */
537 : 344459 : m_client_data_hooks = hooks.release ();
538 : 344459 : }
539 : :
540 : : void
541 : 289302 : context::set_original_argv (unique_argv original_argv)
542 : : {
543 : : /* Ideally we'd use a unique_argv for m_original_argv, but
544 : : diagnostics::context doesn't yet have a ctor/dtor pair. */
545 : :
546 : : // Ensure any old value is freed
547 : 289302 : freeargv (m_original_argv);
548 : :
549 : : // Take ownership of the new value
550 : 289302 : m_original_argv = original_argv.release ();
551 : 289302 : }
552 : :
553 : : void
554 : 302473 : context::set_option_id_manager (std::unique_ptr<option_id_manager> mgr,
555 : : unsigned lang_mask)
556 : : {
557 : 302473 : delete m_option_id_mgr;
558 : 302473 : m_option_id_mgr = mgr.release ();
559 : 302473 : m_lang_mask = lang_mask;
560 : 302473 : }
561 : :
562 : : void
563 : 587746 : context::push_owned_urlifier (std::unique_ptr<urlifier> ptr)
564 : : {
565 : 587746 : gcc_assert (m_urlifier_stack);
566 : 587746 : const urlifier_stack_node node = { ptr.release (), true };
567 : 587746 : m_urlifier_stack->safe_push (node);
568 : 587746 : }
569 : :
570 : : void
571 : 1926878773 : context::push_borrowed_urlifier (const urlifier &loan)
572 : : {
573 : 1926878773 : gcc_assert (m_urlifier_stack);
574 : 1926878773 : const urlifier_stack_node node = { const_cast <urlifier *> (&loan), false };
575 : 1926878773 : m_urlifier_stack->safe_push (node);
576 : 1926878773 : }
577 : :
578 : : void
579 : 1927167879 : context::pop_urlifier ()
580 : : {
581 : 1927167879 : gcc_assert (m_urlifier_stack);
582 : 1927167879 : gcc_assert (m_urlifier_stack->length () > 0);
583 : :
584 : 1927167879 : const urlifier_stack_node node = m_urlifier_stack->pop ();
585 : 1927167879 : if (node.m_owned)
586 : 289106 : delete node.m_urlifier;
587 : 1927167879 : }
588 : :
589 : : const logical_locations::manager *
590 : 3975 : context::get_logical_location_manager () const
591 : : {
592 : 3975 : if (!m_client_data_hooks)
593 : : return nullptr;
594 : 3975 : return m_client_data_hooks->get_logical_location_manager ();
595 : : }
596 : :
597 : : const urlifier *
598 : 1541985 : context::get_urlifier () const
599 : : {
600 : 1541985 : if (!m_urlifier_stack)
601 : : return nullptr;
602 : 1541985 : if (m_urlifier_stack->is_empty ())
603 : : return nullptr;
604 : 1541639 : return m_urlifier_stack->last ().m_urlifier;
605 : : }
606 : :
607 : :
608 : : /* Set PP as the reference printer for this context.
609 : : Refresh all output sinks. */
610 : :
611 : : void
612 : 210211 : context::set_pretty_printer (std::unique_ptr<pretty_printer> pp)
613 : : {
614 : 210211 : delete m_reference_printer;
615 : 210211 : m_reference_printer = pp.release ();
616 : 210211 : refresh_output_sinks ();
617 : 210211 : }
618 : :
619 : : /* Give all output sinks a chance to rebuild their pretty_printer. */
620 : :
621 : : void
622 : 499513 : context::refresh_output_sinks ()
623 : : {
624 : 1998052 : for (auto sink_ : m_sinks)
625 : 499513 : sink_->update_printer ();
626 : 499513 : }
627 : :
628 : : /* Set FORMAT_DECODER on the reference printer and on the pretty_printer
629 : : of all output sinks. */
630 : :
631 : : void
632 : 585614 : context::set_format_decoder (printer_fn format_decoder)
633 : : {
634 : 585614 : pp_format_decoder (m_reference_printer) = format_decoder;
635 : 2342456 : for (auto sink_ : m_sinks)
636 : 585614 : pp_format_decoder (sink_->get_printer ()) = format_decoder;
637 : 585614 : }
638 : :
639 : : void
640 : 578609 : context::set_show_highlight_colors (bool val)
641 : : {
642 : 578609 : pp_show_highlight_colors (m_reference_printer) = val;
643 : 2314436 : for (auto sink_ : m_sinks)
644 : 578609 : if (sink_->follows_reference_printer_p ())
645 : 578609 : pp_show_highlight_colors (sink_->get_printer ()) = val;
646 : 578609 : }
647 : :
648 : : void
649 : 422 : context::set_prefixing_rule (diagnostic_prefixing_rule_t rule)
650 : : {
651 : 422 : pp_prefixing_rule (m_reference_printer) = rule;
652 : 1688 : for (auto sink_ : m_sinks)
653 : 422 : if (sink_->follows_reference_printer_p ())
654 : 422 : pp_prefixing_rule (sink_->get_printer ()) = rule;
655 : 422 : }
656 : :
657 : : void
658 : 17 : context::initialize_fixits_change_set ()
659 : : {
660 : 17 : delete m_fixits_change_set;
661 : 17 : gcc_assert (m_file_cache);
662 : 17 : m_fixits_change_set = new changes::change_set (*m_file_cache);
663 : 17 : }
664 : :
665 : : // class client_data_hooks
666 : :
667 : : void
668 : 0 : client_data_hooks::dump (FILE *outfile, int indent) const
669 : : {
670 : 0 : dumping::emit_heading (outfile, indent, "logical location manager");
671 : 0 : if (auto mgr = get_logical_location_manager ())
672 : 0 : mgr->dump (outfile, indent + 2);
673 : : else
674 : 0 : dumping::emit_none (outfile, indent + 2);
675 : 0 : }
676 : :
677 : : } // namespace diagnostics
678 : :
679 : : /* Initialize DIAGNOSTIC, where the message MSG has already been
680 : : translated. */
681 : : void
682 : 97088460 : diagnostic_set_info_translated (diagnostics::diagnostic_info *diagnostic,
683 : : const char *msg, va_list *args,
684 : : rich_location *richloc,
685 : : enum diagnostics::kind kind)
686 : : {
687 : 97088460 : gcc_assert (richloc);
688 : 97088460 : diagnostic->m_message.m_err_no = errno;
689 : 97088460 : diagnostic->m_message.m_args_ptr = args;
690 : 97088460 : diagnostic->m_message.m_format_spec = msg;
691 : 97088460 : diagnostic->m_message.m_richloc = richloc;
692 : 97088460 : diagnostic->m_richloc = richloc;
693 : 97088460 : diagnostic->m_metadata = nullptr;
694 : 97088460 : diagnostic->m_kind = kind;
695 : 97088460 : diagnostic->m_option_id = 0;
696 : 97088460 : }
697 : :
698 : : /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
699 : : translated. */
700 : : void
701 : 96927779 : diagnostic_set_info (diagnostics::diagnostic_info *diagnostic,
702 : : const char *gmsgid, va_list *args,
703 : : rich_location *richloc,
704 : : enum diagnostics::kind kind)
705 : : {
706 : 96927779 : gcc_assert (richloc);
707 : 96927779 : diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
708 : 96927779 : }
709 : :
710 : : namespace diagnostics {
711 : :
712 : : static const char *const diagnostic_kind_text[] = {
713 : : #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
714 : : #include "diagnostics/kinds.def"
715 : : #undef DEFINE_DIAGNOSTIC_KIND
716 : : "must-not-happen"
717 : : };
718 : :
719 : : /* Get unlocalized string describing KIND. */
720 : :
721 : : const char *
722 : 354242 : get_text_for_kind (enum kind kind)
723 : : {
724 : 354242 : return diagnostic_kind_text[static_cast<int> (kind)];
725 : : }
726 : :
727 : : static const char *const diagnostic_kind_debug_text[] = {
728 : : #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (#K),
729 : : #include "diagnostics/kinds.def"
730 : : #undef DEFINE_DIAGNOSTIC_KIND
731 : : "must-not-happen"
732 : : };
733 : :
734 : : const char *
735 : 0 : get_debug_string_for_kind (enum kind kind)
736 : : {
737 : 0 : return diagnostic_kind_debug_text[static_cast<int> (kind)];
738 : : }
739 : :
740 : : static const char *const diagnostic_kind_color[] = {
741 : : #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
742 : : #include "diagnostics/kinds.def"
743 : : #undef DEFINE_DIAGNOSTIC_KIND
744 : : nullptr
745 : : };
746 : :
747 : : /* Get a color name for diagnostics of type KIND
748 : : Result could be nullptr. */
749 : :
750 : : const char *
751 : 1708537 : get_color_for_kind (enum kind kind)
752 : : {
753 : 1708537 : return diagnostic_kind_color[static_cast<int> (kind)];
754 : : }
755 : :
756 : : /* Given an expanded_location, convert the column (which is in 1-based bytes)
757 : : to the requested units, without converting the origin.
758 : : Return -1 if the column is invalid (<= 0). */
759 : :
760 : : static int
761 : 314965 : convert_column_unit (file_cache &fc,
762 : : enum diagnostics_column_unit column_unit,
763 : : int tabstop,
764 : : expanded_location s)
765 : : {
766 : 314965 : if (s.column <= 0)
767 : : return -1;
768 : :
769 : 312143 : switch (column_unit)
770 : : {
771 : 0 : default:
772 : 0 : gcc_unreachable ();
773 : :
774 : 312007 : case DIAGNOSTICS_COLUMN_UNIT_DISPLAY:
775 : 312007 : {
776 : 312007 : cpp_char_column_policy policy (tabstop, cpp_wcwidth);
777 : 312007 : return location_compute_display_column (fc, s, policy);
778 : : }
779 : :
780 : : case DIAGNOSTICS_COLUMN_UNIT_BYTE:
781 : : return s.column;
782 : : }
783 : : }
784 : :
785 : : /* Given an expanded_location, convert the column (which is in 1-based bytes)
786 : : to the requested units and origin. Return -1 if the column is
787 : : invalid (<= 0). */
788 : : int
789 : 314887 : column_options::convert_column (file_cache &fc,
790 : : expanded_location s) const
791 : : {
792 : 314887 : int one_based_col = convert_column_unit (fc, m_column_unit, m_tabstop, s);
793 : 314887 : if (one_based_col <= 0)
794 : : return -1;
795 : 312065 : return one_based_col + (m_column_origin - 1);
796 : : }
797 : :
798 : 2323576 : column_policy::column_policy (const context &dc)
799 : 2323576 : : m_file_cache (dc.get_file_cache ()),
800 : 2323576 : m_column_options (dc.get_column_options ())
801 : : {
802 : 2323576 : }
803 : :
804 : : /* Given an expanded_location, convert the column (which is in 1-based bytes)
805 : : to the requested units and origin. Return -1 if the column is
806 : : invalid (<= 0). */
807 : : int
808 : 314887 : column_policy::converted_column (expanded_location s) const
809 : : {
810 : 314887 : return m_column_options.convert_column (m_file_cache, s);
811 : : }
812 : :
813 : : /* Return a string describing a location e.g. "foo.c:42:10". */
814 : :
815 : : label_text
816 : 344840 : column_policy::get_location_text (const expanded_location &s,
817 : : bool show_column,
818 : : bool colorize) const
819 : : {
820 : 344840 : const char *locus_cs = colorize_start (colorize, "locus");
821 : 344840 : const char *locus_ce = colorize_stop (colorize);
822 : 344840 : const char *file = s.file ? s.file : progname;
823 : 344840 : int line = 0;
824 : 344840 : int col = -1;
825 : 344840 : if (strcmp (file, special_fname_builtin ()))
826 : : {
827 : 344047 : line = s.line;
828 : 344047 : if (show_column)
829 : 313777 : col = converted_column (s);
830 : : }
831 : :
832 : 344840 : const char *line_col = text_sink::maybe_line_and_column (line, col);
833 : 344840 : return label_text::take (build_message_string ("%s%s%s:%s", locus_cs, file,
834 : 344840 : line_col, locus_ce));
835 : : }
836 : :
837 : 50248 : location_print_policy::
838 : 50248 : location_print_policy (const context &dc)
839 : 50248 : : m_column_policy (dc),
840 : 50248 : m_show_column (dc.m_show_column)
841 : : {
842 : 50248 : }
843 : :
844 : 1212124 : location_print_policy::
845 : 1212124 : location_print_policy (const text_sink &text_output)
846 : : :
847 : 1212124 : m_column_policy (text_output.get_context ()),
848 : 1212124 : m_show_column (text_output.get_context ().m_show_column)
849 : : {
850 : 1212124 : }
851 : :
852 : : } // namespace diagnostics
853 : :
854 : : /* Functions at which to stop the backtrace print. It's not
855 : : particularly helpful to print the callers of these functions. */
856 : :
857 : : static const char * const bt_stop[] =
858 : : {
859 : : "main",
860 : : "toplev::main",
861 : : "execute_one_pass",
862 : : "compile_file",
863 : : };
864 : :
865 : : /* A callback function passed to the backtrace_full function. */
866 : :
867 : : static int
868 : 277 : bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
869 : : const char *function)
870 : : {
871 : 277 : int *pcount = (int *) data;
872 : :
873 : : /* If we don't have any useful information, don't print
874 : : anything. */
875 : 277 : if (filename == nullptr && function == nullptr)
876 : : return 0;
877 : :
878 : : /* Skip functions in context.cc. */
879 : 274 : if (*pcount == 0
880 : 40 : && filename != nullptr
881 : 314 : && strcmp (lbasename (filename), "context.cc") == 0)
882 : : return 0;
883 : :
884 : : /* Print up to 20 functions. We could make this a --param, but
885 : : since this is only for debugging just use a constant for now. */
886 : 254 : if (*pcount >= 20)
887 : : {
888 : : /* Returning a non-zero value stops the backtrace. */
889 : : return 1;
890 : : }
891 : 250 : ++*pcount;
892 : :
893 : 250 : char *alc = nullptr;
894 : 250 : if (function != nullptr)
895 : : {
896 : 250 : char *str = cplus_demangle_v3 (function,
897 : : (DMGL_VERBOSE | DMGL_ANSI
898 : : | DMGL_GNU_V3 | DMGL_PARAMS));
899 : 250 : if (str != nullptr)
900 : : {
901 : 126 : alc = str;
902 : 126 : function = str;
903 : : }
904 : :
905 : 1221 : for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
906 : : {
907 : 987 : size_t len = strlen (bt_stop[i]);
908 : 987 : if (strncmp (function, bt_stop[i], len) == 0
909 : 16 : && (function[len] == '\0' || function[len] == '('))
910 : : {
911 : 16 : if (alc != nullptr)
912 : 13 : free (alc);
913 : : /* Returning a non-zero value stops the backtrace. */
914 : 16 : return 1;
915 : : }
916 : : }
917 : : }
918 : :
919 : 468 : fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
920 : : (unsigned long) pc,
921 : : function == nullptr ? "???" : function,
922 : : filename == nullptr ? "???" : filename,
923 : : lineno);
924 : :
925 : 234 : if (alc != nullptr)
926 : 113 : free (alc);
927 : :
928 : : return 0;
929 : : }
930 : :
931 : : /* A callback function passed to the backtrace_full function. This is
932 : : called if backtrace_full has an error. */
933 : :
934 : : static void
935 : 0 : bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
936 : : {
937 : 0 : if (errnum < 0)
938 : : {
939 : : /* This means that no debug info was available. Just quietly
940 : : skip printing backtrace info. */
941 : : return;
942 : : }
943 : 0 : fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
944 : 0 : errnum == 0 ? "" : xstrerror (errnum));
945 : : }
946 : :
947 : : namespace diagnostics {
948 : :
949 : : /* Check if we've met the maximum error limit, and if so fatally exit
950 : : with a message.
951 : : FLUSH indicates whether a diagnostics::context::finish call is needed. */
952 : :
953 : : void
954 : 1437317 : context::check_max_errors (bool flush)
955 : : {
956 : 1437317 : if (!m_max_errors)
957 : : return;
958 : :
959 : 3665 : int count = (diagnostic_count (kind::error)
960 : 3665 : + diagnostic_count (kind::sorry)
961 : 3665 : + diagnostic_count (kind::werror));
962 : :
963 : 3665 : if (count >= m_max_errors)
964 : : {
965 : 7 : fnotice (stderr,
966 : : "compilation terminated due to -fmax-errors=%u.\n",
967 : : m_max_errors);
968 : 7 : if (flush)
969 : 3 : finish ();
970 : 7 : exit (FATAL_EXIT_CODE);
971 : : }
972 : : }
973 : :
974 : : /* Take any action which is expected to happen after the diagnostic
975 : : is written out. This function does not always return. */
976 : :
977 : : void
978 : 351726 : context::action_after_output (enum kind diag_kind)
979 : : {
980 : 351726 : switch (diag_kind)
981 : : {
982 : : case kind::debug:
983 : : case kind::note:
984 : : case kind::anachronism:
985 : : case kind::warning:
986 : : break;
987 : :
988 : 142260 : case kind::error:
989 : 142260 : case kind::sorry:
990 : 142260 : if (m_abort_on_error)
991 : 0 : real_abort ();
992 : 142260 : if (m_fatal_errors)
993 : : {
994 : 7 : fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
995 : 7 : finish ();
996 : 7 : exit (FATAL_EXIT_CODE);
997 : : }
998 : : break;
999 : :
1000 : 20 : case kind::ice:
1001 : 20 : case kind::ice_nobt:
1002 : 20 : {
1003 : : /* Attempt to ensure that any outputs are flushed e.g. that .sarif
1004 : : files are written out.
1005 : : Only do it once. */
1006 : 20 : static bool finishing_due_to_ice = false;
1007 : 20 : if (!finishing_due_to_ice)
1008 : : {
1009 : 20 : finishing_due_to_ice = true;
1010 : 20 : finish ();
1011 : : }
1012 : :
1013 : 20 : struct backtrace_state *state = nullptr;
1014 : 20 : if (diag_kind == kind::ice)
1015 : 20 : state = backtrace_create_state (nullptr, 0, bt_err_callback, nullptr);
1016 : 20 : int count = 0;
1017 : 20 : if (state != nullptr)
1018 : 20 : backtrace_full (state, 2, bt_callback, bt_err_callback,
1019 : : (void *) &count);
1020 : :
1021 : 20 : if (m_abort_on_error)
1022 : 0 : real_abort ();
1023 : :
1024 : 20 : if (m_report_bug)
1025 : 0 : fnotice (stderr, "Please submit a full bug report, "
1026 : : "with preprocessed source.\n");
1027 : : else
1028 : 20 : fnotice (stderr, "Please submit a full bug report, "
1029 : : "with preprocessed source (by using -freport-bug).\n");
1030 : :
1031 : 20 : if (count > 0)
1032 : 20 : fnotice (stderr, "Please include the complete backtrace "
1033 : : "with any bug report.\n");
1034 : 20 : fnotice (stderr, "See %s for instructions.\n", bug_report_url);
1035 : :
1036 : 20 : exit (ICE_EXIT_CODE);
1037 : : }
1038 : :
1039 : 682 : case kind::fatal:
1040 : 682 : if (m_abort_on_error)
1041 : 0 : real_abort ();
1042 : 682 : fnotice (stderr, "compilation terminated.\n");
1043 : 682 : finish ();
1044 : 682 : exit (FATAL_EXIT_CODE);
1045 : :
1046 : 0 : default:
1047 : 0 : gcc_unreachable ();
1048 : : }
1049 : 351017 : }
1050 : :
1051 : : /* State whether we should inhibit notes in the current diagnostic_group and
1052 : : its future children if any. */
1053 : :
1054 : : void
1055 : 954405883 : context::inhibit_notes_in_group (bool inhibit)
1056 : : {
1057 : 954405883 : int curr_depth = (m_diagnostic_groups.m_group_nesting_depth
1058 : 954405883 : + m_diagnostic_groups.m_diagnostic_nesting_level);
1059 : :
1060 : 954405883 : if (inhibit)
1061 : : {
1062 : : /* If we're already inhibiting, there's nothing to do. */
1063 : 95319959 : if (m_diagnostic_groups.m_inhibiting_notes_from)
1064 : : return;
1065 : :
1066 : : /* Since we're called via warning/error/... that all have their own
1067 : : diagnostic_group, we must consider that we started inhibiting in their
1068 : : parent. */
1069 : 95319897 : gcc_assert (m_diagnostic_groups.m_group_nesting_depth > 0);
1070 : 95319897 : m_diagnostic_groups.m_inhibiting_notes_from = curr_depth - 1;
1071 : : }
1072 : 859085924 : else if (m_diagnostic_groups.m_inhibiting_notes_from)
1073 : : {
1074 : : /* Only cancel inhibition at the depth that set it up. */
1075 : 3128698 : if (curr_depth >= m_diagnostic_groups.m_inhibiting_notes_from)
1076 : : return;
1077 : :
1078 : 1564305 : m_diagnostic_groups.m_inhibiting_notes_from = 0;
1079 : : }
1080 : : }
1081 : :
1082 : : /* Return whether notes must be inhibited in the current diagnostic_group. */
1083 : :
1084 : : bool
1085 : 111672 : context::notes_inhibited_in_group () const
1086 : : {
1087 : 111672 : if (m_diagnostic_groups.m_inhibiting_notes_from
1088 : 17 : && (m_diagnostic_groups.m_group_nesting_depth
1089 : 17 : + m_diagnostic_groups.m_diagnostic_nesting_level
1090 : : >= m_diagnostic_groups.m_inhibiting_notes_from))
1091 : 17 : return true;
1092 : : return false;
1093 : : }
1094 : :
1095 : : /* class diagnostics::logical_locations::manager. */
1096 : :
1097 : : /* Return true iff this is a function or method. */
1098 : :
1099 : : bool
1100 : 3995 : logical_locations::manager::function_p (key k) const
1101 : : {
1102 : 3995 : switch (get_kind (k))
1103 : : {
1104 : 0 : default:
1105 : 0 : gcc_unreachable ();
1106 : : case kind::unknown:
1107 : : case kind::module_:
1108 : : case kind::namespace_:
1109 : : case kind::type:
1110 : : case kind::return_type:
1111 : : case kind::parameter:
1112 : : case kind::variable:
1113 : : return false;
1114 : :
1115 : 3995 : case kind::function:
1116 : 3995 : case kind::member:
1117 : 3995 : return true;
1118 : : }
1119 : : }
1120 : :
1121 : : /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
1122 : : escaping rules for -fdiagnostics-parseable-fixits. */
1123 : :
1124 : : static void
1125 : 105 : print_escaped_string (pretty_printer *pp, const char *text)
1126 : : {
1127 : 105 : gcc_assert (pp);
1128 : 105 : gcc_assert (text);
1129 : :
1130 : 105 : pp_character (pp, '"');
1131 : 3786 : for (const char *ch = text; *ch; ch++)
1132 : : {
1133 : 3681 : switch (*ch)
1134 : : {
1135 : 3 : case '\\':
1136 : : /* Escape backslash as two backslashes. */
1137 : 3 : pp_string (pp, "\\\\");
1138 : 3 : break;
1139 : 3 : case '\t':
1140 : : /* Escape tab as "\t". */
1141 : 3 : pp_string (pp, "\\t");
1142 : 3 : break;
1143 : 6 : case '\n':
1144 : : /* Escape newline as "\n". */
1145 : 6 : pp_string (pp, "\\n");
1146 : 6 : break;
1147 : 3 : case '"':
1148 : : /* Escape doublequotes as \". */
1149 : 3 : pp_string (pp, "\\\"");
1150 : 3 : break;
1151 : 3666 : default:
1152 : 3666 : if (ISPRINT (*ch))
1153 : 3660 : pp_character (pp, *ch);
1154 : : else
1155 : : /* Use octal for non-printable chars. */
1156 : : {
1157 : 6 : unsigned char c = (*ch & 0xff);
1158 : 6 : pp_printf (pp, "\\%o%o%o", (c / 64), (c / 8) & 007, c & 007);
1159 : : }
1160 : : break;
1161 : : }
1162 : : }
1163 : 105 : pp_character (pp, '"');
1164 : 105 : }
1165 : :
1166 : : /* Implementation of -fdiagnostics-parseable-fixits and
1167 : : GCC_EXTRA_DIAGNOSTIC_OUTPUT.
1168 : : Print a machine-parseable version of all fixits in RICHLOC to PP,
1169 : : using COLUMN_UNIT to express columns.
1170 : : Use TABSTOP when handling DIAGNOSTICS_COLUMN_UNIT_DISPLAY. */
1171 : :
1172 : : static void
1173 : 39 : print_parseable_fixits (file_cache &fc,
1174 : : pretty_printer *pp, rich_location *richloc,
1175 : : enum diagnostics_column_unit column_unit,
1176 : : int tabstop)
1177 : : {
1178 : 39 : gcc_assert (pp);
1179 : 39 : gcc_assert (richloc);
1180 : :
1181 : 39 : char *saved_prefix = pp_take_prefix (pp);
1182 : 39 : pp_set_prefix (pp, nullptr);
1183 : :
1184 : 78 : for (unsigned i = 0; i < richloc->get_num_fixit_hints (); i++)
1185 : : {
1186 : 39 : const fixit_hint *hint = richloc->get_fixit_hint (i);
1187 : 39 : location_t start_loc = hint->get_start_loc ();
1188 : 39 : expanded_location start_exploc = expand_location (start_loc);
1189 : 39 : pp_string (pp, "fix-it:");
1190 : 39 : print_escaped_string (pp, start_exploc.file);
1191 : : /* For compatibility with clang, print as a half-open range. */
1192 : 39 : location_t next_loc = hint->get_next_loc ();
1193 : 39 : expanded_location next_exploc = expand_location (next_loc);
1194 : 39 : int start_col
1195 : 39 : = convert_column_unit (fc, column_unit, tabstop, start_exploc);
1196 : 39 : int next_col
1197 : 39 : = convert_column_unit (fc, column_unit, tabstop, next_exploc);
1198 : 39 : pp_printf (pp, ":{%i:%i-%i:%i}:",
1199 : : start_exploc.line, start_col,
1200 : : next_exploc.line, next_col);
1201 : 39 : print_escaped_string (pp, hint->get_string ());
1202 : 39 : pp_newline (pp);
1203 : : }
1204 : :
1205 : 39 : pp_set_prefix (pp, saved_prefix);
1206 : 39 : }
1207 : :
1208 : : /* Update the inlining info in this context for a DIAGNOSTIC. */
1209 : :
1210 : : void
1211 : 94794884 : context::get_any_inlining_info (diagnostic_info *diagnostic)
1212 : : {
1213 : 94794884 : auto &ilocs = diagnostic->m_iinfo.m_ilocs;
1214 : :
1215 : 94794884 : if (m_set_locations_cb)
1216 : : /* Retrieve the locations into which the expression about to be
1217 : : diagnosed has been inlined, including those of all the callers
1218 : : all the way down the inlining stack. */
1219 : 94793231 : m_set_locations_cb (*this, diagnostic);
1220 : : else
1221 : : {
1222 : : /* When there's no callback use just the one location provided
1223 : : by the caller of the diagnostic function. */
1224 : 1653 : location_t loc = diagnostic_location (diagnostic);
1225 : 1653 : ilocs.safe_push (loc);
1226 : 1653 : diagnostic->m_iinfo.m_allsyslocs = in_system_header_at (loc);
1227 : : }
1228 : 94794884 : }
1229 : :
1230 : : /* Generate a URL string describing CWE. The caller is responsible for
1231 : : freeing the string. */
1232 : :
1233 : : char *
1234 : 28 : get_cwe_url (int cwe)
1235 : : {
1236 : 28 : return xasprintf ("https://cwe.mitre.org/data/definitions/%i.html", cwe);
1237 : : }
1238 : :
1239 : : /* Returns whether a DIAGNOSTIC should be printed, and adjusts diagnostic->kind
1240 : : as appropriate for #pragma GCC diagnostic and -Werror=foo. */
1241 : :
1242 : : bool
1243 : 94794884 : context::diagnostic_enabled (diagnostic_info *diagnostic)
1244 : : {
1245 : : /* Update the inlining stack for this diagnostic. */
1246 : 94794884 : get_any_inlining_info (diagnostic);
1247 : :
1248 : : /* Diagnostics with no option or -fpermissive are always enabled. */
1249 : 94794884 : if (!diagnostic->m_option_id.m_idx
1250 : 94794884 : || diagnostic->m_option_id == m_opt_permissive)
1251 : : return true;
1252 : :
1253 : : /* This tests if the user provided the appropriate -Wfoo or
1254 : : -Wno-foo option. */
1255 : 93342511 : if (!option_enabled_p (diagnostic->m_option_id))
1256 : : return false;
1257 : :
1258 : : /* This tests for #pragma diagnostic changes. */
1259 : 2578633 : enum kind diag_class
1260 : 2578633 : = m_option_classifier.update_effective_level_from_pragmas (diagnostic);
1261 : :
1262 : : /* This tests if the user provided the appropriate -Werror=foo
1263 : : option. */
1264 : 2578633 : if (diag_class == kind::unspecified
1265 : 3584135 : && !option_unspecified_p (diagnostic->m_option_id))
1266 : : {
1267 : 8840 : const enum kind new_kind
1268 : 8840 : = m_option_classifier.get_current_override (diagnostic->m_option_id);
1269 : 8840 : if (new_kind != kind::any)
1270 : : /* kind::any means the diagnostic is not to be ignored, but we don't want
1271 : : to change it specifically to kind::error or kind::warning; we want to
1272 : : preserve whatever the caller has specified. */
1273 : 5712 : diagnostic->m_kind = new_kind;
1274 : : }
1275 : :
1276 : : /* This allows for future extensions, like temporarily disabling
1277 : : warnings for ranges of source code. */
1278 : 2578633 : if (diagnostic->m_kind == kind::ignored)
1279 : : return false;
1280 : :
1281 : : return true;
1282 : : }
1283 : :
1284 : : /* Returns whether warning OPT_ID is enabled at LOC. */
1285 : :
1286 : : bool
1287 : 831817 : context::warning_enabled_at (location_t loc,
1288 : : option_id opt_id)
1289 : : {
1290 : 1653246 : if (!diagnostic_report_warnings_p (this, loc))
1291 : 102365 : return false;
1292 : :
1293 : 729452 : rich_location richloc (line_table, loc);
1294 : 729452 : diagnostic_info diagnostic = {};
1295 : 729452 : diagnostic.m_option_id = opt_id;
1296 : 729452 : diagnostic.m_richloc = &richloc;
1297 : 729452 : diagnostic.m_message.m_richloc = &richloc;
1298 : 729452 : diagnostic.m_kind = kind::warning;
1299 : 729452 : return diagnostic_enabled (&diagnostic);
1300 : 729452 : }
1301 : :
1302 : : /* Emit a diagnostic within a diagnostic group on this context. */
1303 : :
1304 : : bool
1305 : 8 : context::emit_diagnostic_with_group (enum kind kind,
1306 : : rich_location &richloc,
1307 : : const metadata *metadata,
1308 : : option_id opt_id,
1309 : : const char *gmsgid, ...)
1310 : : {
1311 : 8 : begin_group ();
1312 : :
1313 : 8 : va_list ap;
1314 : 8 : va_start (ap, gmsgid);
1315 : 8 : bool ret = emit_diagnostic_with_group_va (kind, richloc, metadata, opt_id,
1316 : : gmsgid, &ap);
1317 : 8 : va_end (ap);
1318 : :
1319 : 8 : end_group ();
1320 : :
1321 : 8 : return ret;
1322 : : }
1323 : :
1324 : : /* As above, but taking a va_list *. */
1325 : :
1326 : : bool
1327 : 8 : context::emit_diagnostic_with_group_va (enum kind kind,
1328 : : rich_location &richloc,
1329 : : const metadata *metadata,
1330 : : option_id opt_id,
1331 : : const char *gmsgid, va_list *ap)
1332 : : {
1333 : 8 : begin_group ();
1334 : :
1335 : 8 : bool ret = diagnostic_impl (&richloc, metadata, opt_id,
1336 : : gmsgid, ap, kind);
1337 : :
1338 : 8 : end_group ();
1339 : :
1340 : 8 : return ret;
1341 : : }
1342 : :
1343 : : /* Report a diagnostic message (an error or a warning) as specified by
1344 : : this diagnostics::context.
1345 : : front-end independent format specifiers are exactly those described
1346 : : in the documentation of output_format.
1347 : : Return true if a diagnostic was printed, false otherwise. */
1348 : :
1349 : : bool
1350 : 97075747 : context::report_diagnostic (diagnostic_info *diagnostic)
1351 : : {
1352 : 97075747 : auto logger = get_logger ();
1353 : 97075747 : DIAGNOSTICS_LOG_SCOPE_PRINTF0 (logger, "diagnostics::context::report_diagnostic");
1354 : :
1355 : 97075747 : enum kind orig_diag_kind = diagnostic->m_kind;
1356 : :
1357 : : /* Every call to report_diagnostic should be within a
1358 : : begin_group/end_group pair so that output formats can reliably
1359 : : flush diagnostics with on_end_group when the topmost group is ended. */
1360 : 97075747 : gcc_assert (m_diagnostic_groups.m_group_nesting_depth > 0);
1361 : :
1362 : : /* Give preference to being able to inhibit warnings, before they
1363 : : get reclassified to something else. */
1364 : 97075747 : bool was_warning = (diagnostic->m_kind == kind::warning
1365 : 97075747 : || diagnostic->m_kind == kind::pedwarn);
1366 : 97075747 : if (was_warning && m_inhibit_warnings)
1367 : : {
1368 : 3010315 : inhibit_notes_in_group ();
1369 : 3010315 : if (m_logger)
1370 : 0 : m_logger->log_printf ("rejecting: inhibiting warnings");
1371 : 3010315 : return false;
1372 : : }
1373 : :
1374 : 94065432 : if (m_adjust_diagnostic_info)
1375 : 88523923 : m_adjust_diagnostic_info (*this, diagnostic);
1376 : :
1377 : 94065432 : if (diagnostic->m_kind == kind::pedwarn)
1378 : : {
1379 : 498597 : diagnostic->m_kind = m_pedantic_errors ? kind::error : kind::warning;
1380 : :
1381 : : /* We do this to avoid giving the message for -pedantic-errors. */
1382 : 498597 : orig_diag_kind = diagnostic->m_kind;
1383 : : }
1384 : :
1385 : 94065432 : if (diagnostic->m_kind == kind::note && m_inhibit_notes_p)
1386 : : {
1387 : 0 : if (m_logger)
1388 : 0 : m_logger->log_printf ("rejecting: inhibiting notes");
1389 : 0 : return false;
1390 : : }
1391 : :
1392 : : /* If the user requested that warnings be treated as errors, so be
1393 : : it. Note that we do this before the next block so that
1394 : : individual warnings can be overridden back to warnings with
1395 : : -Wno-error=*. */
1396 : 94065432 : if (m_warning_as_error_requested
1397 : 233487 : && diagnostic->m_kind == kind::warning)
1398 : 198254 : diagnostic->m_kind = kind::error;
1399 : :
1400 : 94065432 : diagnostic->m_message.m_data = &diagnostic->m_x_data;
1401 : :
1402 : : /* Check to see if the diagnostic is enabled at the location and
1403 : : not disabled by #pragma GCC diagnostic anywhere along the inlining
1404 : : stack. . */
1405 : 94065432 : if (!diagnostic_enabled (diagnostic))
1406 : : {
1407 : 92309644 : if (m_logger)
1408 : 0 : m_logger->log_printf ("rejecting: diagnostic not enabled");
1409 : 92309644 : inhibit_notes_in_group ();
1410 : 92309644 : return false;
1411 : : }
1412 : :
1413 : 1755788 : if ((was_warning || diagnostic->m_kind == kind::warning)
1414 : 329769 : && ((!m_warn_system_headers
1415 : 329435 : && diagnostic->m_iinfo.m_allsyslocs)
1416 : 115773 : || m_inhibit_warnings))
1417 : : {
1418 : : /* Bail if the warning is not to be reported because all locations in the
1419 : : inlining stack (if there is one) are in system headers. */
1420 : 214011 : if (m_logger)
1421 : 0 : m_logger->log_printf ("rejecting: warning in system header");
1422 : 214011 : return false;
1423 : : }
1424 : :
1425 : 1541777 : if (diagnostic->m_kind == kind::note && notes_inhibited_in_group ())
1426 : : {
1427 : : /* Bail for all the notes in the diagnostic_group that started to inhibit notes. */
1428 : 17 : if (m_logger)
1429 : 0 : m_logger->log_printf ("rejecting: notes inhibited within group");
1430 : 17 : return false;
1431 : : }
1432 : :
1433 : 1541760 : if (diagnostic->m_kind != kind::note && diagnostic->m_kind != kind::ice)
1434 : 1430085 : check_max_errors (false);
1435 : :
1436 : 1541756 : if (m_lock > 0)
1437 : : {
1438 : : /* If we're reporting an ICE in the middle of some other error,
1439 : : try to flush out the previous error, then let this one
1440 : : through. Don't do this more than once. */
1441 : 0 : if ((diagnostic->m_kind == kind::ice
1442 : 0 : || diagnostic->m_kind == kind::ice_nobt)
1443 : 0 : && m_lock == 1)
1444 : 0 : pp_newline_and_flush (m_reference_printer);
1445 : : else
1446 : 0 : error_recursion ();
1447 : : }
1448 : :
1449 : : /* We are accepting the diagnostic, so should stop inhibiting notes. */
1450 : 1541756 : inhibit_notes_in_group (/*inhibit=*/false);
1451 : :
1452 : 1541756 : m_lock++;
1453 : :
1454 : 1541756 : if (diagnostic->m_kind == kind::ice || diagnostic->m_kind == kind::ice_nobt)
1455 : : {
1456 : : /* When not checking, ICEs are converted to fatal errors when an
1457 : : error has already occurred. This is counteracted by
1458 : : abort_on_error. */
1459 : 21 : if (!CHECKING_P
1460 : : && (diagnostic_count (kind::error) > 0
1461 : : || diagnostic_count (kind::sorry) > 0)
1462 : : && !m_abort_on_error)
1463 : : {
1464 : : expanded_location s
1465 : : = expand_location (diagnostic_location (diagnostic));
1466 : : fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1467 : : s.file, s.line);
1468 : : exit (ICE_EXIT_CODE);
1469 : : }
1470 : 21 : if (m_internal_error)
1471 : 21 : (*m_internal_error) (this,
1472 : : diagnostic->m_message.m_format_spec,
1473 : : diagnostic->m_message.m_args_ptr);
1474 : : }
1475 : :
1476 : : /* Increment the counter for the appropriate diagnostic kind, either
1477 : : within this context, or within the diagnostic_buffer. */
1478 : 1541756 : {
1479 : 1541443 : const enum kind kind_for_count =
1480 : 1319922 : ((diagnostic->m_kind == kind::error && orig_diag_kind == kind::warning)
1481 : 1541756 : ? kind::werror
1482 : : : diagnostic->m_kind);
1483 : 1541756 : counters &cs
1484 : 1541756 : = (m_diagnostic_buffer
1485 : : ? m_diagnostic_buffer->m_diagnostic_counters
1486 : : : m_diagnostic_counters);
1487 : 1541756 : ++cs.m_count_for_kind[static_cast<size_t> (kind_for_count)];
1488 : : }
1489 : :
1490 : : /* Is this the initial diagnostic within the stack of groups? */
1491 : 1541756 : if (m_diagnostic_groups.m_emission_count == 0)
1492 : : {
1493 : 1443488 : DIAGNOSTICS_LOG_SCOPE_PRINTF0
1494 : : (get_logger (),
1495 : 1443488 : "diagnostics::context: beginning group");
1496 : 5774048 : for (auto sink_ : m_sinks)
1497 : 1443584 : sink_->on_begin_group ();
1498 : 1541756 : }
1499 : 1541756 : m_diagnostic_groups.m_emission_count++;
1500 : :
1501 : 1541756 : va_list *orig_args = diagnostic->m_message.m_args_ptr;
1502 : 6167252 : for (auto sink_ : m_sinks)
1503 : : {
1504 : : /* Formatting the message is done per-output-format,
1505 : : so that each output format gets its own set of pp_token_lists
1506 : : to work with.
1507 : :
1508 : : Run phases 1 and 2 of formatting the message before calling
1509 : : the format's on_report_diagnostic.
1510 : : In particular, some format codes may have side-effects here which
1511 : : need to happen before sending the diagnostic to the output format.
1512 : : For example, Fortran's %C and %L formatting codes populate the
1513 : : rich_location.
1514 : : Such side-effects must be idempotent, since they are run per
1515 : : output-format.
1516 : :
1517 : : Make a duplicate of the varargs for each call to pp_format,
1518 : : so that each has its own set to consume. */
1519 : 1541985 : va_list copied_args;
1520 : 1541985 : va_copy (copied_args, *orig_args);
1521 : 1541985 : diagnostic->m_message.m_args_ptr = &copied_args;
1522 : 1541985 : pp_format (sink_->get_printer (), &diagnostic->m_message);
1523 : 1541985 : va_end (copied_args);
1524 : :
1525 : : /* Call vfunc in the output format. This is responsible for
1526 : : phase 3 of formatting, and for printing the result. */
1527 : 1541985 : sink_->on_report_diagnostic (*diagnostic, orig_diag_kind);
1528 : : }
1529 : :
1530 : 1541755 : const int tabstop = get_column_options ().m_tabstop;
1531 : :
1532 : 1541755 : switch (m_extra_output_kind)
1533 : : {
1534 : : default:
1535 : : break;
1536 : 15 : case EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1:
1537 : 15 : print_parseable_fixits (get_file_cache (),
1538 : : m_reference_printer, diagnostic->m_richloc,
1539 : : DIAGNOSTICS_COLUMN_UNIT_BYTE,
1540 : : tabstop);
1541 : 15 : pp_flush (m_reference_printer);
1542 : 15 : break;
1543 : 6 : case EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2:
1544 : 6 : print_parseable_fixits (get_file_cache (),
1545 : : m_reference_printer, diagnostic->m_richloc,
1546 : : DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
1547 : : tabstop);
1548 : 6 : pp_flush (m_reference_printer);
1549 : 6 : break;
1550 : : }
1551 : 1541755 : if (m_diagnostic_buffer == nullptr
1552 : 1197261 : || diagnostic->m_kind == kind::ice
1553 : 1197261 : || diagnostic->m_kind == kind::ice_nobt)
1554 : 344494 : action_after_output (diagnostic->m_kind);
1555 : 1541046 : diagnostic->m_x_data = nullptr;
1556 : :
1557 : 1541046 : if (m_fixits_change_set)
1558 : 60 : if (diagnostic->m_richloc->fixits_can_be_auto_applied_p ())
1559 : 58 : if (!m_diagnostic_buffer)
1560 : 58 : m_fixits_change_set->add_fixits (diagnostic->m_richloc);
1561 : :
1562 : 1541046 : m_lock--;
1563 : :
1564 : 1541046 : if (!m_diagnostic_buffer)
1565 : 1375363 : for (auto sink_ : m_sinks)
1566 : 344008 : sink_->after_diagnostic (*diagnostic);
1567 : :
1568 : : return true;
1569 : 97075033 : }
1570 : :
1571 : : void
1572 : 0 : context::report_verbatim (text_info &text)
1573 : : {
1574 : 0 : va_list *orig_args = text.m_args_ptr;
1575 : 0 : for (auto sink_ : m_sinks)
1576 : : {
1577 : 0 : va_list copied_args;
1578 : 0 : va_copy (copied_args, *orig_args);
1579 : 0 : text.m_args_ptr = &copied_args;
1580 : 0 : sink_->on_report_verbatim (text);
1581 : 0 : va_end (copied_args);
1582 : : }
1583 : 0 : }
1584 : :
1585 : : void
1586 : 3 : context::report_global_digraph (const lazily_created<digraphs::digraph> &ldg)
1587 : : {
1588 : 14 : for (auto sink_ : m_sinks)
1589 : 5 : sink_->report_global_digraph (ldg);
1590 : 3 : }
1591 : :
1592 : : /* Get the number of digits in the decimal representation of VALUE. */
1593 : :
1594 : : int
1595 : 74600 : num_digits (uint64_t value)
1596 : : {
1597 : : /* Perhaps simpler to use log10 for this, but doing it this way avoids
1598 : : using floating point. */
1599 : :
1600 : 74600 : if (value == 0)
1601 : : return 1;
1602 : :
1603 : : int digits = 0;
1604 : 235620 : while (value > 0)
1605 : : {
1606 : 161061 : digits++;
1607 : 161061 : value /= 10;
1608 : : }
1609 : : return digits;
1610 : : }
1611 : :
1612 : : } // namespace diagnostics
1613 : :
1614 : : /* Given a partial pathname as input, return another pathname that
1615 : : shares no directory elements with the pathname of __FILE__. This
1616 : : is used by fancy_abort() to print `internal compiler error in expr.cc'
1617 : : instead of `internal compiler error in ../../GCC/gcc/expr.cc'. */
1618 : :
1619 : : const char *
1620 : 7 : trim_filename (const char *name)
1621 : : {
1622 : 7 : static const char this_file[] = __FILE__;
1623 : 7 : const char *p = name, *q = this_file;
1624 : :
1625 : : /* First skip any "../" in each filename. This allows us to give a proper
1626 : : reference to a file in a subdirectory. */
1627 : 7 : while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
1628 : 0 : p += 3;
1629 : :
1630 : : while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
1631 : : q += 3;
1632 : :
1633 : : /* Now skip any parts the two filenames have in common. */
1634 : 329 : while (*p == *q && *p != 0 && *q != 0)
1635 : 322 : p++, q++;
1636 : :
1637 : : /* Now go backwards until the previous directory separator. */
1638 : 7 : while (p > name && !IS_DIR_SEPARATOR (p[-1]))
1639 : 0 : p--;
1640 : :
1641 : 7 : return p;
1642 : : }
1643 : :
1644 : : namespace diagnostics {
1645 : :
1646 : : /* Implement emit_diagnostic, inform, warning, warning_at, pedwarn,
1647 : : permerror, error, error_at, error_at, sorry, fatal_error, internal_error,
1648 : : and internal_error_no_backtrace, as documented and defined below. */
1649 : : bool
1650 : 92016865 : context::diagnostic_impl (rich_location *richloc,
1651 : : const metadata *metadata,
1652 : : option_id opt_id,
1653 : : const char *gmsgid,
1654 : : va_list *ap, enum kind kind)
1655 : : {
1656 : 92016865 : logging::log_function_params
1657 : 92016865 : (m_logger, "diagnostics::context::diagnostic_impl")
1658 : 92016865 : .log_param_option_id ("option_id", opt_id)
1659 : 92016865 : .log_param_kind ("kind", kind)
1660 : 92016865 : .log_param_string ("gmsgid", gmsgid);
1661 : 92016865 : logging::auto_inc_depth depth_sentinel (m_logger);
1662 : :
1663 : 92016865 : diagnostic_info diagnostic;
1664 : 92016865 : if (kind == diagnostics::kind::permerror)
1665 : : {
1666 : 18334 : diagnostic_set_info (&diagnostic, gmsgid, ap, richloc,
1667 : 18334 : (m_permissive
1668 : : ? diagnostics::kind::warning
1669 : : : diagnostics::kind::error));
1670 : 18334 : diagnostic.m_option_id = (opt_id.m_idx != -1 ? opt_id : m_opt_permissive);
1671 : : }
1672 : : else
1673 : : {
1674 : 91998531 : diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind);
1675 : 91998531 : if (kind == diagnostics::kind::warning
1676 : 91998531 : || kind == diagnostics::kind::pedwarn)
1677 : 91782747 : diagnostic.m_option_id = opt_id;
1678 : : }
1679 : 92016865 : diagnostic.m_metadata = metadata;
1680 : :
1681 : 92016865 : bool ret = report_diagnostic (&diagnostic);
1682 : 92016524 : if (m_logger)
1683 : 0 : m_logger->log_bool_return ("diagnostics::context::diagnostic_impl", ret);
1684 : 92016524 : return ret;
1685 : 92016524 : }
1686 : :
1687 : : /* Implement inform_n, warning_n, and error_n, as documented and
1688 : : defined below. */
1689 : : bool
1690 : 11949 : context::diagnostic_n_impl (rich_location *richloc,
1691 : : const metadata *metadata,
1692 : : option_id opt_id,
1693 : : unsigned HOST_WIDE_INT n,
1694 : : const char *singular_gmsgid,
1695 : : const char *plural_gmsgid,
1696 : : va_list *ap, enum kind kind)
1697 : : {
1698 : 11949 : logging::log_function_params
1699 : 11949 : (m_logger, "diagnostics::context::diagnostic_n_impl")
1700 : 11949 : .log_param_option_id ("option_id", opt_id)
1701 : 11949 : .log_param_kind ("kind", kind)
1702 : 11949 : .log_params_n_gmsgids (n, singular_gmsgid, plural_gmsgid);
1703 : 11949 : logging::auto_inc_depth depth_sentinel (m_logger);
1704 : :
1705 : 11949 : diagnostic_info diagnostic;
1706 : 11949 : unsigned long gtn;
1707 : :
1708 : 11949 : if (sizeof n <= sizeof gtn)
1709 : 11949 : gtn = n;
1710 : : else
1711 : : /* Use the largest number ngettext can handle, otherwise
1712 : : preserve the six least significant decimal digits for
1713 : : languages where the plural form depends on them. */
1714 : : gtn = n <= ULONG_MAX ? n : n % 1000000LU + 1000000LU;
1715 : :
1716 : 11949 : const char *text = ngettext (singular_gmsgid, plural_gmsgid, gtn);
1717 : 11949 : diagnostic_set_info_translated (&diagnostic, text, ap, richloc, kind);
1718 : 11949 : if (kind == diagnostics::kind::warning)
1719 : 4473 : diagnostic.m_option_id = opt_id;
1720 : 11949 : diagnostic.m_metadata = metadata;
1721 : :
1722 : 11949 : bool ret = report_diagnostic (&diagnostic);
1723 : 11949 : if (m_logger)
1724 : 0 : m_logger->log_bool_return ("diagnostics::context::diagnostic_n_impl", ret);
1725 : 11949 : return ret;
1726 : 11949 : }
1727 : :
1728 : :
1729 : : /* Emit DIAGRAM to this context, respecting the output format. */
1730 : :
1731 : : void
1732 : 86 : context::emit_diagram (const diagram &diag)
1733 : : {
1734 : 86 : if (m_diagrams.m_theme == nullptr)
1735 : : return;
1736 : :
1737 : 336 : for (auto sink_ : m_sinks)
1738 : 84 : sink_->on_diagram (diag);
1739 : : }
1740 : :
1741 : : /* Inform the user that an error occurred while trying to report some
1742 : : other error. This indicates catastrophic internal inconsistencies,
1743 : : so give up now. But do try to flush out the previous error.
1744 : : This mustn't use internal_error, that will cause infinite recursion. */
1745 : :
1746 : : void
1747 : 0 : context::error_recursion ()
1748 : : {
1749 : 0 : if (m_lock < 3)
1750 : 0 : pp_newline_and_flush (m_reference_printer);
1751 : :
1752 : 0 : fnotice (stderr,
1753 : : "internal compiler error: error reporting routines re-entered.\n");
1754 : :
1755 : : /* Call action_after_output to get the "please submit a bug report"
1756 : : message. */
1757 : 0 : action_after_output (kind::ice);
1758 : :
1759 : : /* Do not use gcc_unreachable here; that goes through internal_error
1760 : : and therefore would cause infinite recursion. */
1761 : 0 : real_abort ();
1762 : : }
1763 : :
1764 : : } // namespace diagnostics
1765 : :
1766 : : /* Report an internal compiler error in a friendly manner. This is
1767 : : the function that gets called upon use of abort() in the source
1768 : : code generally, thanks to a special macro. */
1769 : :
1770 : : void
1771 : 7 : fancy_abort (const char *file, int line, const char *function)
1772 : : {
1773 : : /* If fancy_abort is called before the diagnostic subsystem is initialized,
1774 : : internal_error will crash internally in a way that prevents a
1775 : : useful message reaching the user.
1776 : : This can happen with libgccjit in the case of gcc_assert failures
1777 : : that occur outside of the libgccjit mutex that guards the rest of
1778 : : gcc's state, including global_dc (when global_dc may not be
1779 : : initialized yet, or might be in use by another thread).
1780 : : Handle such cases as gracefully as possible by falling back to a
1781 : : minimal abort handler that only relies on i18n. */
1782 : 7 : if (global_dc->get_reference_printer () == nullptr)
1783 : : {
1784 : : /* Print the error message. */
1785 : 0 : fnotice (stderr, diagnostics::get_text_for_kind (diagnostics::kind::ice));
1786 : 0 : fnotice (stderr, "in %s, at %s:%d", function, trim_filename (file), line);
1787 : 0 : fputc ('\n', stderr);
1788 : :
1789 : : /* Attempt to print a backtrace. */
1790 : 0 : struct backtrace_state *state
1791 : 0 : = backtrace_create_state (nullptr, 0, bt_err_callback, nullptr);
1792 : 0 : int count = 0;
1793 : 0 : if (state != nullptr)
1794 : 0 : backtrace_full (state, 2, bt_callback, bt_err_callback,
1795 : : (void *) &count);
1796 : :
1797 : : /* We can't call warn_if_plugins or emergency_dump_function as these
1798 : : rely on GCC state that might not be initialized, or might be in
1799 : : use by another thread. */
1800 : :
1801 : : /* Abort the process. */
1802 : 0 : real_abort ();
1803 : : }
1804 : :
1805 : 7 : internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1806 : : }
1807 : :
1808 : : namespace diagnostics {
1809 : :
1810 : : /* class diagnostics::context. */
1811 : :
1812 : : void
1813 : 857514899 : context::begin_group ()
1814 : : {
1815 : 857514899 : m_diagnostic_groups.m_group_nesting_depth++;
1816 : 857514899 : }
1817 : :
1818 : : void
1819 : 857514894 : context::end_group ()
1820 : : {
1821 : 857514894 : if (--m_diagnostic_groups.m_group_nesting_depth == 0)
1822 : : {
1823 : : /* Handle the case where we've popped the final diagnostic group.
1824 : : If any diagnostics were emitted, give the context a chance
1825 : : to do something. */
1826 : 855441135 : if (m_diagnostic_groups.m_emission_count > 0)
1827 : : {
1828 : 1443487 : DIAGNOSTICS_LOG_SCOPE_PRINTF0
1829 : : (get_logger (),
1830 : 1443487 : "diagnostics::context::end_group: ending group");
1831 : 5774044 : for (auto sink_ : m_sinks)
1832 : 1443583 : sink_->on_end_group ();
1833 : 855441135 : }
1834 : 855441135 : m_diagnostic_groups.m_emission_count = 0;
1835 : : }
1836 : : /* We're popping one level, so might need to stop inhibiting notes. */
1837 : 857514894 : inhibit_notes_in_group (/*inhibit=*/false);
1838 : 857514894 : }
1839 : :
1840 : : void
1841 : 29276 : context::push_nesting_level ()
1842 : : {
1843 : 29276 : ++m_diagnostic_groups.m_diagnostic_nesting_level;
1844 : 29276 : }
1845 : :
1846 : : void
1847 : 29274 : context::pop_nesting_level ()
1848 : : {
1849 : 29274 : --m_diagnostic_groups.m_diagnostic_nesting_level;
1850 : : /* We're popping one level, so might need to stop inhibiting notes. */
1851 : 29274 : inhibit_notes_in_group (/*inhibit=*/false);
1852 : 29274 : }
1853 : :
1854 : : void
1855 : 0 : context::set_nesting_level (int new_level)
1856 : : {
1857 : 0 : m_diagnostic_groups.m_diagnostic_nesting_level = new_level;
1858 : 0 : }
1859 : :
1860 : : void
1861 : 0 : sink::dump (FILE *out, int indent) const
1862 : : {
1863 : 0 : dumping::emit_heading (out, indent, "printer");
1864 : 0 : m_printer->dump (out, indent + 2);
1865 : :
1866 : 0 : dumping::emit_heading (out, indent, "extensions");
1867 : 0 : if (m_extensions.empty ())
1868 : 0 : dumping::emit_none (out, indent + 2);
1869 : : else
1870 : 0 : for (auto &ext : m_extensions)
1871 : 0 : ext->dump (out, indent + 2);
1872 : 0 : }
1873 : :
1874 : : void
1875 : 308444 : sink::finalize_extensions ()
1876 : : {
1877 : 308444 : for (auto &ext : m_extensions)
1878 : 0 : ext->finalize ();
1879 : 308444 : }
1880 : :
1881 : : void
1882 : 0 : sink::on_report_verbatim (text_info &)
1883 : : {
1884 : : /* No-op. */
1885 : 0 : }
1886 : :
1887 : : /* Set the output format for DC to FORMAT, using BASE_FILE_NAME for
1888 : : file-based output formats. */
1889 : :
1890 : : void
1891 : 90 : output_format_init (context &dc,
1892 : : const char *main_input_filename_,
1893 : : const char *base_file_name,
1894 : : enum diagnostics_output_format format,
1895 : : bool json_formatting)
1896 : : {
1897 : 90 : sink *new_sink = nullptr;
1898 : 90 : switch (format)
1899 : : {
1900 : 0 : default:
1901 : 0 : gcc_unreachable ();
1902 : : case DIAGNOSTICS_OUTPUT_FORMAT_TEXT:
1903 : : /* The default; do nothing. */
1904 : : break;
1905 : :
1906 : 0 : case DIAGNOSTICS_OUTPUT_FORMAT_SARIF_STDERR:
1907 : 0 : new_sink = &init_sarif_stderr (dc,
1908 : : line_table,
1909 : : json_formatting);
1910 : 0 : break;
1911 : :
1912 : 90 : case DIAGNOSTICS_OUTPUT_FORMAT_SARIF_FILE:
1913 : 90 : new_sink = &init_sarif_file (dc,
1914 : : line_table,
1915 : : json_formatting,
1916 : : base_file_name);
1917 : 90 : break;
1918 : : }
1919 : 90 : if (new_sink)
1920 : 90 : new_sink->set_main_input_filename (main_input_filename_);
1921 : 90 : }
1922 : :
1923 : : /* Initialize this context's m_diagrams based on CHARSET.
1924 : : Specifically, make a text_art::theme object for m_diagrams.m_theme,
1925 : : (or nullptr for "no diagrams"). */
1926 : :
1927 : : void
1928 : 1604955 : context::set_text_art_charset (enum diagnostic_text_art_charset charset)
1929 : : {
1930 : 1604955 : delete m_diagrams.m_theme;
1931 : 1604955 : switch (charset)
1932 : : {
1933 : 0 : default:
1934 : 0 : gcc_unreachable ();
1935 : :
1936 : 888173 : case DIAGNOSTICS_TEXT_ART_CHARSET_NONE:
1937 : 888173 : m_diagrams.m_theme = nullptr;
1938 : 888173 : break;
1939 : :
1940 : 630429 : case DIAGNOSTICS_TEXT_ART_CHARSET_ASCII:
1941 : 630429 : m_diagrams.m_theme = new text_art::ascii_theme ();
1942 : 630429 : break;
1943 : :
1944 : 295 : case DIAGNOSTICS_TEXT_ART_CHARSET_UNICODE:
1945 : 295 : m_diagrams.m_theme = new text_art::unicode_theme ();
1946 : 295 : break;
1947 : :
1948 : 86058 : case DIAGNOSTICS_TEXT_ART_CHARSET_EMOJI:
1949 : 86058 : m_diagrams.m_theme = new text_art::emoji_theme ();
1950 : 86058 : break;
1951 : : }
1952 : 1604955 : }
1953 : :
1954 : : /* struct diagnostics::counters. */
1955 : :
1956 : 9142639 : counters::counters ()
1957 : : {
1958 : 9142639 : clear ();
1959 : 9142639 : }
1960 : :
1961 : : void
1962 : 0 : counters::dump (FILE *out, int indent) const
1963 : : {
1964 : 0 : dumping::emit_heading (out, indent, "counts");
1965 : 0 : bool none = true;
1966 : 0 : for (int i = 0; i < static_cast<int> (kind::last_diagnostic_kind); i++)
1967 : 0 : if (m_count_for_kind[i] > 0)
1968 : : {
1969 : 0 : dumping::emit_indent (out, indent + 2);
1970 : 0 : fprintf (out, "%s%i\n",
1971 : : get_text_for_kind (static_cast<enum kind> (i)),
1972 : 0 : m_count_for_kind[i]);
1973 : 0 : none = false;
1974 : : }
1975 : 0 : if (none)
1976 : 0 : dumping::emit_none (out, indent + 2);
1977 : 0 : }
1978 : :
1979 : : void
1980 : 111046 : counters::move_to (counters &dest)
1981 : : {
1982 : 1776736 : for (int i = 0; i < static_cast<int> (kind::last_diagnostic_kind); i++)
1983 : 1665690 : dest.m_count_for_kind[i] += m_count_for_kind[i];
1984 : 111046 : clear ();
1985 : 111046 : }
1986 : :
1987 : : void
1988 : 32918461 : counters::clear ()
1989 : : {
1990 : 32918461 : memset (&m_count_for_kind, 0, sizeof m_count_for_kind);
1991 : 32918461 : }
1992 : :
1993 : : #if CHECKING_P
1994 : :
1995 : : namespace selftest {
1996 : :
1997 : : using line_table_test = ::selftest::line_table_test;
1998 : : using temp_source_file = ::selftest::temp_source_file;
1999 : :
2000 : : /* Helper function for test_print_escaped_string. */
2001 : :
2002 : : static void
2003 : 24 : assert_print_escaped_string (const ::selftest::location &loc,
2004 : : const char *expected_output,
2005 : : const char *input)
2006 : : {
2007 : 24 : pretty_printer pp;
2008 : 24 : print_escaped_string (&pp, input);
2009 : 24 : ASSERT_STREQ_AT (loc, expected_output, pp_formatted_text (&pp));
2010 : 24 : }
2011 : :
2012 : : #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
2013 : : assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
2014 : :
2015 : : /* Tests of print_escaped_string. */
2016 : :
2017 : : static void
2018 : 3 : test_print_escaped_string ()
2019 : : {
2020 : : /* Empty string. */
2021 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
2022 : :
2023 : : /* Non-empty string. */
2024 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
2025 : :
2026 : : /* Various things that need to be escaped: */
2027 : : /* Backslash. */
2028 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
2029 : : "before\\after");
2030 : : /* Tab. */
2031 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
2032 : : "before\tafter");
2033 : : /* Newline. */
2034 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
2035 : : "before\nafter");
2036 : : /* Double quote. */
2037 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
2038 : : "before\"after");
2039 : :
2040 : : /* Non-printable characters: BEL: '\a': 0x07 */
2041 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
2042 : : "before\aafter");
2043 : : /* Non-printable characters: vertical tab: '\v': 0x0b */
2044 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
2045 : : "before\vafter");
2046 : 3 : }
2047 : :
2048 : : /* Tests of print_parseable_fixits. */
2049 : :
2050 : : /* Verify that print_parseable_fixits emits the empty string if there
2051 : : are no fixits. */
2052 : :
2053 : : static void
2054 : 3 : test_print_parseable_fixits_none ()
2055 : : {
2056 : 3 : pretty_printer pp;
2057 : 3 : file_cache fc;
2058 : 3 : rich_location richloc (line_table, UNKNOWN_LOCATION);
2059 : :
2060 : 3 : print_parseable_fixits (fc, &pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
2061 : 3 : ASSERT_STREQ ("", pp_formatted_text (&pp));
2062 : 3 : }
2063 : :
2064 : : /* Verify that print_parseable_fixits does the right thing if there
2065 : : is an insertion fixit hint. */
2066 : :
2067 : : static void
2068 : 3 : test_print_parseable_fixits_insert ()
2069 : : {
2070 : 3 : pretty_printer pp;
2071 : 3 : file_cache fc;
2072 : 3 : rich_location richloc (line_table, UNKNOWN_LOCATION);
2073 : :
2074 : 3 : linemap_add (line_table, LC_ENTER, false, "test.c", 0);
2075 : 3 : linemap_line_start (line_table, 5, 100);
2076 : 3 : linemap_add (line_table, LC_LEAVE, false, nullptr, 0);
2077 : 3 : location_t where = linemap_position_for_column (line_table, 10);
2078 : 3 : richloc.add_fixit_insert_before (where, "added content");
2079 : :
2080 : 3 : print_parseable_fixits (fc, &pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
2081 : 3 : ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
2082 : : pp_formatted_text (&pp));
2083 : 3 : }
2084 : :
2085 : : /* Verify that print_parseable_fixits does the right thing if there
2086 : : is an removal fixit hint. */
2087 : :
2088 : : static void
2089 : 3 : test_print_parseable_fixits_remove ()
2090 : : {
2091 : 3 : pretty_printer pp;
2092 : 3 : file_cache fc;
2093 : 3 : rich_location richloc (line_table, UNKNOWN_LOCATION);
2094 : :
2095 : 3 : linemap_add (line_table, LC_ENTER, false, "test.c", 0);
2096 : 3 : linemap_line_start (line_table, 5, 100);
2097 : 3 : linemap_add (line_table, LC_LEAVE, false, nullptr, 0);
2098 : 3 : source_range where;
2099 : 3 : where.m_start = linemap_position_for_column (line_table, 10);
2100 : 3 : where.m_finish = linemap_position_for_column (line_table, 20);
2101 : 3 : richloc.add_fixit_remove (where);
2102 : :
2103 : 3 : print_parseable_fixits (fc, &pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
2104 : 3 : ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
2105 : : pp_formatted_text (&pp));
2106 : 3 : }
2107 : :
2108 : : /* Verify that print_parseable_fixits does the right thing if there
2109 : : is an replacement fixit hint. */
2110 : :
2111 : : static void
2112 : 3 : test_print_parseable_fixits_replace ()
2113 : : {
2114 : 3 : pretty_printer pp;
2115 : 3 : file_cache fc;
2116 : 3 : rich_location richloc (line_table, UNKNOWN_LOCATION);
2117 : :
2118 : 3 : linemap_add (line_table, LC_ENTER, false, "test.c", 0);
2119 : 3 : linemap_line_start (line_table, 5, 100);
2120 : 3 : linemap_add (line_table, LC_LEAVE, false, nullptr, 0);
2121 : 3 : source_range where;
2122 : 3 : where.m_start = linemap_position_for_column (line_table, 10);
2123 : 3 : where.m_finish = linemap_position_for_column (line_table, 20);
2124 : 3 : richloc.add_fixit_replace (where, "replacement");
2125 : :
2126 : 3 : print_parseable_fixits (fc, &pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
2127 : 3 : ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
2128 : : pp_formatted_text (&pp));
2129 : 3 : }
2130 : :
2131 : : /* Verify that print_parseable_fixits correctly handles
2132 : : DIAGNOSTICS_COLUMN_UNIT_BYTE vs DIAGNOSTICS_COLUMN_UNIT_COLUMN. */
2133 : :
2134 : : static void
2135 : 3 : test_print_parseable_fixits_bytes_vs_display_columns ()
2136 : : {
2137 : 3 : line_table_test ltt;
2138 : 3 : rich_location richloc (line_table, UNKNOWN_LOCATION);
2139 : :
2140 : : /* 1-based byte offsets: 12345677778888999900001234567. */
2141 : 3 : const char *const content = "smile \xf0\x9f\x98\x82 colour\n";
2142 : : /* 1-based display cols: 123456[......7-8.....]9012345. */
2143 : 3 : const int tabstop = 8;
2144 : :
2145 : 3 : temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
2146 : 3 : file_cache fc;
2147 : 3 : const char *const fname = tmp.get_filename ();
2148 : :
2149 : 3 : linemap_add (line_table, LC_ENTER, false, fname, 0);
2150 : 3 : linemap_line_start (line_table, 1, 100);
2151 : 3 : linemap_add (line_table, LC_LEAVE, false, nullptr, 0);
2152 : 3 : source_range where;
2153 : 3 : where.m_start = linemap_position_for_column (line_table, 12);
2154 : 3 : where.m_finish = linemap_position_for_column (line_table, 17);
2155 : 3 : richloc.add_fixit_replace (where, "color");
2156 : :
2157 : : /* Escape fname. */
2158 : 3 : pretty_printer tmp_pp;
2159 : 3 : print_escaped_string (&tmp_pp, fname);
2160 : 3 : char *escaped_fname = xstrdup (pp_formatted_text (&tmp_pp));
2161 : :
2162 : 3 : const int buf_len = strlen (escaped_fname) + 100;
2163 : 3 : char *const expected = XNEWVEC (char, buf_len);
2164 : :
2165 : 3 : {
2166 : 3 : pretty_printer pp;
2167 : 3 : print_parseable_fixits (fc, &pp, &richloc,
2168 : : DIAGNOSTICS_COLUMN_UNIT_BYTE,
2169 : : tabstop);
2170 : 3 : snprintf (expected, buf_len,
2171 : : "fix-it:%s:{1:12-1:18}:\"color\"\n", escaped_fname);
2172 : 3 : ASSERT_STREQ (expected, pp_formatted_text (&pp));
2173 : 3 : }
2174 : 3 : {
2175 : 3 : pretty_printer pp;
2176 : 3 : print_parseable_fixits (fc, &pp, &richloc,
2177 : : DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
2178 : : tabstop);
2179 : 3 : snprintf (expected, buf_len,
2180 : : "fix-it:%s:{1:10-1:16}:\"color\"\n", escaped_fname);
2181 : 3 : ASSERT_STREQ (expected, pp_formatted_text (&pp));
2182 : 3 : }
2183 : :
2184 : 3 : XDELETEVEC (expected);
2185 : 3 : free (escaped_fname);
2186 : 3 : }
2187 : :
2188 : : /* Verify that
2189 : : diagnostics::column_policy::get_location_text (..., SHOW_COLUMN, ...)
2190 : : generates EXPECTED_LOC_TEXT, given FILENAME, LINE, COLUMN, with
2191 : : colorization disabled. */
2192 : :
2193 : : static void
2194 : 42 : assert_location_text (const char *expected_loc_text,
2195 : : const char *filename, int line, int column,
2196 : : bool show_column,
2197 : : int origin = 1,
2198 : : enum diagnostics_column_unit column_unit
2199 : : = DIAGNOSTICS_COLUMN_UNIT_BYTE)
2200 : : {
2201 : 42 : diagnostics::selftest::test_context dc;
2202 : 42 : dc.get_column_options ().m_column_unit = column_unit;
2203 : 42 : dc.get_column_options ().m_column_origin = origin;
2204 : :
2205 : 42 : expanded_location xloc;
2206 : 42 : xloc.file = filename;
2207 : 42 : xloc.line = line;
2208 : 42 : xloc.column = column;
2209 : 42 : xloc.data = nullptr;
2210 : 42 : xloc.sysp = false;
2211 : :
2212 : 42 : diagnostics::column_policy column_policy (dc);
2213 : 42 : label_text actual_loc_text
2214 : 42 : = column_policy.get_location_text (xloc, show_column, false);
2215 : 42 : ASSERT_STREQ (expected_loc_text, actual_loc_text.get ());
2216 : 42 : }
2217 : :
2218 : : /* Verify that get_location_text works as expected. */
2219 : :
2220 : : static void
2221 : 3 : test_get_location_text ()
2222 : : {
2223 : 3 : const char *old_progname = progname;
2224 : 3 : progname = "PROGNAME";
2225 : 3 : assert_location_text ("PROGNAME:", nullptr, 0, 0, true);
2226 : 3 : char *built_in_colon = concat (special_fname_builtin (), ":", (char *) 0);
2227 : 3 : assert_location_text (built_in_colon, special_fname_builtin (),
2228 : : 42, 10, true);
2229 : 3 : free (built_in_colon);
2230 : 3 : assert_location_text ("foo.c:42:10:", "foo.c", 42, 10, true);
2231 : 3 : assert_location_text ("foo.c:42:9:", "foo.c", 42, 10, true, 0);
2232 : 3 : assert_location_text ("foo.c:42:1010:", "foo.c", 42, 10, true, 1001);
2233 : 9 : for (int origin = 0; origin != 2; ++origin)
2234 : 6 : assert_location_text ("foo.c:42:", "foo.c", 42, 0, true, origin);
2235 : 3 : assert_location_text ("foo.c:", "foo.c", 0, 10, true);
2236 : 3 : assert_location_text ("foo.c:42:", "foo.c", 42, 10, false);
2237 : 3 : assert_location_text ("foo.c:", "foo.c", 0, 10, false);
2238 : :
2239 : 3 : diagnostics::text_sink::maybe_line_and_column (INT_MAX, INT_MAX);
2240 : 3 : diagnostics::text_sink::maybe_line_and_column (INT_MIN, INT_MIN);
2241 : :
2242 : 3 : {
2243 : : /* In order to test display columns vs byte columns, we need to create a
2244 : : file for location_get_source_line() to read. */
2245 : :
2246 : 3 : const char *const content = "smile \xf0\x9f\x98\x82\n";
2247 : 3 : const int line_bytes = strlen (content) - 1;
2248 : 3 : const int def_tabstop = 8;
2249 : 3 : const cpp_char_column_policy policy (def_tabstop, cpp_wcwidth);
2250 : 3 : const int display_width = cpp_display_width (content, line_bytes, policy);
2251 : 3 : ASSERT_EQ (line_bytes - 2, display_width);
2252 : 3 : temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
2253 : 3 : const char *const fname = tmp.get_filename ();
2254 : 3 : const int buf_len = strlen (fname) + 16;
2255 : 3 : char *const expected = XNEWVEC (char, buf_len);
2256 : :
2257 : 3 : snprintf (expected, buf_len, "%s:1:%d:", fname, line_bytes);
2258 : 3 : assert_location_text (expected, fname, 1, line_bytes, true,
2259 : : 1, DIAGNOSTICS_COLUMN_UNIT_BYTE);
2260 : :
2261 : 3 : snprintf (expected, buf_len, "%s:1:%d:", fname, line_bytes - 1);
2262 : 3 : assert_location_text (expected, fname, 1, line_bytes, true,
2263 : : 0, DIAGNOSTICS_COLUMN_UNIT_BYTE);
2264 : :
2265 : 3 : snprintf (expected, buf_len, "%s:1:%d:", fname, display_width);
2266 : 3 : assert_location_text (expected, fname, 1, line_bytes, true,
2267 : : 1, DIAGNOSTICS_COLUMN_UNIT_DISPLAY);
2268 : :
2269 : 3 : snprintf (expected, buf_len, "%s:1:%d:", fname, display_width - 1);
2270 : 3 : assert_location_text (expected, fname, 1, line_bytes, true,
2271 : : 0, DIAGNOSTICS_COLUMN_UNIT_DISPLAY);
2272 : :
2273 : 3 : XDELETEVEC (expected);
2274 : 3 : }
2275 : :
2276 : :
2277 : 3 : progname = old_progname;
2278 : 3 : }
2279 : :
2280 : : /* Selftest for num_digits. */
2281 : :
2282 : : static void
2283 : 3 : test_num_digits ()
2284 : : {
2285 : 3 : ASSERT_EQ (1, num_digits (0));
2286 : 3 : ASSERT_EQ (1, num_digits (9));
2287 : 3 : ASSERT_EQ (2, num_digits (10));
2288 : 3 : ASSERT_EQ (2, num_digits (99));
2289 : 3 : ASSERT_EQ (3, num_digits (100));
2290 : 3 : ASSERT_EQ (3, num_digits (999));
2291 : 3 : ASSERT_EQ (4, num_digits (1000));
2292 : 3 : ASSERT_EQ (4, num_digits (9999));
2293 : 3 : ASSERT_EQ (5, num_digits (10000));
2294 : 3 : ASSERT_EQ (5, num_digits (99999));
2295 : 3 : ASSERT_EQ (6, num_digits (100000));
2296 : 3 : ASSERT_EQ (6, num_digits (999999));
2297 : 3 : ASSERT_EQ (7, num_digits (1000000));
2298 : 3 : ASSERT_EQ (7, num_digits (9999999));
2299 : 3 : ASSERT_EQ (8, num_digits (10000000));
2300 : 3 : ASSERT_EQ (8, num_digits (99999999));
2301 : 3 : ASSERT_EQ (20, num_digits (uint64_t (-1)));
2302 : 3 : }
2303 : :
2304 : : /* Run all of the selftests within this file.
2305 : :
2306 : : According to https://gcc.gnu.org/pipermail/gcc/2021-November/237703.html
2307 : : there are some language-specific assumptions within these tests, so only
2308 : : run them from C/C++. */
2309 : :
2310 : : void
2311 : 3 : context_cc_tests ()
2312 : : {
2313 : 3 : test_print_escaped_string ();
2314 : 3 : test_print_parseable_fixits_none ();
2315 : 3 : test_print_parseable_fixits_insert ();
2316 : 3 : test_print_parseable_fixits_remove ();
2317 : 3 : test_print_parseable_fixits_replace ();
2318 : 3 : test_print_parseable_fixits_bytes_vs_display_columns ();
2319 : 3 : test_get_location_text ();
2320 : 3 : test_num_digits ();
2321 : 3 : }
2322 : :
2323 : : } // namespace diagnostics::selftest
2324 : :
2325 : : #endif /* #if CHECKING_P */
2326 : :
2327 : : } // namespace diagnostics
2328 : :
2329 : : #if __GNUC__ >= 10
2330 : : # pragma GCC diagnostic pop
2331 : : #endif
2332 : :
2333 : : static void real_abort (void) ATTRIBUTE_NORETURN;
2334 : :
2335 : : /* Really call the system 'abort'. This has to go right at the end of
2336 : : this file, so that there are no functions after it that call abort
2337 : : and get the system abort instead of our macro. */
2338 : : #undef abort
2339 : : static void
2340 : 0 : real_abort (void)
2341 : : {
2342 : 0 : abort ();
2343 : : }
|