2012-04-29 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / diagnostic.c
blob4496803f2ff7946f68903e650635f554a89c1026
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_module = 0;
151 context->x_data = NULL;
152 context->lock = 0;
153 context->inhibit_notes_p = false;
156 /* Do any cleaning up required after the last diagnostic is emitted. */
158 void
159 diagnostic_finish (diagnostic_context *context)
161 /* Some of the errors may actually have been warnings. */
162 if (context->some_warnings_are_errors)
164 /* -Werror was given. */
165 if (context->warning_as_error_requested)
166 pp_verbatim (context->printer,
167 _("%s: all warnings being treated as errors"),
168 progname);
169 /* At least one -Werror= was given. */
170 else
171 pp_verbatim (context->printer,
172 _("%s: some warnings being treated as errors"),
173 progname);
174 pp_flush (context->printer);
178 /* Initialize DIAGNOSTIC, where the message MSG has already been
179 translated. */
180 void
181 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
182 va_list *args, location_t location,
183 diagnostic_t kind)
185 diagnostic->message.err_no = errno;
186 diagnostic->message.args_ptr = args;
187 diagnostic->message.format_spec = msg;
188 diagnostic->location = location;
189 diagnostic->override_column = 0;
190 diagnostic->kind = kind;
191 diagnostic->option_index = 0;
194 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
195 translated. */
196 void
197 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
198 va_list *args, location_t location,
199 diagnostic_t kind)
201 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
204 /* Return a malloc'd string describing a location. The caller is
205 responsible for freeing the memory. */
206 char *
207 diagnostic_build_prefix (diagnostic_context *context,
208 diagnostic_info *diagnostic)
210 static const char *const diagnostic_kind_text[] = {
211 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
212 #include "diagnostic.def"
213 #undef DEFINE_DIAGNOSTIC_KIND
214 "must-not-happen"
216 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
217 expanded_location s = expand_location (diagnostic->location);
218 if (diagnostic->override_column)
219 s.column = diagnostic->override_column;
220 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
222 return
223 (s.file == NULL
224 ? build_message_string ("%s: %s", progname, text)
225 : context->show_column
226 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
227 : build_message_string ("%s:%d: %s", s.file, s.line, text));
230 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
231 MAX_WIDTH by some margin, then adjust the start of the line such
232 that the COLUMN is smaller than MAX_WIDTH minus the margin. The
233 margin is either 10 characters or the difference between the column
234 and the length of the line, whatever is smaller. */
235 static const char *
236 adjust_line (const char *line, int max_width, int *column_p)
238 int right_margin = 10;
239 int line_width = strlen (line);
240 int column = *column_p;
242 right_margin = MIN(line_width - column, right_margin);
243 right_margin = max_width - right_margin;
244 if (line_width >= max_width && column > right_margin)
246 line += column - right_margin;
247 *column_p = right_margin;
249 return line;
252 /* Print the physical source line corresponding to the location of
253 this diagnostics, and a caret indicating the precise column. */
254 void
255 diagnostic_show_locus (diagnostic_context * context,
256 const diagnostic_info *diagnostic)
258 const char *line;
259 char *buffer;
260 expanded_location s;
261 int max_width;
262 const char *saved_prefix;
265 if (!context->show_caret
266 || diagnostic->location <= BUILTINS_LOCATION)
267 return;
269 s = expand_location(diagnostic->location);
270 line = location_get_source_line (s);
271 if (line == NULL)
272 return;
274 max_width = context->caret_max_width;
275 line = adjust_line (line, max_width, &(s.column));
277 pp_newline (context->printer);
278 saved_prefix = pp_get_prefix (context->printer);
279 pp_set_prefix (context->printer, NULL);
280 pp_character (context->printer, ' ');
281 while (max_width > 0 && *line != '\0')
283 char c = *line == '\t' ? ' ' : *line;
284 pp_character (context->printer, c);
285 max_width--;
286 line++;
288 pp_newline (context->printer);
289 /* pp_printf does not implement %*c. */
290 buffer = XALLOCAVEC (char, s.column + 3);
291 snprintf (buffer, s.column + 3, " %*c", s.column, '^');
292 pp_string (context->printer, buffer);
293 pp_set_prefix (context->printer, saved_prefix);
296 /* Take any action which is expected to happen after the diagnostic
297 is written out. This function does not always return. */
298 static void
299 diagnostic_action_after_output (diagnostic_context *context,
300 diagnostic_info *diagnostic)
302 switch (diagnostic->kind)
304 case DK_DEBUG:
305 case DK_NOTE:
306 case DK_ANACHRONISM:
307 case DK_WARNING:
308 break;
310 case DK_ERROR:
311 case DK_SORRY:
312 if (context->abort_on_error)
313 real_abort ();
314 if (context->fatal_errors)
316 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
317 diagnostic_finish (context);
318 exit (FATAL_EXIT_CODE);
320 if (context->max_errors != 0
321 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
322 + diagnostic_kind_count (context, DK_SORRY))
323 >= context->max_errors))
325 fnotice (stderr,
326 "compilation terminated due to -fmax-errors=%u.\n",
327 context->max_errors);
328 diagnostic_finish (context);
329 exit (FATAL_EXIT_CODE);
331 break;
333 case DK_ICE:
334 if (context->abort_on_error)
335 real_abort ();
337 fnotice (stderr, "Please submit a full bug report,\n"
338 "with preprocessed source if appropriate.\n"
339 "See %s for instructions.\n", bug_report_url);
340 exit (ICE_EXIT_CODE);
342 case DK_FATAL:
343 if (context->abort_on_error)
344 real_abort ();
345 diagnostic_finish (context);
346 fnotice (stderr, "compilation terminated.\n");
347 exit (FATAL_EXIT_CODE);
349 default:
350 gcc_unreachable ();
354 void
355 diagnostic_report_current_module (diagnostic_context *context, location_t where)
357 const struct line_map *map = NULL;
359 if (pp_needs_newline (context->printer))
361 pp_newline (context->printer);
362 pp_needs_newline (context->printer) = false;
365 if (where <= BUILTINS_LOCATION)
366 return;
368 linemap_resolve_location (line_table, where,
369 LRK_MACRO_DEFINITION_LOCATION,
370 &map);
372 if (map && diagnostic_last_module_changed (context, map))
374 diagnostic_set_last_module (context, map);
375 if (! MAIN_FILE_P (map))
377 map = INCLUDED_FROM (line_table, map);
378 if (context->show_column)
379 pp_verbatim (context->printer,
380 "In file included from %s:%d:%d",
381 LINEMAP_FILE (map),
382 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
383 else
384 pp_verbatim (context->printer,
385 "In file included from %s:%d",
386 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
387 while (! MAIN_FILE_P (map))
389 map = INCLUDED_FROM (line_table, map);
390 pp_verbatim (context->printer,
391 ",\n from %s:%d",
392 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
394 pp_verbatim (context->printer, ":");
395 pp_newline (context->printer);
400 void
401 default_diagnostic_starter (diagnostic_context *context,
402 diagnostic_info *diagnostic)
404 diagnostic_report_current_module (context, diagnostic->location);
405 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
406 diagnostic));
409 void
410 default_diagnostic_finalizer (diagnostic_context *context,
411 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
413 pp_destroy_prefix (context->printer);
416 /* Interface to specify diagnostic kind overrides. Returns the
417 previous setting, or DK_UNSPECIFIED if the parameters are out of
418 range. */
419 diagnostic_t
420 diagnostic_classify_diagnostic (diagnostic_context *context,
421 int option_index,
422 diagnostic_t new_kind,
423 location_t where)
425 diagnostic_t old_kind;
427 if (option_index <= 0
428 || option_index >= context->n_opts
429 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
430 return DK_UNSPECIFIED;
432 old_kind = context->classify_diagnostic[option_index];
434 /* Handle pragmas separately, since we need to keep track of *where*
435 the pragmas were. */
436 if (where != UNKNOWN_LOCATION)
438 int i;
440 for (i = context->n_classification_history - 1; i >= 0; i --)
441 if (context->classification_history[i].option == option_index)
443 old_kind = context->classification_history[i].kind;
444 break;
447 i = context->n_classification_history;
448 context->classification_history =
449 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
450 * sizeof (diagnostic_classification_change_t));
451 context->classification_history[i].location = where;
452 context->classification_history[i].option = option_index;
453 context->classification_history[i].kind = new_kind;
454 context->n_classification_history ++;
456 else
457 context->classify_diagnostic[option_index] = new_kind;
459 return old_kind;
462 /* Save all diagnostic classifications in a stack. */
463 void
464 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
466 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
467 context->push_list[context->n_push ++] = context->n_classification_history;
470 /* Restore the topmost classification set off the stack. If the stack
471 is empty, revert to the state based on command line parameters. */
472 void
473 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
475 int jump_to;
476 int i;
478 if (context->n_push)
479 jump_to = context->push_list [-- context->n_push];
480 else
481 jump_to = 0;
483 i = context->n_classification_history;
484 context->classification_history =
485 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
486 * sizeof (diagnostic_classification_change_t));
487 context->classification_history[i].location = where;
488 context->classification_history[i].option = jump_to;
489 context->classification_history[i].kind = DK_POP;
490 context->n_classification_history ++;
493 /* Report a diagnostic message (an error or a warning) as specified by
494 DC. This function is *the* subroutine in terms of which front-ends
495 should implement their specific diagnostic handling modules. The
496 front-end independent format specifiers are exactly those described
497 in the documentation of output_format.
498 Return true if a diagnostic was printed, false otherwise. */
500 bool
501 diagnostic_report_diagnostic (diagnostic_context *context,
502 diagnostic_info *diagnostic)
504 location_t location = diagnostic->location;
505 diagnostic_t orig_diag_kind = diagnostic->kind;
506 const char *saved_format_spec;
508 /* Give preference to being able to inhibit warnings, before they
509 get reclassified to something else. */
510 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
511 && !diagnostic_report_warnings_p (context, location))
512 return false;
514 if (diagnostic->kind == DK_PEDWARN)
516 diagnostic->kind = pedantic_warning_kind (context);
517 /* We do this to avoid giving the message for -pedantic-errors. */
518 orig_diag_kind = diagnostic->kind;
521 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
522 return false;
524 if (context->lock > 0)
526 /* If we're reporting an ICE in the middle of some other error,
527 try to flush out the previous error, then let this one
528 through. Don't do this more than once. */
529 if (diagnostic->kind == DK_ICE && context->lock == 1)
530 pp_flush (context->printer);
531 else
532 error_recursion (context);
535 /* If the user requested that warnings be treated as errors, so be
536 it. Note that we do this before the next block so that
537 individual warnings can be overridden back to warnings with
538 -Wno-error=*. */
539 if (context->warning_as_error_requested
540 && diagnostic->kind == DK_WARNING)
542 diagnostic->kind = DK_ERROR;
545 if (diagnostic->option_index)
547 diagnostic_t diag_class = DK_UNSPECIFIED;
549 /* This tests if the user provided the appropriate -Wfoo or
550 -Wno-foo option. */
551 if (! context->option_enabled (diagnostic->option_index,
552 context->option_state))
553 return false;
555 /* This tests for #pragma diagnostic changes. */
556 if (context->n_classification_history > 0)
558 int i;
559 /* FIXME: Stupid search. Optimize later. */
560 for (i = context->n_classification_history - 1; i >= 0; i --)
562 if (linemap_location_before_p
563 (line_table,
564 context->classification_history[i].location,
565 location))
567 if (context->classification_history[i].kind == (int) DK_POP)
569 i = context->classification_history[i].option;
570 continue;
572 if (context->classification_history[i].option == diagnostic->option_index)
574 diag_class = context->classification_history[i].kind;
575 if (diag_class != DK_UNSPECIFIED)
576 diagnostic->kind = diag_class;
577 break;
582 /* This tests if the user provided the appropriate -Werror=foo
583 option. */
584 if (diag_class == DK_UNSPECIFIED
585 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
587 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
589 /* This allows for future extensions, like temporarily disabling
590 warnings for ranges of source code. */
591 if (diagnostic->kind == DK_IGNORED)
592 return false;
595 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
596 context->some_warnings_are_errors = true;
598 context->lock++;
600 if (diagnostic->kind == DK_ICE)
602 #ifndef ENABLE_CHECKING
603 /* When not checking, ICEs are converted to fatal errors when an
604 error has already occurred. This is counteracted by
605 abort_on_error. */
606 if ((diagnostic_kind_count (context, DK_ERROR) > 0
607 || diagnostic_kind_count (context, DK_SORRY) > 0)
608 && !context->abort_on_error)
610 expanded_location s = expand_location (diagnostic->location);
611 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
612 s.file, s.line);
613 exit (ICE_EXIT_CODE);
615 #endif
616 if (context->internal_error)
617 (*context->internal_error) (context,
618 diagnostic->message.format_spec,
619 diagnostic->message.args_ptr);
621 ++diagnostic_kind_count (context, diagnostic->kind);
623 saved_format_spec = diagnostic->message.format_spec;
624 if (context->show_option_requested)
626 char *option_text;
628 option_text = context->option_name (context, diagnostic->option_index,
629 orig_diag_kind, diagnostic->kind);
631 if (option_text)
633 diagnostic->message.format_spec
634 = ACONCAT ((diagnostic->message.format_spec,
635 " ",
636 "[", option_text, "]",
637 NULL));
638 free (option_text);
641 diagnostic->message.locus = &diagnostic->location;
642 diagnostic->message.x_data = &diagnostic->x_data;
643 diagnostic->x_data = NULL;
644 pp_format (context->printer, &diagnostic->message);
645 (*diagnostic_starter (context)) (context, diagnostic);
646 pp_output_formatted_text (context->printer);
647 diagnostic_show_locus (context, diagnostic);
648 (*diagnostic_finalizer (context)) (context, diagnostic);
649 pp_flush (context->printer);
650 diagnostic_action_after_output (context, diagnostic);
651 diagnostic->message.format_spec = saved_format_spec;
652 diagnostic->x_data = NULL;
654 context->lock--;
656 return true;
659 /* Given a partial pathname as input, return another pathname that
660 shares no directory elements with the pathname of __FILE__. This
661 is used by fancy_abort() to print `Internal compiler error in expr.c'
662 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
664 const char *
665 trim_filename (const char *name)
667 static const char this_file[] = __FILE__;
668 const char *p = name, *q = this_file;
670 /* First skip any "../" in each filename. This allows us to give a proper
671 reference to a file in a subdirectory. */
672 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
673 p += 3;
675 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
676 q += 3;
678 /* Now skip any parts the two filenames have in common. */
679 while (*p == *q && *p != 0 && *q != 0)
680 p++, q++;
682 /* Now go backwards until the previous directory separator. */
683 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
684 p--;
686 return p;
689 /* Standard error reporting routines in increasing order of severity.
690 All of these take arguments like printf. */
692 /* Text to be emitted verbatim to the error message stream; this
693 produces no prefix and disables line-wrapping. Use rarely. */
694 void
695 verbatim (const char *gmsgid, ...)
697 text_info text;
698 va_list ap;
700 va_start (ap, gmsgid);
701 text.err_no = errno;
702 text.args_ptr = &ap;
703 text.format_spec = _(gmsgid);
704 text.locus = NULL;
705 text.x_data = NULL;
706 pp_format_verbatim (global_dc->printer, &text);
707 pp_flush (global_dc->printer);
708 va_end (ap);
711 bool
712 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
713 const char *gmsgid, ...)
715 diagnostic_info diagnostic;
716 va_list ap;
717 bool ret;
719 va_start (ap, gmsgid);
720 if (kind == DK_PERMERROR)
722 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
723 permissive_error_kind (global_dc));
724 diagnostic.option_index = permissive_error_option (global_dc);
726 else {
727 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
728 if (kind == DK_WARNING || kind == DK_PEDWARN)
729 diagnostic.option_index = opt;
732 ret = report_diagnostic (&diagnostic);
733 va_end (ap);
734 return ret;
737 /* An informative note at LOCATION. Use this for additional details on an error
738 message. */
739 void
740 inform (location_t location, const char *gmsgid, ...)
742 diagnostic_info diagnostic;
743 va_list ap;
745 va_start (ap, gmsgid);
746 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
747 report_diagnostic (&diagnostic);
748 va_end (ap);
751 /* An informative note at LOCATION. Use this for additional details on an
752 error message. */
753 void
754 inform_n (location_t location, int n, const char *singular_gmsgid,
755 const char *plural_gmsgid, ...)
757 diagnostic_info diagnostic;
758 va_list ap;
760 va_start (ap, plural_gmsgid);
761 diagnostic_set_info_translated (&diagnostic,
762 ngettext (singular_gmsgid, plural_gmsgid, n),
763 &ap, location, DK_NOTE);
764 report_diagnostic (&diagnostic);
765 va_end (ap);
768 /* A warning at INPUT_LOCATION. Use this for code which is correct according
769 to the relevant language specification but is likely to be buggy anyway.
770 Returns true if the warning was printed, false if it was inhibited. */
771 bool
772 warning (int opt, const char *gmsgid, ...)
774 diagnostic_info diagnostic;
775 va_list ap;
776 bool ret;
778 va_start (ap, gmsgid);
779 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
780 diagnostic.option_index = opt;
782 ret = report_diagnostic (&diagnostic);
783 va_end (ap);
784 return ret;
787 /* A warning at LOCATION. Use this for code which is correct according to the
788 relevant language specification but is likely to be buggy anyway.
789 Returns true if the warning was printed, false if it was inhibited. */
791 bool
792 warning_at (location_t location, int opt, const char *gmsgid, ...)
794 diagnostic_info diagnostic;
795 va_list ap;
796 bool ret;
798 va_start (ap, gmsgid);
799 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
800 diagnostic.option_index = opt;
801 ret = report_diagnostic (&diagnostic);
802 va_end (ap);
803 return ret;
806 /* A "pedantic" warning at LOCATION: issues a warning unless
807 -pedantic-errors was given on the command line, in which case it
808 issues an error. Use this for diagnostics required by the relevant
809 language standard, if you have chosen not to make them errors.
811 Note that these diagnostics are issued independent of the setting
812 of the -Wpedantic command-line switch. To get a warning enabled
813 only with that switch, use either "if (pedantic) pedwarn
814 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
815 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
817 Returns true if the warning was printed, false if it was inhibited. */
819 bool
820 pedwarn (location_t location, int opt, const char *gmsgid, ...)
822 diagnostic_info diagnostic;
823 va_list ap;
824 bool ret;
826 va_start (ap, gmsgid);
827 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN);
828 diagnostic.option_index = opt;
829 ret = report_diagnostic (&diagnostic);
830 va_end (ap);
831 return ret;
834 /* A "permissive" error at LOCATION: issues an error unless
835 -fpermissive was given on the command line, in which case it issues
836 a warning. Use this for things that really should be errors but we
837 want to support legacy code.
839 Returns true if the warning was printed, false if it was inhibited. */
841 bool
842 permerror (location_t location, const char *gmsgid, ...)
844 diagnostic_info diagnostic;
845 va_list ap;
846 bool ret;
848 va_start (ap, gmsgid);
849 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
850 permissive_error_kind (global_dc));
851 diagnostic.option_index = permissive_error_option (global_dc);
852 ret = report_diagnostic (&diagnostic);
853 va_end (ap);
854 return ret;
857 /* A hard error: the code is definitely ill-formed, and an object file
858 will not be produced. */
859 void
860 error (const char *gmsgid, ...)
862 diagnostic_info diagnostic;
863 va_list ap;
865 va_start (ap, gmsgid);
866 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
867 report_diagnostic (&diagnostic);
868 va_end (ap);
871 /* A hard error: the code is definitely ill-formed, and an object file
872 will not be produced. */
873 void
874 error_n (location_t location, int n, const char *singular_gmsgid,
875 const char *plural_gmsgid, ...)
877 diagnostic_info diagnostic;
878 va_list ap;
880 va_start (ap, plural_gmsgid);
881 diagnostic_set_info_translated (&diagnostic,
882 ngettext (singular_gmsgid, plural_gmsgid, n),
883 &ap, location, DK_ERROR);
884 report_diagnostic (&diagnostic);
885 va_end (ap);
888 /* Same as ebove, but use location LOC instead of input_location. */
889 void
890 error_at (location_t loc, const char *gmsgid, ...)
892 diagnostic_info diagnostic;
893 va_list ap;
895 va_start (ap, gmsgid);
896 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
897 report_diagnostic (&diagnostic);
898 va_end (ap);
901 /* "Sorry, not implemented." Use for a language feature which is
902 required by the relevant specification but not implemented by GCC.
903 An object file will not be produced. */
904 void
905 sorry (const char *gmsgid, ...)
907 diagnostic_info diagnostic;
908 va_list ap;
910 va_start (ap, gmsgid);
911 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
912 report_diagnostic (&diagnostic);
913 va_end (ap);
916 /* Return true if an error or a "sorry" has been seen. Various
917 processing is disabled after errors. */
918 bool
919 seen_error (void)
921 return errorcount || sorrycount;
924 /* An error which is severe enough that we make no attempt to
925 continue. Do not use this for internal consistency checks; that's
926 internal_error. Use of this function should be rare. */
927 void
928 fatal_error (const char *gmsgid, ...)
930 diagnostic_info diagnostic;
931 va_list ap;
933 va_start (ap, gmsgid);
934 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
935 report_diagnostic (&diagnostic);
936 va_end (ap);
938 gcc_unreachable ();
941 /* An internal consistency check has failed. We make no attempt to
942 continue. Note that unless there is debugging value to be had from
943 a more specific message, or some other good reason, you should use
944 abort () instead of calling this function directly. */
945 void
946 internal_error (const char *gmsgid, ...)
948 diagnostic_info diagnostic;
949 va_list ap;
951 va_start (ap, gmsgid);
952 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
953 report_diagnostic (&diagnostic);
954 va_end (ap);
956 gcc_unreachable ();
959 /* Special case error functions. Most are implemented in terms of the
960 above, or should be. */
962 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
963 runs its second argument through gettext. */
964 void
965 fnotice (FILE *file, const char *cmsgid, ...)
967 va_list ap;
969 va_start (ap, cmsgid);
970 vfprintf (file, _(cmsgid), ap);
971 va_end (ap);
974 /* Inform the user that an error occurred while trying to report some
975 other error. This indicates catastrophic internal inconsistencies,
976 so give up now. But do try to flush out the previous error.
977 This mustn't use internal_error, that will cause infinite recursion. */
979 static void
980 error_recursion (diagnostic_context *context)
982 diagnostic_info diagnostic;
984 if (context->lock < 3)
985 pp_flush (context->printer);
987 fnotice (stderr,
988 "Internal compiler error: Error reporting routines re-entered.\n");
990 /* Call diagnostic_action_after_output to get the "please submit a bug
991 report" message. It only looks at the kind field of diagnostic_info. */
992 diagnostic.kind = DK_ICE;
993 diagnostic_action_after_output (context, &diagnostic);
995 /* Do not use gcc_unreachable here; that goes through internal_error
996 and therefore would cause infinite recursion. */
997 real_abort ();
1000 /* Report an internal compiler error in a friendly manner. This is
1001 the function that gets called upon use of abort() in the source
1002 code generally, thanks to a special macro. */
1004 void
1005 fancy_abort (const char *file, int line, const char *function)
1007 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1010 /* Really call the system 'abort'. This has to go right at the end of
1011 this file, so that there are no functions after it that call abort
1012 and get the system abort instead of our macro. */
1013 #undef abort
1014 static void
1015 real_abort (void)
1017 abort ();