diagnostic.h (file_name_as_prefix): Add context argument.
[platform/upstream/gcc.git] / gcc / diagnostic.c
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2    Copyright (C) 1999-2013 Free Software Foundation, Inc.
3    Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21
22 /* This file implements the language independent aspect of diagnostic
23    message module.  */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "version.h"
29 #include "demangle.h"
30 #include "input.h"
31 #include "intl.h"
32 #include "backtrace.h"
33 #include "diagnostic.h"
34 #include "diagnostic-color.h"
35
36 #define pedantic_warning_kind(DC)                       \
37   ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
38 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
39 #define permissive_error_option(DC) ((DC)->opt_permissive)
40
41 /* Prototypes.  */
42 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
43
44 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
45
46 static void diagnostic_action_after_output (diagnostic_context *,
47                                             diagnostic_info *);
48 static void real_abort (void) ATTRIBUTE_NORETURN;
49
50 /* Name of program invoked, sans directories.  */
51
52 const char *progname;
53
54 /* A diagnostic_context surrogate for stderr.  */
55 static diagnostic_context global_diagnostic_context;
56 diagnostic_context *global_dc = &global_diagnostic_context;
57 \f
58 /* Return a malloc'd string containing MSG formatted a la printf.  The
59    caller is responsible for freeing the memory.  */
60 static char *
61 build_message_string (const char *msg, ...)
62 {
63   char *str;
64   va_list ap;
65
66   va_start (ap, msg);
67   vasprintf (&str, msg, ap);
68   va_end (ap);
69
70   return str;
71 }
72
73 /* Same as diagnostic_build_prefix, but only the source FILE is given.  */
74 char *
75 file_name_as_prefix (diagnostic_context *context, const char *f)
76 {
77   const char *locus_cs
78     = colorize_start (pp_show_color (context->printer), "locus");
79   const char *locus_ce = colorize_stop (pp_show_color (context->printer));
80   return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
81 }
82
83
84 \f
85 /* Return the value of the getenv("COLUMNS") as an integer. If the
86    value is not set to a positive integer, then return INT_MAX.  */
87 static int
88 getenv_columns (void)
89 {
90   const char * s = getenv ("COLUMNS");
91   if (s != NULL) {
92     int n = atoi (s);
93     if (n > 0)
94       return n;
95   }
96   return INT_MAX;
97 }
98
99 /* Set caret_max_width to value.  */
100 void
101 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
102 {
103   /* One minus to account for the leading empty space.  */
104   value = value ? value - 1 
105     : (isatty (fileno (context->printer->buffer->stream))
106        ? getenv_columns () - 1: INT_MAX);
107   
108   if (value <= 0) 
109     value = INT_MAX;
110
111   context->caret_max_width = value;
112 }
113
114 /* Initialize the diagnostic message outputting machinery.  */
115 void
116 diagnostic_initialize (diagnostic_context *context, int n_opts)
117 {
118   int i;
119
120   /* Allocate a basic pretty-printer.  Clients will replace this a
121      much more elaborated pretty-printer if they wish.  */
122   context->printer = XNEW (pretty_printer);
123   pp_construct (context->printer, NULL, 0);
124   /* By default, diagnostics are sent to stderr.  */
125   context->printer->buffer->stream = stderr;
126   /* By default, we emit prefixes once per message.  */
127   context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
128
129   memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
130   context->some_warnings_are_errors = false;
131   context->warning_as_error_requested = false;
132   context->n_opts = n_opts;
133   context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
134   for (i = 0; i < n_opts; i++)
135     context->classify_diagnostic[i] = DK_UNSPECIFIED;
136   context->show_caret = false;
137   diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
138   context->show_option_requested = false;
139   context->abort_on_error = false;
140   context->show_column = false;
141   context->pedantic_errors = false;
142   context->permissive = false;
143   context->opt_permissive = 0;
144   context->fatal_errors = false;
145   context->dc_inhibit_warnings = false;
146   context->dc_warn_system_headers = false;
147   context->max_errors = 0;
148   context->internal_error = NULL;
149   diagnostic_starter (context) = default_diagnostic_starter;
150   diagnostic_finalizer (context) = default_diagnostic_finalizer;
151   context->option_enabled = NULL;
152   context->option_state = NULL;
153   context->option_name = NULL;
154   context->last_location = UNKNOWN_LOCATION;
155   context->last_module = 0;
156   context->x_data = NULL;
157   context->lock = 0;
158   context->inhibit_notes_p = false;
159 }
160
161 /* Do any cleaning up required after the last diagnostic is emitted.  */
162
163 void
164 diagnostic_finish (diagnostic_context *context)
165 {
166   /* Some of the errors may actually have been warnings.  */
167   if (context->some_warnings_are_errors)
168     {
169       /* -Werror was given.  */
170       if (context->warning_as_error_requested)
171         pp_verbatim (context->printer,
172                      _("%s: all warnings being treated as errors"),
173                      progname);
174       /* At least one -Werror= was given.  */
175       else
176         pp_verbatim (context->printer,
177                      _("%s: some warnings being treated as errors"),
178                      progname);
179       pp_newline_and_flush (context->printer);
180     }
181 }
182
183 /* Initialize DIAGNOSTIC, where the message MSG has already been
184    translated.  */
185 void
186 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
187                                 va_list *args, location_t location,
188                                 diagnostic_t kind)
189 {
190   diagnostic->message.err_no = errno;
191   diagnostic->message.args_ptr = args;
192   diagnostic->message.format_spec = msg;
193   diagnostic->location = location;
194   diagnostic->override_column = 0;
195   diagnostic->kind = kind;
196   diagnostic->option_index = 0;
197 }
198
199 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
200    translated.  */
201 void
202 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
203                      va_list *args, location_t location,
204                      diagnostic_t kind)
205 {
206   diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
207 }
208
209 /* Return a malloc'd string describing a location.  The caller is
210    responsible for freeing the memory.  */
211 char *
212 diagnostic_build_prefix (diagnostic_context *context,
213                          const diagnostic_info *diagnostic)
214 {
215   static const char *const diagnostic_kind_text[] = {
216 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
217 #include "diagnostic.def"
218 #undef DEFINE_DIAGNOSTIC_KIND
219     "must-not-happen"
220   };
221   static const char *const diagnostic_kind_color[] = {
222 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
223 #include "diagnostic.def"
224 #undef DEFINE_DIAGNOSTIC_KIND
225     NULL
226   };
227   const char *text = _(diagnostic_kind_text[diagnostic->kind]);
228   const char *text_cs = "", *text_ce = "";
229   const char *locus_cs, *locus_ce;
230   pretty_printer *pp = context->printer;
231
232   if (diagnostic_kind_color[diagnostic->kind])
233     {
234       text_cs = colorize_start (pp_show_color (pp),
235                                 diagnostic_kind_color[diagnostic->kind]);
236       text_ce = colorize_stop (pp_show_color (pp));
237     }
238   locus_cs = colorize_start (pp_show_color (pp), "locus");
239   locus_ce = colorize_stop (pp_show_color (pp));
240
241   expanded_location s = expand_location_to_spelling_point (diagnostic->location);
242   if (diagnostic->override_column)
243     s.column = diagnostic->override_column;
244   gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
245
246   return
247     (s.file == NULL
248      ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce,
249                              text_cs, text, text_ce)
250      : context->show_column
251      ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
252                              s.column, locus_ce, text_cs, text, text_ce)
253      : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line, locus_ce,
254                              text_cs, text, text_ce));
255 }
256
257 /* If LINE is longer than MAX_WIDTH, and COLUMN is not smaller than
258    MAX_WIDTH by some margin, then adjust the start of the line such
259    that the COLUMN is smaller than MAX_WIDTH minus the margin.  The
260    margin is either 10 characters or the difference between the column
261    and the length of the line, whatever is smaller.  */
262 static const char *
263 adjust_line (const char *line, int max_width, int *column_p)
264 {
265   int right_margin = 10;
266   int line_width = strlen (line);
267   int column = *column_p;
268
269   right_margin = MIN(line_width - column, right_margin);
270   right_margin = max_width - right_margin;
271   if (line_width >= max_width && column > right_margin)
272     {
273       line += column - right_margin;
274       *column_p = right_margin;
275     }
276   return line;
277 }
278
279 /* Print the physical source line corresponding to the location of
280    this diagnostics, and a caret indicating the precise column.  */
281 void
282 diagnostic_show_locus (diagnostic_context * context,
283                        const diagnostic_info *diagnostic)
284 {
285   const char *line;
286   char *buffer;
287   expanded_location s;
288   int max_width;
289   const char *saved_prefix;
290   const char *caret_cs, *caret_ce;
291
292   if (!context->show_caret
293       || diagnostic->location <= BUILTINS_LOCATION
294       || diagnostic->location == context->last_location)
295     return;
296
297   context->last_location = diagnostic->location;
298   s = expand_location_to_spelling_point (diagnostic->location);
299   line = location_get_source_line (s);
300   if (line == NULL)
301     return;
302
303   max_width = context->caret_max_width;
304   line = adjust_line (line, max_width, &(s.column));
305
306   pp_newline (context->printer);
307   saved_prefix = pp_get_prefix (context->printer);
308   pp_set_prefix (context->printer, NULL);
309   pp_character (context->printer, ' ');
310   while (max_width > 0 && *line != '\0')
311     {
312       char c = *line == '\t' ? ' ' : *line;
313       pp_character (context->printer, c);
314       max_width--;
315       line++;
316     }
317   pp_newline (context->printer);
318   caret_cs = colorize_start (pp_show_color (context->printer), "caret");
319   caret_ce = colorize_stop (pp_show_color (context->printer));
320
321   /* pp_printf does not implement %*c.  */
322   size_t len = s.column + 3 + strlen (caret_cs) + strlen (caret_ce);
323   buffer = XALLOCAVEC (char, len);
324   snprintf (buffer, len, "%s %*c%s", caret_cs, s.column, '^', caret_ce);
325   pp_string (context->printer, buffer);
326   pp_set_prefix (context->printer, saved_prefix);
327 }
328
329 /* Functions at which to stop the backtrace print.  It's not
330    particularly helpful to print the callers of these functions.  */
331
332 static const char * const bt_stop[] =
333 {
334   "main",
335   "toplev_main",
336   "execute_one_pass",
337   "compile_file",
338 };
339
340 /* A callback function passed to the backtrace_full function.  */
341
342 static int
343 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
344              const char *function)
345 {
346   int *pcount = (int *) data;
347
348   /* If we don't have any useful information, don't print
349      anything.  */
350   if (filename == NULL && function == NULL)
351     return 0;
352
353   /* Skip functions in diagnostic.c.  */
354   if (*pcount == 0
355       && filename != NULL
356       && strcmp (lbasename(filename), "diagnostic.c") == 0)
357     return 0;
358
359   /* Print up to 20 functions.  We could make this a --param, but
360      since this is only for debugging just use a constant for now.  */
361   if (*pcount >= 20)
362     {
363       /* Returning a non-zero value stops the backtrace.  */
364       return 1;
365     }
366   ++*pcount;
367
368   char *alc = NULL;
369   if (function != NULL)
370     {
371       char *str = cplus_demangle_v3 (function,
372                                      (DMGL_VERBOSE | DMGL_ANSI
373                                       | DMGL_GNU_V3 | DMGL_PARAMS));
374       if (str != NULL)
375         {
376           alc = str;
377           function = str;
378         }
379
380       for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
381         {
382           size_t len = strlen (bt_stop[i]);
383           if (strncmp (function, bt_stop[i], len) == 0
384               && (function[len] == '\0' || function[len] == '('))
385             {
386               if (alc != NULL)
387                 free (alc);
388               /* Returning a non-zero value stops the backtrace.  */
389               return 1;
390             }
391         }
392     }
393
394   fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
395            (unsigned long) pc,
396            function == NULL ? "???" : function,
397            filename == NULL ? "???" : filename,
398            lineno);
399
400   if (alc != NULL)
401     free (alc);
402
403   return 0;
404 }
405
406 /* A callback function passed to the backtrace_full function.  This is
407    called if backtrace_full has an error.  */
408
409 static void
410 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
411 {
412   if (errnum < 0)
413     {
414       /* This means that no debug info was available.  Just quietly
415          skip printing backtrace info.  */
416       return;
417     }
418   fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
419            errnum == 0 ? "" : xstrerror (errnum));
420 }
421
422 /* Take any action which is expected to happen after the diagnostic
423    is written out.  This function does not always return.  */
424 static void
425 diagnostic_action_after_output (diagnostic_context *context,
426                                 diagnostic_info *diagnostic)
427 {
428   switch (diagnostic->kind)
429     {
430     case DK_DEBUG:
431     case DK_NOTE:
432     case DK_ANACHRONISM:
433     case DK_WARNING:
434       break;
435
436     case DK_ERROR:
437     case DK_SORRY:
438       if (context->abort_on_error)
439         real_abort ();
440       if (context->fatal_errors)
441         {
442           fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
443           diagnostic_finish (context);
444           exit (FATAL_EXIT_CODE);
445         }
446       if (context->max_errors != 0
447           && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
448                           + diagnostic_kind_count (context, DK_SORRY))
449               >= context->max_errors))
450         {
451           fnotice (stderr,
452                    "compilation terminated due to -fmax-errors=%u.\n",
453                    context->max_errors);
454           diagnostic_finish (context);
455           exit (FATAL_EXIT_CODE);
456         }
457       break;
458
459     case DK_ICE:
460       {
461         struct backtrace_state *state =
462           backtrace_create_state (NULL, 0, bt_err_callback, NULL);
463         int count = 0;
464         if (state != NULL)
465           backtrace_full (state, 2, bt_callback, bt_err_callback,
466                           (void *) &count);
467
468         if (context->abort_on_error)
469           real_abort ();
470
471         fnotice (stderr, "Please submit a full bug report,\n"
472                  "with preprocessed source if appropriate.\n");
473         if (count > 0)
474           fnotice (stderr,
475                    ("Please include the complete backtrace "
476                     "with any bug report.\n"));
477         fnotice (stderr, "See %s for instructions.\n", bug_report_url);
478
479         exit (ICE_EXIT_CODE);
480       }
481
482     case DK_FATAL:
483       if (context->abort_on_error)
484         real_abort ();
485       diagnostic_finish (context);
486       fnotice (stderr, "compilation terminated.\n");
487       exit (FATAL_EXIT_CODE);
488
489     default:
490       gcc_unreachable ();
491     }
492 }
493
494 void
495 diagnostic_report_current_module (diagnostic_context *context, location_t where)
496 {
497   const struct line_map *map = NULL;
498
499   if (pp_needs_newline (context->printer))
500     {
501       pp_newline (context->printer);
502       pp_needs_newline (context->printer) = false;
503     }
504
505   if (where <= BUILTINS_LOCATION)
506     return;
507
508   linemap_resolve_location (line_table, where,
509                             LRK_MACRO_DEFINITION_LOCATION,
510                             &map);
511
512   if (map && diagnostic_last_module_changed (context, map))
513     {
514       diagnostic_set_last_module (context, map);
515       if (! MAIN_FILE_P (map))
516         {
517           map = INCLUDED_FROM (line_table, map);
518           if (context->show_column)
519             pp_verbatim (context->printer,
520                          "In file included from %s:%d:%d",
521                          LINEMAP_FILE (map),
522                          LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
523           else
524             pp_verbatim (context->printer,
525                          "In file included from %s:%d",
526                          LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
527           while (! MAIN_FILE_P (map))
528             {
529               map = INCLUDED_FROM (line_table, map);
530               pp_verbatim (context->printer,
531                            ",\n                 from %s:%d",
532                            LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
533             }
534           pp_verbatim (context->printer, ":");
535           pp_newline (context->printer);
536         }
537     }
538 }
539
540 void
541 default_diagnostic_starter (diagnostic_context *context,
542                             diagnostic_info *diagnostic)
543 {
544   diagnostic_report_current_module (context, diagnostic->location);
545   pp_set_prefix (context->printer, diagnostic_build_prefix (context,
546                                                             diagnostic));
547 }
548
549 void
550 default_diagnostic_finalizer (diagnostic_context *context ATTRIBUTE_UNUSED,
551                               diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
552 {
553 }
554
555 /* Interface to specify diagnostic kind overrides.  Returns the
556    previous setting, or DK_UNSPECIFIED if the parameters are out of
557    range.  */
558 diagnostic_t
559 diagnostic_classify_diagnostic (diagnostic_context *context,
560                                 int option_index,
561                                 diagnostic_t new_kind,
562                                 location_t where)
563 {
564   diagnostic_t old_kind;
565
566   if (option_index <= 0
567       || option_index >= context->n_opts
568       || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
569     return DK_UNSPECIFIED;
570
571   old_kind = context->classify_diagnostic[option_index];
572
573   /* Handle pragmas separately, since we need to keep track of *where*
574      the pragmas were.  */
575   if (where != UNKNOWN_LOCATION)
576     {
577       int i;
578
579       for (i = context->n_classification_history - 1; i >= 0; i --)
580         if (context->classification_history[i].option == option_index)
581           {
582             old_kind = context->classification_history[i].kind;
583             break;
584           }
585
586       i = context->n_classification_history;
587       context->classification_history =
588         (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
589                                                          * sizeof (diagnostic_classification_change_t));
590       context->classification_history[i].location = where;
591       context->classification_history[i].option = option_index;
592       context->classification_history[i].kind = new_kind;
593       context->n_classification_history ++;
594     }
595   else
596     context->classify_diagnostic[option_index] = new_kind;
597
598   return old_kind;
599 }
600
601 /* Save all diagnostic classifications in a stack.  */
602 void
603 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
604 {
605   context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
606   context->push_list[context->n_push ++] = context->n_classification_history;
607 }
608
609 /* Restore the topmost classification set off the stack.  If the stack
610    is empty, revert to the state based on command line parameters.  */
611 void
612 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
613 {
614   int jump_to;
615   int i;
616
617   if (context->n_push)
618     jump_to = context->push_list [-- context->n_push];
619   else
620     jump_to = 0;
621
622   i = context->n_classification_history;
623   context->classification_history =
624     (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
625                                                      * sizeof (diagnostic_classification_change_t));
626   context->classification_history[i].location = where;
627   context->classification_history[i].option = jump_to;
628   context->classification_history[i].kind = DK_POP;
629   context->n_classification_history ++;
630 }
631
632 /* Report a diagnostic message (an error or a warning) as specified by
633    DC.  This function is *the* subroutine in terms of which front-ends
634    should implement their specific diagnostic handling modules.  The
635    front-end independent format specifiers are exactly those described
636    in the documentation of output_format.
637    Return true if a diagnostic was printed, false otherwise.  */
638
639 bool
640 diagnostic_report_diagnostic (diagnostic_context *context,
641                               diagnostic_info *diagnostic)
642 {
643   location_t location = diagnostic->location;
644   diagnostic_t orig_diag_kind = diagnostic->kind;
645   const char *saved_format_spec;
646
647   /* Give preference to being able to inhibit warnings, before they
648      get reclassified to something else.  */
649   if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
650       && !diagnostic_report_warnings_p (context, location))
651     return false;
652
653   if (diagnostic->kind == DK_PEDWARN)
654     {
655       diagnostic->kind = pedantic_warning_kind (context);
656       /* We do this to avoid giving the message for -pedantic-errors.  */
657       orig_diag_kind = diagnostic->kind;
658     }
659  
660   if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
661     return false;
662
663   if (context->lock > 0)
664     {
665       /* If we're reporting an ICE in the middle of some other error,
666          try to flush out the previous error, then let this one
667          through.  Don't do this more than once.  */
668       if (diagnostic->kind == DK_ICE && context->lock == 1)
669         pp_newline_and_flush (context->printer);
670       else
671         error_recursion (context);
672     }
673
674   /* If the user requested that warnings be treated as errors, so be
675      it.  Note that we do this before the next block so that
676      individual warnings can be overridden back to warnings with
677      -Wno-error=*.  */
678   if (context->warning_as_error_requested
679       && diagnostic->kind == DK_WARNING)
680     {
681       diagnostic->kind = DK_ERROR;
682     }
683
684   if (diagnostic->option_index
685       && diagnostic->option_index != permissive_error_option (context))
686     {
687       diagnostic_t diag_class = DK_UNSPECIFIED;
688
689       /* This tests if the user provided the appropriate -Wfoo or
690          -Wno-foo option.  */
691       if (! context->option_enabled (diagnostic->option_index,
692                                      context->option_state))
693         return false;
694
695       /* This tests for #pragma diagnostic changes.  */
696       if (context->n_classification_history > 0)
697         {
698           int i;
699           /* FIXME: Stupid search.  Optimize later. */
700           for (i = context->n_classification_history - 1; i >= 0; i --)
701             {
702               if (linemap_location_before_p
703                   (line_table,
704                    context->classification_history[i].location,
705                    location))
706                 {
707                   if (context->classification_history[i].kind == (int) DK_POP)
708                     {
709                       i = context->classification_history[i].option;
710                       continue;
711                     }
712                   if (context->classification_history[i].option == diagnostic->option_index)
713                     {
714                       diag_class = context->classification_history[i].kind;
715                       if (diag_class != DK_UNSPECIFIED)
716                         diagnostic->kind = diag_class;
717                       break;
718                     }
719                 }
720             }
721         }
722       /* This tests if the user provided the appropriate -Werror=foo
723          option.  */
724       if (diag_class == DK_UNSPECIFIED
725           && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
726         {
727           diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
728         }
729       /* This allows for future extensions, like temporarily disabling
730          warnings for ranges of source code.  */
731       if (diagnostic->kind == DK_IGNORED)
732         return false;
733     }
734
735   if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
736     context->some_warnings_are_errors = true;
737
738   context->lock++;
739
740   if (diagnostic->kind == DK_ICE)
741     {
742 #ifndef ENABLE_CHECKING
743       /* When not checking, ICEs are converted to fatal errors when an
744          error has already occurred.  This is counteracted by
745          abort_on_error.  */
746       if ((diagnostic_kind_count (context, DK_ERROR) > 0
747            || diagnostic_kind_count (context, DK_SORRY) > 0)
748           && !context->abort_on_error)
749         {
750           expanded_location s = expand_location (diagnostic->location);
751           fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
752                    s.file, s.line);
753           exit (ICE_EXIT_CODE);
754         }
755 #endif
756       if (context->internal_error)
757         (*context->internal_error) (context,
758                                     diagnostic->message.format_spec,
759                                     diagnostic->message.args_ptr);
760     }
761   if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
762     ++diagnostic_kind_count (context, DK_WERROR);
763   else
764     ++diagnostic_kind_count (context, diagnostic->kind);
765
766   saved_format_spec = diagnostic->message.format_spec;
767   if (context->show_option_requested)
768     {
769       char *option_text;
770
771       option_text = context->option_name (context, diagnostic->option_index,
772                                           orig_diag_kind, diagnostic->kind);
773
774       if (option_text)
775         {
776           diagnostic->message.format_spec
777             = ACONCAT ((diagnostic->message.format_spec,
778                         " ", 
779                         "[", option_text, "]",
780                         NULL));
781           free (option_text);
782         }
783     }
784   diagnostic->message.locus = &diagnostic->location;
785   diagnostic->message.x_data = &diagnostic->x_data;
786   diagnostic->x_data = NULL;
787   pp_format (context->printer, &diagnostic->message);
788   (*diagnostic_starter (context)) (context, diagnostic);
789   pp_output_formatted_text (context->printer);
790   diagnostic_show_locus (context, diagnostic);
791   (*diagnostic_finalizer (context)) (context, diagnostic);
792   pp_destroy_prefix (context->printer);
793   pp_newline_and_flush (context->printer);
794   diagnostic_action_after_output (context, diagnostic);
795   diagnostic->message.format_spec = saved_format_spec;
796   diagnostic->x_data = NULL;
797
798   context->lock--;
799
800   return true;
801 }
802
803 /* Given a partial pathname as input, return another pathname that
804    shares no directory elements with the pathname of __FILE__.  This
805    is used by fancy_abort() to print `Internal compiler error in expr.c'
806    instead of `Internal compiler error in ../../GCC/gcc/expr.c'.  */
807
808 const char *
809 trim_filename (const char *name)
810 {
811   static const char this_file[] = __FILE__;
812   const char *p = name, *q = this_file;
813
814   /* First skip any "../" in each filename.  This allows us to give a proper
815      reference to a file in a subdirectory.  */
816   while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
817     p += 3;
818
819   while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
820     q += 3;
821
822   /* Now skip any parts the two filenames have in common.  */
823   while (*p == *q && *p != 0 && *q != 0)
824     p++, q++;
825
826   /* Now go backwards until the previous directory separator.  */
827   while (p > name && !IS_DIR_SEPARATOR (p[-1]))
828     p--;
829
830   return p;
831 }
832 \f
833 /* Standard error reporting routines in increasing order of severity.
834    All of these take arguments like printf.  */
835
836 /* Text to be emitted verbatim to the error message stream; this
837    produces no prefix and disables line-wrapping.  Use rarely.  */
838 void
839 verbatim (const char *gmsgid, ...)
840 {
841   text_info text;
842   va_list ap;
843
844   va_start (ap, gmsgid);
845   text.err_no = errno;
846   text.args_ptr = &ap;
847   text.format_spec = _(gmsgid);
848   text.locus = NULL;
849   text.x_data = NULL;
850   pp_format_verbatim (global_dc->printer, &text);
851   pp_newline_and_flush (global_dc->printer);
852   va_end (ap);
853 }
854
855 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT.  */
856 void
857 diagnostic_append_note (diagnostic_context *context,
858                         location_t location,
859                         const char * gmsgid, ...)
860 {
861   diagnostic_info diagnostic;
862   va_list ap;
863   const char *saved_prefix;
864
865   va_start (ap, gmsgid);
866   diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
867   if (context->inhibit_notes_p)
868     {
869       va_end (ap);
870       return;
871     }
872   saved_prefix = pp_get_prefix (context->printer);
873   pp_set_prefix (context->printer,
874                  diagnostic_build_prefix (context, &diagnostic));
875   pp_newline (context->printer);
876   pp_format (context->printer, &diagnostic.message);
877   pp_output_formatted_text (context->printer);
878   pp_destroy_prefix (context->printer);
879   pp_set_prefix (context->printer, saved_prefix);
880   diagnostic_show_locus (context, &diagnostic);
881   va_end(ap);
882 }
883
884 bool
885 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
886                  const char *gmsgid, ...)
887 {
888   diagnostic_info diagnostic;
889   va_list ap;
890   bool ret;
891
892   va_start (ap, gmsgid);
893   if (kind == DK_PERMERROR)
894     {
895       diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
896                            permissive_error_kind (global_dc));
897       diagnostic.option_index = permissive_error_option (global_dc);
898     }
899   else {
900       diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
901       if (kind == DK_WARNING || kind == DK_PEDWARN)
902         diagnostic.option_index = opt;
903   }
904
905   ret = report_diagnostic (&diagnostic);
906   va_end (ap);
907   return ret;
908 }
909
910 /* An informative note at LOCATION.  Use this for additional details on an error
911    message.  */
912 void
913 inform (location_t location, const char *gmsgid, ...)
914 {
915   diagnostic_info diagnostic;
916   va_list ap;
917
918   va_start (ap, gmsgid);
919   diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
920   report_diagnostic (&diagnostic);
921   va_end (ap);
922 }
923
924 /* An informative note at LOCATION.  Use this for additional details on an
925    error message.  */
926 void
927 inform_n (location_t location, int n, const char *singular_gmsgid,
928           const char *plural_gmsgid, ...)
929 {
930   diagnostic_info diagnostic;
931   va_list ap;
932
933   va_start (ap, plural_gmsgid);
934   diagnostic_set_info_translated (&diagnostic,
935                                   ngettext (singular_gmsgid, plural_gmsgid, n),
936                                   &ap, location, DK_NOTE);
937   report_diagnostic (&diagnostic);
938   va_end (ap);
939 }
940
941 /* A warning at INPUT_LOCATION.  Use this for code which is correct according
942    to the relevant language specification but is likely to be buggy anyway.
943    Returns true if the warning was printed, false if it was inhibited.  */
944 bool
945 warning (int opt, const char *gmsgid, ...)
946 {
947   diagnostic_info diagnostic;
948   va_list ap;
949   bool ret;
950
951   va_start (ap, gmsgid);
952   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
953   diagnostic.option_index = opt;
954
955   ret = report_diagnostic (&diagnostic);
956   va_end (ap);
957   return ret;
958 }
959
960 /* A warning at LOCATION.  Use this for code which is correct according to the
961    relevant language specification but is likely to be buggy anyway.
962    Returns true if the warning was printed, false if it was inhibited.  */
963
964 bool
965 warning_at (location_t location, int opt, const char *gmsgid, ...)
966 {
967   diagnostic_info diagnostic;
968   va_list ap;
969   bool ret;
970
971   va_start (ap, gmsgid);
972   diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING);
973   diagnostic.option_index = opt;
974   ret = report_diagnostic (&diagnostic);
975   va_end (ap);
976   return ret;
977 }
978
979 /* A "pedantic" warning at LOCATION: issues a warning unless
980    -pedantic-errors was given on the command line, in which case it
981    issues an error.  Use this for diagnostics required by the relevant
982    language standard, if you have chosen not to make them errors.
983
984    Note that these diagnostics are issued independent of the setting
985    of the -Wpedantic command-line switch.  To get a warning enabled
986    only with that switch, use either "if (pedantic) pedwarn
987    (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)".  To get a
988    pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
989
990    Returns true if the warning was printed, false if it was inhibited.  */
991
992 bool
993 pedwarn (location_t location, int opt, const char *gmsgid, ...)
994 {
995   diagnostic_info diagnostic;
996   va_list ap;
997   bool ret;
998
999   va_start (ap, gmsgid);
1000   diagnostic_set_info (&diagnostic, gmsgid, &ap, location,  DK_PEDWARN);
1001   diagnostic.option_index = opt;
1002   ret = report_diagnostic (&diagnostic);
1003   va_end (ap);
1004   return ret;
1005 }
1006
1007 /* A "permissive" error at LOCATION: issues an error unless
1008    -fpermissive was given on the command line, in which case it issues
1009    a warning.  Use this for things that really should be errors but we
1010    want to support legacy code.
1011
1012    Returns true if the warning was printed, false if it was inhibited.  */
1013
1014 bool
1015 permerror (location_t location, const char *gmsgid, ...)
1016 {
1017   diagnostic_info diagnostic;
1018   va_list ap;
1019   bool ret;
1020
1021   va_start (ap, gmsgid);
1022   diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
1023                        permissive_error_kind (global_dc));
1024   diagnostic.option_index = permissive_error_option (global_dc);
1025   ret = report_diagnostic (&diagnostic);
1026   va_end (ap);
1027   return ret;
1028 }
1029
1030 /* A hard error: the code is definitely ill-formed, and an object file
1031    will not be produced.  */
1032 void
1033 error (const char *gmsgid, ...)
1034 {
1035   diagnostic_info diagnostic;
1036   va_list ap;
1037
1038   va_start (ap, gmsgid);
1039   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1040   report_diagnostic (&diagnostic);
1041   va_end (ap);
1042 }
1043
1044 /* A hard error: the code is definitely ill-formed, and an object file
1045    will not be produced.  */
1046 void
1047 error_n (location_t location, int n, const char *singular_gmsgid,
1048          const char *plural_gmsgid, ...)
1049 {
1050   diagnostic_info diagnostic;
1051   va_list ap;
1052
1053   va_start (ap, plural_gmsgid);
1054   diagnostic_set_info_translated (&diagnostic,
1055                                   ngettext (singular_gmsgid, plural_gmsgid, n),
1056                                   &ap, location, DK_ERROR);
1057   report_diagnostic (&diagnostic);
1058   va_end (ap);
1059 }
1060
1061 /* Same as ebove, but use location LOC instead of input_location.  */
1062 void
1063 error_at (location_t loc, const char *gmsgid, ...)
1064 {
1065   diagnostic_info diagnostic;
1066   va_list ap;
1067
1068   va_start (ap, gmsgid);
1069   diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1070   report_diagnostic (&diagnostic);
1071   va_end (ap);
1072 }
1073
1074 /* "Sorry, not implemented."  Use for a language feature which is
1075    required by the relevant specification but not implemented by GCC.
1076    An object file will not be produced.  */
1077 void
1078 sorry (const char *gmsgid, ...)
1079 {
1080   diagnostic_info diagnostic;
1081   va_list ap;
1082
1083   va_start (ap, gmsgid);
1084   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1085   report_diagnostic (&diagnostic);
1086   va_end (ap);
1087 }
1088
1089 /* Return true if an error or a "sorry" has been seen.  Various
1090    processing is disabled after errors.  */
1091 bool
1092 seen_error (void)
1093 {
1094   return errorcount || sorrycount;
1095 }
1096
1097 /* An error which is severe enough that we make no attempt to
1098    continue.  Do not use this for internal consistency checks; that's
1099    internal_error.  Use of this function should be rare.  */
1100 void
1101 fatal_error (const char *gmsgid, ...)
1102 {
1103   diagnostic_info diagnostic;
1104   va_list ap;
1105
1106   va_start (ap, gmsgid);
1107   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
1108   report_diagnostic (&diagnostic);
1109   va_end (ap);
1110
1111   gcc_unreachable ();
1112 }
1113
1114 /* An internal consistency check has failed.  We make no attempt to
1115    continue.  Note that unless there is debugging value to be had from
1116    a more specific message, or some other good reason, you should use
1117    abort () instead of calling this function directly.  */
1118 void
1119 internal_error (const char *gmsgid, ...)
1120 {
1121   diagnostic_info diagnostic;
1122   va_list ap;
1123
1124   va_start (ap, gmsgid);
1125   diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1126   report_diagnostic (&diagnostic);
1127   va_end (ap);
1128
1129   gcc_unreachable ();
1130 }
1131 \f
1132 /* Special case error functions.  Most are implemented in terms of the
1133    above, or should be.  */
1134
1135 /* Print a diagnostic MSGID on FILE.  This is just fprintf, except it
1136    runs its second argument through gettext.  */
1137 void
1138 fnotice (FILE *file, const char *cmsgid, ...)
1139 {
1140   va_list ap;
1141
1142   va_start (ap, cmsgid);
1143   vfprintf (file, _(cmsgid), ap);
1144   va_end (ap);
1145 }
1146
1147 /* Inform the user that an error occurred while trying to report some
1148    other error.  This indicates catastrophic internal inconsistencies,
1149    so give up now.  But do try to flush out the previous error.
1150    This mustn't use internal_error, that will cause infinite recursion.  */
1151
1152 static void
1153 error_recursion (diagnostic_context *context)
1154 {
1155   diagnostic_info diagnostic;
1156
1157   if (context->lock < 3)
1158     pp_newline_and_flush (context->printer);
1159
1160   fnotice (stderr,
1161            "Internal compiler error: Error reporting routines re-entered.\n");
1162
1163   /* Call diagnostic_action_after_output to get the "please submit a bug
1164      report" message.  It only looks at the kind field of diagnostic_info.  */
1165   diagnostic.kind = DK_ICE;
1166   diagnostic_action_after_output (context, &diagnostic);
1167
1168   /* Do not use gcc_unreachable here; that goes through internal_error
1169      and therefore would cause infinite recursion.  */
1170   real_abort ();
1171 }
1172
1173 /* Report an internal compiler error in a friendly manner.  This is
1174    the function that gets called upon use of abort() in the source
1175    code generally, thanks to a special macro.  */
1176
1177 void
1178 fancy_abort (const char *file, int line, const char *function)
1179 {
1180   internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1181 }
1182
1183 /* Really call the system 'abort'.  This has to go right at the end of
1184    this file, so that there are no functions after it that call abort
1185    and get the system abort instead of our macro.  */
1186 #undef abort
1187 static void
1188 real_abort (void)
1189 {
1190   abort ();
1191 }