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