Mark ChangeLog
[official-gcc.git] / gcc / diagnostic.c
blob9f166f194b266f20ee9aee0efcf56ba3f398d6ce
1 /* Language-independent diagnostic subroutines for the GNU Compiler Collection
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
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 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 /* Return a malloc'd string containing MSG formatted a la printf. The
65 caller is responsible for freeing the memory. */
66 static char *
67 build_message_string (const char *msg, ...)
69 char *str;
70 va_list ap;
72 va_start (ap, msg);
73 vasprintf (&str, msg, ap);
74 va_end (ap);
76 return str;
79 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
80 char *
81 file_name_as_prefix (const char *f)
83 return build_message_string ("%s: ", f);
88 /* Initialize the diagnostic message outputting machinery. */
89 void
90 diagnostic_initialize (diagnostic_context *context)
92 /* Allocate a basic pretty-printer. Clients will replace this a
93 much more elaborated pretty-printer if they wish. */
94 context->printer = xmalloc (sizeof (pretty_printer));
95 pp_construct (context->printer, NULL, 0);
96 /* By default, diagnostics are sent to stderr. */
97 context->printer->buffer->stream = stderr;
98 /* By default, we emit prefixes once per message. */
99 context->printer->prefixing_rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
101 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
102 context->issue_warnings_are_errors_message = true;
103 context->warning_as_error_requested = false;
104 context->abort_on_error = false;
105 context->internal_error = NULL;
106 diagnostic_starter (context) = default_diagnostic_starter;
107 diagnostic_finalizer (context) = default_diagnostic_finalizer;
108 context->last_module = 0;
109 context->last_function = NULL;
110 context->lock = 0;
113 /* Initialize DIAGNOSTIC, where the message MSG has already been
114 translated. */
115 void
116 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
117 va_list *args, location_t location,
118 diagnostic_t kind)
120 diagnostic->message.err_no = errno;
121 diagnostic->message.args_ptr = args;
122 diagnostic->message.format_spec = msg;
123 diagnostic->location = location;
124 diagnostic->kind = kind;
127 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
128 translated. */
129 void
130 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
131 va_list *args, location_t location,
132 diagnostic_t kind)
134 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
137 /* Return a malloc'd string describing a location. The caller is
138 responsible for freeing the memory. */
139 char *
140 diagnostic_build_prefix (diagnostic_info *diagnostic)
142 static const char *const diagnostic_kind_text[] = {
143 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
144 #include "diagnostic.def"
145 #undef DEFINE_DIAGNOSTIC_KIND
146 "must-not-happen"
148 expanded_location s = expand_location (diagnostic->location);
149 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
151 return s.file
152 ? build_message_string ("%s:%d: %s",
153 s.file, s.line,
154 _(diagnostic_kind_text[diagnostic->kind]))
155 : build_message_string ("%s: %s", progname,
156 _(diagnostic_kind_text[diagnostic->kind]));
159 /* Count a diagnostic. Return true if the message should be printed. */
160 static bool
161 diagnostic_count_diagnostic (diagnostic_context *context,
162 diagnostic_info *diagnostic)
164 diagnostic_t kind = diagnostic->kind;
165 switch (kind)
167 default:
168 gcc_unreachable ();
170 case DK_ICE:
171 #ifndef ENABLE_CHECKING
172 /* When not checking, ICEs are converted to fatal errors when an
173 error has already occurred. This is counteracted by
174 abort_on_error. */
175 if ((diagnostic_kind_count (context, DK_ERROR) > 0
176 || diagnostic_kind_count (context, DK_SORRY) > 0)
177 && !context->abort_on_error)
179 expanded_location s = expand_location (diagnostic->location);
180 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
181 s.file, s.line);
182 exit (FATAL_EXIT_CODE);
184 #endif
185 if (context->internal_error)
186 (*context->internal_error) (diagnostic->message.format_spec,
187 diagnostic->message.args_ptr);
188 /* Fall through. */
190 case DK_FATAL: case DK_SORRY:
191 case DK_ANACHRONISM: case DK_NOTE:
192 ++diagnostic_kind_count (context, kind);
193 break;
195 case DK_WARNING:
196 if (!diagnostic_report_warnings_p ())
197 return false;
199 if (!context->warning_as_error_requested)
201 ++diagnostic_kind_count (context, DK_WARNING);
202 break;
204 else if (context->issue_warnings_are_errors_message)
206 pp_verbatim (context->printer,
207 "%s: warnings being treated as errors\n", progname);
208 context->issue_warnings_are_errors_message = false;
211 /* And fall through. */
212 case DK_ERROR:
213 ++diagnostic_kind_count (context, DK_ERROR);
214 break;
217 return true;
220 /* Take any action which is expected to happen after the diagnostic
221 is written out. This function does not always return. */
222 static void
223 diagnostic_action_after_output (diagnostic_context *context,
224 diagnostic_info *diagnostic)
226 switch (diagnostic->kind)
228 case DK_DEBUG:
229 case DK_NOTE:
230 case DK_ANACHRONISM:
231 case DK_WARNING:
232 break;
234 case DK_ERROR:
235 case DK_SORRY:
236 if (context->abort_on_error)
237 real_abort ();
238 if (flag_fatal_errors)
240 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
241 exit (FATAL_EXIT_CODE);
243 break;
245 case DK_ICE:
246 if (context->abort_on_error)
247 real_abort ();
249 fnotice (stderr, "Please submit a full bug report,\n"
250 "with preprocessed source if appropriate.\n"
251 "See %s for instructions.\n", bug_report_url);
252 exit (FATAL_EXIT_CODE);
254 case DK_FATAL:
255 if (context->abort_on_error)
256 real_abort ();
258 fnotice (stderr, "compilation terminated.\n");
259 exit (FATAL_EXIT_CODE);
261 default:
262 gcc_unreachable ();
266 /* Prints out, if necessary, the name of the current function
267 that caused an error. Called from all error and warning functions. */
268 void
269 diagnostic_report_current_function (diagnostic_context *context)
271 diagnostic_report_current_module (context);
272 lang_hooks.print_error_function (context, input_filename);
275 void
276 diagnostic_report_current_module (diagnostic_context *context)
278 struct file_stack *p;
280 if (pp_needs_newline (context->printer))
282 pp_newline (context->printer);
283 pp_needs_newline (context->printer) = false;
286 p = input_file_stack;
287 if (p && diagnostic_last_module_changed (context))
289 expanded_location xloc = expand_location (p->location);
290 pp_verbatim (context->printer,
291 "In file included from %s:%d",
292 xloc.file, xloc.line);
293 while ((p = p->next) != NULL)
295 xloc = expand_location (p->location);
296 pp_verbatim (context->printer,
297 ",\n from %s:%d",
298 xloc.file, xloc.line);
300 pp_verbatim (context->printer, ":\n");
301 diagnostic_set_last_module (context);
305 static void
306 default_diagnostic_starter (diagnostic_context *context,
307 diagnostic_info *diagnostic)
309 diagnostic_report_current_function (context);
310 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
313 static void
314 default_diagnostic_finalizer (diagnostic_context *context,
315 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
317 pp_destroy_prefix (context->printer);
320 /* Report a diagnostic message (an error or a warning) as specified by
321 DC. This function is *the* subroutine in terms of which front-ends
322 should implement their specific diagnostic handling modules. The
323 front-end independent format specifiers are exactly those described
324 in the documentation of output_format. */
326 void
327 diagnostic_report_diagnostic (diagnostic_context *context,
328 diagnostic_info *diagnostic)
330 if (context->lock > 0)
332 /* If we're reporting an ICE in the middle of some other error,
333 try to flush out the previous error, then let this one
334 through. Don't do this more than once. */
335 if (diagnostic->kind == DK_ICE && context->lock == 1)
336 pp_flush (context->printer);
337 else
338 error_recursion (context);
341 context->lock++;
343 if (diagnostic_count_diagnostic (context, diagnostic))
345 pp_prepare_to_format (context->printer, &diagnostic->message,
346 &diagnostic->location);
347 (*diagnostic_starter (context)) (context, diagnostic);
348 pp_format_text (context->printer, &diagnostic->message);
349 (*diagnostic_finalizer (context)) (context, diagnostic);
350 pp_flush (context->printer);
351 diagnostic_action_after_output (context, diagnostic);
354 context->lock--;
357 /* Given a partial pathname as input, return another pathname that
358 shares no directory elements with the pathname of __FILE__. This
359 is used by fancy_abort() to print `Internal compiler error in expr.c'
360 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
362 const char *
363 trim_filename (const char *name)
365 static const char this_file[] = __FILE__;
366 const char *p = name, *q = this_file;
368 /* First skip any "../" in each filename. This allows us to give a proper
369 reference to a file in a subdirectory. */
370 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
371 p += 3;
373 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
374 q += 3;
376 /* Now skip any parts the two filenames have in common. */
377 while (*p == *q && *p != 0 && *q != 0)
378 p++, q++;
380 /* Now go backwards until the previous directory separator. */
381 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
382 p--;
384 return p;
387 /* Standard error reporting routines in increasing order of severity.
388 All of these take arguments like printf. */
390 /* Text to be emitted verbatim to the error message stream; this
391 produces no prefix and disables line-wrapping. Use rarely. */
392 void
393 verbatim (const char *gmsgid, ...)
395 text_info text;
396 va_list ap;
398 va_start (ap, gmsgid);
399 text.err_no = errno;
400 text.args_ptr = &ap;
401 text.format_spec = _(gmsgid);
402 pp_format_verbatim (global_dc->printer, &text);
403 pp_flush (global_dc->printer);
404 va_end (ap);
407 /* An informative note. Use this for additional details on an error
408 message. */
409 void
410 inform (const char *gmsgid, ...)
412 diagnostic_info diagnostic;
413 va_list ap;
415 va_start (ap, gmsgid);
416 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE);
417 report_diagnostic (&diagnostic);
418 va_end (ap);
421 /* A warning. Use this for code which is correct according to the
422 relevant language specification but is likely to be buggy anyway. */
423 void
424 warning (const char *gmsgid, ...)
426 diagnostic_info diagnostic;
427 va_list ap;
429 va_start (ap, gmsgid);
430 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
431 report_diagnostic (&diagnostic);
432 va_end (ap);
435 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
436 given on the command line, in which case it issues an error. Use
437 this for diagnostics required by the relevant language standard,
438 if you have chosen not to make them errors.
440 Note that these diagnostics are issued independent of the setting
441 of the -pedantic command-line switch. To get a warning enabled
442 only with that switch, write "if (pedantic) pedwarn (...);" */
443 void
444 pedwarn (const char *gmsgid, ...)
446 diagnostic_info diagnostic;
447 va_list ap;
449 va_start (ap, gmsgid);
450 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
451 pedantic_error_kind ());
452 report_diagnostic (&diagnostic);
453 va_end (ap);
456 /* A hard error: the code is definitely ill-formed, and an object file
457 will not be produced. */
458 void
459 error (const char *gmsgid, ...)
461 diagnostic_info diagnostic;
462 va_list ap;
464 va_start (ap, gmsgid);
465 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
466 report_diagnostic (&diagnostic);
467 va_end (ap);
470 /* "Sorry, not implemented." Use for a language feature which is
471 required by the relevant specification but not implemented by GCC.
472 An object file will not be produced. */
473 void
474 sorry (const char *gmsgid, ...)
476 diagnostic_info diagnostic;
477 va_list ap;
479 va_start (ap, gmsgid);
480 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
481 report_diagnostic (&diagnostic);
482 va_end (ap);
485 /* An error which is severe enough that we make no attempt to
486 continue. Do not use this for internal consistency checks; that's
487 internal_error. Use of this function should be rare. */
488 void
489 fatal_error (const char *gmsgid, ...)
491 diagnostic_info diagnostic;
492 va_list ap;
494 va_start (ap, gmsgid);
495 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
496 report_diagnostic (&diagnostic);
497 va_end (ap);
499 gcc_unreachable ();
502 /* An internal consistency check has failed. We make no attempt to
503 continue. Note that unless there is debugging value to be had from
504 a more specific message, or some other good reason, you should use
505 abort () instead of calling this function directly. */
506 void
507 internal_error (const char *gmsgid, ...)
509 diagnostic_info diagnostic;
510 va_list ap;
512 va_start (ap, gmsgid);
513 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
514 report_diagnostic (&diagnostic);
515 va_end (ap);
517 gcc_unreachable ();
520 /* Special case error functions. Most are implemented in terms of the
521 above, or should be. */
523 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
524 runs its second argument through gettext. */
525 void
526 fnotice (FILE *file, const char *cmsgid, ...)
528 va_list ap;
530 va_start (ap, cmsgid);
531 vfprintf (file, _(cmsgid), ap);
532 va_end (ap);
535 /* Inform the user that an error occurred while trying to report some
536 other error. This indicates catastrophic internal inconsistencies,
537 so give up now. But do try to flush out the previous error.
538 This mustn't use internal_error, that will cause infinite recursion. */
540 static void
541 error_recursion (diagnostic_context *context)
543 diagnostic_info diagnostic;
545 if (context->lock < 3)
546 pp_flush (context->printer);
548 fnotice (stderr,
549 "Internal compiler error: Error reporting routines re-entered.\n");
551 /* Call diagnostic_action_after_output to get the "please submit a bug
552 report" message. It only looks at the kind field of diagnostic_info. */
553 diagnostic.kind = DK_ICE;
554 diagnostic_action_after_output (context, &diagnostic);
556 /* Do not use gcc_unreachable here; that goes through internal_error
557 and therefore would cause infinite recursion. */
558 real_abort ();
561 /* Report an internal compiler error in a friendly manner. This is
562 the function that gets called upon use of abort() in the source
563 code generally, thanks to a special macro. */
565 void
566 fancy_abort (const char *file, int line, const char *function)
568 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
571 /* Really call the system 'abort'. This has to go right at the end of
572 this file, so that there are no functions after it that call abort
573 and get the system abort instead of our macro. */
574 #undef abort
575 static void
576 real_abort (void)
578 abort ();