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