2013-09-06 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / fortran / error.c
blob35fe62706c6a946b25cd95e3efb263fbbcf6321e
1 /* Handle errors.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Niels Kristian Bech Jensen
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Handle the inevitable errors. A major catch here is that things
22 flagged as errors in one match subroutine can conceivably be legal
23 elsewhere. This means that error messages are recorded and saved
24 for possible use later. If a line does not match a legal
25 construction, then the saved error message is reported. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "flags.h"
31 #include "gfortran.h"
33 #ifdef HAVE_TERMIOS_H
34 # include <termios.h>
35 #endif
37 #ifdef GWINSZ_IN_SYS_IOCTL
38 # include <sys/ioctl.h>
39 #endif
42 static int suppress_errors = 0;
44 static int warnings_not_errors = 0;
46 static int terminal_width, buffer_flag, errors, warnings;
48 static gfc_error_buf error_buffer, warning_buffer, *cur_error_buffer;
51 /* Go one level deeper suppressing errors. */
53 void
54 gfc_push_suppress_errors (void)
56 gcc_assert (suppress_errors >= 0);
57 ++suppress_errors;
61 /* Leave one level of error suppressing. */
63 void
64 gfc_pop_suppress_errors (void)
66 gcc_assert (suppress_errors > 0);
67 --suppress_errors;
71 /* Determine terminal width (for trimming source lines in output). */
73 static int
74 get_terminal_width (void)
76 /* Only limit the width if we're outputting to a terminal. */
77 #ifdef HAVE_UNISTD_H
78 if (!isatty (STDERR_FILENO))
79 return INT_MAX;
80 #endif
82 /* Method #1: Use ioctl (not available on all systems). */
83 #ifdef TIOCGWINSZ
84 struct winsize w;
85 w.ws_col = 0;
86 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
87 return w.ws_col;
88 #endif
90 /* Method #2: Query environment variable $COLUMNS. */
91 const char *p = getenv ("COLUMNS");
92 if (p)
94 int value = atoi (p);
95 if (value > 0)
96 return value;
99 /* If both fail, use reasonable default. */
100 return 80;
104 /* Per-file error initialization. */
106 void
107 gfc_error_init_1 (void)
109 terminal_width = get_terminal_width ();
110 errors = 0;
111 warnings = 0;
112 buffer_flag = 0;
116 /* Set the flag for buffering errors or not. */
118 void
119 gfc_buffer_error (int flag)
121 buffer_flag = flag;
125 /* Add a single character to the error buffer or output depending on
126 buffer_flag. */
128 static void
129 error_char (char c)
131 if (buffer_flag)
133 if (cur_error_buffer->index >= cur_error_buffer->allocated)
135 cur_error_buffer->allocated = cur_error_buffer->allocated
136 ? cur_error_buffer->allocated * 2 : 1000;
137 cur_error_buffer->message = XRESIZEVEC (char, cur_error_buffer->message,
138 cur_error_buffer->allocated);
140 cur_error_buffer->message[cur_error_buffer->index++] = c;
142 else
144 if (c != 0)
146 /* We build up complete lines before handing things
147 over to the library in order to speed up error printing. */
148 static char *line;
149 static size_t allocated = 0, index = 0;
151 if (index + 1 >= allocated)
153 allocated = allocated ? allocated * 2 : 1000;
154 line = XRESIZEVEC (char, line, allocated);
156 line[index++] = c;
157 if (c == '\n')
159 line[index] = '\0';
160 fputs (line, stderr);
161 index = 0;
168 /* Copy a string to wherever it needs to go. */
170 static void
171 error_string (const char *p)
173 while (*p)
174 error_char (*p++);
178 /* Print a formatted integer to the error buffer or output. */
180 #define IBUF_LEN 60
182 static void
183 error_uinteger (unsigned long int i)
185 char *p, int_buf[IBUF_LEN];
187 p = int_buf + IBUF_LEN - 1;
188 *p-- = '\0';
190 if (i == 0)
191 *p-- = '0';
193 while (i > 0)
195 *p-- = i % 10 + '0';
196 i = i / 10;
199 error_string (p + 1);
202 static void
203 error_integer (long int i)
205 unsigned long int u;
207 if (i < 0)
209 u = (unsigned long int) -i;
210 error_char ('-');
212 else
213 u = i;
215 error_uinteger (u);
219 static size_t
220 gfc_widechar_display_length (gfc_char_t c)
222 if (gfc_wide_is_printable (c) || c == '\t')
223 /* Printable ASCII character, or tabulation (output as a space). */
224 return 1;
225 else if (c < ((gfc_char_t) 1 << 8))
226 /* Displayed as \x?? */
227 return 4;
228 else if (c < ((gfc_char_t) 1 << 16))
229 /* Displayed as \u???? */
230 return 6;
231 else
232 /* Displayed as \U???????? */
233 return 10;
237 /* Length of the ASCII representation of the wide string, escaping wide
238 characters as print_wide_char_into_buffer() does. */
240 static size_t
241 gfc_wide_display_length (const gfc_char_t *str)
243 size_t i, len;
245 for (i = 0, len = 0; str[i]; i++)
246 len += gfc_widechar_display_length (str[i]);
248 return len;
251 static int
252 print_wide_char_into_buffer (gfc_char_t c, char *buf)
254 static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6',
255 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
257 if (gfc_wide_is_printable (c) || c == '\t')
259 buf[1] = '\0';
260 /* Tabulation is output as a space. */
261 buf[0] = (unsigned char) (c == '\t' ? ' ' : c);
262 return 1;
264 else if (c < ((gfc_char_t) 1 << 8))
266 buf[4] = '\0';
267 buf[3] = xdigit[c & 0x0F];
268 c = c >> 4;
269 buf[2] = xdigit[c & 0x0F];
271 buf[1] = 'x';
272 buf[0] = '\\';
273 return 4;
275 else if (c < ((gfc_char_t) 1 << 16))
277 buf[6] = '\0';
278 buf[5] = xdigit[c & 0x0F];
279 c = c >> 4;
280 buf[4] = xdigit[c & 0x0F];
281 c = c >> 4;
282 buf[3] = xdigit[c & 0x0F];
283 c = c >> 4;
284 buf[2] = xdigit[c & 0x0F];
286 buf[1] = 'u';
287 buf[0] = '\\';
288 return 6;
290 else
292 buf[10] = '\0';
293 buf[9] = xdigit[c & 0x0F];
294 c = c >> 4;
295 buf[8] = xdigit[c & 0x0F];
296 c = c >> 4;
297 buf[7] = xdigit[c & 0x0F];
298 c = c >> 4;
299 buf[6] = xdigit[c & 0x0F];
300 c = c >> 4;
301 buf[5] = xdigit[c & 0x0F];
302 c = c >> 4;
303 buf[4] = xdigit[c & 0x0F];
304 c = c >> 4;
305 buf[3] = xdigit[c & 0x0F];
306 c = c >> 4;
307 buf[2] = xdigit[c & 0x0F];
309 buf[1] = 'U';
310 buf[0] = '\\';
311 return 10;
315 static char wide_char_print_buffer[11];
317 const char *
318 gfc_print_wide_char (gfc_char_t c)
320 print_wide_char_into_buffer (c, wide_char_print_buffer);
321 return wide_char_print_buffer;
325 /* Show the file, where it was included, and the source line, give a
326 locus. Calls error_printf() recursively, but the recursion is at
327 most one level deep. */
329 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
331 static void
332 show_locus (locus *loc, int c1, int c2)
334 gfc_linebuf *lb;
335 gfc_file *f;
336 gfc_char_t *p;
337 int i, offset, cmax;
339 /* TODO: Either limit the total length and number of included files
340 displayed or add buffering of arbitrary number of characters in
341 error messages. */
343 /* Write out the error header line, giving the source file and error
344 location (in GNU standard "[file]:[line].[column]:" format),
345 followed by an "included by" stack and a blank line. This header
346 format is matched by a testsuite parser defined in
347 lib/gfortran-dg.exp. */
349 lb = loc->lb;
350 f = lb->file;
352 error_string (f->filename);
353 error_char (':');
355 error_integer (LOCATION_LINE (lb->location));
357 if ((c1 > 0) || (c2 > 0))
358 error_char ('.');
360 if (c1 > 0)
361 error_integer (c1);
363 if ((c1 > 0) && (c2 > 0))
364 error_char ('-');
366 if (c2 > 0)
367 error_integer (c2);
369 error_char (':');
370 error_char ('\n');
372 for (;;)
374 i = f->inclusion_line;
376 f = f->up;
377 if (f == NULL) break;
379 error_printf (" Included at %s:%d:", f->filename, i);
382 error_char ('\n');
384 /* Calculate an appropriate horizontal offset of the source line in
385 order to get the error locus within the visible portion of the
386 line. Note that if the margin of 5 here is changed, the
387 corresponding margin of 10 in show_loci should be changed. */
389 offset = 0;
391 /* If the two loci would appear in the same column, we shift
392 '2' one column to the right, so as to print '12' rather than
393 just '1'. We do this here so it will be accounted for in the
394 margin calculations. */
396 if (c1 == c2)
397 c2 += 1;
399 cmax = (c1 < c2) ? c2 : c1;
400 if (cmax > terminal_width - 5)
401 offset = cmax - terminal_width + 5;
403 /* Show the line itself, taking care not to print more than what can
404 show up on the terminal. Tabs are converted to spaces, and
405 nonprintable characters are converted to a "\xNN" sequence. */
407 p = &(lb->line[offset]);
408 i = gfc_wide_display_length (p);
409 if (i > terminal_width)
410 i = terminal_width - 1;
412 while (i > 0)
414 static char buffer[11];
415 i -= print_wide_char_into_buffer (*p++, buffer);
416 error_string (buffer);
419 error_char ('\n');
421 /* Show the '1' and/or '2' corresponding to the column of the error
422 locus. Note that a value of -1 for c1 or c2 will simply cause
423 the relevant number not to be printed. */
425 c1 -= offset;
426 c2 -= offset;
427 cmax -= offset;
429 p = &(lb->line[offset]);
430 for (i = 0; i < cmax; i++)
432 int spaces, j;
433 spaces = gfc_widechar_display_length (*p++);
435 if (i == c1)
436 error_char ('1'), spaces--;
437 else if (i == c2)
438 error_char ('2'), spaces--;
440 for (j = 0; j < spaces; j++)
441 error_char (' ');
444 if (i == c1)
445 error_char ('1');
446 else if (i == c2)
447 error_char ('2');
449 error_char ('\n');
454 /* As part of printing an error, we show the source lines that caused
455 the problem. We show at least one, and possibly two loci; the two
456 loci may or may not be on the same source line. */
458 static void
459 show_loci (locus *l1, locus *l2)
461 int m, c1, c2;
463 if (l1 == NULL || l1->lb == NULL)
465 error_printf ("<During initialization>\n");
466 return;
469 /* While calculating parameters for printing the loci, we consider possible
470 reasons for printing one per line. If appropriate, print the loci
471 individually; otherwise we print them both on the same line. */
473 c1 = l1->nextc - l1->lb->line;
474 if (l2 == NULL)
476 show_locus (l1, c1, -1);
477 return;
480 c2 = l2->nextc - l2->lb->line;
482 if (c1 < c2)
483 m = c2 - c1;
484 else
485 m = c1 - c2;
487 /* Note that the margin value of 10 here needs to be less than the
488 margin of 5 used in the calculation of offset in show_locus. */
490 if (l1->lb != l2->lb || m > terminal_width - 10)
492 show_locus (l1, c1, -1);
493 show_locus (l2, -1, c2);
494 return;
497 show_locus (l1, c1, c2);
499 return;
503 /* Workhorse for the error printing subroutines. This subroutine is
504 inspired by g77's error handling and is similar to printf() with
505 the following %-codes:
507 %c Character, %d or %i Integer, %s String, %% Percent
508 %L Takes locus argument
509 %C Current locus (no argument)
511 If a locus pointer is given, the actual source line is printed out
512 and the column is indicated. Since we want the error message at
513 the bottom of any source file information, we must scan the
514 argument list twice -- once to determine whether the loci are
515 present and record this for printing, and once to print the error
516 message after and loci have been printed. A maximum of two locus
517 arguments are permitted.
519 This function is also called (recursively) by show_locus in the
520 case of included files; however, as show_locus does not resupply
521 any loci, the recursion is at most one level deep. */
523 #define MAX_ARGS 10
525 static void ATTRIBUTE_GCC_GFC(2,0)
526 error_print (const char *type, const char *format0, va_list argp)
528 enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
529 TYPE_LONGINT, TYPE_ULONGINT, TYPE_CHAR, TYPE_STRING,
530 NOTYPE };
531 struct
533 int type;
534 int pos;
535 union
537 int intval;
538 unsigned int uintval;
539 long int longintval;
540 unsigned long int ulongintval;
541 char charval;
542 const char * stringval;
543 } u;
544 } arg[MAX_ARGS], spec[MAX_ARGS];
545 /* spec is the array of specifiers, in the same order as they
546 appear in the format string. arg is the array of arguments,
547 in the same order as they appear in the va_list. */
549 char c;
550 int i, n, have_l1, pos, maxpos;
551 locus *l1, *l2, *loc;
552 const char *format;
554 loc = l1 = l2 = NULL;
556 have_l1 = 0;
557 pos = -1;
558 maxpos = -1;
560 n = 0;
561 format = format0;
563 for (i = 0; i < MAX_ARGS; i++)
565 arg[i].type = NOTYPE;
566 spec[i].pos = -1;
569 /* First parse the format string for position specifiers. */
570 while (*format)
572 c = *format++;
573 if (c != '%')
574 continue;
576 if (*format == '%')
578 format++;
579 continue;
582 if (ISDIGIT (*format))
584 /* This is a position specifier. For example, the number
585 12 in the format string "%12$d", which specifies the third
586 argument of the va_list, formatted in %d format.
587 For details, see "man 3 printf". */
588 pos = atoi(format) - 1;
589 gcc_assert (pos >= 0);
590 while (ISDIGIT(*format))
591 format++;
592 gcc_assert (*format == '$');
593 format++;
595 else
596 pos++;
598 c = *format++;
600 if (pos > maxpos)
601 maxpos = pos;
603 switch (c)
605 case 'C':
606 arg[pos].type = TYPE_CURRENTLOC;
607 break;
609 case 'L':
610 arg[pos].type = TYPE_LOCUS;
611 break;
613 case 'd':
614 case 'i':
615 arg[pos].type = TYPE_INTEGER;
616 break;
618 case 'u':
619 arg[pos].type = TYPE_UINTEGER;
620 break;
622 case 'l':
623 c = *format++;
624 if (c == 'u')
625 arg[pos].type = TYPE_ULONGINT;
626 else if (c == 'i' || c == 'd')
627 arg[pos].type = TYPE_LONGINT;
628 else
629 gcc_unreachable ();
630 break;
632 case 'c':
633 arg[pos].type = TYPE_CHAR;
634 break;
636 case 's':
637 arg[pos].type = TYPE_STRING;
638 break;
640 default:
641 gcc_unreachable ();
644 spec[n++].pos = pos;
647 /* Then convert the values for each %-style argument. */
648 for (pos = 0; pos <= maxpos; pos++)
650 gcc_assert (arg[pos].type != NOTYPE);
651 switch (arg[pos].type)
653 case TYPE_CURRENTLOC:
654 loc = &gfc_current_locus;
655 /* Fall through. */
657 case TYPE_LOCUS:
658 if (arg[pos].type == TYPE_LOCUS)
659 loc = va_arg (argp, locus *);
661 if (have_l1)
663 l2 = loc;
664 arg[pos].u.stringval = "(2)";
666 else
668 l1 = loc;
669 have_l1 = 1;
670 arg[pos].u.stringval = "(1)";
672 break;
674 case TYPE_INTEGER:
675 arg[pos].u.intval = va_arg (argp, int);
676 break;
678 case TYPE_UINTEGER:
679 arg[pos].u.uintval = va_arg (argp, unsigned int);
680 break;
682 case TYPE_LONGINT:
683 arg[pos].u.longintval = va_arg (argp, long int);
684 break;
686 case TYPE_ULONGINT:
687 arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
688 break;
690 case TYPE_CHAR:
691 arg[pos].u.charval = (char) va_arg (argp, int);
692 break;
694 case TYPE_STRING:
695 arg[pos].u.stringval = (const char *) va_arg (argp, char *);
696 break;
698 default:
699 gcc_unreachable ();
703 for (n = 0; spec[n].pos >= 0; n++)
704 spec[n].u = arg[spec[n].pos].u;
706 /* Show the current loci if we have to. */
707 if (have_l1)
708 show_loci (l1, l2);
710 if (*type)
712 error_string (type);
713 error_char (' ');
716 have_l1 = 0;
717 format = format0;
718 n = 0;
720 for (; *format; format++)
722 if (*format != '%')
724 error_char (*format);
725 continue;
728 format++;
729 if (ISDIGIT (*format))
731 /* This is a position specifier. See comment above. */
732 while (ISDIGIT (*format))
733 format++;
735 /* Skip over the dollar sign. */
736 format++;
739 switch (*format)
741 case '%':
742 error_char ('%');
743 break;
745 case 'c':
746 error_char (spec[n++].u.charval);
747 break;
749 case 's':
750 case 'C': /* Current locus */
751 case 'L': /* Specified locus */
752 error_string (spec[n++].u.stringval);
753 break;
755 case 'd':
756 case 'i':
757 error_integer (spec[n++].u.intval);
758 break;
760 case 'u':
761 error_uinteger (spec[n++].u.uintval);
762 break;
764 case 'l':
765 format++;
766 if (*format == 'u')
767 error_uinteger (spec[n++].u.ulongintval);
768 else
769 error_integer (spec[n++].u.longintval);
770 break;
775 error_char ('\n');
779 /* Wrapper for error_print(). */
781 static void
782 error_printf (const char *gmsgid, ...)
784 va_list argp;
786 va_start (argp, gmsgid);
787 error_print ("", _(gmsgid), argp);
788 va_end (argp);
792 /* Increment the number of errors, and check whether too many have
793 been printed. */
795 static void
796 gfc_increment_error_count (void)
798 errors++;
799 if ((gfc_option.max_errors != 0) && (errors >= gfc_option.max_errors))
800 gfc_fatal_error ("Error count reached limit of %d.", gfc_option.max_errors);
804 /* Issue a warning. */
806 void
807 gfc_warning (const char *gmsgid, ...)
809 va_list argp;
811 if (inhibit_warnings)
812 return;
814 warning_buffer.flag = 1;
815 warning_buffer.index = 0;
816 cur_error_buffer = &warning_buffer;
818 va_start (argp, gmsgid);
819 error_print (_("Warning:"), _(gmsgid), argp);
820 va_end (argp);
822 error_char ('\0');
824 if (buffer_flag == 0)
826 warnings++;
827 if (warnings_are_errors)
828 gfc_increment_error_count();
833 /* Whether, for a feature included in a given standard set (GFC_STD_*),
834 we should issue an error or a warning, or be quiet. */
836 notification
837 gfc_notification_std (int std)
839 bool warning;
841 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
842 if ((gfc_option.allow_std & std) != 0 && !warning)
843 return SILENT;
845 return warning ? WARNING : ERROR;
849 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
850 feature. An error/warning will be issued if the currently selected
851 standard does not contain the requested bits. Return false if
852 an error is generated. */
854 bool
855 gfc_notify_std (int std, const char *gmsgid, ...)
857 va_list argp;
858 bool warning;
859 const char *msg1, *msg2;
860 char *buffer;
862 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
863 if ((gfc_option.allow_std & std) != 0 && !warning)
864 return true;
866 if (suppress_errors)
867 return warning ? true : false;
869 cur_error_buffer = warning ? &warning_buffer : &error_buffer;
870 cur_error_buffer->flag = 1;
871 cur_error_buffer->index = 0;
873 if (warning)
874 msg1 = _("Warning:");
875 else
876 msg1 = _("Error:");
878 switch (std)
880 case GFC_STD_F2008_TS:
881 msg2 = "TS 29113:";
882 break;
883 case GFC_STD_F2008_OBS:
884 msg2 = _("Fortran 2008 obsolescent feature:");
885 break;
886 case GFC_STD_F2008:
887 msg2 = "Fortran 2008:";
888 break;
889 case GFC_STD_F2003:
890 msg2 = "Fortran 2003:";
891 break;
892 case GFC_STD_GNU:
893 msg2 = _("GNU Extension:");
894 break;
895 case GFC_STD_LEGACY:
896 msg2 = _("Legacy Extension:");
897 break;
898 case GFC_STD_F95_OBS:
899 msg2 = _("Obsolescent feature:");
900 break;
901 case GFC_STD_F95_DEL:
902 msg2 = _("Deleted feature:");
903 break;
904 default:
905 gcc_unreachable ();
908 buffer = (char *) alloca (strlen (msg1) + strlen (msg2) + 2);
909 strcpy (buffer, msg1);
910 strcat (buffer, " ");
911 strcat (buffer, msg2);
913 va_start (argp, gmsgid);
914 error_print (buffer, _(gmsgid), argp);
915 va_end (argp);
917 error_char ('\0');
919 if (buffer_flag == 0)
921 if (warning && !warnings_are_errors)
922 warnings++;
923 else
924 gfc_increment_error_count();
925 cur_error_buffer->flag = 0;
928 return (warning && !warnings_are_errors) ? true : false;
932 /* Immediate warning (i.e. do not buffer the warning). */
934 void
935 gfc_warning_now (const char *gmsgid, ...)
937 va_list argp;
938 int i;
940 if (inhibit_warnings)
941 return;
943 i = buffer_flag;
944 buffer_flag = 0;
945 warnings++;
947 va_start (argp, gmsgid);
948 error_print (_("Warning:"), _(gmsgid), argp);
949 va_end (argp);
951 error_char ('\0');
953 if (warnings_are_errors)
954 gfc_increment_error_count();
956 buffer_flag = i;
960 /* Clear the warning flag. */
962 void
963 gfc_clear_warning (void)
965 warning_buffer.flag = 0;
969 /* Check to see if any warnings have been saved.
970 If so, print the warning. */
972 void
973 gfc_warning_check (void)
975 if (warning_buffer.flag)
977 warnings++;
978 if (warning_buffer.message != NULL)
979 fputs (warning_buffer.message, stderr);
980 warning_buffer.flag = 0;
985 /* Issue an error. */
987 void
988 gfc_error (const char *gmsgid, ...)
990 va_list argp;
992 if (warnings_not_errors)
993 goto warning;
995 if (suppress_errors)
996 return;
998 error_buffer.flag = 1;
999 error_buffer.index = 0;
1000 cur_error_buffer = &error_buffer;
1002 va_start (argp, gmsgid);
1003 error_print (_("Error:"), _(gmsgid), argp);
1004 va_end (argp);
1006 error_char ('\0');
1008 if (buffer_flag == 0)
1009 gfc_increment_error_count();
1011 return;
1013 warning:
1015 if (inhibit_warnings)
1016 return;
1018 warning_buffer.flag = 1;
1019 warning_buffer.index = 0;
1020 cur_error_buffer = &warning_buffer;
1022 va_start (argp, gmsgid);
1023 error_print (_("Warning:"), _(gmsgid), argp);
1024 va_end (argp);
1026 error_char ('\0');
1028 if (buffer_flag == 0)
1030 warnings++;
1031 if (warnings_are_errors)
1032 gfc_increment_error_count();
1037 /* Immediate error. */
1039 void
1040 gfc_error_now (const char *gmsgid, ...)
1042 va_list argp;
1043 int i;
1045 error_buffer.flag = 1;
1046 error_buffer.index = 0;
1047 cur_error_buffer = &error_buffer;
1049 i = buffer_flag;
1050 buffer_flag = 0;
1052 va_start (argp, gmsgid);
1053 error_print (_("Error:"), _(gmsgid), argp);
1054 va_end (argp);
1056 error_char ('\0');
1058 gfc_increment_error_count();
1060 buffer_flag = i;
1062 if (flag_fatal_errors)
1063 exit (FATAL_EXIT_CODE);
1067 /* Fatal error, never returns. */
1069 void
1070 gfc_fatal_error (const char *gmsgid, ...)
1072 va_list argp;
1074 buffer_flag = 0;
1076 va_start (argp, gmsgid);
1077 error_print (_("Fatal Error:"), _(gmsgid), argp);
1078 va_end (argp);
1080 exit (FATAL_EXIT_CODE);
1084 /* This shouldn't happen... but sometimes does. */
1086 void
1087 gfc_internal_error (const char *format, ...)
1089 va_list argp;
1091 buffer_flag = 0;
1093 va_start (argp, format);
1095 show_loci (&gfc_current_locus, NULL);
1096 error_printf ("Internal Error at (1):");
1098 error_print ("", format, argp);
1099 va_end (argp);
1101 exit (ICE_EXIT_CODE);
1105 /* Clear the error flag when we start to compile a source line. */
1107 void
1108 gfc_clear_error (void)
1110 error_buffer.flag = 0;
1111 warnings_not_errors = 0;
1115 /* Tests the state of error_flag. */
1118 gfc_error_flag_test (void)
1120 return error_buffer.flag;
1124 /* Check to see if any errors have been saved.
1125 If so, print the error. Returns the state of error_flag. */
1128 gfc_error_check (void)
1130 int rc;
1132 rc = error_buffer.flag;
1134 if (error_buffer.flag)
1136 if (error_buffer.message != NULL)
1137 fputs (error_buffer.message, stderr);
1138 error_buffer.flag = 0;
1140 gfc_increment_error_count();
1142 if (flag_fatal_errors)
1143 exit (FATAL_EXIT_CODE);
1146 return rc;
1150 /* Save the existing error state. */
1152 void
1153 gfc_push_error (gfc_error_buf *err)
1155 err->flag = error_buffer.flag;
1156 if (error_buffer.flag)
1157 err->message = xstrdup (error_buffer.message);
1159 error_buffer.flag = 0;
1163 /* Restore a previous pushed error state. */
1165 void
1166 gfc_pop_error (gfc_error_buf *err)
1168 error_buffer.flag = err->flag;
1169 if (error_buffer.flag)
1171 size_t len = strlen (err->message) + 1;
1172 gcc_assert (len <= error_buffer.allocated);
1173 memcpy (error_buffer.message, err->message, len);
1174 free (err->message);
1179 /* Free a pushed error state, but keep the current error state. */
1181 void
1182 gfc_free_error (gfc_error_buf *err)
1184 if (err->flag)
1185 free (err->message);
1189 /* Report the number of warnings and errors that occurred to the caller. */
1191 void
1192 gfc_get_errors (int *w, int *e)
1194 if (w != NULL)
1195 *w = warnings;
1196 if (e != NULL)
1197 *e = errors;
1201 /* Switch errors into warnings. */
1203 void
1204 gfc_errors_to_warnings (int f)
1206 warnings_not_errors = (f == 1) ? 1 : 0;