2014-01-17 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / diagnostic.c
blob819af2e070177e6bf82def228ce189e25c267752
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);
181 /* Initialize DIAGNOSTIC, where the message MSG has already been
182 translated. */
183 void
184 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
185 va_list *args, location_t location,
186 diagnostic_t kind)
188 diagnostic->message.err_no = errno;
189 diagnostic->message.args_ptr = args;
190 diagnostic->message.format_spec = msg;
191 diagnostic->location = location;
192 diagnostic->override_column = 0;
193 diagnostic->kind = kind;
194 diagnostic->option_index = 0;
197 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
198 translated. */
199 void
200 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
201 va_list *args, location_t location,
202 diagnostic_t kind)
204 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
207 /* Return a malloc'd string describing a location. The caller is
208 responsible for freeing the memory. */
209 char *
210 diagnostic_build_prefix (diagnostic_context *context,
211 const diagnostic_info *diagnostic)
213 static const char *const diagnostic_kind_text[] = {
214 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
215 #include "diagnostic.def"
216 #undef DEFINE_DIAGNOSTIC_KIND
217 "must-not-happen"
219 static const char *const diagnostic_kind_color[] = {
220 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
221 #include "diagnostic.def"
222 #undef DEFINE_DIAGNOSTIC_KIND
223 NULL
225 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
226 const char *text_cs = "", *text_ce = "";
227 const char *locus_cs, *locus_ce;
228 pretty_printer *pp = context->printer;
230 if (diagnostic_kind_color[diagnostic->kind])
232 text_cs = colorize_start (pp_show_color (pp),
233 diagnostic_kind_color[diagnostic->kind]);
234 text_ce = colorize_stop (pp_show_color (pp));
236 locus_cs = colorize_start (pp_show_color (pp), "locus");
237 locus_ce = colorize_stop (pp_show_color (pp));
239 expanded_location s = expand_location_to_spelling_point (diagnostic->location);
240 if (diagnostic->override_column)
241 s.column = diagnostic->override_column;
242 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
244 return
245 (s.file == NULL
246 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce,
247 text_cs, text, text_ce)
248 : !strcmp (s.file, N_("<built-in>"))
249 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, s.file, locus_ce,
250 text_cs, text, text_ce)
251 : context->show_column
252 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
253 s.column, locus_ce, text_cs, text, text_ce)
254 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line, locus_ce,
255 text_cs, text, text_ce));
258 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
259 MAX_WIDTH by some margin, then adjust the start of the line such
260 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
261 margin is either 10 characters or the difference between the column
262 and the length of the line, whatever is smaller. */
263 static const char *
264 adjust_line (const char *line, int max_width, int *column_p)
266 int right_margin = 10;
267 int line_width = strlen (line);
268 int column = *column_p;
270 right_margin = MIN (line_width - column, right_margin);
271 right_margin = max_width - right_margin;
272 if (line_width >= max_width && column > right_margin)
274 line += column - right_margin;
275 *column_p = right_margin;
277 return line;
280 /* Print the physical source line corresponding to the location of
281 this diagnostics, and a caret indicating the precise column. */
282 void
283 diagnostic_show_locus (diagnostic_context * context,
284 const diagnostic_info *diagnostic)
286 const char *line;
287 char *buffer;
288 expanded_location s;
289 int max_width;
290 const char *saved_prefix;
291 const char *caret_cs, *caret_ce;
293 if (!context->show_caret
294 || diagnostic->location <= BUILTINS_LOCATION
295 || diagnostic->location == context->last_location)
296 return;
298 context->last_location = diagnostic->location;
299 s = expand_location_to_spelling_point (diagnostic->location);
300 line = location_get_source_line (s);
301 if (line == NULL)
302 return;
304 max_width = context->caret_max_width;
305 line = adjust_line (line, max_width, &(s.column));
307 pp_newline (context->printer);
308 saved_prefix = pp_get_prefix (context->printer);
309 pp_set_prefix (context->printer, NULL);
310 pp_space (context->printer);
311 while (max_width > 0 && *line != '\0')
313 char c = *line == '\t' ? ' ' : *line;
314 pp_character (context->printer, c);
315 max_width--;
316 line++;
318 pp_newline (context->printer);
319 caret_cs = colorize_start (pp_show_color (context->printer), "caret");
320 caret_ce = colorize_stop (pp_show_color (context->printer));
322 /* pp_printf does not implement %*c. */
323 size_t len = s.column + 3 + strlen (caret_cs) + strlen (caret_ce);
324 buffer = XALLOCAVEC (char, len);
325 snprintf (buffer, len, "%s %*c%s", caret_cs, s.column, '^', caret_ce);
326 pp_string (context->printer, buffer);
327 pp_set_prefix (context->printer, saved_prefix);
330 /* Functions at which to stop the backtrace print. It's not
331 particularly helpful to print the callers of these functions. */
333 static const char * const bt_stop[] =
335 "main",
336 "toplev_main",
337 "execute_one_pass",
338 "compile_file",
341 /* A callback function passed to the backtrace_full function. */
343 static int
344 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
345 const char *function)
347 int *pcount = (int *) data;
349 /* If we don't have any useful information, don't print
350 anything. */
351 if (filename == NULL && function == NULL)
352 return 0;
354 /* Skip functions in diagnostic.c. */
355 if (*pcount == 0
356 && filename != NULL
357 && strcmp (lbasename (filename), "diagnostic.c") == 0)
358 return 0;
360 /* Print up to 20 functions. We could make this a --param, but
361 since this is only for debugging just use a constant for now. */
362 if (*pcount >= 20)
364 /* Returning a non-zero value stops the backtrace. */
365 return 1;
367 ++*pcount;
369 char *alc = NULL;
370 if (function != NULL)
372 char *str = cplus_demangle_v3 (function,
373 (DMGL_VERBOSE | DMGL_ANSI
374 | DMGL_GNU_V3 | DMGL_PARAMS));
375 if (str != NULL)
377 alc = str;
378 function = str;
381 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
383 size_t len = strlen (bt_stop[i]);
384 if (strncmp (function, bt_stop[i], len) == 0
385 && (function[len] == '\0' || function[len] == '('))
387 if (alc != NULL)
388 free (alc);
389 /* Returning a non-zero value stops the backtrace. */
390 return 1;
395 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
396 (unsigned long) pc,
397 function == NULL ? "???" : function,
398 filename == NULL ? "???" : filename,
399 lineno);
401 if (alc != NULL)
402 free (alc);
404 return 0;
407 /* A callback function passed to the backtrace_full function. This is
408 called if backtrace_full has an error. */
410 static void
411 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
413 if (errnum < 0)
415 /* This means that no debug info was available. Just quietly
416 skip printing backtrace info. */
417 return;
419 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
420 errnum == 0 ? "" : xstrerror (errnum));
423 /* Take any action which is expected to happen after the diagnostic
424 is written out. This function does not always return. */
425 static void
426 diagnostic_action_after_output (diagnostic_context *context,
427 diagnostic_info *diagnostic)
429 switch (diagnostic->kind)
431 case DK_DEBUG:
432 case DK_NOTE:
433 case DK_ANACHRONISM:
434 case DK_WARNING:
435 break;
437 case DK_ERROR:
438 case DK_SORRY:
439 if (context->abort_on_error)
440 real_abort ();
441 if (context->fatal_errors)
443 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
444 diagnostic_finish (context);
445 exit (FATAL_EXIT_CODE);
447 if (context->max_errors != 0
448 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
449 + diagnostic_kind_count (context, DK_SORRY))
450 >= context->max_errors))
452 fnotice (stderr,
453 "compilation terminated due to -fmax-errors=%u.\n",
454 context->max_errors);
455 diagnostic_finish (context);
456 exit (FATAL_EXIT_CODE);
458 break;
460 case DK_ICE:
462 struct backtrace_state *state =
463 backtrace_create_state (NULL, 0, bt_err_callback, NULL);
464 int count = 0;
465 if (state != NULL)
466 backtrace_full (state, 2, bt_callback, bt_err_callback,
467 (void *) &count);
469 if (context->abort_on_error)
470 real_abort ();
472 fnotice (stderr, "Please submit a full bug report,\n"
473 "with preprocessed source if appropriate.\n");
474 if (count > 0)
475 fnotice (stderr,
476 ("Please include the complete backtrace "
477 "with any bug report.\n"));
478 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
480 exit (ICE_EXIT_CODE);
483 case DK_FATAL:
484 if (context->abort_on_error)
485 real_abort ();
486 diagnostic_finish (context);
487 fnotice (stderr, "compilation terminated.\n");
488 exit (FATAL_EXIT_CODE);
490 default:
491 gcc_unreachable ();
495 void
496 diagnostic_report_current_module (diagnostic_context *context, location_t where)
498 const struct line_map *map = NULL;
500 if (pp_needs_newline (context->printer))
502 pp_newline (context->printer);
503 pp_needs_newline (context->printer) = false;
506 if (where <= BUILTINS_LOCATION)
507 return;
509 linemap_resolve_location (line_table, where,
510 LRK_MACRO_DEFINITION_LOCATION,
511 &map);
513 if (map && diagnostic_last_module_changed (context, map))
515 diagnostic_set_last_module (context, map);
516 if (! MAIN_FILE_P (map))
518 map = INCLUDED_FROM (line_table, map);
519 if (context->show_column)
520 pp_verbatim (context->printer,
521 "In file included from %r%s:%d:%d%R", "locus",
522 LINEMAP_FILE (map),
523 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
524 else
525 pp_verbatim (context->printer,
526 "In file included from %r%s:%d%R", "locus",
527 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
528 while (! MAIN_FILE_P (map))
530 map = INCLUDED_FROM (line_table, map);
531 pp_verbatim (context->printer,
532 ",\n from %r%s:%d%R", "locus",
533 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
535 pp_verbatim (context->printer, ":");
536 pp_newline (context->printer);
541 void
542 default_diagnostic_starter (diagnostic_context *context,
543 diagnostic_info *diagnostic)
545 diagnostic_report_current_module (context, diagnostic->location);
546 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
547 diagnostic));
550 void
551 default_diagnostic_finalizer (diagnostic_context *context ATTRIBUTE_UNUSED,
552 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
556 /* Interface to specify diagnostic kind overrides. Returns the
557 previous setting, or DK_UNSPECIFIED if the parameters are out of
558 range. If OPTION_INDEX is zero, the new setting is for all the
559 diagnostics. */
560 diagnostic_t
561 diagnostic_classify_diagnostic (diagnostic_context *context,
562 int option_index,
563 diagnostic_t new_kind,
564 location_t where)
566 diagnostic_t old_kind;
568 if (option_index < 0
569 || option_index >= context->n_opts
570 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
571 return DK_UNSPECIFIED;
573 old_kind = context->classify_diagnostic[option_index];
575 /* Handle pragmas separately, since we need to keep track of *where*
576 the pragmas were. */
577 if (where != UNKNOWN_LOCATION)
579 int i;
581 for (i = context->n_classification_history - 1; i >= 0; i --)
582 if (context->classification_history[i].option == option_index)
584 old_kind = context->classification_history[i].kind;
585 break;
588 i = context->n_classification_history;
589 context->classification_history =
590 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
591 * sizeof (diagnostic_classification_change_t));
592 context->classification_history[i].location = where;
593 context->classification_history[i].option = option_index;
594 context->classification_history[i].kind = new_kind;
595 context->n_classification_history ++;
597 else
598 context->classify_diagnostic[option_index] = new_kind;
600 return old_kind;
603 /* Save all diagnostic classifications in a stack. */
604 void
605 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
607 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
608 context->push_list[context->n_push ++] = context->n_classification_history;
611 /* Restore the topmost classification set off the stack. If the stack
612 is empty, revert to the state based on command line parameters. */
613 void
614 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
616 int jump_to;
617 int i;
619 if (context->n_push)
620 jump_to = context->push_list [-- context->n_push];
621 else
622 jump_to = 0;
624 i = context->n_classification_history;
625 context->classification_history =
626 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
627 * sizeof (diagnostic_classification_change_t));
628 context->classification_history[i].location = where;
629 context->classification_history[i].option = jump_to;
630 context->classification_history[i].kind = DK_POP;
631 context->n_classification_history ++;
634 /* Report a diagnostic message (an error or a warning) as specified by
635 DC. This function is *the* subroutine in terms of which front-ends
636 should implement their specific diagnostic handling modules. The
637 front-end independent format specifiers are exactly those described
638 in the documentation of output_format.
639 Return true if a diagnostic was printed, false otherwise. */
641 bool
642 diagnostic_report_diagnostic (diagnostic_context *context,
643 diagnostic_info *diagnostic)
645 location_t location = diagnostic->location;
646 diagnostic_t orig_diag_kind = diagnostic->kind;
647 const char *saved_format_spec;
649 /* Give preference to being able to inhibit warnings, before they
650 get reclassified to something else. */
651 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
652 && !diagnostic_report_warnings_p (context, location))
653 return false;
655 if (diagnostic->kind == DK_PEDWARN)
657 diagnostic->kind = pedantic_warning_kind (context);
658 /* We do this to avoid giving the message for -pedantic-errors. */
659 orig_diag_kind = diagnostic->kind;
662 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
663 return false;
665 if (context->lock > 0)
667 /* If we're reporting an ICE in the middle of some other error,
668 try to flush out the previous error, then let this one
669 through. Don't do this more than once. */
670 if (diagnostic->kind == DK_ICE && context->lock == 1)
671 pp_newline_and_flush (context->printer);
672 else
673 error_recursion (context);
676 /* If the user requested that warnings be treated as errors, so be
677 it. Note that we do this before the next block so that
678 individual warnings can be overridden back to warnings with
679 -Wno-error=*. */
680 if (context->warning_as_error_requested
681 && diagnostic->kind == DK_WARNING)
683 diagnostic->kind = DK_ERROR;
686 if (diagnostic->option_index
687 && diagnostic->option_index != permissive_error_option (context))
689 diagnostic_t diag_class = DK_UNSPECIFIED;
691 /* This tests if the user provided the appropriate -Wfoo or
692 -Wno-foo option. */
693 if (! context->option_enabled (diagnostic->option_index,
694 context->option_state))
695 return false;
697 /* This tests for #pragma diagnostic changes. */
698 if (context->n_classification_history > 0)
700 /* FIXME: Stupid search. Optimize later. */
701 for (int i = context->n_classification_history - 1; i >= 0; i --)
703 if (linemap_location_before_p
704 (line_table,
705 context->classification_history[i].location,
706 location))
708 if (context->classification_history[i].kind == (int) DK_POP)
710 i = context->classification_history[i].option;
711 continue;
713 int option = context->classification_history[i].option;
714 /* The option 0 is for all the diagnostics. */
715 if (option == 0 || option == diagnostic->option_index)
717 diag_class = context->classification_history[i].kind;
718 if (diag_class != DK_UNSPECIFIED)
719 diagnostic->kind = diag_class;
720 break;
725 /* This tests if the user provided the appropriate -Werror=foo
726 option. */
727 if (diag_class == DK_UNSPECIFIED
728 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
730 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
732 /* This allows for future extensions, like temporarily disabling
733 warnings for ranges of source code. */
734 if (diagnostic->kind == DK_IGNORED)
735 return false;
738 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
739 context->some_warnings_are_errors = true;
741 context->lock++;
743 if (diagnostic->kind == DK_ICE)
745 #ifndef ENABLE_CHECKING
746 /* When not checking, ICEs are converted to fatal errors when an
747 error has already occurred. This is counteracted by
748 abort_on_error. */
749 if ((diagnostic_kind_count (context, DK_ERROR) > 0
750 || diagnostic_kind_count (context, DK_SORRY) > 0)
751 && !context->abort_on_error)
753 expanded_location s = expand_location (diagnostic->location);
754 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
755 s.file, s.line);
756 exit (ICE_EXIT_CODE);
758 #endif
759 if (context->internal_error)
760 (*context->internal_error) (context,
761 diagnostic->message.format_spec,
762 diagnostic->message.args_ptr);
764 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
765 ++diagnostic_kind_count (context, DK_WERROR);
766 else
767 ++diagnostic_kind_count (context, diagnostic->kind);
769 saved_format_spec = diagnostic->message.format_spec;
770 if (context->show_option_requested)
772 char *option_text;
774 option_text = context->option_name (context, diagnostic->option_index,
775 orig_diag_kind, diagnostic->kind);
777 if (option_text)
779 diagnostic->message.format_spec
780 = ACONCAT ((diagnostic->message.format_spec,
781 " ",
782 "[", option_text, "]",
783 NULL));
784 free (option_text);
787 diagnostic->message.locus = &diagnostic->location;
788 diagnostic->message.x_data = &diagnostic->x_data;
789 diagnostic->x_data = NULL;
790 pp_format (context->printer, &diagnostic->message);
791 (*diagnostic_starter (context)) (context, diagnostic);
792 pp_output_formatted_text (context->printer);
793 diagnostic_show_locus (context, diagnostic);
794 (*diagnostic_finalizer (context)) (context, diagnostic);
795 pp_destroy_prefix (context->printer);
796 pp_newline_and_flush (context->printer);
797 diagnostic_action_after_output (context, diagnostic);
798 diagnostic->message.format_spec = saved_format_spec;
799 diagnostic->x_data = NULL;
801 context->lock--;
803 return true;
806 /* Given a partial pathname as input, return another pathname that
807 shares no directory elements with the pathname of __FILE__. This
808 is used by fancy_abort() to print `Internal compiler error in expr.c'
809 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
811 const char *
812 trim_filename (const char *name)
814 static const char this_file[] = __FILE__;
815 const char *p = name, *q = this_file;
817 /* First skip any "../" in each filename. This allows us to give a proper
818 reference to a file in a subdirectory. */
819 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
820 p += 3;
822 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
823 q += 3;
825 /* Now skip any parts the two filenames have in common. */
826 while (*p == *q && *p != 0 && *q != 0)
827 p++, q++;
829 /* Now go backwards until the previous directory separator. */
830 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
831 p--;
833 return p;
836 /* Standard error reporting routines in increasing order of severity.
837 All of these take arguments like printf. */
839 /* Text to be emitted verbatim to the error message stream; this
840 produces no prefix and disables line-wrapping. Use rarely. */
841 void
842 verbatim (const char *gmsgid, ...)
844 text_info text;
845 va_list ap;
847 va_start (ap, gmsgid);
848 text.err_no = errno;
849 text.args_ptr = &ap;
850 text.format_spec = _(gmsgid);
851 text.locus = NULL;
852 text.x_data = NULL;
853 pp_format_verbatim (global_dc->printer, &text);
854 pp_newline_and_flush (global_dc->printer);
855 va_end (ap);
858 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
859 void
860 diagnostic_append_note (diagnostic_context *context,
861 location_t location,
862 const char * gmsgid, ...)
864 diagnostic_info diagnostic;
865 va_list ap;
866 const char *saved_prefix;
868 va_start (ap, gmsgid);
869 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
870 if (context->inhibit_notes_p)
872 va_end (ap);
873 return;
875 saved_prefix = pp_get_prefix (context->printer);
876 pp_set_prefix (context->printer,
877 diagnostic_build_prefix (context, &diagnostic));
878 pp_newline (context->printer);
879 pp_format (context->printer, &diagnostic.message);
880 pp_output_formatted_text (context->printer);
881 pp_destroy_prefix (context->printer);
882 pp_set_prefix (context->printer, saved_prefix);
883 diagnostic_show_locus (context, &diagnostic);
884 va_end (ap);
887 bool
888 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
889 const char *gmsgid, ...)
891 diagnostic_info diagnostic;
892 va_list ap;
893 bool ret;
895 va_start (ap, gmsgid);
896 if (kind == DK_PERMERROR)
898 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
899 permissive_error_kind (global_dc));
900 diagnostic.option_index = permissive_error_option (global_dc);
902 else {
903 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
904 if (kind == DK_WARNING || kind == DK_PEDWARN)
905 diagnostic.option_index = opt;
908 ret = report_diagnostic (&diagnostic);
909 va_end (ap);
910 return ret;
913 /* An informative note at LOCATION. Use this for additional details on an error
914 message. */
915 void
916 inform (location_t location, const char *gmsgid, ...)
918 diagnostic_info diagnostic;
919 va_list ap;
921 va_start (ap, gmsgid);
922 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
923 report_diagnostic (&diagnostic);
924 va_end (ap);
927 /* An informative note at LOCATION. Use this for additional details on an
928 error message. */
929 void
930 inform_n (location_t location, int n, const char *singular_gmsgid,
931 const char *plural_gmsgid, ...)
933 diagnostic_info diagnostic;
934 va_list ap;
936 va_start (ap, plural_gmsgid);
937 diagnostic_set_info_translated (&diagnostic,
938 ngettext (singular_gmsgid, plural_gmsgid, n),
939 &ap, location, DK_NOTE);
940 report_diagnostic (&diagnostic);
941 va_end (ap);
944 /* A warning at INPUT_LOCATION. Use this for code which is correct according
945 to the relevant language specification but is likely to be buggy anyway.
946 Returns true if the warning was printed, false if it was inhibited. */
947 bool
948 warning (int opt, const char *gmsgid, ...)
950 diagnostic_info diagnostic;
951 va_list ap;
952 bool ret;
954 va_start (ap, gmsgid);
955 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
956 diagnostic.option_index = opt;
958 ret = report_diagnostic (&diagnostic);
959 va_end (ap);
960 return ret;
963 /* A warning at LOCATION. Use this for code which is correct according to the
964 relevant language specification but is likely to be buggy anyway.
965 Returns true if the warning was printed, false if it was inhibited. */
967 bool
968 warning_at (location_t location, int opt, const char *gmsgid, ...)
970 diagnostic_info diagnostic;
971 va_list ap;
972 bool ret;
974 va_start (ap, gmsgid);
975 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
976 diagnostic.option_index = opt;
977 ret = report_diagnostic (&diagnostic);
978 va_end (ap);
979 return ret;
982 /* A "pedantic" warning at LOCATION: issues a warning unless
983 -pedantic-errors was given on the command line, in which case it
984 issues an error. Use this for diagnostics required by the relevant
985 language standard, if you have chosen not to make them errors.
987 Note that these diagnostics are issued independent of the setting
988 of the -Wpedantic command-line switch. To get a warning enabled
989 only with that switch, use either "if (pedantic) pedwarn
990 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
991 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
993 Returns true if the warning was printed, false if it was inhibited. */
995 bool
996 pedwarn (location_t location, int opt, const char *gmsgid, ...)
998 diagnostic_info diagnostic;
999 va_list ap;
1000 bool ret;
1002 va_start (ap, gmsgid);
1003 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
1004 diagnostic.option_index = opt;
1005 ret = report_diagnostic (&diagnostic);
1006 va_end (ap);
1007 return ret;
1010 /* A "permissive" error at LOCATION: issues an error unless
1011 -fpermissive was given on the command line, in which case it issues
1012 a warning. Use this for things that really should be errors but we
1013 want to support legacy code.
1015 Returns true if the warning was printed, false if it was inhibited. */
1017 bool
1018 permerror (location_t location, const char *gmsgid, ...)
1020 diagnostic_info diagnostic;
1021 va_list ap;
1022 bool ret;
1024 va_start (ap, gmsgid);
1025 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1026 permissive_error_kind (global_dc));
1027 diagnostic.option_index = permissive_error_option (global_dc);
1028 ret = report_diagnostic (&diagnostic);
1029 va_end (ap);
1030 return ret;
1033 /* A hard error: the code is definitely ill-formed, and an object file
1034 will not be produced. */
1035 void
1036 error (const char *gmsgid, ...)
1038 diagnostic_info diagnostic;
1039 va_list ap;
1041 va_start (ap, gmsgid);
1042 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1043 report_diagnostic (&diagnostic);
1044 va_end (ap);
1047 /* A hard error: the code is definitely ill-formed, and an object file
1048 will not be produced. */
1049 void
1050 error_n (location_t location, int n, const char *singular_gmsgid,
1051 const char *plural_gmsgid, ...)
1053 diagnostic_info diagnostic;
1054 va_list ap;
1056 va_start (ap, plural_gmsgid);
1057 diagnostic_set_info_translated (&diagnostic,
1058 ngettext (singular_gmsgid, plural_gmsgid, n),
1059 &ap, location, DK_ERROR);
1060 report_diagnostic (&diagnostic);
1061 va_end (ap);
1064 /* Same as ebove, but use location LOC instead of input_location. */
1065 void
1066 error_at (location_t loc, const char *gmsgid, ...)
1068 diagnostic_info diagnostic;
1069 va_list ap;
1071 va_start (ap, gmsgid);
1072 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1073 report_diagnostic (&diagnostic);
1074 va_end (ap);
1077 /* "Sorry, not implemented." Use for a language feature which is
1078 required by the relevant specification but not implemented by GCC.
1079 An object file will not be produced. */
1080 void
1081 sorry (const char *gmsgid, ...)
1083 diagnostic_info diagnostic;
1084 va_list ap;
1086 va_start (ap, gmsgid);
1087 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1088 report_diagnostic (&diagnostic);
1089 va_end (ap);
1092 /* Return true if an error or a "sorry" has been seen. Various
1093 processing is disabled after errors. */
1094 bool
1095 seen_error (void)
1097 return errorcount || sorrycount;
1100 /* An error which is severe enough that we make no attempt to
1101 continue. Do not use this for internal consistency checks; that's
1102 internal_error. Use of this function should be rare. */
1103 void
1104 fatal_error (const char *gmsgid, ...)
1106 diagnostic_info diagnostic;
1107 va_list ap;
1109 va_start (ap, gmsgid);
1110 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
1111 report_diagnostic (&diagnostic);
1112 va_end (ap);
1114 gcc_unreachable ();
1117 /* An internal consistency check has failed. We make no attempt to
1118 continue. Note that unless there is debugging value to be had from
1119 a more specific message, or some other good reason, you should use
1120 abort () instead of calling this function directly. */
1121 void
1122 internal_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_ICE);
1129 report_diagnostic (&diagnostic);
1130 va_end (ap);
1132 gcc_unreachable ();
1135 /* Special case error functions. Most are implemented in terms of the
1136 above, or should be. */
1138 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1139 runs its second argument through gettext. */
1140 void
1141 fnotice (FILE *file, const char *cmsgid, ...)
1143 va_list ap;
1145 va_start (ap, cmsgid);
1146 vfprintf (file, _(cmsgid), ap);
1147 va_end (ap);
1150 /* Inform the user that an error occurred while trying to report some
1151 other error. This indicates catastrophic internal inconsistencies,
1152 so give up now. But do try to flush out the previous error.
1153 This mustn't use internal_error, that will cause infinite recursion. */
1155 static void
1156 error_recursion (diagnostic_context *context)
1158 diagnostic_info diagnostic;
1160 if (context->lock < 3)
1161 pp_newline_and_flush (context->printer);
1163 fnotice (stderr,
1164 "Internal compiler error: Error reporting routines re-entered.\n");
1166 /* Call diagnostic_action_after_output to get the "please submit a bug
1167 report" message. It only looks at the kind field of diagnostic_info. */
1168 diagnostic.kind = DK_ICE;
1169 diagnostic_action_after_output (context, &diagnostic);
1171 /* Do not use gcc_unreachable here; that goes through internal_error
1172 and therefore would cause infinite recursion. */
1173 real_abort ();
1176 /* Report an internal compiler error in a friendly manner. This is
1177 the function that gets called upon use of abort() in the source
1178 code generally, thanks to a special macro. */
1180 void
1181 fancy_abort (const char *file, int line, const char *function)
1183 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1186 /* Really call the system 'abort'. This has to go right at the end of
1187 this file, so that there are no functions after it that call abort
1188 and get the system abort instead of our macro. */
1189 #undef abort
1190 static void
1191 real_abort (void)
1193 abort ();