2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught & Niels Kristian Bech Jensen
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
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. */
31 #include "coretypes.h"
35 static int suppress_errors = 0;
37 static int warnings_not_errors = 0;
39 static int terminal_width, buffer_flag, errors, warnings;
41 static gfc_error_buf error_buffer, warning_buffer, *cur_error_buffer;
44 /* Go one level deeper suppressing errors. */
47 gfc_push_suppress_errors (void)
49 gcc_assert (suppress_errors >= 0);
54 /* Leave one level of error suppressing. */
57 gfc_pop_suppress_errors (void)
59 gcc_assert (suppress_errors > 0);
64 /* Per-file error initialization. */
67 gfc_error_init_1 (void)
69 terminal_width = gfc_terminal_width ();
76 /* Set the flag for buffering errors or not. */
79 gfc_buffer_error (int flag)
85 /* Add a single character to the error buffer or output depending on
93 if (cur_error_buffer->index >= cur_error_buffer->allocated)
95 cur_error_buffer->allocated = cur_error_buffer->allocated
96 ? cur_error_buffer->allocated * 2 : 1000;
97 cur_error_buffer->message = XRESIZEVEC (char, cur_error_buffer->message,
98 cur_error_buffer->allocated);
100 cur_error_buffer->message[cur_error_buffer->index++] = c;
106 /* We build up complete lines before handing things
107 over to the library in order to speed up error printing. */
109 static size_t allocated = 0, index = 0;
111 if (index + 1 >= allocated)
113 allocated = allocated ? allocated * 2 : 1000;
114 line = XRESIZEVEC (char, line, allocated);
120 fputs (line, stderr);
128 /* Copy a string to wherever it needs to go. */
131 error_string (const char *p)
138 /* Print a formatted integer to the error buffer or output. */
143 error_uinteger (unsigned long int i)
145 char *p, int_buf[IBUF_LEN];
147 p = int_buf + IBUF_LEN - 1;
159 error_string (p + 1);
163 error_integer (long int i)
169 u = (unsigned long int) -i;
180 gfc_widechar_display_length (gfc_char_t c)
182 if (gfc_wide_is_printable (c) || c == '\t')
183 /* Printable ASCII character, or tabulation (output as a space). */
185 else if (c < ((gfc_char_t) 1 << 8))
186 /* Displayed as \x?? */
188 else if (c < ((gfc_char_t) 1 << 16))
189 /* Displayed as \u???? */
192 /* Displayed as \U???????? */
197 /* Length of the ASCII representation of the wide string, escaping wide
198 characters as print_wide_char_into_buffer() does. */
201 gfc_wide_display_length (const gfc_char_t *str)
205 for (i = 0, len = 0; str[i]; i++)
206 len += gfc_widechar_display_length (str[i]);
212 print_wide_char_into_buffer (gfc_char_t c, char *buf)
214 static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6',
215 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
217 if (gfc_wide_is_printable (c) || c == '\t')
220 /* Tabulation is output as a space. */
221 buf[0] = (unsigned char) (c == '\t' ? ' ' : c);
224 else if (c < ((gfc_char_t) 1 << 8))
227 buf[3] = xdigit[c & 0x0F];
229 buf[2] = xdigit[c & 0x0F];
235 else if (c < ((gfc_char_t) 1 << 16))
238 buf[5] = xdigit[c & 0x0F];
240 buf[4] = xdigit[c & 0x0F];
242 buf[3] = xdigit[c & 0x0F];
244 buf[2] = xdigit[c & 0x0F];
253 buf[9] = xdigit[c & 0x0F];
255 buf[8] = xdigit[c & 0x0F];
257 buf[7] = xdigit[c & 0x0F];
259 buf[6] = xdigit[c & 0x0F];
261 buf[5] = xdigit[c & 0x0F];
263 buf[4] = xdigit[c & 0x0F];
265 buf[3] = xdigit[c & 0x0F];
267 buf[2] = xdigit[c & 0x0F];
275 static char wide_char_print_buffer[11];
278 gfc_print_wide_char (gfc_char_t c)
280 print_wide_char_into_buffer (c, wide_char_print_buffer);
281 return wide_char_print_buffer;
285 /* Show the file, where it was included, and the source line, give a
286 locus. Calls error_printf() recursively, but the recursion is at
287 most one level deep. */
289 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
292 show_locus (locus *loc, int c1, int c2)
299 /* TODO: Either limit the total length and number of included files
300 displayed or add buffering of arbitrary number of characters in
303 /* Write out the error header line, giving the source file and error
304 location (in GNU standard "[file]:[line].[column]:" format),
305 followed by an "included by" stack and a blank line. This header
306 format is matched by a testsuite parser defined in
307 lib/gfortran-dg.exp. */
312 error_string (f->filename);
315 error_integer (LOCATION_LINE (lb->location));
317 if ((c1 > 0) || (c2 > 0))
323 if ((c1 > 0) && (c2 > 0))
334 i = f->inclusion_line;
337 if (f == NULL) break;
339 error_printf (" Included at %s:%d:", f->filename, i);
344 /* Calculate an appropriate horizontal offset of the source line in
345 order to get the error locus within the visible portion of the
346 line. Note that if the margin of 5 here is changed, the
347 corresponding margin of 10 in show_loci should be changed. */
351 /* If the two loci would appear in the same column, we shift
352 '2' one column to the right, so as to print '12' rather than
353 just '1'. We do this here so it will be accounted for in the
354 margin calculations. */
359 cmax = (c1 < c2) ? c2 : c1;
360 if (cmax > terminal_width - 5)
361 offset = cmax - terminal_width + 5;
363 /* Show the line itself, taking care not to print more than what can
364 show up on the terminal. Tabs are converted to spaces, and
365 nonprintable characters are converted to a "\xNN" sequence. */
367 p = &(lb->line[offset]);
368 i = gfc_wide_display_length (p);
369 if (i > terminal_width)
370 i = terminal_width - 1;
374 static char buffer[11];
375 i -= print_wide_char_into_buffer (*p++, buffer);
376 error_string (buffer);
381 /* Show the '1' and/or '2' corresponding to the column of the error
382 locus. Note that a value of -1 for c1 or c2 will simply cause
383 the relevant number not to be printed. */
389 p = &(lb->line[offset]);
390 for (i = 0; i < cmax; i++)
393 spaces = gfc_widechar_display_length (*p++);
396 error_char ('1'), spaces--;
398 error_char ('2'), spaces--;
400 for (j = 0; j < spaces; j++)
414 /* As part of printing an error, we show the source lines that caused
415 the problem. We show at least one, and possibly two loci; the two
416 loci may or may not be on the same source line. */
419 show_loci (locus *l1, locus *l2)
423 if (l1 == NULL || l1->lb == NULL)
425 error_printf ("<During initialization>\n");
429 /* While calculating parameters for printing the loci, we consider possible
430 reasons for printing one per line. If appropriate, print the loci
431 individually; otherwise we print them both on the same line. */
433 c1 = l1->nextc - l1->lb->line;
436 show_locus (l1, c1, -1);
440 c2 = l2->nextc - l2->lb->line;
447 /* Note that the margin value of 10 here needs to be less than the
448 margin of 5 used in the calculation of offset in show_locus. */
450 if (l1->lb != l2->lb || m > terminal_width - 10)
452 show_locus (l1, c1, -1);
453 show_locus (l2, -1, c2);
457 show_locus (l1, c1, c2);
463 /* Workhorse for the error printing subroutines. This subroutine is
464 inspired by g77's error handling and is similar to printf() with
465 the following %-codes:
467 %c Character, %d or %i Integer, %s String, %% Percent
468 %L Takes locus argument
469 %C Current locus (no argument)
471 If a locus pointer is given, the actual source line is printed out
472 and the column is indicated. Since we want the error message at
473 the bottom of any source file information, we must scan the
474 argument list twice -- once to determine whether the loci are
475 present and record this for printing, and once to print the error
476 message after and loci have been printed. A maximum of two locus
477 arguments are permitted.
479 This function is also called (recursively) by show_locus in the
480 case of included files; however, as show_locus does not resupply
481 any loci, the recursion is at most one level deep. */
485 static void ATTRIBUTE_GCC_GFC(2,0)
486 error_print (const char *type, const char *format0, va_list argp)
488 enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
489 TYPE_LONGINT, TYPE_ULONGINT, TYPE_CHAR, TYPE_STRING,
498 unsigned int uintval;
500 unsigned long int ulongintval;
502 const char * stringval;
504 } arg[MAX_ARGS], spec[MAX_ARGS];
505 /* spec is the array of specifiers, in the same order as they
506 appear in the format string. arg is the array of arguments,
507 in the same order as they appear in the va_list. */
510 int i, n, have_l1, pos, maxpos;
511 locus *l1, *l2, *loc;
514 loc = l1 = l2 = NULL;
523 for (i = 0; i < MAX_ARGS; i++)
525 arg[i].type = NOTYPE;
529 /* First parse the format string for position specifiers. */
542 if (ISDIGIT (*format))
544 /* This is a position specifier. For example, the number
545 12 in the format string "%12$d", which specifies the third
546 argument of the va_list, formatted in %d format.
547 For details, see "man 3 printf". */
548 pos = atoi(format) - 1;
549 gcc_assert (pos >= 0);
550 while (ISDIGIT(*format))
552 gcc_assert (*format == '$');
566 arg[pos].type = TYPE_CURRENTLOC;
570 arg[pos].type = TYPE_LOCUS;
575 arg[pos].type = TYPE_INTEGER;
579 arg[pos].type = TYPE_UINTEGER;
585 arg[pos].type = TYPE_ULONGINT;
586 else if (c == 'i' || c == 'd')
587 arg[pos].type = TYPE_LONGINT;
593 arg[pos].type = TYPE_CHAR;
597 arg[pos].type = TYPE_STRING;
607 /* Then convert the values for each %-style argument. */
608 for (pos = 0; pos <= maxpos; pos++)
610 gcc_assert (arg[pos].type != NOTYPE);
611 switch (arg[pos].type)
613 case TYPE_CURRENTLOC:
614 loc = &gfc_current_locus;
618 if (arg[pos].type == TYPE_LOCUS)
619 loc = va_arg (argp, locus *);
624 arg[pos].u.stringval = "(2)";
630 arg[pos].u.stringval = "(1)";
635 arg[pos].u.intval = va_arg (argp, int);
639 arg[pos].u.uintval = va_arg (argp, unsigned int);
643 arg[pos].u.longintval = va_arg (argp, long int);
647 arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
651 arg[pos].u.charval = (char) va_arg (argp, int);
655 arg[pos].u.stringval = (const char *) va_arg (argp, char *);
663 for (n = 0; spec[n].pos >= 0; n++)
664 spec[n].u = arg[spec[n].pos].u;
666 /* Show the current loci if we have to. */
680 for (; *format; format++)
684 error_char (*format);
689 if (ISDIGIT (*format))
691 /* This is a position specifier. See comment above. */
692 while (ISDIGIT (*format))
695 /* Skip over the dollar sign. */
706 error_char (spec[n++].u.charval);
710 case 'C': /* Current locus */
711 case 'L': /* Specified locus */
712 error_string (spec[n++].u.stringval);
717 error_integer (spec[n++].u.intval);
721 error_uinteger (spec[n++].u.uintval);
727 error_uinteger (spec[n++].u.ulongintval);
729 error_integer (spec[n++].u.longintval);
739 /* Wrapper for error_print(). */
742 error_printf (const char *gmsgid, ...)
746 va_start (argp, gmsgid);
747 error_print ("", _(gmsgid), argp);
752 /* Increment the number of errors, and check whether too many have
756 gfc_increment_error_count (void)
759 if ((gfc_option.max_errors != 0) && (errors >= gfc_option.max_errors))
760 gfc_fatal_error ("Error count reached limit of %d.", gfc_option.max_errors);
764 /* Issue a warning. */
767 gfc_warning (const char *gmsgid, ...)
771 if (inhibit_warnings)
774 warning_buffer.flag = 1;
775 warning_buffer.index = 0;
776 cur_error_buffer = &warning_buffer;
778 va_start (argp, gmsgid);
779 error_print (_("Warning:"), _(gmsgid), argp);
784 if (buffer_flag == 0)
787 if (warnings_are_errors)
788 gfc_increment_error_count();
793 /* Whether, for a feature included in a given standard set (GFC_STD_*),
794 we should issue an error or a warning, or be quiet. */
797 gfc_notification_std (int std)
801 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
802 if ((gfc_option.allow_std & std) != 0 && !warning)
805 return warning ? WARNING : ERROR;
809 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
810 feature. An error/warning will be issued if the currently selected
811 standard does not contain the requested bits. Return FAILURE if
812 an error is generated. */
815 gfc_notify_std (int std, const char *gmsgid, ...)
819 const char *msg1, *msg2;
822 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
823 if ((gfc_option.allow_std & std) != 0 && !warning)
827 return warning ? SUCCESS : FAILURE;
829 cur_error_buffer = warning ? &warning_buffer : &error_buffer;
830 cur_error_buffer->flag = 1;
831 cur_error_buffer->index = 0;
834 msg1 = _("Warning:");
840 case GFC_STD_F2008_TS:
843 case GFC_STD_F2008_OBS:
844 msg2 = _("Fortran 2008 obsolescent feature:");
847 msg2 = "Fortran 2008:";
850 msg2 = "Fortran 2003:";
853 msg2 = _("GNU Extension:");
856 msg2 = _("Legacy Extension:");
858 case GFC_STD_F95_OBS:
859 msg2 = _("Obsolescent feature:");
861 case GFC_STD_F95_DEL:
862 msg2 = _("Deleted feature:");
868 buffer = (char *) alloca (strlen (msg1) + strlen (msg2) + 2);
869 strcpy (buffer, msg1);
870 strcat (buffer, " ");
871 strcat (buffer, msg2);
873 va_start (argp, gmsgid);
874 error_print (buffer, _(gmsgid), argp);
879 if (buffer_flag == 0)
881 if (warning && !warnings_are_errors)
884 gfc_increment_error_count();
885 cur_error_buffer->flag = 0;
888 return (warning && !warnings_are_errors) ? SUCCESS : FAILURE;
892 /* Immediate warning (i.e. do not buffer the warning). */
895 gfc_warning_now (const char *gmsgid, ...)
900 if (inhibit_warnings)
907 va_start (argp, gmsgid);
908 error_print (_("Warning:"), _(gmsgid), argp);
913 if (warnings_are_errors)
914 gfc_increment_error_count();
920 /* Clear the warning flag. */
923 gfc_clear_warning (void)
925 warning_buffer.flag = 0;
929 /* Check to see if any warnings have been saved.
930 If so, print the warning. */
933 gfc_warning_check (void)
935 if (warning_buffer.flag)
938 if (warning_buffer.message != NULL)
939 fputs (warning_buffer.message, stderr);
940 warning_buffer.flag = 0;
945 /* Issue an error. */
948 gfc_error (const char *gmsgid, ...)
952 if (warnings_not_errors)
958 error_buffer.flag = 1;
959 error_buffer.index = 0;
960 cur_error_buffer = &error_buffer;
962 va_start (argp, gmsgid);
963 error_print (_("Error:"), _(gmsgid), argp);
968 if (buffer_flag == 0)
969 gfc_increment_error_count();
975 if (inhibit_warnings)
978 warning_buffer.flag = 1;
979 warning_buffer.index = 0;
980 cur_error_buffer = &warning_buffer;
982 va_start (argp, gmsgid);
983 error_print (_("Warning:"), _(gmsgid), argp);
988 if (buffer_flag == 0)
991 if (warnings_are_errors)
992 gfc_increment_error_count();
997 /* Immediate error. */
1000 gfc_error_now (const char *gmsgid, ...)
1005 error_buffer.flag = 1;
1006 error_buffer.index = 0;
1007 cur_error_buffer = &error_buffer;
1012 va_start (argp, gmsgid);
1013 error_print (_("Error:"), _(gmsgid), argp);
1018 gfc_increment_error_count();
1022 if (flag_fatal_errors)
1023 exit (FATAL_EXIT_CODE);
1027 /* Fatal error, never returns. */
1030 gfc_fatal_error (const char *gmsgid, ...)
1036 va_start (argp, gmsgid);
1037 error_print (_("Fatal Error:"), _(gmsgid), argp);
1040 exit (FATAL_EXIT_CODE);
1044 /* This shouldn't happen... but sometimes does. */
1047 gfc_internal_error (const char *format, ...)
1053 va_start (argp, format);
1055 show_loci (&gfc_current_locus, NULL);
1056 error_printf ("Internal Error at (1):");
1058 error_print ("", format, argp);
1061 exit (ICE_EXIT_CODE);
1065 /* Clear the error flag when we start to compile a source line. */
1068 gfc_clear_error (void)
1070 error_buffer.flag = 0;
1071 warnings_not_errors = 0;
1075 /* Tests the state of error_flag. */
1078 gfc_error_flag_test (void)
1080 return error_buffer.flag;
1084 /* Check to see if any errors have been saved.
1085 If so, print the error. Returns the state of error_flag. */
1088 gfc_error_check (void)
1092 rc = error_buffer.flag;
1094 if (error_buffer.flag)
1096 if (error_buffer.message != NULL)
1097 fputs (error_buffer.message, stderr);
1098 error_buffer.flag = 0;
1100 gfc_increment_error_count();
1102 if (flag_fatal_errors)
1103 exit (FATAL_EXIT_CODE);
1110 /* Save the existing error state. */
1113 gfc_push_error (gfc_error_buf *err)
1115 err->flag = error_buffer.flag;
1116 if (error_buffer.flag)
1117 err->message = xstrdup (error_buffer.message);
1119 error_buffer.flag = 0;
1123 /* Restore a previous pushed error state. */
1126 gfc_pop_error (gfc_error_buf *err)
1128 error_buffer.flag = err->flag;
1129 if (error_buffer.flag)
1131 size_t len = strlen (err->message) + 1;
1132 gcc_assert (len <= error_buffer.allocated);
1133 memcpy (error_buffer.message, err->message, len);
1134 free (err->message);
1139 /* Free a pushed error state, but keep the current error state. */
1142 gfc_free_error (gfc_error_buf *err)
1145 free (err->message);
1149 /* Report the number of warnings and errors that occurred to the caller. */
1152 gfc_get_errors (int *w, int *e)
1161 /* Switch errors into warnings. */
1164 gfc_errors_to_warnings (int f)
1166 warnings_not_errors = (f == 1) ? 1 : 0;