OSDN Git Service

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