]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/diagnostic.c
Handle fancy_abort before diagnostic initialization [PR98586]
[thirdparty/gcc.git] / gcc / diagnostic.c
CommitLineData
47b69537 1/* Language-independent diagnostic subroutines for the GNU Compiler Collection
99dee823 2 Copyright (C) 1999-2021 Free Software Foundation, Inc.
406a65d0 3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
6ed551b4 4
1322177d 5This file is part of GCC.
6ed551b4 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
1322177d 10version.
6ed551b4 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
6ed551b4
GDR
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
6ed551b4
GDR
20
21
4eb00163 22/* This file implements the language independent aspect of diagnostic
6ed551b4
GDR
23 message module. */
24
25#include "config.h"
6ed551b4 26#include "system.h"
4977bab6 27#include "coretypes.h"
a757585a 28#include "version.h"
d83697f4 29#include "demangle.h"
6ed551b4 30#include "intl.h"
d83697f4 31#include "backtrace.h"
345ed1fe 32#include "diagnostic.h"
4b84d650 33#include "diagnostic-color.h"
d2608235 34#include "diagnostic-url.h"
6d4a35ca 35#include "diagnostic-metadata.h"
4bc1899b 36#include "diagnostic-path.h"
717ebe91 37#include "edit-context.h"
a93eac6a 38#include "selftest.h"
ace72598 39#include "selftest-diagnostic.h"
fa5baeed 40#include "opts.h"
004bb936 41#include "cpplib.h"
6ed551b4 42
c9db45aa
TB
43#ifdef HAVE_TERMIOS_H
44# include <termios.h>
45#endif
46
47#ifdef GWINSZ_IN_SYS_IOCTL
48# include <sys/ioctl.h>
49#endif
50
0ecf545c
MS
51/* Disable warnings about quoting issues in the pp_xxx calls below
52 that (intentionally) don't follow GCC diagnostic conventions. */
53#if __GNUC__ >= 10
54# pragma GCC diagnostic push
55# pragma GCC diagnostic ignored "-Wformat-diag"
56#endif
57
243fbddd
JM
58#define pedantic_warning_kind(DC) \
59 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
60#define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
5f0f4a3b 61#define permissive_error_option(DC) ((DC)->opt_permissive)
c707a408 62
30f7a378 63/* Prototypes. */
6d4a35ca
DM
64static bool diagnostic_impl (rich_location *, const diagnostic_metadata *,
65 int, const char *,
66 va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(4,0);
67static bool diagnostic_n_impl (rich_location *, const diagnostic_metadata *,
68 int, unsigned HOST_WIDE_INT,
1c89478a 69 const char *, const char *, va_list *,
6d4a35ca 70 diagnostic_t) ATTRIBUTE_GCC_DIAG(6,0);
a5fb7ad2 71
79a490a9 72static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
79a490a9 73static void real_abort (void) ATTRIBUTE_NORETURN;
fbfc1192 74
1da2ed5f
JM
75/* Name of program invoked, sans directories. */
76
77const char *progname;
78
f68fc4db
GDR
79/* A diagnostic_context surrogate for stderr. */
80static diagnostic_context global_diagnostic_context;
81diagnostic_context *global_dc = &global_diagnostic_context;
6ed551b4 82\f
59650e48
ZW
83/* Return a malloc'd string containing MSG formatted a la printf. The
84 caller is responsible for freeing the memory. */
8e54f6d3 85char *
e34d07f2 86build_message_string (const char *msg, ...)
b903d81e 87{
36244024 88 char *str;
e34d07f2 89 va_list ap;
b903d81e 90
e34d07f2 91 va_start (ap, msg);
582f770b 92 str = xvasprintf (msg, ap);
e34d07f2 93 va_end (ap);
b903d81e
GDR
94
95 return str;
96}
97
95bd1dd7 98/* Same as diagnostic_build_prefix, but only the source FILE is given. */
24805e80 99char *
e78e8a0b 100file_name_as_prefix (diagnostic_context *context, const char *f)
24805e80 101{
e78e8a0b
JJ
102 const char *locus_cs
103 = colorize_start (pp_show_color (context->printer), "locus");
104 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
105 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
24805e80
GDR
106}
107
47b69537 108
6ed551b4 109\f
9fec0042 110/* Return the value of the getenv("COLUMNS") as an integer. If the
c9db45aa
TB
111 value is not set to a positive integer, use ioctl to get the
112 terminal width. If it fails, return INT_MAX. */
113int
114get_terminal_width (void)
9fec0042
MLI
115{
116 const char * s = getenv ("COLUMNS");
117 if (s != NULL) {
118 int n = atoi (s);
119 if (n > 0)
120 return n;
121 }
c9db45aa
TB
122
123#ifdef TIOCGWINSZ
124 struct winsize w;
125 w.ws_col = 0;
126 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
127 return w.ws_col;
128#endif
129
9fec0042
MLI
130 return INT_MAX;
131}
132
133/* Set caret_max_width to value. */
134void
135diagnostic_set_caret_max_width (diagnostic_context *context, int value)
136{
137 /* One minus to account for the leading empty space. */
138 value = value ? value - 1
025311c4 139 : (isatty (fileno (pp_buffer (context->printer)->stream))
c9db45aa 140 ? get_terminal_width () - 1: INT_MAX);
9fec0042
MLI
141
142 if (value <= 0)
143 value = INT_MAX;
144
145 context->caret_max_width = value;
146}
147
478dd60d
DM
148/* Default implementation of final_cb. */
149
150static void
151default_diagnostic_final_cb (diagnostic_context *context)
152{
153 /* Some of the errors may actually have been warnings. */
154 if (diagnostic_kind_count (context, DK_WERROR))
155 {
156 /* -Werror was given. */
157 if (context->warning_as_error_requested)
158 pp_verbatim (context->printer,
159 _("%s: all warnings being treated as errors"),
160 progname);
161 /* At least one -Werror= was given. */
162 else
163 pp_verbatim (context->printer,
164 _("%s: some warnings being treated as errors"),
165 progname);
166 pp_newline_and_flush (context->printer);
167 }
168}
169
47b69537
GDR
170/* Initialize the diagnostic message outputting machinery. */
171void
5f0f4a3b 172diagnostic_initialize (diagnostic_context *context, int n_opts)
47b69537 173{
5f0f4a3b
JM
174 int i;
175
b6fe0bb8
GDR
176 /* Allocate a basic pretty-printer. Clients will replace this a
177 much more elaborated pretty-printer if they wish. */
5ed6ace5 178 context->printer = XNEW (pretty_printer);
da6ca2b5 179 new (context->printer) pretty_printer ();
47b69537 180
b6fe0bb8 181 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
7783b402 182 context->warning_as_error_requested = false;
5f0f4a3b
JM
183 context->n_opts = n_opts;
184 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
185 for (i = 0; i < n_opts; i++)
186 context->classify_diagnostic[i] = DK_UNSPECIFIED;
9fec0042
MLI
187 context->show_caret = false;
188 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
b816477a 189 for (i = 0; i < rich_location::STATICALLY_ALLOCATED_RANGES; i++)
2a2703a2 190 context->caret_chars[i] = '^';
6d4a35ca 191 context->show_cwe = false;
4bc1899b
DM
192 context->path_format = DPF_NONE;
193 context->show_path_depths = false;
2098fe9e 194 context->show_option_requested = false;
b6fe0bb8 195 context->abort_on_error = false;
5f0f4a3b
JM
196 context->show_column = false;
197 context->pedantic_errors = false;
198 context->permissive = false;
199 context->opt_permissive = 0;
200 context->fatal_errors = false;
e3339d0f
JM
201 context->dc_inhibit_warnings = false;
202 context->dc_warn_system_headers = false;
3a789837 203 context->max_errors = 0;
b6fe0bb8 204 context->internal_error = NULL;
47b69537 205 diagnostic_starter (context) = default_diagnostic_starter;
876217ae 206 context->start_span = default_diagnostic_start_span_fn;
47b69537 207 diagnostic_finalizer (context) = default_diagnostic_finalizer;
5f0f4a3b 208 context->option_enabled = NULL;
46625112 209 context->option_state = NULL;
5f0f4a3b 210 context->option_name = NULL;
b4c7ca2e 211 context->get_option_url = NULL;
1b8b126f 212 context->last_location = UNKNOWN_LOCATION;
b6fe0bb8 213 context->last_module = 0;
cf835838 214 context->x_data = NULL;
b6fe0bb8 215 context->lock = 0;
0e94b750 216 context->inhibit_notes_p = false;
cc015f3a 217 context->colorize_source_p = false;
96e6ae57 218 context->show_labels_p = false;
56b61d7f 219 context->show_line_numbers_p = false;
0141ab44 220 context->min_margin_width = 0;
cc015f3a 221 context->show_ruler_p = false;
f1096055
DM
222 if (const char *var = getenv ("GCC_EXTRA_DIAGNOSTIC_OUTPUT"))
223 {
224 if (!strcmp (var, "fixits-v1"))
225 context->extra_output_kind = EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1;
226 else if (!strcmp (var, "fixits-v2"))
227 context->extra_output_kind = EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2;
228 /* Silently ignore unrecognized values. */
229 }
004bb936
LH
230 context->column_unit = DIAGNOSTICS_COLUMN_UNIT_DISPLAY;
231 context->column_origin = 1;
232 context->tabstop = 8;
717ebe91 233 context->edit_context_ptr = NULL;
097f82ec
DM
234 context->diagnostic_group_nesting_depth = 0;
235 context->diagnostic_group_emission_count = 0;
236 context->begin_group_cb = NULL;
237 context->end_group_cb = NULL;
478dd60d 238 context->final_cb = default_diagnostic_final_cb;
47b69537
GDR
239}
240
97aa8bb6
MLI
241/* Maybe initialize the color support. We require clients to do this
242 explicitly, since most clients don't want color. When called
243 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
244
245void
246diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
247{
248 /* value == -1 is the default value. */
249 if (value < 0)
250 {
251 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
252 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
253 otherwise default to -fdiagnostics-color=never, for other
254 values default to that
255 -fdiagnostics-color={never,auto,always}. */
256 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
257 {
258 if (!getenv ("GCC_COLORS"))
259 return;
260 value = DIAGNOSTICS_COLOR_AUTO;
261 }
262 else
263 value = DIAGNOSTICS_COLOR_DEFAULT;
264 }
265 pp_show_color (context->printer)
266 = colorize_init ((diagnostic_color_rule_t) value);
267}
268
d2608235
DM
269/* Initialize URL support within CONTEXT based on VALUE, handling "auto". */
270
271void
272diagnostic_urls_init (diagnostic_context *context, int value /*= -1 */)
273{
458c8d64 274 /* value == -1 is the default value. */
d2608235 275 if (value < 0)
458c8d64
BE
276 {
277 /* If DIAGNOSTICS_URLS_DEFAULT is -1, default to
278 -fdiagnostics-urls=auto if GCC_URLS or TERM_URLS is in the
279 environment, otherwise default to -fdiagnostics-urls=never,
280 for other values default to that
281 -fdiagnostics-urls={never,auto,always}. */
282 if (DIAGNOSTICS_URLS_DEFAULT == -1)
283 {
284 if (!getenv ("GCC_URLS") && !getenv ("TERM_URLS"))
285 return;
286 value = DIAGNOSTICS_URL_AUTO;
287 }
288 else
289 value = DIAGNOSTICS_URLS_DEFAULT;
290 }
d2608235 291
458c8d64
BE
292 context->printer->url_format
293 = determine_url_format ((diagnostic_url_rule_t) value);
d2608235
DM
294}
295
d0b8780d
MLI
296/* Do any cleaning up required after the last diagnostic is emitted. */
297
298void
299diagnostic_finish (diagnostic_context *context)
300{
478dd60d
DM
301 if (context->final_cb)
302 context->final_cb (context);
7ecc3eb9
DS
303
304 diagnostic_file_cache_fini ();
3edf64aa
DM
305
306 XDELETEVEC (context->classify_diagnostic);
307 context->classify_diagnostic = NULL;
308
309 /* diagnostic_initialize allocates context->printer using XNEW
310 and placement-new. */
311 context->printer->~pretty_printer ();
312 XDELETE (context->printer);
313 context->printer = NULL;
717ebe91
DM
314
315 if (context->edit_context_ptr)
316 {
317 delete context->edit_context_ptr;
318 context->edit_context_ptr = NULL;
319 }
d0b8780d
MLI
320}
321
178b58b5
JM
322/* Initialize DIAGNOSTIC, where the message MSG has already been
323 translated. */
47b69537 324void
178b58b5 325diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
8a645150 326 va_list *args, rich_location *richloc,
178b58b5 327 diagnostic_t kind)
47b69537 328{
8a645150 329 gcc_assert (richloc);
fa6ef813 330 diagnostic->message.err_no = errno;
47b69537 331 diagnostic->message.args_ptr = args;
178b58b5 332 diagnostic->message.format_spec = msg;
8a645150
DM
333 diagnostic->message.m_richloc = richloc;
334 diagnostic->richloc = richloc;
6d4a35ca 335 diagnostic->metadata = NULL;
47b69537 336 diagnostic->kind = kind;
ccf08a6e 337 diagnostic->option_index = 0;
47b69537
GDR
338}
339
178b58b5
JM
340/* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
341 translated. */
342void
343diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
8a645150 344 va_list *args, rich_location *richloc,
178b58b5
JM
345 diagnostic_t kind)
346{
8a645150
DM
347 gcc_assert (richloc);
348 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
349}
350
351static const char *const diagnostic_kind_color[] = {
352#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
353#include "diagnostic.def"
354#undef DEFINE_DIAGNOSTIC_KIND
355 NULL
356};
357
358/* Get a color name for diagnostics of type KIND
359 Result could be NULL. */
360
361const char *
362diagnostic_get_color_for_kind (diagnostic_t kind)
363{
364 return diagnostic_kind_color[kind];
178b58b5
JM
365}
366
004bb936 367/* Given an expanded_location, convert the column (which is in 1-based bytes)
f1096055
DM
368 to the requested units, without converting the origin.
369 Return -1 if the column is invalid (<= 0). */
370
371static int
372convert_column_unit (enum diagnostics_column_unit column_unit,
373 int tabstop,
374 expanded_location s)
004bb936
LH
375{
376 if (s.column <= 0)
377 return -1;
378
f1096055 379 switch (column_unit)
004bb936 380 {
f1096055
DM
381 default:
382 gcc_unreachable ();
383
004bb936 384 case DIAGNOSTICS_COLUMN_UNIT_DISPLAY:
f1096055 385 return location_compute_display_column (s, tabstop);
004bb936
LH
386
387 case DIAGNOSTICS_COLUMN_UNIT_BYTE:
f1096055 388 return s.column;
004bb936 389 }
f1096055 390}
004bb936 391
f1096055
DM
392/* Given an expanded_location, convert the column (which is in 1-based bytes)
393 to the requested units and origin. Return -1 if the column is
394 invalid (<= 0). */
395int
396diagnostic_converted_column (diagnostic_context *context, expanded_location s)
397{
398 int one_based_col
399 = convert_column_unit (context->column_unit, context->tabstop, s);
400 if (one_based_col <= 0)
401 return -1;
004bb936
LH
402 return one_based_col + (context->column_origin - 1);
403}
404
101e910b 405/* Return a formatted line and column ':%line:%column'. Elided if
004bb936
LH
406 line == 0 or col < 0. (A column of 0 may be valid due to the
407 -fdiagnostics-column-origin option.)
408 The result is a statically allocated buffer. */
101e910b
NS
409
410static const char *
411maybe_line_and_column (int line, int col)
412{
413 static char result[32];
414
415 if (line)
416 {
004bb936
LH
417 size_t l
418 = snprintf (result, sizeof (result),
419 col >= 0 ? ":%d:%d" : ":%d", line, col);
101e910b
NS
420 gcc_checking_assert (l < sizeof (result));
421 }
422 else
423 result[0] = 0;
424 return result;
425}
426
876217ae
DM
427/* Return a malloc'd string describing a location e.g. "foo.c:42:10".
428 The caller is responsible for freeing the memory. */
429
430static char *
431diagnostic_get_location_text (diagnostic_context *context,
432 expanded_location s)
433{
434 pretty_printer *pp = context->printer;
435 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
436 const char *locus_ce = colorize_stop (pp_show_color (pp));
101e910b 437 const char *file = s.file ? s.file : progname;
004bb936
LH
438 int line = 0;
439 int col = -1;
440 if (strcmp (file, N_("<built-in>")))
441 {
442 line = s.line;
443 if (context->show_column)
444 col = diagnostic_converted_column (context, s);
445 }
876217ae 446
101e910b
NS
447 const char *line_col = maybe_line_and_column (line, col);
448 return build_message_string ("%s%s%s:%s", locus_cs, file,
449 line_col, locus_ce);
876217ae
DM
450}
451
387f6c15
DM
452static const char *const diagnostic_kind_text[] = {
453#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
454#include "diagnostic.def"
455#undef DEFINE_DIAGNOSTIC_KIND
456 "must-not-happen"
457};
458
876217ae
DM
459/* Return a malloc'd string describing a location and the severity of the
460 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
461 freeing the memory. */
47b69537 462char *
243fbddd 463diagnostic_build_prefix (diagnostic_context *context,
dfa32261 464 const diagnostic_info *diagnostic)
47b69537 465{
99abe958
MLI
466 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
467
255508dd 468 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
4b84d650 469 const char *text_cs = "", *text_ce = "";
4b84d650
JJ
470 pretty_printer *pp = context->printer;
471
472 if (diagnostic_kind_color[diagnostic->kind])
473 {
474 text_cs = colorize_start (pp_show_color (pp),
475 diagnostic_kind_color[diagnostic->kind]);
476 text_ce = colorize_stop (pp_show_color (pp));
477 }
4b84d650 478
99abe958 479 expanded_location s = diagnostic_expand_location (diagnostic);
876217ae
DM
480 char *location_text = diagnostic_get_location_text (context, s);
481
482 char *result = build_message_string ("%s %s%s%s", location_text,
483 text_cs, text, text_ce);
484 free (location_text);
485 return result;
47b69537
GDR
486}
487
d83697f4
ILT
488/* Functions at which to stop the backtrace print. It's not
489 particularly helpful to print the callers of these functions. */
490
491static const char * const bt_stop[] =
492{
493 "main",
3edf64aa 494 "toplev::main",
d83697f4
ILT
495 "execute_one_pass",
496 "compile_file",
497};
498
499/* A callback function passed to the backtrace_full function. */
500
501static int
502bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
503 const char *function)
504{
505 int *pcount = (int *) data;
506
507 /* If we don't have any useful information, don't print
508 anything. */
509 if (filename == NULL && function == NULL)
510 return 0;
511
512 /* Skip functions in diagnostic.c. */
513 if (*pcount == 0
514 && filename != NULL
c3284718 515 && strcmp (lbasename (filename), "diagnostic.c") == 0)
d83697f4
ILT
516 return 0;
517
518 /* Print up to 20 functions. We could make this a --param, but
519 since this is only for debugging just use a constant for now. */
520 if (*pcount >= 20)
521 {
522 /* Returning a non-zero value stops the backtrace. */
523 return 1;
524 }
525 ++*pcount;
526
527 char *alc = NULL;
528 if (function != NULL)
529 {
530 char *str = cplus_demangle_v3 (function,
531 (DMGL_VERBOSE | DMGL_ANSI
532 | DMGL_GNU_V3 | DMGL_PARAMS));
533 if (str != NULL)
534 {
535 alc = str;
536 function = str;
537 }
538
539 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
540 {
541 size_t len = strlen (bt_stop[i]);
542 if (strncmp (function, bt_stop[i], len) == 0
543 && (function[len] == '\0' || function[len] == '('))
544 {
545 if (alc != NULL)
546 free (alc);
547 /* Returning a non-zero value stops the backtrace. */
548 return 1;
549 }
550 }
551 }
552
553 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
a67a73fd 554 (unsigned long) pc,
d83697f4
ILT
555 function == NULL ? "???" : function,
556 filename == NULL ? "???" : filename,
557 lineno);
558
559 if (alc != NULL)
560 free (alc);
561
562 return 0;
563}
564
565/* A callback function passed to the backtrace_full function. This is
566 called if backtrace_full has an error. */
567
568static void
569bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
570{
571 if (errnum < 0)
572 {
573 /* This means that no debug info was available. Just quietly
574 skip printing backtrace info. */
575 return;
576 }
577 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
578 errnum == 0 ? "" : xstrerror (errnum));
579}
580
d0ea9f0a
NS
581/* Check if we've met the maximum error limit, and if so fatally exit
582 with a message. CONTEXT is the context to check, and FLUSH
583 indicates whether a diagnostic_finish call is needed. */
584
585void
586diagnostic_check_max_errors (diagnostic_context *context, bool flush)
587{
588 if (!context->max_errors)
589 return;
590
591 int count = (diagnostic_kind_count (context, DK_ERROR)
592 + diagnostic_kind_count (context, DK_SORRY)
593 + diagnostic_kind_count (context, DK_WERROR));
594
595 if (count >= context->max_errors)
596 {
597 fnotice (stderr,
598 "compilation terminated due to -fmax-errors=%u.\n",
599 context->max_errors);
600 if (flush)
601 diagnostic_finish (context);
602 exit (FATAL_EXIT_CODE);
603 }
604}
605
9804f5fb
ZW
606/* Take any action which is expected to happen after the diagnostic
607 is written out. This function does not always return. */
c4100eae 608void
79a490a9 609diagnostic_action_after_output (diagnostic_context *context,
c4100eae 610 diagnostic_t diag_kind)
9804f5fb 611{
c4100eae 612 switch (diag_kind)
9804f5fb
ZW
613 {
614 case DK_DEBUG:
615 case DK_NOTE:
616 case DK_ANACHRONISM:
617 case DK_WARNING:
618 break;
619
620 case DK_ERROR:
621 case DK_SORRY:
622 if (context->abort_on_error)
623 real_abort ();
243fbddd 624 if (context->fatal_errors)
c65a01af
RG
625 {
626 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
d0b8780d 627 diagnostic_finish (context);
c65a01af
RG
628 exit (FATAL_EXIT_CODE);
629 }
9804f5fb
ZW
630 break;
631
632 case DK_ICE:
b55f40c1 633 case DK_ICE_NOBT:
d83697f4 634 {
b55f40c1
JJ
635 struct backtrace_state *state = NULL;
636 if (diag_kind == DK_ICE)
637 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
d83697f4
ILT
638 int count = 0;
639 if (state != NULL)
640 backtrace_full (state, 2, bt_callback, bt_err_callback,
641 (void *) &count);
642
643 if (context->abort_on_error)
644 real_abort ();
645
646 fnotice (stderr, "Please submit a full bug report,\n"
647 "with preprocessed source if appropriate.\n");
648 if (count > 0)
649 fnotice (stderr,
650 ("Please include the complete backtrace "
651 "with any bug report.\n"));
652 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
9804f5fb 653
d83697f4
ILT
654 exit (ICE_EXIT_CODE);
655 }
9804f5fb
ZW
656
657 case DK_FATAL:
658 if (context->abort_on_error)
659 real_abort ();
d0b8780d 660 diagnostic_finish (context);
9804f5fb
ZW
661 fnotice (stderr, "compilation terminated.\n");
662 exit (FATAL_EXIT_CODE);
663
664 default:
d5706a1e 665 gcc_unreachable ();
9804f5fb
ZW
666 }
667}
668
26d5ed6c
DM
669/* True if the last module or file in which a diagnostic was reported is
670 different from the current one. */
671
672static bool
673last_module_changed_p (diagnostic_context *context,
674 const line_map_ordinary *map)
675{
676 return context->last_module != map;
677}
678
679/* Remember the current module or file as being the last one in which we
680 report a diagnostic. */
681
682static void
683set_last_module (diagnostic_context *context, const line_map_ordinary *map)
684{
685 context->last_module = map;
686}
687
6ed551b4 688void
07a0b324 689diagnostic_report_current_module (diagnostic_context *context, location_t where)
6ed551b4 690{
0e50b624 691 const line_map_ordinary *map = NULL;
6ed551b4 692
b6fe0bb8 693 if (pp_needs_newline (context->printer))
59650e48 694 {
b6fe0bb8
GDR
695 pp_newline (context->printer);
696 pp_needs_newline (context->printer) = false;
59650e48 697 }
6ed551b4 698
07a0b324 699 if (where <= BUILTINS_LOCATION)
966e8f4d
TT
700 return;
701
07a0b324
TT
702 linemap_resolve_location (line_table, where,
703 LRK_MACRO_DEFINITION_LOCATION,
704 &map);
705
26d5ed6c 706 if (map && last_module_changed_p (context, map))
59650e48 707 {
26d5ed6c 708 set_last_module (context, map);
966e8f4d 709 if (! MAIN_FILE_P (map))
6773e15f 710 {
204b61b9 711 bool first = true, need_inc = true, was_module = MAP_MODULE_P (map);
004bb936 712 expanded_location s = {};
9e525f08 713 do
966e8f4d 714 {
f10a9135
NS
715 where = linemap_included_from (map);
716 map = linemap_included_from_linemap (line_table, map);
204b61b9 717 bool is_module = MAP_MODULE_P (map);
004bb936
LH
718 s.file = LINEMAP_FILE (map);
719 s.line = SOURCE_LINE (map, where);
720 int col = -1;
721 if (first && context->show_column)
722 {
723 s.column = SOURCE_COLUMN (map, where);
724 col = diagnostic_converted_column (context, s);
725 }
726 const char *line_col = maybe_line_and_column (s.line, col);
9e525f08
NS
727 static const char *const msgs[] =
728 {
204b61b9 729 NULL,
9e525f08 730 N_(" from"),
204b61b9
NS
731 N_("In file included from"), /* 2 */
732 N_(" included from"),
733 N_("In module"), /* 4 */
734 N_("of module"),
735 N_("In module imported at"), /* 6 */
736 N_("imported at"),
9e525f08 737 };
204b61b9
NS
738
739 unsigned index = (was_module ? 6 : is_module ? 4
740 : need_inc ? 2 : 0) + !first;
741
9e525f08 742 pp_verbatim (context->printer, "%s%s %r%s%s%R",
204b61b9
NS
743 first ? "" : was_module ? ", " : ",\n",
744 _(msgs[index]),
004bb936 745 "locus", s.file, line_col);
204b61b9 746 first = false, need_inc = was_module, was_module = is_module;
966e8f4d 747 }
9e525f08 748 while (! MAIN_FILE_P (map));
966e8f4d
TT
749 pp_verbatim (context->printer, ":");
750 pp_newline (context->printer);
6773e15f 751 }
59650e48
ZW
752 }
753}
400500c4 754
4bc1899b
DM
755/* If DIAGNOSTIC has a diagnostic_path and CONTEXT supports printing paths,
756 print the path. */
757
758void
759diagnostic_show_any_path (diagnostic_context *context,
760 diagnostic_info *diagnostic)
761{
762 const diagnostic_path *path = diagnostic->richloc->get_path ();
763 if (!path)
764 return;
765
766 if (context->print_path)
767 context->print_path (context, path);
768}
769
770/* Return true if the events in this path involve more than one
771 function, or false if it is purely intraprocedural. */
772
773bool
774diagnostic_path::interprocedural_p () const
775{
776 const unsigned num = num_events ();
777 for (unsigned i = 0; i < num; i++)
778 {
779 if (get_event (i).get_fndecl () != get_event (0).get_fndecl ())
780 return true;
781 if (get_event (i).get_stack_depth () != get_event (0).get_stack_depth ())
782 return true;
783 }
784 return false;
785}
786
4537ec0c 787void
79a490a9
AJ
788default_diagnostic_starter (diagnostic_context *context,
789 diagnostic_info *diagnostic)
6ed551b4 790{
2a2703a2 791 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
243fbddd
JM
792 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
793 diagnostic));
59650e48 794}
6ed551b4 795
876217ae
DM
796void
797default_diagnostic_start_span_fn (diagnostic_context *context,
798 expanded_location exploc)
799{
5c6a2bf2
DM
800 char *text = diagnostic_get_location_text (context, exploc);
801 pp_string (context->printer, text);
802 free (text);
876217ae
DM
803 pp_newline (context->printer);
804}
805
4537ec0c 806void
18767f65 807default_diagnostic_finalizer (diagnostic_context *context,
478dd60d
DM
808 diagnostic_info *diagnostic,
809 diagnostic_t)
59650e48 810{
e9c9a142
DM
811 char *saved_prefix = pp_take_prefix (context->printer);
812 pp_set_prefix (context->printer, NULL);
d3e28653 813 pp_newline (context->printer);
cc015f3a 814 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
e9c9a142 815 pp_set_prefix (context->printer, saved_prefix);
01e1dea3 816 pp_flush (context->printer);
6ed551b4
GDR
817}
818
79cf5994
DD
819/* Interface to specify diagnostic kind overrides. Returns the
820 previous setting, or DK_UNSPECIFIED if the parameters are out of
3c8ca1ab
EB
821 range. If OPTION_INDEX is zero, the new setting is for all the
822 diagnostics. */
79cf5994
DD
823diagnostic_t
824diagnostic_classify_diagnostic (diagnostic_context *context,
825 int option_index,
cd7fe53b
DD
826 diagnostic_t new_kind,
827 location_t where)
79cf5994
DD
828{
829 diagnostic_t old_kind;
830
3c8ca1ab 831 if (option_index < 0
5f0f4a3b 832 || option_index >= context->n_opts
79cf5994
DD
833 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
834 return DK_UNSPECIFIED;
835
836 old_kind = context->classify_diagnostic[option_index];
cd7fe53b
DD
837
838 /* Handle pragmas separately, since we need to keep track of *where*
839 the pragmas were. */
840 if (where != UNKNOWN_LOCATION)
841 {
842 int i;
843
3ba421e8
MLI
844 /* Record the command-line status, so we can reset it back on DK_POP. */
845 if (old_kind == DK_UNSPECIFIED)
846 {
b32bc1ac 847 old_kind = !context->option_enabled (option_index,
fa5baeed 848 context->lang_mask,
b32bc1ac
MLI
849 context->option_state)
850 ? DK_IGNORED : (context->warning_as_error_requested
851 ? DK_ERROR : DK_WARNING);
3ba421e8
MLI
852 context->classify_diagnostic[option_index] = old_kind;
853 }
854
cd7fe53b
DD
855 for (i = context->n_classification_history - 1; i >= 0; i --)
856 if (context->classification_history[i].option == option_index)
857 {
858 old_kind = context->classification_history[i].kind;
859 break;
860 }
861
862 i = context->n_classification_history;
863 context->classification_history =
864 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
865 * sizeof (diagnostic_classification_change_t));
866 context->classification_history[i].location = where;
867 context->classification_history[i].option = option_index;
868 context->classification_history[i].kind = new_kind;
869 context->n_classification_history ++;
870 }
871 else
872 context->classify_diagnostic[option_index] = new_kind;
873
79cf5994
DD
874 return old_kind;
875}
876
cd7fe53b
DD
877/* Save all diagnostic classifications in a stack. */
878void
879diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
880{
881 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
882 context->push_list[context->n_push ++] = context->n_classification_history;
883}
884
885/* Restore the topmost classification set off the stack. If the stack
886 is empty, revert to the state based on command line parameters. */
887void
888diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
889{
890 int jump_to;
891 int i;
892
893 if (context->n_push)
894 jump_to = context->push_list [-- context->n_push];
895 else
896 jump_to = 0;
897
898 i = context->n_classification_history;
899 context->classification_history =
900 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
901 * sizeof (diagnostic_classification_change_t));
902 context->classification_history[i].location = where;
903 context->classification_history[i].option = jump_to;
904 context->classification_history[i].kind = DK_POP;
905 context->n_classification_history ++;
906}
907
a93eac6a
DM
908/* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
909 escaping rules for -fdiagnostics-parseable-fixits. */
910
911static void
912print_escaped_string (pretty_printer *pp, const char *text)
913{
914 gcc_assert (pp);
915 gcc_assert (text);
916
917 pp_character (pp, '"');
918 for (const char *ch = text; *ch; ch++)
919 {
920 switch (*ch)
921 {
922 case '\\':
923 /* Escape backslash as two backslashes. */
924 pp_string (pp, "\\\\");
925 break;
926 case '\t':
927 /* Escape tab as "\t". */
928 pp_string (pp, "\\t");
929 break;
930 case '\n':
931 /* Escape newline as "\n". */
932 pp_string (pp, "\\n");
933 break;
934 case '"':
935 /* Escape doublequotes as \". */
936 pp_string (pp, "\\\"");
937 break;
938 default:
939 if (ISPRINT (*ch))
940 pp_character (pp, *ch);
941 else
942 /* Use octal for non-printable chars. */
943 {
944 unsigned char c = (*ch & 0xff);
945 pp_printf (pp, "\\%o%o%o", (c / 64), (c / 8) & 007, c & 007);
946 }
947 break;
948 }
949 }
950 pp_character (pp, '"');
951}
952
f1096055
DM
953/* Implementation of -fdiagnostics-parseable-fixits and
954 GCC_EXTRA_DIAGNOSTIC_OUTPUT.
955 Print a machine-parseable version of all fixits in RICHLOC to PP,
956 using COLUMN_UNIT to express columns.
957 Use TABSTOP when handling DIAGNOSTICS_COLUMN_UNIT_DISPLAY. */
a93eac6a
DM
958
959static void
f1096055
DM
960print_parseable_fixits (pretty_printer *pp, rich_location *richloc,
961 enum diagnostics_column_unit column_unit,
962 int tabstop)
a93eac6a
DM
963{
964 gcc_assert (pp);
965 gcc_assert (richloc);
966
e9c9a142
DM
967 char *saved_prefix = pp_take_prefix (pp);
968 pp_set_prefix (pp, NULL);
969
a93eac6a
DM
970 for (unsigned i = 0; i < richloc->get_num_fixit_hints (); i++)
971 {
972 const fixit_hint *hint = richloc->get_fixit_hint (i);
620e594b 973 location_t start_loc = hint->get_start_loc ();
a93eac6a
DM
974 expanded_location start_exploc = expand_location (start_loc);
975 pp_string (pp, "fix-it:");
976 print_escaped_string (pp, start_exploc.file);
a93eac6a 977 /* For compatibility with clang, print as a half-open range. */
620e594b 978 location_t next_loc = hint->get_next_loc ();
338035aa 979 expanded_location next_exploc = expand_location (next_loc);
f1096055
DM
980 int start_col
981 = convert_column_unit (column_unit, tabstop, start_exploc);
982 int next_col
983 = convert_column_unit (column_unit, tabstop, next_exploc);
338035aa 984 pp_printf (pp, ":{%i:%i-%i:%i}:",
f1096055
DM
985 start_exploc.line, start_col,
986 next_exploc.line, next_col);
338035aa 987 print_escaped_string (pp, hint->get_string ());
a93eac6a
DM
988 pp_newline (pp);
989 }
e9c9a142
DM
990
991 pp_set_prefix (pp, saved_prefix);
a93eac6a
DM
992}
993
dc41c9b0
DM
994/* Update the diag_class of DIAGNOSTIC based on its location
995 relative to any
996 #pragma GCC diagnostic
997 directives recorded within CONTEXT.
998
999 Return the new diag_class of DIAGNOSTIC if it was updated, or
1000 DK_UNSPECIFIED otherwise. */
1001
1002static diagnostic_t
1003update_effective_level_from_pragmas (diagnostic_context *context,
1004 diagnostic_info *diagnostic)
1005{
1006 diagnostic_t diag_class = DK_UNSPECIFIED;
1007
1008 if (context->n_classification_history > 0)
1009 {
1010 location_t location = diagnostic_location (diagnostic);
1011
1012 /* FIXME: Stupid search. Optimize later. */
1013 for (int i = context->n_classification_history - 1; i >= 0; i --)
1014 {
1015 if (linemap_location_before_p
1016 (line_table,
1017 context->classification_history[i].location,
1018 location))
1019 {
1020 if (context->classification_history[i].kind == (int) DK_POP)
1021 {
1022 i = context->classification_history[i].option;
1023 continue;
1024 }
1025 int option = context->classification_history[i].option;
1026 /* The option 0 is for all the diagnostics. */
1027 if (option == 0 || option == diagnostic->option_index)
1028 {
1029 diag_class = context->classification_history[i].kind;
1030 if (diag_class != DK_UNSPECIFIED)
1031 diagnostic->kind = diag_class;
1032 break;
1033 }
1034 }
1035 }
1036 }
1037
1038 return diag_class;
1039}
1040
6d4a35ca
DM
1041/* Generate a URL string describing CWE. The caller is responsible for
1042 freeing the string. */
1043
1044static char *
1045get_cwe_url (int cwe)
1046{
1047 return xasprintf ("https://cwe.mitre.org/data/definitions/%i.html", cwe);
1048}
1049
1050/* If DIAGNOSTIC has a CWE identifier, print it.
1051
1052 For example, if the diagnostic metadata associates it with CWE-119,
1053 " [CWE-119]" will be printed, suitably colorized, and with a URL of a
1054 description of the security issue. */
1055
1056static void
1057print_any_cwe (diagnostic_context *context,
1058 const diagnostic_info *diagnostic)
1059{
1060 if (diagnostic->metadata == NULL)
1061 return;
1062
1063 int cwe = diagnostic->metadata->get_cwe ();
1064 if (cwe)
1065 {
1066 pretty_printer *pp = context->printer;
1067 char *saved_prefix = pp_take_prefix (context->printer);
1068 pp_string (pp, " [");
1069 pp_string (pp, colorize_start (pp_show_color (pp),
1070 diagnostic_kind_color[diagnostic->kind]));
abb48524
DM
1071 if (pp->url_format != URL_FORMAT_NONE)
1072 {
1073 char *cwe_url = get_cwe_url (cwe);
1074 pp_begin_url (pp, cwe_url);
1075 free (cwe_url);
1076 }
6d4a35ca
DM
1077 pp_printf (pp, "CWE-%i", cwe);
1078 pp_set_prefix (context->printer, saved_prefix);
abb48524
DM
1079 if (pp->url_format != URL_FORMAT_NONE)
1080 pp_end_url (pp);
6d4a35ca
DM
1081 pp_string (pp, colorize_stop (pp_show_color (pp)));
1082 pp_character (pp, ']');
1083 }
1084}
1085
80ceac09
DM
1086/* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's
1087 printer, e.g. " [-Werror=uninitialized]".
1088 Subroutine of diagnostic_report_diagnostic. */
1089
1090static void
1091print_option_information (diagnostic_context *context,
1092 const diagnostic_info *diagnostic,
1093 diagnostic_t orig_diag_kind)
1094{
1095 char *option_text;
1096
1097 option_text = context->option_name (context, diagnostic->option_index,
1098 orig_diag_kind, diagnostic->kind);
1099
1100 if (option_text)
1101 {
b4c7ca2e 1102 char *option_url = NULL;
abb48524
DM
1103 if (context->get_option_url
1104 && context->printer->url_format != URL_FORMAT_NONE)
b4c7ca2e
DM
1105 option_url = context->get_option_url (context,
1106 diagnostic->option_index);
80ceac09
DM
1107 pretty_printer *pp = context->printer;
1108 pp_string (pp, " [");
1109 pp_string (pp, colorize_start (pp_show_color (pp),
1110 diagnostic_kind_color[diagnostic->kind]));
b4c7ca2e
DM
1111 if (option_url)
1112 pp_begin_url (pp, option_url);
80ceac09 1113 pp_string (pp, option_text);
b4c7ca2e
DM
1114 if (option_url)
1115 {
1116 pp_end_url (pp);
1117 free (option_url);
1118 }
80ceac09
DM
1119 pp_string (pp, colorize_stop (pp_show_color (pp)));
1120 pp_character (pp, ']');
1121 free (option_text);
1122 }
1123}
dc41c9b0 1124
59650e48
ZW
1125/* Report a diagnostic message (an error or a warning) as specified by
1126 DC. This function is *the* subroutine in terms of which front-ends
1127 should implement their specific diagnostic handling modules. The
1128 front-end independent format specifiers are exactly those described
b8698a0f 1129 in the documentation of output_format.
71205d17 1130 Return true if a diagnostic was printed, false otherwise. */
400500c4 1131
71205d17 1132bool
79a490a9
AJ
1133diagnostic_report_diagnostic (diagnostic_context *context,
1134 diagnostic_info *diagnostic)
400500c4 1135{
2a2703a2 1136 location_t location = diagnostic_location (diagnostic);
d0b8780d 1137 diagnostic_t orig_diag_kind = diagnostic->kind;
4e2bae26
MLI
1138
1139 /* Give preference to being able to inhibit warnings, before they
1140 get reclassified to something else. */
71205d17 1141 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
5f0f4a3b 1142 && !diagnostic_report_warnings_p (context, location))
71205d17
MLI
1143 return false;
1144
b8698a0f 1145 if (diagnostic->kind == DK_PEDWARN)
d0b8780d 1146 {
243fbddd 1147 diagnostic->kind = pedantic_warning_kind (context);
d0b8780d
MLI
1148 /* We do this to avoid giving the message for -pedantic-errors. */
1149 orig_diag_kind = diagnostic->kind;
1150 }
0e94b750
MLI
1151
1152 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
1153 return false;
b8698a0f 1154
d5706a1e
ZW
1155 if (context->lock > 0)
1156 {
1157 /* If we're reporting an ICE in the middle of some other error,
1158 try to flush out the previous error, then let this one
1159 through. Don't do this more than once. */
b55f40c1
JJ
1160 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
1161 && context->lock == 1)
f8923f7e 1162 pp_newline_and_flush (context->printer);
d5706a1e
ZW
1163 else
1164 error_recursion (context);
1165 }
1166
4e2bae26
MLI
1167 /* If the user requested that warnings be treated as errors, so be
1168 it. Note that we do this before the next block so that
1169 individual warnings can be overridden back to warnings with
1170 -Wno-error=*. */
1171 if (context->warning_as_error_requested
1172 && diagnostic->kind == DK_WARNING)
03fd1ef6 1173 diagnostic->kind = DK_ERROR;
b8698a0f 1174
0c3641b0
MLI
1175 if (diagnostic->option_index
1176 && diagnostic->option_index != permissive_error_option (context))
79cf5994
DD
1177 {
1178 /* This tests if the user provided the appropriate -Wfoo or
1179 -Wno-foo option. */
46625112 1180 if (! context->option_enabled (diagnostic->option_index,
fa5baeed 1181 context->lang_mask,
46625112 1182 context->option_state))
71205d17 1183 return false;
cd7fe53b
DD
1184
1185 /* This tests for #pragma diagnostic changes. */
dc41c9b0
DM
1186 diagnostic_t diag_class
1187 = update_effective_level_from_pragmas (context, diagnostic);
d5e7854c 1188
79cf5994
DD
1189 /* This tests if the user provided the appropriate -Werror=foo
1190 option. */
cd7fe53b 1191 if (diag_class == DK_UNSPECIFIED
d5e7854c
NS
1192 && (context->classify_diagnostic[diagnostic->option_index]
1193 != DK_UNSPECIFIED))
1194 diagnostic->kind
1195 = context->classify_diagnostic[diagnostic->option_index];
1196
c0220ea4 1197 /* This allows for future extensions, like temporarily disabling
79cf5994
DD
1198 warnings for ranges of source code. */
1199 if (diagnostic->kind == DK_IGNORED)
71205d17 1200 return false;
79cf5994 1201 }
2098fe9e 1202
49ca22dd 1203 if (diagnostic->kind != DK_NOTE && diagnostic->kind != DK_ICE)
d0ea9f0a
NS
1204 diagnostic_check_max_errors (context);
1205
d5706a1e 1206 context->lock++;
400500c4 1207
b55f40c1 1208 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
59650e48 1209 {
52249a2e
MLI
1210 /* When not checking, ICEs are converted to fatal errors when an
1211 error has already occurred. This is counteracted by
1212 abort_on_error. */
b2b29377
MM
1213 if (!CHECKING_P
1214 && (diagnostic_kind_count (context, DK_ERROR) > 0
1215 || diagnostic_kind_count (context, DK_SORRY) > 0)
52249a2e
MLI
1216 && !context->abort_on_error)
1217 {
2a2703a2
MLI
1218 expanded_location s
1219 = expand_location (diagnostic_location (diagnostic));
52249a2e
MLI
1220 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1221 s.file, s.line);
1222 exit (ICE_EXIT_CODE);
1223 }
52249a2e 1224 if (context->internal_error)
a13812e2
JM
1225 (*context->internal_error) (context,
1226 diagnostic->message.format_spec,
52249a2e 1227 diagnostic->message.args_ptr);
43db5b3c 1228 }
37e99116
JJ
1229 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
1230 ++diagnostic_kind_count (context, DK_WERROR);
1231 else
1232 ++diagnostic_kind_count (context, diagnostic->kind);
b8698a0f 1233
097f82ec
DM
1234 /* Is this the initial diagnostic within the stack of groups? */
1235 if (context->diagnostic_group_emission_count == 0)
1236 {
1237 if (context->begin_group_cb)
1238 context->begin_group_cb (context);
1239 }
1240 context->diagnostic_group_emission_count++;
1241
cf835838
JM
1242 diagnostic->message.x_data = &diagnostic->x_data;
1243 diagnostic->x_data = NULL;
52249a2e
MLI
1244 pp_format (context->printer, &diagnostic->message);
1245 (*diagnostic_starter (context)) (context, diagnostic);
1246 pp_output_formatted_text (context->printer);
6d4a35ca
DM
1247 if (context->show_cwe)
1248 print_any_cwe (context, diagnostic);
80ceac09
DM
1249 if (context->show_option_requested)
1250 print_option_information (context, diagnostic, orig_diag_kind);
478dd60d 1251 (*diagnostic_finalizer (context)) (context, diagnostic, orig_diag_kind);
f1096055 1252 switch (context->extra_output_kind)
a93eac6a 1253 {
f1096055
DM
1254 default:
1255 break;
1256 case EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1:
1257 print_parseable_fixits (context->printer, diagnostic->richloc,
1258 DIAGNOSTICS_COLUMN_UNIT_BYTE,
1259 context->tabstop);
1260 pp_flush (context->printer);
1261 break;
1262 case EXTRA_DIAGNOSTIC_OUTPUT_fixits_v2:
1263 print_parseable_fixits (context->printer, diagnostic->richloc,
1264 DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
1265 context->tabstop);
a93eac6a 1266 pp_flush (context->printer);
f1096055 1267 break;
a93eac6a 1268 }
c4100eae 1269 diagnostic_action_after_output (context, diagnostic->kind);
cf835838 1270 diagnostic->x_data = NULL;
9804f5fb 1271
717ebe91 1272 if (context->edit_context_ptr)
b09649fd
DM
1273 if (diagnostic->richloc->fixits_can_be_auto_applied_p ())
1274 context->edit_context_ptr->add_fixits (diagnostic->richloc);
717ebe91 1275
9804f5fb 1276 context->lock--;
71205d17 1277
4bc1899b
DM
1278 diagnostic_show_any_path (context, diagnostic);
1279
71205d17 1280 return true;
59650e48 1281}
43db5b3c 1282
bc65bad2
MG
1283/* Get the number of digits in the decimal representation of VALUE. */
1284
1285int
1286num_digits (int value)
1287{
1288 /* Perhaps simpler to use log10 for this, but doing it this way avoids
1289 using floating point. */
1290 gcc_assert (value >= 0);
1291
1292 if (value == 0)
1293 return 1;
1294
1295 int digits = 0;
1296 while (value > 0)
1297 {
1298 digits++;
1299 value /= 10;
1300 }
1301 return digits;
1302}
1303
59650e48
ZW
1304/* Given a partial pathname as input, return another pathname that
1305 shares no directory elements with the pathname of __FILE__. This
1306 is used by fancy_abort() to print `Internal compiler error in expr.c'
1307 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
3a538a66 1308
59650e48 1309const char *
79a490a9 1310trim_filename (const char *name)
59650e48
ZW
1311{
1312 static const char this_file[] = __FILE__;
1313 const char *p = name, *q = this_file;
43db5b3c 1314
59650e48
ZW
1315 /* First skip any "../" in each filename. This allows us to give a proper
1316 reference to a file in a subdirectory. */
d5706a1e 1317 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
59650e48
ZW
1318 p += 3;
1319
d5706a1e 1320 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
59650e48
ZW
1321 q += 3;
1322
1323 /* Now skip any parts the two filenames have in common. */
1324 while (*p == *q && *p != 0 && *q != 0)
1325 p++, q++;
1326
1327 /* Now go backwards until the previous directory separator. */
ef9af077 1328 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
59650e48
ZW
1329 p--;
1330
1331 return p;
6ed551b4 1332}
59650e48
ZW
1333\f
1334/* Standard error reporting routines in increasing order of severity.
1335 All of these take arguments like printf. */
6ed551b4 1336
59650e48
ZW
1337/* Text to be emitted verbatim to the error message stream; this
1338 produces no prefix and disables line-wrapping. Use rarely. */
6ed551b4 1339void
4b794eaf 1340verbatim (const char *gmsgid, ...)
59650e48
ZW
1341{
1342 text_info text;
e34d07f2 1343 va_list ap;
59650e48 1344
4b794eaf 1345 va_start (ap, gmsgid);
fa6ef813 1346 text.err_no = errno;
59650e48 1347 text.args_ptr = &ap;
4b794eaf 1348 text.format_spec = _(gmsgid);
cf835838 1349 text.x_data = NULL;
b6fe0bb8 1350 pp_format_verbatim (global_dc->printer, &text);
f8923f7e 1351 pp_newline_and_flush (global_dc->printer);
e34d07f2 1352 va_end (ap);
59650e48
ZW
1353}
1354
dfa32261
MLI
1355/* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1356void
1357diagnostic_append_note (diagnostic_context *context,
1358 location_t location,
1359 const char * gmsgid, ...)
1360{
1361 diagnostic_info diagnostic;
1362 va_list ap;
ebedc9a3 1363 rich_location richloc (line_table, location);
dfa32261
MLI
1364
1365 va_start (ap, gmsgid);
8a645150
DM
1366 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
1367 if (context->inhibit_notes_p)
1368 {
1369 va_end (ap);
1370 return;
1371 }
653fee19 1372 char *saved_prefix = pp_take_prefix (context->printer);
8a645150
DM
1373 pp_set_prefix (context->printer,
1374 diagnostic_build_prefix (context, &diagnostic));
dfa32261
MLI
1375 pp_format (context->printer, &diagnostic.message);
1376 pp_output_formatted_text (context->printer);
1377 pp_destroy_prefix (context->printer);
4432aa6c 1378 pp_set_prefix (context->printer, saved_prefix);
d3e28653 1379 pp_newline (context->printer);
cc015f3a 1380 diagnostic_show_locus (context, &richloc, DK_NOTE);
c3284718 1381 va_end (ap);
dfa32261
MLI
1382}
1383
64a5912c
DM
1384/* Implement emit_diagnostic, inform, warning, warning_at, pedwarn,
1385 permerror, error, error_at, error_at, sorry, fatal_error, internal_error,
1386 and internal_error_no_backtrace, as documented and defined below. */
c3811744 1387static bool
6d4a35ca
DM
1388diagnostic_impl (rich_location *richloc, const diagnostic_metadata *metadata,
1389 int opt, const char *gmsgid,
c3811744 1390 va_list *ap, diagnostic_t kind)
6ed551b4 1391{
47b69537 1392 diagnostic_info diagnostic;
71205d17
MLI
1393 if (kind == DK_PERMERROR)
1394 {
c3811744 1395 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc,
243fbddd 1396 permissive_error_kind (global_dc));
5f0f4a3b 1397 diagnostic.option_index = permissive_error_option (global_dc);
71205d17 1398 }
c3811744
PC
1399 else
1400 {
1401 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind);
71205d17
MLI
1402 if (kind == DK_WARNING || kind == DK_PEDWARN)
1403 diagnostic.option_index = opt;
c3811744 1404 }
6d4a35ca 1405 diagnostic.metadata = metadata;
56d35585 1406 return diagnostic_report_diagnostic (global_dc, &diagnostic);
c3811744
PC
1407}
1408
64a5912c
DM
1409/* Implement inform_n, warning_n, and error_n, as documented and
1410 defined below. */
a5fb7ad2 1411static bool
6d4a35ca
DM
1412diagnostic_n_impl (rich_location *richloc, const diagnostic_metadata *metadata,
1413 int opt, unsigned HOST_WIDE_INT n,
64a5912c
DM
1414 const char *singular_gmsgid,
1415 const char *plural_gmsgid,
1416 va_list *ap, diagnostic_t kind)
a5fb7ad2
PK
1417{
1418 diagnostic_info diagnostic;
1c89478a
MS
1419 unsigned long gtn;
1420
1421 if (sizeof n <= sizeof gtn)
1422 gtn = n;
1423 else
1424 /* Use the largest number ngettext can handle, otherwise
1425 preserve the six least significant decimal digits for
1426 languages where the plural form depends on them. */
1427 gtn = n <= ULONG_MAX ? n : n % 1000000LU + 1000000LU;
1428
1429 const char *text = ngettext (singular_gmsgid, plural_gmsgid, gtn);
1430 diagnostic_set_info_translated (&diagnostic, text, ap, richloc, kind);
a5fb7ad2
PK
1431 if (kind == DK_WARNING)
1432 diagnostic.option_index = opt;
6d4a35ca 1433 diagnostic.metadata = metadata;
56d35585 1434 return diagnostic_report_diagnostic (global_dc, &diagnostic);
c3811744 1435}
71205d17 1436
296c53ac
MP
1437/* Wrapper around diagnostic_impl taking a variable argument list. */
1438
c3811744
PC
1439bool
1440emit_diagnostic (diagnostic_t kind, location_t location, int opt,
1441 const char *gmsgid, ...)
1442{
097f82ec 1443 auto_diagnostic_group d;
c3811744
PC
1444 va_list ap;
1445 va_start (ap, gmsgid);
1446 rich_location richloc (line_table, location);
6d4a35ca 1447 bool ret = diagnostic_impl (&richloc, NULL, opt, gmsgid, &ap, kind);
80f24351
MM
1448 va_end (ap);
1449 return ret;
6ed551b4
GDR
1450}
1451
8ba109ce
DM
1452/* As above, but for rich_location *. */
1453
1454bool
1455emit_diagnostic (diagnostic_t kind, rich_location *richloc, int opt,
1456 const char *gmsgid, ...)
1457{
1458 auto_diagnostic_group d;
1459 va_list ap;
1460 va_start (ap, gmsgid);
6d4a35ca 1461 bool ret = diagnostic_impl (richloc, NULL, opt, gmsgid, &ap, kind);
8ba109ce
DM
1462 va_end (ap);
1463 return ret;
1464}
1465
296c53ac
MP
1466/* Wrapper around diagnostic_impl taking a va_list parameter. */
1467
1468bool
1469emit_diagnostic_valist (diagnostic_t kind, location_t location, int opt,
1470 const char *gmsgid, va_list *ap)
1471{
1472 rich_location richloc (line_table, location);
6d4a35ca
DM
1473 return diagnostic_impl (&richloc, NULL, opt, gmsgid, ap, kind);
1474}
1475
1f5b3869 1476/* An informative note at LOCATION. Use this for additional details on an error
71205d17 1477 message. */
6ed551b4 1478void
1f5b3869 1479inform (location_t location, const char *gmsgid, ...)
8a645150 1480{
097f82ec 1481 auto_diagnostic_group d;
8a645150 1482 va_list ap;
8a645150 1483 va_start (ap, gmsgid);
c3811744 1484 rich_location richloc (line_table, location);
6d4a35ca 1485 diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_NOTE);
8a645150
DM
1486 va_end (ap);
1487}
1488
64a5912c 1489/* Same as "inform" above, but at RICHLOC. */
8a645150 1490void
64a5912c 1491inform (rich_location *richloc, const char *gmsgid, ...)
d4ee4d25 1492{
64a5912c
DM
1493 gcc_assert (richloc);
1494
097f82ec 1495 auto_diagnostic_group d;
d4ee4d25 1496 va_list ap;
4b794eaf 1497 va_start (ap, gmsgid);
6d4a35ca 1498 diagnostic_impl (richloc, NULL, -1, gmsgid, &ap, DK_NOTE);
d4ee4d25
DD
1499 va_end (ap);
1500}
1501
894e2652
SZ
1502/* An informative note at LOCATION. Use this for additional details on an
1503 error message. */
1504void
1c89478a
MS
1505inform_n (location_t location, unsigned HOST_WIDE_INT n,
1506 const char *singular_gmsgid, const char *plural_gmsgid, ...)
894e2652 1507{
894e2652 1508 va_list ap;
894e2652 1509 va_start (ap, plural_gmsgid);
097f82ec 1510 auto_diagnostic_group d;
64a5912c 1511 rich_location richloc (line_table, location);
6d4a35ca 1512 diagnostic_n_impl (&richloc, NULL, -1, n, singular_gmsgid, plural_gmsgid,
c3811744 1513 &ap, DK_NOTE);
894e2652
SZ
1514 va_end (ap);
1515}
1516
71205d17 1517/* A warning at INPUT_LOCATION. Use this for code which is correct according
b8698a0f 1518 to the relevant language specification but is likely to be buggy anyway.
71205d17
MLI
1519 Returns true if the warning was printed, false if it was inhibited. */
1520bool
1521warning (int opt, const char *gmsgid, ...)
6ed551b4 1522{
097f82ec 1523 auto_diagnostic_group d;
e34d07f2 1524 va_list ap;
4b794eaf 1525 va_start (ap, gmsgid);
c3811744 1526 rich_location richloc (line_table, input_location);
6d4a35ca 1527 bool ret = diagnostic_impl (&richloc, NULL, opt, gmsgid, &ap, DK_WARNING);
e34d07f2 1528 va_end (ap);
80f24351 1529 return ret;
6ed551b4
GDR
1530}
1531
aa14403d 1532/* A warning at LOCATION. Use this for code which is correct according to the
71205d17
MLI
1533 relevant language specification but is likely to be buggy anyway.
1534 Returns true if the warning was printed, false if it was inhibited. */
1535
1536bool
aa14403d 1537warning_at (location_t location, int opt, const char *gmsgid, ...)
8a645150 1538{
097f82ec 1539 auto_diagnostic_group d;
8a645150 1540 va_list ap;
8a645150 1541 va_start (ap, gmsgid);
c3811744 1542 rich_location richloc (line_table, location);
6d4a35ca 1543 bool ret = diagnostic_impl (&richloc, NULL, opt, gmsgid, &ap, DK_WARNING);
8a645150
DM
1544 va_end (ap);
1545 return ret;
1546}
1547
64a5912c 1548/* Same as "warning at" above, but using RICHLOC. */
8a645150
DM
1549
1550bool
64a5912c 1551warning_at (rich_location *richloc, int opt, const char *gmsgid, ...)
aa14403d 1552{
64a5912c
DM
1553 gcc_assert (richloc);
1554
097f82ec 1555 auto_diagnostic_group d;
aa14403d 1556 va_list ap;
aa14403d 1557 va_start (ap, gmsgid);
6d4a35ca
DM
1558 bool ret = diagnostic_impl (richloc, NULL, opt, gmsgid, &ap, DK_WARNING);
1559 va_end (ap);
1560 return ret;
1561}
1562
1563/* Same as "warning at" above, but using METADATA. */
1564
1565bool
6c8e5844
DM
1566warning_meta (rich_location *richloc,
1567 const diagnostic_metadata &metadata,
1568 int opt, const char *gmsgid, ...)
6d4a35ca
DM
1569{
1570 gcc_assert (richloc);
1571
1572 auto_diagnostic_group d;
1573 va_list ap;
1574 va_start (ap, gmsgid);
1575 bool ret
1576 = diagnostic_impl (richloc, &metadata, opt, gmsgid, &ap,
1577 DK_WARNING);
aa14403d 1578 va_end (ap);
80f24351 1579 return ret;
aa14403d
RAE
1580}
1581
64a5912c 1582/* Same as warning_n plural variant below, but using RICHLOC. */
a5fb7ad2
PK
1583
1584bool
1c89478a 1585warning_n (rich_location *richloc, int opt, unsigned HOST_WIDE_INT n,
64a5912c 1586 const char *singular_gmsgid, const char *plural_gmsgid, ...)
a5fb7ad2 1587{
64a5912c
DM
1588 gcc_assert (richloc);
1589
097f82ec 1590 auto_diagnostic_group d;
a5fb7ad2
PK
1591 va_list ap;
1592 va_start (ap, plural_gmsgid);
6d4a35ca 1593 bool ret = diagnostic_n_impl (richloc, NULL, opt, n,
64a5912c
DM
1594 singular_gmsgid, plural_gmsgid,
1595 &ap, DK_WARNING);
a5fb7ad2
PK
1596 va_end (ap);
1597 return ret;
1598}
1599
26e82579
JH
1600/* A warning at LOCATION. Use this for code which is correct according to the
1601 relevant language specification but is likely to be buggy anyway.
1602 Returns true if the warning was printed, false if it was inhibited. */
1603
1604bool
1c89478a
MS
1605warning_n (location_t location, int opt, unsigned HOST_WIDE_INT n,
1606 const char *singular_gmsgid, const char *plural_gmsgid, ...)
26e82579 1607{
097f82ec 1608 auto_diagnostic_group d;
26e82579 1609 va_list ap;
26e82579 1610 va_start (ap, plural_gmsgid);
64a5912c 1611 rich_location richloc (line_table, location);
6d4a35ca 1612 bool ret = diagnostic_n_impl (&richloc, NULL, opt, n,
c3811744
PC
1613 singular_gmsgid, plural_gmsgid,
1614 &ap, DK_WARNING);
26e82579
JH
1615 va_end (ap);
1616 return ret;
1617}
1618
85790e66
MLI
1619/* A "pedantic" warning at LOCATION: issues a warning unless
1620 -pedantic-errors was given on the command line, in which case it
1621 issues an error. Use this for diagnostics required by the relevant
1622 language standard, if you have chosen not to make them errors.
9804f5fb
ZW
1623
1624 Note that these diagnostics are issued independent of the setting
c1771a20 1625 of the -Wpedantic command-line switch. To get a warning enabled
fcf73884 1626 only with that switch, use either "if (pedantic) pedwarn
c1771a20
MLI
1627 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1628 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
fcf73884 1629
71205d17
MLI
1630 Returns true if the warning was printed, false if it was inhibited. */
1631
85790e66 1632bool
509c9d60 1633pedwarn (location_t location, int opt, const char *gmsgid, ...)
85790e66 1634{
097f82ec 1635 auto_diagnostic_group d;
85790e66 1636 va_list ap;
85790e66 1637 va_start (ap, gmsgid);
c3811744 1638 rich_location richloc (line_table, location);
6d4a35ca 1639 bool ret = diagnostic_impl (&richloc, NULL, opt, gmsgid, &ap, DK_PEDWARN);
85790e66 1640 va_end (ap);
80f24351 1641 return ret;
85790e66
MLI
1642}
1643
64a5912c 1644/* Same as pedwarn above, but using RICHLOC. */
1a4f11c8
DM
1645
1646bool
64a5912c 1647pedwarn (rich_location *richloc, int opt, const char *gmsgid, ...)
1a4f11c8 1648{
64a5912c
DM
1649 gcc_assert (richloc);
1650
097f82ec 1651 auto_diagnostic_group d;
1a4f11c8
DM
1652 va_list ap;
1653 va_start (ap, gmsgid);
6d4a35ca 1654 bool ret = diagnostic_impl (richloc, NULL, opt, gmsgid, &ap, DK_PEDWARN);
1a4f11c8
DM
1655 va_end (ap);
1656 return ret;
1657}
1658
7e99f74b
MLI
1659/* A "permissive" error at LOCATION: issues an error unless
1660 -fpermissive was given on the command line, in which case it issues
1661 a warning. Use this for things that really should be errors but we
71205d17 1662 want to support legacy code.
7e99f74b 1663
71205d17
MLI
1664 Returns true if the warning was printed, false if it was inhibited. */
1665
1666bool
cbe5f3b3 1667permerror (location_t location, const char *gmsgid, ...)
7e99f74b 1668{
097f82ec 1669 auto_diagnostic_group d;
7e99f74b 1670 va_list ap;
7e99f74b 1671 va_start (ap, gmsgid);
c3811744 1672 rich_location richloc (line_table, location);
6d4a35ca 1673 bool ret = diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_PERMERROR);
8a645150
DM
1674 va_end (ap);
1675 return ret;
1676}
1677
64a5912c 1678/* Same as "permerror" above, but at RICHLOC. */
8a645150
DM
1679
1680bool
64a5912c 1681permerror (rich_location *richloc, const char *gmsgid, ...)
8a645150 1682{
64a5912c
DM
1683 gcc_assert (richloc);
1684
097f82ec 1685 auto_diagnostic_group d;
8a645150 1686 va_list ap;
8a645150 1687 va_start (ap, gmsgid);
6d4a35ca 1688 bool ret = diagnostic_impl (richloc, NULL, -1, gmsgid, &ap, DK_PERMERROR);
7e99f74b 1689 va_end (ap);
80f24351 1690 return ret;
7e99f74b
MLI
1691}
1692
59650e48
ZW
1693/* A hard error: the code is definitely ill-formed, and an object file
1694 will not be produced. */
8514e318 1695void
4b794eaf 1696error (const char *gmsgid, ...)
8514e318 1697{
097f82ec 1698 auto_diagnostic_group d;
e34d07f2 1699 va_list ap;
4b794eaf 1700 va_start (ap, gmsgid);
c3811744 1701 rich_location richloc (line_table, input_location);
6d4a35ca 1702 diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_ERROR);
e34d07f2 1703 va_end (ap);
93d87cb1
GDR
1704}
1705
894e2652
SZ
1706/* A hard error: the code is definitely ill-formed, and an object file
1707 will not be produced. */
1708void
1c89478a
MS
1709error_n (location_t location, unsigned HOST_WIDE_INT n,
1710 const char *singular_gmsgid, const char *plural_gmsgid, ...)
894e2652 1711{
097f82ec 1712 auto_diagnostic_group d;
894e2652 1713 va_list ap;
894e2652 1714 va_start (ap, plural_gmsgid);
64a5912c 1715 rich_location richloc (line_table, location);
6d4a35ca 1716 diagnostic_n_impl (&richloc, NULL, -1, n, singular_gmsgid, plural_gmsgid,
c3811744 1717 &ap, DK_ERROR);
894e2652
SZ
1718 va_end (ap);
1719}
1720
c3811744 1721/* Same as above, but use location LOC instead of input_location. */
a63068b6
AH
1722void
1723error_at (location_t loc, const char *gmsgid, ...)
1724{
097f82ec 1725 auto_diagnostic_group d;
a63068b6 1726 va_list ap;
a63068b6 1727 va_start (ap, gmsgid);
c3811744 1728 rich_location richloc (line_table, loc);
6d4a35ca 1729 diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_ERROR);
8a645150
DM
1730 va_end (ap);
1731}
1732
1733/* Same as above, but use RICH_LOC. */
1734
1735void
64a5912c 1736error_at (rich_location *richloc, const char *gmsgid, ...)
8a645150 1737{
64a5912c
DM
1738 gcc_assert (richloc);
1739
097f82ec 1740 auto_diagnostic_group d;
8a645150 1741 va_list ap;
8a645150 1742 va_start (ap, gmsgid);
6d4a35ca 1743 diagnostic_impl (richloc, NULL, -1, gmsgid, &ap, DK_ERROR);
a63068b6
AH
1744 va_end (ap);
1745}
1746
59650e48
ZW
1747/* "Sorry, not implemented." Use for a language feature which is
1748 required by the relevant specification but not implemented by GCC.
1749 An object file will not be produced. */
93d87cb1 1750void
4b794eaf 1751sorry (const char *gmsgid, ...)
93d87cb1 1752{
097f82ec 1753 auto_diagnostic_group d;
e34d07f2 1754 va_list ap;
4b794eaf 1755 va_start (ap, gmsgid);
c3811744 1756 rich_location richloc (line_table, input_location);
6d4a35ca 1757 diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_SORRY);
e34d07f2 1758 va_end (ap);
59650e48 1759}
43db5b3c 1760
ad172f72
AS
1761/* Same as above, but use location LOC instead of input_location. */
1762void
1763sorry_at (location_t loc, const char *gmsgid, ...)
1764{
1765 auto_diagnostic_group d;
1766 va_list ap;
1767 va_start (ap, gmsgid);
1768 rich_location richloc (line_table, loc);
6d4a35ca 1769 diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_SORRY);
ad172f72
AS
1770 va_end (ap);
1771}
1772
1da2ed5f
JM
1773/* Return true if an error or a "sorry" has been seen. Various
1774 processing is disabled after errors. */
1775bool
1776seen_error (void)
1777{
1778 return errorcount || sorrycount;
1779}
1780
3d00119c
AB
1781/* An error which is severe enough that we make no attempt to
1782 continue. Do not use this for internal consistency checks; that's
1783 internal_error. Use of this function should be rare. */
1784void
1785fatal_error (location_t loc, const char *gmsgid, ...)
1786{
097f82ec 1787 auto_diagnostic_group d;
3d00119c 1788 va_list ap;
3d00119c 1789 va_start (ap, gmsgid);
c3811744 1790 rich_location richloc (line_table, loc);
6d4a35ca 1791 diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_FATAL);
3d00119c
AB
1792 va_end (ap);
1793
1794 gcc_unreachable ();
1795}
1796
59650e48
ZW
1797/* An internal consistency check has failed. We make no attempt to
1798 continue. Note that unless there is debugging value to be had from
1799 a more specific message, or some other good reason, you should use
1800 abort () instead of calling this function directly. */
1801void
4b794eaf 1802internal_error (const char *gmsgid, ...)
fbfc1192 1803{
097f82ec 1804 auto_diagnostic_group d;
e34d07f2 1805 va_list ap;
4b794eaf 1806 va_start (ap, gmsgid);
c3811744 1807 rich_location richloc (line_table, input_location);
6d4a35ca 1808 diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_ICE);
e34d07f2 1809 va_end (ap);
fbfc1192 1810
d5706a1e 1811 gcc_unreachable ();
fbfc1192 1812}
b55f40c1
JJ
1813
1814/* Like internal_error, but no backtrace will be printed. Used when
1815 the internal error does not happen at the current location, but happened
1816 somewhere else. */
1817void
1818internal_error_no_backtrace (const char *gmsgid, ...)
1819{
097f82ec 1820 auto_diagnostic_group d;
b55f40c1 1821 va_list ap;
b55f40c1 1822 va_start (ap, gmsgid);
c3811744 1823 rich_location richloc (line_table, input_location);
6d4a35ca 1824 diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_ICE_NOBT);
b55f40c1
JJ
1825 va_end (ap);
1826
1827 gcc_unreachable ();
1828}
59650e48 1829\f
59650e48
ZW
1830/* Special case error functions. Most are implemented in terms of the
1831 above, or should be. */
e23bd218 1832
59650e48
ZW
1833/* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1834 runs its second argument through gettext. */
04c1334c 1835void
4b794eaf 1836fnotice (FILE *file, const char *cmsgid, ...)
04c1334c 1837{
e34d07f2 1838 va_list ap;
04c1334c 1839
4b794eaf
JJ
1840 va_start (ap, cmsgid);
1841 vfprintf (file, _(cmsgid), ap);
e34d07f2 1842 va_end (ap);
04c1334c
GDR
1843}
1844
59650e48
ZW
1845/* Inform the user that an error occurred while trying to report some
1846 other error. This indicates catastrophic internal inconsistencies,
1847 so give up now. But do try to flush out the previous error.
1848 This mustn't use internal_error, that will cause infinite recursion. */
1849
1850static void
79a490a9 1851error_recursion (diagnostic_context *context)
59650e48
ZW
1852{
1853 if (context->lock < 3)
f8923f7e 1854 pp_newline_and_flush (context->printer);
59650e48
ZW
1855
1856 fnotice (stderr,
1857 "Internal compiler error: Error reporting routines re-entered.\n");
d5706a1e
ZW
1858
1859 /* Call diagnostic_action_after_output to get the "please submit a bug
c4100eae
MLI
1860 report" message. */
1861 diagnostic_action_after_output (context, DK_ICE);
d5706a1e
ZW
1862
1863 /* Do not use gcc_unreachable here; that goes through internal_error
1864 and therefore would cause infinite recursion. */
1865 real_abort ();
59650e48
ZW
1866}
1867
1868/* Report an internal compiler error in a friendly manner. This is
1869 the function that gets called upon use of abort() in the source
1870 code generally, thanks to a special macro. */
1871
1872void
79a490a9 1873fancy_abort (const char *file, int line, const char *function)
59650e48 1874{
387f6c15
DM
1875 /* If fancy_abort is called before the diagnostic subsystem is initialized,
1876 internal_error will crash internally in a way that prevents a
1877 useful message reaching the user.
1878 This can happen with libgccjit in the case of gcc_assert failures
1879 that occur outside of the libgccjit mutex that guards the rest of
1880 gcc's state, including global_dc (when global_dc may not be
1881 initialized yet, or might be in use by another thread).
1882 Handle such cases as gracefully as possible by falling back to a
1883 minimal abort handler that only relies on i18n. */
1884 if (global_dc->printer == NULL)
1885 {
1886 /* Print the error message. */
1887 fnotice (stderr, diagnostic_kind_text[DK_ICE]);
1888 fnotice (stderr, "in %s, at %s:%d", function, trim_filename (file), line);
1889 fputc ('\n', stderr);
1890
1891 /* Attempt to print a backtrace. */
1892 struct backtrace_state *state
1893 = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
1894 int count = 0;
1895 if (state != NULL)
1896 backtrace_full (state, 2, bt_callback, bt_err_callback,
1897 (void *) &count);
1898
1899 /* We can't call warn_if_plugins or emergency_dump_function as these
1900 rely on GCC state that might not be initialized, or might be in
1901 use by another thread. */
1902
1903 /* Abort the process. */
1904 real_abort ();
1905 }
1906
59650e48
ZW
1907 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1908}
1909
097f82ec
DM
1910/* class auto_diagnostic_group. */
1911
1912/* Constructor: "push" this group into global_dc. */
1913
1914auto_diagnostic_group::auto_diagnostic_group ()
1915{
1916 global_dc->diagnostic_group_nesting_depth++;
1917}
1918
1919/* Destructor: "pop" this group from global_dc. */
1920
1921auto_diagnostic_group::~auto_diagnostic_group ()
1922{
1923 if (--global_dc->diagnostic_group_nesting_depth == 0)
1924 {
1925 /* Handle the case where we've popped the final diagnostic group.
1926 If any diagnostics were emitted, give the context a chance
1927 to do something. */
1928 if (global_dc->diagnostic_group_emission_count > 0)
1929 {
1930 if (global_dc->end_group_cb)
1931 global_dc->end_group_cb (global_dc);
1932 }
1933 global_dc->diagnostic_group_emission_count = 0;
1934 }
1935}
1936
4bc1899b
DM
1937/* Implementation of diagnostic_path::num_events vfunc for
1938 simple_diagnostic_path: simply get the number of events in the vec. */
1939
1940unsigned
1941simple_diagnostic_path::num_events () const
1942{
1943 return m_events.length ();
1944}
1945
1946/* Implementation of diagnostic_path::get_event vfunc for
1947 simple_diagnostic_path: simply return the event in the vec. */
1948
1949const diagnostic_event &
1950simple_diagnostic_path::get_event (int idx) const
1951{
1952 return *m_events[idx];
1953}
1954
1955/* Add an event to this path at LOC within function FNDECL at
1956 stack depth DEPTH.
1957
1958 Use m_context's printer to format FMT, as the text of the new
1959 event.
1960
1961 Return the id of the new event. */
1962
1963diagnostic_event_id_t
1964simple_diagnostic_path::add_event (location_t loc, tree fndecl, int depth,
1965 const char *fmt, ...)
1966{
1967 pretty_printer *pp = m_event_pp;
1968 pp_clear_output_area (pp);
1969
1970 text_info ti;
1971 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1972
1973 va_list ap;
1974
1975 va_start (ap, fmt);
1976
1977 ti.format_spec = _(fmt);
1978 ti.args_ptr = &ap;
1979 ti.err_no = 0;
1980 ti.x_data = NULL;
1981 ti.m_richloc = &rich_loc;
1982
1983 pp_format (pp, &ti);
1984 pp_output_formatted_text (pp);
1985
1986 va_end (ap);
1987
1988 simple_diagnostic_event *new_event
1989 = new simple_diagnostic_event (loc, fndecl, depth, pp_formatted_text (pp));
1990 m_events.safe_push (new_event);
1991
1992 pp_clear_output_area (pp);
1993
1994 return diagnostic_event_id_t (m_events.length () - 1);
1995}
1996
1997/* struct simple_diagnostic_event. */
1998
1999/* simple_diagnostic_event's ctor. */
2000
2001simple_diagnostic_event::simple_diagnostic_event (location_t loc,
2002 tree fndecl,
2003 int depth,
2004 const char *desc)
2005: m_loc (loc), m_fndecl (fndecl), m_depth (depth), m_desc (xstrdup (desc))
2006{
2007}
2008
2009/* simple_diagnostic_event's dtor. */
2010
2011simple_diagnostic_event::~simple_diagnostic_event ()
2012{
2013 free (m_desc);
2014}
2015
2016/* Print PATH by emitting a dummy "note" associated with it. */
2017
2018DEBUG_FUNCTION
2019void debug (diagnostic_path *path)
2020{
2021 rich_location richloc (line_table, UNKNOWN_LOCATION);
2022 richloc.set_path (path);
2023 inform (&richloc, "debug path");
2024}
2025
886e0865
GK
2026/* Really call the system 'abort'. This has to go right at the end of
2027 this file, so that there are no functions after it that call abort
2028 and get the system abort instead of our macro. */
2029#undef abort
79a490a9
AJ
2030static void
2031real_abort (void)
886e0865
GK
2032{
2033 abort ();
2034}
a93eac6a
DM
2035
2036#if CHECKING_P
2037
2038namespace selftest {
2039
2040/* Helper function for test_print_escaped_string. */
2041
2042static void
2043assert_print_escaped_string (const location &loc, const char *expected_output,
2044 const char *input)
2045{
2046 pretty_printer pp;
2047 print_escaped_string (&pp, input);
2048 ASSERT_STREQ_AT (loc, expected_output, pp_formatted_text (&pp));
2049}
2050
2051#define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
2052 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
2053
2054/* Tests of print_escaped_string. */
2055
2056static void
2057test_print_escaped_string ()
2058{
2059 /* Empty string. */
2060 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
2061
2062 /* Non-empty string. */
2063 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
2064
2065 /* Various things that need to be escaped: */
2066 /* Backslash. */
2067 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
2068 "before\\after");
2069 /* Tab. */
2070 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
2071 "before\tafter");
2072 /* Newline. */
2073 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
2074 "before\nafter");
2075 /* Double quote. */
2076 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
2077 "before\"after");
2078
2079 /* Non-printable characters: BEL: '\a': 0x07 */
2080 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
2081 "before\aafter");
2082 /* Non-printable characters: vertical tab: '\v': 0x0b */
2083 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
2084 "before\vafter");
2085}
2086
2087/* Tests of print_parseable_fixits. */
2088
2089/* Verify that print_parseable_fixits emits the empty string if there
2090 are no fixits. */
2091
2092static void
2093test_print_parseable_fixits_none ()
2094{
2095 pretty_printer pp;
2096 rich_location richloc (line_table, UNKNOWN_LOCATION);
2097
f1096055 2098 print_parseable_fixits (&pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
a93eac6a
DM
2099 ASSERT_STREQ ("", pp_formatted_text (&pp));
2100}
2101
2102/* Verify that print_parseable_fixits does the right thing if there
2103 is an insertion fixit hint. */
2104
2105static void
2106test_print_parseable_fixits_insert ()
2107{
2108 pretty_printer pp;
2109 rich_location richloc (line_table, UNKNOWN_LOCATION);
2110
2111 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
2112 linemap_line_start (line_table, 5, 100);
2113 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
2114 location_t where = linemap_position_for_column (line_table, 10);
254830ba 2115 richloc.add_fixit_insert_before (where, "added content");
a93eac6a 2116
f1096055 2117 print_parseable_fixits (&pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
a93eac6a
DM
2118 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
2119 pp_formatted_text (&pp));
2120}
2121
2122/* Verify that print_parseable_fixits does the right thing if there
2123 is an removal fixit hint. */
2124
2125static void
2126test_print_parseable_fixits_remove ()
2127{
2128 pretty_printer pp;
2129 rich_location richloc (line_table, UNKNOWN_LOCATION);
2130
2131 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
2132 linemap_line_start (line_table, 5, 100);
2133 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
2134 source_range where;
2135 where.m_start = linemap_position_for_column (line_table, 10);
2136 where.m_finish = linemap_position_for_column (line_table, 20);
2137 richloc.add_fixit_remove (where);
2138
f1096055 2139 print_parseable_fixits (&pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
a93eac6a
DM
2140 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
2141 pp_formatted_text (&pp));
2142}
2143
2144/* Verify that print_parseable_fixits does the right thing if there
2145 is an replacement fixit hint. */
2146
2147static void
2148test_print_parseable_fixits_replace ()
2149{
2150 pretty_printer pp;
2151 rich_location richloc (line_table, UNKNOWN_LOCATION);
2152
2153 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
2154 linemap_line_start (line_table, 5, 100);
2155 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
2156 source_range where;
2157 where.m_start = linemap_position_for_column (line_table, 10);
2158 where.m_finish = linemap_position_for_column (line_table, 20);
2159 richloc.add_fixit_replace (where, "replacement");
2160
f1096055 2161 print_parseable_fixits (&pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE, 8);
a93eac6a
DM
2162 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
2163 pp_formatted_text (&pp));
2164}
2165
f1096055
DM
2166/* Verify that print_parseable_fixits correctly handles
2167 DIAGNOSTICS_COLUMN_UNIT_BYTE vs DIAGNOSTICS_COLUMN_UNIT_COLUMN. */
2168
2169static void
2170test_print_parseable_fixits_bytes_vs_display_columns ()
2171{
2172 line_table_test ltt;
2173 rich_location richloc (line_table, UNKNOWN_LOCATION);
2174
2175 /* 1-based byte offsets: 12345677778888999900001234567. */
2176 const char *const content = "smile \xf0\x9f\x98\x82 colour\n";
2177 /* 1-based display cols: 123456[......7-8.....]9012345. */
2178 const int tabstop = 8;
2179
2180 temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
2181 const char *const fname = tmp.get_filename ();
2182
2183 linemap_add (line_table, LC_ENTER, false, fname, 0);
2184 linemap_line_start (line_table, 1, 100);
2185 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
2186 source_range where;
2187 where.m_start = linemap_position_for_column (line_table, 12);
2188 where.m_finish = linemap_position_for_column (line_table, 17);
2189 richloc.add_fixit_replace (where, "color");
2190
2191 const int buf_len = strlen (fname) + 100;
2192 char *const expected = XNEWVEC (char, buf_len);
2193
2194 {
2195 pretty_printer pp;
2196 print_parseable_fixits (&pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_BYTE,
2197 tabstop);
2198 snprintf (expected, buf_len,
2199 "fix-it:\"%s\":{1:12-1:18}:\"color\"\n", fname);
2200 ASSERT_STREQ (expected, pp_formatted_text (&pp));
2201 }
2202 {
2203 pretty_printer pp;
2204 print_parseable_fixits (&pp, &richloc, DIAGNOSTICS_COLUMN_UNIT_DISPLAY,
2205 tabstop);
2206 snprintf (expected, buf_len,
2207 "fix-it:\"%s\":{1:10-1:16}:\"color\"\n", fname);
2208 ASSERT_STREQ (expected, pp_formatted_text (&pp));
2209 }
2210
2211 XDELETEVEC (expected);
2212}
2213
ace72598
DM
2214/* Verify that
2215 diagnostic_get_location_text (..., SHOW_COLUMN)
2216 generates EXPECTED_LOC_TEXT, given FILENAME, LINE, COLUMN, with
2217 colorization disabled. */
2218
2219static void
2220assert_location_text (const char *expected_loc_text,
2221 const char *filename, int line, int column,
004bb936
LH
2222 bool show_column,
2223 int origin = 1,
2224 enum diagnostics_column_unit column_unit
2225 = DIAGNOSTICS_COLUMN_UNIT_BYTE)
ace72598
DM
2226{
2227 test_diagnostic_context dc;
2228 dc.show_column = show_column;
004bb936
LH
2229 dc.column_unit = column_unit;
2230 dc.column_origin = origin;
ace72598
DM
2231
2232 expanded_location xloc;
2233 xloc.file = filename;
2234 xloc.line = line;
2235 xloc.column = column;
2236 xloc.data = NULL;
2237 xloc.sysp = false;
2238
2239 char *actual_loc_text = diagnostic_get_location_text (&dc, xloc);
2240 ASSERT_STREQ (expected_loc_text, actual_loc_text);
2241 free (actual_loc_text);
2242}
2243
2244/* Verify that diagnostic_get_location_text works as expected. */
2245
2246static void
2247test_diagnostic_get_location_text ()
2248{
2249 const char *old_progname = progname;
2250 progname = "PROGNAME";
2251 assert_location_text ("PROGNAME:", NULL, 0, 0, true);
2252 assert_location_text ("<built-in>:", "<built-in>", 42, 10, true);
2253 assert_location_text ("foo.c:42:10:", "foo.c", 42, 10, true);
004bb936
LH
2254 assert_location_text ("foo.c:42:9:", "foo.c", 42, 10, true, 0);
2255 assert_location_text ("foo.c:42:1010:", "foo.c", 42, 10, true, 1001);
2256 for (int origin = 0; origin != 2; ++origin)
2257 assert_location_text ("foo.c:42:", "foo.c", 42, 0, true, origin);
101e910b 2258 assert_location_text ("foo.c:", "foo.c", 0, 10, true);
ace72598 2259 assert_location_text ("foo.c:42:", "foo.c", 42, 10, false);
101e910b
NS
2260 assert_location_text ("foo.c:", "foo.c", 0, 10, false);
2261
2262 maybe_line_and_column (INT_MAX, INT_MAX);
2263 maybe_line_and_column (INT_MIN, INT_MIN);
2264
004bb936
LH
2265 {
2266 /* In order to test display columns vs byte columns, we need to create a
2267 file for location_get_source_line() to read. */
2268
2269 const char *const content = "smile \xf0\x9f\x98\x82\n";
2270 const int line_bytes = strlen (content) - 1;
2271 const int def_tabstop = 8;
2272 const int display_width = cpp_display_width (content, line_bytes,
2273 def_tabstop);
2274 ASSERT_EQ (line_bytes - 2, display_width);
2275 temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
2276 const char *const fname = tmp.get_filename ();
2277 const int buf_len = strlen (fname) + 16;
2278 char *const expected = XNEWVEC (char, buf_len);
2279
2280 snprintf (expected, buf_len, "%s:1:%d:", fname, line_bytes);
2281 assert_location_text (expected, fname, 1, line_bytes, true,
2282 1, DIAGNOSTICS_COLUMN_UNIT_BYTE);
2283
2284 snprintf (expected, buf_len, "%s:1:%d:", fname, line_bytes - 1);
2285 assert_location_text (expected, fname, 1, line_bytes, true,
2286 0, DIAGNOSTICS_COLUMN_UNIT_BYTE);
2287
2288 snprintf (expected, buf_len, "%s:1:%d:", fname, display_width);
2289 assert_location_text (expected, fname, 1, line_bytes, true,
2290 1, DIAGNOSTICS_COLUMN_UNIT_DISPLAY);
2291
2292 snprintf (expected, buf_len, "%s:1:%d:", fname, display_width - 1);
2293 assert_location_text (expected, fname, 1, line_bytes, true,
2294 0, DIAGNOSTICS_COLUMN_UNIT_DISPLAY);
2295
2296 XDELETEVEC (expected);
2297 }
2298
2299
ace72598
DM
2300 progname = old_progname;
2301}
2302
bc65bad2
MG
2303/* Selftest for num_digits. */
2304
2305static void
2306test_num_digits ()
2307{
2308 ASSERT_EQ (1, num_digits (0));
2309 ASSERT_EQ (1, num_digits (9));
2310 ASSERT_EQ (2, num_digits (10));
2311 ASSERT_EQ (2, num_digits (99));
2312 ASSERT_EQ (3, num_digits (100));
2313 ASSERT_EQ (3, num_digits (999));
2314 ASSERT_EQ (4, num_digits (1000));
2315 ASSERT_EQ (4, num_digits (9999));
2316 ASSERT_EQ (5, num_digits (10000));
2317 ASSERT_EQ (5, num_digits (99999));
2318 ASSERT_EQ (6, num_digits (100000));
2319 ASSERT_EQ (6, num_digits (999999));
2320 ASSERT_EQ (7, num_digits (1000000));
2321 ASSERT_EQ (7, num_digits (9999999));
2322 ASSERT_EQ (8, num_digits (10000000));
2323 ASSERT_EQ (8, num_digits (99999999));
2324}
2325
a93eac6a
DM
2326/* Run all of the selftests within this file. */
2327
2328void
2329diagnostic_c_tests ()
2330{
2331 test_print_escaped_string ();
2332 test_print_parseable_fixits_none ();
2333 test_print_parseable_fixits_insert ();
2334 test_print_parseable_fixits_remove ();
2335 test_print_parseable_fixits_replace ();
f1096055 2336 test_print_parseable_fixits_bytes_vs_display_columns ();
ace72598 2337 test_diagnostic_get_location_text ();
bc65bad2
MG
2338 test_num_digits ();
2339
a93eac6a
DM
2340}
2341
2342} // namespace selftest
2343
2344#endif /* #if CHECKING_P */
0ecf545c
MS
2345
2346#if __GNUC__ >= 10
2347# pragma GCC diagnostic pop
2348#endif