2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
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 = cur_error_buffer->allocated
73 ? cur_error_buffer->allocated * 2 : 1000;
74 cur_error_buffer->message = xrealloc (cur_error_buffer->message,
75 cur_error_buffer->allocated);
77 cur_error_buffer->message[cur_error_buffer->index++] = c;
83 /* We build up complete lines before handing things
84 over to the library in order to speed up error printing. */
86 static size_t allocated = 0, index = 0;
88 if (index + 1 >= allocated)
90 allocated = allocated ? allocated * 2 : 1000;
91 line = xrealloc (line, allocated);
105 /* Copy a string to wherever it needs to go. */
108 error_string (const char *p)
115 /* Print a formatted integer to the error buffer or output. */
120 error_integer (int i)
122 char *p, int_buf[IBUF_LEN];
130 p = int_buf + IBUF_LEN - 1;
142 error_string (p + 1);
146 /* Show the file, where it was included, and the source line, give a
147 locus. Calls error_printf() recursively, but the recursion is at
148 most one level deep. */
150 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
153 show_locus (locus *loc, int c1, int c2)
158 int i, m, offset, cmax;
160 /* TODO: Either limit the total length and number of included files
161 displayed or add buffering of arbitrary number of characters in
164 /* Write out the error header line, giving the source file and error
165 location (in GNU standard "[file]:[line].[column]:" format),
166 followed by an "included by" stack and a blank line. This header
167 format is matched by a testsuite parser defined in
168 lib/gfortran-dg.exp. */
173 error_string (f->filename);
176 #ifdef USE_MAPPED_LOCATION
177 error_integer (LOCATION_LINE (lb->location));
179 error_integer (lb->linenum);
182 if ((c1 > 0) || (c2 > 0))
188 if ((c1 > 0) && (c2 > 0))
199 i = f->inclusion_line;
202 if (f == NULL) break;
204 error_printf (" Included at %s:%d:", f->filename, i);
209 /* Calculate an appropriate horizontal offset of the source line in
210 order to get the error locus within the visible portion of the
211 line. Note that if the margin of 5 here is changed, the
212 corresponding margin of 10 in show_loci should be changed. */
216 /* When the loci is not associated with a column, it will have a
217 value of zero. We adjust this to 1 so that it will appear. */
224 /* If the two loci would appear in the same column, we shift
225 '2' one column to the right, so as to print '12' rather than
226 just '1'. We do this here so it will be accounted for in the
227 margin calculations. */
232 cmax = (c1 < c2) ? c2 : c1;
233 if (cmax > terminal_width - 5)
234 offset = cmax - terminal_width + 5;
236 /* Show the line itself, taking care not to print more than what can
237 show up on the terminal. Tabs are converted to spaces, and
238 nonprintable characters are converted to a "\xNN" sequence. */
240 /* TODO: Although setting i to the terminal width is clever, it fails
241 to work correctly when nonprintable characters exist. A better
242 solution should be found. */
244 p = lb->line + offset;
246 if (i > terminal_width)
247 i = terminal_width - 1;
262 m = ((c >> 4) & 0x0F) + '0';
267 m = (c & 0x0F) + '0';
276 /* Show the '1' and/or '2' corresponding to the column of the error
277 locus. Note that a value of -1 for c1 or c2 will simply cause
278 the relevant number not to be printed. */
283 for (i = 1; i <= cmax; i++)
298 /* As part of printing an error, we show the source lines that caused
299 the problem. We show at least one, and possibly two loci; the two
300 loci may or may not be on the same source line. */
303 show_loci (locus *l1, locus *l2)
307 if (l1 == NULL || l1->lb == NULL)
309 error_printf ("<During initialization>\n");
313 /* While calculating parameters for printing the loci, we consider possible
314 reasons for printing one per line. If appropriate, print the loci
315 individually; otherwise we print them both on the same line. */
317 c1 = l1->nextc - l1->lb->line;
320 show_locus (l1, c1, -1);
324 c2 = l2->nextc - l2->lb->line;
331 /* Note that the margin value of 10 here needs to be less than the
332 margin of 5 used in the calculation of offset in show_locus. */
334 if (l1->lb != l2->lb || m > terminal_width - 10)
336 show_locus (l1, c1, -1);
337 show_locus (l2, -1, c2);
341 show_locus (l1, c1, c2);
347 /* Workhorse for the error printing subroutines. This subroutine is
348 inspired by g77's error handling and is similar to printf() with
349 the following %-codes:
351 %c Character, %d or %i Integer, %s String, %% Percent
352 %L Takes locus argument
353 %C Current locus (no argument)
355 If a locus pointer is given, the actual source line is printed out
356 and the column is indicated. Since we want the error message at
357 the bottom of any source file information, we must scan the
358 argument list twice -- once to determine whether the loci are
359 present and record this for printing, and once to print the error
360 message after and loci have been printed. A maximum of two locus
361 arguments are permitted.
363 This function is also called (recursively) by show_locus in the
364 case of included files; however, as show_locus does not resupply
365 any loci, the recursion is at most one level deep. */
369 static void ATTRIBUTE_GCC_GFC(2,0)
370 error_print (const char *type, const char *format0, va_list argp)
372 enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_CHAR, TYPE_STRING,
382 const char * stringval;
384 } arg[MAX_ARGS], spec[MAX_ARGS];
385 /* spec is the array of specifiers, in the same order as they
386 appear in the format string. arg is the array of arguments,
387 in the same order as they appear in the va_list. */
390 int i, n, have_l1, pos, maxpos;
391 locus *l1, *l2, *loc;
403 for (i = 0; i < MAX_ARGS; i++)
405 arg[i].type = NOTYPE;
409 /* First parse the format string for position specifiers. */
422 if (ISDIGIT (*format))
424 /* This is a position specifier. For example, the number
425 12 in the format string "%12$d", which specifies the third
426 argument of the va_list, formatted in %d format.
427 For details, see "man 3 printf". */
428 pos = atoi(format) - 1;
429 gcc_assert (pos >= 0);
430 while (ISDIGIT(*format))
432 gcc_assert (*format++ == '$');
445 arg[pos].type = TYPE_CURRENTLOC;
449 arg[pos].type = TYPE_LOCUS;
454 arg[pos].type = TYPE_INTEGER;
458 arg[pos].type = TYPE_CHAR;
462 arg[pos].type = TYPE_STRING;
472 /* Then convert the values for each %-style argument. */
473 for (pos = 0; pos <= maxpos; pos++)
475 gcc_assert (arg[pos].type != NOTYPE);
476 switch (arg[pos].type)
478 case TYPE_CURRENTLOC:
479 loc = &gfc_current_locus;
483 if (arg[pos].type == TYPE_LOCUS)
484 loc = va_arg (argp, locus *);
489 arg[pos].u.stringval = "(2)";
495 arg[pos].u.stringval = "(1)";
500 arg[pos].u.intval = va_arg (argp, int);
504 arg[pos].u.charval = (char) va_arg (argp, int);
508 arg[pos].u.stringval = (const char *) va_arg (argp, char *);
516 for (n = 0; spec[n].pos >= 0; n++)
517 spec[n].u = arg[spec[n].pos].u;
519 /* Show the current loci if we have to. */
533 for (; *format; format++)
537 error_char (*format);
542 if (ISDIGIT (*format))
544 /* This is a position specifier. See comment above. */
545 while (ISDIGIT (*format))
548 /* Skip over the dollar sign. */
559 error_char (spec[n++].u.charval);
563 case 'C': /* Current locus */
564 case 'L': /* Specified locus */
565 error_string (spec[n++].u.stringval);
570 error_integer (spec[n++].u.intval);
579 /* Wrapper for error_print(). */
582 error_printf (const char *nocmsgid, ...)
586 va_start (argp, nocmsgid);
587 error_print ("", _(nocmsgid), argp);
592 /* Increment the number of errors, and check whether too many have
596 gfc_increment_error_count (void)
599 if ((gfc_option.max_errors != 0) && (errors >= gfc_option.max_errors))
600 gfc_fatal_error ("Error count reached limit of %d.", gfc_option.max_errors);
604 /* Issue a warning. */
607 gfc_warning (const char *nocmsgid, ...)
611 if (inhibit_warnings)
614 warning_buffer.flag = 1;
615 warning_buffer.index = 0;
616 cur_error_buffer = &warning_buffer;
618 va_start (argp, nocmsgid);
619 error_print (_("Warning:"), _(nocmsgid), argp);
624 if (buffer_flag == 0)
627 if (warnings_are_errors)
628 gfc_increment_error_count();
633 /* Whether, for a feature included in a given standard set (GFC_STD_*),
634 we should issue an error or a warning, or be quiet. */
637 gfc_notification_std (int std)
641 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
642 if ((gfc_option.allow_std & std) != 0 && !warning)
645 return warning ? WARNING : ERROR;
649 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
650 feature. An error/warning will be issued if the currently selected
651 standard does not contain the requested bits. Return FAILURE if
652 an error is generated. */
655 gfc_notify_std (int std, const char *nocmsgid, ...)
660 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
661 if ((gfc_option.allow_std & std) != 0 && !warning)
664 if (gfc_suppress_error)
665 return warning ? SUCCESS : FAILURE;
667 cur_error_buffer = (warning && !warnings_are_errors)
668 ? &warning_buffer : &error_buffer;
669 cur_error_buffer->flag = 1;
670 cur_error_buffer->index = 0;
672 va_start (argp, nocmsgid);
674 error_print (_("Warning:"), _(nocmsgid), argp);
676 error_print (_("Error:"), _(nocmsgid), argp);
681 if (buffer_flag == 0)
683 if (warning && !warnings_are_errors)
686 gfc_increment_error_count();
689 return (warning && !warnings_are_errors) ? SUCCESS : FAILURE;
693 /* Immediate warning (i.e. do not buffer the warning). */
696 gfc_warning_now (const char *nocmsgid, ...)
701 if (inhibit_warnings)
707 if (warnings_are_errors)
708 gfc_increment_error_count();
710 va_start (argp, nocmsgid);
711 error_print (_("Warning:"), _(nocmsgid), argp);
719 /* Clear the warning flag. */
722 gfc_clear_warning (void)
724 warning_buffer.flag = 0;
728 /* Check to see if any warnings have been saved.
729 If so, print the warning. */
732 gfc_warning_check (void)
734 if (warning_buffer.flag)
737 if (warning_buffer.message != NULL)
738 fputs (warning_buffer.message, stderr);
739 warning_buffer.flag = 0;
744 /* Issue an error. */
747 gfc_error (const char *nocmsgid, ...)
751 if (gfc_suppress_error)
754 error_buffer.flag = 1;
755 error_buffer.index = 0;
756 cur_error_buffer = &error_buffer;
758 va_start (argp, nocmsgid);
759 error_print (_("Error:"), _(nocmsgid), argp);
764 if (buffer_flag == 0)
765 gfc_increment_error_count();
769 /* Immediate error. */
772 gfc_error_now (const char *nocmsgid, ...)
777 error_buffer.flag = 1;
778 error_buffer.index = 0;
779 cur_error_buffer = &error_buffer;
784 va_start (argp, nocmsgid);
785 error_print (_("Error:"), _(nocmsgid), argp);
790 gfc_increment_error_count();
794 if (flag_fatal_errors)
799 /* Fatal error, never returns. */
802 gfc_fatal_error (const char *nocmsgid, ...)
808 va_start (argp, nocmsgid);
809 error_print (_("Fatal Error:"), _(nocmsgid), argp);
816 /* This shouldn't happen... but sometimes does. */
819 gfc_internal_error (const char *format, ...)
825 va_start (argp, format);
827 show_loci (&gfc_current_locus, NULL);
828 error_printf ("Internal Error at (1):");
830 error_print ("", format, argp);
833 exit (ICE_EXIT_CODE);
837 /* Clear the error flag when we start to compile a source line. */
840 gfc_clear_error (void)
842 error_buffer.flag = 0;
846 /* Tests the state of error_flag. */
849 gfc_error_flag_test (void)
851 return error_buffer.flag;
855 /* Check to see if any errors have been saved.
856 If so, print the error. Returns the state of error_flag. */
859 gfc_error_check (void)
863 rc = error_buffer.flag;
865 if (error_buffer.flag)
867 if (error_buffer.message != NULL)
868 fputs (error_buffer.message, stderr);
869 error_buffer.flag = 0;
871 gfc_increment_error_count();
873 if (flag_fatal_errors)
881 /* Save the existing error state. */
884 gfc_push_error (gfc_error_buf *err)
886 err->flag = error_buffer.flag;
887 if (error_buffer.flag)
888 err->message = xstrdup (error_buffer.message);
890 error_buffer.flag = 0;
894 /* Restore a previous pushed error state. */
897 gfc_pop_error (gfc_error_buf *err)
899 error_buffer.flag = err->flag;
900 if (error_buffer.flag)
902 size_t len = strlen (err->message) + 1;
903 gcc_assert (len <= error_buffer.allocated);
904 memcpy (error_buffer.message, err->message, len);
905 gfc_free (err->message);
910 /* Free a pushed error state, but keep the current error state. */
913 gfc_free_error (gfc_error_buf *err)
916 gfc_free (err->message);
920 /* Debug wrapper for printf. */
923 gfc_status (const char *cmsgid, ...)
927 va_start (argp, cmsgid);
929 vprintf (_(cmsgid), argp);
935 /* Subroutine for outputting a single char so that we don't have to go
936 around creating a lot of 1-character strings. */
939 gfc_status_char (char c)
945 /* Report the number of warnings and errors that occurred to the caller. */
948 gfc_get_errors (int *w, int *e)