comparison gcc/diagnostic.c @ 16:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents f6334be47118
children 84e7813d76e9
comparison
equal deleted inserted replaced
15:561a7518be6b 16:04ced10e8804
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection 1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2 Copyright (C) 1999-2017 Free Software Foundation, Inc.
3 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@codesourcery.com> 3 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5 4
6 This file is part of GCC. 5 This file is part of GCC.
7 6
8 GCC is free software; you can redistribute it and/or modify it under 7 GCC is free software; you can redistribute it and/or modify it under
25 24
26 #include "config.h" 25 #include "config.h"
27 #include "system.h" 26 #include "system.h"
28 #include "coretypes.h" 27 #include "coretypes.h"
29 #include "version.h" 28 #include "version.h"
30 #include "input.h" 29 #include "demangle.h"
31 #include "intl.h" 30 #include "intl.h"
31 #include "backtrace.h"
32 #include "diagnostic.h" 32 #include "diagnostic.h"
33 #include "diagnostic-color.h"
34 #include "edit-context.h"
35 #include "selftest.h"
36
37 #ifdef HAVE_TERMIOS_H
38 # include <termios.h>
39 #endif
40
41 #ifdef GWINSZ_IN_SYS_IOCTL
42 # include <sys/ioctl.h>
43 #endif
33 44
34 #define pedantic_warning_kind(DC) \ 45 #define pedantic_warning_kind(DC) \
35 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING) 46 ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
36 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR) 47 #define permissive_error_kind(DC) ((DC)->permissive ? DK_WARNING : DK_ERROR)
37 #define permissive_error_option(DC) ((DC)->opt_permissive) 48 #define permissive_error_option(DC) ((DC)->opt_permissive)
38 49
39 /* Prototypes. */ 50 /* Prototypes. */
40 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1; 51 static bool diagnostic_impl (rich_location *, int, const char *,
52 va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(3,0);
53 static bool diagnostic_n_impl (location_t, int, int, const char *,
54 const char *, va_list *,
55 diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0);
56 static bool diagnostic_n_impl_richloc (rich_location *, int, int, const char *,
57 const char *, va_list *,
58 diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0);
41 59
42 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN; 60 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
43
44 static void diagnostic_action_after_output (diagnostic_context *,
45 diagnostic_info *);
46 static void real_abort (void) ATTRIBUTE_NORETURN; 61 static void real_abort (void) ATTRIBUTE_NORETURN;
47 62
48 /* Name of program invoked, sans directories. */ 63 /* Name of program invoked, sans directories. */
49 64
50 const char *progname; 65 const char *progname;
51 66
52 /* A diagnostic_context surrogate for stderr. */ 67 /* A diagnostic_context surrogate for stderr. */
53 static diagnostic_context global_diagnostic_context; 68 static diagnostic_context global_diagnostic_context;
54 diagnostic_context *global_dc = &global_diagnostic_context; 69 diagnostic_context *global_dc = &global_diagnostic_context;
55
56 70
57 /* Return a malloc'd string containing MSG formatted a la printf. The 71 /* Return a malloc'd string containing MSG formatted a la printf. The
58 caller is responsible for freeing the memory. */ 72 caller is responsible for freeing the memory. */
59 static char * 73 char *
60 build_message_string (const char *msg, ...) 74 build_message_string (const char *msg, ...)
61 { 75 {
62 char *str; 76 char *str;
63 va_list ap; 77 va_list ap;
64 78
65 va_start (ap, msg); 79 va_start (ap, msg);
66 vasprintf (&str, msg, ap); 80 str = xvasprintf (msg, ap);
67 va_end (ap); 81 va_end (ap);
68 82
69 return str; 83 return str;
70 } 84 }
71 85
72 /* Same as diagnostic_build_prefix, but only the source FILE is given. */ 86 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
73 char * 87 char *
74 file_name_as_prefix (const char *f) 88 file_name_as_prefix (diagnostic_context *context, const char *f)
75 { 89 {
76 return build_message_string ("%s: ", f); 90 const char *locus_cs
91 = colorize_start (pp_show_color (context->printer), "locus");
92 const char *locus_ce = colorize_stop (pp_show_color (context->printer));
93 return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce);
77 } 94 }
78 95
79 96
80 97
98 /* Return the value of the getenv("COLUMNS") as an integer. If the
99 value is not set to a positive integer, use ioctl to get the
100 terminal width. If it fails, return INT_MAX. */
101 int
102 get_terminal_width (void)
103 {
104 const char * s = getenv ("COLUMNS");
105 if (s != NULL) {
106 int n = atoi (s);
107 if (n > 0)
108 return n;
109 }
110
111 #ifdef TIOCGWINSZ
112 struct winsize w;
113 w.ws_col = 0;
114 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
115 return w.ws_col;
116 #endif
117
118 return INT_MAX;
119 }
120
121 /* Set caret_max_width to value. */
122 void
123 diagnostic_set_caret_max_width (diagnostic_context *context, int value)
124 {
125 /* One minus to account for the leading empty space. */
126 value = value ? value - 1
127 : (isatty (fileno (pp_buffer (context->printer)->stream))
128 ? get_terminal_width () - 1: INT_MAX);
129
130 if (value <= 0)
131 value = INT_MAX;
132
133 context->caret_max_width = value;
134 }
135
81 /* Initialize the diagnostic message outputting machinery. */ 136 /* Initialize the diagnostic message outputting machinery. */
82 void 137 void
83 diagnostic_initialize (diagnostic_context *context, int n_opts) 138 diagnostic_initialize (diagnostic_context *context, int n_opts)
84 { 139 {
85 int i; 140 int i;
86 141
87 /* Allocate a basic pretty-printer. Clients will replace this a 142 /* Allocate a basic pretty-printer. Clients will replace this a
88 much more elaborated pretty-printer if they wish. */ 143 much more elaborated pretty-printer if they wish. */
89 context->printer = XNEW (pretty_printer); 144 context->printer = XNEW (pretty_printer);
90 pp_construct (context->printer, NULL, 0); 145 new (context->printer) pretty_printer ();
91 /* By default, diagnostics are sent to stderr. */
92 context->printer->buffer->stream = stderr;
93 /* By default, we emit prefixes once per message. */
94 context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
95 146
96 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count); 147 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
97 context->some_warnings_are_errors = false;
98 context->warning_as_error_requested = false; 148 context->warning_as_error_requested = false;
99 context->n_opts = n_opts; 149 context->n_opts = n_opts;
100 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts); 150 context->classify_diagnostic = XNEWVEC (diagnostic_t, n_opts);
101 for (i = 0; i < n_opts; i++) 151 for (i = 0; i < n_opts; i++)
102 context->classify_diagnostic[i] = DK_UNSPECIFIED; 152 context->classify_diagnostic[i] = DK_UNSPECIFIED;
153 context->show_caret = false;
154 diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer));
155 for (i = 0; i < rich_location::STATICALLY_ALLOCATED_RANGES; i++)
156 context->caret_chars[i] = '^';
103 context->show_option_requested = false; 157 context->show_option_requested = false;
104 context->abort_on_error = false; 158 context->abort_on_error = false;
105 context->show_column = false; 159 context->show_column = false;
106 context->pedantic_errors = false; 160 context->pedantic_errors = false;
107 context->permissive = false; 161 context->permissive = false;
110 context->dc_inhibit_warnings = false; 164 context->dc_inhibit_warnings = false;
111 context->dc_warn_system_headers = false; 165 context->dc_warn_system_headers = false;
112 context->max_errors = 0; 166 context->max_errors = 0;
113 context->internal_error = NULL; 167 context->internal_error = NULL;
114 diagnostic_starter (context) = default_diagnostic_starter; 168 diagnostic_starter (context) = default_diagnostic_starter;
169 context->start_span = default_diagnostic_start_span_fn;
115 diagnostic_finalizer (context) = default_diagnostic_finalizer; 170 diagnostic_finalizer (context) = default_diagnostic_finalizer;
116 context->option_enabled = NULL; 171 context->option_enabled = NULL;
117 context->option_state = NULL; 172 context->option_state = NULL;
118 context->option_name = NULL; 173 context->option_name = NULL;
174 context->last_location = UNKNOWN_LOCATION;
119 context->last_module = 0; 175 context->last_module = 0;
120 context->x_data = NULL; 176 context->x_data = NULL;
121 context->lock = 0; 177 context->lock = 0;
122 context->inhibit_notes_p = false; 178 context->inhibit_notes_p = false;
179 context->colorize_source_p = false;
180 context->show_ruler_p = false;
181 context->parseable_fixits_p = false;
182 context->edit_context_ptr = NULL;
183 }
184
185 /* Maybe initialize the color support. We require clients to do this
186 explicitly, since most clients don't want color. When called
187 without a VALUE, it initializes with DIAGNOSTICS_COLOR_DEFAULT. */
188
189 void
190 diagnostic_color_init (diagnostic_context *context, int value /*= -1 */)
191 {
192 /* value == -1 is the default value. */
193 if (value < 0)
194 {
195 /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to
196 -fdiagnostics-color=auto if GCC_COLORS is in the environment,
197 otherwise default to -fdiagnostics-color=never, for other
198 values default to that
199 -fdiagnostics-color={never,auto,always}. */
200 if (DIAGNOSTICS_COLOR_DEFAULT == -1)
201 {
202 if (!getenv ("GCC_COLORS"))
203 return;
204 value = DIAGNOSTICS_COLOR_AUTO;
205 }
206 else
207 value = DIAGNOSTICS_COLOR_DEFAULT;
208 }
209 pp_show_color (context->printer)
210 = colorize_init ((diagnostic_color_rule_t) value);
123 } 211 }
124 212
125 /* Do any cleaning up required after the last diagnostic is emitted. */ 213 /* Do any cleaning up required after the last diagnostic is emitted. */
126 214
127 void 215 void
128 diagnostic_finish (diagnostic_context *context) 216 diagnostic_finish (diagnostic_context *context)
129 { 217 {
130 /* Some of the errors may actually have been warnings. */ 218 /* Some of the errors may actually have been warnings. */
131 if (context->some_warnings_are_errors) 219 if (diagnostic_kind_count (context, DK_WERROR))
132 { 220 {
133 /* -Werror was given. */ 221 /* -Werror was given. */
134 if (context->warning_as_error_requested) 222 if (context->warning_as_error_requested)
135 pp_verbatim (context->printer, 223 pp_verbatim (context->printer,
136 _("%s: all warnings being treated as errors\n"), 224 _("%s: all warnings being treated as errors"),
137 progname); 225 progname);
138 /* At least one -Werror= was given. */ 226 /* At least one -Werror= was given. */
139 else 227 else
140 pp_verbatim (context->printer, 228 pp_verbatim (context->printer,
141 _("%s: some warnings being treated as errors\n"), 229 _("%s: some warnings being treated as errors"),
142 progname); 230 progname);
143 pp_flush (context->printer); 231 pp_newline_and_flush (context->printer);
232 }
233
234 diagnostic_file_cache_fini ();
235
236 XDELETEVEC (context->classify_diagnostic);
237 context->classify_diagnostic = NULL;
238
239 /* diagnostic_initialize allocates context->printer using XNEW
240 and placement-new. */
241 context->printer->~pretty_printer ();
242 XDELETE (context->printer);
243 context->printer = NULL;
244
245 if (context->edit_context_ptr)
246 {
247 delete context->edit_context_ptr;
248 context->edit_context_ptr = NULL;
144 } 249 }
145 } 250 }
146 251
147 /* Initialize DIAGNOSTIC, where the message MSG has already been 252 /* Initialize DIAGNOSTIC, where the message MSG has already been
148 translated. */ 253 translated. */
149 void 254 void
150 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg, 255 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
151 va_list *args, location_t location, 256 va_list *args, rich_location *richloc,
152 diagnostic_t kind) 257 diagnostic_t kind)
153 { 258 {
259 gcc_assert (richloc);
154 diagnostic->message.err_no = errno; 260 diagnostic->message.err_no = errno;
155 diagnostic->message.args_ptr = args; 261 diagnostic->message.args_ptr = args;
156 diagnostic->message.format_spec = msg; 262 diagnostic->message.format_spec = msg;
157 diagnostic->location = location; 263 diagnostic->message.m_richloc = richloc;
158 diagnostic->override_column = 0; 264 diagnostic->richloc = richloc;
159 diagnostic->kind = kind; 265 diagnostic->kind = kind;
160 diagnostic->option_index = 0; 266 diagnostic->option_index = 0;
161 } 267 }
162 268
163 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been 269 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
164 translated. */ 270 translated. */
165 void 271 void
166 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid, 272 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
167 va_list *args, location_t location, 273 va_list *args, rich_location *richloc,
168 diagnostic_t kind) 274 diagnostic_t kind)
169 { 275 {
170 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind); 276 gcc_assert (richloc);
171 } 277 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, richloc, kind);
172 278 }
173 /* Return a malloc'd string describing a location. The caller is 279
174 responsible for freeing the memory. */ 280 static const char *const diagnostic_kind_color[] = {
281 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
282 #include "diagnostic.def"
283 #undef DEFINE_DIAGNOSTIC_KIND
284 NULL
285 };
286
287 /* Get a color name for diagnostics of type KIND
288 Result could be NULL. */
289
290 const char *
291 diagnostic_get_color_for_kind (diagnostic_t kind)
292 {
293 return diagnostic_kind_color[kind];
294 }
295
296 /* Return a malloc'd string describing a location e.g. "foo.c:42:10".
297 The caller is responsible for freeing the memory. */
298
299 static char *
300 diagnostic_get_location_text (diagnostic_context *context,
301 expanded_location s)
302 {
303 pretty_printer *pp = context->printer;
304 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
305 const char *locus_ce = colorize_stop (pp_show_color (pp));
306
307 if (s.file == NULL)
308 return build_message_string ("%s%s:%s", locus_cs, progname, locus_ce);
309
310 if (!strcmp (s.file, N_("<built-in>")))
311 return build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce);
312
313 if (context->show_column)
314 return build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
315 s.column, locus_ce);
316 else
317 return build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
318 locus_ce);
319 }
320
321 /* Return a malloc'd string describing a location and the severity of the
322 diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for
323 freeing the memory. */
175 char * 324 char *
176 diagnostic_build_prefix (diagnostic_context *context, 325 diagnostic_build_prefix (diagnostic_context *context,
177 diagnostic_info *diagnostic) 326 const diagnostic_info *diagnostic)
178 { 327 {
179 static const char *const diagnostic_kind_text[] = { 328 static const char *const diagnostic_kind_text[] = {
180 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T), 329 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
181 #include "diagnostic.def" 330 #include "diagnostic.def"
182 #undef DEFINE_DIAGNOSTIC_KIND 331 #undef DEFINE_DIAGNOSTIC_KIND
183 "must-not-happen" 332 "must-not-happen"
184 }; 333 };
334 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
335
185 const char *text = _(diagnostic_kind_text[diagnostic->kind]); 336 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
186 expanded_location s = expand_location (diagnostic->location); 337 const char *text_cs = "", *text_ce = "";
187 if (diagnostic->override_column) 338 pretty_printer *pp = context->printer;
188 s.column = diagnostic->override_column; 339
189 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND); 340 if (diagnostic_kind_color[diagnostic->kind])
190 341 {
191 return 342 text_cs = colorize_start (pp_show_color (pp),
192 (s.file == NULL 343 diagnostic_kind_color[diagnostic->kind]);
193 ? build_message_string ("%s: %s", progname, text) 344 text_ce = colorize_stop (pp_show_color (pp));
194 : context->show_column 345 }
195 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text) 346
196 : build_message_string ("%s:%d: %s", s.file, s.line, text)); 347 expanded_location s = diagnostic_expand_location (diagnostic);
348 char *location_text = diagnostic_get_location_text (context, s);
349
350 char *result = build_message_string ("%s %s%s%s", location_text,
351 text_cs, text, text_ce);
352 free (location_text);
353 return result;
354 }
355
356 /* Functions at which to stop the backtrace print. It's not
357 particularly helpful to print the callers of these functions. */
358
359 static const char * const bt_stop[] =
360 {
361 "main",
362 "toplev::main",
363 "execute_one_pass",
364 "compile_file",
365 };
366
367 /* A callback function passed to the backtrace_full function. */
368
369 static int
370 bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
371 const char *function)
372 {
373 int *pcount = (int *) data;
374
375 /* If we don't have any useful information, don't print
376 anything. */
377 if (filename == NULL && function == NULL)
378 return 0;
379
380 /* Skip functions in diagnostic.c. */
381 if (*pcount == 0
382 && filename != NULL
383 && strcmp (lbasename (filename), "diagnostic.c") == 0)
384 return 0;
385
386 /* Print up to 20 functions. We could make this a --param, but
387 since this is only for debugging just use a constant for now. */
388 if (*pcount >= 20)
389 {
390 /* Returning a non-zero value stops the backtrace. */
391 return 1;
392 }
393 ++*pcount;
394
395 char *alc = NULL;
396 if (function != NULL)
397 {
398 char *str = cplus_demangle_v3 (function,
399 (DMGL_VERBOSE | DMGL_ANSI
400 | DMGL_GNU_V3 | DMGL_PARAMS));
401 if (str != NULL)
402 {
403 alc = str;
404 function = str;
405 }
406
407 for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
408 {
409 size_t len = strlen (bt_stop[i]);
410 if (strncmp (function, bt_stop[i], len) == 0
411 && (function[len] == '\0' || function[len] == '('))
412 {
413 if (alc != NULL)
414 free (alc);
415 /* Returning a non-zero value stops the backtrace. */
416 return 1;
417 }
418 }
419 }
420
421 fprintf (stderr, "0x%lx %s\n\t%s:%d\n",
422 (unsigned long) pc,
423 function == NULL ? "???" : function,
424 filename == NULL ? "???" : filename,
425 lineno);
426
427 if (alc != NULL)
428 free (alc);
429
430 return 0;
431 }
432
433 /* A callback function passed to the backtrace_full function. This is
434 called if backtrace_full has an error. */
435
436 static void
437 bt_err_callback (void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
438 {
439 if (errnum < 0)
440 {
441 /* This means that no debug info was available. Just quietly
442 skip printing backtrace info. */
443 return;
444 }
445 fprintf (stderr, "%s%s%s\n", msg, errnum == 0 ? "" : ": ",
446 errnum == 0 ? "" : xstrerror (errnum));
447 }
448
449 /* Check if we've met the maximum error limit, and if so fatally exit
450 with a message. CONTEXT is the context to check, and FLUSH
451 indicates whether a diagnostic_finish call is needed. */
452
453 void
454 diagnostic_check_max_errors (diagnostic_context *context, bool flush)
455 {
456 if (!context->max_errors)
457 return;
458
459 int count = (diagnostic_kind_count (context, DK_ERROR)
460 + diagnostic_kind_count (context, DK_SORRY)
461 + diagnostic_kind_count (context, DK_WERROR));
462
463 if (count >= context->max_errors)
464 {
465 fnotice (stderr,
466 "compilation terminated due to -fmax-errors=%u.\n",
467 context->max_errors);
468 if (flush)
469 diagnostic_finish (context);
470 exit (FATAL_EXIT_CODE);
471 }
197 } 472 }
198 473
199 /* Take any action which is expected to happen after the diagnostic 474 /* Take any action which is expected to happen after the diagnostic
200 is written out. This function does not always return. */ 475 is written out. This function does not always return. */
201 static void 476 void
202 diagnostic_action_after_output (diagnostic_context *context, 477 diagnostic_action_after_output (diagnostic_context *context,
203 diagnostic_info *diagnostic) 478 diagnostic_t diag_kind)
204 { 479 {
205 switch (diagnostic->kind) 480 switch (diag_kind)
206 { 481 {
207 case DK_DEBUG: 482 case DK_DEBUG:
208 case DK_NOTE: 483 case DK_NOTE:
209 case DK_ANACHRONISM: 484 case DK_ANACHRONISM:
210 case DK_WARNING: 485 case DK_WARNING:
218 { 493 {
219 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n"); 494 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
220 diagnostic_finish (context); 495 diagnostic_finish (context);
221 exit (FATAL_EXIT_CODE); 496 exit (FATAL_EXIT_CODE);
222 } 497 }
223 if (context->max_errors != 0 498 break;
224 && ((unsigned) (diagnostic_kind_count (context, DK_ERROR) 499
225 + diagnostic_kind_count (context, DK_SORRY)) 500 case DK_ICE:
226 >= context->max_errors)) 501 case DK_ICE_NOBT:
227 { 502 {
503 struct backtrace_state *state = NULL;
504 if (diag_kind == DK_ICE)
505 state = backtrace_create_state (NULL, 0, bt_err_callback, NULL);
506 int count = 0;
507 if (state != NULL)
508 backtrace_full (state, 2, bt_callback, bt_err_callback,
509 (void *) &count);
510
511 if (context->abort_on_error)
512 real_abort ();
513
514 fnotice (stderr, "Please submit a full bug report,\n"
515 "with preprocessed source if appropriate.\n");
516 if (count > 0)
228 fnotice (stderr, 517 fnotice (stderr,
229 "compilation terminated due to -fmax-errors=%u.\n", 518 ("Please include the complete backtrace "
230 context->max_errors); 519 "with any bug report.\n"));
231 diagnostic_finish (context); 520 fnotice (stderr, "See %s for instructions.\n", bug_report_url);
232 exit (FATAL_EXIT_CODE); 521
233 } 522 exit (ICE_EXIT_CODE);
234 break; 523 }
235
236 case DK_ICE:
237 if (context->abort_on_error)
238 real_abort ();
239
240 fnotice (stderr, "Please submit a full bug report,\n"
241 "with preprocessed source if appropriate.\n"
242 "See %s for instructions.\n", bug_report_url);
243 exit (ICE_EXIT_CODE);
244 524
245 case DK_FATAL: 525 case DK_FATAL:
246 if (context->abort_on_error) 526 if (context->abort_on_error)
247 real_abort (); 527 real_abort ();
248 diagnostic_finish (context); 528 diagnostic_finish (context);
252 default: 532 default:
253 gcc_unreachable (); 533 gcc_unreachable ();
254 } 534 }
255 } 535 }
256 536
257 void 537 /* True if the last module or file in which a diagnostic was reported is
258 diagnostic_report_current_module (diagnostic_context *context) 538 different from the current one. */
259 { 539
260 const struct line_map *map; 540 static bool
541 last_module_changed_p (diagnostic_context *context,
542 const line_map_ordinary *map)
543 {
544 return context->last_module != map;
545 }
546
547 /* Remember the current module or file as being the last one in which we
548 report a diagnostic. */
549
550 static void
551 set_last_module (diagnostic_context *context, const line_map_ordinary *map)
552 {
553 context->last_module = map;
554 }
555
556 void
557 diagnostic_report_current_module (diagnostic_context *context, location_t where)
558 {
559 const line_map_ordinary *map = NULL;
261 560
262 if (pp_needs_newline (context->printer)) 561 if (pp_needs_newline (context->printer))
263 { 562 {
264 pp_newline (context->printer); 563 pp_newline (context->printer);
265 pp_needs_newline (context->printer) = false; 564 pp_needs_newline (context->printer) = false;
266 } 565 }
267 566
268 if (input_location <= BUILTINS_LOCATION) 567 if (where <= BUILTINS_LOCATION)
269 return; 568 return;
270 569
271 map = linemap_lookup (line_table, input_location); 570 linemap_resolve_location (line_table, where,
272 if (map && diagnostic_last_module_changed (context, map)) 571 LRK_MACRO_DEFINITION_LOCATION,
273 { 572 &map);
274 diagnostic_set_last_module (context, map); 573
574 if (map && last_module_changed_p (context, map))
575 {
576 set_last_module (context, map);
275 if (! MAIN_FILE_P (map)) 577 if (! MAIN_FILE_P (map))
276 { 578 {
277 map = INCLUDED_FROM (line_table, map); 579 map = INCLUDED_FROM (line_table, map);
278 if (context->show_column) 580 if (context->show_column)
279 pp_verbatim (context->printer, 581 pp_verbatim (context->printer,
280 "In file included from %s:%d:%d", 582 "In file included from %r%s:%d:%d%R", "locus",
281 map->to_file, 583 LINEMAP_FILE (map),
282 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map)); 584 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
283 else 585 else
284 pp_verbatim (context->printer, 586 pp_verbatim (context->printer,
285 "In file included from %s:%d", 587 "In file included from %r%s:%d%R", "locus",
286 map->to_file, LAST_SOURCE_LINE (map)); 588 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
287 while (! MAIN_FILE_P (map)) 589 while (! MAIN_FILE_P (map))
288 { 590 {
289 map = INCLUDED_FROM (line_table, map); 591 map = INCLUDED_FROM (line_table, map);
290 pp_verbatim (context->printer, 592 pp_verbatim (context->printer,
291 ",\n from %s:%d", 593 ",\n from %r%s:%d%R", "locus",
292 map->to_file, LAST_SOURCE_LINE (map)); 594 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
293 } 595 }
294 pp_verbatim (context->printer, ":"); 596 pp_verbatim (context->printer, ":");
295 pp_newline (context->printer); 597 pp_newline (context->printer);
296 } 598 }
297 } 599 }
299 601
300 void 602 void
301 default_diagnostic_starter (diagnostic_context *context, 603 default_diagnostic_starter (diagnostic_context *context,
302 diagnostic_info *diagnostic) 604 diagnostic_info *diagnostic)
303 { 605 {
304 diagnostic_report_current_module (context); 606 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
305 pp_set_prefix (context->printer, diagnostic_build_prefix (context, 607 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
306 diagnostic)); 608 diagnostic));
307 } 609 }
308 610
309 void 611 void
612 default_diagnostic_start_span_fn (diagnostic_context *context,
613 expanded_location exploc)
614 {
615 pp_set_prefix (context->printer,
616 diagnostic_get_location_text (context, exploc));
617 pp_string (context->printer, "");
618 pp_newline (context->printer);
619 }
620
621 void
310 default_diagnostic_finalizer (diagnostic_context *context, 622 default_diagnostic_finalizer (diagnostic_context *context,
311 diagnostic_info *diagnostic ATTRIBUTE_UNUSED) 623 diagnostic_info *diagnostic)
312 { 624 {
625 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
313 pp_destroy_prefix (context->printer); 626 pp_destroy_prefix (context->printer);
627 pp_flush (context->printer);
314 } 628 }
315 629
316 /* Interface to specify diagnostic kind overrides. Returns the 630 /* Interface to specify diagnostic kind overrides. Returns the
317 previous setting, or DK_UNSPECIFIED if the parameters are out of 631 previous setting, or DK_UNSPECIFIED if the parameters are out of
318 range. */ 632 range. If OPTION_INDEX is zero, the new setting is for all the
633 diagnostics. */
319 diagnostic_t 634 diagnostic_t
320 diagnostic_classify_diagnostic (diagnostic_context *context, 635 diagnostic_classify_diagnostic (diagnostic_context *context,
321 int option_index, 636 int option_index,
322 diagnostic_t new_kind, 637 diagnostic_t new_kind,
323 location_t where) 638 location_t where)
324 { 639 {
325 diagnostic_t old_kind; 640 diagnostic_t old_kind;
326 641
327 if (option_index <= 0 642 if (option_index < 0
328 || option_index >= context->n_opts 643 || option_index >= context->n_opts
329 || new_kind >= DK_LAST_DIAGNOSTIC_KIND) 644 || new_kind >= DK_LAST_DIAGNOSTIC_KIND)
330 return DK_UNSPECIFIED; 645 return DK_UNSPECIFIED;
331 646
332 old_kind = context->classify_diagnostic[option_index]; 647 old_kind = context->classify_diagnostic[option_index];
334 /* Handle pragmas separately, since we need to keep track of *where* 649 /* Handle pragmas separately, since we need to keep track of *where*
335 the pragmas were. */ 650 the pragmas were. */
336 if (where != UNKNOWN_LOCATION) 651 if (where != UNKNOWN_LOCATION)
337 { 652 {
338 int i; 653 int i;
654
655 /* Record the command-line status, so we can reset it back on DK_POP. */
656 if (old_kind == DK_UNSPECIFIED)
657 {
658 old_kind = !context->option_enabled (option_index,
659 context->option_state)
660 ? DK_IGNORED : (context->warning_as_error_requested
661 ? DK_ERROR : DK_WARNING);
662 context->classify_diagnostic[option_index] = old_kind;
663 }
339 664
340 for (i = context->n_classification_history - 1; i >= 0; i --) 665 for (i = context->n_classification_history - 1; i >= 0; i --)
341 if (context->classification_history[i].option == option_index) 666 if (context->classification_history[i].option == option_index)
342 { 667 {
343 old_kind = context->classification_history[i].kind; 668 old_kind = context->classification_history[i].kind;
388 context->classification_history[i].option = jump_to; 713 context->classification_history[i].option = jump_to;
389 context->classification_history[i].kind = DK_POP; 714 context->classification_history[i].kind = DK_POP;
390 context->n_classification_history ++; 715 context->n_classification_history ++;
391 } 716 }
392 717
718 /* Helper function for print_parseable_fixits. Print TEXT to PP, obeying the
719 escaping rules for -fdiagnostics-parseable-fixits. */
720
721 static void
722 print_escaped_string (pretty_printer *pp, const char *text)
723 {
724 gcc_assert (pp);
725 gcc_assert (text);
726
727 pp_character (pp, '"');
728 for (const char *ch = text; *ch; ch++)
729 {
730 switch (*ch)
731 {
732 case '\\':
733 /* Escape backslash as two backslashes. */
734 pp_string (pp, "\\\\");
735 break;
736 case '\t':
737 /* Escape tab as "\t". */
738 pp_string (pp, "\\t");
739 break;
740 case '\n':
741 /* Escape newline as "\n". */
742 pp_string (pp, "\\n");
743 break;
744 case '"':
745 /* Escape doublequotes as \". */
746 pp_string (pp, "\\\"");
747 break;
748 default:
749 if (ISPRINT (*ch))
750 pp_character (pp, *ch);
751 else
752 /* Use octal for non-printable chars. */
753 {
754 unsigned char c = (*ch & 0xff);
755 pp_printf (pp, "\\%o%o%o", (c / 64), (c / 8) & 007, c & 007);
756 }
757 break;
758 }
759 }
760 pp_character (pp, '"');
761 }
762
763 /* Implementation of -fdiagnostics-parseable-fixits. Print a
764 machine-parseable version of all fixits in RICHLOC to PP. */
765
766 static void
767 print_parseable_fixits (pretty_printer *pp, rich_location *richloc)
768 {
769 gcc_assert (pp);
770 gcc_assert (richloc);
771
772 for (unsigned i = 0; i < richloc->get_num_fixit_hints (); i++)
773 {
774 const fixit_hint *hint = richloc->get_fixit_hint (i);
775 source_location start_loc = hint->get_start_loc ();
776 expanded_location start_exploc = expand_location (start_loc);
777 pp_string (pp, "fix-it:");
778 print_escaped_string (pp, start_exploc.file);
779 /* For compatibility with clang, print as a half-open range. */
780 source_location next_loc = hint->get_next_loc ();
781 expanded_location next_exploc = expand_location (next_loc);
782 pp_printf (pp, ":{%i:%i-%i:%i}:",
783 start_exploc.line, start_exploc.column,
784 next_exploc.line, next_exploc.column);
785 print_escaped_string (pp, hint->get_string ());
786 pp_newline (pp);
787 }
788 }
789
790 /* Update the diag_class of DIAGNOSTIC based on its location
791 relative to any
792 #pragma GCC diagnostic
793 directives recorded within CONTEXT.
794
795 Return the new diag_class of DIAGNOSTIC if it was updated, or
796 DK_UNSPECIFIED otherwise. */
797
798 static diagnostic_t
799 update_effective_level_from_pragmas (diagnostic_context *context,
800 diagnostic_info *diagnostic)
801 {
802 diagnostic_t diag_class = DK_UNSPECIFIED;
803
804 if (context->n_classification_history > 0)
805 {
806 location_t location = diagnostic_location (diagnostic);
807
808 /* FIXME: Stupid search. Optimize later. */
809 for (int i = context->n_classification_history - 1; i >= 0; i --)
810 {
811 if (linemap_location_before_p
812 (line_table,
813 context->classification_history[i].location,
814 location))
815 {
816 if (context->classification_history[i].kind == (int) DK_POP)
817 {
818 i = context->classification_history[i].option;
819 continue;
820 }
821 int option = context->classification_history[i].option;
822 /* The option 0 is for all the diagnostics. */
823 if (option == 0 || option == diagnostic->option_index)
824 {
825 diag_class = context->classification_history[i].kind;
826 if (diag_class != DK_UNSPECIFIED)
827 diagnostic->kind = diag_class;
828 break;
829 }
830 }
831 }
832 }
833
834 return diag_class;
835 }
836
837 /* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's
838 printer, e.g. " [-Werror=uninitialized]".
839 Subroutine of diagnostic_report_diagnostic. */
840
841 static void
842 print_option_information (diagnostic_context *context,
843 const diagnostic_info *diagnostic,
844 diagnostic_t orig_diag_kind)
845 {
846 char *option_text;
847
848 option_text = context->option_name (context, diagnostic->option_index,
849 orig_diag_kind, diagnostic->kind);
850
851 if (option_text)
852 {
853 pretty_printer *pp = context->printer;
854 pp_string (pp, " [");
855 pp_string (pp, colorize_start (pp_show_color (pp),
856 diagnostic_kind_color[diagnostic->kind]));
857 pp_string (pp, option_text);
858 pp_string (pp, colorize_stop (pp_show_color (pp)));
859 pp_character (pp, ']');
860 free (option_text);
861 }
862 }
863
393 /* Report a diagnostic message (an error or a warning) as specified by 864 /* Report a diagnostic message (an error or a warning) as specified by
394 DC. This function is *the* subroutine in terms of which front-ends 865 DC. This function is *the* subroutine in terms of which front-ends
395 should implement their specific diagnostic handling modules. The 866 should implement their specific diagnostic handling modules. The
396 front-end independent format specifiers are exactly those described 867 front-end independent format specifiers are exactly those described
397 in the documentation of output_format. 868 in the documentation of output_format.
399 870
400 bool 871 bool
401 diagnostic_report_diagnostic (diagnostic_context *context, 872 diagnostic_report_diagnostic (diagnostic_context *context,
402 diagnostic_info *diagnostic) 873 diagnostic_info *diagnostic)
403 { 874 {
404 location_t location = diagnostic->location; 875 location_t location = diagnostic_location (diagnostic);
405 diagnostic_t orig_diag_kind = diagnostic->kind; 876 diagnostic_t orig_diag_kind = diagnostic->kind;
406 const char *saved_format_spec;
407 877
408 /* Give preference to being able to inhibit warnings, before they 878 /* Give preference to being able to inhibit warnings, before they
409 get reclassified to something else. */ 879 get reclassified to something else. */
410 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN) 880 if ((diagnostic->kind == DK_WARNING || diagnostic->kind == DK_PEDWARN)
411 && !diagnostic_report_warnings_p (context, location)) 881 && !diagnostic_report_warnings_p (context, location))
424 if (context->lock > 0) 894 if (context->lock > 0)
425 { 895 {
426 /* If we're reporting an ICE in the middle of some other error, 896 /* If we're reporting an ICE in the middle of some other error,
427 try to flush out the previous error, then let this one 897 try to flush out the previous error, then let this one
428 through. Don't do this more than once. */ 898 through. Don't do this more than once. */
429 if (diagnostic->kind == DK_ICE && context->lock == 1) 899 if ((diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
430 pp_flush (context->printer); 900 && context->lock == 1)
901 pp_newline_and_flush (context->printer);
431 else 902 else
432 error_recursion (context); 903 error_recursion (context);
433 } 904 }
434 905
435 /* If the user requested that warnings be treated as errors, so be 906 /* If the user requested that warnings be treated as errors, so be
436 it. Note that we do this before the next block so that 907 it. Note that we do this before the next block so that
437 individual warnings can be overridden back to warnings with 908 individual warnings can be overridden back to warnings with
438 -Wno-error=*. */ 909 -Wno-error=*. */
439 if (context->warning_as_error_requested 910 if (context->warning_as_error_requested
440 && diagnostic->kind == DK_WARNING) 911 && diagnostic->kind == DK_WARNING)
441 { 912 diagnostic->kind = DK_ERROR;
442 diagnostic->kind = DK_ERROR; 913
443 } 914 if (diagnostic->option_index
444 915 && diagnostic->option_index != permissive_error_option (context))
445 if (diagnostic->option_index) 916 {
446 {
447 diagnostic_t diag_class = DK_UNSPECIFIED;
448
449 /* This tests if the user provided the appropriate -Wfoo or 917 /* This tests if the user provided the appropriate -Wfoo or
450 -Wno-foo option. */ 918 -Wno-foo option. */
451 if (! context->option_enabled (diagnostic->option_index, 919 if (! context->option_enabled (diagnostic->option_index,
452 context->option_state)) 920 context->option_state))
453 return false; 921 return false;
454 922
455 /* This tests for #pragma diagnostic changes. */ 923 /* This tests for #pragma diagnostic changes. */
456 if (context->n_classification_history > 0) 924 diagnostic_t diag_class
457 { 925 = update_effective_level_from_pragmas (context, diagnostic);
458 int i; 926
459 /* FIXME: Stupid search. Optimize later. */
460 for (i = context->n_classification_history - 1; i >= 0; i --)
461 {
462 if (context->classification_history[i].location <= location)
463 {
464 if (context->classification_history[i].kind == (int) DK_POP)
465 {
466 i = context->classification_history[i].option;
467 continue;
468 }
469 if (context->classification_history[i].option == diagnostic->option_index)
470 {
471 diag_class = context->classification_history[i].kind;
472 if (diag_class != DK_UNSPECIFIED)
473 diagnostic->kind = diag_class;
474 break;
475 }
476 }
477 }
478 }
479 /* This tests if the user provided the appropriate -Werror=foo 927 /* This tests if the user provided the appropriate -Werror=foo
480 option. */ 928 option. */
481 if (diag_class == DK_UNSPECIFIED 929 if (diag_class == DK_UNSPECIFIED
482 && context->classify_diagnostic[diagnostic->option_index] != DK_UNSPECIFIED) 930 && (context->classify_diagnostic[diagnostic->option_index]
483 { 931 != DK_UNSPECIFIED))
484 diagnostic->kind = context->classify_diagnostic[diagnostic->option_index]; 932 diagnostic->kind
485 } 933 = context->classify_diagnostic[diagnostic->option_index];
934
486 /* This allows for future extensions, like temporarily disabling 935 /* This allows for future extensions, like temporarily disabling
487 warnings for ranges of source code. */ 936 warnings for ranges of source code. */
488 if (diagnostic->kind == DK_IGNORED) 937 if (diagnostic->kind == DK_IGNORED)
489 return false; 938 return false;
490 } 939 }
491 940
492 if (orig_diag_kind == DK_WARNING && diagnostic->kind == DK_ERROR) 941 if (diagnostic->kind != DK_NOTE)
493 context->some_warnings_are_errors = true; 942 diagnostic_check_max_errors (context);
494 943
495 context->lock++; 944 context->lock++;
496 945
497 if (diagnostic->kind == DK_ICE) 946 if (diagnostic->kind == DK_ICE || diagnostic->kind == DK_ICE_NOBT)
498 { 947 {
499 #ifndef ENABLE_CHECKING
500 /* When not checking, ICEs are converted to fatal errors when an 948 /* When not checking, ICEs are converted to fatal errors when an
501 error has already occurred. This is counteracted by 949 error has already occurred. This is counteracted by
502 abort_on_error. */ 950 abort_on_error. */
503 if ((diagnostic_kind_count (context, DK_ERROR) > 0 951 if (!CHECKING_P
504 || diagnostic_kind_count (context, DK_SORRY) > 0) 952 && (diagnostic_kind_count (context, DK_ERROR) > 0
953 || diagnostic_kind_count (context, DK_SORRY) > 0)
505 && !context->abort_on_error) 954 && !context->abort_on_error)
506 { 955 {
507 expanded_location s = expand_location (diagnostic->location); 956 expanded_location s
957 = expand_location (diagnostic_location (diagnostic));
508 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n", 958 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
509 s.file, s.line); 959 s.file, s.line);
510 exit (ICE_EXIT_CODE); 960 exit (ICE_EXIT_CODE);
511 } 961 }
512 #endif
513 if (context->internal_error) 962 if (context->internal_error)
514 (*context->internal_error) (context, 963 (*context->internal_error) (context,
515 diagnostic->message.format_spec, 964 diagnostic->message.format_spec,
516 diagnostic->message.args_ptr); 965 diagnostic->message.args_ptr);
517 } 966 }
518 ++diagnostic_kind_count (context, diagnostic->kind); 967 if (diagnostic->kind == DK_ERROR && orig_diag_kind == DK_WARNING)
519 968 ++diagnostic_kind_count (context, DK_WERROR);
520 saved_format_spec = diagnostic->message.format_spec; 969 else
521 if (context->show_option_requested) 970 ++diagnostic_kind_count (context, diagnostic->kind);
522 { 971
523 char *option_text;
524
525 option_text = context->option_name (context, diagnostic->option_index,
526 orig_diag_kind, diagnostic->kind);
527
528 if (option_text)
529 {
530 diagnostic->message.format_spec
531 = ACONCAT ((diagnostic->message.format_spec,
532 " ",
533 "[", option_text, "]",
534 NULL));
535 free (option_text);
536 }
537 }
538 diagnostic->message.locus = &diagnostic->location;
539 diagnostic->message.x_data = &diagnostic->x_data; 972 diagnostic->message.x_data = &diagnostic->x_data;
540 diagnostic->x_data = NULL; 973 diagnostic->x_data = NULL;
541 pp_format (context->printer, &diagnostic->message); 974 pp_format (context->printer, &diagnostic->message);
542 (*diagnostic_starter (context)) (context, diagnostic); 975 (*diagnostic_starter (context)) (context, diagnostic);
543 pp_output_formatted_text (context->printer); 976 pp_output_formatted_text (context->printer);
977 if (context->show_option_requested)
978 print_option_information (context, diagnostic, orig_diag_kind);
544 (*diagnostic_finalizer (context)) (context, diagnostic); 979 (*diagnostic_finalizer (context)) (context, diagnostic);
545 pp_flush (context->printer); 980 if (context->parseable_fixits_p)
546 diagnostic_action_after_output (context, diagnostic); 981 {
547 diagnostic->message.format_spec = saved_format_spec; 982 print_parseable_fixits (context->printer, diagnostic->richloc);
983 pp_flush (context->printer);
984 }
985 diagnostic_action_after_output (context, diagnostic->kind);
548 diagnostic->x_data = NULL; 986 diagnostic->x_data = NULL;
987
988 if (context->edit_context_ptr)
989 if (diagnostic->richloc->fixits_can_be_auto_applied_p ())
990 context->edit_context_ptr->add_fixits (diagnostic->richloc);
549 991
550 context->lock--; 992 context->lock--;
551 993
552 return true; 994 return true;
553 } 995 }
595 1037
596 va_start (ap, gmsgid); 1038 va_start (ap, gmsgid);
597 text.err_no = errno; 1039 text.err_no = errno;
598 text.args_ptr = &ap; 1040 text.args_ptr = &ap;
599 text.format_spec = _(gmsgid); 1041 text.format_spec = _(gmsgid);
600 text.locus = NULL;
601 text.x_data = NULL; 1042 text.x_data = NULL;
602 pp_format_verbatim (global_dc->printer, &text); 1043 pp_format_verbatim (global_dc->printer, &text);
603 pp_flush (global_dc->printer); 1044 pp_newline_and_flush (global_dc->printer);
604 va_end (ap); 1045 va_end (ap);
605 } 1046 }
1047
1048 /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */
1049 void
1050 diagnostic_append_note (diagnostic_context *context,
1051 location_t location,
1052 const char * gmsgid, ...)
1053 {
1054 diagnostic_info diagnostic;
1055 va_list ap;
1056 const char *saved_prefix;
1057 rich_location richloc (line_table, location);
1058
1059 va_start (ap, gmsgid);
1060 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_NOTE);
1061 if (context->inhibit_notes_p)
1062 {
1063 va_end (ap);
1064 return;
1065 }
1066 saved_prefix = pp_get_prefix (context->printer);
1067 pp_set_prefix (context->printer,
1068 diagnostic_build_prefix (context, &diagnostic));
1069 pp_format (context->printer, &diagnostic.message);
1070 pp_output_formatted_text (context->printer);
1071 pp_destroy_prefix (context->printer);
1072 pp_set_prefix (context->printer, saved_prefix);
1073 diagnostic_show_locus (context, &richloc, DK_NOTE);
1074 va_end (ap);
1075 }
1076
1077 /* Implement emit_diagnostic, inform, inform_at_rich_loc, warning, warning_at,
1078 warning_at_rich_loc, pedwarn, permerror, permerror_at_rich_loc, error,
1079 error_at, error_at_rich_loc, sorry, fatal_error, internal_error, and
1080 internal_error_no_backtrace, as documented and defined below. */
1081 static bool
1082 diagnostic_impl (rich_location *richloc, int opt,
1083 const char *gmsgid,
1084 va_list *ap, diagnostic_t kind)
1085 {
1086 diagnostic_info diagnostic;
1087 if (kind == DK_PERMERROR)
1088 {
1089 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc,
1090 permissive_error_kind (global_dc));
1091 diagnostic.option_index = permissive_error_option (global_dc);
1092 }
1093 else
1094 {
1095 diagnostic_set_info (&diagnostic, gmsgid, ap, richloc, kind);
1096 if (kind == DK_WARNING || kind == DK_PEDWARN)
1097 diagnostic.option_index = opt;
1098 }
1099 return diagnostic_report_diagnostic (global_dc, &diagnostic);
1100 }
1101
1102 /* Same as diagonostic_n_impl taking rich_location instead of location_t. */
1103 static bool
1104 diagnostic_n_impl_richloc (rich_location *richloc, int opt, int n,
1105 const char *singular_gmsgid,
1106 const char *plural_gmsgid,
1107 va_list *ap, diagnostic_t kind)
1108 {
1109 diagnostic_info diagnostic;
1110 diagnostic_set_info_translated (&diagnostic,
1111 ngettext (singular_gmsgid, plural_gmsgid, n),
1112 ap, richloc, kind);
1113 if (kind == DK_WARNING)
1114 diagnostic.option_index = opt;
1115 return diagnostic_report_diagnostic (global_dc, &diagnostic);
1116 }
1117
1118 /* Implement inform_n, warning_n, and error_n, as documented and
1119 defined below. */
1120 static bool
1121 diagnostic_n_impl (location_t location, int opt, int n,
1122 const char *singular_gmsgid,
1123 const char *plural_gmsgid,
1124 va_list *ap, diagnostic_t kind)
1125 {
1126 rich_location richloc (line_table, location);
1127 return diagnostic_n_impl_richloc (&richloc, opt, n,
1128 singular_gmsgid, plural_gmsgid, ap, kind);
1129 }
1130
1131 /* Wrapper around diagnostic_impl taking a variable argument list. */
606 1132
607 bool 1133 bool
608 emit_diagnostic (diagnostic_t kind, location_t location, int opt, 1134 emit_diagnostic (diagnostic_t kind, location_t location, int opt,
609 const char *gmsgid, ...) 1135 const char *gmsgid, ...)
610 { 1136 {
611 diagnostic_info diagnostic; 1137 va_list ap;
612 va_list ap; 1138 va_start (ap, gmsgid);
613 1139 rich_location richloc (line_table, location);
614 va_start (ap, gmsgid); 1140 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, kind);
615 if (kind == DK_PERMERROR) 1141 va_end (ap);
616 { 1142 return ret;
617 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, 1143 }
618 permissive_error_kind (global_dc)); 1144
619 diagnostic.option_index = permissive_error_option (global_dc); 1145 /* Wrapper around diagnostic_impl taking a va_list parameter. */
620 } 1146
621 else { 1147 bool
622 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, kind); 1148 emit_diagnostic_valist (diagnostic_t kind, location_t location, int opt,
623 if (kind == DK_WARNING || kind == DK_PEDWARN) 1149 const char *gmsgid, va_list *ap)
624 diagnostic.option_index = opt; 1150 {
625 } 1151 rich_location richloc (line_table, location);
626 va_end (ap); 1152 return diagnostic_impl (&richloc, opt, gmsgid, ap, kind);
627
628 return report_diagnostic (&diagnostic);
629 } 1153 }
630 1154
631 /* An informative note at LOCATION. Use this for additional details on an error 1155 /* An informative note at LOCATION. Use this for additional details on an error
632 message. */ 1156 message. */
633 void 1157 void
634 inform (location_t location, const char *gmsgid, ...) 1158 inform (location_t location, const char *gmsgid, ...)
635 { 1159 {
636 diagnostic_info diagnostic; 1160 va_list ap;
637 va_list ap; 1161 va_start (ap, gmsgid);
638 1162 rich_location richloc (line_table, location);
639 va_start (ap, gmsgid); 1163 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_NOTE);
640 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_NOTE); 1164 va_end (ap);
641 report_diagnostic (&diagnostic); 1165 }
1166
1167 /* Same as "inform", but at RICHLOC. */
1168 void
1169 inform_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1170 {
1171 va_list ap;
1172 va_start (ap, gmsgid);
1173 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_NOTE);
642 va_end (ap); 1174 va_end (ap);
643 } 1175 }
644 1176
645 /* An informative note at LOCATION. Use this for additional details on an 1177 /* An informative note at LOCATION. Use this for additional details on an
646 error message. */ 1178 error message. */
647 void 1179 void
648 inform_n (location_t location, int n, const char *singular_gmsgid, 1180 inform_n (location_t location, int n, const char *singular_gmsgid,
649 const char *plural_gmsgid, ...) 1181 const char *plural_gmsgid, ...)
650 { 1182 {
651 diagnostic_info diagnostic; 1183 va_list ap;
652 va_list ap;
653
654 va_start (ap, plural_gmsgid); 1184 va_start (ap, plural_gmsgid);
655 diagnostic_set_info_translated (&diagnostic, 1185 diagnostic_n_impl (location, -1, n, singular_gmsgid, plural_gmsgid,
656 ngettext (singular_gmsgid, plural_gmsgid, n), 1186 &ap, DK_NOTE);
657 &ap, location, DK_NOTE);
658 report_diagnostic (&diagnostic);
659 va_end (ap); 1187 va_end (ap);
660 } 1188 }
661 1189
662 /* A warning at INPUT_LOCATION. Use this for code which is correct according 1190 /* A warning at INPUT_LOCATION. Use this for code which is correct according
663 to the relevant language specification but is likely to be buggy anyway. 1191 to the relevant language specification but is likely to be buggy anyway.
664 Returns true if the warning was printed, false if it was inhibited. */ 1192 Returns true if the warning was printed, false if it was inhibited. */
665 bool 1193 bool
666 warning (int opt, const char *gmsgid, ...) 1194 warning (int opt, const char *gmsgid, ...)
667 { 1195 {
668 diagnostic_info diagnostic; 1196 va_list ap;
669 va_list ap; 1197 va_start (ap, gmsgid);
670 1198 rich_location richloc (line_table, input_location);
671 va_start (ap, gmsgid); 1199 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
672 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING); 1200 va_end (ap);
673 diagnostic.option_index = opt; 1201 return ret;
674
675 va_end (ap);
676 return report_diagnostic (&diagnostic);
677 } 1202 }
678 1203
679 /* A warning at LOCATION. Use this for code which is correct according to the 1204 /* A warning at LOCATION. Use this for code which is correct according to the
680 relevant language specification but is likely to be buggy anyway. 1205 relevant language specification but is likely to be buggy anyway.
681 Returns true if the warning was printed, false if it was inhibited. */ 1206 Returns true if the warning was printed, false if it was inhibited. */
682 1207
683 bool 1208 bool
684 warning_at (location_t location, int opt, const char *gmsgid, ...) 1209 warning_at (location_t location, int opt, const char *gmsgid, ...)
685 { 1210 {
686 diagnostic_info diagnostic; 1211 va_list ap;
687 va_list ap; 1212 va_start (ap, gmsgid);
688 1213 rich_location richloc (line_table, location);
689 va_start (ap, gmsgid); 1214 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING);
690 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_WARNING); 1215 va_end (ap);
691 diagnostic.option_index = opt; 1216 return ret;
692 va_end (ap); 1217 }
693 return report_diagnostic (&diagnostic); 1218
1219 /* Same as warning at, but using RICHLOC. */
1220
1221 bool
1222 warning_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1223 {
1224 va_list ap;
1225 va_start (ap, gmsgid);
1226 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_WARNING);
1227 va_end (ap);
1228 return ret;
1229 }
1230
1231 /* Same as warning_at_rich_loc but for plural variant. */
1232
1233 bool
1234 warning_at_rich_loc_n (rich_location *richloc, int opt, int n,
1235 const char *singular_gmsgid, const char *plural_gmsgid, ...)
1236 {
1237 va_list ap;
1238 va_start (ap, plural_gmsgid);
1239 bool ret = diagnostic_n_impl_richloc (richloc, opt, n,
1240 singular_gmsgid, plural_gmsgid,
1241 &ap, DK_WARNING);
1242 va_end (ap);
1243 return ret;
1244 }
1245
1246 /* A warning at LOCATION. Use this for code which is correct according to the
1247 relevant language specification but is likely to be buggy anyway.
1248 Returns true if the warning was printed, false if it was inhibited. */
1249
1250 bool
1251 warning_n (location_t location, int opt, int n, const char *singular_gmsgid,
1252 const char *plural_gmsgid, ...)
1253 {
1254 va_list ap;
1255 va_start (ap, plural_gmsgid);
1256 bool ret = diagnostic_n_impl (location, opt, n,
1257 singular_gmsgid, plural_gmsgid,
1258 &ap, DK_WARNING);
1259 va_end (ap);
1260 return ret;
694 } 1261 }
695 1262
696 /* A "pedantic" warning at LOCATION: issues a warning unless 1263 /* A "pedantic" warning at LOCATION: issues a warning unless
697 -pedantic-errors was given on the command line, in which case it 1264 -pedantic-errors was given on the command line, in which case it
698 issues an error. Use this for diagnostics required by the relevant 1265 issues an error. Use this for diagnostics required by the relevant
699 language standard, if you have chosen not to make them errors. 1266 language standard, if you have chosen not to make them errors.
700 1267
701 Note that these diagnostics are issued independent of the setting 1268 Note that these diagnostics are issued independent of the setting
702 of the -pedantic command-line switch. To get a warning enabled 1269 of the -Wpedantic command-line switch. To get a warning enabled
703 only with that switch, use either "if (pedantic) pedwarn 1270 only with that switch, use either "if (pedantic) pedwarn
704 (OPT_pedantic,...)" or just "pedwarn (OPT_pedantic,..)". To get a 1271 (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a
705 pedwarn independently of the -pedantic switch use "pedwarn (0,...)". 1272 pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)".
706 1273
707 Returns true if the warning was printed, false if it was inhibited. */ 1274 Returns true if the warning was printed, false if it was inhibited. */
708 1275
709 bool 1276 bool
710 pedwarn (location_t location, int opt, const char *gmsgid, ...) 1277 pedwarn (location_t location, int opt, const char *gmsgid, ...)
711 { 1278 {
712 diagnostic_info diagnostic; 1279 va_list ap;
713 va_list ap; 1280 va_start (ap, gmsgid);
714 1281 rich_location richloc (line_table, location);
715 va_start (ap, gmsgid); 1282 bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_PEDWARN);
716 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, DK_PEDWARN); 1283 va_end (ap);
717 diagnostic.option_index = opt; 1284 return ret;
718 va_end (ap); 1285 }
719 return report_diagnostic (&diagnostic); 1286
1287 /* Same as pedwarn, but using RICHLOC. */
1288
1289 bool
1290 pedwarn_at_rich_loc (rich_location *richloc, int opt, const char *gmsgid, ...)
1291 {
1292 va_list ap;
1293 va_start (ap, gmsgid);
1294 bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_PEDWARN);
1295 va_end (ap);
1296 return ret;
720 } 1297 }
721 1298
722 /* A "permissive" error at LOCATION: issues an error unless 1299 /* A "permissive" error at LOCATION: issues an error unless
723 -fpermissive was given on the command line, in which case it issues 1300 -fpermissive was given on the command line, in which case it issues
724 a warning. Use this for things that really should be errors but we 1301 a warning. Use this for things that really should be errors but we
727 Returns true if the warning was printed, false if it was inhibited. */ 1304 Returns true if the warning was printed, false if it was inhibited. */
728 1305
729 bool 1306 bool
730 permerror (location_t location, const char *gmsgid, ...) 1307 permerror (location_t location, const char *gmsgid, ...)
731 { 1308 {
732 diagnostic_info diagnostic; 1309 va_list ap;
733 va_list ap; 1310 va_start (ap, gmsgid);
734 1311 rich_location richloc (line_table, location);
735 va_start (ap, gmsgid); 1312 bool ret = diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_PERMERROR);
736 diagnostic_set_info (&diagnostic, gmsgid, &ap, location, 1313 va_end (ap);
737 permissive_error_kind (global_dc)); 1314 return ret;
738 diagnostic.option_index = permissive_error_option (global_dc); 1315 }
739 va_end (ap); 1316
740 return report_diagnostic (&diagnostic); 1317 /* Same as "permerror", but at RICHLOC. */
1318
1319 bool
1320 permerror_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1321 {
1322 va_list ap;
1323 va_start (ap, gmsgid);
1324 bool ret = diagnostic_impl (richloc, -1, gmsgid, &ap, DK_PERMERROR);
1325 va_end (ap);
1326 return ret;
741 } 1327 }
742 1328
743 /* A hard error: the code is definitely ill-formed, and an object file 1329 /* A hard error: the code is definitely ill-formed, and an object file
744 will not be produced. */ 1330 will not be produced. */
745 void 1331 void
746 error (const char *gmsgid, ...) 1332 error (const char *gmsgid, ...)
747 { 1333 {
748 diagnostic_info diagnostic; 1334 va_list ap;
749 va_list ap; 1335 va_start (ap, gmsgid);
750 1336 rich_location richloc (line_table, input_location);
751 va_start (ap, gmsgid); 1337 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
752 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
753 report_diagnostic (&diagnostic);
754 va_end (ap); 1338 va_end (ap);
755 } 1339 }
756 1340
757 /* A hard error: the code is definitely ill-formed, and an object file 1341 /* A hard error: the code is definitely ill-formed, and an object file
758 will not be produced. */ 1342 will not be produced. */
759 void 1343 void
760 error_n (location_t location, int n, const char *singular_gmsgid, 1344 error_n (location_t location, int n, const char *singular_gmsgid,
761 const char *plural_gmsgid, ...) 1345 const char *plural_gmsgid, ...)
762 { 1346 {
763 diagnostic_info diagnostic; 1347 va_list ap;
764 va_list ap;
765
766 va_start (ap, plural_gmsgid); 1348 va_start (ap, plural_gmsgid);
767 diagnostic_set_info_translated (&diagnostic, 1349 diagnostic_n_impl (location, -1, n, singular_gmsgid, plural_gmsgid,
768 ngettext (singular_gmsgid, plural_gmsgid, n), 1350 &ap, DK_ERROR);
769 &ap, location, DK_ERROR); 1351 va_end (ap);
770 report_diagnostic (&diagnostic); 1352 }
771 va_end (ap); 1353
772 } 1354 /* Same as above, but use location LOC instead of input_location. */
773
774 /* Same as ebove, but use location LOC instead of input_location. */
775 void 1355 void
776 error_at (location_t loc, const char *gmsgid, ...) 1356 error_at (location_t loc, const char *gmsgid, ...)
777 { 1357 {
778 diagnostic_info diagnostic; 1358 va_list ap;
779 va_list ap; 1359 va_start (ap, gmsgid);
780 1360 rich_location richloc (line_table, loc);
781 va_start (ap, gmsgid); 1361 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR);
782 diagnostic_set_info (&diagnostic, gmsgid, &ap, loc, DK_ERROR); 1362 va_end (ap);
783 report_diagnostic (&diagnostic); 1363 }
1364
1365 /* Same as above, but use RICH_LOC. */
1366
1367 void
1368 error_at_rich_loc (rich_location *richloc, const char *gmsgid, ...)
1369 {
1370 va_list ap;
1371 va_start (ap, gmsgid);
1372 diagnostic_impl (richloc, -1, gmsgid, &ap, DK_ERROR);
784 va_end (ap); 1373 va_end (ap);
785 } 1374 }
786 1375
787 /* "Sorry, not implemented." Use for a language feature which is 1376 /* "Sorry, not implemented." Use for a language feature which is
788 required by the relevant specification but not implemented by GCC. 1377 required by the relevant specification but not implemented by GCC.
789 An object file will not be produced. */ 1378 An object file will not be produced. */
790 void 1379 void
791 sorry (const char *gmsgid, ...) 1380 sorry (const char *gmsgid, ...)
792 { 1381 {
793 diagnostic_info diagnostic; 1382 va_list ap;
794 va_list ap; 1383 va_start (ap, gmsgid);
795 1384 rich_location richloc (line_table, input_location);
796 va_start (ap, gmsgid); 1385 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY);
797 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
798 report_diagnostic (&diagnostic);
799 va_end (ap); 1386 va_end (ap);
800 } 1387 }
801 1388
802 /* Return true if an error or a "sorry" has been seen. Various 1389 /* Return true if an error or a "sorry" has been seen. Various
803 processing is disabled after errors. */ 1390 processing is disabled after errors. */
809 1396
810 /* An error which is severe enough that we make no attempt to 1397 /* An error which is severe enough that we make no attempt to
811 continue. Do not use this for internal consistency checks; that's 1398 continue. Do not use this for internal consistency checks; that's
812 internal_error. Use of this function should be rare. */ 1399 internal_error. Use of this function should be rare. */
813 void 1400 void
814 fatal_error (const char *gmsgid, ...) 1401 fatal_error (location_t loc, const char *gmsgid, ...)
815 { 1402 {
816 diagnostic_info diagnostic; 1403 va_list ap;
817 va_list ap; 1404 va_start (ap, gmsgid);
818 1405 rich_location richloc (line_table, loc);
819 va_start (ap, gmsgid); 1406 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_FATAL);
820 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
821 report_diagnostic (&diagnostic);
822 va_end (ap); 1407 va_end (ap);
823 1408
824 gcc_unreachable (); 1409 gcc_unreachable ();
825 } 1410 }
826 1411
829 a more specific message, or some other good reason, you should use 1414 a more specific message, or some other good reason, you should use
830 abort () instead of calling this function directly. */ 1415 abort () instead of calling this function directly. */
831 void 1416 void
832 internal_error (const char *gmsgid, ...) 1417 internal_error (const char *gmsgid, ...)
833 { 1418 {
834 diagnostic_info diagnostic; 1419 va_list ap;
835 va_list ap; 1420 va_start (ap, gmsgid);
836 1421 rich_location richloc (line_table, input_location);
837 va_start (ap, gmsgid); 1422 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE);
838 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE); 1423 va_end (ap);
839 report_diagnostic (&diagnostic); 1424
1425 gcc_unreachable ();
1426 }
1427
1428 /* Like internal_error, but no backtrace will be printed. Used when
1429 the internal error does not happen at the current location, but happened
1430 somewhere else. */
1431 void
1432 internal_error_no_backtrace (const char *gmsgid, ...)
1433 {
1434 va_list ap;
1435 va_start (ap, gmsgid);
1436 rich_location richloc (line_table, input_location);
1437 diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE_NOBT);
840 va_end (ap); 1438 va_end (ap);
841 1439
842 gcc_unreachable (); 1440 gcc_unreachable ();
843 } 1441 }
844 1442
863 This mustn't use internal_error, that will cause infinite recursion. */ 1461 This mustn't use internal_error, that will cause infinite recursion. */
864 1462
865 static void 1463 static void
866 error_recursion (diagnostic_context *context) 1464 error_recursion (diagnostic_context *context)
867 { 1465 {
868 diagnostic_info diagnostic;
869
870 if (context->lock < 3) 1466 if (context->lock < 3)
871 pp_flush (context->printer); 1467 pp_newline_and_flush (context->printer);
872 1468
873 fnotice (stderr, 1469 fnotice (stderr,
874 "Internal compiler error: Error reporting routines re-entered.\n"); 1470 "Internal compiler error: Error reporting routines re-entered.\n");
875 1471
876 /* Call diagnostic_action_after_output to get the "please submit a bug 1472 /* Call diagnostic_action_after_output to get the "please submit a bug
877 report" message. It only looks at the kind field of diagnostic_info. */ 1473 report" message. */
878 diagnostic.kind = DK_ICE; 1474 diagnostic_action_after_output (context, DK_ICE);
879 diagnostic_action_after_output (context, &diagnostic);
880 1475
881 /* Do not use gcc_unreachable here; that goes through internal_error 1476 /* Do not use gcc_unreachable here; that goes through internal_error
882 and therefore would cause infinite recursion. */ 1477 and therefore would cause infinite recursion. */
883 real_abort (); 1478 real_abort ();
884 } 1479 }
900 static void 1495 static void
901 real_abort (void) 1496 real_abort (void)
902 { 1497 {
903 abort (); 1498 abort ();
904 } 1499 }
1500
1501 #if CHECKING_P
1502
1503 namespace selftest {
1504
1505 /* Helper function for test_print_escaped_string. */
1506
1507 static void
1508 assert_print_escaped_string (const location &loc, const char *expected_output,
1509 const char *input)
1510 {
1511 pretty_printer pp;
1512 print_escaped_string (&pp, input);
1513 ASSERT_STREQ_AT (loc, expected_output, pp_formatted_text (&pp));
1514 }
1515
1516 #define ASSERT_PRINT_ESCAPED_STRING_STREQ(EXPECTED_OUTPUT, INPUT) \
1517 assert_print_escaped_string (SELFTEST_LOCATION, EXPECTED_OUTPUT, INPUT)
1518
1519 /* Tests of print_escaped_string. */
1520
1521 static void
1522 test_print_escaped_string ()
1523 {
1524 /* Empty string. */
1525 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"\"", "");
1526
1527 /* Non-empty string. */
1528 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"hello world\"", "hello world");
1529
1530 /* Various things that need to be escaped: */
1531 /* Backslash. */
1532 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\\after\"",
1533 "before\\after");
1534 /* Tab. */
1535 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\tafter\"",
1536 "before\tafter");
1537 /* Newline. */
1538 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\nafter\"",
1539 "before\nafter");
1540 /* Double quote. */
1541 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\\"after\"",
1542 "before\"after");
1543
1544 /* Non-printable characters: BEL: '\a': 0x07 */
1545 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\007after\"",
1546 "before\aafter");
1547 /* Non-printable characters: vertical tab: '\v': 0x0b */
1548 ASSERT_PRINT_ESCAPED_STRING_STREQ ("\"before\\013after\"",
1549 "before\vafter");
1550 }
1551
1552 /* Tests of print_parseable_fixits. */
1553
1554 /* Verify that print_parseable_fixits emits the empty string if there
1555 are no fixits. */
1556
1557 static void
1558 test_print_parseable_fixits_none ()
1559 {
1560 pretty_printer pp;
1561 rich_location richloc (line_table, UNKNOWN_LOCATION);
1562
1563 print_parseable_fixits (&pp, &richloc);
1564 ASSERT_STREQ ("", pp_formatted_text (&pp));
1565 }
1566
1567 /* Verify that print_parseable_fixits does the right thing if there
1568 is an insertion fixit hint. */
1569
1570 static void
1571 test_print_parseable_fixits_insert ()
1572 {
1573 pretty_printer pp;
1574 rich_location richloc (line_table, UNKNOWN_LOCATION);
1575
1576 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1577 linemap_line_start (line_table, 5, 100);
1578 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1579 location_t where = linemap_position_for_column (line_table, 10);
1580 richloc.add_fixit_insert_before (where, "added content");
1581
1582 print_parseable_fixits (&pp, &richloc);
1583 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:10}:\"added content\"\n",
1584 pp_formatted_text (&pp));
1585 }
1586
1587 /* Verify that print_parseable_fixits does the right thing if there
1588 is an removal fixit hint. */
1589
1590 static void
1591 test_print_parseable_fixits_remove ()
1592 {
1593 pretty_printer pp;
1594 rich_location richloc (line_table, UNKNOWN_LOCATION);
1595
1596 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1597 linemap_line_start (line_table, 5, 100);
1598 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1599 source_range where;
1600 where.m_start = linemap_position_for_column (line_table, 10);
1601 where.m_finish = linemap_position_for_column (line_table, 20);
1602 richloc.add_fixit_remove (where);
1603
1604 print_parseable_fixits (&pp, &richloc);
1605 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"\"\n",
1606 pp_formatted_text (&pp));
1607 }
1608
1609 /* Verify that print_parseable_fixits does the right thing if there
1610 is an replacement fixit hint. */
1611
1612 static void
1613 test_print_parseable_fixits_replace ()
1614 {
1615 pretty_printer pp;
1616 rich_location richloc (line_table, UNKNOWN_LOCATION);
1617
1618 linemap_add (line_table, LC_ENTER, false, "test.c", 0);
1619 linemap_line_start (line_table, 5, 100);
1620 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1621 source_range where;
1622 where.m_start = linemap_position_for_column (line_table, 10);
1623 where.m_finish = linemap_position_for_column (line_table, 20);
1624 richloc.add_fixit_replace (where, "replacement");
1625
1626 print_parseable_fixits (&pp, &richloc);
1627 ASSERT_STREQ ("fix-it:\"test.c\":{5:10-5:21}:\"replacement\"\n",
1628 pp_formatted_text (&pp));
1629 }
1630
1631 /* Run all of the selftests within this file. */
1632
1633 void
1634 diagnostic_c_tests ()
1635 {
1636 test_print_escaped_string ();
1637 test_print_parseable_fixits_none ();
1638 test_print_parseable_fixits_insert ();
1639 test_print_parseable_fixits_remove ();
1640 test_print_parseable_fixits_replace ();
1641 }
1642
1643 } // namespace selftest
1644
1645 #endif /* #if CHECKING_P */