Update concepts branch to revision 131834
[official-gcc.git] / gcc / fortran / error.c
blobc34899f1337267b30cd36efb79e49d659d82b28b
1 /* Handle errors.
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
11 version.
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
16 for more details.
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. */
28 #include "config.h"
29 #include "system.h"
30 #include "flags.h"
31 #include "gfortran.h"
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. */
42 void
43 gfc_error_init_1 (void)
45 terminal_width = gfc_terminal_width ();
46 errors = 0;
47 warnings = 0;
48 buffer_flag = 0;
52 /* Set the flag for buffering errors or not. */
54 void
55 gfc_buffer_error (int flag)
57 buffer_flag = flag;
61 /* Add a single character to the error buffer or output depending on
62 buffer_flag. */
64 static void
65 error_char (char c)
67 if (buffer_flag)
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;
78 else
80 if (c != 0)
82 /* We build up complete lines before handing things
83 over to the library in order to speed up error printing. */
84 static char *line;
85 static size_t allocated = 0, index = 0;
87 if (index + 1 >= allocated)
89 allocated = allocated ? allocated * 2 : 1000;
90 line = xrealloc (line, allocated);
92 line[index++] = c;
93 if (c == '\n')
95 line[index] = '\0';
96 fputs (line, stderr);
97 index = 0;
104 /* Copy a string to wherever it needs to go. */
106 static void
107 error_string (const char *p)
109 while (*p)
110 error_char (*p++);
114 /* Print a formatted integer to the error buffer or output. */
116 #define IBUF_LEN 60
118 static void
119 error_uinteger (unsigned long int i)
121 char *p, int_buf[IBUF_LEN];
123 p = int_buf + IBUF_LEN - 1;
124 *p-- = '\0';
126 if (i == 0)
127 *p-- = '0';
129 while (i > 0)
131 *p-- = i % 10 + '0';
132 i = i / 10;
135 error_string (p + 1);
138 static void
139 error_integer (long int i)
141 unsigned long int u;
143 if (i < 0)
145 u = (unsigned long int) -i;
146 error_char ('-');
148 else
149 u = i;
151 error_uinteger (u);
155 static void
156 print_wide_char_into_buffer (gfc_char_t c, char *buf)
158 static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6',
159 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
161 if (gfc_wide_is_printable (c))
163 buf[1] = '\0';
164 buf[0] = (unsigned char) c;
166 else if (c < ((gfc_char_t) 1 << 8))
168 buf[4] = '\0';
169 buf[3] = xdigit[c & 0x0F];
170 c = c >> 4;
171 buf[2] = xdigit[c & 0x0F];
173 buf[1] = 'x';
174 buf[0] = '\\';
176 else if (c < ((gfc_char_t) 1 << 16))
178 buf[6] = '\0';
179 buf[5] = xdigit[c & 0x0F];
180 c = c >> 4;
181 buf[4] = xdigit[c & 0x0F];
182 c = c >> 4;
183 buf[3] = xdigit[c & 0x0F];
184 c = c >> 4;
185 buf[2] = xdigit[c & 0x0F];
187 buf[1] = 'u';
188 buf[0] = '\\';
190 else
192 buf[10] = '\0';
193 buf[9] = xdigit[c & 0x0F];
194 c = c >> 4;
195 buf[8] = xdigit[c & 0x0F];
196 c = c >> 4;
197 buf[7] = xdigit[c & 0x0F];
198 c = c >> 4;
199 buf[6] = xdigit[c & 0x0F];
200 c = c >> 4;
201 buf[5] = xdigit[c & 0x0F];
202 c = c >> 4;
203 buf[4] = xdigit[c & 0x0F];
204 c = c >> 4;
205 buf[3] = xdigit[c & 0x0F];
206 c = c >> 4;
207 buf[2] = xdigit[c & 0x0F];
209 buf[1] = 'U';
210 buf[0] = '\\';
214 static char wide_char_print_buffer[11];
216 const char *
217 gfc_print_wide_char (gfc_char_t c)
219 print_wide_char_into_buffer (c, wide_char_print_buffer);
220 return wide_char_print_buffer;
224 /* Show the file, where it was included, and the source line, give a
225 locus. Calls error_printf() recursively, but the recursion is at
226 most one level deep. */
228 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
230 static void
231 show_locus (locus *loc, int c1, int c2)
233 gfc_linebuf *lb;
234 gfc_file *f;
235 gfc_char_t c, *p;
236 int i, offset, cmax;
238 /* TODO: Either limit the total length and number of included files
239 displayed or add buffering of arbitrary number of characters in
240 error messages. */
242 /* Write out the error header line, giving the source file and error
243 location (in GNU standard "[file]:[line].[column]:" format),
244 followed by an "included by" stack and a blank line. This header
245 format is matched by a testsuite parser defined in
246 lib/gfortran-dg.exp. */
248 lb = loc->lb;
249 f = lb->file;
251 error_string (f->filename);
252 error_char (':');
254 error_integer (LOCATION_LINE (lb->location));
256 if ((c1 > 0) || (c2 > 0))
257 error_char ('.');
259 if (c1 > 0)
260 error_integer (c1);
262 if ((c1 > 0) && (c2 > 0))
263 error_char ('-');
265 if (c2 > 0)
266 error_integer (c2);
268 error_char (':');
269 error_char ('\n');
271 for (;;)
273 i = f->inclusion_line;
275 f = f->up;
276 if (f == NULL) break;
278 error_printf (" Included at %s:%d:", f->filename, i);
281 error_char ('\n');
283 /* Calculate an appropriate horizontal offset of the source line in
284 order to get the error locus within the visible portion of the
285 line. Note that if the margin of 5 here is changed, the
286 corresponding margin of 10 in show_loci should be changed. */
288 offset = 0;
290 /* When the loci is not associated with a column, it will have a
291 value of zero. We adjust this to 1 so that it will appear. */
293 if (c1 == 0)
294 c1 = 1;
295 if (c2 == 0)
296 c2 = 1;
298 /* If the two loci would appear in the same column, we shift
299 '2' one column to the right, so as to print '12' rather than
300 just '1'. We do this here so it will be accounted for in the
301 margin calculations. */
303 if (c1 == c2)
304 c2 += 1;
306 cmax = (c1 < c2) ? c2 : c1;
307 if (cmax > terminal_width - 5)
308 offset = cmax - terminal_width + 5;
310 /* Show the line itself, taking care not to print more than what can
311 show up on the terminal. Tabs are converted to spaces, and
312 nonprintable characters are converted to a "\xNN" sequence. */
314 /* TODO: Although setting i to the terminal width is clever, it fails
315 to work correctly when nonprintable characters exist. A better
316 solution should be found. */
318 p = &(lb->line[offset]);
319 i = gfc_wide_strlen (p);
320 if (i > terminal_width)
321 i = terminal_width - 1;
323 for (; i > 0; i--)
325 static char buffer[11];
327 c = *p++;
328 if (c == '\t')
329 c = ' ';
331 print_wide_char_into_buffer (c, buffer);
332 error_string (buffer);
335 error_char ('\n');
337 /* Show the '1' and/or '2' corresponding to the column of the error
338 locus. Note that a value of -1 for c1 or c2 will simply cause
339 the relevant number not to be printed. */
341 c1 -= offset;
342 c2 -= offset;
344 for (i = 1; i <= cmax; i++)
346 if (i == c1)
347 error_char ('1');
348 else if (i == c2)
349 error_char ('2');
350 else
351 error_char (' ');
354 error_char ('\n');
359 /* As part of printing an error, we show the source lines that caused
360 the problem. We show at least one, and possibly two loci; the two
361 loci may or may not be on the same source line. */
363 static void
364 show_loci (locus *l1, locus *l2)
366 int m, c1, c2;
368 if (l1 == NULL || l1->lb == NULL)
370 error_printf ("<During initialization>\n");
371 return;
374 /* While calculating parameters for printing the loci, we consider possible
375 reasons for printing one per line. If appropriate, print the loci
376 individually; otherwise we print them both on the same line. */
378 c1 = l1->nextc - l1->lb->line;
379 if (l2 == NULL)
381 show_locus (l1, c1, -1);
382 return;
385 c2 = l2->nextc - l2->lb->line;
387 if (c1 < c2)
388 m = c2 - c1;
389 else
390 m = c1 - c2;
392 /* Note that the margin value of 10 here needs to be less than the
393 margin of 5 used in the calculation of offset in show_locus. */
395 if (l1->lb != l2->lb || m > terminal_width - 10)
397 show_locus (l1, c1, -1);
398 show_locus (l2, -1, c2);
399 return;
402 show_locus (l1, c1, c2);
404 return;
408 /* Workhorse for the error printing subroutines. This subroutine is
409 inspired by g77's error handling and is similar to printf() with
410 the following %-codes:
412 %c Character, %d or %i Integer, %s String, %% Percent
413 %L Takes locus argument
414 %C Current locus (no argument)
416 If a locus pointer is given, the actual source line is printed out
417 and the column is indicated. Since we want the error message at
418 the bottom of any source file information, we must scan the
419 argument list twice -- once to determine whether the loci are
420 present and record this for printing, and once to print the error
421 message after and loci have been printed. A maximum of two locus
422 arguments are permitted.
424 This function is also called (recursively) by show_locus in the
425 case of included files; however, as show_locus does not resupply
426 any loci, the recursion is at most one level deep. */
428 #define MAX_ARGS 10
430 static void ATTRIBUTE_GCC_GFC(2,0)
431 error_print (const char *type, const char *format0, va_list argp)
433 enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
434 TYPE_LONGINT, TYPE_ULONGINT, TYPE_CHAR, TYPE_STRING,
435 NOTYPE };
436 struct
438 int type;
439 int pos;
440 union
442 int intval;
443 unsigned int uintval;
444 long int longintval;
445 unsigned long int ulongintval;
446 char charval;
447 const char * stringval;
448 } u;
449 } arg[MAX_ARGS], spec[MAX_ARGS];
450 /* spec is the array of specifiers, in the same order as they
451 appear in the format string. arg is the array of arguments,
452 in the same order as they appear in the va_list. */
454 char c;
455 int i, n, have_l1, pos, maxpos;
456 locus *l1, *l2, *loc;
457 const char *format;
459 l1 = l2 = NULL;
461 have_l1 = 0;
462 pos = -1;
463 maxpos = -1;
465 n = 0;
466 format = format0;
468 for (i = 0; i < MAX_ARGS; i++)
470 arg[i].type = NOTYPE;
471 spec[i].pos = -1;
474 /* First parse the format string for position specifiers. */
475 while (*format)
477 c = *format++;
478 if (c != '%')
479 continue;
481 if (*format == '%')
483 format++;
484 continue;
487 if (ISDIGIT (*format))
489 /* This is a position specifier. For example, the number
490 12 in the format string "%12$d", which specifies the third
491 argument of the va_list, formatted in %d format.
492 For details, see "man 3 printf". */
493 pos = atoi(format) - 1;
494 gcc_assert (pos >= 0);
495 while (ISDIGIT(*format))
496 format++;
497 gcc_assert (*format++ == '$');
499 else
500 pos++;
502 c = *format++;
504 if (pos > maxpos)
505 maxpos = pos;
507 switch (c)
509 case 'C':
510 arg[pos].type = TYPE_CURRENTLOC;
511 break;
513 case 'L':
514 arg[pos].type = TYPE_LOCUS;
515 break;
517 case 'd':
518 case 'i':
519 arg[pos].type = TYPE_INTEGER;
520 break;
522 case 'u':
523 arg[pos].type = TYPE_UINTEGER;
525 case 'l':
526 c = *format++;
527 if (c == 'u')
528 arg[pos].type = TYPE_ULONGINT;
529 else if (c == 'i' || c == 'd')
530 arg[pos].type = TYPE_LONGINT;
531 else
532 gcc_unreachable ();
533 break;
535 case 'c':
536 arg[pos].type = TYPE_CHAR;
537 break;
539 case 's':
540 arg[pos].type = TYPE_STRING;
541 break;
543 default:
544 gcc_unreachable ();
547 spec[n++].pos = pos;
550 /* Then convert the values for each %-style argument. */
551 for (pos = 0; pos <= maxpos; pos++)
553 gcc_assert (arg[pos].type != NOTYPE);
554 switch (arg[pos].type)
556 case TYPE_CURRENTLOC:
557 loc = &gfc_current_locus;
558 /* Fall through. */
560 case TYPE_LOCUS:
561 if (arg[pos].type == TYPE_LOCUS)
562 loc = va_arg (argp, locus *);
564 if (have_l1)
566 l2 = loc;
567 arg[pos].u.stringval = "(2)";
569 else
571 l1 = loc;
572 have_l1 = 1;
573 arg[pos].u.stringval = "(1)";
575 break;
577 case TYPE_INTEGER:
578 arg[pos].u.intval = va_arg (argp, int);
579 break;
581 case TYPE_UINTEGER:
582 arg[pos].u.uintval = va_arg (argp, unsigned int);
583 break;
585 case TYPE_LONGINT:
586 arg[pos].u.longintval = va_arg (argp, long int);
587 break;
589 case TYPE_ULONGINT:
590 arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
591 break;
593 case TYPE_CHAR:
594 arg[pos].u.charval = (char) va_arg (argp, int);
595 break;
597 case TYPE_STRING:
598 arg[pos].u.stringval = (const char *) va_arg (argp, char *);
599 break;
601 default:
602 gcc_unreachable ();
606 for (n = 0; spec[n].pos >= 0; n++)
607 spec[n].u = arg[spec[n].pos].u;
609 /* Show the current loci if we have to. */
610 if (have_l1)
611 show_loci (l1, l2);
613 if (*type)
615 error_string (type);
616 error_char (' ');
619 have_l1 = 0;
620 format = format0;
621 n = 0;
623 for (; *format; format++)
625 if (*format != '%')
627 error_char (*format);
628 continue;
631 format++;
632 if (ISDIGIT (*format))
634 /* This is a position specifier. See comment above. */
635 while (ISDIGIT (*format))
636 format++;
638 /* Skip over the dollar sign. */
639 format++;
642 switch (*format)
644 case '%':
645 error_char ('%');
646 break;
648 case 'c':
649 error_char (spec[n++].u.charval);
650 break;
652 case 's':
653 case 'C': /* Current locus */
654 case 'L': /* Specified locus */
655 error_string (spec[n++].u.stringval);
656 break;
658 case 'd':
659 case 'i':
660 error_integer (spec[n++].u.intval);
661 break;
663 case 'u':
664 error_uinteger (spec[n++].u.uintval);
665 break;
667 case 'l':
668 format++;
669 if (*format == 'u')
670 error_uinteger (spec[n++].u.ulongintval);
671 else
672 error_integer (spec[n++].u.longintval);
673 break;
678 error_char ('\n');
682 /* Wrapper for error_print(). */
684 static void
685 error_printf (const char *nocmsgid, ...)
687 va_list argp;
689 va_start (argp, nocmsgid);
690 error_print ("", _(nocmsgid), argp);
691 va_end (argp);
695 /* Increment the number of errors, and check whether too many have
696 been printed. */
698 static void
699 gfc_increment_error_count (void)
701 errors++;
702 if ((gfc_option.max_errors != 0) && (errors >= gfc_option.max_errors))
703 gfc_fatal_error ("Error count reached limit of %d.", gfc_option.max_errors);
707 /* Issue a warning. */
709 void
710 gfc_warning (const char *nocmsgid, ...)
712 va_list argp;
714 if (inhibit_warnings)
715 return;
717 warning_buffer.flag = 1;
718 warning_buffer.index = 0;
719 cur_error_buffer = &warning_buffer;
721 va_start (argp, nocmsgid);
722 error_print (_("Warning:"), _(nocmsgid), argp);
723 va_end (argp);
725 error_char ('\0');
727 if (buffer_flag == 0)
729 warnings++;
730 if (warnings_are_errors)
731 gfc_increment_error_count();
736 /* Whether, for a feature included in a given standard set (GFC_STD_*),
737 we should issue an error or a warning, or be quiet. */
739 notification
740 gfc_notification_std (int std)
742 bool warning;
744 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
745 if ((gfc_option.allow_std & std) != 0 && !warning)
746 return SILENT;
748 return warning ? WARNING : ERROR;
752 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
753 feature. An error/warning will be issued if the currently selected
754 standard does not contain the requested bits. Return FAILURE if
755 an error is generated. */
758 gfc_notify_std (int std, const char *nocmsgid, ...)
760 va_list argp;
761 bool warning;
763 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
764 if ((gfc_option.allow_std & std) != 0 && !warning)
765 return SUCCESS;
767 if (gfc_suppress_error)
768 return warning ? SUCCESS : FAILURE;
770 cur_error_buffer = warning ? &warning_buffer : &error_buffer;
771 cur_error_buffer->flag = 1;
772 cur_error_buffer->index = 0;
774 va_start (argp, nocmsgid);
775 if (warning)
776 error_print (_("Warning:"), _(nocmsgid), argp);
777 else
778 error_print (_("Error:"), _(nocmsgid), argp);
779 va_end (argp);
781 error_char ('\0');
783 if (buffer_flag == 0)
785 if (warning && !warnings_are_errors)
786 warnings++;
787 else
788 gfc_increment_error_count();
791 return (warning && !warnings_are_errors) ? SUCCESS : FAILURE;
795 /* Immediate warning (i.e. do not buffer the warning). */
797 void
798 gfc_warning_now (const char *nocmsgid, ...)
800 va_list argp;
801 int i;
803 if (inhibit_warnings)
804 return;
806 i = buffer_flag;
807 buffer_flag = 0;
808 warnings++;
809 if (warnings_are_errors)
810 gfc_increment_error_count();
812 va_start (argp, nocmsgid);
813 error_print (_("Warning:"), _(nocmsgid), argp);
814 va_end (argp);
816 error_char ('\0');
817 buffer_flag = i;
821 /* Clear the warning flag. */
823 void
824 gfc_clear_warning (void)
826 warning_buffer.flag = 0;
830 /* Check to see if any warnings have been saved.
831 If so, print the warning. */
833 void
834 gfc_warning_check (void)
836 if (warning_buffer.flag)
838 warnings++;
839 if (warning_buffer.message != NULL)
840 fputs (warning_buffer.message, stderr);
841 warning_buffer.flag = 0;
846 /* Issue an error. */
848 void
849 gfc_error (const char *nocmsgid, ...)
851 va_list argp;
853 if (gfc_suppress_error)
854 return;
856 error_buffer.flag = 1;
857 error_buffer.index = 0;
858 cur_error_buffer = &error_buffer;
860 va_start (argp, nocmsgid);
861 error_print (_("Error:"), _(nocmsgid), argp);
862 va_end (argp);
864 error_char ('\0');
866 if (buffer_flag == 0)
867 gfc_increment_error_count();
871 /* Immediate error. */
873 void
874 gfc_error_now (const char *nocmsgid, ...)
876 va_list argp;
877 int i;
879 error_buffer.flag = 1;
880 error_buffer.index = 0;
881 cur_error_buffer = &error_buffer;
883 i = buffer_flag;
884 buffer_flag = 0;
886 va_start (argp, nocmsgid);
887 error_print (_("Error:"), _(nocmsgid), argp);
888 va_end (argp);
890 error_char ('\0');
892 gfc_increment_error_count();
894 buffer_flag = i;
896 if (flag_fatal_errors)
897 exit (1);
901 /* Fatal error, never returns. */
903 void
904 gfc_fatal_error (const char *nocmsgid, ...)
906 va_list argp;
908 buffer_flag = 0;
910 va_start (argp, nocmsgid);
911 error_print (_("Fatal Error:"), _(nocmsgid), argp);
912 va_end (argp);
914 exit (3);
918 /* This shouldn't happen... but sometimes does. */
920 void
921 gfc_internal_error (const char *format, ...)
923 va_list argp;
925 buffer_flag = 0;
927 va_start (argp, format);
929 show_loci (&gfc_current_locus, NULL);
930 error_printf ("Internal Error at (1):");
932 error_print ("", format, argp);
933 va_end (argp);
935 exit (ICE_EXIT_CODE);
939 /* Clear the error flag when we start to compile a source line. */
941 void
942 gfc_clear_error (void)
944 error_buffer.flag = 0;
948 /* Tests the state of error_flag. */
951 gfc_error_flag_test (void)
953 return error_buffer.flag;
957 /* Check to see if any errors have been saved.
958 If so, print the error. Returns the state of error_flag. */
961 gfc_error_check (void)
963 int rc;
965 rc = error_buffer.flag;
967 if (error_buffer.flag)
969 if (error_buffer.message != NULL)
970 fputs (error_buffer.message, stderr);
971 error_buffer.flag = 0;
973 gfc_increment_error_count();
975 if (flag_fatal_errors)
976 exit (1);
979 return rc;
983 /* Save the existing error state. */
985 void
986 gfc_push_error (gfc_error_buf *err)
988 err->flag = error_buffer.flag;
989 if (error_buffer.flag)
990 err->message = xstrdup (error_buffer.message);
992 error_buffer.flag = 0;
996 /* Restore a previous pushed error state. */
998 void
999 gfc_pop_error (gfc_error_buf *err)
1001 error_buffer.flag = err->flag;
1002 if (error_buffer.flag)
1004 size_t len = strlen (err->message) + 1;
1005 gcc_assert (len <= error_buffer.allocated);
1006 memcpy (error_buffer.message, err->message, len);
1007 gfc_free (err->message);
1012 /* Free a pushed error state, but keep the current error state. */
1014 void
1015 gfc_free_error (gfc_error_buf *err)
1017 if (err->flag)
1018 gfc_free (err->message);
1022 /* Report the number of warnings and errors that occurred to the caller. */
1024 void
1025 gfc_get_errors (int *w, int *e)
1027 if (w != NULL)
1028 *w = warnings;
1029 if (e != NULL)
1030 *e = errors;