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 6This file is part of GCC. 7 8GCC is free software; you can redistribute it and/or modify it under 9the terms of the GNU General Public License as published by the Free 10Software Foundation; either version 2, or (at your option) any later 11version. 12 13GCC is distributed in the hope that it will be useful, but WITHOUT ANY 14WARRANTY; without even the implied warranty of MERCHANTABILITY or 15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16for more details. 17 18You should have received a copy of the GNU General Public License 19along with GCC; see the file COPYING. If not, write to the Free 20Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 2102110-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. */ 47static char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1; 48 49static void default_diagnostic_starter (diagnostic_context *, 50 diagnostic_info *); 51static void default_diagnostic_finalizer (diagnostic_context *, 52 diagnostic_info *); 53 54static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN; 55static bool diagnostic_count_diagnostic (diagnostic_context *, 56 diagnostic_info *); 57static void diagnostic_action_after_output (diagnostic_context *, 58 diagnostic_info *); 59static void real_abort (void) ATTRIBUTE_NORETURN; 60 61/* A diagnostic_context surrogate for stderr. */ 62static diagnostic_context global_diagnostic_context; 63diagnostic_context *global_dc = &global_diagnostic_context; 64 65/* Return a malloc'd string containing MSG formatted a la printf. The 66 caller is responsible for freeing the memory. */ 67static char * 68build_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. */ 81char * 82file_name_as_prefix (const char *f) 83{ 84 return build_message_string ("%s: ", f); 85} 86 87 88 89/* Initialize the diagnostic message outputting machinery. */ 90void 91diagnostic_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/* Initialize DIAGNOSTIC, where the message MSG has already been 116 translated. */ 117void 118diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg, 119 va_list *args, location_t location, 120 diagnostic_t kind) 121{ 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; 128} 129 130/* Initialize DIAGNOSTIC, where the message GMSGID has not yet been 131 translated. */ 132void 133diagnostic_set_info (diagnostic_info *diagnostic, const char *gmsgid, 134 va_list *args, location_t location, 135 diagnostic_t kind) 136{ 137 diagnostic_set_info_translated (diagnostic, _(gmsgid), args, location, kind); 138} 139 140/* Return a malloc'd string describing a location. The caller is 141 responsible for freeing the memory. */ 142char * 143diagnostic_build_prefix (diagnostic_info *diagnostic) 144{ 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" 150 }; 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); 154 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)); 163} 164 165/* Count a diagnostic. Return true if the message should be printed. */ 166static bool 167diagnostic_count_diagnostic (diagnostic_context *context, 168 diagnostic_info *diagnostic) 169{ 170 diagnostic_t kind = diagnostic->kind; 171 switch (kind) 172 { 173 default: 174 gcc_unreachable (); 175 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) 184 { 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); 189 } 190#endif 191 if (context->internal_error) 192 (*context->internal_error) (diagnostic->message.format_spec, 193 diagnostic->message.args_ptr); 194 /* Fall through. */ 195 196 case DK_FATAL: case DK_SORRY: 197 case DK_ANACHRONISM: case DK_NOTE: 198 ++diagnostic_kind_count (context, kind); 199 break; 200 201 case DK_WARNING: 202 if (!diagnostic_report_warnings_p ()) 203 return false; 204 205 if (!context->warning_as_error_requested) 206 { 207 ++diagnostic_kind_count (context, DK_WARNING); 208 break; 209 } 210 else if (context->issue_warnings_are_errors_message) 211 { 212 pp_verbatim (context->printer, 213 "%s: warnings being treated as errors\n", progname); 214 context->issue_warnings_are_errors_message = false; 215 } 216 217 /* And fall through. */ 218 case DK_ERROR: 219 ++diagnostic_kind_count (context, DK_ERROR); 220 break; 221 } 222 223 return true; 224} 225 226/* Take any action which is expected to happen after the diagnostic 227 is written out. This function does not always return. */ 228static void 229diagnostic_action_after_output (diagnostic_context *context, 230 diagnostic_info *diagnostic) 231{ 232 switch (diagnostic->kind) 233 { 234 case DK_DEBUG: 235 case DK_NOTE: 236 case DK_ANACHRONISM: 237 case DK_WARNING: 238 break; 239 240 case DK_ERROR: 241 case DK_SORRY: 242 if (context->abort_on_error) 243 real_abort (); 244 if (flag_fatal_errors) 245 { 246 fnotice (stderr, "compilation terminated due to -Wfatal-errors.\n"); 247 exit (FATAL_EXIT_CODE); 248 } 249 break; 250 251 case DK_ICE: 252 if (context->abort_on_error) 253 real_abort (); 254 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); 259 260 case DK_FATAL: 261 if (context->abort_on_error) 262 real_abort (); 263 264 fnotice (stderr, "compilation terminated.\n"); 265 exit (FATAL_EXIT_CODE); 266 267 default: 268 gcc_unreachable (); 269 } 270} 271 272/* Prints out, if necessary, the name of the current function 273 that caused an error. Called from all error and warning functions. */ 274void 275diagnostic_report_current_function (diagnostic_context *context) 276{ 277 diagnostic_report_current_module (context); 278 lang_hooks.print_error_function (context, input_filename); 279} 280 281void 282diagnostic_report_current_module (diagnostic_context *context) 283{ 284 struct file_stack *p; 285 286 if (pp_needs_newline (context->printer)) 287 { 288 pp_newline (context->printer); 289 pp_needs_newline (context->printer) = false; 290 } 291 292 p = input_file_stack; 293 if (p && diagnostic_last_module_changed (context)) 294 { 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) 300 { 301 xloc = expand_location (p->location); 302 pp_verbatim (context->printer, 303 ",\n from %s:%d", 304 xloc.file, xloc.line); 305 } 306 pp_verbatim (context->printer, ":"); 307 diagnostic_set_last_module (context); 308 pp_newline (context->printer); 309 } 310} 311 312static void 313default_diagnostic_starter (diagnostic_context *context, 314 diagnostic_info *diagnostic) 315{ 316 diagnostic_report_current_function (context); 317 pp_set_prefix (context->printer, diagnostic_build_prefix (diagnostic)); 318} 319 320static void 321default_diagnostic_finalizer (diagnostic_context *context, 322 diagnostic_info *diagnostic ATTRIBUTE_UNUSED) 323{ 324 pp_destroy_prefix (context->printer); 325} 326 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. */ 332 333void 334diagnostic_report_diagnostic (diagnostic_context *context, 335 diagnostic_info *diagnostic) 336{ 337 if (context->lock > 0) 338 { 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); 346 } 347 348 if (diagnostic->option_index 349 && ! option_enabled (diagnostic->option_index)) 350 return; 351 352 context->lock++; 353 354 if (diagnostic_count_diagnostic (context, diagnostic)) 355 { 356 const char *saved_format_spec = diagnostic->message.format_spec; 357 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)); 362 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; 371 } 372 373 context->lock--; 374} 375 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'. */ 380 381const char * 382trim_filename (const char *name) 383{ 384 static const char this_file[] = __FILE__; 385 const char *p = name, *q = this_file; 386 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; 391 392 while (q[0] == '.' && q[1] == '.' && IS_DIR_SEPARATOR (q[2])) 393 q += 3; 394 395 /* Now skip any parts the two filenames have in common. */ 396 while (*p == *q && *p != 0 && *q != 0) 397 p++, q++; 398 399 /* Now go backwards until the previous directory separator. */ 400 while (p > name && !IS_DIR_SEPARATOR (p[-1])) 401 p--; 402 403 return p; 404} 405 406/* Standard error reporting routines in increasing order of severity. 407 All of these take arguments like printf. */ 408 409/* Text to be emitted verbatim to the error message stream; this 410 produces no prefix and disables line-wrapping. Use rarely. */ 411void 412verbatim (const char *gmsgid, ...) 413{ 414 text_info text; 415 va_list ap; 416 417 va_start (ap, gmsgid); 418 text.err_no = errno; 419 text.args_ptr = ≈ 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); 425} 426 427/* An informative note. Use this for additional details on an error 428 message. */ 429void 430inform (const char *gmsgid, ...) 431{ 432 diagnostic_info diagnostic; 433 va_list ap; 434 435 va_start (ap, gmsgid); 436 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_NOTE); 437 report_diagnostic (&diagnostic); 438 va_end (ap); 439} 440 441/* A warning. Use this for code which is correct according to the 442 relevant language specification but is likely to be buggy anyway. */ 443void 444warning (int opt, const char *gmsgid, ...) 445{ 446 diagnostic_info diagnostic; 447 va_list ap; 448 449 va_start (ap, gmsgid); 450 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING); 451 diagnostic.option_index = opt; 452 453 report_diagnostic (&diagnostic); 454 va_end (ap); 455} 456 457void 458warning0 (const char *gmsgid, ...) 459{ 460 diagnostic_info diagnostic; 461 va_list ap; 462 463 va_start (ap, gmsgid); 464 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_WARNING); 465 report_diagnostic (&diagnostic); 466 va_end (ap); 467} 468 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. 473 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 (...);" */ 477void 478pedwarn (const char *gmsgid, ...) 479{ 480 diagnostic_info diagnostic; 481 va_list ap; 482 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); 488} 489 490/* A hard error: the code is definitely ill-formed, and an object file 491 will not be produced. */ 492void 493error (const char *gmsgid, ...) 494{ 495 diagnostic_info diagnostic; 496 va_list ap; 497 498 va_start (ap, gmsgid); 499 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ERROR); 500 report_diagnostic (&diagnostic); 501 va_end (ap); 502} 503 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. */ 507void 508sorry (const char *gmsgid, ...) 509{ 510 diagnostic_info diagnostic; 511 va_list ap; 512 513 va_start (ap, gmsgid); 514 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_SORRY); 515 report_diagnostic (&diagnostic); 516 va_end (ap); 517} 518 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. */ 522void 523fatal_error (const char *gmsgid, ...) 524{ 525 diagnostic_info diagnostic; 526 va_list ap; 527 528 va_start (ap, gmsgid); 529 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_FATAL); 530 report_diagnostic (&diagnostic); 531 va_end (ap); 532 533 gcc_unreachable (); 534} 535 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. */ 540void 541internal_error (const char *gmsgid, ...) 542{ 543 diagnostic_info diagnostic; 544 va_list ap; 545 546 va_start (ap, gmsgid); 547 diagnostic_set_info (&diagnostic, gmsgid, &ap, input_location, DK_ICE); 548 report_diagnostic (&diagnostic); 549 va_end (ap); 550 551 gcc_unreachable (); 552} 553 554/* Special case error functions. Most are implemented in terms of the 555 above, or should be. */ 556 557/* Print a diagnostic MSGID on FILE. This is just fprintf, except it 558 runs its second argument through gettext. */ 559void 560fnotice (FILE *file, const char *cmsgid, ...) 561{ 562 va_list ap; 563 564 va_start (ap, cmsgid); 565 vfprintf (file, _(cmsgid), ap); 566 va_end (ap); 567} 568 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. */ 573 574static void 575error_recursion (diagnostic_context *context) 576{ 577 diagnostic_info diagnostic; 578 579 if (context->lock < 3) 580 pp_flush (context->printer); 581 582 fnotice (stderr, 583 "Internal compiler error: Error reporting routines re-entered.\n"); 584 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); 589 590 /* Do not use gcc_unreachable here; that goes through internal_error 591 and therefore would cause infinite recursion. */ 592 real_abort (); 593} 594 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. */ 598 599void 600fancy_abort (const char *file, int line, const char *function) 601{ 602 internal_error ("in %s, at %s:%d", function, trim_filename (file), line); 603} 604 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 609static void 610real_abort (void) 611{ 612 abort (); 613} 614