re PR inline-asm/61692 (ICE in extract_insn in recog.c for asm with many parameters)
[official-gcc.git] / gcc / diagnostic.c
blob28ef81c5dad4071cf3dfd20e163f2f83530116b2
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 /* Maybe initialize the color support. We require clients to do this
159 explicitly, since most clients don't want color. When called
160 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
162 void
163 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
165 /* value == -1 is the default value. */
166 if (value < 0)
168 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
169 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
170 otherwise default to -fdiagnostics-color=never, for other
171 values default to that
172 -fdiagnostics-color={never,auto,always}. */
173 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
175 if (!getenv ("GCC_COLORS"))
176 return;
177 value = DIAGNOSTICS_COLOR_AUTO;
179 else
180 value = DIAGNOSTICS_COLOR_DEFAULT;
182 pp_show_color (context->printer)
183 = colorize_init ((diagnostic_color_rule_t) value);
186 /* Do any cleaning up required after the last diagnostic is emitted. */
188 void
189 diagnostic_finish (diagnostic_context *context)
191 /* Some of the errors may actually have been warnings. */
192 if (context->some_warnings_are_errors)
194 /* -Werror was given. */
195 if (context->warning_as_error_requested)
196 pp_verbatim (context->printer,
197 _("%s: all warnings being treated as errors"),
198 progname);
199 /* At least one -Werror= was given. */
200 else
201 pp_verbatim (context->printer,
202 _("%s: some warnings being treated as errors"),
203 progname);
204 pp_newline_and_flush (context->printer);
207 diagnostic_file_cache_fini ();
209 XDELETEVEC (context->classify_diagnostic);
210 context->classify_diagnostic = NULL;
212 /* diagnostic_initialize allocates context->printer using XNEW
213 and placement-new. */
214 context->printer->~pretty_printer ();
215 XDELETE (context->printer);
216 context->printer = NULL;
219 /* Initialize DIAGNOSTIC, where the message MSG has already been
220 translated. */
221 void
222 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
223 va_list *args, location_t location,
224 diagnostic_t kind)
226 diagnostic->message.err_no = errno;
227 diagnostic->message.args_ptr = args;
228 diagnostic->message.format_spec = msg;
229 diagnostic->location = location;
230 diagnostic->override_column = 0;
231 diagnostic->kind = kind;
232 diagnostic->option_index = 0;
235 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
236 translated. */
237 void
238 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
239 va_list *args, location_t location,
240 diagnostic_t kind)
242 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
245 /* Return a malloc'd string describing a location. The caller is
246 responsible for freeing the memory. */
247 char *
248 diagnostic_build_prefix (diagnostic_context *context,
249 const diagnostic_info *diagnostic)
251 static const char *const diagnostic_kind_text[] = {
252 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
253 #include "diagnostic.def"
254 #undef DEFINE_DIAGNOSTIC_KIND
255 "must-not-happen"
257 static const char *const diagnostic_kind_color[] = {
258 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
259 #include "diagnostic.def"
260 #undef DEFINE_DIAGNOSTIC_KIND
261 NULL
263 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
265 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
266 const char *text_cs = "", *text_ce = "";
267 const char *locus_cs, *locus_ce;
268 pretty_printer *pp = context->printer;
270 if (diagnostic_kind_color[diagnostic->kind])
272 text_cs = colorize_start (pp_show_color (pp),
273 diagnostic_kind_color[diagnostic->kind]);
274 text_ce = colorize_stop (pp_show_color (pp));
276 locus_cs = colorize_start (pp_show_color (pp), "locus");
277 locus_ce = colorize_stop (pp_show_color (pp));
279 expanded_location s = diagnostic_expand_location (diagnostic);
280 return
281 (s.file == NULL
282 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce,
283 text_cs, text, text_ce)
284 : !strcmp (s.file, N_("<built-in>"))
285 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, s.file, locus_ce,
286 text_cs, text, text_ce)
287 : context->show_column
288 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
289 s.column, locus_ce, text_cs, text, text_ce)
290 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line,
291 locus_ce, text_cs, text, text_ce));
294 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
295 MAX_WIDTH by some margin, then adjust the start of the line such
296 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
297 margin is either 10 characters or the difference between the column
298 and the length of the line, whatever is smaller. The length of
299 LINE is given by LINE_WIDTH. */
300 static const char *
301 adjust_line (const char *line, int line_width,
302 int max_width, int *column_p)
304 int right_margin = 10;
305 int column = *column_p;
307 gcc_checking_assert (line_width >= column);
308 right_margin = MIN (line_width - column, right_margin);
309 right_margin = max_width - right_margin;
310 if (line_width >= max_width && column > right_margin)
312 line += column - right_margin;
313 *column_p = right_margin;
315 return line;
318 /* Print the physical source line corresponding to the location of
319 this diagnostic, and a caret indicating the precise column. */
320 void
321 diagnostic_show_locus (diagnostic_context * context,
322 const diagnostic_info *diagnostic)
324 const char *line;
325 int line_width;
326 char *buffer;
327 expanded_location s;
328 int max_width;
329 const char *saved_prefix;
330 const char *caret_cs, *caret_ce;
332 if (!context->show_caret
333 || diagnostic->location <= BUILTINS_LOCATION
334 || diagnostic->location == context->last_location)
335 return;
337 context->last_location = diagnostic->location;
338 s = diagnostic_expand_location (diagnostic);
339 line = location_get_source_line (s, &line_width);
340 if (line == NULL || s.column > line_width)
341 return;
343 max_width = context->caret_max_width;
344 line = adjust_line (line, line_width, max_width, &(s.column));
346 pp_newline (context->printer);
347 saved_prefix = pp_get_prefix (context->printer);
348 pp_set_prefix (context->printer, NULL);
349 pp_space (context->printer);
350 while (max_width > 0 && line_width > 0)
352 char c = *line == '\t' ? ' ' : *line;
353 if (c == '\0')
354 c = ' ';
355 pp_character (context->printer, c);
356 max_width--;
357 line_width--;
358 line++;
360 pp_newline (context->printer);
361 caret_cs = colorize_start (pp_show_color (context->printer), "caret");
362 caret_ce = colorize_stop (pp_show_color (context->printer));
364 /* pp_printf does not implement %*c. */
365 size_t len = s.column + 3 + strlen (caret_cs) + strlen (caret_ce);
366 buffer = XALLOCAVEC (char, len);
367 snprintf (buffer, len, "%s %*c%s", caret_cs, s.column, context->caret_char,
368 caret_ce);
369 pp_string (context->printer, buffer);
370 pp_set_prefix (context->printer, saved_prefix);
371 pp_needs_newline (context->printer) = true;
374 /* Functions at which to stop the backtrace print. It's not
375 particularly helpful to print the callers of these functions. */
377 static const char * const bt_stop[] =
379 "main",
380 "toplev::main",
381 "execute_one_pass",
382 "compile_file",
385 /* A callback function passed to the backtrace_full function. */
387 static int
388 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
389 const char *function)
391 int *pcount = (int *) data;
393 /* If we don't have any useful information, don't print
394 anything. */
395 if (filename == NULL && function == NULL)
396 return 0;
398 /* Skip functions in diagnostic.c. */
399 if (*pcount == 0
400 && filename != NULL
401 && strcmp (lbasename (filename), "diagnostic.c") == 0)
402 return 0;
404 /* Print up to 20 functions. We could make this a --param, but
405 since this is only for debugging just use a constant for now. */
406 if (*pcount >= 20)
408 /* Returning a non-zero value stops the backtrace. */
409 return 1;
411 ++*pcount;
413 char *alc = NULL;
414 if (function != NULL)
416 char *str = cplus_demangle_v3 (function,
417 (DMGL_VERBOSE | DMGL_ANSI
418 | DMGL_GNU_V3 | DMGL_PARAMS));
419 if (str != NULL)
421 alc = str;
422 function = str;
425 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
427 size_t len = strlen (bt_stop[i]);
428 if (strncmp (function, bt_stop[i], len) == 0
429 && (function[len] == '\0' || function[len] == '('))
431 if (alc != NULL)
432 free (alc);
433 /* Returning a non-zero value stops the backtrace. */
434 return 1;
439 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
440 (unsigned long) pc,
441 function == NULL ? "???" : function,
442 filename == NULL ? "???" : filename,
443 lineno);
445 if (alc != NULL)
446 free (alc);
448 return 0;
451 /* A callback function passed to the backtrace_full function. This is
452 called if backtrace_full has an error. */
454 static void
455 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
457 if (errnum < 0)
459 /* This means that no debug info was available. Just quietly
460 skip printing backtrace info. */
461 return;
463 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
464 errnum == 0 ? "" : xstrerror (errnum));
467 /* Take any action which is expected to happen after the diagnostic
468 is written out. This function does not always return. */
469 static void
470 diagnostic_action_after_output (diagnostic_context *context,
471 diagnostic_info *diagnostic)
473 switch (diagnostic->kind)
475 case DK_DEBUG:
476 case DK_NOTE:
477 case DK_ANACHRONISM:
478 case DK_WARNING:
479 break;
481 case DK_ERROR:
482 case DK_SORRY:
483 if (context->abort_on_error)
484 real_abort ();
485 if (context->fatal_errors)
487 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
488 diagnostic_finish (context);
489 exit (FATAL_EXIT_CODE);
491 if (context->max_errors != 0
492 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
493 + diagnostic_kind_count (context, DK_SORRY))
494 >= context->max_errors))
496 fnotice (stderr,
497 "compilation terminated due to -fmax-errors=%u.\n",
498 context->max_errors);
499 diagnostic_finish (context);
500 exit (FATAL_EXIT_CODE);
502 break;
504 case DK_ICE:
506 struct backtrace_state *state =
507 backtrace_create_state (NULL, 0, bt_err_callback, NULL);
508 int count = 0;
509 if (state != NULL)
510 backtrace_full (state, 2, bt_callback, bt_err_callback,
511 (void *) &count);
513 if (context->abort_on_error)
514 real_abort ();
516 fnotice (stderr, "Please submit a full bug report,\n"
517 "with preprocessed source if appropriate.\n");
518 if (count > 0)
519 fnotice (stderr,
520 ("Please include the complete backtrace "
521 "with any bug report.\n"));
522 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
524 exit (ICE_EXIT_CODE);
527 case DK_FATAL:
528 if (context->abort_on_error)
529 real_abort ();
530 diagnostic_finish (context);
531 fnotice (stderr, "compilation terminated.\n");
532 exit (FATAL_EXIT_CODE);
534 default:
535 gcc_unreachable ();
539 void
540 diagnostic_report_current_module (diagnostic_context *context, location_t where)
542 const struct line_map *map = NULL;
544 if (pp_needs_newline (context->printer))
546 pp_newline (context->printer);
547 pp_needs_newline (context->printer) = false;
550 if (where <= BUILTINS_LOCATION)
551 return;
553 linemap_resolve_location (line_table, where,
554 LRK_MACRO_DEFINITION_LOCATION,
555 &map);
557 if (map && diagnostic_last_module_changed (context, map))
559 diagnostic_set_last_module (context, map);
560 if (! MAIN_FILE_P (map))
562 map = INCLUDED_FROM (line_table, map);
563 if (context->show_column)
564 pp_verbatim (context->printer,
565 "In file included from %r%s:%d:%d%R", "locus",
566 LINEMAP_FILE (map),
567 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
568 else
569 pp_verbatim (context->printer,
570 "In file included from %r%s:%d%R", "locus",
571 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
572 while (! MAIN_FILE_P (map))
574 map = INCLUDED_FROM (line_table, map);
575 pp_verbatim (context->printer,
576 ",\n from %r%s:%d%R", "locus",
577 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
579 pp_verbatim (context->printer, ":");
580 pp_newline (context->printer);
585 void
586 default_diagnostic_starter (diagnostic_context *context,
587 diagnostic_info *diagnostic)
589 diagnostic_report_current_module (context, diagnostic->location);
590 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
591 diagnostic));
594 void
595 default_diagnostic_finalizer (diagnostic_context *context,
596 diagnostic_info *diagnostic)
598 diagnostic_show_locus (context, diagnostic);
599 pp_destroy_prefix (context->printer);
600 pp_newline_and_flush (context->printer);
603 /* Interface to specify diagnostic kind overrides. Returns the
604 previous setting, or DK_UNSPECIFIED if the parameters are out of
605 range. If OPTION_INDEX is zero, the new setting is for all the
606 diagnostics. */
607 diagnostic_t
608 diagnostic_classify_diagnostic (diagnostic_context *context,
609 int option_index,
610 diagnostic_t new_kind,
611 location_t where)
613 diagnostic_t old_kind;
615 if (option_index < 0
616 || option_index >= context->n_opts
617 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
618 return DK_UNSPECIFIED;
620 old_kind = context->classify_diagnostic[option_index];
622 /* Handle pragmas separately, since we need to keep track of *where*
623 the pragmas were. */
624 if (where != UNKNOWN_LOCATION)
626 int i;
628 /* Record the command-line status, so we can reset it back on DK_POP. */
629 if (old_kind == DK_UNSPECIFIED)
631 old_kind = context->option_enabled (option_index,
632 context->option_state)
633 ? DK_WARNING : DK_IGNORED;
634 context->classify_diagnostic[option_index] = old_kind;
637 for (i = context->n_classification_history - 1; i >= 0; i --)
638 if (context->classification_history[i].option == option_index)
640 old_kind = context->classification_history[i].kind;
641 break;
644 i = context->n_classification_history;
645 context->classification_history =
646 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
647 * sizeof (diagnostic_classification_change_t));
648 context->classification_history[i].location = where;
649 context->classification_history[i].option = option_index;
650 context->classification_history[i].kind = new_kind;
651 context->n_classification_history ++;
653 else
654 context->classify_diagnostic[option_index] = new_kind;
656 return old_kind;
659 /* Save all diagnostic classifications in a stack. */
660 void
661 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
663 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
664 context->push_list[context->n_push ++] = context->n_classification_history;
667 /* Restore the topmost classification set off the stack. If the stack
668 is empty, revert to the state based on command line parameters. */
669 void
670 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
672 int jump_to;
673 int i;
675 if (context->n_push)
676 jump_to = context->push_list [-- context->n_push];
677 else
678 jump_to = 0;
680 i = context->n_classification_history;
681 context->classification_history =
682 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
683 * sizeof (diagnostic_classification_change_t));
684 context->classification_history[i].location = where;
685 context->classification_history[i].option = jump_to;
686 context->classification_history[i].kind = DK_POP;
687 context->n_classification_history ++;
690 /* Report a diagnostic message (an error or a warning) as specified by
691 DC. This function is *the* subroutine in terms of which front-ends
692 should implement their specific diagnostic handling modules. The
693 front-end independent format specifiers are exactly those described
694 in the documentation of output_format.
695 Return true if a diagnostic was printed, false otherwise. */
697 bool
698 diagnostic_report_diagnostic (diagnostic_context *context,
699 diagnostic_info *diagnostic)
701 location_t location = diagnostic->location;
702 diagnostic_t orig_diag_kind = diagnostic->kind;
703 const char *saved_format_spec;
705 /* Give preference to being able to inhibit warnings, before they
706 get reclassified to something else. */
707 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
708 && !diagnostic_report_warnings_p (context, location))
709 return false;
711 if (diagnostic->kind == DK_PEDWARN)
713 diagnostic->kind = pedantic_warning_kind (context);
714 /* We do this to avoid giving the message for -pedantic-errors. */
715 orig_diag_kind = diagnostic->kind;
718 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
719 return false;
721 if (context->lock > 0)
723 /* If we're reporting an ICE in the middle of some other error,
724 try to flush out the previous error, then let this one
725 through. Don't do this more than once. */
726 if (diagnostic->kind == DK_ICE && context->lock == 1)
727 pp_newline_and_flush (context->printer);
728 else
729 error_recursion (context);
732 /* If the user requested that warnings be treated as errors, so be
733 it. Note that we do this before the next block so that
734 individual warnings can be overridden back to warnings with
735 -Wno-error=*. */
736 if (context->warning_as_error_requested
737 && diagnostic->kind == DK_WARNING)
739 diagnostic->kind = DK_ERROR;
742 if (diagnostic->option_index
743 && diagnostic->option_index != permissive_error_option (context))
745 diagnostic_t diag_class = DK_UNSPECIFIED;
747 /* This tests if the user provided the appropriate -Wfoo or
748 -Wno-foo option. */
749 if (! context->option_enabled (diagnostic->option_index,
750 context->option_state))
751 return false;
753 /* This tests for #pragma diagnostic changes. */
754 if (context->n_classification_history > 0)
756 /* FIXME: Stupid search. Optimize later. */
757 for (int i = context->n_classification_history - 1; i >= 0; i --)
759 if (linemap_location_before_p
760 (line_table,
761 context->classification_history[i].location,
762 location))
764 if (context->classification_history[i].kind == (int) DK_POP)
766 i = context->classification_history[i].option;
767 continue;
769 int option = context->classification_history[i].option;
770 /* The option 0 is for all the diagnostics. */
771 if (option == 0 || option == diagnostic->option_index)
773 diag_class = context->classification_history[i].kind;
774 if (diag_class != DK_UNSPECIFIED)
775 diagnostic->kind = diag_class;
776 break;
781 /* This tests if the user provided the appropriate -Werror=foo
782 option. */
783 if (diag_class == DK_UNSPECIFIED
784 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
786 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
788 /* This allows for future extensions, like temporarily disabling
789 warnings for ranges of source code. */
790 if (diagnostic->kind == DK_IGNORED)
791 return false;
794 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
795 context->some_warnings_are_errors = true;
797 context->lock++;
799 if (diagnostic->kind == DK_ICE)
801 #ifndef ENABLE_CHECKING
802 /* When not checking, ICEs are converted to fatal errors when an
803 error has already occurred. This is counteracted by
804 abort_on_error. */
805 if ((diagnostic_kind_count (context, DK_ERROR) > 0
806 || diagnostic_kind_count (context, DK_SORRY) > 0)
807 && !context->abort_on_error)
809 expanded_location s = expand_location (diagnostic->location);
810 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
811 s.file, s.line);
812 exit (ICE_EXIT_CODE);
814 #endif
815 if (context->internal_error)
816 (*context->internal_error) (context,
817 diagnostic->message.format_spec,
818 diagnostic->message.args_ptr);
820 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
821 ++diagnostic_kind_count (context, DK_WERROR);
822 else
823 ++diagnostic_kind_count (context, diagnostic->kind);
825 saved_format_spec = diagnostic->message.format_spec;
826 if (context->show_option_requested)
828 char *option_text;
830 option_text = context->option_name (context, diagnostic->option_index,
831 orig_diag_kind, diagnostic->kind);
833 if (option_text)
835 diagnostic->message.format_spec
836 = ACONCAT ((diagnostic->message.format_spec,
837 " ",
838 "[", option_text, "]",
839 NULL));
840 free (option_text);
843 diagnostic->message.locus = &diagnostic->location;
844 diagnostic->message.x_data = &diagnostic->x_data;
845 diagnostic->x_data = NULL;
846 pp_format (context->printer, &diagnostic->message);
847 (*diagnostic_starter (context)) (context, diagnostic);
848 pp_output_formatted_text (context->printer);
849 (*diagnostic_finalizer (context)) (context, diagnostic);
850 diagnostic_action_after_output (context, diagnostic);
851 diagnostic->message.format_spec = saved_format_spec;
852 diagnostic->x_data = NULL;
854 context->lock--;
856 return true;
859 /* Given a partial pathname as input, return another pathname that
860 shares no directory elements with the pathname of __FILE__. This
861 is used by fancy_abort() to print `Internal compiler error in expr.c'
862 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
864 const char *
865 trim_filename (const char *name)
867 static const char this_file[] = __FILE__;
868 const char *p = name, *q = this_file;
870 /* First skip any "../" in each filename. This allows us to give a proper
871 reference to a file in a subdirectory. */
872 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
873 p += 3;
875 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
876 q += 3;
878 /* Now skip any parts the two filenames have in common. */
879 while (*p == *q && *p != 0 && *q != 0)
880 p++, q++;
882 /* Now go backwards until the previous directory separator. */
883 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
884 p--;
886 return p;
889 /* Standard error reporting routines in increasing order of severity.
890 All of these take arguments like printf. */
892 /* Text to be emitted verbatim to the error message stream; this
893 produces no prefix and disables line-wrapping. Use rarely. */
894 void
895 verbatim (const char *gmsgid, ...)
897 text_info text;
898 va_list ap;
900 va_start (ap, gmsgid);
901 text.err_no = errno;
902 text.args_ptr = &ap;
903 text.format_spec = _(gmsgid);
904 text.locus = NULL;
905 text.x_data = NULL;
906 pp_format_verbatim (global_dc->printer, &text);
907 pp_newline_and_flush (global_dc->printer);
908 va_end (ap);
911 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
912 void
913 diagnostic_append_note (diagnostic_context *context,
914 location_t location,
915 const char * gmsgid, ...)
917 diagnostic_info diagnostic;
918 va_list ap;
919 const char *saved_prefix;
921 va_start (ap, gmsgid);
922 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
923 if (context->inhibit_notes_p)
925 va_end (ap);
926 return;
928 saved_prefix = pp_get_prefix (context->printer);
929 pp_set_prefix (context->printer,
930 diagnostic_build_prefix (context, &diagnostic));
931 pp_newline (context->printer);
932 pp_format (context->printer, &diagnostic.message);
933 pp_output_formatted_text (context->printer);
934 pp_destroy_prefix (context->printer);
935 pp_set_prefix (context->printer, saved_prefix);
936 diagnostic_show_locus (context, &diagnostic);
937 va_end (ap);
940 bool
941 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
942 const char *gmsgid, ...)
944 diagnostic_info diagnostic;
945 va_list ap;
946 bool ret;
948 va_start (ap, gmsgid);
949 if (kind == DK_PERMERROR)
951 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
952 permissive_error_kind (global_dc));
953 diagnostic.option_index = permissive_error_option (global_dc);
955 else {
956 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
957 if (kind == DK_WARNING || kind == DK_PEDWARN)
958 diagnostic.option_index = opt;
961 ret = report_diagnostic (&diagnostic);
962 va_end (ap);
963 return ret;
966 /* An informative note at LOCATION. Use this for additional details on an error
967 message. */
968 void
969 inform (location_t location, const char *gmsgid, ...)
971 diagnostic_info diagnostic;
972 va_list ap;
974 va_start (ap, gmsgid);
975 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
976 report_diagnostic (&diagnostic);
977 va_end (ap);
980 /* An informative note at LOCATION. Use this for additional details on an
981 error message. */
982 void
983 inform_n (location_t location, int n, const char *singular_gmsgid,
984 const char *plural_gmsgid, ...)
986 diagnostic_info diagnostic;
987 va_list ap;
989 va_start (ap, plural_gmsgid);
990 diagnostic_set_info_translated (&diagnostic,
991 ngettext (singular_gmsgid, plural_gmsgid, n),
992 &ap, location, DK_NOTE);
993 report_diagnostic (&diagnostic);
994 va_end (ap);
997 /* A warning at INPUT_LOCATION. Use this for code which is correct according
998 to the relevant language specification but is likely to be buggy anyway.
999 Returns true if the warning was printed, false if it was inhibited. */
1000 bool
1001 warning (int opt, const char *gmsgid, ...)
1003 diagnostic_info diagnostic;
1004 va_list ap;
1005 bool ret;
1007 va_start (ap, gmsgid);
1008 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
1009 diagnostic.option_index = opt;
1011 ret = report_diagnostic (&diagnostic);
1012 va_end (ap);
1013 return ret;
1016 /* A warning at LOCATION. Use this for code which is correct according to the
1017 relevant language specification but is likely to be buggy anyway.
1018 Returns true if the warning was printed, false if it was inhibited. */
1020 bool
1021 warning_at (location_t location, int opt, const char *gmsgid, ...)
1023 diagnostic_info diagnostic;
1024 va_list ap;
1025 bool ret;
1027 va_start (ap, gmsgid);
1028 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
1029 diagnostic.option_index = opt;
1030 ret = report_diagnostic (&diagnostic);
1031 va_end (ap);
1032 return ret;
1035 /* A warning at LOCATION. Use this for code which is correct according to the
1036 relevant language specification but is likely to be buggy anyway.
1037 Returns true if the warning was printed, false if it was inhibited. */
1039 bool
1040 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1041 const char *plural_gmsgid, ...)
1043 diagnostic_info diagnostic;
1044 va_list ap;
1045 bool ret;
1047 va_start (ap, plural_gmsgid);
1048 diagnostic_set_info_translated (&diagnostic,
1049 ngettext (singular_gmsgid, plural_gmsgid, n),
1050 &ap, location, DK_WARNING);
1051 diagnostic.option_index = opt;
1052 ret = report_diagnostic (&diagnostic);
1053 va_end (ap);
1054 return ret;
1057 /* A "pedantic" warning at LOCATION: issues a warning unless
1058 -pedantic-errors was given on the command line, in which case it
1059 issues an error. Use this for diagnostics required by the relevant
1060 language standard, if you have chosen not to make them errors.
1062 Note that these diagnostics are issued independent of the setting
1063 of the -Wpedantic command-line switch. To get a warning enabled
1064 only with that switch, use either "if (pedantic) pedwarn
1065 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1066 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1068 Returns true if the warning was printed, false if it was inhibited. */
1070 bool
1071 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1073 diagnostic_info diagnostic;
1074 va_list ap;
1075 bool ret;
1077 va_start (ap, gmsgid);
1078 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
1079 diagnostic.option_index = opt;
1080 ret = report_diagnostic (&diagnostic);
1081 va_end (ap);
1082 return ret;
1085 /* A "permissive" error at LOCATION: issues an error unless
1086 -fpermissive was given on the command line, in which case it issues
1087 a warning. Use this for things that really should be errors but we
1088 want to support legacy code.
1090 Returns true if the warning was printed, false if it was inhibited. */
1092 bool
1093 permerror (location_t location, const char *gmsgid, ...)
1095 diagnostic_info diagnostic;
1096 va_list ap;
1097 bool ret;
1099 va_start (ap, gmsgid);
1100 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1101 permissive_error_kind (global_dc));
1102 diagnostic.option_index = permissive_error_option (global_dc);
1103 ret = report_diagnostic (&diagnostic);
1104 va_end (ap);
1105 return ret;
1108 /* A hard error: the code is definitely ill-formed, and an object file
1109 will not be produced. */
1110 void
1111 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_ERROR);
1118 report_diagnostic (&diagnostic);
1119 va_end (ap);
1122 /* A hard error: the code is definitely ill-formed, and an object file
1123 will not be produced. */
1124 void
1125 error_n (location_t location, int n, const char *singular_gmsgid,
1126 const char *plural_gmsgid, ...)
1128 diagnostic_info diagnostic;
1129 va_list ap;
1131 va_start (ap, plural_gmsgid);
1132 diagnostic_set_info_translated (&diagnostic,
1133 ngettext (singular_gmsgid, plural_gmsgid, n),
1134 &ap, location, DK_ERROR);
1135 report_diagnostic (&diagnostic);
1136 va_end (ap);
1139 /* Same as ebove, but use location LOC instead of input_location. */
1140 void
1141 error_at (location_t loc, const char *gmsgid, ...)
1143 diagnostic_info diagnostic;
1144 va_list ap;
1146 va_start (ap, gmsgid);
1147 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1148 report_diagnostic (&diagnostic);
1149 va_end (ap);
1152 /* "Sorry, not implemented." Use for a language feature which is
1153 required by the relevant specification but not implemented by GCC.
1154 An object file will not be produced. */
1155 void
1156 sorry (const char *gmsgid, ...)
1158 diagnostic_info diagnostic;
1159 va_list ap;
1161 va_start (ap, gmsgid);
1162 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1163 report_diagnostic (&diagnostic);
1164 va_end (ap);
1167 /* Return true if an error or a "sorry" has been seen. Various
1168 processing is disabled after errors. */
1169 bool
1170 seen_error (void)
1172 return errorcount || sorrycount;
1175 /* An error which is severe enough that we make no attempt to
1176 continue. Do not use this for internal consistency checks; that's
1177 internal_error. Use of this function should be rare. */
1178 void
1179 fatal_error (const char *gmsgid, ...)
1181 diagnostic_info diagnostic;
1182 va_list ap;
1184 va_start (ap, gmsgid);
1185 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
1186 report_diagnostic (&diagnostic);
1187 va_end (ap);
1189 gcc_unreachable ();
1192 /* An error which is severe enough that we make no attempt to
1193 continue. Do not use this for internal consistency checks; that's
1194 internal_error. Use of this function should be rare. */
1195 void
1196 fatal_error (location_t loc, const char *gmsgid, ...)
1198 diagnostic_info diagnostic;
1199 va_list ap;
1201 va_start (ap, gmsgid);
1202 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_FATAL);
1203 report_diagnostic (&diagnostic);
1204 va_end (ap);
1206 gcc_unreachable ();
1209 /* An internal consistency check has failed. We make no attempt to
1210 continue. Note that unless there is debugging value to be had from
1211 a more specific message, or some other good reason, you should use
1212 abort () instead of calling this function directly. */
1213 void
1214 internal_error (const char *gmsgid, ...)
1216 diagnostic_info diagnostic;
1217 va_list ap;
1219 va_start (ap, gmsgid);
1220 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1221 report_diagnostic (&diagnostic);
1222 va_end (ap);
1224 gcc_unreachable ();
1227 /* Special case error functions. Most are implemented in terms of the
1228 above, or should be. */
1230 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1231 runs its second argument through gettext. */
1232 void
1233 fnotice (FILE *file, const char *cmsgid, ...)
1235 va_list ap;
1237 va_start (ap, cmsgid);
1238 vfprintf (file, _(cmsgid), ap);
1239 va_end (ap);
1242 /* Inform the user that an error occurred while trying to report some
1243 other error. This indicates catastrophic internal inconsistencies,
1244 so give up now. But do try to flush out the previous error.
1245 This mustn't use internal_error, that will cause infinite recursion. */
1247 static void
1248 error_recursion (diagnostic_context *context)
1250 diagnostic_info diagnostic;
1252 if (context->lock < 3)
1253 pp_newline_and_flush (context->printer);
1255 fnotice (stderr,
1256 "Internal compiler error: Error reporting routines re-entered.\n");
1258 /* Call diagnostic_action_after_output to get the "please submit a bug
1259 report" message. It only looks at the kind field of diagnostic_info. */
1260 diagnostic.kind = DK_ICE;
1261 diagnostic_action_after_output (context, &diagnostic);
1263 /* Do not use gcc_unreachable here; that goes through internal_error
1264 and therefore would cause infinite recursion. */
1265 real_abort ();
1268 /* Report an internal compiler error in a friendly manner. This is
1269 the function that gets called upon use of abort() in the source
1270 code generally, thanks to a special macro. */
1272 void
1273 fancy_abort (const char *file, int line, const char *function)
1275 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1278 /* Really call the system 'abort'. This has to go right at the end of
1279 this file, so that there are no functions after it that call abort
1280 and get the system abort instead of our macro. */
1281 #undef abort
1282 static void
1283 real_abort (void)
1285 abort ();