tree.h: Include vec.h
[gcc.git] / gcc / diagnostic.c
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23
24 /* This file implements the language independent aspect of diagnostic
25 message module. */
26
27 #include "config.h"
28 #undef FLOAT /* This is for hpux. They should change hpux. */
29 #undef FFS /* Some systems define this in param.h. */
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "version.h"
35 #include "tm_p.h"
36 #include "flags.h"
37 #include "input.h"
38 #include "toplev.h"
39 #include "intl.h"
40 #include "diagnostic.h"
41 #include "langhooks.h"
42 #include "langhooks-def.h"
43
44
45 /* Prototypes. */
46 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
47
48 static void default_diagnostic_starter (diagnostic_context *,
49 diagnostic_info *);
50 static void default_diagnostic_finalizer (diagnostic_context *,
51 diagnostic_info *);
52
53 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
54 static bool text_specifies_location (text_info *, location_t *);
55 static bool diagnostic_count_diagnostic (diagnostic_context *,
56 diagnostic_info *);
57 static void diagnostic_action_after_output (diagnostic_context *,
58 diagnostic_info *);
59 static void real_abort (void) ATTRIBUTE_NORETURN;
60
61 /* A diagnostic_context surrogate for stderr. */
62 static diagnostic_context global_diagnostic_context;
63 diagnostic_context *global_dc = &global_diagnostic_context;
64
65 /* Boilerplate text used in two locations. */
66 #define bug_report_request \
67 "Please submit a full bug report,\n\
68 with preprocessed source if appropriate.\n\
69 See %s for instructions.\n"
70
71 \f
72 /* Return a malloc'd string containing MSG formatted a la printf. The
73 caller is responsible for freeing the memory. */
74 static char *
75 build_message_string (const char *msg, ...)
76 {
77 char *str;
78 va_list ap;
79
80 va_start (ap, msg);
81 vasprintf (&str, msg, ap);
82 va_end (ap);
83
84 return str;
85 }
86
87 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
88 char *
89 file_name_as_prefix (const char *f)
90 {
91 return build_message_string ("%s: ", f);
92 }
93
94
95 \f
96 /* Initialize the diagnostic message outputting machinery. */
97 void
98 diagnostic_initialize (diagnostic_context *context)
99 {
100 /* Allocate a basic pretty-printer. Clients will replace this a
101 much more elaborated pretty-printer if they wish. */
102 context->printer = xmalloc (sizeof (pretty_printer));
103 pp_construct (context->printer, NULL, 0);
104 /* By default, diagnostics are sent to stderr. */
105 context->printer->buffer->stream = stderr;
106 /* By default, we emit prefixes once per message. */
107 context->printer->prefixing_rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
108
109 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
110 context->warnings_are_errors_message = warnings_are_errors;
111 context->abort_on_error = false;
112 context->internal_error = NULL;
113 diagnostic_starter (context) = default_diagnostic_starter;
114 diagnostic_finalizer (context) = default_diagnostic_finalizer;
115 context->last_module = 0;
116 context->last_function = NULL;
117 context->lock = 0;
118 context->x_data = NULL;
119 }
120
121 /* Returns true if the next format specifier in TEXT is a format specifier
122 for a location_t. If so, update the object pointed by LOCUS to reflect
123 the specified location in *TEXT->args_ptr. */
124 static bool
125 text_specifies_location (text_info *text, location_t *locus)
126 {
127 const char *p;
128 /* Skip any leading text. */
129 for (p = text->format_spec; *p && *p != '%'; ++p)
130 ;
131
132 /* Extract the location information if any. */
133 if (p[0] == '%' && p[1] == 'H')
134 {
135 *locus = *va_arg (*text->args_ptr, location_t *);
136 text->format_spec = p + 2;
137 return true;
138 }
139 else if (p[0] == '%' && p[1] == 'J')
140 {
141 tree t = va_arg (*text->args_ptr, tree);
142 *locus = DECL_SOURCE_LOCATION (t);
143 text->format_spec = p + 2;
144 return true;
145 }
146
147 return false;
148 }
149
150 void
151 diagnostic_set_info (diagnostic_info *diagnostic, const char *msgid,
152 va_list *args, location_t location,
153 diagnostic_t kind)
154 {
155 diagnostic->message.err_no = errno;
156 diagnostic->message.args_ptr = args;
157 diagnostic->message.format_spec = _(msgid);
158 /* If the diagnostic message doesn't specify a location,
159 use LOCATION. */
160 if (!text_specifies_location (&diagnostic->message, &diagnostic->location))
161 diagnostic->location = location;
162 diagnostic->kind = kind;
163 }
164
165 /* Return a malloc'd string describing a location. The caller is
166 responsible for freeing the memory. */
167 char *
168 diagnostic_build_prefix (diagnostic_info *diagnostic)
169 {
170 static const char *const diagnostic_kind_text[] = {
171 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
172 #include "diagnostic.def"
173 #undef DEFINE_DIAGNOSTIC_KIND
174 "must-not-happen"
175 };
176 expanded_location s = expand_location (diagnostic->location);
177 if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
178 abort();
179
180 return s.file
181 ? build_message_string ("%s:%d: %s",
182 s.file, s.line,
183 _(diagnostic_kind_text[diagnostic->kind]))
184 : build_message_string ("%s: %s", progname,
185 _(diagnostic_kind_text[diagnostic->kind]));
186 }
187
188 /* Count a diagnostic. Return true if the message should be printed. */
189 static bool
190 diagnostic_count_diagnostic (diagnostic_context *context,
191 diagnostic_info *diagnostic)
192 {
193 diagnostic_t kind = diagnostic->kind;
194 switch (kind)
195 {
196 default:
197 abort();
198 break;
199
200 case DK_ICE:
201 #ifndef ENABLE_CHECKING
202 /* When not checking, ICEs are converted to fatal errors when an
203 error has already occurred. This is counteracted by
204 abort_on_error. */
205 if ((diagnostic_kind_count (context, DK_ERROR) > 0
206 || diagnostic_kind_count (context, DK_SORRY) > 0)
207 && !context->abort_on_error)
208 {
209 expanded_location s = expand_location (diagnostic->location);
210 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
211 s.file, s.line);
212 exit (FATAL_EXIT_CODE);
213 }
214 #endif
215 if (context->internal_error)
216 (*context->internal_error) (diagnostic->message.format_spec,
217 diagnostic->message.args_ptr);
218 /* Fall through. */
219
220 case DK_FATAL: case DK_SORRY:
221 case DK_ANACHRONISM: case DK_NOTE:
222 ++diagnostic_kind_count (context, kind);
223 break;
224
225 case DK_WARNING:
226 if (!diagnostic_report_warnings_p ())
227 return false;
228
229 if (!warnings_are_errors)
230 {
231 ++diagnostic_kind_count (context, DK_WARNING);
232 break;
233 }
234
235 if (context->warnings_are_errors_message)
236 {
237 pp_verbatim (context->printer,
238 "%s: warnings being treated as errors\n", progname);
239 context->warnings_are_errors_message = false;
240 }
241
242 /* And fall through. */
243 case DK_ERROR:
244 ++diagnostic_kind_count (context, DK_ERROR);
245 break;
246 }
247
248 return true;
249 }
250
251 /* Take any action which is expected to happen after the diagnostic
252 is written out. This function does not always return. */
253 static void
254 diagnostic_action_after_output (diagnostic_context *context,
255 diagnostic_info *diagnostic)
256 {
257 switch (diagnostic->kind)
258 {
259 case DK_DEBUG:
260 case DK_NOTE:
261 case DK_ANACHRONISM:
262 case DK_WARNING:
263 break;
264
265 case DK_ERROR:
266 case DK_SORRY:
267 if (context->abort_on_error)
268 real_abort ();
269 if (flag_fatal_errors)
270 {
271 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
272 exit (FATAL_EXIT_CODE);
273 }
274 break;
275
276 case DK_ICE:
277 if (context->abort_on_error)
278 real_abort ();
279
280 fnotice (stderr, bug_report_request, bug_report_url);
281 exit (FATAL_EXIT_CODE);
282
283 case DK_FATAL:
284 if (context->abort_on_error)
285 real_abort ();
286
287 fnotice (stderr, "compilation terminated.\n");
288 exit (FATAL_EXIT_CODE);
289
290 default:
291 real_abort ();
292 }
293 }
294
295 /* Prints out, if necessary, the name of the current function
296 that caused an error. Called from all error and warning functions.
297 We ignore the FILE parameter, as it cannot be relied upon. */
298
299 void
300 diagnostic_report_current_function (diagnostic_context *context)
301 {
302 diagnostic_report_current_module (context);
303 lang_hooks.print_error_function (context, input_filename);
304 }
305
306 void
307 diagnostic_report_current_module (diagnostic_context *context)
308 {
309 struct file_stack *p;
310
311 if (pp_needs_newline (context->printer))
312 {
313 pp_newline (context->printer);
314 pp_needs_newline (context->printer) = false;
315 }
316
317 p = input_file_stack;
318 if (p && diagnostic_last_module_changed (context))
319 {
320 expanded_location xloc = expand_location (p->location);
321 pp_verbatim (context->printer,
322 "In file included from %s:%d",
323 xloc.file, xloc.line);
324 while ((p = p->next) != NULL)
325 {
326 xloc = expand_location (p->location);
327 pp_verbatim (context->printer,
328 ",\n from %s:%d",
329 xloc.file, xloc.line);
330 }
331 pp_verbatim (context->printer, ":\n");
332 diagnostic_set_last_module (context);
333 }
334 }
335
336 static void
337 default_diagnostic_starter (diagnostic_context *context,
338 diagnostic_info *diagnostic)
339 {
340 diagnostic_report_current_function (context);
341 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
342 }
343
344 static void
345 default_diagnostic_finalizer (diagnostic_context *context,
346 diagnostic_info *diagnostic __attribute__((unused)))
347 {
348 pp_destroy_prefix (context->printer);
349 }
350
351 /* Report a diagnostic message (an error or a warning) as specified by
352 DC. This function is *the* subroutine in terms of which front-ends
353 should implement their specific diagnostic handling modules. The
354 front-end independent format specifiers are exactly those described
355 in the documentation of output_format. */
356
357 void
358 diagnostic_report_diagnostic (diagnostic_context *context,
359 diagnostic_info *diagnostic)
360 {
361 if (context->lock++ && diagnostic->kind < DK_SORRY)
362 error_recursion (context);
363
364 if (diagnostic_count_diagnostic (context, diagnostic))
365 {
366 (*diagnostic_starter (context)) (context, diagnostic);
367 pp_format_text (context->printer, &diagnostic->message);
368 (*diagnostic_finalizer (context)) (context, diagnostic);
369 pp_flush (context->printer);
370 diagnostic_action_after_output (context, diagnostic);
371 }
372
373 context->lock--;
374 }
375
376 /* Given a partial pathname as input, return another pathname that
377 shares no directory elements with the pathname of __FILE__. This
378 is used by fancy_abort() to print `Internal compiler error in expr.c'
379 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
380
381 const char *
382 trim_filename (const char *name)
383 {
384 static const char this_file[] = __FILE__;
385 const char *p = name, *q = this_file;
386
387 /* First skip any "../" in each filename. This allows us to give a proper
388 reference to a file in a subdirectory. */
389 while (p[0] == '.' && p[1] == '.'
390 && (p[2] == DIR_SEPARATOR
391 #ifdef DIR_SEPARATOR_2
392 || p[2] == DIR_SEPARATOR_2
393 #endif
394 ))
395 p += 3;
396
397 while (q[0] == '.' && q[1] == '.'
398 && (q[2] == DIR_SEPARATOR
399 #ifdef DIR_SEPARATOR_2
400 || p[2] == DIR_SEPARATOR_2
401 #endif
402 ))
403 q += 3;
404
405 /* Now skip any parts the two filenames have in common. */
406 while (*p == *q && *p != 0 && *q != 0)
407 p++, q++;
408
409 /* Now go backwards until the previous directory separator. */
410 while (p > name && p[-1] != DIR_SEPARATOR
411 #ifdef DIR_SEPARATOR_2
412 && p[-1] != DIR_SEPARATOR_2
413 #endif
414 )
415 p--;
416
417 return p;
418 }
419 \f
420 /* Standard error reporting routines in increasing order of severity.
421 All of these take arguments like printf. */
422
423 /* Text to be emitted verbatim to the error message stream; this
424 produces no prefix and disables line-wrapping. Use rarely. */
425 void
426 verbatim (const char *msgid, ...)
427 {
428 text_info text;
429 va_list ap;
430
431 va_start (ap, msgid);
432 text.err_no = errno;
433 text.args_ptr = &ap;
434 text.format_spec = _(msgid);
435 pp_format_verbatim (global_dc->printer, &text);
436 pp_flush (global_dc->printer);
437 va_end (ap);
438 }
439
440 /* An informative note. Use this for additional details on an error
441 message. */
442 void
443 inform (const char *msgid, ...)
444 {
445 diagnostic_info diagnostic;
446 va_list ap;
447
448 va_start (ap, msgid);
449 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_NOTE);
450 report_diagnostic (&diagnostic);
451 va_end (ap);
452 }
453
454 /* A warning. Use this for code which is correct according to the
455 relevant language specification but is likely to be buggy anyway. */
456 void
457 warning (const char *msgid, ...)
458 {
459 diagnostic_info diagnostic;
460 va_list ap;
461
462 va_start (ap, msgid);
463 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_WARNING);
464 report_diagnostic (&diagnostic);
465 va_end (ap);
466 }
467
468 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
469 given on the command line, in which case it issues an error. Use
470 this for diagnostics required by the relevant language standard,
471 if you have chosen not to make them errors.
472
473 Note that these diagnostics are issued independent of the setting
474 of the -pedantic command-line switch. To get a warning enabled
475 only with that switch, write "if (pedantic) pedwarn (...);" */
476 void
477 pedwarn (const char *msgid, ...)
478 {
479 diagnostic_info diagnostic;
480 va_list ap;
481
482 va_start (ap, msgid);
483 diagnostic_set_info (&diagnostic, msgid, &ap, input_location,
484 pedantic_error_kind ());
485 report_diagnostic (&diagnostic);
486 va_end (ap);
487 }
488
489 /* A hard error: the code is definitely ill-formed, and an object file
490 will not be produced. */
491 void
492 error (const char *msgid, ...)
493 {
494 diagnostic_info diagnostic;
495 va_list ap;
496
497 va_start (ap, msgid);
498 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ERROR);
499 report_diagnostic (&diagnostic);
500 va_end (ap);
501 }
502
503 /* "Sorry, not implemented." Use for a language feature which is
504 required by the relevant specification but not implemented by GCC.
505 An object file will not be produced. */
506 void
507 sorry (const char *msgid, ...)
508 {
509 diagnostic_info diagnostic;
510 va_list ap;
511
512 va_start (ap, msgid);
513 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_SORRY);
514 report_diagnostic (&diagnostic);
515 va_end (ap);
516 }
517
518 /* An error which is severe enough that we make no attempt to
519 continue. Do not use this for internal consistency checks; that's
520 internal_error. Use of this function should be rare. */
521 void
522 fatal_error (const char *msgid, ...)
523 {
524 diagnostic_info diagnostic;
525 va_list ap;
526
527 va_start (ap, msgid);
528 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_FATAL);
529 report_diagnostic (&diagnostic);
530 va_end (ap);
531
532 /* NOTREACHED */
533 real_abort ();
534 }
535
536 /* An internal consistency check has failed. We make no attempt to
537 continue. Note that unless there is debugging value to be had from
538 a more specific message, or some other good reason, you should use
539 abort () instead of calling this function directly. */
540 void
541 internal_error (const char *msgid, ...)
542 {
543 diagnostic_info diagnostic;
544 va_list ap;
545
546 va_start (ap, msgid);
547 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ICE);
548 report_diagnostic (&diagnostic);
549 va_end (ap);
550
551 /* NOTREACHED */
552 real_abort ();
553 }
554 \f
555 /* Special case error functions. Most are implemented in terms of the
556 above, or should be. */
557
558 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
559 runs its second argument through gettext. */
560 void
561 fnotice (FILE *file, const char *msgid, ...)
562 {
563 va_list ap;
564
565 va_start (ap, msgid);
566 vfprintf (file, _(msgid), ap);
567 va_end (ap);
568 }
569
570 /* Inform the user that an error occurred while trying to report some
571 other error. This indicates catastrophic internal inconsistencies,
572 so give up now. But do try to flush out the previous error.
573 This mustn't use internal_error, that will cause infinite recursion. */
574
575 static void
576 error_recursion (diagnostic_context *context)
577 {
578 if (context->lock < 3)
579 pp_flush (context->printer);
580
581 fnotice (stderr,
582 "Internal compiler error: Error reporting routines re-entered.\n");
583 fnotice (stderr, bug_report_request, bug_report_url);
584 exit (FATAL_EXIT_CODE);
585 }
586
587 /* Report an internal compiler error in a friendly manner. This is
588 the function that gets called upon use of abort() in the source
589 code generally, thanks to a special macro. */
590
591 void
592 fancy_abort (const char *file, int line, const char *function)
593 {
594 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
595 }
596
597 /* Really call the system 'abort'. This has to go right at the end of
598 this file, so that there are no functions after it that call abort
599 and get the system abort instead of our macro. */
600 #undef abort
601 static void
602 real_abort (void)
603 {
604 abort ();
605 }