2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4 Contributed by Andy Vaught & Niels Kristian Bech Jensen
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
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
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
23 /* Handle the inevitable errors. A major catch here is that things
24 flagged as errors in one match subroutine can conceivably be legal
25 elsewhere. This means that error messages are recorded and saved
26 for possible use later. If a line does not match a legal
27 construction, then the saved error message is reported. */
34 int gfc_suppress_error = 0;
36 static int terminal_width, buffer_flag, errors, warnings;
38 static gfc_error_buf error_buffer, warning_buffer, *cur_error_buffer;
41 /* Per-file error initialization. */
44 gfc_error_init_1 (void)
46 terminal_width = gfc_terminal_width ();
53 /* Set the flag for buffering errors or not. */
56 gfc_buffer_error (int flag)
62 /* Add a single character to the error buffer or output depending on
70 if (cur_error_buffer->index >= cur_error_buffer->allocated)
72 cur_error_buffer->allocated =
73 cur_error_buffer->allocated
74 ? cur_error_buffer->allocated * 2 : 1000;
75 cur_error_buffer->message
76 = xrealloc (cur_error_buffer->message,
77 cur_error_buffer->allocated);
79 cur_error_buffer->message[cur_error_buffer->index++] = c;
85 /* We build up complete lines before handing things
86 over to the library in order to speed up error printing. */
88 static size_t allocated = 0, index = 0;
90 if (index + 1 >= allocated)
92 allocated = allocated ? allocated * 2 : 1000;
93 line = xrealloc (line, allocated);
107 /* Copy a string to wherever it needs to go. */
110 error_string (const char *p)
117 /* Show the file, where it was included and the source line, give a
118 locus. Calls error_printf() recursively, but the recursion is at
119 most one level deep. */
121 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
124 show_locus (int offset, locus * loc)
131 /* TODO: Either limit the total length and number of included files
132 displayed or add buffering of arbitrary number of characters in
137 error_printf ("%s:%d:\n", f->filename,
138 #ifdef USE_MAPPED_LOCATION
139 LOCATION_LINE (lb->location)
147 i = f->inclusion_line;
150 if (f == NULL) break;
152 error_printf (" Included at %s:%d\n", f->filename, i);
155 /* Show the line itself, taking care not to print more than what can
156 show up on the terminal. Tabs are converted to spaces. */
158 p = lb->line + offset;
160 if (i > terminal_width)
161 i = terminal_width - 1;
176 m = ((c >> 4) & 0x0F) + '0';
181 m = (c & 0x0F) + '0';
192 /* As part of printing an error, we show the source lines that caused
193 the problem. We show at least one, possibly two loci. If we're
194 showing two loci and they both refer to the same file and line, we
195 only print the line once. */
198 show_loci (locus * l1, locus * l2)
200 int offset, flag, i, m, c1, c2, cmax;
202 if (l1 == NULL || l1->lb == NULL)
204 error_printf ("<During initialization>\n");
208 c1 = l1->nextc - l1->lb->line;
213 c2 = l2->nextc - l2->lb->line;
221 if (l1->lb != l2->lb || m > terminal_width - 10)
225 cmax = (c1 < c2) ? c2 : c1;
226 if (cmax > terminal_width - 5)
227 offset = cmax - terminal_width + 5;
235 show_locus (offset, l1);
237 /* Arrange that '1' and '2' will show up even if the two columns are equal. */
238 for (i = 1; i <= cmax; i++)
262 if (c1 > terminal_width - 5)
270 show_locus (offset, l1);
271 for (i = 1; i < c1; i++)
281 if (c2 > terminal_width - 20)
289 show_locus (offset, l2);
291 for (i = 1; i < c2; i++)
300 /* Workhorse for the error printing subroutines. This subroutine is
301 inspired by g77's error handling and is similar to printf() with
302 the following %-codes:
304 %c Character, %d Integer, %s String, %% Percent
305 %L Takes locus argument
306 %C Current locus (no argument)
308 If a locus pointer is given, the actual source line is printed out
309 and the column is indicated. Since we want the error message at
310 the bottom of any source file information, we must scan the
311 argument list twice. A maximum of two locus arguments are
317 static void ATTRIBUTE_GCC_GFC(2,0)
318 error_print (const char *type, const char *format0, va_list argp)
320 char c, *p, int_buf[IBUF_LEN], c_arg[MAX_ARGS], *cp_arg[MAX_ARGS];
321 int i, n, have_l1, i_arg[MAX_ARGS];
322 locus *l1, *l2, *loc;
325 l1 = l2 = loc = NULL;
345 loc = va_arg (argp, locus *);
350 loc = &gfc_current_locus;
365 i_arg[n++] = va_arg (argp, int);
369 c_arg[n++] = va_arg (argp, int);
373 cp_arg[n++] = va_arg (argp, char *);
379 /* Show the current loci if we have to. */
390 for (; *format; format++)
394 error_char (*format);
406 error_char (c_arg[n++]);
410 error_string (cp_arg[n++]);
423 p = int_buf + IBUF_LEN - 1;
435 error_string (p + 1);
438 case 'C': /* Current locus */
439 case 'L': /* Specified locus */
440 error_string (have_l1 ? "(2)" : "(1)");
450 /* Wrapper for error_print(). */
453 error_printf (const char *nocmsgid, ...)
457 va_start (argp, nocmsgid);
458 error_print ("", _(nocmsgid), argp);
463 /* Increment the number of errors, and check whether too many have
467 gfc_increment_error_count (void)
470 if ((gfc_option.max_errors != 0) && (errors >= gfc_option.max_errors))
471 gfc_fatal_error ("Error count reached limit of %d.", gfc_option.max_errors);
475 /* Issue a warning. */
478 gfc_warning (const char *nocmsgid, ...)
482 if (inhibit_warnings)
485 warning_buffer.flag = 1;
486 warning_buffer.index = 0;
487 cur_error_buffer = &warning_buffer;
489 va_start (argp, nocmsgid);
490 error_print (_("Warning:"), _(nocmsgid), argp);
495 if (buffer_flag == 0)
498 if (warnings_are_errors)
499 gfc_increment_error_count();
504 /* Whether, for a feature included in a given standard set (GFC_STD_*),
505 we should issue an error or a warning, or be quiet. */
508 gfc_notification_std (int std)
512 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
513 if ((gfc_option.allow_std & std) != 0 && !warning)
516 return warning ? WARNING : ERROR;
520 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
521 feature. An error/warning will be issued if the currently selected
522 standard does not contain the requested bits. Return FAILURE if
523 an error is generated. */
526 gfc_notify_std (int std, const char *nocmsgid, ...)
531 warning = ((gfc_option.warn_std & std) != 0)
532 && !inhibit_warnings;
533 if ((gfc_option.allow_std & std) != 0
537 if (gfc_suppress_error)
538 return warning ? SUCCESS : FAILURE;
540 cur_error_buffer = (warning && !warnings_are_errors)
541 ? &warning_buffer : &error_buffer;
542 cur_error_buffer->flag = 1;
543 cur_error_buffer->index = 0;
545 va_start (argp, nocmsgid);
547 error_print (_("Warning:"), _(nocmsgid), argp);
549 error_print (_("Error:"), _(nocmsgid), argp);
554 if (buffer_flag == 0)
556 if (warning && !warnings_are_errors)
559 gfc_increment_error_count();
562 return (warning && !warnings_are_errors) ? SUCCESS : FAILURE;
566 /* Immediate warning (i.e. do not buffer the warning). */
569 gfc_warning_now (const char *nocmsgid, ...)
574 if (inhibit_warnings)
580 if (warnings_are_errors)
581 gfc_increment_error_count();
583 va_start (argp, nocmsgid);
584 error_print (_("Warning:"), _(nocmsgid), argp);
592 /* Clear the warning flag. */
595 gfc_clear_warning (void)
597 warning_buffer.flag = 0;
601 /* Check to see if any warnings have been saved.
602 If so, print the warning. */
605 gfc_warning_check (void)
607 if (warning_buffer.flag)
610 if (warning_buffer.message != NULL)
611 fputs (warning_buffer.message, stderr);
612 warning_buffer.flag = 0;
617 /* Issue an error. */
620 gfc_error (const char *nocmsgid, ...)
624 if (gfc_suppress_error)
627 error_buffer.flag = 1;
628 error_buffer.index = 0;
629 cur_error_buffer = &error_buffer;
631 va_start (argp, nocmsgid);
632 error_print (_("Error:"), _(nocmsgid), argp);
637 if (buffer_flag == 0)
638 gfc_increment_error_count();
642 /* Immediate error. */
645 gfc_error_now (const char *nocmsgid, ...)
650 error_buffer.flag = 1;
651 error_buffer.index = 0;
652 cur_error_buffer = &error_buffer;
657 va_start (argp, nocmsgid);
658 error_print (_("Error:"), _(nocmsgid), argp);
663 gfc_increment_error_count();
667 if (flag_fatal_errors)
672 /* Fatal error, never returns. */
675 gfc_fatal_error (const char *nocmsgid, ...)
681 va_start (argp, nocmsgid);
682 error_print (_("Fatal Error:"), _(nocmsgid), argp);
689 /* This shouldn't happen... but sometimes does. */
692 gfc_internal_error (const char *format, ...)
698 va_start (argp, format);
700 show_loci (&gfc_current_locus, NULL);
701 error_printf ("Internal Error at (1):");
703 error_print ("", format, argp);
706 exit (ICE_EXIT_CODE);
710 /* Clear the error flag when we start to compile a source line. */
713 gfc_clear_error (void)
715 error_buffer.flag = 0;
719 /* Tests the state of error_flag. */
722 gfc_error_flag_test (void)
724 return error_buffer.flag;
728 /* Check to see if any errors have been saved.
729 If so, print the error. Returns the state of error_flag. */
732 gfc_error_check (void)
736 rc = error_buffer.flag;
738 if (error_buffer.flag)
740 if (error_buffer.message != NULL)
741 fputs (error_buffer.message, stderr);
742 error_buffer.flag = 0;
744 gfc_increment_error_count();
746 if (flag_fatal_errors)
754 /* Save the existing error state. */
757 gfc_push_error (gfc_error_buf * err)
759 err->flag = error_buffer.flag;
760 if (error_buffer.flag)
761 err->message = xstrdup (error_buffer.message);
763 error_buffer.flag = 0;
767 /* Restore a previous pushed error state. */
770 gfc_pop_error (gfc_error_buf * err)
772 error_buffer.flag = err->flag;
773 if (error_buffer.flag)
775 size_t len = strlen (err->message) + 1;
776 gcc_assert (len <= error_buffer.allocated);
777 memcpy (error_buffer.message, err->message, len);
778 gfc_free (err->message);
783 /* Free a pushed error state, but keep the current error state. */
786 gfc_free_error (gfc_error_buf * err)
789 gfc_free (err->message);
793 /* Debug wrapper for printf. */
796 gfc_status (const char *cmsgid, ...)
800 va_start (argp, cmsgid);
802 vprintf (_(cmsgid), argp);
808 /* Subroutine for outputting a single char so that we don't have to go
809 around creating a lot of 1-character strings. */
812 gfc_status_char (char c)
818 /* Report the number of warnings and errors that occurred to the caller. */
821 gfc_get_errors (int *w, int *e)