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 void error_recursion (diagnostic_context
*) ATTRIBUTE_NORETURN
;
51 static void real_abort (void) ATTRIBUTE_NORETURN
;
53 /* Name of program invoked, sans directories. */
57 /* A diagnostic_context surrogate for stderr. */
58 static diagnostic_context global_diagnostic_context
;
59 diagnostic_context
*global_dc
= &global_diagnostic_context
;
61 /* Return a malloc'd string containing MSG formatted a la printf. The
62 caller is responsible for freeing the memory. */
64 build_message_string (const char *msg
, ...)
70 str
= xvasprintf (msg
, ap
);
76 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
78 file_name_as_prefix (diagnostic_context
*context
, const char *f
)
81 = colorize_start (pp_show_color (context
->printer
), "locus");
82 const char *locus_ce
= colorize_stop (pp_show_color (context
->printer
));
83 return build_message_string ("%s%s:%s ", locus_cs
, f
, locus_ce
);
88 /* Return the value of the getenv("COLUMNS") as an integer. If the
89 value is not set to a positive integer, use ioctl to get the
90 terminal width. If it fails, return INT_MAX. */
92 get_terminal_width (void)
94 const char * s
= getenv ("COLUMNS");
104 if (ioctl (0, TIOCGWINSZ
, &w
) == 0 && w
.ws_col
> 0)
111 /* Set caret_max_width to value. */
113 diagnostic_set_caret_max_width (diagnostic_context
*context
, int value
)
115 /* One minus to account for the leading empty space. */
116 value
= value
? value
- 1
117 : (isatty (fileno (pp_buffer (context
->printer
)->stream
))
118 ? get_terminal_width () - 1: INT_MAX
);
123 context
->caret_max_width
= value
;
126 /* Initialize the diagnostic message outputting machinery. */
128 diagnostic_initialize (diagnostic_context
*context
, int n_opts
)
132 /* Allocate a basic pretty-printer. Clients will replace this a
133 much more elaborated pretty-printer if they wish. */
134 context
->printer
= XNEW (pretty_printer
);
135 new (context
->printer
) pretty_printer ();
137 memset (context
->diagnostic_count
, 0, sizeof context
->diagnostic_count
);
138 context
->warning_as_error_requested
= false;
139 context
->n_opts
= n_opts
;
140 context
->classify_diagnostic
= XNEWVEC (diagnostic_t
, n_opts
);
141 for (i
= 0; i
< n_opts
; i
++)
142 context
->classify_diagnostic
[i
] = DK_UNSPECIFIED
;
143 context
->show_caret
= false;
144 diagnostic_set_caret_max_width (context
, pp_line_cutoff (context
->printer
));
145 for (i
= 0; i
< rich_location::MAX_RANGES
; i
++)
146 context
->caret_chars
[i
] = '^';
147 context
->show_option_requested
= false;
148 context
->abort_on_error
= false;
149 context
->show_column
= false;
150 context
->pedantic_errors
= false;
151 context
->permissive
= false;
152 context
->opt_permissive
= 0;
153 context
->fatal_errors
= false;
154 context
->dc_inhibit_warnings
= false;
155 context
->dc_warn_system_headers
= false;
156 context
->max_errors
= 0;
157 context
->internal_error
= NULL
;
158 diagnostic_starter (context
) = default_diagnostic_starter
;
159 context
->start_span
= default_diagnostic_start_span_fn
;
160 diagnostic_finalizer (context
) = default_diagnostic_finalizer
;
161 context
->option_enabled
= NULL
;
162 context
->option_state
= NULL
;
163 context
->option_name
= NULL
;
164 context
->last_location
= UNKNOWN_LOCATION
;
165 context
->last_module
= 0;
166 context
->x_data
= NULL
;
168 context
->inhibit_notes_p
= false;
171 /* Maybe initialize the color support. We require clients to do this
172 explicitly, since most clients don't want color. When called
173 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
176 diagnostic_color_init (diagnostic_context
*context
, int value
/*= -1 */)
178 /* value == -1 is the default value. */
181 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
182 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
183 otherwise default to -fdiagnostics-color=never, for other
184 values default to that
185 -fdiagnostics-color={never,auto,always}. */
186 if (DIAGNOSTICS_COLOR_DEFAULT
== -1)
188 if (!getenv ("GCC_COLORS"))
190 value
= DIAGNOSTICS_COLOR_AUTO
;
193 value
= DIAGNOSTICS_COLOR_DEFAULT
;
195 pp_show_color (context
->printer
)
196 = colorize_init ((diagnostic_color_rule_t
) value
);
199 /* Do any cleaning up required after the last diagnostic is emitted. */
202 diagnostic_finish (diagnostic_context
*context
)
204 /* Some of the errors may actually have been warnings. */
205 if (diagnostic_kind_count (context
, DK_WERROR
))
207 /* -Werror was given. */
208 if (context
->warning_as_error_requested
)
209 pp_verbatim (context
->printer
,
210 _("%s: all warnings being treated as errors"),
212 /* At least one -Werror= was given. */
214 pp_verbatim (context
->printer
,
215 _("%s: some warnings being treated as errors"),
217 pp_newline_and_flush (context
->printer
);
220 diagnostic_file_cache_fini ();
222 XDELETEVEC (context
->classify_diagnostic
);
223 context
->classify_diagnostic
= NULL
;
225 /* diagnostic_initialize allocates context->printer using XNEW
226 and placement-new. */
227 context
->printer
->~pretty_printer ();
228 XDELETE (context
->printer
);
229 context
->printer
= NULL
;
232 /* Initialize DIAGNOSTIC, where the message MSG has already been
235 diagnostic_set_info_translated (diagnostic_info
*diagnostic
, const char *msg
,
236 va_list *args
, rich_location
*richloc
,
239 gcc_assert (richloc
);
240 diagnostic
->message
.err_no
= errno
;
241 diagnostic
->message
.args_ptr
= args
;
242 diagnostic
->message
.format_spec
= msg
;
243 diagnostic
->message
.m_richloc
= richloc
;
244 diagnostic
->richloc
= richloc
;
245 diagnostic
->kind
= kind
;
246 diagnostic
->option_index
= 0;
249 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
252 diagnostic_set_info (diagnostic_info
*diagnostic
, const char *gmsgid
,
253 va_list *args
, rich_location
*richloc
,
256 gcc_assert (richloc
);
257 diagnostic_set_info_translated (diagnostic
, _(gmsgid
), args
, richloc
, kind
);
260 static const char *const diagnostic_kind_color
[] = {
261 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
262 #include "diagnostic.def"
263 #undef DEFINE_DIAGNOSTIC_KIND
267 /* Get a color name for diagnostics of type KIND
268 Result could be NULL. */
271 diagnostic_get_color_for_kind (diagnostic_t kind
)
273 return diagnostic_kind_color
[kind
];
276 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
277 The caller is responsible for freeing the memory. */
280 diagnostic_get_location_text (diagnostic_context
*context
,
283 pretty_printer
*pp
= context
->printer
;
284 const char *locus_cs
= colorize_start (pp_show_color (pp
), "locus");
285 const char *locus_ce
= colorize_stop (pp_show_color (pp
));
288 return build_message_string ("%s%s:%s", locus_cs
, progname
, locus_ce
);
290 if (!strcmp (s
.file
, N_("<built-in>")))
291 return build_message_string ("%s%s:%s", locus_cs
, s
.file
, locus_ce
);
293 if (context
->show_column
)
294 return build_message_string ("%s%s:%d:%d:%s", locus_cs
, s
.file
, s
.line
,
297 return build_message_string ("%s%s:%d:%s", locus_cs
, s
.file
, s
.line
,
301 /* Return a malloc'd string describing a location and the severity of the
302 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
303 freeing the memory. */
305 diagnostic_build_prefix (diagnostic_context
*context
,
306 const diagnostic_info
*diagnostic
)
308 static const char *const diagnostic_kind_text
[] = {
309 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
310 #include "diagnostic.def"
311 #undef DEFINE_DIAGNOSTIC_KIND
314 gcc_assert (diagnostic
->kind
< DK_LAST_DIAGNOSTIC_KIND
);
316 const char *text
= _(diagnostic_kind_text
[diagnostic
->kind
]);
317 const char *text_cs
= "", *text_ce
= "";
318 pretty_printer
*pp
= context
->printer
;
320 if (diagnostic_kind_color
[diagnostic
->kind
])
322 text_cs
= colorize_start (pp_show_color (pp
),
323 diagnostic_kind_color
[diagnostic
->kind
]);
324 text_ce
= colorize_stop (pp_show_color (pp
));
327 expanded_location s
= diagnostic_expand_location (diagnostic
);
328 char *location_text
= diagnostic_get_location_text (context
, s
);
330 char *result
= build_message_string ("%s %s%s%s", location_text
,
331 text_cs
, text
, text_ce
);
332 free (location_text
);
336 /* Functions at which to stop the backtrace print. It's not
337 particularly helpful to print the callers of these functions. */
339 static const char * const bt_stop
[] =
347 /* A callback function passed to the backtrace_full function. */
350 bt_callback (void *data
, uintptr_t pc
, const char *filename
, int lineno
,
351 const char *function
)
353 int *pcount
= (int *) data
;
355 /* If we don't have any useful information, don't print
357 if (filename
== NULL
&& function
== NULL
)
360 /* Skip functions in diagnostic.c. */
363 && strcmp (lbasename (filename
), "diagnostic.c") == 0)
366 /* Print up to 20 functions. We could make this a --param, but
367 since this is only for debugging just use a constant for now. */
370 /* Returning a non-zero value stops the backtrace. */
376 if (function
!= NULL
)
378 char *str
= cplus_demangle_v3 (function
,
379 (DMGL_VERBOSE
| DMGL_ANSI
380 | DMGL_GNU_V3
| DMGL_PARAMS
));
387 for (size_t i
= 0; i
< ARRAY_SIZE (bt_stop
); ++i
)
389 size_t len
= strlen (bt_stop
[i
]);
390 if (strncmp (function
, bt_stop
[i
], len
) == 0
391 && (function
[len
] == '\0' || function
[len
] == '('))
395 /* Returning a non-zero value stops the backtrace. */
401 fprintf (stderr
, "0x%lx %s\n\t%s:%d\n",
403 function
== NULL
? "???" : function
,
404 filename
== NULL
? "???" : filename
,
413 /* A callback function passed to the backtrace_full function. This is
414 called if backtrace_full has an error. */
417 bt_err_callback (void *data ATTRIBUTE_UNUSED
, const char *msg
, int errnum
)
421 /* This means that no debug info was available. Just quietly
422 skip printing backtrace info. */
425 fprintf (stderr
, "%s%s%s\n", msg
, errnum
== 0 ? "" : ": ",
426 errnum
== 0 ? "" : xstrerror (errnum
));
429 /* Take any action which is expected to happen after the diagnostic
430 is written out. This function does not always return. */
432 diagnostic_action_after_output (diagnostic_context
*context
,
433 diagnostic_t diag_kind
)
445 if (context
->abort_on_error
)
447 if (context
->fatal_errors
)
449 fnotice (stderr
, "compilation terminated due to -Wfatal-errors.\n");
450 diagnostic_finish (context
);
451 exit (FATAL_EXIT_CODE
);
453 if (context
->max_errors
!= 0
454 && ((unsigned) (diagnostic_kind_count (context
, DK_ERROR
)
455 + diagnostic_kind_count (context
, DK_SORRY
)
456 + diagnostic_kind_count (context
, DK_WERROR
))
457 >= context
->max_errors
))
460 "compilation terminated due to -fmax-errors=%u.\n",
461 context
->max_errors
);
462 diagnostic_finish (context
);
463 exit (FATAL_EXIT_CODE
);
470 struct backtrace_state
*state
= NULL
;
471 if (diag_kind
== DK_ICE
)
472 state
= backtrace_create_state (NULL
, 0, bt_err_callback
, NULL
);
475 backtrace_full (state
, 2, bt_callback
, bt_err_callback
,
478 if (context
->abort_on_error
)
481 fnotice (stderr
, "Please submit a full bug report,\n"
482 "with preprocessed source if appropriate.\n");
485 ("Please include the complete backtrace "
486 "with any bug report.\n"));
487 fnotice (stderr
, "See %s for instructions.\n", bug_report_url
);
489 exit (ICE_EXIT_CODE
);
493 if (context
->abort_on_error
)
495 diagnostic_finish (context
);
496 fnotice (stderr
, "compilation terminated.\n");
497 exit (FATAL_EXIT_CODE
);
505 diagnostic_report_current_module (diagnostic_context
*context
, location_t where
)
507 const line_map_ordinary
*map
= NULL
;
509 if (pp_needs_newline (context
->printer
))
511 pp_newline (context
->printer
);
512 pp_needs_newline (context
->printer
) = false;
515 if (where
<= BUILTINS_LOCATION
)
518 linemap_resolve_location (line_table
, where
,
519 LRK_MACRO_DEFINITION_LOCATION
,
522 if (map
&& diagnostic_last_module_changed (context
, map
))
524 diagnostic_set_last_module (context
, map
);
525 if (! MAIN_FILE_P (map
))
527 map
= INCLUDED_FROM (line_table
, map
);
528 if (context
->show_column
)
529 pp_verbatim (context
->printer
,
530 "In file included from %r%s:%d:%d%R", "locus",
532 LAST_SOURCE_LINE (map
), LAST_SOURCE_COLUMN (map
));
534 pp_verbatim (context
->printer
,
535 "In file included from %r%s:%d%R", "locus",
536 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
537 while (! MAIN_FILE_P (map
))
539 map
= INCLUDED_FROM (line_table
, map
);
540 pp_verbatim (context
->printer
,
541 ",\n from %r%s:%d%R", "locus",
542 LINEMAP_FILE (map
), LAST_SOURCE_LINE (map
));
544 pp_verbatim (context
->printer
, ":");
545 pp_newline (context
->printer
);
551 default_diagnostic_starter (diagnostic_context
*context
,
552 diagnostic_info
*diagnostic
)
554 diagnostic_report_current_module (context
, diagnostic_location (diagnostic
));
555 pp_set_prefix (context
->printer
, diagnostic_build_prefix (context
,
560 default_diagnostic_start_span_fn (diagnostic_context
*context
,
561 expanded_location exploc
)
563 pp_set_prefix (context
->printer
,
564 diagnostic_get_location_text (context
, exploc
));
565 pp_string (context
->printer
, "");
566 pp_newline (context
->printer
);
570 default_diagnostic_finalizer (diagnostic_context
*context
,
571 diagnostic_info
*diagnostic
)
573 diagnostic_show_locus (context
, diagnostic
);
574 pp_destroy_prefix (context
->printer
);
575 pp_flush (context
->printer
);
578 /* Interface to specify diagnostic kind overrides. Returns the
579 previous setting, or DK_UNSPECIFIED if the parameters are out of
580 range. If OPTION_INDEX is zero, the new setting is for all the
583 diagnostic_classify_diagnostic (diagnostic_context
*context
,
585 diagnostic_t new_kind
,
588 diagnostic_t old_kind
;
591 || option_index
>= context
->n_opts
592 || new_kind
>= DK_LAST_DIAGNOSTIC_KIND
)
593 return DK_UNSPECIFIED
;
595 old_kind
= context
->classify_diagnostic
[option_index
];
597 /* Handle pragmas separately, since we need to keep track of *where*
599 if (where
!= UNKNOWN_LOCATION
)
603 /* Record the command-line status, so we can reset it back on DK_POP. */
604 if (old_kind
== DK_UNSPECIFIED
)
606 old_kind
= !context
->option_enabled (option_index
,
607 context
->option_state
)
608 ? DK_IGNORED
: (context
->warning_as_error_requested
609 ? DK_ERROR
: DK_WARNING
);
610 context
->classify_diagnostic
[option_index
] = old_kind
;
613 for (i
= context
->n_classification_history
- 1; i
>= 0; i
--)
614 if (context
->classification_history
[i
].option
== option_index
)
616 old_kind
= context
->classification_history
[i
].kind
;
620 i
= context
->n_classification_history
;
621 context
->classification_history
=
622 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
623 * sizeof (diagnostic_classification_change_t
));
624 context
->classification_history
[i
].location
= where
;
625 context
->classification_history
[i
].option
= option_index
;
626 context
->classification_history
[i
].kind
= new_kind
;
627 context
->n_classification_history
++;
630 context
->classify_diagnostic
[option_index
] = new_kind
;
635 /* Save all diagnostic classifications in a stack. */
637 diagnostic_push_diagnostics (diagnostic_context
*context
, location_t where ATTRIBUTE_UNUSED
)
639 context
->push_list
= (int *) xrealloc (context
->push_list
, (context
->n_push
+ 1) * sizeof (int));
640 context
->push_list
[context
->n_push
++] = context
->n_classification_history
;
643 /* Restore the topmost classification set off the stack. If the stack
644 is empty, revert to the state based on command line parameters. */
646 diagnostic_pop_diagnostics (diagnostic_context
*context
, location_t where
)
652 jump_to
= context
->push_list
[-- context
->n_push
];
656 i
= context
->n_classification_history
;
657 context
->classification_history
=
658 (diagnostic_classification_change_t
*) xrealloc (context
->classification_history
, (i
+ 1)
659 * sizeof (diagnostic_classification_change_t
));
660 context
->classification_history
[i
].location
= where
;
661 context
->classification_history
[i
].option
= jump_to
;
662 context
->classification_history
[i
].kind
= DK_POP
;
663 context
->n_classification_history
++;
666 /* Report a diagnostic message (an error or a warning) as specified by
667 DC. This function is *the* subroutine in terms of which front-ends
668 should implement their specific diagnostic handling modules. The
669 front-end independent format specifiers are exactly those described
670 in the documentation of output_format.
671 Return true if a diagnostic was printed, false otherwise. */
674 diagnostic_report_diagnostic (diagnostic_context
*context
,
675 diagnostic_info
*diagnostic
)
677 location_t location
= diagnostic_location (diagnostic
);
678 diagnostic_t orig_diag_kind
= diagnostic
->kind
;
679 const char *saved_format_spec
;
681 /* Give preference to being able to inhibit warnings, before they
682 get reclassified to something else. */
683 if ((diagnostic
->kind
== DK_WARNING
|| diagnostic
->kind
== DK_PEDWARN
)
684 && !diagnostic_report_warnings_p (context
, location
))
687 if (diagnostic
->kind
== DK_PEDWARN
)
689 diagnostic
->kind
= pedantic_warning_kind (context
);
690 /* We do this to avoid giving the message for -pedantic-errors. */
691 orig_diag_kind
= diagnostic
->kind
;
694 if (diagnostic
->kind
== DK_NOTE
&& context
->inhibit_notes_p
)
697 if (context
->lock
> 0)
699 /* If we're reporting an ICE in the middle of some other error,
700 try to flush out the previous error, then let this one
701 through. Don't do this more than once. */
702 if ((diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
703 && context
->lock
== 1)
704 pp_newline_and_flush (context
->printer
);
706 error_recursion (context
);
709 /* If the user requested that warnings be treated as errors, so be
710 it. Note that we do this before the next block so that
711 individual warnings can be overridden back to warnings with
713 if (context
->warning_as_error_requested
714 && diagnostic
->kind
== DK_WARNING
)
716 diagnostic
->kind
= DK_ERROR
;
719 if (diagnostic
->option_index
720 && diagnostic
->option_index
!= permissive_error_option (context
))
722 diagnostic_t diag_class
= DK_UNSPECIFIED
;
724 /* This tests if the user provided the appropriate -Wfoo or
726 if (! context
->option_enabled (diagnostic
->option_index
,
727 context
->option_state
))
730 /* This tests for #pragma diagnostic changes. */
731 if (context
->n_classification_history
> 0)
733 /* FIXME: Stupid search. Optimize later. */
734 for (int i
= context
->n_classification_history
- 1; i
>= 0; i
--)
736 if (linemap_location_before_p
738 context
->classification_history
[i
].location
,
741 if (context
->classification_history
[i
].kind
== (int) DK_POP
)
743 i
= context
->classification_history
[i
].option
;
746 int option
= context
->classification_history
[i
].option
;
747 /* The option 0 is for all the diagnostics. */
748 if (option
== 0 || option
== diagnostic
->option_index
)
750 diag_class
= context
->classification_history
[i
].kind
;
751 if (diag_class
!= DK_UNSPECIFIED
)
752 diagnostic
->kind
= diag_class
;
758 /* This tests if the user provided the appropriate -Werror=foo
760 if (diag_class
== DK_UNSPECIFIED
761 && context
->classify_diagnostic
[diagnostic
->option_index
] != DK_UNSPECIFIED
)
763 diagnostic
->kind
= context
->classify_diagnostic
[diagnostic
->option_index
];
765 /* This allows for future extensions, like temporarily disabling
766 warnings for ranges of source code. */
767 if (diagnostic
->kind
== DK_IGNORED
)
773 if (diagnostic
->kind
== DK_ICE
|| diagnostic
->kind
== DK_ICE_NOBT
)
775 /* When not checking, ICEs are converted to fatal errors when an
776 error has already occurred. This is counteracted by
779 && (diagnostic_kind_count (context
, DK_ERROR
) > 0
780 || diagnostic_kind_count (context
, DK_SORRY
) > 0)
781 && !context
->abort_on_error
)
784 = expand_location (diagnostic_location (diagnostic
));
785 fnotice (stderr
, "%s:%d: confused by earlier errors, bailing out\n",
787 exit (ICE_EXIT_CODE
);
789 if (context
->internal_error
)
790 (*context
->internal_error
) (context
,
791 diagnostic
->message
.format_spec
,
792 diagnostic
->message
.args_ptr
);
794 if (diagnostic
->kind
== DK_ERROR
&& orig_diag_kind
== DK_WARNING
)
795 ++diagnostic_kind_count (context
, DK_WERROR
);
797 ++diagnostic_kind_count (context
, diagnostic
->kind
);
799 saved_format_spec
= diagnostic
->message
.format_spec
;
800 if (context
->show_option_requested
)
804 option_text
= context
->option_name (context
, diagnostic
->option_index
,
805 orig_diag_kind
, diagnostic
->kind
);
810 = colorize_start (pp_show_color (context
->printer
),
811 diagnostic_kind_color
[diagnostic
->kind
]);
812 const char *ce
= colorize_stop (pp_show_color (context
->printer
));
813 diagnostic
->message
.format_spec
814 = ACONCAT ((diagnostic
->message
.format_spec
,
816 "[", cs
, option_text
, ce
, "]",
821 diagnostic
->message
.x_data
= &diagnostic
->x_data
;
822 diagnostic
->x_data
= NULL
;
823 pp_format (context
->printer
, &diagnostic
->message
);
824 (*diagnostic_starter (context
)) (context
, diagnostic
);
825 pp_output_formatted_text (context
->printer
);
826 (*diagnostic_finalizer (context
)) (context
, diagnostic
);
827 diagnostic_action_after_output (context
, diagnostic
->kind
);
828 diagnostic
->message
.format_spec
= saved_format_spec
;
829 diagnostic
->x_data
= NULL
;
836 /* Given a partial pathname as input, return another pathname that
837 shares no directory elements with the pathname of __FILE__. This
838 is used by fancy_abort() to print `Internal compiler error in expr.c'
839 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
842 trim_filename (const char *name
)
844 static const char this_file
[] = __FILE__
;
845 const char *p
= name
, *q
= this_file
;
847 /* First skip any "../" in each filename. This allows us to give a proper
848 reference to a file in a subdirectory. */
849 while (p
[0] == '.' && p
[1] == '.' && IS_DIR_SEPARATOR (p
[2]))
852 while (q
[0] == '.' && q
[1] == '.' && IS_DIR_SEPARATOR (q
[2]))
855 /* Now skip any parts the two filenames have in common. */
856 while (*p
== *q
&& *p
!= 0 && *q
!= 0)
859 /* Now go backwards until the previous directory separator. */
860 while (p
> name
&& !IS_DIR_SEPARATOR (p
[-1]))
866 /* Standard error reporting routines in increasing order of severity.
867 All of these take arguments like printf. */
869 /* Text to be emitted verbatim to the error message stream; this
870 produces no prefix and disables line-wrapping. Use rarely. */
872 verbatim (const char *gmsgid
, ...)
877 va_start (ap
, gmsgid
);
880 text
.format_spec
= _(gmsgid
);
882 pp_format_verbatim (global_dc
->printer
, &text
);
883 pp_newline_and_flush (global_dc
->printer
);
887 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
889 diagnostic_append_note (diagnostic_context
*context
,
891 const char * gmsgid
, ...)
893 diagnostic_info diagnostic
;
895 const char *saved_prefix
;
896 rich_location
richloc (line_table
, location
);
898 va_start (ap
, gmsgid
);
899 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_NOTE
);
900 if (context
->inhibit_notes_p
)
905 saved_prefix
= pp_get_prefix (context
->printer
);
906 pp_set_prefix (context
->printer
,
907 diagnostic_build_prefix (context
, &diagnostic
));
908 pp_format (context
->printer
, &diagnostic
.message
);
909 pp_output_formatted_text (context
->printer
);
910 pp_destroy_prefix (context
->printer
);
911 pp_set_prefix (context
->printer
, saved_prefix
);
912 diagnostic_show_locus (context
, &diagnostic
);
917 emit_diagnostic (diagnostic_t kind
, location_t location
, int opt
,
918 const char *gmsgid
, ...)
920 diagnostic_info diagnostic
;
923 rich_location
richloc (line_table
, location
);
925 va_start (ap
, gmsgid
);
926 if (kind
== DK_PERMERROR
)
928 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
,
929 permissive_error_kind (global_dc
));
930 diagnostic
.option_index
= permissive_error_option (global_dc
);
933 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, kind
);
934 if (kind
== DK_WARNING
|| kind
== DK_PEDWARN
)
935 diagnostic
.option_index
= opt
;
938 ret
= report_diagnostic (&diagnostic
);
943 /* An informative note at LOCATION. Use this for additional details on an error
946 inform (location_t location
, const char *gmsgid
, ...)
948 diagnostic_info diagnostic
;
950 rich_location
richloc (line_table
, location
);
952 va_start (ap
, gmsgid
);
953 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_NOTE
);
954 report_diagnostic (&diagnostic
);
958 /* Same as "inform", but at RICHLOC. */
960 inform_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
962 diagnostic_info diagnostic
;
965 va_start (ap
, gmsgid
);
966 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, richloc
, DK_NOTE
);
967 report_diagnostic (&diagnostic
);
971 /* An informative note at LOCATION. Use this for additional details on an
974 inform_n (location_t location
, int n
, const char *singular_gmsgid
,
975 const char *plural_gmsgid
, ...)
977 diagnostic_info diagnostic
;
979 rich_location
richloc (line_table
, location
);
981 va_start (ap
, plural_gmsgid
);
982 diagnostic_set_info_translated (&diagnostic
,
983 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
984 &ap
, &richloc
, DK_NOTE
);
985 report_diagnostic (&diagnostic
);
989 /* A warning at INPUT_LOCATION. Use this for code which is correct according
990 to the relevant language specification but is likely to be buggy anyway.
991 Returns true if the warning was printed, false if it was inhibited. */
993 warning (int opt
, const char *gmsgid
, ...)
995 diagnostic_info diagnostic
;
998 rich_location
richloc (line_table
, input_location
);
1000 va_start (ap
, gmsgid
);
1001 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_WARNING
);
1002 diagnostic
.option_index
= opt
;
1004 ret
= report_diagnostic (&diagnostic
);
1009 /* A warning at LOCATION. Use this for code which is correct according to the
1010 relevant language specification but is likely to be buggy anyway.
1011 Returns true if the warning was printed, false if it was inhibited. */
1014 warning_at (location_t location
, int opt
, const char *gmsgid
, ...)
1016 diagnostic_info diagnostic
;
1019 rich_location
richloc (line_table
, location
);
1021 va_start (ap
, gmsgid
);
1022 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_WARNING
);
1023 diagnostic
.option_index
= opt
;
1024 ret
= report_diagnostic (&diagnostic
);
1029 /* Same as warning at, but using RICHLOC. */
1032 warning_at_rich_loc (rich_location
*richloc
, int opt
, const char *gmsgid
, ...)
1034 diagnostic_info diagnostic
;
1038 va_start (ap
, gmsgid
);
1039 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, richloc
, DK_WARNING
);
1040 diagnostic
.option_index
= opt
;
1041 ret
= report_diagnostic (&diagnostic
);
1046 /* A warning at LOCATION. Use this for code which is correct according to the
1047 relevant language specification but is likely to be buggy anyway.
1048 Returns true if the warning was printed, false if it was inhibited. */
1051 warning_n (location_t location
, int opt
, int n
, const char *singular_gmsgid
,
1052 const char *plural_gmsgid
, ...)
1054 diagnostic_info diagnostic
;
1057 rich_location
richloc (line_table
, location
);
1059 va_start (ap
, plural_gmsgid
);
1060 diagnostic_set_info_translated (&diagnostic
,
1061 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
1062 &ap
, &richloc
, DK_WARNING
1064 diagnostic
.option_index
= opt
;
1065 ret
= report_diagnostic (&diagnostic
);
1070 /* A "pedantic" warning at LOCATION: issues a warning unless
1071 -pedantic-errors was given on the command line, in which case it
1072 issues an error. Use this for diagnostics required by the relevant
1073 language standard, if you have chosen not to make them errors.
1075 Note that these diagnostics are issued independent of the setting
1076 of the -Wpedantic command-line switch. To get a warning enabled
1077 only with that switch, use either "if (pedantic) pedwarn
1078 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
1079 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
1081 Returns true if the warning was printed, false if it was inhibited. */
1084 pedwarn (location_t location
, int opt
, const char *gmsgid
, ...)
1086 diagnostic_info diagnostic
;
1089 rich_location
richloc (line_table
, location
);
1091 va_start (ap
, gmsgid
);
1092 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_PEDWARN
);
1093 diagnostic
.option_index
= opt
;
1094 ret
= report_diagnostic (&diagnostic
);
1099 /* A "permissive" error at LOCATION: issues an error unless
1100 -fpermissive was given on the command line, in which case it issues
1101 a warning. Use this for things that really should be errors but we
1102 want to support legacy code.
1104 Returns true if the warning was printed, false if it was inhibited. */
1107 permerror (location_t location
, const char *gmsgid
, ...)
1109 diagnostic_info diagnostic
;
1112 rich_location
richloc (line_table
, location
);
1114 va_start (ap
, gmsgid
);
1115 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
,
1116 permissive_error_kind (global_dc
));
1117 diagnostic
.option_index
= permissive_error_option (global_dc
);
1118 ret
= report_diagnostic (&diagnostic
);
1123 /* Same as "permerror", but at RICHLOC. */
1126 permerror_at_rich_loc (rich_location
*richloc
, const char *gmsgid
, ...)
1128 diagnostic_info diagnostic
;
1132 va_start (ap
, gmsgid
);
1133 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, richloc
,
1134 permissive_error_kind (global_dc
));
1135 diagnostic
.option_index
= permissive_error_option (global_dc
);
1136 ret
= report_diagnostic (&diagnostic
);
1141 /* A hard error: the code is definitely ill-formed, and an object file
1142 will not be produced. */
1144 error (const char *gmsgid
, ...)
1146 diagnostic_info diagnostic
;
1148 rich_location
richloc (line_table
, input_location
);
1150 va_start (ap
, gmsgid
);
1151 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_ERROR
);
1152 report_diagnostic (&diagnostic
);
1156 /* A hard error: the code is definitely ill-formed, and an object file
1157 will not be produced. */
1159 error_n (location_t location
, int n
, const char *singular_gmsgid
,
1160 const char *plural_gmsgid
, ...)
1162 diagnostic_info diagnostic
;
1164 rich_location
richloc (line_table
, location
);
1166 va_start (ap
, plural_gmsgid
);
1167 diagnostic_set_info_translated (&diagnostic
,
1168 ngettext (singular_gmsgid
, plural_gmsgid
, n
),
1169 &ap
, &richloc
, DK_ERROR
);
1170 report_diagnostic (&diagnostic
);
1174 /* Same as ebove, but use location LOC instead of input_location. */
1176 error_at (location_t loc
, const char *gmsgid
, ...)
1178 diagnostic_info diagnostic
;
1180 rich_location
richloc (line_table
, loc
);
1182 va_start (ap
, gmsgid
);
1183 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_ERROR
);
1184 report_diagnostic (&diagnostic
);
1188 /* Same as above, but use RICH_LOC. */
1191 error_at_rich_loc (rich_location
*rich_loc
, const char *gmsgid
, ...)
1193 diagnostic_info diagnostic
;
1196 va_start (ap
, gmsgid
);
1197 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, rich_loc
,
1199 report_diagnostic (&diagnostic
);
1203 /* "Sorry, not implemented." Use for a language feature which is
1204 required by the relevant specification but not implemented by GCC.
1205 An object file will not be produced. */
1207 sorry (const char *gmsgid
, ...)
1209 diagnostic_info diagnostic
;
1211 rich_location
richloc (line_table
, input_location
);
1213 va_start (ap
, gmsgid
);
1214 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_SORRY
);
1215 report_diagnostic (&diagnostic
);
1219 /* Return true if an error or a "sorry" has been seen. Various
1220 processing is disabled after errors. */
1224 return errorcount
|| sorrycount
;
1227 /* An error which is severe enough that we make no attempt to
1228 continue. Do not use this for internal consistency checks; that's
1229 internal_error. Use of this function should be rare. */
1231 fatal_error (location_t loc
, const char *gmsgid
, ...)
1233 diagnostic_info diagnostic
;
1235 rich_location
richloc (line_table
, loc
);
1237 va_start (ap
, gmsgid
);
1238 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_FATAL
);
1239 report_diagnostic (&diagnostic
);
1245 /* An internal consistency check has failed. We make no attempt to
1246 continue. Note that unless there is debugging value to be had from
1247 a more specific message, or some other good reason, you should use
1248 abort () instead of calling this function directly. */
1250 internal_error (const char *gmsgid
, ...)
1252 diagnostic_info diagnostic
;
1254 rich_location
richloc (line_table
, input_location
);
1256 va_start (ap
, gmsgid
);
1257 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_ICE
);
1258 report_diagnostic (&diagnostic
);
1264 /* Like internal_error, but no backtrace will be printed. Used when
1265 the internal error does not happen at the current location, but happened
1268 internal_error_no_backtrace (const char *gmsgid
, ...)
1270 diagnostic_info diagnostic
;
1272 rich_location
richloc (line_table
, input_location
);
1274 va_start (ap
, gmsgid
);
1275 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_ICE_NOBT
);
1276 report_diagnostic (&diagnostic
);
1282 /* Special case error functions. Most are implemented in terms of the
1283 above, or should be. */
1285 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
1286 runs its second argument through gettext. */
1288 fnotice (FILE *file
, const char *cmsgid
, ...)
1292 va_start (ap
, cmsgid
);
1293 vfprintf (file
, _(cmsgid
), ap
);
1297 /* Inform the user that an error occurred while trying to report some
1298 other error. This indicates catastrophic internal inconsistencies,
1299 so give up now. But do try to flush out the previous error.
1300 This mustn't use internal_error, that will cause infinite recursion. */
1303 error_recursion (diagnostic_context
*context
)
1305 if (context
->lock
< 3)
1306 pp_newline_and_flush (context
->printer
);
1309 "Internal compiler error: Error reporting routines re-entered.\n");
1311 /* Call diagnostic_action_after_output to get the "please submit a bug
1313 diagnostic_action_after_output (context
, DK_ICE
);
1315 /* Do not use gcc_unreachable here; that goes through internal_error
1316 and therefore would cause infinite recursion. */
1320 /* Report an internal compiler error in a friendly manner. This is
1321 the function that gets called upon use of abort() in the source
1322 code generally, thanks to a special macro. */
1325 fancy_abort (const char *file
, int line
, const char *function
)
1327 internal_error ("in %s, at %s:%d", function
, trim_filename (file
), line
);
1330 /* Really call the system 'abort'. This has to go right at the end of
1331 this file, so that there are no functions after it that call abort
1332 and get the system abort instead of our macro. */