]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/diagnostic.c
pretty-print: support URL escape sequences (PR 87488)
[thirdparty/gcc.git] / gcc / diagnostic.c
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21
22 /* This file implements the language independent aspect of diagnostic
23 message module. */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "version.h"
29 #include "demangle.h"
30 #include "intl.h"
31 #include "backtrace.h"
32 #include "diagnostic.h"
33 #include "diagnostic-color.h"
34 #include "diagnostic-url.h"
35 #include "edit-context.h"
36 #include "selftest.h"
37 #include "selftest-diagnostic.h"
38 #include "opts.h"
39
40 #ifdef HAVE_TERMIOS_H
41 # include <termios.h>
42 #endif
43
44 #ifdef GWINSZ_IN_SYS_IOCTL
45 # include <sys/ioctl.h>
46 #endif
47
48 /* Disable warnings about quoting issues in the pp_xxx calls below
49 that (intentionally) don't follow GCC diagnostic conventions. */
50 #if __GNUC__ >= 10
51 # pragma GCC diagnostic push
52 # pragma GCC diagnostic ignored "-Wformat-diag"
53 #endif
54
55 #define pedantic_warning_kind(DC) \
56 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
57 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
58 #define permissive_error_option(DC) ((DC)->opt_permissive)
59
60 /* Prototypes. */
61 static bool diagnostic_impl (rich_location *, int, const char *,
62 va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(3,0);
63 static bool diagnostic_n_impl (rich_location *, int, unsigned HOST_WIDE_INT,
64 const char *, const char *, va_list *,
65 diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0);
66
67 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
68 static void real_abort (void) ATTRIBUTE_NORETURN;
69
70 /* Name of program invoked, sans directories. */
71
72 const char *progname;
73
74 /* A diagnostic_context surrogate for stderr. */
75 static diagnostic_context global_diagnostic_context;
76 diagnostic_context *global_dc = &global_diagnostic_context;
77 \f
78 /* Return a malloc'd string containing MSG formatted a la printf. The
79 caller is responsible for freeing the memory. */
80 char *
81 build_message_string (const char *msg, ...)
82 {
83 char *str;
84 va_list ap;
85
86 va_start (ap, msg);
87 str = xvasprintf (msg, ap);
88 va_end (ap);
89
90 return str;
91 }
92
93 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
94 char *
95 file_name_as_prefix (diagnostic_context *context, const char *f)
96 {
97 const char *locus_cs
98 = colorize_start (pp_show_color (context->printer), "locus");
99 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
100 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
101 }
102
103
104 \f
105 /* Return the value of the getenv("COLUMNS") as an integer. If the
106 value is not set to a positive integer, use ioctl to get the
107 terminal width. If it fails, return INT_MAX. */
108 int
109 get_terminal_width (void)
110 {
111 const char * s = getenv ("COLUMNS");
112 if (s != NULL) {
113 int n = atoi (s);
114 if (n > 0)
115 return n;
116 }
117
118 #ifdef TIOCGWINSZ
119 struct winsize w;
120 w.ws_col = 0;
121 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
122 return w.ws_col;
123 #endif
124
125 return INT_MAX;
126 }
127
128 /* Set caret_max_width to value. */
129 void
130 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
131 {
132 /* One minus to account for the leading empty space. */
133 value = value ? value - 1
134 : (isatty (fileno (pp_buffer (context->printer)->stream))
135 ? get_terminal_width () - 1: INT_MAX);
136
137 if (value <= 0)
138 value = INT_MAX;
139
140 context->caret_max_width = value;
141 }
142
143 /* Default implementation of final_cb. */
144
145 static void
146 default_diagnostic_final_cb (diagnostic_context *context)
147 {
148 /* Some of the errors may actually have been warnings. */
149 if (diagnostic_kind_count (context, DK_WERROR))
150 {
151 /* -Werror was given. */
152 if (context->warning_as_error_requested)
153 pp_verbatim (context->printer,
154 _("%s: all warnings being treated as errors"),
155 progname);
156 /* At least one -Werror= was given. */
157 else
158 pp_verbatim (context->printer,
159 _("%s: some warnings being treated as errors"),
160 progname);
161 pp_newline_and_flush (context->printer);
162 }
163 }
164
165 /* Initialize the diagnostic message outputting machinery. */
166 void
167 diagnostic_initialize (diagnostic_context *context, int n_opts)
168 {
169 int i;
170
171 /* Allocate a basic pretty-printer. Clients will replace this a
172 much more elaborated pretty-printer if they wish. */
173 context->printer = XNEW (pretty_printer);
174 new (context->printer) pretty_printer ();
175
176 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
177 context->warning_as_error_requested = false;
178 context->n_opts = n_opts;
179 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
180 for (i = 0; i < n_opts; i++)
181 context->classify_diagnostic[i] = DK_UNSPECIFIED;
182 context->show_caret = false;
183 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
184 for (i = 0; i < rich_location::STATICALLY_ALLOCATED_RANGES; i++)
185 context->caret_chars[i] = '^';
186 context->show_option_requested = false;
187 context->abort_on_error = false;
188 context->show_column = false;
189 context->pedantic_errors = false;
190 context->permissive = false;
191 context->opt_permissive = 0;
192 context->fatal_errors = false;
193 context->dc_inhibit_warnings = false;
194 context->dc_warn_system_headers = false;
195 context->max_errors = 0;
196 context->internal_error = NULL;
197 diagnostic_starter (context) = default_diagnostic_starter;
198 context->start_span = default_diagnostic_start_span_fn;
199 diagnostic_finalizer (context) = default_diagnostic_finalizer;
200 context->option_enabled = NULL;
201 context->option_state = NULL;
202 context->option_name = NULL;
203 context->last_location = UNKNOWN_LOCATION;
204 context->last_module = 0;
205 context->x_data = NULL;
206 context->lock = 0;
207 context->inhibit_notes_p = false;
208 context->colorize_source_p = false;
209 context->show_labels_p = false;
210 context->show_line_numbers_p = false;
211 context->min_margin_width = 0;
212 context->show_ruler_p = false;
213 context->parseable_fixits_p = false;
214 context->edit_context_ptr = NULL;
215 context->diagnostic_group_nesting_depth = 0;
216 context->diagnostic_group_emission_count = 0;
217 context->begin_group_cb = NULL;
218 context->end_group_cb = NULL;
219 context->final_cb = default_diagnostic_final_cb;
220 }
221
222 /* Maybe initialize the color support. We require clients to do this
223 explicitly, since most clients don't want color. When called
224 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
225
226 void
227 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
228 {
229 /* value == -1 is the default value. */
230 if (value < 0)
231 {
232 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
233 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
234 otherwise default to -fdiagnostics-color=never, for other
235 values default to that
236 -fdiagnostics-color={never,auto,always}. */
237 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
238 {
239 if (!getenv ("GCC_COLORS"))
240 return;
241 value = DIAGNOSTICS_COLOR_AUTO;
242 }
243 else
244 value = DIAGNOSTICS_COLOR_DEFAULT;
245 }
246 pp_show_color (context->printer)
247 = colorize_init ((diagnostic_color_rule_t) value);
248 }
249
250 /* Initialize URL support within CONTEXT based on VALUE, handling "auto". */
251
252 void
253 diagnostic_urls_init (diagnostic_context *context, int value /*= -1 */)
254 {
255 if (value < 0)
256 value = DIAGNOSTICS_COLOR_DEFAULT;
257
258 context->printer->show_urls
259 = diagnostic_urls_enabled_p ((diagnostic_url_rule_t) value);
260 }
261
262 /* Do any cleaning up required after the last diagnostic is emitted. */
263
264 void
265 diagnostic_finish (diagnostic_context *context)
266 {
267 if (context->final_cb)
268 context->final_cb (context);
269
270 diagnostic_file_cache_fini ();
271
272 XDELETEVEC (context->classify_diagnostic);
273 context->classify_diagnostic = NULL;
274
275 /* diagnostic_initialize allocates context->printer using XNEW
276 and placement-new. */
277 context->printer->~pretty_printer ();
278 XDELETE (context->printer);
279 context->printer = NULL;
280
281 if (context->edit_context_ptr)
282 {
283 delete context->edit_context_ptr;
284 context->edit_context_ptr = NULL;
285 }
286 }
287
288 /* Initialize DIAGNOSTIC, where the message MSG has already been
289 translated. */
290 void
291 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
292 va_list *args, rich_location *richloc,
293 diagnostic_t kind)
294 {
295 gcc_assert (richloc);
296 diagnostic->message.err_no = errno;
297 diagnostic->message.args_ptr = args;
298 diagnostic->message.format_spec = msg;
299 diagnostic->message.m_richloc = richloc;
300 diagnostic->richloc = richloc;
301 diagnostic->kind = kind;
302 diagnostic->option_index = 0;
303 }
304
305 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
306 translated. */
307 void
308 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
309 va_list *args, rich_location *richloc,
310 diagnostic_t kind)
311 {
312 gcc_assert (richloc);
313 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
314 }
315
316 static const char *const diagnostic_kind_color[] = {
317 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
318 #include "diagnostic.def"
319 #undef DEFINE_DIAGNOSTIC_KIND
320 NULL
321 };
322
323 /* Get a color name for diagnostics of type KIND
324 Result could be NULL. */
325
326 const char *
327 diagnostic_get_color_for_kind (diagnostic_t kind)
328 {
329 return diagnostic_kind_color[kind];
330 }
331
332 /* Return a formatted line and column ':%line:%column'. Elided if
333 zero. The result is a statically allocated buffer. */
334
335 static const char *
336 maybe_line_and_column (int line, int col)
337 {
338 static char result[32];
339
340 if (line)
341 {
342 size_t l = snprintf (result, sizeof (result),
343 col ? ":%d:%d" : ":%d", line, col);
344 gcc_checking_assert (l < sizeof (result));
345 }
346 else
347 result[0] = 0;
348 return result;
349 }
350
351 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
352 The caller is responsible for freeing the memory. */
353
354 static char *
355 diagnostic_get_location_text (diagnostic_context *context,
356 expanded_location s)
357 {
358 pretty_printer *pp = context->printer;
359 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
360 const char *locus_ce = colorize_stop (pp_show_color (pp));
361 const char *file = s.file ? s.file : progname;
362 int line = strcmp (file, N_("<built-in>")) ? s.line : 0;
363 int col = context->show_column ? s.column : 0;
364
365 const char *line_col = maybe_line_and_column (line, col);
366 return build_message_string ("%s%s%s:%s", locus_cs, file,
367 line_col, locus_ce);
368 }
369
370 /* Return a malloc'd string describing a location and the severity of the
371 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
372 freeing the memory. */
373 char *
374 diagnostic_build_prefix (diagnostic_context *context,
375 const diagnostic_info *diagnostic)
376 {
377 static const char *const diagnostic_kind_text[] = {
378 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
379 #include "diagnostic.def"
380 #undef DEFINE_DIAGNOSTIC_KIND
381 "must-not-happen"
382 };
383 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
384
385 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
386 const char *text_cs = "", *text_ce = "";
387 pretty_printer *pp = context->printer;
388
389 if (diagnostic_kind_color[diagnostic->kind])
390 {
391 text_cs = colorize_start (pp_show_color (pp),
392 diagnostic_kind_color[diagnostic->kind]);
393 text_ce = colorize_stop (pp_show_color (pp));
394 }
395
396 expanded_location s = diagnostic_expand_location (diagnostic);
397 char *location_text = diagnostic_get_location_text (context, s);
398
399 char *result = build_message_string ("%s %s%s%s", location_text,
400 text_cs, text, text_ce);
401 free (location_text);
402 return result;
403 }
404
405 /* Functions at which to stop the backtrace print. It's not
406 particularly helpful to print the callers of these functions. */
407
408 static const char * const bt_stop[] =
409 {
410 "main",
411 "toplev::main",
412 "execute_one_pass",
413 "compile_file",
414 };
415
416 /* A callback function passed to the backtrace_full function. */
417
418 static int
419 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
420 const char *function)
421 {
422 int *pcount = (int *) data;
423
424 /* If we don't have any useful information, don't print
425 anything. */
426 if (filename == NULL && function == NULL)
427 return 0;
428
429 /* Skip functions in diagnostic.c. */
430 if (*pcount == 0
431 && filename != NULL
432 && strcmp (lbasename (filename), "diagnostic.c") == 0)
433 return 0;
434
435 /* Print up to 20 functions. We could make this a --param, but
436 since this is only for debugging just use a constant for now. */
437 if (*pcount >= 20)
438 {
439 /* Returning a non-zero value stops the backtrace. */
440 return 1;
441 }
442 ++*pcount;
443
444 char *alc = NULL;
445 if (function != NULL)
446 {
447 char *str = cplus_demangle_v3 (function,
448 (DMGL_VERBOSE | DMGL_ANSI
449 | DMGL_GNU_V3 | DMGL_PARAMS));
450 if (str != NULL)
451 {
452 alc = str;
453 function = str;
454 }
455
456 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
457 {
458 size_t len = strlen (bt_stop[i]);
459 if (strncmp (function, bt_stop[i], len) == 0
460 && (function[len] == '\0' || function[len] == '('))
461 {
462 if (alc != NULL)
463 free (alc);
464 /* Returning a non-zero value stops the backtrace. */
465 return 1;
466 }
467 }
468 }
469
470 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
471 (unsigned long) pc,
472 function == NULL ? "???" : function,
473 filename == NULL ? "???" : filename,
474 lineno);
475
476 if (alc != NULL)
477 free (alc);
478
479 return 0;
480 }
481
482 /* A callback function passed to the backtrace_full function. This is
483 called if backtrace_full has an error. */
484
485 static void
486 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
487 {
488 if (errnum < 0)
489 {
490 /* This means that no debug info was available. Just quietly
491 skip printing backtrace info. */
492 return;
493 }
494 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
495 errnum == 0 ? "" : xstrerror (errnum));
496 }
497
498 /* Check if we've met the maximum error limit, and if so fatally exit
499 with a message. CONTEXT is the context to check, and FLUSH
500 indicates whether a diagnostic_finish call is needed. */
501
502 void
503 diagnostic_check_max_errors (diagnostic_context *context, bool flush)
504 {
505 if (!context->max_errors)
506 return;
507
508 int count = (diagnostic_kind_count (context, DK_ERROR)
509 + diagnostic_kind_count (context, DK_SORRY)
510 + diagnostic_kind_count (context, DK_WERROR));
511
512 if (count >= context->max_errors)
513 {
514 fnotice (stderr,
515 "compilation terminated due to -fmax-errors=%u.\n",
516 context->max_errors);
517 if (flush)
518 diagnostic_finish (context);
519 exit (FATAL_EXIT_CODE);
520 }
521 }
522
523 /* Take any action which is expected to happen after the diagnostic
524 is written out. This function does not always return. */
525 void
526 diagnostic_action_after_output (diagnostic_context *context,
527 diagnostic_t diag_kind)
528 {
529 switch (diag_kind)
530 {
531 case DK_DEBUG:
532 case DK_NOTE:
533 case DK_ANACHRONISM:
534 case DK_WARNING:
535 break;
536
537 case DK_ERROR:
538 case DK_SORRY:
539 if (context->abort_on_error)
540 real_abort ();
541 if (context->fatal_errors)
542 {
543 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
544 diagnostic_finish (context);
545 exit (FATAL_EXIT_CODE);
546 }
547 break;
548
549 case DK_ICE:
550 case DK_ICE_NOBT:
551 {
552 struct backtrace_state *state = NULL;
553 if (diag_kind == DK_ICE)
554 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
555 int count = 0;
556 if (state != NULL)
557 backtrace_full (state, 2, bt_callback, bt_err_callback,
558 (void *) &count);
559
560 if (context->abort_on_error)
561 real_abort ();
562
563 fnotice (stderr, "Please submit a full bug report,\n"
564 "with preprocessed source if appropriate.\n");
565 if (count > 0)
566 fnotice (stderr,
567 ("Please include the complete backtrace "
568 "with any bug report.\n"));
569 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
570
571 exit (ICE_EXIT_CODE);
572 }
573
574 case DK_FATAL:
575 if (context->abort_on_error)
576 real_abort ();
577 diagnostic_finish (context);
578 fnotice (stderr, "compilation terminated.\n");
579 exit (FATAL_EXIT_CODE);
580
581 default:
582 gcc_unreachable ();
583 }
584 }
585
586 /* True if the last module or file in which a diagnostic was reported is
587 different from the current one. */
588
589 static bool
590 last_module_changed_p (diagnostic_context *context,
591 const line_map_ordinary *map)
592 {
593 return context->last_module != map;
594 }
595
596 /* Remember the current module or file as being the last one in which we
597 report a diagnostic. */
598
599 static void
600 set_last_module (diagnostic_context *context, const line_map_ordinary *map)
601 {
602 context->last_module = map;
603 }
604
605 void
606 diagnostic_report_current_module (diagnostic_context *context, location_t where)
607 {
608 const line_map_ordinary *map = NULL;
609
610 if (pp_needs_newline (context->printer))
611 {
612 pp_newline (context->printer);
613 pp_needs_newline (context->printer) = false;
614 }
615
616 if (where <= BUILTINS_LOCATION)
617 return;
618
619 linemap_resolve_location (line_table, where,
620 LRK_MACRO_DEFINITION_LOCATION,
621 &map);
622
623 if (map && last_module_changed_p (context, map))
624 {
625 set_last_module (context, map);
626 if (! MAIN_FILE_P (map))
627 {
628 bool first = true;
629 do
630 {
631 where = linemap_included_from (map);
632 map = linemap_included_from_linemap (line_table, map);
633 const char *line_col
634 = maybe_line_and_column (SOURCE_LINE (map, where),
635 first && context->show_column
636 ? SOURCE_COLUMN (map, where) : 0);
637 static const char *const msgs[] =
638 {
639 N_("In file included from"),
640 N_(" from"),
641 };
642 unsigned index = !first;
643 pp_verbatim (context->printer, "%s%s %r%s%s%R",
644 first ? "" : ",\n", _(msgs[index]),
645 "locus", LINEMAP_FILE (map), line_col);
646 first = false;
647 }
648 while (! MAIN_FILE_P (map));
649 pp_verbatim (context->printer, ":");
650 pp_newline (context->printer);
651 }
652 }
653 }
654
655 void
656 default_diagnostic_starter (diagnostic_context *context,
657 diagnostic_info *diagnostic)
658 {
659 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
660 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
661 diagnostic));
662 }
663
664 void
665 default_diagnostic_start_span_fn (diagnostic_context *context,
666 expanded_location exploc)
667 {
668 char *text = diagnostic_get_location_text (context, exploc);
669 pp_string (context->printer, text);
670 free (text);
671 pp_newline (context->printer);
672 }
673
674 void
675 default_diagnostic_finalizer (diagnostic_context *context,
676 diagnostic_info *diagnostic,
677 diagnostic_t)
678 {
679 char *saved_prefix = pp_take_prefix (context->printer);
680 pp_set_prefix (context->printer, NULL);
681 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
682 pp_set_prefix (context->printer, saved_prefix);
683 pp_flush (context->printer);
684 }
685
686 /* Interface to specify diagnostic kind overrides. Returns the
687 previous setting, or DK_UNSPECIFIED if the parameters are out of
688 range. If OPTION_INDEX is zero, the new setting is for all the
689 diagnostics. */
690 diagnostic_t
691 diagnostic_classify_diagnostic (diagnostic_context *context,
692 int option_index,
693 diagnostic_t new_kind,
694 location_t where)
695 {
696 diagnostic_t old_kind;
697
698 if (option_index < 0
699 || option_index >= context->n_opts
700 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
701 return DK_UNSPECIFIED;
702
703 old_kind = context->classify_diagnostic[option_index];
704
705 /* Handle pragmas separately, since we need to keep track of *where*
706 the pragmas were. */
707 if (where != UNKNOWN_LOCATION)
708 {
709 int i;
710
711 /* Record the command-line status, so we can reset it back on DK_POP. */
712 if (old_kind == DK_UNSPECIFIED)
713 {
714 old_kind = !context->option_enabled (option_index,
715 context->lang_mask,
716 context->option_state)
717 ? DK_IGNORED : (context->warning_as_error_requested
718 ? DK_ERROR : DK_WARNING);
719 context->classify_diagnostic[option_index] = old_kind;
720 }
721
722 for (i = context->n_classification_history - 1; i >= 0; i --)
723 if (context->classification_history[i].option == option_index)
724 {
725 old_kind = context->classification_history[i].kind;
726 break;
727 }
728
729 i = context->n_classification_history;
730 context->classification_history =
731 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
732 * sizeof (diagnostic_classification_change_t));
733 context->classification_history[i].location = where;
734 context->classification_history[i].option = option_index;
735 context->classification_history[i].kind = new_kind;
736 context->n_classification_history ++;
737 }
738 else
739 context->classify_diagnostic[option_index] = new_kind;
740
741 return old_kind;
742 }
743
744 /* Save all diagnostic classifications in a stack. */
745 void
746 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
747 {
748 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
749 context->push_list[context->n_push ++] = context->n_classification_history;
750 }
751
752 /* Restore the topmost classification set off the stack. If the stack
753 is empty, revert to the state based on command line parameters. */
754 void
755 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
756 {
757 int jump_to;
758 int i;
759
760 if (context->n_push)
761 jump_to = context->push_list [-- context->n_push];
762 else
763 jump_to = 0;
764
765 i = context->n_classification_history;
766 context->classification_history =
767 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
768 * sizeof (diagnostic_classification_change_t));
769 context->classification_history[i].location = where;
770 context->classification_history[i].option = jump_to;
771 context->classification_history[i].kind = DK_POP;
772 context->n_classification_history ++;
773 }
774
775 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
776 escaping rules for -fdiagnostics-parseable-fixits. */
777
778 static void
779 print_escaped_string (pretty_printer *pp, const char *text)
780 {
781 gcc_assert (pp);
782 gcc_assert (text);
783
784 pp_character (pp, '"');
785 for (const char *ch = text; *ch; ch++)
786 {
787 switch (*ch)
788 {
789 case '\\':
790 /* Escape backslash as two backslashes. */
791 pp_string (pp, "\\\\");
792 break;
793 case '\t':
794 /* Escape tab as "\t". */
795 pp_string (pp, "\\t");
796 break;
797 case '\n':
798 /* Escape newline as "\n". */
799 pp_string (pp, "\\n");
800 break;
801 case '"':
802 /* Escape doublequotes as \". */
803 pp_string (pp, "\\\"");
804 break;
805 default:
806 if (ISPRINT (*ch))
807 pp_character (pp, *ch);
808 else
809 /* Use octal for non-printable chars. */
810 {
811 unsigned char c = (*ch & 0xff);
812 pp_printf (pp, "\\%o%o%o", (c / 64), (c / 8) & 007, c & 007);
813 }
814 break;
815 }
816 }
817 pp_character (pp, '"');
818 }
819
820 /* Implementation of -fdiagnostics-parseable-fixits. Print a
821 machine-parseable version of all fixits in RICHLOC to PP. */
822
823 static void
824 print_parseable_fixits (pretty_printer *pp, rich_location *richloc)
825 {
826 gcc_assert (pp);
827 gcc_assert (richloc);
828
829 char *saved_prefix = pp_take_prefix (pp);
830 pp_set_prefix (pp, NULL);
831
832 for (unsigned i = 0; i < richloc->get_num_fixit_hints (); i++)
833 {
834 const fixit_hint *hint = richloc->get_fixit_hint (i);
835 location_t start_loc = hint->get_start_loc ();
836 expanded_location start_exploc = expand_location (start_loc);
837 pp_string (pp, "fix-it:");
838 print_escaped_string (pp, start_exploc.file);
839 /* For compatibility with clang, print as a half-open range. */
840 location_t next_loc = hint->get_next_loc ();
841 expanded_location next_exploc = expand_location (next_loc);
842 pp_printf (pp, ":{%i:%i-%i:%i}:",
843 start_exploc.line, start_exploc.column,
844 next_exploc.line, next_exploc.column);
845 print_escaped_string (pp, hint->get_string ());
846 pp_newline (pp);
847 }
848
849 pp_set_prefix (pp, saved_prefix);
850 }
851
852 /* Update the diag_class of DIAGNOSTIC based on its location
853 relative to any
854 #pragma GCC diagnostic
855 directives recorded within CONTEXT.
856
857 Return the new diag_class of DIAGNOSTIC if it was updated, or
858 DK_UNSPECIFIED otherwise. */
859
860 static diagnostic_t
861 update_effective_level_from_pragmas (diagnostic_context *context,
862 diagnostic_info *diagnostic)
863 {
864 diagnostic_t diag_class = DK_UNSPECIFIED;
865
866 if (context->n_classification_history > 0)
867 {
868 location_t location = diagnostic_location (diagnostic);
869
870 /* FIXME: Stupid search. Optimize later. */
871 for (int i = context->n_classification_history - 1; i >= 0; i --)
872 {
873 if (linemap_location_before_p
874 (line_table,
875 context->classification_history[i].location,
876 location))
877 {
878 if (context->classification_history[i].kind == (int) DK_POP)
879 {
880 i = context->classification_history[i].option;
881 continue;
882 }
883 int option = context->classification_history[i].option;
884 /* The option 0 is for all the diagnostics. */
885 if (option == 0 || option == diagnostic->option_index)
886 {
887 diag_class = context->classification_history[i].kind;
888 if (diag_class != DK_UNSPECIFIED)
889 diagnostic->kind = diag_class;
890 break;
891 }
892 }
893 }
894 }
895
896 return diag_class;
897 }
898
899 /* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's
900 printer, e.g. " [-Werror=uninitialized]".
901 Subroutine of diagnostic_report_diagnostic. */
902
903 static void
904 print_option_information (diagnostic_context *context,
905 const diagnostic_info *diagnostic,
906 diagnostic_t orig_diag_kind)
907 {
908 char *option_text;
909
910 option_text = context->option_name (context, diagnostic->option_index,
911 orig_diag_kind, diagnostic->kind);
912
913 if (option_text)
914 {
915 pretty_printer *pp = context->printer;
916 pp_string (pp, " [");
917 pp_string (pp, colorize_start (pp_show_color (pp),
918 diagnostic_kind_color[diagnostic->kind]));
919 pp_string (pp, option_text);
920 pp_string (pp, colorize_stop (pp_show_color (pp)));
921 pp_character (pp, ']');
922 free (option_text);
923 }
924 }
925
926 /* Report a diagnostic message (an error or a warning) as specified by
927 DC. This function is *the* subroutine in terms of which front-ends
928 should implement their specific diagnostic handling modules. The
929 front-end independent format specifiers are exactly those described
930 in the documentation of output_format.
931 Return true if a diagnostic was printed, false otherwise. */
932
933 bool
934 diagnostic_report_diagnostic (diagnostic_context *context,
935 diagnostic_info *diagnostic)
936 {
937 location_t location = diagnostic_location (diagnostic);
938 diagnostic_t orig_diag_kind = diagnostic->kind;
939
940 /* Give preference to being able to inhibit warnings, before they
941 get reclassified to something else. */
942 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
943 && !diagnostic_report_warnings_p (context, location))
944 return false;
945
946 if (diagnostic->kind == DK_PEDWARN)
947 {
948 diagnostic->kind = pedantic_warning_kind (context);
949 /* We do this to avoid giving the message for -pedantic-errors. */
950 orig_diag_kind = diagnostic->kind;
951 }
952
953 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
954 return false;
955
956 if (context->lock > 0)
957 {
958 /* If we're reporting an ICE in the middle of some other error,
959 try to flush out the previous error, then let this one
960 through. Don't do this more than once. */
961 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
962 && context->lock == 1)
963 pp_newline_and_flush (context->printer);
964 else
965 error_recursion (context);
966 }
967
968 /* If the user requested that warnings be treated as errors, so be
969 it. Note that we do this before the next block so that
970 individual warnings can be overridden back to warnings with
971 -Wno-error=*. */
972 if (context->warning_as_error_requested
973 && diagnostic->kind == DK_WARNING)
974 diagnostic->kind = DK_ERROR;
975
976 if (diagnostic->option_index
977 && diagnostic->option_index != permissive_error_option (context))
978 {
979 /* This tests if the user provided the appropriate -Wfoo or
980 -Wno-foo option. */
981 if (! context->option_enabled (diagnostic->option_index,
982 context->lang_mask,
983 context->option_state))
984 return false;
985
986 /* This tests for #pragma diagnostic changes. */
987 diagnostic_t diag_class
988 = update_effective_level_from_pragmas (context, diagnostic);
989
990 /* This tests if the user provided the appropriate -Werror=foo
991 option. */
992 if (diag_class == DK_UNSPECIFIED
993 && (context->classify_diagnostic[diagnostic->option_index]
994 != DK_UNSPECIFIED))
995 diagnostic->kind
996 = context->classify_diagnostic[diagnostic->option_index];
997
998 /* This allows for future extensions, like temporarily disabling
999 warnings for ranges of source code. */
1000 if (diagnostic->kind == DK_IGNORED)
1001 return false;
1002 }
1003
1004 if (diagnostic->kind != DK_NOTE)
1005 diagnostic_check_max_errors (context);
1006
1007 context->lock++;
1008
1009 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
1010 {
1011 /* When not checking, ICEs are converted to fatal errors when an
1012 error has already occurred. This is counteracted by
1013 abort_on_error. */
1014 if (!CHECKING_P
1015 && (diagnostic_kind_count (context, DK_ERROR) > 0
1016 || diagnostic_kind_count (context, DK_SORRY) > 0)
1017 && !context->abort_on_error)
1018 {
1019 expanded_location s
1020 = expand_location (diagnostic_location (diagnostic));
1021 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
1022 s.file, s.line);
1023 exit (ICE_EXIT_CODE);
1024 }
1025 if (context->internal_error)
1026 (*context->internal_error) (context,
1027 diagnostic->message.format_spec,
1028 diagnostic->message.args_ptr);
1029 }
1030 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
1031 ++diagnostic_kind_count (context, DK_WERROR);
1032 else
1033 ++diagnostic_kind_count (context, diagnostic->kind);
1034
1035 /* Is this the initial diagnostic within the stack of groups? */
1036 if (context->diagnostic_group_emission_count == 0)
1037 {
1038 if (context->begin_group_cb)
1039 context->begin_group_cb (context);
1040 }
1041 context->diagnostic_group_emission_count++;
1042
1043 diagnostic->message.x_data = &diagnostic->x_data;
1044 diagnostic->x_data = NULL;
1045 pp_format (context->printer, &diagnostic->message);
1046 (*diagnostic_starter (context)) (context, diagnostic);
1047 pp_output_formatted_text (context->printer);
1048 if (context->show_option_requested)
1049 print_option_information (context, diagnostic, orig_diag_kind);
1050 (*diagnostic_finalizer (context)) (context, diagnostic, orig_diag_kind);
1051 if (context->parseable_fixits_p)
1052 {
1053 print_parseable_fixits (context->printer, diagnostic->richloc);
1054 pp_flush (context->printer);
1055 }
1056 diagnostic_action_after_output (context, diagnostic->kind);
1057 diagnostic->x_data = NULL;
1058
1059 if (context->edit_context_ptr)
1060 if (diagnostic->richloc->fixits_can_be_auto_applied_p ())
1061 context->edit_context_ptr->add_fixits (diagnostic->richloc);
1062
1063 context->lock--;
1064
1065 return true;
1066 }
1067
1068 /* Get the number of digits in the decimal representation of VALUE. */
1069
1070 int
1071 num_digits (int value)
1072 {
1073 /* Perhaps simpler to use log10 for this, but doing it this way avoids
1074 using floating point. */
1075 gcc_assert (value >= 0);
1076
1077 if (value == 0)
1078 return 1;
1079
1080 int digits = 0;
1081 while (value > 0)
1082 {
1083 digits++;
1084 value /= 10;
1085 }
1086 return digits;
1087 }
1088
1089 /* Given a partial pathname as input, return another pathname that
1090 shares no directory elements with the pathname of __FILE__. This
1091 is used by fancy_abort() to print `Internal compiler error in expr.c'
1092 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1093
1094 const char *
1095 trim_filename (const char *name)
1096 {
1097 static const char this_file[] = __FILE__;
1098 const char *p = name, *q = this_file;
1099
1100 /* First skip any "../" in each filename. This allows us to give a proper
1101 reference to a file in a subdirectory. */
1102 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
1103 p += 3;
1104
1105 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
1106 q += 3;
1107
1108 /* Now skip any parts the two filenames have in common. */
1109 while (*p == *q && *p != 0 && *q != 0)
1110 p++, q++;
1111
1112 /* Now go backwards until the previous directory separator. */
1113 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
1114 p--;
1115
1116 return p;
1117 }
1118 \f
1119 /* Standard error reporting routines in increasing order of severity.
1120 All of these take arguments like printf. */
1121
1122 /* Text to be emitted verbatim to the error message stream; this
1123 produces no prefix and disables line-wrapping. Use rarely. */
1124 void
1125 verbatim (const char *gmsgid, ...)
1126 {
1127 text_info text;
1128 va_list ap;
1129
1130 va_start (ap, gmsgid);
1131 text.err_no = errno;
1132 text.args_ptr = &ap;
1133 text.format_spec = _(gmsgid);
1134 text.x_data = NULL;
1135 pp_format_verbatim (global_dc->printer, &text);
1136 pp_newline_and_flush (global_dc->printer);
1137 va_end (ap);
1138 }
1139
1140 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1141 void
1142 diagnostic_append_note (diagnostic_context *context,
1143 location_t location,
1144 const char * gmsgid, ...)
1145 {
1146 diagnostic_info diagnostic;
1147 va_list ap;
1148 rich_location richloc (line_table, location);
1149
1150 va_start (ap, gmsgid);
1151 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
1152 if (context->inhibit_notes_p)
1153 {
1154 va_end (ap);
1155 return;
1156 }
1157 char *saved_prefix = pp_take_prefix (context->printer);
1158 pp_set_prefix (context->printer,
1159 diagnostic_build_prefix (context, &diagnostic));
1160 pp_format (context->printer, &diagnostic.message);
1161 pp_output_formatted_text (context->printer);
1162 pp_destroy_prefix (context->printer);
1163 pp_set_prefix (context->printer, saved_prefix);
1164 diagnostic_show_locus (context, &richloc, DK_NOTE);
1165 va_end (ap);
1166 }
1167
1168 /* Implement emit_diagnostic, inform, warning, warning_at, pedwarn,
1169 permerror, error, error_at, error_at, sorry, fatal_error, internal_error,
1170 and internal_error_no_backtrace, as documented and defined below. */
1171 static bool
1172 diagnostic_impl (rich_location *richloc, int opt,
1173 const char *gmsgid,
1174 va_list *ap, diagnostic_t kind)
1175 {
1176 diagnostic_info diagnostic;
1177 if (kind == DK_PERMERROR)
1178 {
1179 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc,
1180 permissive_error_kind (global_dc));
1181 diagnostic.option_index = permissive_error_option (global_dc);
1182 }
1183 else
1184 {
1185 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind);
1186 if (kind == DK_WARNING || kind == DK_PEDWARN)
1187 diagnostic.option_index = opt;
1188 }
1189 return diagnostic_report_diagnostic (global_dc, &diagnostic);
1190 }
1191
1192 /* Implement inform_n, warning_n, and error_n, as documented and
1193 defined below. */
1194 static bool
1195 diagnostic_n_impl (rich_location *richloc, int opt, unsigned HOST_WIDE_INT n,
1196 const char *singular_gmsgid,
1197 const char *plural_gmsgid,
1198 va_list *ap, diagnostic_t kind)
1199 {
1200 diagnostic_info diagnostic;
1201 unsigned long gtn;
1202
1203 if (sizeof n <= sizeof gtn)
1204 gtn = n;
1205 else
1206 /* Use the largest number ngettext can handle, otherwise
1207 preserve the six least significant decimal digits for
1208 languages where the plural form depends on them. */
1209 gtn = n <= ULONG_MAX ? n : n % 1000000LU + 1000000LU;
1210
1211 const char *text = ngettext (singular_gmsgid, plural_gmsgid, gtn);
1212 diagnostic_set_info_translated (&diagnostic, text, ap, richloc, kind);
1213 if (kind == DK_WARNING)
1214 diagnostic.option_index = opt;
1215 return diagnostic_report_diagnostic (global_dc, &diagnostic);
1216 }
1217
1218 /* Wrapper around diagnostic_impl taking a variable argument list. */
1219
1220 bool
1221 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
1222 const char *gmsgid, ...)
1223 {
1224 auto_diagnostic_group d;
1225 va_list ap;
1226 va_start (ap, gmsgid);
1227 rich_location richloc (line_table, location);
1228 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, kind);
1229 va_end (ap);
1230 return ret;
1231 }
1232
1233 /* As above, but for rich_location *. */
1234
1235 bool
1236 emit_diagnostic (diagnostic_t kind, rich_location *richloc, int opt,
1237 const char *gmsgid, ...)
1238 {
1239 auto_diagnostic_group d;
1240 va_list ap;
1241 va_start (ap, gmsgid);
1242 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, kind);
1243 va_end (ap);
1244 return ret;
1245 }
1246
1247 /* Wrapper around diagnostic_impl taking a va_list parameter. */
1248
1249 bool
1250 emit_diagnostic_valist (diagnostic_t kind, location_t location, int opt,
1251 const char *gmsgid, va_list *ap)
1252 {
1253 rich_location richloc (line_table, location);
1254 return diagnostic_impl (&richloc, opt, gmsgid, ap, kind);
1255 }
1256
1257 /* An informative note at LOCATION. Use this for additional details on an error
1258 message. */
1259 void
1260 inform (location_t location, const char *gmsgid, ...)
1261 {
1262 auto_diagnostic_group d;
1263 va_list ap;
1264 va_start (ap, gmsgid);
1265 rich_location richloc (line_table, location);
1266 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_NOTE);
1267 va_end (ap);
1268 }
1269
1270 /* Same as "inform" above, but at RICHLOC. */
1271 void
1272 inform (rich_location *richloc, const char *gmsgid, ...)
1273 {
1274 gcc_assert (richloc);
1275
1276 auto_diagnostic_group d;
1277 va_list ap;
1278 va_start (ap, gmsgid);
1279 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_NOTE);
1280 va_end (ap);
1281 }
1282
1283 /* An informative note at LOCATION. Use this for additional details on an
1284 error message. */
1285 void
1286 inform_n (location_t location, unsigned HOST_WIDE_INT n,
1287 const char *singular_gmsgid, const char *plural_gmsgid, ...)
1288 {
1289 va_list ap;
1290 va_start (ap, plural_gmsgid);
1291 auto_diagnostic_group d;
1292 rich_location richloc (line_table, location);
1293 diagnostic_n_impl (&richloc, -1, n, singular_gmsgid, plural_gmsgid,
1294 &ap, DK_NOTE);
1295 va_end (ap);
1296 }
1297
1298 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1299 to the relevant language specification but is likely to be buggy anyway.
1300 Returns true if the warning was printed, false if it was inhibited. */
1301 bool
1302 warning (int opt, const char *gmsgid, ...)
1303 {
1304 auto_diagnostic_group d;
1305 va_list ap;
1306 va_start (ap, gmsgid);
1307 rich_location richloc (line_table, input_location);
1308 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1309 va_end (ap);
1310 return ret;
1311 }
1312
1313 /* A warning at LOCATION. Use this for code which is correct according to the
1314 relevant language specification but is likely to be buggy anyway.
1315 Returns true if the warning was printed, false if it was inhibited. */
1316
1317 bool
1318 warning_at (location_t location, int opt, const char *gmsgid, ...)
1319 {
1320 auto_diagnostic_group d;
1321 va_list ap;
1322 va_start (ap, gmsgid);
1323 rich_location richloc (line_table, location);
1324 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1325 va_end (ap);
1326 return ret;
1327 }
1328
1329 /* Same as "warning at" above, but using RICHLOC. */
1330
1331 bool
1332 warning_at (rich_location *richloc, int opt, const char *gmsgid, ...)
1333 {
1334 gcc_assert (richloc);
1335
1336 auto_diagnostic_group d;
1337 va_list ap;
1338 va_start (ap, gmsgid);
1339 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_WARNING);
1340 va_end (ap);
1341 return ret;
1342 }
1343
1344 /* Same as warning_n plural variant below, but using RICHLOC. */
1345
1346 bool
1347 warning_n (rich_location *richloc, int opt, unsigned HOST_WIDE_INT n,
1348 const char *singular_gmsgid, const char *plural_gmsgid, ...)
1349 {
1350 gcc_assert (richloc);
1351
1352 auto_diagnostic_group d;
1353 va_list ap;
1354 va_start (ap, plural_gmsgid);
1355 bool ret = diagnostic_n_impl (richloc, opt, n,
1356 singular_gmsgid, plural_gmsgid,
1357 &ap, DK_WARNING);
1358 va_end (ap);
1359 return ret;
1360 }
1361
1362 /* A warning at LOCATION. Use this for code which is correct according to the
1363 relevant language specification but is likely to be buggy anyway.
1364 Returns true if the warning was printed, false if it was inhibited. */
1365
1366 bool
1367 warning_n (location_t location, int opt, unsigned HOST_WIDE_INT n,
1368 const char *singular_gmsgid, const char *plural_gmsgid, ...)
1369 {
1370 auto_diagnostic_group d;
1371 va_list ap;
1372 va_start (ap, plural_gmsgid);
1373 rich_location richloc (line_table, location);
1374 bool ret = diagnostic_n_impl (&richloc, opt, n,
1375 singular_gmsgid, plural_gmsgid,
1376 &ap, DK_WARNING);
1377 va_end (ap);
1378 return ret;
1379 }
1380
1381 /* A "pedantic" warning at LOCATION: issues a warning unless
1382 -pedantic-errors was given on the command line, in which case it
1383 issues an error. Use this for diagnostics required by the relevant
1384 language standard, if you have chosen not to make them errors.
1385
1386 Note that these diagnostics are issued independent of the setting
1387 of the -Wpedantic command-line switch. To get a warning enabled
1388 only with that switch, use either "if (pedantic) pedwarn
1389 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1390 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1391
1392 Returns true if the warning was printed, false if it was inhibited. */
1393
1394 bool
1395 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1396 {
1397 auto_diagnostic_group d;
1398 va_list ap;
1399 va_start (ap, gmsgid);
1400 rich_location richloc (line_table, location);
1401 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_PEDWARN);
1402 va_end (ap);
1403 return ret;
1404 }
1405
1406 /* Same as pedwarn above, but using RICHLOC. */
1407
1408 bool
1409 pedwarn (rich_location *richloc, int opt, const char *gmsgid, ...)
1410 {
1411 gcc_assert (richloc);
1412
1413 auto_diagnostic_group d;
1414 va_list ap;
1415 va_start (ap, gmsgid);
1416 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_PEDWARN);
1417 va_end (ap);
1418 return ret;
1419 }
1420
1421 /* A "permissive" error at LOCATION: issues an error unless
1422 -fpermissive was given on the command line, in which case it issues
1423 a warning. Use this for things that really should be errors but we
1424 want to support legacy code.
1425
1426 Returns true if the warning was printed, false if it was inhibited. */
1427
1428 bool
1429 permerror (location_t location, const char *gmsgid, ...)
1430 {
1431 auto_diagnostic_group d;
1432 va_list ap;
1433 va_start (ap, gmsgid);
1434 rich_location richloc (line_table, location);
1435 bool ret = diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_PERMERROR);
1436 va_end (ap);
1437 return ret;
1438 }
1439
1440 /* Same as "permerror" above, but at RICHLOC. */
1441
1442 bool
1443 permerror (rich_location *richloc, const char *gmsgid, ...)
1444 {
1445 gcc_assert (richloc);
1446
1447 auto_diagnostic_group d;
1448 va_list ap;
1449 va_start (ap, gmsgid);
1450 bool ret = diagnostic_impl (richloc, -1, gmsgid, &ap, DK_PERMERROR);
1451 va_end (ap);
1452 return ret;
1453 }
1454
1455 /* A hard error: the code is definitely ill-formed, and an object file
1456 will not be produced. */
1457 void
1458 error (const char *gmsgid, ...)
1459 {
1460 auto_diagnostic_group d;
1461 va_list ap;
1462 va_start (ap, gmsgid);
1463 rich_location richloc (line_table, input_location);
1464 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1465 va_end (ap);
1466 }
1467
1468 /* A hard error: the code is definitely ill-formed, and an object file
1469 will not be produced. */
1470 void
1471 error_n (location_t location, unsigned HOST_WIDE_INT n,
1472 const char *singular_gmsgid, const char *plural_gmsgid, ...)
1473 {
1474 auto_diagnostic_group d;
1475 va_list ap;
1476 va_start (ap, plural_gmsgid);
1477 rich_location richloc (line_table, location);
1478 diagnostic_n_impl (&richloc, -1, n, singular_gmsgid, plural_gmsgid,
1479 &ap, DK_ERROR);
1480 va_end (ap);
1481 }
1482
1483 /* Same as above, but use location LOC instead of input_location. */
1484 void
1485 error_at (location_t loc, const char *gmsgid, ...)
1486 {
1487 auto_diagnostic_group d;
1488 va_list ap;
1489 va_start (ap, gmsgid);
1490 rich_location richloc (line_table, loc);
1491 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1492 va_end (ap);
1493 }
1494
1495 /* Same as above, but use RICH_LOC. */
1496
1497 void
1498 error_at (rich_location *richloc, const char *gmsgid, ...)
1499 {
1500 gcc_assert (richloc);
1501
1502 auto_diagnostic_group d;
1503 va_list ap;
1504 va_start (ap, gmsgid);
1505 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_ERROR);
1506 va_end (ap);
1507 }
1508
1509 /* "Sorry, not implemented." Use for a language feature which is
1510 required by the relevant specification but not implemented by GCC.
1511 An object file will not be produced. */
1512 void
1513 sorry (const char *gmsgid, ...)
1514 {
1515 auto_diagnostic_group d;
1516 va_list ap;
1517 va_start (ap, gmsgid);
1518 rich_location richloc (line_table, input_location);
1519 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY);
1520 va_end (ap);
1521 }
1522
1523 /* Same as above, but use location LOC instead of input_location. */
1524 void
1525 sorry_at (location_t loc, const char *gmsgid, ...)
1526 {
1527 auto_diagnostic_group d;
1528 va_list ap;
1529 va_start (ap, gmsgid);
1530 rich_location richloc (line_table, loc);
1531 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY);
1532 va_end (ap);
1533 }
1534
1535 /* Return true if an error or a "sorry" has been seen. Various
1536 processing is disabled after errors. */
1537 bool
1538 seen_error (void)
1539 {
1540 return errorcount || sorrycount;
1541 }
1542
1543 /* An error which is severe enough that we make no attempt to
1544 continue. Do not use this for internal consistency checks; that's
1545 internal_error. Use of this function should be rare. */
1546 void
1547 fatal_error (location_t loc, const char *gmsgid, ...)
1548 {
1549 auto_diagnostic_group d;
1550 va_list ap;
1551 va_start (ap, gmsgid);
1552 rich_location richloc (line_table, loc);
1553 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_FATAL);
1554 va_end (ap);
1555
1556 gcc_unreachable ();
1557 }
1558
1559 /* An internal consistency check has failed. We make no attempt to
1560 continue. Note that unless there is debugging value to be had from
1561 a more specific message, or some other good reason, you should use
1562 abort () instead of calling this function directly. */
1563 void
1564 internal_error (const char *gmsgid, ...)
1565 {
1566 auto_diagnostic_group d;
1567 va_list ap;
1568 va_start (ap, gmsgid);
1569 rich_location richloc (line_table, input_location);
1570 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE);
1571 va_end (ap);
1572
1573 gcc_unreachable ();
1574 }
1575
1576 /* Like internal_error, but no backtrace will be printed. Used when
1577 the internal error does not happen at the current location, but happened
1578 somewhere else. */
1579 void
1580 internal_error_no_backtrace (const char *gmsgid, ...)
1581 {
1582 auto_diagnostic_group d;
1583 va_list ap;
1584 va_start (ap, gmsgid);
1585 rich_location richloc (line_table, input_location);
1586 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE_NOBT);
1587 va_end (ap);
1588
1589 gcc_unreachable ();
1590 }
1591 \f
1592 /* Special case error functions. Most are implemented in terms of the
1593 above, or should be. */
1594
1595 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1596 runs its second argument through gettext. */
1597 void
1598 fnotice (FILE *file, const char *cmsgid, ...)
1599 {
1600 va_list ap;
1601
1602 va_start (ap, cmsgid);
1603 vfprintf (file, _(cmsgid), ap);
1604 va_end (ap);
1605 }
1606
1607 /* Inform the user that an error occurred while trying to report some
1608 other error. This indicates catastrophic internal inconsistencies,
1609 so give up now. But do try to flush out the previous error.
1610 This mustn't use internal_error, that will cause infinite recursion. */
1611
1612 static void
1613 error_recursion (diagnostic_context *context)
1614 {
1615 if (context->lock < 3)
1616 pp_newline_and_flush (context->printer);
1617
1618 fnotice (stderr,
1619 "Internal compiler error: Error reporting routines re-entered.\n");
1620
1621 /* Call diagnostic_action_after_output to get the "please submit a bug
1622 report" message. */
1623 diagnostic_action_after_output (context, DK_ICE);
1624
1625 /* Do not use gcc_unreachable here; that goes through internal_error
1626 and therefore would cause infinite recursion. */
1627 real_abort ();
1628 }
1629
1630 /* Report an internal compiler error in a friendly manner. This is
1631 the function that gets called upon use of abort() in the source
1632 code generally, thanks to a special macro. */
1633
1634 void
1635 fancy_abort (const char *file, int line, const char *function)
1636 {
1637 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1638 }
1639
1640 /* class auto_diagnostic_group. */
1641
1642 /* Constructor: "push" this group into global_dc. */
1643
1644 auto_diagnostic_group::auto_diagnostic_group ()
1645 {
1646 global_dc->diagnostic_group_nesting_depth++;
1647 }
1648
1649 /* Destructor: "pop" this group from global_dc. */
1650
1651 auto_diagnostic_group::~auto_diagnostic_group ()
1652 {
1653 if (--global_dc->diagnostic_group_nesting_depth == 0)
1654 {
1655 /* Handle the case where we've popped the final diagnostic group.
1656 If any diagnostics were emitted, give the context a chance
1657 to do something. */
1658 if (global_dc->diagnostic_group_emission_count > 0)
1659 {
1660 if (global_dc->end_group_cb)
1661 global_dc->end_group_cb (global_dc);
1662 }
1663 global_dc->diagnostic_group_emission_count = 0;
1664 }
1665 }
1666
1667 /* Really call the system 'abort'. This has to go right at the end of
1668 this file, so that there are no functions after it that call abort
1669 and get the system abort instead of our macro. */
1670 #undef abort
1671 static void
1672 real_abort (void)
1673 {
1674 abort ();
1675 }
1676
1677 #if CHECKING_P
1678
1679 namespace selftest {
1680
1681 /* Helper function for test_print_escaped_string. */
1682
1683 static void
1684 assert_print_escaped_string (const location &loc, const char *expected_output,
1685 const char *input)
1686 {
1687 pretty_printer pp;
1688 print_escaped_string (&pp, input);
1689 ASSERT_STREQ_AT (loc, expected_output, pp_formatted_text (&pp));
1690 }
1691
1692 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1693 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1694
1695 /* Tests of print_escaped_string. */
1696
1697 static void
1698 test_print_escaped_string ()
1699 {
1700 /* Empty string. */
1701 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1702
1703 /* Non-empty string. */
1704 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1705
1706 /* Various things that need to be escaped: */
1707 /* Backslash. */
1708 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1709 "before\\after");
1710 /* Tab. */
1711 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1712 "before\tafter");
1713 /* Newline. */
1714 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1715 "before\nafter");
1716 /* Double quote. */
1717 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1718 "before\"after");
1719
1720 /* Non-printable characters: BEL: '\a': 0x07 */
1721 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1722 "before\aafter");
1723 /* Non-printable characters: vertical tab: '\v': 0x0b */
1724 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1725 "before\vafter");
1726 }
1727
1728 /* Tests of print_parseable_fixits. */
1729
1730 /* Verify that print_parseable_fixits emits the empty string if there
1731 are no fixits. */
1732
1733 static void
1734 test_print_parseable_fixits_none ()
1735 {
1736 pretty_printer pp;
1737 rich_location richloc (line_table, UNKNOWN_LOCATION);
1738
1739 print_parseable_fixits (&pp, &richloc);
1740 ASSERT_STREQ ("", pp_formatted_text (&pp));
1741 }
1742
1743 /* Verify that print_parseable_fixits does the right thing if there
1744 is an insertion fixit hint. */
1745
1746 static void
1747 test_print_parseable_fixits_insert ()
1748 {
1749 pretty_printer pp;
1750 rich_location richloc (line_table, UNKNOWN_LOCATION);
1751
1752 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1753 linemap_line_start (line_table, 5, 100);
1754 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1755 location_t where = linemap_position_for_column (line_table, 10);
1756 richloc.add_fixit_insert_before (where, "added content");
1757
1758 print_parseable_fixits (&pp, &richloc);
1759 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1760 pp_formatted_text (&pp));
1761 }
1762
1763 /* Verify that print_parseable_fixits does the right thing if there
1764 is an removal fixit hint. */
1765
1766 static void
1767 test_print_parseable_fixits_remove ()
1768 {
1769 pretty_printer pp;
1770 rich_location richloc (line_table, UNKNOWN_LOCATION);
1771
1772 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1773 linemap_line_start (line_table, 5, 100);
1774 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1775 source_range where;
1776 where.m_start = linemap_position_for_column (line_table, 10);
1777 where.m_finish = linemap_position_for_column (line_table, 20);
1778 richloc.add_fixit_remove (where);
1779
1780 print_parseable_fixits (&pp, &richloc);
1781 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1782 pp_formatted_text (&pp));
1783 }
1784
1785 /* Verify that print_parseable_fixits does the right thing if there
1786 is an replacement fixit hint. */
1787
1788 static void
1789 test_print_parseable_fixits_replace ()
1790 {
1791 pretty_printer pp;
1792 rich_location richloc (line_table, UNKNOWN_LOCATION);
1793
1794 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1795 linemap_line_start (line_table, 5, 100);
1796 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1797 source_range where;
1798 where.m_start = linemap_position_for_column (line_table, 10);
1799 where.m_finish = linemap_position_for_column (line_table, 20);
1800 richloc.add_fixit_replace (where, "replacement");
1801
1802 print_parseable_fixits (&pp, &richloc);
1803 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1804 pp_formatted_text (&pp));
1805 }
1806
1807 /* Verify that
1808 diagnostic_get_location_text (..., SHOW_COLUMN)
1809 generates EXPECTED_LOC_TEXT, given FILENAME, LINE, COLUMN, with
1810 colorization disabled. */
1811
1812 static void
1813 assert_location_text (const char *expected_loc_text,
1814 const char *filename, int line, int column,
1815 bool show_column)
1816 {
1817 test_diagnostic_context dc;
1818 dc.show_column = show_column;
1819
1820 expanded_location xloc;
1821 xloc.file = filename;
1822 xloc.line = line;
1823 xloc.column = column;
1824 xloc.data = NULL;
1825 xloc.sysp = false;
1826
1827 char *actual_loc_text = diagnostic_get_location_text (&dc, xloc);
1828 ASSERT_STREQ (expected_loc_text, actual_loc_text);
1829 free (actual_loc_text);
1830 }
1831
1832 /* Verify that diagnostic_get_location_text works as expected. */
1833
1834 static void
1835 test_diagnostic_get_location_text ()
1836 {
1837 const char *old_progname = progname;
1838 progname = "PROGNAME";
1839 assert_location_text ("PROGNAME:", NULL, 0, 0, true);
1840 assert_location_text ("<built-in>:", "<built-in>", 42, 10, true);
1841 assert_location_text ("foo.c:42:10:", "foo.c", 42, 10, true);
1842 assert_location_text ("foo.c:42:", "foo.c", 42, 0, true);
1843 assert_location_text ("foo.c:", "foo.c", 0, 10, true);
1844 assert_location_text ("foo.c:42:", "foo.c", 42, 10, false);
1845 assert_location_text ("foo.c:", "foo.c", 0, 10, false);
1846
1847 maybe_line_and_column (INT_MAX, INT_MAX);
1848 maybe_line_and_column (INT_MIN, INT_MIN);
1849
1850 progname = old_progname;
1851 }
1852
1853 /* Selftest for num_digits. */
1854
1855 static void
1856 test_num_digits ()
1857 {
1858 ASSERT_EQ (1, num_digits (0));
1859 ASSERT_EQ (1, num_digits (9));
1860 ASSERT_EQ (2, num_digits (10));
1861 ASSERT_EQ (2, num_digits (99));
1862 ASSERT_EQ (3, num_digits (100));
1863 ASSERT_EQ (3, num_digits (999));
1864 ASSERT_EQ (4, num_digits (1000));
1865 ASSERT_EQ (4, num_digits (9999));
1866 ASSERT_EQ (5, num_digits (10000));
1867 ASSERT_EQ (5, num_digits (99999));
1868 ASSERT_EQ (6, num_digits (100000));
1869 ASSERT_EQ (6, num_digits (999999));
1870 ASSERT_EQ (7, num_digits (1000000));
1871 ASSERT_EQ (7, num_digits (9999999));
1872 ASSERT_EQ (8, num_digits (10000000));
1873 ASSERT_EQ (8, num_digits (99999999));
1874 }
1875
1876 /* Run all of the selftests within this file. */
1877
1878 void
1879 diagnostic_c_tests ()
1880 {
1881 test_print_escaped_string ();
1882 test_print_parseable_fixits_none ();
1883 test_print_parseable_fixits_insert ();
1884 test_print_parseable_fixits_remove ();
1885 test_print_parseable_fixits_replace ();
1886 test_diagnostic_get_location_text ();
1887 test_num_digits ();
1888
1889 }
1890
1891 } // namespace selftest
1892
1893 #endif /* #if CHECKING_P */
1894
1895 #if __GNUC__ >= 10
1896 # pragma GCC diagnostic pop
1897 #endif