Remove assert in get_def_bb_for_const
[official-gcc.git] / gcc / diagnostic.c
blob810617243f2e2fd3d51f6b40032539b9dc2408b7
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 void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
51 static void real_abort (void) ATTRIBUTE_NORETURN;
53 /* Name of program invoked, sans directories. */
55 const char *progname;
57 /* A diagnostic_context surrogate for stderr. */
58 static diagnostic_context global_diagnostic_context;
59 diagnostic_context *global_dc = &global_diagnostic_context;
61 /* Return a malloc'd string containing MSG formatted a la printf. The
62 caller is responsible for freeing the memory. */
63 char *
64 build_message_string (const char *msg, ...)
66 char *str;
67 va_list ap;
69 va_start (ap, msg);
70 str = xvasprintf (msg, ap);
71 va_end (ap);
73 return str;
76 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
77 char *
78 file_name_as_prefix (diagnostic_context *context, const char *f)
80 const char *locus_cs
81 = colorize_start (pp_show_color (context->printer), "locus");
82 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
83 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
88 /* Return the value of the getenv("COLUMNS") as an integer. If the
89 value is not set to a positive integer, use ioctl to get the
90 terminal width. If it fails, return INT_MAX. */
91 int
92 get_terminal_width (void)
94 const char * s = getenv ("COLUMNS");
95 if (s != NULL) {
96 int n = atoi (s);
97 if (n > 0)
98 return n;
101 #ifdef TIOCGWINSZ
102 struct winsize w;
103 w.ws_col = 0;
104 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
105 return w.ws_col;
106 #endif
108 return INT_MAX;
111 /* Set caret_max_width to value. */
112 void
113 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
115 /* One minus to account for the leading empty space. */
116 value = value ? value - 1
117 : (isatty (fileno (pp_buffer (context->printer)->stream))
118 ? get_terminal_width () - 1: INT_MAX);
120 if (value <= 0)
121 value = INT_MAX;
123 context->caret_max_width = value;
126 /* Initialize the diagnostic message outputting machinery. */
127 void
128 diagnostic_initialize (diagnostic_context *context, int n_opts)
130 int i;
132 /* Allocate a basic pretty-printer. Clients will replace this a
133 much more elaborated pretty-printer if they wish. */
134 context->printer = XNEW (pretty_printer);
135 new (context->printer) pretty_printer ();
137 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
138 context->warning_as_error_requested = false;
139 context->n_opts = n_opts;
140 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
141 for (i = 0; i < n_opts; i++)
142 context->classify_diagnostic[i] = DK_UNSPECIFIED;
143 context->show_caret = false;
144 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
145 for (i = 0; i < rich_location::MAX_RANGES; i++)
146 context->caret_chars[i] = '^';
147 context->show_option_requested = false;
148 context->abort_on_error = false;
149 context->show_column = false;
150 context->pedantic_errors = false;
151 context->permissive = false;
152 context->opt_permissive = 0;
153 context->fatal_errors = false;
154 context->dc_inhibit_warnings = false;
155 context->dc_warn_system_headers = false;
156 context->max_errors = 0;
157 context->internal_error = NULL;
158 diagnostic_starter (context) = default_diagnostic_starter;
159 context->start_span = default_diagnostic_start_span_fn;
160 diagnostic_finalizer (context) = default_diagnostic_finalizer;
161 context->option_enabled = NULL;
162 context->option_state = NULL;
163 context->option_name = NULL;
164 context->last_location = UNKNOWN_LOCATION;
165 context->last_module = 0;
166 context->x_data = NULL;
167 context->lock = 0;
168 context->inhibit_notes_p = false;
171 /* Maybe initialize the color support. We require clients to do this
172 explicitly, since most clients don't want color. When called
173 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
175 void
176 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
178 /* value == -1 is the default value. */
179 if (value < 0)
181 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
182 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
183 otherwise default to -fdiagnostics-color=never, for other
184 values default to that
185 -fdiagnostics-color={never,auto,always}. */
186 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
188 if (!getenv ("GCC_COLORS"))
189 return;
190 value = DIAGNOSTICS_COLOR_AUTO;
192 else
193 value = DIAGNOSTICS_COLOR_DEFAULT;
195 pp_show_color (context->printer)
196 = colorize_init ((diagnostic_color_rule_t) value);
199 /* Do any cleaning up required after the last diagnostic is emitted. */
201 void
202 diagnostic_finish (diagnostic_context *context)
204 /* Some of the errors may actually have been warnings. */
205 if (diagnostic_kind_count (context, DK_WERROR))
207 /* -Werror was given. */
208 if (context->warning_as_error_requested)
209 pp_verbatim (context->printer,
210 _("%s: all warnings being treated as errors"),
211 progname);
212 /* At least one -Werror= was given. */
213 else
214 pp_verbatim (context->printer,
215 _("%s: some warnings being treated as errors"),
216 progname);
217 pp_newline_and_flush (context->printer);
220 diagnostic_file_cache_fini ();
222 XDELETEVEC (context->classify_diagnostic);
223 context->classify_diagnostic = NULL;
225 /* diagnostic_initialize allocates context->printer using XNEW
226 and placement-new. */
227 context->printer->~pretty_printer ();
228 XDELETE (context->printer);
229 context->printer = NULL;
232 /* Initialize DIAGNOSTIC, where the message MSG has already been
233 translated. */
234 void
235 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
236 va_list *args, rich_location *richloc,
237 diagnostic_t kind)
239 gcc_assert (richloc);
240 diagnostic->message.err_no = errno;
241 diagnostic->message.args_ptr = args;
242 diagnostic->message.format_spec = msg;
243 diagnostic->message.m_richloc = richloc;
244 diagnostic->richloc = richloc;
245 diagnostic->kind = kind;
246 diagnostic->option_index = 0;
249 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
250 translated. */
251 void
252 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
253 va_list *args, rich_location *richloc,
254 diagnostic_t kind)
256 gcc_assert (richloc);
257 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
260 static const char *const diagnostic_kind_color[] = {
261 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
262 #include "diagnostic.def"
263 #undef DEFINE_DIAGNOSTIC_KIND
264 NULL
267 /* Get a color name for diagnostics of type KIND
268 Result could be NULL. */
270 const char *
271 diagnostic_get_color_for_kind (diagnostic_t kind)
273 return diagnostic_kind_color[kind];
276 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
277 The caller is responsible for freeing the memory. */
279 static char *
280 diagnostic_get_location_text (diagnostic_context *context,
281 expanded_location s)
283 pretty_printer *pp = context->printer;
284 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
285 const char *locus_ce = colorize_stop (pp_show_color (pp));
287 if (s.file == NULL)
288 return build_message_string ("%s%s:%s", locus_cs, progname, locus_ce);
290 if (!strcmp (s.file, N_("<built-in>")))
291 return build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce);
293 if (context->show_column)
294 return build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
295 s.column, locus_ce);
296 else
297 return build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
298 locus_ce);
301 /* Return a malloc'd string describing a location and the severity of the
302 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
303 freeing the memory. */
304 char *
305 diagnostic_build_prefix (diagnostic_context *context,
306 const diagnostic_info *diagnostic)
308 static const char *const diagnostic_kind_text[] = {
309 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
310 #include "diagnostic.def"
311 #undef DEFINE_DIAGNOSTIC_KIND
312 "must-not-happen"
314 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
316 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
317 const char *text_cs = "", *text_ce = "";
318 pretty_printer *pp = context->printer;
320 if (diagnostic_kind_color[diagnostic->kind])
322 text_cs = colorize_start (pp_show_color (pp),
323 diagnostic_kind_color[diagnostic->kind]);
324 text_ce = colorize_stop (pp_show_color (pp));
327 expanded_location s = diagnostic_expand_location (diagnostic);
328 char *location_text = diagnostic_get_location_text (context, s);
330 char *result = build_message_string ("%s %s%s%s", location_text,
331 text_cs, text, text_ce);
332 free (location_text);
333 return result;
336 /* Functions at which to stop the backtrace print. It's not
337 particularly helpful to print the callers of these functions. */
339 static const char * const bt_stop[] =
341 "main",
342 "toplev::main",
343 "execute_one_pass",
344 "compile_file",
347 /* A callback function passed to the backtrace_full function. */
349 static int
350 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
351 const char *function)
353 int *pcount = (int *) data;
355 /* If we don't have any useful information, don't print
356 anything. */
357 if (filename == NULL && function == NULL)
358 return 0;
360 /* Skip functions in diagnostic.c. */
361 if (*pcount == 0
362 && filename != NULL
363 && strcmp (lbasename (filename), "diagnostic.c") == 0)
364 return 0;
366 /* Print up to 20 functions. We could make this a --param, but
367 since this is only for debugging just use a constant for now. */
368 if (*pcount >= 20)
370 /* Returning a non-zero value stops the backtrace. */
371 return 1;
373 ++*pcount;
375 char *alc = NULL;
376 if (function != NULL)
378 char *str = cplus_demangle_v3 (function,
379 (DMGL_VERBOSE | DMGL_ANSI
380 | DMGL_GNU_V3 | DMGL_PARAMS));
381 if (str != NULL)
383 alc = str;
384 function = str;
387 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
389 size_t len = strlen (bt_stop[i]);
390 if (strncmp (function, bt_stop[i], len) == 0
391 && (function[len] == '\0' || function[len] == '('))
393 if (alc != NULL)
394 free (alc);
395 /* Returning a non-zero value stops the backtrace. */
396 return 1;
401 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
402 (unsigned long) pc,
403 function == NULL ? "???" : function,
404 filename == NULL ? "???" : filename,
405 lineno);
407 if (alc != NULL)
408 free (alc);
410 return 0;
413 /* A callback function passed to the backtrace_full function. This is
414 called if backtrace_full has an error. */
416 static void
417 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
419 if (errnum < 0)
421 /* This means that no debug info was available. Just quietly
422 skip printing backtrace info. */
423 return;
425 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
426 errnum == 0 ? "" : xstrerror (errnum));
429 /* Take any action which is expected to happen after the diagnostic
430 is written out. This function does not always return. */
431 void
432 diagnostic_action_after_output (diagnostic_context *context,
433 diagnostic_t diag_kind)
435 switch (diag_kind)
437 case DK_DEBUG:
438 case DK_NOTE:
439 case DK_ANACHRONISM:
440 case DK_WARNING:
441 break;
443 case DK_ERROR:
444 case DK_SORRY:
445 if (context->abort_on_error)
446 real_abort ();
447 if (context->fatal_errors)
449 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
450 diagnostic_finish (context);
451 exit (FATAL_EXIT_CODE);
453 if (context->max_errors != 0
454 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
455 + diagnostic_kind_count (context, DK_SORRY)
456 + diagnostic_kind_count (context, DK_WERROR))
457 >= context->max_errors))
459 fnotice (stderr,
460 "compilation terminated due to -fmax-errors=%u.\n",
461 context->max_errors);
462 diagnostic_finish (context);
463 exit (FATAL_EXIT_CODE);
465 break;
467 case DK_ICE:
468 case DK_ICE_NOBT:
470 struct backtrace_state *state = NULL;
471 if (diag_kind == DK_ICE)
472 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
473 int count = 0;
474 if (state != NULL)
475 backtrace_full (state, 2, bt_callback, bt_err_callback,
476 (void *) &count);
478 if (context->abort_on_error)
479 real_abort ();
481 fnotice (stderr, "Please submit a full bug report,\n"
482 "with preprocessed source if appropriate.\n");
483 if (count > 0)
484 fnotice (stderr,
485 ("Please include the complete backtrace "
486 "with any bug report.\n"));
487 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
489 exit (ICE_EXIT_CODE);
492 case DK_FATAL:
493 if (context->abort_on_error)
494 real_abort ();
495 diagnostic_finish (context);
496 fnotice (stderr, "compilation terminated.\n");
497 exit (FATAL_EXIT_CODE);
499 default:
500 gcc_unreachable ();
504 void
505 diagnostic_report_current_module (diagnostic_context *context, location_t where)
507 const line_map_ordinary *map = NULL;
509 if (pp_needs_newline (context->printer))
511 pp_newline (context->printer);
512 pp_needs_newline (context->printer) = false;
515 if (where <= BUILTINS_LOCATION)
516 return;
518 linemap_resolve_location (line_table, where,
519 LRK_MACRO_DEFINITION_LOCATION,
520 &map);
522 if (map && diagnostic_last_module_changed (context, map))
524 diagnostic_set_last_module (context, map);
525 if (! MAIN_FILE_P (map))
527 map = INCLUDED_FROM (line_table, map);
528 if (context->show_column)
529 pp_verbatim (context->printer,
530 "In file included from %r%s:%d:%d%R", "locus",
531 LINEMAP_FILE (map),
532 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
533 else
534 pp_verbatim (context->printer,
535 "In file included from %r%s:%d%R", "locus",
536 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
537 while (! MAIN_FILE_P (map))
539 map = INCLUDED_FROM (line_table, map);
540 pp_verbatim (context->printer,
541 ",\n from %r%s:%d%R", "locus",
542 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
544 pp_verbatim (context->printer, ":");
545 pp_newline (context->printer);
550 void
551 default_diagnostic_starter (diagnostic_context *context,
552 diagnostic_info *diagnostic)
554 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
555 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
556 diagnostic));
559 void
560 default_diagnostic_start_span_fn (diagnostic_context *context,
561 expanded_location exploc)
563 pp_set_prefix (context->printer,
564 diagnostic_get_location_text (context, exploc));
565 pp_string (context->printer, "");
566 pp_newline (context->printer);
569 void
570 default_diagnostic_finalizer (diagnostic_context *context,
571 diagnostic_info *diagnostic)
573 diagnostic_show_locus (context, diagnostic);
574 pp_destroy_prefix (context->printer);
575 pp_flush (context->printer);
578 /* Interface to specify diagnostic kind overrides. Returns the
579 previous setting, or DK_UNSPECIFIED if the parameters are out of
580 range. If OPTION_INDEX is zero, the new setting is for all the
581 diagnostics. */
582 diagnostic_t
583 diagnostic_classify_diagnostic (diagnostic_context *context,
584 int option_index,
585 diagnostic_t new_kind,
586 location_t where)
588 diagnostic_t old_kind;
590 if (option_index < 0
591 || option_index >= context->n_opts
592 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
593 return DK_UNSPECIFIED;
595 old_kind = context->classify_diagnostic[option_index];
597 /* Handle pragmas separately, since we need to keep track of *where*
598 the pragmas were. */
599 if (where != UNKNOWN_LOCATION)
601 int i;
603 /* Record the command-line status, so we can reset it back on DK_POP. */
604 if (old_kind == DK_UNSPECIFIED)
606 old_kind = !context->option_enabled (option_index,
607 context->option_state)
608 ? DK_IGNORED : (context->warning_as_error_requested
609 ? DK_ERROR : DK_WARNING);
610 context->classify_diagnostic[option_index] = old_kind;
613 for (i = context->n_classification_history - 1; i >= 0; i --)
614 if (context->classification_history[i].option == option_index)
616 old_kind = context->classification_history[i].kind;
617 break;
620 i = context->n_classification_history;
621 context->classification_history =
622 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
623 * sizeof (diagnostic_classification_change_t));
624 context->classification_history[i].location = where;
625 context->classification_history[i].option = option_index;
626 context->classification_history[i].kind = new_kind;
627 context->n_classification_history ++;
629 else
630 context->classify_diagnostic[option_index] = new_kind;
632 return old_kind;
635 /* Save all diagnostic classifications in a stack. */
636 void
637 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
639 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
640 context->push_list[context->n_push ++] = context->n_classification_history;
643 /* Restore the topmost classification set off the stack. If the stack
644 is empty, revert to the state based on command line parameters. */
645 void
646 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
648 int jump_to;
649 int i;
651 if (context->n_push)
652 jump_to = context->push_list [-- context->n_push];
653 else
654 jump_to = 0;
656 i = context->n_classification_history;
657 context->classification_history =
658 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
659 * sizeof (diagnostic_classification_change_t));
660 context->classification_history[i].location = where;
661 context->classification_history[i].option = jump_to;
662 context->classification_history[i].kind = DK_POP;
663 context->n_classification_history ++;
666 /* Report a diagnostic message (an error or a warning) as specified by
667 DC. This function is *the* subroutine in terms of which front-ends
668 should implement their specific diagnostic handling modules. The
669 front-end independent format specifiers are exactly those described
670 in the documentation of output_format.
671 Return true if a diagnostic was printed, false otherwise. */
673 bool
674 diagnostic_report_diagnostic (diagnostic_context *context,
675 diagnostic_info *diagnostic)
677 location_t location = diagnostic_location (diagnostic);
678 diagnostic_t orig_diag_kind = diagnostic->kind;
679 const char *saved_format_spec;
681 /* Give preference to being able to inhibit warnings, before they
682 get reclassified to something else. */
683 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
684 && !diagnostic_report_warnings_p (context, location))
685 return false;
687 if (diagnostic->kind == DK_PEDWARN)
689 diagnostic->kind = pedantic_warning_kind (context);
690 /* We do this to avoid giving the message for -pedantic-errors. */
691 orig_diag_kind = diagnostic->kind;
694 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
695 return false;
697 if (context->lock > 0)
699 /* If we're reporting an ICE in the middle of some other error,
700 try to flush out the previous error, then let this one
701 through. Don't do this more than once. */
702 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
703 && context->lock == 1)
704 pp_newline_and_flush (context->printer);
705 else
706 error_recursion (context);
709 /* If the user requested that warnings be treated as errors, so be
710 it. Note that we do this before the next block so that
711 individual warnings can be overridden back to warnings with
712 -Wno-error=*. */
713 if (context->warning_as_error_requested
714 && diagnostic->kind == DK_WARNING)
716 diagnostic->kind = DK_ERROR;
719 if (diagnostic->option_index
720 && diagnostic->option_index != permissive_error_option (context))
722 diagnostic_t diag_class = DK_UNSPECIFIED;
724 /* This tests if the user provided the appropriate -Wfoo or
725 -Wno-foo option. */
726 if (! context->option_enabled (diagnostic->option_index,
727 context->option_state))
728 return false;
730 /* This tests for #pragma diagnostic changes. */
731 if (context->n_classification_history > 0)
733 /* FIXME: Stupid search. Optimize later. */
734 for (int i = context->n_classification_history - 1; i >= 0; i --)
736 if (linemap_location_before_p
737 (line_table,
738 context->classification_history[i].location,
739 location))
741 if (context->classification_history[i].kind == (int) DK_POP)
743 i = context->classification_history[i].option;
744 continue;
746 int option = context->classification_history[i].option;
747 /* The option 0 is for all the diagnostics. */
748 if (option == 0 || option == diagnostic->option_index)
750 diag_class = context->classification_history[i].kind;
751 if (diag_class != DK_UNSPECIFIED)
752 diagnostic->kind = diag_class;
753 break;
758 /* This tests if the user provided the appropriate -Werror=foo
759 option. */
760 if (diag_class == DK_UNSPECIFIED
761 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
763 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
765 /* This allows for future extensions, like temporarily disabling
766 warnings for ranges of source code. */
767 if (diagnostic->kind == DK_IGNORED)
768 return false;
771 context->lock++;
773 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
775 /* When not checking, ICEs are converted to fatal errors when an
776 error has already occurred. This is counteracted by
777 abort_on_error. */
778 if (!CHECKING_P
779 && (diagnostic_kind_count (context, DK_ERROR) > 0
780 || diagnostic_kind_count (context, DK_SORRY) > 0)
781 && !context->abort_on_error)
783 expanded_location s
784 = expand_location (diagnostic_location (diagnostic));
785 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
786 s.file, s.line);
787 exit (ICE_EXIT_CODE);
789 if (context->internal_error)
790 (*context->internal_error) (context,
791 diagnostic->message.format_spec,
792 diagnostic->message.args_ptr);
794 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
795 ++diagnostic_kind_count (context, DK_WERROR);
796 else
797 ++diagnostic_kind_count (context, diagnostic->kind);
799 saved_format_spec = diagnostic->message.format_spec;
800 if (context->show_option_requested)
802 char *option_text;
804 option_text = context->option_name (context, diagnostic->option_index,
805 orig_diag_kind, diagnostic->kind);
807 if (option_text)
809 const char *cs
810 = colorize_start (pp_show_color (context->printer),
811 diagnostic_kind_color[diagnostic->kind]);
812 const char *ce = colorize_stop (pp_show_color (context->printer));
813 diagnostic->message.format_spec
814 = ACONCAT ((diagnostic->message.format_spec,
815 " ",
816 "[", cs, option_text, ce, "]",
817 NULL));
818 free (option_text);
821 diagnostic->message.x_data = &diagnostic->x_data;
822 diagnostic->x_data = NULL;
823 pp_format (context->printer, &diagnostic->message);
824 (*diagnostic_starter (context)) (context, diagnostic);
825 pp_output_formatted_text (context->printer);
826 (*diagnostic_finalizer (context)) (context, diagnostic);
827 diagnostic_action_after_output (context, diagnostic->kind);
828 diagnostic->message.format_spec = saved_format_spec;
829 diagnostic->x_data = NULL;
831 context->lock--;
833 return true;
836 /* Given a partial pathname as input, return another pathname that
837 shares no directory elements with the pathname of __FILE__. This
838 is used by fancy_abort() to print `Internal compiler error in expr.c'
839 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
841 const char *
842 trim_filename (const char *name)
844 static const char this_file[] = __FILE__;
845 const char *p = name, *q = this_file;
847 /* First skip any "../" in each filename. This allows us to give a proper
848 reference to a file in a subdirectory. */
849 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
850 p += 3;
852 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
853 q += 3;
855 /* Now skip any parts the two filenames have in common. */
856 while (*p == *q && *p != 0 && *q != 0)
857 p++, q++;
859 /* Now go backwards until the previous directory separator. */
860 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
861 p--;
863 return p;
866 /* Standard error reporting routines in increasing order of severity.
867 All of these take arguments like printf. */
869 /* Text to be emitted verbatim to the error message stream; this
870 produces no prefix and disables line-wrapping. Use rarely. */
871 void
872 verbatim (const char *gmsgid, ...)
874 text_info text;
875 va_list ap;
877 va_start (ap, gmsgid);
878 text.err_no = errno;
879 text.args_ptr = &ap;
880 text.format_spec = _(gmsgid);
881 text.x_data = NULL;
882 pp_format_verbatim (global_dc->printer, &text);
883 pp_newline_and_flush (global_dc->printer);
884 va_end (ap);
887 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
888 void
889 diagnostic_append_note (diagnostic_context *context,
890 location_t location,
891 const char * gmsgid, ...)
893 diagnostic_info diagnostic;
894 va_list ap;
895 const char *saved_prefix;
896 rich_location richloc (line_table, location);
898 va_start (ap, gmsgid);
899 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
900 if (context->inhibit_notes_p)
902 va_end (ap);
903 return;
905 saved_prefix = pp_get_prefix (context->printer);
906 pp_set_prefix (context->printer,
907 diagnostic_build_prefix (context, &diagnostic));
908 pp_format (context->printer, &diagnostic.message);
909 pp_output_formatted_text (context->printer);
910 pp_destroy_prefix (context->printer);
911 pp_set_prefix (context->printer, saved_prefix);
912 diagnostic_show_locus (context, &diagnostic);
913 va_end (ap);
916 bool
917 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
918 const char *gmsgid, ...)
920 diagnostic_info diagnostic;
921 va_list ap;
922 bool ret;
923 rich_location richloc (line_table, location);
925 va_start (ap, gmsgid);
926 if (kind == DK_PERMERROR)
928 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
929 permissive_error_kind (global_dc));
930 diagnostic.option_index = permissive_error_option (global_dc);
932 else {
933 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, kind);
934 if (kind == DK_WARNING || kind == DK_PEDWARN)
935 diagnostic.option_index = opt;
938 ret = report_diagnostic (&diagnostic);
939 va_end (ap);
940 return ret;
943 /* An informative note at LOCATION. Use this for additional details on an error
944 message. */
945 void
946 inform (location_t location, const char *gmsgid, ...)
948 diagnostic_info diagnostic;
949 va_list ap;
950 rich_location richloc (line_table, location);
952 va_start (ap, gmsgid);
953 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
954 report_diagnostic (&diagnostic);
955 va_end (ap);
958 /* Same as "inform", but at RICHLOC. */
959 void
960 inform_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
962 diagnostic_info diagnostic;
963 va_list ap;
965 va_start (ap, gmsgid);
966 diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc, DK_NOTE);
967 report_diagnostic (&diagnostic);
968 va_end (ap);
971 /* An informative note at LOCATION. Use this for additional details on an
972 error message. */
973 void
974 inform_n (location_t location, int n, const char *singular_gmsgid,
975 const char *plural_gmsgid, ...)
977 diagnostic_info diagnostic;
978 va_list ap;
979 rich_location richloc (line_table, location);
981 va_start (ap, plural_gmsgid);
982 diagnostic_set_info_translated (&diagnostic,
983 ngettext (singular_gmsgid, plural_gmsgid, n),
984 &ap, &richloc, DK_NOTE);
985 report_diagnostic (&diagnostic);
986 va_end (ap);
989 /* A warning at INPUT_LOCATION. Use this for code which is correct according
990 to the relevant language specification but is likely to be buggy anyway.
991 Returns true if the warning was printed, false if it was inhibited. */
992 bool
993 warning (int opt, const char *gmsgid, ...)
995 diagnostic_info diagnostic;
996 va_list ap;
997 bool ret;
998 rich_location richloc (line_table, input_location);
1000 va_start (ap, gmsgid);
1001 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_WARNING);
1002 diagnostic.option_index = opt;
1004 ret = report_diagnostic (&diagnostic);
1005 va_end (ap);
1006 return ret;
1009 /* A warning at LOCATION. Use this for code which is correct according to the
1010 relevant language specification but is likely to be buggy anyway.
1011 Returns true if the warning was printed, false if it was inhibited. */
1013 bool
1014 warning_at (location_t location, int opt, const char *gmsgid, ...)
1016 diagnostic_info diagnostic;
1017 va_list ap;
1018 bool ret;
1019 rich_location richloc (line_table, location);
1021 va_start (ap, gmsgid);
1022 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_WARNING);
1023 diagnostic.option_index = opt;
1024 ret = report_diagnostic (&diagnostic);
1025 va_end (ap);
1026 return ret;
1029 /* Same as warning at, but using RICHLOC. */
1031 bool
1032 warning_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1034 diagnostic_info diagnostic;
1035 va_list ap;
1036 bool ret;
1038 va_start (ap, gmsgid);
1039 diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc, DK_WARNING);
1040 diagnostic.option_index = opt;
1041 ret = report_diagnostic (&diagnostic);
1042 va_end (ap);
1043 return ret;
1046 /* A warning at LOCATION. Use this for code which is correct according to the
1047 relevant language specification but is likely to be buggy anyway.
1048 Returns true if the warning was printed, false if it was inhibited. */
1050 bool
1051 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1052 const char *plural_gmsgid, ...)
1054 diagnostic_info diagnostic;
1055 va_list ap;
1056 bool ret;
1057 rich_location richloc (line_table, location);
1059 va_start (ap, plural_gmsgid);
1060 diagnostic_set_info_translated (&diagnostic,
1061 ngettext (singular_gmsgid, plural_gmsgid, n),
1062 &ap, &richloc, DK_WARNING
1064 diagnostic.option_index = opt;
1065 ret = report_diagnostic (&diagnostic);
1066 va_end (ap);
1067 return ret;
1070 /* A "pedantic" warning at LOCATION: issues a warning unless
1071 -pedantic-errors was given on the command line, in which case it
1072 issues an error. Use this for diagnostics required by the relevant
1073 language standard, if you have chosen not to make them errors.
1075 Note that these diagnostics are issued independent of the setting
1076 of the -Wpedantic command-line switch. To get a warning enabled
1077 only with that switch, use either "if (pedantic) pedwarn
1078 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1079 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1081 Returns true if the warning was printed, false if it was inhibited. */
1083 bool
1084 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1086 diagnostic_info diagnostic;
1087 va_list ap;
1088 bool ret;
1089 rich_location richloc (line_table, location);
1091 va_start (ap, gmsgid);
1092 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_PEDWARN);
1093 diagnostic.option_index = opt;
1094 ret = report_diagnostic (&diagnostic);
1095 va_end (ap);
1096 return ret;
1099 /* A "permissive" error at LOCATION: issues an error unless
1100 -fpermissive was given on the command line, in which case it issues
1101 a warning. Use this for things that really should be errors but we
1102 want to support legacy code.
1104 Returns true if the warning was printed, false if it was inhibited. */
1106 bool
1107 permerror (location_t location, const char *gmsgid, ...)
1109 diagnostic_info diagnostic;
1110 va_list ap;
1111 bool ret;
1112 rich_location richloc (line_table, location);
1114 va_start (ap, gmsgid);
1115 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
1116 permissive_error_kind (global_dc));
1117 diagnostic.option_index = permissive_error_option (global_dc);
1118 ret = report_diagnostic (&diagnostic);
1119 va_end (ap);
1120 return ret;
1123 /* Same as "permerror", but at RICHLOC. */
1125 bool
1126 permerror_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1128 diagnostic_info diagnostic;
1129 va_list ap;
1130 bool ret;
1132 va_start (ap, gmsgid);
1133 diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc,
1134 permissive_error_kind (global_dc));
1135 diagnostic.option_index = permissive_error_option (global_dc);
1136 ret = report_diagnostic (&diagnostic);
1137 va_end (ap);
1138 return ret;
1141 /* A hard error: the code is definitely ill-formed, and an object file
1142 will not be produced. */
1143 void
1144 error (const char *gmsgid, ...)
1146 diagnostic_info diagnostic;
1147 va_list ap;
1148 rich_location richloc (line_table, input_location);
1150 va_start (ap, gmsgid);
1151 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR);
1152 report_diagnostic (&diagnostic);
1153 va_end (ap);
1156 /* A hard error: the code is definitely ill-formed, and an object file
1157 will not be produced. */
1158 void
1159 error_n (location_t location, int n, const char *singular_gmsgid,
1160 const char *plural_gmsgid, ...)
1162 diagnostic_info diagnostic;
1163 va_list ap;
1164 rich_location richloc (line_table, location);
1166 va_start (ap, plural_gmsgid);
1167 diagnostic_set_info_translated (&diagnostic,
1168 ngettext (singular_gmsgid, plural_gmsgid, n),
1169 &ap, &richloc, DK_ERROR);
1170 report_diagnostic (&diagnostic);
1171 va_end (ap);
1174 /* Same as ebove, but use location LOC instead of input_location. */
1175 void
1176 error_at (location_t loc, const char *gmsgid, ...)
1178 diagnostic_info diagnostic;
1179 va_list ap;
1180 rich_location richloc (line_table, loc);
1182 va_start (ap, gmsgid);
1183 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR);
1184 report_diagnostic (&diagnostic);
1185 va_end (ap);
1188 /* Same as above, but use RICH_LOC. */
1190 void
1191 error_at_rich_loc (rich_location *rich_loc, const char *gmsgid, ...)
1193 diagnostic_info diagnostic;
1194 va_list ap;
1196 va_start (ap, gmsgid);
1197 diagnostic_set_info (&diagnostic, gmsgid, &ap, rich_loc,
1198 DK_ERROR);
1199 report_diagnostic (&diagnostic);
1200 va_end (ap);
1203 /* "Sorry, not implemented." Use for a language feature which is
1204 required by the relevant specification but not implemented by GCC.
1205 An object file will not be produced. */
1206 void
1207 sorry (const char *gmsgid, ...)
1209 diagnostic_info diagnostic;
1210 va_list ap;
1211 rich_location richloc (line_table, input_location);
1213 va_start (ap, gmsgid);
1214 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_SORRY);
1215 report_diagnostic (&diagnostic);
1216 va_end (ap);
1219 /* Return true if an error or a "sorry" has been seen. Various
1220 processing is disabled after errors. */
1221 bool
1222 seen_error (void)
1224 return errorcount || sorrycount;
1227 /* An error which is severe enough that we make no attempt to
1228 continue. Do not use this for internal consistency checks; that's
1229 internal_error. Use of this function should be rare. */
1230 void
1231 fatal_error (location_t loc, const char *gmsgid, ...)
1233 diagnostic_info diagnostic;
1234 va_list ap;
1235 rich_location richloc (line_table, loc);
1237 va_start (ap, gmsgid);
1238 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_FATAL);
1239 report_diagnostic (&diagnostic);
1240 va_end (ap);
1242 gcc_unreachable ();
1245 /* An internal consistency check has failed. We make no attempt to
1246 continue. Note that unless there is debugging value to be had from
1247 a more specific message, or some other good reason, you should use
1248 abort () instead of calling this function directly. */
1249 void
1250 internal_error (const char *gmsgid, ...)
1252 diagnostic_info diagnostic;
1253 va_list ap;
1254 rich_location richloc (line_table, input_location);
1256 va_start (ap, gmsgid);
1257 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ICE);
1258 report_diagnostic (&diagnostic);
1259 va_end (ap);
1261 gcc_unreachable ();
1264 /* Like internal_error, but no backtrace will be printed. Used when
1265 the internal error does not happen at the current location, but happened
1266 somewhere else. */
1267 void
1268 internal_error_no_backtrace (const char *gmsgid, ...)
1270 diagnostic_info diagnostic;
1271 va_list ap;
1272 rich_location richloc (line_table, input_location);
1274 va_start (ap, gmsgid);
1275 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ICE_NOBT);
1276 report_diagnostic (&diagnostic);
1277 va_end (ap);
1279 gcc_unreachable ();
1282 /* Special case error functions. Most are implemented in terms of the
1283 above, or should be. */
1285 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1286 runs its second argument through gettext. */
1287 void
1288 fnotice (FILE *file, const char *cmsgid, ...)
1290 va_list ap;
1292 va_start (ap, cmsgid);
1293 vfprintf (file, _(cmsgid), ap);
1294 va_end (ap);
1297 /* Inform the user that an error occurred while trying to report some
1298 other error. This indicates catastrophic internal inconsistencies,
1299 so give up now. But do try to flush out the previous error.
1300 This mustn't use internal_error, that will cause infinite recursion. */
1302 static void
1303 error_recursion (diagnostic_context *context)
1305 if (context->lock < 3)
1306 pp_newline_and_flush (context->printer);
1308 fnotice (stderr,
1309 "Internal compiler error: Error reporting routines re-entered.\n");
1311 /* Call diagnostic_action_after_output to get the "please submit a bug
1312 report" message. */
1313 diagnostic_action_after_output (context, DK_ICE);
1315 /* Do not use gcc_unreachable here; that goes through internal_error
1316 and therefore would cause infinite recursion. */
1317 real_abort ();
1320 /* Report an internal compiler error in a friendly manner. This is
1321 the function that gets called upon use of abort() in the source
1322 code generally, thanks to a special macro. */
1324 void
1325 fancy_abort (const char *file, int line, const char *function)
1327 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1330 /* Really call the system 'abort'. This has to go right at the end of
1331 this file, so that there are no functions after it that call abort
1332 and get the system abort instead of our macro. */
1333 #undef abort
1334 static void
1335 real_abort (void)
1337 abort ();