]>
Commit | Line | Data |
---|---|---|
47b69537 | 1 | /* Language-independent diagnostic subroutines for the GNU Compiler Collection |
85ec4feb | 2 | Copyright (C) 1999-2018 Free Software Foundation, Inc. |
406a65d0 | 3 | Contributed by Gabriel Dos Reis <gdr@codesourcery.com> |
6ed551b4 | 4 | |
1322177d | 5 | This file is part of GCC. |
6ed551b4 | 6 | |
1322177d LB |
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 | |
9dcd6f09 | 9 | Software Foundation; either version 3, or (at your option) any later |
1322177d | 10 | version. |
6ed551b4 | 11 | |
1322177d LB |
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. | |
6ed551b4 GDR |
16 | |
17 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
6ed551b4 GDR |
20 | |
21 | ||
4eb00163 | 22 | /* This file implements the language independent aspect of diagnostic |
6ed551b4 GDR |
23 | message module. */ |
24 | ||
25 | #include "config.h" | |
6ed551b4 | 26 | #include "system.h" |
4977bab6 | 27 | #include "coretypes.h" |
a757585a | 28 | #include "version.h" |
d83697f4 | 29 | #include "demangle.h" |
6ed551b4 | 30 | #include "intl.h" |
d83697f4 | 31 | #include "backtrace.h" |
345ed1fe | 32 | #include "diagnostic.h" |
4b84d650 | 33 | #include "diagnostic-color.h" |
717ebe91 | 34 | #include "edit-context.h" |
a93eac6a | 35 | #include "selftest.h" |
ace72598 | 36 | #include "selftest-diagnostic.h" |
6ed551b4 | 37 | |
c9db45aa TB |
38 | #ifdef HAVE_TERMIOS_H |
39 | # include <termios.h> | |
40 | #endif | |
41 | ||
42 | #ifdef GWINSZ_IN_SYS_IOCTL | |
43 | # include <sys/ioctl.h> | |
44 | #endif | |
45 | ||
243fbddd JM |
46 | #define pedantic_warning_kind(DC) \ |
47 | ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING) | |
48 | #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR) | |
5f0f4a3b | 49 | #define permissive_error_option(DC) ((DC)->opt_permissive) |
c707a408 | 50 | |
30f7a378 | 51 | /* Prototypes. */ |
c3811744 PC |
52 | static bool diagnostic_impl (rich_location *, int, const char *, |
53 | va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(3,0); | |
1c89478a MS |
54 | static bool diagnostic_n_impl (rich_location *, int, unsigned HOST_WIDE_INT, |
55 | const char *, const char *, va_list *, | |
c3811744 | 56 | diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0); |
a5fb7ad2 | 57 | |
79a490a9 | 58 | static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN; |
79a490a9 | 59 | static void real_abort (void) ATTRIBUTE_NORETURN; |
fbfc1192 | 60 | |
1da2ed5f JM |
61 | /* Name of program invoked, sans directories. */ |
62 | ||
63 | const char *progname; | |
64 | ||
f68fc4db GDR |
65 | /* A diagnostic_context surrogate for stderr. */ |
66 | static diagnostic_context global_diagnostic_context; | |
67 | diagnostic_context *global_dc = &global_diagnostic_context; | |
6ed551b4 | 68 | \f |
59650e48 ZW |
69 | /* Return a malloc'd string containing MSG formatted a la printf. The |
70 | caller is responsible for freeing the memory. */ | |
8e54f6d3 | 71 | char * |
e34d07f2 | 72 | build_message_string (const char *msg, ...) |
b903d81e | 73 | { |
36244024 | 74 | char *str; |
e34d07f2 | 75 | va_list ap; |
b903d81e | 76 | |
e34d07f2 | 77 | va_start (ap, msg); |
582f770b | 78 | str = xvasprintf (msg, ap); |
e34d07f2 | 79 | va_end (ap); |
b903d81e GDR |
80 | |
81 | return str; | |
82 | } | |
83 | ||
95bd1dd7 | 84 | /* Same as diagnostic_build_prefix, but only the source FILE is given. */ |
24805e80 | 85 | char * |
e78e8a0b | 86 | file_name_as_prefix (diagnostic_context *context, const char *f) |
24805e80 | 87 | { |
e78e8a0b JJ |
88 | const char *locus_cs |
89 | = colorize_start (pp_show_color (context->printer), "locus"); | |
90 | const char *locus_ce = colorize_stop (pp_show_color (context->printer)); | |
91 | return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce); | |
24805e80 GDR |
92 | } |
93 | ||
47b69537 | 94 | |
6ed551b4 | 95 | \f |
9fec0042 | 96 | /* Return the value of the getenv("COLUMNS") as an integer. If the |
c9db45aa TB |
97 | value is not set to a positive integer, use ioctl to get the |
98 | terminal width. If it fails, return INT_MAX. */ | |
99 | int | |
100 | get_terminal_width (void) | |
9fec0042 MLI |
101 | { |
102 | const char * s = getenv ("COLUMNS"); | |
103 | if (s != NULL) { | |
104 | int n = atoi (s); | |
105 | if (n > 0) | |
106 | return n; | |
107 | } | |
c9db45aa TB |
108 | |
109 | #ifdef TIOCGWINSZ | |
110 | struct winsize w; | |
111 | w.ws_col = 0; | |
112 | if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0) | |
113 | return w.ws_col; | |
114 | #endif | |
115 | ||
9fec0042 MLI |
116 | return INT_MAX; |
117 | } | |
118 | ||
119 | /* Set caret_max_width to value. */ | |
120 | void | |
121 | diagnostic_set_caret_max_width (diagnostic_context *context, int value) | |
122 | { | |
123 | /* One minus to account for the leading empty space. */ | |
124 | value = value ? value - 1 | |
025311c4 | 125 | : (isatty (fileno (pp_buffer (context->printer)->stream)) |
c9db45aa | 126 | ? get_terminal_width () - 1: INT_MAX); |
9fec0042 MLI |
127 | |
128 | if (value <= 0) | |
129 | value = INT_MAX; | |
130 | ||
131 | context->caret_max_width = value; | |
132 | } | |
133 | ||
47b69537 GDR |
134 | /* Initialize the diagnostic message outputting machinery. */ |
135 | void | |
5f0f4a3b | 136 | diagnostic_initialize (diagnostic_context *context, int n_opts) |
47b69537 | 137 | { |
5f0f4a3b JM |
138 | int i; |
139 | ||
b6fe0bb8 GDR |
140 | /* Allocate a basic pretty-printer. Clients will replace this a |
141 | much more elaborated pretty-printer if they wish. */ | |
5ed6ace5 | 142 | context->printer = XNEW (pretty_printer); |
da6ca2b5 | 143 | new (context->printer) pretty_printer (); |
47b69537 | 144 | |
b6fe0bb8 | 145 | memset (context->diagnostic_count, 0, sizeof context->diagnostic_count); |
7783b402 | 146 | context->warning_as_error_requested = false; |
5f0f4a3b JM |
147 | context->n_opts = n_opts; |
148 | context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts); | |
149 | for (i = 0; i < n_opts; i++) | |
150 | context->classify_diagnostic[i] = DK_UNSPECIFIED; | |
9fec0042 MLI |
151 | context->show_caret = false; |
152 | diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer)); | |
b816477a | 153 | for (i = 0; i < rich_location::STATICALLY_ALLOCATED_RANGES; i++) |
2a2703a2 | 154 | context->caret_chars[i] = '^'; |
2098fe9e | 155 | context->show_option_requested = false; |
b6fe0bb8 | 156 | context->abort_on_error = false; |
5f0f4a3b JM |
157 | context->show_column = false; |
158 | context->pedantic_errors = false; | |
159 | context->permissive = false; | |
160 | context->opt_permissive = 0; | |
161 | context->fatal_errors = false; | |
e3339d0f JM |
162 | context->dc_inhibit_warnings = false; |
163 | context->dc_warn_system_headers = false; | |
3a789837 | 164 | context->max_errors = 0; |
b6fe0bb8 | 165 | context->internal_error = NULL; |
47b69537 | 166 | diagnostic_starter (context) = default_diagnostic_starter; |
876217ae | 167 | context->start_span = default_diagnostic_start_span_fn; |
47b69537 | 168 | diagnostic_finalizer (context) = default_diagnostic_finalizer; |
5f0f4a3b | 169 | context->option_enabled = NULL; |
46625112 | 170 | context->option_state = NULL; |
5f0f4a3b | 171 | context->option_name = NULL; |
1b8b126f | 172 | context->last_location = UNKNOWN_LOCATION; |
b6fe0bb8 | 173 | context->last_module = 0; |
cf835838 | 174 | context->x_data = NULL; |
b6fe0bb8 | 175 | context->lock = 0; |
0e94b750 | 176 | context->inhibit_notes_p = false; |
cc015f3a | 177 | context->colorize_source_p = false; |
96e6ae57 | 178 | context->show_labels_p = false; |
56b61d7f | 179 | context->show_line_numbers_p = false; |
0141ab44 | 180 | context->min_margin_width = 0; |
cc015f3a DM |
181 | context->show_ruler_p = false; |
182 | context->parseable_fixits_p = false; | |
717ebe91 | 183 | context->edit_context_ptr = NULL; |
097f82ec DM |
184 | context->diagnostic_group_nesting_depth = 0; |
185 | context->diagnostic_group_emission_count = 0; | |
186 | context->begin_group_cb = NULL; | |
187 | context->end_group_cb = NULL; | |
47b69537 GDR |
188 | } |
189 | ||
97aa8bb6 MLI |
190 | /* Maybe initialize the color support. We require clients to do this |
191 | explicitly, since most clients don't want color. When called | |
192 | without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */ | |
193 | ||
194 | void | |
195 | diagnostic_color_init (diagnostic_context *context, int value /*= -1 */) | |
196 | { | |
197 | /* value == -1 is the default value. */ | |
198 | if (value < 0) | |
199 | { | |
200 | /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to | |
201 | -fdiagnostics-color=auto if GCC_COLORS is in the environment, | |
202 | otherwise default to -fdiagnostics-color=never, for other | |
203 | values default to that | |
204 | -fdiagnostics-color={never,auto,always}. */ | |
205 | if (DIAGNOSTICS_COLOR_DEFAULT == -1) | |
206 | { | |
207 | if (!getenv ("GCC_COLORS")) | |
208 | return; | |
209 | value = DIAGNOSTICS_COLOR_AUTO; | |
210 | } | |
211 | else | |
212 | value = DIAGNOSTICS_COLOR_DEFAULT; | |
213 | } | |
214 | pp_show_color (context->printer) | |
215 | = colorize_init ((diagnostic_color_rule_t) value); | |
216 | } | |
217 | ||
d0b8780d MLI |
218 | /* Do any cleaning up required after the last diagnostic is emitted. */ |
219 | ||
220 | void | |
221 | diagnostic_finish (diagnostic_context *context) | |
222 | { | |
223 | /* Some of the errors may actually have been warnings. */ | |
0d179691 | 224 | if (diagnostic_kind_count (context, DK_WERROR)) |
d0b8780d MLI |
225 | { |
226 | /* -Werror was given. */ | |
227 | if (context->warning_as_error_requested) | |
228 | pp_verbatim (context->printer, | |
1f01c3ec | 229 | _("%s: all warnings being treated as errors"), |
d0b8780d MLI |
230 | progname); |
231 | /* At least one -Werror= was given. */ | |
232 | else | |
233 | pp_verbatim (context->printer, | |
1f01c3ec | 234 | _("%s: some warnings being treated as errors"), |
d0b8780d | 235 | progname); |
f8923f7e | 236 | pp_newline_and_flush (context->printer); |
d0b8780d | 237 | } |
7ecc3eb9 DS |
238 | |
239 | diagnostic_file_cache_fini (); | |
3edf64aa DM |
240 | |
241 | XDELETEVEC (context->classify_diagnostic); | |
242 | context->classify_diagnostic = NULL; | |
243 | ||
244 | /* diagnostic_initialize allocates context->printer using XNEW | |
245 | and placement-new. */ | |
246 | context->printer->~pretty_printer (); | |
247 | XDELETE (context->printer); | |
248 | context->printer = NULL; | |
717ebe91 DM |
249 | |
250 | if (context->edit_context_ptr) | |
251 | { | |
252 | delete context->edit_context_ptr; | |
253 | context->edit_context_ptr = NULL; | |
254 | } | |
d0b8780d MLI |
255 | } |
256 | ||
178b58b5 JM |
257 | /* Initialize DIAGNOSTIC, where the message MSG has already been |
258 | translated. */ | |
47b69537 | 259 | void |
178b58b5 | 260 | diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg, |
8a645150 | 261 | va_list *args, rich_location *richloc, |
178b58b5 | 262 | diagnostic_t kind) |
47b69537 | 263 | { |
8a645150 | 264 | gcc_assert (richloc); |
fa6ef813 | 265 | diagnostic->message.err_no = errno; |
47b69537 | 266 | diagnostic->message.args_ptr = args; |
178b58b5 | 267 | diagnostic->message.format_spec = msg; |
8a645150 DM |
268 | diagnostic->message.m_richloc = richloc; |
269 | diagnostic->richloc = richloc; | |
47b69537 | 270 | diagnostic->kind = kind; |
ccf08a6e | 271 | diagnostic->option_index = 0; |
47b69537 GDR |
272 | } |
273 | ||
178b58b5 JM |
274 | /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been |
275 | translated. */ | |
276 | void | |
277 | diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid, | |
8a645150 | 278 | va_list *args, rich_location *richloc, |
178b58b5 JM |
279 | diagnostic_t kind) |
280 | { | |
8a645150 DM |
281 | gcc_assert (richloc); |
282 | diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind); | |
283 | } | |
284 | ||
285 | static const char *const diagnostic_kind_color[] = { | |
286 | #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C), | |
287 | #include "diagnostic.def" | |
288 | #undef DEFINE_DIAGNOSTIC_KIND | |
289 | NULL | |
290 | }; | |
291 | ||
292 | /* Get a color name for diagnostics of type KIND | |
293 | Result could be NULL. */ | |
294 | ||
295 | const char * | |
296 | diagnostic_get_color_for_kind (diagnostic_t kind) | |
297 | { | |
298 | return diagnostic_kind_color[kind]; | |
178b58b5 JM |
299 | } |
300 | ||
101e910b NS |
301 | /* Return a formatted line and column ':%line:%column'. Elided if |
302 | zero. The result is a statically allocated buffer. */ | |
303 | ||
304 | static const char * | |
305 | maybe_line_and_column (int line, int col) | |
306 | { | |
307 | static char result[32]; | |
308 | ||
309 | if (line) | |
310 | { | |
311 | size_t l = snprintf (result, sizeof (result), | |
312 | col ? ":%d:%d" : ":%d", line, col); | |
313 | gcc_checking_assert (l < sizeof (result)); | |
314 | } | |
315 | else | |
316 | result[0] = 0; | |
317 | return result; | |
318 | } | |
319 | ||
876217ae DM |
320 | /* Return a malloc'd string describing a location e.g. "foo.c:42:10". |
321 | The caller is responsible for freeing the memory. */ | |
322 | ||
323 | static char * | |
324 | diagnostic_get_location_text (diagnostic_context *context, | |
325 | expanded_location s) | |
326 | { | |
327 | pretty_printer *pp = context->printer; | |
328 | const char *locus_cs = colorize_start (pp_show_color (pp), "locus"); | |
329 | const char *locus_ce = colorize_stop (pp_show_color (pp)); | |
101e910b NS |
330 | const char *file = s.file ? s.file : progname; |
331 | int line = strcmp (file, N_("<built-in>")) ? s.line : 0; | |
332 | int col = context->show_column ? s.column : 0; | |
876217ae | 333 | |
101e910b NS |
334 | const char *line_col = maybe_line_and_column (line, col); |
335 | return build_message_string ("%s%s%s:%s", locus_cs, file, | |
336 | line_col, locus_ce); | |
876217ae DM |
337 | } |
338 | ||
339 | /* Return a malloc'd string describing a location and the severity of the | |
340 | diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for | |
341 | freeing the memory. */ | |
47b69537 | 342 | char * |
243fbddd | 343 | diagnostic_build_prefix (diagnostic_context *context, |
dfa32261 | 344 | const diagnostic_info *diagnostic) |
47b69537 | 345 | { |
ef9772c8 | 346 | static const char *const diagnostic_kind_text[] = { |
4b84d650 | 347 | #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T), |
4b1d52c7 GDR |
348 | #include "diagnostic.def" |
349 | #undef DEFINE_DIAGNOSTIC_KIND | |
350 | "must-not-happen" | |
351 | }; | |
99abe958 MLI |
352 | gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND); |
353 | ||
255508dd | 354 | const char *text = _(diagnostic_kind_text[diagnostic->kind]); |
4b84d650 | 355 | const char *text_cs = "", *text_ce = ""; |
4b84d650 JJ |
356 | pretty_printer *pp = context->printer; |
357 | ||
358 | if (diagnostic_kind_color[diagnostic->kind]) | |
359 | { | |
360 | text_cs = colorize_start (pp_show_color (pp), | |
361 | diagnostic_kind_color[diagnostic->kind]); | |
362 | text_ce = colorize_stop (pp_show_color (pp)); | |
363 | } | |
4b84d650 | 364 | |
99abe958 | 365 | expanded_location s = diagnostic_expand_location (diagnostic); |
876217ae DM |
366 | char *location_text = diagnostic_get_location_text (context, s); |
367 | ||
368 | char *result = build_message_string ("%s %s%s%s", location_text, | |
369 | text_cs, text, text_ce); | |
370 | free (location_text); | |
371 | return result; | |
47b69537 GDR |
372 | } |
373 | ||
d83697f4 ILT |
374 | /* Functions at which to stop the backtrace print. It's not |
375 | particularly helpful to print the callers of these functions. */ | |
376 | ||
377 | static const char * const bt_stop[] = | |
378 | { | |
379 | "main", | |
3edf64aa | 380 | "toplev::main", |
d83697f4 ILT |
381 | "execute_one_pass", |
382 | "compile_file", | |
383 | }; | |
384 | ||
385 | /* A callback function passed to the backtrace_full function. */ | |
386 | ||
387 | static int | |
388 | bt_callback (void *data, uintptr_t pc, const char *filename, int lineno, | |
389 | const char *function) | |
390 | { | |
391 | int *pcount = (int *) data; | |
392 | ||
393 | /* If we don't have any useful information, don't print | |
394 | anything. */ | |
395 | if (filename == NULL && function == NULL) | |
396 | return 0; | |
397 | ||
398 | /* Skip functions in diagnostic.c. */ | |
399 | if (*pcount == 0 | |
400 | && filename != NULL | |
c3284718 | 401 | && strcmp (lbasename (filename), "diagnostic.c") == 0) |
d83697f4 ILT |
402 | return 0; |
403 | ||
404 | /* Print up to 20 functions. We could make this a --param, but | |
405 | since this is only for debugging just use a constant for now. */ | |
406 | if (*pcount >= 20) | |
407 | { | |
408 | /* Returning a non-zero value stops the backtrace. */ | |
409 | return 1; | |
410 | } | |
411 | ++*pcount; | |
412 | ||
413 | char *alc = NULL; | |
414 | if (function != NULL) | |
415 | { | |
416 | char *str = cplus_demangle_v3 (function, | |
417 | (DMGL_VERBOSE | DMGL_ANSI | |
418 | | DMGL_GNU_V3 | DMGL_PARAMS)); | |
419 | if (str != NULL) | |
420 | { | |
421 | alc = str; | |
422 | function = str; | |
423 | } | |
424 | ||
425 | for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i) | |
426 | { | |
427 | size_t len = strlen (bt_stop[i]); | |
428 | if (strncmp (function, bt_stop[i], len) == 0 | |
429 | && (function[len] == '\0' || function[len] == '(')) | |
430 | { | |
431 | if (alc != NULL) | |
432 | free (alc); | |
433 | /* Returning a non-zero value stops the backtrace. */ | |
434 | return 1; | |
435 | } | |
436 | } | |
437 | } | |
438 | ||
439 | fprintf (stderr, "0x%lx %s\n\t%s:%d\n", | |
a67a73fd | 440 | (unsigned long) pc, |
d83697f4 ILT |
441 | function == NULL ? "???" : function, |
442 | filename == NULL ? "???" : filename, | |
443 | lineno); | |
444 | ||
445 | if (alc != NULL) | |
446 | free (alc); | |
447 | ||
448 | return 0; | |
449 | } | |
450 | ||
451 | /* A callback function passed to the backtrace_full function. This is | |
452 | called if backtrace_full has an error. */ | |
453 | ||
454 | static void | |
455 | bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum) | |
456 | { | |
457 | if (errnum < 0) | |
458 | { | |
459 | /* This means that no debug info was available. Just quietly | |
460 | skip printing backtrace info. */ | |
461 | return; | |
462 | } | |
463 | fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ", | |
464 | errnum == 0 ? "" : xstrerror (errnum)); | |
465 | } | |
466 | ||
d0ea9f0a NS |
467 | /* Check if we've met the maximum error limit, and if so fatally exit |
468 | with a message. CONTEXT is the context to check, and FLUSH | |
469 | indicates whether a diagnostic_finish call is needed. */ | |
470 | ||
471 | void | |
472 | diagnostic_check_max_errors (diagnostic_context *context, bool flush) | |
473 | { | |
474 | if (!context->max_errors) | |
475 | return; | |
476 | ||
477 | int count = (diagnostic_kind_count (context, DK_ERROR) | |
478 | + diagnostic_kind_count (context, DK_SORRY) | |
479 | + diagnostic_kind_count (context, DK_WERROR)); | |
480 | ||
481 | if (count >= context->max_errors) | |
482 | { | |
483 | fnotice (stderr, | |
484 | "compilation terminated due to -fmax-errors=%u.\n", | |
485 | context->max_errors); | |
486 | if (flush) | |
487 | diagnostic_finish (context); | |
488 | exit (FATAL_EXIT_CODE); | |
489 | } | |
490 | } | |
491 | ||
9804f5fb ZW |
492 | /* Take any action which is expected to happen after the diagnostic |
493 | is written out. This function does not always return. */ | |
c4100eae | 494 | void |
79a490a9 | 495 | diagnostic_action_after_output (diagnostic_context *context, |
c4100eae | 496 | diagnostic_t diag_kind) |
9804f5fb | 497 | { |
c4100eae | 498 | switch (diag_kind) |
9804f5fb ZW |
499 | { |
500 | case DK_DEBUG: | |
501 | case DK_NOTE: | |
502 | case DK_ANACHRONISM: | |
503 | case DK_WARNING: | |
504 | break; | |
505 | ||
506 | case DK_ERROR: | |
507 | case DK_SORRY: | |
508 | if (context->abort_on_error) | |
509 | real_abort (); | |
243fbddd | 510 | if (context->fatal_errors) |
c65a01af RG |
511 | { |
512 | fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n"); | |
d0b8780d | 513 | diagnostic_finish (context); |
c65a01af RG |
514 | exit (FATAL_EXIT_CODE); |
515 | } | |
9804f5fb ZW |
516 | break; |
517 | ||
518 | case DK_ICE: | |
b55f40c1 | 519 | case DK_ICE_NOBT: |
d83697f4 | 520 | { |
b55f40c1 JJ |
521 | struct backtrace_state *state = NULL; |
522 | if (diag_kind == DK_ICE) | |
523 | state = backtrace_create_state (NULL, 0, bt_err_callback, NULL); | |
d83697f4 ILT |
524 | int count = 0; |
525 | if (state != NULL) | |
526 | backtrace_full (state, 2, bt_callback, bt_err_callback, | |
527 | (void *) &count); | |
528 | ||
529 | if (context->abort_on_error) | |
530 | real_abort (); | |
531 | ||
532 | fnotice (stderr, "Please submit a full bug report,\n" | |
533 | "with preprocessed source if appropriate.\n"); | |
534 | if (count > 0) | |
535 | fnotice (stderr, | |
536 | ("Please include the complete backtrace " | |
537 | "with any bug report.\n")); | |
538 | fnotice (stderr, "See %s for instructions.\n", bug_report_url); | |
9804f5fb | 539 | |
d83697f4 ILT |
540 | exit (ICE_EXIT_CODE); |
541 | } | |
9804f5fb ZW |
542 | |
543 | case DK_FATAL: | |
544 | if (context->abort_on_error) | |
545 | real_abort (); | |
d0b8780d | 546 | diagnostic_finish (context); |
9804f5fb ZW |
547 | fnotice (stderr, "compilation terminated.\n"); |
548 | exit (FATAL_EXIT_CODE); | |
549 | ||
550 | default: | |
d5706a1e | 551 | gcc_unreachable (); |
9804f5fb ZW |
552 | } |
553 | } | |
554 | ||
26d5ed6c DM |
555 | /* True if the last module or file in which a diagnostic was reported is |
556 | different from the current one. */ | |
557 | ||
558 | static bool | |
559 | last_module_changed_p (diagnostic_context *context, | |
560 | const line_map_ordinary *map) | |
561 | { | |
562 | return context->last_module != map; | |
563 | } | |
564 | ||
565 | /* Remember the current module or file as being the last one in which we | |
566 | report a diagnostic. */ | |
567 | ||
568 | static void | |
569 | set_last_module (diagnostic_context *context, const line_map_ordinary *map) | |
570 | { | |
571 | context->last_module = map; | |
572 | } | |
573 | ||
6ed551b4 | 574 | void |
07a0b324 | 575 | diagnostic_report_current_module (diagnostic_context *context, location_t where) |
6ed551b4 | 576 | { |
0e50b624 | 577 | const line_map_ordinary *map = NULL; |
6ed551b4 | 578 | |
b6fe0bb8 | 579 | if (pp_needs_newline (context->printer)) |
59650e48 | 580 | { |
b6fe0bb8 GDR |
581 | pp_newline (context->printer); |
582 | pp_needs_newline (context->printer) = false; | |
59650e48 | 583 | } |
6ed551b4 | 584 | |
07a0b324 | 585 | if (where <= BUILTINS_LOCATION) |
966e8f4d TT |
586 | return; |
587 | ||
07a0b324 TT |
588 | linemap_resolve_location (line_table, where, |
589 | LRK_MACRO_DEFINITION_LOCATION, | |
590 | &map); | |
591 | ||
26d5ed6c | 592 | if (map && last_module_changed_p (context, map)) |
59650e48 | 593 | { |
26d5ed6c | 594 | set_last_module (context, map); |
966e8f4d | 595 | if (! MAIN_FILE_P (map)) |
6773e15f | 596 | { |
9e525f08 NS |
597 | bool first = true; |
598 | do | |
966e8f4d | 599 | { |
f10a9135 NS |
600 | where = linemap_included_from (map); |
601 | map = linemap_included_from_linemap (line_table, map); | |
9e525f08 | 602 | const char *line_col |
f10a9135 | 603 | = maybe_line_and_column (SOURCE_LINE (map, where), |
9e525f08 NS |
604 | first && context->show_column |
605 | ? SOURCE_COLUMN (map, where) : 0); | |
606 | static const char *const msgs[] = | |
607 | { | |
608 | N_("In file included from"), | |
609 | N_(" from"), | |
610 | }; | |
611 | unsigned index = !first; | |
612 | pp_verbatim (context->printer, "%s%s %r%s%s%R", | |
613 | first ? "" : ",\n", _(msgs[index]), | |
614 | "locus", LINEMAP_FILE (map), line_col); | |
615 | first = false; | |
966e8f4d | 616 | } |
9e525f08 | 617 | while (! MAIN_FILE_P (map)); |
966e8f4d TT |
618 | pp_verbatim (context->printer, ":"); |
619 | pp_newline (context->printer); | |
6773e15f | 620 | } |
59650e48 ZW |
621 | } |
622 | } | |
400500c4 | 623 | |
4537ec0c | 624 | void |
79a490a9 AJ |
625 | default_diagnostic_starter (diagnostic_context *context, |
626 | diagnostic_info *diagnostic) | |
6ed551b4 | 627 | { |
2a2703a2 | 628 | diagnostic_report_current_module (context, diagnostic_location (diagnostic)); |
243fbddd JM |
629 | pp_set_prefix (context->printer, diagnostic_build_prefix (context, |
630 | diagnostic)); | |
59650e48 | 631 | } |
6ed551b4 | 632 | |
876217ae DM |
633 | void |
634 | default_diagnostic_start_span_fn (diagnostic_context *context, | |
635 | expanded_location exploc) | |
636 | { | |
5c6a2bf2 DM |
637 | char *text = diagnostic_get_location_text (context, exploc); |
638 | pp_string (context->printer, text); | |
639 | free (text); | |
876217ae DM |
640 | pp_newline (context->printer); |
641 | } | |
642 | ||
4537ec0c | 643 | void |
18767f65 MLI |
644 | default_diagnostic_finalizer (diagnostic_context *context, |
645 | diagnostic_info *diagnostic) | |
59650e48 | 646 | { |
cc015f3a | 647 | diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind); |
18767f65 | 648 | pp_destroy_prefix (context->printer); |
01e1dea3 | 649 | pp_flush (context->printer); |
6ed551b4 GDR |
650 | } |
651 | ||
79cf5994 DD |
652 | /* Interface to specify diagnostic kind overrides. Returns the |
653 | previous setting, or DK_UNSPECIFIED if the parameters are out of | |
3c8ca1ab EB |
654 | range. If OPTION_INDEX is zero, the new setting is for all the |
655 | diagnostics. */ | |
79cf5994 DD |
656 | diagnostic_t |
657 | diagnostic_classify_diagnostic (diagnostic_context *context, | |
658 | int option_index, | |
cd7fe53b DD |
659 | diagnostic_t new_kind, |
660 | location_t where) | |
79cf5994 DD |
661 | { |
662 | diagnostic_t old_kind; | |
663 | ||
3c8ca1ab | 664 | if (option_index < 0 |
5f0f4a3b | 665 | || option_index >= context->n_opts |
79cf5994 DD |
666 | || new_kind >= DK_LAST_DIAGNOSTIC_KIND) |
667 | return DK_UNSPECIFIED; | |
668 | ||
669 | old_kind = context->classify_diagnostic[option_index]; | |
cd7fe53b DD |
670 | |
671 | /* Handle pragmas separately, since we need to keep track of *where* | |
672 | the pragmas were. */ | |
673 | if (where != UNKNOWN_LOCATION) | |
674 | { | |
675 | int i; | |
676 | ||
3ba421e8 MLI |
677 | /* Record the command-line status, so we can reset it back on DK_POP. */ |
678 | if (old_kind == DK_UNSPECIFIED) | |
679 | { | |
b32bc1ac MLI |
680 | old_kind = !context->option_enabled (option_index, |
681 | context->option_state) | |
682 | ? DK_IGNORED : (context->warning_as_error_requested | |
683 | ? DK_ERROR : DK_WARNING); | |
3ba421e8 MLI |
684 | context->classify_diagnostic[option_index] = old_kind; |
685 | } | |
686 | ||
cd7fe53b DD |
687 | for (i = context->n_classification_history - 1; i >= 0; i --) |
688 | if (context->classification_history[i].option == option_index) | |
689 | { | |
690 | old_kind = context->classification_history[i].kind; | |
691 | break; | |
692 | } | |
693 | ||
694 | i = context->n_classification_history; | |
695 | context->classification_history = | |
696 | (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1) | |
697 | * sizeof (diagnostic_classification_change_t)); | |
698 | context->classification_history[i].location = where; | |
699 | context->classification_history[i].option = option_index; | |
700 | context->classification_history[i].kind = new_kind; | |
701 | context->n_classification_history ++; | |
702 | } | |
703 | else | |
704 | context->classify_diagnostic[option_index] = new_kind; | |
705 | ||
79cf5994 DD |
706 | return old_kind; |
707 | } | |
708 | ||
cd7fe53b DD |
709 | /* Save all diagnostic classifications in a stack. */ |
710 | void | |
711 | diagnostic_push_diagnostics (diagnostic_context *context, location_t where ATTRIBUTE_UNUSED) | |
712 | { | |
713 | context->push_list = (int *) xrealloc (context->push_list, (context->n_push + 1) * sizeof (int)); | |
714 | context->push_list[context->n_push ++] = context->n_classification_history; | |
715 | } | |
716 | ||
717 | /* Restore the topmost classification set off the stack. If the stack | |
718 | is empty, revert to the state based on command line parameters. */ | |
719 | void | |
720 | diagnostic_pop_diagnostics (diagnostic_context *context, location_t where) | |
721 | { | |
722 | int jump_to; | |
723 | int i; | |
724 | ||
725 | if (context->n_push) | |
726 | jump_to = context->push_list [-- context->n_push]; | |
727 | else | |
728 | jump_to = 0; | |
729 | ||
730 | i = context->n_classification_history; | |
731 | context->classification_history = | |
732 | (diagnostic_classification_change_t *) xrealloc (context->classification_history, (i + 1) | |
733 | * sizeof (diagnostic_classification_change_t)); | |
734 | context->classification_history[i].location = where; | |
735 | context->classification_history[i].option = jump_to; | |
736 | context->classification_history[i].kind = DK_POP; | |
737 | context->n_classification_history ++; | |
738 | } | |
739 | ||
a93eac6a DM |
740 | /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the |
741 | escaping rules for -fdiagnostics-parseable-fixits. */ | |
742 | ||
743 | static void | |
744 | print_escaped_string (pretty_printer *pp, const char *text) | |
745 | { | |
746 | gcc_assert (pp); | |
747 | gcc_assert (text); | |
748 | ||
749 | pp_character (pp, '"'); | |
750 | for (const char *ch = text; *ch; ch++) | |
751 | { | |
752 | switch (*ch) | |
753 | { | |
754 | case '\\': | |
755 | /* Escape backslash as two backslashes. */ | |
756 | pp_string (pp, "\\\\"); | |
757 | break; | |
758 | case '\t': | |
759 | /* Escape tab as "\t". */ | |
760 | pp_string (pp, "\\t"); | |
761 | break; | |
762 | case '\n': | |
763 | /* Escape newline as "\n". */ | |
764 | pp_string (pp, "\\n"); | |
765 | break; | |
766 | case '"': | |
767 | /* Escape doublequotes as \". */ | |
768 | pp_string (pp, "\\\""); | |
769 | break; | |
770 | default: | |
771 | if (ISPRINT (*ch)) | |
772 | pp_character (pp, *ch); | |
773 | else | |
774 | /* Use octal for non-printable chars. */ | |
775 | { | |
776 | unsigned char c = (*ch & 0xff); | |
777 | pp_printf (pp, "\\%o%o%o", (c / 64), (c / 8) & 007, c & 007); | |
778 | } | |
779 | break; | |
780 | } | |
781 | } | |
782 | pp_character (pp, '"'); | |
783 | } | |
784 | ||
785 | /* Implementation of -fdiagnostics-parseable-fixits. Print a | |
786 | machine-parseable version of all fixits in RICHLOC to PP. */ | |
787 | ||
788 | static void | |
789 | print_parseable_fixits (pretty_printer *pp, rich_location *richloc) | |
790 | { | |
791 | gcc_assert (pp); | |
792 | gcc_assert (richloc); | |
793 | ||
794 | for (unsigned i = 0; i < richloc->get_num_fixit_hints (); i++) | |
795 | { | |
796 | const fixit_hint *hint = richloc->get_fixit_hint (i); | |
620e594b | 797 | location_t start_loc = hint->get_start_loc (); |
a93eac6a DM |
798 | expanded_location start_exploc = expand_location (start_loc); |
799 | pp_string (pp, "fix-it:"); | |
800 | print_escaped_string (pp, start_exploc.file); | |
a93eac6a | 801 | /* For compatibility with clang, print as a half-open range. */ |
620e594b | 802 | location_t next_loc = hint->get_next_loc (); |
338035aa DM |
803 | expanded_location next_exploc = expand_location (next_loc); |
804 | pp_printf (pp, ":{%i:%i-%i:%i}:", | |
805 | start_exploc.line, start_exploc.column, | |
806 | next_exploc.line, next_exploc.column); | |
807 | print_escaped_string (pp, hint->get_string ()); | |
a93eac6a DM |
808 | pp_newline (pp); |
809 | } | |
810 | } | |
811 | ||
dc41c9b0 DM |
812 | /* Update the diag_class of DIAGNOSTIC based on its location |
813 | relative to any | |
814 | #pragma GCC diagnostic | |
815 | directives recorded within CONTEXT. | |
816 | ||
817 | Return the new diag_class of DIAGNOSTIC if it was updated, or | |
818 | DK_UNSPECIFIED otherwise. */ | |
819 | ||
820 | static diagnostic_t | |
821 | update_effective_level_from_pragmas (diagnostic_context *context, | |
822 | diagnostic_info *diagnostic) | |
823 | { | |
824 | diagnostic_t diag_class = DK_UNSPECIFIED; | |
825 | ||
826 | if (context->n_classification_history > 0) | |
827 | { | |
828 | location_t location = diagnostic_location (diagnostic); | |
829 | ||
830 | /* FIXME: Stupid search. Optimize later. */ | |
831 | for (int i = context->n_classification_history - 1; i >= 0; i --) | |
832 | { | |
833 | if (linemap_location_before_p | |
834 | (line_table, | |
835 | context->classification_history[i].location, | |
836 | location)) | |
837 | { | |
838 | if (context->classification_history[i].kind == (int) DK_POP) | |
839 | { | |
840 | i = context->classification_history[i].option; | |
841 | continue; | |
842 | } | |
843 | int option = context->classification_history[i].option; | |
844 | /* The option 0 is for all the diagnostics. */ | |
845 | if (option == 0 || option == diagnostic->option_index) | |
846 | { | |
847 | diag_class = context->classification_history[i].kind; | |
848 | if (diag_class != DK_UNSPECIFIED) | |
849 | diagnostic->kind = diag_class; | |
850 | break; | |
851 | } | |
852 | } | |
853 | } | |
854 | } | |
855 | ||
856 | return diag_class; | |
857 | } | |
858 | ||
80ceac09 DM |
859 | /* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's |
860 | printer, e.g. " [-Werror=uninitialized]". | |
861 | Subroutine of diagnostic_report_diagnostic. */ | |
862 | ||
863 | static void | |
864 | print_option_information (diagnostic_context *context, | |
865 | const diagnostic_info *diagnostic, | |
866 | diagnostic_t orig_diag_kind) | |
867 | { | |
868 | char *option_text; | |
869 | ||
870 | option_text = context->option_name (context, diagnostic->option_index, | |
871 | orig_diag_kind, diagnostic->kind); | |
872 | ||
873 | if (option_text) | |
874 | { | |
875 | pretty_printer *pp = context->printer; | |
876 | pp_string (pp, " ["); | |
877 | pp_string (pp, colorize_start (pp_show_color (pp), | |
878 | diagnostic_kind_color[diagnostic->kind])); | |
879 | pp_string (pp, option_text); | |
880 | pp_string (pp, colorize_stop (pp_show_color (pp))); | |
881 | pp_character (pp, ']'); | |
882 | free (option_text); | |
883 | } | |
884 | } | |
dc41c9b0 | 885 | |
59650e48 ZW |
886 | /* Report a diagnostic message (an error or a warning) as specified by |
887 | DC. This function is *the* subroutine in terms of which front-ends | |
888 | should implement their specific diagnostic handling modules. The | |
889 | front-end independent format specifiers are exactly those described | |
b8698a0f | 890 | in the documentation of output_format. |
71205d17 | 891 | Return true if a diagnostic was printed, false otherwise. */ |
400500c4 | 892 | |
71205d17 | 893 | bool |
79a490a9 AJ |
894 | diagnostic_report_diagnostic (diagnostic_context *context, |
895 | diagnostic_info *diagnostic) | |
400500c4 | 896 | { |
2a2703a2 | 897 | location_t location = diagnostic_location (diagnostic); |
d0b8780d | 898 | diagnostic_t orig_diag_kind = diagnostic->kind; |
4e2bae26 MLI |
899 | |
900 | /* Give preference to being able to inhibit warnings, before they | |
901 | get reclassified to something else. */ | |
71205d17 | 902 | if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN) |
5f0f4a3b | 903 | && !diagnostic_report_warnings_p (context, location)) |
71205d17 MLI |
904 | return false; |
905 | ||
b8698a0f | 906 | if (diagnostic->kind == DK_PEDWARN) |
d0b8780d | 907 | { |
243fbddd | 908 | diagnostic->kind = pedantic_warning_kind (context); |
d0b8780d MLI |
909 | /* We do this to avoid giving the message for -pedantic-errors. */ |
910 | orig_diag_kind = diagnostic->kind; | |
911 | } | |
0e94b750 MLI |
912 | |
913 | if (diagnostic->kind == DK_NOTE && context->inhibit_notes_p) | |
914 | return false; | |
b8698a0f | 915 | |
d5706a1e ZW |
916 | if (context->lock > 0) |
917 | { | |
918 | /* If we're reporting an ICE in the middle of some other error, | |
919 | try to flush out the previous error, then let this one | |
920 | through. Don't do this more than once. */ | |
b55f40c1 JJ |
921 | if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT) |
922 | && context->lock == 1) | |
f8923f7e | 923 | pp_newline_and_flush (context->printer); |
d5706a1e ZW |
924 | else |
925 | error_recursion (context); | |
926 | } | |
927 | ||
4e2bae26 MLI |
928 | /* If the user requested that warnings be treated as errors, so be |
929 | it. Note that we do this before the next block so that | |
930 | individual warnings can be overridden back to warnings with | |
931 | -Wno-error=*. */ | |
932 | if (context->warning_as_error_requested | |
933 | && diagnostic->kind == DK_WARNING) | |
03fd1ef6 | 934 | diagnostic->kind = DK_ERROR; |
b8698a0f | 935 | |
0c3641b0 MLI |
936 | if (diagnostic->option_index |
937 | && diagnostic->option_index != permissive_error_option (context)) | |
79cf5994 DD |
938 | { |
939 | /* This tests if the user provided the appropriate -Wfoo or | |
940 | -Wno-foo option. */ | |
46625112 JM |
941 | if (! context->option_enabled (diagnostic->option_index, |
942 | context->option_state)) | |
71205d17 | 943 | return false; |
cd7fe53b DD |
944 | |
945 | /* This tests for #pragma diagnostic changes. */ | |
dc41c9b0 DM |
946 | diagnostic_t diag_class |
947 | = update_effective_level_from_pragmas (context, diagnostic); | |
d5e7854c | 948 | |
79cf5994 DD |
949 | /* This tests if the user provided the appropriate -Werror=foo |
950 | option. */ | |
cd7fe53b | 951 | if (diag_class == DK_UNSPECIFIED |
d5e7854c NS |
952 | && (context->classify_diagnostic[diagnostic->option_index] |
953 | != DK_UNSPECIFIED)) | |
954 | diagnostic->kind | |
955 | = context->classify_diagnostic[diagnostic->option_index]; | |
956 | ||
c0220ea4 | 957 | /* This allows for future extensions, like temporarily disabling |
79cf5994 DD |
958 | warnings for ranges of source code. */ |
959 | if (diagnostic->kind == DK_IGNORED) | |
71205d17 | 960 | return false; |
79cf5994 | 961 | } |
2098fe9e | 962 | |
d0ea9f0a NS |
963 | if (diagnostic->kind != DK_NOTE) |
964 | diagnostic_check_max_errors (context); | |
965 | ||
d5706a1e | 966 | context->lock++; |
400500c4 | 967 | |
b55f40c1 | 968 | if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT) |
59650e48 | 969 | { |
52249a2e MLI |
970 | /* When not checking, ICEs are converted to fatal errors when an |
971 | error has already occurred. This is counteracted by | |
972 | abort_on_error. */ | |
b2b29377 MM |
973 | if (!CHECKING_P |
974 | && (diagnostic_kind_count (context, DK_ERROR) > 0 | |
975 | || diagnostic_kind_count (context, DK_SORRY) > 0) | |
52249a2e MLI |
976 | && !context->abort_on_error) |
977 | { | |
2a2703a2 MLI |
978 | expanded_location s |
979 | = expand_location (diagnostic_location (diagnostic)); | |
52249a2e MLI |
980 | fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n", |
981 | s.file, s.line); | |
982 | exit (ICE_EXIT_CODE); | |
983 | } | |
52249a2e | 984 | if (context->internal_error) |
a13812e2 JM |
985 | (*context->internal_error) (context, |
986 | diagnostic->message.format_spec, | |
52249a2e | 987 | diagnostic->message.args_ptr); |
43db5b3c | 988 | } |
37e99116 JJ |
989 | if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING) |
990 | ++diagnostic_kind_count (context, DK_WERROR); | |
991 | else | |
992 | ++diagnostic_kind_count (context, diagnostic->kind); | |
b8698a0f | 993 | |
097f82ec DM |
994 | /* Is this the initial diagnostic within the stack of groups? */ |
995 | if (context->diagnostic_group_emission_count == 0) | |
996 | { | |
997 | if (context->begin_group_cb) | |
998 | context->begin_group_cb (context); | |
999 | } | |
1000 | context->diagnostic_group_emission_count++; | |
1001 | ||
cf835838 JM |
1002 | diagnostic->message.x_data = &diagnostic->x_data; |
1003 | diagnostic->x_data = NULL; | |
52249a2e MLI |
1004 | pp_format (context->printer, &diagnostic->message); |
1005 | (*diagnostic_starter (context)) (context, diagnostic); | |
1006 | pp_output_formatted_text (context->printer); | |
80ceac09 DM |
1007 | if (context->show_option_requested) |
1008 | print_option_information (context, diagnostic, orig_diag_kind); | |
52249a2e | 1009 | (*diagnostic_finalizer (context)) (context, diagnostic); |
a93eac6a DM |
1010 | if (context->parseable_fixits_p) |
1011 | { | |
1012 | print_parseable_fixits (context->printer, diagnostic->richloc); | |
1013 | pp_flush (context->printer); | |
1014 | } | |
c4100eae | 1015 | diagnostic_action_after_output (context, diagnostic->kind); |
cf835838 | 1016 | diagnostic->x_data = NULL; |
9804f5fb | 1017 | |
717ebe91 | 1018 | if (context->edit_context_ptr) |
b09649fd DM |
1019 | if (diagnostic->richloc->fixits_can_be_auto_applied_p ()) |
1020 | context->edit_context_ptr->add_fixits (diagnostic->richloc); | |
717ebe91 | 1021 | |
9804f5fb | 1022 | context->lock--; |
71205d17 MLI |
1023 | |
1024 | return true; | |
59650e48 | 1025 | } |
43db5b3c | 1026 | |
59650e48 ZW |
1027 | /* Given a partial pathname as input, return another pathname that |
1028 | shares no directory elements with the pathname of __FILE__. This | |
1029 | is used by fancy_abort() to print `Internal compiler error in expr.c' | |
1030 | instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */ | |
3a538a66 | 1031 | |
59650e48 | 1032 | const char * |
79a490a9 | 1033 | trim_filename (const char *name) |
59650e48 ZW |
1034 | { |
1035 | static const char this_file[] = __FILE__; | |
1036 | const char *p = name, *q = this_file; | |
43db5b3c | 1037 | |
59650e48 ZW |
1038 | /* First skip any "../" in each filename. This allows us to give a proper |
1039 | reference to a file in a subdirectory. */ | |
d5706a1e | 1040 | while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2])) |
59650e48 ZW |
1041 | p += 3; |
1042 | ||
d5706a1e | 1043 | while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2])) |
59650e48 ZW |
1044 | q += 3; |
1045 | ||
1046 | /* Now skip any parts the two filenames have in common. */ | |
1047 | while (*p == *q && *p != 0 && *q != 0) | |
1048 | p++, q++; | |
1049 | ||
1050 | /* Now go backwards until the previous directory separator. */ | |
ef9af077 | 1051 | while (p > name && !IS_DIR_SEPARATOR (p[-1])) |
59650e48 ZW |
1052 | p--; |
1053 | ||
1054 | return p; | |
6ed551b4 | 1055 | } |
59650e48 ZW |
1056 | \f |
1057 | /* Standard error reporting routines in increasing order of severity. | |
1058 | All of these take arguments like printf. */ | |
6ed551b4 | 1059 | |
59650e48 ZW |
1060 | /* Text to be emitted verbatim to the error message stream; this |
1061 | produces no prefix and disables line-wrapping. Use rarely. */ | |
6ed551b4 | 1062 | void |
4b794eaf | 1063 | verbatim (const char *gmsgid, ...) |
59650e48 ZW |
1064 | { |
1065 | text_info text; | |
e34d07f2 | 1066 | va_list ap; |
59650e48 | 1067 | |
4b794eaf | 1068 | va_start (ap, gmsgid); |
fa6ef813 | 1069 | text.err_no = errno; |
59650e48 | 1070 | text.args_ptr = ≈ |
4b794eaf | 1071 | text.format_spec = _(gmsgid); |
cf835838 | 1072 | text.x_data = NULL; |
b6fe0bb8 | 1073 | pp_format_verbatim (global_dc->printer, &text); |
f8923f7e | 1074 | pp_newline_and_flush (global_dc->printer); |
e34d07f2 | 1075 | va_end (ap); |
59650e48 ZW |
1076 | } |
1077 | ||
dfa32261 MLI |
1078 | /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */ |
1079 | void | |
1080 | diagnostic_append_note (diagnostic_context *context, | |
1081 | location_t location, | |
1082 | const char * gmsgid, ...) | |
1083 | { | |
1084 | diagnostic_info diagnostic; | |
1085 | va_list ap; | |
ebedc9a3 | 1086 | rich_location richloc (line_table, location); |
dfa32261 MLI |
1087 | |
1088 | va_start (ap, gmsgid); | |
8a645150 DM |
1089 | diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE); |
1090 | if (context->inhibit_notes_p) | |
1091 | { | |
1092 | va_end (ap); | |
1093 | return; | |
1094 | } | |
653fee19 | 1095 | char *saved_prefix = pp_take_prefix (context->printer); |
8a645150 DM |
1096 | pp_set_prefix (context->printer, |
1097 | diagnostic_build_prefix (context, &diagnostic)); | |
dfa32261 MLI |
1098 | pp_format (context->printer, &diagnostic.message); |
1099 | pp_output_formatted_text (context->printer); | |
1100 | pp_destroy_prefix (context->printer); | |
4432aa6c | 1101 | pp_set_prefix (context->printer, saved_prefix); |
cc015f3a | 1102 | diagnostic_show_locus (context, &richloc, DK_NOTE); |
c3284718 | 1103 | va_end (ap); |
dfa32261 MLI |
1104 | } |
1105 | ||
64a5912c DM |
1106 | /* Implement emit_diagnostic, inform, warning, warning_at, pedwarn, |
1107 | permerror, error, error_at, error_at, sorry, fatal_error, internal_error, | |
1108 | and internal_error_no_backtrace, as documented and defined below. */ | |
c3811744 PC |
1109 | static bool |
1110 | diagnostic_impl (rich_location *richloc, int opt, | |
1111 | const char *gmsgid, | |
1112 | va_list *ap, diagnostic_t kind) | |
6ed551b4 | 1113 | { |
47b69537 | 1114 | diagnostic_info diagnostic; |
71205d17 MLI |
1115 | if (kind == DK_PERMERROR) |
1116 | { | |
c3811744 | 1117 | diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, |
243fbddd | 1118 | permissive_error_kind (global_dc)); |
5f0f4a3b | 1119 | diagnostic.option_index = permissive_error_option (global_dc); |
71205d17 | 1120 | } |
c3811744 PC |
1121 | else |
1122 | { | |
1123 | diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind); | |
71205d17 MLI |
1124 | if (kind == DK_WARNING || kind == DK_PEDWARN) |
1125 | diagnostic.option_index = opt; | |
c3811744 | 1126 | } |
56d35585 | 1127 | return diagnostic_report_diagnostic (global_dc, &diagnostic); |
c3811744 PC |
1128 | } |
1129 | ||
64a5912c DM |
1130 | /* Implement inform_n, warning_n, and error_n, as documented and |
1131 | defined below. */ | |
a5fb7ad2 | 1132 | static bool |
1c89478a | 1133 | diagnostic_n_impl (rich_location *richloc, int opt, unsigned HOST_WIDE_INT n, |
64a5912c DM |
1134 | const char *singular_gmsgid, |
1135 | const char *plural_gmsgid, | |
1136 | va_list *ap, diagnostic_t kind) | |
a5fb7ad2 PK |
1137 | { |
1138 | diagnostic_info diagnostic; | |
1c89478a MS |
1139 | unsigned long gtn; |
1140 | ||
1141 | if (sizeof n <= sizeof gtn) | |
1142 | gtn = n; | |
1143 | else | |
1144 | /* Use the largest number ngettext can handle, otherwise | |
1145 | preserve the six least significant decimal digits for | |
1146 | languages where the plural form depends on them. */ | |
1147 | gtn = n <= ULONG_MAX ? n : n % 1000000LU + 1000000LU; | |
1148 | ||
1149 | const char *text = ngettext (singular_gmsgid, plural_gmsgid, gtn); | |
1150 | diagnostic_set_info_translated (&diagnostic, text, ap, richloc, kind); | |
a5fb7ad2 PK |
1151 | if (kind == DK_WARNING) |
1152 | diagnostic.option_index = opt; | |
56d35585 | 1153 | return diagnostic_report_diagnostic (global_dc, &diagnostic); |
c3811744 | 1154 | } |
71205d17 | 1155 | |
296c53ac MP |
1156 | /* Wrapper around diagnostic_impl taking a variable argument list. */ |
1157 | ||
c3811744 PC |
1158 | bool |
1159 | emit_diagnostic (diagnostic_t kind, location_t location, int opt, | |
1160 | const char *gmsgid, ...) | |
1161 | { | |
097f82ec | 1162 | auto_diagnostic_group d; |
c3811744 PC |
1163 | va_list ap; |
1164 | va_start (ap, gmsgid); | |
1165 | rich_location richloc (line_table, location); | |
1166 | bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, kind); | |
80f24351 MM |
1167 | va_end (ap); |
1168 | return ret; | |
6ed551b4 GDR |
1169 | } |
1170 | ||
296c53ac MP |
1171 | /* Wrapper around diagnostic_impl taking a va_list parameter. */ |
1172 | ||
1173 | bool | |
1174 | emit_diagnostic_valist (diagnostic_t kind, location_t location, int opt, | |
1175 | const char *gmsgid, va_list *ap) | |
1176 | { | |
1177 | rich_location richloc (line_table, location); | |
1178 | return diagnostic_impl (&richloc, opt, gmsgid, ap, kind); | |
1179 | } | |
1180 | ||
1f5b3869 | 1181 | /* An informative note at LOCATION. Use this for additional details on an error |
71205d17 | 1182 | message. */ |
6ed551b4 | 1183 | void |
1f5b3869 | 1184 | inform (location_t location, const char *gmsgid, ...) |
8a645150 | 1185 | { |
097f82ec | 1186 | auto_diagnostic_group d; |
8a645150 | 1187 | va_list ap; |
8a645150 | 1188 | va_start (ap, gmsgid); |
c3811744 PC |
1189 | rich_location richloc (line_table, location); |
1190 | diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_NOTE); | |
8a645150 DM |
1191 | va_end (ap); |
1192 | } | |
1193 | ||
64a5912c | 1194 | /* Same as "inform" above, but at RICHLOC. */ |
8a645150 | 1195 | void |
64a5912c | 1196 | inform (rich_location *richloc, const char *gmsgid, ...) |
d4ee4d25 | 1197 | { |
64a5912c DM |
1198 | gcc_assert (richloc); |
1199 | ||
097f82ec | 1200 | auto_diagnostic_group d; |
d4ee4d25 | 1201 | va_list ap; |
4b794eaf | 1202 | va_start (ap, gmsgid); |
c3811744 | 1203 | diagnostic_impl (richloc, -1, gmsgid, &ap, DK_NOTE); |
d4ee4d25 DD |
1204 | va_end (ap); |
1205 | } | |
1206 | ||
894e2652 SZ |
1207 | /* An informative note at LOCATION. Use this for additional details on an |
1208 | error message. */ | |
1209 | void | |
1c89478a MS |
1210 | inform_n (location_t location, unsigned HOST_WIDE_INT n, |
1211 | const char *singular_gmsgid, const char *plural_gmsgid, ...) | |
894e2652 | 1212 | { |
894e2652 | 1213 | va_list ap; |
894e2652 | 1214 | va_start (ap, plural_gmsgid); |
097f82ec | 1215 | auto_diagnostic_group d; |
64a5912c DM |
1216 | rich_location richloc (line_table, location); |
1217 | diagnostic_n_impl (&richloc, -1, n, singular_gmsgid, plural_gmsgid, | |
c3811744 | 1218 | &ap, DK_NOTE); |
894e2652 SZ |
1219 | va_end (ap); |
1220 | } | |
1221 | ||
71205d17 | 1222 | /* A warning at INPUT_LOCATION. Use this for code which is correct according |
b8698a0f | 1223 | to the relevant language specification but is likely to be buggy anyway. |
71205d17 MLI |
1224 | Returns true if the warning was printed, false if it was inhibited. */ |
1225 | bool | |
1226 | warning (int opt, const char *gmsgid, ...) | |
6ed551b4 | 1227 | { |
097f82ec | 1228 | auto_diagnostic_group d; |
e34d07f2 | 1229 | va_list ap; |
4b794eaf | 1230 | va_start (ap, gmsgid); |
c3811744 PC |
1231 | rich_location richloc (line_table, input_location); |
1232 | bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING); | |
e34d07f2 | 1233 | va_end (ap); |
80f24351 | 1234 | return ret; |
6ed551b4 GDR |
1235 | } |
1236 | ||
aa14403d | 1237 | /* A warning at LOCATION. Use this for code which is correct according to the |
71205d17 MLI |
1238 | relevant language specification but is likely to be buggy anyway. |
1239 | Returns true if the warning was printed, false if it was inhibited. */ | |
1240 | ||
1241 | bool | |
aa14403d | 1242 | warning_at (location_t location, int opt, const char *gmsgid, ...) |
8a645150 | 1243 | { |
097f82ec | 1244 | auto_diagnostic_group d; |
8a645150 | 1245 | va_list ap; |
8a645150 | 1246 | va_start (ap, gmsgid); |
c3811744 PC |
1247 | rich_location richloc (line_table, location); |
1248 | bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING); | |
8a645150 DM |
1249 | va_end (ap); |
1250 | return ret; | |
1251 | } | |
1252 | ||
64a5912c | 1253 | /* Same as "warning at" above, but using RICHLOC. */ |
8a645150 DM |
1254 | |
1255 | bool | |
64a5912c | 1256 | warning_at (rich_location *richloc, int opt, const char *gmsgid, ...) |
aa14403d | 1257 | { |
64a5912c DM |
1258 | gcc_assert (richloc); |
1259 | ||
097f82ec | 1260 | auto_diagnostic_group d; |
aa14403d | 1261 | va_list ap; |
aa14403d | 1262 | va_start (ap, gmsgid); |
c3811744 | 1263 | bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_WARNING); |
aa14403d | 1264 | va_end (ap); |
80f24351 | 1265 | return ret; |
aa14403d RAE |
1266 | } |
1267 | ||
64a5912c | 1268 | /* Same as warning_n plural variant below, but using RICHLOC. */ |
a5fb7ad2 PK |
1269 | |
1270 | bool | |
1c89478a | 1271 | warning_n (rich_location *richloc, int opt, unsigned HOST_WIDE_INT n, |
64a5912c | 1272 | const char *singular_gmsgid, const char *plural_gmsgid, ...) |
a5fb7ad2 | 1273 | { |
64a5912c DM |
1274 | gcc_assert (richloc); |
1275 | ||
097f82ec | 1276 | auto_diagnostic_group d; |
a5fb7ad2 PK |
1277 | va_list ap; |
1278 | va_start (ap, plural_gmsgid); | |
64a5912c DM |
1279 | bool ret = diagnostic_n_impl (richloc, opt, n, |
1280 | singular_gmsgid, plural_gmsgid, | |
1281 | &ap, DK_WARNING); | |
a5fb7ad2 PK |
1282 | va_end (ap); |
1283 | return ret; | |
1284 | } | |
1285 | ||
26e82579 JH |
1286 | /* A warning at LOCATION. Use this for code which is correct according to the |
1287 | relevant language specification but is likely to be buggy anyway. | |
1288 | Returns true if the warning was printed, false if it was inhibited. */ | |
1289 | ||
1290 | bool | |
1c89478a MS |
1291 | warning_n (location_t location, int opt, unsigned HOST_WIDE_INT n, |
1292 | const char *singular_gmsgid, const char *plural_gmsgid, ...) | |
26e82579 | 1293 | { |
097f82ec | 1294 | auto_diagnostic_group d; |
26e82579 | 1295 | va_list ap; |
26e82579 | 1296 | va_start (ap, plural_gmsgid); |
64a5912c DM |
1297 | rich_location richloc (line_table, location); |
1298 | bool ret = diagnostic_n_impl (&richloc, opt, n, | |
c3811744 PC |
1299 | singular_gmsgid, plural_gmsgid, |
1300 | &ap, DK_WARNING); | |
26e82579 JH |
1301 | va_end (ap); |
1302 | return ret; | |
1303 | } | |
1304 | ||
85790e66 MLI |
1305 | /* A "pedantic" warning at LOCATION: issues a warning unless |
1306 | -pedantic-errors was given on the command line, in which case it | |
1307 | issues an error. Use this for diagnostics required by the relevant | |
1308 | language standard, if you have chosen not to make them errors. | |
9804f5fb ZW |
1309 | |
1310 | Note that these diagnostics are issued independent of the setting | |
c1771a20 | 1311 | of the -Wpedantic command-line switch. To get a warning enabled |
fcf73884 | 1312 | only with that switch, use either "if (pedantic) pedwarn |
c1771a20 MLI |
1313 | (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a |
1314 | pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)". | |
fcf73884 | 1315 | |
71205d17 MLI |
1316 | Returns true if the warning was printed, false if it was inhibited. */ |
1317 | ||
85790e66 | 1318 | bool |
509c9d60 | 1319 | pedwarn (location_t location, int opt, const char *gmsgid, ...) |
85790e66 | 1320 | { |
097f82ec | 1321 | auto_diagnostic_group d; |
85790e66 | 1322 | va_list ap; |
85790e66 | 1323 | va_start (ap, gmsgid); |
c3811744 PC |
1324 | rich_location richloc (line_table, location); |
1325 | bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_PEDWARN); | |
85790e66 | 1326 | va_end (ap); |
80f24351 | 1327 | return ret; |
85790e66 MLI |
1328 | } |
1329 | ||
64a5912c | 1330 | /* Same as pedwarn above, but using RICHLOC. */ |
1a4f11c8 DM |
1331 | |
1332 | bool | |
64a5912c | 1333 | pedwarn (rich_location *richloc, int opt, const char *gmsgid, ...) |
1a4f11c8 | 1334 | { |
64a5912c DM |
1335 | gcc_assert (richloc); |
1336 | ||
097f82ec | 1337 | auto_diagnostic_group d; |
1a4f11c8 DM |
1338 | va_list ap; |
1339 | va_start (ap, gmsgid); | |
1340 | bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_PEDWARN); | |
1341 | va_end (ap); | |
1342 | return ret; | |
1343 | } | |
1344 | ||
7e99f74b MLI |
1345 | /* A "permissive" error at LOCATION: issues an error unless |
1346 | -fpermissive was given on the command line, in which case it issues | |
1347 | a warning. Use this for things that really should be errors but we | |
71205d17 | 1348 | want to support legacy code. |
7e99f74b | 1349 | |
71205d17 MLI |
1350 | Returns true if the warning was printed, false if it was inhibited. */ |
1351 | ||
1352 | bool | |
cbe5f3b3 | 1353 | permerror (location_t location, const char *gmsgid, ...) |
7e99f74b | 1354 | { |
097f82ec | 1355 | auto_diagnostic_group d; |
7e99f74b | 1356 | va_list ap; |
7e99f74b | 1357 | va_start (ap, gmsgid); |
c3811744 PC |
1358 | rich_location richloc (line_table, location); |
1359 | bool ret = diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_PERMERROR); | |
8a645150 DM |
1360 | va_end (ap); |
1361 | return ret; | |
1362 | } | |
1363 | ||
64a5912c | 1364 | /* Same as "permerror" above, but at RICHLOC. */ |
8a645150 DM |
1365 | |
1366 | bool | |
64a5912c | 1367 | permerror (rich_location *richloc, const char *gmsgid, ...) |
8a645150 | 1368 | { |
64a5912c DM |
1369 | gcc_assert (richloc); |
1370 | ||
097f82ec | 1371 | auto_diagnostic_group d; |
8a645150 | 1372 | va_list ap; |
8a645150 | 1373 | va_start (ap, gmsgid); |
c3811744 | 1374 | bool ret = diagnostic_impl (richloc, -1, gmsgid, &ap, DK_PERMERROR); |
7e99f74b | 1375 | va_end (ap); |
80f24351 | 1376 | return ret; |
7e99f74b MLI |
1377 | } |
1378 | ||
59650e48 ZW |
1379 | /* A hard error: the code is definitely ill-formed, and an object file |
1380 | will not be produced. */ | |
8514e318 | 1381 | void |
4b794eaf | 1382 | error (const char *gmsgid, ...) |
8514e318 | 1383 | { |
097f82ec | 1384 | auto_diagnostic_group d; |
e34d07f2 | 1385 | va_list ap; |
4b794eaf | 1386 | va_start (ap, gmsgid); |
c3811744 PC |
1387 | rich_location richloc (line_table, input_location); |
1388 | diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR); | |
e34d07f2 | 1389 | va_end (ap); |
93d87cb1 GDR |
1390 | } |
1391 | ||
894e2652 SZ |
1392 | /* A hard error: the code is definitely ill-formed, and an object file |
1393 | will not be produced. */ | |
1394 | void | |
1c89478a MS |
1395 | error_n (location_t location, unsigned HOST_WIDE_INT n, |
1396 | const char *singular_gmsgid, const char *plural_gmsgid, ...) | |
894e2652 | 1397 | { |
097f82ec | 1398 | auto_diagnostic_group d; |
894e2652 | 1399 | va_list ap; |
894e2652 | 1400 | va_start (ap, plural_gmsgid); |
64a5912c DM |
1401 | rich_location richloc (line_table, location); |
1402 | diagnostic_n_impl (&richloc, -1, n, singular_gmsgid, plural_gmsgid, | |
c3811744 | 1403 | &ap, DK_ERROR); |
894e2652 SZ |
1404 | va_end (ap); |
1405 | } | |
1406 | ||
c3811744 | 1407 | /* Same as above, but use location LOC instead of input_location. */ |
a63068b6 AH |
1408 | void |
1409 | error_at (location_t loc, const char *gmsgid, ...) | |
1410 | { | |
097f82ec | 1411 | auto_diagnostic_group d; |
a63068b6 | 1412 | va_list ap; |
a63068b6 | 1413 | va_start (ap, gmsgid); |
c3811744 PC |
1414 | rich_location richloc (line_table, loc); |
1415 | diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR); | |
8a645150 DM |
1416 | va_end (ap); |
1417 | } | |
1418 | ||
1419 | /* Same as above, but use RICH_LOC. */ | |
1420 | ||
1421 | void | |
64a5912c | 1422 | error_at (rich_location *richloc, const char *gmsgid, ...) |
8a645150 | 1423 | { |
64a5912c DM |
1424 | gcc_assert (richloc); |
1425 | ||
097f82ec | 1426 | auto_diagnostic_group d; |
8a645150 | 1427 | va_list ap; |
8a645150 | 1428 | va_start (ap, gmsgid); |
c3811744 | 1429 | diagnostic_impl (richloc, -1, gmsgid, &ap, DK_ERROR); |
a63068b6 AH |
1430 | va_end (ap); |
1431 | } | |
1432 | ||
59650e48 ZW |
1433 | /* "Sorry, not implemented." Use for a language feature which is |
1434 | required by the relevant specification but not implemented by GCC. | |
1435 | An object file will not be produced. */ | |
93d87cb1 | 1436 | void |
4b794eaf | 1437 | sorry (const char *gmsgid, ...) |
93d87cb1 | 1438 | { |
097f82ec | 1439 | auto_diagnostic_group d; |
e34d07f2 | 1440 | va_list ap; |
4b794eaf | 1441 | va_start (ap, gmsgid); |
c3811744 PC |
1442 | rich_location richloc (line_table, input_location); |
1443 | diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY); | |
e34d07f2 | 1444 | va_end (ap); |
59650e48 | 1445 | } |
43db5b3c | 1446 | |
ad172f72 AS |
1447 | /* Same as above, but use location LOC instead of input_location. */ |
1448 | void | |
1449 | sorry_at (location_t loc, const char *gmsgid, ...) | |
1450 | { | |
1451 | auto_diagnostic_group d; | |
1452 | va_list ap; | |
1453 | va_start (ap, gmsgid); | |
1454 | rich_location richloc (line_table, loc); | |
1455 | diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY); | |
1456 | va_end (ap); | |
1457 | } | |
1458 | ||
1da2ed5f JM |
1459 | /* Return true if an error or a "sorry" has been seen. Various |
1460 | processing is disabled after errors. */ | |
1461 | bool | |
1462 | seen_error (void) | |
1463 | { | |
1464 | return errorcount || sorrycount; | |
1465 | } | |
1466 | ||
3d00119c AB |
1467 | /* An error which is severe enough that we make no attempt to |
1468 | continue. Do not use this for internal consistency checks; that's | |
1469 | internal_error. Use of this function should be rare. */ | |
1470 | void | |
1471 | fatal_error (location_t loc, const char *gmsgid, ...) | |
1472 | { | |
097f82ec | 1473 | auto_diagnostic_group d; |
3d00119c | 1474 | va_list ap; |
3d00119c | 1475 | va_start (ap, gmsgid); |
c3811744 PC |
1476 | rich_location richloc (line_table, loc); |
1477 | diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_FATAL); | |
3d00119c AB |
1478 | va_end (ap); |
1479 | ||
1480 | gcc_unreachable (); | |
1481 | } | |
1482 | ||
59650e48 ZW |
1483 | /* An internal consistency check has failed. We make no attempt to |
1484 | continue. Note that unless there is debugging value to be had from | |
1485 | a more specific message, or some other good reason, you should use | |
1486 | abort () instead of calling this function directly. */ | |
1487 | void | |
4b794eaf | 1488 | internal_error (const char *gmsgid, ...) |
fbfc1192 | 1489 | { |
097f82ec | 1490 | auto_diagnostic_group d; |
e34d07f2 | 1491 | va_list ap; |
4b794eaf | 1492 | va_start (ap, gmsgid); |
c3811744 PC |
1493 | rich_location richloc (line_table, input_location); |
1494 | diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE); | |
e34d07f2 | 1495 | va_end (ap); |
fbfc1192 | 1496 | |
d5706a1e | 1497 | gcc_unreachable (); |
fbfc1192 | 1498 | } |
b55f40c1 JJ |
1499 | |
1500 | /* Like internal_error, but no backtrace will be printed. Used when | |
1501 | the internal error does not happen at the current location, but happened | |
1502 | somewhere else. */ | |
1503 | void | |
1504 | internal_error_no_backtrace (const char *gmsgid, ...) | |
1505 | { | |
097f82ec | 1506 | auto_diagnostic_group d; |
b55f40c1 | 1507 | va_list ap; |
b55f40c1 | 1508 | va_start (ap, gmsgid); |
c3811744 PC |
1509 | rich_location richloc (line_table, input_location); |
1510 | diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE_NOBT); | |
b55f40c1 JJ |
1511 | va_end (ap); |
1512 | ||
1513 | gcc_unreachable (); | |
1514 | } | |
59650e48 | 1515 | \f |
59650e48 ZW |
1516 | /* Special case error functions. Most are implemented in terms of the |
1517 | above, or should be. */ | |
e23bd218 | 1518 | |
59650e48 ZW |
1519 | /* Print a diagnostic MSGID on FILE. This is just fprintf, except it |
1520 | runs its second argument through gettext. */ | |
04c1334c | 1521 | void |
4b794eaf | 1522 | fnotice (FILE *file, const char *cmsgid, ...) |
04c1334c | 1523 | { |
e34d07f2 | 1524 | va_list ap; |
04c1334c | 1525 | |
4b794eaf JJ |
1526 | va_start (ap, cmsgid); |
1527 | vfprintf (file, _(cmsgid), ap); | |
e34d07f2 | 1528 | va_end (ap); |
04c1334c GDR |
1529 | } |
1530 | ||
59650e48 ZW |
1531 | /* Inform the user that an error occurred while trying to report some |
1532 | other error. This indicates catastrophic internal inconsistencies, | |
1533 | so give up now. But do try to flush out the previous error. | |
1534 | This mustn't use internal_error, that will cause infinite recursion. */ | |
1535 | ||
1536 | static void | |
79a490a9 | 1537 | error_recursion (diagnostic_context *context) |
59650e48 ZW |
1538 | { |
1539 | if (context->lock < 3) | |
f8923f7e | 1540 | pp_newline_and_flush (context->printer); |
59650e48 ZW |
1541 | |
1542 | fnotice (stderr, | |
1543 | "Internal compiler error: Error reporting routines re-entered.\n"); | |
d5706a1e ZW |
1544 | |
1545 | /* Call diagnostic_action_after_output to get the "please submit a bug | |
c4100eae MLI |
1546 | report" message. */ |
1547 | diagnostic_action_after_output (context, DK_ICE); | |
d5706a1e ZW |
1548 | |
1549 | /* Do not use gcc_unreachable here; that goes through internal_error | |
1550 | and therefore would cause infinite recursion. */ | |
1551 | real_abort (); | |
59650e48 ZW |
1552 | } |
1553 | ||
1554 | /* Report an internal compiler error in a friendly manner. This is | |
1555 | the function that gets called upon use of abort() in the source | |
1556 | code generally, thanks to a special macro. */ | |
1557 | ||
1558 | void | |
79a490a9 | 1559 | fancy_abort (const char *file, int line, const char *function) |
59650e48 ZW |
1560 | { |
1561 | internal_error ("in %s, at %s:%d", function, trim_filename (file), line); | |
1562 | } | |
1563 | ||
097f82ec DM |
1564 | /* class auto_diagnostic_group. */ |
1565 | ||
1566 | /* Constructor: "push" this group into global_dc. */ | |
1567 | ||
1568 | auto_diagnostic_group::auto_diagnostic_group () | |
1569 | { | |
1570 | global_dc->diagnostic_group_nesting_depth++; | |
1571 | } | |
1572 | ||
1573 | /* Destructor: "pop" this group from global_dc. */ | |
1574 | ||
1575 | auto_diagnostic_group::~auto_diagnostic_group () | |
1576 | { | |
1577 | if (--global_dc->diagnostic_group_nesting_depth == 0) | |
1578 | { | |
1579 | /* Handle the case where we've popped the final diagnostic group. | |
1580 | If any diagnostics were emitted, give the context a chance | |
1581 | to do something. */ | |
1582 | if (global_dc->diagnostic_group_emission_count > 0) | |
1583 | { | |
1584 | if (global_dc->end_group_cb) | |
1585 | global_dc->end_group_cb (global_dc); | |
1586 | } | |
1587 | global_dc->diagnostic_group_emission_count = 0; | |
1588 | } | |
1589 | } | |
1590 | ||
886e0865 GK |
1591 | /* Really call the system 'abort'. This has to go right at the end of |
1592 | this file, so that there are no functions after it that call abort | |
1593 | and get the system abort instead of our macro. */ | |
1594 | #undef abort | |
79a490a9 AJ |
1595 | static void |
1596 | real_abort (void) | |
886e0865 GK |
1597 | { |
1598 | abort (); | |
1599 | } | |
a93eac6a DM |
1600 | |
1601 | #if CHECKING_P | |
1602 | ||
1603 | namespace selftest { | |
1604 | ||
1605 | /* Helper function for test_print_escaped_string. */ | |
1606 | ||
1607 | static void | |
1608 | assert_print_escaped_string (const location &loc, const char *expected_output, | |
1609 | const char *input) | |
1610 | { | |
1611 | pretty_printer pp; | |
1612 | print_escaped_string (&pp, input); | |
1613 | ASSERT_STREQ_AT (loc, expected_output, pp_formatted_text (&pp)); | |
1614 | } | |
1615 | ||
1616 | #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \ | |
1617 | assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT) | |
1618 | ||
1619 | /* Tests of print_escaped_string. */ | |
1620 | ||
1621 | static void | |
1622 | test_print_escaped_string () | |
1623 | { | |
1624 | /* Empty string. */ | |
1625 | ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", ""); | |
1626 | ||
1627 | /* Non-empty string. */ | |
1628 | ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world"); | |
1629 | ||
1630 | /* Various things that need to be escaped: */ | |
1631 | /* Backslash. */ | |
1632 | ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"", | |
1633 | "before\\after"); | |
1634 | /* Tab. */ | |
1635 | ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"", | |
1636 | "before\tafter"); | |
1637 | /* Newline. */ | |
1638 | ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"", | |
1639 | "before\nafter"); | |
1640 | /* Double quote. */ | |
1641 | ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"", | |
1642 | "before\"after"); | |
1643 | ||
1644 | /* Non-printable characters: BEL: '\a': 0x07 */ | |
1645 | ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"", | |
1646 | "before\aafter"); | |
1647 | /* Non-printable characters: vertical tab: '\v': 0x0b */ | |
1648 | ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"", | |
1649 | "before\vafter"); | |
1650 | } | |
1651 | ||
1652 | /* Tests of print_parseable_fixits. */ | |
1653 | ||
1654 | /* Verify that print_parseable_fixits emits the empty string if there | |
1655 | are no fixits. */ | |
1656 | ||
1657 | static void | |
1658 | test_print_parseable_fixits_none () | |
1659 | { | |
1660 | pretty_printer pp; | |
1661 | rich_location richloc (line_table, UNKNOWN_LOCATION); | |
1662 | ||
1663 | print_parseable_fixits (&pp, &richloc); | |
1664 | ASSERT_STREQ ("", pp_formatted_text (&pp)); | |
1665 | } | |
1666 | ||
1667 | /* Verify that print_parseable_fixits does the right thing if there | |
1668 | is an insertion fixit hint. */ | |
1669 | ||
1670 | static void | |
1671 | test_print_parseable_fixits_insert () | |
1672 | { | |
1673 | pretty_printer pp; | |
1674 | rich_location richloc (line_table, UNKNOWN_LOCATION); | |
1675 | ||
1676 | linemap_add (line_table, LC_ENTER, false, "test.c", 0); | |
1677 | linemap_line_start (line_table, 5, 100); | |
1678 | linemap_add (line_table, LC_LEAVE, false, NULL, 0); | |
1679 | location_t where = linemap_position_for_column (line_table, 10); | |
254830ba | 1680 | richloc.add_fixit_insert_before (where, "added content"); |
a93eac6a DM |
1681 | |
1682 | print_parseable_fixits (&pp, &richloc); | |
1683 | ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n", | |
1684 | pp_formatted_text (&pp)); | |
1685 | } | |
1686 | ||
1687 | /* Verify that print_parseable_fixits does the right thing if there | |
1688 | is an removal fixit hint. */ | |
1689 | ||
1690 | static void | |
1691 | test_print_parseable_fixits_remove () | |
1692 | { | |
1693 | pretty_printer pp; | |
1694 | rich_location richloc (line_table, UNKNOWN_LOCATION); | |
1695 | ||
1696 | linemap_add (line_table, LC_ENTER, false, "test.c", 0); | |
1697 | linemap_line_start (line_table, 5, 100); | |
1698 | linemap_add (line_table, LC_LEAVE, false, NULL, 0); | |
1699 | source_range where; | |
1700 | where.m_start = linemap_position_for_column (line_table, 10); | |
1701 | where.m_finish = linemap_position_for_column (line_table, 20); | |
1702 | richloc.add_fixit_remove (where); | |
1703 | ||
1704 | print_parseable_fixits (&pp, &richloc); | |
1705 | ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n", | |
1706 | pp_formatted_text (&pp)); | |
1707 | } | |
1708 | ||
1709 | /* Verify that print_parseable_fixits does the right thing if there | |
1710 | is an replacement fixit hint. */ | |
1711 | ||
1712 | static void | |
1713 | test_print_parseable_fixits_replace () | |
1714 | { | |
1715 | pretty_printer pp; | |
1716 | rich_location richloc (line_table, UNKNOWN_LOCATION); | |
1717 | ||
1718 | linemap_add (line_table, LC_ENTER, false, "test.c", 0); | |
1719 | linemap_line_start (line_table, 5, 100); | |
1720 | linemap_add (line_table, LC_LEAVE, false, NULL, 0); | |
1721 | source_range where; | |
1722 | where.m_start = linemap_position_for_column (line_table, 10); | |
1723 | where.m_finish = linemap_position_for_column (line_table, 20); | |
1724 | richloc.add_fixit_replace (where, "replacement"); | |
1725 | ||
1726 | print_parseable_fixits (&pp, &richloc); | |
1727 | ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n", | |
1728 | pp_formatted_text (&pp)); | |
1729 | } | |
1730 | ||
ace72598 DM |
1731 | /* Verify that |
1732 | diagnostic_get_location_text (..., SHOW_COLUMN) | |
1733 | generates EXPECTED_LOC_TEXT, given FILENAME, LINE, COLUMN, with | |
1734 | colorization disabled. */ | |
1735 | ||
1736 | static void | |
1737 | assert_location_text (const char *expected_loc_text, | |
1738 | const char *filename, int line, int column, | |
1739 | bool show_column) | |
1740 | { | |
1741 | test_diagnostic_context dc; | |
1742 | dc.show_column = show_column; | |
1743 | ||
1744 | expanded_location xloc; | |
1745 | xloc.file = filename; | |
1746 | xloc.line = line; | |
1747 | xloc.column = column; | |
1748 | xloc.data = NULL; | |
1749 | xloc.sysp = false; | |
1750 | ||
1751 | char *actual_loc_text = diagnostic_get_location_text (&dc, xloc); | |
1752 | ASSERT_STREQ (expected_loc_text, actual_loc_text); | |
1753 | free (actual_loc_text); | |
1754 | } | |
1755 | ||
1756 | /* Verify that diagnostic_get_location_text works as expected. */ | |
1757 | ||
1758 | static void | |
1759 | test_diagnostic_get_location_text () | |
1760 | { | |
1761 | const char *old_progname = progname; | |
1762 | progname = "PROGNAME"; | |
1763 | assert_location_text ("PROGNAME:", NULL, 0, 0, true); | |
1764 | assert_location_text ("<built-in>:", "<built-in>", 42, 10, true); | |
1765 | assert_location_text ("foo.c:42:10:", "foo.c", 42, 10, true); | |
101e910b NS |
1766 | assert_location_text ("foo.c:42:", "foo.c", 42, 0, true); |
1767 | assert_location_text ("foo.c:", "foo.c", 0, 10, true); | |
ace72598 | 1768 | assert_location_text ("foo.c:42:", "foo.c", 42, 10, false); |
101e910b NS |
1769 | assert_location_text ("foo.c:", "foo.c", 0, 10, false); |
1770 | ||
1771 | maybe_line_and_column (INT_MAX, INT_MAX); | |
1772 | maybe_line_and_column (INT_MIN, INT_MIN); | |
1773 | ||
ace72598 DM |
1774 | progname = old_progname; |
1775 | } | |
1776 | ||
a93eac6a DM |
1777 | /* Run all of the selftests within this file. */ |
1778 | ||
1779 | void | |
1780 | diagnostic_c_tests () | |
1781 | { | |
1782 | test_print_escaped_string (); | |
1783 | test_print_parseable_fixits_none (); | |
1784 | test_print_parseable_fixits_insert (); | |
1785 | test_print_parseable_fixits_remove (); | |
1786 | test_print_parseable_fixits_replace (); | |
ace72598 | 1787 | test_diagnostic_get_location_text (); |
a93eac6a DM |
1788 | } |
1789 | ||
1790 | } // namespace selftest | |
1791 | ||
1792 | #endif /* #if CHECKING_P */ |