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