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