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 /* Print a formatted integer to the error buffer or output. */
122 error_integer (int i)
124 char *p, int_buf[IBUF_LEN];
132 p = int_buf + IBUF_LEN - 1;
144 error_string (p + 1);
148 /* Show the file, where it was included, and the source line, give a
149 locus. Calls error_printf() recursively, but the recursion is at
150 most one level deep. */
152 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
155 show_locus (locus * loc, int c1, int c2)
160 int i, m, offset, cmax;
162 /* TODO: Either limit the total length and number of included files
163 displayed or add buffering of arbitrary number of characters in
166 /* Write out the error header line, giving the source file and error
167 location (in GNU standard "[file]:[line].[column]:" format),
168 followed by an "included by" stack and a blank line. This header
169 format is matched by a testsuite parser defined in
170 lib/gfortran-dg.exp. */
175 error_string (f->filename);
178 #ifdef USE_MAPPED_LOCATION
179 error_integer (LOCATION_LINE (lb->location));
181 error_integer (lb->linenum);
184 if ((c1 > 0) || (c2 > 0))
190 if ((c1 > 0) && (c2 > 0))
201 i = f->inclusion_line;
204 if (f == NULL) break;
206 error_printf (" Included at %s:%d:", f->filename, i);
211 /* Calculate an appropriate horizontal offset of the source line in
212 order to get the error locus within the visible portion of the
213 line. Note that if the margin of 5 here is changed, the
214 corresponding margin of 10 in show_loci should be changed. */
218 /* When the loci is not associated with a column, it will have a
219 value of zero. We adjust this to 1 so that it will appear. */
226 /* If the two loci would appear in the same column, we shift
227 '2' one column to the right, so as to print '12' rather than
228 just '1'. We do this here so it will be accounted for in the
229 margin calculations. */
234 cmax = (c1 < c2) ? c2 : c1;
235 if (cmax > terminal_width - 5)
236 offset = cmax - terminal_width + 5;
238 /* TODO: Is there a good reason for the following apparently-redundant
239 check, and the similar ones in the single-locus cases below? */
244 /* Show the line itself, taking care not to print more than what can
245 show up on the terminal. Tabs are converted to spaces, and
246 nonprintable characters are converted to a "\xNN" sequence. */
248 /* TODO: Although setting i to the terminal width is clever, it fails
249 to work correctly when nonprintable characters exist. A better
250 solution should be found. */
252 p = lb->line + offset;
254 if (i > terminal_width)
255 i = terminal_width - 1;
270 m = ((c >> 4) & 0x0F) + '0';
275 m = (c & 0x0F) + '0';
284 /* Show the '1' and/or '2' corresponding to the column of the error
285 locus. Note that a value of -1 for c1 or c2 will simply cause
286 the relevant number not to be printed. */
291 for (i = 1; i <= cmax; i++)
306 /* As part of printing an error, we show the source lines that caused
307 the problem. We show at least one, and possibly two loci; the two
308 loci may or may not be on the same source line. */
311 show_loci (locus * l1, locus * l2)
315 if (l1 == NULL || l1->lb == NULL)
317 error_printf ("<During initialization>\n");
321 /* While calculating parameters for printing the loci, we consider possible
322 reasons for printing one per line. If appropriate, print the loci
323 individually; otherwise we print them both on the same line. */
325 c1 = l1->nextc - l1->lb->line;
328 show_locus (l1, c1, -1);
332 c2 = l2->nextc - l2->lb->line;
339 /* Note that the margin value of 10 here needs to be less than the
340 margin of 5 used in the calculation of offset in show_locus. */
342 if (l1->lb != l2->lb || m > terminal_width - 10)
344 show_locus (l1, c1, -1);
345 show_locus (l2, -1, c2);
349 show_locus (l1, c1, c2);
356 /* Workhorse for the error printing subroutines. This subroutine is
357 inspired by g77's error handling and is similar to printf() with
358 the following %-codes:
360 %c Character, %d or %i Integer, %s String, %% Percent
361 %L Takes locus argument
362 %C Current locus (no argument)
364 If a locus pointer is given, the actual source line is printed out
365 and the column is indicated. Since we want the error message at
366 the bottom of any source file information, we must scan the
367 argument list twice -- once to determine whether the loci are
368 present and record this for printing, and once to print the error
369 message after and loci have been printed. A maximum of two locus
370 arguments are permitted.
372 This function is also called (recursively) by show_locus in the
373 case of included files; however, as show_locus does not resupply
374 any loci, the recursion is at most one level deep. */
378 static void ATTRIBUTE_GCC_GFC(2,0)
379 error_print (const char *type, const char *format0, va_list argp)
381 enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_CHAR, TYPE_STRING,
391 const char * stringval;
393 } arg[MAX_ARGS], spec[MAX_ARGS];
394 /* spec is the array of specifiers, in the same order as they
395 appear in the format string. arg is the array of arguments,
396 in the same order as they appear in the va_list. */
399 int i, n, have_l1, pos, maxpos;
400 locus *l1, *l2, *loc;
412 for (i = 0; i < MAX_ARGS; i++)
414 arg[i].type = NOTYPE;
418 /* First parse the format string for position specifiers. */
428 if (ISDIGIT (*format))
430 /* This is a position specifier. For example, the number
431 12 in the format string "%12$d", which specifies the third
432 argument of the va_list, formatted in %d format.
433 For details, see "man 3 printf". */
434 pos = atoi(format) - 1;
435 gcc_assert (pos >= 0);
436 while (ISDIGIT(*format))
438 gcc_assert (*format++ == '$');
451 arg[pos].type = TYPE_CURRENTLOC;
455 arg[pos].type = TYPE_LOCUS;
460 arg[pos].type = TYPE_INTEGER;
464 arg[pos].type = TYPE_CHAR;
468 arg[pos].type = TYPE_STRING;
478 /* Then convert the values for each %-style argument. */
479 for (pos = 0; pos <= maxpos; pos++)
481 gcc_assert (arg[pos].type != NOTYPE);
482 switch (arg[pos].type)
484 case TYPE_CURRENTLOC:
485 loc = &gfc_current_locus;
489 if (arg[pos].type == TYPE_LOCUS)
490 loc = va_arg (argp, locus *);
495 arg[pos].u.stringval = "(2)";
501 arg[pos].u.stringval = "(1)";
506 arg[pos].u.intval = va_arg (argp, int);
510 arg[pos].u.charval = (char) va_arg (argp, int);
514 arg[pos].u.stringval = (const char *) va_arg (argp, char *);
522 for (n = 0; spec[n].pos >= 0; n++)
523 spec[n].u = arg[spec[n].pos].u;
525 /* Show the current loci if we have to. */
539 for (; *format; format++)
543 error_char (*format);
548 if (ISDIGIT(*format))
550 /* This is a position specifier. See comment above. */
551 while (ISDIGIT(*format))
553 /* Skip over the dollar sign. */
564 error_char (spec[n++].u.charval);
568 case 'C': /* Current locus */
569 case 'L': /* Specified locus */
570 error_string (spec[n++].u.stringval);
575 error_integer (spec[n++].u.intval);
584 /* Wrapper for error_print(). */
587 error_printf (const char *nocmsgid, ...)
591 va_start (argp, nocmsgid);
592 error_print ("", _(nocmsgid), argp);
597 /* Increment the number of errors, and check whether too many have
601 gfc_increment_error_count (void)
604 if ((gfc_option.max_errors != 0) && (errors >= gfc_option.max_errors))
605 gfc_fatal_error ("Error count reached limit of %d.", gfc_option.max_errors);
609 /* Issue a warning. */
612 gfc_warning (const char *nocmsgid, ...)
616 if (inhibit_warnings)
619 warning_buffer.flag = 1;
620 warning_buffer.index = 0;
621 cur_error_buffer = &warning_buffer;
623 va_start (argp, nocmsgid);
624 error_print (_("Warning:"), _(nocmsgid), argp);
629 if (buffer_flag == 0)
632 if (warnings_are_errors)
633 gfc_increment_error_count();
638 /* Whether, for a feature included in a given standard set (GFC_STD_*),
639 we should issue an error or a warning, or be quiet. */
642 gfc_notification_std (int std)
646 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
647 if ((gfc_option.allow_std & std) != 0 && !warning)
650 return warning ? WARNING : ERROR;
654 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
655 feature. An error/warning will be issued if the currently selected
656 standard does not contain the requested bits. Return FAILURE if
657 an error is generated. */
660 gfc_notify_std (int std, const char *nocmsgid, ...)
665 warning = ((gfc_option.warn_std & std) != 0)
666 && !inhibit_warnings;
667 if ((gfc_option.allow_std & std) != 0
671 if (gfc_suppress_error)
672 return warning ? SUCCESS : FAILURE;
674 cur_error_buffer = (warning && !warnings_are_errors)
675 ? &warning_buffer : &error_buffer;
676 cur_error_buffer->flag = 1;
677 cur_error_buffer->index = 0;
679 va_start (argp, nocmsgid);
681 error_print (_("Warning:"), _(nocmsgid), argp);
683 error_print (_("Error:"), _(nocmsgid), argp);
688 if (buffer_flag == 0)
690 if (warning && !warnings_are_errors)
693 gfc_increment_error_count();
696 return (warning && !warnings_are_errors) ? SUCCESS : FAILURE;
700 /* Immediate warning (i.e. do not buffer the warning). */
703 gfc_warning_now (const char *nocmsgid, ...)
708 if (inhibit_warnings)
714 if (warnings_are_errors)
715 gfc_increment_error_count();
717 va_start (argp, nocmsgid);
718 error_print (_("Warning:"), _(nocmsgid), argp);
726 /* Clear the warning flag. */
729 gfc_clear_warning (void)
731 warning_buffer.flag = 0;
735 /* Check to see if any warnings have been saved.
736 If so, print the warning. */
739 gfc_warning_check (void)
741 if (warning_buffer.flag)
744 if (warning_buffer.message != NULL)
745 fputs (warning_buffer.message, stderr);
746 warning_buffer.flag = 0;
751 /* Issue an error. */
754 gfc_error (const char *nocmsgid, ...)
758 if (gfc_suppress_error)
761 error_buffer.flag = 1;
762 error_buffer.index = 0;
763 cur_error_buffer = &error_buffer;
765 va_start (argp, nocmsgid);
766 error_print (_("Error:"), _(nocmsgid), argp);
771 if (buffer_flag == 0)
772 gfc_increment_error_count();
776 /* Immediate error. */
779 gfc_error_now (const char *nocmsgid, ...)
784 error_buffer.flag = 1;
785 error_buffer.index = 0;
786 cur_error_buffer = &error_buffer;
791 va_start (argp, nocmsgid);
792 error_print (_("Error:"), _(nocmsgid), argp);
797 gfc_increment_error_count();
801 if (flag_fatal_errors)
806 /* Fatal error, never returns. */
809 gfc_fatal_error (const char *nocmsgid, ...)
815 va_start (argp, nocmsgid);
816 error_print (_("Fatal Error:"), _(nocmsgid), argp);
823 /* This shouldn't happen... but sometimes does. */
826 gfc_internal_error (const char *format, ...)
832 va_start (argp, format);
834 show_loci (&gfc_current_locus, NULL);
835 error_printf ("Internal Error at (1):");
837 error_print ("", format, argp);
840 exit (ICE_EXIT_CODE);
844 /* Clear the error flag when we start to compile a source line. */
847 gfc_clear_error (void)
849 error_buffer.flag = 0;
853 /* Tests the state of error_flag. */
856 gfc_error_flag_test (void)
858 return error_buffer.flag;
862 /* Check to see if any errors have been saved.
863 If so, print the error. Returns the state of error_flag. */
866 gfc_error_check (void)
870 rc = error_buffer.flag;
872 if (error_buffer.flag)
874 if (error_buffer.message != NULL)
875 fputs (error_buffer.message, stderr);
876 error_buffer.flag = 0;
878 gfc_increment_error_count();
880 if (flag_fatal_errors)
888 /* Save the existing error state. */
891 gfc_push_error (gfc_error_buf * err)
893 err->flag = error_buffer.flag;
894 if (error_buffer.flag)
895 err->message = xstrdup (error_buffer.message);
897 error_buffer.flag = 0;
901 /* Restore a previous pushed error state. */
904 gfc_pop_error (gfc_error_buf * err)
906 error_buffer.flag = err->flag;
907 if (error_buffer.flag)
909 size_t len = strlen (err->message) + 1;
910 gcc_assert (len <= error_buffer.allocated);
911 memcpy (error_buffer.message, err->message, len);
912 gfc_free (err->message);
917 /* Free a pushed error state, but keep the current error state. */
920 gfc_free_error (gfc_error_buf * err)
923 gfc_free (err->message);
927 /* Debug wrapper for printf. */
930 gfc_status (const char *cmsgid, ...)
934 va_start (argp, cmsgid);
936 vprintf (_(cmsgid), argp);
942 /* Subroutine for outputting a single char so that we don't have to go
943 around creating a lot of 1-character strings. */
946 gfc_status_char (char c)
952 /* Report the number of warnings and errors that occurred to the caller. */
955 gfc_get_errors (int *w, int *e)