* pretty-print.c (pp_base_maybe_space): New function.
[official-gcc.git] / gcc / diagnostic.c
blobb495d6451c36759757d374059a5f5e7bafaead3b
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>
6 This file is part of GCC.
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.
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.
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. */
24 /* This file implements the language independent aspect of diagnostic
25 message module. */
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 "tm_p.h"
35 #include "flags.h"
36 #include "input.h"
37 #include "toplev.h"
38 #include "intl.h"
39 #include "diagnostic.h"
40 #include "langhooks.h"
41 #include "langhooks-def.h"
44 /* Prototypes. */
45 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
47 static void default_diagnostic_starter (diagnostic_context *,
48 diagnostic_info *);
49 static void default_diagnostic_finalizer (diagnostic_context *,
50 diagnostic_info *);
52 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
53 static bool text_specifies_location (text_info *, location_t *);
54 static bool diagnostic_count_diagnostic (diagnostic_context *,
55 diagnostic_info *);
56 static void diagnostic_action_after_output (diagnostic_context *,
57 diagnostic_info *);
58 static void real_abort (void) ATTRIBUTE_NORETURN;
60 /* A diagnostic_context surrogate for stderr. */
61 static diagnostic_context global_diagnostic_context;
62 diagnostic_context *global_dc = &global_diagnostic_context;
64 /* Boilerplate text used in two locations. */
65 #define bug_report_request \
66 "Please submit a full bug report,\n\
67 with preprocessed source if appropriate.\n\
68 See %s for instructions.\n"
71 /* Return a malloc'd string containing MSG formatted a la printf. The
72 caller is responsible for freeing the memory. */
73 static char *
74 build_message_string (const char *msg, ...)
76 char *str;
77 va_list ap;
79 va_start (ap, msg);
80 vasprintf (&str, msg, ap);
81 va_end (ap);
83 return str;
86 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
87 char *
88 file_name_as_prefix (const char *f)
90 return build_message_string ("%s: ", f);
95 /* Initialize the diagnostic message outputting machinery. */
96 void
97 diagnostic_initialize (diagnostic_context *context)
99 /* Allocate a basic pretty-printer. Clients will replace this a
100 much more elaborated pretty-printer if they wish. */
101 context->printer = xmalloc (sizeof (pretty_printer));
102 pp_construct (context->printer, NULL, 0);
103 /* By default, diagnostics are sent to stderr. */
104 context->printer->buffer->stream = stderr;
105 /* By default, we emit prefixes once per message. */
106 context->printer->prefixing_rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
108 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
109 context->warnings_are_errors_message = warnings_are_errors;
110 context->abort_on_error = false;
111 context->internal_error = NULL;
112 diagnostic_starter (context) = default_diagnostic_starter;
113 diagnostic_finalizer (context) = default_diagnostic_finalizer;
114 context->last_module = 0;
115 context->last_function = NULL;
116 context->lock = 0;
117 context->x_data = NULL;
120 /* Returns true if the next format specifier in TEXT is a format specifier
121 for a location_t. If so, update the object pointed by LOCUS to reflect
122 the specified location in *TEXT->args_ptr. */
123 static bool
124 text_specifies_location (text_info *text, location_t *locus)
126 const char *p;
127 /* Skip any leading text. */
128 for (p = text->format_spec; *p && *p != '%'; ++p)
131 /* Extract the location information if any. */
132 if (p[0] == '%' && p[1] == 'H')
134 *locus = *va_arg (*text->args_ptr, location_t *);
135 text->format_spec = p + 2;
136 return true;
138 else if (p[0] == '%' && p[1] == 'J')
140 tree t = va_arg (*text->args_ptr, tree);
141 *locus = DECL_SOURCE_LOCATION (t);
142 text->format_spec = p + 2;
143 return true;
146 return false;
149 void
150 diagnostic_set_info (diagnostic_info *diagnostic, const char *msgid,
151 va_list *args, location_t location,
152 diagnostic_t kind)
154 diagnostic->message.err_no = errno;
155 diagnostic->message.args_ptr = args;
156 diagnostic->message.format_spec = _(msgid);
157 /* If the diagnostic message doesn't specify a location,
158 use LOCATION. */
159 if (!text_specifies_location (&diagnostic->message, &diagnostic->location))
160 diagnostic->location = location;
161 diagnostic->kind = kind;
164 /* Return a malloc'd string describing a location. The caller is
165 responsible for freeing the memory. */
166 char *
167 diagnostic_build_prefix (diagnostic_info *diagnostic)
169 static const char *const diagnostic_kind_text[] = {
170 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
171 #include "diagnostic.def"
172 #undef DEFINE_DIAGNOSTIC_KIND
173 "must-not-happen"
175 if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
176 abort();
178 return diagnostic->location.file
179 ? build_message_string ("%s:%d: %s",
180 diagnostic->location.file,
181 diagnostic->location.line,
182 _(diagnostic_kind_text[diagnostic->kind]))
183 : build_message_string ("%s: %s", progname,
184 _(diagnostic_kind_text[diagnostic->kind]));
187 /* Count a diagnostic. Return true if the message should be printed. */
188 static bool
189 diagnostic_count_diagnostic (diagnostic_context *context,
190 diagnostic_info *diagnostic)
192 diagnostic_t kind = diagnostic->kind;
193 switch (kind)
195 default:
196 abort();
197 break;
199 case DK_ICE:
200 #ifndef ENABLE_CHECKING
201 /* When not checking, ICEs are converted to fatal errors when an
202 error has already occurred. This is counteracted by
203 abort_on_error. */
204 if ((diagnostic_kind_count (context, DK_ERROR) > 0
205 || diagnostic_kind_count (context, DK_SORRY) > 0)
206 && !context->abort_on_error)
208 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
209 diagnostic->location.file, diagnostic->location.line);
210 exit (FATAL_EXIT_CODE);
212 #endif
213 if (context->internal_error)
214 (*context->internal_error) (diagnostic->message.format_spec,
215 diagnostic->message.args_ptr);
216 /* Fall through. */
218 case DK_FATAL: case DK_SORRY:
219 case DK_ANACHRONISM: case DK_NOTE:
220 ++diagnostic_kind_count (context, kind);
221 break;
223 case DK_WARNING:
224 if (!diagnostic_report_warnings_p ())
225 return false;
227 if (!warnings_are_errors)
229 ++diagnostic_kind_count (context, DK_WARNING);
230 break;
233 if (context->warnings_are_errors_message)
235 pp_verbatim (context->printer,
236 "%s: warnings being treated as errors\n", progname);
237 context->warnings_are_errors_message = false;
240 /* And fall through. */
241 case DK_ERROR:
242 ++diagnostic_kind_count (context, DK_ERROR);
243 break;
246 return true;
249 /* Take any action which is expected to happen after the diagnostic
250 is written out. This function does not always return. */
251 static void
252 diagnostic_action_after_output (diagnostic_context *context,
253 diagnostic_info *diagnostic)
255 switch (diagnostic->kind)
257 case DK_DEBUG:
258 case DK_NOTE:
259 case DK_ANACHRONISM:
260 case DK_WARNING:
261 break;
263 case DK_ERROR:
264 case DK_SORRY:
265 if (context->abort_on_error)
266 real_abort ();
267 break;
269 case DK_ICE:
270 if (context->abort_on_error)
271 real_abort ();
273 fnotice (stderr, bug_report_request, bug_report_url);
274 exit (FATAL_EXIT_CODE);
276 case DK_FATAL:
277 if (context->abort_on_error)
278 real_abort ();
280 fnotice (stderr, "compilation terminated.\n");
281 exit (FATAL_EXIT_CODE);
283 default:
284 real_abort ();
288 /* Prints out, if necessary, the name of the current function
289 that caused an error. Called from all error and warning functions.
290 We ignore the FILE parameter, as it cannot be relied upon. */
292 void
293 diagnostic_report_current_function (diagnostic_context *context)
295 diagnostic_report_current_module (context);
296 lang_hooks.print_error_function (context, input_filename);
299 void
300 diagnostic_report_current_module (diagnostic_context *context)
302 struct file_stack *p;
304 if (pp_needs_newline (context->printer))
306 pp_newline (context->printer);
307 pp_needs_newline (context->printer) = false;
310 if (input_file_stack && diagnostic_last_module_changed (context))
312 p = input_file_stack;
313 pp_verbatim (context->printer,
314 "In file included from %s:%d",
315 p->location.file, p->location.line);
316 while ((p = p->next) != NULL)
317 pp_verbatim (context->printer,
318 ",\n from %s:%d",
319 p->location.file, p->location.line);
320 pp_verbatim (context->printer, ":\n");
321 diagnostic_set_last_module (context);
325 static void
326 default_diagnostic_starter (diagnostic_context *context,
327 diagnostic_info *diagnostic)
329 diagnostic_report_current_function (context);
330 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
333 static void
334 default_diagnostic_finalizer (diagnostic_context *context,
335 diagnostic_info *diagnostic __attribute__((unused)))
337 pp_destroy_prefix (context->printer);
340 /* Report a diagnostic message (an error or a warning) as specified by
341 DC. This function is *the* subroutine in terms of which front-ends
342 should implement their specific diagnostic handling modules. The
343 front-end independent format specifiers are exactly those described
344 in the documentation of output_format. */
346 void
347 diagnostic_report_diagnostic (diagnostic_context *context,
348 diagnostic_info *diagnostic)
350 if (context->lock++ && diagnostic->kind < DK_SORRY)
351 error_recursion (context);
353 if (diagnostic_count_diagnostic (context, diagnostic))
355 (*diagnostic_starter (context)) (context, diagnostic);
356 pp_format_text (context->printer, &diagnostic->message);
357 (*diagnostic_finalizer (context)) (context, diagnostic);
358 pp_flush (context->printer);
359 diagnostic_action_after_output (context, diagnostic);
362 context->lock--;
365 /* Given a partial pathname as input, return another pathname that
366 shares no directory elements with the pathname of __FILE__. This
367 is used by fancy_abort() to print `Internal compiler error in expr.c'
368 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
370 const char *
371 trim_filename (const char *name)
373 static const char this_file[] = __FILE__;
374 const char *p = name, *q = this_file;
376 /* First skip any "../" in each filename. This allows us to give a proper
377 reference to a file in a subdirectory. */
378 while (p[0] == '.' && p[1] == '.'
379 && (p[2] == DIR_SEPARATOR
380 #ifdef DIR_SEPARATOR_2
381 || p[2] == DIR_SEPARATOR_2
382 #endif
384 p += 3;
386 while (q[0] == '.' && q[1] == '.'
387 && (q[2] == DIR_SEPARATOR
388 #ifdef DIR_SEPARATOR_2
389 || p[2] == DIR_SEPARATOR_2
390 #endif
392 q += 3;
394 /* Now skip any parts the two filenames have in common. */
395 while (*p == *q && *p != 0 && *q != 0)
396 p++, q++;
398 /* Now go backwards until the previous directory separator. */
399 while (p > name && p[-1] != DIR_SEPARATOR
400 #ifdef DIR_SEPARATOR_2
401 && p[-1] != DIR_SEPARATOR_2
402 #endif
404 p--;
406 return p;
409 /* Standard error reporting routines in increasing order of severity.
410 All of these take arguments like printf. */
412 /* Text to be emitted verbatim to the error message stream; this
413 produces no prefix and disables line-wrapping. Use rarely. */
414 void
415 verbatim (const char *msgid, ...)
417 text_info text;
418 va_list ap;
420 va_start (ap, msgid);
421 text.err_no = errno;
422 text.args_ptr = &ap;
423 text.format_spec = _(msgid);
424 pp_format_verbatim (global_dc->printer, &text);
425 pp_flush (global_dc->printer);
426 va_end (ap);
429 /* An informative note. Use this for additional details on an error
430 message. */
431 void
432 inform (const char *msgid, ...)
434 diagnostic_info diagnostic;
435 va_list ap;
437 va_start (ap, msgid);
438 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_NOTE);
439 report_diagnostic (&diagnostic);
440 va_end (ap);
443 /* A warning. Use this for code which is correct according to the
444 relevant language specification but is likely to be buggy anyway. */
445 void
446 warning (const char *msgid, ...)
448 diagnostic_info diagnostic;
449 va_list ap;
451 va_start (ap, msgid);
452 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_WARNING);
453 report_diagnostic (&diagnostic);
454 va_end (ap);
457 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
458 given on the command line, in which case it issues an error. Use
459 this for diagnostics required by the relevant language standard,
460 if you have chosen not to make them errors.
462 Note that these diagnostics are issued independent of the setting
463 of the -pedantic command-line switch. To get a warning enabled
464 only with that switch, write "if (pedantic) pedwarn (...);" */
465 void
466 pedwarn (const char *msgid, ...)
468 diagnostic_info diagnostic;
469 va_list ap;
471 va_start (ap, msgid);
472 diagnostic_set_info (&diagnostic, msgid, &ap, input_location,
473 pedantic_error_kind ());
474 report_diagnostic (&diagnostic);
475 va_end (ap);
478 /* A hard error: the code is definitely ill-formed, and an object file
479 will not be produced. */
480 void
481 error (const char *msgid, ...)
483 diagnostic_info diagnostic;
484 va_list ap;
486 va_start (ap, msgid);
487 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ERROR);
488 report_diagnostic (&diagnostic);
489 va_end (ap);
492 /* "Sorry, not implemented." Use for a language feature which is
493 required by the relevant specification but not implemented by GCC.
494 An object file will not be produced. */
495 void
496 sorry (const char *msgid, ...)
498 diagnostic_info diagnostic;
499 va_list ap;
501 va_start (ap, msgid);
502 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_SORRY);
503 report_diagnostic (&diagnostic);
504 va_end (ap);
507 /* An error which is severe enough that we make no attempt to
508 continue. Do not use this for internal consistency checks; that's
509 internal_error. Use of this function should be rare. */
510 void
511 fatal_error (const char *msgid, ...)
513 diagnostic_info diagnostic;
514 va_list ap;
516 va_start (ap, msgid);
517 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_FATAL);
518 report_diagnostic (&diagnostic);
519 va_end (ap);
521 /* NOTREACHED */
522 real_abort ();
525 /* An internal consistency check has failed. We make no attempt to
526 continue. Note that unless there is debugging value to be had from
527 a more specific message, or some other good reason, you should use
528 abort () instead of calling this function directly. */
529 void
530 internal_error (const char *msgid, ...)
532 diagnostic_info diagnostic;
533 va_list ap;
535 va_start (ap, msgid);
536 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ICE);
537 report_diagnostic (&diagnostic);
538 va_end (ap);
540 /* NOTREACHED */
541 real_abort ();
544 /* Special case error functions. Most are implemented in terms of the
545 above, or should be. */
547 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
548 runs its second argument through gettext. */
549 void
550 fnotice (FILE *file, const char *msgid, ...)
552 va_list ap;
554 va_start (ap, msgid);
555 vfprintf (file, _(msgid), ap);
556 va_end (ap);
559 /* Inform the user that an error occurred while trying to report some
560 other error. This indicates catastrophic internal inconsistencies,
561 so give up now. But do try to flush out the previous error.
562 This mustn't use internal_error, that will cause infinite recursion. */
564 static void
565 error_recursion (diagnostic_context *context)
567 if (context->lock < 3)
568 pp_flush (context->printer);
570 fnotice (stderr,
571 "Internal compiler error: Error reporting routines re-entered.\n");
572 fnotice (stderr, bug_report_request, bug_report_url);
573 exit (FATAL_EXIT_CODE);
576 /* Report an internal compiler error in a friendly manner. This is
577 the function that gets called upon use of abort() in the source
578 code generally, thanks to a special macro. */
580 void
581 fancy_abort (const char *file, int line, const char *function)
583 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
586 /* Really call the system 'abort'. This has to go right at the end of
587 this file, so that there are no functions after it that call abort
588 and get the system abort instead of our macro. */
589 #undef abort
590 static void
591 real_abort (void)
593 abort ();