New vectorizer messages; message format change.
[official-gcc.git] / gcc / diagnostic.c
blob87c002a498ab6b40ba27ae1974edd87a2fa81e9b
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2013 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 : context->show_column
249 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
250 s.column, locus_ce, text_cs, text, text_ce)
251 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line, locus_ce,
252 text_cs, text, text_ce));
255 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
256 MAX_WIDTH by some margin, then adjust the start of the line such
257 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
258 margin is either 10 characters or the difference between the column
259 and the length of the line, whatever is smaller. */
260 static const char *
261 adjust_line (const char *line, int max_width, int *column_p)
263 int right_margin = 10;
264 int line_width = strlen (line);
265 int column = *column_p;
267 right_margin = MIN(line_width - column, right_margin);
268 right_margin = max_width - right_margin;
269 if (line_width >= max_width && column > right_margin)
271 line += column - right_margin;
272 *column_p = right_margin;
274 return line;
277 /* Print the physical source line corresponding to the location of
278 this diagnostics, and a caret indicating the precise column. */
279 void
280 diagnostic_show_locus (diagnostic_context * context,
281 const diagnostic_info *diagnostic)
283 const char *line;
284 char *buffer;
285 expanded_location s;
286 int max_width;
287 const char *saved_prefix;
288 const char *caret_cs, *caret_ce;
290 if (!context->show_caret
291 || diagnostic->location <= BUILTINS_LOCATION
292 || diagnostic->location == context->last_location)
293 return;
295 context->last_location = diagnostic->location;
296 s = expand_location_to_spelling_point (diagnostic->location);
297 line = location_get_source_line (s);
298 if (line == NULL)
299 return;
301 max_width = context->caret_max_width;
302 line = adjust_line (line, max_width, &(s.column));
304 pp_newline (context->printer);
305 saved_prefix = pp_get_prefix (context->printer);
306 pp_set_prefix (context->printer, NULL);
307 pp_space (context->printer);
308 while (max_width > 0 && *line != '\0')
310 char c = *line == '\t' ? ' ' : *line;
311 pp_character (context->printer, c);
312 max_width--;
313 line++;
315 pp_newline (context->printer);
316 caret_cs = colorize_start (pp_show_color (context->printer), "caret");
317 caret_ce = colorize_stop (pp_show_color (context->printer));
319 /* pp_printf does not implement %*c. */
320 size_t len = s.column + 3 + strlen (caret_cs) + strlen (caret_ce);
321 buffer = XALLOCAVEC (char, len);
322 snprintf (buffer, len, "%s %*c%s", caret_cs, s.column, '^', caret_ce);
323 pp_string (context->printer, buffer);
324 pp_set_prefix (context->printer, saved_prefix);
327 /* Functions at which to stop the backtrace print. It's not
328 particularly helpful to print the callers of these functions. */
330 static const char * const bt_stop[] =
332 "main",
333 "toplev_main",
334 "execute_one_pass",
335 "compile_file",
338 /* A callback function passed to the backtrace_full function. */
340 static int
341 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
342 const char *function)
344 int *pcount = (int *) data;
346 /* If we don't have any useful information, don't print
347 anything. */
348 if (filename == NULL && function == NULL)
349 return 0;
351 /* Skip functions in diagnostic.c. */
352 if (*pcount == 0
353 && filename != NULL
354 && strcmp (lbasename(filename), "diagnostic.c") == 0)
355 return 0;
357 /* Print up to 20 functions. We could make this a --param, but
358 since this is only for debugging just use a constant for now. */
359 if (*pcount >= 20)
361 /* Returning a non-zero value stops the backtrace. */
362 return 1;
364 ++*pcount;
366 char *alc = NULL;
367 if (function != NULL)
369 char *str = cplus_demangle_v3 (function,
370 (DMGL_VERBOSE | DMGL_ANSI
371 | DMGL_GNU_V3 | DMGL_PARAMS));
372 if (str != NULL)
374 alc = str;
375 function = str;
378 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
380 size_t len = strlen (bt_stop[i]);
381 if (strncmp (function, bt_stop[i], len) == 0
382 && (function[len] == '\0' || function[len] == '('))
384 if (alc != NULL)
385 free (alc);
386 /* Returning a non-zero value stops the backtrace. */
387 return 1;
392 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
393 (unsigned long) pc,
394 function == NULL ? "???" : function,
395 filename == NULL ? "???" : filename,
396 lineno);
398 if (alc != NULL)
399 free (alc);
401 return 0;
404 /* A callback function passed to the backtrace_full function. This is
405 called if backtrace_full has an error. */
407 static void
408 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
410 if (errnum < 0)
412 /* This means that no debug info was available. Just quietly
413 skip printing backtrace info. */
414 return;
416 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
417 errnum == 0 ? "" : xstrerror (errnum));
420 /* Take any action which is expected to happen after the diagnostic
421 is written out. This function does not always return. */
422 static void
423 diagnostic_action_after_output (diagnostic_context *context,
424 diagnostic_info *diagnostic)
426 switch (diagnostic->kind)
428 case DK_DEBUG:
429 case DK_NOTE:
430 case DK_ANACHRONISM:
431 case DK_WARNING:
432 break;
434 case DK_ERROR:
435 case DK_SORRY:
436 if (context->abort_on_error)
437 real_abort ();
438 if (context->fatal_errors)
440 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
441 diagnostic_finish (context);
442 exit (FATAL_EXIT_CODE);
444 if (context->max_errors != 0
445 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
446 + diagnostic_kind_count (context, DK_SORRY))
447 >= context->max_errors))
449 fnotice (stderr,
450 "compilation terminated due to -fmax-errors=%u.\n",
451 context->max_errors);
452 diagnostic_finish (context);
453 exit (FATAL_EXIT_CODE);
455 break;
457 case DK_ICE:
459 struct backtrace_state *state =
460 backtrace_create_state (NULL, 0, bt_err_callback, NULL);
461 int count = 0;
462 if (state != NULL)
463 backtrace_full (state, 2, bt_callback, bt_err_callback,
464 (void *) &count);
466 if (context->abort_on_error)
467 real_abort ();
469 fnotice (stderr, "Please submit a full bug report,\n"
470 "with preprocessed source if appropriate.\n");
471 if (count > 0)
472 fnotice (stderr,
473 ("Please include the complete backtrace "
474 "with any bug report.\n"));
475 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
477 exit (ICE_EXIT_CODE);
480 case DK_FATAL:
481 if (context->abort_on_error)
482 real_abort ();
483 diagnostic_finish (context);
484 fnotice (stderr, "compilation terminated.\n");
485 exit (FATAL_EXIT_CODE);
487 default:
488 gcc_unreachable ();
492 void
493 diagnostic_report_current_module (diagnostic_context *context, location_t where)
495 const struct line_map *map = NULL;
497 if (pp_needs_newline (context->printer))
499 pp_newline (context->printer);
500 pp_needs_newline (context->printer) = false;
503 if (where <= BUILTINS_LOCATION)
504 return;
506 linemap_resolve_location (line_table, where,
507 LRK_MACRO_DEFINITION_LOCATION,
508 &map);
510 if (map && diagnostic_last_module_changed (context, map))
512 diagnostic_set_last_module (context, map);
513 if (! MAIN_FILE_P (map))
515 map = INCLUDED_FROM (line_table, map);
516 if (context->show_column)
517 pp_verbatim (context->printer,
518 "In file included from %r%s:%d:%d%R", "locus",
519 LINEMAP_FILE (map),
520 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
521 else
522 pp_verbatim (context->printer,
523 "In file included from %r%s:%d%R", "locus",
524 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
525 while (! MAIN_FILE_P (map))
527 map = INCLUDED_FROM (line_table, map);
528 pp_verbatim (context->printer,
529 ",\n from %r%s:%d%R", "locus",
530 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
532 pp_verbatim (context->printer, ":");
533 pp_newline (context->printer);
538 void
539 default_diagnostic_starter (diagnostic_context *context,
540 diagnostic_info *diagnostic)
542 diagnostic_report_current_module (context, diagnostic->location);
543 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
544 diagnostic));
547 void
548 default_diagnostic_finalizer (diagnostic_context *context ATTRIBUTE_UNUSED,
549 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
553 /* Interface to specify diagnostic kind overrides. Returns the
554 previous setting, or DK_UNSPECIFIED if the parameters are out of
555 range. If OPTION_INDEX is zero, the new setting is for all the
556 diagnostics. */
557 diagnostic_t
558 diagnostic_classify_diagnostic (diagnostic_context *context,
559 int option_index,
560 diagnostic_t new_kind,
561 location_t where)
563 diagnostic_t old_kind;
565 if (option_index < 0
566 || option_index >= context->n_opts
567 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
568 return DK_UNSPECIFIED;
570 old_kind = context->classify_diagnostic[option_index];
572 /* Handle pragmas separately, since we need to keep track of *where*
573 the pragmas were. */
574 if (where != UNKNOWN_LOCATION)
576 int i;
578 for (i = context->n_classification_history - 1; i >= 0; i --)
579 if (context->classification_history[i].option == option_index)
581 old_kind = context->classification_history[i].kind;
582 break;
585 i = context->n_classification_history;
586 context->classification_history =
587 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
588 * sizeof (diagnostic_classification_change_t));
589 context->classification_history[i].location = where;
590 context->classification_history[i].option = option_index;
591 context->classification_history[i].kind = new_kind;
592 context->n_classification_history ++;
594 else
595 context->classify_diagnostic[option_index] = new_kind;
597 return old_kind;
600 /* Save all diagnostic classifications in a stack. */
601 void
602 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
604 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
605 context->push_list[context->n_push ++] = context->n_classification_history;
608 /* Restore the topmost classification set off the stack. If the stack
609 is empty, revert to the state based on command line parameters. */
610 void
611 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
613 int jump_to;
614 int i;
616 if (context->n_push)
617 jump_to = context->push_list [-- context->n_push];
618 else
619 jump_to = 0;
621 i = context->n_classification_history;
622 context->classification_history =
623 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
624 * sizeof (diagnostic_classification_change_t));
625 context->classification_history[i].location = where;
626 context->classification_history[i].option = jump_to;
627 context->classification_history[i].kind = DK_POP;
628 context->n_classification_history ++;
631 /* Report a diagnostic message (an error or a warning) as specified by
632 DC. This function is *the* subroutine in terms of which front-ends
633 should implement their specific diagnostic handling modules. The
634 front-end independent format specifiers are exactly those described
635 in the documentation of output_format.
636 Return true if a diagnostic was printed, false otherwise. */
638 bool
639 diagnostic_report_diagnostic (diagnostic_context *context,
640 diagnostic_info *diagnostic)
642 location_t location = diagnostic->location;
643 diagnostic_t orig_diag_kind = diagnostic->kind;
644 const char *saved_format_spec;
646 /* Give preference to being able to inhibit warnings, before they
647 get reclassified to something else. */
648 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
649 && !diagnostic_report_warnings_p (context, location))
650 return false;
652 if (diagnostic->kind == DK_PEDWARN)
654 diagnostic->kind = pedantic_warning_kind (context);
655 /* We do this to avoid giving the message for -pedantic-errors. */
656 orig_diag_kind = diagnostic->kind;
659 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
660 return false;
662 if (context->lock > 0)
664 /* If we're reporting an ICE in the middle of some other error,
665 try to flush out the previous error, then let this one
666 through. Don't do this more than once. */
667 if (diagnostic->kind == DK_ICE && context->lock == 1)
668 pp_newline_and_flush (context->printer);
669 else
670 error_recursion (context);
673 /* If the user requested that warnings be treated as errors, so be
674 it. Note that we do this before the next block so that
675 individual warnings can be overridden back to warnings with
676 -Wno-error=*. */
677 if (context->warning_as_error_requested
678 && diagnostic->kind == DK_WARNING)
680 diagnostic->kind = DK_ERROR;
683 if (diagnostic->option_index
684 && diagnostic->option_index != permissive_error_option (context))
686 diagnostic_t diag_class = DK_UNSPECIFIED;
688 /* This tests if the user provided the appropriate -Wfoo or
689 -Wno-foo option. */
690 if (! context->option_enabled (diagnostic->option_index,
691 context->option_state))
692 return false;
694 /* This tests for #pragma diagnostic changes. */
695 if (context->n_classification_history > 0)
697 /* FIXME: Stupid search. Optimize later. */
698 for (int i = context->n_classification_history - 1; i >= 0; i --)
700 if (linemap_location_before_p
701 (line_table,
702 context->classification_history[i].location,
703 location))
705 if (context->classification_history[i].kind == (int) DK_POP)
707 i = context->classification_history[i].option;
708 continue;
710 int option = context->classification_history[i].option;
711 /* The option 0 is for all the diagnostics. */
712 if (option == 0 || option == diagnostic->option_index)
714 diag_class = context->classification_history[i].kind;
715 if (diag_class != DK_UNSPECIFIED)
716 diagnostic->kind = diag_class;
717 break;
722 /* This tests if the user provided the appropriate -Werror=foo
723 option. */
724 if (diag_class == DK_UNSPECIFIED
725 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
727 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
729 /* This allows for future extensions, like temporarily disabling
730 warnings for ranges of source code. */
731 if (diagnostic->kind == DK_IGNORED)
732 return false;
735 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
736 context->some_warnings_are_errors = true;
738 context->lock++;
740 if (diagnostic->kind == DK_ICE)
742 #ifndef ENABLE_CHECKING
743 /* When not checking, ICEs are converted to fatal errors when an
744 error has already occurred. This is counteracted by
745 abort_on_error. */
746 if ((diagnostic_kind_count (context, DK_ERROR) > 0
747 || diagnostic_kind_count (context, DK_SORRY) > 0)
748 && !context->abort_on_error)
750 expanded_location s = expand_location (diagnostic->location);
751 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
752 s.file, s.line);
753 exit (ICE_EXIT_CODE);
755 #endif
756 if (context->internal_error)
757 (*context->internal_error) (context,
758 diagnostic->message.format_spec,
759 diagnostic->message.args_ptr);
761 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
762 ++diagnostic_kind_count (context, DK_WERROR);
763 else
764 ++diagnostic_kind_count (context, diagnostic->kind);
766 saved_format_spec = diagnostic->message.format_spec;
767 if (context->show_option_requested)
769 char *option_text;
771 option_text = context->option_name (context, diagnostic->option_index,
772 orig_diag_kind, diagnostic->kind);
774 if (option_text)
776 diagnostic->message.format_spec
777 = ACONCAT ((diagnostic->message.format_spec,
778 " ",
779 "[", option_text, "]",
780 NULL));
781 free (option_text);
784 diagnostic->message.locus = &diagnostic->location;
785 diagnostic->message.x_data = &diagnostic->x_data;
786 diagnostic->x_data = NULL;
787 pp_format (context->printer, &diagnostic->message);
788 (*diagnostic_starter (context)) (context, diagnostic);
789 pp_output_formatted_text (context->printer);
790 diagnostic_show_locus (context, diagnostic);
791 (*diagnostic_finalizer (context)) (context, diagnostic);
792 pp_destroy_prefix (context->printer);
793 pp_newline_and_flush (context->printer);
794 diagnostic_action_after_output (context, diagnostic);
795 diagnostic->message.format_spec = saved_format_spec;
796 diagnostic->x_data = NULL;
798 context->lock--;
800 return true;
803 /* Given a partial pathname as input, return another pathname that
804 shares no directory elements with the pathname of __FILE__. This
805 is used by fancy_abort() to print `Internal compiler error in expr.c'
806 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
808 const char *
809 trim_filename (const char *name)
811 static const char this_file[] = __FILE__;
812 const char *p = name, *q = this_file;
814 /* First skip any "../" in each filename. This allows us to give a proper
815 reference to a file in a subdirectory. */
816 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
817 p += 3;
819 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
820 q += 3;
822 /* Now skip any parts the two filenames have in common. */
823 while (*p == *q && *p != 0 && *q != 0)
824 p++, q++;
826 /* Now go backwards until the previous directory separator. */
827 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
828 p--;
830 return p;
833 /* Standard error reporting routines in increasing order of severity.
834 All of these take arguments like printf. */
836 /* Text to be emitted verbatim to the error message stream; this
837 produces no prefix and disables line-wrapping. Use rarely. */
838 void
839 verbatim (const char *gmsgid, ...)
841 text_info text;
842 va_list ap;
844 va_start (ap, gmsgid);
845 text.err_no = errno;
846 text.args_ptr = &ap;
847 text.format_spec = _(gmsgid);
848 text.locus = NULL;
849 text.x_data = NULL;
850 pp_format_verbatim (global_dc->printer, &text);
851 pp_newline_and_flush (global_dc->printer);
852 va_end (ap);
855 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
856 void
857 diagnostic_append_note (diagnostic_context *context,
858 location_t location,
859 const char * gmsgid, ...)
861 diagnostic_info diagnostic;
862 va_list ap;
863 const char *saved_prefix;
865 va_start (ap, gmsgid);
866 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
867 if (context->inhibit_notes_p)
869 va_end (ap);
870 return;
872 saved_prefix = pp_get_prefix (context->printer);
873 pp_set_prefix (context->printer,
874 diagnostic_build_prefix (context, &diagnostic));
875 pp_newline (context->printer);
876 pp_format (context->printer, &diagnostic.message);
877 pp_output_formatted_text (context->printer);
878 pp_destroy_prefix (context->printer);
879 pp_set_prefix (context->printer, saved_prefix);
880 diagnostic_show_locus (context, &diagnostic);
881 va_end(ap);
884 bool
885 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
886 const char *gmsgid, ...)
888 diagnostic_info diagnostic;
889 va_list ap;
890 bool ret;
892 va_start (ap, gmsgid);
893 if (kind == DK_PERMERROR)
895 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
896 permissive_error_kind (global_dc));
897 diagnostic.option_index = permissive_error_option (global_dc);
899 else {
900 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
901 if (kind == DK_WARNING || kind == DK_PEDWARN)
902 diagnostic.option_index = opt;
905 ret = report_diagnostic (&diagnostic);
906 va_end (ap);
907 return ret;
910 /* An informative note at LOCATION. Use this for additional details on an error
911 message. */
912 void
913 inform (location_t location, const char *gmsgid, ...)
915 diagnostic_info diagnostic;
916 va_list ap;
918 va_start (ap, gmsgid);
919 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
920 report_diagnostic (&diagnostic);
921 va_end (ap);
924 /* An informative note at LOCATION. Use this for additional details on an
925 error message. */
926 void
927 inform_n (location_t location, int n, const char *singular_gmsgid,
928 const char *plural_gmsgid, ...)
930 diagnostic_info diagnostic;
931 va_list ap;
933 va_start (ap, plural_gmsgid);
934 diagnostic_set_info_translated (&diagnostic,
935 ngettext (singular_gmsgid, plural_gmsgid, n),
936 &ap, location, DK_NOTE);
937 report_diagnostic (&diagnostic);
938 va_end (ap);
941 /* A warning at INPUT_LOCATION. Use this for code which is correct according
942 to the relevant language specification but is likely to be buggy anyway.
943 Returns true if the warning was printed, false if it was inhibited. */
944 bool
945 warning (int opt, const char *gmsgid, ...)
947 diagnostic_info diagnostic;
948 va_list ap;
949 bool ret;
951 va_start (ap, gmsgid);
952 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
953 diagnostic.option_index = opt;
955 ret = report_diagnostic (&diagnostic);
956 va_end (ap);
957 return ret;
960 /* A warning at LOCATION. Use this for code which is correct according to the
961 relevant language specification but is likely to be buggy anyway.
962 Returns true if the warning was printed, false if it was inhibited. */
964 bool
965 warning_at (location_t location, int opt, const char *gmsgid, ...)
967 diagnostic_info diagnostic;
968 va_list ap;
969 bool ret;
971 va_start (ap, gmsgid);
972 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
973 diagnostic.option_index = opt;
974 ret = report_diagnostic (&diagnostic);
975 va_end (ap);
976 return ret;
979 /* A "pedantic" warning at LOCATION: issues a warning unless
980 -pedantic-errors was given on the command line, in which case it
981 issues an error. Use this for diagnostics required by the relevant
982 language standard, if you have chosen not to make them errors.
984 Note that these diagnostics are issued independent of the setting
985 of the -Wpedantic command-line switch. To get a warning enabled
986 only with that switch, use either "if (pedantic) pedwarn
987 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
988 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
990 Returns true if the warning was printed, false if it was inhibited. */
992 bool
993 pedwarn (location_t location, int opt, const char *gmsgid, ...)
995 diagnostic_info diagnostic;
996 va_list ap;
997 bool ret;
999 va_start (ap, gmsgid);
1000 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
1001 diagnostic.option_index = opt;
1002 ret = report_diagnostic (&diagnostic);
1003 va_end (ap);
1004 return ret;
1007 /* A "permissive" error at LOCATION: issues an error unless
1008 -fpermissive was given on the command line, in which case it issues
1009 a warning. Use this for things that really should be errors but we
1010 want to support legacy code.
1012 Returns true if the warning was printed, false if it was inhibited. */
1014 bool
1015 permerror (location_t location, const char *gmsgid, ...)
1017 diagnostic_info diagnostic;
1018 va_list ap;
1019 bool ret;
1021 va_start (ap, gmsgid);
1022 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1023 permissive_error_kind (global_dc));
1024 diagnostic.option_index = permissive_error_option (global_dc);
1025 ret = report_diagnostic (&diagnostic);
1026 va_end (ap);
1027 return ret;
1030 /* A hard error: the code is definitely ill-formed, and an object file
1031 will not be produced. */
1032 void
1033 error (const char *gmsgid, ...)
1035 diagnostic_info diagnostic;
1036 va_list ap;
1038 va_start (ap, gmsgid);
1039 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1040 report_diagnostic (&diagnostic);
1041 va_end (ap);
1044 /* A hard error: the code is definitely ill-formed, and an object file
1045 will not be produced. */
1046 void
1047 error_n (location_t location, int n, const char *singular_gmsgid,
1048 const char *plural_gmsgid, ...)
1050 diagnostic_info diagnostic;
1051 va_list ap;
1053 va_start (ap, plural_gmsgid);
1054 diagnostic_set_info_translated (&diagnostic,
1055 ngettext (singular_gmsgid, plural_gmsgid, n),
1056 &ap, location, DK_ERROR);
1057 report_diagnostic (&diagnostic);
1058 va_end (ap);
1061 /* Same as ebove, but use location LOC instead of input_location. */
1062 void
1063 error_at (location_t loc, const char *gmsgid, ...)
1065 diagnostic_info diagnostic;
1066 va_list ap;
1068 va_start (ap, gmsgid);
1069 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1070 report_diagnostic (&diagnostic);
1071 va_end (ap);
1074 /* "Sorry, not implemented." Use for a language feature which is
1075 required by the relevant specification but not implemented by GCC.
1076 An object file will not be produced. */
1077 void
1078 sorry (const char *gmsgid, ...)
1080 diagnostic_info diagnostic;
1081 va_list ap;
1083 va_start (ap, gmsgid);
1084 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1085 report_diagnostic (&diagnostic);
1086 va_end (ap);
1089 /* Return true if an error or a "sorry" has been seen. Various
1090 processing is disabled after errors. */
1091 bool
1092 seen_error (void)
1094 return errorcount || sorrycount;
1097 /* An error which is severe enough that we make no attempt to
1098 continue. Do not use this for internal consistency checks; that's
1099 internal_error. Use of this function should be rare. */
1100 void
1101 fatal_error (const char *gmsgid, ...)
1103 diagnostic_info diagnostic;
1104 va_list ap;
1106 va_start (ap, gmsgid);
1107 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
1108 report_diagnostic (&diagnostic);
1109 va_end (ap);
1111 gcc_unreachable ();
1114 /* An internal consistency check has failed. We make no attempt to
1115 continue. Note that unless there is debugging value to be had from
1116 a more specific message, or some other good reason, you should use
1117 abort () instead of calling this function directly. */
1118 void
1119 internal_error (const char *gmsgid, ...)
1121 diagnostic_info diagnostic;
1122 va_list ap;
1124 va_start (ap, gmsgid);
1125 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1126 report_diagnostic (&diagnostic);
1127 va_end (ap);
1129 gcc_unreachable ();
1132 /* Special case error functions. Most are implemented in terms of the
1133 above, or should be. */
1135 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1136 runs its second argument through gettext. */
1137 void
1138 fnotice (FILE *file, const char *cmsgid, ...)
1140 va_list ap;
1142 va_start (ap, cmsgid);
1143 vfprintf (file, _(cmsgid), ap);
1144 va_end (ap);
1147 /* Inform the user that an error occurred while trying to report some
1148 other error. This indicates catastrophic internal inconsistencies,
1149 so give up now. But do try to flush out the previous error.
1150 This mustn't use internal_error, that will cause infinite recursion. */
1152 static void
1153 error_recursion (diagnostic_context *context)
1155 diagnostic_info diagnostic;
1157 if (context->lock < 3)
1158 pp_newline_and_flush (context->printer);
1160 fnotice (stderr,
1161 "Internal compiler error: Error reporting routines re-entered.\n");
1163 /* Call diagnostic_action_after_output to get the "please submit a bug
1164 report" message. It only looks at the kind field of diagnostic_info. */
1165 diagnostic.kind = DK_ICE;
1166 diagnostic_action_after_output (context, &diagnostic);
1168 /* Do not use gcc_unreachable here; that goes through internal_error
1169 and therefore would cause infinite recursion. */
1170 real_abort ();
1173 /* Report an internal compiler error in a friendly manner. This is
1174 the function that gets called upon use of abort() in the source
1175 code generally, thanks to a special macro. */
1177 void
1178 fancy_abort (const char *file, int line, const char *function)
1180 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1183 /* Really call the system 'abort'. This has to go right at the end of
1184 this file, so that there are no functions after it that call abort
1185 and get the system abort instead of our macro. */
1186 #undef abort
1187 static void
1188 real_abort (void)
1190 abort ();