Core 1402
[official-gcc.git] / gcc / diagnostic.c
blobff210dcaed4587bc09d46e417cfb5a676df3c550
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file implements the language independent aspect of diagnostic
24 message module. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "version.h"
30 #include "demangle.h"
31 #include "input.h"
32 #include "intl.h"
33 #include "backtrace.h"
34 #include "diagnostic.h"
36 #define pedantic_warning_kind(DC) \
37 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
38 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
39 #define permissive_error_option(DC) ((DC)->opt_permissive)
41 /* Prototypes. */
42 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
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;
59 /* Return a malloc'd string containing MSG formatted a la printf. The
60 caller is responsible for freeing the memory. */
61 static char *
62 build_message_string (const char *msg, ...)
64 char *str;
65 va_list ap;
67 va_start (ap, msg);
68 vasprintf (&str, msg, ap);
69 va_end (ap);
71 return str;
74 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
75 char *
76 file_name_as_prefix (const char *f)
78 return build_message_string ("%s: ", f);
83 /* Return the value of the getenv("COLUMNS") as an integer. If the
84 value is not set to a positive integer, then return INT_MAX. */
85 static int
86 getenv_columns (void)
88 const char * s = getenv ("COLUMNS");
89 if (s != NULL) {
90 int n = atoi (s);
91 if (n > 0)
92 return n;
94 return INT_MAX;
97 /* Set caret_max_width to value. */
98 void
99 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
101 /* One minus to account for the leading empty space. */
102 value = value ? value - 1
103 : (isatty (fileno (context->printer->buffer->stream))
104 ? getenv_columns () - 1: INT_MAX);
106 if (value <= 0)
107 value = INT_MAX;
109 context->caret_max_width = value;
112 /* Initialize the diagnostic message outputting machinery. */
113 void
114 diagnostic_initialize (diagnostic_context *context, int n_opts)
116 int i;
118 /* Allocate a basic pretty-printer. Clients will replace this a
119 much more elaborated pretty-printer if they wish. */
120 context->printer = XNEW (pretty_printer);
121 pp_construct (context->printer, NULL, 0);
122 /* By default, diagnostics are sent to stderr. */
123 context->printer->buffer->stream = stderr;
124 /* By default, we emit prefixes once per message. */
125 context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
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) (T),
215 #include "diagnostic.def"
216 #undef DEFINE_DIAGNOSTIC_KIND
217 "must-not-happen"
219 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
220 expanded_location s = expand_location_to_spelling_point (diagnostic->location);
221 if (diagnostic->override_column)
222 s.column = diagnostic->override_column;
223 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
225 return
226 (s.file == NULL
227 ? build_message_string ("%s: %s", progname, text)
228 : context->show_column
229 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
230 : build_message_string ("%s:%d: %s", s.file, s.line, text));
233 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
234 MAX_WIDTH by some margin, then adjust the start of the line such
235 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
236 margin is either 10 characters or the difference between the column
237 and the length of the line, whatever is smaller. */
238 static const char *
239 adjust_line (const char *line, int max_width, int *column_p)
241 int right_margin = 10;
242 int line_width = strlen (line);
243 int column = *column_p;
245 right_margin = MIN(line_width - column, right_margin);
246 right_margin = max_width - right_margin;
247 if (line_width >= max_width && column > right_margin)
249 line += column - right_margin;
250 *column_p = right_margin;
252 return line;
255 /* Print the physical source line corresponding to the location of
256 this diagnostics, and a caret indicating the precise column. */
257 void
258 diagnostic_show_locus (diagnostic_context * context,
259 const diagnostic_info *diagnostic)
261 const char *line;
262 char *buffer;
263 expanded_location s;
264 int max_width;
265 const char *saved_prefix;
268 if (!context->show_caret
269 || diagnostic->location <= BUILTINS_LOCATION
270 || diagnostic->location == context->last_location)
271 return;
273 context->last_location = diagnostic->location;
274 s = expand_location_to_spelling_point (diagnostic->location);
275 line = location_get_source_line (s);
276 if (line == NULL)
277 return;
279 max_width = context->caret_max_width;
280 line = adjust_line (line, max_width, &(s.column));
282 pp_newline (context->printer);
283 saved_prefix = pp_get_prefix (context->printer);
284 pp_set_prefix (context->printer, NULL);
285 pp_character (context->printer, ' ');
286 while (max_width > 0 && *line != '\0')
288 char c = *line == '\t' ? ' ' : *line;
289 pp_character (context->printer, c);
290 max_width--;
291 line++;
293 pp_newline (context->printer);
294 /* pp_printf does not implement %*c. */
295 buffer = XALLOCAVEC (char, s.column + 3);
296 snprintf (buffer, s.column + 3, " %*c", s.column, '^');
297 pp_string (context->printer, buffer);
298 pp_set_prefix (context->printer, saved_prefix);
301 /* Functions at which to stop the backtrace print. It's not
302 particularly helpful to print the callers of these functions. */
304 static const char * const bt_stop[] =
306 "main",
307 "toplev_main",
308 "execute_one_pass",
309 "compile_file",
312 /* A callback function passed to the backtrace_full function. */
314 static int
315 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
316 const char *function)
318 int *pcount = (int *) data;
320 /* If we don't have any useful information, don't print
321 anything. */
322 if (filename == NULL && function == NULL)
323 return 0;
325 /* Skip functions in diagnostic.c. */
326 if (*pcount == 0
327 && filename != NULL
328 && strcmp (lbasename(filename), "diagnostic.c") == 0)
329 return 0;
331 /* Print up to 20 functions. We could make this a --param, but
332 since this is only for debugging just use a constant for now. */
333 if (*pcount >= 20)
335 /* Returning a non-zero value stops the backtrace. */
336 return 1;
338 ++*pcount;
340 char *alc = NULL;
341 if (function != NULL)
343 char *str = cplus_demangle_v3 (function,
344 (DMGL_VERBOSE | DMGL_ANSI
345 | DMGL_GNU_V3 | DMGL_PARAMS));
346 if (str != NULL)
348 alc = str;
349 function = str;
352 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
354 size_t len = strlen (bt_stop[i]);
355 if (strncmp (function, bt_stop[i], len) == 0
356 && (function[len] == '\0' || function[len] == '('))
358 if (alc != NULL)
359 free (alc);
360 /* Returning a non-zero value stops the backtrace. */
361 return 1;
366 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
367 (unsigned long) pc,
368 function == NULL ? "???" : function,
369 filename == NULL ? "???" : filename,
370 lineno);
372 if (alc != NULL)
373 free (alc);
375 return 0;
378 /* A callback function passed to the backtrace_full function. This is
379 called if backtrace_full has an error. */
381 static void
382 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
384 if (errnum < 0)
386 /* This means that no debug info was available. Just quietly
387 skip printing backtrace info. */
388 return;
390 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
391 errnum == 0 ? "" : xstrerror (errnum));
394 /* Take any action which is expected to happen after the diagnostic
395 is written out. This function does not always return. */
396 static void
397 diagnostic_action_after_output (diagnostic_context *context,
398 diagnostic_info *diagnostic)
400 switch (diagnostic->kind)
402 case DK_DEBUG:
403 case DK_NOTE:
404 case DK_ANACHRONISM:
405 case DK_WARNING:
406 break;
408 case DK_ERROR:
409 case DK_SORRY:
410 if (context->abort_on_error)
411 real_abort ();
412 if (context->fatal_errors)
414 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
415 diagnostic_finish (context);
416 exit (FATAL_EXIT_CODE);
418 if (context->max_errors != 0
419 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
420 + diagnostic_kind_count (context, DK_SORRY))
421 >= context->max_errors))
423 fnotice (stderr,
424 "compilation terminated due to -fmax-errors=%u.\n",
425 context->max_errors);
426 diagnostic_finish (context);
427 exit (FATAL_EXIT_CODE);
429 break;
431 case DK_ICE:
433 struct backtrace_state *state =
434 backtrace_create_state (NULL, 0, bt_err_callback, NULL);
435 int count = 0;
436 if (state != NULL)
437 backtrace_full (state, 2, bt_callback, bt_err_callback,
438 (void *) &count);
440 if (context->abort_on_error)
441 real_abort ();
443 fnotice (stderr, "Please submit a full bug report,\n"
444 "with preprocessed source if appropriate.\n");
445 if (count > 0)
446 fnotice (stderr,
447 ("Please include the complete backtrace "
448 "with any bug report.\n"));
449 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
451 exit (ICE_EXIT_CODE);
454 case DK_FATAL:
455 if (context->abort_on_error)
456 real_abort ();
457 diagnostic_finish (context);
458 fnotice (stderr, "compilation terminated.\n");
459 exit (FATAL_EXIT_CODE);
461 default:
462 gcc_unreachable ();
466 void
467 diagnostic_report_current_module (diagnostic_context *context, location_t where)
469 const struct line_map *map = NULL;
471 if (pp_needs_newline (context->printer))
473 pp_newline (context->printer);
474 pp_needs_newline (context->printer) = false;
477 if (where <= BUILTINS_LOCATION)
478 return;
480 linemap_resolve_location (line_table, where,
481 LRK_MACRO_DEFINITION_LOCATION,
482 &map);
484 if (map && diagnostic_last_module_changed (context, map))
486 diagnostic_set_last_module (context, map);
487 if (! MAIN_FILE_P (map))
489 map = INCLUDED_FROM (line_table, map);
490 if (context->show_column)
491 pp_verbatim (context->printer,
492 "In file included from %s:%d:%d",
493 LINEMAP_FILE (map),
494 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
495 else
496 pp_verbatim (context->printer,
497 "In file included from %s:%d",
498 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
499 while (! MAIN_FILE_P (map))
501 map = INCLUDED_FROM (line_table, map);
502 pp_verbatim (context->printer,
503 ",\n from %s:%d",
504 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
506 pp_verbatim (context->printer, ":");
507 pp_newline (context->printer);
512 void
513 default_diagnostic_starter (diagnostic_context *context,
514 diagnostic_info *diagnostic)
516 diagnostic_report_current_module (context, diagnostic->location);
517 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
518 diagnostic));
521 void
522 default_diagnostic_finalizer (diagnostic_context *context ATTRIBUTE_UNUSED,
523 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
527 /* Interface to specify diagnostic kind overrides. Returns the
528 previous setting, or DK_UNSPECIFIED if the parameters are out of
529 range. */
530 diagnostic_t
531 diagnostic_classify_diagnostic (diagnostic_context *context,
532 int option_index,
533 diagnostic_t new_kind,
534 location_t where)
536 diagnostic_t old_kind;
538 if (option_index <= 0
539 || option_index >= context->n_opts
540 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
541 return DK_UNSPECIFIED;
543 old_kind = context->classify_diagnostic[option_index];
545 /* Handle pragmas separately, since we need to keep track of *where*
546 the pragmas were. */
547 if (where != UNKNOWN_LOCATION)
549 int i;
551 for (i = context->n_classification_history - 1; i >= 0; i --)
552 if (context->classification_history[i].option == option_index)
554 old_kind = context->classification_history[i].kind;
555 break;
558 i = context->n_classification_history;
559 context->classification_history =
560 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
561 * sizeof (diagnostic_classification_change_t));
562 context->classification_history[i].location = where;
563 context->classification_history[i].option = option_index;
564 context->classification_history[i].kind = new_kind;
565 context->n_classification_history ++;
567 else
568 context->classify_diagnostic[option_index] = new_kind;
570 return old_kind;
573 /* Save all diagnostic classifications in a stack. */
574 void
575 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
577 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
578 context->push_list[context->n_push ++] = context->n_classification_history;
581 /* Restore the topmost classification set off the stack. If the stack
582 is empty, revert to the state based on command line parameters. */
583 void
584 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
586 int jump_to;
587 int i;
589 if (context->n_push)
590 jump_to = context->push_list [-- context->n_push];
591 else
592 jump_to = 0;
594 i = context->n_classification_history;
595 context->classification_history =
596 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
597 * sizeof (diagnostic_classification_change_t));
598 context->classification_history[i].location = where;
599 context->classification_history[i].option = jump_to;
600 context->classification_history[i].kind = DK_POP;
601 context->n_classification_history ++;
604 /* Report a diagnostic message (an error or a warning) as specified by
605 DC. This function is *the* subroutine in terms of which front-ends
606 should implement their specific diagnostic handling modules. The
607 front-end independent format specifiers are exactly those described
608 in the documentation of output_format.
609 Return true if a diagnostic was printed, false otherwise. */
611 bool
612 diagnostic_report_diagnostic (diagnostic_context *context,
613 diagnostic_info *diagnostic)
615 location_t location = diagnostic->location;
616 diagnostic_t orig_diag_kind = diagnostic->kind;
617 const char *saved_format_spec;
619 /* Give preference to being able to inhibit warnings, before they
620 get reclassified to something else. */
621 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
622 && !diagnostic_report_warnings_p (context, location))
623 return false;
625 if (diagnostic->kind == DK_PEDWARN)
627 diagnostic->kind = pedantic_warning_kind (context);
628 /* We do this to avoid giving the message for -pedantic-errors. */
629 orig_diag_kind = diagnostic->kind;
632 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
633 return false;
635 if (context->lock > 0)
637 /* If we're reporting an ICE in the middle of some other error,
638 try to flush out the previous error, then let this one
639 through. Don't do this more than once. */
640 if (diagnostic->kind == DK_ICE && context->lock == 1)
641 pp_newline_and_flush (context->printer);
642 else
643 error_recursion (context);
646 /* If the user requested that warnings be treated as errors, so be
647 it. Note that we do this before the next block so that
648 individual warnings can be overridden back to warnings with
649 -Wno-error=*. */
650 if (context->warning_as_error_requested
651 && diagnostic->kind == DK_WARNING)
653 diagnostic->kind = DK_ERROR;
656 if (diagnostic->option_index
657 && diagnostic->option_index != permissive_error_option (context))
659 diagnostic_t diag_class = DK_UNSPECIFIED;
661 /* This tests if the user provided the appropriate -Wfoo or
662 -Wno-foo option. */
663 if (! context->option_enabled (diagnostic->option_index,
664 context->option_state))
665 return false;
667 /* This tests for #pragma diagnostic changes. */
668 if (context->n_classification_history > 0)
670 int i;
671 /* FIXME: Stupid search. Optimize later. */
672 for (i = context->n_classification_history - 1; i >= 0; i --)
674 if (linemap_location_before_p
675 (line_table,
676 context->classification_history[i].location,
677 location))
679 if (context->classification_history[i].kind == (int) DK_POP)
681 i = context->classification_history[i].option;
682 continue;
684 if (context->classification_history[i].option == diagnostic->option_index)
686 diag_class = context->classification_history[i].kind;
687 if (diag_class != DK_UNSPECIFIED)
688 diagnostic->kind = diag_class;
689 break;
694 /* This tests if the user provided the appropriate -Werror=foo
695 option. */
696 if (diag_class == DK_UNSPECIFIED
697 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
699 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
701 /* This allows for future extensions, like temporarily disabling
702 warnings for ranges of source code. */
703 if (diagnostic->kind == DK_IGNORED)
704 return false;
707 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
708 context->some_warnings_are_errors = true;
710 context->lock++;
712 if (diagnostic->kind == DK_ICE)
714 #ifndef ENABLE_CHECKING
715 /* When not checking, ICEs are converted to fatal errors when an
716 error has already occurred. This is counteracted by
717 abort_on_error. */
718 if ((diagnostic_kind_count (context, DK_ERROR) > 0
719 || diagnostic_kind_count (context, DK_SORRY) > 0)
720 && !context->abort_on_error)
722 expanded_location s = expand_location (diagnostic->location);
723 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
724 s.file, s.line);
725 exit (ICE_EXIT_CODE);
727 #endif
728 if (context->internal_error)
729 (*context->internal_error) (context,
730 diagnostic->message.format_spec,
731 diagnostic->message.args_ptr);
733 ++diagnostic_kind_count (context, diagnostic->kind);
735 saved_format_spec = diagnostic->message.format_spec;
736 if (context->show_option_requested)
738 char *option_text;
740 option_text = context->option_name (context, diagnostic->option_index,
741 orig_diag_kind, diagnostic->kind);
743 if (option_text)
745 diagnostic->message.format_spec
746 = ACONCAT ((diagnostic->message.format_spec,
747 " ",
748 "[", option_text, "]",
749 NULL));
750 free (option_text);
753 diagnostic->message.locus = &diagnostic->location;
754 diagnostic->message.x_data = &diagnostic->x_data;
755 diagnostic->x_data = NULL;
756 pp_format (context->printer, &diagnostic->message);
757 (*diagnostic_starter (context)) (context, diagnostic);
758 pp_output_formatted_text (context->printer);
759 diagnostic_show_locus (context, diagnostic);
760 (*diagnostic_finalizer (context)) (context, diagnostic);
761 pp_destroy_prefix (context->printer);
762 pp_newline_and_flush (context->printer);
763 diagnostic_action_after_output (context, diagnostic);
764 diagnostic->message.format_spec = saved_format_spec;
765 diagnostic->x_data = NULL;
767 context->lock--;
769 return true;
772 /* Given a partial pathname as input, return another pathname that
773 shares no directory elements with the pathname of __FILE__. This
774 is used by fancy_abort() to print `Internal compiler error in expr.c'
775 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
777 const char *
778 trim_filename (const char *name)
780 static const char this_file[] = __FILE__;
781 const char *p = name, *q = this_file;
783 /* First skip any "../" in each filename. This allows us to give a proper
784 reference to a file in a subdirectory. */
785 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
786 p += 3;
788 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
789 q += 3;
791 /* Now skip any parts the two filenames have in common. */
792 while (*p == *q && *p != 0 && *q != 0)
793 p++, q++;
795 /* Now go backwards until the previous directory separator. */
796 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
797 p--;
799 return p;
802 /* Standard error reporting routines in increasing order of severity.
803 All of these take arguments like printf. */
805 /* Text to be emitted verbatim to the error message stream; this
806 produces no prefix and disables line-wrapping. Use rarely. */
807 void
808 verbatim (const char *gmsgid, ...)
810 text_info text;
811 va_list ap;
813 va_start (ap, gmsgid);
814 text.err_no = errno;
815 text.args_ptr = &ap;
816 text.format_spec = _(gmsgid);
817 text.locus = NULL;
818 text.x_data = NULL;
819 pp_format_verbatim (global_dc->printer, &text);
820 pp_newline_and_flush (global_dc->printer);
821 va_end (ap);
824 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
825 void
826 diagnostic_append_note (diagnostic_context *context,
827 location_t location,
828 const char * gmsgid, ...)
830 diagnostic_info diagnostic;
831 va_list ap;
833 va_start (ap, gmsgid);
834 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
835 if (context->inhibit_notes_p)
836 return;
837 pp_set_prefix (context->printer,
838 diagnostic_build_prefix (context, &diagnostic));
839 pp_newline (context->printer);
840 pp_format (context->printer, &diagnostic.message);
841 pp_output_formatted_text (context->printer);
842 pp_destroy_prefix (context->printer);
843 diagnostic_show_locus (context, &diagnostic);
844 va_end(ap);
847 bool
848 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
849 const char *gmsgid, ...)
851 diagnostic_info diagnostic;
852 va_list ap;
853 bool ret;
855 va_start (ap, gmsgid);
856 if (kind == DK_PERMERROR)
858 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
859 permissive_error_kind (global_dc));
860 diagnostic.option_index = permissive_error_option (global_dc);
862 else {
863 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
864 if (kind == DK_WARNING || kind == DK_PEDWARN)
865 diagnostic.option_index = opt;
868 ret = report_diagnostic (&diagnostic);
869 va_end (ap);
870 return ret;
873 /* An informative note at LOCATION. Use this for additional details on an error
874 message. */
875 void
876 inform (location_t location, const char *gmsgid, ...)
878 diagnostic_info diagnostic;
879 va_list ap;
881 va_start (ap, gmsgid);
882 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
883 report_diagnostic (&diagnostic);
884 va_end (ap);
887 /* An informative note at LOCATION. Use this for additional details on an
888 error message. */
889 void
890 inform_n (location_t location, int n, const char *singular_gmsgid,
891 const char *plural_gmsgid, ...)
893 diagnostic_info diagnostic;
894 va_list ap;
896 va_start (ap, plural_gmsgid);
897 diagnostic_set_info_translated (&diagnostic,
898 ngettext (singular_gmsgid, plural_gmsgid, n),
899 &ap, location, DK_NOTE);
900 report_diagnostic (&diagnostic);
901 va_end (ap);
904 /* A warning at INPUT_LOCATION. Use this for code which is correct according
905 to the relevant language specification but is likely to be buggy anyway.
906 Returns true if the warning was printed, false if it was inhibited. */
907 bool
908 warning (int opt, const char *gmsgid, ...)
910 diagnostic_info diagnostic;
911 va_list ap;
912 bool ret;
914 va_start (ap, gmsgid);
915 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
916 diagnostic.option_index = opt;
918 ret = report_diagnostic (&diagnostic);
919 va_end (ap);
920 return ret;
923 /* A warning at LOCATION. Use this for code which is correct according to the
924 relevant language specification but is likely to be buggy anyway.
925 Returns true if the warning was printed, false if it was inhibited. */
927 bool
928 warning_at (location_t location, int opt, const char *gmsgid, ...)
930 diagnostic_info diagnostic;
931 va_list ap;
932 bool ret;
934 va_start (ap, gmsgid);
935 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
936 diagnostic.option_index = opt;
937 ret = report_diagnostic (&diagnostic);
938 va_end (ap);
939 return ret;
942 /* A "pedantic" warning at LOCATION: issues a warning unless
943 -pedantic-errors was given on the command line, in which case it
944 issues an error. Use this for diagnostics required by the relevant
945 language standard, if you have chosen not to make them errors.
947 Note that these diagnostics are issued independent of the setting
948 of the -Wpedantic command-line switch. To get a warning enabled
949 only with that switch, use either "if (pedantic) pedwarn
950 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
951 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
953 Returns true if the warning was printed, false if it was inhibited. */
955 bool
956 pedwarn (location_t location, int opt, const char *gmsgid, ...)
958 diagnostic_info diagnostic;
959 va_list ap;
960 bool ret;
962 va_start (ap, gmsgid);
963 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
964 diagnostic.option_index = opt;
965 ret = report_diagnostic (&diagnostic);
966 va_end (ap);
967 return ret;
970 /* A "permissive" error at LOCATION: issues an error unless
971 -fpermissive was given on the command line, in which case it issues
972 a warning. Use this for things that really should be errors but we
973 want to support legacy code.
975 Returns true if the warning was printed, false if it was inhibited. */
977 bool
978 permerror (location_t location, const char *gmsgid, ...)
980 diagnostic_info diagnostic;
981 va_list ap;
982 bool ret;
984 va_start (ap, gmsgid);
985 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
986 permissive_error_kind (global_dc));
987 diagnostic.option_index = permissive_error_option (global_dc);
988 ret = report_diagnostic (&diagnostic);
989 va_end (ap);
990 return ret;
993 /* A hard error: the code is definitely ill-formed, and an object file
994 will not be produced. */
995 void
996 error (const char *gmsgid, ...)
998 diagnostic_info diagnostic;
999 va_list ap;
1001 va_start (ap, gmsgid);
1002 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1003 report_diagnostic (&diagnostic);
1004 va_end (ap);
1007 /* A hard error: the code is definitely ill-formed, and an object file
1008 will not be produced. */
1009 void
1010 error_n (location_t location, int n, const char *singular_gmsgid,
1011 const char *plural_gmsgid, ...)
1013 diagnostic_info diagnostic;
1014 va_list ap;
1016 va_start (ap, plural_gmsgid);
1017 diagnostic_set_info_translated (&diagnostic,
1018 ngettext (singular_gmsgid, plural_gmsgid, n),
1019 &ap, location, DK_ERROR);
1020 report_diagnostic (&diagnostic);
1021 va_end (ap);
1024 /* Same as ebove, but use location LOC instead of input_location. */
1025 void
1026 error_at (location_t loc, const char *gmsgid, ...)
1028 diagnostic_info diagnostic;
1029 va_list ap;
1031 va_start (ap, gmsgid);
1032 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1033 report_diagnostic (&diagnostic);
1034 va_end (ap);
1037 /* "Sorry, not implemented." Use for a language feature which is
1038 required by the relevant specification but not implemented by GCC.
1039 An object file will not be produced. */
1040 void
1041 sorry (const char *gmsgid, ...)
1043 diagnostic_info diagnostic;
1044 va_list ap;
1046 va_start (ap, gmsgid);
1047 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1048 report_diagnostic (&diagnostic);
1049 va_end (ap);
1052 /* Return true if an error or a "sorry" has been seen. Various
1053 processing is disabled after errors. */
1054 bool
1055 seen_error (void)
1057 return errorcount || sorrycount;
1060 /* An error which is severe enough that we make no attempt to
1061 continue. Do not use this for internal consistency checks; that's
1062 internal_error. Use of this function should be rare. */
1063 void
1064 fatal_error (const char *gmsgid, ...)
1066 diagnostic_info diagnostic;
1067 va_list ap;
1069 va_start (ap, gmsgid);
1070 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
1071 report_diagnostic (&diagnostic);
1072 va_end (ap);
1074 gcc_unreachable ();
1077 /* An internal consistency check has failed. We make no attempt to
1078 continue. Note that unless there is debugging value to be had from
1079 a more specific message, or some other good reason, you should use
1080 abort () instead of calling this function directly. */
1081 void
1082 internal_error (const char *gmsgid, ...)
1084 diagnostic_info diagnostic;
1085 va_list ap;
1087 va_start (ap, gmsgid);
1088 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1089 report_diagnostic (&diagnostic);
1090 va_end (ap);
1092 gcc_unreachable ();
1095 /* Special case error functions. Most are implemented in terms of the
1096 above, or should be. */
1098 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1099 runs its second argument through gettext. */
1100 void
1101 fnotice (FILE *file, const char *cmsgid, ...)
1103 va_list ap;
1105 va_start (ap, cmsgid);
1106 vfprintf (file, _(cmsgid), ap);
1107 va_end (ap);
1110 /* Inform the user that an error occurred while trying to report some
1111 other error. This indicates catastrophic internal inconsistencies,
1112 so give up now. But do try to flush out the previous error.
1113 This mustn't use internal_error, that will cause infinite recursion. */
1115 static void
1116 error_recursion (diagnostic_context *context)
1118 diagnostic_info diagnostic;
1120 if (context->lock < 3)
1121 pp_newline_and_flush (context->printer);
1123 fnotice (stderr,
1124 "Internal compiler error: Error reporting routines re-entered.\n");
1126 /* Call diagnostic_action_after_output to get the "please submit a bug
1127 report" message. It only looks at the kind field of diagnostic_info. */
1128 diagnostic.kind = DK_ICE;
1129 diagnostic_action_after_output (context, &diagnostic);
1131 /* Do not use gcc_unreachable here; that goes through internal_error
1132 and therefore would cause infinite recursion. */
1133 real_abort ();
1136 /* Report an internal compiler error in a friendly manner. This is
1137 the function that gets called upon use of abort() in the source
1138 code generally, thanks to a special macro. */
1140 void
1141 fancy_abort (const char *file, int line, const char *function)
1143 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1146 /* Really call the system 'abort'. This has to go right at the end of
1147 this file, so that there are no functions after it that call abort
1148 and get the system abort instead of our macro. */
1149 #undef abort
1150 static void
1151 real_abort (void)
1153 abort ();