* configure.ac: Enable checking assembler dwarf2 support for bfin
[official-gcc/alias-decl.git] / gcc / diagnostic.c
blob7f4d8147340f8f31670314929d61930c29409328
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, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, 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"
43 #include "opts.h"
46 /* Prototypes. */
47 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
49 static void default_diagnostic_starter (diagnostic_context *,
50 diagnostic_info *);
51 static void default_diagnostic_finalizer (diagnostic_context *,
52 diagnostic_info *);
54 static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN;
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 /* Return a malloc'd string containing MSG formatted a la printf. The
66 caller is responsible for freeing the memory. */
67 static char *
68 build_message_string (const char *msg, ...)
70 char *str;
71 va_list ap;
73 va_start (ap, msg);
74 vasprintf (&str, msg, ap);
75 va_end (ap);
77 return str;
80 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
81 char *
82 file_name_as_prefix (const char *f)
84 return build_message_string ("%s: ", f);
89 /* Initialize the diagnostic message outputting machinery. */
90 void
91 diagnostic_initialize (diagnostic_context *context)
93 /* Allocate a basic pretty-printer. Clients will replace this a
94 much more elaborated pretty-printer if they wish. */
95 context->printer = xmalloc (sizeof (pretty_printer));
96 pp_construct (context->printer, NULL, 0);
97 /* By default, diagnostics are sent to stderr. */
98 context->printer->buffer->stream = stderr;
99 /* By default, we emit prefixes once per message. */
100 context->printer->wrapping.rule = DIAGNOSTICS_SHOW_PREFIX_ONCE;
102 memset (context->diagnostic_count, 0, sizeof context->diagnostic_count);
103 context->issue_warnings_are_errors_message = true;
104 context->warning_as_error_requested = false;
105 context->show_option_requested = false;
106 context->abort_on_error = false;
107 context->internal_error = NULL;
108 diagnostic_starter (context) = default_diagnostic_starter;
109 diagnostic_finalizer (context) = default_diagnostic_finalizer;
110 context->last_module = 0;
111 context->last_function = NULL;
112 context->lock = 0;
115 /* Initialize DIAGNOSTIC, where the message MSG has already been
116 translated. */
117 void
118 diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
119 va_list *args, location_t location,
120 diagnostic_t kind)
122 diagnostic->message.err_no = errno;
123 diagnostic->message.args_ptr = args;
124 diagnostic->message.format_spec = msg;
125 diagnostic->location = location;
126 diagnostic->kind = kind;
127 diagnostic->option_index = 0;
130 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
131 translated. */
132 void
133 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
134 va_list *args, location_t location,
135 diagnostic_t kind)
137 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind);
140 /* Return a malloc'd string describing a location. The caller is
141 responsible for freeing the memory. */
142 char *
143 diagnostic_build_prefix (diagnostic_info *diagnostic)
145 static const char *const diagnostic_kind_text[] = {
146 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
147 #include "diagnostic.def"
148 #undef DEFINE_DIAGNOSTIC_KIND
149 "must-not-happen"
151 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
152 expanded_location s = expand_location (diagnostic->location);
153 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
155 return
156 (s.file == NULL
157 ? build_message_string ("%s: %s", progname, text)
158 #ifdef USE_MAPPED_LOCATION
159 : flag_show_column && s.column != 0
160 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
161 #endif
162 : build_message_string ("%s:%d: %s", s.file, s.line, text));
165 /* Count a diagnostic. Return true if the message should be printed. */
166 static bool
167 diagnostic_count_diagnostic (diagnostic_context *context,
168 diagnostic_info *diagnostic)
170 diagnostic_t kind = diagnostic->kind;
171 switch (kind)
173 default:
174 gcc_unreachable ();
176 case DK_ICE:
177 #ifndef ENABLE_CHECKING
178 /* When not checking, ICEs are converted to fatal errors when an
179 error has already occurred. This is counteracted by
180 abort_on_error. */
181 if ((diagnostic_kind_count (context, DK_ERROR) > 0
182 || diagnostic_kind_count (context, DK_SORRY) > 0)
183 && !context->abort_on_error)
185 expanded_location s = expand_location (diagnostic->location);
186 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
187 s.file, s.line);
188 exit (FATAL_EXIT_CODE);
190 #endif
191 if (context->internal_error)
192 (*context->internal_error) (diagnostic->message.format_spec,
193 diagnostic->message.args_ptr);
194 /* Fall through. */
196 case DK_FATAL: case DK_SORRY:
197 case DK_ANACHRONISM: case DK_NOTE:
198 ++diagnostic_kind_count (context, kind);
199 break;
201 case DK_WARNING:
202 if (!diagnostic_report_warnings_p ())
203 return false;
205 if (!context->warning_as_error_requested)
207 ++diagnostic_kind_count (context, DK_WARNING);
208 break;
210 else if (context->issue_warnings_are_errors_message)
212 pp_verbatim (context->printer,
213 "%s: warnings being treated as errors\n", progname);
214 context->issue_warnings_are_errors_message = false;
217 /* And fall through. */
218 case DK_ERROR:
219 ++diagnostic_kind_count (context, DK_ERROR);
220 break;
223 return true;
226 /* Take any action which is expected to happen after the diagnostic
227 is written out. This function does not always return. */
228 static void
229 diagnostic_action_after_output (diagnostic_context *context,
230 diagnostic_info *diagnostic)
232 switch (diagnostic->kind)
234 case DK_DEBUG:
235 case DK_NOTE:
236 case DK_ANACHRONISM:
237 case DK_WARNING:
238 break;
240 case DK_ERROR:
241 case DK_SORRY:
242 if (context->abort_on_error)
243 real_abort ();
244 if (flag_fatal_errors)
246 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
247 exit (FATAL_EXIT_CODE);
249 break;
251 case DK_ICE:
252 if (context->abort_on_error)
253 real_abort ();
255 fnotice (stderr, "Please submit a full bug report,\n"
256 "with preprocessed source if appropriate.\n"
257 "See %s for instructions.\n", bug_report_url);
258 exit (FATAL_EXIT_CODE);
260 case DK_FATAL:
261 if (context->abort_on_error)
262 real_abort ();
264 fnotice (stderr, "compilation terminated.\n");
265 exit (FATAL_EXIT_CODE);
267 default:
268 gcc_unreachable ();
272 /* Prints out, if necessary, the name of the current function
273 that caused an error. Called from all error and warning functions. */
274 void
275 diagnostic_report_current_function (diagnostic_context *context)
277 diagnostic_report_current_module (context);
278 lang_hooks.print_error_function (context, input_filename);
281 void
282 diagnostic_report_current_module (diagnostic_context *context)
284 struct file_stack *p;
286 if (pp_needs_newline (context->printer))
288 pp_newline (context->printer);
289 pp_needs_newline (context->printer) = false;
292 p = input_file_stack;
293 if (p && diagnostic_last_module_changed (context))
295 expanded_location xloc = expand_location (p->location);
296 pp_verbatim (context->printer,
297 "In file included from %s:%d",
298 xloc.file, xloc.line);
299 while ((p = p->next) != NULL)
301 xloc = expand_location (p->location);
302 pp_verbatim (context->printer,
303 ",\n from %s:%d",
304 xloc.file, xloc.line);
306 pp_verbatim (context->printer, ":");
307 diagnostic_set_last_module (context);
308 pp_newline (context->printer);
312 static void
313 default_diagnostic_starter (diagnostic_context *context,
314 diagnostic_info *diagnostic)
316 diagnostic_report_current_function (context);
317 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
320 static void
321 default_diagnostic_finalizer (diagnostic_context *context,
322 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
324 pp_destroy_prefix (context->printer);
327 /* Report a diagnostic message (an error or a warning) as specified by
328 DC. This function is *the* subroutine in terms of which front-ends
329 should implement their specific diagnostic handling modules. The
330 front-end independent format specifiers are exactly those described
331 in the documentation of output_format. */
333 void
334 diagnostic_report_diagnostic (diagnostic_context *context,
335 diagnostic_info *diagnostic)
337 if (context->lock > 0)
339 /* If we're reporting an ICE in the middle of some other error,
340 try to flush out the previous error, then let this one
341 through. Don't do this more than once. */
342 if (diagnostic->kind == DK_ICE && context->lock == 1)
343 pp_flush (context->printer);
344 else
345 error_recursion (context);
348 if (diagnostic->option_index
349 && ! option_enabled (diagnostic->option_index))
350 return;
352 context->lock++;
354 if (diagnostic_count_diagnostic (context, diagnostic))
356 const char *saved_format_spec = diagnostic->message.format_spec;
358 if (context->show_option_requested && diagnostic->option_index)
359 diagnostic->message.format_spec
360 = ACONCAT ((diagnostic->message.format_spec,
361 " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
363 diagnostic->message.locus = &diagnostic->location;
364 pp_format (context->printer, &diagnostic->message);
365 (*diagnostic_starter (context)) (context, diagnostic);
366 pp_output_formatted_text (context->printer);
367 (*diagnostic_finalizer (context)) (context, diagnostic);
368 pp_flush (context->printer);
369 diagnostic_action_after_output (context, diagnostic);
370 diagnostic->message.format_spec = saved_format_spec;
373 context->lock--;
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'. */
381 const char *
382 trim_filename (const char *name)
384 static const char this_file[] = __FILE__;
385 const char *p = name, *q = this_file;
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] == '.' && IS_DIR_SEPARATOR (p[2]))
390 p += 3;
392 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
393 q += 3;
395 /* Now skip any parts the two filenames have in common. */
396 while (*p == *q && *p != 0 && *q != 0)
397 p++, q++;
399 /* Now go backwards until the previous directory separator. */
400 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
401 p--;
403 return p;
406 /* Standard error reporting routines in increasing order of severity.
407 All of these take arguments like printf. */
409 /* Text to be emitted verbatim to the error message stream; this
410 produces no prefix and disables line-wrapping. Use rarely. */
411 void
412 verbatim (const char *gmsgid, ...)
414 text_info text;
415 va_list ap;
417 va_start (ap, gmsgid);
418 text.err_no = errno;
419 text.args_ptr = &ap;
420 text.format_spec = _(gmsgid);
421 text.locus = NULL;
422 pp_format_verbatim (global_dc->printer, &text);
423 pp_flush (global_dc->printer);
424 va_end (ap);
427 /* An informative note. Use this for additional details on an error
428 message. */
429 void
430 inform (const char *gmsgid, ...)
432 diagnostic_info diagnostic;
433 va_list ap;
435 va_start (ap, gmsgid);
436 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE);
437 report_diagnostic (&diagnostic);
438 va_end (ap);
441 /* A warning. Use this for code which is correct according to the
442 relevant language specification but is likely to be buggy anyway. */
443 void
444 warning (int opt, 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, DK_WARNING);
451 diagnostic.option_index = opt;
453 report_diagnostic (&diagnostic);
454 va_end (ap);
457 void
458 warning0 (const char *gmsgid, ...)
460 diagnostic_info diagnostic;
461 va_list ap;
463 va_start (ap, gmsgid);
464 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
465 report_diagnostic (&diagnostic);
466 va_end (ap);
469 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
470 given on the command line, in which case it issues an error. Use
471 this for diagnostics required by the relevant language standard,
472 if you have chosen not to make them errors.
474 Note that these diagnostics are issued independent of the setting
475 of the -pedantic command-line switch. To get a warning enabled
476 only with that switch, write "if (pedantic) pedwarn (...);" */
477 void
478 pedwarn (const char *gmsgid, ...)
480 diagnostic_info diagnostic;
481 va_list ap;
483 va_start (ap, gmsgid);
484 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
485 pedantic_error_kind ());
486 report_diagnostic (&diagnostic);
487 va_end (ap);
490 /* A hard error: the code is definitely ill-formed, and an object file
491 will not be produced. */
492 void
493 error (const char *gmsgid, ...)
495 diagnostic_info diagnostic;
496 va_list ap;
498 va_start (ap, gmsgid);
499 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
500 report_diagnostic (&diagnostic);
501 va_end (ap);
504 /* "Sorry, not implemented." Use for a language feature which is
505 required by the relevant specification but not implemented by GCC.
506 An object file will not be produced. */
507 void
508 sorry (const char *gmsgid, ...)
510 diagnostic_info diagnostic;
511 va_list ap;
513 va_start (ap, gmsgid);
514 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
515 report_diagnostic (&diagnostic);
516 va_end (ap);
519 /* An error which is severe enough that we make no attempt to
520 continue. Do not use this for internal consistency checks; that's
521 internal_error. Use of this function should be rare. */
522 void
523 fatal_error (const char *gmsgid, ...)
525 diagnostic_info diagnostic;
526 va_list ap;
528 va_start (ap, gmsgid);
529 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
530 report_diagnostic (&diagnostic);
531 va_end (ap);
533 gcc_unreachable ();
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 *gmsgid, ...)
543 diagnostic_info diagnostic;
544 va_list ap;
546 va_start (ap, gmsgid);
547 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
548 report_diagnostic (&diagnostic);
549 va_end (ap);
551 gcc_unreachable ();
554 /* Special case error functions. Most are implemented in terms of the
555 above, or should be. */
557 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
558 runs its second argument through gettext. */
559 void
560 fnotice (FILE *file, const char *cmsgid, ...)
562 va_list ap;
564 va_start (ap, cmsgid);
565 vfprintf (file, _(cmsgid), ap);
566 va_end (ap);
569 /* Inform the user that an error occurred while trying to report some
570 other error. This indicates catastrophic internal inconsistencies,
571 so give up now. But do try to flush out the previous error.
572 This mustn't use internal_error, that will cause infinite recursion. */
574 static void
575 error_recursion (diagnostic_context *context)
577 diagnostic_info diagnostic;
579 if (context->lock < 3)
580 pp_flush (context->printer);
582 fnotice (stderr,
583 "Internal compiler error: Error reporting routines re-entered.\n");
585 /* Call diagnostic_action_after_output to get the "please submit a bug
586 report" message. It only looks at the kind field of diagnostic_info. */
587 diagnostic.kind = DK_ICE;
588 diagnostic_action_after_output (context, &diagnostic);
590 /* Do not use gcc_unreachable here; that goes through internal_error
591 and therefore would cause infinite recursion. */
592 real_abort ();
595 /* Report an internal compiler error in a friendly manner. This is
596 the function that gets called upon use of abort() in the source
597 code generally, thanks to a special macro. */
599 void
600 fancy_abort (const char *file, int line, const char *function)
602 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
605 /* Really call the system 'abort'. This has to go right at the end of
606 this file, so that there are no functions after it that call abort
607 and get the system abort instead of our macro. */
608 #undef abort
609 static void
610 real_abort (void)
612 abort ();