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 static int suppress_errors = 0;
35 static int warnings_not_errors = 0;
37 static int terminal_width, buffer_flag, errors, warnings;
39 static gfc_error_buf error_buffer, warning_buffer, *cur_error_buffer;
42 /* Go one level deeper suppressing errors. */
45 gfc_push_suppress_errors (void)
47 gcc_assert (suppress_errors >= 0);
52 /* Leave one level of error suppressing. */
55 gfc_pop_suppress_errors (void)
57 gcc_assert (suppress_errors > 0);
62 /* Per-file error initialization. */
65 gfc_error_init_1 (void)
67 terminal_width = gfc_terminal_width ();
74 /* Set the flag for buffering errors or not. */
77 gfc_buffer_error (int flag)
83 /* Add a single character to the error buffer or output depending on
91 if (cur_error_buffer->index >= cur_error_buffer->allocated)
93 cur_error_buffer->allocated = cur_error_buffer->allocated
94 ? cur_error_buffer->allocated * 2 : 1000;
95 cur_error_buffer->message = XRESIZEVEC (char, cur_error_buffer->message,
96 cur_error_buffer->allocated);
98 cur_error_buffer->message[cur_error_buffer->index++] = c;
104 /* We build up complete lines before handing things
105 over to the library in order to speed up error printing. */
107 static size_t allocated = 0, index = 0;
109 if (index + 1 >= allocated)
111 allocated = allocated ? allocated * 2 : 1000;
112 line = XRESIZEVEC (char, line, allocated);
118 fputs (line, stderr);
126 /* Copy a string to wherever it needs to go. */
129 error_string (const char *p)
136 /* Print a formatted integer to the error buffer or output. */
141 error_uinteger (unsigned long int i)
143 char *p, int_buf[IBUF_LEN];
145 p = int_buf + IBUF_LEN - 1;
157 error_string (p + 1);
161 error_integer (long int i)
167 u = (unsigned long int) -i;
178 print_wide_char_into_buffer (gfc_char_t c, char *buf)
180 static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6',
181 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
183 if (gfc_wide_is_printable (c))
186 buf[0] = (unsigned char) c;
188 else if (c < ((gfc_char_t) 1 << 8))
191 buf[3] = xdigit[c & 0x0F];
193 buf[2] = xdigit[c & 0x0F];
198 else if (c < ((gfc_char_t) 1 << 16))
201 buf[5] = xdigit[c & 0x0F];
203 buf[4] = xdigit[c & 0x0F];
205 buf[3] = xdigit[c & 0x0F];
207 buf[2] = xdigit[c & 0x0F];
215 buf[9] = xdigit[c & 0x0F];
217 buf[8] = xdigit[c & 0x0F];
219 buf[7] = xdigit[c & 0x0F];
221 buf[6] = xdigit[c & 0x0F];
223 buf[5] = xdigit[c & 0x0F];
225 buf[4] = xdigit[c & 0x0F];
227 buf[3] = xdigit[c & 0x0F];
229 buf[2] = xdigit[c & 0x0F];
236 static char wide_char_print_buffer[11];
239 gfc_print_wide_char (gfc_char_t c)
241 print_wide_char_into_buffer (c, wide_char_print_buffer);
242 return wide_char_print_buffer;
246 /* Show the file, where it was included, and the source line, give a
247 locus. Calls error_printf() recursively, but the recursion is at
248 most one level deep. */
250 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
253 show_locus (locus *loc, int c1, int c2)
260 /* TODO: Either limit the total length and number of included files
261 displayed or add buffering of arbitrary number of characters in
264 /* Write out the error header line, giving the source file and error
265 location (in GNU standard "[file]:[line].[column]:" format),
266 followed by an "included by" stack and a blank line. This header
267 format is matched by a testsuite parser defined in
268 lib/gfortran-dg.exp. */
273 error_string (f->filename);
276 error_integer (LOCATION_LINE (lb->location));
278 if ((c1 > 0) || (c2 > 0))
284 if ((c1 > 0) && (c2 > 0))
295 i = f->inclusion_line;
298 if (f == NULL) break;
300 error_printf (" Included at %s:%d:", f->filename, i);
305 /* Calculate an appropriate horizontal offset of the source line in
306 order to get the error locus within the visible portion of the
307 line. Note that if the margin of 5 here is changed, the
308 corresponding margin of 10 in show_loci should be changed. */
312 /* If the two loci would appear in the same column, we shift
313 '2' one column to the right, so as to print '12' rather than
314 just '1'. We do this here so it will be accounted for in the
315 margin calculations. */
320 cmax = (c1 < c2) ? c2 : c1;
321 if (cmax > terminal_width - 5)
322 offset = cmax - terminal_width + 5;
324 /* Show the line itself, taking care not to print more than what can
325 show up on the terminal. Tabs are converted to spaces, and
326 nonprintable characters are converted to a "\xNN" sequence. */
328 /* TODO: Although setting i to the terminal width is clever, it fails
329 to work correctly when nonprintable characters exist. A better
330 solution should be found. */
332 p = &(lb->line[offset]);
333 i = gfc_wide_strlen (p);
334 if (i > terminal_width)
335 i = terminal_width - 1;
339 static char buffer[11];
345 print_wide_char_into_buffer (c, buffer);
346 error_string (buffer);
351 /* Show the '1' and/or '2' corresponding to the column of the error
352 locus. Note that a value of -1 for c1 or c2 will simply cause
353 the relevant number not to be printed. */
358 for (i = 0; i <= cmax; i++)
373 /* As part of printing an error, we show the source lines that caused
374 the problem. We show at least one, and possibly two loci; the two
375 loci may or may not be on the same source line. */
378 show_loci (locus *l1, locus *l2)
382 if (l1 == NULL || l1->lb == NULL)
384 error_printf ("<During initialization>\n");
388 /* While calculating parameters for printing the loci, we consider possible
389 reasons for printing one per line. If appropriate, print the loci
390 individually; otherwise we print them both on the same line. */
392 c1 = l1->nextc - l1->lb->line;
395 show_locus (l1, c1, -1);
399 c2 = l2->nextc - l2->lb->line;
406 /* Note that the margin value of 10 here needs to be less than the
407 margin of 5 used in the calculation of offset in show_locus. */
409 if (l1->lb != l2->lb || m > terminal_width - 10)
411 show_locus (l1, c1, -1);
412 show_locus (l2, -1, c2);
416 show_locus (l1, c1, c2);
422 /* Workhorse for the error printing subroutines. This subroutine is
423 inspired by g77's error handling and is similar to printf() with
424 the following %-codes:
426 %c Character, %d or %i Integer, %s String, %% Percent
427 %L Takes locus argument
428 %C Current locus (no argument)
430 If a locus pointer is given, the actual source line is printed out
431 and the column is indicated. Since we want the error message at
432 the bottom of any source file information, we must scan the
433 argument list twice -- once to determine whether the loci are
434 present and record this for printing, and once to print the error
435 message after and loci have been printed. A maximum of two locus
436 arguments are permitted.
438 This function is also called (recursively) by show_locus in the
439 case of included files; however, as show_locus does not resupply
440 any loci, the recursion is at most one level deep. */
444 static void ATTRIBUTE_GCC_GFC(2,0)
445 error_print (const char *type, const char *format0, va_list argp)
447 enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
448 TYPE_LONGINT, TYPE_ULONGINT, TYPE_CHAR, TYPE_STRING,
457 unsigned int uintval;
459 unsigned long int ulongintval;
461 const char * stringval;
463 } arg[MAX_ARGS], spec[MAX_ARGS];
464 /* spec is the array of specifiers, in the same order as they
465 appear in the format string. arg is the array of arguments,
466 in the same order as they appear in the va_list. */
469 int i, n, have_l1, pos, maxpos;
470 locus *l1, *l2, *loc;
482 for (i = 0; i < MAX_ARGS; i++)
484 arg[i].type = NOTYPE;
488 /* First parse the format string for position specifiers. */
501 if (ISDIGIT (*format))
503 /* This is a position specifier. For example, the number
504 12 in the format string "%12$d", which specifies the third
505 argument of the va_list, formatted in %d format.
506 For details, see "man 3 printf". */
507 pos = atoi(format) - 1;
508 gcc_assert (pos >= 0);
509 while (ISDIGIT(*format))
511 gcc_assert (*format++ == '$');
524 arg[pos].type = TYPE_CURRENTLOC;
528 arg[pos].type = TYPE_LOCUS;
533 arg[pos].type = TYPE_INTEGER;
537 arg[pos].type = TYPE_UINTEGER;
543 arg[pos].type = TYPE_ULONGINT;
544 else if (c == 'i' || c == 'd')
545 arg[pos].type = TYPE_LONGINT;
551 arg[pos].type = TYPE_CHAR;
555 arg[pos].type = TYPE_STRING;
565 /* Then convert the values for each %-style argument. */
566 for (pos = 0; pos <= maxpos; pos++)
568 gcc_assert (arg[pos].type != NOTYPE);
569 switch (arg[pos].type)
571 case TYPE_CURRENTLOC:
572 loc = &gfc_current_locus;
576 if (arg[pos].type == TYPE_LOCUS)
577 loc = va_arg (argp, locus *);
582 arg[pos].u.stringval = "(2)";
588 arg[pos].u.stringval = "(1)";
593 arg[pos].u.intval = va_arg (argp, int);
597 arg[pos].u.uintval = va_arg (argp, unsigned int);
601 arg[pos].u.longintval = va_arg (argp, long int);
605 arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
609 arg[pos].u.charval = (char) va_arg (argp, int);
613 arg[pos].u.stringval = (const char *) va_arg (argp, char *);
621 for (n = 0; spec[n].pos >= 0; n++)
622 spec[n].u = arg[spec[n].pos].u;
624 /* Show the current loci if we have to. */
638 for (; *format; format++)
642 error_char (*format);
647 if (ISDIGIT (*format))
649 /* This is a position specifier. See comment above. */
650 while (ISDIGIT (*format))
653 /* Skip over the dollar sign. */
664 error_char (spec[n++].u.charval);
668 case 'C': /* Current locus */
669 case 'L': /* Specified locus */
670 error_string (spec[n++].u.stringval);
675 error_integer (spec[n++].u.intval);
679 error_uinteger (spec[n++].u.uintval);
685 error_uinteger (spec[n++].u.ulongintval);
687 error_integer (spec[n++].u.longintval);
697 /* Wrapper for error_print(). */
700 error_printf (const char *nocmsgid, ...)
704 va_start (argp, nocmsgid);
705 error_print ("", _(nocmsgid), argp);
710 /* Increment the number of errors, and check whether too many have
714 gfc_increment_error_count (void)
717 if ((gfc_option.max_errors != 0) && (errors >= gfc_option.max_errors))
718 gfc_fatal_error ("Error count reached limit of %d.", gfc_option.max_errors);
722 /* Issue a warning. */
725 gfc_warning (const char *nocmsgid, ...)
729 if (inhibit_warnings)
732 warning_buffer.flag = 1;
733 warning_buffer.index = 0;
734 cur_error_buffer = &warning_buffer;
736 va_start (argp, nocmsgid);
737 error_print (_("Warning:"), _(nocmsgid), argp);
742 if (buffer_flag == 0)
745 if (warnings_are_errors)
746 gfc_increment_error_count();
751 /* Whether, for a feature included in a given standard set (GFC_STD_*),
752 we should issue an error or a warning, or be quiet. */
755 gfc_notification_std (int std)
759 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
760 if ((gfc_option.allow_std & std) != 0 && !warning)
763 return warning ? WARNING : ERROR;
767 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
768 feature. An error/warning will be issued if the currently selected
769 standard does not contain the requested bits. Return FAILURE if
770 an error is generated. */
773 gfc_notify_std (int std, const char *nocmsgid, ...)
778 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
779 if ((gfc_option.allow_std & std) != 0 && !warning)
783 return warning ? SUCCESS : FAILURE;
785 cur_error_buffer = warning ? &warning_buffer : &error_buffer;
786 cur_error_buffer->flag = 1;
787 cur_error_buffer->index = 0;
789 va_start (argp, nocmsgid);
791 error_print (_("Warning:"), _(nocmsgid), argp);
793 error_print (_("Error:"), _(nocmsgid), argp);
798 if (buffer_flag == 0)
800 if (warning && !warnings_are_errors)
803 gfc_increment_error_count();
806 return (warning && !warnings_are_errors) ? SUCCESS : FAILURE;
810 /* Immediate warning (i.e. do not buffer the warning). */
813 gfc_warning_now (const char *nocmsgid, ...)
818 if (inhibit_warnings)
824 if (warnings_are_errors)
825 gfc_increment_error_count();
827 va_start (argp, nocmsgid);
828 error_print (_("Warning:"), _(nocmsgid), argp);
836 /* Clear the warning flag. */
839 gfc_clear_warning (void)
841 warning_buffer.flag = 0;
845 /* Check to see if any warnings have been saved.
846 If so, print the warning. */
849 gfc_warning_check (void)
851 if (warning_buffer.flag)
854 if (warning_buffer.message != NULL)
855 fputs (warning_buffer.message, stderr);
856 warning_buffer.flag = 0;
861 /* Issue an error. */
864 gfc_error (const char *nocmsgid, ...)
868 if (warnings_not_errors)
874 error_buffer.flag = 1;
875 error_buffer.index = 0;
876 cur_error_buffer = &error_buffer;
878 va_start (argp, nocmsgid);
879 error_print (_("Error:"), _(nocmsgid), argp);
884 if (buffer_flag == 0)
885 gfc_increment_error_count();
891 if (inhibit_warnings)
894 warning_buffer.flag = 1;
895 warning_buffer.index = 0;
896 cur_error_buffer = &warning_buffer;
898 va_start (argp, nocmsgid);
899 error_print (_("Warning:"), _(nocmsgid), argp);
904 if (buffer_flag == 0)
907 if (warnings_are_errors)
908 gfc_increment_error_count();
913 /* Immediate error. */
916 gfc_error_now (const char *nocmsgid, ...)
921 error_buffer.flag = 1;
922 error_buffer.index = 0;
923 cur_error_buffer = &error_buffer;
928 va_start (argp, nocmsgid);
929 error_print (_("Error:"), _(nocmsgid), argp);
934 gfc_increment_error_count();
938 if (flag_fatal_errors)
943 /* Fatal error, never returns. */
946 gfc_fatal_error (const char *nocmsgid, ...)
952 va_start (argp, nocmsgid);
953 error_print (_("Fatal Error:"), _(nocmsgid), argp);
960 /* This shouldn't happen... but sometimes does. */
963 gfc_internal_error (const char *format, ...)
969 va_start (argp, format);
971 show_loci (&gfc_current_locus, NULL);
972 error_printf ("Internal Error at (1):");
974 error_print ("", format, argp);
977 exit (ICE_EXIT_CODE);
981 /* Clear the error flag when we start to compile a source line. */
984 gfc_clear_error (void)
986 error_buffer.flag = 0;
987 warnings_not_errors = 0;
991 /* Tests the state of error_flag. */
994 gfc_error_flag_test (void)
996 return error_buffer.flag;
1000 /* Check to see if any errors have been saved.
1001 If so, print the error. Returns the state of error_flag. */
1004 gfc_error_check (void)
1008 rc = error_buffer.flag;
1010 if (error_buffer.flag)
1012 if (error_buffer.message != NULL)
1013 fputs (error_buffer.message, stderr);
1014 error_buffer.flag = 0;
1016 gfc_increment_error_count();
1018 if (flag_fatal_errors)
1026 /* Save the existing error state. */
1029 gfc_push_error (gfc_error_buf *err)
1031 err->flag = error_buffer.flag;
1032 if (error_buffer.flag)
1033 err->message = xstrdup (error_buffer.message);
1035 error_buffer.flag = 0;
1039 /* Restore a previous pushed error state. */
1042 gfc_pop_error (gfc_error_buf *err)
1044 error_buffer.flag = err->flag;
1045 if (error_buffer.flag)
1047 size_t len = strlen (err->message) + 1;
1048 gcc_assert (len <= error_buffer.allocated);
1049 memcpy (error_buffer.message, err->message, len);
1050 gfc_free (err->message);
1055 /* Free a pushed error state, but keep the current error state. */
1058 gfc_free_error (gfc_error_buf *err)
1061 gfc_free (err->message);
1065 /* Report the number of warnings and errors that occurred to the caller. */
1068 gfc_get_errors (int *w, int *e)
1077 /* Switch errors into warnings. */
1080 gfc_errors_to_warnings (int f)
1082 warnings_not_errors = (f == 1) ? 1 : 0;