2004-09-23 H.J. Lu <hongjiu.lu@intel.com>
[official-gcc.git] / gcc / diagnostic.c
blobd6b6ea724a2e7606d70e6560ff5fa1adb80ea600
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 "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"
45 /* Prototypes. */
46 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
48 static void default_diagnostic_starter (diagnostic_context *,
49 diagnostic_info *);
50 static void default_diagnostic_finalizer (diagnostic_context *,
51 diagnostic_info *);
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;
61 /* A diagnostic_context surrogate for stderr. */
62 static diagnostic_context global_diagnostic_context;
63 diagnostic_context *global_dc = &global_diagnostic_context;
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"
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, ...)
77 char *str;
78 va_list ap;
80 va_start (ap, msg);
81 vasprintf (&str, msg, ap);
82 va_end (ap);
84 return str;
87 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
88 char *
89 file_name_as_prefix (const char *f)
91 return build_message_string ("%s: ", f);
96 /* Initialize the diagnostic message outputting machinery. */
97 void
98 diagnostic_initialize (diagnostic_context *context)
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;
109 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
110 context->issue_warnings_are_errors_message = true;
111 context->warning_as_error_requested = false;
112 context->abort_on_error = false;
113 context->internal_error = NULL;
114 diagnostic_starter (context) = default_diagnostic_starter;
115 diagnostic_finalizer (context) = default_diagnostic_finalizer;
116 context->last_module = 0;
117 context->last_function = NULL;
118 context->lock = 0;
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)
127 const char *p;
128 /* Skip any leading text. */
129 for (p = text->format_spec; *p && *p != '%'; ++p)
132 /* Extract the location information if any. */
133 if (p[0] == '%' && p[1] == 'H')
135 *locus = *va_arg (*text->args_ptr, location_t *);
136 text->format_spec = p + 2;
137 return true;
139 else if (p[0] == '%' && p[1] == 'J')
141 tree t = va_arg (*text->args_ptr, tree);
142 *locus = DECL_SOURCE_LOCATION (t);
143 text->format_spec = p + 2;
144 return true;
147 return false;
150 void
151 diagnostic_set_info (diagnostic_info *diagnostic, const char *msgid,
152 va_list *args, location_t location,
153 diagnostic_t kind)
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;
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)
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"
176 expanded_location s = expand_location (diagnostic->location);
177 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
179 return s.file
180 ? build_message_string ("%s:%d: %s",
181 s.file, s.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 gcc_unreachable ();
198 case DK_ICE:
199 #ifndef ENABLE_CHECKING
200 /* When not checking, ICEs are converted to fatal errors when an
201 error has already occurred. This is counteracted by
202 abort_on_error. */
203 if ((diagnostic_kind_count (context, DK_ERROR) > 0
204 || diagnostic_kind_count (context, DK_SORRY) > 0)
205 && !context->abort_on_error)
207 expanded_location s = expand_location (diagnostic->location);
208 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
209 s.file, s.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 (!context->warning_as_error_requested)
229 ++diagnostic_kind_count (context, DK_WARNING);
230 break;
232 else if (context->issue_warnings_are_errors_message)
234 pp_verbatim (context->printer,
235 "%s: warnings being treated as errors\n", progname);
236 context->issue_warnings_are_errors_message = false;
239 /* And fall through. */
240 case DK_ERROR:
241 ++diagnostic_kind_count (context, DK_ERROR);
242 break;
245 return true;
248 /* Take any action which is expected to happen after the diagnostic
249 is written out. This function does not always return. */
250 static void
251 diagnostic_action_after_output (diagnostic_context *context,
252 diagnostic_info *diagnostic)
254 switch (diagnostic->kind)
256 case DK_DEBUG:
257 case DK_NOTE:
258 case DK_ANACHRONISM:
259 case DK_WARNING:
260 break;
262 case DK_ERROR:
263 case DK_SORRY:
264 if (context->abort_on_error)
265 real_abort ();
266 if (flag_fatal_errors)
268 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
269 exit (FATAL_EXIT_CODE);
271 break;
273 case DK_ICE:
274 if (context->abort_on_error)
275 real_abort ();
277 fnotice (stderr, bug_report_request, bug_report_url);
278 exit (FATAL_EXIT_CODE);
280 case DK_FATAL:
281 if (context->abort_on_error)
282 real_abort ();
284 fnotice (stderr, "compilation terminated.\n");
285 exit (FATAL_EXIT_CODE);
287 default:
288 real_abort ();
292 /* Prints out, if necessary, the name of the current function
293 that caused an error. Called from all error and warning functions.
294 We ignore the FILE parameter, as it cannot be relied upon. */
296 void
297 diagnostic_report_current_function (diagnostic_context *context)
299 diagnostic_report_current_module (context);
300 lang_hooks.print_error_function (context, input_filename);
303 void
304 diagnostic_report_current_module (diagnostic_context *context)
306 struct file_stack *p;
308 if (pp_needs_newline (context->printer))
310 pp_newline (context->printer);
311 pp_needs_newline (context->printer) = false;
314 p = input_file_stack;
315 if (p && diagnostic_last_module_changed (context))
317 expanded_location xloc = expand_location (p->location);
318 pp_verbatim (context->printer,
319 "In file included from %s:%d",
320 xloc.file, xloc.line);
321 while ((p = p->next) != NULL)
323 xloc = expand_location (p->location);
324 pp_verbatim (context->printer,
325 ",\n from %s:%d",
326 xloc.file, xloc.line);
328 pp_verbatim (context->printer, ":\n");
329 diagnostic_set_last_module (context);
333 static void
334 default_diagnostic_starter (diagnostic_context *context,
335 diagnostic_info *diagnostic)
337 diagnostic_report_current_function (context);
338 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
341 static void
342 default_diagnostic_finalizer (diagnostic_context *context,
343 diagnostic_info *diagnostic __attribute__((unused)))
345 pp_destroy_prefix (context->printer);
348 /* Report a diagnostic message (an error or a warning) as specified by
349 DC. This function is *the* subroutine in terms of which front-ends
350 should implement their specific diagnostic handling modules. The
351 front-end independent format specifiers are exactly those described
352 in the documentation of output_format. */
354 void
355 diagnostic_report_diagnostic (diagnostic_context *context,
356 diagnostic_info *diagnostic)
358 if (context->lock++ && diagnostic->kind < DK_SORRY)
359 error_recursion (context);
361 if (diagnostic_count_diagnostic (context, diagnostic))
363 (*diagnostic_starter (context)) (context, diagnostic);
364 pp_format_text (context->printer, &diagnostic->message);
365 (*diagnostic_finalizer (context)) (context, diagnostic);
366 pp_flush (context->printer);
367 diagnostic_action_after_output (context, diagnostic);
370 context->lock--;
373 /* Given a partial pathname as input, return another pathname that
374 shares no directory elements with the pathname of __FILE__. This
375 is used by fancy_abort() to print `Internal compiler error in expr.c'
376 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
378 const char *
379 trim_filename (const char *name)
381 static const char this_file[] = __FILE__;
382 const char *p = name, *q = this_file;
384 /* First skip any "../" in each filename. This allows us to give a proper
385 reference to a file in a subdirectory. */
386 while (p[0] == '.' && p[1] == '.'
387 && (p[2] == DIR_SEPARATOR
388 #ifdef DIR_SEPARATOR_2
389 || p[2] == DIR_SEPARATOR_2
390 #endif
392 p += 3;
394 while (q[0] == '.' && q[1] == '.'
395 && (q[2] == DIR_SEPARATOR
396 #ifdef DIR_SEPARATOR_2
397 || p[2] == DIR_SEPARATOR_2
398 #endif
400 q += 3;
402 /* Now skip any parts the two filenames have in common. */
403 while (*p == *q && *p != 0 && *q != 0)
404 p++, q++;
406 /* Now go backwards until the previous directory separator. */
407 while (p > name && p[-1] != DIR_SEPARATOR
408 #ifdef DIR_SEPARATOR_2
409 && p[-1] != DIR_SEPARATOR_2
410 #endif
412 p--;
414 return p;
417 /* Standard error reporting routines in increasing order of severity.
418 All of these take arguments like printf. */
420 /* Text to be emitted verbatim to the error message stream; this
421 produces no prefix and disables line-wrapping. Use rarely. */
422 void
423 verbatim (const char *msgid, ...)
425 text_info text;
426 va_list ap;
428 va_start (ap, msgid);
429 text.err_no = errno;
430 text.args_ptr = &ap;
431 text.format_spec = _(msgid);
432 pp_format_verbatim (global_dc->printer, &text);
433 pp_flush (global_dc->printer);
434 va_end (ap);
437 /* An informative note. Use this for additional details on an error
438 message. */
439 void
440 inform (const char *msgid, ...)
442 diagnostic_info diagnostic;
443 va_list ap;
445 va_start (ap, msgid);
446 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_NOTE);
447 report_diagnostic (&diagnostic);
448 va_end (ap);
451 /* A warning. Use this for code which is correct according to the
452 relevant language specification but is likely to be buggy anyway. */
453 void
454 warning (const char *msgid, ...)
456 diagnostic_info diagnostic;
457 va_list ap;
459 va_start (ap, msgid);
460 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_WARNING);
461 report_diagnostic (&diagnostic);
462 va_end (ap);
465 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
466 given on the command line, in which case it issues an error. Use
467 this for diagnostics required by the relevant language standard,
468 if you have chosen not to make them errors.
470 Note that these diagnostics are issued independent of the setting
471 of the -pedantic command-line switch. To get a warning enabled
472 only with that switch, write "if (pedantic) pedwarn (...);" */
473 void
474 pedwarn (const char *msgid, ...)
476 diagnostic_info diagnostic;
477 va_list ap;
479 va_start (ap, msgid);
480 diagnostic_set_info (&diagnostic, msgid, &ap, input_location,
481 pedantic_error_kind ());
482 report_diagnostic (&diagnostic);
483 va_end (ap);
486 /* A hard error: the code is definitely ill-formed, and an object file
487 will not be produced. */
488 void
489 error (const char *msgid, ...)
491 diagnostic_info diagnostic;
492 va_list ap;
494 va_start (ap, msgid);
495 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ERROR);
496 report_diagnostic (&diagnostic);
497 va_end (ap);
500 /* "Sorry, not implemented." Use for a language feature which is
501 required by the relevant specification but not implemented by GCC.
502 An object file will not be produced. */
503 void
504 sorry (const char *msgid, ...)
506 diagnostic_info diagnostic;
507 va_list ap;
509 va_start (ap, msgid);
510 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_SORRY);
511 report_diagnostic (&diagnostic);
512 va_end (ap);
515 /* An error which is severe enough that we make no attempt to
516 continue. Do not use this for internal consistency checks; that's
517 internal_error. Use of this function should be rare. */
518 void
519 fatal_error (const char *msgid, ...)
521 diagnostic_info diagnostic;
522 va_list ap;
524 va_start (ap, msgid);
525 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_FATAL);
526 report_diagnostic (&diagnostic);
527 va_end (ap);
529 /* NOTREACHED */
530 real_abort ();
533 /* An internal consistency check has failed. We make no attempt to
534 continue. Note that unless there is debugging value to be had from
535 a more specific message, or some other good reason, you should use
536 abort () instead of calling this function directly. */
537 void
538 internal_error (const char *msgid, ...)
540 diagnostic_info diagnostic;
541 va_list ap;
543 va_start (ap, msgid);
544 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ICE);
545 report_diagnostic (&diagnostic);
546 va_end (ap);
548 /* NOTREACHED */
549 real_abort ();
552 /* Special case error functions. Most are implemented in terms of the
553 above, or should be. */
555 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
556 runs its second argument through gettext. */
557 void
558 fnotice (FILE *file, const char *msgid, ...)
560 va_list ap;
562 va_start (ap, msgid);
563 vfprintf (file, _(msgid), ap);
564 va_end (ap);
567 /* Inform the user that an error occurred while trying to report some
568 other error. This indicates catastrophic internal inconsistencies,
569 so give up now. But do try to flush out the previous error.
570 This mustn't use internal_error, that will cause infinite recursion. */
572 static void
573 error_recursion (diagnostic_context *context)
575 if (context->lock < 3)
576 pp_flush (context->printer);
578 fnotice (stderr,
579 "Internal compiler error: Error reporting routines re-entered.\n");
580 fnotice (stderr, bug_report_request, bug_report_url);
581 exit (FATAL_EXIT_CODE);
584 /* Report an internal compiler error in a friendly manner. This is
585 the function that gets called upon use of abort() in the source
586 code generally, thanks to a special macro. */
588 void
589 fancy_abort (const char *file, int line, const char *function)
591 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
594 /* Really call the system 'abort'. This has to go right at the end of
595 this file, so that there are no functions after it that call abort
596 and get the system abort instead of our macro. */
597 #undef abort
598 static void
599 real_abort (void)
601 abort ();