PR c++/68795: fix uninitialized close_paren_loc in cp_parser_postfix_expression
[gcc.git] / gcc / diagnostic.c
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999-2016 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 < rich_location::MAX_RANGES; 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, rich_location *richloc,
238 diagnostic_t kind)
239 {
240 gcc_assert (richloc);
241 diagnostic->message.err_no = errno;
242 diagnostic->message.args_ptr = args;
243 diagnostic->message.format_spec = msg;
244 diagnostic->message.m_richloc = richloc;
245 diagnostic->richloc = richloc;
246 diagnostic->kind = kind;
247 diagnostic->option_index = 0;
248 }
249
250 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
251 translated. */
252 void
253 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
254 va_list *args, rich_location *richloc,
255 diagnostic_t kind)
256 {
257 gcc_assert (richloc);
258 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
259 }
260
261 static const char *const diagnostic_kind_color[] = {
262 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
263 #include "diagnostic.def"
264 #undef DEFINE_DIAGNOSTIC_KIND
265 NULL
266 };
267
268 /* Get a color name for diagnostics of type KIND
269 Result could be NULL. */
270
271 const char *
272 diagnostic_get_color_for_kind (diagnostic_t kind)
273 {
274 return diagnostic_kind_color[kind];
275 }
276
277 /* Return a malloc'd string describing a location. The caller is
278 responsible for freeing the memory. */
279 char *
280 diagnostic_build_prefix (diagnostic_context *context,
281 const diagnostic_info *diagnostic)
282 {
283 static const char *const diagnostic_kind_text[] = {
284 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
285 #include "diagnostic.def"
286 #undef DEFINE_DIAGNOSTIC_KIND
287 "must-not-happen"
288 };
289 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
290
291 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
292 const char *text_cs = "", *text_ce = "";
293 const char *locus_cs, *locus_ce;
294 pretty_printer *pp = context->printer;
295
296 if (diagnostic_kind_color[diagnostic->kind])
297 {
298 text_cs = colorize_start (pp_show_color (pp),
299 diagnostic_kind_color[diagnostic->kind]);
300 text_ce = colorize_stop (pp_show_color (pp));
301 }
302 locus_cs = colorize_start (pp_show_color (pp), "locus");
303 locus_ce = colorize_stop (pp_show_color (pp));
304
305 expanded_location s = diagnostic_expand_location (diagnostic);
306 return
307 (s.file == NULL
308 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce,
309 text_cs, text, text_ce)
310 : !strcmp (s.file, N_("<built-in>"))
311 ? build_message_string ("%s%s:%s %s%s%s", locus_cs, s.file, locus_ce,
312 text_cs, text, text_ce)
313 : context->show_column
314 ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line,
315 s.column, locus_ce, text_cs, text, text_ce)
316 : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line,
317 locus_ce, text_cs, text, text_ce));
318 }
319
320 /* Functions at which to stop the backtrace print. It's not
321 particularly helpful to print the callers of these functions. */
322
323 static const char * const bt_stop[] =
324 {
325 "main",
326 "toplev::main",
327 "execute_one_pass",
328 "compile_file",
329 };
330
331 /* A callback function passed to the backtrace_full function. */
332
333 static int
334 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
335 const char *function)
336 {
337 int *pcount = (int *) data;
338
339 /* If we don't have any useful information, don't print
340 anything. */
341 if (filename == NULL && function == NULL)
342 return 0;
343
344 /* Skip functions in diagnostic.c. */
345 if (*pcount == 0
346 && filename != NULL
347 && strcmp (lbasename (filename), "diagnostic.c") == 0)
348 return 0;
349
350 /* Print up to 20 functions. We could make this a --param, but
351 since this is only for debugging just use a constant for now. */
352 if (*pcount >= 20)
353 {
354 /* Returning a non-zero value stops the backtrace. */
355 return 1;
356 }
357 ++*pcount;
358
359 char *alc = NULL;
360 if (function != NULL)
361 {
362 char *str = cplus_demangle_v3 (function,
363 (DMGL_VERBOSE | DMGL_ANSI
364 | DMGL_GNU_V3 | DMGL_PARAMS));
365 if (str != NULL)
366 {
367 alc = str;
368 function = str;
369 }
370
371 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
372 {
373 size_t len = strlen (bt_stop[i]);
374 if (strncmp (function, bt_stop[i], len) == 0
375 && (function[len] == '\0' || function[len] == '('))
376 {
377 if (alc != NULL)
378 free (alc);
379 /* Returning a non-zero value stops the backtrace. */
380 return 1;
381 }
382 }
383 }
384
385 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
386 (unsigned long) pc,
387 function == NULL ? "???" : function,
388 filename == NULL ? "???" : filename,
389 lineno);
390
391 if (alc != NULL)
392 free (alc);
393
394 return 0;
395 }
396
397 /* A callback function passed to the backtrace_full function. This is
398 called if backtrace_full has an error. */
399
400 static void
401 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
402 {
403 if (errnum < 0)
404 {
405 /* This means that no debug info was available. Just quietly
406 skip printing backtrace info. */
407 return;
408 }
409 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
410 errnum == 0 ? "" : xstrerror (errnum));
411 }
412
413 /* Take any action which is expected to happen after the diagnostic
414 is written out. This function does not always return. */
415 void
416 diagnostic_action_after_output (diagnostic_context *context,
417 diagnostic_t diag_kind)
418 {
419 switch (diag_kind)
420 {
421 case DK_DEBUG:
422 case DK_NOTE:
423 case DK_ANACHRONISM:
424 case DK_WARNING:
425 break;
426
427 case DK_ERROR:
428 case DK_SORRY:
429 if (context->abort_on_error)
430 real_abort ();
431 if (context->fatal_errors)
432 {
433 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
434 diagnostic_finish (context);
435 exit (FATAL_EXIT_CODE);
436 }
437 if (context->max_errors != 0
438 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR)
439 + diagnostic_kind_count (context, DK_SORRY)
440 + diagnostic_kind_count (context, DK_WERROR))
441 >= context->max_errors))
442 {
443 fnotice (stderr,
444 "compilation terminated due to -fmax-errors=%u.\n",
445 context->max_errors);
446 diagnostic_finish (context);
447 exit (FATAL_EXIT_CODE);
448 }
449 break;
450
451 case DK_ICE:
452 case DK_ICE_NOBT:
453 {
454 struct backtrace_state *state = NULL;
455 if (diag_kind == DK_ICE)
456 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
457 int count = 0;
458 if (state != NULL)
459 backtrace_full (state, 2, bt_callback, bt_err_callback,
460 (void *) &count);
461
462 if (context->abort_on_error)
463 real_abort ();
464
465 fnotice (stderr, "Please submit a full bug report,\n"
466 "with preprocessed source if appropriate.\n");
467 if (count > 0)
468 fnotice (stderr,
469 ("Please include the complete backtrace "
470 "with any bug report.\n"));
471 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
472
473 exit (ICE_EXIT_CODE);
474 }
475
476 case DK_FATAL:
477 if (context->abort_on_error)
478 real_abort ();
479 diagnostic_finish (context);
480 fnotice (stderr, "compilation terminated.\n");
481 exit (FATAL_EXIT_CODE);
482
483 default:
484 gcc_unreachable ();
485 }
486 }
487
488 void
489 diagnostic_report_current_module (diagnostic_context *context, location_t where)
490 {
491 const line_map_ordinary *map = NULL;
492
493 if (pp_needs_newline (context->printer))
494 {
495 pp_newline (context->printer);
496 pp_needs_newline (context->printer) = false;
497 }
498
499 if (where <= BUILTINS_LOCATION)
500 return;
501
502 linemap_resolve_location (line_table, where,
503 LRK_MACRO_DEFINITION_LOCATION,
504 &map);
505
506 if (map && diagnostic_last_module_changed (context, map))
507 {
508 diagnostic_set_last_module (context, map);
509 if (! MAIN_FILE_P (map))
510 {
511 map = INCLUDED_FROM (line_table, map);
512 if (context->show_column)
513 pp_verbatim (context->printer,
514 "In file included from %r%s:%d:%d%R", "locus",
515 LINEMAP_FILE (map),
516 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
517 else
518 pp_verbatim (context->printer,
519 "In file included from %r%s:%d%R", "locus",
520 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
521 while (! MAIN_FILE_P (map))
522 {
523 map = INCLUDED_FROM (line_table, map);
524 pp_verbatim (context->printer,
525 ",\n from %r%s:%d%R", "locus",
526 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
527 }
528 pp_verbatim (context->printer, ":");
529 pp_newline (context->printer);
530 }
531 }
532 }
533
534 void
535 default_diagnostic_starter (diagnostic_context *context,
536 diagnostic_info *diagnostic)
537 {
538 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
539 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
540 diagnostic));
541 }
542
543 void
544 default_diagnostic_finalizer (diagnostic_context *context,
545 diagnostic_info *diagnostic)
546 {
547 diagnostic_show_locus (context, diagnostic);
548 pp_destroy_prefix (context->printer);
549 pp_newline_and_flush (context->printer);
550 }
551
552 /* Interface to specify diagnostic kind overrides. Returns the
553 previous setting, or DK_UNSPECIFIED if the parameters are out of
554 range. If OPTION_INDEX is zero, the new setting is for all the
555 diagnostics. */
556 diagnostic_t
557 diagnostic_classify_diagnostic (diagnostic_context *context,
558 int option_index,
559 diagnostic_t new_kind,
560 location_t where)
561 {
562 diagnostic_t old_kind;
563
564 if (option_index < 0
565 || option_index >= context->n_opts
566 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
567 return DK_UNSPECIFIED;
568
569 old_kind = context->classify_diagnostic[option_index];
570
571 /* Handle pragmas separately, since we need to keep track of *where*
572 the pragmas were. */
573 if (where != UNKNOWN_LOCATION)
574 {
575 int i;
576
577 /* Record the command-line status, so we can reset it back on DK_POP. */
578 if (old_kind == DK_UNSPECIFIED)
579 {
580 old_kind = !context->option_enabled (option_index,
581 context->option_state)
582 ? DK_IGNORED : (context->warning_as_error_requested
583 ? DK_ERROR : DK_WARNING);
584 context->classify_diagnostic[option_index] = old_kind;
585 }
586
587 for (i = context->n_classification_history - 1; i >= 0; i --)
588 if (context->classification_history[i].option == option_index)
589 {
590 old_kind = context->classification_history[i].kind;
591 break;
592 }
593
594 i = context->n_classification_history;
595 context->classification_history =
596 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
597 * sizeof (diagnostic_classification_change_t));
598 context->classification_history[i].location = where;
599 context->classification_history[i].option = option_index;
600 context->classification_history[i].kind = new_kind;
601 context->n_classification_history ++;
602 }
603 else
604 context->classify_diagnostic[option_index] = new_kind;
605
606 return old_kind;
607 }
608
609 /* Save all diagnostic classifications in a stack. */
610 void
611 diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED)
612 {
613 context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int));
614 context->push_list[context->n_push ++] = context->n_classification_history;
615 }
616
617 /* Restore the topmost classification set off the stack. If the stack
618 is empty, revert to the state based on command line parameters. */
619 void
620 diagnostic_pop_diagnostics (diagnostic_context *context, location_t where)
621 {
622 int jump_to;
623 int i;
624
625 if (context->n_push)
626 jump_to = context->push_list [-- context->n_push];
627 else
628 jump_to = 0;
629
630 i = context->n_classification_history;
631 context->classification_history =
632 (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1)
633 * sizeof (diagnostic_classification_change_t));
634 context->classification_history[i].location = where;
635 context->classification_history[i].option = jump_to;
636 context->classification_history[i].kind = DK_POP;
637 context->n_classification_history ++;
638 }
639
640 /* Report a diagnostic message (an error or a warning) as specified by
641 DC. This function is *the* subroutine in terms of which front-ends
642 should implement their specific diagnostic handling modules. The
643 front-end independent format specifiers are exactly those described
644 in the documentation of output_format.
645 Return true if a diagnostic was printed, false otherwise. */
646
647 bool
648 diagnostic_report_diagnostic (diagnostic_context *context,
649 diagnostic_info *diagnostic)
650 {
651 location_t location = diagnostic_location (diagnostic);
652 diagnostic_t orig_diag_kind = diagnostic->kind;
653 const char *saved_format_spec;
654
655 /* Give preference to being able to inhibit warnings, before they
656 get reclassified to something else. */
657 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
658 && !diagnostic_report_warnings_p (context, location))
659 return false;
660
661 if (diagnostic->kind == DK_PEDWARN)
662 {
663 diagnostic->kind = pedantic_warning_kind (context);
664 /* We do this to avoid giving the message for -pedantic-errors. */
665 orig_diag_kind = diagnostic->kind;
666 }
667
668 if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p)
669 return false;
670
671 if (context->lock > 0)
672 {
673 /* If we're reporting an ICE in the middle of some other error,
674 try to flush out the previous error, then let this one
675 through. Don't do this more than once. */
676 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
677 && context->lock == 1)
678 pp_newline_and_flush (context->printer);
679 else
680 error_recursion (context);
681 }
682
683 /* If the user requested that warnings be treated as errors, so be
684 it. Note that we do this before the next block so that
685 individual warnings can be overridden back to warnings with
686 -Wno-error=*. */
687 if (context->warning_as_error_requested
688 && diagnostic->kind == DK_WARNING)
689 {
690 diagnostic->kind = DK_ERROR;
691 }
692
693 if (diagnostic->option_index
694 && diagnostic->option_index != permissive_error_option (context))
695 {
696 diagnostic_t diag_class = DK_UNSPECIFIED;
697
698 /* This tests if the user provided the appropriate -Wfoo or
699 -Wno-foo option. */
700 if (! context->option_enabled (diagnostic->option_index,
701 context->option_state))
702 return false;
703
704 /* This tests for #pragma diagnostic changes. */
705 if (context->n_classification_history > 0)
706 {
707 /* FIXME: Stupid search. Optimize later. */
708 for (int i = context->n_classification_history - 1; i >= 0; i --)
709 {
710 if (linemap_location_before_p
711 (line_table,
712 context->classification_history[i].location,
713 location))
714 {
715 if (context->classification_history[i].kind == (int) DK_POP)
716 {
717 i = context->classification_history[i].option;
718 continue;
719 }
720 int option = context->classification_history[i].option;
721 /* The option 0 is for all the diagnostics. */
722 if (option == 0 || option == diagnostic->option_index)
723 {
724 diag_class = context->classification_history[i].kind;
725 if (diag_class != DK_UNSPECIFIED)
726 diagnostic->kind = diag_class;
727 break;
728 }
729 }
730 }
731 }
732 /* This tests if the user provided the appropriate -Werror=foo
733 option. */
734 if (diag_class == DK_UNSPECIFIED
735 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED)
736 {
737 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index];
738 }
739 /* This allows for future extensions, like temporarily disabling
740 warnings for ranges of source code. */
741 if (diagnostic->kind == DK_IGNORED)
742 return false;
743 }
744
745 context->lock++;
746
747 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
748 {
749 /* When not checking, ICEs are converted to fatal errors when an
750 error has already occurred. This is counteracted by
751 abort_on_error. */
752 if (!CHECKING_P
753 && (diagnostic_kind_count (context, DK_ERROR) > 0
754 || diagnostic_kind_count (context, DK_SORRY) > 0)
755 && !context->abort_on_error)
756 {
757 expanded_location s
758 = expand_location (diagnostic_location (diagnostic));
759 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
760 s.file, s.line);
761 exit (ICE_EXIT_CODE);
762 }
763 if (context->internal_error)
764 (*context->internal_error) (context,
765 diagnostic->message.format_spec,
766 diagnostic->message.args_ptr);
767 }
768 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
769 ++diagnostic_kind_count (context, DK_WERROR);
770 else
771 ++diagnostic_kind_count (context, diagnostic->kind);
772
773 saved_format_spec = diagnostic->message.format_spec;
774 if (context->show_option_requested)
775 {
776 char *option_text;
777
778 option_text = context->option_name (context, diagnostic->option_index,
779 orig_diag_kind, diagnostic->kind);
780
781 if (option_text)
782 {
783 const char *cs
784 = colorize_start (pp_show_color (context->printer),
785 diagnostic_kind_color[diagnostic->kind]);
786 const char *ce = colorize_stop (pp_show_color (context->printer));
787 diagnostic->message.format_spec
788 = ACONCAT ((diagnostic->message.format_spec,
789 " ",
790 "[", cs, option_text, ce, "]",
791 NULL));
792 free (option_text);
793 }
794 }
795 diagnostic->message.x_data = &diagnostic->x_data;
796 diagnostic->x_data = NULL;
797 pp_format (context->printer, &diagnostic->message);
798 (*diagnostic_starter (context)) (context, diagnostic);
799 pp_output_formatted_text (context->printer);
800 (*diagnostic_finalizer (context)) (context, diagnostic);
801 diagnostic_action_after_output (context, diagnostic->kind);
802 diagnostic->message.format_spec = saved_format_spec;
803 diagnostic->x_data = NULL;
804
805 context->lock--;
806
807 return true;
808 }
809
810 /* Given a partial pathname as input, return another pathname that
811 shares no directory elements with the pathname of __FILE__. This
812 is used by fancy_abort() to print `Internal compiler error in expr.c'
813 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
814
815 const char *
816 trim_filename (const char *name)
817 {
818 static const char this_file[] = __FILE__;
819 const char *p = name, *q = this_file;
820
821 /* First skip any "../" in each filename. This allows us to give a proper
822 reference to a file in a subdirectory. */
823 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
824 p += 3;
825
826 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
827 q += 3;
828
829 /* Now skip any parts the two filenames have in common. */
830 while (*p == *q && *p != 0 && *q != 0)
831 p++, q++;
832
833 /* Now go backwards until the previous directory separator. */
834 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
835 p--;
836
837 return p;
838 }
839 \f
840 /* Standard error reporting routines in increasing order of severity.
841 All of these take arguments like printf. */
842
843 /* Text to be emitted verbatim to the error message stream; this
844 produces no prefix and disables line-wrapping. Use rarely. */
845 void
846 verbatim (const char *gmsgid, ...)
847 {
848 text_info text;
849 va_list ap;
850
851 va_start (ap, gmsgid);
852 text.err_no = errno;
853 text.args_ptr = &ap;
854 text.format_spec = _(gmsgid);
855 text.x_data = NULL;
856 pp_format_verbatim (global_dc->printer, &text);
857 pp_newline_and_flush (global_dc->printer);
858 va_end (ap);
859 }
860
861 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
862 void
863 diagnostic_append_note (diagnostic_context *context,
864 location_t location,
865 const char * gmsgid, ...)
866 {
867 diagnostic_info diagnostic;
868 va_list ap;
869 const char *saved_prefix;
870 rich_location richloc (line_table, location);
871
872 va_start (ap, gmsgid);
873 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
874 if (context->inhibit_notes_p)
875 {
876 va_end (ap);
877 return;
878 }
879 saved_prefix = pp_get_prefix (context->printer);
880 pp_set_prefix (context->printer,
881 diagnostic_build_prefix (context, &diagnostic));
882 pp_newline (context->printer);
883 pp_format (context->printer, &diagnostic.message);
884 pp_output_formatted_text (context->printer);
885 pp_destroy_prefix (context->printer);
886 pp_set_prefix (context->printer, saved_prefix);
887 diagnostic_show_locus (context, &diagnostic);
888 va_end (ap);
889 }
890
891 /* Same as diagnostic_append_note, but at RICHLOC. */
892
893 void
894 diagnostic_append_note_at_rich_loc (diagnostic_context *context,
895 rich_location *richloc,
896 const char * gmsgid, ...)
897 {
898 diagnostic_info diagnostic;
899 va_list ap;
900 const char *saved_prefix;
901
902 va_start (ap, gmsgid);
903 diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc, DK_NOTE);
904 if (context->inhibit_notes_p)
905 {
906 va_end (ap);
907 return;
908 }
909 saved_prefix = pp_get_prefix (context->printer);
910 pp_set_prefix (context->printer,
911 diagnostic_build_prefix (context, &diagnostic));
912 pp_newline (context->printer);
913 pp_format (context->printer, &diagnostic.message);
914 pp_output_formatted_text (context->printer);
915 pp_destroy_prefix (context->printer);
916 pp_set_prefix (context->printer, saved_prefix);
917 diagnostic_show_locus (context, &diagnostic);
918 va_end (ap);
919 }
920
921 bool
922 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
923 const char *gmsgid, ...)
924 {
925 diagnostic_info diagnostic;
926 va_list ap;
927 bool ret;
928 rich_location richloc (line_table, location);
929
930 va_start (ap, gmsgid);
931 if (kind == DK_PERMERROR)
932 {
933 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
934 permissive_error_kind (global_dc));
935 diagnostic.option_index = permissive_error_option (global_dc);
936 }
937 else {
938 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, kind);
939 if (kind == DK_WARNING || kind == DK_PEDWARN)
940 diagnostic.option_index = opt;
941 }
942
943 ret = report_diagnostic (&diagnostic);
944 va_end (ap);
945 return ret;
946 }
947
948 /* An informative note at LOCATION. Use this for additional details on an error
949 message. */
950 void
951 inform (location_t location, const char *gmsgid, ...)
952 {
953 diagnostic_info diagnostic;
954 va_list ap;
955 rich_location richloc (line_table, location);
956
957 va_start (ap, gmsgid);
958 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
959 report_diagnostic (&diagnostic);
960 va_end (ap);
961 }
962
963 /* Same as "inform", but at RICHLOC. */
964 void
965 inform_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
966 {
967 diagnostic_info diagnostic;
968 va_list ap;
969
970 va_start (ap, gmsgid);
971 diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc, DK_NOTE);
972 report_diagnostic (&diagnostic);
973 va_end (ap);
974 }
975
976 /* An informative note at LOCATION. Use this for additional details on an
977 error message. */
978 void
979 inform_n (location_t location, int n, const char *singular_gmsgid,
980 const char *plural_gmsgid, ...)
981 {
982 diagnostic_info diagnostic;
983 va_list ap;
984 rich_location richloc (line_table, location);
985
986 va_start (ap, plural_gmsgid);
987 diagnostic_set_info_translated (&diagnostic,
988 ngettext (singular_gmsgid, plural_gmsgid, n),
989 &ap, &richloc, DK_NOTE);
990 report_diagnostic (&diagnostic);
991 va_end (ap);
992 }
993
994 /* A warning at INPUT_LOCATION. Use this for code which is correct according
995 to the relevant language specification but is likely to be buggy anyway.
996 Returns true if the warning was printed, false if it was inhibited. */
997 bool
998 warning (int opt, const char *gmsgid, ...)
999 {
1000 diagnostic_info diagnostic;
1001 va_list ap;
1002 bool ret;
1003 rich_location richloc (line_table, input_location);
1004
1005 va_start (ap, gmsgid);
1006 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_WARNING);
1007 diagnostic.option_index = opt;
1008
1009 ret = report_diagnostic (&diagnostic);
1010 va_end (ap);
1011 return ret;
1012 }
1013
1014 /* A warning at LOCATION. Use this for code which is correct according to the
1015 relevant language specification but is likely to be buggy anyway.
1016 Returns true if the warning was printed, false if it was inhibited. */
1017
1018 bool
1019 warning_at (location_t location, int opt, const char *gmsgid, ...)
1020 {
1021 diagnostic_info diagnostic;
1022 va_list ap;
1023 bool ret;
1024 rich_location richloc (line_table, location);
1025
1026 va_start (ap, gmsgid);
1027 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_WARNING);
1028 diagnostic.option_index = opt;
1029 ret = report_diagnostic (&diagnostic);
1030 va_end (ap);
1031 return ret;
1032 }
1033
1034 /* Same as warning at, but using RICHLOC. */
1035
1036 bool
1037 warning_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1038 {
1039 diagnostic_info diagnostic;
1040 va_list ap;
1041 bool ret;
1042
1043 va_start (ap, gmsgid);
1044 diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc, DK_WARNING);
1045 diagnostic.option_index = opt;
1046 ret = report_diagnostic (&diagnostic);
1047 va_end (ap);
1048 return ret;
1049 }
1050
1051 /* A warning at LOCATION. Use this for code which is correct according to the
1052 relevant language specification but is likely to be buggy anyway.
1053 Returns true if the warning was printed, false if it was inhibited. */
1054
1055 bool
1056 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1057 const char *plural_gmsgid, ...)
1058 {
1059 diagnostic_info diagnostic;
1060 va_list ap;
1061 bool ret;
1062 rich_location richloc (line_table, location);
1063
1064 va_start (ap, plural_gmsgid);
1065 diagnostic_set_info_translated (&diagnostic,
1066 ngettext (singular_gmsgid, plural_gmsgid, n),
1067 &ap, &richloc, DK_WARNING
1068 );
1069 diagnostic.option_index = opt;
1070 ret = report_diagnostic (&diagnostic);
1071 va_end (ap);
1072 return ret;
1073 }
1074
1075 /* A "pedantic" warning at LOCATION: issues a warning unless
1076 -pedantic-errors was given on the command line, in which case it
1077 issues an error. Use this for diagnostics required by the relevant
1078 language standard, if you have chosen not to make them errors.
1079
1080 Note that these diagnostics are issued independent of the setting
1081 of the -Wpedantic command-line switch. To get a warning enabled
1082 only with that switch, use either "if (pedantic) pedwarn
1083 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1084 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1085
1086 Returns true if the warning was printed, false if it was inhibited. */
1087
1088 bool
1089 pedwarn (location_t location, int opt, const char *gmsgid, ...)
1090 {
1091 diagnostic_info diagnostic;
1092 va_list ap;
1093 bool ret;
1094 rich_location richloc (line_table, location);
1095
1096 va_start (ap, gmsgid);
1097 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_PEDWARN);
1098 diagnostic.option_index = opt;
1099 ret = report_diagnostic (&diagnostic);
1100 va_end (ap);
1101 return ret;
1102 }
1103
1104 /* A "permissive" error at LOCATION: issues an error unless
1105 -fpermissive was given on the command line, in which case it issues
1106 a warning. Use this for things that really should be errors but we
1107 want to support legacy code.
1108
1109 Returns true if the warning was printed, false if it was inhibited. */
1110
1111 bool
1112 permerror (location_t location, const char *gmsgid, ...)
1113 {
1114 diagnostic_info diagnostic;
1115 va_list ap;
1116 bool ret;
1117 rich_location richloc (line_table, location);
1118
1119 va_start (ap, gmsgid);
1120 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
1121 permissive_error_kind (global_dc));
1122 diagnostic.option_index = permissive_error_option (global_dc);
1123 ret = report_diagnostic (&diagnostic);
1124 va_end (ap);
1125 return ret;
1126 }
1127
1128 /* Same as "permerror", but at RICHLOC. */
1129
1130 bool
1131 permerror_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1132 {
1133 diagnostic_info diagnostic;
1134 va_list ap;
1135 bool ret;
1136
1137 va_start (ap, gmsgid);
1138 diagnostic_set_info (&diagnostic, gmsgid, &ap, richloc,
1139 permissive_error_kind (global_dc));
1140 diagnostic.option_index = permissive_error_option (global_dc);
1141 ret = report_diagnostic (&diagnostic);
1142 va_end (ap);
1143 return ret;
1144 }
1145
1146 /* A hard error: the code is definitely ill-formed, and an object file
1147 will not be produced. */
1148 void
1149 error (const char *gmsgid, ...)
1150 {
1151 diagnostic_info diagnostic;
1152 va_list ap;
1153 rich_location richloc (line_table, input_location);
1154
1155 va_start (ap, gmsgid);
1156 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR);
1157 report_diagnostic (&diagnostic);
1158 va_end (ap);
1159 }
1160
1161 /* A hard error: the code is definitely ill-formed, and an object file
1162 will not be produced. */
1163 void
1164 error_n (location_t location, int n, const char *singular_gmsgid,
1165 const char *plural_gmsgid, ...)
1166 {
1167 diagnostic_info diagnostic;
1168 va_list ap;
1169 rich_location richloc (line_table, location);
1170
1171 va_start (ap, plural_gmsgid);
1172 diagnostic_set_info_translated (&diagnostic,
1173 ngettext (singular_gmsgid, plural_gmsgid, n),
1174 &ap, &richloc, DK_ERROR);
1175 report_diagnostic (&diagnostic);
1176 va_end (ap);
1177 }
1178
1179 /* Same as ebove, but use location LOC instead of input_location. */
1180 void
1181 error_at (location_t loc, const char *gmsgid, ...)
1182 {
1183 diagnostic_info diagnostic;
1184 va_list ap;
1185 rich_location richloc (line_table, loc);
1186
1187 va_start (ap, gmsgid);
1188 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR);
1189 report_diagnostic (&diagnostic);
1190 va_end (ap);
1191 }
1192
1193 /* Same as above, but use RICH_LOC. */
1194
1195 void
1196 error_at_rich_loc (rich_location *rich_loc, const char *gmsgid, ...)
1197 {
1198 diagnostic_info diagnostic;
1199 va_list ap;
1200
1201 va_start (ap, gmsgid);
1202 diagnostic_set_info (&diagnostic, gmsgid, &ap, rich_loc,
1203 DK_ERROR);
1204 report_diagnostic (&diagnostic);
1205 va_end (ap);
1206 }
1207
1208 /* "Sorry, not implemented." Use for a language feature which is
1209 required by the relevant specification but not implemented by GCC.
1210 An object file will not be produced. */
1211 void
1212 sorry (const char *gmsgid, ...)
1213 {
1214 diagnostic_info diagnostic;
1215 va_list ap;
1216 rich_location richloc (line_table, input_location);
1217
1218 va_start (ap, gmsgid);
1219 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_SORRY);
1220 report_diagnostic (&diagnostic);
1221 va_end (ap);
1222 }
1223
1224 /* Return true if an error or a "sorry" has been seen. Various
1225 processing is disabled after errors. */
1226 bool
1227 seen_error (void)
1228 {
1229 return errorcount || sorrycount;
1230 }
1231
1232 /* An error which is severe enough that we make no attempt to
1233 continue. Do not use this for internal consistency checks; that's
1234 internal_error. Use of this function should be rare. */
1235 void
1236 fatal_error (location_t loc, const char *gmsgid, ...)
1237 {
1238 diagnostic_info diagnostic;
1239 va_list ap;
1240 rich_location richloc (line_table, loc);
1241
1242 va_start (ap, gmsgid);
1243 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_FATAL);
1244 report_diagnostic (&diagnostic);
1245 va_end (ap);
1246
1247 gcc_unreachable ();
1248 }
1249
1250 /* An internal consistency check has failed. We make no attempt to
1251 continue. Note that unless there is debugging value to be had from
1252 a more specific message, or some other good reason, you should use
1253 abort () instead of calling this function directly. */
1254 void
1255 internal_error (const char *gmsgid, ...)
1256 {
1257 diagnostic_info diagnostic;
1258 va_list ap;
1259 rich_location richloc (line_table, input_location);
1260
1261 va_start (ap, gmsgid);
1262 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ICE);
1263 report_diagnostic (&diagnostic);
1264 va_end (ap);
1265
1266 gcc_unreachable ();
1267 }
1268
1269 /* Like internal_error, but no backtrace will be printed. Used when
1270 the internal error does not happen at the current location, but happened
1271 somewhere else. */
1272 void
1273 internal_error_no_backtrace (const char *gmsgid, ...)
1274 {
1275 diagnostic_info diagnostic;
1276 va_list ap;
1277 rich_location richloc (line_table, input_location);
1278
1279 va_start (ap, gmsgid);
1280 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ICE_NOBT);
1281 report_diagnostic (&diagnostic);
1282 va_end (ap);
1283
1284 gcc_unreachable ();
1285 }
1286 \f
1287 /* Special case error functions. Most are implemented in terms of the
1288 above, or should be. */
1289
1290 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1291 runs its second argument through gettext. */
1292 void
1293 fnotice (FILE *file, const char *cmsgid, ...)
1294 {
1295 va_list ap;
1296
1297 va_start (ap, cmsgid);
1298 vfprintf (file, _(cmsgid), ap);
1299 va_end (ap);
1300 }
1301
1302 /* Inform the user that an error occurred while trying to report some
1303 other error. This indicates catastrophic internal inconsistencies,
1304 so give up now. But do try to flush out the previous error.
1305 This mustn't use internal_error, that will cause infinite recursion. */
1306
1307 static void
1308 error_recursion (diagnostic_context *context)
1309 {
1310 if (context->lock < 3)
1311 pp_newline_and_flush (context->printer);
1312
1313 fnotice (stderr,
1314 "Internal compiler error: Error reporting routines re-entered.\n");
1315
1316 /* Call diagnostic_action_after_output to get the "please submit a bug
1317 report" message. */
1318 diagnostic_action_after_output (context, DK_ICE);
1319
1320 /* Do not use gcc_unreachable here; that goes through internal_error
1321 and therefore would cause infinite recursion. */
1322 real_abort ();
1323 }
1324
1325 /* Report an internal compiler error in a friendly manner. This is
1326 the function that gets called upon use of abort() in the source
1327 code generally, thanks to a special macro. */
1328
1329 void
1330 fancy_abort (const char *file, int line, const char *function)
1331 {
1332 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
1333 }
1334
1335 /* Really call the system 'abort'. This has to go right at the end of
1336 this file, so that there are no functions after it that call abort
1337 and get the system abort instead of our macro. */
1338 #undef abort
1339 static void
1340 real_abort (void)
1341 {
1342 abort ();
1343 }
1344
1345 /* Display the given source_range instance, with MSG as a descriptive
1346 comment. This issues a "note" diagnostic at the range.
1347
1348 This is declared within libcpp, but implemented here, since it
1349 makes use of the diagnostic-printing machinery. */
1350
1351 DEBUG_FUNCTION void
1352 source_range::debug (const char *msg) const
1353 {
1354 rich_location richloc (line_table, m_start);
1355 richloc.add_range (m_start, m_finish, false);
1356 inform_at_rich_loc (&richloc, "%s", msg);
1357 }