* config/avr/avr.md: Fix indentations of insn C snippets.
[official-gcc.git] / gcc / diagnostic.c
blobb2ba901cfe456a27bfbc0d41f02ba9dea3a848e3
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 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,
523 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
525 pp_destroy_prefix (context->printer);
528 /* Interface to specify diagnostic kind overrides. Returns the
529 previous setting, or DK_UNSPECIFIED if the parameters are out of
530 range. */
531 diagnostic_t
532 diagnostic_classify_diagnostic (diagnostic_context *context,
533 int option_index,
534 diagnostic_t new_kind,
535 location_t where)
537 diagnostic_t old_kind;
539 if (option_index <= 0
540 || option_index >= context->n_opts
541 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
542 return DK_UNSPECIFIED;
544 old_kind = context->classify_diagnostic[option_index];
546 /* Handle pragmas separately, since we need to keep track of *where*
547 the pragmas were. */
548 if (where != UNKNOWN_LOCATION)
550 int i;
552 for (i = context->n_classification_history - 1; i >= 0; i --)
553 if (context->classification_history[i].option == option_index)
555 old_kind = context->classification_history[i].kind;
556 break;
559 i = context->n_classification_history;
560 context->classification_history =
561 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
562 * sizeof (diagnostic_classification_change_t));
563 context->classification_history[i].location = where;
564 context->classification_history[i].option = option_index;
565 context->classification_history[i].kind = new_kind;
566 context->n_classification_history ++;
568 else
569 context->classify_diagnostic[option_index] = new_kind;
571 return old_kind;
574 /* Save all diagnostic classifications in a stack. */
575 void
576 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
578 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
579 context->push_list[context->n_push ++] = context->n_classification_history;
582 /* Restore the topmost classification set off the stack. If the stack
583 is empty, revert to the state based on command line parameters. */
584 void
585 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
587 int jump_to;
588 int i;
590 if (context->n_push)
591 jump_to = context->push_list [-- context->n_push];
592 else
593 jump_to = 0;
595 i = context->n_classification_history;
596 context->classification_history =
597 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
598 * sizeof (diagnostic_classification_change_t));
599 context->classification_history[i].location = where;
600 context->classification_history[i].option = jump_to;
601 context->classification_history[i].kind = DK_POP;
602 context->n_classification_history ++;
605 /* Report a diagnostic message (an error or a warning) as specified by
606 DC. This function is *the* subroutine in terms of which front-ends
607 should implement their specific diagnostic handling modules. The
608 front-end independent format specifiers are exactly those described
609 in the documentation of output_format.
610 Return true if a diagnostic was printed, false otherwise. */
612 bool
613 diagnostic_report_diagnostic (diagnostic_context *context,
614 diagnostic_info *diagnostic)
616 location_t location = diagnostic->location;
617 diagnostic_t orig_diag_kind = diagnostic->kind;
618 const char *saved_format_spec;
620 /* Give preference to being able to inhibit warnings, before they
621 get reclassified to something else. */
622 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
623 && !diagnostic_report_warnings_p (context, location))
624 return false;
626 if (diagnostic->kind == DK_PEDWARN)
628 diagnostic->kind = pedantic_warning_kind (context);
629 /* We do this to avoid giving the message for -pedantic-errors. */
630 orig_diag_kind = diagnostic->kind;
633 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
634 return false;
636 if (context->lock > 0)
638 /* If we're reporting an ICE in the middle of some other error,
639 try to flush out the previous error, then let this one
640 through. Don't do this more than once. */
641 if (diagnostic->kind == DK_ICE && context->lock == 1)
642 pp_newline_and_flush (context->printer);
643 else
644 error_recursion (context);
647 /* If the user requested that warnings be treated as errors, so be
648 it. Note that we do this before the next block so that
649 individual warnings can be overridden back to warnings with
650 -Wno-error=*. */
651 if (context->warning_as_error_requested
652 && diagnostic->kind == DK_WARNING)
654 diagnostic->kind = DK_ERROR;
657 if (diagnostic->option_index
658 && diagnostic->option_index != permissive_error_option (context))
660 diagnostic_t diag_class = DK_UNSPECIFIED;
662 /* This tests if the user provided the appropriate -Wfoo or
663 -Wno-foo option. */
664 if (! context->option_enabled (diagnostic->option_index,
665 context->option_state))
666 return false;
668 /* This tests for #pragma diagnostic changes. */
669 if (context->n_classification_history > 0)
671 int i;
672 /* FIXME: Stupid search. Optimize later. */
673 for (i = context->n_classification_history - 1; i >= 0; i --)
675 if (linemap_location_before_p
676 (line_table,
677 context->classification_history[i].location,
678 location))
680 if (context->classification_history[i].kind == (int) DK_POP)
682 i = context->classification_history[i].option;
683 continue;
685 if (context->classification_history[i].option == diagnostic->option_index)
687 diag_class = context->classification_history[i].kind;
688 if (diag_class != DK_UNSPECIFIED)
689 diagnostic->kind = diag_class;
690 break;
695 /* This tests if the user provided the appropriate -Werror=foo
696 option. */
697 if (diag_class == DK_UNSPECIFIED
698 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
700 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
702 /* This allows for future extensions, like temporarily disabling
703 warnings for ranges of source code. */
704 if (diagnostic->kind == DK_IGNORED)
705 return false;
708 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
709 context->some_warnings_are_errors = true;
711 context->lock++;
713 if (diagnostic->kind == DK_ICE)
715 #ifndef ENABLE_CHECKING
716 /* When not checking, ICEs are converted to fatal errors when an
717 error has already occurred. This is counteracted by
718 abort_on_error. */
719 if ((diagnostic_kind_count (context, DK_ERROR) > 0
720 || diagnostic_kind_count (context, DK_SORRY) > 0)
721 && !context->abort_on_error)
723 expanded_location s = expand_location (diagnostic->location);
724 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
725 s.file, s.line);
726 exit (ICE_EXIT_CODE);
728 #endif
729 if (context->internal_error)
730 (*context->internal_error) (context,
731 diagnostic->message.format_spec,
732 diagnostic->message.args_ptr);
734 ++diagnostic_kind_count (context, diagnostic->kind);
736 saved_format_spec = diagnostic->message.format_spec;
737 if (context->show_option_requested)
739 char *option_text;
741 option_text = context->option_name (context, diagnostic->option_index,
742 orig_diag_kind, diagnostic->kind);
744 if (option_text)
746 diagnostic->message.format_spec
747 = ACONCAT ((diagnostic->message.format_spec,
748 " ",
749 "[", option_text, "]",
750 NULL));
751 free (option_text);
754 diagnostic->message.locus = &diagnostic->location;
755 diagnostic->message.x_data = &diagnostic->x_data;
756 diagnostic->x_data = NULL;
757 pp_format (context->printer, &diagnostic->message);
758 (*diagnostic_starter (context)) (context, diagnostic);
759 pp_output_formatted_text (context->printer);
760 diagnostic_show_locus (context, diagnostic);
761 (*diagnostic_finalizer (context)) (context, diagnostic);
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 bool
825 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
826 const char *gmsgid, ...)
828 diagnostic_info diagnostic;
829 va_list ap;
830 bool ret;
832 va_start (ap, gmsgid);
833 if (kind == DK_PERMERROR)
835 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
836 permissive_error_kind (global_dc));
837 diagnostic.option_index = permissive_error_option (global_dc);
839 else {
840 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
841 if (kind == DK_WARNING || kind == DK_PEDWARN)
842 diagnostic.option_index = opt;
845 ret = report_diagnostic (&diagnostic);
846 va_end (ap);
847 return ret;
850 /* An informative note at LOCATION. Use this for additional details on an error
851 message. */
852 void
853 inform (location_t location, const char *gmsgid, ...)
855 diagnostic_info diagnostic;
856 va_list ap;
858 va_start (ap, gmsgid);
859 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
860 report_diagnostic (&diagnostic);
861 va_end (ap);
864 /* An informative note at LOCATION. Use this for additional details on an
865 error message. */
866 void
867 inform_n (location_t location, int n, const char *singular_gmsgid,
868 const char *plural_gmsgid, ...)
870 diagnostic_info diagnostic;
871 va_list ap;
873 va_start (ap, plural_gmsgid);
874 diagnostic_set_info_translated (&diagnostic,
875 ngettext (singular_gmsgid, plural_gmsgid, n),
876 &ap, location, DK_NOTE);
877 report_diagnostic (&diagnostic);
878 va_end (ap);
881 /* A warning at INPUT_LOCATION. Use this for code which is correct according
882 to the relevant language specification but is likely to be buggy anyway.
883 Returns true if the warning was printed, false if it was inhibited. */
884 bool
885 warning (int opt, const char *gmsgid, ...)
887 diagnostic_info diagnostic;
888 va_list ap;
889 bool ret;
891 va_start (ap, gmsgid);
892 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
893 diagnostic.option_index = opt;
895 ret = report_diagnostic (&diagnostic);
896 va_end (ap);
897 return ret;
900 /* A warning at LOCATION. Use this for code which is correct according to the
901 relevant language specification but is likely to be buggy anyway.
902 Returns true if the warning was printed, false if it was inhibited. */
904 bool
905 warning_at (location_t location, int opt, const char *gmsgid, ...)
907 diagnostic_info diagnostic;
908 va_list ap;
909 bool ret;
911 va_start (ap, gmsgid);
912 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
913 diagnostic.option_index = opt;
914 ret = report_diagnostic (&diagnostic);
915 va_end (ap);
916 return ret;
919 /* A "pedantic" warning at LOCATION: issues a warning unless
920 -pedantic-errors was given on the command line, in which case it
921 issues an error. Use this for diagnostics required by the relevant
922 language standard, if you have chosen not to make them errors.
924 Note that these diagnostics are issued independent of the setting
925 of the -Wpedantic command-line switch. To get a warning enabled
926 only with that switch, use either "if (pedantic) pedwarn
927 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
928 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
930 Returns true if the warning was printed, false if it was inhibited. */
932 bool
933 pedwarn (location_t location, int opt, const char *gmsgid, ...)
935 diagnostic_info diagnostic;
936 va_list ap;
937 bool ret;
939 va_start (ap, gmsgid);
940 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
941 diagnostic.option_index = opt;
942 ret = report_diagnostic (&diagnostic);
943 va_end (ap);
944 return ret;
947 /* A "permissive" error at LOCATION: issues an error unless
948 -fpermissive was given on the command line, in which case it issues
949 a warning. Use this for things that really should be errors but we
950 want to support legacy code.
952 Returns true if the warning was printed, false if it was inhibited. */
954 bool
955 permerror (location_t location, const char *gmsgid, ...)
957 diagnostic_info diagnostic;
958 va_list ap;
959 bool ret;
961 va_start (ap, gmsgid);
962 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
963 permissive_error_kind (global_dc));
964 diagnostic.option_index = permissive_error_option (global_dc);
965 ret = report_diagnostic (&diagnostic);
966 va_end (ap);
967 return ret;
970 /* A hard error: the code is definitely ill-formed, and an object file
971 will not be produced. */
972 void
973 error (const char *gmsgid, ...)
975 diagnostic_info diagnostic;
976 va_list ap;
978 va_start (ap, gmsgid);
979 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
980 report_diagnostic (&diagnostic);
981 va_end (ap);
984 /* A hard error: the code is definitely ill-formed, and an object file
985 will not be produced. */
986 void
987 error_n (location_t location, int n, const char *singular_gmsgid,
988 const char *plural_gmsgid, ...)
990 diagnostic_info diagnostic;
991 va_list ap;
993 va_start (ap, plural_gmsgid);
994 diagnostic_set_info_translated (&diagnostic,
995 ngettext (singular_gmsgid, plural_gmsgid, n),
996 &ap, location, DK_ERROR);
997 report_diagnostic (&diagnostic);
998 va_end (ap);
1001 /* Same as ebove, but use location LOC instead of input_location. */
1002 void
1003 error_at (location_t loc, const char *gmsgid, ...)
1005 diagnostic_info diagnostic;
1006 va_list ap;
1008 va_start (ap, gmsgid);
1009 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1010 report_diagnostic (&diagnostic);
1011 va_end (ap);
1014 /* "Sorry, not implemented." Use for a language feature which is
1015 required by the relevant specification but not implemented by GCC.
1016 An object file will not be produced. */
1017 void
1018 sorry (const char *gmsgid, ...)
1020 diagnostic_info diagnostic;
1021 va_list ap;
1023 va_start (ap, gmsgid);
1024 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1025 report_diagnostic (&diagnostic);
1026 va_end (ap);
1029 /* Return true if an error or a "sorry" has been seen. Various
1030 processing is disabled after errors. */
1031 bool
1032 seen_error (void)
1034 return errorcount || sorrycount;
1037 /* An error which is severe enough that we make no attempt to
1038 continue. Do not use this for internal consistency checks; that's
1039 internal_error. Use of this function should be rare. */
1040 void
1041 fatal_error (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_FATAL);
1048 report_diagnostic (&diagnostic);
1049 va_end (ap);
1051 gcc_unreachable ();
1054 /* An internal consistency check has failed. We make no attempt to
1055 continue. Note that unless there is debugging value to be had from
1056 a more specific message, or some other good reason, you should use
1057 abort () instead of calling this function directly. */
1058 void
1059 internal_error (const char *gmsgid, ...)
1061 diagnostic_info diagnostic;
1062 va_list ap;
1064 va_start (ap, gmsgid);
1065 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1066 report_diagnostic (&diagnostic);
1067 va_end (ap);
1069 gcc_unreachable ();
1072 /* Special case error functions. Most are implemented in terms of the
1073 above, or should be. */
1075 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1076 runs its second argument through gettext. */
1077 void
1078 fnotice (FILE *file, const char *cmsgid, ...)
1080 va_list ap;
1082 va_start (ap, cmsgid);
1083 vfprintf (file, _(cmsgid), ap);
1084 va_end (ap);
1087 /* Inform the user that an error occurred while trying to report some
1088 other error. This indicates catastrophic internal inconsistencies,
1089 so give up now. But do try to flush out the previous error.
1090 This mustn't use internal_error, that will cause infinite recursion. */
1092 static void
1093 error_recursion (diagnostic_context *context)
1095 diagnostic_info diagnostic;
1097 if (context->lock < 3)
1098 pp_newline_and_flush (context->printer);
1100 fnotice (stderr,
1101 "Internal compiler error: Error reporting routines re-entered.\n");
1103 /* Call diagnostic_action_after_output to get the "please submit a bug
1104 report" message. It only looks at the kind field of diagnostic_info. */
1105 diagnostic.kind = DK_ICE;
1106 diagnostic_action_after_output (context, &diagnostic);
1108 /* Do not use gcc_unreachable here; that goes through internal_error
1109 and therefore would cause infinite recursion. */
1110 real_abort ();
1113 /* Report an internal compiler error in a friendly manner. This is
1114 the function that gets called upon use of abort() in the source
1115 code generally, thanks to a special macro. */
1117 void
1118 fancy_abort (const char *file, int line, const char *function)
1120 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1123 /* Really call the system 'abort'. This has to go right at the end of
1124 this file, so that there are no functions after it that call abort
1125 and get the system abort instead of our macro. */
1126 #undef abort
1127 static void
1128 real_abort (void)
1130 abort ();