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