* configure.in: Fix sed invocation for GFORTRAN_FOR_TARGET.
[official-gcc.git] / gcc / diagnostic.c
blob16afddc1b4befcbad83311e06b447e21236da79d
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"
70 int flag_fatal_errors = 0;
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->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;
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 if (diagnostic->kind >= DK_LAST_DIAGNOSTIC_KIND)
177 abort();
179 return diagnostic->location.file
180 ? build_message_string ("%s:%d: %s",
181 diagnostic->location.file,
182 diagnostic->location.line,
183 _(diagnostic_kind_text[diagnostic->kind]))
184 : build_message_string ("%s: %s", progname,
185 _(diagnostic_kind_text[diagnostic->kind]));
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)
193 diagnostic_t kind = diagnostic->kind;
194 switch (kind)
196 default:
197 abort();
198 break;
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)
209 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
210 diagnostic->location.file, diagnostic->location.line);
211 exit (FATAL_EXIT_CODE);
213 #endif
214 if (context->internal_error)
215 (*context->internal_error) (diagnostic->message.format_spec,
216 diagnostic->message.args_ptr);
217 /* Fall through. */
219 case DK_FATAL: case DK_SORRY:
220 case DK_ANACHRONISM: case DK_NOTE:
221 ++diagnostic_kind_count (context, kind);
222 break;
224 case DK_WARNING:
225 if (!diagnostic_report_warnings_p ())
226 return false;
228 if (!warnings_are_errors)
230 ++diagnostic_kind_count (context, DK_WARNING);
231 break;
234 if (context->warnings_are_errors_message)
236 pp_verbatim (context->printer,
237 "%s: warnings being treated as errors\n", progname);
238 context->warnings_are_errors_message = false;
241 /* And fall through. */
242 case DK_ERROR:
243 ++diagnostic_kind_count (context, DK_ERROR);
244 break;
247 return true;
250 /* Take any action which is expected to happen after the diagnostic
251 is written out. This function does not always return. */
252 static void
253 diagnostic_action_after_output (diagnostic_context *context,
254 diagnostic_info *diagnostic)
256 switch (diagnostic->kind)
258 case DK_DEBUG:
259 case DK_NOTE:
260 case DK_ANACHRONISM:
261 case DK_WARNING:
262 break;
264 case DK_ERROR:
265 case DK_SORRY:
266 if (context->abort_on_error)
267 real_abort ();
268 if (flag_fatal_errors)
270 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
271 exit (FATAL_EXIT_CODE);
273 break;
275 case DK_ICE:
276 if (context->abort_on_error)
277 real_abort ();
279 fnotice (stderr, bug_report_request, bug_report_url);
280 exit (FATAL_EXIT_CODE);
282 case DK_FATAL:
283 if (context->abort_on_error)
284 real_abort ();
286 fnotice (stderr, "compilation terminated.\n");
287 exit (FATAL_EXIT_CODE);
289 default:
290 real_abort ();
294 /* Prints out, if necessary, the name of the current function
295 that caused an error. Called from all error and warning functions.
296 We ignore the FILE parameter, as it cannot be relied upon. */
298 void
299 diagnostic_report_current_function (diagnostic_context *context)
301 diagnostic_report_current_module (context);
302 lang_hooks.print_error_function (context, input_filename);
305 void
306 diagnostic_report_current_module (diagnostic_context *context)
308 struct file_stack *p;
310 if (pp_needs_newline (context->printer))
312 pp_newline (context->printer);
313 pp_needs_newline (context->printer) = false;
316 if (input_file_stack && diagnostic_last_module_changed (context))
318 p = input_file_stack;
319 pp_verbatim (context->printer,
320 "In file included from %s:%d",
321 p->location.file, p->location.line);
322 while ((p = p->next) != NULL)
323 pp_verbatim (context->printer,
324 ",\n from %s:%d",
325 p->location.file, p->location.line);
326 pp_verbatim (context->printer, ":\n");
327 diagnostic_set_last_module (context);
331 static void
332 default_diagnostic_starter (diagnostic_context *context,
333 diagnostic_info *diagnostic)
335 diagnostic_report_current_function (context);
336 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
339 static void
340 default_diagnostic_finalizer (diagnostic_context *context,
341 diagnostic_info *diagnostic __attribute__((unused)))
343 pp_destroy_prefix (context->printer);
346 /* Report a diagnostic message (an error or a warning) as specified by
347 DC. This function is *the* subroutine in terms of which front-ends
348 should implement their specific diagnostic handling modules. The
349 front-end independent format specifiers are exactly those described
350 in the documentation of output_format. */
352 void
353 diagnostic_report_diagnostic (diagnostic_context *context,
354 diagnostic_info *diagnostic)
356 if (context->lock++ && diagnostic->kind < DK_SORRY)
357 error_recursion (context);
359 if (diagnostic_count_diagnostic (context, diagnostic))
361 (*diagnostic_starter (context)) (context, diagnostic);
362 pp_format_text (context->printer, &diagnostic->message);
363 (*diagnostic_finalizer (context)) (context, diagnostic);
364 pp_flush (context->printer);
365 diagnostic_action_after_output (context, diagnostic);
368 context->lock--;
371 /* Given a partial pathname as input, return another pathname that
372 shares no directory elements with the pathname of __FILE__. This
373 is used by fancy_abort() to print `Internal compiler error in expr.c'
374 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
376 const char *
377 trim_filename (const char *name)
379 static const char this_file[] = __FILE__;
380 const char *p = name, *q = this_file;
382 /* First skip any "../" in each filename. This allows us to give a proper
383 reference to a file in a subdirectory. */
384 while (p[0] == '.' && p[1] == '.'
385 && (p[2] == DIR_SEPARATOR
386 #ifdef DIR_SEPARATOR_2
387 || p[2] == DIR_SEPARATOR_2
388 #endif
390 p += 3;
392 while (q[0] == '.' && q[1] == '.'
393 && (q[2] == DIR_SEPARATOR
394 #ifdef DIR_SEPARATOR_2
395 || p[2] == DIR_SEPARATOR_2
396 #endif
398 q += 3;
400 /* Now skip any parts the two filenames have in common. */
401 while (*p == *q && *p != 0 && *q != 0)
402 p++, q++;
404 /* Now go backwards until the previous directory separator. */
405 while (p > name && p[-1] != DIR_SEPARATOR
406 #ifdef DIR_SEPARATOR_2
407 && p[-1] != DIR_SEPARATOR_2
408 #endif
410 p--;
412 return p;
415 /* Standard error reporting routines in increasing order of severity.
416 All of these take arguments like printf. */
418 /* Text to be emitted verbatim to the error message stream; this
419 produces no prefix and disables line-wrapping. Use rarely. */
420 void
421 verbatim (const char *msgid, ...)
423 text_info text;
424 va_list ap;
426 va_start (ap, msgid);
427 text.err_no = errno;
428 text.args_ptr = &ap;
429 text.format_spec = _(msgid);
430 pp_format_verbatim (global_dc->printer, &text);
431 pp_flush (global_dc->printer);
432 va_end (ap);
435 /* An informative note. Use this for additional details on an error
436 message. */
437 void
438 inform (const char *msgid, ...)
440 diagnostic_info diagnostic;
441 va_list ap;
443 va_start (ap, msgid);
444 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_NOTE);
445 report_diagnostic (&diagnostic);
446 va_end (ap);
449 /* A warning. Use this for code which is correct according to the
450 relevant language specification but is likely to be buggy anyway. */
451 void
452 warning (const char *msgid, ...)
454 diagnostic_info diagnostic;
455 va_list ap;
457 va_start (ap, msgid);
458 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_WARNING);
459 report_diagnostic (&diagnostic);
460 va_end (ap);
463 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
464 given on the command line, in which case it issues an error. Use
465 this for diagnostics required by the relevant language standard,
466 if you have chosen not to make them errors.
468 Note that these diagnostics are issued independent of the setting
469 of the -pedantic command-line switch. To get a warning enabled
470 only with that switch, write "if (pedantic) pedwarn (...);" */
471 void
472 pedwarn (const char *msgid, ...)
474 diagnostic_info diagnostic;
475 va_list ap;
477 va_start (ap, msgid);
478 diagnostic_set_info (&diagnostic, msgid, &ap, input_location,
479 pedantic_error_kind ());
480 report_diagnostic (&diagnostic);
481 va_end (ap);
484 /* A hard error: the code is definitely ill-formed, and an object file
485 will not be produced. */
486 void
487 error (const char *msgid, ...)
489 diagnostic_info diagnostic;
490 va_list ap;
492 va_start (ap, msgid);
493 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ERROR);
494 report_diagnostic (&diagnostic);
495 va_end (ap);
498 /* "Sorry, not implemented." Use for a language feature which is
499 required by the relevant specification but not implemented by GCC.
500 An object file will not be produced. */
501 void
502 sorry (const char *msgid, ...)
504 diagnostic_info diagnostic;
505 va_list ap;
507 va_start (ap, msgid);
508 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_SORRY);
509 report_diagnostic (&diagnostic);
510 va_end (ap);
513 /* An error which is severe enough that we make no attempt to
514 continue. Do not use this for internal consistency checks; that's
515 internal_error. Use of this function should be rare. */
516 void
517 fatal_error (const char *msgid, ...)
519 diagnostic_info diagnostic;
520 va_list ap;
522 va_start (ap, msgid);
523 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_FATAL);
524 report_diagnostic (&diagnostic);
525 va_end (ap);
527 /* NOTREACHED */
528 real_abort ();
531 /* An internal consistency check has failed. We make no attempt to
532 continue. Note that unless there is debugging value to be had from
533 a more specific message, or some other good reason, you should use
534 abort () instead of calling this function directly. */
535 void
536 internal_error (const char *msgid, ...)
538 diagnostic_info diagnostic;
539 va_list ap;
541 va_start (ap, msgid);
542 diagnostic_set_info (&diagnostic, msgid, &ap, input_location, DK_ICE);
543 report_diagnostic (&diagnostic);
544 va_end (ap);
546 /* NOTREACHED */
547 real_abort ();
550 /* Special case error functions. Most are implemented in terms of the
551 above, or should be. */
553 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
554 runs its second argument through gettext. */
555 void
556 fnotice (FILE *file, const char *msgid, ...)
558 va_list ap;
560 va_start (ap, msgid);
561 vfprintf (file, _(msgid), ap);
562 va_end (ap);
565 /* Inform the user that an error occurred while trying to report some
566 other error. This indicates catastrophic internal inconsistencies,
567 so give up now. But do try to flush out the previous error.
568 This mustn't use internal_error, that will cause infinite recursion. */
570 static void
571 error_recursion (diagnostic_context *context)
573 if (context->lock < 3)
574 pp_flush (context->printer);
576 fnotice (stderr,
577 "Internal compiler error: Error reporting routines re-entered.\n");
578 fnotice (stderr, bug_report_request, bug_report_url);
579 exit (FATAL_EXIT_CODE);
582 /* Report an internal compiler error in a friendly manner. This is
583 the function that gets called upon use of abort() in the source
584 code generally, thanks to a special macro. */
586 void
587 fancy_abort (const char *file, int line, const char *function)
589 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
592 /* Really call the system 'abort'. This has to go right at the end of
593 this file, so that there are no functions after it that call abort
594 and get the system abort instead of our macro. */
595 #undef abort
596 static void
597 real_abort (void)
599 abort ();