2014-04-14 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / diagnostic.c
blob0cc75934da0dd801218a37351a9a303ef6dbf278
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2014 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 "input.h"
31 #include "intl.h"
32 #include "backtrace.h"
33 #include "diagnostic.h"
34 #include "diagnostic-color.h"
36 #include <new> // For placement new.
38 #define pedantic_warning_kind(DC) \
39 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
40 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
41 #define permissive_error_option(DC) ((DC)->opt_permissive)
43 /* Prototypes. */
44 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
46 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
48 static void diagnostic_action_after_output (diagnostic_context *,
49 diagnostic_info *);
50 static void real_abort (void) ATTRIBUTE_NORETURN;
52 /* Name of program invoked, sans directories. */
54 const char *progname;
56 /* A diagnostic_context surrogate for stderr. */
57 static diagnostic_context global_diagnostic_context;
58 diagnostic_context *global_dc = &global_diagnostic_context;
60 /* Return a malloc'd string containing MSG formatted a la printf. The
61 caller is responsible for freeing the memory. */
62 static char *
63 build_message_string (const char *msg, ...)
65 char *str;
66 va_list ap;
68 va_start (ap, msg);
69 vasprintf (&str, msg, ap);
70 va_end (ap);
72 return str;
75 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
76 char *
77 file_name_as_prefix (diagnostic_context *context, const char *f)
79 const char *locus_cs
80 = colorize_start (pp_show_color (context->printer), "locus");
81 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
82 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
87 /* Return the value of the getenv("COLUMNS") as an integer. If the
88 value is not set to a positive integer, then return INT_MAX. */
89 static int
90 getenv_columns (void)
92 const char * s = getenv ("COLUMNS");
93 if (s != NULL) {
94 int n = atoi (s);
95 if (n > 0)
96 return n;
98 return INT_MAX;
101 /* Set caret_max_width to value. */
102 void
103 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
105 /* One minus to account for the leading empty space. */
106 value = value ? value - 1
107 : (isatty (fileno (pp_buffer (context->printer)->stream))
108 ? getenv_columns () - 1: INT_MAX);
110 if (value <= 0)
111 value = INT_MAX;
113 context->caret_max_width = value;
116 /* Initialize the diagnostic message outputting machinery. */
117 void
118 diagnostic_initialize (diagnostic_context *context, int n_opts)
120 int i;
122 /* Allocate a basic pretty-printer. Clients will replace this a
123 much more elaborated pretty-printer if they wish. */
124 context->printer = XNEW (pretty_printer);
125 new (context->printer) pretty_printer ();
127 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
128 context->some_warnings_are_errors = false;
129 context->warning_as_error_requested = false;
130 context->n_opts = n_opts;
131 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
132 for (i = 0; i < n_opts; i++)
133 context->classify_diagnostic[i] = DK_UNSPECIFIED;
134 context->show_caret = false;
135 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
136 context->show_option_requested = false;
137 context->abort_on_error = false;
138 context->show_column = false;
139 context->pedantic_errors = false;
140 context->permissive = false;
141 context->opt_permissive = 0;
142 context->fatal_errors = false;
143 context->dc_inhibit_warnings = false;
144 context->dc_warn_system_headers = false;
145 context->max_errors = 0;
146 context->internal_error = NULL;
147 diagnostic_starter (context) = default_diagnostic_starter;
148 diagnostic_finalizer (context) = default_diagnostic_finalizer;
149 context->option_enabled = NULL;
150 context->option_state = NULL;
151 context->option_name = NULL;
152 context->last_location = UNKNOWN_LOCATION;
153 context->last_module = 0;
154 context->x_data = NULL;
155 context->lock = 0;
156 context->inhibit_notes_p = false;
159 /* Do any cleaning up required after the last diagnostic is emitted. */
161 void
162 diagnostic_finish (diagnostic_context *context)
164 /* Some of the errors may actually have been warnings. */
165 if (context->some_warnings_are_errors)
167 /* -Werror was given. */
168 if (context->warning_as_error_requested)
169 pp_verbatim (context->printer,
170 _("%s: all warnings being treated as errors"),
171 progname);
172 /* At least one -Werror= was given. */
173 else
174 pp_verbatim (context->printer,
175 _("%s: some warnings being treated as errors"),
176 progname);
177 pp_newline_and_flush (context->printer);
180 diagnostic_file_cache_fini ();
183 /* Initialize DIAGNOSTIC, where the message MSG has already been
184 translated. */
185 void
186 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
187 va_list *args, location_t location,
188 diagnostic_t kind)
190 diagnostic->message.err_no = errno;
191 diagnostic->message.args_ptr = args;
192 diagnostic->message.format_spec = msg;
193 diagnostic->location = location;
194 diagnostic->override_column = 0;
195 diagnostic->kind = kind;
196 diagnostic->option_index = 0;
199 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
200 translated. */
201 void
202 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
203 va_list *args, location_t location,
204 diagnostic_t kind)
206 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
209 /* Return a malloc'd string describing a location. The caller is
210 responsible for freeing the memory. */
211 char *
212 diagnostic_build_prefix (diagnostic_context *context,
213 const diagnostic_info *diagnostic)
215 static const char *const diagnostic_kind_text[] = {
216 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
217 #include "diagnostic.def"
218 #undef DEFINE_DIAGNOSTIC_KIND
219 "must-not-happen"
221 static const char *const diagnostic_kind_color[] = {
222 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
223 #include "diagnostic.def"
224 #undef DEFINE_DIAGNOSTIC_KIND
225 NULL
227 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
228 const char *text_cs = "", *text_ce = "";
229 const char *locus_cs, *locus_ce;
230 pretty_printer *pp = context->printer;
232 if (diagnostic_kind_color[diagnostic->kind])
234 text_cs = colorize_start (pp_show_color (pp),
235 diagnostic_kind_color[diagnostic->kind]);
236 text_ce = colorize_stop (pp_show_color (pp));
238 locus_cs = colorize_start (pp_show_color (pp), "locus");
239 locus_ce = colorize_stop (pp_show_color (pp));
241 expanded_location s = expand_location_to_spelling_point (diagnostic->location);
242 if (diagnostic->override_column)
243 s.column = diagnostic->override_column;
244 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
246 return
247 (s.file == NULL
248 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce,
249 text_cs, text, text_ce)
250 : !strcmp (s.file, N_("<built-in>"))
251 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, s.file, locus_ce,
252 text_cs, text, text_ce)
253 : context->show_column
254 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
255 s.column, locus_ce, text_cs, text, text_ce)
256 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line, locus_ce,
257 text_cs, text, text_ce));
260 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
261 MAX_WIDTH by some margin, then adjust the start of the line such
262 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
263 margin is either 10 characters or the difference between the column
264 and the length of the line, whatever is smaller. The length of
265 LINE is given by LINE_WIDTH. */
266 static const char *
267 adjust_line (const char *line, int line_width,
268 int max_width, int *column_p)
270 int right_margin = 10;
271 int column = *column_p;
273 right_margin = MIN (line_width - column, right_margin);
274 right_margin = max_width - right_margin;
275 if (line_width >= max_width && column > right_margin)
277 line += column - right_margin;
278 *column_p = right_margin;
280 return line;
283 /* Print the physical source line corresponding to the location of
284 this diagnostics, and a caret indicating the precise column. */
285 void
286 diagnostic_show_locus (diagnostic_context * context,
287 const diagnostic_info *diagnostic)
289 const char *line;
290 int line_width;
291 char *buffer;
292 expanded_location s;
293 int max_width;
294 const char *saved_prefix;
295 const char *caret_cs, *caret_ce;
297 if (!context->show_caret
298 || diagnostic->location <= BUILTINS_LOCATION
299 || diagnostic->location == context->last_location)
300 return;
302 context->last_location = diagnostic->location;
303 s = expand_location_to_spelling_point (diagnostic->location);
304 line = location_get_source_line (s, &line_width);
305 if (line == NULL)
306 return;
308 max_width = context->caret_max_width;
309 line = adjust_line (line, line_width, max_width, &(s.column));
311 pp_newline (context->printer);
312 saved_prefix = pp_get_prefix (context->printer);
313 pp_set_prefix (context->printer, NULL);
314 pp_space (context->printer);
315 while (max_width > 0 && line_width > 0)
317 char c = *line == '\t' ? ' ' : *line;
318 if (c == '\0')
319 c = ' ';
320 pp_character (context->printer, c);
321 max_width--;
322 line_width--;
323 line++;
325 pp_newline (context->printer);
326 caret_cs = colorize_start (pp_show_color (context->printer), "caret");
327 caret_ce = colorize_stop (pp_show_color (context->printer));
329 /* pp_printf does not implement %*c. */
330 size_t len = s.column + 3 + strlen (caret_cs) + strlen (caret_ce);
331 buffer = XALLOCAVEC (char, len);
332 snprintf (buffer, len, "%s %*c%s", caret_cs, s.column, '^', caret_ce);
333 pp_string (context->printer, buffer);
334 pp_set_prefix (context->printer, saved_prefix);
337 /* Functions at which to stop the backtrace print. It's not
338 particularly helpful to print the callers of these functions. */
340 static const char * const bt_stop[] =
342 "main",
343 "toplev_main",
344 "execute_one_pass",
345 "compile_file",
348 /* A callback function passed to the backtrace_full function. */
350 static int
351 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
352 const char *function)
354 int *pcount = (int *) data;
356 /* If we don't have any useful information, don't print
357 anything. */
358 if (filename == NULL && function == NULL)
359 return 0;
361 /* Skip functions in diagnostic.c. */
362 if (*pcount == 0
363 && filename != NULL
364 && strcmp (lbasename (filename), "diagnostic.c") == 0)
365 return 0;
367 /* Print up to 20 functions. We could make this a --param, but
368 since this is only for debugging just use a constant for now. */
369 if (*pcount >= 20)
371 /* Returning a non-zero value stops the backtrace. */
372 return 1;
374 ++*pcount;
376 char *alc = NULL;
377 if (function != NULL)
379 char *str = cplus_demangle_v3 (function,
380 (DMGL_VERBOSE | DMGL_ANSI
381 | DMGL_GNU_V3 | DMGL_PARAMS));
382 if (str != NULL)
384 alc = str;
385 function = str;
388 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
390 size_t len = strlen (bt_stop[i]);
391 if (strncmp (function, bt_stop[i], len) == 0
392 && (function[len] == '\0' || function[len] == '('))
394 if (alc != NULL)
395 free (alc);
396 /* Returning a non-zero value stops the backtrace. */
397 return 1;
402 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
403 (unsigned long) pc,
404 function == NULL ? "???" : function,
405 filename == NULL ? "???" : filename,
406 lineno);
408 if (alc != NULL)
409 free (alc);
411 return 0;
414 /* A callback function passed to the backtrace_full function. This is
415 called if backtrace_full has an error. */
417 static void
418 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
420 if (errnum < 0)
422 /* This means that no debug info was available. Just quietly
423 skip printing backtrace info. */
424 return;
426 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
427 errnum == 0 ? "" : xstrerror (errnum));
430 /* Take any action which is expected to happen after the diagnostic
431 is written out. This function does not always return. */
432 static void
433 diagnostic_action_after_output (diagnostic_context *context,
434 diagnostic_info *diagnostic)
436 switch (diagnostic->kind)
438 case DK_DEBUG:
439 case DK_NOTE:
440 case DK_ANACHRONISM:
441 case DK_WARNING:
442 break;
444 case DK_ERROR:
445 case DK_SORRY:
446 if (context->abort_on_error)
447 real_abort ();
448 if (context->fatal_errors)
450 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
451 diagnostic_finish (context);
452 exit (FATAL_EXIT_CODE);
454 if (context->max_errors != 0
455 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
456 + diagnostic_kind_count (context, DK_SORRY))
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:
469 struct backtrace_state *state =
470 backtrace_create_state (NULL, 0, bt_err_callback, NULL);
471 int count = 0;
472 if (state != NULL)
473 backtrace_full (state, 2, bt_callback, bt_err_callback,
474 (void *) &count);
476 if (context->abort_on_error)
477 real_abort ();
479 fnotice (stderr, "Please submit a full bug report,\n"
480 "with preprocessed source if appropriate.\n");
481 if (count > 0)
482 fnotice (stderr,
483 ("Please include the complete backtrace "
484 "with any bug report.\n"));
485 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
487 exit (ICE_EXIT_CODE);
490 case DK_FATAL:
491 if (context->abort_on_error)
492 real_abort ();
493 diagnostic_finish (context);
494 fnotice (stderr, "compilation terminated.\n");
495 exit (FATAL_EXIT_CODE);
497 default:
498 gcc_unreachable ();
502 void
503 diagnostic_report_current_module (diagnostic_context *context, location_t where)
505 const struct line_map *map = NULL;
507 if (pp_needs_newline (context->printer))
509 pp_newline (context->printer);
510 pp_needs_newline (context->printer) = false;
513 if (where <= BUILTINS_LOCATION)
514 return;
516 linemap_resolve_location (line_table, where,
517 LRK_MACRO_DEFINITION_LOCATION,
518 &map);
520 if (map && diagnostic_last_module_changed (context, map))
522 diagnostic_set_last_module (context, map);
523 if (! MAIN_FILE_P (map))
525 map = INCLUDED_FROM (line_table, map);
526 if (context->show_column)
527 pp_verbatim (context->printer,
528 "In file included from %r%s:%d:%d%R", "locus",
529 LINEMAP_FILE (map),
530 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
531 else
532 pp_verbatim (context->printer,
533 "In file included from %r%s:%d%R", "locus",
534 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
535 while (! MAIN_FILE_P (map))
537 map = INCLUDED_FROM (line_table, map);
538 pp_verbatim (context->printer,
539 ",\n from %r%s:%d%R", "locus",
540 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
542 pp_verbatim (context->printer, ":");
543 pp_newline (context->printer);
548 void
549 default_diagnostic_starter (diagnostic_context *context,
550 diagnostic_info *diagnostic)
552 diagnostic_report_current_module (context, diagnostic->location);
553 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
554 diagnostic));
557 void
558 default_diagnostic_finalizer (diagnostic_context *context ATTRIBUTE_UNUSED,
559 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
563 /* Interface to specify diagnostic kind overrides. Returns the
564 previous setting, or DK_UNSPECIFIED if the parameters are out of
565 range. If OPTION_INDEX is zero, the new setting is for all the
566 diagnostics. */
567 diagnostic_t
568 diagnostic_classify_diagnostic (diagnostic_context *context,
569 int option_index,
570 diagnostic_t new_kind,
571 location_t where)
573 diagnostic_t old_kind;
575 if (option_index < 0
576 || option_index >= context->n_opts
577 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
578 return DK_UNSPECIFIED;
580 old_kind = context->classify_diagnostic[option_index];
582 /* Handle pragmas separately, since we need to keep track of *where*
583 the pragmas were. */
584 if (where != UNKNOWN_LOCATION)
586 int i;
588 for (i = context->n_classification_history - 1; i >= 0; i --)
589 if (context->classification_history[i].option == option_index)
591 old_kind = context->classification_history[i].kind;
592 break;
595 i = context->n_classification_history;
596 context->classification_history =
597 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
598 * sizeof (diagnostic_classification_change_t));
599 context->classification_history[i].location = where;
600 context->classification_history[i].option = option_index;
601 context->classification_history[i].kind = new_kind;
602 context->n_classification_history ++;
604 else
605 context->classify_diagnostic[option_index] = new_kind;
607 return old_kind;
610 /* Save all diagnostic classifications in a stack. */
611 void
612 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
614 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
615 context->push_list[context->n_push ++] = context->n_classification_history;
618 /* Restore the topmost classification set off the stack. If the stack
619 is empty, revert to the state based on command line parameters. */
620 void
621 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
623 int jump_to;
624 int i;
626 if (context->n_push)
627 jump_to = context->push_list [-- context->n_push];
628 else
629 jump_to = 0;
631 i = context->n_classification_history;
632 context->classification_history =
633 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
634 * sizeof (diagnostic_classification_change_t));
635 context->classification_history[i].location = where;
636 context->classification_history[i].option = jump_to;
637 context->classification_history[i].kind = DK_POP;
638 context->n_classification_history ++;
641 /* Report a diagnostic message (an error or a warning) as specified by
642 DC. This function is *the* subroutine in terms of which front-ends
643 should implement their specific diagnostic handling modules. The
644 front-end independent format specifiers are exactly those described
645 in the documentation of output_format.
646 Return true if a diagnostic was printed, false otherwise. */
648 bool
649 diagnostic_report_diagnostic (diagnostic_context *context,
650 diagnostic_info *diagnostic)
652 location_t location = diagnostic->location;
653 diagnostic_t orig_diag_kind = diagnostic->kind;
654 const char *saved_format_spec;
656 /* Give preference to being able to inhibit warnings, before they
657 get reclassified to something else. */
658 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
659 && !diagnostic_report_warnings_p (context, location))
660 return false;
662 if (diagnostic->kind == DK_PEDWARN)
664 diagnostic->kind = pedantic_warning_kind (context);
665 /* We do this to avoid giving the message for -pedantic-errors. */
666 orig_diag_kind = diagnostic->kind;
669 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
670 return false;
672 if (context->lock > 0)
674 /* If we're reporting an ICE in the middle of some other error,
675 try to flush out the previous error, then let this one
676 through. Don't do this more than once. */
677 if (diagnostic->kind == DK_ICE && context->lock == 1)
678 pp_newline_and_flush (context->printer);
679 else
680 error_recursion (context);
683 /* If the user requested that warnings be treated as errors, so be
684 it. Note that we do this before the next block so that
685 individual warnings can be overridden back to warnings with
686 -Wno-error=*. */
687 if (context->warning_as_error_requested
688 && diagnostic->kind == DK_WARNING)
690 diagnostic->kind = DK_ERROR;
693 if (diagnostic->option_index
694 && diagnostic->option_index != permissive_error_option (context))
696 diagnostic_t diag_class = DK_UNSPECIFIED;
698 /* This tests if the user provided the appropriate -Wfoo or
699 -Wno-foo option. */
700 if (! context->option_enabled (diagnostic->option_index,
701 context->option_state))
702 return false;
704 /* This tests for #pragma diagnostic changes. */
705 if (context->n_classification_history > 0)
707 /* FIXME: Stupid search. Optimize later. */
708 for (int i = context->n_classification_history - 1; i >= 0; i --)
710 if (linemap_location_before_p
711 (line_table,
712 context->classification_history[i].location,
713 location))
715 if (context->classification_history[i].kind == (int) DK_POP)
717 i = context->classification_history[i].option;
718 continue;
720 int option = context->classification_history[i].option;
721 /* The option 0 is for all the diagnostics. */
722 if (option == 0 || option == diagnostic->option_index)
724 diag_class = context->classification_history[i].kind;
725 if (diag_class != DK_UNSPECIFIED)
726 diagnostic->kind = diag_class;
727 break;
732 /* This tests if the user provided the appropriate -Werror=foo
733 option. */
734 if (diag_class == DK_UNSPECIFIED
735 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
737 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
739 /* This allows for future extensions, like temporarily disabling
740 warnings for ranges of source code. */
741 if (diagnostic->kind == DK_IGNORED)
742 return false;
745 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
746 context->some_warnings_are_errors = true;
748 context->lock++;
750 if (diagnostic->kind == DK_ICE)
752 #ifndef ENABLE_CHECKING
753 /* When not checking, ICEs are converted to fatal errors when an
754 error has already occurred. This is counteracted by
755 abort_on_error. */
756 if ((diagnostic_kind_count (context, DK_ERROR) > 0
757 || diagnostic_kind_count (context, DK_SORRY) > 0)
758 && !context->abort_on_error)
760 expanded_location s = expand_location (diagnostic->location);
761 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
762 s.file, s.line);
763 exit (ICE_EXIT_CODE);
765 #endif
766 if (context->internal_error)
767 (*context->internal_error) (context,
768 diagnostic->message.format_spec,
769 diagnostic->message.args_ptr);
771 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
772 ++diagnostic_kind_count (context, DK_WERROR);
773 else
774 ++diagnostic_kind_count (context, diagnostic->kind);
776 saved_format_spec = diagnostic->message.format_spec;
777 if (context->show_option_requested)
779 char *option_text;
781 option_text = context->option_name (context, diagnostic->option_index,
782 orig_diag_kind, diagnostic->kind);
784 if (option_text)
786 diagnostic->message.format_spec
787 = ACONCAT ((diagnostic->message.format_spec,
788 " ",
789 "[", option_text, "]",
790 NULL));
791 free (option_text);
794 diagnostic->message.locus = &diagnostic->location;
795 diagnostic->message.x_data = &diagnostic->x_data;
796 diagnostic->x_data = NULL;
797 pp_format (context->printer, &diagnostic->message);
798 (*diagnostic_starter (context)) (context, diagnostic);
799 pp_output_formatted_text (context->printer);
800 diagnostic_show_locus (context, diagnostic);
801 (*diagnostic_finalizer (context)) (context, diagnostic);
802 pp_destroy_prefix (context->printer);
803 pp_newline_and_flush (context->printer);
804 diagnostic_action_after_output (context, diagnostic);
805 diagnostic->message.format_spec = saved_format_spec;
806 diagnostic->x_data = NULL;
808 context->lock--;
810 return true;
813 /* Given a partial pathname as input, return another pathname that
814 shares no directory elements with the pathname of __FILE__. This
815 is used by fancy_abort() to print `Internal compiler error in expr.c'
816 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
818 const char *
819 trim_filename (const char *name)
821 static const char this_file[] = __FILE__;
822 const char *p = name, *q = this_file;
824 /* First skip any "../" in each filename. This allows us to give a proper
825 reference to a file in a subdirectory. */
826 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
827 p += 3;
829 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
830 q += 3;
832 /* Now skip any parts the two filenames have in common. */
833 while (*p == *q && *p != 0 && *q != 0)
834 p++, q++;
836 /* Now go backwards until the previous directory separator. */
837 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
838 p--;
840 return p;
843 /* Standard error reporting routines in increasing order of severity.
844 All of these take arguments like printf. */
846 /* Text to be emitted verbatim to the error message stream; this
847 produces no prefix and disables line-wrapping. Use rarely. */
848 void
849 verbatim (const char *gmsgid, ...)
851 text_info text;
852 va_list ap;
854 va_start (ap, gmsgid);
855 text.err_no = errno;
856 text.args_ptr = &ap;
857 text.format_spec = _(gmsgid);
858 text.locus = NULL;
859 text.x_data = NULL;
860 pp_format_verbatim (global_dc->printer, &text);
861 pp_newline_and_flush (global_dc->printer);
862 va_end (ap);
865 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
866 void
867 diagnostic_append_note (diagnostic_context *context,
868 location_t location,
869 const char * gmsgid, ...)
871 diagnostic_info diagnostic;
872 va_list ap;
873 const char *saved_prefix;
875 va_start (ap, gmsgid);
876 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
877 if (context->inhibit_notes_p)
879 va_end (ap);
880 return;
882 saved_prefix = pp_get_prefix (context->printer);
883 pp_set_prefix (context->printer,
884 diagnostic_build_prefix (context, &diagnostic));
885 pp_newline (context->printer);
886 pp_format (context->printer, &diagnostic.message);
887 pp_output_formatted_text (context->printer);
888 pp_destroy_prefix (context->printer);
889 pp_set_prefix (context->printer, saved_prefix);
890 diagnostic_show_locus (context, &diagnostic);
891 va_end (ap);
894 bool
895 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
896 const char *gmsgid, ...)
898 diagnostic_info diagnostic;
899 va_list ap;
900 bool ret;
902 va_start (ap, gmsgid);
903 if (kind == DK_PERMERROR)
905 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
906 permissive_error_kind (global_dc));
907 diagnostic.option_index = permissive_error_option (global_dc);
909 else {
910 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
911 if (kind == DK_WARNING || kind == DK_PEDWARN)
912 diagnostic.option_index = opt;
915 ret = report_diagnostic (&diagnostic);
916 va_end (ap);
917 return ret;
920 /* An informative note at LOCATION. Use this for additional details on an error
921 message. */
922 void
923 inform (location_t location, const char *gmsgid, ...)
925 diagnostic_info diagnostic;
926 va_list ap;
928 va_start (ap, gmsgid);
929 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
930 report_diagnostic (&diagnostic);
931 va_end (ap);
934 /* An informative note at LOCATION. Use this for additional details on an
935 error message. */
936 void
937 inform_n (location_t location, int n, const char *singular_gmsgid,
938 const char *plural_gmsgid, ...)
940 diagnostic_info diagnostic;
941 va_list ap;
943 va_start (ap, plural_gmsgid);
944 diagnostic_set_info_translated (&diagnostic,
945 ngettext (singular_gmsgid, plural_gmsgid, n),
946 &ap, location, DK_NOTE);
947 report_diagnostic (&diagnostic);
948 va_end (ap);
951 /* A warning at INPUT_LOCATION. Use this for code which is correct according
952 to the relevant language specification but is likely to be buggy anyway.
953 Returns true if the warning was printed, false if it was inhibited. */
954 bool
955 warning (int opt, const char *gmsgid, ...)
957 diagnostic_info diagnostic;
958 va_list ap;
959 bool ret;
961 va_start (ap, gmsgid);
962 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
963 diagnostic.option_index = opt;
965 ret = report_diagnostic (&diagnostic);
966 va_end (ap);
967 return ret;
970 /* A warning at LOCATION. Use this for code which is correct according to the
971 relevant language specification but is likely to be buggy anyway.
972 Returns true if the warning was printed, false if it was inhibited. */
974 bool
975 warning_at (location_t location, int opt, const char *gmsgid, ...)
977 diagnostic_info diagnostic;
978 va_list ap;
979 bool ret;
981 va_start (ap, gmsgid);
982 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
983 diagnostic.option_index = opt;
984 ret = report_diagnostic (&diagnostic);
985 va_end (ap);
986 return ret;
989 /* A "pedantic" warning at LOCATION: issues a warning unless
990 -pedantic-errors was given on the command line, in which case it
991 issues an error. Use this for diagnostics required by the relevant
992 language standard, if you have chosen not to make them errors.
994 Note that these diagnostics are issued independent of the setting
995 of the -Wpedantic command-line switch. To get a warning enabled
996 only with that switch, use either "if (pedantic) pedwarn
997 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
998 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1000 Returns true if the warning was printed, false if it was inhibited. */
1002 bool
1003 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1005 diagnostic_info diagnostic;
1006 va_list ap;
1007 bool ret;
1009 va_start (ap, gmsgid);
1010 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
1011 diagnostic.option_index = opt;
1012 ret = report_diagnostic (&diagnostic);
1013 va_end (ap);
1014 return ret;
1017 /* A "permissive" error at LOCATION: issues an error unless
1018 -fpermissive was given on the command line, in which case it issues
1019 a warning. Use this for things that really should be errors but we
1020 want to support legacy code.
1022 Returns true if the warning was printed, false if it was inhibited. */
1024 bool
1025 permerror (location_t location, const char *gmsgid, ...)
1027 diagnostic_info diagnostic;
1028 va_list ap;
1029 bool ret;
1031 va_start (ap, gmsgid);
1032 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1033 permissive_error_kind (global_dc));
1034 diagnostic.option_index = permissive_error_option (global_dc);
1035 ret = report_diagnostic (&diagnostic);
1036 va_end (ap);
1037 return ret;
1040 /* A hard error: the code is definitely ill-formed, and an object file
1041 will not be produced. */
1042 void
1043 error (const char *gmsgid, ...)
1045 diagnostic_info diagnostic;
1046 va_list ap;
1048 va_start (ap, gmsgid);
1049 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1050 report_diagnostic (&diagnostic);
1051 va_end (ap);
1054 /* A hard error: the code is definitely ill-formed, and an object file
1055 will not be produced. */
1056 void
1057 error_n (location_t location, int n, const char *singular_gmsgid,
1058 const char *plural_gmsgid, ...)
1060 diagnostic_info diagnostic;
1061 va_list ap;
1063 va_start (ap, plural_gmsgid);
1064 diagnostic_set_info_translated (&diagnostic,
1065 ngettext (singular_gmsgid, plural_gmsgid, n),
1066 &ap, location, DK_ERROR);
1067 report_diagnostic (&diagnostic);
1068 va_end (ap);
1071 /* Same as ebove, but use location LOC instead of input_location. */
1072 void
1073 error_at (location_t loc, const char *gmsgid, ...)
1075 diagnostic_info diagnostic;
1076 va_list ap;
1078 va_start (ap, gmsgid);
1079 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1080 report_diagnostic (&diagnostic);
1081 va_end (ap);
1084 /* "Sorry, not implemented." Use for a language feature which is
1085 required by the relevant specification but not implemented by GCC.
1086 An object file will not be produced. */
1087 void
1088 sorry (const char *gmsgid, ...)
1090 diagnostic_info diagnostic;
1091 va_list ap;
1093 va_start (ap, gmsgid);
1094 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1095 report_diagnostic (&diagnostic);
1096 va_end (ap);
1099 /* Return true if an error or a "sorry" has been seen. Various
1100 processing is disabled after errors. */
1101 bool
1102 seen_error (void)
1104 return errorcount || sorrycount;
1107 /* An error which is severe enough that we make no attempt to
1108 continue. Do not use this for internal consistency checks; that's
1109 internal_error. Use of this function should be rare. */
1110 void
1111 fatal_error (const char *gmsgid, ...)
1113 diagnostic_info diagnostic;
1114 va_list ap;
1116 va_start (ap, gmsgid);
1117 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
1118 report_diagnostic (&diagnostic);
1119 va_end (ap);
1121 gcc_unreachable ();
1124 /* An internal consistency check has failed. We make no attempt to
1125 continue. Note that unless there is debugging value to be had from
1126 a more specific message, or some other good reason, you should use
1127 abort () instead of calling this function directly. */
1128 void
1129 internal_error (const char *gmsgid, ...)
1131 diagnostic_info diagnostic;
1132 va_list ap;
1134 va_start (ap, gmsgid);
1135 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1136 report_diagnostic (&diagnostic);
1137 va_end (ap);
1139 gcc_unreachable ();
1142 /* Special case error functions. Most are implemented in terms of the
1143 above, or should be. */
1145 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1146 runs its second argument through gettext. */
1147 void
1148 fnotice (FILE *file, const char *cmsgid, ...)
1150 va_list ap;
1152 va_start (ap, cmsgid);
1153 vfprintf (file, _(cmsgid), ap);
1154 va_end (ap);
1157 /* Inform the user that an error occurred while trying to report some
1158 other error. This indicates catastrophic internal inconsistencies,
1159 so give up now. But do try to flush out the previous error.
1160 This mustn't use internal_error, that will cause infinite recursion. */
1162 static void
1163 error_recursion (diagnostic_context *context)
1165 diagnostic_info diagnostic;
1167 if (context->lock < 3)
1168 pp_newline_and_flush (context->printer);
1170 fnotice (stderr,
1171 "Internal compiler error: Error reporting routines re-entered.\n");
1173 /* Call diagnostic_action_after_output to get the "please submit a bug
1174 report" message. It only looks at the kind field of diagnostic_info. */
1175 diagnostic.kind = DK_ICE;
1176 diagnostic_action_after_output (context, &diagnostic);
1178 /* Do not use gcc_unreachable here; that goes through internal_error
1179 and therefore would cause infinite recursion. */
1180 real_abort ();
1183 /* Report an internal compiler error in a friendly manner. This is
1184 the function that gets called upon use of abort() in the source
1185 code generally, thanks to a special macro. */
1187 void
1188 fancy_abort (const char *file, int line, const char *function)
1190 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1193 /* Really call the system 'abort'. This has to go right at the end of
1194 this file, so that there are no functions after it that call abort
1195 and get the system abort instead of our macro. */
1196 #undef abort
1197 static void
1198 real_abort (void)
1200 abort ();