Skip several gcc.dg/builtin-dynamic-object-size tests on hppa*-*-hpux*
[official-gcc.git] / gcc / fortran / error.cc
blob9b9018581d59c1a69627451ac094942c58fc40b5
1 /* Handle errors.
2 Copyright (C) 2000-2024 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 buffered_p. */
53 bool
54 gfc_buffered_p (void)
56 return buffered_p;
59 /* Return true if there output_buffer is empty. */
61 static bool
62 gfc_output_buffer_empty_p (const output_buffer * buf)
64 return output_buffer_last_position_in_text (buf) == NULL;
67 /* Go one level deeper suppressing errors. */
69 void
70 gfc_push_suppress_errors (void)
72 gcc_assert (suppress_errors >= 0);
73 ++suppress_errors;
76 static void
77 gfc_error_opt (int opt, const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(2,0);
79 static bool
80 gfc_warning (int opt, const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(2,0);
83 /* Leave one level of error suppressing. */
85 void
86 gfc_pop_suppress_errors (void)
88 gcc_assert (suppress_errors > 0);
89 --suppress_errors;
93 /* Query whether errors are suppressed. */
95 bool
96 gfc_query_suppress_errors (void)
98 return suppress_errors > 0;
102 /* Determine terminal width (for trimming source lines in output). */
104 static int
105 gfc_get_terminal_width (void)
107 return isatty (STDERR_FILENO) ? get_terminal_width () : INT_MAX;
111 /* Per-file error initialization. */
113 void
114 gfc_error_init_1 (void)
116 terminal_width = gfc_get_terminal_width ();
117 gfc_buffer_error (false);
121 /* Set the flag for buffering errors or not. */
123 void
124 gfc_buffer_error (bool flag)
126 buffered_p = flag;
130 /* Add a single character to the error buffer or output depending on
131 buffered_p. */
133 static void
134 error_char (char)
136 /* FIXME: Unused function to be removed in a subsequent patch. */
140 /* Copy a string to wherever it needs to go. */
142 static void
143 error_string (const char *p)
145 while (*p)
146 error_char (*p++);
150 /* Print a formatted integer to the error buffer or output. */
152 #define IBUF_LEN 60
154 static void
155 error_uinteger (unsigned long long int i)
157 char *p, int_buf[IBUF_LEN];
159 p = int_buf + IBUF_LEN - 1;
160 *p-- = '\0';
162 if (i == 0)
163 *p-- = '0';
165 while (i > 0)
167 *p-- = i % 10 + '0';
168 i = i / 10;
171 error_string (p + 1);
174 static void
175 error_integer (long long int i)
177 unsigned long long int u;
179 if (i < 0)
181 u = (unsigned long long int) -i;
182 error_char ('-');
184 else
185 u = i;
187 error_uinteger (u);
191 static void
192 error_hwuint (unsigned HOST_WIDE_INT i)
194 char *p, int_buf[IBUF_LEN];
196 p = int_buf + IBUF_LEN - 1;
197 *p-- = '\0';
199 if (i == 0)
200 *p-- = '0';
202 while (i > 0)
204 *p-- = i % 10 + '0';
205 i = i / 10;
208 error_string (p + 1);
211 static void
212 error_hwint (HOST_WIDE_INT i)
214 unsigned HOST_WIDE_INT u;
216 if (i < 0)
218 u = (unsigned HOST_WIDE_INT) -i;
219 error_char ('-');
221 else
222 u = i;
224 error_uinteger (u);
228 static size_t
229 gfc_widechar_display_length (gfc_char_t c)
231 if (gfc_wide_is_printable (c) || c == '\t')
232 /* Printable ASCII character, or tabulation (output as a space). */
233 return 1;
234 else if (c < ((gfc_char_t) 1 << 8))
235 /* Displayed as \x?? */
236 return 4;
237 else if (c < ((gfc_char_t) 1 << 16))
238 /* Displayed as \u???? */
239 return 6;
240 else
241 /* Displayed as \U???????? */
242 return 10;
246 /* Length of the ASCII representation of the wide string, escaping wide
247 characters as print_wide_char_into_buffer() does. */
249 static size_t
250 gfc_wide_display_length (const gfc_char_t *str)
252 size_t i, len;
254 for (i = 0, len = 0; str[i]; i++)
255 len += gfc_widechar_display_length (str[i]);
257 return len;
260 static int
261 print_wide_char_into_buffer (gfc_char_t c, char *buf)
263 static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6',
264 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
266 if (gfc_wide_is_printable (c) || c == '\t')
268 buf[1] = '\0';
269 /* Tabulation is output as a space. */
270 buf[0] = (unsigned char) (c == '\t' ? ' ' : c);
271 return 1;
273 else if (c < ((gfc_char_t) 1 << 8))
275 buf[4] = '\0';
276 buf[3] = xdigit[c & 0x0F];
277 c = c >> 4;
278 buf[2] = xdigit[c & 0x0F];
280 buf[1] = 'x';
281 buf[0] = '\\';
282 return 4;
284 else if (c < ((gfc_char_t) 1 << 16))
286 buf[6] = '\0';
287 buf[5] = xdigit[c & 0x0F];
288 c = c >> 4;
289 buf[4] = xdigit[c & 0x0F];
290 c = c >> 4;
291 buf[3] = xdigit[c & 0x0F];
292 c = c >> 4;
293 buf[2] = xdigit[c & 0x0F];
295 buf[1] = 'u';
296 buf[0] = '\\';
297 return 6;
299 else
301 buf[10] = '\0';
302 buf[9] = xdigit[c & 0x0F];
303 c = c >> 4;
304 buf[8] = xdigit[c & 0x0F];
305 c = c >> 4;
306 buf[7] = xdigit[c & 0x0F];
307 c = c >> 4;
308 buf[6] = xdigit[c & 0x0F];
309 c = c >> 4;
310 buf[5] = xdigit[c & 0x0F];
311 c = c >> 4;
312 buf[4] = xdigit[c & 0x0F];
313 c = c >> 4;
314 buf[3] = xdigit[c & 0x0F];
315 c = c >> 4;
316 buf[2] = xdigit[c & 0x0F];
318 buf[1] = 'U';
319 buf[0] = '\\';
320 return 10;
324 static char wide_char_print_buffer[11];
326 const char *
327 gfc_print_wide_char (gfc_char_t c)
329 print_wide_char_into_buffer (c, wide_char_print_buffer);
330 return wide_char_print_buffer;
334 /* Show the file, where it was included, and the source line, give a
335 locus. Calls error_printf() recursively, but the recursion is at
336 most one level deep. */
338 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
340 static void
341 show_locus (locus *loc, int c1, int c2)
343 gfc_linebuf *lb;
344 gfc_file *f;
345 gfc_char_t *p;
346 int i, offset, cmax;
348 /* TODO: Either limit the total length and number of included files
349 displayed or add buffering of arbitrary number of characters in
350 error messages. */
352 /* Write out the error header line, giving the source file and error
353 location (in GNU standard "[file]:[line].[column]:" format),
354 followed by an "included by" stack and a blank line. This header
355 format is matched by a testsuite parser defined in
356 lib/gfortran-dg.exp. */
358 lb = loc->lb;
359 f = lb->file;
361 error_string (f->filename);
362 error_char (':');
364 error_integer (LOCATION_LINE (lb->location));
366 if ((c1 > 0) || (c2 > 0))
367 error_char ('.');
369 if (c1 > 0)
370 error_integer (c1);
372 if ((c1 > 0) && (c2 > 0))
373 error_char ('-');
375 if (c2 > 0)
376 error_integer (c2);
378 error_char (':');
379 error_char ('\n');
381 for (;;)
383 i = f->inclusion_line;
385 f = f->up;
386 if (f == NULL) break;
388 error_printf (" Included at %s:%d:", f->filename, i);
391 error_char ('\n');
393 /* Calculate an appropriate horizontal offset of the source line in
394 order to get the error locus within the visible portion of the
395 line. Note that if the margin of 5 here is changed, the
396 corresponding margin of 10 in show_loci should be changed. */
398 offset = 0;
400 /* If the two loci would appear in the same column, we shift
401 '2' one column to the right, so as to print '12' rather than
402 just '1'. We do this here so it will be accounted for in the
403 margin calculations. */
405 if (c1 == c2)
406 c2 += 1;
408 cmax = (c1 < c2) ? c2 : c1;
409 if (cmax > terminal_width - 5)
410 offset = cmax - terminal_width + 5;
412 /* Show the line itself, taking care not to print more than what can
413 show up on the terminal. Tabs are converted to spaces, and
414 nonprintable characters are converted to a "\xNN" sequence. */
416 p = &(lb->line[offset]);
417 i = gfc_wide_display_length (p);
418 if (i > terminal_width)
419 i = terminal_width - 1;
421 while (i > 0)
423 static char buffer[11];
424 i -= print_wide_char_into_buffer (*p++, buffer);
425 error_string (buffer);
428 error_char ('\n');
430 /* Show the '1' and/or '2' corresponding to the column of the error
431 locus. Note that a value of -1 for c1 or c2 will simply cause
432 the relevant number not to be printed. */
434 c1 -= offset;
435 c2 -= offset;
436 cmax -= offset;
438 p = &(lb->line[offset]);
439 for (i = 0; i < cmax; i++)
441 int spaces, j;
442 spaces = gfc_widechar_display_length (*p++);
444 if (i == c1)
445 error_char ('1'), spaces--;
446 else if (i == c2)
447 error_char ('2'), spaces--;
449 for (j = 0; j < spaces; j++)
450 error_char (' ');
453 if (i == c1)
454 error_char ('1');
455 else if (i == c2)
456 error_char ('2');
458 error_char ('\n');
463 /* As part of printing an error, we show the source lines that caused
464 the problem. We show at least one, and possibly two loci; the two
465 loci may or may not be on the same source line. */
467 static void
468 show_loci (locus *l1, locus *l2)
470 int m, c1, c2;
472 if (l1 == NULL || l1->lb == NULL)
474 error_printf ("<During initialization>\n");
475 return;
478 /* While calculating parameters for printing the loci, we consider possible
479 reasons for printing one per line. If appropriate, print the loci
480 individually; otherwise we print them both on the same line. */
482 c1 = l1->nextc - l1->lb->line;
483 if (l2 == NULL)
485 show_locus (l1, c1, -1);
486 return;
489 c2 = l2->nextc - l2->lb->line;
491 if (c1 < c2)
492 m = c2 - c1;
493 else
494 m = c1 - c2;
496 /* Note that the margin value of 10 here needs to be less than the
497 margin of 5 used in the calculation of offset in show_locus. */
499 if (l1->lb != l2->lb || m > terminal_width - 10)
501 show_locus (l1, c1, -1);
502 show_locus (l2, -1, c2);
503 return;
506 show_locus (l1, c1, c2);
508 return;
512 /* Workhorse for the error printing subroutines. This subroutine is
513 inspired by g77's error handling and is similar to printf() with
514 the following %-codes:
516 %c Character, %d or %i Integer, %s String, %% Percent
517 %L Takes locus argument
518 %C Current locus (no argument)
520 If a locus pointer is given, the actual source line is printed out
521 and the column is indicated. Since we want the error message at
522 the bottom of any source file information, we must scan the
523 argument list twice -- once to determine whether the loci are
524 present and record this for printing, and once to print the error
525 message after and loci have been printed. A maximum of two locus
526 arguments are permitted.
528 This function is also called (recursively) by show_locus in the
529 case of included files; however, as show_locus does not resupply
530 any loci, the recursion is at most one level deep. */
532 #define MAX_ARGS 10
534 static void ATTRIBUTE_GCC_GFC(2,0)
535 error_print (const char *type, const char *format0, va_list argp)
537 enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
538 TYPE_LONGINT, TYPE_ULONGINT, TYPE_LLONGINT, TYPE_ULLONGINT,
539 TYPE_HWINT, TYPE_HWUINT, TYPE_CHAR, TYPE_STRING, NOTYPE };
540 struct
542 int type;
543 int pos;
544 union
546 int intval;
547 unsigned int uintval;
548 long int longintval;
549 unsigned long int ulongintval;
550 long long int llongintval;
551 unsigned long long int ullongintval;
552 HOST_WIDE_INT hwintval;
553 unsigned HOST_WIDE_INT hwuintval;
554 char charval;
555 const char * stringval;
556 } u;
557 } arg[MAX_ARGS], spec[MAX_ARGS];
558 /* spec is the array of specifiers, in the same order as they
559 appear in the format string. arg is the array of arguments,
560 in the same order as they appear in the va_list. */
562 char c;
563 int i, n, have_l1, pos, maxpos;
564 locus *l1, *l2, *loc;
565 const char *format;
567 loc = l1 = l2 = NULL;
569 have_l1 = 0;
570 pos = -1;
571 maxpos = -1;
573 n = 0;
574 format = format0;
576 for (i = 0; i < MAX_ARGS; i++)
578 arg[i].type = NOTYPE;
579 spec[i].pos = -1;
582 /* First parse the format string for position specifiers. */
583 while (*format)
585 c = *format++;
586 if (c != '%')
587 continue;
589 if (*format == '%')
591 format++;
592 continue;
595 if (ISDIGIT (*format))
597 /* This is a position specifier. For example, the number
598 12 in the format string "%12$d", which specifies the third
599 argument of the va_list, formatted in %d format.
600 For details, see "man 3 printf". */
601 pos = atoi(format) - 1;
602 gcc_assert (pos >= 0);
603 while (ISDIGIT(*format))
604 format++;
605 gcc_assert (*format == '$');
606 format++;
608 else
609 pos++;
611 c = *format++;
613 if (pos > maxpos)
614 maxpos = pos;
616 switch (c)
618 case 'C':
619 arg[pos].type = TYPE_CURRENTLOC;
620 break;
622 case 'L':
623 arg[pos].type = TYPE_LOCUS;
624 break;
626 case 'd':
627 case 'i':
628 arg[pos].type = TYPE_INTEGER;
629 break;
631 case 'u':
632 arg[pos].type = TYPE_UINTEGER;
633 break;
635 case 'l':
636 c = *format++;
637 if (c == 'l')
639 c = *format++;
640 if (c == 'u')
641 arg[pos].type = TYPE_ULLONGINT;
642 else if (c == 'i' || c == 'd')
643 arg[pos].type = TYPE_LLONGINT;
644 else
645 gcc_unreachable ();
647 else if (c == 'u')
648 arg[pos].type = TYPE_ULONGINT;
649 else if (c == 'i' || c == 'd')
650 arg[pos].type = TYPE_LONGINT;
651 else
652 gcc_unreachable ();
653 break;
655 case 'w':
656 c = *format++;
657 if (c == 'u')
658 arg[pos].type = TYPE_HWUINT;
659 else if (c == 'i' || c == 'd')
660 arg[pos].type = TYPE_HWINT;
661 else
662 gcc_unreachable ();
663 break;
665 case 'c':
666 arg[pos].type = TYPE_CHAR;
667 break;
669 case 's':
670 arg[pos].type = TYPE_STRING;
671 break;
673 default:
674 gcc_unreachable ();
677 spec[n++].pos = pos;
680 /* Then convert the values for each %-style argument. */
681 for (pos = 0; pos <= maxpos; pos++)
683 gcc_assert (arg[pos].type != NOTYPE);
684 switch (arg[pos].type)
686 case TYPE_CURRENTLOC:
687 loc = &gfc_current_locus;
688 /* Fall through. */
690 case TYPE_LOCUS:
691 if (arg[pos].type == TYPE_LOCUS)
692 loc = va_arg (argp, locus *);
694 if (have_l1)
696 l2 = loc;
697 arg[pos].u.stringval = "(2)";
698 /* Point %C first offending character not the last good one. */
699 if (arg[pos].type == TYPE_CURRENTLOC && *l2->nextc != '\0')
700 l2->nextc++;
702 else
704 l1 = loc;
705 have_l1 = 1;
706 arg[pos].u.stringval = "(1)";
707 /* Point %C first offending character not the last good one. */
708 if (arg[pos].type == TYPE_CURRENTLOC && *l1->nextc != '\0')
709 l1->nextc++;
711 break;
713 case TYPE_INTEGER:
714 arg[pos].u.intval = va_arg (argp, int);
715 break;
717 case TYPE_UINTEGER:
718 arg[pos].u.uintval = va_arg (argp, unsigned int);
719 break;
721 case TYPE_LONGINT:
722 arg[pos].u.longintval = va_arg (argp, long int);
723 break;
725 case TYPE_ULONGINT:
726 arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
727 break;
729 case TYPE_LLONGINT:
730 arg[pos].u.llongintval = va_arg (argp, long long int);
731 break;
733 case TYPE_ULLONGINT:
734 arg[pos].u.ullongintval = va_arg (argp, unsigned long long int);
735 break;
737 case TYPE_HWINT:
738 arg[pos].u.hwintval = va_arg (argp, HOST_WIDE_INT);
739 break;
741 case TYPE_HWUINT:
742 arg[pos].u.hwuintval = va_arg (argp, unsigned HOST_WIDE_INT);
743 break;
745 case TYPE_CHAR:
746 arg[pos].u.charval = (char) va_arg (argp, int);
747 break;
749 case TYPE_STRING:
750 arg[pos].u.stringval = (const char *) va_arg (argp, char *);
751 break;
753 default:
754 gcc_unreachable ();
758 for (n = 0; spec[n].pos >= 0; n++)
759 spec[n].u = arg[spec[n].pos].u;
761 /* Show the current loci if we have to. */
762 if (have_l1)
763 show_loci (l1, l2);
765 if (*type)
767 error_string (type);
768 error_char (' ');
771 have_l1 = 0;
772 format = format0;
773 n = 0;
775 for (; *format; format++)
777 if (*format != '%')
779 error_char (*format);
780 continue;
783 format++;
784 if (ISDIGIT (*format))
786 /* This is a position specifier. See comment above. */
787 while (ISDIGIT (*format))
788 format++;
790 /* Skip over the dollar sign. */
791 format++;
794 switch (*format)
796 case '%':
797 error_char ('%');
798 break;
800 case 'c':
801 error_char (spec[n++].u.charval);
802 break;
804 case 's':
805 case 'C': /* Current locus */
806 case 'L': /* Specified locus */
807 error_string (spec[n++].u.stringval);
808 break;
810 case 'd':
811 case 'i':
812 error_integer (spec[n++].u.intval);
813 break;
815 case 'u':
816 error_uinteger (spec[n++].u.uintval);
817 break;
819 case 'l':
820 format++;
821 if (*format == 'l')
823 format++;
824 if (*format == 'u')
825 error_uinteger (spec[n++].u.ullongintval);
826 else
827 error_integer (spec[n++].u.llongintval);
829 if (*format == 'u')
830 error_uinteger (spec[n++].u.ulongintval);
831 else
832 error_integer (spec[n++].u.longintval);
833 break;
835 case 'w':
836 format++;
837 if (*format == 'u')
838 error_hwuint (spec[n++].u.hwintval);
839 else
840 error_hwint (spec[n++].u.hwuintval);
841 break;
845 error_char ('\n');
849 /* Wrapper for error_print(). */
851 static void
852 error_printf (const char *gmsgid, ...)
854 va_list argp;
856 va_start (argp, gmsgid);
857 error_print ("", _(gmsgid), argp);
858 va_end (argp);
862 /* Clear any output buffered in a pretty-print output_buffer. */
864 static void
865 gfc_clear_pp_buffer (output_buffer *this_buffer)
867 pretty_printer *pp = global_dc->printer;
868 output_buffer *tmp_buffer = pp->buffer;
869 pp->buffer = this_buffer;
870 pp_clear_output_area (pp);
871 pp->buffer = tmp_buffer;
872 /* We need to reset last_location, otherwise we may skip caret lines
873 when we actually give a diagnostic. */
874 global_dc->m_last_location = UNKNOWN_LOCATION;
877 /* The currently-printing diagnostic, for use by gfc_format_decoder,
878 for colorizing %C and %L. */
880 static diagnostic_info *curr_diagnostic;
882 /* A helper function to call diagnostic_report_diagnostic, while setting
883 curr_diagnostic for the duration of the call. */
885 static bool
886 gfc_report_diagnostic (diagnostic_info *diagnostic)
888 gcc_assert (diagnostic != NULL);
889 curr_diagnostic = diagnostic;
890 bool ret = diagnostic_report_diagnostic (global_dc, diagnostic);
891 curr_diagnostic = NULL;
892 return ret;
895 /* This is just a helper function to avoid duplicating the logic of
896 gfc_warning. */
898 static bool
899 gfc_warning (int opt, const char *gmsgid, va_list ap)
901 va_list argp;
902 va_copy (argp, ap);
904 diagnostic_info diagnostic;
905 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
906 bool fatal_errors = global_dc->m_fatal_errors;
907 pretty_printer *pp = global_dc->printer;
908 output_buffer *tmp_buffer = pp->buffer;
910 gfc_clear_pp_buffer (pp_warning_buffer);
912 if (buffered_p)
914 pp->buffer = pp_warning_buffer;
915 global_dc->m_fatal_errors = false;
916 /* To prevent -fmax-errors= triggering. */
917 --werrorcount;
920 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc,
921 DK_WARNING);
922 diagnostic.option_index = opt;
923 bool ret = gfc_report_diagnostic (&diagnostic);
925 if (buffered_p)
927 pp->buffer = tmp_buffer;
928 global_dc->m_fatal_errors = fatal_errors;
930 warningcount_buffered = 0;
931 werrorcount_buffered = 0;
932 /* Undo the above --werrorcount if not Werror, otherwise
933 werrorcount is correct already. */
934 if (!ret)
935 ++werrorcount;
936 else if (diagnostic.kind == DK_ERROR)
937 ++werrorcount_buffered;
938 else
939 ++werrorcount, --warningcount, ++warningcount_buffered;
942 va_end (argp);
943 return ret;
946 /* Issue a warning. */
948 bool
949 gfc_warning (int opt, const char *gmsgid, ...)
951 va_list argp;
953 va_start (argp, gmsgid);
954 bool ret = gfc_warning (opt, gmsgid, argp);
955 va_end (argp);
956 return ret;
960 /* Whether, for a feature included in a given standard set (GFC_STD_*),
961 we should issue an error or a warning, or be quiet. */
963 notification
964 gfc_notification_std (int std)
966 bool warning;
968 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
969 if ((gfc_option.allow_std & std) != 0 && !warning)
970 return SILENT;
972 return warning ? WARNING : ERROR;
976 /* Return a string describing the nature of a standard violation
977 * and/or the relevant version of the standard. */
979 char const*
980 notify_std_msg(int std)
983 if (std & GFC_STD_F2023_DEL)
984 return _("Prohibited in Fortran 2023:");
985 else if (std & GFC_STD_F2023)
986 return _("Fortran 2023:");
987 else if (std & GFC_STD_F2018_DEL)
988 return _("Fortran 2018 deleted feature:");
989 else if (std & GFC_STD_F2018_OBS)
990 return _("Fortran 2018 obsolescent feature:");
991 else if (std & GFC_STD_F2018)
992 return _("Fortran 2018:");
993 else if (std & GFC_STD_F2008_OBS)
994 return _("Fortran 2008 obsolescent feature:");
995 else if (std & GFC_STD_F2008)
996 return "Fortran 2008:";
997 else if (std & GFC_STD_F2003)
998 return "Fortran 2003:";
999 else if (std & GFC_STD_GNU)
1000 return _("GNU Extension:");
1001 else if (std & GFC_STD_LEGACY)
1002 return _("Legacy Extension:");
1003 else if (std & GFC_STD_F95_OBS)
1004 return _("Obsolescent feature:");
1005 else if (std & GFC_STD_F95_DEL)
1006 return _("Deleted feature:");
1007 else
1008 gcc_unreachable ();
1012 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
1013 feature. An error/warning will be issued if the currently selected
1014 standard does not contain the requested bits. Return false if
1015 an error is generated. */
1017 bool
1018 gfc_notify_std (int std, const char *gmsgid, ...)
1020 va_list argp;
1021 const char *msg, *msg2;
1022 char *buffer;
1024 /* Determine whether an error or a warning is needed. */
1025 const int wstd = std & gfc_option.warn_std; /* Standard to warn about. */
1026 const int estd = std & ~gfc_option.allow_std; /* Standard to error about. */
1027 const bool warning = (wstd != 0) && !inhibit_warnings;
1028 const bool error = (estd != 0);
1030 if (!error && !warning)
1031 return true;
1032 if (suppress_errors)
1033 return !error;
1035 if (error)
1036 msg = notify_std_msg (estd);
1037 else
1038 msg = notify_std_msg (wstd);
1040 msg2 = _(gmsgid);
1041 buffer = (char *) alloca (strlen (msg) + strlen (msg2) + 2);
1042 strcpy (buffer, msg);
1043 strcat (buffer, " ");
1044 strcat (buffer, msg2);
1046 va_start (argp, gmsgid);
1047 if (error)
1048 gfc_error_opt (0, buffer, argp);
1049 else
1050 gfc_warning (0, buffer, argp);
1051 va_end (argp);
1053 if (error)
1054 return false;
1055 else
1056 return (warning && !warnings_are_errors);
1060 /* Called from output_format -- during diagnostic message processing
1061 to handle Fortran specific format specifiers with the following meanings:
1063 %C Current locus (no argument)
1064 %L Takes locus argument
1066 static bool
1067 gfc_format_decoder (pretty_printer *pp, text_info *text, const char *spec,
1068 int precision, bool wide, bool set_locus, bool hash,
1069 bool *quoted, const char **buffer_ptr)
1071 switch (*spec)
1073 case 'C':
1074 case 'L':
1076 static const char *result[2] = { "(1)", "(2)" };
1077 locus *loc;
1078 if (*spec == 'C')
1079 loc = &gfc_current_locus;
1080 else
1081 loc = va_arg (*text->m_args_ptr, locus *);
1082 gcc_assert (loc->nextc - loc->lb->line >= 0);
1083 unsigned int offset = loc->nextc - loc->lb->line;
1084 if (*spec == 'C' && *loc->nextc != '\0')
1085 /* Point %C first offending character not the last good one. */
1086 offset++;
1087 /* If location[0] != UNKNOWN_LOCATION means that we already
1088 processed one of %C/%L. */
1089 int loc_num = text->get_location (0) == UNKNOWN_LOCATION ? 0 : 1;
1090 location_t src_loc
1091 = linemap_position_for_loc_and_offset (line_table,
1092 loc->lb->location,
1093 offset);
1094 text->set_location (loc_num, src_loc, SHOW_RANGE_WITH_CARET);
1095 /* Colorize the markers to match the color choices of
1096 diagnostic_show_locus (the initial location has a color given
1097 by the "kind" of the diagnostic, the secondary location has
1098 color "range1"). */
1099 gcc_assert (curr_diagnostic != NULL);
1100 const char *color
1101 = (loc_num
1102 ? "range1"
1103 : diagnostic_get_color_for_kind (curr_diagnostic->kind));
1104 pp_string (pp, colorize_start (pp_show_color (pp), color));
1105 pp_string (pp, result[loc_num]);
1106 pp_string (pp, colorize_stop (pp_show_color (pp)));
1107 return true;
1109 default:
1110 /* Fall through info the middle-end decoder, as e.g. stor-layout.cc
1111 etc. diagnostics can use the FE printer while the FE is still
1112 active. */
1113 return default_tree_printer (pp, text, spec, precision, wide,
1114 set_locus, hash, quoted, buffer_ptr);
1118 /* Return a malloc'd string describing the kind of diagnostic. The
1119 caller is responsible for freeing the memory. */
1120 static char *
1121 gfc_diagnostic_build_kind_prefix (diagnostic_context *context,
1122 const diagnostic_info *diagnostic)
1124 static const char *const diagnostic_kind_text[] = {
1125 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
1126 #include "gfc-diagnostic.def"
1127 #undef DEFINE_DIAGNOSTIC_KIND
1128 "must-not-happen"
1130 static const char *const diagnostic_kind_color[] = {
1131 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
1132 #include "gfc-diagnostic.def"
1133 #undef DEFINE_DIAGNOSTIC_KIND
1134 NULL
1136 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
1137 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
1138 const char *text_cs = "", *text_ce = "";
1139 pretty_printer *pp = context->printer;
1141 if (diagnostic_kind_color[diagnostic->kind])
1143 text_cs = colorize_start (pp_show_color (pp),
1144 diagnostic_kind_color[diagnostic->kind]);
1145 text_ce = colorize_stop (pp_show_color (pp));
1147 return build_message_string ("%s%s:%s ", text_cs, text, text_ce);
1150 /* Return a malloc'd string describing a location. The caller is
1151 responsible for freeing the memory. */
1152 static char *
1153 gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
1154 expanded_location s)
1156 pretty_printer *pp = context->printer;
1157 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
1158 const char *locus_ce = colorize_stop (pp_show_color (pp));
1159 return (s.file == NULL
1160 ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
1161 : !strcmp (s.file, special_fname_builtin ())
1162 ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
1163 : context->m_show_column
1164 ? build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
1165 s.column, locus_ce)
1166 : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line, locus_ce));
1169 /* Return a malloc'd string describing two locations. The caller is
1170 responsible for freeing the memory. */
1171 static char *
1172 gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
1173 expanded_location s, expanded_location s2)
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));
1179 return (s.file == NULL
1180 ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
1181 : !strcmp (s.file, special_fname_builtin ())
1182 ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
1183 : context->m_show_column
1184 ? build_message_string ("%s%s:%d:%d-%d:%s", locus_cs, s.file, s.line,
1185 MIN (s.column, s2.column),
1186 MAX (s.column, s2.column), locus_ce)
1187 : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
1188 locus_ce));
1191 /* This function prints the locus (file:line:column), the diagnostic kind
1192 (Error, Warning) and (optionally) the relevant lines of code with
1193 annotation lines with '1' and/or '2' below them.
1195 With -fdiagnostic-show-caret (the default) it prints:
1197 [locus of primary range]:
1199 some code
1201 Error: Some error at (1)
1203 With -fno-diagnostic-show-caret or if the primary range is not
1204 valid, it prints:
1206 [locus of primary range]: Error: Some error at (1) and (2)
1208 static void
1209 gfc_diagnostic_starter (diagnostic_context *context,
1210 const diagnostic_info *diagnostic)
1212 char * kind_prefix = gfc_diagnostic_build_kind_prefix (context, diagnostic);
1214 expanded_location s1 = diagnostic_expand_location (diagnostic);
1215 expanded_location s2;
1216 bool one_locus = diagnostic->richloc->get_num_locations () < 2;
1217 bool same_locus = false;
1219 if (!one_locus)
1221 s2 = diagnostic_expand_location (diagnostic, 1);
1222 same_locus = diagnostic_same_line (context, s1, s2);
1225 char * locus_prefix = (one_locus || !same_locus)
1226 ? gfc_diagnostic_build_locus_prefix (context, s1)
1227 : gfc_diagnostic_build_locus_prefix (context, s1, s2);
1229 if (!context->m_source_printing.enabled
1230 || diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION
1231 || diagnostic_location (diagnostic, 0) == context->m_last_location)
1233 pp_set_prefix (context->printer,
1234 concat (locus_prefix, " ", kind_prefix, NULL));
1235 free (locus_prefix);
1237 if (one_locus || same_locus)
1239 free (kind_prefix);
1240 return;
1242 /* In this case, we print the previous locus and prefix as:
1244 [locus]:[prefix]: (1)
1246 and we flush with a new line before setting the new prefix. */
1247 pp_string (context->printer, "(1)");
1248 pp_newline (context->printer);
1249 locus_prefix = gfc_diagnostic_build_locus_prefix (context, s2);
1250 pp_set_prefix (context->printer,
1251 concat (locus_prefix, " ", kind_prefix, NULL));
1252 free (kind_prefix);
1253 free (locus_prefix);
1255 else
1257 pp_verbatim (context->printer, "%s", locus_prefix);
1258 free (locus_prefix);
1259 /* Fortran uses an empty line between locus and caret line. */
1260 pp_newline (context->printer);
1261 pp_set_prefix (context->printer, NULL);
1262 pp_newline (context->printer);
1263 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
1264 /* If the caret line was shown, the prefix does not contain the
1265 locus. */
1266 pp_set_prefix (context->printer, kind_prefix);
1270 static void
1271 gfc_diagnostic_start_span (diagnostic_context *context,
1272 expanded_location exploc)
1274 char *locus_prefix;
1275 locus_prefix = gfc_diagnostic_build_locus_prefix (context, exploc);
1276 pp_verbatim (context->printer, "%s", locus_prefix);
1277 free (locus_prefix);
1278 pp_newline (context->printer);
1279 /* Fortran uses an empty line between locus and caret line. */
1280 pp_newline (context->printer);
1284 static void
1285 gfc_diagnostic_finalizer (diagnostic_context *context,
1286 const diagnostic_info *diagnostic ATTRIBUTE_UNUSED,
1287 diagnostic_t orig_diag_kind ATTRIBUTE_UNUSED)
1289 pp_destroy_prefix (context->printer);
1290 pp_newline_and_flush (context->printer);
1293 /* Immediate warning (i.e. do not buffer the warning) with an explicit
1294 location. */
1296 bool
1297 gfc_warning_now_at (location_t loc, int opt, const char *gmsgid, ...)
1299 va_list argp;
1300 diagnostic_info diagnostic;
1301 rich_location rich_loc (line_table, loc);
1302 bool ret;
1304 va_start (argp, gmsgid);
1305 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_WARNING);
1306 diagnostic.option_index = opt;
1307 ret = gfc_report_diagnostic (&diagnostic);
1308 va_end (argp);
1309 return ret;
1312 /* Immediate warning (i.e. do not buffer the warning). */
1314 bool
1315 gfc_warning_now (int opt, const char *gmsgid, ...)
1317 va_list argp;
1318 diagnostic_info diagnostic;
1319 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1320 bool ret;
1322 va_start (argp, gmsgid);
1323 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc,
1324 DK_WARNING);
1325 diagnostic.option_index = opt;
1326 ret = gfc_report_diagnostic (&diagnostic);
1327 va_end (argp);
1328 return ret;
1331 /* Internal warning, do not buffer. */
1333 bool
1334 gfc_warning_internal (int opt, const char *gmsgid, ...)
1336 va_list argp;
1337 diagnostic_info diagnostic;
1338 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1339 bool ret;
1341 va_start (argp, gmsgid);
1342 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc,
1343 DK_WARNING);
1344 diagnostic.option_index = opt;
1345 ret = gfc_report_diagnostic (&diagnostic);
1346 va_end (argp);
1347 return ret;
1350 /* Immediate error (i.e. do not buffer). */
1352 void
1353 gfc_error_now (const char *gmsgid, ...)
1355 va_list argp;
1356 diagnostic_info diagnostic;
1357 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1359 error_buffer.flag = true;
1361 va_start (argp, gmsgid);
1362 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_ERROR);
1363 gfc_report_diagnostic (&diagnostic);
1364 va_end (argp);
1368 /* Fatal error, never returns. */
1370 void
1371 gfc_fatal_error (const char *gmsgid, ...)
1373 va_list argp;
1374 diagnostic_info diagnostic;
1375 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1377 va_start (argp, gmsgid);
1378 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_FATAL);
1379 gfc_report_diagnostic (&diagnostic);
1380 va_end (argp);
1382 gcc_unreachable ();
1385 /* Clear the warning flag. */
1387 void
1388 gfc_clear_warning (void)
1390 gfc_clear_pp_buffer (pp_warning_buffer);
1391 warningcount_buffered = 0;
1392 werrorcount_buffered = 0;
1396 /* Check to see if any warnings have been saved.
1397 If so, print the warning. */
1399 void
1400 gfc_warning_check (void)
1402 if (! gfc_output_buffer_empty_p (pp_warning_buffer))
1404 pretty_printer *pp = global_dc->printer;
1405 output_buffer *tmp_buffer = pp->buffer;
1406 pp->buffer = pp_warning_buffer;
1407 pp_really_flush (pp);
1408 warningcount += warningcount_buffered;
1409 werrorcount += werrorcount_buffered;
1410 gcc_assert (warningcount_buffered + werrorcount_buffered == 1);
1411 pp->buffer = tmp_buffer;
1412 diagnostic_action_after_output (global_dc,
1413 warningcount_buffered
1414 ? DK_WARNING : DK_ERROR);
1415 diagnostic_check_max_errors (global_dc, true);
1420 /* Issue an error. */
1422 static void
1423 gfc_error_opt (int opt, const char *gmsgid, va_list ap)
1425 va_list argp;
1426 va_copy (argp, ap);
1427 bool saved_abort_on_error = false;
1429 if (warnings_not_errors)
1431 gfc_warning (opt, gmsgid, argp);
1432 va_end (argp);
1433 return;
1436 if (suppress_errors)
1438 va_end (argp);
1439 return;
1442 diagnostic_info diagnostic;
1443 rich_location richloc (line_table, UNKNOWN_LOCATION);
1444 bool fatal_errors = global_dc->m_fatal_errors;
1445 pretty_printer *pp = global_dc->printer;
1446 output_buffer *tmp_buffer = pp->buffer;
1448 gfc_clear_pp_buffer (pp_error_buffer);
1450 if (buffered_p)
1452 /* To prevent -dH from triggering an abort on a buffered error,
1453 save abort_on_error and restore it below. */
1454 saved_abort_on_error = global_dc->m_abort_on_error;
1455 global_dc->m_abort_on_error = false;
1456 pp->buffer = pp_error_buffer;
1457 global_dc->m_fatal_errors = false;
1458 /* To prevent -fmax-errors= triggering, we decrease it before
1459 report_diagnostic increases it. */
1460 --errorcount;
1463 diagnostic_set_info (&diagnostic, gmsgid, &argp, &richloc, DK_ERROR);
1464 gfc_report_diagnostic (&diagnostic);
1466 if (buffered_p)
1468 pp->buffer = tmp_buffer;
1469 global_dc->m_fatal_errors = fatal_errors;
1470 global_dc->m_abort_on_error = saved_abort_on_error;
1474 va_end (argp);
1478 void
1479 gfc_error_opt (int opt, const char *gmsgid, ...)
1481 va_list argp;
1482 va_start (argp, gmsgid);
1483 gfc_error_opt (opt, gmsgid, argp);
1484 va_end (argp);
1488 void
1489 gfc_error (const char *gmsgid, ...)
1491 va_list argp;
1492 va_start (argp, gmsgid);
1493 gfc_error_opt (0, gmsgid, argp);
1494 va_end (argp);
1498 /* This shouldn't happen... but sometimes does. */
1500 void
1501 gfc_internal_error (const char *gmsgid, ...)
1503 int e, w;
1504 va_list argp;
1505 diagnostic_info diagnostic;
1506 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1508 gfc_get_errors (&w, &e);
1509 if (e > 0)
1510 exit(EXIT_FAILURE);
1512 va_start (argp, gmsgid);
1513 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_ICE);
1514 gfc_report_diagnostic (&diagnostic);
1515 va_end (argp);
1517 gcc_unreachable ();
1521 /* Clear the error flag when we start to compile a source line. */
1523 void
1524 gfc_clear_error (void)
1526 error_buffer.flag = false;
1527 warnings_not_errors = false;
1528 gfc_clear_pp_buffer (pp_error_buffer);
1532 /* Tests the state of error_flag. */
1534 bool
1535 gfc_error_flag_test (void)
1537 return error_buffer.flag
1538 || !gfc_output_buffer_empty_p (pp_error_buffer);
1542 /* Check to see if any errors have been saved.
1543 If so, print the error. Returns the state of error_flag. */
1545 bool
1546 gfc_error_check (void)
1548 if (error_buffer.flag
1549 || ! gfc_output_buffer_empty_p (pp_error_buffer))
1551 error_buffer.flag = false;
1552 pretty_printer *pp = global_dc->printer;
1553 output_buffer *tmp_buffer = pp->buffer;
1554 pp->buffer = pp_error_buffer;
1555 pp_really_flush (pp);
1556 ++errorcount;
1557 gcc_assert (gfc_output_buffer_empty_p (pp_error_buffer));
1558 pp->buffer = tmp_buffer;
1559 diagnostic_action_after_output (global_dc, DK_ERROR);
1560 diagnostic_check_max_errors (global_dc, true);
1561 return true;
1564 return false;
1567 /* Move the text buffered from FROM to TO, then clear
1568 FROM. Independently if there was text in FROM, TO is also
1569 cleared. */
1571 static void
1572 gfc_move_error_buffer_from_to (gfc_error_buffer * buffer_from,
1573 gfc_error_buffer * buffer_to)
1575 output_buffer * from = &(buffer_from->buffer);
1576 output_buffer * to = &(buffer_to->buffer);
1578 buffer_to->flag = buffer_from->flag;
1579 buffer_from->flag = false;
1581 gfc_clear_pp_buffer (to);
1582 /* We make sure this is always buffered. */
1583 to->flush_p = false;
1585 if (! gfc_output_buffer_empty_p (from))
1587 const char *str = output_buffer_formatted_text (from);
1588 output_buffer_append_r (to, str, strlen (str));
1589 gfc_clear_pp_buffer (from);
1593 /* Save the existing error state. */
1595 void
1596 gfc_push_error (gfc_error_buffer *err)
1598 gfc_move_error_buffer_from_to (&error_buffer, err);
1602 /* Restore a previous pushed error state. */
1604 void
1605 gfc_pop_error (gfc_error_buffer *err)
1607 gfc_move_error_buffer_from_to (err, &error_buffer);
1611 /* Free a pushed error state, but keep the current error state. */
1613 void
1614 gfc_free_error (gfc_error_buffer *err)
1616 gfc_clear_pp_buffer (&(err->buffer));
1620 /* Report the number of warnings and errors that occurred to the caller. */
1622 void
1623 gfc_get_errors (int *w, int *e)
1625 if (w != NULL)
1626 *w = warningcount + werrorcount;
1627 if (e != NULL)
1628 *e = errorcount + sorrycount + werrorcount;
1632 /* Switch errors into warnings. */
1634 void
1635 gfc_errors_to_warnings (bool f)
1637 warnings_not_errors = f;
1640 void
1641 gfc_diagnostics_init (void)
1643 diagnostic_starter (global_dc) = gfc_diagnostic_starter;
1644 diagnostic_start_span (global_dc) = gfc_diagnostic_start_span;
1645 diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
1646 diagnostic_format_decoder (global_dc) = gfc_format_decoder;
1647 global_dc->m_source_printing.caret_chars[0] = '1';
1648 global_dc->m_source_printing.caret_chars[1] = '2';
1649 pp_warning_buffer = new (XNEW (output_buffer)) output_buffer ();
1650 pp_warning_buffer->flush_p = false;
1651 /* pp_error_buffer is statically allocated. This simplifies memory
1652 management when using gfc_push/pop_error. */
1653 pp_error_buffer = &(error_buffer.buffer);
1654 pp_error_buffer->flush_p = false;
1657 void
1658 gfc_diagnostics_finish (void)
1660 tree_diagnostics_defaults (global_dc);
1661 /* We still want to use the gfc starter and finalizer, not the tree
1662 defaults. */
1663 diagnostic_starter (global_dc) = gfc_diagnostic_starter;
1664 diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
1665 global_dc->m_source_printing.caret_chars[0] = '^';
1666 global_dc->m_source_printing.caret_chars[1] = '^';