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>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file implements the language independent aspect of diagnostic
27 #include "coretypes.h"
31 #include "backtrace.h"
32 #include "diagnostic.h"
33 #include "diagnostic-color.h"
39 #ifdef GWINSZ_IN_SYS_IOCTL
40 # include <sys/ioctl.h>
43 #define pedantic_warning_kind(DC) \
44 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
45 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
46 #define permissive_error_option(DC) ((DC)->opt_permissive)
49 static bool diagnostic_impl (rich_location
*, int, const char *,
50 va_list *, diagnostic_t
) ATTRIBUTE_GCC_DIAG(3,0);
51 static bool diagnostic_n_impl (location_t
, int, int, const char *,
52 const char *, va_list *,
53 diagnostic_t
) ATTRIBUTE_GCC_DIAG(5,0);
54 static void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
55 static void real_abort (void) ATTRIBUTE_NORETURN
;
57 /* Name of program invoked, sans directories. */
61 /* A diagnostic_context surrogate for stderr. */
62 static diagnostic_context global_diagnostic_context
;
63 diagnostic_context
*global_dc
= &global_diagnostic_context
;
65 /* Return a malloc'd string containing MSG formatted a la printf. The
66 caller is responsible for freeing the memory. */
68 build_message_string (const char *msg
, ...)
74 str
= xvasprintf (msg
, ap
);
80 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
82 file_name_as_prefix (diagnostic_context
*context
, const char *f
)
85 = colorize_start (pp_show_color (context
->printer
), "locus");
86 const char *locus_ce
= colorize_stop (pp_show_color (context
->printer
));
87 return build_message_string ("%s%s:%s ", locus_cs
, f
, locus_ce
);
92 /* Return the value of the getenv("COLUMNS") as an integer. If the
93 value is not set to a positive integer, use ioctl to get the
94 terminal width. If it fails, return INT_MAX. */
96 get_terminal_width (void)
98 const char * s
= getenv ("COLUMNS");
108 if (ioctl (0, TIOCGWINSZ
, &w
) == 0 && w
.ws_col
> 0)
115 /* Set caret_max_width to value. */
117 diagnostic_set_caret_max_width (diagnostic_context
*context
, int value
)
119 /* One minus to account for the leading empty space. */
120 value
= value
? value
- 1
121 : (isatty (fileno (pp_buffer (context
->printer
)->stream
))
122 ? get_terminal_width () - 1: INT_MAX
);
127 context
->caret_max_width
= value
;
130 /* Initialize the diagnostic message outputting machinery. */
132 diagnostic_initialize (diagnostic_context
*context
, int n_opts
)
136 /* Allocate a basic pretty-printer. Clients will replace this a
137 much more elaborated pretty-printer if they wish. */
138 context
->printer
= XNEW (pretty_printer
);
139 new (context
->printer
) pretty_printer ();
141 memset (context
->diagnostic_count
, 0, sizeof context
->diagnostic_count
);
142 context
->warning_as_error_requested
= false;
143 context
->n_opts
= n_opts
;
144 context
->classify_diagnostic
= XNEWVEC (diagnostic_t
, n_opts
);
145 for (i
= 0; i
< n_opts
; i
++)
146 context
->classify_diagnostic
[i
] = DK_UNSPECIFIED
;
147 context
->show_caret
= false;
148 diagnostic_set_caret_max_width (context
, pp_line_cutoff (context
->printer
));
149 for (i
= 0; i
< rich_location::MAX_RANGES
; i
++)
150 context
->caret_chars
[i
] = '^';
151 context
->show_option_requested
= false;
152 context
->abort_on_error
= false;
153 context
->show_column
= false;
154 context
->pedantic_errors
= false;
155 context
->permissive
= false;
156 context
->opt_permissive
= 0;
157 context
->fatal_errors
= false;
158 context
->dc_inhibit_warnings
= false;
159 context
->dc_warn_system_headers
= false;
160 context
->max_errors
= 0;
161 context
->internal_error
= NULL
;
162 diagnostic_starter (context
) = default_diagnostic_starter
;
163 context
->start_span
= default_diagnostic_start_span_fn
;
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
;
172 context
->inhibit_notes_p
= false;
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. */
180 diagnostic_color_init (diagnostic_context
*context
, int value
/*= -1 */)
182 /* value == -1 is the default value. */
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)
192 if (!getenv ("GCC_COLORS"))
194 value
= DIAGNOSTICS_COLOR_AUTO
;
197 value
= DIAGNOSTICS_COLOR_DEFAULT
;
199 pp_show_color (context
->printer
)
200 = colorize_init ((diagnostic_color_rule_t
) value
);
203 /* Do any cleaning up required after the last diagnostic is emitted. */
206 diagnostic_finish (diagnostic_context
*context
)
208 /* Some of the errors may actually have been warnings. */
209 if (diagnostic_kind_count (context
, DK_WERROR
))
211 /* -Werror was given. */
212 if (context
->warning_as_error_requested
)
213 pp_verbatim (context
->printer
,
214 _("%s: all warnings being treated as errors"),
216 /* At least one -Werror= was given. */
218 pp_verbatim (context
->printer
,
219 _("%s: some warnings being treated as errors"),
221 pp_newline_and_flush (context
->printer
);
224 diagnostic_file_cache_fini ();
226 XDELETEVEC (context
->classify_diagnostic
);
227 context
->classify_diagnostic
= NULL
;
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
;
236 /* Initialize DIAGNOSTIC, where the message MSG has already been
239 diagnostic_set_info_translated (diagnostic_info
*diagnostic
, const char *msg
,
240 va_list *args
, rich_location
*richloc
,
243 gcc_assert (richloc
);
244 diagnostic
->message
.err_no
= errno
;
245 diagnostic
->message
.args_ptr
= args
;
246 diagnostic
->message
.format_spec
= msg
;
247 diagnostic
->message
.m_richloc
= richloc
;
248 diagnostic
->richloc
= richloc
;
249 diagnostic
->kind
= kind
;
250 diagnostic
->option_index
= 0;
253 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
256 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *gmsgid
,
257 va_list *args
, rich_location
*richloc
,
260 gcc_assert (richloc
);
261 diagnostic_set_info_translated (diagnostic
, _(gmsgid
), args
, richloc
, kind
);
264 static const char *const diagnostic_kind_color
[] = {
265 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
266 #include "diagnostic.def"
267 #undef DEFINE_DIAGNOSTIC_KIND
271 /* Get a color name for diagnostics of type KIND
272 Result could be NULL. */
275 diagnostic_get_color_for_kind (diagnostic_t kind
)
277 return diagnostic_kind_color
[kind
];
280 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
281 The caller is responsible for freeing the memory. */
284 diagnostic_get_location_text (diagnostic_context
*context
,
287 pretty_printer
*pp
= context
->printer
;
288 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
289 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
292 return build_message_string ("%s%s:%s", locus_cs
, progname
, locus_ce
);
294 if (!strcmp (s
.file
, N_("<built-in>")))
295 return build_message_string ("%s%s:%s", locus_cs
, s
.file
, locus_ce
);
297 if (context
->show_column
)
298 return build_message_string ("%s%s:%d:%d:%s", locus_cs
, s
.file
, s
.line
,
301 return build_message_string ("%s%s:%d:%s", locus_cs
, s
.file
, s
.line
,
305 /* Return a malloc'd string describing a location and the severity of the
306 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
307 freeing the memory. */
309 diagnostic_build_prefix (diagnostic_context
*context
,
310 const diagnostic_info
*diagnostic
)
312 static const char *const diagnostic_kind_text
[] = {
313 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
314 #include "diagnostic.def"
315 #undef DEFINE_DIAGNOSTIC_KIND
318 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
320 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
321 const char *text_cs
= "", *text_ce
= "";
322 pretty_printer
*pp
= context
->printer
;
324 if (diagnostic_kind_color
[diagnostic
->kind
])
326 text_cs
= colorize_start (pp_show_color (pp
),
327 diagnostic_kind_color
[diagnostic
->kind
]);
328 text_ce
= colorize_stop (pp_show_color (pp
));
331 expanded_location s
= diagnostic_expand_location (diagnostic
);
332 char *location_text
= diagnostic_get_location_text (context
, s
);
334 char *result
= build_message_string ("%s %s%s%s", location_text
,
335 text_cs
, text
, text_ce
);
336 free (location_text
);
340 /* Functions at which to stop the backtrace print. It's not
341 particularly helpful to print the callers of these functions. */
343 static const char * const bt_stop
[] =
351 /* A callback function passed to the backtrace_full function. */
354 bt_callback (void *data
, uintptr_t pc
, const char *filename
, int lineno
,
355 const char *function
)
357 int *pcount
= (int *) data
;
359 /* If we don't have any useful information, don't print
361 if (filename
== NULL
&& function
== NULL
)
364 /* Skip functions in diagnostic.c. */
367 && strcmp (lbasename (filename
), "diagnostic.c") == 0)
370 /* Print up to 20 functions. We could make this a --param, but
371 since this is only for debugging just use a constant for now. */
374 /* Returning a non-zero value stops the backtrace. */
380 if (function
!= NULL
)
382 char *str
= cplus_demangle_v3 (function
,
383 (DMGL_VERBOSE
| DMGL_ANSI
384 | DMGL_GNU_V3
| DMGL_PARAMS
));
391 for (size_t i
= 0; i
< ARRAY_SIZE (bt_stop
); ++i
)
393 size_t len
= strlen (bt_stop
[i
]);
394 if (strncmp (function
, bt_stop
[i
], len
) == 0
395 && (function
[len
] == '\0' || function
[len
] == '('))
399 /* Returning a non-zero value stops the backtrace. */
405 fprintf (stderr
, "0x%lx %s\n\t%s:%d\n",
407 function
== NULL
? "???" : function
,
408 filename
== NULL
? "???" : filename
,
417 /* A callback function passed to the backtrace_full function. This is
418 called if backtrace_full has an error. */
421 bt_err_callback (void *data ATTRIBUTE_UNUSED
, const char *msg
, int errnum
)
425 /* This means that no debug info was available. Just quietly
426 skip printing backtrace info. */
429 fprintf (stderr
, "%s%s%s\n", msg
, errnum
== 0 ? "" : ": ",
430 errnum
== 0 ? "" : xstrerror (errnum
));
433 /* Take any action which is expected to happen after the diagnostic
434 is written out. This function does not always return. */
436 diagnostic_action_after_output (diagnostic_context
*context
,
437 diagnostic_t diag_kind
)
449 if (context
->abort_on_error
)
451 if (context
->fatal_errors
)
453 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
454 diagnostic_finish (context
);
455 exit (FATAL_EXIT_CODE
);
457 if (context
->max_errors
!= 0
458 && ((unsigned) (diagnostic_kind_count (context
, DK_ERROR
)
459 + diagnostic_kind_count (context
, DK_SORRY
)
460 + diagnostic_kind_count (context
, DK_WERROR
))
461 >= context
->max_errors
))
464 "compilation terminated due to -fmax-errors=%u.\n",
465 context
->max_errors
);
466 diagnostic_finish (context
);
467 exit (FATAL_EXIT_CODE
);
474 struct backtrace_state
*state
= NULL
;
475 if (diag_kind
== DK_ICE
)
476 state
= backtrace_create_state (NULL
, 0, bt_err_callback
, NULL
);
479 backtrace_full (state
, 2, bt_callback
, bt_err_callback
,
482 if (context
->abort_on_error
)
485 fnotice (stderr
, "Please submit a full bug report,\n"
486 "with preprocessed source if appropriate.\n");
489 ("Please include the complete backtrace "
490 "with any bug report.\n"));
491 fnotice (stderr
, "See %s for instructions.\n", bug_report_url
);
493 exit (ICE_EXIT_CODE
);
497 if (context
->abort_on_error
)
499 diagnostic_finish (context
);
500 fnotice (stderr
, "compilation terminated.\n");
501 exit (FATAL_EXIT_CODE
);
509 diagnostic_report_current_module (diagnostic_context
*context
, location_t where
)
511 const line_map_ordinary
*map
= NULL
;
513 if (pp_needs_newline (context
->printer
))
515 pp_newline (context
->printer
);
516 pp_needs_newline (context
->printer
) = false;
519 if (where
<= BUILTINS_LOCATION
)
522 linemap_resolve_location (line_table
, where
,
523 LRK_MACRO_DEFINITION_LOCATION
,
526 if (map
&& diagnostic_last_module_changed (context
, map
))
528 diagnostic_set_last_module (context
, map
);
529 if (! MAIN_FILE_P (map
))
531 map
= INCLUDED_FROM (line_table
, map
);
532 if (context
->show_column
)
533 pp_verbatim (context
->printer
,
534 "In file included from %r%s:%d:%d%R", "locus",
536 LAST_SOURCE_LINE (map
), LAST_SOURCE_COLUMN (map
));
538 pp_verbatim (context
->printer
,
539 "In file included from %r%s:%d%R", "locus",
540 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
541 while (! MAIN_FILE_P (map
))
543 map
= INCLUDED_FROM (line_table
, map
);
544 pp_verbatim (context
->printer
,
545 ",\n from %r%s:%d%R", "locus",
546 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
548 pp_verbatim (context
->printer
, ":");
549 pp_newline (context
->printer
);
555 default_diagnostic_starter (diagnostic_context
*context
,
556 diagnostic_info
*diagnostic
)
558 diagnostic_report_current_module (context
, diagnostic_location (diagnostic
));
559 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
564 default_diagnostic_start_span_fn (diagnostic_context
*context
,
565 expanded_location exploc
)
567 pp_set_prefix (context
->printer
,
568 diagnostic_get_location_text (context
, exploc
));
569 pp_string (context
->printer
, "");
570 pp_newline (context
->printer
);
574 default_diagnostic_finalizer (diagnostic_context
*context
,
575 diagnostic_info
*diagnostic
)
577 diagnostic_show_locus (context
, diagnostic
);
578 pp_destroy_prefix (context
->printer
);
579 pp_flush (context
->printer
);
582 /* Interface to specify diagnostic kind overrides. Returns the
583 previous setting, or DK_UNSPECIFIED if the parameters are out of
584 range. If OPTION_INDEX is zero, the new setting is for all the
587 diagnostic_classify_diagnostic (diagnostic_context
*context
,
589 diagnostic_t new_kind
,
592 diagnostic_t old_kind
;
595 || option_index
>= context
->n_opts
596 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
597 return DK_UNSPECIFIED
;
599 old_kind
= context
->classify_diagnostic
[option_index
];
601 /* Handle pragmas separately, since we need to keep track of *where*
603 if (where
!= UNKNOWN_LOCATION
)
607 /* Record the command-line status, so we can reset it back on DK_POP. */
608 if (old_kind
== DK_UNSPECIFIED
)
610 old_kind
= !context
->option_enabled (option_index
,
611 context
->option_state
)
612 ? DK_IGNORED
: (context
->warning_as_error_requested
613 ? DK_ERROR
: DK_WARNING
);
614 context
->classify_diagnostic
[option_index
] = old_kind
;
617 for (i
= context
->n_classification_history
- 1; i
>= 0; i
--)
618 if (context
->classification_history
[i
].option
== option_index
)
620 old_kind
= context
->classification_history
[i
].kind
;
624 i
= context
->n_classification_history
;
625 context
->classification_history
=
626 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
627 * sizeof (diagnostic_classification_change_t
));
628 context
->classification_history
[i
].location
= where
;
629 context
->classification_history
[i
].option
= option_index
;
630 context
->classification_history
[i
].kind
= new_kind
;
631 context
->n_classification_history
++;
634 context
->classify_diagnostic
[option_index
] = new_kind
;
639 /* Save all diagnostic classifications in a stack. */
641 diagnostic_push_diagnostics (diagnostic_context
*context
, location_t where ATTRIBUTE_UNUSED
)
643 context
->push_list
= (int *) xrealloc (context
->push_list
, (context
->n_push
+ 1) * sizeof (int));
644 context
->push_list
[context
->n_push
++] = context
->n_classification_history
;
647 /* Restore the topmost classification set off the stack. If the stack
648 is empty, revert to the state based on command line parameters. */
650 diagnostic_pop_diagnostics (diagnostic_context
*context
, location_t where
)
656 jump_to
= context
->push_list
[-- context
->n_push
];
660 i
= context
->n_classification_history
;
661 context
->classification_history
=
662 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
663 * sizeof (diagnostic_classification_change_t
));
664 context
->classification_history
[i
].location
= where
;
665 context
->classification_history
[i
].option
= jump_to
;
666 context
->classification_history
[i
].kind
= DK_POP
;
667 context
->n_classification_history
++;
670 /* Report a diagnostic message (an error or a warning) as specified by
671 DC. This function is *the* subroutine in terms of which front-ends
672 should implement their specific diagnostic handling modules. The
673 front-end independent format specifiers are exactly those described
674 in the documentation of output_format.
675 Return true if a diagnostic was printed, false otherwise. */
678 diagnostic_report_diagnostic (diagnostic_context
*context
,
679 diagnostic_info
*diagnostic
)
681 location_t location
= diagnostic_location (diagnostic
);
682 diagnostic_t orig_diag_kind
= diagnostic
->kind
;
683 const char *saved_format_spec
;
685 /* Give preference to being able to inhibit warnings, before they
686 get reclassified to something else. */
687 if ((diagnostic
->kind
== DK_WARNING
|| diagnostic
->kind
== DK_PEDWARN
)
688 && !diagnostic_report_warnings_p (context
, location
))
691 if (diagnostic
->kind
== DK_PEDWARN
)
693 diagnostic
->kind
= pedantic_warning_kind (context
);
694 /* We do this to avoid giving the message for -pedantic-errors. */
695 orig_diag_kind
= diagnostic
->kind
;
698 if (diagnostic
->kind
== DK_NOTE
&& context
->inhibit_notes_p
)
701 if (context
->lock
> 0)
703 /* If we're reporting an ICE in the middle of some other error,
704 try to flush out the previous error, then let this one
705 through. Don't do this more than once. */
706 if ((diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
707 && context
->lock
== 1)
708 pp_newline_and_flush (context
->printer
);
710 error_recursion (context
);
713 /* If the user requested that warnings be treated as errors, so be
714 it. Note that we do this before the next block so that
715 individual warnings can be overridden back to warnings with
717 if (context
->warning_as_error_requested
718 && diagnostic
->kind
== DK_WARNING
)
720 diagnostic
->kind
= DK_ERROR
;
723 if (diagnostic
->option_index
724 && diagnostic
->option_index
!= permissive_error_option (context
))
726 diagnostic_t diag_class
= DK_UNSPECIFIED
;
728 /* This tests if the user provided the appropriate -Wfoo or
730 if (! context
->option_enabled (diagnostic
->option_index
,
731 context
->option_state
))
734 /* This tests for #pragma diagnostic changes. */
735 if (context
->n_classification_history
> 0)
737 /* FIXME: Stupid search. Optimize later. */
738 for (int i
= context
->n_classification_history
- 1; i
>= 0; i
--)
740 if (linemap_location_before_p
742 context
->classification_history
[i
].location
,
745 if (context
->classification_history
[i
].kind
== (int) DK_POP
)
747 i
= context
->classification_history
[i
].option
;
750 int option
= context
->classification_history
[i
].option
;
751 /* The option 0 is for all the diagnostics. */
752 if (option
== 0 || option
== diagnostic
->option_index
)
754 diag_class
= context
->classification_history
[i
].kind
;
755 if (diag_class
!= DK_UNSPECIFIED
)
756 diagnostic
->kind
= diag_class
;
762 /* This tests if the user provided the appropriate -Werror=foo
764 if (diag_class
== DK_UNSPECIFIED
765 && context
->classify_diagnostic
[diagnostic
->option_index
] != DK_UNSPECIFIED
)
767 diagnostic
->kind
= context
->classify_diagnostic
[diagnostic
->option_index
];
769 /* This allows for future extensions, like temporarily disabling
770 warnings for ranges of source code. */
771 if (diagnostic
->kind
== DK_IGNORED
)
777 if (diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
779 /* When not checking, ICEs are converted to fatal errors when an
780 error has already occurred. This is counteracted by
783 && (diagnostic_kind_count (context
, DK_ERROR
) > 0
784 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
785 && !context
->abort_on_error
)
788 = expand_location (diagnostic_location (diagnostic
));
789 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
791 exit (ICE_EXIT_CODE
);
793 if (context
->internal_error
)
794 (*context
->internal_error
) (context
,
795 diagnostic
->message
.format_spec
,
796 diagnostic
->message
.args_ptr
);
798 if (diagnostic
->kind
== DK_ERROR
&& orig_diag_kind
== DK_WARNING
)
799 ++diagnostic_kind_count (context
, DK_WERROR
);
801 ++diagnostic_kind_count (context
, diagnostic
->kind
);
803 saved_format_spec
= diagnostic
->message
.format_spec
;
804 if (context
->show_option_requested
)
808 option_text
= context
->option_name (context
, diagnostic
->option_index
,
809 orig_diag_kind
, diagnostic
->kind
);
814 = colorize_start (pp_show_color (context
->printer
),
815 diagnostic_kind_color
[diagnostic
->kind
]);
816 const char *ce
= colorize_stop (pp_show_color (context
->printer
));
817 diagnostic
->message
.format_spec
818 = ACONCAT ((diagnostic
->message
.format_spec
,
820 "[", cs
, option_text
, ce
, "]",
825 diagnostic
->message
.x_data
= &diagnostic
->x_data
;
826 diagnostic
->x_data
= NULL
;
827 pp_format (context
->printer
, &diagnostic
->message
);
828 (*diagnostic_starter (context
)) (context
, diagnostic
);
829 pp_output_formatted_text (context
->printer
);
830 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
831 diagnostic_action_after_output (context
, diagnostic
->kind
);
832 diagnostic
->message
.format_spec
= saved_format_spec
;
833 diagnostic
->x_data
= NULL
;
840 /* Given a partial pathname as input, return another pathname that
841 shares no directory elements with the pathname of __FILE__. This
842 is used by fancy_abort() to print `Internal compiler error in expr.c'
843 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
846 trim_filename (const char *name
)
848 static const char this_file
[] = __FILE__
;
849 const char *p
= name
, *q
= this_file
;
851 /* First skip any "../" in each filename. This allows us to give a proper
852 reference to a file in a subdirectory. */
853 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
856 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
859 /* Now skip any parts the two filenames have in common. */
860 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
863 /* Now go backwards until the previous directory separator. */
864 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
870 /* Standard error reporting routines in increasing order of severity.
871 All of these take arguments like printf. */
873 /* Text to be emitted verbatim to the error message stream; this
874 produces no prefix and disables line-wrapping. Use rarely. */
876 verbatim (const char *gmsgid
, ...)
881 va_start (ap
, gmsgid
);
884 text
.format_spec
= _(gmsgid
);
886 pp_format_verbatim (global_dc
->printer
, &text
);
887 pp_newline_and_flush (global_dc
->printer
);
891 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
893 diagnostic_append_note (diagnostic_context
*context
,
895 const char * gmsgid
, ...)
897 diagnostic_info diagnostic
;
899 const char *saved_prefix
;
900 rich_location
richloc (line_table
, location
);
902 va_start (ap
, gmsgid
);
903 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_NOTE
);
904 if (context
->inhibit_notes_p
)
909 saved_prefix
= pp_get_prefix (context
->printer
);
910 pp_set_prefix (context
->printer
,
911 diagnostic_build_prefix (context
, &diagnostic
));
912 pp_format (context
->printer
, &diagnostic
.message
);
913 pp_output_formatted_text (context
->printer
);
914 pp_destroy_prefix (context
->printer
);
915 pp_set_prefix (context
->printer
, saved_prefix
);
916 diagnostic_show_locus (context
, &diagnostic
);
920 /* Implement emit_diagnostic, inform, inform_at_rich_loc, warning, warning_at,
921 warning_at_rich_loc, pedwarn, permerror, permerror_at_rich_loc, error,
922 error_at, error_at_rich_loc, sorry, fatal_error, internal_error, and
923 internal_error_no_backtrace, as documented and defined below. */
925 diagnostic_impl (rich_location
*richloc
, int opt
,
927 va_list *ap
, diagnostic_t kind
)
929 diagnostic_info diagnostic
;
930 if (kind
== DK_PERMERROR
)
932 diagnostic_set_info (&diagnostic
, gmsgid
, ap
, richloc
,
933 permissive_error_kind (global_dc
));
934 diagnostic
.option_index
= permissive_error_option (global_dc
);
938 diagnostic_set_info (&diagnostic
, gmsgid
, ap
, richloc
, kind
);
939 if (kind
== DK_WARNING
|| kind
== DK_PEDWARN
)
940 diagnostic
.option_index
= opt
;
942 return report_diagnostic (&diagnostic
);
945 /* Implement inform_n, warning_n, and error_n, as documented and
948 diagnostic_n_impl (location_t location
, int opt
, int n
,
949 const char *singular_gmsgid
,
950 const char *plural_gmsgid
,
951 va_list *ap
, diagnostic_t kind
)
953 diagnostic_info diagnostic
;
954 rich_location
richloc (line_table
, location
);
955 diagnostic_set_info_translated (&diagnostic
,
956 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
958 if (kind
== DK_WARNING
)
959 diagnostic
.option_index
= opt
;
960 return report_diagnostic (&diagnostic
);
964 emit_diagnostic (diagnostic_t kind
, location_t location
, int opt
,
965 const char *gmsgid
, ...)
968 va_start (ap
, gmsgid
);
969 rich_location
richloc (line_table
, location
);
970 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, kind
);
975 /* An informative note at LOCATION. Use this for additional details on an error
978 inform (location_t location
, const char *gmsgid
, ...)
981 va_start (ap
, gmsgid
);
982 rich_location
richloc (line_table
, location
);
983 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_NOTE
);
987 /* Same as "inform", but at RICHLOC. */
989 inform_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
992 va_start (ap
, gmsgid
);
993 diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_NOTE
);
997 /* An informative note at LOCATION. Use this for additional details on an
1000 inform_n (location_t location
, int n
, const char *singular_gmsgid
,
1001 const char *plural_gmsgid
, ...)
1004 va_start (ap
, plural_gmsgid
);
1005 diagnostic_n_impl (location
, -1, n
, singular_gmsgid
, plural_gmsgid
,
1010 /* A warning at INPUT_LOCATION. Use this for code which is correct according
1011 to the relevant language specification but is likely to be buggy anyway.
1012 Returns true if the warning was printed, false if it was inhibited. */
1014 warning (int opt
, const char *gmsgid
, ...)
1017 va_start (ap
, gmsgid
);
1018 rich_location
richloc (line_table
, input_location
);
1019 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1024 /* A warning at LOCATION. Use this for code which is correct according to the
1025 relevant language specification but is likely to be buggy anyway.
1026 Returns true if the warning was printed, false if it was inhibited. */
1029 warning_at (location_t location
, int opt
, const char *gmsgid
, ...)
1032 va_start (ap
, gmsgid
);
1033 rich_location
richloc (line_table
, location
);
1034 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
1039 /* Same as warning at, but using RICHLOC. */
1042 warning_at_rich_loc (rich_location
*richloc
, int opt
, const char *gmsgid
, ...)
1045 va_start (ap
, gmsgid
);
1046 bool ret
= diagnostic_impl (richloc
, opt
, gmsgid
, &ap
, DK_WARNING
);
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. */
1056 warning_n (location_t location
, int opt
, int n
, const char *singular_gmsgid
,
1057 const char *plural_gmsgid
, ...)
1060 va_start (ap
, plural_gmsgid
);
1061 bool ret
= diagnostic_n_impl (location
, opt
, n
,
1062 singular_gmsgid
, plural_gmsgid
,
1068 /* A "pedantic" warning at LOCATION: issues a warning unless
1069 -pedantic-errors was given on the command line, in which case it
1070 issues an error. Use this for diagnostics required by the relevant
1071 language standard, if you have chosen not to make them errors.
1073 Note that these diagnostics are issued independent of the setting
1074 of the -Wpedantic command-line switch. To get a warning enabled
1075 only with that switch, use either "if (pedantic) pedwarn
1076 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1077 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1079 Returns true if the warning was printed, false if it was inhibited. */
1082 pedwarn (location_t location
, int opt
, const char *gmsgid
, ...)
1085 va_start (ap
, gmsgid
);
1086 rich_location
richloc (line_table
, location
);
1087 bool ret
= diagnostic_impl (&richloc
, opt
, gmsgid
, &ap
, DK_PEDWARN
);
1092 /* A "permissive" error at LOCATION: issues an error unless
1093 -fpermissive was given on the command line, in which case it issues
1094 a warning. Use this for things that really should be errors but we
1095 want to support legacy code.
1097 Returns true if the warning was printed, false if it was inhibited. */
1100 permerror (location_t location
, const char *gmsgid
, ...)
1103 va_start (ap
, gmsgid
);
1104 rich_location
richloc (line_table
, location
);
1105 bool ret
= diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_PERMERROR
);
1110 /* Same as "permerror", but at RICHLOC. */
1113 permerror_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
1116 va_start (ap
, gmsgid
);
1117 bool ret
= diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_PERMERROR
);
1122 /* A hard error: the code is definitely ill-formed, and an object file
1123 will not be produced. */
1125 error (const char *gmsgid
, ...)
1128 va_start (ap
, gmsgid
);
1129 rich_location
richloc (line_table
, input_location
);
1130 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1134 /* A hard error: the code is definitely ill-formed, and an object file
1135 will not be produced. */
1137 error_n (location_t location
, int n
, const char *singular_gmsgid
,
1138 const char *plural_gmsgid
, ...)
1141 va_start (ap
, plural_gmsgid
);
1142 diagnostic_n_impl (location
, -1, n
, singular_gmsgid
, plural_gmsgid
,
1147 /* Same as above, but use location LOC instead of input_location. */
1149 error_at (location_t loc
, const char *gmsgid
, ...)
1152 va_start (ap
, gmsgid
);
1153 rich_location
richloc (line_table
, loc
);
1154 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
1158 /* Same as above, but use RICH_LOC. */
1161 error_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
1164 va_start (ap
, gmsgid
);
1165 diagnostic_impl (richloc
, -1, gmsgid
, &ap
, DK_ERROR
);
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. */
1173 sorry (const char *gmsgid
, ...)
1176 va_start (ap
, gmsgid
);
1177 rich_location
richloc (line_table
, input_location
);
1178 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_SORRY
);
1182 /* Return true if an error or a "sorry" has been seen. Various
1183 processing is disabled after errors. */
1187 return errorcount
|| sorrycount
;
1190 /* An error which is severe enough that we make no attempt to
1191 continue. Do not use this for internal consistency checks; that's
1192 internal_error. Use of this function should be rare. */
1194 fatal_error (location_t loc
, const char *gmsgid
, ...)
1197 va_start (ap
, gmsgid
);
1198 rich_location
richloc (line_table
, loc
);
1199 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_FATAL
);
1205 /* An internal consistency check has failed. We make no attempt to
1206 continue. Note that unless there is debugging value to be had from
1207 a more specific message, or some other good reason, you should use
1208 abort () instead of calling this function directly. */
1210 internal_error (const char *gmsgid
, ...)
1213 va_start (ap
, gmsgid
);
1214 rich_location
richloc (line_table
, input_location
);
1215 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ICE
);
1221 /* Like internal_error, but no backtrace will be printed. Used when
1222 the internal error does not happen at the current location, but happened
1225 internal_error_no_backtrace (const char *gmsgid
, ...)
1228 va_start (ap
, gmsgid
);
1229 rich_location
richloc (line_table
, input_location
);
1230 diagnostic_impl (&richloc
, -1, gmsgid
, &ap
, DK_ICE_NOBT
);
1236 /* Special case error functions. Most are implemented in terms of the
1237 above, or should be. */
1239 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1240 runs its second argument through gettext. */
1242 fnotice (FILE *file
, const char *cmsgid
, ...)
1246 va_start (ap
, cmsgid
);
1247 vfprintf (file
, _(cmsgid
), ap
);
1251 /* Inform the user that an error occurred while trying to report some
1252 other error. This indicates catastrophic internal inconsistencies,
1253 so give up now. But do try to flush out the previous error.
1254 This mustn't use internal_error, that will cause infinite recursion. */
1257 error_recursion (diagnostic_context
*context
)
1259 if (context
->lock
< 3)
1260 pp_newline_and_flush (context
->printer
);
1263 "Internal compiler error: Error reporting routines re-entered.\n");
1265 /* Call diagnostic_action_after_output to get the "please submit a bug
1267 diagnostic_action_after_output (context
, DK_ICE
);
1269 /* Do not use gcc_unreachable here; that goes through internal_error
1270 and therefore would cause infinite recursion. */
1274 /* Report an internal compiler error in a friendly manner. This is
1275 the function that gets called upon use of abort() in the source
1276 code generally, thanks to a special macro. */
1279 fancy_abort (const char *file
, int line
, const char *function
)
1281 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1284 /* Really call the system 'abort'. This has to go right at the end of
1285 this file, so that there are no functions after it that call abort
1286 and get the system abort instead of our macro. */