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