2016-07-28 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / error.c
blobacba1c874cc513af12045b40775b37a0f066b425
1 /* Handle errors.
2 Copyright (C) 2000-2016 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 "options.h"
31 #include "gfortran.h"
33 #include "diagnostic.h"
34 #include "diagnostic-color.h"
35 #include "tree-diagnostic.h" /* tree_diagnostics_defaults */
37 static int suppress_errors = 0;
39 static bool warnings_not_errors = false;
41 static int terminal_width;
43 /* True if the error/warnings should be buffered. */
44 static bool buffered_p;
46 static gfc_error_buffer error_buffer;
47 /* These are always buffered buffers (.flush_p == false) to be used by
48 the pretty-printer. */
49 static output_buffer *pp_error_buffer, *pp_warning_buffer;
50 static int warningcount_buffered, werrorcount_buffered;
52 /* Return true if there output_buffer is empty. */
54 static bool
55 gfc_output_buffer_empty_p (const output_buffer * buf)
57 return output_buffer_last_position_in_text (buf) == NULL;
60 /* Go one level deeper suppressing errors. */
62 void
63 gfc_push_suppress_errors (void)
65 gcc_assert (suppress_errors >= 0);
66 ++suppress_errors;
69 static void
70 gfc_error (const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(1,0);
72 static bool
73 gfc_warning (int opt, const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(2,0);
76 /* Leave one level of error suppressing. */
78 void
79 gfc_pop_suppress_errors (void)
81 gcc_assert (suppress_errors > 0);
82 --suppress_errors;
86 /* Determine terminal width (for trimming source lines in output). */
88 static int
89 gfc_get_terminal_width (void)
91 return isatty (STDERR_FILENO) ? get_terminal_width () : INT_MAX;
95 /* Per-file error initialization. */
97 void
98 gfc_error_init_1 (void)
100 terminal_width = gfc_get_terminal_width ();
101 gfc_buffer_error (false);
105 /* Set the flag for buffering errors or not. */
107 void
108 gfc_buffer_error (bool flag)
110 buffered_p = flag;
114 /* Add a single character to the error buffer or output depending on
115 buffered_p. */
117 static void
118 error_char (char)
120 /* FIXME: Unused function to be removed in a subsequent patch. */
124 /* Copy a string to wherever it needs to go. */
126 static void
127 error_string (const char *p)
129 while (*p)
130 error_char (*p++);
134 /* Print a formatted integer to the error buffer or output. */
136 #define IBUF_LEN 60
138 static void
139 error_uinteger (unsigned long int i)
141 char *p, int_buf[IBUF_LEN];
143 p = int_buf + IBUF_LEN - 1;
144 *p-- = '\0';
146 if (i == 0)
147 *p-- = '0';
149 while (i > 0)
151 *p-- = i % 10 + '0';
152 i = i / 10;
155 error_string (p + 1);
158 static void
159 error_integer (long int i)
161 unsigned long int u;
163 if (i < 0)
165 u = (unsigned long int) -i;
166 error_char ('-');
168 else
169 u = i;
171 error_uinteger (u);
175 static size_t
176 gfc_widechar_display_length (gfc_char_t c)
178 if (gfc_wide_is_printable (c) || c == '\t')
179 /* Printable ASCII character, or tabulation (output as a space). */
180 return 1;
181 else if (c < ((gfc_char_t) 1 << 8))
182 /* Displayed as \x?? */
183 return 4;
184 else if (c < ((gfc_char_t) 1 << 16))
185 /* Displayed as \u???? */
186 return 6;
187 else
188 /* Displayed as \U???????? */
189 return 10;
193 /* Length of the ASCII representation of the wide string, escaping wide
194 characters as print_wide_char_into_buffer() does. */
196 static size_t
197 gfc_wide_display_length (const gfc_char_t *str)
199 size_t i, len;
201 for (i = 0, len = 0; str[i]; i++)
202 len += gfc_widechar_display_length (str[i]);
204 return len;
207 static int
208 print_wide_char_into_buffer (gfc_char_t c, char *buf)
210 static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6',
211 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
213 if (gfc_wide_is_printable (c) || c == '\t')
215 buf[1] = '\0';
216 /* Tabulation is output as a space. */
217 buf[0] = (unsigned char) (c == '\t' ? ' ' : c);
218 return 1;
220 else if (c < ((gfc_char_t) 1 << 8))
222 buf[4] = '\0';
223 buf[3] = xdigit[c & 0x0F];
224 c = c >> 4;
225 buf[2] = xdigit[c & 0x0F];
227 buf[1] = 'x';
228 buf[0] = '\\';
229 return 4;
231 else if (c < ((gfc_char_t) 1 << 16))
233 buf[6] = '\0';
234 buf[5] = xdigit[c & 0x0F];
235 c = c >> 4;
236 buf[4] = xdigit[c & 0x0F];
237 c = c >> 4;
238 buf[3] = xdigit[c & 0x0F];
239 c = c >> 4;
240 buf[2] = xdigit[c & 0x0F];
242 buf[1] = 'u';
243 buf[0] = '\\';
244 return 6;
246 else
248 buf[10] = '\0';
249 buf[9] = xdigit[c & 0x0F];
250 c = c >> 4;
251 buf[8] = xdigit[c & 0x0F];
252 c = c >> 4;
253 buf[7] = xdigit[c & 0x0F];
254 c = c >> 4;
255 buf[6] = xdigit[c & 0x0F];
256 c = c >> 4;
257 buf[5] = xdigit[c & 0x0F];
258 c = c >> 4;
259 buf[4] = xdigit[c & 0x0F];
260 c = c >> 4;
261 buf[3] = xdigit[c & 0x0F];
262 c = c >> 4;
263 buf[2] = xdigit[c & 0x0F];
265 buf[1] = 'U';
266 buf[0] = '\\';
267 return 10;
271 static char wide_char_print_buffer[11];
273 const char *
274 gfc_print_wide_char (gfc_char_t c)
276 print_wide_char_into_buffer (c, wide_char_print_buffer);
277 return wide_char_print_buffer;
281 /* Show the file, where it was included, and the source line, give a
282 locus. Calls error_printf() recursively, but the recursion is at
283 most one level deep. */
285 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
287 static void
288 show_locus (locus *loc, int c1, int c2)
290 gfc_linebuf *lb;
291 gfc_file *f;
292 gfc_char_t *p;
293 int i, offset, cmax;
295 /* TODO: Either limit the total length and number of included files
296 displayed or add buffering of arbitrary number of characters in
297 error messages. */
299 /* Write out the error header line, giving the source file and error
300 location (in GNU standard "[file]:[line].[column]:" format),
301 followed by an "included by" stack and a blank line. This header
302 format is matched by a testsuite parser defined in
303 lib/gfortran-dg.exp. */
305 lb = loc->lb;
306 f = lb->file;
308 error_string (f->filename);
309 error_char (':');
311 error_integer (LOCATION_LINE (lb->location));
313 if ((c1 > 0) || (c2 > 0))
314 error_char ('.');
316 if (c1 > 0)
317 error_integer (c1);
319 if ((c1 > 0) && (c2 > 0))
320 error_char ('-');
322 if (c2 > 0)
323 error_integer (c2);
325 error_char (':');
326 error_char ('\n');
328 for (;;)
330 i = f->inclusion_line;
332 f = f->up;
333 if (f == NULL) break;
335 error_printf (" Included at %s:%d:", f->filename, i);
338 error_char ('\n');
340 /* Calculate an appropriate horizontal offset of the source line in
341 order to get the error locus within the visible portion of the
342 line. Note that if the margin of 5 here is changed, the
343 corresponding margin of 10 in show_loci should be changed. */
345 offset = 0;
347 /* If the two loci would appear in the same column, we shift
348 '2' one column to the right, so as to print '12' rather than
349 just '1'. We do this here so it will be accounted for in the
350 margin calculations. */
352 if (c1 == c2)
353 c2 += 1;
355 cmax = (c1 < c2) ? c2 : c1;
356 if (cmax > terminal_width - 5)
357 offset = cmax - terminal_width + 5;
359 /* Show the line itself, taking care not to print more than what can
360 show up on the terminal. Tabs are converted to spaces, and
361 nonprintable characters are converted to a "\xNN" sequence. */
363 p = &(lb->line[offset]);
364 i = gfc_wide_display_length (p);
365 if (i > terminal_width)
366 i = terminal_width - 1;
368 while (i > 0)
370 static char buffer[11];
371 i -= print_wide_char_into_buffer (*p++, buffer);
372 error_string (buffer);
375 error_char ('\n');
377 /* Show the '1' and/or '2' corresponding to the column of the error
378 locus. Note that a value of -1 for c1 or c2 will simply cause
379 the relevant number not to be printed. */
381 c1 -= offset;
382 c2 -= offset;
383 cmax -= offset;
385 p = &(lb->line[offset]);
386 for (i = 0; i < cmax; i++)
388 int spaces, j;
389 spaces = gfc_widechar_display_length (*p++);
391 if (i == c1)
392 error_char ('1'), spaces--;
393 else if (i == c2)
394 error_char ('2'), spaces--;
396 for (j = 0; j < spaces; j++)
397 error_char (' ');
400 if (i == c1)
401 error_char ('1');
402 else if (i == c2)
403 error_char ('2');
405 error_char ('\n');
410 /* As part of printing an error, we show the source lines that caused
411 the problem. We show at least one, and possibly two loci; the two
412 loci may or may not be on the same source line. */
414 static void
415 show_loci (locus *l1, locus *l2)
417 int m, c1, c2;
419 if (l1 == NULL || l1->lb == NULL)
421 error_printf ("<During initialization>\n");
422 return;
425 /* While calculating parameters for printing the loci, we consider possible
426 reasons for printing one per line. If appropriate, print the loci
427 individually; otherwise we print them both on the same line. */
429 c1 = l1->nextc - l1->lb->line;
430 if (l2 == NULL)
432 show_locus (l1, c1, -1);
433 return;
436 c2 = l2->nextc - l2->lb->line;
438 if (c1 < c2)
439 m = c2 - c1;
440 else
441 m = c1 - c2;
443 /* Note that the margin value of 10 here needs to be less than the
444 margin of 5 used in the calculation of offset in show_locus. */
446 if (l1->lb != l2->lb || m > terminal_width - 10)
448 show_locus (l1, c1, -1);
449 show_locus (l2, -1, c2);
450 return;
453 show_locus (l1, c1, c2);
455 return;
459 /* Workhorse for the error printing subroutines. This subroutine is
460 inspired by g77's error handling and is similar to printf() with
461 the following %-codes:
463 %c Character, %d or %i Integer, %s String, %% Percent
464 %L Takes locus argument
465 %C Current locus (no argument)
467 If a locus pointer is given, the actual source line is printed out
468 and the column is indicated. Since we want the error message at
469 the bottom of any source file information, we must scan the
470 argument list twice -- once to determine whether the loci are
471 present and record this for printing, and once to print the error
472 message after and loci have been printed. A maximum of two locus
473 arguments are permitted.
475 This function is also called (recursively) by show_locus in the
476 case of included files; however, as show_locus does not resupply
477 any loci, the recursion is at most one level deep. */
479 #define MAX_ARGS 10
481 static void ATTRIBUTE_GCC_GFC(2,0)
482 error_print (const char *type, const char *format0, va_list argp)
484 enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
485 TYPE_LONGINT, TYPE_ULONGINT, TYPE_CHAR, TYPE_STRING,
486 NOTYPE };
487 struct
489 int type;
490 int pos;
491 union
493 int intval;
494 unsigned int uintval;
495 long int longintval;
496 unsigned long int ulongintval;
497 char charval;
498 const char * stringval;
499 } u;
500 } arg[MAX_ARGS], spec[MAX_ARGS];
501 /* spec is the array of specifiers, in the same order as they
502 appear in the format string. arg is the array of arguments,
503 in the same order as they appear in the va_list. */
505 char c;
506 int i, n, have_l1, pos, maxpos;
507 locus *l1, *l2, *loc;
508 const char *format;
510 loc = l1 = l2 = NULL;
512 have_l1 = 0;
513 pos = -1;
514 maxpos = -1;
516 n = 0;
517 format = format0;
519 for (i = 0; i < MAX_ARGS; i++)
521 arg[i].type = NOTYPE;
522 spec[i].pos = -1;
525 /* First parse the format string for position specifiers. */
526 while (*format)
528 c = *format++;
529 if (c != '%')
530 continue;
532 if (*format == '%')
534 format++;
535 continue;
538 if (ISDIGIT (*format))
540 /* This is a position specifier. For example, the number
541 12 in the format string "%12$d", which specifies the third
542 argument of the va_list, formatted in %d format.
543 For details, see "man 3 printf". */
544 pos = atoi(format) - 1;
545 gcc_assert (pos >= 0);
546 while (ISDIGIT(*format))
547 format++;
548 gcc_assert (*format == '$');
549 format++;
551 else
552 pos++;
554 c = *format++;
556 if (pos > maxpos)
557 maxpos = pos;
559 switch (c)
561 case 'C':
562 arg[pos].type = TYPE_CURRENTLOC;
563 break;
565 case 'L':
566 arg[pos].type = TYPE_LOCUS;
567 break;
569 case 'd':
570 case 'i':
571 arg[pos].type = TYPE_INTEGER;
572 break;
574 case 'u':
575 arg[pos].type = TYPE_UINTEGER;
576 break;
578 case 'l':
579 c = *format++;
580 if (c == 'u')
581 arg[pos].type = TYPE_ULONGINT;
582 else if (c == 'i' || c == 'd')
583 arg[pos].type = TYPE_LONGINT;
584 else
585 gcc_unreachable ();
586 break;
588 case 'c':
589 arg[pos].type = TYPE_CHAR;
590 break;
592 case 's':
593 arg[pos].type = TYPE_STRING;
594 break;
596 default:
597 gcc_unreachable ();
600 spec[n++].pos = pos;
603 /* Then convert the values for each %-style argument. */
604 for (pos = 0; pos <= maxpos; pos++)
606 gcc_assert (arg[pos].type != NOTYPE);
607 switch (arg[pos].type)
609 case TYPE_CURRENTLOC:
610 loc = &gfc_current_locus;
611 /* Fall through. */
613 case TYPE_LOCUS:
614 if (arg[pos].type == TYPE_LOCUS)
615 loc = va_arg (argp, locus *);
617 if (have_l1)
619 l2 = loc;
620 arg[pos].u.stringval = "(2)";
622 else
624 l1 = loc;
625 have_l1 = 1;
626 arg[pos].u.stringval = "(1)";
628 break;
630 case TYPE_INTEGER:
631 arg[pos].u.intval = va_arg (argp, int);
632 break;
634 case TYPE_UINTEGER:
635 arg[pos].u.uintval = va_arg (argp, unsigned int);
636 break;
638 case TYPE_LONGINT:
639 arg[pos].u.longintval = va_arg (argp, long int);
640 break;
642 case TYPE_ULONGINT:
643 arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
644 break;
646 case TYPE_CHAR:
647 arg[pos].u.charval = (char) va_arg (argp, int);
648 break;
650 case TYPE_STRING:
651 arg[pos].u.stringval = (const char *) va_arg (argp, char *);
652 break;
654 default:
655 gcc_unreachable ();
659 for (n = 0; spec[n].pos >= 0; n++)
660 spec[n].u = arg[spec[n].pos].u;
662 /* Show the current loci if we have to. */
663 if (have_l1)
664 show_loci (l1, l2);
666 if (*type)
668 error_string (type);
669 error_char (' ');
672 have_l1 = 0;
673 format = format0;
674 n = 0;
676 for (; *format; format++)
678 if (*format != '%')
680 error_char (*format);
681 continue;
684 format++;
685 if (ISDIGIT (*format))
687 /* This is a position specifier. See comment above. */
688 while (ISDIGIT (*format))
689 format++;
691 /* Skip over the dollar sign. */
692 format++;
695 switch (*format)
697 case '%':
698 error_char ('%');
699 break;
701 case 'c':
702 error_char (spec[n++].u.charval);
703 break;
705 case 's':
706 case 'C': /* Current locus */
707 case 'L': /* Specified locus */
708 error_string (spec[n++].u.stringval);
709 break;
711 case 'd':
712 case 'i':
713 error_integer (spec[n++].u.intval);
714 break;
716 case 'u':
717 error_uinteger (spec[n++].u.uintval);
718 break;
720 case 'l':
721 format++;
722 if (*format == 'u')
723 error_uinteger (spec[n++].u.ulongintval);
724 else
725 error_integer (spec[n++].u.longintval);
726 break;
731 error_char ('\n');
735 /* Wrapper for error_print(). */
737 static void
738 error_printf (const char *gmsgid, ...)
740 va_list argp;
742 va_start (argp, gmsgid);
743 error_print ("", _(gmsgid), argp);
744 va_end (argp);
748 /* Clear any output buffered in a pretty-print output_buffer. */
750 static void
751 gfc_clear_pp_buffer (output_buffer *this_buffer)
753 pretty_printer *pp = global_dc->printer;
754 output_buffer *tmp_buffer = pp->buffer;
755 pp->buffer = this_buffer;
756 pp_clear_output_area (pp);
757 pp->buffer = tmp_buffer;
758 /* We need to reset last_location, otherwise we may skip caret lines
759 when we actually give a diagnostic. */
760 global_dc->last_location = UNKNOWN_LOCATION;
764 /* This is just a helper function to avoid duplicating the logic of
765 gfc_warning. */
767 static bool
768 gfc_warning (int opt, const char *gmsgid, va_list ap)
770 va_list argp;
771 va_copy (argp, ap);
773 diagnostic_info diagnostic;
774 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
775 bool fatal_errors = global_dc->fatal_errors;
776 pretty_printer *pp = global_dc->printer;
777 output_buffer *tmp_buffer = pp->buffer;
779 gfc_clear_pp_buffer (pp_warning_buffer);
781 if (buffered_p)
783 pp->buffer = pp_warning_buffer;
784 global_dc->fatal_errors = false;
785 /* To prevent -fmax-errors= triggering. */
786 --werrorcount;
789 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc,
790 DK_WARNING);
791 diagnostic.option_index = opt;
792 bool ret = report_diagnostic (&diagnostic);
794 if (buffered_p)
796 pp->buffer = tmp_buffer;
797 global_dc->fatal_errors = fatal_errors;
799 warningcount_buffered = 0;
800 werrorcount_buffered = 0;
801 /* Undo the above --werrorcount if not Werror, otherwise
802 werrorcount is correct already. */
803 if (!ret)
804 ++werrorcount;
805 else if (diagnostic.kind == DK_ERROR)
806 ++werrorcount_buffered;
807 else
808 ++werrorcount, --warningcount, ++warningcount_buffered;
811 va_end (argp);
812 return ret;
815 /* Issue a warning. */
817 bool
818 gfc_warning (int opt, const char *gmsgid, ...)
820 va_list argp;
822 va_start (argp, gmsgid);
823 bool ret = gfc_warning (opt, gmsgid, argp);
824 va_end (argp);
825 return ret;
829 /* Whether, for a feature included in a given standard set (GFC_STD_*),
830 we should issue an error or a warning, or be quiet. */
832 notification
833 gfc_notification_std (int std)
835 bool warning;
837 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
838 if ((gfc_option.allow_std & std) != 0 && !warning)
839 return SILENT;
841 return warning ? WARNING : ERROR;
845 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
846 feature. An error/warning will be issued if the currently selected
847 standard does not contain the requested bits. Return false if
848 an error is generated. */
850 bool
851 gfc_notify_std (int std, const char *gmsgid, ...)
853 va_list argp;
854 bool warning;
855 const char *msg, *msg2;
856 char *buffer;
858 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
859 if ((gfc_option.allow_std & std) != 0 && !warning)
860 return true;
862 if (suppress_errors)
863 return warning ? true : false;
865 switch (std)
867 case GFC_STD_F2008_TS:
868 msg = "TS 29113/TS 18508:";
869 break;
870 case GFC_STD_F2008_OBS:
871 msg = _("Fortran 2008 obsolescent feature:");
872 break;
873 case GFC_STD_F2008:
874 msg = "Fortran 2008:";
875 break;
876 case GFC_STD_F2003:
877 msg = "Fortran 2003:";
878 break;
879 case GFC_STD_GNU:
880 msg = _("GNU Extension:");
881 break;
882 case GFC_STD_LEGACY:
883 msg = _("Legacy Extension:");
884 break;
885 case GFC_STD_F95_OBS:
886 msg = _("Obsolescent feature:");
887 break;
888 case GFC_STD_F95_DEL:
889 msg = _("Deleted feature:");
890 break;
891 default:
892 gcc_unreachable ();
895 msg2 = _(gmsgid);
896 buffer = (char *) alloca (strlen (msg) + strlen (msg2) + 2);
897 strcpy (buffer, msg);
898 strcat (buffer, " ");
899 strcat (buffer, msg2);
901 va_start (argp, gmsgid);
902 if (warning)
903 gfc_warning (0, buffer, argp);
904 else
905 gfc_error (buffer, argp);
906 va_end (argp);
908 return (warning && !warnings_are_errors) ? true : false;
912 /* Called from output_format -- during diagnostic message processing
913 to handle Fortran specific format specifiers with the following meanings:
915 %C Current locus (no argument)
916 %L Takes locus argument
918 static bool
919 gfc_format_decoder (pretty_printer *pp,
920 text_info *text, const char *spec,
921 int precision ATTRIBUTE_UNUSED, bool wide ATTRIBUTE_UNUSED,
922 bool plus ATTRIBUTE_UNUSED, bool hash ATTRIBUTE_UNUSED)
924 switch (*spec)
926 case 'C':
927 case 'L':
929 static const char *result[2] = { "(1)", "(2)" };
930 locus *loc;
931 if (*spec == 'C')
932 loc = &gfc_current_locus;
933 else
934 loc = va_arg (*text->args_ptr, locus *);
935 gcc_assert (loc->nextc - loc->lb->line >= 0);
936 unsigned int offset = loc->nextc - loc->lb->line;
937 /* If location[0] != UNKNOWN_LOCATION means that we already
938 processed one of %C/%L. */
939 int loc_num = text->get_location (0) == UNKNOWN_LOCATION ? 0 : 1;
940 location_t src_loc
941 = linemap_position_for_loc_and_offset (line_table,
942 loc->lb->location,
943 offset);
944 text->set_location (loc_num, src_loc, true);
945 pp_string (pp, result[loc_num]);
946 return true;
948 default:
949 return false;
953 /* Return a malloc'd string describing the kind of diagnostic. The
954 caller is responsible for freeing the memory. */
955 static char *
956 gfc_diagnostic_build_kind_prefix (diagnostic_context *context,
957 const diagnostic_info *diagnostic)
959 static const char *const diagnostic_kind_text[] = {
960 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
961 #include "gfc-diagnostic.def"
962 #undef DEFINE_DIAGNOSTIC_KIND
963 "must-not-happen"
965 static const char *const diagnostic_kind_color[] = {
966 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
967 #include "gfc-diagnostic.def"
968 #undef DEFINE_DIAGNOSTIC_KIND
969 NULL
971 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
972 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
973 const char *text_cs = "", *text_ce = "";
974 pretty_printer *pp = context->printer;
976 if (diagnostic_kind_color[diagnostic->kind])
978 text_cs = colorize_start (pp_show_color (pp),
979 diagnostic_kind_color[diagnostic->kind]);
980 text_ce = colorize_stop (pp_show_color (pp));
982 return build_message_string ("%s%s:%s ", text_cs, text, text_ce);
985 /* Return a malloc'd string describing a location. The caller is
986 responsible for freeing the memory. */
987 static char *
988 gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
989 expanded_location s)
991 pretty_printer *pp = context->printer;
992 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
993 const char *locus_ce = colorize_stop (pp_show_color (pp));
994 return (s.file == NULL
995 ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
996 : !strcmp (s.file, N_("<built-in>"))
997 ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
998 : context->show_column
999 ? build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
1000 s.column, locus_ce)
1001 : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line, locus_ce));
1004 /* Return a malloc'd string describing two locations. The caller is
1005 responsible for freeing the memory. */
1006 static char *
1007 gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
1008 expanded_location s, expanded_location s2)
1010 pretty_printer *pp = context->printer;
1011 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
1012 const char *locus_ce = colorize_stop (pp_show_color (pp));
1014 return (s.file == NULL
1015 ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
1016 : !strcmp (s.file, N_("<built-in>"))
1017 ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
1018 : context->show_column
1019 ? build_message_string ("%s%s:%d:%d-%d:%s", locus_cs, s.file, s.line,
1020 MIN (s.column, s2.column),
1021 MAX (s.column, s2.column), locus_ce)
1022 : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
1023 locus_ce));
1026 /* This function prints the locus (file:line:column), the diagnostic kind
1027 (Error, Warning) and (optionally) the relevant lines of code with
1028 annotation lines with '1' and/or '2' below them.
1030 With -fdiagnostic-show-caret (the default) it prints:
1032 [locus of primary range]:
1034 some code
1036 Error: Some error at (1)
1038 With -fno-diagnostic-show-caret or if the primary range is not
1039 valid, it prints:
1041 [locus of primary range]: Error: Some error at (1) and (2)
1043 static void
1044 gfc_diagnostic_starter (diagnostic_context *context,
1045 diagnostic_info *diagnostic)
1047 char * kind_prefix = gfc_diagnostic_build_kind_prefix (context, diagnostic);
1049 expanded_location s1 = diagnostic_expand_location (diagnostic);
1050 expanded_location s2;
1051 bool one_locus = diagnostic->richloc->get_num_locations () < 2;
1052 bool same_locus = false;
1054 if (!one_locus)
1056 s2 = diagnostic_expand_location (diagnostic, 1);
1057 same_locus = diagnostic_same_line (context, s1, s2);
1060 char * locus_prefix = (one_locus || !same_locus)
1061 ? gfc_diagnostic_build_locus_prefix (context, s1)
1062 : gfc_diagnostic_build_locus_prefix (context, s1, s2);
1064 if (!context->show_caret
1065 || diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION
1066 || diagnostic_location (diagnostic, 0) == context->last_location)
1068 pp_set_prefix (context->printer,
1069 concat (locus_prefix, " ", kind_prefix, NULL));
1070 free (locus_prefix);
1072 if (one_locus || same_locus)
1074 free (kind_prefix);
1075 return;
1077 /* In this case, we print the previous locus and prefix as:
1079 [locus]:[prefix]: (1)
1081 and we flush with a new line before setting the new prefix. */
1082 pp_string (context->printer, "(1)");
1083 pp_newline (context->printer);
1084 locus_prefix = gfc_diagnostic_build_locus_prefix (context, s2);
1085 pp_set_prefix (context->printer,
1086 concat (locus_prefix, " ", kind_prefix, NULL));
1087 free (kind_prefix);
1088 free (locus_prefix);
1090 else
1092 pp_verbatim (context->printer, locus_prefix);
1093 free (locus_prefix);
1094 /* Fortran uses an empty line between locus and caret line. */
1095 pp_newline (context->printer);
1096 diagnostic_show_locus (context, diagnostic);
1097 /* If the caret line was shown, the prefix does not contain the
1098 locus. */
1099 pp_set_prefix (context->printer, kind_prefix);
1103 static void
1104 gfc_diagnostic_start_span (diagnostic_context *context,
1105 expanded_location exploc)
1107 char *locus_prefix;
1108 locus_prefix = gfc_diagnostic_build_locus_prefix (context, exploc);
1109 pp_verbatim (context->printer, locus_prefix);
1110 free (locus_prefix);
1111 pp_newline (context->printer);
1112 /* Fortran uses an empty line between locus and caret line. */
1113 pp_newline (context->printer);
1117 static void
1118 gfc_diagnostic_finalizer (diagnostic_context *context,
1119 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
1121 pp_destroy_prefix (context->printer);
1122 pp_newline_and_flush (context->printer);
1125 /* Immediate warning (i.e. do not buffer the warning) with an explicit
1126 location. */
1128 bool
1129 gfc_warning_now_at (location_t loc, int opt, const char *gmsgid, ...)
1131 va_list argp;
1132 diagnostic_info diagnostic;
1133 rich_location rich_loc (line_table, loc);
1134 bool ret;
1136 va_start (argp, gmsgid);
1137 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_WARNING);
1138 diagnostic.option_index = opt;
1139 ret = report_diagnostic (&diagnostic);
1140 va_end (argp);
1141 return ret;
1144 /* Immediate warning (i.e. do not buffer the warning). */
1146 bool
1147 gfc_warning_now (int opt, const char *gmsgid, ...)
1149 va_list argp;
1150 diagnostic_info diagnostic;
1151 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1152 bool ret;
1154 va_start (argp, gmsgid);
1155 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc,
1156 DK_WARNING);
1157 diagnostic.option_index = opt;
1158 ret = report_diagnostic (&diagnostic);
1159 va_end (argp);
1160 return ret;
1164 /* Immediate error (i.e. do not buffer). */
1166 void
1167 gfc_error_now (const char *gmsgid, ...)
1169 va_list argp;
1170 diagnostic_info diagnostic;
1171 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1173 error_buffer.flag = true;
1175 va_start (argp, gmsgid);
1176 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_ERROR);
1177 report_diagnostic (&diagnostic);
1178 va_end (argp);
1182 /* Fatal error, never returns. */
1184 void
1185 gfc_fatal_error (const char *gmsgid, ...)
1187 va_list argp;
1188 diagnostic_info diagnostic;
1189 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1191 va_start (argp, gmsgid);
1192 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_FATAL);
1193 report_diagnostic (&diagnostic);
1194 va_end (argp);
1196 gcc_unreachable ();
1199 /* Clear the warning flag. */
1201 void
1202 gfc_clear_warning (void)
1204 gfc_clear_pp_buffer (pp_warning_buffer);
1205 warningcount_buffered = 0;
1206 werrorcount_buffered = 0;
1210 /* Check to see if any warnings have been saved.
1211 If so, print the warning. */
1213 void
1214 gfc_warning_check (void)
1216 if (! gfc_output_buffer_empty_p (pp_warning_buffer))
1218 pretty_printer *pp = global_dc->printer;
1219 output_buffer *tmp_buffer = pp->buffer;
1220 pp->buffer = pp_warning_buffer;
1221 pp_really_flush (pp);
1222 warningcount += warningcount_buffered;
1223 werrorcount += werrorcount_buffered;
1224 gcc_assert (warningcount_buffered + werrorcount_buffered == 1);
1225 pp->buffer = tmp_buffer;
1226 diagnostic_action_after_output (global_dc,
1227 warningcount_buffered
1228 ? DK_WARNING : DK_ERROR);
1233 /* Issue an error. */
1235 static void
1236 gfc_error (const char *gmsgid, va_list ap)
1238 va_list argp;
1239 va_copy (argp, ap);
1240 bool saved_abort_on_error = false;
1242 if (warnings_not_errors)
1244 gfc_warning (/*opt=*/0, gmsgid, argp);
1245 va_end (argp);
1246 return;
1249 if (suppress_errors)
1251 va_end (argp);
1252 return;
1255 diagnostic_info diagnostic;
1256 rich_location richloc (line_table, UNKNOWN_LOCATION);
1257 bool fatal_errors = global_dc->fatal_errors;
1258 pretty_printer *pp = global_dc->printer;
1259 output_buffer *tmp_buffer = pp->buffer;
1261 gfc_clear_pp_buffer (pp_error_buffer);
1263 if (buffered_p)
1265 /* To prevent -dH from triggering an abort on a buffered error,
1266 save abort_on_error and restore it below. */
1267 saved_abort_on_error = global_dc->abort_on_error;
1268 global_dc->abort_on_error = false;
1269 pp->buffer = pp_error_buffer;
1270 global_dc->fatal_errors = false;
1271 /* To prevent -fmax-errors= triggering, we decrease it before
1272 report_diagnostic increases it. */
1273 --errorcount;
1276 diagnostic_set_info (&diagnostic, gmsgid, &argp, &richloc, DK_ERROR);
1277 report_diagnostic (&diagnostic);
1279 if (buffered_p)
1281 pp->buffer = tmp_buffer;
1282 global_dc->fatal_errors = fatal_errors;
1283 global_dc->abort_on_error = saved_abort_on_error;
1287 va_end (argp);
1291 void
1292 gfc_error (const char *gmsgid, ...)
1294 va_list argp;
1295 va_start (argp, gmsgid);
1296 gfc_error (gmsgid, argp);
1297 va_end (argp);
1301 /* This shouldn't happen... but sometimes does. */
1303 void
1304 gfc_internal_error (const char *gmsgid, ...)
1306 int e, w;
1307 va_list argp;
1308 diagnostic_info diagnostic;
1309 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1311 gfc_get_errors (&w, &e);
1312 if (e > 0)
1313 exit(EXIT_FAILURE);
1315 va_start (argp, gmsgid);
1316 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_ICE);
1317 report_diagnostic (&diagnostic);
1318 va_end (argp);
1320 gcc_unreachable ();
1324 /* Clear the error flag when we start to compile a source line. */
1326 void
1327 gfc_clear_error (void)
1329 error_buffer.flag = 0;
1330 warnings_not_errors = false;
1331 gfc_clear_pp_buffer (pp_error_buffer);
1335 /* Tests the state of error_flag. */
1337 bool
1338 gfc_error_flag_test (void)
1340 return error_buffer.flag
1341 || !gfc_output_buffer_empty_p (pp_error_buffer);
1345 /* Check to see if any errors have been saved.
1346 If so, print the error. Returns the state of error_flag. */
1348 bool
1349 gfc_error_check (void)
1351 if (error_buffer.flag
1352 || ! gfc_output_buffer_empty_p (pp_error_buffer))
1354 error_buffer.flag = false;
1355 pretty_printer *pp = global_dc->printer;
1356 output_buffer *tmp_buffer = pp->buffer;
1357 pp->buffer = pp_error_buffer;
1358 pp_really_flush (pp);
1359 ++errorcount;
1360 gcc_assert (gfc_output_buffer_empty_p (pp_error_buffer));
1361 pp->buffer = tmp_buffer;
1362 diagnostic_action_after_output (global_dc, DK_ERROR);
1363 return true;
1366 return false;
1369 /* Move the text buffered from FROM to TO, then clear
1370 FROM. Independently if there was text in FROM, TO is also
1371 cleared. */
1373 static void
1374 gfc_move_error_buffer_from_to (gfc_error_buffer * buffer_from,
1375 gfc_error_buffer * buffer_to)
1377 output_buffer * from = &(buffer_from->buffer);
1378 output_buffer * to = &(buffer_to->buffer);
1380 buffer_to->flag = buffer_from->flag;
1381 buffer_from->flag = false;
1383 gfc_clear_pp_buffer (to);
1384 /* We make sure this is always buffered. */
1385 to->flush_p = false;
1387 if (! gfc_output_buffer_empty_p (from))
1389 const char *str = output_buffer_formatted_text (from);
1390 output_buffer_append_r (to, str, strlen (str));
1391 gfc_clear_pp_buffer (from);
1395 /* Save the existing error state. */
1397 void
1398 gfc_push_error (gfc_error_buffer *err)
1400 gfc_move_error_buffer_from_to (&error_buffer, err);
1404 /* Restore a previous pushed error state. */
1406 void
1407 gfc_pop_error (gfc_error_buffer *err)
1409 gfc_move_error_buffer_from_to (err, &error_buffer);
1413 /* Free a pushed error state, but keep the current error state. */
1415 void
1416 gfc_free_error (gfc_error_buffer *err)
1418 gfc_clear_pp_buffer (&(err->buffer));
1422 /* Report the number of warnings and errors that occurred to the caller. */
1424 void
1425 gfc_get_errors (int *w, int *e)
1427 if (w != NULL)
1428 *w = warningcount + werrorcount;
1429 if (e != NULL)
1430 *e = errorcount + sorrycount + werrorcount;
1434 /* Switch errors into warnings. */
1436 void
1437 gfc_errors_to_warnings (bool f)
1439 warnings_not_errors = f;
1442 void
1443 gfc_diagnostics_init (void)
1445 diagnostic_starter (global_dc) = gfc_diagnostic_starter;
1446 global_dc->start_span = gfc_diagnostic_start_span;
1447 diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
1448 diagnostic_format_decoder (global_dc) = gfc_format_decoder;
1449 global_dc->caret_chars[0] = '1';
1450 global_dc->caret_chars[1] = '2';
1451 pp_warning_buffer = new (XNEW (output_buffer)) output_buffer ();
1452 pp_warning_buffer->flush_p = false;
1453 /* pp_error_buffer is statically allocated. This simplifies memory
1454 management when using gfc_push/pop_error. */
1455 pp_error_buffer = &(error_buffer.buffer);
1456 pp_error_buffer->flush_p = false;
1459 void
1460 gfc_diagnostics_finish (void)
1462 tree_diagnostics_defaults (global_dc);
1463 /* We still want to use the gfc starter and finalizer, not the tree
1464 defaults. */
1465 diagnostic_starter (global_dc) = gfc_diagnostic_starter;
1466 diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
1467 global_dc->caret_chars[0] = '^';
1468 global_dc->caret_chars[1] = '^';