ada/
[official-gcc.git] / gcc / fortran / error.c
blobda0eb8f664e9ec6fd3afe4998861b02919ff0613
1 /* Handle errors.
2 Copyright (C) 2000-2015 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 #include "diagnostic.h"
34 #include "diagnostic-color.h"
35 #include "tree-diagnostic.h" /* tree_diagnostics_defaults */
37 #include <new> /* For placement-new */
39 static int suppress_errors = 0;
41 static bool warnings_not_errors = false;
43 static int terminal_width, errors, warnings;
45 static gfc_error_buf error_buffer, warning_buffer, *cur_error_buffer;
47 /* True if the error/warnings should be buffered. */
48 static bool buffered_p;
49 /* These are always buffered buffers (.flush_p == false) to be used by
50 the pretty-printer. */
51 static output_buffer *pp_error_buffer, *pp_warning_buffer;
52 static int warningcount_buffered, werrorcount_buffered;
54 /* Return true if there output_buffer is empty. */
56 static bool
57 gfc_output_buffer_empty_p (const output_buffer * buf)
59 return output_buffer_last_position_in_text (buf) == NULL;
62 /* Go one level deeper suppressing errors. */
64 void
65 gfc_push_suppress_errors (void)
67 gcc_assert (suppress_errors >= 0);
68 ++suppress_errors;
71 static void
72 gfc_error (const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(1,0);
74 static bool
75 gfc_warning (int opt, const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(2,0);
78 /* Leave one level of error suppressing. */
80 void
81 gfc_pop_suppress_errors (void)
83 gcc_assert (suppress_errors > 0);
84 --suppress_errors;
88 /* Determine terminal width (for trimming source lines in output). */
90 static int
91 gfc_get_terminal_width (void)
93 return isatty (STDERR_FILENO) ? get_terminal_width () : INT_MAX;
97 /* Per-file error initialization. */
99 void
100 gfc_error_init_1 (void)
102 terminal_width = gfc_get_terminal_width ();
103 errors = 0;
104 warnings = 0;
105 gfc_buffer_error (false);
109 /* Set the flag for buffering errors or not. */
111 void
112 gfc_buffer_error (bool flag)
114 buffered_p = flag;
118 /* Add a single character to the error buffer or output depending on
119 buffered_p. */
121 static void
122 error_char (char c)
124 if (buffered_p)
126 if (cur_error_buffer->index >= cur_error_buffer->allocated)
128 cur_error_buffer->allocated = cur_error_buffer->allocated
129 ? cur_error_buffer->allocated * 2 : 1000;
130 cur_error_buffer->message = XRESIZEVEC (char, cur_error_buffer->message,
131 cur_error_buffer->allocated);
133 cur_error_buffer->message[cur_error_buffer->index++] = c;
135 else
137 if (c != 0)
139 /* We build up complete lines before handing things
140 over to the library in order to speed up error printing. */
141 static char *line;
142 static size_t allocated = 0, index = 0;
144 if (index + 1 >= allocated)
146 allocated = allocated ? allocated * 2 : 1000;
147 line = XRESIZEVEC (char, line, allocated);
149 line[index++] = c;
150 if (c == '\n')
152 line[index] = '\0';
153 fputs (line, stderr);
154 index = 0;
161 /* Copy a string to wherever it needs to go. */
163 static void
164 error_string (const char *p)
166 while (*p)
167 error_char (*p++);
171 /* Print a formatted integer to the error buffer or output. */
173 #define IBUF_LEN 60
175 static void
176 error_uinteger (unsigned long int i)
178 char *p, int_buf[IBUF_LEN];
180 p = int_buf + IBUF_LEN - 1;
181 *p-- = '\0';
183 if (i == 0)
184 *p-- = '0';
186 while (i > 0)
188 *p-- = i % 10 + '0';
189 i = i / 10;
192 error_string (p + 1);
195 static void
196 error_integer (long int i)
198 unsigned long int u;
200 if (i < 0)
202 u = (unsigned long int) -i;
203 error_char ('-');
205 else
206 u = i;
208 error_uinteger (u);
212 static size_t
213 gfc_widechar_display_length (gfc_char_t c)
215 if (gfc_wide_is_printable (c) || c == '\t')
216 /* Printable ASCII character, or tabulation (output as a space). */
217 return 1;
218 else if (c < ((gfc_char_t) 1 << 8))
219 /* Displayed as \x?? */
220 return 4;
221 else if (c < ((gfc_char_t) 1 << 16))
222 /* Displayed as \u???? */
223 return 6;
224 else
225 /* Displayed as \U???????? */
226 return 10;
230 /* Length of the ASCII representation of the wide string, escaping wide
231 characters as print_wide_char_into_buffer() does. */
233 static size_t
234 gfc_wide_display_length (const gfc_char_t *str)
236 size_t i, len;
238 for (i = 0, len = 0; str[i]; i++)
239 len += gfc_widechar_display_length (str[i]);
241 return len;
244 static int
245 print_wide_char_into_buffer (gfc_char_t c, char *buf)
247 static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6',
248 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
250 if (gfc_wide_is_printable (c) || c == '\t')
252 buf[1] = '\0';
253 /* Tabulation is output as a space. */
254 buf[0] = (unsigned char) (c == '\t' ? ' ' : c);
255 return 1;
257 else if (c < ((gfc_char_t) 1 << 8))
259 buf[4] = '\0';
260 buf[3] = xdigit[c & 0x0F];
261 c = c >> 4;
262 buf[2] = xdigit[c & 0x0F];
264 buf[1] = 'x';
265 buf[0] = '\\';
266 return 4;
268 else if (c < ((gfc_char_t) 1 << 16))
270 buf[6] = '\0';
271 buf[5] = xdigit[c & 0x0F];
272 c = c >> 4;
273 buf[4] = xdigit[c & 0x0F];
274 c = c >> 4;
275 buf[3] = xdigit[c & 0x0F];
276 c = c >> 4;
277 buf[2] = xdigit[c & 0x0F];
279 buf[1] = 'u';
280 buf[0] = '\\';
281 return 6;
283 else
285 buf[10] = '\0';
286 buf[9] = xdigit[c & 0x0F];
287 c = c >> 4;
288 buf[8] = xdigit[c & 0x0F];
289 c = c >> 4;
290 buf[7] = xdigit[c & 0x0F];
291 c = c >> 4;
292 buf[6] = xdigit[c & 0x0F];
293 c = c >> 4;
294 buf[5] = xdigit[c & 0x0F];
295 c = c >> 4;
296 buf[4] = xdigit[c & 0x0F];
297 c = c >> 4;
298 buf[3] = xdigit[c & 0x0F];
299 c = c >> 4;
300 buf[2] = xdigit[c & 0x0F];
302 buf[1] = 'U';
303 buf[0] = '\\';
304 return 10;
308 static char wide_char_print_buffer[11];
310 const char *
311 gfc_print_wide_char (gfc_char_t c)
313 print_wide_char_into_buffer (c, wide_char_print_buffer);
314 return wide_char_print_buffer;
318 /* Show the file, where it was included, and the source line, give a
319 locus. Calls error_printf() recursively, but the recursion is at
320 most one level deep. */
322 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
324 static void
325 show_locus (locus *loc, int c1, int c2)
327 gfc_linebuf *lb;
328 gfc_file *f;
329 gfc_char_t *p;
330 int i, offset, cmax;
332 /* TODO: Either limit the total length and number of included files
333 displayed or add buffering of arbitrary number of characters in
334 error messages. */
336 /* Write out the error header line, giving the source file and error
337 location (in GNU standard "[file]:[line].[column]:" format),
338 followed by an "included by" stack and a blank line. This header
339 format is matched by a testsuite parser defined in
340 lib/gfortran-dg.exp. */
342 lb = loc->lb;
343 f = lb->file;
345 error_string (f->filename);
346 error_char (':');
348 error_integer (LOCATION_LINE (lb->location));
350 if ((c1 > 0) || (c2 > 0))
351 error_char ('.');
353 if (c1 > 0)
354 error_integer (c1);
356 if ((c1 > 0) && (c2 > 0))
357 error_char ('-');
359 if (c2 > 0)
360 error_integer (c2);
362 error_char (':');
363 error_char ('\n');
365 for (;;)
367 i = f->inclusion_line;
369 f = f->up;
370 if (f == NULL) break;
372 error_printf (" Included at %s:%d:", f->filename, i);
375 error_char ('\n');
377 /* Calculate an appropriate horizontal offset of the source line in
378 order to get the error locus within the visible portion of the
379 line. Note that if the margin of 5 here is changed, the
380 corresponding margin of 10 in show_loci should be changed. */
382 offset = 0;
384 /* If the two loci would appear in the same column, we shift
385 '2' one column to the right, so as to print '12' rather than
386 just '1'. We do this here so it will be accounted for in the
387 margin calculations. */
389 if (c1 == c2)
390 c2 += 1;
392 cmax = (c1 < c2) ? c2 : c1;
393 if (cmax > terminal_width - 5)
394 offset = cmax - terminal_width + 5;
396 /* Show the line itself, taking care not to print more than what can
397 show up on the terminal. Tabs are converted to spaces, and
398 nonprintable characters are converted to a "\xNN" sequence. */
400 p = &(lb->line[offset]);
401 i = gfc_wide_display_length (p);
402 if (i > terminal_width)
403 i = terminal_width - 1;
405 while (i > 0)
407 static char buffer[11];
408 i -= print_wide_char_into_buffer (*p++, buffer);
409 error_string (buffer);
412 error_char ('\n');
414 /* Show the '1' and/or '2' corresponding to the column of the error
415 locus. Note that a value of -1 for c1 or c2 will simply cause
416 the relevant number not to be printed. */
418 c1 -= offset;
419 c2 -= offset;
420 cmax -= offset;
422 p = &(lb->line[offset]);
423 for (i = 0; i < cmax; i++)
425 int spaces, j;
426 spaces = gfc_widechar_display_length (*p++);
428 if (i == c1)
429 error_char ('1'), spaces--;
430 else if (i == c2)
431 error_char ('2'), spaces--;
433 for (j = 0; j < spaces; j++)
434 error_char (' ');
437 if (i == c1)
438 error_char ('1');
439 else if (i == c2)
440 error_char ('2');
442 error_char ('\n');
447 /* As part of printing an error, we show the source lines that caused
448 the problem. We show at least one, and possibly two loci; the two
449 loci may or may not be on the same source line. */
451 static void
452 show_loci (locus *l1, locus *l2)
454 int m, c1, c2;
456 if (l1 == NULL || l1->lb == NULL)
458 error_printf ("<During initialization>\n");
459 return;
462 /* While calculating parameters for printing the loci, we consider possible
463 reasons for printing one per line. If appropriate, print the loci
464 individually; otherwise we print them both on the same line. */
466 c1 = l1->nextc - l1->lb->line;
467 if (l2 == NULL)
469 show_locus (l1, c1, -1);
470 return;
473 c2 = l2->nextc - l2->lb->line;
475 if (c1 < c2)
476 m = c2 - c1;
477 else
478 m = c1 - c2;
480 /* Note that the margin value of 10 here needs to be less than the
481 margin of 5 used in the calculation of offset in show_locus. */
483 if (l1->lb != l2->lb || m > terminal_width - 10)
485 show_locus (l1, c1, -1);
486 show_locus (l2, -1, c2);
487 return;
490 show_locus (l1, c1, c2);
492 return;
496 /* Workhorse for the error printing subroutines. This subroutine is
497 inspired by g77's error handling and is similar to printf() with
498 the following %-codes:
500 %c Character, %d or %i Integer, %s String, %% Percent
501 %L Takes locus argument
502 %C Current locus (no argument)
504 If a locus pointer is given, the actual source line is printed out
505 and the column is indicated. Since we want the error message at
506 the bottom of any source file information, we must scan the
507 argument list twice -- once to determine whether the loci are
508 present and record this for printing, and once to print the error
509 message after and loci have been printed. A maximum of two locus
510 arguments are permitted.
512 This function is also called (recursively) by show_locus in the
513 case of included files; however, as show_locus does not resupply
514 any loci, the recursion is at most one level deep. */
516 #define MAX_ARGS 10
518 static void ATTRIBUTE_GCC_GFC(2,0)
519 error_print (const char *type, const char *format0, va_list argp)
521 enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
522 TYPE_LONGINT, TYPE_ULONGINT, TYPE_CHAR, TYPE_STRING,
523 NOTYPE };
524 struct
526 int type;
527 int pos;
528 union
530 int intval;
531 unsigned int uintval;
532 long int longintval;
533 unsigned long int ulongintval;
534 char charval;
535 const char * stringval;
536 } u;
537 } arg[MAX_ARGS], spec[MAX_ARGS];
538 /* spec is the array of specifiers, in the same order as they
539 appear in the format string. arg is the array of arguments,
540 in the same order as they appear in the va_list. */
542 char c;
543 int i, n, have_l1, pos, maxpos;
544 locus *l1, *l2, *loc;
545 const char *format;
547 loc = l1 = l2 = NULL;
549 have_l1 = 0;
550 pos = -1;
551 maxpos = -1;
553 n = 0;
554 format = format0;
556 for (i = 0; i < MAX_ARGS; i++)
558 arg[i].type = NOTYPE;
559 spec[i].pos = -1;
562 /* First parse the format string for position specifiers. */
563 while (*format)
565 c = *format++;
566 if (c != '%')
567 continue;
569 if (*format == '%')
571 format++;
572 continue;
575 if (ISDIGIT (*format))
577 /* This is a position specifier. For example, the number
578 12 in the format string "%12$d", which specifies the third
579 argument of the va_list, formatted in %d format.
580 For details, see "man 3 printf". */
581 pos = atoi(format) - 1;
582 gcc_assert (pos >= 0);
583 while (ISDIGIT(*format))
584 format++;
585 gcc_assert (*format == '$');
586 format++;
588 else
589 pos++;
591 c = *format++;
593 if (pos > maxpos)
594 maxpos = pos;
596 switch (c)
598 case 'C':
599 arg[pos].type = TYPE_CURRENTLOC;
600 break;
602 case 'L':
603 arg[pos].type = TYPE_LOCUS;
604 break;
606 case 'd':
607 case 'i':
608 arg[pos].type = TYPE_INTEGER;
609 break;
611 case 'u':
612 arg[pos].type = TYPE_UINTEGER;
613 break;
615 case 'l':
616 c = *format++;
617 if (c == 'u')
618 arg[pos].type = TYPE_ULONGINT;
619 else if (c == 'i' || c == 'd')
620 arg[pos].type = TYPE_LONGINT;
621 else
622 gcc_unreachable ();
623 break;
625 case 'c':
626 arg[pos].type = TYPE_CHAR;
627 break;
629 case 's':
630 arg[pos].type = TYPE_STRING;
631 break;
633 default:
634 gcc_unreachable ();
637 spec[n++].pos = pos;
640 /* Then convert the values for each %-style argument. */
641 for (pos = 0; pos <= maxpos; pos++)
643 gcc_assert (arg[pos].type != NOTYPE);
644 switch (arg[pos].type)
646 case TYPE_CURRENTLOC:
647 loc = &gfc_current_locus;
648 /* Fall through. */
650 case TYPE_LOCUS:
651 if (arg[pos].type == TYPE_LOCUS)
652 loc = va_arg (argp, locus *);
654 if (have_l1)
656 l2 = loc;
657 arg[pos].u.stringval = "(2)";
659 else
661 l1 = loc;
662 have_l1 = 1;
663 arg[pos].u.stringval = "(1)";
665 break;
667 case TYPE_INTEGER:
668 arg[pos].u.intval = va_arg (argp, int);
669 break;
671 case TYPE_UINTEGER:
672 arg[pos].u.uintval = va_arg (argp, unsigned int);
673 break;
675 case TYPE_LONGINT:
676 arg[pos].u.longintval = va_arg (argp, long int);
677 break;
679 case TYPE_ULONGINT:
680 arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
681 break;
683 case TYPE_CHAR:
684 arg[pos].u.charval = (char) va_arg (argp, int);
685 break;
687 case TYPE_STRING:
688 arg[pos].u.stringval = (const char *) va_arg (argp, char *);
689 break;
691 default:
692 gcc_unreachable ();
696 for (n = 0; spec[n].pos >= 0; n++)
697 spec[n].u = arg[spec[n].pos].u;
699 /* Show the current loci if we have to. */
700 if (have_l1)
701 show_loci (l1, l2);
703 if (*type)
705 error_string (type);
706 error_char (' ');
709 have_l1 = 0;
710 format = format0;
711 n = 0;
713 for (; *format; format++)
715 if (*format != '%')
717 error_char (*format);
718 continue;
721 format++;
722 if (ISDIGIT (*format))
724 /* This is a position specifier. See comment above. */
725 while (ISDIGIT (*format))
726 format++;
728 /* Skip over the dollar sign. */
729 format++;
732 switch (*format)
734 case '%':
735 error_char ('%');
736 break;
738 case 'c':
739 error_char (spec[n++].u.charval);
740 break;
742 case 's':
743 case 'C': /* Current locus */
744 case 'L': /* Specified locus */
745 error_string (spec[n++].u.stringval);
746 break;
748 case 'd':
749 case 'i':
750 error_integer (spec[n++].u.intval);
751 break;
753 case 'u':
754 error_uinteger (spec[n++].u.uintval);
755 break;
757 case 'l':
758 format++;
759 if (*format == 'u')
760 error_uinteger (spec[n++].u.ulongintval);
761 else
762 error_integer (spec[n++].u.longintval);
763 break;
768 error_char ('\n');
772 /* Wrapper for error_print(). */
774 static void
775 error_printf (const char *gmsgid, ...)
777 va_list argp;
779 va_start (argp, gmsgid);
780 error_print ("", _(gmsgid), argp);
781 va_end (argp);
785 /* Increment the number of errors, and check whether too many have
786 been printed. */
788 static void
789 gfc_increment_error_count (void)
791 errors++;
792 if ((gfc_option.max_errors != 0) && (errors >= gfc_option.max_errors))
793 gfc_fatal_error ("Error count reached limit of %d.", gfc_option.max_errors);
797 /* Clear any output buffered in a pretty-print output_buffer. */
799 static void
800 gfc_clear_pp_buffer (output_buffer *this_buffer)
802 pretty_printer *pp = global_dc->printer;
803 output_buffer *tmp_buffer = pp->buffer;
804 pp->buffer = this_buffer;
805 pp_clear_output_area (pp);
806 pp->buffer = tmp_buffer;
810 /* Issue a warning. */
811 /* Use gfc_warning instead, unless two locations are used in the same
812 warning or for scanner.c, if the location is not properly set up. */
814 void
815 gfc_warning_1 (const char *gmsgid, ...)
817 va_list argp;
819 if (inhibit_warnings)
820 return;
822 warning_buffer.flag = 1;
823 warning_buffer.index = 0;
824 cur_error_buffer = &warning_buffer;
826 va_start (argp, gmsgid);
827 error_print (_("Warning:"), _(gmsgid), argp);
828 va_end (argp);
830 error_char ('\0');
832 if (!buffered_p)
834 warnings++;
835 if (warnings_are_errors)
836 gfc_increment_error_count();
841 /* This is just a helper function to avoid duplicating the logic of
842 gfc_warning. */
844 static bool
845 gfc_warning (int opt, const char *gmsgid, va_list ap)
847 va_list argp;
848 va_copy (argp, ap);
850 diagnostic_info diagnostic;
851 bool fatal_errors = global_dc->fatal_errors;
852 pretty_printer *pp = global_dc->printer;
853 output_buffer *tmp_buffer = pp->buffer;
855 gfc_clear_pp_buffer (pp_warning_buffer);
857 if (buffered_p)
859 pp->buffer = pp_warning_buffer;
860 global_dc->fatal_errors = false;
861 /* To prevent -fmax-errors= triggering. */
862 --werrorcount;
865 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION,
866 DK_WARNING);
867 diagnostic.option_index = opt;
868 bool ret = report_diagnostic (&diagnostic);
870 if (buffered_p)
872 pp->buffer = tmp_buffer;
873 global_dc->fatal_errors = fatal_errors;
875 warningcount_buffered = 0;
876 werrorcount_buffered = 0;
877 /* Undo the above --werrorcount if not Werror, otherwise
878 werrorcount is correct already. */
879 if (!ret)
880 ++werrorcount;
881 else if (diagnostic.kind == DK_ERROR)
882 ++werrorcount_buffered;
883 else
884 ++werrorcount, --warningcount, ++warningcount_buffered;
887 va_end (argp);
888 return ret;
891 /* Issue a warning. */
892 /* This function uses the common diagnostics, but does not support
893 two locations; when being used in scanner.c, ensure that the location
894 is properly setup. Otherwise, use gfc_warning_1. */
896 bool
897 gfc_warning (int opt, const char *gmsgid, ...)
899 va_list argp;
901 va_start (argp, gmsgid);
902 bool ret = gfc_warning (opt, gmsgid, argp);
903 va_end (argp);
904 return ret;
908 /* Whether, for a feature included in a given standard set (GFC_STD_*),
909 we should issue an error or a warning, or be quiet. */
911 notification
912 gfc_notification_std (int std)
914 bool warning;
916 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
917 if ((gfc_option.allow_std & std) != 0 && !warning)
918 return SILENT;
920 return warning ? WARNING : ERROR;
924 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
925 feature. An error/warning will be issued if the currently selected
926 standard does not contain the requested bits. Return false if
927 an error is generated. */
929 bool
930 gfc_notify_std_1 (int std, const char *gmsgid, ...)
932 va_list argp;
933 bool warning;
934 const char *msg1, *msg2;
935 char *buffer;
937 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
938 if ((gfc_option.allow_std & std) != 0 && !warning)
939 return true;
941 if (suppress_errors)
942 return warning ? true : false;
944 cur_error_buffer = warning ? &warning_buffer : &error_buffer;
945 cur_error_buffer->flag = 1;
946 cur_error_buffer->index = 0;
948 if (warning)
949 msg1 = _("Warning:");
950 else
951 msg1 = _("Error:");
953 switch (std)
955 case GFC_STD_F2008_TS:
956 msg2 = "TS 29113/TS 18508:";
957 break;
958 case GFC_STD_F2008_OBS:
959 msg2 = _("Fortran 2008 obsolescent feature:");
960 break;
961 case GFC_STD_F2008:
962 msg2 = "Fortran 2008:";
963 break;
964 case GFC_STD_F2003:
965 msg2 = "Fortran 2003:";
966 break;
967 case GFC_STD_GNU:
968 msg2 = _("GNU Extension:");
969 break;
970 case GFC_STD_LEGACY:
971 msg2 = _("Legacy Extension:");
972 break;
973 case GFC_STD_F95_OBS:
974 msg2 = _("Obsolescent feature:");
975 break;
976 case GFC_STD_F95_DEL:
977 msg2 = _("Deleted feature:");
978 break;
979 default:
980 gcc_unreachable ();
983 buffer = (char *) alloca (strlen (msg1) + strlen (msg2) + 2);
984 strcpy (buffer, msg1);
985 strcat (buffer, " ");
986 strcat (buffer, msg2);
988 va_start (argp, gmsgid);
989 error_print (buffer, _(gmsgid), argp);
990 va_end (argp);
992 error_char ('\0');
994 if (!buffered_p)
996 if (warning && !warnings_are_errors)
997 warnings++;
998 else
999 gfc_increment_error_count();
1000 cur_error_buffer->flag = 0;
1003 return (warning && !warnings_are_errors) ? true : false;
1007 bool
1008 gfc_notify_std (int std, const char *gmsgid, ...)
1010 va_list argp;
1011 bool warning;
1012 const char *msg, *msg2;
1013 char *buffer;
1015 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
1016 if ((gfc_option.allow_std & std) != 0 && !warning)
1017 return true;
1019 if (suppress_errors)
1020 return warning ? true : false;
1022 switch (std)
1024 case GFC_STD_F2008_TS:
1025 msg = "TS 29113/TS 18508:";
1026 break;
1027 case GFC_STD_F2008_OBS:
1028 msg = _("Fortran 2008 obsolescent feature:");
1029 break;
1030 case GFC_STD_F2008:
1031 msg = "Fortran 2008:";
1032 break;
1033 case GFC_STD_F2003:
1034 msg = "Fortran 2003:";
1035 break;
1036 case GFC_STD_GNU:
1037 msg = _("GNU Extension:");
1038 break;
1039 case GFC_STD_LEGACY:
1040 msg = _("Legacy Extension:");
1041 break;
1042 case GFC_STD_F95_OBS:
1043 msg = _("Obsolescent feature:");
1044 break;
1045 case GFC_STD_F95_DEL:
1046 msg = _("Deleted feature:");
1047 break;
1048 default:
1049 gcc_unreachable ();
1052 msg2 = _(gmsgid);
1053 buffer = (char *) alloca (strlen (msg) + strlen (msg2) + 2);
1054 strcpy (buffer, msg);
1055 strcat (buffer, " ");
1056 strcat (buffer, msg2);
1058 va_start (argp, gmsgid);
1059 if (warning)
1060 gfc_warning (0, buffer, argp);
1061 else
1062 gfc_error (buffer, argp);
1063 va_end (argp);
1065 return (warning && !warnings_are_errors) ? true : false;
1069 /* Immediate warning (i.e. do not buffer the warning). */
1070 /* Use gfc_warning_now instead, unless two locations are used in the same
1071 warning or for scanner.c, if the location is not properly set up. */
1073 void
1074 gfc_warning_now_1 (const char *gmsgid, ...)
1076 va_list argp;
1077 bool buffered_p_saved;
1079 if (inhibit_warnings)
1080 return;
1082 buffered_p_saved = buffered_p;
1083 buffered_p = false;
1084 warnings++;
1086 va_start (argp, gmsgid);
1087 error_print (_("Warning:"), _(gmsgid), argp);
1088 va_end (argp);
1090 error_char ('\0');
1092 if (warnings_are_errors)
1093 gfc_increment_error_count();
1095 buffered_p = buffered_p_saved;
1098 /* Called from output_format -- during diagnostic message processing
1099 to handle Fortran specific format specifiers with the following meanings:
1101 %C Current locus (no argument)
1102 %L Takes locus argument
1104 static bool
1105 gfc_format_decoder (pretty_printer *pp,
1106 text_info *text, const char *spec,
1107 int precision ATTRIBUTE_UNUSED, bool wide ATTRIBUTE_UNUSED,
1108 bool plus ATTRIBUTE_UNUSED, bool hash ATTRIBUTE_UNUSED)
1110 switch (*spec)
1112 case 'C':
1113 case 'L':
1115 static const char *result = "(1)";
1116 locus *loc;
1117 if (*spec == 'C')
1118 loc = &gfc_current_locus;
1119 else
1120 loc = va_arg (*text->args_ptr, locus *);
1121 gcc_assert (loc->nextc - loc->lb->line >= 0);
1122 unsigned int offset = loc->nextc - loc->lb->line;
1123 gcc_assert (text->locus);
1124 *text->locus
1125 = linemap_position_for_loc_and_offset (line_table,
1126 loc->lb->location,
1127 offset);
1128 global_dc->caret_char = '1';
1129 pp_string (pp, result);
1130 return true;
1132 default:
1133 return false;
1137 /* Return a malloc'd string describing a location. The caller is
1138 responsible for freeing the memory. */
1139 static char *
1140 gfc_diagnostic_build_prefix (diagnostic_context *context,
1141 const diagnostic_info *diagnostic)
1143 static const char *const diagnostic_kind_text[] = {
1144 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
1145 #include "gfc-diagnostic.def"
1146 #undef DEFINE_DIAGNOSTIC_KIND
1147 "must-not-happen"
1149 static const char *const diagnostic_kind_color[] = {
1150 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
1151 #include "gfc-diagnostic.def"
1152 #undef DEFINE_DIAGNOSTIC_KIND
1153 NULL
1155 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
1156 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
1157 const char *text_cs = "", *text_ce = "";
1158 pretty_printer *pp = context->printer;
1160 if (diagnostic_kind_color[diagnostic->kind])
1162 text_cs = colorize_start (pp_show_color (pp),
1163 diagnostic_kind_color[diagnostic->kind]);
1164 text_ce = colorize_stop (pp_show_color (pp));
1166 return build_message_string ("%s%s:%s ", text_cs, text, text_ce);
1169 /* Return a malloc'd string describing a location. The caller is
1170 responsible for freeing the memory. */
1171 static char *
1172 gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
1173 const diagnostic_info *diagnostic)
1175 pretty_printer *pp = context->printer;
1176 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
1177 const char *locus_ce = colorize_stop (pp_show_color (pp));
1178 expanded_location s = diagnostic_expand_location (diagnostic);
1179 return (s.file == NULL
1180 ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
1181 : !strcmp (s.file, N_("<built-in>"))
1182 ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
1183 : context->show_column
1184 ? build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
1185 s.column, locus_ce)
1186 : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line, locus_ce));
1189 static void
1190 gfc_diagnostic_starter (diagnostic_context *context,
1191 diagnostic_info *diagnostic)
1193 char * locus_prefix = gfc_diagnostic_build_locus_prefix (context, diagnostic);
1194 char * prefix = gfc_diagnostic_build_prefix (context, diagnostic);
1195 /* First we assume there is a caret line. */
1196 pp_set_prefix (context->printer, NULL);
1197 if (pp_needs_newline (context->printer))
1198 pp_newline (context->printer);
1199 pp_verbatim (context->printer, locus_prefix);
1200 /* Fortran uses an empty line between locus and caret line. */
1201 pp_newline (context->printer);
1202 diagnostic_show_locus (context, diagnostic);
1203 if (pp_needs_newline (context->printer))
1205 pp_newline (context->printer);
1206 /* If the caret line was shown, the prefix does not contain the
1207 locus. */
1208 pp_set_prefix (context->printer, prefix);
1210 else
1212 /* Otherwise, start again. */
1213 pp_clear_output_area(context->printer);
1214 pp_set_prefix (context->printer, concat (locus_prefix, " ", prefix, NULL));
1215 free (prefix);
1217 free (locus_prefix);
1220 static void
1221 gfc_diagnostic_finalizer (diagnostic_context *context,
1222 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
1224 pp_destroy_prefix (context->printer);
1225 pp_newline_and_flush (context->printer);
1228 /* Immediate warning (i.e. do not buffer the warning). */
1229 /* This function uses the common diagnostics, but does not support
1230 two locations; when being used in scanner.c, ensure that the location
1231 is properly setup. Otherwise, use gfc_warning_now_1. */
1233 bool
1234 gfc_warning_now (int opt, const char *gmsgid, ...)
1236 va_list argp;
1237 diagnostic_info diagnostic;
1238 bool ret;
1240 va_start (argp, gmsgid);
1241 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION,
1242 DK_WARNING);
1243 diagnostic.option_index = opt;
1244 ret = report_diagnostic (&diagnostic);
1245 va_end (argp);
1246 return ret;
1250 /* Immediate error (i.e. do not buffer). */
1251 /* This function uses the common diagnostics, but does not support
1252 two locations; when being used in scanner.c, ensure that the location
1253 is properly setup. Otherwise, use gfc_error_now_1. */
1255 void
1256 gfc_error_now (const char *gmsgid, ...)
1258 va_list argp;
1259 diagnostic_info diagnostic;
1261 va_start (argp, gmsgid);
1262 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_ERROR);
1263 report_diagnostic (&diagnostic);
1264 va_end (argp);
1268 /* Fatal error, never returns. */
1270 void
1271 gfc_fatal_error (const char *gmsgid, ...)
1273 va_list argp;
1274 diagnostic_info diagnostic;
1276 va_start (argp, gmsgid);
1277 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_FATAL);
1278 report_diagnostic (&diagnostic);
1279 va_end (argp);
1281 gcc_unreachable ();
1284 /* Clear the warning flag. */
1286 void
1287 gfc_clear_warning (void)
1289 warning_buffer.flag = 0;
1291 gfc_clear_pp_buffer (pp_warning_buffer);
1292 warningcount_buffered = 0;
1293 werrorcount_buffered = 0;
1297 /* Check to see if any warnings have been saved.
1298 If so, print the warning. */
1300 void
1301 gfc_warning_check (void)
1303 if (warning_buffer.flag)
1305 warnings++;
1306 if (warning_buffer.message != NULL)
1307 fputs (warning_buffer.message, stderr);
1308 gfc_clear_warning ();
1310 /* This is for the new diagnostics machinery. */
1311 else if (! gfc_output_buffer_empty_p (pp_warning_buffer))
1313 pretty_printer *pp = global_dc->printer;
1314 output_buffer *tmp_buffer = pp->buffer;
1315 pp->buffer = pp_warning_buffer;
1316 pp_really_flush (pp);
1317 warningcount += warningcount_buffered;
1318 werrorcount += werrorcount_buffered;
1319 gcc_assert (warningcount_buffered + werrorcount_buffered == 1);
1320 diagnostic_action_after_output (global_dc,
1321 warningcount_buffered
1322 ? DK_WARNING : DK_ERROR);
1323 pp->buffer = tmp_buffer;
1328 /* Issue an error. */
1329 /* Use gfc_error instead, unless two locations are used in the same
1330 warning or for scanner.c, if the location is not properly set up. */
1332 void
1333 gfc_error_1 (const char *gmsgid, ...)
1335 va_list argp;
1337 if (warnings_not_errors)
1338 goto warning;
1340 if (suppress_errors)
1341 return;
1343 error_buffer.flag = 1;
1344 error_buffer.index = 0;
1345 cur_error_buffer = &error_buffer;
1347 va_start (argp, gmsgid);
1348 error_print (_("Error:"), _(gmsgid), argp);
1349 va_end (argp);
1351 error_char ('\0');
1353 if (!buffered_p)
1354 gfc_increment_error_count();
1356 return;
1358 warning:
1360 if (inhibit_warnings)
1361 return;
1363 warning_buffer.flag = 1;
1364 warning_buffer.index = 0;
1365 cur_error_buffer = &warning_buffer;
1367 va_start (argp, gmsgid);
1368 error_print (_("Warning:"), _(gmsgid), argp);
1369 va_end (argp);
1371 error_char ('\0');
1373 if (!buffered_p)
1375 warnings++;
1376 if (warnings_are_errors)
1377 gfc_increment_error_count();
1381 /* Issue an error. */
1382 /* This function uses the common diagnostics, but does not support
1383 two locations; when being used in scanner.c, ensure that the location
1384 is properly setup. Otherwise, use gfc_error_1. */
1386 static void
1387 gfc_error (const char *gmsgid, va_list ap)
1389 va_list argp;
1390 va_copy (argp, ap);
1392 if (warnings_not_errors)
1394 gfc_warning (/*opt=*/0, gmsgid, argp);
1395 va_end (argp);
1396 return;
1399 if (suppress_errors)
1401 va_end (argp);
1402 return;
1405 diagnostic_info diagnostic;
1406 bool fatal_errors = global_dc->fatal_errors;
1407 pretty_printer *pp = global_dc->printer;
1408 output_buffer *tmp_buffer = pp->buffer;
1410 gfc_clear_pp_buffer (pp_error_buffer);
1412 if (buffered_p)
1414 pp->buffer = pp_error_buffer;
1415 global_dc->fatal_errors = false;
1416 /* To prevent -fmax-errors= triggering, we decrease it before
1417 report_diagnostic increases it. */
1418 --errorcount;
1421 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_ERROR);
1422 report_diagnostic (&diagnostic);
1424 if (buffered_p)
1426 pp->buffer = tmp_buffer;
1427 global_dc->fatal_errors = fatal_errors;
1430 va_end (argp);
1434 void
1435 gfc_error (const char *gmsgid, ...)
1437 va_list argp;
1438 va_start (argp, gmsgid);
1439 gfc_error (gmsgid, argp);
1440 va_end (argp);
1444 /* Immediate error. */
1445 /* Use gfc_error_now instead, unless two locations are used in the same
1446 warning or for scanner.c, if the location is not properly set up. */
1448 void
1449 gfc_error_now_1 (const char *gmsgid, ...)
1451 va_list argp;
1452 bool buffered_p_saved;
1454 error_buffer.flag = 1;
1455 error_buffer.index = 0;
1456 cur_error_buffer = &error_buffer;
1458 buffered_p_saved = buffered_p;
1459 buffered_p = false;
1461 va_start (argp, gmsgid);
1462 error_print (_("Error:"), _(gmsgid), argp);
1463 va_end (argp);
1465 error_char ('\0');
1467 gfc_increment_error_count();
1469 buffered_p = buffered_p_saved;
1471 if (flag_fatal_errors)
1472 exit (FATAL_EXIT_CODE);
1476 /* This shouldn't happen... but sometimes does. */
1478 void
1479 gfc_internal_error (const char *gmsgid, ...)
1481 va_list argp;
1482 diagnostic_info diagnostic;
1484 va_start (argp, gmsgid);
1485 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_ICE);
1486 report_diagnostic (&diagnostic);
1487 va_end (argp);
1489 gcc_unreachable ();
1493 /* Clear the error flag when we start to compile a source line. */
1495 void
1496 gfc_clear_error (void)
1498 error_buffer.flag = 0;
1499 warnings_not_errors = false;
1500 gfc_clear_pp_buffer (pp_error_buffer);
1504 /* Tests the state of error_flag. */
1506 bool
1507 gfc_error_flag_test (void)
1509 return error_buffer.flag
1510 || !gfc_output_buffer_empty_p (pp_error_buffer);
1514 /* Check to see if any errors have been saved.
1515 If so, print the error. Returns the state of error_flag. */
1517 bool
1518 gfc_error_check (void)
1520 bool error_raised = (bool) error_buffer.flag;
1522 if (error_raised)
1524 if (error_buffer.message != NULL)
1525 fputs (error_buffer.message, stderr);
1526 error_buffer.flag = 0;
1527 gfc_clear_pp_buffer (pp_error_buffer);
1529 gfc_increment_error_count();
1531 if (flag_fatal_errors)
1532 exit (FATAL_EXIT_CODE);
1534 /* This is for the new diagnostics machinery. */
1535 else if (! gfc_output_buffer_empty_p (pp_error_buffer))
1537 error_raised = true;
1538 pretty_printer *pp = global_dc->printer;
1539 output_buffer *tmp_buffer = pp->buffer;
1540 pp->buffer = pp_error_buffer;
1541 pp_really_flush (pp);
1542 ++errorcount;
1543 gcc_assert (gfc_output_buffer_empty_p (pp_error_buffer));
1544 diagnostic_action_after_output (global_dc, DK_ERROR);
1545 pp->buffer = tmp_buffer;
1548 return error_raised;
1551 /* Move the text buffered from FROM to TO, then clear
1552 FROM. Independently if there was text in FROM, TO is also
1553 cleared. */
1555 static void
1556 gfc_move_output_buffer_from_to (output_buffer *from, output_buffer *to)
1558 gfc_clear_pp_buffer (to);
1559 /* We make sure this is always buffered. */
1560 to->flush_p = false;
1562 if (! gfc_output_buffer_empty_p (from))
1564 const char *str = output_buffer_formatted_text (from);
1565 output_buffer_append_r (to, str, strlen (str));
1566 gfc_clear_pp_buffer (from);
1570 /* Save the existing error state. */
1572 void
1573 gfc_push_error (output_buffer *buffer_err, gfc_error_buf *err)
1575 err->flag = error_buffer.flag;
1576 if (error_buffer.flag)
1577 err->message = xstrdup (error_buffer.message);
1579 error_buffer.flag = 0;
1581 /* This part uses the common diagnostics. */
1582 gfc_move_output_buffer_from_to (pp_error_buffer, buffer_err);
1586 /* Restore a previous pushed error state. */
1588 void
1589 gfc_pop_error (output_buffer *buffer_err, gfc_error_buf *err)
1591 error_buffer.flag = err->flag;
1592 if (error_buffer.flag)
1594 size_t len = strlen (err->message) + 1;
1595 gcc_assert (len <= error_buffer.allocated);
1596 memcpy (error_buffer.message, err->message, len);
1597 free (err->message);
1599 /* This part uses the common diagnostics. */
1600 gfc_move_output_buffer_from_to (buffer_err, pp_error_buffer);
1604 /* Free a pushed error state, but keep the current error state. */
1606 void
1607 gfc_free_error (output_buffer *buffer_err, gfc_error_buf *err)
1609 if (err->flag)
1610 free (err->message);
1612 gfc_clear_pp_buffer (buffer_err);
1616 /* Report the number of warnings and errors that occurred to the caller. */
1618 void
1619 gfc_get_errors (int *w, int *e)
1621 if (w != NULL)
1622 *w = warnings + warningcount + werrorcount;
1623 if (e != NULL)
1624 *e = errors + errorcount + sorrycount + werrorcount;
1628 /* Switch errors into warnings. */
1630 void
1631 gfc_errors_to_warnings (bool f)
1633 warnings_not_errors = f;
1636 void
1637 gfc_diagnostics_init (void)
1639 diagnostic_starter (global_dc) = gfc_diagnostic_starter;
1640 diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
1641 diagnostic_format_decoder (global_dc) = gfc_format_decoder;
1642 global_dc->caret_char = '^';
1643 pp_warning_buffer = new (XNEW (output_buffer)) output_buffer ();
1644 pp_warning_buffer->flush_p = false;
1645 pp_error_buffer = new (XNEW (output_buffer)) output_buffer ();
1646 pp_error_buffer->flush_p = false;
1649 void
1650 gfc_diagnostics_finish (void)
1652 tree_diagnostics_defaults (global_dc);
1653 /* We still want to use the gfc starter and finalizer, not the tree
1654 defaults. */
1655 diagnostic_starter (global_dc) = gfc_diagnostic_starter;
1656 diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
1657 global_dc->caret_char = '^';