2014-10-24 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / fortran / error.c
blobb623ea82c0132ea6a6136ce4de04f70106f632b6
1 /* Handle errors.
2 Copyright (C) 2000-2014 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
41 #include "diagnostic.h"
42 #include "diagnostic-color.h"
44 static int suppress_errors = 0;
46 static int warnings_not_errors = 0;
48 static int terminal_width, buffer_flag, errors, warnings;
50 static gfc_error_buf error_buffer, warning_buffer, *cur_error_buffer;
53 /* Go one level deeper suppressing errors. */
55 void
56 gfc_push_suppress_errors (void)
58 gcc_assert (suppress_errors >= 0);
59 ++suppress_errors;
63 /* Leave one level of error suppressing. */
65 void
66 gfc_pop_suppress_errors (void)
68 gcc_assert (suppress_errors > 0);
69 --suppress_errors;
73 /* Determine terminal width (for trimming source lines in output). */
75 static int
76 get_terminal_width (void)
78 /* Only limit the width if we're outputting to a terminal. */
79 #ifdef HAVE_UNISTD_H
80 if (!isatty (STDERR_FILENO))
81 return INT_MAX;
82 #endif
84 /* Method #1: Use ioctl (not available on all systems). */
85 #ifdef TIOCGWINSZ
86 struct winsize w;
87 w.ws_col = 0;
88 if (ioctl (0, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
89 return w.ws_col;
90 #endif
92 /* Method #2: Query environment variable $COLUMNS. */
93 const char *p = getenv ("COLUMNS");
94 if (p)
96 int value = atoi (p);
97 if (value > 0)
98 return value;
101 /* If both fail, use reasonable default. */
102 return 80;
106 /* Per-file error initialization. */
108 void
109 gfc_error_init_1 (void)
111 terminal_width = get_terminal_width ();
112 errors = 0;
113 warnings = 0;
114 buffer_flag = 0;
118 /* Set the flag for buffering errors or not. */
120 void
121 gfc_buffer_error (int flag)
123 buffer_flag = flag;
127 /* Add a single character to the error buffer or output depending on
128 buffer_flag. */
130 static void
131 error_char (char c)
133 if (buffer_flag)
135 if (cur_error_buffer->index >= cur_error_buffer->allocated)
137 cur_error_buffer->allocated = cur_error_buffer->allocated
138 ? cur_error_buffer->allocated * 2 : 1000;
139 cur_error_buffer->message = XRESIZEVEC (char, cur_error_buffer->message,
140 cur_error_buffer->allocated);
142 cur_error_buffer->message[cur_error_buffer->index++] = c;
144 else
146 if (c != 0)
148 /* We build up complete lines before handing things
149 over to the library in order to speed up error printing. */
150 static char *line;
151 static size_t allocated = 0, index = 0;
153 if (index + 1 >= allocated)
155 allocated = allocated ? allocated * 2 : 1000;
156 line = XRESIZEVEC (char, line, allocated);
158 line[index++] = c;
159 if (c == '\n')
161 line[index] = '\0';
162 fputs (line, stderr);
163 index = 0;
170 /* Copy a string to wherever it needs to go. */
172 static void
173 error_string (const char *p)
175 while (*p)
176 error_char (*p++);
180 /* Print a formatted integer to the error buffer or output. */
182 #define IBUF_LEN 60
184 static void
185 error_uinteger (unsigned long int i)
187 char *p, int_buf[IBUF_LEN];
189 p = int_buf + IBUF_LEN - 1;
190 *p-- = '\0';
192 if (i == 0)
193 *p-- = '0';
195 while (i > 0)
197 *p-- = i % 10 + '0';
198 i = i / 10;
201 error_string (p + 1);
204 static void
205 error_integer (long int i)
207 unsigned long int u;
209 if (i < 0)
211 u = (unsigned long int) -i;
212 error_char ('-');
214 else
215 u = i;
217 error_uinteger (u);
221 static size_t
222 gfc_widechar_display_length (gfc_char_t c)
224 if (gfc_wide_is_printable (c) || c == '\t')
225 /* Printable ASCII character, or tabulation (output as a space). */
226 return 1;
227 else if (c < ((gfc_char_t) 1 << 8))
228 /* Displayed as \x?? */
229 return 4;
230 else if (c < ((gfc_char_t) 1 << 16))
231 /* Displayed as \u???? */
232 return 6;
233 else
234 /* Displayed as \U???????? */
235 return 10;
239 /* Length of the ASCII representation of the wide string, escaping wide
240 characters as print_wide_char_into_buffer() does. */
242 static size_t
243 gfc_wide_display_length (const gfc_char_t *str)
245 size_t i, len;
247 for (i = 0, len = 0; str[i]; i++)
248 len += gfc_widechar_display_length (str[i]);
250 return len;
253 static int
254 print_wide_char_into_buffer (gfc_char_t c, char *buf)
256 static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6',
257 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
259 if (gfc_wide_is_printable (c) || c == '\t')
261 buf[1] = '\0';
262 /* Tabulation is output as a space. */
263 buf[0] = (unsigned char) (c == '\t' ? ' ' : c);
264 return 1;
266 else if (c < ((gfc_char_t) 1 << 8))
268 buf[4] = '\0';
269 buf[3] = xdigit[c & 0x0F];
270 c = c >> 4;
271 buf[2] = xdigit[c & 0x0F];
273 buf[1] = 'x';
274 buf[0] = '\\';
275 return 4;
277 else if (c < ((gfc_char_t) 1 << 16))
279 buf[6] = '\0';
280 buf[5] = xdigit[c & 0x0F];
281 c = c >> 4;
282 buf[4] = xdigit[c & 0x0F];
283 c = c >> 4;
284 buf[3] = xdigit[c & 0x0F];
285 c = c >> 4;
286 buf[2] = xdigit[c & 0x0F];
288 buf[1] = 'u';
289 buf[0] = '\\';
290 return 6;
292 else
294 buf[10] = '\0';
295 buf[9] = xdigit[c & 0x0F];
296 c = c >> 4;
297 buf[8] = xdigit[c & 0x0F];
298 c = c >> 4;
299 buf[7] = xdigit[c & 0x0F];
300 c = c >> 4;
301 buf[6] = xdigit[c & 0x0F];
302 c = c >> 4;
303 buf[5] = xdigit[c & 0x0F];
304 c = c >> 4;
305 buf[4] = xdigit[c & 0x0F];
306 c = c >> 4;
307 buf[3] = xdigit[c & 0x0F];
308 c = c >> 4;
309 buf[2] = xdigit[c & 0x0F];
311 buf[1] = 'U';
312 buf[0] = '\\';
313 return 10;
317 static char wide_char_print_buffer[11];
319 const char *
320 gfc_print_wide_char (gfc_char_t c)
322 print_wide_char_into_buffer (c, wide_char_print_buffer);
323 return wide_char_print_buffer;
327 /* Show the file, where it was included, and the source line, give a
328 locus. Calls error_printf() recursively, but the recursion is at
329 most one level deep. */
331 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
333 static void
334 show_locus (locus *loc, int c1, int c2)
336 gfc_linebuf *lb;
337 gfc_file *f;
338 gfc_char_t *p;
339 int i, offset, cmax;
341 /* TODO: Either limit the total length and number of included files
342 displayed or add buffering of arbitrary number of characters in
343 error messages. */
345 /* Write out the error header line, giving the source file and error
346 location (in GNU standard "[file]:[line].[column]:" format),
347 followed by an "included by" stack and a blank line. This header
348 format is matched by a testsuite parser defined in
349 lib/gfortran-dg.exp. */
351 lb = loc->lb;
352 f = lb->file;
354 error_string (f->filename);
355 error_char (':');
357 error_integer (LOCATION_LINE (lb->location));
359 if ((c1 > 0) || (c2 > 0))
360 error_char ('.');
362 if (c1 > 0)
363 error_integer (c1);
365 if ((c1 > 0) && (c2 > 0))
366 error_char ('-');
368 if (c2 > 0)
369 error_integer (c2);
371 error_char (':');
372 error_char ('\n');
374 for (;;)
376 i = f->inclusion_line;
378 f = f->up;
379 if (f == NULL) break;
381 error_printf (" Included at %s:%d:", f->filename, i);
384 error_char ('\n');
386 /* Calculate an appropriate horizontal offset of the source line in
387 order to get the error locus within the visible portion of the
388 line. Note that if the margin of 5 here is changed, the
389 corresponding margin of 10 in show_loci should be changed. */
391 offset = 0;
393 /* If the two loci would appear in the same column, we shift
394 '2' one column to the right, so as to print '12' rather than
395 just '1'. We do this here so it will be accounted for in the
396 margin calculations. */
398 if (c1 == c2)
399 c2 += 1;
401 cmax = (c1 < c2) ? c2 : c1;
402 if (cmax > terminal_width - 5)
403 offset = cmax - terminal_width + 5;
405 /* Show the line itself, taking care not to print more than what can
406 show up on the terminal. Tabs are converted to spaces, and
407 nonprintable characters are converted to a "\xNN" sequence. */
409 p = &(lb->line[offset]);
410 i = gfc_wide_display_length (p);
411 if (i > terminal_width)
412 i = terminal_width - 1;
414 while (i > 0)
416 static char buffer[11];
417 i -= print_wide_char_into_buffer (*p++, buffer);
418 error_string (buffer);
421 error_char ('\n');
423 /* Show the '1' and/or '2' corresponding to the column of the error
424 locus. Note that a value of -1 for c1 or c2 will simply cause
425 the relevant number not to be printed. */
427 c1 -= offset;
428 c2 -= offset;
429 cmax -= offset;
431 p = &(lb->line[offset]);
432 for (i = 0; i < cmax; i++)
434 int spaces, j;
435 spaces = gfc_widechar_display_length (*p++);
437 if (i == c1)
438 error_char ('1'), spaces--;
439 else if (i == c2)
440 error_char ('2'), spaces--;
442 for (j = 0; j < spaces; j++)
443 error_char (' ');
446 if (i == c1)
447 error_char ('1');
448 else if (i == c2)
449 error_char ('2');
451 error_char ('\n');
456 /* As part of printing an error, we show the source lines that caused
457 the problem. We show at least one, and possibly two loci; the two
458 loci may or may not be on the same source line. */
460 static void
461 show_loci (locus *l1, locus *l2)
463 int m, c1, c2;
465 if (l1 == NULL || l1->lb == NULL)
467 error_printf ("<During initialization>\n");
468 return;
471 /* While calculating parameters for printing the loci, we consider possible
472 reasons for printing one per line. If appropriate, print the loci
473 individually; otherwise we print them both on the same line. */
475 c1 = l1->nextc - l1->lb->line;
476 if (l2 == NULL)
478 show_locus (l1, c1, -1);
479 return;
482 c2 = l2->nextc - l2->lb->line;
484 if (c1 < c2)
485 m = c2 - c1;
486 else
487 m = c1 - c2;
489 /* Note that the margin value of 10 here needs to be less than the
490 margin of 5 used in the calculation of offset in show_locus. */
492 if (l1->lb != l2->lb || m > terminal_width - 10)
494 show_locus (l1, c1, -1);
495 show_locus (l2, -1, c2);
496 return;
499 show_locus (l1, c1, c2);
501 return;
505 /* Workhorse for the error printing subroutines. This subroutine is
506 inspired by g77's error handling and is similar to printf() with
507 the following %-codes:
509 %c Character, %d or %i Integer, %s String, %% Percent
510 %L Takes locus argument
511 %C Current locus (no argument)
513 If a locus pointer is given, the actual source line is printed out
514 and the column is indicated. Since we want the error message at
515 the bottom of any source file information, we must scan the
516 argument list twice -- once to determine whether the loci are
517 present and record this for printing, and once to print the error
518 message after and loci have been printed. A maximum of two locus
519 arguments are permitted.
521 This function is also called (recursively) by show_locus in the
522 case of included files; however, as show_locus does not resupply
523 any loci, the recursion is at most one level deep. */
525 #define MAX_ARGS 10
527 static void ATTRIBUTE_GCC_GFC(2,0)
528 error_print (const char *type, const char *format0, va_list argp)
530 enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
531 TYPE_LONGINT, TYPE_ULONGINT, TYPE_CHAR, TYPE_STRING,
532 NOTYPE };
533 struct
535 int type;
536 int pos;
537 union
539 int intval;
540 unsigned int uintval;
541 long int longintval;
542 unsigned long int ulongintval;
543 char charval;
544 const char * stringval;
545 } u;
546 } arg[MAX_ARGS], spec[MAX_ARGS];
547 /* spec is the array of specifiers, in the same order as they
548 appear in the format string. arg is the array of arguments,
549 in the same order as they appear in the va_list. */
551 char c;
552 int i, n, have_l1, pos, maxpos;
553 locus *l1, *l2, *loc;
554 const char *format;
556 loc = l1 = l2 = NULL;
558 have_l1 = 0;
559 pos = -1;
560 maxpos = -1;
562 n = 0;
563 format = format0;
565 for (i = 0; i < MAX_ARGS; i++)
567 arg[i].type = NOTYPE;
568 spec[i].pos = -1;
571 /* First parse the format string for position specifiers. */
572 while (*format)
574 c = *format++;
575 if (c != '%')
576 continue;
578 if (*format == '%')
580 format++;
581 continue;
584 if (ISDIGIT (*format))
586 /* This is a position specifier. For example, the number
587 12 in the format string "%12$d", which specifies the third
588 argument of the va_list, formatted in %d format.
589 For details, see "man 3 printf". */
590 pos = atoi(format) - 1;
591 gcc_assert (pos >= 0);
592 while (ISDIGIT(*format))
593 format++;
594 gcc_assert (*format == '$');
595 format++;
597 else
598 pos++;
600 c = *format++;
602 if (pos > maxpos)
603 maxpos = pos;
605 switch (c)
607 case 'C':
608 arg[pos].type = TYPE_CURRENTLOC;
609 break;
611 case 'L':
612 arg[pos].type = TYPE_LOCUS;
613 break;
615 case 'd':
616 case 'i':
617 arg[pos].type = TYPE_INTEGER;
618 break;
620 case 'u':
621 arg[pos].type = TYPE_UINTEGER;
622 break;
624 case 'l':
625 c = *format++;
626 if (c == 'u')
627 arg[pos].type = TYPE_ULONGINT;
628 else if (c == 'i' || c == 'd')
629 arg[pos].type = TYPE_LONGINT;
630 else
631 gcc_unreachable ();
632 break;
634 case 'c':
635 arg[pos].type = TYPE_CHAR;
636 break;
638 case 's':
639 arg[pos].type = TYPE_STRING;
640 break;
642 default:
643 gcc_unreachable ();
646 spec[n++].pos = pos;
649 /* Then convert the values for each %-style argument. */
650 for (pos = 0; pos <= maxpos; pos++)
652 gcc_assert (arg[pos].type != NOTYPE);
653 switch (arg[pos].type)
655 case TYPE_CURRENTLOC:
656 loc = &gfc_current_locus;
657 /* Fall through. */
659 case TYPE_LOCUS:
660 if (arg[pos].type == TYPE_LOCUS)
661 loc = va_arg (argp, locus *);
663 if (have_l1)
665 l2 = loc;
666 arg[pos].u.stringval = "(2)";
668 else
670 l1 = loc;
671 have_l1 = 1;
672 arg[pos].u.stringval = "(1)";
674 break;
676 case TYPE_INTEGER:
677 arg[pos].u.intval = va_arg (argp, int);
678 break;
680 case TYPE_UINTEGER:
681 arg[pos].u.uintval = va_arg (argp, unsigned int);
682 break;
684 case TYPE_LONGINT:
685 arg[pos].u.longintval = va_arg (argp, long int);
686 break;
688 case TYPE_ULONGINT:
689 arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
690 break;
692 case TYPE_CHAR:
693 arg[pos].u.charval = (char) va_arg (argp, int);
694 break;
696 case TYPE_STRING:
697 arg[pos].u.stringval = (const char *) va_arg (argp, char *);
698 break;
700 default:
701 gcc_unreachable ();
705 for (n = 0; spec[n].pos >= 0; n++)
706 spec[n].u = arg[spec[n].pos].u;
708 /* Show the current loci if we have to. */
709 if (have_l1)
710 show_loci (l1, l2);
712 if (*type)
714 error_string (type);
715 error_char (' ');
718 have_l1 = 0;
719 format = format0;
720 n = 0;
722 for (; *format; format++)
724 if (*format != '%')
726 error_char (*format);
727 continue;
730 format++;
731 if (ISDIGIT (*format))
733 /* This is a position specifier. See comment above. */
734 while (ISDIGIT (*format))
735 format++;
737 /* Skip over the dollar sign. */
738 format++;
741 switch (*format)
743 case '%':
744 error_char ('%');
745 break;
747 case 'c':
748 error_char (spec[n++].u.charval);
749 break;
751 case 's':
752 case 'C': /* Current locus */
753 case 'L': /* Specified locus */
754 error_string (spec[n++].u.stringval);
755 break;
757 case 'd':
758 case 'i':
759 error_integer (spec[n++].u.intval);
760 break;
762 case 'u':
763 error_uinteger (spec[n++].u.uintval);
764 break;
766 case 'l':
767 format++;
768 if (*format == 'u')
769 error_uinteger (spec[n++].u.ulongintval);
770 else
771 error_integer (spec[n++].u.longintval);
772 break;
777 error_char ('\n');
781 /* Wrapper for error_print(). */
783 static void
784 error_printf (const char *gmsgid, ...)
786 va_list argp;
788 va_start (argp, gmsgid);
789 error_print ("", _(gmsgid), argp);
790 va_end (argp);
794 /* Increment the number of errors, and check whether too many have
795 been printed. */
797 static void
798 gfc_increment_error_count (void)
800 errors++;
801 if ((gfc_option.max_errors != 0) && (errors >= gfc_option.max_errors))
802 gfc_fatal_error ("Error count reached limit of %d.", gfc_option.max_errors);
806 /* Issue a warning. */
808 void
809 gfc_warning (const char *gmsgid, ...)
811 va_list argp;
813 if (inhibit_warnings)
814 return;
816 warning_buffer.flag = 1;
817 warning_buffer.index = 0;
818 cur_error_buffer = &warning_buffer;
820 va_start (argp, gmsgid);
821 error_print (_("Warning:"), _(gmsgid), argp);
822 va_end (argp);
824 error_char ('\0');
826 if (buffer_flag == 0)
828 warnings++;
829 if (warnings_are_errors)
830 gfc_increment_error_count();
835 /* Whether, for a feature included in a given standard set (GFC_STD_*),
836 we should issue an error or a warning, or be quiet. */
838 notification
839 gfc_notification_std (int std)
841 bool warning;
843 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
844 if ((gfc_option.allow_std & std) != 0 && !warning)
845 return SILENT;
847 return warning ? WARNING : ERROR;
851 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
852 feature. An error/warning will be issued if the currently selected
853 standard does not contain the requested bits. Return false if
854 an error is generated. */
856 bool
857 gfc_notify_std (int std, const char *gmsgid, ...)
859 va_list argp;
860 bool warning;
861 const char *msg1, *msg2;
862 char *buffer;
864 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
865 if ((gfc_option.allow_std & std) != 0 && !warning)
866 return true;
868 if (suppress_errors)
869 return warning ? true : false;
871 cur_error_buffer = warning ? &warning_buffer : &error_buffer;
872 cur_error_buffer->flag = 1;
873 cur_error_buffer->index = 0;
875 if (warning)
876 msg1 = _("Warning:");
877 else
878 msg1 = _("Error:");
880 switch (std)
882 case GFC_STD_F2008_TS:
883 msg2 = "TS 29113/TS 18508:";
884 break;
885 case GFC_STD_F2008_OBS:
886 msg2 = _("Fortran 2008 obsolescent feature:");
887 break;
888 case GFC_STD_F2008:
889 msg2 = "Fortran 2008:";
890 break;
891 case GFC_STD_F2003:
892 msg2 = "Fortran 2003:";
893 break;
894 case GFC_STD_GNU:
895 msg2 = _("GNU Extension:");
896 break;
897 case GFC_STD_LEGACY:
898 msg2 = _("Legacy Extension:");
899 break;
900 case GFC_STD_F95_OBS:
901 msg2 = _("Obsolescent feature:");
902 break;
903 case GFC_STD_F95_DEL:
904 msg2 = _("Deleted feature:");
905 break;
906 default:
907 gcc_unreachable ();
910 buffer = (char *) alloca (strlen (msg1) + strlen (msg2) + 2);
911 strcpy (buffer, msg1);
912 strcat (buffer, " ");
913 strcat (buffer, msg2);
915 va_start (argp, gmsgid);
916 error_print (buffer, _(gmsgid), argp);
917 va_end (argp);
919 error_char ('\0');
921 if (buffer_flag == 0)
923 if (warning && !warnings_are_errors)
924 warnings++;
925 else
926 gfc_increment_error_count();
927 cur_error_buffer->flag = 0;
930 return (warning && !warnings_are_errors) ? true : false;
934 /* Immediate warning (i.e. do not buffer the warning). */
936 void
937 gfc_warning_now (const char *gmsgid, ...)
939 va_list argp;
940 int i;
942 if (inhibit_warnings)
943 return;
945 i = buffer_flag;
946 buffer_flag = 0;
947 warnings++;
949 va_start (argp, gmsgid);
950 error_print (_("Warning:"), _(gmsgid), argp);
951 va_end (argp);
953 error_char ('\0');
955 if (warnings_are_errors)
956 gfc_increment_error_count();
958 buffer_flag = i;
961 /* Return a malloc'd string describing a location. The caller is
962 responsible for freeing the memory. */
963 static char *
964 gfc_diagnostic_build_prefix (diagnostic_context *context,
965 const diagnostic_info *diagnostic)
967 static const char *const diagnostic_kind_text[] = {
968 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
969 #include "gfc-diagnostic.def"
970 #undef DEFINE_DIAGNOSTIC_KIND
971 "must-not-happen"
973 static const char *const diagnostic_kind_color[] = {
974 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
975 #include "gfc-diagnostic.def"
976 #undef DEFINE_DIAGNOSTIC_KIND
977 NULL
979 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
980 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
981 const char *text_cs = "", *text_ce = "";
982 pretty_printer *pp = context->printer;
984 if (diagnostic_kind_color[diagnostic->kind])
986 text_cs = colorize_start (pp_show_color (pp),
987 diagnostic_kind_color[diagnostic->kind]);
988 text_ce = colorize_stop (pp_show_color (pp));
990 return build_message_string ("%s%s%s: ", text_cs, text, text_ce);
993 /* Return a malloc'd string describing a location. The caller is
994 responsible for freeing the memory. */
995 static char *
996 gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
997 const diagnostic_info *diagnostic)
999 pretty_printer *pp = context->printer;
1000 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
1001 const char *locus_ce = colorize_stop (pp_show_color (pp));
1002 expanded_location s = expand_location_to_spelling_point (diagnostic->location);
1003 if (diagnostic->override_column)
1004 s.column = diagnostic->override_column;
1006 return (s.file == NULL
1007 ? build_message_string ("%s%s:%s ", locus_cs, progname, locus_ce )
1008 : !strcmp (s.file, N_("<built-in>"))
1009 ? build_message_string ("%s%s:%s ", locus_cs, s.file, locus_ce)
1010 : context->show_column
1011 ? build_message_string ("%s%s:%d:%d:%s ", locus_cs, s.file, s.line,
1012 s.column, locus_ce)
1013 : build_message_string ("%s%s:%d:%s ", locus_cs, s.file, s.line, locus_ce));
1016 static void
1017 gfc_diagnostic_starter (diagnostic_context *context,
1018 diagnostic_info *diagnostic)
1020 char * locus_prefix = gfc_diagnostic_build_locus_prefix (context, diagnostic);
1021 char * prefix = gfc_diagnostic_build_prefix (context, diagnostic);
1022 /* First we assume there is a caret line. */
1023 pp_set_prefix (context->printer, NULL);
1024 if (pp_needs_newline (context->printer))
1025 pp_newline (context->printer);
1026 pp_verbatim (context->printer, locus_prefix);
1027 /* Fortran uses an empty line between locus and caret line. */
1028 pp_newline (context->printer);
1029 diagnostic_show_locus (context, diagnostic);
1030 if (pp_needs_newline (context->printer))
1032 pp_newline (context->printer);
1033 /* If the caret line was shown, the prefix does not contain the
1034 locus. */
1035 pp_set_prefix (context->printer, prefix);
1037 else
1039 /* Otherwise, start again. */
1040 pp_clear_output_area(context->printer);
1041 pp_set_prefix (context->printer, concat (locus_prefix, prefix, NULL));
1042 free (prefix);
1044 free (locus_prefix);
1047 static void
1048 gfc_diagnostic_finalizer (diagnostic_context *context,
1049 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
1051 pp_destroy_prefix (context->printer);
1052 pp_newline_and_flush (context->printer);
1055 /* Give a warning about the command-line. */
1057 bool
1058 gfc_warning_cmdline (int opt, const char *gmsgid, ...)
1060 va_list argp;
1061 diagnostic_info diagnostic;
1062 bool ret;
1064 va_start (argp, gmsgid);
1065 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION,
1066 DK_WARNING);
1067 diagnostic.option_index = opt;
1068 ret = report_diagnostic (&diagnostic);
1069 va_end (argp);
1070 return ret;
1074 /* Give a warning about the command-line. */
1076 bool
1077 gfc_warning_cmdline (const char *gmsgid, ...)
1079 va_list argp;
1080 diagnostic_info diagnostic;
1081 bool ret;
1083 va_start (argp, gmsgid);
1084 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION,
1085 DK_WARNING);
1086 ret = report_diagnostic (&diagnostic);
1087 va_end (argp);
1088 return ret;
1092 /* Give an error about the command-line. */
1094 void
1095 gfc_error_cmdline (const char *gmsgid, ...)
1097 va_list argp;
1098 diagnostic_info diagnostic;
1100 va_start (argp, gmsgid);
1101 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_ERROR);
1102 report_diagnostic (&diagnostic);
1103 va_end (argp);
1106 /* Clear the warning flag. */
1108 void
1109 gfc_clear_warning (void)
1111 warning_buffer.flag = 0;
1115 /* Check to see if any warnings have been saved.
1116 If so, print the warning. */
1118 void
1119 gfc_warning_check (void)
1121 if (warning_buffer.flag)
1123 warnings++;
1124 if (warning_buffer.message != NULL)
1125 fputs (warning_buffer.message, stderr);
1126 warning_buffer.flag = 0;
1131 /* Issue an error. */
1133 void
1134 gfc_error (const char *gmsgid, ...)
1136 va_list argp;
1138 if (warnings_not_errors)
1139 goto warning;
1141 if (suppress_errors)
1142 return;
1144 error_buffer.flag = 1;
1145 error_buffer.index = 0;
1146 cur_error_buffer = &error_buffer;
1148 va_start (argp, gmsgid);
1149 error_print (_("Error:"), _(gmsgid), argp);
1150 va_end (argp);
1152 error_char ('\0');
1154 if (buffer_flag == 0)
1155 gfc_increment_error_count();
1157 return;
1159 warning:
1161 if (inhibit_warnings)
1162 return;
1164 warning_buffer.flag = 1;
1165 warning_buffer.index = 0;
1166 cur_error_buffer = &warning_buffer;
1168 va_start (argp, gmsgid);
1169 error_print (_("Warning:"), _(gmsgid), argp);
1170 va_end (argp);
1172 error_char ('\0');
1174 if (buffer_flag == 0)
1176 warnings++;
1177 if (warnings_are_errors)
1178 gfc_increment_error_count();
1183 /* Immediate error. */
1185 void
1186 gfc_error_now (const char *gmsgid, ...)
1188 va_list argp;
1189 int i;
1191 error_buffer.flag = 1;
1192 error_buffer.index = 0;
1193 cur_error_buffer = &error_buffer;
1195 i = buffer_flag;
1196 buffer_flag = 0;
1198 va_start (argp, gmsgid);
1199 error_print (_("Error:"), _(gmsgid), argp);
1200 va_end (argp);
1202 error_char ('\0');
1204 gfc_increment_error_count();
1206 buffer_flag = i;
1208 if (flag_fatal_errors)
1209 exit (FATAL_EXIT_CODE);
1213 /* Fatal error, never returns. */
1215 void
1216 gfc_fatal_error (const char *gmsgid, ...)
1218 va_list argp;
1220 buffer_flag = 0;
1222 va_start (argp, gmsgid);
1223 error_print (_("Fatal Error:"), _(gmsgid), argp);
1224 va_end (argp);
1226 exit (FATAL_EXIT_CODE);
1230 /* This shouldn't happen... but sometimes does. */
1232 void
1233 gfc_internal_error (const char *format, ...)
1235 va_list argp;
1237 buffer_flag = 0;
1239 va_start (argp, format);
1241 show_loci (&gfc_current_locus, NULL);
1242 error_printf ("Internal Error at (1):");
1244 error_print ("", format, argp);
1245 va_end (argp);
1247 exit (ICE_EXIT_CODE);
1251 /* Clear the error flag when we start to compile a source line. */
1253 void
1254 gfc_clear_error (void)
1256 error_buffer.flag = 0;
1257 warnings_not_errors = 0;
1261 /* Tests the state of error_flag. */
1264 gfc_error_flag_test (void)
1266 return error_buffer.flag;
1270 /* Check to see if any errors have been saved.
1271 If so, print the error. Returns the state of error_flag. */
1274 gfc_error_check (void)
1276 int rc;
1278 rc = error_buffer.flag;
1280 if (error_buffer.flag)
1282 if (error_buffer.message != NULL)
1283 fputs (error_buffer.message, stderr);
1284 error_buffer.flag = 0;
1286 gfc_increment_error_count();
1288 if (flag_fatal_errors)
1289 exit (FATAL_EXIT_CODE);
1292 return rc;
1296 /* Save the existing error state. */
1298 void
1299 gfc_push_error (gfc_error_buf *err)
1301 err->flag = error_buffer.flag;
1302 if (error_buffer.flag)
1303 err->message = xstrdup (error_buffer.message);
1305 error_buffer.flag = 0;
1309 /* Restore a previous pushed error state. */
1311 void
1312 gfc_pop_error (gfc_error_buf *err)
1314 error_buffer.flag = err->flag;
1315 if (error_buffer.flag)
1317 size_t len = strlen (err->message) + 1;
1318 gcc_assert (len <= error_buffer.allocated);
1319 memcpy (error_buffer.message, err->message, len);
1320 free (err->message);
1325 /* Free a pushed error state, but keep the current error state. */
1327 void
1328 gfc_free_error (gfc_error_buf *err)
1330 if (err->flag)
1331 free (err->message);
1335 /* Report the number of warnings and errors that occurred to the caller. */
1337 void
1338 gfc_get_errors (int *w, int *e)
1340 if (w != NULL)
1341 *w = warnings;
1342 if (e != NULL)
1343 *e = errors;
1347 /* Switch errors into warnings. */
1349 void
1350 gfc_errors_to_warnings (int f)
1352 warnings_not_errors = (f == 1) ? 1 : 0;
1355 void
1356 gfc_diagnostics_init (void)
1358 diagnostic_starter (global_dc) = gfc_diagnostic_starter;
1359 diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
1360 global_dc->caret_char = '^';