ipa-inline-analysis.c (simple_edge_hints): Fix check for cross-module inlining.
[official-gcc.git] / gcc / diagnostic.c
blob2196406f8814f6ff950c41c2610e0445a44a8e00
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>
5 This file is part of GCC.
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.
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.
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/>. */
22 /* This file implements the language independent aspect of diagnostic
23 message module. */
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"
36 #ifdef HAVE_TERMIOS_H
37 # include <termios.h>
38 #endif
40 #ifdef GWINSZ_IN_SYS_IOCTL
41 # include <sys/ioctl.h>
42 #endif
44 #include <new> // For placement new.
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)
51 /* Prototypes. */
52 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
54 static void real_abort (void) ATTRIBUTE_NORETURN;
56 /* Name of program invoked, sans directories. */
58 const char *progname;
60 /* A diagnostic_context surrogate for stderr. */
61 static diagnostic_context global_diagnostic_context;
62 diagnostic_context *global_dc = &global_diagnostic_context;
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, ...)
69 char *str;
70 va_list ap;
72 va_start (ap, msg);
73 str = xvasprintf (msg, ap);
74 va_end (ap);
76 return str;
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)
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);
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)
97 const char * s = getenv ("COLUMNS");
98 if (s != NULL) {
99 int n = atoi (s);
100 if (n > 0)
101 return n;
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
111 return INT_MAX;
114 /* Set caret_max_width to value. */
115 void
116 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
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);
123 if (value <= 0)
124 value = INT_MAX;
126 context->caret_max_width = value;
129 /* Initialize the diagnostic message outputting machinery. */
130 void
131 diagnostic_initialize (diagnostic_context *context, int n_opts)
133 int i;
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 ();
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;
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. */
177 void
178 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
180 /* value == -1 is the default value. */
181 if (value < 0)
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)
190 if (!getenv ("GCC_COLORS"))
191 return;
192 value = DIAGNOSTICS_COLOR_AUTO;
194 else
195 value = DIAGNOSTICS_COLOR_DEFAULT;
197 pp_show_color (context->printer)
198 = colorize_init ((diagnostic_color_rule_t) value);
201 /* Do any cleaning up required after the last diagnostic is emitted. */
203 void
204 diagnostic_finish (diagnostic_context *context)
206 /* Some of the errors may actually have been warnings. */
207 if (context->some_warnings_are_errors)
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);
222 diagnostic_file_cache_fini ();
224 XDELETEVEC (context->classify_diagnostic);
225 context->classify_diagnostic = NULL;
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;
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)
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;
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)
257 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
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)
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"
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
278 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
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;
285 if (diagnostic_kind_color[diagnostic->kind])
287 text_cs = colorize_start (pp_show_color (pp),
288 diagnostic_kind_color[diagnostic->kind]);
289 text_ce = colorize_stop (pp_show_color (pp));
291 locus_cs = colorize_start (pp_show_color (pp), "locus");
292 locus_ce = colorize_stop (pp_show_color (pp));
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));
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)
319 int right_margin = 10;
320 int column = *column_p;
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)
327 line += column - right_margin;
328 *column_p = right_margin;
330 return line;
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)
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;
347 if (!context->show_caret
348 || diagnostic->location <= BUILTINS_LOCATION
349 || diagnostic->location == context->last_location)
350 return;
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;
358 max_width = context->caret_max_width;
359 line = adjust_line (line, line_width, max_width, &(s.column));
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)
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++;
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));
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;
389 /* Functions at which to stop the backtrace print. It's not
390 particularly helpful to print the callers of these functions. */
392 static const char * const bt_stop[] =
394 "main",
395 "toplev::main",
396 "execute_one_pass",
397 "compile_file",
400 /* A callback function passed to the backtrace_full function. */
402 static int
403 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
404 const char *function)
406 int *pcount = (int *) data;
408 /* If we don't have any useful information, don't print
409 anything. */
410 if (filename == NULL && function == NULL)
411 return 0;
413 /* Skip functions in diagnostic.c. */
414 if (*pcount == 0
415 && filename != NULL
416 && strcmp (lbasename (filename), "diagnostic.c") == 0)
417 return 0;
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)
423 /* Returning a non-zero value stops the backtrace. */
424 return 1;
426 ++*pcount;
428 char *alc = NULL;
429 if (function != NULL)
431 char *str = cplus_demangle_v3 (function,
432 (DMGL_VERBOSE | DMGL_ANSI
433 | DMGL_GNU_V3 | DMGL_PARAMS));
434 if (str != NULL)
436 alc = str;
437 function = str;
440 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
442 size_t len = strlen (bt_stop[i]);
443 if (strncmp (function, bt_stop[i], len) == 0
444 && (function[len] == '\0' || function[len] == '('))
446 if (alc != NULL)
447 free (alc);
448 /* Returning a non-zero value stops the backtrace. */
449 return 1;
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);
460 if (alc != NULL)
461 free (alc);
463 return 0;
466 /* A callback function passed to the backtrace_full function. This is
467 called if backtrace_full has an error. */
469 static void
470 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
472 if (errnum < 0)
474 /* This means that no debug info was available. Just quietly
475 skip printing backtrace info. */
476 return;
478 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
479 errnum == 0 ? "" : xstrerror (errnum));
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)
488 switch (diag_kind)
490 case DK_DEBUG:
491 case DK_NOTE:
492 case DK_ANACHRONISM:
493 case DK_WARNING:
494 break;
496 case DK_ERROR:
497 case DK_SORRY:
498 if (context->abort_on_error)
499 real_abort ();
500 if (context->fatal_errors)
502 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
503 diagnostic_finish (context);
504 exit (FATAL_EXIT_CODE);
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))
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);
518 break;
520 case DK_ICE:
521 case DK_ICE_NOBT:
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);
531 if (context->abort_on_error)
532 real_abort ();
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);
542 exit (ICE_EXIT_CODE);
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);
552 default:
553 gcc_unreachable ();
557 void
558 diagnostic_report_current_module (diagnostic_context *context, location_t where)
560 const struct line_map *map = NULL;
562 if (pp_needs_newline (context->printer))
564 pp_newline (context->printer);
565 pp_needs_newline (context->printer) = false;
568 if (where <= BUILTINS_LOCATION)
569 return;
571 linemap_resolve_location (line_table, where,
572 LRK_MACRO_DEFINITION_LOCATION,
573 &map);
575 if (map && diagnostic_last_module_changed (context, map))
577 diagnostic_set_last_module (context, map);
578 if (! MAIN_FILE_P (map))
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))
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));
597 pp_verbatim (context->printer, ":");
598 pp_newline (context->printer);
603 void
604 default_diagnostic_starter (diagnostic_context *context,
605 diagnostic_info *diagnostic)
607 diagnostic_report_current_module (context, diagnostic->location);
608 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
609 diagnostic));
612 void
613 default_diagnostic_finalizer (diagnostic_context *context,
614 diagnostic_info *diagnostic)
616 diagnostic_show_locus (context, diagnostic);
617 pp_destroy_prefix (context->printer);
618 pp_newline_and_flush (context->printer);
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)
631 diagnostic_t old_kind;
633 if (option_index < 0
634 || option_index >= context->n_opts
635 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
636 return DK_UNSPECIFIED;
638 old_kind = context->classify_diagnostic[option_index];
640 /* Handle pragmas separately, since we need to keep track of *where*
641 the pragmas were. */
642 if (where != UNKNOWN_LOCATION)
644 int i;
646 /* Record the command-line status, so we can reset it back on DK_POP. */
647 if (old_kind == DK_UNSPECIFIED)
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;
655 for (i = context->n_classification_history - 1; i >= 0; i --)
656 if (context->classification_history[i].option == option_index)
658 old_kind = context->classification_history[i].kind;
659 break;
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 ++;
671 else
672 context->classify_diagnostic[option_index] = new_kind;
674 return old_kind;
677 /* Save all diagnostic classifications in a stack. */
678 void
679 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
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;
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)
690 int jump_to;
691 int i;
693 if (context->n_push)
694 jump_to = context->push_list [-- context->n_push];
695 else
696 jump_to = 0;
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 ++;
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. */
715 bool
716 diagnostic_report_diagnostic (diagnostic_context *context,
717 diagnostic_info *diagnostic)
719 location_t location = diagnostic->location;
720 diagnostic_t orig_diag_kind = diagnostic->kind;
721 const char *saved_format_spec;
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;
729 if (diagnostic->kind == DK_PEDWARN)
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;
736 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
737 return false;
739 if (context->lock > 0)
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);
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)
758 diagnostic->kind = DK_ERROR;
761 if (diagnostic->option_index
762 && diagnostic->option_index != permissive_error_option (context))
764 diagnostic_t diag_class = DK_UNSPECIFIED;
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;
772 /* This tests for #pragma diagnostic changes. */
773 if (context->n_classification_history > 0)
775 /* FIXME: Stupid search. Optimize later. */
776 for (int i = context->n_classification_history - 1; i >= 0; i --)
778 if (linemap_location_before_p
779 (line_table,
780 context->classification_history[i].location,
781 location))
783 if (context->classification_history[i].kind == (int) DK_POP)
785 i = context->classification_history[i].option;
786 continue;
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)
792 diag_class = context->classification_history[i].kind;
793 if (diag_class != DK_UNSPECIFIED)
794 diagnostic->kind = diag_class;
795 break;
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)
805 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
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;
813 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR)
814 context->some_warnings_are_errors = true;
816 context->lock++;
818 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
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)
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);
833 #endif
834 if (context->internal_error)
835 (*context->internal_error) (context,
836 diagnostic->message.format_spec,
837 diagnostic->message.args_ptr);
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);
844 saved_format_spec = diagnostic->message.format_spec;
845 if (context->show_option_requested)
847 char *option_text;
849 option_text = context->option_name (context, diagnostic->option_index,
850 orig_diag_kind, diagnostic->kind);
852 if (option_text)
854 diagnostic->message.format_spec
855 = ACONCAT ((diagnostic->message.format_spec,
856 " ",
857 "[", option_text, "]",
858 NULL));
859 free (option_text);
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;
873 context->lock--;
875 return true;
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'. */
883 const char *
884 trim_filename (const char *name)
886 static const char this_file[] = __FILE__;
887 const char *p = name, *q = this_file;
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;
894 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
895 q += 3;
897 /* Now skip any parts the two filenames have in common. */
898 while (*p == *q && *p != 0 && *q != 0)
899 p++, q++;
901 /* Now go backwards until the previous directory separator. */
902 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
903 p--;
905 return p;
908 /* Standard error reporting routines in increasing order of severity.
909 All of these take arguments like printf. */
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, ...)
916 text_info text;
917 va_list ap;
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);
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, ...)
936 diagnostic_info diagnostic;
937 va_list ap;
938 const char *saved_prefix;
940 va_start (ap, gmsgid);
941 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
942 if (context->inhibit_notes_p)
944 va_end (ap);
945 return;
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);
959 bool
960 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
961 const char *gmsgid, ...)
963 diagnostic_info diagnostic;
964 va_list ap;
965 bool ret;
967 va_start (ap, gmsgid);
968 if (kind == DK_PERMERROR)
970 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
971 permissive_error_kind (global_dc));
972 diagnostic.option_index = permissive_error_option (global_dc);
974 else {
975 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind);
976 if (kind == DK_WARNING || kind == DK_PEDWARN)
977 diagnostic.option_index = opt;
980 ret = report_diagnostic (&diagnostic);
981 va_end (ap);
982 return ret;
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, ...)
990 diagnostic_info diagnostic;
991 va_list ap;
993 va_start (ap, gmsgid);
994 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE);
995 report_diagnostic (&diagnostic);
996 va_end (ap);
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, ...)
1005 diagnostic_info diagnostic;
1006 va_list ap;
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);
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, ...)
1022 diagnostic_info diagnostic;
1023 va_list ap;
1024 bool ret;
1026 va_start (ap, gmsgid);
1027 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
1028 diagnostic.option_index = opt;
1030 ret = report_diagnostic (&diagnostic);
1031 va_end (ap);
1032 return ret;
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. */
1039 bool
1040 warning_at (location_t location, int opt, const char *gmsgid, ...)
1042 diagnostic_info diagnostic;
1043 va_list ap;
1044 bool ret;
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;
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. */
1058 bool
1059 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1060 const char *plural_gmsgid, ...)
1062 diagnostic_info diagnostic;
1063 va_list ap;
1064 bool ret;
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;
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.
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,...)".
1087 Returns true if the warning was printed, false if it was inhibited. */
1089 bool
1090 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1092 diagnostic_info diagnostic;
1093 va_list ap;
1094 bool ret;
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;
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.
1109 Returns true if the warning was printed, false if it was inhibited. */
1111 bool
1112 permerror (location_t location, const char *gmsgid, ...)
1114 diagnostic_info diagnostic;
1115 va_list ap;
1116 bool ret;
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;
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, ...)
1132 diagnostic_info diagnostic;
1133 va_list ap;
1135 va_start (ap, gmsgid);
1136 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
1137 report_diagnostic (&diagnostic);
1138 va_end (ap);
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, ...)
1147 diagnostic_info diagnostic;
1148 va_list ap;
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);
1158 /* Same as ebove, but use location LOC instead of input_location. */
1159 void
1160 error_at (location_t loc, const char *gmsgid, ...)
1162 diagnostic_info diagnostic;
1163 va_list ap;
1165 va_start (ap, gmsgid);
1166 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR);
1167 report_diagnostic (&diagnostic);
1168 va_end (ap);
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, ...)
1177 diagnostic_info diagnostic;
1178 va_list ap;
1180 va_start (ap, gmsgid);
1181 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
1182 report_diagnostic (&diagnostic);
1183 va_end (ap);
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)
1191 return errorcount || sorrycount;
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, ...)
1200 diagnostic_info diagnostic;
1201 va_list ap;
1203 va_start (ap, gmsgid);
1204 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_FATAL);
1205 report_diagnostic (&diagnostic);
1206 va_end (ap);
1208 gcc_unreachable ();
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, ...)
1218 diagnostic_info diagnostic;
1219 va_list ap;
1221 va_start (ap, gmsgid);
1222 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
1223 report_diagnostic (&diagnostic);
1224 va_end (ap);
1226 gcc_unreachable ();
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, ...)
1235 diagnostic_info diagnostic;
1236 va_list ap;
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);
1243 gcc_unreachable ();
1246 /* Special case error functions. Most are implemented in terms of the
1247 above, or should be. */
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, ...)
1254 va_list ap;
1256 va_start (ap, cmsgid);
1257 vfprintf (file, _(cmsgid), ap);
1258 va_end (ap);
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. */
1266 static void
1267 error_recursion (diagnostic_context *context)
1269 if (context->lock < 3)
1270 pp_newline_and_flush (context->printer);
1272 fnotice (stderr,
1273 "Internal compiler error: Error reporting routines re-entered.\n");
1275 /* Call diagnostic_action_after_output to get the "please submit a bug
1276 report" message. */
1277 diagnostic_action_after_output (context, DK_ICE);
1279 /* Do not use gcc_unreachable here; that goes through internal_error
1280 and therefore would cause infinite recursion. */
1281 real_abort ();
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. */
1288 void
1289 fancy_abort (const char *file, int line, const char *function)
1291 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
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)
1301 abort ();