/cp
[official-gcc.git] / gcc / diagnostic.c
blob8467aaa377dbb2f958d0ad860294dadfe8382eec
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2016 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"
35 #ifdef HAVE_TERMIOS_H
36 # include <termios.h>
37 #endif
39 #ifdef GWINSZ_IN_SYS_IOCTL
40 # include <sys/ioctl.h>
41 #endif
43 #define pedantic_warning_kind(DC) \
44 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
45 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
46 #define permissive_error_option(DC) ((DC)->opt_permissive)
48 /* Prototypes. */
49 static bool diagnostic_impl (rich_location *, int, const char *,
50 va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(3,0);
51 static bool diagnostic_n_impl (location_t, int, int, const char *,
52 const char *, va_list *,
53 diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0);
54 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
55 static void real_abort (void) ATTRIBUTE_NORETURN;
57 /* Name of program invoked, sans directories. */
59 const char *progname;
61 /* A diagnostic_context surrogate for stderr. */
62 static diagnostic_context global_diagnostic_context;
63 diagnostic_context *global_dc = &global_diagnostic_context;
65 /* Return a malloc'd string containing MSG formatted a la printf. The
66 caller is responsible for freeing the memory. */
67 char *
68 build_message_string (const char *msg, ...)
70 char *str;
71 va_list ap;
73 va_start (ap, msg);
74 str = xvasprintf (msg, ap);
75 va_end (ap);
77 return str;
80 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
81 char *
82 file_name_as_prefix (diagnostic_context *context, const char *f)
84 const char *locus_cs
85 = colorize_start (pp_show_color (context->printer), "locus");
86 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
87 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
92 /* Return the value of the getenv("COLUMNS") as an integer. If the
93 value is not set to a positive integer, use ioctl to get the
94 terminal width. If it fails, return INT_MAX. */
95 int
96 get_terminal_width (void)
98 const char * s = getenv ("COLUMNS");
99 if (s != NULL) {
100 int n = atoi (s);
101 if (n > 0)
102 return n;
105 #ifdef TIOCGWINSZ
106 struct winsize w;
107 w.ws_col = 0;
108 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
109 return w.ws_col;
110 #endif
112 return INT_MAX;
115 /* Set caret_max_width to value. */
116 void
117 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
119 /* One minus to account for the leading empty space. */
120 value = value ? value - 1
121 : (isatty (fileno (pp_buffer (context->printer)->stream))
122 ? get_terminal_width () - 1: INT_MAX);
124 if (value <= 0)
125 value = INT_MAX;
127 context->caret_max_width = value;
130 /* Initialize the diagnostic message outputting machinery. */
131 void
132 diagnostic_initialize (diagnostic_context *context, int n_opts)
134 int i;
136 /* Allocate a basic pretty-printer. Clients will replace this a
137 much more elaborated pretty-printer if they wish. */
138 context->printer = XNEW (pretty_printer);
139 new (context->printer) pretty_printer ();
141 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
142 context->warning_as_error_requested = false;
143 context->n_opts = n_opts;
144 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
145 for (i = 0; i < n_opts; i++)
146 context->classify_diagnostic[i] = DK_UNSPECIFIED;
147 context->show_caret = false;
148 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
149 for (i = 0; i < rich_location::MAX_RANGES; i++)
150 context->caret_chars[i] = '^';
151 context->show_option_requested = false;
152 context->abort_on_error = false;
153 context->show_column = false;
154 context->pedantic_errors = false;
155 context->permissive = false;
156 context->opt_permissive = 0;
157 context->fatal_errors = false;
158 context->dc_inhibit_warnings = false;
159 context->dc_warn_system_headers = false;
160 context->max_errors = 0;
161 context->internal_error = NULL;
162 diagnostic_starter (context) = default_diagnostic_starter;
163 context->start_span = default_diagnostic_start_span_fn;
164 diagnostic_finalizer (context) = default_diagnostic_finalizer;
165 context->option_enabled = NULL;
166 context->option_state = NULL;
167 context->option_name = NULL;
168 context->last_location = UNKNOWN_LOCATION;
169 context->last_module = 0;
170 context->x_data = NULL;
171 context->lock = 0;
172 context->inhibit_notes_p = false;
175 /* Maybe initialize the color support. We require clients to do this
176 explicitly, since most clients don't want color. When called
177 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
179 void
180 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
182 /* value == -1 is the default value. */
183 if (value < 0)
185 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
186 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
187 otherwise default to -fdiagnostics-color=never, for other
188 values default to that
189 -fdiagnostics-color={never,auto,always}. */
190 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
192 if (!getenv ("GCC_COLORS"))
193 return;
194 value = DIAGNOSTICS_COLOR_AUTO;
196 else
197 value = DIAGNOSTICS_COLOR_DEFAULT;
199 pp_show_color (context->printer)
200 = colorize_init ((diagnostic_color_rule_t) value);
203 /* Do any cleaning up required after the last diagnostic is emitted. */
205 void
206 diagnostic_finish (diagnostic_context *context)
208 /* Some of the errors may actually have been warnings. */
209 if (diagnostic_kind_count (context, DK_WERROR))
211 /* -Werror was given. */
212 if (context->warning_as_error_requested)
213 pp_verbatim (context->printer,
214 _("%s: all warnings being treated as errors"),
215 progname);
216 /* At least one -Werror= was given. */
217 else
218 pp_verbatim (context->printer,
219 _("%s: some warnings being treated as errors"),
220 progname);
221 pp_newline_and_flush (context->printer);
224 diagnostic_file_cache_fini ();
226 XDELETEVEC (context->classify_diagnostic);
227 context->classify_diagnostic = NULL;
229 /* diagnostic_initialize allocates context->printer using XNEW
230 and placement-new. */
231 context->printer->~pretty_printer ();
232 XDELETE (context->printer);
233 context->printer = NULL;
236 /* Initialize DIAGNOSTIC, where the message MSG has already been
237 translated. */
238 void
239 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
240 va_list *args, rich_location *richloc,
241 diagnostic_t kind)
243 gcc_assert (richloc);
244 diagnostic->message.err_no = errno;
245 diagnostic->message.args_ptr = args;
246 diagnostic->message.format_spec = msg;
247 diagnostic->message.m_richloc = richloc;
248 diagnostic->richloc = richloc;
249 diagnostic->kind = kind;
250 diagnostic->option_index = 0;
253 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
254 translated. */
255 void
256 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
257 va_list *args, rich_location *richloc,
258 diagnostic_t kind)
260 gcc_assert (richloc);
261 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
264 static const char *const diagnostic_kind_color[] = {
265 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
266 #include "diagnostic.def"
267 #undef DEFINE_DIAGNOSTIC_KIND
268 NULL
271 /* Get a color name for diagnostics of type KIND
272 Result could be NULL. */
274 const char *
275 diagnostic_get_color_for_kind (diagnostic_t kind)
277 return diagnostic_kind_color[kind];
280 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
281 The caller is responsible for freeing the memory. */
283 static char *
284 diagnostic_get_location_text (diagnostic_context *context,
285 expanded_location s)
287 pretty_printer *pp = context->printer;
288 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
289 const char *locus_ce = colorize_stop (pp_show_color (pp));
291 if (s.file == NULL)
292 return build_message_string ("%s%s:%s", locus_cs, progname, locus_ce);
294 if (!strcmp (s.file, N_("<built-in>")))
295 return build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce);
297 if (context->show_column)
298 return build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
299 s.column, locus_ce);
300 else
301 return build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
302 locus_ce);
305 /* Return a malloc'd string describing a location and the severity of the
306 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
307 freeing the memory. */
308 char *
309 diagnostic_build_prefix (diagnostic_context *context,
310 const diagnostic_info *diagnostic)
312 static const char *const diagnostic_kind_text[] = {
313 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
314 #include "diagnostic.def"
315 #undef DEFINE_DIAGNOSTIC_KIND
316 "must-not-happen"
318 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
320 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
321 const char *text_cs = "", *text_ce = "";
322 pretty_printer *pp = context->printer;
324 if (diagnostic_kind_color[diagnostic->kind])
326 text_cs = colorize_start (pp_show_color (pp),
327 diagnostic_kind_color[diagnostic->kind]);
328 text_ce = colorize_stop (pp_show_color (pp));
331 expanded_location s = diagnostic_expand_location (diagnostic);
332 char *location_text = diagnostic_get_location_text (context, s);
334 char *result = build_message_string ("%s %s%s%s", location_text,
335 text_cs, text, text_ce);
336 free (location_text);
337 return result;
340 /* Functions at which to stop the backtrace print. It's not
341 particularly helpful to print the callers of these functions. */
343 static const char * const bt_stop[] =
345 "main",
346 "toplev::main",
347 "execute_one_pass",
348 "compile_file",
351 /* A callback function passed to the backtrace_full function. */
353 static int
354 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
355 const char *function)
357 int *pcount = (int *) data;
359 /* If we don't have any useful information, don't print
360 anything. */
361 if (filename == NULL && function == NULL)
362 return 0;
364 /* Skip functions in diagnostic.c. */
365 if (*pcount == 0
366 && filename != NULL
367 && strcmp (lbasename (filename), "diagnostic.c") == 0)
368 return 0;
370 /* Print up to 20 functions. We could make this a --param, but
371 since this is only for debugging just use a constant for now. */
372 if (*pcount >= 20)
374 /* Returning a non-zero value stops the backtrace. */
375 return 1;
377 ++*pcount;
379 char *alc = NULL;
380 if (function != NULL)
382 char *str = cplus_demangle_v3 (function,
383 (DMGL_VERBOSE | DMGL_ANSI
384 | DMGL_GNU_V3 | DMGL_PARAMS));
385 if (str != NULL)
387 alc = str;
388 function = str;
391 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
393 size_t len = strlen (bt_stop[i]);
394 if (strncmp (function, bt_stop[i], len) == 0
395 && (function[len] == '\0' || function[len] == '('))
397 if (alc != NULL)
398 free (alc);
399 /* Returning a non-zero value stops the backtrace. */
400 return 1;
405 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
406 (unsigned long) pc,
407 function == NULL ? "???" : function,
408 filename == NULL ? "???" : filename,
409 lineno);
411 if (alc != NULL)
412 free (alc);
414 return 0;
417 /* A callback function passed to the backtrace_full function. This is
418 called if backtrace_full has an error. */
420 static void
421 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
423 if (errnum < 0)
425 /* This means that no debug info was available. Just quietly
426 skip printing backtrace info. */
427 return;
429 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
430 errnum == 0 ? "" : xstrerror (errnum));
433 /* Take any action which is expected to happen after the diagnostic
434 is written out. This function does not always return. */
435 void
436 diagnostic_action_after_output (diagnostic_context *context,
437 diagnostic_t diag_kind)
439 switch (diag_kind)
441 case DK_DEBUG:
442 case DK_NOTE:
443 case DK_ANACHRONISM:
444 case DK_WARNING:
445 break;
447 case DK_ERROR:
448 case DK_SORRY:
449 if (context->abort_on_error)
450 real_abort ();
451 if (context->fatal_errors)
453 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
454 diagnostic_finish (context);
455 exit (FATAL_EXIT_CODE);
457 if (context->max_errors != 0
458 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
459 + diagnostic_kind_count (context, DK_SORRY)
460 + diagnostic_kind_count (context, DK_WERROR))
461 >= context->max_errors))
463 fnotice (stderr,
464 "compilation terminated due to -fmax-errors=%u.\n",
465 context->max_errors);
466 diagnostic_finish (context);
467 exit (FATAL_EXIT_CODE);
469 break;
471 case DK_ICE:
472 case DK_ICE_NOBT:
474 struct backtrace_state *state = NULL;
475 if (diag_kind == DK_ICE)
476 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
477 int count = 0;
478 if (state != NULL)
479 backtrace_full (state, 2, bt_callback, bt_err_callback,
480 (void *) &count);
482 if (context->abort_on_error)
483 real_abort ();
485 fnotice (stderr, "Please submit a full bug report,\n"
486 "with preprocessed source if appropriate.\n");
487 if (count > 0)
488 fnotice (stderr,
489 ("Please include the complete backtrace "
490 "with any bug report.\n"));
491 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
493 exit (ICE_EXIT_CODE);
496 case DK_FATAL:
497 if (context->abort_on_error)
498 real_abort ();
499 diagnostic_finish (context);
500 fnotice (stderr, "compilation terminated.\n");
501 exit (FATAL_EXIT_CODE);
503 default:
504 gcc_unreachable ();
508 void
509 diagnostic_report_current_module (diagnostic_context *context, location_t where)
511 const line_map_ordinary *map = NULL;
513 if (pp_needs_newline (context->printer))
515 pp_newline (context->printer);
516 pp_needs_newline (context->printer) = false;
519 if (where <= BUILTINS_LOCATION)
520 return;
522 linemap_resolve_location (line_table, where,
523 LRK_MACRO_DEFINITION_LOCATION,
524 &map);
526 if (map && diagnostic_last_module_changed (context, map))
528 diagnostic_set_last_module (context, map);
529 if (! MAIN_FILE_P (map))
531 map = INCLUDED_FROM (line_table, map);
532 if (context->show_column)
533 pp_verbatim (context->printer,
534 "In file included from %r%s:%d:%d%R", "locus",
535 LINEMAP_FILE (map),
536 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
537 else
538 pp_verbatim (context->printer,
539 "In file included from %r%s:%d%R", "locus",
540 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
541 while (! MAIN_FILE_P (map))
543 map = INCLUDED_FROM (line_table, map);
544 pp_verbatim (context->printer,
545 ",\n from %r%s:%d%R", "locus",
546 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
548 pp_verbatim (context->printer, ":");
549 pp_newline (context->printer);
554 void
555 default_diagnostic_starter (diagnostic_context *context,
556 diagnostic_info *diagnostic)
558 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
559 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
560 diagnostic));
563 void
564 default_diagnostic_start_span_fn (diagnostic_context *context,
565 expanded_location exploc)
567 pp_set_prefix (context->printer,
568 diagnostic_get_location_text (context, exploc));
569 pp_string (context->printer, "");
570 pp_newline (context->printer);
573 void
574 default_diagnostic_finalizer (diagnostic_context *context,
575 diagnostic_info *diagnostic)
577 diagnostic_show_locus (context, diagnostic);
578 pp_destroy_prefix (context->printer);
579 pp_flush (context->printer);
582 /* Interface to specify diagnostic kind overrides. Returns the
583 previous setting, or DK_UNSPECIFIED if the parameters are out of
584 range. If OPTION_INDEX is zero, the new setting is for all the
585 diagnostics. */
586 diagnostic_t
587 diagnostic_classify_diagnostic (diagnostic_context *context,
588 int option_index,
589 diagnostic_t new_kind,
590 location_t where)
592 diagnostic_t old_kind;
594 if (option_index < 0
595 || option_index >= context->n_opts
596 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
597 return DK_UNSPECIFIED;
599 old_kind = context->classify_diagnostic[option_index];
601 /* Handle pragmas separately, since we need to keep track of *where*
602 the pragmas were. */
603 if (where != UNKNOWN_LOCATION)
605 int i;
607 /* Record the command-line status, so we can reset it back on DK_POP. */
608 if (old_kind == DK_UNSPECIFIED)
610 old_kind = !context->option_enabled (option_index,
611 context->option_state)
612 ? DK_IGNORED : (context->warning_as_error_requested
613 ? DK_ERROR : DK_WARNING);
614 context->classify_diagnostic[option_index] = old_kind;
617 for (i = context->n_classification_history - 1; i >= 0; i --)
618 if (context->classification_history[i].option == option_index)
620 old_kind = context->classification_history[i].kind;
621 break;
624 i = context->n_classification_history;
625 context->classification_history =
626 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
627 * sizeof (diagnostic_classification_change_t));
628 context->classification_history[i].location = where;
629 context->classification_history[i].option = option_index;
630 context->classification_history[i].kind = new_kind;
631 context->n_classification_history ++;
633 else
634 context->classify_diagnostic[option_index] = new_kind;
636 return old_kind;
639 /* Save all diagnostic classifications in a stack. */
640 void
641 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
643 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
644 context->push_list[context->n_push ++] = context->n_classification_history;
647 /* Restore the topmost classification set off the stack. If the stack
648 is empty, revert to the state based on command line parameters. */
649 void
650 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
652 int jump_to;
653 int i;
655 if (context->n_push)
656 jump_to = context->push_list [-- context->n_push];
657 else
658 jump_to = 0;
660 i = context->n_classification_history;
661 context->classification_history =
662 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
663 * sizeof (diagnostic_classification_change_t));
664 context->classification_history[i].location = where;
665 context->classification_history[i].option = jump_to;
666 context->classification_history[i].kind = DK_POP;
667 context->n_classification_history ++;
670 /* Report a diagnostic message (an error or a warning) as specified by
671 DC. This function is *the* subroutine in terms of which front-ends
672 should implement their specific diagnostic handling modules. The
673 front-end independent format specifiers are exactly those described
674 in the documentation of output_format.
675 Return true if a diagnostic was printed, false otherwise. */
677 bool
678 diagnostic_report_diagnostic (diagnostic_context *context,
679 diagnostic_info *diagnostic)
681 location_t location = diagnostic_location (diagnostic);
682 diagnostic_t orig_diag_kind = diagnostic->kind;
683 const char *saved_format_spec;
685 /* Give preference to being able to inhibit warnings, before they
686 get reclassified to something else. */
687 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
688 && !diagnostic_report_warnings_p (context, location))
689 return false;
691 if (diagnostic->kind == DK_PEDWARN)
693 diagnostic->kind = pedantic_warning_kind (context);
694 /* We do this to avoid giving the message for -pedantic-errors. */
695 orig_diag_kind = diagnostic->kind;
698 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
699 return false;
701 if (context->lock > 0)
703 /* If we're reporting an ICE in the middle of some other error,
704 try to flush out the previous error, then let this one
705 through. Don't do this more than once. */
706 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
707 && context->lock == 1)
708 pp_newline_and_flush (context->printer);
709 else
710 error_recursion (context);
713 /* If the user requested that warnings be treated as errors, so be
714 it. Note that we do this before the next block so that
715 individual warnings can be overridden back to warnings with
716 -Wno-error=*. */
717 if (context->warning_as_error_requested
718 && diagnostic->kind == DK_WARNING)
720 diagnostic->kind = DK_ERROR;
723 if (diagnostic->option_index
724 && diagnostic->option_index != permissive_error_option (context))
726 diagnostic_t diag_class = DK_UNSPECIFIED;
728 /* This tests if the user provided the appropriate -Wfoo or
729 -Wno-foo option. */
730 if (! context->option_enabled (diagnostic->option_index,
731 context->option_state))
732 return false;
734 /* This tests for #pragma diagnostic changes. */
735 if (context->n_classification_history > 0)
737 /* FIXME: Stupid search. Optimize later. */
738 for (int i = context->n_classification_history - 1; i >= 0; i --)
740 if (linemap_location_before_p
741 (line_table,
742 context->classification_history[i].location,
743 location))
745 if (context->classification_history[i].kind == (int) DK_POP)
747 i = context->classification_history[i].option;
748 continue;
750 int option = context->classification_history[i].option;
751 /* The option 0 is for all the diagnostics. */
752 if (option == 0 || option == diagnostic->option_index)
754 diag_class = context->classification_history[i].kind;
755 if (diag_class != DK_UNSPECIFIED)
756 diagnostic->kind = diag_class;
757 break;
762 /* This tests if the user provided the appropriate -Werror=foo
763 option. */
764 if (diag_class == DK_UNSPECIFIED
765 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
767 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
769 /* This allows for future extensions, like temporarily disabling
770 warnings for ranges of source code. */
771 if (diagnostic->kind == DK_IGNORED)
772 return false;
775 context->lock++;
777 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
779 /* When not checking, ICEs are converted to fatal errors when an
780 error has already occurred. This is counteracted by
781 abort_on_error. */
782 if (!CHECKING_P
783 && (diagnostic_kind_count (context, DK_ERROR) > 0
784 || diagnostic_kind_count (context, DK_SORRY) > 0)
785 && !context->abort_on_error)
787 expanded_location s
788 = expand_location (diagnostic_location (diagnostic));
789 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
790 s.file, s.line);
791 exit (ICE_EXIT_CODE);
793 if (context->internal_error)
794 (*context->internal_error) (context,
795 diagnostic->message.format_spec,
796 diagnostic->message.args_ptr);
798 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
799 ++diagnostic_kind_count (context, DK_WERROR);
800 else
801 ++diagnostic_kind_count (context, diagnostic->kind);
803 saved_format_spec = diagnostic->message.format_spec;
804 if (context->show_option_requested)
806 char *option_text;
808 option_text = context->option_name (context, diagnostic->option_index,
809 orig_diag_kind, diagnostic->kind);
811 if (option_text)
813 const char *cs
814 = colorize_start (pp_show_color (context->printer),
815 diagnostic_kind_color[diagnostic->kind]);
816 const char *ce = colorize_stop (pp_show_color (context->printer));
817 diagnostic->message.format_spec
818 = ACONCAT ((diagnostic->message.format_spec,
819 " ",
820 "[", cs, option_text, ce, "]",
821 NULL));
822 free (option_text);
825 diagnostic->message.x_data = &diagnostic->x_data;
826 diagnostic->x_data = NULL;
827 pp_format (context->printer, &diagnostic->message);
828 (*diagnostic_starter (context)) (context, diagnostic);
829 pp_output_formatted_text (context->printer);
830 (*diagnostic_finalizer (context)) (context, diagnostic);
831 diagnostic_action_after_output (context, diagnostic->kind);
832 diagnostic->message.format_spec = saved_format_spec;
833 diagnostic->x_data = NULL;
835 context->lock--;
837 return true;
840 /* Given a partial pathname as input, return another pathname that
841 shares no directory elements with the pathname of __FILE__. This
842 is used by fancy_abort() to print `Internal compiler error in expr.c'
843 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
845 const char *
846 trim_filename (const char *name)
848 static const char this_file[] = __FILE__;
849 const char *p = name, *q = this_file;
851 /* First skip any "../" in each filename. This allows us to give a proper
852 reference to a file in a subdirectory. */
853 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
854 p += 3;
856 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
857 q += 3;
859 /* Now skip any parts the two filenames have in common. */
860 while (*p == *q && *p != 0 && *q != 0)
861 p++, q++;
863 /* Now go backwards until the previous directory separator. */
864 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
865 p--;
867 return p;
870 /* Standard error reporting routines in increasing order of severity.
871 All of these take arguments like printf. */
873 /* Text to be emitted verbatim to the error message stream; this
874 produces no prefix and disables line-wrapping. Use rarely. */
875 void
876 verbatim (const char *gmsgid, ...)
878 text_info text;
879 va_list ap;
881 va_start (ap, gmsgid);
882 text.err_no = errno;
883 text.args_ptr = &ap;
884 text.format_spec = _(gmsgid);
885 text.x_data = NULL;
886 pp_format_verbatim (global_dc->printer, &text);
887 pp_newline_and_flush (global_dc->printer);
888 va_end (ap);
891 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
892 void
893 diagnostic_append_note (diagnostic_context *context,
894 location_t location,
895 const char * gmsgid, ...)
897 diagnostic_info diagnostic;
898 va_list ap;
899 const char *saved_prefix;
900 rich_location richloc (line_table, location);
902 va_start (ap, gmsgid);
903 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
904 if (context->inhibit_notes_p)
906 va_end (ap);
907 return;
909 saved_prefix = pp_get_prefix (context->printer);
910 pp_set_prefix (context->printer,
911 diagnostic_build_prefix (context, &diagnostic));
912 pp_format (context->printer, &diagnostic.message);
913 pp_output_formatted_text (context->printer);
914 pp_destroy_prefix (context->printer);
915 pp_set_prefix (context->printer, saved_prefix);
916 diagnostic_show_locus (context, &diagnostic);
917 va_end (ap);
920 /* Implement emit_diagnostic, inform, inform_at_rich_loc, warning, warning_at,
921 warning_at_rich_loc, pedwarn, permerror, permerror_at_rich_loc, error,
922 error_at, error_at_rich_loc, sorry, fatal_error, internal_error, and
923 internal_error_no_backtrace, as documented and defined below. */
924 static bool
925 diagnostic_impl (rich_location *richloc, int opt,
926 const char *gmsgid,
927 va_list *ap, diagnostic_t kind)
929 diagnostic_info diagnostic;
930 if (kind == DK_PERMERROR)
932 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc,
933 permissive_error_kind (global_dc));
934 diagnostic.option_index = permissive_error_option (global_dc);
936 else
938 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind);
939 if (kind == DK_WARNING || kind == DK_PEDWARN)
940 diagnostic.option_index = opt;
942 return report_diagnostic (&diagnostic);
945 /* Implement inform_n, warning_n, and error_n, as documented and
946 defined below. */
947 static bool
948 diagnostic_n_impl (location_t location, int opt, int n,
949 const char *singular_gmsgid,
950 const char *plural_gmsgid,
951 va_list *ap, diagnostic_t kind)
953 diagnostic_info diagnostic;
954 rich_location richloc (line_table, location);
955 diagnostic_set_info_translated (&diagnostic,
956 ngettext (singular_gmsgid, plural_gmsgid, n),
957 ap, &richloc, kind);
958 if (kind == DK_WARNING)
959 diagnostic.option_index = opt;
960 return report_diagnostic (&diagnostic);
963 bool
964 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
965 const char *gmsgid, ...)
967 va_list ap;
968 va_start (ap, gmsgid);
969 rich_location richloc (line_table, location);
970 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, kind);
971 va_end (ap);
972 return ret;
975 /* An informative note at LOCATION. Use this for additional details on an error
976 message. */
977 void
978 inform (location_t location, const char *gmsgid, ...)
980 va_list ap;
981 va_start (ap, gmsgid);
982 rich_location richloc (line_table, location);
983 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_NOTE);
984 va_end (ap);
987 /* Same as "inform", but at RICHLOC. */
988 void
989 inform_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
991 va_list ap;
992 va_start (ap, gmsgid);
993 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_NOTE);
994 va_end (ap);
997 /* An informative note at LOCATION. Use this for additional details on an
998 error message. */
999 void
1000 inform_n (location_t location, int n, const char *singular_gmsgid,
1001 const char *plural_gmsgid, ...)
1003 va_list ap;
1004 va_start (ap, plural_gmsgid);
1005 diagnostic_n_impl (location, -1, n, singular_gmsgid, plural_gmsgid,
1006 &ap, DK_NOTE);
1007 va_end (ap);
1010 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1011 to the relevant language specification but is likely to be buggy anyway.
1012 Returns true if the warning was printed, false if it was inhibited. */
1013 bool
1014 warning (int opt, const char *gmsgid, ...)
1016 va_list ap;
1017 va_start (ap, gmsgid);
1018 rich_location richloc (line_table, input_location);
1019 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1020 va_end (ap);
1021 return ret;
1024 /* A warning at LOCATION. Use this for code which is correct according to the
1025 relevant language specification but is likely to be buggy anyway.
1026 Returns true if the warning was printed, false if it was inhibited. */
1028 bool
1029 warning_at (location_t location, int opt, const char *gmsgid, ...)
1031 va_list ap;
1032 va_start (ap, gmsgid);
1033 rich_location richloc (line_table, location);
1034 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
1035 va_end (ap);
1036 return ret;
1039 /* Same as warning at, but using RICHLOC. */
1041 bool
1042 warning_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1044 va_list ap;
1045 va_start (ap, gmsgid);
1046 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_WARNING);
1047 va_end (ap);
1048 return ret;
1051 /* A warning at LOCATION. Use this for code which is correct according to the
1052 relevant language specification but is likely to be buggy anyway.
1053 Returns true if the warning was printed, false if it was inhibited. */
1055 bool
1056 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1057 const char *plural_gmsgid, ...)
1059 va_list ap;
1060 va_start (ap, plural_gmsgid);
1061 bool ret = diagnostic_n_impl (location, opt, n,
1062 singular_gmsgid, plural_gmsgid,
1063 &ap, DK_WARNING);
1064 va_end (ap);
1065 return ret;
1068 /* A "pedantic" warning at LOCATION: issues a warning unless
1069 -pedantic-errors was given on the command line, in which case it
1070 issues an error. Use this for diagnostics required by the relevant
1071 language standard, if you have chosen not to make them errors.
1073 Note that these diagnostics are issued independent of the setting
1074 of the -Wpedantic command-line switch. To get a warning enabled
1075 only with that switch, use either "if (pedantic) pedwarn
1076 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1077 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1079 Returns true if the warning was printed, false if it was inhibited. */
1081 bool
1082 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1084 va_list ap;
1085 va_start (ap, gmsgid);
1086 rich_location richloc (line_table, location);
1087 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_PEDWARN);
1088 va_end (ap);
1089 return ret;
1092 /* A "permissive" error at LOCATION: issues an error unless
1093 -fpermissive was given on the command line, in which case it issues
1094 a warning. Use this for things that really should be errors but we
1095 want to support legacy code.
1097 Returns true if the warning was printed, false if it was inhibited. */
1099 bool
1100 permerror (location_t location, const char *gmsgid, ...)
1102 va_list ap;
1103 va_start (ap, gmsgid);
1104 rich_location richloc (line_table, location);
1105 bool ret = diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_PERMERROR);
1106 va_end (ap);
1107 return ret;
1110 /* Same as "permerror", but at RICHLOC. */
1112 bool
1113 permerror_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1115 va_list ap;
1116 va_start (ap, gmsgid);
1117 bool ret = diagnostic_impl (richloc, -1, gmsgid, &ap, DK_PERMERROR);
1118 va_end (ap);
1119 return ret;
1122 /* A hard error: the code is definitely ill-formed, and an object file
1123 will not be produced. */
1124 void
1125 error (const char *gmsgid, ...)
1127 va_list ap;
1128 va_start (ap, gmsgid);
1129 rich_location richloc (line_table, input_location);
1130 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1131 va_end (ap);
1134 /* A hard error: the code is definitely ill-formed, and an object file
1135 will not be produced. */
1136 void
1137 error_n (location_t location, int n, const char *singular_gmsgid,
1138 const char *plural_gmsgid, ...)
1140 va_list ap;
1141 va_start (ap, plural_gmsgid);
1142 diagnostic_n_impl (location, -1, n, singular_gmsgid, plural_gmsgid,
1143 &ap, DK_ERROR);
1144 va_end (ap);
1147 /* Same as above, but use location LOC instead of input_location. */
1148 void
1149 error_at (location_t loc, const char *gmsgid, ...)
1151 va_list ap;
1152 va_start (ap, gmsgid);
1153 rich_location richloc (line_table, loc);
1154 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
1155 va_end (ap);
1158 /* Same as above, but use RICH_LOC. */
1160 void
1161 error_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1163 va_list ap;
1164 va_start (ap, gmsgid);
1165 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_ERROR);
1166 va_end (ap);
1169 /* "Sorry, not implemented." Use for a language feature which is
1170 required by the relevant specification but not implemented by GCC.
1171 An object file will not be produced. */
1172 void
1173 sorry (const char *gmsgid, ...)
1175 va_list ap;
1176 va_start (ap, gmsgid);
1177 rich_location richloc (line_table, input_location);
1178 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY);
1179 va_end (ap);
1182 /* Return true if an error or a "sorry" has been seen. Various
1183 processing is disabled after errors. */
1184 bool
1185 seen_error (void)
1187 return errorcount || sorrycount;
1190 /* An error which is severe enough that we make no attempt to
1191 continue. Do not use this for internal consistency checks; that's
1192 internal_error. Use of this function should be rare. */
1193 void
1194 fatal_error (location_t loc, const char *gmsgid, ...)
1196 va_list ap;
1197 va_start (ap, gmsgid);
1198 rich_location richloc (line_table, loc);
1199 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_FATAL);
1200 va_end (ap);
1202 gcc_unreachable ();
1205 /* An internal consistency check has failed. We make no attempt to
1206 continue. Note that unless there is debugging value to be had from
1207 a more specific message, or some other good reason, you should use
1208 abort () instead of calling this function directly. */
1209 void
1210 internal_error (const char *gmsgid, ...)
1212 va_list ap;
1213 va_start (ap, gmsgid);
1214 rich_location richloc (line_table, input_location);
1215 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE);
1216 va_end (ap);
1218 gcc_unreachable ();
1221 /* Like internal_error, but no backtrace will be printed. Used when
1222 the internal error does not happen at the current location, but happened
1223 somewhere else. */
1224 void
1225 internal_error_no_backtrace (const char *gmsgid, ...)
1227 va_list ap;
1228 va_start (ap, gmsgid);
1229 rich_location richloc (line_table, input_location);
1230 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE_NOBT);
1231 va_end (ap);
1233 gcc_unreachable ();
1236 /* Special case error functions. Most are implemented in terms of the
1237 above, or should be. */
1239 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1240 runs its second argument through gettext. */
1241 void
1242 fnotice (FILE *file, const char *cmsgid, ...)
1244 va_list ap;
1246 va_start (ap, cmsgid);
1247 vfprintf (file, _(cmsgid), ap);
1248 va_end (ap);
1251 /* Inform the user that an error occurred while trying to report some
1252 other error. This indicates catastrophic internal inconsistencies,
1253 so give up now. But do try to flush out the previous error.
1254 This mustn't use internal_error, that will cause infinite recursion. */
1256 static void
1257 error_recursion (diagnostic_context *context)
1259 if (context->lock < 3)
1260 pp_newline_and_flush (context->printer);
1262 fnotice (stderr,
1263 "Internal compiler error: Error reporting routines re-entered.\n");
1265 /* Call diagnostic_action_after_output to get the "please submit a bug
1266 report" message. */
1267 diagnostic_action_after_output (context, DK_ICE);
1269 /* Do not use gcc_unreachable here; that goes through internal_error
1270 and therefore would cause infinite recursion. */
1271 real_abort ();
1274 /* Report an internal compiler error in a friendly manner. This is
1275 the function that gets called upon use of abort() in the source
1276 code generally, thanks to a special macro. */
1278 void
1279 fancy_abort (const char *file, int line, const char *function)
1281 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1284 /* Really call the system 'abort'. This has to go right at the end of
1285 this file, so that there are no functions after it that call abort
1286 and get the system abort instead of our macro. */
1287 #undef abort
1288 static void
1289 real_abort (void)
1291 abort ();