OSDN Git Service

40ddc23cfd7eadd3cfcb67a36e7a8c75ee41ac8f
[pf3gnuchains/gcc-fork.git] / gcc / diagnostic.c
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 Free Software Foundation, Inc.
4    Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>.  */
21
22
23 /* This file implements the language independent aspect of diagnostic
24    message module.  */
25
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"
33
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)
38
39 /* Prototypes.  */
40 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
41
42 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
43
44 static void diagnostic_action_after_output (diagnostic_context *,
45                                             diagnostic_info *);
46 static void real_abort (void) ATTRIBUTE_NORETURN;
47
48 /* Name of program invoked, sans directories.  */
49
50 const char *progname;
51
52 /* A diagnostic_context surrogate for stderr.  */
53 static diagnostic_context global_diagnostic_context;
54 diagnostic_context *global_dc = &global_diagnostic_context;
55
56 \f
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, ...)
61 {
62   char *str;
63   va_list ap;
64
65   va_start (ap, msg);
66   vasprintf (&str, msg, ap);
67   va_end (ap);
68
69   return str;
70 }
71
72 /* Same as diagnostic_build_prefix, but only the source FILE is given.  */
73 char *
74 file_name_as_prefix (const char *f)
75 {
76   return build_message_string ("%s: ", f);
77 }
78
79
80 \f
81 /* Initialize the diagnostic message outputting machinery.  */
82 void
83 diagnostic_initialize (diagnostic_context *context, int n_opts)
84 {
85   int i;
86
87   /* Allocate a basic pretty-printer.  Clients will replace this a
88      much more elaborated pretty-printer if they wish.  */
89   context->printer = XNEW (pretty_printer);
90   pp_construct (context->printer, NULL, 0);
91   /* By default, diagnostics are sent to stderr.  */
92   context->printer->buffer->stream = stderr;
93   /* By default, we emit prefixes once per message.  */
94   context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
95
96   memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
97   context->some_warnings_are_errors = false;
98   context->warning_as_error_requested = false;
99   context->n_opts = n_opts;
100   context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
101   for (i = 0; i < n_opts; i++)
102     context->classify_diagnostic[i] = DK_UNSPECIFIED;
103   context->show_option_requested = false;
104   context->abort_on_error = false;
105   context->show_column = false;
106   context->pedantic_errors = false;
107   context->permissive = false;
108   context->opt_permissive = 0;
109   context->fatal_errors = false;
110   context->dc_inhibit_warnings = false;
111   context->dc_warn_system_headers = false;
112   context->internal_error = NULL;
113   diagnostic_starter (context) = default_diagnostic_starter;
114   diagnostic_finalizer (context) = default_diagnostic_finalizer;
115   context->option_enabled = NULL;
116   context->option_state = NULL;
117   context->option_name = NULL;
118   context->last_module = 0;
119   context->x_data = NULL;
120   context->lock = 0;
121   context->inhibit_notes_p = false;
122 }
123
124 /* Do any cleaning up required after the last diagnostic is emitted.  */
125
126 void
127 diagnostic_finish (diagnostic_context *context)
128 {
129   /* Some of the errors may actually have been warnings.  */
130   if (context->some_warnings_are_errors)
131     {
132       /* -Werror was given.  */
133       if (context->warning_as_error_requested)
134         pp_verbatim (context->printer,
135                      _("%s: all warnings being treated as errors\n"),
136                      progname);
137       /* At least one -Werror= was given.  */
138       else
139         pp_verbatim (context->printer,
140                      _("%s: some warnings being treated as errors\n"),
141                      progname);
142       pp_flush (context->printer);
143     }
144 }
145
146 /* Initialize DIAGNOSTIC, where the message MSG has already been
147    translated.  */
148 void
149 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
150                                 va_list *args, location_t location,
151                                 diagnostic_t kind)
152 {
153   diagnostic->message.err_no = errno;
154   diagnostic->message.args_ptr = args;
155   diagnostic->message.format_spec = msg;
156   diagnostic->location = location;
157   diagnostic->override_column = 0;
158   diagnostic->kind = kind;
159   diagnostic->option_index = 0;
160 }
161
162 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
163    translated.  */
164 void
165 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
166                      va_list *args, location_t location,
167                      diagnostic_t kind)
168 {
169   diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
170 }
171
172 /* Return a malloc'd string describing a location.  The caller is
173    responsible for freeing the memory.  */
174 char *
175 diagnostic_build_prefix (diagnostic_context *context,
176                          diagnostic_info *diagnostic)
177 {
178   static const char *const diagnostic_kind_text[] = {
179 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
180 #include "diagnostic.def"
181 #undef DEFINE_DIAGNOSTIC_KIND
182     "must-not-happen"
183   };
184   const char *text = _(diagnostic_kind_text[diagnostic->kind]);
185   expanded_location s = expand_location (diagnostic->location);
186   if (diagnostic->override_column)
187     s.column = diagnostic->override_column;
188   gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
189
190   return
191     (s.file == NULL
192      ? build_message_string ("%s: %s", progname, text)
193      : context->show_column
194      ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
195      : build_message_string ("%s:%d: %s", s.file, s.line, text));
196 }
197
198 /* Take any action which is expected to happen after the diagnostic
199    is written out.  This function does not always return.  */
200 static void
201 diagnostic_action_after_output (diagnostic_context *context,
202                                 diagnostic_info *diagnostic)
203 {
204   switch (diagnostic->kind)
205     {
206     case DK_DEBUG:
207     case DK_NOTE:
208     case DK_ANACHRONISM:
209     case DK_WARNING:
210       break;
211
212     case DK_ERROR:
213     case DK_SORRY:
214       if (context->abort_on_error)
215         real_abort ();
216       if (context->fatal_errors)
217         {
218           fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
219           diagnostic_finish (context);
220           exit (FATAL_EXIT_CODE);
221         }
222       break;
223
224     case DK_ICE:
225       if (context->abort_on_error)
226         real_abort ();
227
228       fnotice (stderr, "Please submit a full bug report,\n"
229                "with preprocessed source if appropriate.\n"
230                "See %s for instructions.\n", bug_report_url);
231       exit (ICE_EXIT_CODE);
232
233     case DK_FATAL:
234       if (context->abort_on_error)
235         real_abort ();
236       diagnostic_finish (context);
237       fnotice (stderr, "compilation terminated.\n");
238       exit (FATAL_EXIT_CODE);
239
240     default:
241       gcc_unreachable ();
242     }
243 }
244
245 void
246 diagnostic_report_current_module (diagnostic_context *context)
247 {
248   const struct line_map *map;
249
250   if (pp_needs_newline (context->printer))
251     {
252       pp_newline (context->printer);
253       pp_needs_newline (context->printer) = false;
254     }
255
256   if (input_location <= BUILTINS_LOCATION)
257     return;
258
259   map = linemap_lookup (line_table, input_location);
260   if (map && diagnostic_last_module_changed (context, map))
261     {
262       diagnostic_set_last_module (context, map);
263       if (! MAIN_FILE_P (map))
264         {
265           map = INCLUDED_FROM (line_table, map);
266           if (context->show_column)
267             pp_verbatim (context->printer,
268                          "In file included from %s:%d:%d",
269                          map->to_file,
270                          LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
271           else
272             pp_verbatim (context->printer,
273                          "In file included from %s:%d",
274                          map->to_file, LAST_SOURCE_LINE (map));
275           while (! MAIN_FILE_P (map))
276             {
277               map = INCLUDED_FROM (line_table, map);
278               pp_verbatim (context->printer,
279                            ",\n                 from %s:%d",
280                            map->to_file, LAST_SOURCE_LINE (map));
281             }
282           pp_verbatim (context->printer, ":");
283           pp_newline (context->printer);
284         }
285     }
286 }
287
288 void
289 default_diagnostic_starter (diagnostic_context *context,
290                             diagnostic_info *diagnostic)
291 {
292   diagnostic_report_current_module (context);
293   pp_set_prefix (context->printer, diagnostic_build_prefix (context,
294                                                             diagnostic));
295 }
296
297 void
298 default_diagnostic_finalizer (diagnostic_context *context,
299                               diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
300 {
301   pp_destroy_prefix (context->printer);
302 }
303
304 /* Interface to specify diagnostic kind overrides.  Returns the
305    previous setting, or DK_UNSPECIFIED if the parameters are out of
306    range.  */
307 diagnostic_t
308 diagnostic_classify_diagnostic (diagnostic_context *context,
309                                 int option_index,
310                                 diagnostic_t new_kind,
311                                 location_t where)
312 {
313   diagnostic_t old_kind;
314
315   if (option_index <= 0
316       || option_index >= context->n_opts
317       || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
318     return DK_UNSPECIFIED;
319
320   old_kind = context->classify_diagnostic[option_index];
321
322   /* Handle pragmas separately, since we need to keep track of *where*
323      the pragmas were.  */
324   if (where != UNKNOWN_LOCATION)
325     {
326       int i;
327
328       for (i = context->n_classification_history - 1; i >= 0; i --)
329         if (context->classification_history[i].option == option_index)
330           {
331             old_kind = context->classification_history[i].kind;
332             break;
333           }
334
335       i = context->n_classification_history;
336       context->classification_history =
337         (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
338                                                          * sizeof (diagnostic_classification_change_t));
339       context->classification_history[i].location = where;
340       context->classification_history[i].option = option_index;
341       context->classification_history[i].kind = new_kind;
342       context->n_classification_history ++;
343     }
344   else
345     context->classify_diagnostic[option_index] = new_kind;
346
347   return old_kind;
348 }
349
350 /* Save all diagnostic classifications in a stack.  */
351 void
352 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
353 {
354   context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
355   context->push_list[context->n_push ++] = context->n_classification_history;
356 }
357
358 /* Restore the topmost classification set off the stack.  If the stack
359    is empty, revert to the state based on command line parameters.  */
360 void
361 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
362 {
363   int jump_to;
364   int i;
365
366   if (context->n_push)
367     jump_to = context->push_list [-- context->n_push];
368   else
369     jump_to = 0;
370
371   i = context->n_classification_history;
372   context->classification_history =
373     (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
374                                                      * sizeof (diagnostic_classification_change_t));
375   context->classification_history[i].location = where;
376   context->classification_history[i].option = jump_to;
377   context->classification_history[i].kind = DK_POP;
378   context->n_classification_history ++;
379 }
380
381 /* Report a diagnostic message (an error or a warning) as specified by
382    DC.  This function is *the* subroutine in terms of which front-ends
383    should implement their specific diagnostic handling modules.  The
384    front-end independent format specifiers are exactly those described
385    in the documentation of output_format.
386    Return true if a diagnostic was printed, false otherwise.  */
387
388 bool
389 diagnostic_report_diagnostic (diagnostic_context *context,
390                               diagnostic_info *diagnostic)
391 {
392   location_t location = diagnostic->location;
393   diagnostic_t orig_diag_kind = diagnostic->kind;
394   const char *saved_format_spec;
395
396   /* Give preference to being able to inhibit warnings, before they
397      get reclassified to something else.  */
398   if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
399       && !diagnostic_report_warnings_p (context, location))
400     return false;
401
402   if (diagnostic->kind == DK_PEDWARN)
403     {
404       diagnostic->kind = pedantic_warning_kind (context);
405       /* We do this to avoid giving the message for -pedantic-errors.  */
406       orig_diag_kind = diagnostic->kind;
407     }
408  
409   if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
410     return false;
411
412   if (context->lock > 0)
413     {
414       /* If we're reporting an ICE in the middle of some other error,
415          try to flush out the previous error, then let this one
416          through.  Don't do this more than once.  */
417       if (diagnostic->kind == DK_ICE && context->lock == 1)
418         pp_flush (context->printer);
419       else
420         error_recursion (context);
421     }
422
423   /* If the user requested that warnings be treated as errors, so be
424      it.  Note that we do this before the next block so that
425      individual warnings can be overridden back to warnings with
426      -Wno-error=*.  */
427   if (context->warning_as_error_requested
428       && diagnostic->kind == DK_WARNING)
429     {
430       diagnostic->kind = DK_ERROR;
431     }
432
433   if (diagnostic->option_index)
434     {
435       diagnostic_t diag_class = DK_UNSPECIFIED;
436
437       /* This tests if the user provided the appropriate -Wfoo or
438          -Wno-foo option.  */
439       if (! context->option_enabled (diagnostic->option_index,
440                                      context->option_state))
441         return false;
442
443       /* This tests for #pragma diagnostic changes.  */
444       if (context->n_classification_history > 0)
445         {
446           int i;
447           /* FIXME: Stupid search.  Optimize later. */
448           for (i = context->n_classification_history - 1; i >= 0; i --)
449             {
450               if (context->classification_history[i].location <= location)
451                 {
452                   if (context->classification_history[i].kind == (int) DK_POP)
453                     {
454                       i = context->classification_history[i].option;
455                       continue;
456                     }
457                   if (context->classification_history[i].option == diagnostic->option_index)
458                     {
459                       diag_class = context->classification_history[i].kind;
460                       if (diag_class != DK_UNSPECIFIED)
461                         diagnostic->kind = diag_class;
462                       break;
463                     }
464                 }
465             }
466         }
467       /* This tests if the user provided the appropriate -Werror=foo
468          option.  */
469       if (diag_class == DK_UNSPECIFIED
470           && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
471         {
472           diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
473         }
474       /* This allows for future extensions, like temporarily disabling
475          warnings for ranges of source code.  */
476       if (diagnostic->kind == DK_IGNORED)
477         return false;
478     }
479
480   if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
481     context->some_warnings_are_errors = true;
482
483   context->lock++;
484
485   if (diagnostic->kind == DK_ICE)
486     {
487 #ifndef ENABLE_CHECKING
488       /* When not checking, ICEs are converted to fatal errors when an
489          error has already occurred.  This is counteracted by
490          abort_on_error.  */
491       if ((diagnostic_kind_count (context, DK_ERROR) > 0
492            || diagnostic_kind_count (context, DK_SORRY) > 0)
493           && !context->abort_on_error)
494         {
495           expanded_location s = expand_location (diagnostic->location);
496           fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
497                    s.file, s.line);
498           exit (ICE_EXIT_CODE);
499         }
500 #endif
501       if (context->internal_error)
502         (*context->internal_error) (context,
503                                     diagnostic->message.format_spec,
504                                     diagnostic->message.args_ptr);
505     }
506   ++diagnostic_kind_count (context, diagnostic->kind);
507
508   saved_format_spec = diagnostic->message.format_spec;
509   if (context->show_option_requested)
510     {
511       char *option_text;
512
513       option_text = context->option_name (context, diagnostic->option_index,
514                                           orig_diag_kind, diagnostic->kind);
515
516       if (option_text)
517         {
518           diagnostic->message.format_spec
519             = ACONCAT ((diagnostic->message.format_spec,
520                         " ", 
521                         "[", option_text, "]",
522                         NULL));
523           free (option_text);
524         }
525     }
526   diagnostic->message.locus = &diagnostic->location;
527   diagnostic->message.x_data = &diagnostic->x_data;
528   diagnostic->x_data = NULL;
529   pp_format (context->printer, &diagnostic->message);
530   (*diagnostic_starter (context)) (context, diagnostic);
531   pp_output_formatted_text (context->printer);
532   (*diagnostic_finalizer (context)) (context, diagnostic);
533   pp_flush (context->printer);
534   diagnostic_action_after_output (context, diagnostic);
535   diagnostic->message.format_spec = saved_format_spec;
536   diagnostic->x_data = NULL;
537
538   context->lock--;
539
540   return true;
541 }
542
543 /* Given a partial pathname as input, return another pathname that
544    shares no directory elements with the pathname of __FILE__.  This
545    is used by fancy_abort() to print `Internal compiler error in expr.c'
546    instead of `Internal compiler error in ../../GCC/gcc/expr.c'.  */
547
548 const char *
549 trim_filename (const char *name)
550 {
551   static const char this_file[] = __FILE__;
552   const char *p = name, *q = this_file;
553
554   /* First skip any "../" in each filename.  This allows us to give a proper
555      reference to a file in a subdirectory.  */
556   while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
557     p += 3;
558
559   while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
560     q += 3;
561
562   /* Now skip any parts the two filenames have in common.  */
563   while (*p == *q && *p != 0 && *q != 0)
564     p++, q++;
565
566   /* Now go backwards until the previous directory separator.  */
567   while (p > name && !IS_DIR_SEPARATOR (p[-1]))
568     p--;
569
570   return p;
571 }
572 \f
573 /* Standard error reporting routines in increasing order of severity.
574    All of these take arguments like printf.  */
575
576 /* Text to be emitted verbatim to the error message stream; this
577    produces no prefix and disables line-wrapping.  Use rarely.  */
578 void
579 verbatim (const char *gmsgid, ...)
580 {
581   text_info text;
582   va_list ap;
583
584   va_start (ap, gmsgid);
585   text.err_no = errno;
586   text.args_ptr = &ap;
587   text.format_spec = _(gmsgid);
588   text.locus = NULL;
589   text.x_data = NULL;
590   pp_format_verbatim (global_dc->printer, &text);
591   pp_flush (global_dc->printer);
592   va_end (ap);
593 }
594
595 bool
596 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
597                  const char *gmsgid, ...)
598 {
599   diagnostic_info diagnostic;
600   va_list ap;
601
602   va_start (ap, gmsgid);
603   if (kind == DK_PERMERROR)
604     {
605       diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
606                            permissive_error_kind (global_dc));
607       diagnostic.option_index = permissive_error_option (global_dc);
608     }
609   else {
610       diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
611       if (kind == DK_WARNING || kind == DK_PEDWARN)
612         diagnostic.option_index = opt;
613   }
614   va_end (ap);
615
616   return report_diagnostic (&diagnostic);
617 }
618
619 /* An informative note at LOCATION.  Use this for additional details on an error
620    message.  */
621 void
622 inform (location_t location, const char *gmsgid, ...)
623 {
624   diagnostic_info diagnostic;
625   va_list ap;
626
627   va_start (ap, gmsgid);
628   diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
629   report_diagnostic (&diagnostic);
630   va_end (ap);
631 }
632
633 /* An informative note at LOCATION.  Use this for additional details on an
634    error message.  */
635 void
636 inform_n (location_t location, int n, const char *singular_gmsgid,
637           const char *plural_gmsgid, ...)
638 {
639   diagnostic_info diagnostic;
640   va_list ap;
641
642   va_start (ap, plural_gmsgid);
643   diagnostic_set_info_translated (&diagnostic,
644                                   ngettext (singular_gmsgid, plural_gmsgid, n),
645                                   &ap, location, DK_NOTE);
646   report_diagnostic (&diagnostic);
647   va_end (ap);
648 }
649
650 /* A warning at INPUT_LOCATION.  Use this for code which is correct according
651    to the relevant language specification but is likely to be buggy anyway.
652    Returns true if the warning was printed, false if it was inhibited.  */
653 bool
654 warning (int opt, const char *gmsgid, ...)
655 {
656   diagnostic_info diagnostic;
657   va_list ap;
658
659   va_start (ap, gmsgid);
660   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
661   diagnostic.option_index = opt;
662
663   va_end (ap);
664   return report_diagnostic (&diagnostic);
665 }
666
667 /* A warning at LOCATION.  Use this for code which is correct according to the
668    relevant language specification but is likely to be buggy anyway.
669    Returns true if the warning was printed, false if it was inhibited.  */
670
671 bool
672 warning_at (location_t location, int opt, const char *gmsgid, ...)
673 {
674   diagnostic_info diagnostic;
675   va_list ap;
676
677   va_start (ap, gmsgid);
678   diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
679   diagnostic.option_index = opt;
680   va_end (ap);
681   return report_diagnostic (&diagnostic);
682 }
683
684 /* A "pedantic" warning at LOCATION: issues a warning unless
685    -pedantic-errors was given on the command line, in which case it
686    issues an error.  Use this for diagnostics required by the relevant
687    language standard, if you have chosen not to make them errors.
688
689    Note that these diagnostics are issued independent of the setting
690    of the -pedantic command-line switch.  To get a warning enabled
691    only with that switch, use either "if (pedantic) pedwarn
692    (OPT_pedantic,...)" or just "pedwarn (OPT_pedantic,..)".  To get a
693    pedwarn independently of the -pedantic switch use "pedwarn (0,...)".
694
695    Returns true if the warning was printed, false if it was inhibited.  */
696
697 bool
698 pedwarn (location_t location, int opt, const char *gmsgid, ...)
699 {
700   diagnostic_info diagnostic;
701   va_list ap;
702
703   va_start (ap, gmsgid);
704   diagnostic_set_info (&diagnostic, gmsgid, &ap, location,  DK_PEDWARN);
705   diagnostic.option_index = opt;
706   va_end (ap);
707   return report_diagnostic (&diagnostic);
708 }
709
710 /* A "permissive" error at LOCATION: issues an error unless
711    -fpermissive was given on the command line, in which case it issues
712    a warning.  Use this for things that really should be errors but we
713    want to support legacy code.
714
715    Returns true if the warning was printed, false if it was inhibited.  */
716
717 bool
718 permerror (location_t location, const char *gmsgid, ...)
719 {
720   diagnostic_info diagnostic;
721   va_list ap;
722
723   va_start (ap, gmsgid);
724   diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
725                        permissive_error_kind (global_dc));
726   diagnostic.option_index = permissive_error_option (global_dc);
727   va_end (ap);
728   return report_diagnostic (&diagnostic);
729 }
730
731 /* A hard error: the code is definitely ill-formed, and an object file
732    will not be produced.  */
733 void
734 error (const char *gmsgid, ...)
735 {
736   diagnostic_info diagnostic;
737   va_list ap;
738
739   va_start (ap, gmsgid);
740   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
741   report_diagnostic (&diagnostic);
742   va_end (ap);
743 }
744
745 /* A hard error: the code is definitely ill-formed, and an object file
746    will not be produced.  */
747 void
748 error_n (location_t location, int n, const char *singular_gmsgid,
749          const char *plural_gmsgid, ...)
750 {
751   diagnostic_info diagnostic;
752   va_list ap;
753
754   va_start (ap, plural_gmsgid);
755   diagnostic_set_info_translated (&diagnostic,
756                                   ngettext (singular_gmsgid, plural_gmsgid, n),
757                                   &ap, location, DK_ERROR);
758   report_diagnostic (&diagnostic);
759   va_end (ap);
760 }
761
762 /* Same as ebove, but use location LOC instead of input_location.  */
763 void
764 error_at (location_t loc, const char *gmsgid, ...)
765 {
766   diagnostic_info diagnostic;
767   va_list ap;
768
769   va_start (ap, gmsgid);
770   diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
771   report_diagnostic (&diagnostic);
772   va_end (ap);
773 }
774
775 /* "Sorry, not implemented."  Use for a language feature which is
776    required by the relevant specification but not implemented by GCC.
777    An object file will not be produced.  */
778 void
779 sorry (const char *gmsgid, ...)
780 {
781   diagnostic_info diagnostic;
782   va_list ap;
783
784   va_start (ap, gmsgid);
785   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
786   report_diagnostic (&diagnostic);
787   va_end (ap);
788 }
789
790 /* Return true if an error or a "sorry" has been seen.  Various
791    processing is disabled after errors.  */
792 bool
793 seen_error (void)
794 {
795   return errorcount || sorrycount;
796 }
797
798 /* An error which is severe enough that we make no attempt to
799    continue.  Do not use this for internal consistency checks; that's
800    internal_error.  Use of this function should be rare.  */
801 void
802 fatal_error (const char *gmsgid, ...)
803 {
804   diagnostic_info diagnostic;
805   va_list ap;
806
807   va_start (ap, gmsgid);
808   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
809   report_diagnostic (&diagnostic);
810   va_end (ap);
811
812   gcc_unreachable ();
813 }
814
815 /* An internal consistency check has failed.  We make no attempt to
816    continue.  Note that unless there is debugging value to be had from
817    a more specific message, or some other good reason, you should use
818    abort () instead of calling this function directly.  */
819 void
820 internal_error (const char *gmsgid, ...)
821 {
822   diagnostic_info diagnostic;
823   va_list ap;
824
825   va_start (ap, gmsgid);
826   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
827   report_diagnostic (&diagnostic);
828   va_end (ap);
829
830   gcc_unreachable ();
831 }
832 \f
833 /* Special case error functions.  Most are implemented in terms of the
834    above, or should be.  */
835
836 /* Print a diagnostic MSGID on FILE.  This is just fprintf, except it
837    runs its second argument through gettext.  */
838 void
839 fnotice (FILE *file, const char *cmsgid, ...)
840 {
841   va_list ap;
842
843   va_start (ap, cmsgid);
844   vfprintf (file, _(cmsgid), ap);
845   va_end (ap);
846 }
847
848 /* Inform the user that an error occurred while trying to report some
849    other error.  This indicates catastrophic internal inconsistencies,
850    so give up now.  But do try to flush out the previous error.
851    This mustn't use internal_error, that will cause infinite recursion.  */
852
853 static void
854 error_recursion (diagnostic_context *context)
855 {
856   diagnostic_info diagnostic;
857
858   if (context->lock < 3)
859     pp_flush (context->printer);
860
861   fnotice (stderr,
862            "Internal compiler error: Error reporting routines re-entered.\n");
863
864   /* Call diagnostic_action_after_output to get the "please submit a bug
865      report" message.  It only looks at the kind field of diagnostic_info.  */
866   diagnostic.kind = DK_ICE;
867   diagnostic_action_after_output (context, &diagnostic);
868
869   /* Do not use gcc_unreachable here; that goes through internal_error
870      and therefore would cause infinite recursion.  */
871   real_abort ();
872 }
873
874 /* Report an internal compiler error in a friendly manner.  This is
875    the function that gets called upon use of abort() in the source
876    code generally, thanks to a special macro.  */
877
878 void
879 fancy_abort (const char *file, int line, const char *function)
880 {
881   internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
882 }
883
884 /* Really call the system 'abort'.  This has to go right at the end of
885    this file, so that there are no functions after it that call abort
886    and get the system abort instead of our macro.  */
887 #undef abort
888 static void
889 real_abort (void)
890 {
891   abort ();
892 }