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 : 3142548 : build_message_string (const char *msg, ...)
82 : : {
83 : 3142548 : char *str;
84 : 3142548 : va_list ap;
85 : :
86 : 3142548 : va_start (ap, msg);
87 : 3142548 : str = xvasprintf (msg, ap);
88 : 3142548 : va_end (ap);
89 : :
90 : 3142548 : 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 : 773808 : get_terminal_width (void)
99 : : {
100 : 773808 : const char * s = getenv ("COLUMNS");
101 : 773808 : if (s != nullptr) {
102 : 126 : int n = atoi (s);
103 : 126 : if (n > 0)
104 : : return n;
105 : : }
106 : :
107 : : #ifdef TIOCGWINSZ
108 : 773682 : struct winsize w;
109 : 773682 : w.ws_col = 0;
110 : 773682 : 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 : 820103 : context::set_caret_max_width (int value)
123 : : {
124 : : /* One minus to account for the leading empty space. */
125 : 1593814 : value = value ? value - 1
126 : 820103 : : (isatty (fileno (pp_buffer (get_reference_printer ())->m_stream))
127 : 820103 : ? get_terminal_width () - 1 : INT_MAX);
128 : :
129 : 773711 : if (value <= 0)
130 : 46392 : value = INT_MAX;
131 : :
132 : 820103 : m_source_printing.max_width = value;
133 : 820103 : }
134 : :
135 : : /* Initialize the diagnostic message outputting machinery. */
136 : :
137 : : void
138 : 716984 : 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 : 716984 : m_reference_printer = std::make_unique<pretty_printer> ().release ();
143 : :
144 : 716984 : m_file_cache = new file_cache ();
145 : 716984 : m_diagnostic_counters.clear ();
146 : 716984 : m_warning_as_error_requested = false;
147 : 716984 : m_n_opts = n_opts;
148 : 716984 : m_option_classifier.init (n_opts);
149 : 716984 : m_source_printing.enabled = false;
150 : 716984 : set_caret_max_width (pp_line_cutoff (get_reference_printer ()));
151 : 2867936 : for (int i = 0; i < rich_location::STATICALLY_ALLOCATED_RANGES; i++)
152 : 2150952 : m_source_printing.caret_chars[i] = '^';
153 : 716984 : m_show_cwe = false;
154 : 716984 : m_show_rules = false;
155 : 716984 : m_path_format = DPF_NONE;
156 : 716984 : m_show_path_depths = false;
157 : 716984 : m_show_option_requested = false;
158 : 716984 : m_abort_on_error = false;
159 : 716984 : m_show_column = false;
160 : 716984 : m_pedantic_errors = false;
161 : 716984 : m_permissive = false;
162 : 716984 : m_opt_permissive = 0;
163 : 716984 : m_fatal_errors = false;
164 : 716984 : m_inhibit_warnings = false;
165 : 716984 : m_warn_system_headers = false;
166 : 716984 : m_max_errors = 0;
167 : 716984 : m_internal_error = nullptr;
168 : 716984 : m_adjust_diagnostic_info = nullptr;
169 : 716984 : m_text_callbacks.m_begin_diagnostic = default_text_starter;
170 : 716984 : m_text_callbacks.m_text_start_span
171 : 716984 : = default_start_span_fn<to_text>;
172 : 716984 : m_text_callbacks.m_html_start_span
173 : 716984 : = default_start_span_fn<to_html>;
174 : 716984 : m_text_callbacks.m_end_diagnostic = default_text_finalizer;
175 : 716984 : m_option_id_mgr = nullptr;
176 : 716984 : m_urlifier_stack = new auto_vec<urlifier_stack_node> ();
177 : 716984 : m_last_location = UNKNOWN_LOCATION;
178 : 716984 : m_client_aux_data = nullptr;
179 : 716984 : m_lock = 0;
180 : 716984 : m_inhibit_notes_p = false;
181 : :
182 : 716984 : m_source_printing.colorize_source_p = false;
183 : 716984 : m_source_printing.show_labels_p = false;
184 : 716984 : m_source_printing.show_line_numbers_p = false;
185 : 716984 : m_source_printing.min_margin_width = 0;
186 : 716984 : m_source_printing.show_ruler_p = false;
187 : 716984 : m_source_printing.show_event_links_p = false;
188 : :
189 : 716984 : m_column_options.m_column_unit = DIAGNOSTICS_COLUMN_UNIT_DISPLAY;
190 : 716984 : m_column_options.m_column_origin = 1;
191 : 716984 : m_column_options.m_tabstop = 8;
192 : :
193 : 716984 : m_report_bug = false;
194 : 716984 : m_extra_output_kind = EXTRA_DIAGNOSTIC_OUTPUT_none;
195 : 716984 : 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 : 716984 : m_escape_format = DIAGNOSTICS_ESCAPE_FORMAT_UNICODE;
204 : 716984 : m_fixits_change_set = nullptr;
205 : 716984 : m_diagnostic_groups.m_group_nesting_depth = 0;
206 : 716984 : m_diagnostic_groups.m_diagnostic_nesting_level = 0;
207 : 716984 : m_diagnostic_groups.m_emission_count = 0;
208 : 716984 : m_diagnostic_groups.m_inhibiting_notes_from = 0;
209 : 716984 : m_sinks.safe_push (new text_sink (*this, nullptr, true));
210 : 716984 : m_set_locations_cb = nullptr;
211 : 716984 : m_client_data_hooks = nullptr;
212 : 716984 : m_diagrams.m_theme = nullptr;
213 : 716984 : m_original_argv = nullptr;
214 : 716984 : m_diagnostic_buffer = nullptr;
215 : 716984 : m_logger = nullptr;
216 : :
217 : 716984 : enum diagnostic_text_art_charset text_art_charset
218 : : = DIAGNOSTICS_TEXT_ART_CHARSET_EMOJI;
219 : 716984 : if (const char *lang = getenv ("LANG"))
220 : : {
221 : : /* For LANG=C, don't assume the terminal supports anything
222 : : other than ASCII. */
223 : 714704 : if (!strcmp (lang, "C"))
224 : 716984 : text_art_charset = DIAGNOSTICS_TEXT_ART_CHARSET_ASCII;
225 : : }
226 : 716984 : set_text_art_charset (text_art_charset);
227 : :
228 : 716984 : 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 : 716984 : if (m_logger)
247 : 0 : m_logger->log_printf ("diagnostics::context::initialize");
248 : 716984 : }
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 : 1425542 : context::color_init (int value)
256 : : {
257 : : /* value == -1 is the default value. */
258 : 1425542 : 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 : 602738 : if (DIAGNOSTICS_COLOR_DEFAULT == -1)
266 : : {
267 : : if (!getenv ("GCC_COLORS"))
268 : : return;
269 : : value = DIAGNOSTICS_COLOR_AUTO;
270 : : }
271 : : else
272 : 602738 : value = DIAGNOSTICS_COLOR_DEFAULT;
273 : : }
274 : 2851084 : pp_show_color (m_reference_printer)
275 : 1425542 : = colorize_init ((diagnostic_color_rule_t) value);
276 : 5702168 : for (auto sink_ : m_sinks)
277 : 1425542 : if (sink_->follows_reference_printer_p ())
278 : 1425542 : pp_show_color (sink_->get_printer ())
279 : 1425542 : = 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 : 1413145 : context::urls_init (int value)
287 : : {
288 : : /* value == -1 is the default value. */
289 : 1413145 : 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 : 602738 : 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 : 602738 : value = DIAGNOSTICS_URLS_DEFAULT;
304 : : }
305 : :
306 : 1413145 : m_reference_printer->set_url_format
307 : 1413145 : (determine_url_format ((diagnostic_url_rule_t) value));
308 : 5652580 : for (auto sink_ : m_sinks)
309 : 1413145 : if (sink_->follows_reference_printer_p ())
310 : 1413145 : sink_->get_printer ()->set_url_format
311 : 1413145 : (m_reference_printer->get_url_format ());
312 : : }
313 : :
314 : : void
315 : 558974 : context::set_show_nesting (bool val)
316 : : {
317 : 2235896 : for (auto sink_ : m_sinks)
318 : 558974 : if (sink_->follows_reference_printer_p ())
319 : 558974 : if (auto text_sink_ = sink_->dyn_cast_text_sink ())
320 : 558974 : text_sink_->set_show_nesting (val);
321 : 558974 : }
322 : :
323 : : void
324 : 289080 : context::set_show_nesting_locations (bool val)
325 : : {
326 : 1156320 : for (auto sink_ : m_sinks)
327 : 289080 : if (sink_->follows_reference_printer_p ())
328 : 289080 : if (auto text_sink_ = sink_->dyn_cast_text_sink ())
329 : 289080 : text_sink_->set_show_locations_in_nesting (val);
330 : 289080 : }
331 : :
332 : : void
333 : 289080 : context::set_show_nesting_levels (bool val)
334 : : {
335 : 1156320 : for (auto sink_ : m_sinks)
336 : 289080 : if (sink_->follows_reference_printer_p ())
337 : 289080 : if (auto text_sink_ = sink_->dyn_cast_text_sink ())
338 : 289080 : text_sink_->set_show_nesting_levels (val);
339 : 289080 : }
340 : :
341 : : /* Create the file_cache, if not already created, and tell it how to
342 : : translate files on input. */
343 : : void
344 : 210883 : context::initialize_input_context (diagnostic_input_charset_callback ccb,
345 : : bool should_skip_bom)
346 : : {
347 : 210883 : m_file_cache->initialize_input_context (ccb, should_skip_bom);
348 : 210883 : }
349 : :
350 : : /* Do any cleaning up required after the last diagnostic is emitted. */
351 : :
352 : : void
353 : 308181 : context::finish ()
354 : : {
355 : 308181 : 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 : 1232747 : for (auto iter : m_sinks)
365 : 308204 : 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 : 308935 : while (m_diagnostic_groups.m_group_nesting_depth > 0)
371 : 754 : end_group ();
372 : :
373 : 308181 : set_diagnostic_buffer (nullptr);
374 : :
375 : : /* Clean ups. */
376 : :
377 : 924566 : while (!m_sinks.is_empty ())
378 : 308204 : delete m_sinks.pop ();
379 : :
380 : 308181 : if (m_diagrams.m_theme)
381 : : {
382 : 39871 : delete m_diagrams.m_theme;
383 : 39871 : m_diagrams.m_theme = nullptr;
384 : : }
385 : :
386 : 308181 : delete m_file_cache;
387 : 308181 : m_file_cache = nullptr;
388 : :
389 : 308181 : m_option_classifier.fini ();
390 : :
391 : 308181 : delete m_reference_printer;
392 : 308181 : m_reference_printer = nullptr;
393 : :
394 : 308181 : if (m_fixits_change_set)
395 : : {
396 : 17 : delete m_fixits_change_set;
397 : 17 : m_fixits_change_set = nullptr;
398 : : }
399 : :
400 : 308181 : if (m_client_data_hooks)
401 : : {
402 : 287559 : delete m_client_data_hooks;
403 : 287559 : m_client_data_hooks = nullptr;
404 : : }
405 : :
406 : 308181 : delete m_option_id_mgr;
407 : 308181 : m_option_id_mgr = nullptr;
408 : :
409 : 308181 : if (m_urlifier_stack)
410 : : {
411 : 597058 : while (!m_urlifier_stack->is_empty ())
412 : 288877 : pop_urlifier ();
413 : 308181 : delete m_urlifier_stack;
414 : 308181 : m_urlifier_stack = nullptr;
415 : : }
416 : :
417 : 308181 : freeargv (m_original_argv);
418 : 308181 : m_original_argv = nullptr;
419 : :
420 : 308181 : delete m_logger;
421 : 308181 : m_logger = nullptr;
422 : 308181 : }
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 : 287345 : context::execution_failed_p () const
473 : : {
474 : 287345 : return (diagnostic_count (kind::fatal)
475 : 287344 : || diagnostic_count (kind::error)
476 : 260435 : || diagnostic_count (kind::sorry)
477 : 547607 : || diagnostic_count (kind::werror));
478 : : }
479 : :
480 : : void
481 : 1560 : context::remove_all_output_sinks ()
482 : : {
483 : 4680 : while (!m_sinks.is_empty ())
484 : 1560 : delete m_sinks.pop ();
485 : 1560 : }
486 : :
487 : : void
488 : 1560 : context::set_sink (std::unique_ptr<sink> sink_)
489 : : {
490 : 1560 : DIAGNOSTICS_LOG_SCOPE_PRINTF0 (m_logger, "diagnostics::context::set_sink");
491 : 1560 : if (m_logger)
492 : 0 : sink_->dump (m_logger->get_stream (), m_logger->get_indent ());
493 : 1560 : remove_all_output_sinks ();
494 : 1560 : m_sinks.safe_push (sink_.release ());
495 : 1560 : }
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 : 23 : context::add_sink (std::unique_ptr<sink> sink_)
507 : : {
508 : 23 : DIAGNOSTICS_LOG_SCOPE_PRINTF0 (m_logger, "diagnostics::context::add_sink");
509 : 23 : if (m_logger)
510 : 0 : sink_->dump (m_logger->get_stream (), m_logger->get_indent ());
511 : 23 : m_sinks.safe_push (sink_.release ());
512 : 23 : }
513 : :
514 : : /* Return true if there are no machine-readable formats writing to stderr. */
515 : :
516 : : bool
517 : 18614 : context::supports_fnotice_on_stderr_p () const
518 : : {
519 : 74410 : for (auto sink_ : m_sinks)
520 : 18568 : 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 : 344256 : context::set_client_data_hooks (std::unique_ptr<client_data_hooks> hooks)
534 : : {
535 : 344256 : delete m_client_data_hooks;
536 : : /* Ideally the field would be a std::unique_ptr here. */
537 : 344256 : m_client_data_hooks = hooks.release ();
538 : 344256 : }
539 : :
540 : : void
541 : 289080 : 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 : 289080 : freeargv (m_original_argv);
548 : :
549 : : // Take ownership of the new value
550 : 289080 : m_original_argv = original_argv.release ();
551 : 289080 : }
552 : :
553 : : void
554 : 302343 : context::set_option_id_manager (std::unique_ptr<option_id_manager> mgr,
555 : : unsigned lang_mask)
556 : : {
557 : 302343 : delete m_option_id_mgr;
558 : 302343 : m_option_id_mgr = mgr.release ();
559 : 302343 : m_lang_mask = lang_mask;
560 : 302343 : }
561 : :
562 : : void
563 : 589487 : context::push_owned_urlifier (std::unique_ptr<urlifier> ptr)
564 : : {
565 : 589487 : gcc_assert (m_urlifier_stack);
566 : 589487 : const urlifier_stack_node node = { ptr.release (), true };
567 : 589487 : m_urlifier_stack->safe_push (node);
568 : 589487 : }
569 : :
570 : : void
571 : 1910736029 : context::push_borrowed_urlifier (const urlifier &loan)
572 : : {
573 : 1910736029 : gcc_assert (m_urlifier_stack);
574 : 1910736029 : const urlifier_stack_node node = { const_cast <urlifier *> (&loan), false };
575 : 1910736029 : m_urlifier_stack->safe_push (node);
576 : 1910736029 : }
577 : :
578 : : void
579 : 1911024905 : context::pop_urlifier ()
580 : : {
581 : 1911024905 : gcc_assert (m_urlifier_stack);
582 : 1911024905 : gcc_assert (m_urlifier_stack->length () > 0);
583 : :
584 : 1911024905 : const urlifier_stack_node node = m_urlifier_stack->pop ();
585 : 1911024905 : if (node.m_owned)
586 : 288876 : delete node.m_urlifier;
587 : 1911024905 : }
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 : 1538316 : context::get_urlifier () const
599 : : {
600 : 1538316 : if (!m_urlifier_stack)
601 : : return nullptr;
602 : 1538316 : if (m_urlifier_stack->is_empty ())
603 : : return nullptr;
604 : 1537969 : 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 : 209847 : context::set_pretty_printer (std::unique_ptr<pretty_printer> pp)
613 : : {
614 : 209847 : delete m_reference_printer;
615 : 209847 : m_reference_printer = pp.release ();
616 : 209847 : refresh_output_sinks ();
617 : 209847 : }
618 : :
619 : : /* Give all output sinks a chance to rebuild their pretty_printer. */
620 : :
621 : : void
622 : 498927 : context::refresh_output_sinks ()
623 : : {
624 : 1995708 : for (auto sink_ : m_sinks)
625 : 498927 : sink_->update_printer ();
626 : 498927 : }
627 : :
628 : : /* Set FORMAT_DECODER on the reference printer and on the pretty_printer
629 : : of all output sinks. */
630 : :
631 : : void
632 : 584956 : context::set_format_decoder (printer_fn format_decoder)
633 : : {
634 : 584956 : pp_format_decoder (m_reference_printer) = format_decoder;
635 : 2339824 : for (auto sink_ : m_sinks)
636 : 584956 : pp_format_decoder (sink_->get_printer ()) = format_decoder;
637 : 584956 : }
638 : :
639 : : void
640 : 578165 : context::set_show_highlight_colors (bool val)
641 : : {
642 : 578165 : pp_show_highlight_colors (m_reference_printer) = val;
643 : 2312660 : for (auto sink_ : m_sinks)
644 : 578165 : if (sink_->follows_reference_printer_p ())
645 : 578165 : pp_show_highlight_colors (sink_->get_printer ()) = val;
646 : 578165 : }
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 : 96256381 : 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 : 96256381 : gcc_assert (richloc);
688 : 96256381 : diagnostic->m_message.m_err_no = errno;
689 : 96256381 : diagnostic->m_message.m_args_ptr = args;
690 : 96256381 : diagnostic->m_message.m_format_spec = msg;
691 : 96256381 : diagnostic->m_message.m_richloc = richloc;
692 : 96256381 : diagnostic->m_richloc = richloc;
693 : 96256381 : diagnostic->m_metadata = nullptr;
694 : 96256381 : diagnostic->m_kind = kind;
695 : 96256381 : diagnostic->m_option_id = 0;
696 : 96256381 : }
697 : :
698 : : /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
699 : : translated. */
700 : : void
701 : 96096992 : 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 : 96096992 : gcc_assert (richloc);
707 : 96096992 : diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
708 : 96096992 : }
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 : 343111 : get_text_for_kind (enum kind kind)
723 : : {
724 : 343111 : 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 : 1704899 : get_color_for_kind (enum kind kind)
752 : : {
753 : 1704899 : 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 : 314857 : convert_column_unit (file_cache &fc,
762 : : enum diagnostics_column_unit column_unit,
763 : : int tabstop,
764 : : expanded_location s)
765 : : {
766 : 314857 : if (s.column <= 0)
767 : : return -1;
768 : :
769 : 312063 : switch (column_unit)
770 : : {
771 : 0 : default:
772 : 0 : gcc_unreachable ();
773 : :
774 : 311927 : case DIAGNOSTICS_COLUMN_UNIT_DISPLAY:
775 : 311927 : {
776 : 311927 : cpp_char_column_policy policy (tabstop, cpp_wcwidth);
777 : 311927 : 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 : 314779 : column_options::convert_column (file_cache &fc,
790 : : expanded_location s) const
791 : : {
792 : 314779 : int one_based_col = convert_column_unit (fc, m_column_unit, m_tabstop, s);
793 : 314779 : if (one_based_col <= 0)
794 : : return -1;
795 : 311985 : return one_based_col + (m_column_origin - 1);
796 : : }
797 : :
798 : 2321632 : column_policy::column_policy (const context &dc)
799 : 2321632 : : m_file_cache (dc.get_file_cache ()),
800 : 2321632 : m_column_options (dc.get_column_options ())
801 : : {
802 : 2321632 : }
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 : 314779 : column_policy::converted_column (expanded_location s) const
809 : : {
810 : 314779 : 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 : 344469 : column_policy::get_location_text (const expanded_location &s,
817 : : bool show_column,
818 : : bool colorize) const
819 : : {
820 : 344469 : const char *locus_cs = colorize_start (colorize, "locus");
821 : 344469 : const char *locus_ce = colorize_stop (colorize);
822 : 344469 : const char *file = s.file ? s.file : progname;
823 : 344469 : int line = 0;
824 : 344469 : int col = -1;
825 : 344469 : if (strcmp (file, special_fname_builtin ()))
826 : : {
827 : 343677 : line = s.line;
828 : 343677 : if (show_column)
829 : 313685 : col = converted_column (s);
830 : : }
831 : :
832 : 344469 : const char *line_col = text_sink::maybe_line_and_column (line, col);
833 : 344469 : return label_text::take (build_message_string ("%s%s%s:%s", locus_cs, file,
834 : 344469 : line_col, locus_ce));
835 : : }
836 : :
837 : 50264 : location_print_policy::
838 : 50264 : location_print_policy (const context &dc)
839 : 50264 : : m_column_policy (dc),
840 : 50264 : m_show_column (dc.m_show_column)
841 : : {
842 : 50264 : }
843 : :
844 : 1208908 : location_print_policy::
845 : 1208908 : location_print_policy (const text_sink &text_output)
846 : : :
847 : 1208908 : m_column_policy (text_output.get_context ()),
848 : 1208908 : m_show_column (text_output.get_context ().m_show_column)
849 : : {
850 : 1208908 : }
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 : 1433934 : context::check_max_errors (bool flush)
955 : : {
956 : 1433934 : 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 : 351315 : context::action_after_output (enum kind diag_kind)
979 : : {
980 : 351315 : switch (diag_kind)
981 : : {
982 : : case kind::debug:
983 : : case kind::note:
984 : : case kind::anachronism:
985 : : case kind::warning:
986 : : break;
987 : :
988 : 142131 : case kind::error:
989 : 142131 : case kind::sorry:
990 : 142131 : if (m_abort_on_error)
991 : 0 : real_abort ();
992 : 142131 : 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 : 350606 : }
1050 : :
1051 : : /* State whether we should inhibit notes in the current diagnostic_group and
1052 : : its future children if any. */
1053 : :
1054 : : void
1055 : 948093913 : context::inhibit_notes_in_group (bool inhibit)
1056 : : {
1057 : 948093913 : int curr_depth = (m_diagnostic_groups.m_group_nesting_depth
1058 : 948093913 : + m_diagnostic_groups.m_diagnostic_nesting_level);
1059 : :
1060 : 948093913 : if (inhibit)
1061 : : {
1062 : : /* If we're already inhibiting, there's nothing to do. */
1063 : 94491510 : 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 : 94491448 : gcc_assert (m_diagnostic_groups.m_group_nesting_depth > 0);
1070 : 94491448 : m_diagnostic_groups.m_inhibiting_notes_from = curr_depth - 1;
1071 : : }
1072 : 853602403 : else if (m_diagnostic_groups.m_inhibiting_notes_from)
1073 : : {
1074 : : /* Only cancel inhibition at the depth that set it up. */
1075 : 3104270 : if (curr_depth >= m_diagnostic_groups.m_inhibiting_notes_from)
1076 : : return;
1077 : :
1078 : 1552091 : 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 : 111460 : context::notes_inhibited_in_group () const
1086 : : {
1087 : 111460 : 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 : 93645157 : context::get_any_inlining_info (diagnostic_info *diagnostic)
1212 : : {
1213 : 93645157 : auto &ilocs = diagnostic->m_iinfo.m_ilocs;
1214 : :
1215 : 93645157 : 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 : 93643522 : 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 : 1635 : location_t loc = diagnostic_location (diagnostic);
1225 : 1635 : ilocs.safe_push (loc);
1226 : 1635 : diagnostic->m_iinfo.m_allsyslocs = in_system_header_at (loc);
1227 : : }
1228 : 93645157 : }
1229 : :
1230 : : /* Generate a URL string describing CWE. The caller is responsible for
1231 : : freeing the string. */
1232 : :
1233 : : char *
1234 : 25 : get_cwe_url (int cwe)
1235 : : {
1236 : 25 : 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 : 93645157 : context::diagnostic_enabled (diagnostic_info *diagnostic)
1244 : : {
1245 : : /* Update the inlining stack for this diagnostic. */
1246 : 93645157 : get_any_inlining_info (diagnostic);
1247 : :
1248 : : /* Diagnostics with no option or -fpermissive are always enabled. */
1249 : 93645157 : if (!diagnostic->m_option_id.m_idx
1250 : 93645157 : || 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 : 92196320 : if (!option_enabled_p (diagnostic->m_option_id))
1256 : : return false;
1257 : :
1258 : : /* This tests for #pragma diagnostic changes. */
1259 : 2050481 : enum kind diag_class
1260 : 2050481 : = m_option_classifier.update_effective_level_from_pragmas (diagnostic);
1261 : :
1262 : : /* This tests if the user provided the appropriate -Werror=foo
1263 : : option. */
1264 : 2050481 : if (diag_class == kind::unspecified
1265 : 2534963 : && !option_unspecified_p (diagnostic->m_option_id))
1266 : : {
1267 : 8728 : const enum kind new_kind
1268 : 8728 : = m_option_classifier.get_current_override (diagnostic->m_option_id);
1269 : 8728 : 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 : 5576 : diagnostic->m_kind = new_kind;
1274 : : }
1275 : :
1276 : : /* This allows for future extensions, like temporarily disabling
1277 : : warnings for ranges of source code. */
1278 : 2050481 : 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 : 301295 : context::warning_enabled_at (location_t loc,
1288 : : option_id opt_id)
1289 : : {
1290 : 592622 : if (!diagnostic_report_warnings_p (this, loc))
1291 : 92901 : return false;
1292 : :
1293 : 208394 : rich_location richloc (line_table, loc);
1294 : 208394 : diagnostic_info diagnostic = {};
1295 : 208394 : diagnostic.m_option_id = opt_id;
1296 : 208394 : diagnostic.m_richloc = &richloc;
1297 : 208394 : diagnostic.m_message.m_richloc = &richloc;
1298 : 208394 : diagnostic.m_kind = kind::warning;
1299 : 208394 : return diagnostic_enabled (&diagnostic);
1300 : 208394 : }
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 : 96243662 : context::report_diagnostic (diagnostic_info *diagnostic)
1351 : : {
1352 : 96243662 : auto logger = get_logger ();
1353 : 96243662 : DIAGNOSTICS_LOG_SCOPE_PRINTF0 (logger, "diagnostics::context::report_diagnostic");
1354 : :
1355 : 96243662 : 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 : 96243662 : 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 : 96243662 : bool was_warning = (diagnostic->m_kind == kind::warning
1365 : 96243662 : || diagnostic->m_kind == kind::pedwarn);
1366 : 96243662 : if (was_warning && m_inhibit_warnings)
1367 : : {
1368 : 2806899 : inhibit_notes_in_group ();
1369 : 2806899 : if (m_logger)
1370 : 0 : m_logger->log_printf ("rejecting: inhibiting warnings");
1371 : 2806899 : return false;
1372 : : }
1373 : :
1374 : 93436763 : if (m_adjust_diagnostic_info)
1375 : 87935720 : m_adjust_diagnostic_info (*this, diagnostic);
1376 : :
1377 : 93436763 : if (diagnostic->m_kind == kind::pedwarn)
1378 : : {
1379 : 487895 : diagnostic->m_kind = m_pedantic_errors ? kind::error : kind::warning;
1380 : :
1381 : : /* We do this to avoid giving the message for -pedantic-errors. */
1382 : 487895 : orig_diag_kind = diagnostic->m_kind;
1383 : : }
1384 : :
1385 : 93436763 : 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 : 93436763 : if (m_warning_as_error_requested
1397 : 232323 : && diagnostic->m_kind == kind::warning)
1398 : 197090 : diagnostic->m_kind = kind::error;
1399 : :
1400 : 93436763 : 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 : 93436763 : if (!diagnostic_enabled (diagnostic))
1406 : : {
1407 : 91684611 : if (m_logger)
1408 : 0 : m_logger->log_printf ("rejecting: diagnostic not enabled");
1409 : 91684611 : inhibit_notes_in_group ();
1410 : 91684611 : return false;
1411 : : }
1412 : :
1413 : 1752152 : if ((was_warning || diagnostic->m_kind == kind::warning)
1414 : 329590 : && ((!m_warn_system_headers
1415 : 329256 : && diagnostic->m_iinfo.m_allsyslocs)
1416 : 115637 : || 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 : 213968 : if (m_logger)
1421 : 0 : m_logger->log_printf ("rejecting: warning in system header");
1422 : 213968 : return false;
1423 : : }
1424 : :
1425 : 1538184 : 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 : 1538167 : if (diagnostic->m_kind != kind::note && diagnostic->m_kind != kind::ice)
1434 : 1426704 : check_max_errors (false);
1435 : :
1436 : 1538163 : 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 : 1538163 : inhibit_notes_in_group (/*inhibit=*/false);
1451 : :
1452 : 1538163 : m_lock++;
1453 : :
1454 : 1538163 : 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 : 1538163 : {
1479 : 1537844 : const enum kind kind_for_count =
1480 : 1316629 : ((diagnostic->m_kind == kind::error && orig_diag_kind == kind::warning)
1481 : 1538163 : ? kind::werror
1482 : : : diagnostic->m_kind);
1483 : 1538163 : counters &cs
1484 : 1538163 : = (m_diagnostic_buffer
1485 : : ? m_diagnostic_buffer->m_diagnostic_counters
1486 : : : m_diagnostic_counters);
1487 : 1538163 : ++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 : 1538163 : if (m_diagnostic_groups.m_emission_count == 0)
1492 : : {
1493 : 1440110 : DIAGNOSTICS_LOG_SCOPE_PRINTF0
1494 : : (get_logger (),
1495 : 1440110 : "diagnostics::context: beginning group");
1496 : 5760523 : for (auto sink_ : m_sinks)
1497 : 1440193 : sink_->on_begin_group ();
1498 : 1538163 : }
1499 : 1538163 : m_diagnostic_groups.m_emission_count++;
1500 : :
1501 : 1538163 : va_list *orig_args = diagnostic->m_message.m_args_ptr;
1502 : 6152804 : 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 : 1538316 : va_list copied_args;
1520 : 1538316 : va_copy (copied_args, *orig_args);
1521 : 1538316 : diagnostic->m_message.m_args_ptr = &copied_args;
1522 : 1538316 : pp_format (sink_->get_printer (), &diagnostic->m_message);
1523 : 1538316 : 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 : 1538316 : sink_->on_report_diagnostic (*diagnostic, orig_diag_kind);
1528 : : }
1529 : :
1530 : 1538162 : const int tabstop = get_column_options ().m_tabstop;
1531 : :
1532 : 1538162 : 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 : 1538162 : if (m_diagnostic_buffer == nullptr
1552 : 1194077 : || diagnostic->m_kind == kind::ice
1553 : 1194077 : || diagnostic->m_kind == kind::ice_nobt)
1554 : 344085 : action_after_output (diagnostic->m_kind);
1555 : 1537453 : diagnostic->m_x_data = nullptr;
1556 : :
1557 : 1537453 : 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 : 1537453 : m_lock--;
1563 : :
1564 : 1537453 : if (!m_diagnostic_buffer)
1565 : 1373651 : for (auto sink_ : m_sinks)
1566 : 343523 : sink_->after_diagnostic (*diagnostic);
1567 : :
1568 : : return true;
1569 : 96242948 : }
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 : 74481 : num_digits (int value)
1596 : : {
1597 : : /* Perhaps simpler to use log10 for this, but doing it this way avoids
1598 : : using floating point. */
1599 : 74481 : gcc_assert (value >= 0);
1600 : :
1601 : 74481 : if (value == 0)
1602 : : return 1;
1603 : :
1604 : : int digits = 0;
1605 : 234433 : while (value > 0)
1606 : : {
1607 : 159993 : digits++;
1608 : 159993 : value /= 10;
1609 : : }
1610 : : return digits;
1611 : : }
1612 : :
1613 : : } // namespace diagnostics
1614 : :
1615 : : /* Given a partial pathname as input, return another pathname that
1616 : : shares no directory elements with the pathname of __FILE__. This
1617 : : is used by fancy_abort() to print `internal compiler error in expr.cc'
1618 : : instead of `internal compiler error in ../../GCC/gcc/expr.cc'. */
1619 : :
1620 : : const char *
1621 : 7 : trim_filename (const char *name)
1622 : : {
1623 : 7 : static const char this_file[] = __FILE__;
1624 : 7 : const char *p = name, *q = this_file;
1625 : :
1626 : : /* First skip any "../" in each filename. This allows us to give a proper
1627 : : reference to a file in a subdirectory. */
1628 : 7 : while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
1629 : 0 : p += 3;
1630 : :
1631 : : while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
1632 : : q += 3;
1633 : :
1634 : : /* Now skip any parts the two filenames have in common. */
1635 : 329 : while (*p == *q && *p != 0 && *q != 0)
1636 : 322 : p++, q++;
1637 : :
1638 : : /* Now go backwards until the previous directory separator. */
1639 : 7 : while (p > name && !IS_DIR_SEPARATOR (p[-1]))
1640 : 0 : p--;
1641 : :
1642 : 7 : return p;
1643 : : }
1644 : :
1645 : : namespace diagnostics {
1646 : :
1647 : : /* Implement emit_diagnostic, inform, warning, warning_at, pedwarn,
1648 : : permerror, error, error_at, error_at, sorry, fatal_error, internal_error,
1649 : : and internal_error_no_backtrace, as documented and defined below. */
1650 : : bool
1651 : 91225597 : context::diagnostic_impl (rich_location *richloc,
1652 : : const metadata *metadata,
1653 : : option_id opt_id,
1654 : : const char *gmsgid,
1655 : : va_list *ap, enum kind kind)
1656 : : {
1657 : 91225597 : logging::log_function_params
1658 : 91225597 : (m_logger, "diagnostics::context::diagnostic_impl")
1659 : 91225597 : .log_param_option_id ("option_id", opt_id)
1660 : 91225597 : .log_param_kind ("kind", kind)
1661 : 91225597 : .log_param_string ("gmsgid", gmsgid);
1662 : 91225597 : logging::auto_inc_depth depth_sentinel (m_logger);
1663 : :
1664 : 91225597 : diagnostic_info diagnostic;
1665 : 91225597 : if (kind == diagnostics::kind::permerror)
1666 : : {
1667 : 18348 : diagnostic_set_info (&diagnostic, gmsgid, ap, richloc,
1668 : 18348 : (m_permissive
1669 : : ? diagnostics::kind::warning
1670 : : : diagnostics::kind::error));
1671 : 18348 : diagnostic.m_option_id = (opt_id.m_idx != -1 ? opt_id : m_opt_permissive);
1672 : : }
1673 : : else
1674 : : {
1675 : 91207249 : diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind);
1676 : 91207249 : if (kind == diagnostics::kind::warning
1677 : 91207249 : || kind == diagnostics::kind::pedwarn)
1678 : 90991703 : diagnostic.m_option_id = opt_id;
1679 : : }
1680 : 91225597 : diagnostic.m_metadata = metadata;
1681 : :
1682 : 91225597 : bool ret = report_diagnostic (&diagnostic);
1683 : 91225253 : if (m_logger)
1684 : 0 : m_logger->log_bool_return ("diagnostics::context::diagnostic_impl", ret);
1685 : 91225253 : return ret;
1686 : 91225253 : }
1687 : :
1688 : : /* Implement inform_n, warning_n, and error_n, as documented and
1689 : : defined below. */
1690 : : bool
1691 : 11921 : context::diagnostic_n_impl (rich_location *richloc,
1692 : : const metadata *metadata,
1693 : : option_id opt_id,
1694 : : unsigned HOST_WIDE_INT n,
1695 : : const char *singular_gmsgid,
1696 : : const char *plural_gmsgid,
1697 : : va_list *ap, enum kind kind)
1698 : : {
1699 : 11921 : logging::log_function_params
1700 : 11921 : (m_logger, "diagnostics::context::diagnostic_n_impl")
1701 : 11921 : .log_param_option_id ("option_id", opt_id)
1702 : 11921 : .log_param_kind ("kind", kind)
1703 : 11921 : .log_params_n_gmsgids (n, singular_gmsgid, plural_gmsgid);
1704 : 11921 : logging::auto_inc_depth depth_sentinel (m_logger);
1705 : :
1706 : 11921 : diagnostic_info diagnostic;
1707 : 11921 : unsigned long gtn;
1708 : :
1709 : 11921 : if (sizeof n <= sizeof gtn)
1710 : 11921 : gtn = n;
1711 : : else
1712 : : /* Use the largest number ngettext can handle, otherwise
1713 : : preserve the six least significant decimal digits for
1714 : : languages where the plural form depends on them. */
1715 : : gtn = n <= ULONG_MAX ? n : n % 1000000LU + 1000000LU;
1716 : :
1717 : 11921 : const char *text = ngettext (singular_gmsgid, plural_gmsgid, gtn);
1718 : 11921 : diagnostic_set_info_translated (&diagnostic, text, ap, richloc, kind);
1719 : 11921 : if (kind == diagnostics::kind::warning)
1720 : 4473 : diagnostic.m_option_id = opt_id;
1721 : 11921 : diagnostic.m_metadata = metadata;
1722 : :
1723 : 11921 : bool ret = report_diagnostic (&diagnostic);
1724 : 11921 : if (m_logger)
1725 : 0 : m_logger->log_bool_return ("diagnostics::context::diagnostic_n_impl", ret);
1726 : 11921 : return ret;
1727 : 11921 : }
1728 : :
1729 : :
1730 : : /* Emit DIAGRAM to this context, respecting the output format. */
1731 : :
1732 : : void
1733 : 86 : context::emit_diagram (const diagram &diag)
1734 : : {
1735 : 86 : if (m_diagrams.m_theme == nullptr)
1736 : : return;
1737 : :
1738 : 336 : for (auto sink_ : m_sinks)
1739 : 84 : sink_->on_diagram (diag);
1740 : : }
1741 : :
1742 : : /* Inform the user that an error occurred while trying to report some
1743 : : other error. This indicates catastrophic internal inconsistencies,
1744 : : so give up now. But do try to flush out the previous error.
1745 : : This mustn't use internal_error, that will cause infinite recursion. */
1746 : :
1747 : : void
1748 : 0 : context::error_recursion ()
1749 : : {
1750 : 0 : if (m_lock < 3)
1751 : 0 : pp_newline_and_flush (m_reference_printer);
1752 : :
1753 : 0 : fnotice (stderr,
1754 : : "internal compiler error: error reporting routines re-entered.\n");
1755 : :
1756 : : /* Call action_after_output to get the "please submit a bug report"
1757 : : message. */
1758 : 0 : action_after_output (kind::ice);
1759 : :
1760 : : /* Do not use gcc_unreachable here; that goes through internal_error
1761 : : and therefore would cause infinite recursion. */
1762 : 0 : real_abort ();
1763 : : }
1764 : :
1765 : : } // namespace diagnostics
1766 : :
1767 : : /* Report an internal compiler error in a friendly manner. This is
1768 : : the function that gets called upon use of abort() in the source
1769 : : code generally, thanks to a special macro. */
1770 : :
1771 : : void
1772 : 7 : fancy_abort (const char *file, int line, const char *function)
1773 : : {
1774 : : /* If fancy_abort is called before the diagnostic subsystem is initialized,
1775 : : internal_error will crash internally in a way that prevents a
1776 : : useful message reaching the user.
1777 : : This can happen with libgccjit in the case of gcc_assert failures
1778 : : that occur outside of the libgccjit mutex that guards the rest of
1779 : : gcc's state, including global_dc (when global_dc may not be
1780 : : initialized yet, or might be in use by another thread).
1781 : : Handle such cases as gracefully as possible by falling back to a
1782 : : minimal abort handler that only relies on i18n. */
1783 : 7 : if (global_dc->get_reference_printer () == nullptr)
1784 : : {
1785 : : /* Print the error message. */
1786 : 0 : fnotice (stderr, diagnostics::get_text_for_kind (diagnostics::kind::ice));
1787 : 0 : fnotice (stderr, "in %s, at %s:%d", function, trim_filename (file), line);
1788 : 0 : fputc ('\n', stderr);
1789 : :
1790 : : /* Attempt to print a backtrace. */
1791 : 0 : struct backtrace_state *state
1792 : 0 : = backtrace_create_state (nullptr, 0, bt_err_callback, nullptr);
1793 : 0 : int count = 0;
1794 : 0 : if (state != nullptr)
1795 : 0 : backtrace_full (state, 2, bt_callback, bt_err_callback,
1796 : : (void *) &count);
1797 : :
1798 : : /* We can't call warn_if_plugins or emergency_dump_function as these
1799 : : rely on GCC state that might not be initialized, or might be in
1800 : : use by another thread. */
1801 : :
1802 : : /* Abort the process. */
1803 : 0 : real_abort ();
1804 : : }
1805 : :
1806 : 7 : internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1807 : : }
1808 : :
1809 : : namespace diagnostics {
1810 : :
1811 : : /* class diagnostics::context. */
1812 : :
1813 : : void
1814 : 852035100 : context::begin_group ()
1815 : : {
1816 : 852035100 : m_diagnostic_groups.m_group_nesting_depth++;
1817 : 852035100 : }
1818 : :
1819 : : void
1820 : 852035095 : context::end_group ()
1821 : : {
1822 : 852035095 : if (--m_diagnostic_groups.m_group_nesting_depth == 0)
1823 : : {
1824 : : /* Handle the case where we've popped the final diagnostic group.
1825 : : If any diagnostics were emitted, give the context a chance
1826 : : to do something. */
1827 : 849975768 : if (m_diagnostic_groups.m_emission_count > 0)
1828 : : {
1829 : 1440109 : DIAGNOSTICS_LOG_SCOPE_PRINTF0
1830 : : (get_logger (),
1831 : 1440109 : "diagnostics::context::end_group: ending group");
1832 : 5760519 : for (auto sink_ : m_sinks)
1833 : 1440192 : sink_->on_end_group ();
1834 : 849975768 : }
1835 : 849975768 : m_diagnostic_groups.m_emission_count = 0;
1836 : : }
1837 : : /* We're popping one level, so might need to stop inhibiting notes. */
1838 : 852035095 : inhibit_notes_in_group (/*inhibit=*/false);
1839 : 852035095 : }
1840 : :
1841 : : void
1842 : 29147 : context::push_nesting_level ()
1843 : : {
1844 : 29147 : ++m_diagnostic_groups.m_diagnostic_nesting_level;
1845 : 29147 : }
1846 : :
1847 : : void
1848 : 29145 : context::pop_nesting_level ()
1849 : : {
1850 : 29145 : --m_diagnostic_groups.m_diagnostic_nesting_level;
1851 : : /* We're popping one level, so might need to stop inhibiting notes. */
1852 : 29145 : inhibit_notes_in_group (/*inhibit=*/false);
1853 : 29145 : }
1854 : :
1855 : : void
1856 : 0 : context::set_nesting_level (int new_level)
1857 : : {
1858 : 0 : m_diagnostic_groups.m_diagnostic_nesting_level = new_level;
1859 : 0 : }
1860 : :
1861 : : void
1862 : 0 : sink::dump (FILE *out, int indent) const
1863 : : {
1864 : 0 : dumping::emit_heading (out, indent, "printer");
1865 : 0 : m_printer->dump (out, indent + 2);
1866 : :
1867 : 0 : dumping::emit_heading (out, indent, "extensions");
1868 : 0 : if (m_extensions.empty ())
1869 : 0 : dumping::emit_none (out, indent + 2);
1870 : : else
1871 : 0 : for (auto &ext : m_extensions)
1872 : 0 : ext->dump (out, indent + 2);
1873 : 0 : }
1874 : :
1875 : : void
1876 : 308204 : sink::finalize_extensions ()
1877 : : {
1878 : 308204 : for (auto &ext : m_extensions)
1879 : 0 : ext->finalize ();
1880 : 308204 : }
1881 : :
1882 : : void
1883 : 0 : sink::on_report_verbatim (text_info &)
1884 : : {
1885 : : /* No-op. */
1886 : 0 : }
1887 : :
1888 : : /* Set the output format for DC to FORMAT, using BASE_FILE_NAME for
1889 : : file-based output formats. */
1890 : :
1891 : : void
1892 : 90 : output_format_init (context &dc,
1893 : : const char *main_input_filename_,
1894 : : const char *base_file_name,
1895 : : enum diagnostics_output_format format,
1896 : : bool json_formatting)
1897 : : {
1898 : 90 : sink *new_sink = nullptr;
1899 : 90 : switch (format)
1900 : : {
1901 : 0 : default:
1902 : 0 : gcc_unreachable ();
1903 : : case DIAGNOSTICS_OUTPUT_FORMAT_TEXT:
1904 : : /* The default; do nothing. */
1905 : : break;
1906 : :
1907 : 0 : case DIAGNOSTICS_OUTPUT_FORMAT_SARIF_STDERR:
1908 : 0 : new_sink = &init_sarif_stderr (dc,
1909 : : line_table,
1910 : : json_formatting);
1911 : 0 : break;
1912 : :
1913 : 90 : case DIAGNOSTICS_OUTPUT_FORMAT_SARIF_FILE:
1914 : 90 : new_sink = &init_sarif_file (dc,
1915 : : line_table,
1916 : : json_formatting,
1917 : : base_file_name);
1918 : 90 : break;
1919 : : }
1920 : 90 : if (new_sink)
1921 : 90 : new_sink->set_main_input_filename (main_input_filename_);
1922 : 90 : }
1923 : :
1924 : : /* Initialize this context's m_diagrams based on CHARSET.
1925 : : Specifically, make a text_art::theme object for m_diagrams.m_theme,
1926 : : (or nullptr for "no diagrams"). */
1927 : :
1928 : : void
1929 : 1605519 : context::set_text_art_charset (enum diagnostic_text_art_charset charset)
1930 : : {
1931 : 1605519 : delete m_diagrams.m_theme;
1932 : 1605519 : switch (charset)
1933 : : {
1934 : 0 : default:
1935 : 0 : gcc_unreachable ();
1936 : :
1937 : 887055 : case DIAGNOSTICS_TEXT_ART_CHARSET_NONE:
1938 : 887055 : m_diagrams.m_theme = nullptr;
1939 : 887055 : break;
1940 : :
1941 : 630838 : case DIAGNOSTICS_TEXT_ART_CHARSET_ASCII:
1942 : 630838 : m_diagrams.m_theme = new text_art::ascii_theme ();
1943 : 630838 : break;
1944 : :
1945 : 295 : case DIAGNOSTICS_TEXT_ART_CHARSET_UNICODE:
1946 : 295 : m_diagrams.m_theme = new text_art::unicode_theme ();
1947 : 295 : break;
1948 : :
1949 : 87331 : case DIAGNOSTICS_TEXT_ART_CHARSET_EMOJI:
1950 : 87331 : m_diagrams.m_theme = new text_art::emoji_theme ();
1951 : 87331 : break;
1952 : : }
1953 : 1605519 : }
1954 : :
1955 : : /* struct diagnostics::counters. */
1956 : :
1957 : 9120268 : counters::counters ()
1958 : : {
1959 : 9120268 : clear ();
1960 : 9120268 : }
1961 : :
1962 : : void
1963 : 0 : counters::dump (FILE *out, int indent) const
1964 : : {
1965 : 0 : dumping::emit_heading (out, indent, "counts");
1966 : 0 : bool none = true;
1967 : 0 : for (int i = 0; i < static_cast<int> (kind::last_diagnostic_kind); i++)
1968 : 0 : if (m_count_for_kind[i] > 0)
1969 : : {
1970 : 0 : dumping::emit_indent (out, indent + 2);
1971 : 0 : fprintf (out, "%s%i\n",
1972 : : get_text_for_kind (static_cast<enum kind> (i)),
1973 : 0 : m_count_for_kind[i]);
1974 : 0 : none = false;
1975 : : }
1976 : 0 : if (none)
1977 : 0 : dumping::emit_none (out, indent + 2);
1978 : 0 : }
1979 : :
1980 : : void
1981 : 110830 : counters::move_to (counters &dest)
1982 : : {
1983 : 1773280 : for (int i = 0; i < static_cast<int> (kind::last_diagnostic_kind); i++)
1984 : 1662450 : dest.m_count_for_kind[i] += m_count_for_kind[i];
1985 : 110830 : clear ();
1986 : 110830 : }
1987 : :
1988 : : void
1989 : 32825795 : counters::clear ()
1990 : : {
1991 : 32825795 : memset (&m_count_for_kind, 0, sizeof m_count_for_kind);
1992 : 32825795 : }
1993 : :
1994 : : #if CHECKING_P
1995 : :
1996 : : namespace selftest {
1997 : :
1998 : : using line_table_test = ::selftest::line_table_test;
1999 : : using temp_source_file = ::selftest::temp_source_file;
2000 : :
2001 : : /* Helper function for test_print_escaped_string. */
2002 : :
2003 : : static void
2004 : 24 : assert_print_escaped_string (const ::selftest::location &loc,
2005 : : const char *expected_output,
2006 : : const char *input)
2007 : : {
2008 : 24 : pretty_printer pp;
2009 : 24 : print_escaped_string (&pp, input);
2010 : 24 : ASSERT_STREQ_AT (loc, expected_output, pp_formatted_text (&pp));
2011 : 24 : }
2012 : :
2013 : : #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
2014 : : assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
2015 : :
2016 : : /* Tests of print_escaped_string. */
2017 : :
2018 : : static void
2019 : 3 : test_print_escaped_string ()
2020 : : {
2021 : : /* Empty string. */
2022 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
2023 : :
2024 : : /* Non-empty string. */
2025 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
2026 : :
2027 : : /* Various things that need to be escaped: */
2028 : : /* Backslash. */
2029 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
2030 : : "before\\after");
2031 : : /* Tab. */
2032 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
2033 : : "before\tafter");
2034 : : /* Newline. */
2035 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
2036 : : "before\nafter");
2037 : : /* Double quote. */
2038 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
2039 : : "before\"after");
2040 : :
2041 : : /* Non-printable characters: BEL: '\a': 0x07 */
2042 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
2043 : : "before\aafter");
2044 : : /* Non-printable characters: vertical tab: '\v': 0x0b */
2045 : 3 : ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
2046 : : "before\vafter");
2047 : 3 : }
2048 : :
2049 : : /* Tests of print_parseable_fixits. */
2050 : :
2051 : : /* Verify that print_parseable_fixits emits the empty string if there
2052 : : are no fixits. */
2053 : :
2054 : : static void
2055 : 3 : test_print_parseable_fixits_none ()
2056 : : {
2057 : 3 : pretty_printer pp;
2058 : 3 : file_cache fc;
2059 : 3 : rich_location richloc (line_table, UNKNOWN_LOCATION);
2060 : :
2061 : 3 : print_parseable_fixits (fc, &pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
2062 : 3 : ASSERT_STREQ ("", pp_formatted_text (&pp));
2063 : 3 : }
2064 : :
2065 : : /* Verify that print_parseable_fixits does the right thing if there
2066 : : is an insertion fixit hint. */
2067 : :
2068 : : static void
2069 : 3 : test_print_parseable_fixits_insert ()
2070 : : {
2071 : 3 : pretty_printer pp;
2072 : 3 : file_cache fc;
2073 : 3 : rich_location richloc (line_table, UNKNOWN_LOCATION);
2074 : :
2075 : 3 : linemap_add (line_table, LC_ENTER, false, "test.c", 0);
2076 : 3 : linemap_line_start (line_table, 5, 100);
2077 : 3 : linemap_add (line_table, LC_LEAVE, false, nullptr, 0);
2078 : 3 : location_t where = linemap_position_for_column (line_table, 10);
2079 : 3 : richloc.add_fixit_insert_before (where, "added content");
2080 : :
2081 : 3 : print_parseable_fixits (fc, &pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
2082 : 3 : ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
2083 : : pp_formatted_text (&pp));
2084 : 3 : }
2085 : :
2086 : : /* Verify that print_parseable_fixits does the right thing if there
2087 : : is an removal fixit hint. */
2088 : :
2089 : : static void
2090 : 3 : test_print_parseable_fixits_remove ()
2091 : : {
2092 : 3 : pretty_printer pp;
2093 : 3 : file_cache fc;
2094 : 3 : rich_location richloc (line_table, UNKNOWN_LOCATION);
2095 : :
2096 : 3 : linemap_add (line_table, LC_ENTER, false, "test.c", 0);
2097 : 3 : linemap_line_start (line_table, 5, 100);
2098 : 3 : linemap_add (line_table, LC_LEAVE, false, nullptr, 0);
2099 : 3 : source_range where;
2100 : 3 : where.m_start = linemap_position_for_column (line_table, 10);
2101 : 3 : where.m_finish = linemap_position_for_column (line_table, 20);
2102 : 3 : richloc.add_fixit_remove (where);
2103 : :
2104 : 3 : print_parseable_fixits (fc, &pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
2105 : 3 : ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
2106 : : pp_formatted_text (&pp));
2107 : 3 : }
2108 : :
2109 : : /* Verify that print_parseable_fixits does the right thing if there
2110 : : is an replacement fixit hint. */
2111 : :
2112 : : static void
2113 : 3 : test_print_parseable_fixits_replace ()
2114 : : {
2115 : 3 : pretty_printer pp;
2116 : 3 : file_cache fc;
2117 : 3 : rich_location richloc (line_table, UNKNOWN_LOCATION);
2118 : :
2119 : 3 : linemap_add (line_table, LC_ENTER, false, "test.c", 0);
2120 : 3 : linemap_line_start (line_table, 5, 100);
2121 : 3 : linemap_add (line_table, LC_LEAVE, false, nullptr, 0);
2122 : 3 : source_range where;
2123 : 3 : where.m_start = linemap_position_for_column (line_table, 10);
2124 : 3 : where.m_finish = linemap_position_for_column (line_table, 20);
2125 : 3 : richloc.add_fixit_replace (where, "replacement");
2126 : :
2127 : 3 : print_parseable_fixits (fc, &pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
2128 : 3 : ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
2129 : : pp_formatted_text (&pp));
2130 : 3 : }
2131 : :
2132 : : /* Verify that print_parseable_fixits correctly handles
2133 : : DIAGNOSTICS_COLUMN_UNIT_BYTE vs DIAGNOSTICS_COLUMN_UNIT_COLUMN. */
2134 : :
2135 : : static void
2136 : 3 : test_print_parseable_fixits_bytes_vs_display_columns ()
2137 : : {
2138 : 3 : line_table_test ltt;
2139 : 3 : rich_location richloc (line_table, UNKNOWN_LOCATION);
2140 : :
2141 : : /* 1-based byte offsets: 12345677778888999900001234567. */
2142 : 3 : const char *const content = "smile \xf0\x9f\x98\x82 colour\n";
2143 : : /* 1-based display cols: 123456[......7-8.....]9012345. */
2144 : 3 : const int tabstop = 8;
2145 : :
2146 : 3 : temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
2147 : 3 : file_cache fc;
2148 : 3 : const char *const fname = tmp.get_filename ();
2149 : :
2150 : 3 : linemap_add (line_table, LC_ENTER, false, fname, 0);
2151 : 3 : linemap_line_start (line_table, 1, 100);
2152 : 3 : linemap_add (line_table, LC_LEAVE, false, nullptr, 0);
2153 : 3 : source_range where;
2154 : 3 : where.m_start = linemap_position_for_column (line_table, 12);
2155 : 3 : where.m_finish = linemap_position_for_column (line_table, 17);
2156 : 3 : richloc.add_fixit_replace (where, "color");
2157 : :
2158 : : /* Escape fname. */
2159 : 3 : pretty_printer tmp_pp;
2160 : 3 : print_escaped_string (&tmp_pp, fname);
2161 : 3 : char *escaped_fname = xstrdup (pp_formatted_text (&tmp_pp));
2162 : :
2163 : 3 : const int buf_len = strlen (escaped_fname) + 100;
2164 : 3 : char *const expected = XNEWVEC (char, buf_len);
2165 : :
2166 : 3 : {
2167 : 3 : pretty_printer pp;
2168 : 3 : print_parseable_fixits (fc, &pp, &richloc,
2169 : : DIAGNOSTICS_COLUMN_UNIT_BYTE,
2170 : : tabstop);
2171 : 3 : snprintf (expected, buf_len,
2172 : : "fix-it:%s:{1:12-1:18}:\"color\"\n", escaped_fname);
2173 : 3 : ASSERT_STREQ (expected, pp_formatted_text (&pp));
2174 : 3 : }
2175 : 3 : {
2176 : 3 : pretty_printer pp;
2177 : 3 : print_parseable_fixits (fc, &pp, &richloc,
2178 : : DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
2179 : : tabstop);
2180 : 3 : snprintf (expected, buf_len,
2181 : : "fix-it:%s:{1:10-1:16}:\"color\"\n", escaped_fname);
2182 : 3 : ASSERT_STREQ (expected, pp_formatted_text (&pp));
2183 : 3 : }
2184 : :
2185 : 3 : XDELETEVEC (expected);
2186 : 3 : free (escaped_fname);
2187 : 3 : }
2188 : :
2189 : : /* Verify that
2190 : : diagnostics::column_policy::get_location_text (..., SHOW_COLUMN, ...)
2191 : : generates EXPECTED_LOC_TEXT, given FILENAME, LINE, COLUMN, with
2192 : : colorization disabled. */
2193 : :
2194 : : static void
2195 : 42 : assert_location_text (const char *expected_loc_text,
2196 : : const char *filename, int line, int column,
2197 : : bool show_column,
2198 : : int origin = 1,
2199 : : enum diagnostics_column_unit column_unit
2200 : : = DIAGNOSTICS_COLUMN_UNIT_BYTE)
2201 : : {
2202 : 42 : diagnostics::selftest::test_context dc;
2203 : 42 : dc.get_column_options ().m_column_unit = column_unit;
2204 : 42 : dc.get_column_options ().m_column_origin = origin;
2205 : :
2206 : 42 : expanded_location xloc;
2207 : 42 : xloc.file = filename;
2208 : 42 : xloc.line = line;
2209 : 42 : xloc.column = column;
2210 : 42 : xloc.data = nullptr;
2211 : 42 : xloc.sysp = false;
2212 : :
2213 : 42 : diagnostics::column_policy column_policy (dc);
2214 : 42 : label_text actual_loc_text
2215 : 42 : = column_policy.get_location_text (xloc, show_column, false);
2216 : 42 : ASSERT_STREQ (expected_loc_text, actual_loc_text.get ());
2217 : 42 : }
2218 : :
2219 : : /* Verify that get_location_text works as expected. */
2220 : :
2221 : : static void
2222 : 3 : test_get_location_text ()
2223 : : {
2224 : 3 : const char *old_progname = progname;
2225 : 3 : progname = "PROGNAME";
2226 : 3 : assert_location_text ("PROGNAME:", nullptr, 0, 0, true);
2227 : 3 : char *built_in_colon = concat (special_fname_builtin (), ":", (char *) 0);
2228 : 3 : assert_location_text (built_in_colon, special_fname_builtin (),
2229 : : 42, 10, true);
2230 : 3 : free (built_in_colon);
2231 : 3 : assert_location_text ("foo.c:42:10:", "foo.c", 42, 10, true);
2232 : 3 : assert_location_text ("foo.c:42:9:", "foo.c", 42, 10, true, 0);
2233 : 3 : assert_location_text ("foo.c:42:1010:", "foo.c", 42, 10, true, 1001);
2234 : 9 : for (int origin = 0; origin != 2; ++origin)
2235 : 6 : assert_location_text ("foo.c:42:", "foo.c", 42, 0, true, origin);
2236 : 3 : assert_location_text ("foo.c:", "foo.c", 0, 10, true);
2237 : 3 : assert_location_text ("foo.c:42:", "foo.c", 42, 10, false);
2238 : 3 : assert_location_text ("foo.c:", "foo.c", 0, 10, false);
2239 : :
2240 : 3 : diagnostics::text_sink::maybe_line_and_column (INT_MAX, INT_MAX);
2241 : 3 : diagnostics::text_sink::maybe_line_and_column (INT_MIN, INT_MIN);
2242 : :
2243 : 3 : {
2244 : : /* In order to test display columns vs byte columns, we need to create a
2245 : : file for location_get_source_line() to read. */
2246 : :
2247 : 3 : const char *const content = "smile \xf0\x9f\x98\x82\n";
2248 : 3 : const int line_bytes = strlen (content) - 1;
2249 : 3 : const int def_tabstop = 8;
2250 : 3 : const cpp_char_column_policy policy (def_tabstop, cpp_wcwidth);
2251 : 3 : const int display_width = cpp_display_width (content, line_bytes, policy);
2252 : 3 : ASSERT_EQ (line_bytes - 2, display_width);
2253 : 3 : temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
2254 : 3 : const char *const fname = tmp.get_filename ();
2255 : 3 : const int buf_len = strlen (fname) + 16;
2256 : 3 : char *const expected = XNEWVEC (char, buf_len);
2257 : :
2258 : 3 : snprintf (expected, buf_len, "%s:1:%d:", fname, line_bytes);
2259 : 3 : assert_location_text (expected, fname, 1, line_bytes, true,
2260 : : 1, DIAGNOSTICS_COLUMN_UNIT_BYTE);
2261 : :
2262 : 3 : snprintf (expected, buf_len, "%s:1:%d:", fname, line_bytes - 1);
2263 : 3 : assert_location_text (expected, fname, 1, line_bytes, true,
2264 : : 0, DIAGNOSTICS_COLUMN_UNIT_BYTE);
2265 : :
2266 : 3 : snprintf (expected, buf_len, "%s:1:%d:", fname, display_width);
2267 : 3 : assert_location_text (expected, fname, 1, line_bytes, true,
2268 : : 1, DIAGNOSTICS_COLUMN_UNIT_DISPLAY);
2269 : :
2270 : 3 : snprintf (expected, buf_len, "%s:1:%d:", fname, display_width - 1);
2271 : 3 : assert_location_text (expected, fname, 1, line_bytes, true,
2272 : : 0, DIAGNOSTICS_COLUMN_UNIT_DISPLAY);
2273 : :
2274 : 3 : XDELETEVEC (expected);
2275 : 3 : }
2276 : :
2277 : :
2278 : 3 : progname = old_progname;
2279 : 3 : }
2280 : :
2281 : : /* Selftest for num_digits. */
2282 : :
2283 : : static void
2284 : 3 : test_num_digits ()
2285 : : {
2286 : 3 : ASSERT_EQ (1, num_digits (0));
2287 : 3 : ASSERT_EQ (1, num_digits (9));
2288 : 3 : ASSERT_EQ (2, num_digits (10));
2289 : 3 : ASSERT_EQ (2, num_digits (99));
2290 : 3 : ASSERT_EQ (3, num_digits (100));
2291 : 3 : ASSERT_EQ (3, num_digits (999));
2292 : 3 : ASSERT_EQ (4, num_digits (1000));
2293 : 3 : ASSERT_EQ (4, num_digits (9999));
2294 : 3 : ASSERT_EQ (5, num_digits (10000));
2295 : 3 : ASSERT_EQ (5, num_digits (99999));
2296 : 3 : ASSERT_EQ (6, num_digits (100000));
2297 : 3 : ASSERT_EQ (6, num_digits (999999));
2298 : 3 : ASSERT_EQ (7, num_digits (1000000));
2299 : 3 : ASSERT_EQ (7, num_digits (9999999));
2300 : 3 : ASSERT_EQ (8, num_digits (10000000));
2301 : 3 : ASSERT_EQ (8, num_digits (99999999));
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 : : }
|