[PR c++/87185] ICE in prune-lambdas
[official-gcc.git] / gcc / diagnostic.c
blobaae0934d21a66a6a336a995eb05ee4f54167ebb2
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2018 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5 This file is part of GCC.
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.
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.
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/>. */
22 /* This file implements the language independent aspect of diagnostic
23 message module. */
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 "edit-context.h"
35 #include "selftest.h"
36 #include "selftest-diagnostic.h"
38 #ifdef HAVE_TERMIOS_H
39 # include <termios.h>
40 #endif
42 #ifdef GWINSZ_IN_SYS_IOCTL
43 # include <sys/ioctl.h>
44 #endif
46 #define pedantic_warning_kind(DC) \
47 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
48 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
49 #define permissive_error_option(DC) ((DC)->opt_permissive)
51 /* Prototypes. */
52 static bool diagnostic_impl (rich_location *, int, const char *,
53 va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(3,0);
54 static bool diagnostic_n_impl (rich_location *, int, unsigned HOST_WIDE_INT,
55 const char *, const char *, va_list *,
56 diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0);
58 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
59 static void real_abort (void) ATTRIBUTE_NORETURN;
61 /* Name of program invoked, sans directories. */
63 const char *progname;
65 /* A diagnostic_context surrogate for stderr. */
66 static diagnostic_context global_diagnostic_context;
67 diagnostic_context *global_dc = &global_diagnostic_context;
69 /* Return a malloc'd string containing MSG formatted a la printf. The
70 caller is responsible for freeing the memory. */
71 char *
72 build_message_string (const char *msg, ...)
74 char *str;
75 va_list ap;
77 va_start (ap, msg);
78 str = xvasprintf (msg, ap);
79 va_end (ap);
81 return str;
84 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
85 char *
86 file_name_as_prefix (diagnostic_context *context, const char *f)
88 const char *locus_cs
89 = colorize_start (pp_show_color (context->printer), "locus");
90 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
91 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
96 /* Return the value of the getenv("COLUMNS") as an integer. If the
97 value is not set to a positive integer, use ioctl to get the
98 terminal width. If it fails, return INT_MAX. */
99 int
100 get_terminal_width (void)
102 const char * s = getenv ("COLUMNS");
103 if (s != NULL) {
104 int n = atoi (s);
105 if (n > 0)
106 return n;
109 #ifdef TIOCGWINSZ
110 struct winsize w;
111 w.ws_col = 0;
112 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
113 return w.ws_col;
114 #endif
116 return INT_MAX;
119 /* Set caret_max_width to value. */
120 void
121 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
123 /* One minus to account for the leading empty space. */
124 value = value ? value - 1
125 : (isatty (fileno (pp_buffer (context->printer)->stream))
126 ? get_terminal_width () - 1: INT_MAX);
128 if (value <= 0)
129 value = INT_MAX;
131 context->caret_max_width = value;
134 /* Initialize the diagnostic message outputting machinery. */
135 void
136 diagnostic_initialize (diagnostic_context *context, int n_opts)
138 int i;
140 /* Allocate a basic pretty-printer. Clients will replace this a
141 much more elaborated pretty-printer if they wish. */
142 context->printer = XNEW (pretty_printer);
143 new (context->printer) pretty_printer ();
145 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
146 context->warning_as_error_requested = false;
147 context->n_opts = n_opts;
148 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
149 for (i = 0; i < n_opts; i++)
150 context->classify_diagnostic[i] = DK_UNSPECIFIED;
151 context->show_caret = false;
152 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
153 for (i = 0; i < rich_location::STATICALLY_ALLOCATED_RANGES; i++)
154 context->caret_chars[i] = '^';
155 context->show_option_requested = false;
156 context->abort_on_error = false;
157 context->show_column = false;
158 context->pedantic_errors = false;
159 context->permissive = false;
160 context->opt_permissive = 0;
161 context->fatal_errors = false;
162 context->dc_inhibit_warnings = false;
163 context->dc_warn_system_headers = false;
164 context->max_errors = 0;
165 context->internal_error = NULL;
166 diagnostic_starter (context) = default_diagnostic_starter;
167 context->start_span = default_diagnostic_start_span_fn;
168 diagnostic_finalizer (context) = default_diagnostic_finalizer;
169 context->option_enabled = NULL;
170 context->option_state = NULL;
171 context->option_name = NULL;
172 context->last_location = UNKNOWN_LOCATION;
173 context->last_module = 0;
174 context->x_data = NULL;
175 context->lock = 0;
176 context->inhibit_notes_p = false;
177 context->colorize_source_p = false;
178 context->show_labels_p = false;
179 context->show_line_numbers_p = false;
180 context->show_ruler_p = false;
181 context->parseable_fixits_p = false;
182 context->edit_context_ptr = NULL;
183 context->diagnostic_group_nesting_depth = 0;
184 context->diagnostic_group_emission_count = 0;
185 context->begin_group_cb = NULL;
186 context->end_group_cb = NULL;
189 /* Maybe initialize the color support. We require clients to do this
190 explicitly, since most clients don't want color. When called
191 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
193 void
194 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
196 /* value == -1 is the default value. */
197 if (value < 0)
199 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
200 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
201 otherwise default to -fdiagnostics-color=never, for other
202 values default to that
203 -fdiagnostics-color={never,auto,always}. */
204 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
206 if (!getenv ("GCC_COLORS"))
207 return;
208 value = DIAGNOSTICS_COLOR_AUTO;
210 else
211 value = DIAGNOSTICS_COLOR_DEFAULT;
213 pp_show_color (context->printer)
214 = colorize_init ((diagnostic_color_rule_t) value);
217 /* Do any cleaning up required after the last diagnostic is emitted. */
219 void
220 diagnostic_finish (diagnostic_context *context)
222 /* Some of the errors may actually have been warnings. */
223 if (diagnostic_kind_count (context, DK_WERROR))
225 /* -Werror was given. */
226 if (context->warning_as_error_requested)
227 pp_verbatim (context->printer,
228 _("%s: all warnings being treated as errors"),
229 progname);
230 /* At least one -Werror= was given. */
231 else
232 pp_verbatim (context->printer,
233 _("%s: some warnings being treated as errors"),
234 progname);
235 pp_newline_and_flush (context->printer);
238 diagnostic_file_cache_fini ();
240 XDELETEVEC (context->classify_diagnostic);
241 context->classify_diagnostic = NULL;
243 /* diagnostic_initialize allocates context->printer using XNEW
244 and placement-new. */
245 context->printer->~pretty_printer ();
246 XDELETE (context->printer);
247 context->printer = NULL;
249 if (context->edit_context_ptr)
251 delete context->edit_context_ptr;
252 context->edit_context_ptr = NULL;
256 /* Initialize DIAGNOSTIC, where the message MSG has already been
257 translated. */
258 void
259 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
260 va_list *args, rich_location *richloc,
261 diagnostic_t kind)
263 gcc_assert (richloc);
264 diagnostic->message.err_no = errno;
265 diagnostic->message.args_ptr = args;
266 diagnostic->message.format_spec = msg;
267 diagnostic->message.m_richloc = richloc;
268 diagnostic->richloc = richloc;
269 diagnostic->kind = kind;
270 diagnostic->option_index = 0;
273 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
274 translated. */
275 void
276 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
277 va_list *args, rich_location *richloc,
278 diagnostic_t kind)
280 gcc_assert (richloc);
281 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
284 static const char *const diagnostic_kind_color[] = {
285 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
286 #include "diagnostic.def"
287 #undef DEFINE_DIAGNOSTIC_KIND
288 NULL
291 /* Get a color name for diagnostics of type KIND
292 Result could be NULL. */
294 const char *
295 diagnostic_get_color_for_kind (diagnostic_t kind)
297 return diagnostic_kind_color[kind];
300 /* Return a formatted line and column ':%line:%column'. Elided if
301 zero. The result is a statically allocated buffer. */
303 static const char *
304 maybe_line_and_column (int line, int col)
306 static char result[32];
308 if (line)
310 size_t l = snprintf (result, sizeof (result),
311 col ? ":%d:%d" : ":%d", line, col);
312 gcc_checking_assert (l < sizeof (result));
314 else
315 result[0] = 0;
316 return result;
319 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
320 The caller is responsible for freeing the memory. */
322 static char *
323 diagnostic_get_location_text (diagnostic_context *context,
324 expanded_location s)
326 pretty_printer *pp = context->printer;
327 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
328 const char *locus_ce = colorize_stop (pp_show_color (pp));
329 const char *file = s.file ? s.file : progname;
330 int line = strcmp (file, N_("<built-in>")) ? s.line : 0;
331 int col = context->show_column ? s.column : 0;
333 const char *line_col = maybe_line_and_column (line, col);
334 return build_message_string ("%s%s%s:%s", locus_cs, file,
335 line_col, locus_ce);
338 /* Return a malloc'd string describing a location and the severity of the
339 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
340 freeing the memory. */
341 char *
342 diagnostic_build_prefix (diagnostic_context *context,
343 const diagnostic_info *diagnostic)
345 static const char *const diagnostic_kind_text[] = {
346 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
347 #include "diagnostic.def"
348 #undef DEFINE_DIAGNOSTIC_KIND
349 "must-not-happen"
351 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
353 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
354 const char *text_cs = "", *text_ce = "";
355 pretty_printer *pp = context->printer;
357 if (diagnostic_kind_color[diagnostic->kind])
359 text_cs = colorize_start (pp_show_color (pp),
360 diagnostic_kind_color[diagnostic->kind]);
361 text_ce = colorize_stop (pp_show_color (pp));
364 expanded_location s = diagnostic_expand_location (diagnostic);
365 char *location_text = diagnostic_get_location_text (context, s);
367 char *result = build_message_string ("%s %s%s%s", location_text,
368 text_cs, text, text_ce);
369 free (location_text);
370 return result;
373 /* Functions at which to stop the backtrace print. It's not
374 particularly helpful to print the callers of these functions. */
376 static const char * const bt_stop[] =
378 "main",
379 "toplev::main",
380 "execute_one_pass",
381 "compile_file",
384 /* A callback function passed to the backtrace_full function. */
386 static int
387 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
388 const char *function)
390 int *pcount = (int *) data;
392 /* If we don't have any useful information, don't print
393 anything. */
394 if (filename == NULL && function == NULL)
395 return 0;
397 /* Skip functions in diagnostic.c. */
398 if (*pcount == 0
399 && filename != NULL
400 && strcmp (lbasename (filename), "diagnostic.c") == 0)
401 return 0;
403 /* Print up to 20 functions. We could make this a --param, but
404 since this is only for debugging just use a constant for now. */
405 if (*pcount >= 20)
407 /* Returning a non-zero value stops the backtrace. */
408 return 1;
410 ++*pcount;
412 char *alc = NULL;
413 if (function != NULL)
415 char *str = cplus_demangle_v3 (function,
416 (DMGL_VERBOSE | DMGL_ANSI
417 | DMGL_GNU_V3 | DMGL_PARAMS));
418 if (str != NULL)
420 alc = str;
421 function = str;
424 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
426 size_t len = strlen (bt_stop[i]);
427 if (strncmp (function, bt_stop[i], len) == 0
428 && (function[len] == '\0' || function[len] == '('))
430 if (alc != NULL)
431 free (alc);
432 /* Returning a non-zero value stops the backtrace. */
433 return 1;
438 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
439 (unsigned long) pc,
440 function == NULL ? "???" : function,
441 filename == NULL ? "???" : filename,
442 lineno);
444 if (alc != NULL)
445 free (alc);
447 return 0;
450 /* A callback function passed to the backtrace_full function. This is
451 called if backtrace_full has an error. */
453 static void
454 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
456 if (errnum < 0)
458 /* This means that no debug info was available. Just quietly
459 skip printing backtrace info. */
460 return;
462 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
463 errnum == 0 ? "" : xstrerror (errnum));
466 /* Check if we've met the maximum error limit, and if so fatally exit
467 with a message. CONTEXT is the context to check, and FLUSH
468 indicates whether a diagnostic_finish call is needed. */
470 void
471 diagnostic_check_max_errors (diagnostic_context *context, bool flush)
473 if (!context->max_errors)
474 return;
476 int count = (diagnostic_kind_count (context, DK_ERROR)
477 + diagnostic_kind_count (context, DK_SORRY)
478 + diagnostic_kind_count (context, DK_WERROR));
480 if (count >= context->max_errors)
482 fnotice (stderr,
483 "compilation terminated due to -fmax-errors=%u.\n",
484 context->max_errors);
485 if (flush)
486 diagnostic_finish (context);
487 exit (FATAL_EXIT_CODE);
491 /* Take any action which is expected to happen after the diagnostic
492 is written out. This function does not always return. */
493 void
494 diagnostic_action_after_output (diagnostic_context *context,
495 diagnostic_t diag_kind)
497 switch (diag_kind)
499 case DK_DEBUG:
500 case DK_NOTE:
501 case DK_ANACHRONISM:
502 case DK_WARNING:
503 break;
505 case DK_ERROR:
506 case DK_SORRY:
507 if (context->abort_on_error)
508 real_abort ();
509 if (context->fatal_errors)
511 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
512 diagnostic_finish (context);
513 exit (FATAL_EXIT_CODE);
515 break;
517 case DK_ICE:
518 case DK_ICE_NOBT:
520 struct backtrace_state *state = NULL;
521 if (diag_kind == DK_ICE)
522 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
523 int count = 0;
524 if (state != NULL)
525 backtrace_full (state, 2, bt_callback, bt_err_callback,
526 (void *) &count);
528 if (context->abort_on_error)
529 real_abort ();
531 fnotice (stderr, "Please submit a full bug report,\n"
532 "with preprocessed source if appropriate.\n");
533 if (count > 0)
534 fnotice (stderr,
535 ("Please include the complete backtrace "
536 "with any bug report.\n"));
537 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
539 exit (ICE_EXIT_CODE);
542 case DK_FATAL:
543 if (context->abort_on_error)
544 real_abort ();
545 diagnostic_finish (context);
546 fnotice (stderr, "compilation terminated.\n");
547 exit (FATAL_EXIT_CODE);
549 default:
550 gcc_unreachable ();
554 /* True if the last module or file in which a diagnostic was reported is
555 different from the current one. */
557 static bool
558 last_module_changed_p (diagnostic_context *context,
559 const line_map_ordinary *map)
561 return context->last_module != map;
564 /* Remember the current module or file as being the last one in which we
565 report a diagnostic. */
567 static void
568 set_last_module (diagnostic_context *context, const line_map_ordinary *map)
570 context->last_module = map;
573 void
574 diagnostic_report_current_module (diagnostic_context *context, location_t where)
576 const line_map_ordinary *map = NULL;
578 if (pp_needs_newline (context->printer))
580 pp_newline (context->printer);
581 pp_needs_newline (context->printer) = false;
584 if (where <= BUILTINS_LOCATION)
585 return;
587 linemap_resolve_location (line_table, where,
588 LRK_MACRO_DEFINITION_LOCATION,
589 &map);
591 if (map && last_module_changed_p (context, map))
593 set_last_module (context, map);
594 if (! MAIN_FILE_P (map))
596 bool first = true;
599 where = linemap_included_from (map);
600 map = linemap_included_from_linemap (line_table, map);
601 const char *line_col
602 = maybe_line_and_column (SOURCE_LINE (map, where),
603 first && context->show_column
604 ? SOURCE_COLUMN (map, where) : 0);
605 static const char *const msgs[] =
607 N_("In file included from"),
608 N_(" from"),
610 unsigned index = !first;
611 pp_verbatim (context->printer, "%s%s %r%s%s%R",
612 first ? "" : ",\n", _(msgs[index]),
613 "locus", LINEMAP_FILE (map), line_col);
614 first = false;
616 while (! MAIN_FILE_P (map));
617 pp_verbatim (context->printer, ":");
618 pp_newline (context->printer);
623 void
624 default_diagnostic_starter (diagnostic_context *context,
625 diagnostic_info *diagnostic)
627 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
628 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
629 diagnostic));
632 void
633 default_diagnostic_start_span_fn (diagnostic_context *context,
634 expanded_location exploc)
636 char *text = diagnostic_get_location_text (context, exploc);
637 pp_string (context->printer, text);
638 free (text);
639 pp_newline (context->printer);
642 void
643 default_diagnostic_finalizer (diagnostic_context *context,
644 diagnostic_info *diagnostic)
646 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
647 pp_destroy_prefix (context->printer);
648 pp_flush (context->printer);
651 /* Interface to specify diagnostic kind overrides. Returns the
652 previous setting, or DK_UNSPECIFIED if the parameters are out of
653 range. If OPTION_INDEX is zero, the new setting is for all the
654 diagnostics. */
655 diagnostic_t
656 diagnostic_classify_diagnostic (diagnostic_context *context,
657 int option_index,
658 diagnostic_t new_kind,
659 location_t where)
661 diagnostic_t old_kind;
663 if (option_index < 0
664 || option_index >= context->n_opts
665 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
666 return DK_UNSPECIFIED;
668 old_kind = context->classify_diagnostic[option_index];
670 /* Handle pragmas separately, since we need to keep track of *where*
671 the pragmas were. */
672 if (where != UNKNOWN_LOCATION)
674 int i;
676 /* Record the command-line status, so we can reset it back on DK_POP. */
677 if (old_kind == DK_UNSPECIFIED)
679 old_kind = !context->option_enabled (option_index,
680 context->option_state)
681 ? DK_IGNORED : (context->warning_as_error_requested
682 ? DK_ERROR : DK_WARNING);
683 context->classify_diagnostic[option_index] = old_kind;
686 for (i = context->n_classification_history - 1; i >= 0; i --)
687 if (context->classification_history[i].option == option_index)
689 old_kind = context->classification_history[i].kind;
690 break;
693 i = context->n_classification_history;
694 context->classification_history =
695 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
696 * sizeof (diagnostic_classification_change_t));
697 context->classification_history[i].location = where;
698 context->classification_history[i].option = option_index;
699 context->classification_history[i].kind = new_kind;
700 context->n_classification_history ++;
702 else
703 context->classify_diagnostic[option_index] = new_kind;
705 return old_kind;
708 /* Save all diagnostic classifications in a stack. */
709 void
710 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
712 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
713 context->push_list[context->n_push ++] = context->n_classification_history;
716 /* Restore the topmost classification set off the stack. If the stack
717 is empty, revert to the state based on command line parameters. */
718 void
719 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
721 int jump_to;
722 int i;
724 if (context->n_push)
725 jump_to = context->push_list [-- context->n_push];
726 else
727 jump_to = 0;
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 = jump_to;
735 context->classification_history[i].kind = DK_POP;
736 context->n_classification_history ++;
739 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
740 escaping rules for -fdiagnostics-parseable-fixits. */
742 static void
743 print_escaped_string (pretty_printer *pp, const char *text)
745 gcc_assert (pp);
746 gcc_assert (text);
748 pp_character (pp, '"');
749 for (const char *ch = text; *ch; ch++)
751 switch (*ch)
753 case '\\':
754 /* Escape backslash as two backslashes. */
755 pp_string (pp, "\\\\");
756 break;
757 case '\t':
758 /* Escape tab as "\t". */
759 pp_string (pp, "\\t");
760 break;
761 case '\n':
762 /* Escape newline as "\n". */
763 pp_string (pp, "\\n");
764 break;
765 case '"':
766 /* Escape doublequotes as \". */
767 pp_string (pp, "\\\"");
768 break;
769 default:
770 if (ISPRINT (*ch))
771 pp_character (pp, *ch);
772 else
773 /* Use octal for non-printable chars. */
775 unsigned char c = (*ch & 0xff);
776 pp_printf (pp, "\\%o%o%o", (c / 64), (c / 8) & 007, c & 007);
778 break;
781 pp_character (pp, '"');
784 /* Implementation of -fdiagnostics-parseable-fixits. Print a
785 machine-parseable version of all fixits in RICHLOC to PP. */
787 static void
788 print_parseable_fixits (pretty_printer *pp, rich_location *richloc)
790 gcc_assert (pp);
791 gcc_assert (richloc);
793 for (unsigned i = 0; i < richloc->get_num_fixit_hints (); i++)
795 const fixit_hint *hint = richloc->get_fixit_hint (i);
796 source_location start_loc = hint->get_start_loc ();
797 expanded_location start_exploc = expand_location (start_loc);
798 pp_string (pp, "fix-it:");
799 print_escaped_string (pp, start_exploc.file);
800 /* For compatibility with clang, print as a half-open range. */
801 source_location next_loc = hint->get_next_loc ();
802 expanded_location next_exploc = expand_location (next_loc);
803 pp_printf (pp, ":{%i:%i-%i:%i}:",
804 start_exploc.line, start_exploc.column,
805 next_exploc.line, next_exploc.column);
806 print_escaped_string (pp, hint->get_string ());
807 pp_newline (pp);
811 /* Update the diag_class of DIAGNOSTIC based on its location
812 relative to any
813 #pragma GCC diagnostic
814 directives recorded within CONTEXT.
816 Return the new diag_class of DIAGNOSTIC if it was updated, or
817 DK_UNSPECIFIED otherwise. */
819 static diagnostic_t
820 update_effective_level_from_pragmas (diagnostic_context *context,
821 diagnostic_info *diagnostic)
823 diagnostic_t diag_class = DK_UNSPECIFIED;
825 if (context->n_classification_history > 0)
827 location_t location = diagnostic_location (diagnostic);
829 /* FIXME: Stupid search. Optimize later. */
830 for (int i = context->n_classification_history - 1; i >= 0; i --)
832 if (linemap_location_before_p
833 (line_table,
834 context->classification_history[i].location,
835 location))
837 if (context->classification_history[i].kind == (int) DK_POP)
839 i = context->classification_history[i].option;
840 continue;
842 int option = context->classification_history[i].option;
843 /* The option 0 is for all the diagnostics. */
844 if (option == 0 || option == diagnostic->option_index)
846 diag_class = context->classification_history[i].kind;
847 if (diag_class != DK_UNSPECIFIED)
848 diagnostic->kind = diag_class;
849 break;
855 return diag_class;
858 /* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's
859 printer, e.g. " [-Werror=uninitialized]".
860 Subroutine of diagnostic_report_diagnostic. */
862 static void
863 print_option_information (diagnostic_context *context,
864 const diagnostic_info *diagnostic,
865 diagnostic_t orig_diag_kind)
867 char *option_text;
869 option_text = context->option_name (context, diagnostic->option_index,
870 orig_diag_kind, diagnostic->kind);
872 if (option_text)
874 pretty_printer *pp = context->printer;
875 pp_string (pp, " [");
876 pp_string (pp, colorize_start (pp_show_color (pp),
877 diagnostic_kind_color[diagnostic->kind]));
878 pp_string (pp, option_text);
879 pp_string (pp, colorize_stop (pp_show_color (pp)));
880 pp_character (pp, ']');
881 free (option_text);
885 /* Report a diagnostic message (an error or a warning) as specified by
886 DC. This function is *the* subroutine in terms of which front-ends
887 should implement their specific diagnostic handling modules. The
888 front-end independent format specifiers are exactly those described
889 in the documentation of output_format.
890 Return true if a diagnostic was printed, false otherwise. */
892 bool
893 diagnostic_report_diagnostic (diagnostic_context *context,
894 diagnostic_info *diagnostic)
896 location_t location = diagnostic_location (diagnostic);
897 diagnostic_t orig_diag_kind = diagnostic->kind;
899 /* Give preference to being able to inhibit warnings, before they
900 get reclassified to something else. */
901 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
902 && !diagnostic_report_warnings_p (context, location))
903 return false;
905 if (diagnostic->kind == DK_PEDWARN)
907 diagnostic->kind = pedantic_warning_kind (context);
908 /* We do this to avoid giving the message for -pedantic-errors. */
909 orig_diag_kind = diagnostic->kind;
912 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
913 return false;
915 if (context->lock > 0)
917 /* If we're reporting an ICE in the middle of some other error,
918 try to flush out the previous error, then let this one
919 through. Don't do this more than once. */
920 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
921 && context->lock == 1)
922 pp_newline_and_flush (context->printer);
923 else
924 error_recursion (context);
927 /* If the user requested that warnings be treated as errors, so be
928 it. Note that we do this before the next block so that
929 individual warnings can be overridden back to warnings with
930 -Wno-error=*. */
931 if (context->warning_as_error_requested
932 && diagnostic->kind == DK_WARNING)
933 diagnostic->kind = DK_ERROR;
935 if (diagnostic->option_index
936 && diagnostic->option_index != permissive_error_option (context))
938 /* This tests if the user provided the appropriate -Wfoo or
939 -Wno-foo option. */
940 if (! context->option_enabled (diagnostic->option_index,
941 context->option_state))
942 return false;
944 /* This tests for #pragma diagnostic changes. */
945 diagnostic_t diag_class
946 = update_effective_level_from_pragmas (context, diagnostic);
948 /* This tests if the user provided the appropriate -Werror=foo
949 option. */
950 if (diag_class == DK_UNSPECIFIED
951 && (context->classify_diagnostic[diagnostic->option_index]
952 != DK_UNSPECIFIED))
953 diagnostic->kind
954 = context->classify_diagnostic[diagnostic->option_index];
956 /* This allows for future extensions, like temporarily disabling
957 warnings for ranges of source code. */
958 if (diagnostic->kind == DK_IGNORED)
959 return false;
962 if (diagnostic->kind != DK_NOTE)
963 diagnostic_check_max_errors (context);
965 context->lock++;
967 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
969 /* When not checking, ICEs are converted to fatal errors when an
970 error has already occurred. This is counteracted by
971 abort_on_error. */
972 if (!CHECKING_P
973 && (diagnostic_kind_count (context, DK_ERROR) > 0
974 || diagnostic_kind_count (context, DK_SORRY) > 0)
975 && !context->abort_on_error)
977 expanded_location s
978 = expand_location (diagnostic_location (diagnostic));
979 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
980 s.file, s.line);
981 exit (ICE_EXIT_CODE);
983 if (context->internal_error)
984 (*context->internal_error) (context,
985 diagnostic->message.format_spec,
986 diagnostic->message.args_ptr);
988 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
989 ++diagnostic_kind_count (context, DK_WERROR);
990 else
991 ++diagnostic_kind_count (context, diagnostic->kind);
993 /* Is this the initial diagnostic within the stack of groups? */
994 if (context->diagnostic_group_emission_count == 0)
996 if (context->begin_group_cb)
997 context->begin_group_cb (context);
999 context->diagnostic_group_emission_count++;
1001 diagnostic->message.x_data = &diagnostic->x_data;
1002 diagnostic->x_data = NULL;
1003 pp_format (context->printer, &diagnostic->message);
1004 (*diagnostic_starter (context)) (context, diagnostic);
1005 pp_output_formatted_text (context->printer);
1006 if (context->show_option_requested)
1007 print_option_information (context, diagnostic, orig_diag_kind);
1008 (*diagnostic_finalizer (context)) (context, diagnostic);
1009 if (context->parseable_fixits_p)
1011 print_parseable_fixits (context->printer, diagnostic->richloc);
1012 pp_flush (context->printer);
1014 diagnostic_action_after_output (context, diagnostic->kind);
1015 diagnostic->x_data = NULL;
1017 if (context->edit_context_ptr)
1018 if (diagnostic->richloc->fixits_can_be_auto_applied_p ())
1019 context->edit_context_ptr->add_fixits (diagnostic->richloc);
1021 context->lock--;
1023 return true;
1026 /* Given a partial pathname as input, return another pathname that
1027 shares no directory elements with the pathname of __FILE__. This
1028 is used by fancy_abort() to print `Internal compiler error in expr.c'
1029 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
1031 const char *
1032 trim_filename (const char *name)
1034 static const char this_file[] = __FILE__;
1035 const char *p = name, *q = this_file;
1037 /* First skip any "../" in each filename. This allows us to give a proper
1038 reference to a file in a subdirectory. */
1039 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
1040 p += 3;
1042 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
1043 q += 3;
1045 /* Now skip any parts the two filenames have in common. */
1046 while (*p == *q && *p != 0 && *q != 0)
1047 p++, q++;
1049 /* Now go backwards until the previous directory separator. */
1050 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
1051 p--;
1053 return p;
1056 /* Standard error reporting routines in increasing order of severity.
1057 All of these take arguments like printf. */
1059 /* Text to be emitted verbatim to the error message stream; this
1060 produces no prefix and disables line-wrapping. Use rarely. */
1061 void
1062 verbatim (const char *gmsgid, ...)
1064 text_info text;
1065 va_list ap;
1067 va_start (ap, gmsgid);
1068 text.err_no = errno;
1069 text.args_ptr = &ap;
1070 text.format_spec = _(gmsgid);
1071 text.x_data = NULL;
1072 pp_format_verbatim (global_dc->printer, &text);
1073 pp_newline_and_flush (global_dc->printer);
1074 va_end (ap);
1077 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1078 void
1079 diagnostic_append_note (diagnostic_context *context,
1080 location_t location,
1081 const char * gmsgid, ...)
1083 diagnostic_info diagnostic;
1084 va_list ap;
1085 rich_location richloc (line_table, location);
1087 va_start (ap, gmsgid);
1088 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
1089 if (context->inhibit_notes_p)
1091 va_end (ap);
1092 return;
1094 char *saved_prefix = pp_take_prefix (context->printer);
1095 pp_set_prefix (context->printer,
1096 diagnostic_build_prefix (context, &diagnostic));
1097 pp_format (context->printer, &diagnostic.message);
1098 pp_output_formatted_text (context->printer);
1099 pp_destroy_prefix (context->printer);
1100 pp_set_prefix (context->printer, saved_prefix);
1101 diagnostic_show_locus (context, &richloc, DK_NOTE);
1102 va_end (ap);
1105 /* Implement emit_diagnostic, inform, warning, warning_at, pedwarn,
1106 permerror, error, error_at, error_at, sorry, fatal_error, internal_error,
1107 and internal_error_no_backtrace, as documented and defined below. */
1108 static bool
1109 diagnostic_impl (rich_location *richloc, int opt,
1110 const char *gmsgid,
1111 va_list *ap, diagnostic_t kind)
1113 diagnostic_info diagnostic;
1114 if (kind == DK_PERMERROR)
1116 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc,
1117 permissive_error_kind (global_dc));
1118 diagnostic.option_index = permissive_error_option (global_dc);
1120 else
1122 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind);
1123 if (kind == DK_WARNING || kind == DK_PEDWARN)
1124 diagnostic.option_index = opt;
1126 return diagnostic_report_diagnostic (global_dc, &diagnostic);
1129 /* Implement inform_n, warning_n, and error_n, as documented and
1130 defined below. */
1131 static bool
1132 diagnostic_n_impl (rich_location *richloc, int opt, unsigned HOST_WIDE_INT n,
1133 const char *singular_gmsgid,
1134 const char *plural_gmsgid,
1135 va_list *ap, diagnostic_t kind)
1137 diagnostic_info diagnostic;
1138 unsigned long gtn;
1140 if (sizeof n <= sizeof gtn)
1141 gtn = n;
1142 else
1143 /* Use the largest number ngettext can handle, otherwise
1144 preserve the six least significant decimal digits for
1145 languages where the plural form depends on them. */
1146 gtn = n <= ULONG_MAX ? n : n % 1000000LU + 1000000LU;
1148 const char *text = ngettext (singular_gmsgid, plural_gmsgid, gtn);
1149 diagnostic_set_info_translated (&diagnostic, text, ap, richloc, kind);
1150 if (kind == DK_WARNING)
1151 diagnostic.option_index = opt;
1152 return diagnostic_report_diagnostic (global_dc, &diagnostic);
1155 /* Wrapper around diagnostic_impl taking a variable argument list. */
1157 bool
1158 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
1159 const char *gmsgid, ...)
1161 auto_diagnostic_group d;
1162 va_list ap;
1163 va_start (ap, gmsgid);
1164 rich_location richloc (line_table, location);
1165 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, kind);
1166 va_end (ap);
1167 return ret;
1170 /* Wrapper around diagnostic_impl taking a va_list parameter. */
1172 bool
1173 emit_diagnostic_valist (diagnostic_t kind, location_t location, int opt,
1174 const char *gmsgid, va_list *ap)
1176 rich_location richloc (line_table, location);
1177 return diagnostic_impl (&richloc, opt, gmsgid, ap, kind);
1180 /* An informative note at LOCATION. Use this for additional details on an error
1181 message. */
1182 void
1183 inform (location_t location, const char *gmsgid, ...)
1185 auto_diagnostic_group d;
1186 va_list ap;
1187 va_start (ap, gmsgid);
1188 rich_location richloc (line_table, location);
1189 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_NOTE);
1190 va_end (ap);
1193 /* Same as "inform" above, but at RICHLOC. */
1194 void
1195 inform (rich_location *richloc, const char *gmsgid, ...)
1197 gcc_assert (richloc);
1199 auto_diagnostic_group d;
1200 va_list ap;
1201 va_start (ap, gmsgid);
1202 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_NOTE);
1203 va_end (ap);
1206 /* An informative note at LOCATION. Use this for additional details on an
1207 error message. */
1208 void
1209 inform_n (location_t location, unsigned HOST_WIDE_INT n,
1210 const char *singular_gmsgid, const char *plural_gmsgid, ...)
1212 va_list ap;
1213 va_start (ap, plural_gmsgid);
1214 auto_diagnostic_group d;
1215 rich_location richloc (line_table, location);
1216 diagnostic_n_impl (&richloc, -1, n, singular_gmsgid, plural_gmsgid,
1217 &ap, DK_NOTE);
1218 va_end (ap);
1221 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1222 to the relevant language specification but is likely to be buggy anyway.
1223 Returns true if the warning was printed, false if it was inhibited. */
1224 bool
1225 warning (int opt, const char *gmsgid, ...)
1227 auto_diagnostic_group d;
1228 va_list ap;
1229 va_start (ap, gmsgid);
1230 rich_location richloc (line_table, input_location);
1231 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1232 va_end (ap);
1233 return ret;
1236 /* A warning at LOCATION. Use this for code which is correct according to the
1237 relevant language specification but is likely to be buggy anyway.
1238 Returns true if the warning was printed, false if it was inhibited. */
1240 bool
1241 warning_at (location_t location, int opt, const char *gmsgid, ...)
1243 auto_diagnostic_group d;
1244 va_list ap;
1245 va_start (ap, gmsgid);
1246 rich_location richloc (line_table, location);
1247 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1248 va_end (ap);
1249 return ret;
1252 /* Same as "warning at" above, but using RICHLOC. */
1254 bool
1255 warning_at (rich_location *richloc, int opt, const char *gmsgid, ...)
1257 gcc_assert (richloc);
1259 auto_diagnostic_group d;
1260 va_list ap;
1261 va_start (ap, gmsgid);
1262 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_WARNING);
1263 va_end (ap);
1264 return ret;
1267 /* Same as warning_n plural variant below, but using RICHLOC. */
1269 bool
1270 warning_n (rich_location *richloc, int opt, unsigned HOST_WIDE_INT n,
1271 const char *singular_gmsgid, const char *plural_gmsgid, ...)
1273 gcc_assert (richloc);
1275 auto_diagnostic_group d;
1276 va_list ap;
1277 va_start (ap, plural_gmsgid);
1278 bool ret = diagnostic_n_impl (richloc, opt, n,
1279 singular_gmsgid, plural_gmsgid,
1280 &ap, DK_WARNING);
1281 va_end (ap);
1282 return ret;
1285 /* A warning at LOCATION. Use this for code which is correct according to the
1286 relevant language specification but is likely to be buggy anyway.
1287 Returns true if the warning was printed, false if it was inhibited. */
1289 bool
1290 warning_n (location_t location, int opt, unsigned HOST_WIDE_INT n,
1291 const char *singular_gmsgid, const char *plural_gmsgid, ...)
1293 auto_diagnostic_group d;
1294 va_list ap;
1295 va_start (ap, plural_gmsgid);
1296 rich_location richloc (line_table, location);
1297 bool ret = diagnostic_n_impl (&richloc, opt, n,
1298 singular_gmsgid, plural_gmsgid,
1299 &ap, DK_WARNING);
1300 va_end (ap);
1301 return ret;
1304 /* A "pedantic" warning at LOCATION: issues a warning unless
1305 -pedantic-errors was given on the command line, in which case it
1306 issues an error. Use this for diagnostics required by the relevant
1307 language standard, if you have chosen not to make them errors.
1309 Note that these diagnostics are issued independent of the setting
1310 of the -Wpedantic command-line switch. To get a warning enabled
1311 only with that switch, use either "if (pedantic) pedwarn
1312 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1313 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1315 Returns true if the warning was printed, false if it was inhibited. */
1317 bool
1318 pedwarn (location_t location, int opt, const char *gmsgid, ...)
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_PEDWARN);
1325 va_end (ap);
1326 return ret;
1329 /* Same as pedwarn above, but using RICHLOC. */
1331 bool
1332 pedwarn (rich_location *richloc, int opt, const char *gmsgid, ...)
1334 gcc_assert (richloc);
1336 auto_diagnostic_group d;
1337 va_list ap;
1338 va_start (ap, gmsgid);
1339 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_PEDWARN);
1340 va_end (ap);
1341 return ret;
1344 /* A "permissive" error at LOCATION: issues an error unless
1345 -fpermissive was given on the command line, in which case it issues
1346 a warning. Use this for things that really should be errors but we
1347 want to support legacy code.
1349 Returns true if the warning was printed, false if it was inhibited. */
1351 bool
1352 permerror (location_t location, const char *gmsgid, ...)
1354 auto_diagnostic_group d;
1355 va_list ap;
1356 va_start (ap, gmsgid);
1357 rich_location richloc (line_table, location);
1358 bool ret = diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_PERMERROR);
1359 va_end (ap);
1360 return ret;
1363 /* Same as "permerror" above, but at RICHLOC. */
1365 bool
1366 permerror (rich_location *richloc, const char *gmsgid, ...)
1368 gcc_assert (richloc);
1370 auto_diagnostic_group d;
1371 va_list ap;
1372 va_start (ap, gmsgid);
1373 bool ret = diagnostic_impl (richloc, -1, gmsgid, &ap, DK_PERMERROR);
1374 va_end (ap);
1375 return ret;
1378 /* A hard error: the code is definitely ill-formed, and an object file
1379 will not be produced. */
1380 void
1381 error (const char *gmsgid, ...)
1383 auto_diagnostic_group d;
1384 va_list ap;
1385 va_start (ap, gmsgid);
1386 rich_location richloc (line_table, input_location);
1387 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1388 va_end (ap);
1391 /* A hard error: the code is definitely ill-formed, and an object file
1392 will not be produced. */
1393 void
1394 error_n (location_t location, unsigned HOST_WIDE_INT n,
1395 const char *singular_gmsgid, const char *plural_gmsgid, ...)
1397 auto_diagnostic_group d;
1398 va_list ap;
1399 va_start (ap, plural_gmsgid);
1400 rich_location richloc (line_table, location);
1401 diagnostic_n_impl (&richloc, -1, n, singular_gmsgid, plural_gmsgid,
1402 &ap, DK_ERROR);
1403 va_end (ap);
1406 /* Same as above, but use location LOC instead of input_location. */
1407 void
1408 error_at (location_t loc, const char *gmsgid, ...)
1410 auto_diagnostic_group d;
1411 va_list ap;
1412 va_start (ap, gmsgid);
1413 rich_location richloc (line_table, loc);
1414 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1415 va_end (ap);
1418 /* Same as above, but use RICH_LOC. */
1420 void
1421 error_at (rich_location *richloc, const char *gmsgid, ...)
1423 gcc_assert (richloc);
1425 auto_diagnostic_group d;
1426 va_list ap;
1427 va_start (ap, gmsgid);
1428 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_ERROR);
1429 va_end (ap);
1432 /* "Sorry, not implemented." Use for a language feature which is
1433 required by the relevant specification but not implemented by GCC.
1434 An object file will not be produced. */
1435 void
1436 sorry (const char *gmsgid, ...)
1438 auto_diagnostic_group d;
1439 va_list ap;
1440 va_start (ap, gmsgid);
1441 rich_location richloc (line_table, input_location);
1442 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY);
1443 va_end (ap);
1446 /* Return true if an error or a "sorry" has been seen. Various
1447 processing is disabled after errors. */
1448 bool
1449 seen_error (void)
1451 return errorcount || sorrycount;
1454 /* An error which is severe enough that we make no attempt to
1455 continue. Do not use this for internal consistency checks; that's
1456 internal_error. Use of this function should be rare. */
1457 void
1458 fatal_error (location_t loc, const char *gmsgid, ...)
1460 auto_diagnostic_group d;
1461 va_list ap;
1462 va_start (ap, gmsgid);
1463 rich_location richloc (line_table, loc);
1464 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_FATAL);
1465 va_end (ap);
1467 gcc_unreachable ();
1470 /* An internal consistency check has failed. We make no attempt to
1471 continue. Note that unless there is debugging value to be had from
1472 a more specific message, or some other good reason, you should use
1473 abort () instead of calling this function directly. */
1474 void
1475 internal_error (const char *gmsgid, ...)
1477 auto_diagnostic_group d;
1478 va_list ap;
1479 va_start (ap, gmsgid);
1480 rich_location richloc (line_table, input_location);
1481 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE);
1482 va_end (ap);
1484 gcc_unreachable ();
1487 /* Like internal_error, but no backtrace will be printed. Used when
1488 the internal error does not happen at the current location, but happened
1489 somewhere else. */
1490 void
1491 internal_error_no_backtrace (const char *gmsgid, ...)
1493 auto_diagnostic_group d;
1494 va_list ap;
1495 va_start (ap, gmsgid);
1496 rich_location richloc (line_table, input_location);
1497 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE_NOBT);
1498 va_end (ap);
1500 gcc_unreachable ();
1503 /* Special case error functions. Most are implemented in terms of the
1504 above, or should be. */
1506 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1507 runs its second argument through gettext. */
1508 void
1509 fnotice (FILE *file, const char *cmsgid, ...)
1511 va_list ap;
1513 va_start (ap, cmsgid);
1514 vfprintf (file, _(cmsgid), ap);
1515 va_end (ap);
1518 /* Inform the user that an error occurred while trying to report some
1519 other error. This indicates catastrophic internal inconsistencies,
1520 so give up now. But do try to flush out the previous error.
1521 This mustn't use internal_error, that will cause infinite recursion. */
1523 static void
1524 error_recursion (diagnostic_context *context)
1526 if (context->lock < 3)
1527 pp_newline_and_flush (context->printer);
1529 fnotice (stderr,
1530 "Internal compiler error: Error reporting routines re-entered.\n");
1532 /* Call diagnostic_action_after_output to get the "please submit a bug
1533 report" message. */
1534 diagnostic_action_after_output (context, DK_ICE);
1536 /* Do not use gcc_unreachable here; that goes through internal_error
1537 and therefore would cause infinite recursion. */
1538 real_abort ();
1541 /* Report an internal compiler error in a friendly manner. This is
1542 the function that gets called upon use of abort() in the source
1543 code generally, thanks to a special macro. */
1545 void
1546 fancy_abort (const char *file, int line, const char *function)
1548 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1551 /* class auto_diagnostic_group. */
1553 /* Constructor: "push" this group into global_dc. */
1555 auto_diagnostic_group::auto_diagnostic_group ()
1557 global_dc->diagnostic_group_nesting_depth++;
1560 /* Destructor: "pop" this group from global_dc. */
1562 auto_diagnostic_group::~auto_diagnostic_group ()
1564 if (--global_dc->diagnostic_group_nesting_depth == 0)
1566 /* Handle the case where we've popped the final diagnostic group.
1567 If any diagnostics were emitted, give the context a chance
1568 to do something. */
1569 if (global_dc->diagnostic_group_emission_count > 0)
1571 if (global_dc->end_group_cb)
1572 global_dc->end_group_cb (global_dc);
1574 global_dc->diagnostic_group_emission_count = 0;
1578 /* Really call the system 'abort'. This has to go right at the end of
1579 this file, so that there are no functions after it that call abort
1580 and get the system abort instead of our macro. */
1581 #undef abort
1582 static void
1583 real_abort (void)
1585 abort ();
1588 #if CHECKING_P
1590 namespace selftest {
1592 /* Helper function for test_print_escaped_string. */
1594 static void
1595 assert_print_escaped_string (const location &loc, const char *expected_output,
1596 const char *input)
1598 pretty_printer pp;
1599 print_escaped_string (&pp, input);
1600 ASSERT_STREQ_AT (loc, expected_output, pp_formatted_text (&pp));
1603 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1604 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1606 /* Tests of print_escaped_string. */
1608 static void
1609 test_print_escaped_string ()
1611 /* Empty string. */
1612 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1614 /* Non-empty string. */
1615 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1617 /* Various things that need to be escaped: */
1618 /* Backslash. */
1619 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1620 "before\\after");
1621 /* Tab. */
1622 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1623 "before\tafter");
1624 /* Newline. */
1625 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1626 "before\nafter");
1627 /* Double quote. */
1628 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1629 "before\"after");
1631 /* Non-printable characters: BEL: '\a': 0x07 */
1632 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1633 "before\aafter");
1634 /* Non-printable characters: vertical tab: '\v': 0x0b */
1635 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1636 "before\vafter");
1639 /* Tests of print_parseable_fixits. */
1641 /* Verify that print_parseable_fixits emits the empty string if there
1642 are no fixits. */
1644 static void
1645 test_print_parseable_fixits_none ()
1647 pretty_printer pp;
1648 rich_location richloc (line_table, UNKNOWN_LOCATION);
1650 print_parseable_fixits (&pp, &richloc);
1651 ASSERT_STREQ ("", pp_formatted_text (&pp));
1654 /* Verify that print_parseable_fixits does the right thing if there
1655 is an insertion fixit hint. */
1657 static void
1658 test_print_parseable_fixits_insert ()
1660 pretty_printer pp;
1661 rich_location richloc (line_table, UNKNOWN_LOCATION);
1663 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1664 linemap_line_start (line_table, 5, 100);
1665 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1666 location_t where = linemap_position_for_column (line_table, 10);
1667 richloc.add_fixit_insert_before (where, "added content");
1669 print_parseable_fixits (&pp, &richloc);
1670 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1671 pp_formatted_text (&pp));
1674 /* Verify that print_parseable_fixits does the right thing if there
1675 is an removal fixit hint. */
1677 static void
1678 test_print_parseable_fixits_remove ()
1680 pretty_printer pp;
1681 rich_location richloc (line_table, UNKNOWN_LOCATION);
1683 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1684 linemap_line_start (line_table, 5, 100);
1685 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1686 source_range where;
1687 where.m_start = linemap_position_for_column (line_table, 10);
1688 where.m_finish = linemap_position_for_column (line_table, 20);
1689 richloc.add_fixit_remove (where);
1691 print_parseable_fixits (&pp, &richloc);
1692 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1693 pp_formatted_text (&pp));
1696 /* Verify that print_parseable_fixits does the right thing if there
1697 is an replacement fixit hint. */
1699 static void
1700 test_print_parseable_fixits_replace ()
1702 pretty_printer pp;
1703 rich_location richloc (line_table, UNKNOWN_LOCATION);
1705 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1706 linemap_line_start (line_table, 5, 100);
1707 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1708 source_range where;
1709 where.m_start = linemap_position_for_column (line_table, 10);
1710 where.m_finish = linemap_position_for_column (line_table, 20);
1711 richloc.add_fixit_replace (where, "replacement");
1713 print_parseable_fixits (&pp, &richloc);
1714 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1715 pp_formatted_text (&pp));
1718 /* Verify that
1719 diagnostic_get_location_text (..., SHOW_COLUMN)
1720 generates EXPECTED_LOC_TEXT, given FILENAME, LINE, COLUMN, with
1721 colorization disabled. */
1723 static void
1724 assert_location_text (const char *expected_loc_text,
1725 const char *filename, int line, int column,
1726 bool show_column)
1728 test_diagnostic_context dc;
1729 dc.show_column = show_column;
1731 expanded_location xloc;
1732 xloc.file = filename;
1733 xloc.line = line;
1734 xloc.column = column;
1735 xloc.data = NULL;
1736 xloc.sysp = false;
1738 char *actual_loc_text = diagnostic_get_location_text (&dc, xloc);
1739 ASSERT_STREQ (expected_loc_text, actual_loc_text);
1740 free (actual_loc_text);
1743 /* Verify that diagnostic_get_location_text works as expected. */
1745 static void
1746 test_diagnostic_get_location_text ()
1748 const char *old_progname = progname;
1749 progname = "PROGNAME";
1750 assert_location_text ("PROGNAME:", NULL, 0, 0, true);
1751 assert_location_text ("<built-in>:", "<built-in>", 42, 10, true);
1752 assert_location_text ("foo.c:42:10:", "foo.c", 42, 10, true);
1753 assert_location_text ("foo.c:42:", "foo.c", 42, 0, true);
1754 assert_location_text ("foo.c:", "foo.c", 0, 10, true);
1755 assert_location_text ("foo.c:42:", "foo.c", 42, 10, false);
1756 assert_location_text ("foo.c:", "foo.c", 0, 10, false);
1758 maybe_line_and_column (INT_MAX, INT_MAX);
1759 maybe_line_and_column (INT_MIN, INT_MIN);
1761 progname = old_progname;
1764 /* Run all of the selftests within this file. */
1766 void
1767 diagnostic_c_tests ()
1769 test_print_escaped_string ();
1770 test_print_parseable_fixits_none ();
1771 test_print_parseable_fixits_insert ();
1772 test_print_parseable_fixits_remove ();
1773 test_print_parseable_fixits_replace ();
1774 test_diagnostic_get_location_text ();
1777 } // namespace selftest
1779 #endif /* #if CHECKING_P */