2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Handle the inevitable errors. A major catch here is that things
23 flagged as errors in one match subroutine can conceivably be legal
24 elsewhere. This means that error messages are recorded and saved
25 for possible use later. If a line does not match a legal
26 construction, then the saved error message is reported. */
33 int gfc_suppress_error = 0;
35 static int terminal_width, buffer_flag, errors, warnings;
37 static gfc_error_buf error_buffer, warning_buffer, *cur_error_buffer;
40 /* Per-file error initialization. */
43 gfc_error_init_1 (void)
45 terminal_width = gfc_terminal_width ();
52 /* Set the flag for buffering errors or not. */
55 gfc_buffer_error (int flag)
61 /* Add a single character to the error buffer or output depending on
69 if (cur_error_buffer->index >= cur_error_buffer->allocated)
71 cur_error_buffer->allocated = cur_error_buffer->allocated
72 ? cur_error_buffer->allocated * 2 : 1000;
73 cur_error_buffer->message = xrealloc (cur_error_buffer->message,
74 cur_error_buffer->allocated);
76 cur_error_buffer->message[cur_error_buffer->index++] = c;
82 /* We build up complete lines before handing things
83 over to the library in order to speed up error printing. */
85 static size_t allocated = 0, index = 0;
87 if (index + 1 >= allocated)
89 allocated = allocated ? allocated * 2 : 1000;
90 line = xrealloc (line, allocated);
104 /* Copy a string to wherever it needs to go. */
107 error_string (const char *p)
114 /* Print a formatted integer to the error buffer or output. */
119 error_uinteger (unsigned long int i)
121 char *p, int_buf[IBUF_LEN];
123 p = int_buf + IBUF_LEN - 1;
135 error_string (p + 1);
139 error_integer (long int i)
145 u = (unsigned long int) -i;
155 /* Show the file, where it was included, and the source line, give a
156 locus. Calls error_printf() recursively, but the recursion is at
157 most one level deep. */
159 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
162 show_locus (locus *loc, int c1, int c2)
167 int i, m, offset, cmax;
169 /* TODO: Either limit the total length and number of included files
170 displayed or add buffering of arbitrary number of characters in
173 /* Write out the error header line, giving the source file and error
174 location (in GNU standard "[file]:[line].[column]:" format),
175 followed by an "included by" stack and a blank line. This header
176 format is matched by a testsuite parser defined in
177 lib/gfortran-dg.exp. */
182 error_string (f->filename);
185 error_integer (LOCATION_LINE (lb->location));
187 if ((c1 > 0) || (c2 > 0))
193 if ((c1 > 0) && (c2 > 0))
204 i = f->inclusion_line;
207 if (f == NULL) break;
209 error_printf (" Included at %s:%d:", f->filename, i);
214 /* Calculate an appropriate horizontal offset of the source line in
215 order to get the error locus within the visible portion of the
216 line. Note that if the margin of 5 here is changed, the
217 corresponding margin of 10 in show_loci should be changed. */
221 /* When the loci is not associated with a column, it will have a
222 value of zero. We adjust this to 1 so that it will appear. */
229 /* If the two loci would appear in the same column, we shift
230 '2' one column to the right, so as to print '12' rather than
231 just '1'. We do this here so it will be accounted for in the
232 margin calculations. */
237 cmax = (c1 < c2) ? c2 : c1;
238 if (cmax > terminal_width - 5)
239 offset = cmax - terminal_width + 5;
241 /* Show the line itself, taking care not to print more than what can
242 show up on the terminal. Tabs are converted to spaces, and
243 nonprintable characters are converted to a "\xNN" sequence. */
245 /* TODO: Although setting i to the terminal width is clever, it fails
246 to work correctly when nonprintable characters exist. A better
247 solution should be found. */
249 p = lb->line + offset;
251 if (i > terminal_width)
252 i = terminal_width - 1;
267 m = ((c >> 4) & 0x0F) + '0';
272 m = (c & 0x0F) + '0';
281 /* Show the '1' and/or '2' corresponding to the column of the error
282 locus. Note that a value of -1 for c1 or c2 will simply cause
283 the relevant number not to be printed. */
288 for (i = 1; i <= cmax; i++)
303 /* As part of printing an error, we show the source lines that caused
304 the problem. We show at least one, and possibly two loci; the two
305 loci may or may not be on the same source line. */
308 show_loci (locus *l1, locus *l2)
312 if (l1 == NULL || l1->lb == NULL)
314 error_printf ("<During initialization>\n");
318 /* While calculating parameters for printing the loci, we consider possible
319 reasons for printing one per line. If appropriate, print the loci
320 individually; otherwise we print them both on the same line. */
322 c1 = l1->nextc - l1->lb->line;
325 show_locus (l1, c1, -1);
329 c2 = l2->nextc - l2->lb->line;
336 /* Note that the margin value of 10 here needs to be less than the
337 margin of 5 used in the calculation of offset in show_locus. */
339 if (l1->lb != l2->lb || m > terminal_width - 10)
341 show_locus (l1, c1, -1);
342 show_locus (l2, -1, c2);
346 show_locus (l1, c1, c2);
352 /* Workhorse for the error printing subroutines. This subroutine is
353 inspired by g77's error handling and is similar to printf() with
354 the following %-codes:
356 %c Character, %d or %i Integer, %s String, %% Percent
357 %L Takes locus argument
358 %C Current locus (no argument)
360 If a locus pointer is given, the actual source line is printed out
361 and the column is indicated. Since we want the error message at
362 the bottom of any source file information, we must scan the
363 argument list twice -- once to determine whether the loci are
364 present and record this for printing, and once to print the error
365 message after and loci have been printed. A maximum of two locus
366 arguments are permitted.
368 This function is also called (recursively) by show_locus in the
369 case of included files; however, as show_locus does not resupply
370 any loci, the recursion is at most one level deep. */
374 static void ATTRIBUTE_GCC_GFC(2,0)
375 error_print (const char *type, const char *format0, va_list argp)
377 enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
378 TYPE_LONGINT, TYPE_ULONGINT, TYPE_CHAR, TYPE_STRING,
387 unsigned int uintval;
389 unsigned long int ulongintval;
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. */
431 if (ISDIGIT (*format))
433 /* This is a position specifier. For example, the number
434 12 in the format string "%12$d", which specifies the third
435 argument of the va_list, formatted in %d format.
436 For details, see "man 3 printf". */
437 pos = atoi(format) - 1;
438 gcc_assert (pos >= 0);
439 while (ISDIGIT(*format))
441 gcc_assert (*format++ == '$');
454 arg[pos].type = TYPE_CURRENTLOC;
458 arg[pos].type = TYPE_LOCUS;
463 arg[pos].type = TYPE_INTEGER;
467 arg[pos].type = TYPE_UINTEGER;
472 arg[pos].type = TYPE_ULONGINT;
473 else if (c == 'i' || c == 'd')
474 arg[pos].type = TYPE_LONGINT;
480 arg[pos].type = TYPE_CHAR;
484 arg[pos].type = TYPE_STRING;
494 /* Then convert the values for each %-style argument. */
495 for (pos = 0; pos <= maxpos; pos++)
497 gcc_assert (arg[pos].type != NOTYPE);
498 switch (arg[pos].type)
500 case TYPE_CURRENTLOC:
501 loc = &gfc_current_locus;
505 if (arg[pos].type == TYPE_LOCUS)
506 loc = va_arg (argp, locus *);
511 arg[pos].u.stringval = "(2)";
517 arg[pos].u.stringval = "(1)";
522 arg[pos].u.intval = va_arg (argp, int);
526 arg[pos].u.uintval = va_arg (argp, unsigned int);
530 arg[pos].u.longintval = va_arg (argp, long int);
534 arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
538 arg[pos].u.charval = (char) va_arg (argp, int);
542 arg[pos].u.stringval = (const char *) va_arg (argp, char *);
550 for (n = 0; spec[n].pos >= 0; n++)
551 spec[n].u = arg[spec[n].pos].u;
553 /* Show the current loci if we have to. */
567 for (; *format; format++)
571 error_char (*format);
576 if (ISDIGIT (*format))
578 /* This is a position specifier. See comment above. */
579 while (ISDIGIT (*format))
582 /* Skip over the dollar sign. */
593 error_char (spec[n++].u.charval);
597 case 'C': /* Current locus */
598 case 'L': /* Specified locus */
599 error_string (spec[n++].u.stringval);
604 error_integer (spec[n++].u.intval);
608 error_uinteger (spec[n++].u.uintval);
614 error_uinteger (spec[n++].u.ulongintval);
616 error_integer (spec[n++].u.longintval);
626 /* Wrapper for error_print(). */
629 error_printf (const char *nocmsgid, ...)
633 va_start (argp, nocmsgid);
634 error_print ("", _(nocmsgid), argp);
639 /* Increment the number of errors, and check whether too many have
643 gfc_increment_error_count (void)
646 if ((gfc_option.max_errors != 0) && (errors >= gfc_option.max_errors))
647 gfc_fatal_error ("Error count reached limit of %d.", gfc_option.max_errors);
651 /* Issue a warning. */
654 gfc_warning (const char *nocmsgid, ...)
658 if (inhibit_warnings)
661 warning_buffer.flag = 1;
662 warning_buffer.index = 0;
663 cur_error_buffer = &warning_buffer;
665 va_start (argp, nocmsgid);
666 error_print (_("Warning:"), _(nocmsgid), argp);
671 if (buffer_flag == 0)
674 if (warnings_are_errors)
675 gfc_increment_error_count();
680 /* Whether, for a feature included in a given standard set (GFC_STD_*),
681 we should issue an error or a warning, or be quiet. */
684 gfc_notification_std (int std)
688 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
689 if ((gfc_option.allow_std & std) != 0 && !warning)
692 return warning ? WARNING : ERROR;
696 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
697 feature. An error/warning will be issued if the currently selected
698 standard does not contain the requested bits. Return FAILURE if
699 an error is generated. */
702 gfc_notify_std (int std, const char *nocmsgid, ...)
707 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
708 if ((gfc_option.allow_std & std) != 0 && !warning)
711 if (gfc_suppress_error)
712 return warning ? SUCCESS : FAILURE;
714 cur_error_buffer = warning ? &warning_buffer : &error_buffer;
715 cur_error_buffer->flag = 1;
716 cur_error_buffer->index = 0;
718 va_start (argp, nocmsgid);
720 error_print (_("Warning:"), _(nocmsgid), argp);
722 error_print (_("Error:"), _(nocmsgid), argp);
727 if (buffer_flag == 0)
729 if (warning && !warnings_are_errors)
732 gfc_increment_error_count();
735 return (warning && !warnings_are_errors) ? SUCCESS : FAILURE;
739 /* Immediate warning (i.e. do not buffer the warning). */
742 gfc_warning_now (const char *nocmsgid, ...)
747 if (inhibit_warnings)
753 if (warnings_are_errors)
754 gfc_increment_error_count();
756 va_start (argp, nocmsgid);
757 error_print (_("Warning:"), _(nocmsgid), argp);
765 /* Clear the warning flag. */
768 gfc_clear_warning (void)
770 warning_buffer.flag = 0;
774 /* Check to see if any warnings have been saved.
775 If so, print the warning. */
778 gfc_warning_check (void)
780 if (warning_buffer.flag)
783 if (warning_buffer.message != NULL)
784 fputs (warning_buffer.message, stderr);
785 warning_buffer.flag = 0;
790 /* Issue an error. */
793 gfc_error (const char *nocmsgid, ...)
797 if (gfc_suppress_error)
800 error_buffer.flag = 1;
801 error_buffer.index = 0;
802 cur_error_buffer = &error_buffer;
804 va_start (argp, nocmsgid);
805 error_print (_("Error:"), _(nocmsgid), argp);
810 if (buffer_flag == 0)
811 gfc_increment_error_count();
815 /* Immediate error. */
818 gfc_error_now (const char *nocmsgid, ...)
823 error_buffer.flag = 1;
824 error_buffer.index = 0;
825 cur_error_buffer = &error_buffer;
830 va_start (argp, nocmsgid);
831 error_print (_("Error:"), _(nocmsgid), argp);
836 gfc_increment_error_count();
840 if (flag_fatal_errors)
845 /* Fatal error, never returns. */
848 gfc_fatal_error (const char *nocmsgid, ...)
854 va_start (argp, nocmsgid);
855 error_print (_("Fatal Error:"), _(nocmsgid), argp);
862 /* This shouldn't happen... but sometimes does. */
865 gfc_internal_error (const char *format, ...)
871 va_start (argp, format);
873 show_loci (&gfc_current_locus, NULL);
874 error_printf ("Internal Error at (1):");
876 error_print ("", format, argp);
879 exit (ICE_EXIT_CODE);
883 /* Clear the error flag when we start to compile a source line. */
886 gfc_clear_error (void)
888 error_buffer.flag = 0;
892 /* Tests the state of error_flag. */
895 gfc_error_flag_test (void)
897 return error_buffer.flag;
901 /* Check to see if any errors have been saved.
902 If so, print the error. Returns the state of error_flag. */
905 gfc_error_check (void)
909 rc = error_buffer.flag;
911 if (error_buffer.flag)
913 if (error_buffer.message != NULL)
914 fputs (error_buffer.message, stderr);
915 error_buffer.flag = 0;
917 gfc_increment_error_count();
919 if (flag_fatal_errors)
927 /* Save the existing error state. */
930 gfc_push_error (gfc_error_buf *err)
932 err->flag = error_buffer.flag;
933 if (error_buffer.flag)
934 err->message = xstrdup (error_buffer.message);
936 error_buffer.flag = 0;
940 /* Restore a previous pushed error state. */
943 gfc_pop_error (gfc_error_buf *err)
945 error_buffer.flag = err->flag;
946 if (error_buffer.flag)
948 size_t len = strlen (err->message) + 1;
949 gcc_assert (len <= error_buffer.allocated);
950 memcpy (error_buffer.message, err->message, len);
951 gfc_free (err->message);
956 /* Free a pushed error state, but keep the current error state. */
959 gfc_free_error (gfc_error_buf *err)
962 gfc_free (err->message);
966 /* Report the number of warnings and errors that occurred to the caller. */
969 gfc_get_errors (int *w, int *e)