Daily bump.
[official-gcc.git] / gcc / diagnostic.c
blob0c8fbe5f766cfdc468c7143c8ca6020a66d568b8
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 ();
181 XDELETEVEC (context->classify_diagnostic);
182 context->classify_diagnostic = NULL;
184 /* diagnostic_initialize allocates context->printer using XNEW
185 and placement-new. */
186 context->printer->~pretty_printer ();
187 XDELETE (context->printer);
188 context->printer = NULL;
191 /* Initialize DIAGNOSTIC, where the message MSG has already been
192 translated. */
193 void
194 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
195 va_list *args, location_t location,
196 diagnostic_t kind)
198 diagnostic->message.err_no = errno;
199 diagnostic->message.args_ptr = args;
200 diagnostic->message.format_spec = msg;
201 diagnostic->location = location;
202 diagnostic->override_column = 0;
203 diagnostic->kind = kind;
204 diagnostic->option_index = 0;
207 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
208 translated. */
209 void
210 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
211 va_list *args, location_t location,
212 diagnostic_t kind)
214 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
217 /* Return a malloc'd string describing a location. The caller is
218 responsible for freeing the memory. */
219 char *
220 diagnostic_build_prefix (diagnostic_context *context,
221 const diagnostic_info *diagnostic)
223 static const char *const diagnostic_kind_text[] = {
224 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
225 #include "diagnostic.def"
226 #undef DEFINE_DIAGNOSTIC_KIND
227 "must-not-happen"
229 static const char *const diagnostic_kind_color[] = {
230 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
231 #include "diagnostic.def"
232 #undef DEFINE_DIAGNOSTIC_KIND
233 NULL
235 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
236 const char *text_cs = "", *text_ce = "";
237 const char *locus_cs, *locus_ce;
238 pretty_printer *pp = context->printer;
240 if (diagnostic_kind_color[diagnostic->kind])
242 text_cs = colorize_start (pp_show_color (pp),
243 diagnostic_kind_color[diagnostic->kind]);
244 text_ce = colorize_stop (pp_show_color (pp));
246 locus_cs = colorize_start (pp_show_color (pp), "locus");
247 locus_ce = colorize_stop (pp_show_color (pp));
249 expanded_location s = expand_location_to_spelling_point (diagnostic->location);
250 if (diagnostic->override_column)
251 s.column = diagnostic->override_column;
252 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
254 return
255 (s.file == NULL
256 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce,
257 text_cs, text, text_ce)
258 : !strcmp (s.file, N_("<built-in>"))
259 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, s.file, locus_ce,
260 text_cs, text, text_ce)
261 : context->show_column
262 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
263 s.column, locus_ce, text_cs, text, text_ce)
264 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line, locus_ce,
265 text_cs, text, text_ce));
268 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
269 MAX_WIDTH by some margin, then adjust the start of the line such
270 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
271 margin is either 10 characters or the difference between the column
272 and the length of the line, whatever is smaller. The length of
273 LINE is given by LINE_WIDTH. */
274 static const char *
275 adjust_line (const char *line, int line_width,
276 int max_width, int *column_p)
278 int right_margin = 10;
279 int column = *column_p;
281 gcc_checking_assert (line_width >= column);
282 right_margin = MIN (line_width - column, right_margin);
283 right_margin = max_width - right_margin;
284 if (line_width >= max_width && column > right_margin)
286 line += column - right_margin;
287 *column_p = right_margin;
289 return line;
292 /* Print the physical source line corresponding to the location of
293 this diagnostic, and a caret indicating the precise column. */
294 void
295 diagnostic_show_locus (diagnostic_context * context,
296 const diagnostic_info *diagnostic)
298 const char *line;
299 int line_width;
300 char *buffer;
301 expanded_location s;
302 int max_width;
303 const char *saved_prefix;
304 const char *caret_cs, *caret_ce;
306 if (!context->show_caret
307 || diagnostic->location <= BUILTINS_LOCATION
308 || diagnostic->location == context->last_location)
309 return;
311 context->last_location = diagnostic->location;
312 s = expand_location_to_spelling_point (diagnostic->location);
313 if (diagnostic->override_column)
314 s.column = diagnostic->override_column;
315 line = location_get_source_line (s, &line_width);
316 if (line == NULL || s.column > line_width)
317 return;
319 max_width = context->caret_max_width;
320 line = adjust_line (line, line_width, max_width, &(s.column));
322 pp_newline (context->printer);
323 saved_prefix = pp_get_prefix (context->printer);
324 pp_set_prefix (context->printer, NULL);
325 pp_space (context->printer);
326 while (max_width > 0 && line_width > 0)
328 char c = *line == '\t' ? ' ' : *line;
329 if (c == '\0')
330 c = ' ';
331 pp_character (context->printer, c);
332 max_width--;
333 line_width--;
334 line++;
336 pp_newline (context->printer);
337 caret_cs = colorize_start (pp_show_color (context->printer), "caret");
338 caret_ce = colorize_stop (pp_show_color (context->printer));
340 /* pp_printf does not implement %*c. */
341 size_t len = s.column + 3 + strlen (caret_cs) + strlen (caret_ce);
342 buffer = XALLOCAVEC (char, len);
343 snprintf (buffer, len, "%s %*c%s", caret_cs, s.column, context->caret_char,
344 caret_ce);
345 pp_string (context->printer, buffer);
346 pp_set_prefix (context->printer, saved_prefix);
347 pp_needs_newline (context->printer) = true;
350 /* Functions at which to stop the backtrace print. It's not
351 particularly helpful to print the callers of these functions. */
353 static const char * const bt_stop[] =
355 "main",
356 "toplev::main",
357 "execute_one_pass",
358 "compile_file",
361 /* A callback function passed to the backtrace_full function. */
363 static int
364 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
365 const char *function)
367 int *pcount = (int *) data;
369 /* If we don't have any useful information, don't print
370 anything. */
371 if (filename == NULL && function == NULL)
372 return 0;
374 /* Skip functions in diagnostic.c. */
375 if (*pcount == 0
376 && filename != NULL
377 && strcmp (lbasename (filename), "diagnostic.c") == 0)
378 return 0;
380 /* Print up to 20 functions. We could make this a --param, but
381 since this is only for debugging just use a constant for now. */
382 if (*pcount >= 20)
384 /* Returning a non-zero value stops the backtrace. */
385 return 1;
387 ++*pcount;
389 char *alc = NULL;
390 if (function != NULL)
392 char *str = cplus_demangle_v3 (function,
393 (DMGL_VERBOSE | DMGL_ANSI
394 | DMGL_GNU_V3 | DMGL_PARAMS));
395 if (str != NULL)
397 alc = str;
398 function = str;
401 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
403 size_t len = strlen (bt_stop[i]);
404 if (strncmp (function, bt_stop[i], len) == 0
405 && (function[len] == '\0' || function[len] == '('))
407 if (alc != NULL)
408 free (alc);
409 /* Returning a non-zero value stops the backtrace. */
410 return 1;
415 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
416 (unsigned long) pc,
417 function == NULL ? "???" : function,
418 filename == NULL ? "???" : filename,
419 lineno);
421 if (alc != NULL)
422 free (alc);
424 return 0;
427 /* A callback function passed to the backtrace_full function. This is
428 called if backtrace_full has an error. */
430 static void
431 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
433 if (errnum < 0)
435 /* This means that no debug info was available. Just quietly
436 skip printing backtrace info. */
437 return;
439 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
440 errnum == 0 ? "" : xstrerror (errnum));
443 /* Take any action which is expected to happen after the diagnostic
444 is written out. This function does not always return. */
445 static void
446 diagnostic_action_after_output (diagnostic_context *context,
447 diagnostic_info *diagnostic)
449 switch (diagnostic->kind)
451 case DK_DEBUG:
452 case DK_NOTE:
453 case DK_ANACHRONISM:
454 case DK_WARNING:
455 break;
457 case DK_ERROR:
458 case DK_SORRY:
459 if (context->abort_on_error)
460 real_abort ();
461 if (context->fatal_errors)
463 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
464 diagnostic_finish (context);
465 exit (FATAL_EXIT_CODE);
467 if (context->max_errors != 0
468 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
469 + diagnostic_kind_count (context, DK_SORRY))
470 >= context->max_errors))
472 fnotice (stderr,
473 "compilation terminated due to -fmax-errors=%u.\n",
474 context->max_errors);
475 diagnostic_finish (context);
476 exit (FATAL_EXIT_CODE);
478 break;
480 case DK_ICE:
482 struct backtrace_state *state =
483 backtrace_create_state (NULL, 0, bt_err_callback, NULL);
484 int count = 0;
485 if (state != NULL)
486 backtrace_full (state, 2, bt_callback, bt_err_callback,
487 (void *) &count);
489 if (context->abort_on_error)
490 real_abort ();
492 fnotice (stderr, "Please submit a full bug report,\n"
493 "with preprocessed source if appropriate.\n");
494 if (count > 0)
495 fnotice (stderr,
496 ("Please include the complete backtrace "
497 "with any bug report.\n"));
498 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
500 exit (ICE_EXIT_CODE);
503 case DK_FATAL:
504 if (context->abort_on_error)
505 real_abort ();
506 diagnostic_finish (context);
507 fnotice (stderr, "compilation terminated.\n");
508 exit (FATAL_EXIT_CODE);
510 default:
511 gcc_unreachable ();
515 void
516 diagnostic_report_current_module (diagnostic_context *context, location_t where)
518 const struct line_map *map = NULL;
520 if (pp_needs_newline (context->printer))
522 pp_newline (context->printer);
523 pp_needs_newline (context->printer) = false;
526 if (where <= BUILTINS_LOCATION)
527 return;
529 linemap_resolve_location (line_table, where,
530 LRK_MACRO_DEFINITION_LOCATION,
531 &map);
533 if (map && diagnostic_last_module_changed (context, map))
535 diagnostic_set_last_module (context, map);
536 if (! MAIN_FILE_P (map))
538 map = INCLUDED_FROM (line_table, map);
539 if (context->show_column)
540 pp_verbatim (context->printer,
541 "In file included from %r%s:%d:%d%R", "locus",
542 LINEMAP_FILE (map),
543 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
544 else
545 pp_verbatim (context->printer,
546 "In file included from %r%s:%d%R", "locus",
547 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
548 while (! MAIN_FILE_P (map))
550 map = INCLUDED_FROM (line_table, map);
551 pp_verbatim (context->printer,
552 ",\n from %r%s:%d%R", "locus",
553 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
555 pp_verbatim (context->printer, ":");
556 pp_newline (context->printer);
561 void
562 default_diagnostic_starter (diagnostic_context *context,
563 diagnostic_info *diagnostic)
565 diagnostic_report_current_module (context, diagnostic->location);
566 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
567 diagnostic));
570 void
571 default_diagnostic_finalizer (diagnostic_context *context,
572 diagnostic_info *diagnostic)
574 diagnostic_show_locus (context, diagnostic);
575 pp_destroy_prefix (context->printer);
576 pp_newline_and_flush (context->printer);
579 /* Interface to specify diagnostic kind overrides. Returns the
580 previous setting, or DK_UNSPECIFIED if the parameters are out of
581 range. If OPTION_INDEX is zero, the new setting is for all the
582 diagnostics. */
583 diagnostic_t
584 diagnostic_classify_diagnostic (diagnostic_context *context,
585 int option_index,
586 diagnostic_t new_kind,
587 location_t where)
589 diagnostic_t old_kind;
591 if (option_index < 0
592 || option_index >= context->n_opts
593 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
594 return DK_UNSPECIFIED;
596 old_kind = context->classify_diagnostic[option_index];
598 /* Handle pragmas separately, since we need to keep track of *where*
599 the pragmas were. */
600 if (where != UNKNOWN_LOCATION)
602 int i;
604 /* Record the command-line status, so we can reset it back on DK_POP. */
605 if (old_kind == DK_UNSPECIFIED)
607 old_kind = context->option_enabled (option_index,
608 context->option_state)
609 ? DK_WARNING : DK_IGNORED;
610 context->classify_diagnostic[option_index] = old_kind;
613 for (i = context->n_classification_history - 1; i >= 0; i --)
614 if (context->classification_history[i].option == option_index)
616 old_kind = context->classification_history[i].kind;
617 break;
620 i = context->n_classification_history;
621 context->classification_history =
622 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
623 * sizeof (diagnostic_classification_change_t));
624 context->classification_history[i].location = where;
625 context->classification_history[i].option = option_index;
626 context->classification_history[i].kind = new_kind;
627 context->n_classification_history ++;
629 else
630 context->classify_diagnostic[option_index] = new_kind;
632 return old_kind;
635 /* Save all diagnostic classifications in a stack. */
636 void
637 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
639 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
640 context->push_list[context->n_push ++] = context->n_classification_history;
643 /* Restore the topmost classification set off the stack. If the stack
644 is empty, revert to the state based on command line parameters. */
645 void
646 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
648 int jump_to;
649 int i;
651 if (context->n_push)
652 jump_to = context->push_list [-- context->n_push];
653 else
654 jump_to = 0;
656 i = context->n_classification_history;
657 context->classification_history =
658 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
659 * sizeof (diagnostic_classification_change_t));
660 context->classification_history[i].location = where;
661 context->classification_history[i].option = jump_to;
662 context->classification_history[i].kind = DK_POP;
663 context->n_classification_history ++;
666 /* Report a diagnostic message (an error or a warning) as specified by
667 DC. This function is *the* subroutine in terms of which front-ends
668 should implement their specific diagnostic handling modules. The
669 front-end independent format specifiers are exactly those described
670 in the documentation of output_format.
671 Return true if a diagnostic was printed, false otherwise. */
673 bool
674 diagnostic_report_diagnostic (diagnostic_context *context,
675 diagnostic_info *diagnostic)
677 location_t location = diagnostic->location;
678 diagnostic_t orig_diag_kind = diagnostic->kind;
679 const char *saved_format_spec;
681 /* Give preference to being able to inhibit warnings, before they
682 get reclassified to something else. */
683 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
684 && !diagnostic_report_warnings_p (context, location))
685 return false;
687 if (diagnostic->kind == DK_PEDWARN)
689 diagnostic->kind = pedantic_warning_kind (context);
690 /* We do this to avoid giving the message for -pedantic-errors. */
691 orig_diag_kind = diagnostic->kind;
694 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
695 return false;
697 if (context->lock > 0)
699 /* If we're reporting an ICE in the middle of some other error,
700 try to flush out the previous error, then let this one
701 through. Don't do this more than once. */
702 if (diagnostic->kind == DK_ICE && context->lock == 1)
703 pp_newline_and_flush (context->printer);
704 else
705 error_recursion (context);
708 /* If the user requested that warnings be treated as errors, so be
709 it. Note that we do this before the next block so that
710 individual warnings can be overridden back to warnings with
711 -Wno-error=*. */
712 if (context->warning_as_error_requested
713 && diagnostic->kind == DK_WARNING)
715 diagnostic->kind = DK_ERROR;
718 if (diagnostic->option_index
719 && diagnostic->option_index != permissive_error_option (context))
721 diagnostic_t diag_class = DK_UNSPECIFIED;
723 /* This tests if the user provided the appropriate -Wfoo or
724 -Wno-foo option. */
725 if (! context->option_enabled (diagnostic->option_index,
726 context->option_state))
727 return false;
729 /* This tests for #pragma diagnostic changes. */
730 if (context->n_classification_history > 0)
732 /* FIXME: Stupid search. Optimize later. */
733 for (int i = context->n_classification_history - 1; i >= 0; i --)
735 if (linemap_location_before_p
736 (line_table,
737 context->classification_history[i].location,
738 location))
740 if (context->classification_history[i].kind == (int) DK_POP)
742 i = context->classification_history[i].option;
743 continue;
745 int option = context->classification_history[i].option;
746 /* The option 0 is for all the diagnostics. */
747 if (option == 0 || option == diagnostic->option_index)
749 diag_class = context->classification_history[i].kind;
750 if (diag_class != DK_UNSPECIFIED)
751 diagnostic->kind = diag_class;
752 break;
757 /* This tests if the user provided the appropriate -Werror=foo
758 option. */
759 if (diag_class == DK_UNSPECIFIED
760 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
762 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
764 /* This allows for future extensions, like temporarily disabling
765 warnings for ranges of source code. */
766 if (diagnostic->kind == DK_IGNORED)
767 return false;
770 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
771 context->some_warnings_are_errors = true;
773 context->lock++;
775 if (diagnostic->kind == DK_ICE)
777 #ifndef ENABLE_CHECKING
778 /* When not checking, ICEs are converted to fatal errors when an
779 error has already occurred. This is counteracted by
780 abort_on_error. */
781 if ((diagnostic_kind_count (context, DK_ERROR) > 0
782 || diagnostic_kind_count (context, DK_SORRY) > 0)
783 && !context->abort_on_error)
785 expanded_location s = expand_location (diagnostic->location);
786 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
787 s.file, s.line);
788 exit (ICE_EXIT_CODE);
790 #endif
791 if (context->internal_error)
792 (*context->internal_error) (context,
793 diagnostic->message.format_spec,
794 diagnostic->message.args_ptr);
796 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
797 ++diagnostic_kind_count (context, DK_WERROR);
798 else
799 ++diagnostic_kind_count (context, diagnostic->kind);
801 saved_format_spec = diagnostic->message.format_spec;
802 if (context->show_option_requested)
804 char *option_text;
806 option_text = context->option_name (context, diagnostic->option_index,
807 orig_diag_kind, diagnostic->kind);
809 if (option_text)
811 diagnostic->message.format_spec
812 = ACONCAT ((diagnostic->message.format_spec,
813 " ",
814 "[", option_text, "]",
815 NULL));
816 free (option_text);
819 diagnostic->message.locus = &diagnostic->location;
820 diagnostic->message.x_data = &diagnostic->x_data;
821 diagnostic->x_data = NULL;
822 pp_format (context->printer, &diagnostic->message);
823 (*diagnostic_starter (context)) (context, diagnostic);
824 pp_output_formatted_text (context->printer);
825 (*diagnostic_finalizer (context)) (context, diagnostic);
826 diagnostic_action_after_output (context, diagnostic);
827 diagnostic->message.format_spec = saved_format_spec;
828 diagnostic->x_data = NULL;
830 context->lock--;
832 return true;
835 /* Given a partial pathname as input, return another pathname that
836 shares no directory elements with the pathname of __FILE__. This
837 is used by fancy_abort() to print `Internal compiler error in expr.c'
838 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
840 const char *
841 trim_filename (const char *name)
843 static const char this_file[] = __FILE__;
844 const char *p = name, *q = this_file;
846 /* First skip any "../" in each filename. This allows us to give a proper
847 reference to a file in a subdirectory. */
848 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
849 p += 3;
851 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
852 q += 3;
854 /* Now skip any parts the two filenames have in common. */
855 while (*p == *q && *p != 0 && *q != 0)
856 p++, q++;
858 /* Now go backwards until the previous directory separator. */
859 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
860 p--;
862 return p;
865 /* Standard error reporting routines in increasing order of severity.
866 All of these take arguments like printf. */
868 /* Text to be emitted verbatim to the error message stream; this
869 produces no prefix and disables line-wrapping. Use rarely. */
870 void
871 verbatim (const char *gmsgid, ...)
873 text_info text;
874 va_list ap;
876 va_start (ap, gmsgid);
877 text.err_no = errno;
878 text.args_ptr = &ap;
879 text.format_spec = _(gmsgid);
880 text.locus = NULL;
881 text.x_data = NULL;
882 pp_format_verbatim (global_dc->printer, &text);
883 pp_newline_and_flush (global_dc->printer);
884 va_end (ap);
887 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
888 void
889 diagnostic_append_note (diagnostic_context *context,
890 location_t location,
891 const char * gmsgid, ...)
893 diagnostic_info diagnostic;
894 va_list ap;
895 const char *saved_prefix;
897 va_start (ap, gmsgid);
898 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
899 if (context->inhibit_notes_p)
901 va_end (ap);
902 return;
904 saved_prefix = pp_get_prefix (context->printer);
905 pp_set_prefix (context->printer,
906 diagnostic_build_prefix (context, &diagnostic));
907 pp_newline (context->printer);
908 pp_format (context->printer, &diagnostic.message);
909 pp_output_formatted_text (context->printer);
910 pp_destroy_prefix (context->printer);
911 pp_set_prefix (context->printer, saved_prefix);
912 diagnostic_show_locus (context, &diagnostic);
913 va_end (ap);
916 bool
917 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
918 const char *gmsgid, ...)
920 diagnostic_info diagnostic;
921 va_list ap;
922 bool ret;
924 va_start (ap, gmsgid);
925 if (kind == DK_PERMERROR)
927 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
928 permissive_error_kind (global_dc));
929 diagnostic.option_index = permissive_error_option (global_dc);
931 else {
932 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
933 if (kind == DK_WARNING || kind == DK_PEDWARN)
934 diagnostic.option_index = opt;
937 ret = report_diagnostic (&diagnostic);
938 va_end (ap);
939 return ret;
942 /* An informative note at LOCATION. Use this for additional details on an error
943 message. */
944 void
945 inform (location_t location, const char *gmsgid, ...)
947 diagnostic_info diagnostic;
948 va_list ap;
950 va_start (ap, gmsgid);
951 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
952 report_diagnostic (&diagnostic);
953 va_end (ap);
956 /* An informative note at LOCATION. Use this for additional details on an
957 error message. */
958 void
959 inform_n (location_t location, int n, const char *singular_gmsgid,
960 const char *plural_gmsgid, ...)
962 diagnostic_info diagnostic;
963 va_list ap;
965 va_start (ap, plural_gmsgid);
966 diagnostic_set_info_translated (&diagnostic,
967 ngettext (singular_gmsgid, plural_gmsgid, n),
968 &ap, location, DK_NOTE);
969 report_diagnostic (&diagnostic);
970 va_end (ap);
973 /* A warning at INPUT_LOCATION. Use this for code which is correct according
974 to the relevant language specification but is likely to be buggy anyway.
975 Returns true if the warning was printed, false if it was inhibited. */
976 bool
977 warning (int opt, const char *gmsgid, ...)
979 diagnostic_info diagnostic;
980 va_list ap;
981 bool ret;
983 va_start (ap, gmsgid);
984 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
985 diagnostic.option_index = opt;
987 ret = report_diagnostic (&diagnostic);
988 va_end (ap);
989 return ret;
992 /* A warning at LOCATION. Use this for code which is correct according to the
993 relevant language specification but is likely to be buggy anyway.
994 Returns true if the warning was printed, false if it was inhibited. */
996 bool
997 warning_at (location_t location, int opt, const char *gmsgid, ...)
999 diagnostic_info diagnostic;
1000 va_list ap;
1001 bool ret;
1003 va_start (ap, gmsgid);
1004 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
1005 diagnostic.option_index = opt;
1006 ret = report_diagnostic (&diagnostic);
1007 va_end (ap);
1008 return ret;
1011 /* A warning at LOCATION. Use this for code which is correct according to the
1012 relevant language specification but is likely to be buggy anyway.
1013 Returns true if the warning was printed, false if it was inhibited. */
1015 bool
1016 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1017 const char *plural_gmsgid, ...)
1019 diagnostic_info diagnostic;
1020 va_list ap;
1021 bool ret;
1023 va_start (ap, plural_gmsgid);
1024 diagnostic_set_info_translated (&diagnostic,
1025 ngettext (singular_gmsgid, plural_gmsgid, n),
1026 &ap, location, DK_WARNING);
1027 diagnostic.option_index = opt;
1028 ret = report_diagnostic (&diagnostic);
1029 va_end (ap);
1030 return ret;
1033 /* A "pedantic" warning at LOCATION: issues a warning unless
1034 -pedantic-errors was given on the command line, in which case it
1035 issues an error. Use this for diagnostics required by the relevant
1036 language standard, if you have chosen not to make them errors.
1038 Note that these diagnostics are issued independent of the setting
1039 of the -Wpedantic command-line switch. To get a warning enabled
1040 only with that switch, use either "if (pedantic) pedwarn
1041 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1042 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1044 Returns true if the warning was printed, false if it was inhibited. */
1046 bool
1047 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1049 diagnostic_info diagnostic;
1050 va_list ap;
1051 bool ret;
1053 va_start (ap, gmsgid);
1054 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
1055 diagnostic.option_index = opt;
1056 ret = report_diagnostic (&diagnostic);
1057 va_end (ap);
1058 return ret;
1061 /* A "permissive" error at LOCATION: issues an error unless
1062 -fpermissive was given on the command line, in which case it issues
1063 a warning. Use this for things that really should be errors but we
1064 want to support legacy code.
1066 Returns true if the warning was printed, false if it was inhibited. */
1068 bool
1069 permerror (location_t location, const char *gmsgid, ...)
1071 diagnostic_info diagnostic;
1072 va_list ap;
1073 bool ret;
1075 va_start (ap, gmsgid);
1076 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1077 permissive_error_kind (global_dc));
1078 diagnostic.option_index = permissive_error_option (global_dc);
1079 ret = report_diagnostic (&diagnostic);
1080 va_end (ap);
1081 return ret;
1084 /* A hard error: the code is definitely ill-formed, and an object file
1085 will not be produced. */
1086 void
1087 error (const char *gmsgid, ...)
1089 diagnostic_info diagnostic;
1090 va_list ap;
1092 va_start (ap, gmsgid);
1093 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1094 report_diagnostic (&diagnostic);
1095 va_end (ap);
1098 /* A hard error: the code is definitely ill-formed, and an object file
1099 will not be produced. */
1100 void
1101 error_n (location_t location, int n, const char *singular_gmsgid,
1102 const char *plural_gmsgid, ...)
1104 diagnostic_info diagnostic;
1105 va_list ap;
1107 va_start (ap, plural_gmsgid);
1108 diagnostic_set_info_translated (&diagnostic,
1109 ngettext (singular_gmsgid, plural_gmsgid, n),
1110 &ap, location, DK_ERROR);
1111 report_diagnostic (&diagnostic);
1112 va_end (ap);
1115 /* Same as ebove, but use location LOC instead of input_location. */
1116 void
1117 error_at (location_t loc, const char *gmsgid, ...)
1119 diagnostic_info diagnostic;
1120 va_list ap;
1122 va_start (ap, gmsgid);
1123 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1124 report_diagnostic (&diagnostic);
1125 va_end (ap);
1128 /* "Sorry, not implemented." Use for a language feature which is
1129 required by the relevant specification but not implemented by GCC.
1130 An object file will not be produced. */
1131 void
1132 sorry (const char *gmsgid, ...)
1134 diagnostic_info diagnostic;
1135 va_list ap;
1137 va_start (ap, gmsgid);
1138 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1139 report_diagnostic (&diagnostic);
1140 va_end (ap);
1143 /* Return true if an error or a "sorry" has been seen. Various
1144 processing is disabled after errors. */
1145 bool
1146 seen_error (void)
1148 return errorcount || sorrycount;
1151 /* An error which is severe enough that we make no attempt to
1152 continue. Do not use this for internal consistency checks; that's
1153 internal_error. Use of this function should be rare. */
1154 void
1155 fatal_error (const char *gmsgid, ...)
1157 diagnostic_info diagnostic;
1158 va_list ap;
1160 va_start (ap, gmsgid);
1161 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
1162 report_diagnostic (&diagnostic);
1163 va_end (ap);
1165 gcc_unreachable ();
1168 /* An error which is severe enough that we make no attempt to
1169 continue. Do not use this for internal consistency checks; that's
1170 internal_error. Use of this function should be rare. */
1171 void
1172 fatal_error (location_t loc, const char *gmsgid, ...)
1174 diagnostic_info diagnostic;
1175 va_list ap;
1177 va_start (ap, gmsgid);
1178 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_FATAL);
1179 report_diagnostic (&diagnostic);
1180 va_end (ap);
1182 gcc_unreachable ();
1185 /* An internal consistency check has failed. We make no attempt to
1186 continue. Note that unless there is debugging value to be had from
1187 a more specific message, or some other good reason, you should use
1188 abort () instead of calling this function directly. */
1189 void
1190 internal_error (const char *gmsgid, ...)
1192 diagnostic_info diagnostic;
1193 va_list ap;
1195 va_start (ap, gmsgid);
1196 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1197 report_diagnostic (&diagnostic);
1198 va_end (ap);
1200 gcc_unreachable ();
1203 /* Special case error functions. Most are implemented in terms of the
1204 above, or should be. */
1206 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1207 runs its second argument through gettext. */
1208 void
1209 fnotice (FILE *file, const char *cmsgid, ...)
1211 va_list ap;
1213 va_start (ap, cmsgid);
1214 vfprintf (file, _(cmsgid), ap);
1215 va_end (ap);
1218 /* Inform the user that an error occurred while trying to report some
1219 other error. This indicates catastrophic internal inconsistencies,
1220 so give up now. But do try to flush out the previous error.
1221 This mustn't use internal_error, that will cause infinite recursion. */
1223 static void
1224 error_recursion (diagnostic_context *context)
1226 diagnostic_info diagnostic;
1228 if (context->lock < 3)
1229 pp_newline_and_flush (context->printer);
1231 fnotice (stderr,
1232 "Internal compiler error: Error reporting routines re-entered.\n");
1234 /* Call diagnostic_action_after_output to get the "please submit a bug
1235 report" message. It only looks at the kind field of diagnostic_info. */
1236 diagnostic.kind = DK_ICE;
1237 diagnostic_action_after_output (context, &diagnostic);
1239 /* Do not use gcc_unreachable here; that goes through internal_error
1240 and therefore would cause infinite recursion. */
1241 real_abort ();
1244 /* Report an internal compiler error in a friendly manner. This is
1245 the function that gets called upon use of abort() in the source
1246 code generally, thanks to a special macro. */
1248 void
1249 fancy_abort (const char *file, int line, const char *function)
1251 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1254 /* Really call the system 'abort'. This has to go right at the end of
1255 this file, so that there are no functions after it that call abort
1256 and get the system abort instead of our macro. */
1257 #undef abort
1258 static void
1259 real_abort (void)
1261 abort ();