2015-05-22 Pascal Obry <obry@adacore.com>
[official-gcc.git] / gcc / fortran / error.c
blob23308b6544e3831916d0c1fddbbec6e9184d2263
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 /* This is just a helper function to avoid duplicating the logic of
811 gfc_warning. */
813 static bool
814 gfc_warning (int opt, const char *gmsgid, va_list ap)
816 va_list argp;
817 va_copy (argp, ap);
819 diagnostic_info diagnostic;
820 bool fatal_errors = global_dc->fatal_errors;
821 pretty_printer *pp = global_dc->printer;
822 output_buffer *tmp_buffer = pp->buffer;
824 gfc_clear_pp_buffer (pp_warning_buffer);
826 if (buffered_p)
828 pp->buffer = pp_warning_buffer;
829 global_dc->fatal_errors = false;
830 /* To prevent -fmax-errors= triggering. */
831 --werrorcount;
834 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION,
835 DK_WARNING);
836 diagnostic.option_index = opt;
837 bool ret = report_diagnostic (&diagnostic);
839 if (buffered_p)
841 pp->buffer = tmp_buffer;
842 global_dc->fatal_errors = fatal_errors;
844 warningcount_buffered = 0;
845 werrorcount_buffered = 0;
846 /* Undo the above --werrorcount if not Werror, otherwise
847 werrorcount is correct already. */
848 if (!ret)
849 ++werrorcount;
850 else if (diagnostic.kind == DK_ERROR)
851 ++werrorcount_buffered;
852 else
853 ++werrorcount, --warningcount, ++warningcount_buffered;
856 va_end (argp);
857 return ret;
860 /* Issue a warning. */
862 bool
863 gfc_warning (int opt, const char *gmsgid, ...)
865 va_list argp;
867 va_start (argp, gmsgid);
868 bool ret = gfc_warning (opt, gmsgid, argp);
869 va_end (argp);
870 return ret;
874 /* Whether, for a feature included in a given standard set (GFC_STD_*),
875 we should issue an error or a warning, or be quiet. */
877 notification
878 gfc_notification_std (int std)
880 bool warning;
882 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
883 if ((gfc_option.allow_std & std) != 0 && !warning)
884 return SILENT;
886 return warning ? WARNING : ERROR;
890 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
891 feature. An error/warning will be issued if the currently selected
892 standard does not contain the requested bits. Return false if
893 an error is generated. */
895 bool
896 gfc_notify_std (int std, const char *gmsgid, ...)
898 va_list argp;
899 bool warning;
900 const char *msg, *msg2;
901 char *buffer;
903 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
904 if ((gfc_option.allow_std & std) != 0 && !warning)
905 return true;
907 if (suppress_errors)
908 return warning ? true : false;
910 switch (std)
912 case GFC_STD_F2008_TS:
913 msg = "TS 29113/TS 18508:";
914 break;
915 case GFC_STD_F2008_OBS:
916 msg = _("Fortran 2008 obsolescent feature:");
917 break;
918 case GFC_STD_F2008:
919 msg = "Fortran 2008:";
920 break;
921 case GFC_STD_F2003:
922 msg = "Fortran 2003:";
923 break;
924 case GFC_STD_GNU:
925 msg = _("GNU Extension:");
926 break;
927 case GFC_STD_LEGACY:
928 msg = _("Legacy Extension:");
929 break;
930 case GFC_STD_F95_OBS:
931 msg = _("Obsolescent feature:");
932 break;
933 case GFC_STD_F95_DEL:
934 msg = _("Deleted feature:");
935 break;
936 default:
937 gcc_unreachable ();
940 msg2 = _(gmsgid);
941 buffer = (char *) alloca (strlen (msg) + strlen (msg2) + 2);
942 strcpy (buffer, msg);
943 strcat (buffer, " ");
944 strcat (buffer, msg2);
946 va_start (argp, gmsgid);
947 if (warning)
948 gfc_warning (0, buffer, argp);
949 else
950 gfc_error (buffer, argp);
951 va_end (argp);
953 return (warning && !warnings_are_errors) ? true : false;
957 /* Called from output_format -- during diagnostic message processing
958 to handle Fortran specific format specifiers with the following meanings:
960 %C Current locus (no argument)
961 %L Takes locus argument
963 static bool
964 gfc_format_decoder (pretty_printer *pp,
965 text_info *text, const char *spec,
966 int precision ATTRIBUTE_UNUSED, bool wide ATTRIBUTE_UNUSED,
967 bool plus ATTRIBUTE_UNUSED, bool hash ATTRIBUTE_UNUSED)
969 switch (*spec)
971 case 'C':
972 case 'L':
974 static const char *result[2] = { "(1)", "(2)" };
975 locus *loc;
976 if (*spec == 'C')
977 loc = &gfc_current_locus;
978 else
979 loc = va_arg (*text->args_ptr, locus *);
980 gcc_assert (loc->nextc - loc->lb->line >= 0);
981 unsigned int offset = loc->nextc - loc->lb->line;
982 /* If location[0] != UNKNOWN_LOCATION means that we already
983 processed one of %C/%L. */
984 int loc_num = text->get_location (0) == UNKNOWN_LOCATION ? 0 : 1;
985 text->set_location (loc_num,
986 linemap_position_for_loc_and_offset (line_table,
987 loc->lb->location,
988 offset));
989 pp_string (pp, result[loc_num]);
990 return true;
992 default:
993 return false;
997 /* Return a malloc'd string describing the kind of diagnostic. The
998 caller is responsible for freeing the memory. */
999 static char *
1000 gfc_diagnostic_build_kind_prefix (diagnostic_context *context,
1001 const diagnostic_info *diagnostic)
1003 static const char *const diagnostic_kind_text[] = {
1004 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
1005 #include "gfc-diagnostic.def"
1006 #undef DEFINE_DIAGNOSTIC_KIND
1007 "must-not-happen"
1009 static const char *const diagnostic_kind_color[] = {
1010 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
1011 #include "gfc-diagnostic.def"
1012 #undef DEFINE_DIAGNOSTIC_KIND
1013 NULL
1015 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
1016 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
1017 const char *text_cs = "", *text_ce = "";
1018 pretty_printer *pp = context->printer;
1020 if (diagnostic_kind_color[diagnostic->kind])
1022 text_cs = colorize_start (pp_show_color (pp),
1023 diagnostic_kind_color[diagnostic->kind]);
1024 text_ce = colorize_stop (pp_show_color (pp));
1026 return build_message_string ("%s%s:%s ", text_cs, text, text_ce);
1029 /* Return a malloc'd string describing a location. The caller is
1030 responsible for freeing the memory. */
1031 static char *
1032 gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
1033 expanded_location s)
1035 pretty_printer *pp = context->printer;
1036 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
1037 const char *locus_ce = colorize_stop (pp_show_color (pp));
1038 return (s.file == NULL
1039 ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
1040 : !strcmp (s.file, N_("<built-in>"))
1041 ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
1042 : context->show_column
1043 ? build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
1044 s.column, locus_ce)
1045 : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line, locus_ce));
1048 /* Return a malloc'd string describing two locations. The caller is
1049 responsible for freeing the memory. */
1050 static char *
1051 gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
1052 expanded_location s, expanded_location s2)
1054 pretty_printer *pp = context->printer;
1055 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
1056 const char *locus_ce = colorize_stop (pp_show_color (pp));
1058 return (s.file == NULL
1059 ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
1060 : !strcmp (s.file, N_("<built-in>"))
1061 ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
1062 : context->show_column
1063 ? build_message_string ("%s%s:%d:%d-%d:%s", locus_cs, s.file, s.line,
1064 MIN (s.column, s2.column),
1065 MAX (s.column, s2.column), locus_ce)
1066 : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
1067 locus_ce));
1070 /* This function prints the locus (file:line:column), the diagnostic kind
1071 (Error, Warning) and (optionally) the caret line (a source line
1072 with '1' and/or '2' below it).
1074 With -fdiagnostic-show-caret (the default) and for valid locations,
1075 it prints for one location:
1077 [locus]:
1079 some code
1081 Error: Some error at (1)
1083 for two locations that fit in the same locus line:
1085 [locus]:
1087 some code and some more code
1089 Error: Some error at (1) and (2)
1091 and for two locations that do not fit in the same locus line:
1093 [locus]:
1095 some code
1097 [locus2]:
1099 some other code
1101 Error: Some error at (1) and (2)
1103 With -fno-diagnostic-show-caret or if one of the locations is not
1104 valid, it prints for one location (or for two locations that fit in
1105 the same locus line):
1107 [locus]: Error: Some error at (1) and (2)
1109 and for two locations that do not fit in the same locus line:
1111 [name]:[locus]: Error: (1)
1112 [name]:[locus2]: Error: Some error at (1) and (2)
1114 static void
1115 gfc_diagnostic_starter (diagnostic_context *context,
1116 diagnostic_info *diagnostic)
1118 char * kind_prefix = gfc_diagnostic_build_kind_prefix (context, diagnostic);
1120 expanded_location s1 = diagnostic_expand_location (diagnostic);
1121 expanded_location s2;
1122 bool one_locus = diagnostic_location (diagnostic, 1) == UNKNOWN_LOCATION;
1123 bool same_locus = false;
1125 if (!one_locus)
1127 s2 = diagnostic_expand_location (diagnostic, 1);
1128 same_locus = diagnostic_same_line (context, s1, s2);
1131 char * locus_prefix = (one_locus || !same_locus)
1132 ? gfc_diagnostic_build_locus_prefix (context, s1)
1133 : gfc_diagnostic_build_locus_prefix (context, s1, s2);
1135 if (!context->show_caret
1136 || diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION
1137 || diagnostic_location (diagnostic, 0) == context->last_location)
1139 pp_set_prefix (context->printer,
1140 concat (locus_prefix, " ", kind_prefix, NULL));
1141 free (locus_prefix);
1143 if (one_locus || same_locus)
1145 free (kind_prefix);
1146 return;
1148 /* In this case, we print the previous locus and prefix as:
1150 [locus]:[prefix]: (1)
1152 and we flush with a new line before setting the new prefix. */
1153 pp_string (context->printer, "(1)");
1154 pp_newline (context->printer);
1155 locus_prefix = gfc_diagnostic_build_locus_prefix (context, s2);
1156 pp_set_prefix (context->printer,
1157 concat (locus_prefix, " ", kind_prefix, NULL));
1158 free (kind_prefix);
1159 free (locus_prefix);
1161 else
1163 pp_verbatim (context->printer, locus_prefix);
1164 free (locus_prefix);
1165 /* Fortran uses an empty line between locus and caret line. */
1166 pp_newline (context->printer);
1167 diagnostic_show_locus (context, diagnostic);
1168 pp_newline (context->printer);
1169 /* If the caret line was shown, the prefix does not contain the
1170 locus. */
1171 pp_set_prefix (context->printer, kind_prefix);
1173 if (one_locus || same_locus)
1174 return;
1176 locus_prefix = gfc_diagnostic_build_locus_prefix (context, s2);
1177 if (diagnostic_location (diagnostic, 1) <= BUILTINS_LOCATION)
1179 /* No caret line for the second location. Override the previous
1180 prefix with [locus2]:[prefix]. */
1181 pp_set_prefix (context->printer,
1182 concat (locus_prefix, " ", kind_prefix, NULL));
1183 free (kind_prefix);
1184 free (locus_prefix);
1186 else
1188 /* We print the caret for the second location. */
1189 pp_verbatim (context->printer, locus_prefix);
1190 free (locus_prefix);
1191 /* Fortran uses an empty line between locus and caret line. */
1192 pp_newline (context->printer);
1193 s1.column = 0; /* Print only a caret line for s2. */
1194 diagnostic_print_caret_line (context, s2, s1,
1195 context->caret_chars[1], '\0');
1196 pp_newline (context->printer);
1197 /* If the caret line was shown, the prefix does not contain the
1198 locus. */
1199 pp_set_prefix (context->printer, kind_prefix);
1204 static void
1205 gfc_diagnostic_finalizer (diagnostic_context *context,
1206 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
1208 pp_destroy_prefix (context->printer);
1209 pp_newline_and_flush (context->printer);
1212 /* Immediate warning (i.e. do not buffer the warning) with an explicit
1213 location. */
1215 bool
1216 gfc_warning_now_at (location_t loc, int opt, const char *gmsgid, ...)
1218 va_list argp;
1219 diagnostic_info diagnostic;
1220 bool ret;
1222 va_start (argp, gmsgid);
1223 diagnostic_set_info (&diagnostic, gmsgid, &argp, loc, DK_WARNING);
1224 diagnostic.option_index = opt;
1225 ret = report_diagnostic (&diagnostic);
1226 va_end (argp);
1227 return ret;
1230 /* Immediate warning (i.e. do not buffer the warning). */
1232 bool
1233 gfc_warning_now (int opt, const char *gmsgid, ...)
1235 va_list argp;
1236 diagnostic_info diagnostic;
1237 bool ret;
1239 va_start (argp, gmsgid);
1240 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION,
1241 DK_WARNING);
1242 diagnostic.option_index = opt;
1243 ret = report_diagnostic (&diagnostic);
1244 va_end (argp);
1245 return ret;
1249 /* Immediate error (i.e. do not buffer). */
1250 /* This function uses the common diagnostics, but does not support
1251 two locations; when being used in scanner.c, ensure that the location
1252 is properly setup. Otherwise, use gfc_error_now_1. */
1254 void
1255 gfc_error_now (const char *gmsgid, ...)
1257 va_list argp;
1258 diagnostic_info diagnostic;
1260 va_start (argp, gmsgid);
1261 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_ERROR);
1262 report_diagnostic (&diagnostic);
1263 va_end (argp);
1267 /* Fatal error, never returns. */
1269 void
1270 gfc_fatal_error (const char *gmsgid, ...)
1272 va_list argp;
1273 diagnostic_info diagnostic;
1275 va_start (argp, gmsgid);
1276 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_FATAL);
1277 report_diagnostic (&diagnostic);
1278 va_end (argp);
1280 gcc_unreachable ();
1283 /* Clear the warning flag. */
1285 void
1286 gfc_clear_warning (void)
1288 warning_buffer.flag = 0;
1290 gfc_clear_pp_buffer (pp_warning_buffer);
1291 warningcount_buffered = 0;
1292 werrorcount_buffered = 0;
1296 /* Check to see if any warnings have been saved.
1297 If so, print the warning. */
1299 void
1300 gfc_warning_check (void)
1302 if (warning_buffer.flag)
1304 warnings++;
1305 if (warning_buffer.message != NULL)
1306 fputs (warning_buffer.message, stderr);
1307 gfc_clear_warning ();
1309 /* This is for the new diagnostics machinery. */
1310 else if (! gfc_output_buffer_empty_p (pp_warning_buffer))
1312 pretty_printer *pp = global_dc->printer;
1313 output_buffer *tmp_buffer = pp->buffer;
1314 pp->buffer = pp_warning_buffer;
1315 pp_really_flush (pp);
1316 warningcount += warningcount_buffered;
1317 werrorcount += werrorcount_buffered;
1318 gcc_assert (warningcount_buffered + werrorcount_buffered == 1);
1319 diagnostic_action_after_output (global_dc,
1320 warningcount_buffered
1321 ? DK_WARNING : DK_ERROR);
1322 pp->buffer = tmp_buffer;
1327 /* Issue an error. */
1328 /* Use gfc_error instead, unless two locations are used in the same
1329 warning or for scanner.c, if the location is not properly set up. */
1331 void
1332 gfc_error_1 (const char *gmsgid, ...)
1334 va_list argp;
1336 if (warnings_not_errors)
1337 goto warning;
1339 if (suppress_errors)
1340 return;
1342 error_buffer.flag = 1;
1343 error_buffer.index = 0;
1344 cur_error_buffer = &error_buffer;
1346 va_start (argp, gmsgid);
1347 error_print (_("Error:"), _(gmsgid), argp);
1348 va_end (argp);
1350 error_char ('\0');
1352 if (!buffered_p)
1353 gfc_increment_error_count();
1355 return;
1357 warning:
1359 if (inhibit_warnings)
1360 return;
1362 warning_buffer.flag = 1;
1363 warning_buffer.index = 0;
1364 cur_error_buffer = &warning_buffer;
1366 va_start (argp, gmsgid);
1367 error_print (_("Warning:"), _(gmsgid), argp);
1368 va_end (argp);
1370 error_char ('\0');
1372 if (!buffered_p)
1374 warnings++;
1375 if (warnings_are_errors)
1376 gfc_increment_error_count();
1380 /* Issue an error. */
1381 /* This function uses the common diagnostics, but does not support
1382 two locations; when being used in scanner.c, ensure that the location
1383 is properly setup. Otherwise, use gfc_error_1. */
1385 static void
1386 gfc_error (const char *gmsgid, va_list ap)
1388 va_list argp;
1389 va_copy (argp, ap);
1391 if (warnings_not_errors)
1393 gfc_warning (/*opt=*/0, gmsgid, argp);
1394 va_end (argp);
1395 return;
1398 if (suppress_errors)
1400 va_end (argp);
1401 return;
1404 diagnostic_info diagnostic;
1405 bool fatal_errors = global_dc->fatal_errors;
1406 pretty_printer *pp = global_dc->printer;
1407 output_buffer *tmp_buffer = pp->buffer;
1409 gfc_clear_pp_buffer (pp_error_buffer);
1411 if (buffered_p)
1413 pp->buffer = pp_error_buffer;
1414 global_dc->fatal_errors = false;
1415 /* To prevent -fmax-errors= triggering, we decrease it before
1416 report_diagnostic increases it. */
1417 --errorcount;
1420 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_ERROR);
1421 report_diagnostic (&diagnostic);
1423 if (buffered_p)
1425 pp->buffer = tmp_buffer;
1426 global_dc->fatal_errors = fatal_errors;
1429 va_end (argp);
1433 void
1434 gfc_error (const char *gmsgid, ...)
1436 va_list argp;
1437 va_start (argp, gmsgid);
1438 gfc_error (gmsgid, argp);
1439 va_end (argp);
1443 /* Immediate error. */
1444 /* Use gfc_error_now instead, unless two locations are used in the same
1445 warning or for scanner.c, if the location is not properly set up. */
1447 void
1448 gfc_error_now_1 (const char *gmsgid, ...)
1450 va_list argp;
1451 bool buffered_p_saved;
1453 error_buffer.flag = 1;
1454 error_buffer.index = 0;
1455 cur_error_buffer = &error_buffer;
1457 buffered_p_saved = buffered_p;
1458 buffered_p = false;
1460 va_start (argp, gmsgid);
1461 error_print (_("Error:"), _(gmsgid), argp);
1462 va_end (argp);
1464 error_char ('\0');
1466 gfc_increment_error_count();
1468 buffered_p = buffered_p_saved;
1470 if (flag_fatal_errors)
1471 exit (FATAL_EXIT_CODE);
1475 /* This shouldn't happen... but sometimes does. */
1477 void
1478 gfc_internal_error (const char *gmsgid, ...)
1480 va_list argp;
1481 diagnostic_info diagnostic;
1483 va_start (argp, gmsgid);
1484 diagnostic_set_info (&diagnostic, gmsgid, &argp, UNKNOWN_LOCATION, DK_ICE);
1485 report_diagnostic (&diagnostic);
1486 va_end (argp);
1488 gcc_unreachable ();
1492 /* Clear the error flag when we start to compile a source line. */
1494 void
1495 gfc_clear_error (void)
1497 error_buffer.flag = 0;
1498 warnings_not_errors = false;
1499 gfc_clear_pp_buffer (pp_error_buffer);
1503 /* Tests the state of error_flag. */
1505 bool
1506 gfc_error_flag_test (void)
1508 return error_buffer.flag
1509 || !gfc_output_buffer_empty_p (pp_error_buffer);
1513 /* Check to see if any errors have been saved.
1514 If so, print the error. Returns the state of error_flag. */
1516 bool
1517 gfc_error_check (void)
1519 bool error_raised = (bool) error_buffer.flag;
1521 if (error_raised)
1523 if (error_buffer.message != NULL)
1524 fputs (error_buffer.message, stderr);
1525 error_buffer.flag = 0;
1526 gfc_clear_pp_buffer (pp_error_buffer);
1528 gfc_increment_error_count();
1530 if (flag_fatal_errors)
1531 exit (FATAL_EXIT_CODE);
1533 /* This is for the new diagnostics machinery. */
1534 else if (! gfc_output_buffer_empty_p (pp_error_buffer))
1536 error_raised = true;
1537 pretty_printer *pp = global_dc->printer;
1538 output_buffer *tmp_buffer = pp->buffer;
1539 pp->buffer = pp_error_buffer;
1540 pp_really_flush (pp);
1541 ++errorcount;
1542 gcc_assert (gfc_output_buffer_empty_p (pp_error_buffer));
1543 diagnostic_action_after_output (global_dc, DK_ERROR);
1544 pp->buffer = tmp_buffer;
1547 return error_raised;
1550 /* Move the text buffered from FROM to TO, then clear
1551 FROM. Independently if there was text in FROM, TO is also
1552 cleared. */
1554 static void
1555 gfc_move_output_buffer_from_to (output_buffer *from, output_buffer *to)
1557 gfc_clear_pp_buffer (to);
1558 /* We make sure this is always buffered. */
1559 to->flush_p = false;
1561 if (! gfc_output_buffer_empty_p (from))
1563 const char *str = output_buffer_formatted_text (from);
1564 output_buffer_append_r (to, str, strlen (str));
1565 gfc_clear_pp_buffer (from);
1569 /* Save the existing error state. */
1571 void
1572 gfc_push_error (output_buffer *buffer_err, gfc_error_buf *err)
1574 err->flag = error_buffer.flag;
1575 if (error_buffer.flag)
1576 err->message = xstrdup (error_buffer.message);
1578 error_buffer.flag = 0;
1580 /* This part uses the common diagnostics. */
1581 gfc_move_output_buffer_from_to (pp_error_buffer, buffer_err);
1585 /* Restore a previous pushed error state. */
1587 void
1588 gfc_pop_error (output_buffer *buffer_err, gfc_error_buf *err)
1590 error_buffer.flag = err->flag;
1591 if (error_buffer.flag)
1593 size_t len = strlen (err->message) + 1;
1594 gcc_assert (len <= error_buffer.allocated);
1595 memcpy (error_buffer.message, err->message, len);
1596 free (err->message);
1598 /* This part uses the common diagnostics. */
1599 gfc_move_output_buffer_from_to (buffer_err, pp_error_buffer);
1603 /* Free a pushed error state, but keep the current error state. */
1605 void
1606 gfc_free_error (output_buffer *buffer_err, gfc_error_buf *err)
1608 if (err->flag)
1609 free (err->message);
1611 gfc_clear_pp_buffer (buffer_err);
1615 /* Report the number of warnings and errors that occurred to the caller. */
1617 void
1618 gfc_get_errors (int *w, int *e)
1620 if (w != NULL)
1621 *w = warnings + warningcount + werrorcount;
1622 if (e != NULL)
1623 *e = errors + errorcount + sorrycount + werrorcount;
1627 /* Switch errors into warnings. */
1629 void
1630 gfc_errors_to_warnings (bool f)
1632 warnings_not_errors = f;
1635 void
1636 gfc_diagnostics_init (void)
1638 diagnostic_starter (global_dc) = gfc_diagnostic_starter;
1639 diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
1640 diagnostic_format_decoder (global_dc) = gfc_format_decoder;
1641 global_dc->caret_chars[0] = '1';
1642 global_dc->caret_chars[1] = '2';
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_chars[0] = '^';
1658 global_dc->caret_chars[1] = '^';