1 /* Handle errors.
2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Niels Kristian Bech Jensen
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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. */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "options.h"
31 #include "gfortran.h"
32
33 #include "diagnostic.h"
34 #include "diagnostic-color.h"
35 #include "tree-diagnostic.h" /* tree_diagnostics_defaults */
36
37 static int suppress_errors = 0;
38
39 static bool warnings_not_errors = false;
40
41 static int terminal_width;
42
43 /* True if the error/warnings should be buffered. */
44 static bool buffered_p;
45
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;
51
52 /* Return true if there output_buffer is empty. */
53
54 static bool
gfc_output_buffer_empty_p(const output_buffer * buf)55 gfc_output_buffer_empty_p (const output_buffer * buf)
56 {
57 return output_buffer_last_position_in_text (buf) == NULL;
58 }
59
60 /* Go one level deeper suppressing errors. */
61
62 void
gfc_push_suppress_errors(void)63 gfc_push_suppress_errors (void)
64 {
65 gcc_assert (suppress_errors >= 0);
66 ++suppress_errors;
67 }
68
69 static void
70 gfc_error_opt (int opt, const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(2,0);
71
72 static bool
73 gfc_warning (int opt, const char *gmsgid, va_list ap) ATTRIBUTE_GCC_GFC(2,0);
74
75
76 /* Leave one level of error suppressing. */
77
78 void
gfc_pop_suppress_errors(void)79 gfc_pop_suppress_errors (void)
80 {
81 gcc_assert (suppress_errors > 0);
82 --suppress_errors;
83 }
84
85
86 /* Query whether errors are suppressed. */
87
88 bool
gfc_query_suppress_errors(void)89 gfc_query_suppress_errors (void)
90 {
91 return suppress_errors > 0;
92 }
93
94
95 /* Determine terminal width (for trimming source lines in output). */
96
97 static int
gfc_get_terminal_width(void)98 gfc_get_terminal_width (void)
99 {
100 return isatty (STDERR_FILENO) ? get_terminal_width () : INT_MAX;
101 }
102
103
104 /* Per-file error initialization. */
105
106 void
gfc_error_init_1(void)107 gfc_error_init_1 (void)
108 {
109 terminal_width = gfc_get_terminal_width ();
110 gfc_buffer_error (false);
111 }
112
113
114 /* Set the flag for buffering errors or not. */
115
116 void
gfc_buffer_error(bool flag)117 gfc_buffer_error (bool flag)
118 {
119 buffered_p = flag;
120 }
121
122
123 /* Add a single character to the error buffer or output depending on
124 buffered_p. */
125
126 static void
error_char(char)127 error_char (char)
128 {
129 /* FIXME: Unused function to be removed in a subsequent patch. */
130 }
131
132
133 /* Copy a string to wherever it needs to go. */
134
135 static void
error_string(const char * p)136 error_string (const char *p)
137 {
138 while (*p)
139 error_char (*p++);
140 }
141
142
143 /* Print a formatted integer to the error buffer or output. */
144
145 #define IBUF_LEN 60
146
147 static void
error_uinteger(unsigned long long int i)148 error_uinteger (unsigned long long int i)
149 {
150 char *p, int_buf[IBUF_LEN];
151
152 p = int_buf + IBUF_LEN - 1;
153 *p-- = '\0';
154
155 if (i == 0)
156 *p-- = '0';
157
158 while (i > 0)
159 {
160 *p-- = i % 10 + '0';
161 i = i / 10;
162 }
163
164 error_string (p + 1);
165 }
166
167 static void
error_integer(long long int i)168 error_integer (long long int i)
169 {
170 unsigned long long int u;
171
172 if (i < 0)
173 {
174 u = (unsigned long long int) -i;
175 error_char ('-');
176 }
177 else
178 u = i;
179
180 error_uinteger (u);
181 }
182
183
184 static void
error_hwuint(unsigned HOST_WIDE_INT i)185 error_hwuint (unsigned HOST_WIDE_INT i)
186 {
187 char *p, int_buf[IBUF_LEN];
188
189 p = int_buf + IBUF_LEN - 1;
190 *p-- = '\0';
191
192 if (i == 0)
193 *p-- = '0';
194
195 while (i > 0)
196 {
197 *p-- = i % 10 + '0';
198 i = i / 10;
199 }
200
201 error_string (p + 1);
202 }
203
204 static void
error_hwint(HOST_WIDE_INT i)205 error_hwint (HOST_WIDE_INT i)
206 {
207 unsigned HOST_WIDE_INT u;
208
209 if (i < 0)
210 {
211 u = (unsigned HOST_WIDE_INT) -i;
212 error_char ('-');
213 }
214 else
215 u = i;
216
217 error_uinteger (u);
218 }
219
220
221 static size_t
gfc_widechar_display_length(gfc_char_t c)222 gfc_widechar_display_length (gfc_char_t c)
223 {
224 if (gfc_wide_is_printable (c) || c == '\t')
225 /* Printable ASCII character, or tabulation (output as a space). */
226 return 1;
227 else if (c < ((gfc_char_t) 1 << 8))
228 /* Displayed as \x?? */
229 return 4;
230 else if (c < ((gfc_char_t) 1 << 16))
231 /* Displayed as \u???? */
232 return 6;
233 else
234 /* Displayed as \U???????? */
235 return 10;
236 }
237
238
239 /* Length of the ASCII representation of the wide string, escaping wide
240 characters as print_wide_char_into_buffer() does. */
241
242 static size_t
gfc_wide_display_length(const gfc_char_t * str)243 gfc_wide_display_length (const gfc_char_t *str)
244 {
245 size_t i, len;
246
247 for (i = 0, len = 0; str[i]; i++)
248 len += gfc_widechar_display_length (str[i]);
249
250 return len;
251 }
252
253 static int
print_wide_char_into_buffer(gfc_char_t c,char * buf)254 print_wide_char_into_buffer (gfc_char_t c, char *buf)
255 {
256 static const char xdigit[16] = { '0', '1', '2', '3', '4', '5', '6',
257 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
258
259 if (gfc_wide_is_printable (c) || c == '\t')
260 {
261 buf[1] = '\0';
262 /* Tabulation is output as a space. */
263 buf[0] = (unsigned char) (c == '\t' ? ' ' : c);
264 return 1;
265 }
266 else if (c < ((gfc_char_t) 1 << 8))
267 {
268 buf[4] = '\0';
269 buf[3] = xdigit[c & 0x0F];
270 c = c >> 4;
271 buf[2] = xdigit[c & 0x0F];
272
273 buf[1] = 'x';
274 buf[0] = '\\';
275 return 4;
276 }
277 else if (c < ((gfc_char_t) 1 << 16))
278 {
279 buf[6] = '\0';
280 buf[5] = xdigit[c & 0x0F];
281 c = c >> 4;
282 buf[4] = xdigit[c & 0x0F];
283 c = c >> 4;
284 buf[3] = xdigit[c & 0x0F];
285 c = c >> 4;
286 buf[2] = xdigit[c & 0x0F];
287
288 buf[1] = 'u';
289 buf[0] = '\\';
290 return 6;
291 }
292 else
293 {
294 buf[10] = '\0';
295 buf[9] = xdigit[c & 0x0F];
296 c = c >> 4;
297 buf[8] = xdigit[c & 0x0F];
298 c = c >> 4;
299 buf[7] = xdigit[c & 0x0F];
300 c = c >> 4;
301 buf[6] = xdigit[c & 0x0F];
302 c = c >> 4;
303 buf[5] = xdigit[c & 0x0F];
304 c = c >> 4;
305 buf[4] = xdigit[c & 0x0F];
306 c = c >> 4;
307 buf[3] = xdigit[c & 0x0F];
308 c = c >> 4;
309 buf[2] = xdigit[c & 0x0F];
310
311 buf[1] = 'U';
312 buf[0] = '\\';
313 return 10;
314 }
315 }
316
317 static char wide_char_print_buffer[11];
318
319 const char *
gfc_print_wide_char(gfc_char_t c)320 gfc_print_wide_char (gfc_char_t c)
321 {
322 print_wide_char_into_buffer (c, wide_char_print_buffer);
323 return wide_char_print_buffer;
324 }
325
326
327 /* Show the file, where it was included, and the source line, give a
328 locus. Calls error_printf() recursively, but the recursion is at
329 most one level deep. */
330
331 static void error_printf (const char *, ...) ATTRIBUTE_GCC_GFC(1,2);
332
333 static void
show_locus(locus * loc,int c1,int c2)334 show_locus (locus *loc, int c1, int c2)
335 {
336 gfc_linebuf *lb;
337 gfc_file *f;
338 gfc_char_t *p;
339 int i, offset, cmax;
340
341 /* TODO: Either limit the total length and number of included files
342 displayed or add buffering of arbitrary number of characters in
343 error messages. */
344
345 /* Write out the error header line, giving the source file and error
346 location (in GNU standard "[file]:[line].[column]:" format),
347 followed by an "included by" stack and a blank line. This header
348 format is matched by a testsuite parser defined in
349 lib/gfortran-dg.exp. */
350
351 lb = loc->lb;
352 f = lb->file;
353
354 error_string (f->filename);
355 error_char (':');
356
357 error_integer (LOCATION_LINE (lb->location));
358
359 if ((c1 > 0) || (c2 > 0))
360 error_char ('.');
361
362 if (c1 > 0)
363 error_integer (c1);
364
365 if ((c1 > 0) && (c2 > 0))
366 error_char ('-');
367
368 if (c2 > 0)
369 error_integer (c2);
370
371 error_char (':');
372 error_char ('\n');
373
374 for (;;)
375 {
376 i = f->inclusion_line;
377
378 f = f->up;
379 if (f == NULL) break;
380
381 error_printf (" Included at %s:%d:", f->filename, i);
382 }
383
384 error_char ('\n');
385
386 /* Calculate an appropriate horizontal offset of the source line in
387 order to get the error locus within the visible portion of the
388 line. Note that if the margin of 5 here is changed, the
389 corresponding margin of 10 in show_loci should be changed. */
390
391 offset = 0;
392
393 /* If the two loci would appear in the same column, we shift
394 '2' one column to the right, so as to print '12' rather than
395 just '1'. We do this here so it will be accounted for in the
396 margin calculations. */
397
398 if (c1 == c2)
399 c2 += 1;
400
401 cmax = (c1 < c2) ? c2 : c1;
402 if (cmax > terminal_width - 5)
403 offset = cmax - terminal_width + 5;
404
405 /* Show the line itself, taking care not to print more than what can
406 show up on the terminal. Tabs are converted to spaces, and
407 nonprintable characters are converted to a "\xNN" sequence. */
408
409 p = &(lb->line[offset]);
410 i = gfc_wide_display_length (p);
411 if (i > terminal_width)
412 i = terminal_width - 1;
413
414 while (i > 0)
415 {
416 static char buffer[11];
417 i -= print_wide_char_into_buffer (*p++, buffer);
418 error_string (buffer);
419 }
420
421 error_char ('\n');
422
423 /* Show the '1' and/or '2' corresponding to the column of the error
424 locus. Note that a value of -1 for c1 or c2 will simply cause
425 the relevant number not to be printed. */
426
427 c1 -= offset;
428 c2 -= offset;
429 cmax -= offset;
430
431 p = &(lb->line[offset]);
432 for (i = 0; i < cmax; i++)
433 {
434 int spaces, j;
435 spaces = gfc_widechar_display_length (*p++);
436
437 if (i == c1)
438 error_char ('1'), spaces--;
439 else if (i == c2)
440 error_char ('2'), spaces--;
441
442 for (j = 0; j < spaces; j++)
443 error_char (' ');
444 }
445
446 if (i == c1)
447 error_char ('1');
448 else if (i == c2)
449 error_char ('2');
450
451 error_char ('\n');
452
453 }
454
455
456 /* As part of printing an error, we show the source lines that caused
457 the problem. We show at least one, and possibly two loci; the two
458 loci may or may not be on the same source line. */
459
460 static void
show_loci(locus * l1,locus * l2)461 show_loci (locus *l1, locus *l2)
462 {
463 int m, c1, c2;
464
465 if (l1 == NULL || l1->lb == NULL)
466 {
467 error_printf ("<During initialization>\n");
468 return;
469 }
470
471 /* While calculating parameters for printing the loci, we consider possible
472 reasons for printing one per line. If appropriate, print the loci
473 individually; otherwise we print them both on the same line. */
474
475 c1 = l1->nextc - l1->lb->line;
476 if (l2 == NULL)
477 {
478 show_locus (l1, c1, -1);
479 return;
480 }
481
482 c2 = l2->nextc - l2->lb->line;
483
484 if (c1 < c2)
485 m = c2 - c1;
486 else
487 m = c1 - c2;
488
489 /* Note that the margin value of 10 here needs to be less than the
490 margin of 5 used in the calculation of offset in show_locus. */
491
492 if (l1->lb != l2->lb || m > terminal_width - 10)
493 {
494 show_locus (l1, c1, -1);
495 show_locus (l2, -1, c2);
496 return;
497 }
498
499 show_locus (l1, c1, c2);
500
501 return;
502 }
503
504
505 /* Workhorse for the error printing subroutines. This subroutine is
506 inspired by g77's error handling and is similar to printf() with
507 the following %-codes:
508
509 %c Character, %d or %i Integer, %s String, %% Percent
510 %L Takes locus argument
511 %C Current locus (no argument)
512
513 If a locus pointer is given, the actual source line is printed out
514 and the column is indicated. Since we want the error message at
515 the bottom of any source file information, we must scan the
516 argument list twice -- once to determine whether the loci are
517 present and record this for printing, and once to print the error
518 message after and loci have been printed. A maximum of two locus
519 arguments are permitted.
520
521 This function is also called (recursively) by show_locus in the
522 case of included files; however, as show_locus does not resupply
523 any loci, the recursion is at most one level deep. */
524
525 #define MAX_ARGS 10
526
527 static void ATTRIBUTE_GCC_GFC(2,0)
error_print(const char * type,const char * format0,va_list argp)528 error_print (const char *type, const char *format0, va_list argp)
529 {
530 enum { TYPE_CURRENTLOC, TYPE_LOCUS, TYPE_INTEGER, TYPE_UINTEGER,
531 TYPE_LONGINT, TYPE_ULONGINT, TYPE_LLONGINT, TYPE_ULLONGINT,
532 TYPE_HWINT, TYPE_HWUINT, TYPE_CHAR, TYPE_STRING, NOTYPE };
533 struct
534 {
535 int type;
536 int pos;
537 union
538 {
539 int intval;
540 unsigned int uintval;
541 long int longintval;
542 unsigned long int ulongintval;
543 long long int llongintval;
544 unsigned long long int ullongintval;
545 HOST_WIDE_INT hwintval;
546 unsigned HOST_WIDE_INT hwuintval;
547 char charval;
548 const char * stringval;
549 } u;
550 } arg[MAX_ARGS], spec[MAX_ARGS];
551 /* spec is the array of specifiers, in the same order as they
552 appear in the format string. arg is the array of arguments,
553 in the same order as they appear in the va_list. */
554
555 char c;
556 int i, n, have_l1, pos, maxpos;
557 locus *l1, *l2, *loc;
558 const char *format;
559
560 loc = l1 = l2 = NULL;
561
562 have_l1 = 0;
563 pos = -1;
564 maxpos = -1;
565
566 n = 0;
567 format = format0;
568
569 for (i = 0; i < MAX_ARGS; i++)
570 {
571 arg[i].type = NOTYPE;
572 spec[i].pos = -1;
573 }
574
575 /* First parse the format string for position specifiers. */
576 while (*format)
577 {
578 c = *format++;
579 if (c != '%')
580 continue;
581
582 if (*format == '%')
583 {
584 format++;
585 continue;
586 }
587
588 if (ISDIGIT (*format))
589 {
590 /* This is a position specifier. For example, the number
591 12 in the format string "%12$d", which specifies the third
592 argument of the va_list, formatted in %d format.
593 For details, see "man 3 printf". */
594 pos = atoi(format) - 1;
595 gcc_assert (pos >= 0);
596 while (ISDIGIT(*format))
597 format++;
598 gcc_assert (*format == '$');
599 format++;
600 }
601 else
602 pos++;
603
604 c = *format++;
605
606 if (pos > maxpos)
607 maxpos = pos;
608
609 switch (c)
610 {
611 case 'C':
612 arg[pos].type = TYPE_CURRENTLOC;
613 break;
614
615 case 'L':
616 arg[pos].type = TYPE_LOCUS;
617 break;
618
619 case 'd':
620 case 'i':
621 arg[pos].type = TYPE_INTEGER;
622 break;
623
624 case 'u':
625 arg[pos].type = TYPE_UINTEGER;
626 break;
627
628 case 'l':
629 c = *format++;
630 if (c == 'l')
631 {
632 c = *format++;
633 if (c == 'u')
634 arg[pos].type = TYPE_ULLONGINT;
635 else if (c == 'i' || c == 'd')
636 arg[pos].type = TYPE_LLONGINT;
637 else
638 gcc_unreachable ();
639 }
640 else if (c == 'u')
641 arg[pos].type = TYPE_ULONGINT;
642 else if (c == 'i' || c == 'd')
643 arg[pos].type = TYPE_LONGINT;
644 else
645 gcc_unreachable ();
646 break;
647
648 case 'w':
649 c = *format++;
650 if (c == 'u')
651 arg[pos].type = TYPE_HWUINT;
652 else if (c == 'i' || c == 'd')
653 arg[pos].type = TYPE_HWINT;
654 else
655 gcc_unreachable ();
656 break;
657
658 case 'c':
659 arg[pos].type = TYPE_CHAR;
660 break;
661
662 case 's':
663 arg[pos].type = TYPE_STRING;
664 break;
665
666 default:
667 gcc_unreachable ();
668 }
669
670 spec[n++].pos = pos;
671 }
672
673 /* Then convert the values for each %-style argument. */
674 for (pos = 0; pos <= maxpos; pos++)
675 {
676 gcc_assert (arg[pos].type != NOTYPE);
677 switch (arg[pos].type)
678 {
679 case TYPE_CURRENTLOC:
680 loc = &gfc_current_locus;
681 /* Fall through. */
682
683 case TYPE_LOCUS:
684 if (arg[pos].type == TYPE_LOCUS)
685 loc = va_arg (argp, locus *);
686
687 if (have_l1)
688 {
689 l2 = loc;
690 arg[pos].u.stringval = "(2)";
691 /* Point %C first offending character not the last good one. */
692 if (arg[pos].type == TYPE_CURRENTLOC && *l2->nextc != '\0')
693 l2->nextc++;
694 }
695 else
696 {
697 l1 = loc;
698 have_l1 = 1;
699 arg[pos].u.stringval = "(1)";
700 /* Point %C first offending character not the last good one. */
701 if (arg[pos].type == TYPE_CURRENTLOC && *l1->nextc != '\0')
702 l1->nextc++;
703 }
704 break;
705
706 case TYPE_INTEGER:
707 arg[pos].u.intval = va_arg (argp, int);
708 break;
709
710 case TYPE_UINTEGER:
711 arg[pos].u.uintval = va_arg (argp, unsigned int);
712 break;
713
714 case TYPE_LONGINT:
715 arg[pos].u.longintval = va_arg (argp, long int);
716 break;
717
718 case TYPE_ULONGINT:
719 arg[pos].u.ulongintval = va_arg (argp, unsigned long int);
720 break;
721
722 case TYPE_LLONGINT:
723 arg[pos].u.llongintval = va_arg (argp, long long int);
724 break;
725
726 case TYPE_ULLONGINT:
727 arg[pos].u.ullongintval = va_arg (argp, unsigned long long int);
728 break;
729
730 case TYPE_HWINT:
731 arg[pos].u.hwintval = va_arg (argp, HOST_WIDE_INT);
732 break;
733
734 case TYPE_HWUINT:
735 arg[pos].u.hwuintval = va_arg (argp, unsigned HOST_WIDE_INT);
736 break;
737
738 case TYPE_CHAR:
739 arg[pos].u.charval = (char) va_arg (argp, int);
740 break;
741
742 case TYPE_STRING:
743 arg[pos].u.stringval = (const char *) va_arg (argp, char *);
744 break;
745
746 default:
747 gcc_unreachable ();
748 }
749 }
750
751 for (n = 0; spec[n].pos >= 0; n++)
752 spec[n].u = arg[spec[n].pos].u;
753
754 /* Show the current loci if we have to. */
755 if (have_l1)
756 show_loci (l1, l2);
757
758 if (*type)
759 {
760 error_string (type);
761 error_char (' ');
762 }
763
764 have_l1 = 0;
765 format = format0;
766 n = 0;
767
768 for (; *format; format++)
769 {
770 if (*format != '%')
771 {
772 error_char (*format);
773 continue;
774 }
775
776 format++;
777 if (ISDIGIT (*format))
778 {
779 /* This is a position specifier. See comment above. */
780 while (ISDIGIT (*format))
781 format++;
782
783 /* Skip over the dollar sign. */
784 format++;
785 }
786
787 switch (*format)
788 {
789 case '%':
790 error_char ('%');
791 break;
792
793 case 'c':
794 error_char (spec[n++].u.charval);
795 break;
796
797 case 's':
798 case 'C': /* Current locus */
799 case 'L': /* Specified locus */
800 error_string (spec[n++].u.stringval);
801 break;
802
803 case 'd':
804 case 'i':
805 error_integer (spec[n++].u.intval);
806 break;
807
808 case 'u':
809 error_uinteger (spec[n++].u.uintval);
810 break;
811
812 case 'l':
813 format++;
814 if (*format == 'l')
815 {
816 format++;
817 if (*format == 'u')
818 error_uinteger (spec[n++].u.ullongintval);
819 else
820 error_integer (spec[n++].u.llongintval);
821 }
822 if (*format == 'u')
823 error_uinteger (spec[n++].u.ulongintval);
824 else
825 error_integer (spec[n++].u.longintval);
826 break;
827
828 case 'w':
829 format++;
830 if (*format == 'u')
831 error_hwuint (spec[n++].u.hwintval);
832 else
833 error_hwint (spec[n++].u.hwuintval);
834 break;
835 }
836 }
837
838 error_char ('\n');
839 }
840
841
842 /* Wrapper for error_print(). */
843
844 static void
error_printf(const char * gmsgid,...)845 error_printf (const char *gmsgid, ...)
846 {
847 va_list argp;
848
849 va_start (argp, gmsgid);
850 error_print ("", _(gmsgid), argp);
851 va_end (argp);
852 }
853
854
855 /* Clear any output buffered in a pretty-print output_buffer. */
856
857 static void
gfc_clear_pp_buffer(output_buffer * this_buffer)858 gfc_clear_pp_buffer (output_buffer *this_buffer)
859 {
860 pretty_printer *pp = global_dc->printer;
861 output_buffer *tmp_buffer = pp->buffer;
862 pp->buffer = this_buffer;
863 pp_clear_output_area (pp);
864 pp->buffer = tmp_buffer;
865 /* We need to reset last_location, otherwise we may skip caret lines
866 when we actually give a diagnostic. */
867 global_dc->last_location = UNKNOWN_LOCATION;
868 }
869
870 /* The currently-printing diagnostic, for use by gfc_format_decoder,
871 for colorizing %C and %L. */
872
873 static diagnostic_info *curr_diagnostic;
874
875 /* A helper function to call diagnostic_report_diagnostic, while setting
876 curr_diagnostic for the duration of the call. */
877
878 static bool
gfc_report_diagnostic(diagnostic_info * diagnostic)879 gfc_report_diagnostic (diagnostic_info *diagnostic)
880 {
881 gcc_assert (diagnostic != NULL);
882 curr_diagnostic = diagnostic;
883 bool ret = diagnostic_report_diagnostic (global_dc, diagnostic);
884 curr_diagnostic = NULL;
885 return ret;
886 }
887
888 /* This is just a helper function to avoid duplicating the logic of
889 gfc_warning. */
890
891 static bool
gfc_warning(int opt,const char * gmsgid,va_list ap)892 gfc_warning (int opt, const char *gmsgid, va_list ap)
893 {
894 va_list argp;
895 va_copy (argp, ap);
896
897 diagnostic_info diagnostic;
898 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
899 bool fatal_errors = global_dc->fatal_errors;
900 pretty_printer *pp = global_dc->printer;
901 output_buffer *tmp_buffer = pp->buffer;
902
903 gfc_clear_pp_buffer (pp_warning_buffer);
904
905 if (buffered_p)
906 {
907 pp->buffer = pp_warning_buffer;
908 global_dc->fatal_errors = false;
909 /* To prevent -fmax-errors= triggering. */
910 --werrorcount;
911 }
912
913 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc,
914 DK_WARNING);
915 diagnostic.option_index = opt;
916 bool ret = gfc_report_diagnostic (&diagnostic);
917
918 if (buffered_p)
919 {
920 pp->buffer = tmp_buffer;
921 global_dc->fatal_errors = fatal_errors;
922
923 warningcount_buffered = 0;
924 werrorcount_buffered = 0;
925 /* Undo the above --werrorcount if not Werror, otherwise
926 werrorcount is correct already. */
927 if (!ret)
928 ++werrorcount;
929 else if (diagnostic.kind == DK_ERROR)
930 ++werrorcount_buffered;
931 else
932 ++werrorcount, --warningcount, ++warningcount_buffered;
933 }
934
935 va_end (argp);
936 return ret;
937 }
938
939 /* Issue a warning. */
940
941 bool
gfc_warning(int opt,const char * gmsgid,...)942 gfc_warning (int opt, const char *gmsgid, ...)
943 {
944 va_list argp;
945
946 va_start (argp, gmsgid);
947 bool ret = gfc_warning (opt, gmsgid, argp);
948 va_end (argp);
949 return ret;
950 }
951
952
953 /* Whether, for a feature included in a given standard set (GFC_STD_*),
954 we should issue an error or a warning, or be quiet. */
955
956 notification
gfc_notification_std(int std)957 gfc_notification_std (int std)
958 {
959 bool warning;
960
961 warning = ((gfc_option.warn_std & std) != 0) && !inhibit_warnings;
962 if ((gfc_option.allow_std & std) != 0 && !warning)
963 return SILENT;
964
965 return warning ? WARNING : ERROR;
966 }
967
968
969 /* Return a string describing the nature of a standard violation
970 * and/or the relevant version of the standard. */
971
972 char const*
notify_std_msg(int std)973 notify_std_msg(int std)
974 {
975
976 if (std & GFC_STD_F2018_DEL)
977 return _("Fortran 2018 deleted feature:");
978 else if (std & GFC_STD_F2018_OBS)
979 return _("Fortran 2018 obsolescent feature:");
980 else if (std & GFC_STD_F2018)
981 return _("Fortran 2018:");
982 else if (std & GFC_STD_F2008_OBS)
983 return _("Fortran 2008 obsolescent feature:");
984 else if (std & GFC_STD_F2008)
985 return "Fortran 2008:";
986 else if (std & GFC_STD_F2003)
987 return "Fortran 2003:";
988 else if (std & GFC_STD_GNU)
989 return _("GNU Extension:");
990 else if (std & GFC_STD_LEGACY)
991 return _("Legacy Extension:");
992 else if (std & GFC_STD_F95_OBS)
993 return _("Obsolescent feature:");
994 else if (std & GFC_STD_F95_DEL)
995 return _("Deleted feature:");
996 else
997 gcc_unreachable ();
998 }
999
1000
1001 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
1002 feature. An error/warning will be issued if the currently selected
1003 standard does not contain the requested bits. Return false if
1004 an error is generated. */
1005
1006 bool
gfc_notify_std(int std,const char * gmsgid,...)1007 gfc_notify_std (int std, const char *gmsgid, ...)
1008 {
1009 va_list argp;
1010 const char *msg, *msg2;
1011 char *buffer;
1012
1013 /* Determine whether an error or a warning is needed. */
1014 const int wstd = std & gfc_option.warn_std; /* Standard to warn about. */
1015 const int estd = std & ~gfc_option.allow_std; /* Standard to error about. */
1016 const bool warning = (wstd != 0) && !inhibit_warnings;
1017 const bool error = (estd != 0);
1018
1019 if (!error && !warning)
1020 return true;
1021 if (suppress_errors)
1022 return !error;
1023
1024 if (error)
1025 msg = notify_std_msg (estd);
1026 else
1027 msg = notify_std_msg (wstd);
1028
1029 msg2 = _(gmsgid);
1030 buffer = (char *) alloca (strlen (msg) + strlen (msg2) + 2);
1031 strcpy (buffer, msg);
1032 strcat (buffer, " ");
1033 strcat (buffer, msg2);
1034
1035 va_start (argp, gmsgid);
1036 if (error)
1037 gfc_error_opt (0, buffer, argp);
1038 else
1039 gfc_warning (0, buffer, argp);
1040 va_end (argp);
1041
1042 if (error)
1043 return false;
1044 else
1045 return (warning && !warnings_are_errors);
1046 }
1047
1048
1049 /* Called from output_format -- during diagnostic message processing
1050 to handle Fortran specific format specifiers with the following meanings:
1051
1052 %C Current locus (no argument)
1053 %L Takes locus argument
1054 */
1055 static bool
gfc_format_decoder(pretty_printer * pp,text_info * text,const char * spec,int precision,bool wide,bool set_locus,bool hash,bool * quoted,const char ** buffer_ptr)1056 gfc_format_decoder (pretty_printer *pp, text_info *text, const char *spec,
1057 int precision, bool wide, bool set_locus, bool hash,
1058 bool *quoted, const char **buffer_ptr)
1059 {
1060 switch (*spec)
1061 {
1062 case 'C':
1063 case 'L':
1064 {
1065 static const char *result[2] = { "(1)", "(2)" };
1066 locus *loc;
1067 if (*spec == 'C')
1068 loc = &gfc_current_locus;
1069 else
1070 loc = va_arg (*text->args_ptr, locus *);
1071 gcc_assert (loc->nextc - loc->lb->line >= 0);
1072 unsigned int offset = loc->nextc - loc->lb->line;
1073 if (*spec == 'C' && *loc->nextc != '\0')
1074 /* Point %C first offending character not the last good one. */
1075 offset++;
1076 /* If location[0] != UNKNOWN_LOCATION means that we already
1077 processed one of %C/%L. */
1078 int loc_num = text->get_location (0) == UNKNOWN_LOCATION ? 0 : 1;
1079 location_t src_loc
1080 = linemap_position_for_loc_and_offset (line_table,
1081 loc->lb->location,
1082 offset);
1083 text->set_location (loc_num, src_loc, SHOW_RANGE_WITH_CARET);
1084 /* Colorize the markers to match the color choices of
1085 diagnostic_show_locus (the initial location has a color given
1086 by the "kind" of the diagnostic, the secondary location has
1087 color "range1"). */
1088 gcc_assert (curr_diagnostic != NULL);
1089 const char *color
1090 = (loc_num
1091 ? "range1"
1092 : diagnostic_get_color_for_kind (curr_diagnostic->kind));
1093 pp_string (pp, colorize_start (pp_show_color (pp), color));
1094 pp_string (pp, result[loc_num]);
1095 pp_string (pp, colorize_stop (pp_show_color (pp)));
1096 return true;
1097 }
1098 default:
1099 /* Fall through info the middle-end decoder, as e.g. stor-layout.cc
1100 etc. diagnostics can use the FE printer while the FE is still
1101 active. */
1102 return default_tree_printer (pp, text, spec, precision, wide,
1103 set_locus, hash, quoted, buffer_ptr);
1104 }
1105 }
1106
1107 /* Return a malloc'd string describing the kind of diagnostic. The
1108 caller is responsible for freeing the memory. */
1109 static char *
gfc_diagnostic_build_kind_prefix(diagnostic_context * context,const diagnostic_info * diagnostic)1110 gfc_diagnostic_build_kind_prefix (diagnostic_context *context,
1111 const diagnostic_info *diagnostic)
1112 {
1113 static const char *const diagnostic_kind_text[] = {
1114 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
1115 #include "gfc-diagnostic.def"
1116 #undef DEFINE_DIAGNOSTIC_KIND
1117 "must-not-happen"
1118 };
1119 static const char *const diagnostic_kind_color[] = {
1120 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C),
1121 #include "gfc-diagnostic.def"
1122 #undef DEFINE_DIAGNOSTIC_KIND
1123 NULL
1124 };
1125 gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND);
1126 const char *text = _(diagnostic_kind_text[diagnostic->kind]);
1127 const char *text_cs = "", *text_ce = "";
1128 pretty_printer *pp = context->printer;
1129
1130 if (diagnostic_kind_color[diagnostic->kind])
1131 {
1132 text_cs = colorize_start (pp_show_color (pp),
1133 diagnostic_kind_color[diagnostic->kind]);
1134 text_ce = colorize_stop (pp_show_color (pp));
1135 }
1136 return build_message_string ("%s%s:%s ", text_cs, text, text_ce);
1137 }
1138
1139 /* Return a malloc'd string describing a location. The caller is
1140 responsible for freeing the memory. */
1141 static char *
gfc_diagnostic_build_locus_prefix(diagnostic_context * context,expanded_location s)1142 gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
1143 expanded_location s)
1144 {
1145 pretty_printer *pp = context->printer;
1146 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
1147 const char *locus_ce = colorize_stop (pp_show_color (pp));
1148 return (s.file == NULL
1149 ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
1150 : !strcmp (s.file, N_("<built-in>"))
1151 ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
1152 : context->show_column
1153 ? build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
1154 s.column, locus_ce)
1155 : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line, locus_ce));
1156 }
1157
1158 /* Return a malloc'd string describing two locations. The caller is
1159 responsible for freeing the memory. */
1160 static char *
gfc_diagnostic_build_locus_prefix(diagnostic_context * context,expanded_location s,expanded_location s2)1161 gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
1162 expanded_location s, expanded_location s2)
1163 {
1164 pretty_printer *pp = context->printer;
1165 const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
1166 const char *locus_ce = colorize_stop (pp_show_color (pp));
1167
1168 return (s.file == NULL
1169 ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
1170 : !strcmp (s.file, N_("<built-in>"))
1171 ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
1172 : context->show_column
1173 ? build_message_string ("%s%s:%d:%d-%d:%s", locus_cs, s.file, s.line,
1174 MIN (s.column, s2.column),
1175 MAX (s.column, s2.column), locus_ce)
1176 : build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line,
1177 locus_ce));
1178 }
1179
1180 /* This function prints the locus (file:line:column), the diagnostic kind
1181 (Error, Warning) and (optionally) the relevant lines of code with
1182 annotation lines with '1' and/or '2' below them.
1183
1184 With -fdiagnostic-show-caret (the default) it prints:
1185
1186 [locus of primary range]:
1187
1188 some code
1189 1
1190 Error: Some error at (1)
1191
1192 With -fno-diagnostic-show-caret or if the primary range is not
1193 valid, it prints:
1194
1195 [locus of primary range]: Error: Some error at (1) and (2)
1196 */
1197 static void
gfc_diagnostic_starter(diagnostic_context * context,diagnostic_info * diagnostic)1198 gfc_diagnostic_starter (diagnostic_context *context,
1199 diagnostic_info *diagnostic)
1200 {
1201 char * kind_prefix = gfc_diagnostic_build_kind_prefix (context, diagnostic);
1202
1203 expanded_location s1 = diagnostic_expand_location (diagnostic);
1204 expanded_location s2;
1205 bool one_locus = diagnostic->richloc->get_num_locations () < 2;
1206 bool same_locus = false;
1207
1208 if (!one_locus)
1209 {
1210 s2 = diagnostic_expand_location (diagnostic, 1);
1211 same_locus = diagnostic_same_line (context, s1, s2);
1212 }
1213
1214 char * locus_prefix = (one_locus || !same_locus)
1215 ? gfc_diagnostic_build_locus_prefix (context, s1)
1216 : gfc_diagnostic_build_locus_prefix (context, s1, s2);
1217
1218 if (!context->show_caret
1219 || diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION
1220 || diagnostic_location (diagnostic, 0) == context->last_location)
1221 {
1222 pp_set_prefix (context->printer,
1223 concat (locus_prefix, " ", kind_prefix, NULL));
1224 free (locus_prefix);
1225
1226 if (one_locus || same_locus)
1227 {
1228 free (kind_prefix);
1229 return;
1230 }
1231 /* In this case, we print the previous locus and prefix as:
1232
1233 [locus]:[prefix]: (1)
1234
1235 and we flush with a new line before setting the new prefix. */
1236 pp_string (context->printer, "(1)");
1237 pp_newline (context->printer);
1238 locus_prefix = gfc_diagnostic_build_locus_prefix (context, s2);
1239 pp_set_prefix (context->printer,
1240 concat (locus_prefix, " ", kind_prefix, NULL));
1241 free (kind_prefix);
1242 free (locus_prefix);
1243 }
1244 else
1245 {
1246 pp_verbatim (context->printer, "%s", locus_prefix);
1247 free (locus_prefix);
1248 /* Fortran uses an empty line between locus and caret line. */
1249 pp_newline (context->printer);
1250 pp_set_prefix (context->printer, NULL);
1251 pp_newline (context->printer);
1252 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
1253 /* If the caret line was shown, the prefix does not contain the
1254 locus. */
1255 pp_set_prefix (context->printer, kind_prefix);
1256 }
1257 }
1258
1259 static void
gfc_diagnostic_start_span(diagnostic_context * context,expanded_location exploc)1260 gfc_diagnostic_start_span (diagnostic_context *context,
1261 expanded_location exploc)
1262 {
1263 char *locus_prefix;
1264 locus_prefix = gfc_diagnostic_build_locus_prefix (context, exploc);
1265 pp_verbatim (context->printer, "%s", locus_prefix);
1266 free (locus_prefix);
1267 pp_newline (context->printer);
1268 /* Fortran uses an empty line between locus and caret line. */
1269 pp_newline (context->printer);
1270 }
1271
1272
1273 static void
gfc_diagnostic_finalizer(diagnostic_context * context,diagnostic_info * diagnostic ATTRIBUTE_UNUSED,diagnostic_t orig_diag_kind ATTRIBUTE_UNUSED)1274 gfc_diagnostic_finalizer (diagnostic_context *context,
1275 diagnostic_info *diagnostic ATTRIBUTE_UNUSED,
1276 diagnostic_t orig_diag_kind ATTRIBUTE_UNUSED)
1277 {
1278 pp_destroy_prefix (context->printer);
1279 pp_newline_and_flush (context->printer);
1280 }
1281
1282 /* Immediate warning (i.e. do not buffer the warning) with an explicit
1283 location. */
1284
1285 bool
gfc_warning_now_at(location_t loc,int opt,const char * gmsgid,...)1286 gfc_warning_now_at (location_t loc, int opt, const char *gmsgid, ...)
1287 {
1288 va_list argp;
1289 diagnostic_info diagnostic;
1290 rich_location rich_loc (line_table, loc);
1291 bool ret;
1292
1293 va_start (argp, gmsgid);
1294 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_WARNING);
1295 diagnostic.option_index = opt;
1296 ret = gfc_report_diagnostic (&diagnostic);
1297 va_end (argp);
1298 return ret;
1299 }
1300
1301 /* Immediate warning (i.e. do not buffer the warning). */
1302
1303 bool
gfc_warning_now(int opt,const char * gmsgid,...)1304 gfc_warning_now (int opt, const char *gmsgid, ...)
1305 {
1306 va_list argp;
1307 diagnostic_info diagnostic;
1308 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1309 bool ret;
1310
1311 va_start (argp, gmsgid);
1312 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc,
1313 DK_WARNING);
1314 diagnostic.option_index = opt;
1315 ret = gfc_report_diagnostic (&diagnostic);
1316 va_end (argp);
1317 return ret;
1318 }
1319
1320 /* Internal warning, do not buffer. */
1321
1322 bool
gfc_warning_internal(int opt,const char * gmsgid,...)1323 gfc_warning_internal (int opt, const char *gmsgid, ...)
1324 {
1325 va_list argp;
1326 diagnostic_info diagnostic;
1327 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1328 bool ret;
1329
1330 va_start (argp, gmsgid);
1331 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc,
1332 DK_WARNING);
1333 diagnostic.option_index = opt;
1334 ret = gfc_report_diagnostic (&diagnostic);
1335 va_end (argp);
1336 return ret;
1337 }
1338
1339 /* Immediate error (i.e. do not buffer). */
1340
1341 void
gfc_error_now(const char * gmsgid,...)1342 gfc_error_now (const char *gmsgid, ...)
1343 {
1344 va_list argp;
1345 diagnostic_info diagnostic;
1346 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1347
1348 error_buffer.flag = true;
1349
1350 va_start (argp, gmsgid);
1351 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_ERROR);
1352 gfc_report_diagnostic (&diagnostic);
1353 va_end (argp);
1354 }
1355
1356
1357 /* Fatal error, never returns. */
1358
1359 void
gfc_fatal_error(const char * gmsgid,...)1360 gfc_fatal_error (const char *gmsgid, ...)
1361 {
1362 va_list argp;
1363 diagnostic_info diagnostic;
1364 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1365
1366 va_start (argp, gmsgid);
1367 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_FATAL);
1368 gfc_report_diagnostic (&diagnostic);
1369 va_end (argp);
1370
1371 gcc_unreachable ();
1372 }
1373
1374 /* Clear the warning flag. */
1375
1376 void
gfc_clear_warning(void)1377 gfc_clear_warning (void)
1378 {
1379 gfc_clear_pp_buffer (pp_warning_buffer);
1380 warningcount_buffered = 0;
1381 werrorcount_buffered = 0;
1382 }
1383
1384
1385 /* Check to see if any warnings have been saved.
1386 If so, print the warning. */
1387
1388 void
gfc_warning_check(void)1389 gfc_warning_check (void)
1390 {
1391 if (! gfc_output_buffer_empty_p (pp_warning_buffer))
1392 {
1393 pretty_printer *pp = global_dc->printer;
1394 output_buffer *tmp_buffer = pp->buffer;
1395 pp->buffer = pp_warning_buffer;
1396 pp_really_flush (pp);
1397 warningcount += warningcount_buffered;
1398 werrorcount += werrorcount_buffered;
1399 gcc_assert (warningcount_buffered + werrorcount_buffered == 1);
1400 pp->buffer = tmp_buffer;
1401 diagnostic_action_after_output (global_dc,
1402 warningcount_buffered
1403 ? DK_WARNING : DK_ERROR);
1404 diagnostic_check_max_errors (global_dc, true);
1405 }
1406 }
1407
1408
1409 /* Issue an error. */
1410
1411 static void
gfc_error_opt(int opt,const char * gmsgid,va_list ap)1412 gfc_error_opt (int opt, const char *gmsgid, va_list ap)
1413 {
1414 va_list argp;
1415 va_copy (argp, ap);
1416 bool saved_abort_on_error = false;
1417
1418 if (warnings_not_errors)
1419 {
1420 gfc_warning (opt, gmsgid, argp);
1421 va_end (argp);
1422 return;
1423 }
1424
1425 if (suppress_errors)
1426 {
1427 va_end (argp);
1428 return;
1429 }
1430
1431 diagnostic_info diagnostic;
1432 rich_location richloc (line_table, UNKNOWN_LOCATION);
1433 bool fatal_errors = global_dc->fatal_errors;
1434 pretty_printer *pp = global_dc->printer;
1435 output_buffer *tmp_buffer = pp->buffer;
1436
1437 gfc_clear_pp_buffer (pp_error_buffer);
1438
1439 if (buffered_p)
1440 {
1441 /* To prevent -dH from triggering an abort on a buffered error,
1442 save abort_on_error and restore it below. */
1443 saved_abort_on_error = global_dc->abort_on_error;
1444 global_dc->abort_on_error = false;
1445 pp->buffer = pp_error_buffer;
1446 global_dc->fatal_errors = false;
1447 /* To prevent -fmax-errors= triggering, we decrease it before
1448 report_diagnostic increases it. */
1449 --errorcount;
1450 }
1451
1452 diagnostic_set_info (&diagnostic, gmsgid, &argp, &richloc, DK_ERROR);
1453 gfc_report_diagnostic (&diagnostic);
1454
1455 if (buffered_p)
1456 {
1457 pp->buffer = tmp_buffer;
1458 global_dc->fatal_errors = fatal_errors;
1459 global_dc->abort_on_error = saved_abort_on_error;
1460
1461 }
1462
1463 va_end (argp);
1464 }
1465
1466
1467 void
gfc_error_opt(int opt,const char * gmsgid,...)1468 gfc_error_opt (int opt, const char *gmsgid, ...)
1469 {
1470 va_list argp;
1471 va_start (argp, gmsgid);
1472 gfc_error_opt (opt, gmsgid, argp);
1473 va_end (argp);
1474 }
1475
1476
1477 void
gfc_error(const char * gmsgid,...)1478 gfc_error (const char *gmsgid, ...)
1479 {
1480 va_list argp;
1481 va_start (argp, gmsgid);
1482 gfc_error_opt (0, gmsgid, argp);
1483 va_end (argp);
1484 }
1485
1486
1487 /* This shouldn't happen... but sometimes does. */
1488
1489 void
gfc_internal_error(const char * gmsgid,...)1490 gfc_internal_error (const char *gmsgid, ...)
1491 {
1492 int e, w;
1493 va_list argp;
1494 diagnostic_info diagnostic;
1495 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1496
1497 gfc_get_errors (&w, &e);
1498 if (e > 0)
1499 exit(EXIT_FAILURE);
1500
1501 va_start (argp, gmsgid);
1502 diagnostic_set_info (&diagnostic, gmsgid, &argp, &rich_loc, DK_ICE);
1503 gfc_report_diagnostic (&diagnostic);
1504 va_end (argp);
1505
1506 gcc_unreachable ();
1507 }
1508
1509
1510 /* Clear the error flag when we start to compile a source line. */
1511
1512 void
gfc_clear_error(void)1513 gfc_clear_error (void)
1514 {
1515 error_buffer.flag = false;
1516 warnings_not_errors = false;
1517 gfc_clear_pp_buffer (pp_error_buffer);
1518 }
1519
1520
1521 /* Tests the state of error_flag. */
1522
1523 bool
gfc_error_flag_test(void)1524 gfc_error_flag_test (void)
1525 {
1526 return error_buffer.flag
1527 || !gfc_output_buffer_empty_p (pp_error_buffer);
1528 }
1529
1530
1531 /* Check to see if any errors have been saved.
1532 If so, print the error. Returns the state of error_flag. */
1533
1534 bool
gfc_error_check(void)1535 gfc_error_check (void)
1536 {
1537 if (error_buffer.flag
1538 || ! gfc_output_buffer_empty_p (pp_error_buffer))
1539 {
1540 error_buffer.flag = false;
1541 pretty_printer *pp = global_dc->printer;
1542 output_buffer *tmp_buffer = pp->buffer;
1543 pp->buffer = pp_error_buffer;
1544 pp_really_flush (pp);
1545 ++errorcount;
1546 gcc_assert (gfc_output_buffer_empty_p (pp_error_buffer));
1547 pp->buffer = tmp_buffer;
1548 diagnostic_action_after_output (global_dc, DK_ERROR);
1549 diagnostic_check_max_errors (global_dc, true);
1550 return true;
1551 }
1552
1553 return false;
1554 }
1555
1556 /* Move the text buffered from FROM to TO, then clear
1557 FROM. Independently if there was text in FROM, TO is also
1558 cleared. */
1559
1560 static void
gfc_move_error_buffer_from_to(gfc_error_buffer * buffer_from,gfc_error_buffer * buffer_to)1561 gfc_move_error_buffer_from_to (gfc_error_buffer * buffer_from,
1562 gfc_error_buffer * buffer_to)
1563 {
1564 output_buffer * from = &(buffer_from->buffer);
1565 output_buffer * to = &(buffer_to->buffer);
1566
1567 buffer_to->flag = buffer_from->flag;
1568 buffer_from->flag = false;
1569
1570 gfc_clear_pp_buffer (to);
1571 /* We make sure this is always buffered. */
1572 to->flush_p = false;
1573
1574 if (! gfc_output_buffer_empty_p (from))
1575 {
1576 const char *str = output_buffer_formatted_text (from);
1577 output_buffer_append_r (to, str, strlen (str));
1578 gfc_clear_pp_buffer (from);
1579 }
1580 }
1581
1582 /* Save the existing error state. */
1583
1584 void
gfc_push_error(gfc_error_buffer * err)1585 gfc_push_error (gfc_error_buffer *err)
1586 {
1587 gfc_move_error_buffer_from_to (&error_buffer, err);
1588 }
1589
1590
1591 /* Restore a previous pushed error state. */
1592
1593 void
gfc_pop_error(gfc_error_buffer * err)1594 gfc_pop_error (gfc_error_buffer *err)
1595 {
1596 gfc_move_error_buffer_from_to (err, &error_buffer);
1597 }
1598
1599
1600 /* Free a pushed error state, but keep the current error state. */
1601
1602 void
gfc_free_error(gfc_error_buffer * err)1603 gfc_free_error (gfc_error_buffer *err)
1604 {
1605 gfc_clear_pp_buffer (&(err->buffer));
1606 }
1607
1608
1609 /* Report the number of warnings and errors that occurred to the caller. */
1610
1611 void
gfc_get_errors(int * w,int * e)1612 gfc_get_errors (int *w, int *e)
1613 {
1614 if (w != NULL)
1615 *w = warningcount + werrorcount;
1616 if (e != NULL)
1617 *e = errorcount + sorrycount + werrorcount;
1618 }
1619
1620
1621 /* Switch errors into warnings. */
1622
1623 void
gfc_errors_to_warnings(bool f)1624 gfc_errors_to_warnings (bool f)
1625 {
1626 warnings_not_errors = f;
1627 }
1628
1629 void
gfc_diagnostics_init(void)1630 gfc_diagnostics_init (void)
1631 {
1632 diagnostic_starter (global_dc) = gfc_diagnostic_starter;
1633 global_dc->start_span = gfc_diagnostic_start_span;
1634 diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
1635 diagnostic_format_decoder (global_dc) = gfc_format_decoder;
1636 global_dc->caret_chars[0] = '1';
1637 global_dc->caret_chars[1] = '2';
1638 pp_warning_buffer = new (XNEW (output_buffer)) output_buffer ();
1639 pp_warning_buffer->flush_p = false;
1640 /* pp_error_buffer is statically allocated. This simplifies memory
1641 management when using gfc_push/pop_error. */
1642 pp_error_buffer = &(error_buffer.buffer);
1643 pp_error_buffer->flush_p = false;
1644 }
1645
1646 void
gfc_diagnostics_finish(void)1647 gfc_diagnostics_finish (void)
1648 {
1649 tree_diagnostics_defaults (global_dc);
1650 /* We still want to use the gfc starter and finalizer, not the tree
1651 defaults. */
1652 diagnostic_starter (global_dc) = gfc_diagnostic_starter;
1653 diagnostic_finalizer (global_dc) = gfc_diagnostic_finalizer;
1654 global_dc->caret_chars[0] = '^';
1655 global_dc->caret_chars[1] = '^';
1656 }
1657