]> gcc.gnu.org Git - gcc.git/blob - gcc/diagnostic.c
pretty-print.h (PP_NL_ARGMAX): New.
[gcc.git] / gcc / diagnostic.c
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>
5
6 This file is part of GCC.
7
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.
12
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.
17
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. */
22
23
24 /* This file implements the language independent aspect of diagnostic
25 message module. */
26
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"
44
45
46 /* Prototypes. */
47 static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
48
49 static void default_diagnostic_starter (diagnostic_context *,
50 diagnostic_info *);
51 static void default_diagnostic_finalizer (diagnostic_context *,
52 diagnostic_info *);
53
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;
60
61 /* A diagnostic_context surrogate for stderr. */
62 static diagnostic_context global_diagnostic_context;
63 diagnostic_context *global_dc = &global_diagnostic_context;
64 \f
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, ...)
69 {
70 char *str;
71 va_list ap;
72
73 va_start (ap, msg);
74 vasprintf (&str, msg, ap);
75 va_end (ap);
76
77 return str;
78 }
79
80 /* Same as diagnostic_build_prefix, but only the source FILE is given. */
81 char *
82 file_name_as_prefix (const char *f)
83 {
84 return build_message_string ("%s: ", f);
85 }
86
87
88 \f
89 /* Initialize the diagnostic message outputting machinery. */
90 void
91 diagnostic_initialize (diagnostic_context *context)
92 {
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;
101
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;
113 }
114
115 void
116 diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid,
117 va_list *args, location_t location,
118 diagnostic_t kind)
119 {
120 diagnostic->message.err_no = errno;
121 diagnostic->message.args_ptr = args;
122 diagnostic->message.format_spec = _(gmsgid);
123 diagnostic->location = location;
124 diagnostic->kind = kind;
125 diagnostic->option_index = 0;
126 }
127
128 /* Return a malloc'd string describing a location. The caller is
129 responsible for freeing the memory. */
130 char *
131 diagnostic_build_prefix (diagnostic_info *diagnostic)
132 {
133 static const char *const diagnostic_kind_text[] = {
134 #define DEFINE_DIAGNOSTIC_KIND(K, T) (T),
135 #include "diagnostic.def"
136 #undef DEFINE_DIAGNOSTIC_KIND
137 "must-not-happen"
138 };
139 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
140 expanded_location s = expand_location (diagnostic->location);
141 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
142
143 return
144 (s.file == NULL
145 ? build_message_string ("%s: %s", progname, text)
146 #ifdef USE_MAPPED_LOCATION
147 : flag_show_column && s.column != 0
148 ? build_message_string ("%s:%d:%d: %s", s.file, s.line, s.column, text)
149 #endif
150 : build_message_string ("%s:%d: %s", s.file, s.line, text));
151 }
152
153 /* Count a diagnostic. Return true if the message should be printed. */
154 static bool
155 diagnostic_count_diagnostic (diagnostic_context *context,
156 diagnostic_info *diagnostic)
157 {
158 diagnostic_t kind = diagnostic->kind;
159 switch (kind)
160 {
161 default:
162 gcc_unreachable ();
163
164 case DK_ICE:
165 #ifndef ENABLE_CHECKING
166 /* When not checking, ICEs are converted to fatal errors when an
167 error has already occurred. This is counteracted by
168 abort_on_error. */
169 if ((diagnostic_kind_count (context, DK_ERROR) > 0
170 || diagnostic_kind_count (context, DK_SORRY) > 0)
171 && !context->abort_on_error)
172 {
173 expanded_location s = expand_location (diagnostic->location);
174 fnotice (stderr, "%s:%d: confused by earlier errors, bailing out\n",
175 s.file, s.line);
176 exit (FATAL_EXIT_CODE);
177 }
178 #endif
179 if (context->internal_error)
180 (*context->internal_error) (diagnostic->message.format_spec,
181 diagnostic->message.args_ptr);
182 /* Fall through. */
183
184 case DK_FATAL: case DK_SORRY:
185 case DK_ANACHRONISM: case DK_NOTE:
186 ++diagnostic_kind_count (context, kind);
187 break;
188
189 case DK_WARNING:
190 if (!diagnostic_report_warnings_p ())
191 return false;
192
193 if (!context->warning_as_error_requested)
194 {
195 ++diagnostic_kind_count (context, DK_WARNING);
196 break;
197 }
198 else if (context->issue_warnings_are_errors_message)
199 {
200 pp_verbatim (context->printer,
201 "%s: warnings being treated as errors\n", progname);
202 context->issue_warnings_are_errors_message = false;
203 }
204
205 /* And fall through. */
206 case DK_ERROR:
207 ++diagnostic_kind_count (context, DK_ERROR);
208 break;
209 }
210
211 return true;
212 }
213
214 /* Take any action which is expected to happen after the diagnostic
215 is written out. This function does not always return. */
216 static void
217 diagnostic_action_after_output (diagnostic_context *context,
218 diagnostic_info *diagnostic)
219 {
220 switch (diagnostic->kind)
221 {
222 case DK_DEBUG:
223 case DK_NOTE:
224 case DK_ANACHRONISM:
225 case DK_WARNING:
226 break;
227
228 case DK_ERROR:
229 case DK_SORRY:
230 if (context->abort_on_error)
231 real_abort ();
232 if (flag_fatal_errors)
233 {
234 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n");
235 exit (FATAL_EXIT_CODE);
236 }
237 break;
238
239 case DK_ICE:
240 if (context->abort_on_error)
241 real_abort ();
242
243 fnotice (stderr, "Please submit a full bug report,\n"
244 "with preprocessed source if appropriate.\n"
245 "See %s for instructions.\n", bug_report_url);
246 exit (FATAL_EXIT_CODE);
247
248 case DK_FATAL:
249 if (context->abort_on_error)
250 real_abort ();
251
252 fnotice (stderr, "compilation terminated.\n");
253 exit (FATAL_EXIT_CODE);
254
255 default:
256 gcc_unreachable ();
257 }
258 }
259
260 /* Prints out, if necessary, the name of the current function
261 that caused an error. Called from all error and warning functions. */
262 void
263 diagnostic_report_current_function (diagnostic_context *context)
264 {
265 diagnostic_report_current_module (context);
266 lang_hooks.print_error_function (context, input_filename);
267 }
268
269 void
270 diagnostic_report_current_module (diagnostic_context *context)
271 {
272 struct file_stack *p;
273
274 if (pp_needs_newline (context->printer))
275 {
276 pp_newline (context->printer);
277 pp_needs_newline (context->printer) = false;
278 }
279
280 p = input_file_stack;
281 if (p && diagnostic_last_module_changed (context))
282 {
283 expanded_location xloc = expand_location (p->location);
284 pp_verbatim (context->printer,
285 "In file included from %s:%d",
286 xloc.file, xloc.line);
287 while ((p = p->next) != NULL)
288 {
289 xloc = expand_location (p->location);
290 pp_verbatim (context->printer,
291 ",\n from %s:%d",
292 xloc.file, xloc.line);
293 }
294 pp_verbatim (context->printer, ":\n");
295 diagnostic_set_last_module (context);
296 }
297 }
298
299 static void
300 default_diagnostic_starter (diagnostic_context *context,
301 diagnostic_info *diagnostic)
302 {
303 diagnostic_report_current_function (context);
304 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic));
305 }
306
307 static void
308 default_diagnostic_finalizer (diagnostic_context *context,
309 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
310 {
311 pp_destroy_prefix (context->printer);
312 }
313
314 /* Report a diagnostic message (an error or a warning) as specified by
315 DC. This function is *the* subroutine in terms of which front-ends
316 should implement their specific diagnostic handling modules. The
317 front-end independent format specifiers are exactly those described
318 in the documentation of output_format. */
319
320 void
321 diagnostic_report_diagnostic (diagnostic_context *context,
322 diagnostic_info *diagnostic)
323 {
324 if (context->lock > 0)
325 {
326 /* If we're reporting an ICE in the middle of some other error,
327 try to flush out the previous error, then let this one
328 through. Don't do this more than once. */
329 if (diagnostic->kind == DK_ICE && context->lock == 1)
330 pp_flush (context->printer);
331 else
332 error_recursion (context);
333 }
334
335 if (diagnostic->option_index
336 && ! option_enabled (diagnostic->option_index))
337 return;
338
339 context->lock++;
340
341 if (diagnostic_count_diagnostic (context, diagnostic))
342 {
343 const char *saved_format_spec = diagnostic->message.format_spec;
344
345 if (context->show_option_requested && diagnostic->option_index)
346 diagnostic->message.format_spec
347 = ACONCAT ((diagnostic->message.format_spec,
348 " [", cl_options[diagnostic->option_index].opt_text, "]", NULL));
349
350 diagnostic->message.locus = &diagnostic->location;
351 pp_format (context->printer, &diagnostic->message);
352 (*diagnostic_starter (context)) (context, diagnostic);
353 pp_output_formatted_text (context->printer);
354 (*diagnostic_finalizer (context)) (context, diagnostic);
355 pp_flush (context->printer);
356 diagnostic_action_after_output (context, diagnostic);
357 diagnostic->message.format_spec = saved_format_spec;
358 }
359
360 context->lock--;
361 }
362
363 /* Given a partial pathname as input, return another pathname that
364 shares no directory elements with the pathname of __FILE__. This
365 is used by fancy_abort() to print `Internal compiler error in expr.c'
366 instead of `Internal compiler error in ../../GCC/gcc/expr.c'. */
367
368 const char *
369 trim_filename (const char *name)
370 {
371 static const char this_file[] = __FILE__;
372 const char *p = name, *q = this_file;
373
374 /* First skip any "../" in each filename. This allows us to give a proper
375 reference to a file in a subdirectory. */
376 while (p[0] == '.' && p[1] == '.' && IS_DIR_SEPARATOR (p[2]))
377 p += 3;
378
379 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2]))
380 q += 3;
381
382 /* Now skip any parts the two filenames have in common. */
383 while (*p == *q && *p != 0 && *q != 0)
384 p++, q++;
385
386 /* Now go backwards until the previous directory separator. */
387 while (p > name && !IS_DIR_SEPARATOR (p[-1]))
388 p--;
389
390 return p;
391 }
392 \f
393 /* Standard error reporting routines in increasing order of severity.
394 All of these take arguments like printf. */
395
396 /* Text to be emitted verbatim to the error message stream; this
397 produces no prefix and disables line-wrapping. Use rarely. */
398 void
399 verbatim (const char *gmsgid, ...)
400 {
401 text_info text;
402 va_list ap;
403
404 va_start (ap, gmsgid);
405 text.err_no = errno;
406 text.args_ptr = &ap;
407 text.format_spec = _(gmsgid);
408 text.locus = NULL;
409 pp_format_verbatim (global_dc->printer, &text);
410 pp_flush (global_dc->printer);
411 va_end (ap);
412 }
413
414 /* An informative note. Use this for additional details on an error
415 message. */
416 void
417 inform (const char *gmsgid, ...)
418 {
419 diagnostic_info diagnostic;
420 va_list ap;
421
422 va_start (ap, gmsgid);
423 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE);
424 report_diagnostic (&diagnostic);
425 va_end (ap);
426 }
427
428 /* A warning. Use this for code which is correct according to the
429 relevant language specification but is likely to be buggy anyway. */
430 void
431 warning (int opt, const char *gmsgid, ...)
432 {
433 diagnostic_info diagnostic;
434 va_list ap;
435
436 va_start (ap, gmsgid);
437 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
438 diagnostic.option_index = opt;
439
440 report_diagnostic (&diagnostic);
441 va_end (ap);
442 }
443
444 void
445 warning0 (const char *gmsgid, ...)
446 {
447 diagnostic_info diagnostic;
448 va_list ap;
449
450 va_start (ap, gmsgid);
451 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING);
452 report_diagnostic (&diagnostic);
453 va_end (ap);
454 }
455
456 /* A "pedantic" warning: issues a warning unless -pedantic-errors was
457 given on the command line, in which case it issues an error. Use
458 this for diagnostics required by the relevant language standard,
459 if you have chosen not to make them errors.
460
461 Note that these diagnostics are issued independent of the setting
462 of the -pedantic command-line switch. To get a warning enabled
463 only with that switch, write "if (pedantic) pedwarn (...);" */
464 void
465 pedwarn (const char *gmsgid, ...)
466 {
467 diagnostic_info diagnostic;
468 va_list ap;
469
470 va_start (ap, gmsgid);
471 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location,
472 pedantic_error_kind ());
473 report_diagnostic (&diagnostic);
474 va_end (ap);
475 }
476
477 /* A hard error: the code is definitely ill-formed, and an object file
478 will not be produced. */
479 void
480 error (const char *gmsgid, ...)
481 {
482 diagnostic_info diagnostic;
483 va_list ap;
484
485 va_start (ap, gmsgid);
486 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR);
487 report_diagnostic (&diagnostic);
488 va_end (ap);
489 }
490
491 /* "Sorry, not implemented." Use for a language feature which is
492 required by the relevant specification but not implemented by GCC.
493 An object file will not be produced. */
494 void
495 sorry (const char *gmsgid, ...)
496 {
497 diagnostic_info diagnostic;
498 va_list ap;
499
500 va_start (ap, gmsgid);
501 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY);
502 report_diagnostic (&diagnostic);
503 va_end (ap);
504 }
505
506 /* An error which is severe enough that we make no attempt to
507 continue. Do not use this for internal consistency checks; that's
508 internal_error. Use of this function should be rare. */
509 void
510 fatal_error (const char *gmsgid, ...)
511 {
512 diagnostic_info diagnostic;
513 va_list ap;
514
515 va_start (ap, gmsgid);
516 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL);
517 report_diagnostic (&diagnostic);
518 va_end (ap);
519
520 gcc_unreachable ();
521 }
522
523 /* An internal consistency check has failed. We make no attempt to
524 continue. Note that unless there is debugging value to be had from
525 a more specific message, or some other good reason, you should use
526 abort () instead of calling this function directly. */
527 void
528 internal_error (const char *gmsgid, ...)
529 {
530 diagnostic_info diagnostic;
531 va_list ap;
532
533 va_start (ap, gmsgid);
534 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE);
535 report_diagnostic (&diagnostic);
536 va_end (ap);
537
538 gcc_unreachable ();
539 }
540 \f
541 /* Special case error functions. Most are implemented in terms of the
542 above, or should be. */
543
544 /* Print a diagnostic MSGID on FILE. This is just fprintf, except it
545 runs its second argument through gettext. */
546 void
547 fnotice (FILE *file, const char *cmsgid, ...)
548 {
549 va_list ap;
550
551 va_start (ap, cmsgid);
552 vfprintf (file, _(cmsgid), ap);
553 va_end (ap);
554 }
555
556 /* Inform the user that an error occurred while trying to report some
557 other error. This indicates catastrophic internal inconsistencies,
558 so give up now. But do try to flush out the previous error.
559 This mustn't use internal_error, that will cause infinite recursion. */
560
561 static void
562 error_recursion (diagnostic_context *context)
563 {
564 diagnostic_info diagnostic;
565
566 if (context->lock < 3)
567 pp_flush (context->printer);
568
569 fnotice (stderr,
570 "Internal compiler error: Error reporting routines re-entered.\n");
571
572 /* Call diagnostic_action_after_output to get the "please submit a bug
573 report" message. It only looks at the kind field of diagnostic_info. */
574 diagnostic.kind = DK_ICE;
575 diagnostic_action_after_output (context, &diagnostic);
576
577 /* Do not use gcc_unreachable here; that goes through internal_error
578 and therefore would cause infinite recursion. */
579 real_abort ();
580 }
581
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. */
585
586 void
587 fancy_abort (const char *file, int line, const char *function)
588 {
589 internal_error ("in %s, at %s:%d", function, trim_filename (file), line);
590 }
591
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)
598 {
599 abort ();
600 }
This page took 0.061768 seconds and 5 git commands to generate.