2014-09-18 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / diagnostic.c
blob73666d62fc7bd074431303f10388c21a6f74e9ff
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 void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
46 static void diagnostic_action_after_output (diagnostic_context *,
47 diagnostic_info *);
48 static void real_abort (void) ATTRIBUTE_NORETURN;
50 /* Name of program invoked, sans directories. */
52 const char *progname;
54 /* A diagnostic_context surrogate for stderr. */
55 static diagnostic_context global_diagnostic_context;
56 diagnostic_context *global_dc = &global_diagnostic_context;
58 /* Return a malloc'd string containing MSG formatted a la printf. The
59 caller is responsible for freeing the memory. */
60 char *
61 build_message_string (const char *msg, ...)
63 char *str;
64 va_list ap;
66 va_start (ap, msg);
67 vasprintf (&str, msg, ap);
68 va_end (ap);
70 return str;
73 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
74 char *
75 file_name_as_prefix (diagnostic_context *context, const char *f)
77 const char *locus_cs
78 = colorize_start (pp_show_color (context->printer), "locus");
79 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
80 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
85 /* Return the value of the getenv("COLUMNS") as an integer. If the
86 value is not set to a positive integer, then return INT_MAX. */
87 static int
88 getenv_columns (void)
90 const char * s = getenv ("COLUMNS");
91 if (s != NULL) {
92 int n = atoi (s);
93 if (n > 0)
94 return n;
96 return INT_MAX;
99 /* Set caret_max_width to value. */
100 void
101 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
103 /* One minus to account for the leading empty space. */
104 value = value ? value - 1
105 : (isatty (fileno (pp_buffer (context->printer)->stream))
106 ? getenv_columns () - 1: INT_MAX);
108 if (value <= 0)
109 value = INT_MAX;
111 context->caret_max_width = value;
114 /* Initialize the diagnostic message outputting machinery. */
115 void
116 diagnostic_initialize (diagnostic_context *context, int n_opts)
118 int i;
120 /* Allocate a basic pretty-printer. Clients will replace this a
121 much more elaborated pretty-printer if they wish. */
122 context->printer = XNEW (pretty_printer);
123 new (context->printer) pretty_printer ();
125 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
126 context->some_warnings_are_errors = false;
127 context->warning_as_error_requested = false;
128 context->n_opts = n_opts;
129 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
130 for (i = 0; i < n_opts; i++)
131 context->classify_diagnostic[i] = DK_UNSPECIFIED;
132 context->show_caret = false;
133 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
134 context->caret_char = '^';
135 context->show_option_requested = false;
136 context->abort_on_error = false;
137 context->show_column = false;
138 context->pedantic_errors = false;
139 context->permissive = false;
140 context->opt_permissive = 0;
141 context->fatal_errors = false;
142 context->dc_inhibit_warnings = false;
143 context->dc_warn_system_headers = false;
144 context->max_errors = 0;
145 context->internal_error = NULL;
146 diagnostic_starter (context) = default_diagnostic_starter;
147 diagnostic_finalizer (context) = default_diagnostic_finalizer;
148 context->option_enabled = NULL;
149 context->option_state = NULL;
150 context->option_name = NULL;
151 context->last_location = UNKNOWN_LOCATION;
152 context->last_module = 0;
153 context->x_data = NULL;
154 context->lock = 0;
155 context->inhibit_notes_p = false;
158 /* Do any cleaning up required after the last diagnostic is emitted. */
160 void
161 diagnostic_finish (diagnostic_context *context)
163 /* Some of the errors may actually have been warnings. */
164 if (context->some_warnings_are_errors)
166 /* -Werror was given. */
167 if (context->warning_as_error_requested)
168 pp_verbatim (context->printer,
169 _("%s: all warnings being treated as errors"),
170 progname);
171 /* At least one -Werror= was given. */
172 else
173 pp_verbatim (context->printer,
174 _("%s: some warnings being treated as errors"),
175 progname);
176 pp_newline_and_flush (context->printer);
179 diagnostic_file_cache_fini ();
182 /* Initialize DIAGNOSTIC, where the message MSG has already been
183 translated. */
184 void
185 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
186 va_list *args, location_t location,
187 diagnostic_t kind)
189 diagnostic->message.err_no = errno;
190 diagnostic->message.args_ptr = args;
191 diagnostic->message.format_spec = msg;
192 diagnostic->location = location;
193 diagnostic->override_column = 0;
194 diagnostic->kind = kind;
195 diagnostic->option_index = 0;
198 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
199 translated. */
200 void
201 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
202 va_list *args, location_t location,
203 diagnostic_t kind)
205 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
208 /* Return a malloc'd string describing a location. The caller is
209 responsible for freeing the memory. */
210 char *
211 diagnostic_build_prefix (diagnostic_context *context,
212 const diagnostic_info *diagnostic)
214 static const char *const diagnostic_kind_text[] = {
215 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
216 #include "diagnostic.def"
217 #undef DEFINE_DIAGNOSTIC_KIND
218 "must-not-happen"
220 static const char *const diagnostic_kind_color[] = {
221 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
222 #include "diagnostic.def"
223 #undef DEFINE_DIAGNOSTIC_KIND
224 NULL
226 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
227 const char *text_cs = "", *text_ce = "";
228 const char *locus_cs, *locus_ce;
229 pretty_printer *pp = context->printer;
231 if (diagnostic_kind_color[diagnostic->kind])
233 text_cs = colorize_start (pp_show_color (pp),
234 diagnostic_kind_color[diagnostic->kind]);
235 text_ce = colorize_stop (pp_show_color (pp));
237 locus_cs = colorize_start (pp_show_color (pp), "locus");
238 locus_ce = colorize_stop (pp_show_color (pp));
240 expanded_location s = expand_location_to_spelling_point (diagnostic->location);
241 if (diagnostic->override_column)
242 s.column = diagnostic->override_column;
243 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
245 return
246 (s.file == NULL
247 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce,
248 text_cs, text, text_ce)
249 : !strcmp (s.file, N_("<built-in>"))
250 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, s.file, locus_ce,
251 text_cs, text, text_ce)
252 : context->show_column
253 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
254 s.column, locus_ce, text_cs, text, text_ce)
255 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line, locus_ce,
256 text_cs, text, text_ce));
259 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
260 MAX_WIDTH by some margin, then adjust the start of the line such
261 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
262 margin is either 10 characters or the difference between the column
263 and the length of the line, whatever is smaller. The length of
264 LINE is given by LINE_WIDTH. */
265 static const char *
266 adjust_line (const char *line, int line_width,
267 int max_width, int *column_p)
269 int right_margin = 10;
270 int column = *column_p;
272 gcc_checking_assert (line_width >= column);
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 diagnostic, 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 || s.column > line_width)
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, context->caret_char,
333 caret_ce);
334 pp_string (context->printer, buffer);
335 pp_set_prefix (context->printer, saved_prefix);
336 pp_needs_newline (context->printer) = true;
339 /* Functions at which to stop the backtrace print. It's not
340 particularly helpful to print the callers of these functions. */
342 static const char * const bt_stop[] =
344 "main",
345 "toplev_main",
346 "execute_one_pass",
347 "compile_file",
350 /* A callback function passed to the backtrace_full function. */
352 static int
353 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
354 const char *function)
356 int *pcount = (int *) data;
358 /* If we don't have any useful information, don't print
359 anything. */
360 if (filename == NULL && function == NULL)
361 return 0;
363 /* Skip functions in diagnostic.c. */
364 if (*pcount == 0
365 && filename != NULL
366 && strcmp (lbasename (filename), "diagnostic.c") == 0)
367 return 0;
369 /* Print up to 20 functions. We could make this a --param, but
370 since this is only for debugging just use a constant for now. */
371 if (*pcount >= 20)
373 /* Returning a non-zero value stops the backtrace. */
374 return 1;
376 ++*pcount;
378 char *alc = NULL;
379 if (function != NULL)
381 char *str = cplus_demangle_v3 (function,
382 (DMGL_VERBOSE | DMGL_ANSI
383 | DMGL_GNU_V3 | DMGL_PARAMS));
384 if (str != NULL)
386 alc = str;
387 function = str;
390 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
392 size_t len = strlen (bt_stop[i]);
393 if (strncmp (function, bt_stop[i], len) == 0
394 && (function[len] == '\0' || function[len] == '('))
396 if (alc != NULL)
397 free (alc);
398 /* Returning a non-zero value stops the backtrace. */
399 return 1;
404 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
405 (unsigned long) pc,
406 function == NULL ? "???" : function,
407 filename == NULL ? "???" : filename,
408 lineno);
410 if (alc != NULL)
411 free (alc);
413 return 0;
416 /* A callback function passed to the backtrace_full function. This is
417 called if backtrace_full has an error. */
419 static void
420 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
422 if (errnum < 0)
424 /* This means that no debug info was available. Just quietly
425 skip printing backtrace info. */
426 return;
428 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
429 errnum == 0 ? "" : xstrerror (errnum));
432 /* Take any action which is expected to happen after the diagnostic
433 is written out. This function does not always return. */
434 static void
435 diagnostic_action_after_output (diagnostic_context *context,
436 diagnostic_info *diagnostic)
438 switch (diagnostic->kind)
440 case DK_DEBUG:
441 case DK_NOTE:
442 case DK_ANACHRONISM:
443 case DK_WARNING:
444 break;
446 case DK_ERROR:
447 case DK_SORRY:
448 if (context->abort_on_error)
449 real_abort ();
450 if (context->fatal_errors)
452 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
453 diagnostic_finish (context);
454 exit (FATAL_EXIT_CODE);
456 if (context->max_errors != 0
457 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
458 + diagnostic_kind_count (context, DK_SORRY))
459 >= context->max_errors))
461 fnotice (stderr,
462 "compilation terminated due to -fmax-errors=%u.\n",
463 context->max_errors);
464 diagnostic_finish (context);
465 exit (FATAL_EXIT_CODE);
467 break;
469 case DK_ICE:
471 struct backtrace_state *state =
472 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 struct line_map *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);
555 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
556 diagnostic));
559 void
560 default_diagnostic_finalizer (diagnostic_context *context,
561 diagnostic_info *diagnostic)
563 diagnostic_show_locus (context, diagnostic);
564 pp_destroy_prefix (context->printer);
565 pp_newline_and_flush (context->printer);
568 /* Interface to specify diagnostic kind overrides. Returns the
569 previous setting, or DK_UNSPECIFIED if the parameters are out of
570 range. If OPTION_INDEX is zero, the new setting is for all the
571 diagnostics. */
572 diagnostic_t
573 diagnostic_classify_diagnostic (diagnostic_context *context,
574 int option_index,
575 diagnostic_t new_kind,
576 location_t where)
578 diagnostic_t old_kind;
580 if (option_index < 0
581 || option_index >= context->n_opts
582 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
583 return DK_UNSPECIFIED;
585 old_kind = context->classify_diagnostic[option_index];
587 /* Handle pragmas separately, since we need to keep track of *where*
588 the pragmas were. */
589 if (where != UNKNOWN_LOCATION)
591 int i;
593 /* Record the command-line status, so we can reset it back on DK_POP. */
594 if (old_kind == DK_UNSPECIFIED)
596 old_kind = context->option_enabled (option_index,
597 context->option_state)
598 ? DK_WARNING : DK_IGNORED;
599 context->classify_diagnostic[option_index] = old_kind;
602 for (i = context->n_classification_history - 1; i >= 0; i --)
603 if (context->classification_history[i].option == option_index)
605 old_kind = context->classification_history[i].kind;
606 break;
609 i = context->n_classification_history;
610 context->classification_history =
611 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
612 * sizeof (diagnostic_classification_change_t));
613 context->classification_history[i].location = where;
614 context->classification_history[i].option = option_index;
615 context->classification_history[i].kind = new_kind;
616 context->n_classification_history ++;
618 else
619 context->classify_diagnostic[option_index] = new_kind;
621 return old_kind;
624 /* Save all diagnostic classifications in a stack. */
625 void
626 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
628 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
629 context->push_list[context->n_push ++] = context->n_classification_history;
632 /* Restore the topmost classification set off the stack. If the stack
633 is empty, revert to the state based on command line parameters. */
634 void
635 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
637 int jump_to;
638 int i;
640 if (context->n_push)
641 jump_to = context->push_list [-- context->n_push];
642 else
643 jump_to = 0;
645 i = context->n_classification_history;
646 context->classification_history =
647 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
648 * sizeof (diagnostic_classification_change_t));
649 context->classification_history[i].location = where;
650 context->classification_history[i].option = jump_to;
651 context->classification_history[i].kind = DK_POP;
652 context->n_classification_history ++;
655 /* Report a diagnostic message (an error or a warning) as specified by
656 DC. This function is *the* subroutine in terms of which front-ends
657 should implement their specific diagnostic handling modules. The
658 front-end independent format specifiers are exactly those described
659 in the documentation of output_format.
660 Return true if a diagnostic was printed, false otherwise. */
662 bool
663 diagnostic_report_diagnostic (diagnostic_context *context,
664 diagnostic_info *diagnostic)
666 location_t location = diagnostic->location;
667 diagnostic_t orig_diag_kind = diagnostic->kind;
668 const char *saved_format_spec;
670 /* Give preference to being able to inhibit warnings, before they
671 get reclassified to something else. */
672 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
673 && !diagnostic_report_warnings_p (context, location))
674 return false;
676 if (diagnostic->kind == DK_PEDWARN)
678 diagnostic->kind = pedantic_warning_kind (context);
679 /* We do this to avoid giving the message for -pedantic-errors. */
680 orig_diag_kind = diagnostic->kind;
683 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
684 return false;
686 if (context->lock > 0)
688 /* If we're reporting an ICE in the middle of some other error,
689 try to flush out the previous error, then let this one
690 through. Don't do this more than once. */
691 if (diagnostic->kind == DK_ICE && context->lock == 1)
692 pp_newline_and_flush (context->printer);
693 else
694 error_recursion (context);
697 /* If the user requested that warnings be treated as errors, so be
698 it. Note that we do this before the next block so that
699 individual warnings can be overridden back to warnings with
700 -Wno-error=*. */
701 if (context->warning_as_error_requested
702 && diagnostic->kind == DK_WARNING)
704 diagnostic->kind = DK_ERROR;
707 if (diagnostic->option_index
708 && diagnostic->option_index != permissive_error_option (context))
710 diagnostic_t diag_class = DK_UNSPECIFIED;
712 /* This tests if the user provided the appropriate -Wfoo or
713 -Wno-foo option. */
714 if (! context->option_enabled (diagnostic->option_index,
715 context->option_state))
716 return false;
718 /* This tests for #pragma diagnostic changes. */
719 if (context->n_classification_history > 0)
721 /* FIXME: Stupid search. Optimize later. */
722 for (int i = context->n_classification_history - 1; i >= 0; i --)
724 if (linemap_location_before_p
725 (line_table,
726 context->classification_history[i].location,
727 location))
729 if (context->classification_history[i].kind == (int) DK_POP)
731 i = context->classification_history[i].option;
732 continue;
734 int option = context->classification_history[i].option;
735 /* The option 0 is for all the diagnostics. */
736 if (option == 0 || option == diagnostic->option_index)
738 diag_class = context->classification_history[i].kind;
739 if (diag_class != DK_UNSPECIFIED)
740 diagnostic->kind = diag_class;
741 break;
746 /* This tests if the user provided the appropriate -Werror=foo
747 option. */
748 if (diag_class == DK_UNSPECIFIED
749 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
751 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
753 /* This allows for future extensions, like temporarily disabling
754 warnings for ranges of source code. */
755 if (diagnostic->kind == DK_IGNORED)
756 return false;
759 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
760 context->some_warnings_are_errors = true;
762 context->lock++;
764 if (diagnostic->kind == DK_ICE)
766 #ifndef ENABLE_CHECKING
767 /* When not checking, ICEs are converted to fatal errors when an
768 error has already occurred. This is counteracted by
769 abort_on_error. */
770 if ((diagnostic_kind_count (context, DK_ERROR) > 0
771 || diagnostic_kind_count (context, DK_SORRY) > 0)
772 && !context->abort_on_error)
774 expanded_location s = expand_location (diagnostic->location);
775 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
776 s.file, s.line);
777 exit (ICE_EXIT_CODE);
779 #endif
780 if (context->internal_error)
781 (*context->internal_error) (context,
782 diagnostic->message.format_spec,
783 diagnostic->message.args_ptr);
785 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
786 ++diagnostic_kind_count (context, DK_WERROR);
787 else
788 ++diagnostic_kind_count (context, diagnostic->kind);
790 saved_format_spec = diagnostic->message.format_spec;
791 if (context->show_option_requested)
793 char *option_text;
795 option_text = context->option_name (context, diagnostic->option_index,
796 orig_diag_kind, diagnostic->kind);
798 if (option_text)
800 diagnostic->message.format_spec
801 = ACONCAT ((diagnostic->message.format_spec,
802 " ",
803 "[", option_text, "]",
804 NULL));
805 free (option_text);
808 diagnostic->message.locus = &diagnostic->location;
809 diagnostic->message.x_data = &diagnostic->x_data;
810 diagnostic->x_data = NULL;
811 pp_format (context->printer, &diagnostic->message);
812 (*diagnostic_starter (context)) (context, diagnostic);
813 pp_output_formatted_text (context->printer);
814 (*diagnostic_finalizer (context)) (context, diagnostic);
815 diagnostic_action_after_output (context, diagnostic);
816 diagnostic->message.format_spec = saved_format_spec;
817 diagnostic->x_data = NULL;
819 context->lock--;
821 return true;
824 /* Given a partial pathname as input, return another pathname that
825 shares no directory elements with the pathname of __FILE__. This
826 is used by fancy_abort() to print `Internal compiler error in expr.c'
827 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
829 const char *
830 trim_filename (const char *name)
832 static const char this_file[] = __FILE__;
833 const char *p = name, *q = this_file;
835 /* First skip any "../" in each filename. This allows us to give a proper
836 reference to a file in a subdirectory. */
837 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
838 p += 3;
840 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
841 q += 3;
843 /* Now skip any parts the two filenames have in common. */
844 while (*p == *q && *p != 0 && *q != 0)
845 p++, q++;
847 /* Now go backwards until the previous directory separator. */
848 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
849 p--;
851 return p;
854 /* Standard error reporting routines in increasing order of severity.
855 All of these take arguments like printf. */
857 /* Text to be emitted verbatim to the error message stream; this
858 produces no prefix and disables line-wrapping. Use rarely. */
859 void
860 verbatim (const char *gmsgid, ...)
862 text_info text;
863 va_list ap;
865 va_start (ap, gmsgid);
866 text.err_no = errno;
867 text.args_ptr = &ap;
868 text.format_spec = _(gmsgid);
869 text.locus = NULL;
870 text.x_data = NULL;
871 pp_format_verbatim (global_dc->printer, &text);
872 pp_newline_and_flush (global_dc->printer);
873 va_end (ap);
876 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
877 void
878 diagnostic_append_note (diagnostic_context *context,
879 location_t location,
880 const char * gmsgid, ...)
882 diagnostic_info diagnostic;
883 va_list ap;
884 const char *saved_prefix;
886 va_start (ap, gmsgid);
887 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
888 if (context->inhibit_notes_p)
890 va_end (ap);
891 return;
893 saved_prefix = pp_get_prefix (context->printer);
894 pp_set_prefix (context->printer,
895 diagnostic_build_prefix (context, &diagnostic));
896 pp_newline (context->printer);
897 pp_format (context->printer, &diagnostic.message);
898 pp_output_formatted_text (context->printer);
899 pp_destroy_prefix (context->printer);
900 pp_set_prefix (context->printer, saved_prefix);
901 diagnostic_show_locus (context, &diagnostic);
902 va_end (ap);
905 bool
906 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
907 const char *gmsgid, ...)
909 diagnostic_info diagnostic;
910 va_list ap;
911 bool ret;
913 va_start (ap, gmsgid);
914 if (kind == DK_PERMERROR)
916 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
917 permissive_error_kind (global_dc));
918 diagnostic.option_index = permissive_error_option (global_dc);
920 else {
921 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
922 if (kind == DK_WARNING || kind == DK_PEDWARN)
923 diagnostic.option_index = opt;
926 ret = report_diagnostic (&diagnostic);
927 va_end (ap);
928 return ret;
931 /* An informative note at LOCATION. Use this for additional details on an error
932 message. */
933 void
934 inform (location_t location, const char *gmsgid, ...)
936 diagnostic_info diagnostic;
937 va_list ap;
939 va_start (ap, gmsgid);
940 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
941 report_diagnostic (&diagnostic);
942 va_end (ap);
945 /* An informative note at LOCATION. Use this for additional details on an
946 error message. */
947 void
948 inform_n (location_t location, int n, const char *singular_gmsgid,
949 const char *plural_gmsgid, ...)
951 diagnostic_info diagnostic;
952 va_list ap;
954 va_start (ap, plural_gmsgid);
955 diagnostic_set_info_translated (&diagnostic,
956 ngettext (singular_gmsgid, plural_gmsgid, n),
957 &ap, location, DK_NOTE);
958 report_diagnostic (&diagnostic);
959 va_end (ap);
962 /* A warning at INPUT_LOCATION. Use this for code which is correct according
963 to the relevant language specification but is likely to be buggy anyway.
964 Returns true if the warning was printed, false if it was inhibited. */
965 bool
966 warning (int opt, const char *gmsgid, ...)
968 diagnostic_info diagnostic;
969 va_list ap;
970 bool ret;
972 va_start (ap, gmsgid);
973 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
974 diagnostic.option_index = opt;
976 ret = report_diagnostic (&diagnostic);
977 va_end (ap);
978 return ret;
981 /* A warning at LOCATION. Use this for code which is correct according to the
982 relevant language specification but is likely to be buggy anyway.
983 Returns true if the warning was printed, false if it was inhibited. */
985 bool
986 warning_at (location_t location, int opt, const char *gmsgid, ...)
988 diagnostic_info diagnostic;
989 va_list ap;
990 bool ret;
992 va_start (ap, gmsgid);
993 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
994 diagnostic.option_index = opt;
995 ret = report_diagnostic (&diagnostic);
996 va_end (ap);
997 return ret;
1000 /* A "pedantic" warning at LOCATION: issues a warning unless
1001 -pedantic-errors was given on the command line, in which case it
1002 issues an error. Use this for diagnostics required by the relevant
1003 language standard, if you have chosen not to make them errors.
1005 Note that these diagnostics are issued independent of the setting
1006 of the -Wpedantic command-line switch. To get a warning enabled
1007 only with that switch, use either "if (pedantic) pedwarn
1008 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1009 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1011 Returns true if the warning was printed, false if it was inhibited. */
1013 bool
1014 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1016 diagnostic_info diagnostic;
1017 va_list ap;
1018 bool ret;
1020 va_start (ap, gmsgid);
1021 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
1022 diagnostic.option_index = opt;
1023 ret = report_diagnostic (&diagnostic);
1024 va_end (ap);
1025 return ret;
1028 /* A "permissive" error at LOCATION: issues an error unless
1029 -fpermissive was given on the command line, in which case it issues
1030 a warning. Use this for things that really should be errors but we
1031 want to support legacy code.
1033 Returns true if the warning was printed, false if it was inhibited. */
1035 bool
1036 permerror (location_t location, const char *gmsgid, ...)
1038 diagnostic_info diagnostic;
1039 va_list ap;
1040 bool ret;
1042 va_start (ap, gmsgid);
1043 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1044 permissive_error_kind (global_dc));
1045 diagnostic.option_index = permissive_error_option (global_dc);
1046 ret = report_diagnostic (&diagnostic);
1047 va_end (ap);
1048 return ret;
1051 /* A hard error: the code is definitely ill-formed, and an object file
1052 will not be produced. */
1053 void
1054 error (const char *gmsgid, ...)
1056 diagnostic_info diagnostic;
1057 va_list ap;
1059 va_start (ap, gmsgid);
1060 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1061 report_diagnostic (&diagnostic);
1062 va_end (ap);
1065 /* A hard error: the code is definitely ill-formed, and an object file
1066 will not be produced. */
1067 void
1068 error_n (location_t location, int n, const char *singular_gmsgid,
1069 const char *plural_gmsgid, ...)
1071 diagnostic_info diagnostic;
1072 va_list ap;
1074 va_start (ap, plural_gmsgid);
1075 diagnostic_set_info_translated (&diagnostic,
1076 ngettext (singular_gmsgid, plural_gmsgid, n),
1077 &ap, location, DK_ERROR);
1078 report_diagnostic (&diagnostic);
1079 va_end (ap);
1082 /* Same as ebove, but use location LOC instead of input_location. */
1083 void
1084 error_at (location_t loc, const char *gmsgid, ...)
1086 diagnostic_info diagnostic;
1087 va_list ap;
1089 va_start (ap, gmsgid);
1090 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1091 report_diagnostic (&diagnostic);
1092 va_end (ap);
1095 /* "Sorry, not implemented." Use for a language feature which is
1096 required by the relevant specification but not implemented by GCC.
1097 An object file will not be produced. */
1098 void
1099 sorry (const char *gmsgid, ...)
1101 diagnostic_info diagnostic;
1102 va_list ap;
1104 va_start (ap, gmsgid);
1105 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1106 report_diagnostic (&diagnostic);
1107 va_end (ap);
1110 /* Return true if an error or a "sorry" has been seen. Various
1111 processing is disabled after errors. */
1112 bool
1113 seen_error (void)
1115 return errorcount || sorrycount;
1118 /* An error which is severe enough that we make no attempt to
1119 continue. Do not use this for internal consistency checks; that's
1120 internal_error. Use of this function should be rare. */
1121 void
1122 fatal_error (const char *gmsgid, ...)
1124 diagnostic_info diagnostic;
1125 va_list ap;
1127 va_start (ap, gmsgid);
1128 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
1129 report_diagnostic (&diagnostic);
1130 va_end (ap);
1132 gcc_unreachable ();
1135 /* An internal consistency check has failed. We make no attempt to
1136 continue. Note that unless there is debugging value to be had from
1137 a more specific message, or some other good reason, you should use
1138 abort () instead of calling this function directly. */
1139 void
1140 internal_error (const char *gmsgid, ...)
1142 diagnostic_info diagnostic;
1143 va_list ap;
1145 va_start (ap, gmsgid);
1146 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1147 report_diagnostic (&diagnostic);
1148 va_end (ap);
1150 gcc_unreachable ();
1153 /* Special case error functions. Most are implemented in terms of the
1154 above, or should be. */
1156 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1157 runs its second argument through gettext. */
1158 void
1159 fnotice (FILE *file, const char *cmsgid, ...)
1161 va_list ap;
1163 va_start (ap, cmsgid);
1164 vfprintf (file, _(cmsgid), ap);
1165 va_end (ap);
1168 /* Inform the user that an error occurred while trying to report some
1169 other error. This indicates catastrophic internal inconsistencies,
1170 so give up now. But do try to flush out the previous error.
1171 This mustn't use internal_error, that will cause infinite recursion. */
1173 static void
1174 error_recursion (diagnostic_context *context)
1176 diagnostic_info diagnostic;
1178 if (context->lock < 3)
1179 pp_newline_and_flush (context->printer);
1181 fnotice (stderr,
1182 "Internal compiler error: Error reporting routines re-entered.\n");
1184 /* Call diagnostic_action_after_output to get the "please submit a bug
1185 report" message. It only looks at the kind field of diagnostic_info. */
1186 diagnostic.kind = DK_ICE;
1187 diagnostic_action_after_output (context, &diagnostic);
1189 /* Do not use gcc_unreachable here; that goes through internal_error
1190 and therefore would cause infinite recursion. */
1191 real_abort ();
1194 /* Report an internal compiler error in a friendly manner. This is
1195 the function that gets called upon use of abort() in the source
1196 code generally, thanks to a special macro. */
1198 void
1199 fancy_abort (const char *file, int line, const char *function)
1201 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1204 /* Really call the system 'abort'. This has to go right at the end of
1205 this file, so that there are no functions after it that call abort
1206 and get the system abort instead of our macro. */
1207 #undef abort
1208 static void
1209 real_abort (void)
1211 abort ();