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