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