gcc/
[official-gcc.git] / gcc / diagnostic.c
blobb3ae86c74a8399be2785faa17b0c9c8e1230236e
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 "input.h"
31 #include "intl.h"
32 #include "diagnostic.h"
34 #define pedantic_warning_kind(DC) \
35 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
36 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
37 #define permissive_error_option(DC) ((DC)->opt_permissive)
39 /* Prototypes. */
40 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
42 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
44 static void diagnostic_action_after_output (diagnostic_context *,
45 diagnostic_info *);
46 static void real_abort (void) ATTRIBUTE_NORETURN;
48 /* Name of program invoked, sans directories. */
50 const char *progname;
52 /* A diagnostic_context surrogate for stderr. */
53 static diagnostic_context global_diagnostic_context;
54 diagnostic_context *global_dc = &global_diagnostic_context;
57 /* Return a malloc'd string containing MSG formatted a la printf. The
58 caller is responsible for freeing the memory. */
59 static char *
60 build_message_string (const char *msg, ...)
62 char *str;
63 va_list ap;
65 va_start (ap, msg);
66 vasprintf (&str, msg, ap);
67 va_end (ap);
69 return str;
72 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
73 char *
74 file_name_as_prefix (const char *f)
76 return build_message_string ("%s: ", f);
81 /* Return the value of the getenv("COLUMNS") as an integer. If the
82 value is not set to a positive integer, then return INT_MAX. */
83 static int
84 getenv_columns (void)
86 const char * s = getenv ("COLUMNS");
87 if (s != NULL) {
88 int n = atoi (s);
89 if (n > 0)
90 return n;
92 return INT_MAX;
95 /* Set caret_max_width to value. */
96 void
97 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
99 /* One minus to account for the leading empty space. */
100 value = value ? value - 1
101 : (isatty (fileno (context->printer->buffer->stream))
102 ? getenv_columns () - 1: INT_MAX);
104 if (value <= 0)
105 value = INT_MAX;
107 context->caret_max_width = value;
110 /* Initialize the diagnostic message outputting machinery. */
111 void
112 diagnostic_initialize (diagnostic_context *context, int n_opts)
114 int i;
116 /* Allocate a basic pretty-printer. Clients will replace this a
117 much more elaborated pretty-printer if they wish. */
118 context->printer = XNEW (pretty_printer);
119 pp_construct (context->printer, NULL, 0);
120 /* By default, diagnostics are sent to stderr. */
121 context->printer->buffer->stream = stderr;
122 /* By default, we emit prefixes once per message. */
123 context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
125 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
126 context->some_warnings_are_errors = false;
127 context->warning_as_error_requested = false;
128 context->n_opts = n_opts;
129 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
130 for (i = 0; i < n_opts; i++)
131 context->classify_diagnostic[i] = DK_UNSPECIFIED;
132 context->show_caret = false;
133 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
134 context->show_option_requested = false;
135 context->abort_on_error = false;
136 context->show_column = false;
137 context->pedantic_errors = false;
138 context->permissive = false;
139 context->opt_permissive = 0;
140 context->fatal_errors = false;
141 context->dc_inhibit_warnings = false;
142 context->dc_warn_system_headers = false;
143 context->max_errors = 0;
144 context->internal_error = NULL;
145 diagnostic_starter (context) = default_diagnostic_starter;
146 diagnostic_finalizer (context) = default_diagnostic_finalizer;
147 context->option_enabled = NULL;
148 context->option_state = NULL;
149 context->option_name = NULL;
150 context->last_location = UNKNOWN_LOCATION;
151 context->last_module = 0;
152 context->x_data = NULL;
153 context->lock = 0;
154 context->inhibit_notes_p = false;
157 /* Do any cleaning up required after the last diagnostic is emitted. */
159 void
160 diagnostic_finish (diagnostic_context *context)
162 /* Some of the errors may actually have been warnings. */
163 if (context->some_warnings_are_errors)
165 /* -Werror was given. */
166 if (context->warning_as_error_requested)
167 pp_verbatim (context->printer,
168 _("%s: all warnings being treated as errors"),
169 progname);
170 /* At least one -Werror= was given. */
171 else
172 pp_verbatim (context->printer,
173 _("%s: some warnings being treated as errors"),
174 progname);
175 pp_newline_and_flush (context->printer);
179 /* Initialize DIAGNOSTIC, where the message MSG has already been
180 translated. */
181 void
182 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
183 va_list *args, location_t location,
184 diagnostic_t kind)
186 diagnostic->message.err_no = errno;
187 diagnostic->message.args_ptr = args;
188 diagnostic->message.format_spec = msg;
189 diagnostic->location = location;
190 diagnostic->override_column = 0;
191 diagnostic->kind = kind;
192 diagnostic->option_index = 0;
195 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
196 translated. */
197 void
198 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
199 va_list *args, location_t location,
200 diagnostic_t kind)
202 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
205 /* Return a malloc'd string describing a location. The caller is
206 responsible for freeing the memory. */
207 char *
208 diagnostic_build_prefix (diagnostic_context *context,
209 diagnostic_info *diagnostic)
211 static const char *const diagnostic_kind_text[] = {
212 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
213 #include "diagnostic.def"
214 #undef DEFINE_DIAGNOSTIC_KIND
215 "must-not-happen"
217 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
218 expanded_location s = expand_location_to_spelling_point (diagnostic->location);
219 if (diagnostic->override_column)
220 s.column = diagnostic->override_column;
221 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
223 return
224 (s.file == NULL
225 ? build_message_string ("%s: %s", progname, text)
226 : context->show_column
227 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
228 : build_message_string ("%s:%d: %s", s.file, s.line, text));
231 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
232 MAX_WIDTH by some margin, then adjust the start of the line such
233 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
234 margin is either 10 characters or the difference between the column
235 and the length of the line, whatever is smaller. */
236 static const char *
237 adjust_line (const char *line, int max_width, int *column_p)
239 int right_margin = 10;
240 int line_width = strlen (line);
241 int column = *column_p;
243 right_margin = MIN(line_width - column, right_margin);
244 right_margin = max_width - right_margin;
245 if (line_width >= max_width && column > right_margin)
247 line += column - right_margin;
248 *column_p = right_margin;
250 return line;
253 /* Print the physical source line corresponding to the location of
254 this diagnostics, and a caret indicating the precise column. */
255 void
256 diagnostic_show_locus (diagnostic_context * context,
257 const diagnostic_info *diagnostic)
259 const char *line;
260 char *buffer;
261 expanded_location s;
262 int max_width;
263 const char *saved_prefix;
266 if (!context->show_caret
267 || diagnostic->location <= BUILTINS_LOCATION
268 || diagnostic->location == context->last_location)
269 return;
271 context->last_location = diagnostic->location;
272 s = expand_location_to_spelling_point (diagnostic->location);
273 line = location_get_source_line (s);
274 if (line == NULL)
275 return;
277 max_width = context->caret_max_width;
278 line = adjust_line (line, max_width, &(s.column));
280 pp_newline (context->printer);
281 saved_prefix = pp_get_prefix (context->printer);
282 pp_set_prefix (context->printer, NULL);
283 pp_character (context->printer, ' ');
284 while (max_width > 0 && *line != '\0')
286 char c = *line == '\t' ? ' ' : *line;
287 pp_character (context->printer, c);
288 max_width--;
289 line++;
291 pp_newline (context->printer);
292 /* pp_printf does not implement %*c. */
293 buffer = XALLOCAVEC (char, s.column + 3);
294 snprintf (buffer, s.column + 3, " %*c", s.column, '^');
295 pp_string (context->printer, buffer);
296 pp_set_prefix (context->printer, saved_prefix);
299 /* Take any action which is expected to happen after the diagnostic
300 is written out. This function does not always return. */
301 static void
302 diagnostic_action_after_output (diagnostic_context *context,
303 diagnostic_info *diagnostic)
305 switch (diagnostic->kind)
307 case DK_DEBUG:
308 case DK_NOTE:
309 case DK_ANACHRONISM:
310 case DK_WARNING:
311 break;
313 case DK_ERROR:
314 case DK_SORRY:
315 if (context->abort_on_error)
316 real_abort ();
317 if (context->fatal_errors)
319 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
320 diagnostic_finish (context);
321 exit (FATAL_EXIT_CODE);
323 if (context->max_errors != 0
324 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
325 + diagnostic_kind_count (context, DK_SORRY))
326 >= context->max_errors))
328 fnotice (stderr,
329 "compilation terminated due to -fmax-errors=%u.\n",
330 context->max_errors);
331 diagnostic_finish (context);
332 exit (FATAL_EXIT_CODE);
334 break;
336 case DK_ICE:
337 if (context->abort_on_error)
338 real_abort ();
340 fnotice (stderr, "Please submit a full bug report,\n"
341 "with preprocessed source if appropriate.\n"
342 "See %s for instructions.\n", bug_report_url);
343 exit (ICE_EXIT_CODE);
345 case DK_FATAL:
346 if (context->abort_on_error)
347 real_abort ();
348 diagnostic_finish (context);
349 fnotice (stderr, "compilation terminated.\n");
350 exit (FATAL_EXIT_CODE);
352 default:
353 gcc_unreachable ();
357 void
358 diagnostic_report_current_module (diagnostic_context *context, location_t where)
360 const struct line_map *map = NULL;
362 if (pp_needs_newline (context->printer))
364 pp_newline (context->printer);
365 pp_needs_newline (context->printer) = false;
368 if (where <= BUILTINS_LOCATION)
369 return;
371 linemap_resolve_location (line_table, where,
372 LRK_MACRO_DEFINITION_LOCATION,
373 &map);
375 if (map && diagnostic_last_module_changed (context, map))
377 diagnostic_set_last_module (context, map);
378 if (! MAIN_FILE_P (map))
380 map = INCLUDED_FROM (line_table, map);
381 if (context->show_column)
382 pp_verbatim (context->printer,
383 "In file included from %s:%d:%d",
384 LINEMAP_FILE (map),
385 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
386 else
387 pp_verbatim (context->printer,
388 "In file included from %s:%d",
389 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
390 while (! MAIN_FILE_P (map))
392 map = INCLUDED_FROM (line_table, map);
393 pp_verbatim (context->printer,
394 ",\n from %s:%d",
395 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
397 pp_verbatim (context->printer, ":");
398 pp_newline (context->printer);
403 void
404 default_diagnostic_starter (diagnostic_context *context,
405 diagnostic_info *diagnostic)
407 diagnostic_report_current_module (context, diagnostic->location);
408 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
409 diagnostic));
412 void
413 default_diagnostic_finalizer (diagnostic_context *context,
414 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
416 pp_destroy_prefix (context->printer);
419 /* Interface to specify diagnostic kind overrides. Returns the
420 previous setting, or DK_UNSPECIFIED if the parameters are out of
421 range. */
422 diagnostic_t
423 diagnostic_classify_diagnostic (diagnostic_context *context,
424 int option_index,
425 diagnostic_t new_kind,
426 location_t where)
428 diagnostic_t old_kind;
430 if (option_index <= 0
431 || option_index >= context->n_opts
432 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
433 return DK_UNSPECIFIED;
435 old_kind = context->classify_diagnostic[option_index];
437 /* Handle pragmas separately, since we need to keep track of *where*
438 the pragmas were. */
439 if (where != UNKNOWN_LOCATION)
441 int i;
443 for (i = context->n_classification_history - 1; i >= 0; i --)
444 if (context->classification_history[i].option == option_index)
446 old_kind = context->classification_history[i].kind;
447 break;
450 i = context->n_classification_history;
451 context->classification_history =
452 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
453 * sizeof (diagnostic_classification_change_t));
454 context->classification_history[i].location = where;
455 context->classification_history[i].option = option_index;
456 context->classification_history[i].kind = new_kind;
457 context->n_classification_history ++;
459 else
460 context->classify_diagnostic[option_index] = new_kind;
462 return old_kind;
465 /* Save all diagnostic classifications in a stack. */
466 void
467 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
469 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
470 context->push_list[context->n_push ++] = context->n_classification_history;
473 /* Restore the topmost classification set off the stack. If the stack
474 is empty, revert to the state based on command line parameters. */
475 void
476 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
478 int jump_to;
479 int i;
481 if (context->n_push)
482 jump_to = context->push_list [-- context->n_push];
483 else
484 jump_to = 0;
486 i = context->n_classification_history;
487 context->classification_history =
488 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
489 * sizeof (diagnostic_classification_change_t));
490 context->classification_history[i].location = where;
491 context->classification_history[i].option = jump_to;
492 context->classification_history[i].kind = DK_POP;
493 context->n_classification_history ++;
496 /* Report a diagnostic message (an error or a warning) as specified by
497 DC. This function is *the* subroutine in terms of which front-ends
498 should implement their specific diagnostic handling modules. The
499 front-end independent format specifiers are exactly those described
500 in the documentation of output_format.
501 Return true if a diagnostic was printed, false otherwise. */
503 bool
504 diagnostic_report_diagnostic (diagnostic_context *context,
505 diagnostic_info *diagnostic)
507 location_t location = diagnostic->location;
508 diagnostic_t orig_diag_kind = diagnostic->kind;
509 const char *saved_format_spec;
511 /* Give preference to being able to inhibit warnings, before they
512 get reclassified to something else. */
513 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
514 && !diagnostic_report_warnings_p (context, location))
515 return false;
517 if (diagnostic->kind == DK_PEDWARN)
519 diagnostic->kind = pedantic_warning_kind (context);
520 /* We do this to avoid giving the message for -pedantic-errors. */
521 orig_diag_kind = diagnostic->kind;
524 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
525 return false;
527 if (context->lock > 0)
529 /* If we're reporting an ICE in the middle of some other error,
530 try to flush out the previous error, then let this one
531 through. Don't do this more than once. */
532 if (diagnostic->kind == DK_ICE && context->lock == 1)
533 pp_newline_and_flush (context->printer);
534 else
535 error_recursion (context);
538 /* If the user requested that warnings be treated as errors, so be
539 it. Note that we do this before the next block so that
540 individual warnings can be overridden back to warnings with
541 -Wno-error=*. */
542 if (context->warning_as_error_requested
543 && diagnostic->kind == DK_WARNING)
545 diagnostic->kind = DK_ERROR;
548 if (diagnostic->option_index
549 && diagnostic->option_index != permissive_error_option (context))
551 diagnostic_t diag_class = DK_UNSPECIFIED;
553 /* This tests if the user provided the appropriate -Wfoo or
554 -Wno-foo option. */
555 if (! context->option_enabled (diagnostic->option_index,
556 context->option_state))
557 return false;
559 /* This tests for #pragma diagnostic changes. */
560 if (context->n_classification_history > 0)
562 int i;
563 /* FIXME: Stupid search. Optimize later. */
564 for (i = context->n_classification_history - 1; i >= 0; i --)
566 if (linemap_location_before_p
567 (line_table,
568 context->classification_history[i].location,
569 location))
571 if (context->classification_history[i].kind == (int) DK_POP)
573 i = context->classification_history[i].option;
574 continue;
576 if (context->classification_history[i].option == diagnostic->option_index)
578 diag_class = context->classification_history[i].kind;
579 if (diag_class != DK_UNSPECIFIED)
580 diagnostic->kind = diag_class;
581 break;
586 /* This tests if the user provided the appropriate -Werror=foo
587 option. */
588 if (diag_class == DK_UNSPECIFIED
589 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
591 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
593 /* This allows for future extensions, like temporarily disabling
594 warnings for ranges of source code. */
595 if (diagnostic->kind == DK_IGNORED)
596 return false;
599 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
600 context->some_warnings_are_errors = true;
602 context->lock++;
604 if (diagnostic->kind == DK_ICE)
606 #ifndef ENABLE_CHECKING
607 /* When not checking, ICEs are converted to fatal errors when an
608 error has already occurred. This is counteracted by
609 abort_on_error. */
610 if ((diagnostic_kind_count (context, DK_ERROR) > 0
611 || diagnostic_kind_count (context, DK_SORRY) > 0)
612 && !context->abort_on_error)
614 expanded_location s = expand_location (diagnostic->location);
615 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
616 s.file, s.line);
617 exit (ICE_EXIT_CODE);
619 #endif
620 if (context->internal_error)
621 (*context->internal_error) (context,
622 diagnostic->message.format_spec,
623 diagnostic->message.args_ptr);
625 ++diagnostic_kind_count (context, diagnostic->kind);
627 saved_format_spec = diagnostic->message.format_spec;
628 if (context->show_option_requested)
630 char *option_text;
632 option_text = context->option_name (context, diagnostic->option_index,
633 orig_diag_kind, diagnostic->kind);
635 if (option_text)
637 diagnostic->message.format_spec
638 = ACONCAT ((diagnostic->message.format_spec,
639 " ",
640 "[", option_text, "]",
641 NULL));
642 free (option_text);
645 diagnostic->message.locus = &diagnostic->location;
646 diagnostic->message.x_data = &diagnostic->x_data;
647 diagnostic->x_data = NULL;
648 pp_format (context->printer, &diagnostic->message);
649 (*diagnostic_starter (context)) (context, diagnostic);
650 pp_output_formatted_text (context->printer);
651 diagnostic_show_locus (context, diagnostic);
652 (*diagnostic_finalizer (context)) (context, diagnostic);
653 pp_newline_and_flush (context->printer);
654 diagnostic_action_after_output (context, diagnostic);
655 diagnostic->message.format_spec = saved_format_spec;
656 diagnostic->x_data = NULL;
658 context->lock--;
660 return true;
663 /* Given a partial pathname as input, return another pathname that
664 shares no directory elements with the pathname of __FILE__. This
665 is used by fancy_abort() to print `Internal compiler error in expr.c'
666 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
668 const char *
669 trim_filename (const char *name)
671 static const char this_file[] = __FILE__;
672 const char *p = name, *q = this_file;
674 /* First skip any "../" in each filename. This allows us to give a proper
675 reference to a file in a subdirectory. */
676 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
677 p += 3;
679 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
680 q += 3;
682 /* Now skip any parts the two filenames have in common. */
683 while (*p == *q && *p != 0 && *q != 0)
684 p++, q++;
686 /* Now go backwards until the previous directory separator. */
687 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
688 p--;
690 return p;
693 /* Standard error reporting routines in increasing order of severity.
694 All of these take arguments like printf. */
696 /* Text to be emitted verbatim to the error message stream; this
697 produces no prefix and disables line-wrapping. Use rarely. */
698 void
699 verbatim (const char *gmsgid, ...)
701 text_info text;
702 va_list ap;
704 va_start (ap, gmsgid);
705 text.err_no = errno;
706 text.args_ptr = &ap;
707 text.format_spec = _(gmsgid);
708 text.locus = NULL;
709 text.x_data = NULL;
710 pp_format_verbatim (global_dc->printer, &text);
711 pp_newline_and_flush (global_dc->printer);
712 va_end (ap);
715 bool
716 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
717 const char *gmsgid, ...)
719 diagnostic_info diagnostic;
720 va_list ap;
721 bool ret;
723 va_start (ap, gmsgid);
724 if (kind == DK_PERMERROR)
726 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
727 permissive_error_kind (global_dc));
728 diagnostic.option_index = permissive_error_option (global_dc);
730 else {
731 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
732 if (kind == DK_WARNING || kind == DK_PEDWARN)
733 diagnostic.option_index = opt;
736 ret = report_diagnostic (&diagnostic);
737 va_end (ap);
738 return ret;
741 /* An informative note at LOCATION. Use this for additional details on an error
742 message. */
743 void
744 inform (location_t location, const char *gmsgid, ...)
746 diagnostic_info diagnostic;
747 va_list ap;
749 va_start (ap, gmsgid);
750 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
751 report_diagnostic (&diagnostic);
752 va_end (ap);
755 /* An informative note at LOCATION. Use this for additional details on an
756 error message. */
757 void
758 inform_n (location_t location, int n, const char *singular_gmsgid,
759 const char *plural_gmsgid, ...)
761 diagnostic_info diagnostic;
762 va_list ap;
764 va_start (ap, plural_gmsgid);
765 diagnostic_set_info_translated (&diagnostic,
766 ngettext (singular_gmsgid, plural_gmsgid, n),
767 &ap, location, DK_NOTE);
768 report_diagnostic (&diagnostic);
769 va_end (ap);
772 /* A warning at INPUT_LOCATION. Use this for code which is correct according
773 to the relevant language specification but is likely to be buggy anyway.
774 Returns true if the warning was printed, false if it was inhibited. */
775 bool
776 warning (int opt, const char *gmsgid, ...)
778 diagnostic_info diagnostic;
779 va_list ap;
780 bool ret;
782 va_start (ap, gmsgid);
783 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
784 diagnostic.option_index = opt;
786 ret = report_diagnostic (&diagnostic);
787 va_end (ap);
788 return ret;
791 /* A warning at LOCATION. Use this for code which is correct according to the
792 relevant language specification but is likely to be buggy anyway.
793 Returns true if the warning was printed, false if it was inhibited. */
795 bool
796 warning_at (location_t location, int opt, const char *gmsgid, ...)
798 diagnostic_info diagnostic;
799 va_list ap;
800 bool ret;
802 va_start (ap, gmsgid);
803 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
804 diagnostic.option_index = opt;
805 ret = report_diagnostic (&diagnostic);
806 va_end (ap);
807 return ret;
810 /* A "pedantic" warning at LOCATION: issues a warning unless
811 -pedantic-errors was given on the command line, in which case it
812 issues an error. Use this for diagnostics required by the relevant
813 language standard, if you have chosen not to make them errors.
815 Note that these diagnostics are issued independent of the setting
816 of the -Wpedantic command-line switch. To get a warning enabled
817 only with that switch, use either "if (pedantic) pedwarn
818 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
819 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
821 Returns true if the warning was printed, false if it was inhibited. */
823 bool
824 pedwarn (location_t location, int opt, const char *gmsgid, ...)
826 diagnostic_info diagnostic;
827 va_list ap;
828 bool ret;
830 va_start (ap, gmsgid);
831 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
832 diagnostic.option_index = opt;
833 ret = report_diagnostic (&diagnostic);
834 va_end (ap);
835 return ret;
838 /* A "permissive" error at LOCATION: issues an error unless
839 -fpermissive was given on the command line, in which case it issues
840 a warning. Use this for things that really should be errors but we
841 want to support legacy code.
843 Returns true if the warning was printed, false if it was inhibited. */
845 bool
846 permerror (location_t location, const char *gmsgid, ...)
848 diagnostic_info diagnostic;
849 va_list ap;
850 bool ret;
852 va_start (ap, gmsgid);
853 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
854 permissive_error_kind (global_dc));
855 diagnostic.option_index = permissive_error_option (global_dc);
856 ret = report_diagnostic (&diagnostic);
857 va_end (ap);
858 return ret;
861 /* A hard error: the code is definitely ill-formed, and an object file
862 will not be produced. */
863 void
864 error (const char *gmsgid, ...)
866 diagnostic_info diagnostic;
867 va_list ap;
869 va_start (ap, gmsgid);
870 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
871 report_diagnostic (&diagnostic);
872 va_end (ap);
875 /* A hard error: the code is definitely ill-formed, and an object file
876 will not be produced. */
877 void
878 error_n (location_t location, int n, const char *singular_gmsgid,
879 const char *plural_gmsgid, ...)
881 diagnostic_info diagnostic;
882 va_list ap;
884 va_start (ap, plural_gmsgid);
885 diagnostic_set_info_translated (&diagnostic,
886 ngettext (singular_gmsgid, plural_gmsgid, n),
887 &ap, location, DK_ERROR);
888 report_diagnostic (&diagnostic);
889 va_end (ap);
892 /* Same as ebove, but use location LOC instead of input_location. */
893 void
894 error_at (location_t loc, const char *gmsgid, ...)
896 diagnostic_info diagnostic;
897 va_list ap;
899 va_start (ap, gmsgid);
900 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
901 report_diagnostic (&diagnostic);
902 va_end (ap);
905 /* "Sorry, not implemented." Use for a language feature which is
906 required by the relevant specification but not implemented by GCC.
907 An object file will not be produced. */
908 void
909 sorry (const char *gmsgid, ...)
911 diagnostic_info diagnostic;
912 va_list ap;
914 va_start (ap, gmsgid);
915 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
916 report_diagnostic (&diagnostic);
917 va_end (ap);
920 /* Return true if an error or a "sorry" has been seen. Various
921 processing is disabled after errors. */
922 bool
923 seen_error (void)
925 return errorcount || sorrycount;
928 /* An error which is severe enough that we make no attempt to
929 continue. Do not use this for internal consistency checks; that's
930 internal_error. Use of this function should be rare. */
931 void
932 fatal_error (const char *gmsgid, ...)
934 diagnostic_info diagnostic;
935 va_list ap;
937 va_start (ap, gmsgid);
938 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
939 report_diagnostic (&diagnostic);
940 va_end (ap);
942 gcc_unreachable ();
945 /* An internal consistency check has failed. We make no attempt to
946 continue. Note that unless there is debugging value to be had from
947 a more specific message, or some other good reason, you should use
948 abort () instead of calling this function directly. */
949 void
950 internal_error (const char *gmsgid, ...)
952 diagnostic_info diagnostic;
953 va_list ap;
955 va_start (ap, gmsgid);
956 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
957 report_diagnostic (&diagnostic);
958 va_end (ap);
960 gcc_unreachable ();
963 /* Special case error functions. Most are implemented in terms of the
964 above, or should be. */
966 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
967 runs its second argument through gettext. */
968 void
969 fnotice (FILE *file, const char *cmsgid, ...)
971 va_list ap;
973 va_start (ap, cmsgid);
974 vfprintf (file, _(cmsgid), ap);
975 va_end (ap);
978 /* Inform the user that an error occurred while trying to report some
979 other error. This indicates catastrophic internal inconsistencies,
980 so give up now. But do try to flush out the previous error.
981 This mustn't use internal_error, that will cause infinite recursion. */
983 static void
984 error_recursion (diagnostic_context *context)
986 diagnostic_info diagnostic;
988 if (context->lock < 3)
989 pp_newline_and_flush (context->printer);
991 fnotice (stderr,
992 "Internal compiler error: Error reporting routines re-entered.\n");
994 /* Call diagnostic_action_after_output to get the "please submit a bug
995 report" message. It only looks at the kind field of diagnostic_info. */
996 diagnostic.kind = DK_ICE;
997 diagnostic_action_after_output (context, &diagnostic);
999 /* Do not use gcc_unreachable here; that goes through internal_error
1000 and therefore would cause infinite recursion. */
1001 real_abort ();
1004 /* Report an internal compiler error in a friendly manner. This is
1005 the function that gets called upon use of abort() in the source
1006 code generally, thanks to a special macro. */
1008 void
1009 fancy_abort (const char *file, int line, const char *function)
1011 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1014 /* Really call the system 'abort'. This has to go right at the end of
1015 this file, so that there are no functions after it that call abort
1016 and get the system abort instead of our macro. */
1017 #undef abort
1018 static void
1019 real_abort (void)
1021 abort ();