* gcc.dg/loop-4.c: New test.
[official-gcc.git] / gcc / fortran / error.c
blobb7b0fdb1bf6d9a37c53bc065de12c12a34767b9b
1 /* Handle errors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation,
3 Inc.
4 Contributed by Andy Vaught & Niels Kristian Bech Jensen
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 /* Handle the inevitable errors. A major catch here is that things
24 flagged as errors in one match subroutine can conceivably be legal
25 elsewhere. This means that error messages are recorded and saved
26 for possible use later. If a line does not match a legal
27 construction, then the saved error message is reported. */
29 #include "config.h"
30 #include "system.h"
32 #include <string.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
37 #include "flags.h"
38 #include "gfortran.h"
40 int gfc_suppress_error = 0;
42 static int terminal_width, buffer_flag, errors,
43 use_warning_buffer, warnings;
45 static char *error_ptr, *warning_ptr;
47 static gfc_error_buf error_buffer, warning_buffer;
50 /* Per-file error initialization. */
52 void
53 gfc_error_init_1 (void)
56 terminal_width = gfc_terminal_width();
57 errors = 0;
58 warnings = 0;
59 buffer_flag = 0;
63 /* Set the flag for buffering errors or not. */
65 void
66 gfc_buffer_error (int flag)
69 buffer_flag = flag;
73 /* Add a single character to the error buffer or output depending on
74 buffer_flag. */
76 static void
77 error_char (char c)
80 if (buffer_flag)
82 if (use_warning_buffer)
84 *warning_ptr++ = c;
85 if (warning_ptr - warning_buffer.message >= MAX_ERROR_MESSAGE)
86 gfc_internal_error ("error_char(): Warning buffer overflow");
88 else
90 *error_ptr++ = c;
91 if (error_ptr - error_buffer.message >= MAX_ERROR_MESSAGE)
92 gfc_internal_error ("error_char(): Error buffer overflow");
95 else
97 if (c != 0)
98 fputc (c, stderr);
103 /* Copy a string to wherever it needs to go. */
105 static void
106 error_string (const char *p)
109 while (*p)
110 error_char (*p++);
114 /* Show the file, where it was included and the source line give a
115 locus. Calls error_printf() recursively, but the recursion is at
116 most one level deep. */
118 static void error_printf (const char *, ...) ATTRIBUTE_PRINTF_1;
120 static void
121 show_locus (int offset, locus * loc)
123 gfc_linebuf *lb;
124 gfc_file *f;
125 char c, *p;
126 int i, m;
128 /* TODO: Either limit the total length and number of included files
129 displayed or add buffering of arbitrary number of characters in
130 error messages. */
132 lb = loc->lb;
133 f = lb->file;
134 error_printf ("In file %s:%d\n", f->filename, lb->linenum);
136 for (;;)
138 i = f->inclusion_line;
140 f = f->included_by;
141 if (f == NULL) break;
143 error_printf (" Included at %s:%d\n", f->filename, i);
146 /* Show the line itself, taking care not to print more than what can
147 show up on the terminal. Tabs are converted to spaces. */
149 p = lb->line + offset;
150 i = strlen (p);
151 if (i > terminal_width)
152 i = terminal_width - 1;
154 for (; i > 0; i--)
156 c = *p++;
157 if (c == '\t')
158 c = ' ';
160 if (ISPRINT (c))
161 error_char (c);
162 else
164 error_char ('\\');
165 error_char ('x');
167 m = ((c >> 4) & 0x0F) + '0';
168 if (m > '9')
169 m += 'A' - '9' - 1;
170 error_char (m);
172 m = (c & 0x0F) + '0';
173 if (m > '9')
174 m += 'A' - '9' - 1;
175 error_char (m);
179 error_char ('\n');
183 /* As part of printing an error, we show the source lines that caused
184 the problem. We show at least one, possibly two loci. If we're
185 showing two loci and they both refer to the same file and line, we
186 only print the line once. */
188 static void
189 show_loci (locus * l1, locus * l2)
191 int offset, flag, i, m, c1, c2, cmax;
193 if (l1 == NULL)
195 error_printf ("<During initialization>\n");
196 return;
199 c1 = l1->nextc - l1->lb->line;
200 c2 = 0;
201 if (l2 == NULL)
202 goto separate;
204 c2 = l2->nextc - l2->lb->line;
206 if (c1 < c2)
207 m = c2 - c1;
208 else
209 m = c1 - c2;
212 if (l1->lb != l2->lb || m > terminal_width - 10)
213 goto separate;
215 offset = 0;
216 cmax = (c1 < c2) ? c2 : c1;
217 if (cmax > terminal_width - 5)
218 offset = cmax - terminal_width + 5;
220 if (offset < 0)
221 offset = 0;
223 c1 -= offset;
224 c2 -= offset;
226 show_locus (offset, l1);
228 /* Arrange that '1' and '2' will show up even if the two columns are equal. */
229 for (i = 1; i <= cmax; i++)
231 flag = 0;
232 if (i == c1)
234 error_char ('1');
235 flag = 1;
237 if (i == c2)
239 error_char ('2');
240 flag = 1;
242 if (flag == 0)
243 error_char (' ');
246 error_char ('\n');
248 return;
250 separate:
251 offset = 0;
253 if (c1 > terminal_width - 5)
255 offset = c1 - 5;
256 if (offset < 0)
257 offset = 0;
258 c1 = c1 - offset;
261 show_locus (offset, l1);
262 for (i = 1; i < c1; i++)
263 error_char (' ');
265 error_char ('1');
266 error_char ('\n');
268 if (l2 != NULL)
270 offset = 0;
272 if (c2 > terminal_width - 20)
274 offset = c2 - 20;
275 if (offset < 0)
276 offset = 0;
277 c2 = c2 - offset;
280 show_locus (offset, l2);
282 for (i = 1; i < c2; i++)
283 error_char (' ');
285 error_char ('2');
286 error_char ('\n');
291 /* Workhorse for the error printing subroutines. This subroutine is
292 inspired by g77's error handling and is similar to printf() with
293 the following %-codes:
295 %c Character, %d Integer, %s String, %% Percent
296 %L Takes locus argument
297 %C Current locus (no argument)
299 If a locus pointer is given, the actual source line is printed out
300 and the column is indicated. Since we want the error message at
301 the bottom of any source file information, we must scan the
302 argument list twice. A maximum of two locus arguments are
303 permitted. */
305 #define IBUF_LEN 30
306 #define MAX_ARGS 10
308 static void
309 error_print (const char *type, const char *format0, va_list argp)
311 char c, *p, int_buf[IBUF_LEN], c_arg[MAX_ARGS], *cp_arg[MAX_ARGS];
312 int i, n, have_l1, i_arg[MAX_ARGS];
313 locus *l1, *l2, *loc;
314 const char *format;
316 l1 = l2 = loc = NULL;
318 have_l1 = 0;
320 n = 0;
321 format = format0;
323 while (*format)
325 c = *format++;
326 if (c == '%')
328 c = *format++;
330 switch (c)
332 case '%':
333 break;
335 case 'L':
336 loc = va_arg (argp, locus *);
337 /* Fall through */
339 case 'C':
340 if (c == 'C')
341 loc = gfc_current_locus ();
343 if (have_l1)
345 l2 = loc;
347 else
349 l1 = loc;
350 have_l1 = 1;
352 break;
354 case 'd':
355 case 'i':
356 i_arg[n++] = va_arg (argp, int);
357 break;
359 case 'c':
360 c_arg[n++] = va_arg (argp, int);
361 break;
363 case 's':
364 cp_arg[n++] = va_arg (argp, char *);
365 break;
370 /* Show the current loci if we have to. */
371 if (have_l1)
372 show_loci (l1, l2);
373 error_string (type);
374 error_char (' ');
376 have_l1 = 0;
377 format = format0;
378 n = 0;
380 for (; *format; format++)
382 if (*format != '%')
384 error_char (*format);
385 continue;
388 format++;
389 switch (*format)
391 case '%':
392 error_char ('%');
393 break;
395 case 'c':
396 error_char (c_arg[n++]);
397 break;
399 case 's':
400 error_string (cp_arg[n++]);
401 break;
403 case 'i':
404 case 'd':
405 i = i_arg[n++];
407 if (i < 0)
409 i = -i;
410 error_char ('-');
413 p = int_buf + IBUF_LEN - 1;
414 *p-- = '\0';
416 if (i == 0)
417 *p-- = '0';
419 while (i > 0)
421 *p-- = i % 10 + '0';
422 i = i / 10;
425 error_string (p + 1);
426 break;
428 case 'C': /* Current locus */
429 case 'L': /* Specified locus */
430 error_string (have_l1 ? "(2)" : "(1)");
431 have_l1 = 1;
432 break;
436 error_char ('\n');
440 /* Wrapper for error_print(). */
442 static void
443 error_printf (const char *format, ...)
445 va_list argp;
447 va_start (argp, format);
448 error_print ("", format, argp);
449 va_end (argp);
453 /* Issue a warning. */
455 void
456 gfc_warning (const char *format, ...)
458 va_list argp;
460 if (inhibit_warnings)
461 return;
463 warning_buffer.flag = 1;
464 warning_ptr = warning_buffer.message;
465 use_warning_buffer = 1;
467 va_start (argp, format);
468 if (buffer_flag == 0)
469 warnings++;
470 error_print ("Warning:", format, argp);
471 va_end (argp);
473 error_char ('\0');
477 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
478 feature. An error/warning will be issued if the currently selected
479 standard does not contain the requested bits. Return FAILURE if
480 and error is generated. */
483 gfc_notify_std (int std, const char *format, ...)
485 va_list argp;
486 bool warning;
488 warning = ((gfc_option.warn_std & std) != 0)
489 && !inhibit_warnings;
490 if ((gfc_option.allow_std & std) != 0
491 && !warning)
492 return SUCCESS;
494 if (gfc_suppress_error)
495 return warning ? SUCCESS : FAILURE;
497 if (warning)
499 warning_buffer.flag = 1;
500 warning_ptr = warning_buffer.message;
501 use_warning_buffer = 1;
503 else
505 error_buffer.flag = 1;
506 error_ptr = error_buffer.message;
507 use_warning_buffer = 0;
510 if (buffer_flag == 0)
512 if (warning)
513 warnings++;
514 else
515 errors++;
517 va_start (argp, format);
518 if (warning)
519 error_print ("Warning:", format, argp);
520 else
521 error_print ("Error:", format, argp);
522 va_end (argp);
524 error_char ('\0');
525 return warning ? SUCCESS : FAILURE;
529 /* Immediate warning (i.e. do not buffer the warning). */
531 void
532 gfc_warning_now (const char *format, ...)
534 va_list argp;
535 int i;
537 if (inhibit_warnings)
538 return;
540 i = buffer_flag;
541 buffer_flag = 0;
542 warnings++;
544 va_start (argp, format);
545 error_print ("Warning:", format, argp);
546 va_end (argp);
548 error_char ('\0');
549 buffer_flag = i;
553 /* Clear the warning flag. */
555 void
556 gfc_clear_warning (void)
559 warning_buffer.flag = 0;
563 /* Check to see if any warnings have been saved.
564 If so, print the warning. */
566 void
567 gfc_warning_check (void)
570 if (warning_buffer.flag)
572 warnings++;
573 fputs (warning_buffer.message, stderr);
574 warning_buffer.flag = 0;
579 /* Issue an error. */
581 void
582 gfc_error (const char *format, ...)
584 va_list argp;
586 if (gfc_suppress_error)
587 return;
589 error_buffer.flag = 1;
590 error_ptr = error_buffer.message;
591 use_warning_buffer = 0;
593 va_start (argp, format);
594 if (buffer_flag == 0)
595 errors++;
596 error_print ("Error:", format, argp);
597 va_end (argp);
599 error_char ('\0');
603 /* Immediate error. */
605 void
606 gfc_error_now (const char *format, ...)
608 va_list argp;
609 int i;
611 error_buffer.flag = 1;
612 error_ptr = error_buffer.message;
614 i = buffer_flag;
615 buffer_flag = 0;
616 errors++;
618 va_start (argp, format);
619 error_print ("Error:", format, argp);
620 va_end (argp);
622 error_char ('\0');
623 buffer_flag = i;
627 /* Fatal error, never returns. */
629 void
630 gfc_fatal_error (const char *format, ...)
632 va_list argp;
634 buffer_flag = 0;
636 va_start (argp, format);
637 error_print ("Fatal Error:", format, argp);
638 va_end (argp);
640 exit (3);
644 /* This shouldn't happen... but sometimes does. */
646 void
647 gfc_internal_error (const char *format, ...)
649 va_list argp;
651 buffer_flag = 0;
653 va_start (argp, format);
655 show_loci (gfc_current_locus (), NULL);
656 error_printf ("Internal Error at (1):");
658 error_print ("", format, argp);
659 va_end (argp);
661 exit (4);
665 /* Clear the error flag when we start to compile a source line. */
667 void
668 gfc_clear_error (void)
671 error_buffer.flag = 0;
675 /* Check to see if any errors have been saved.
676 If so, print the error. Returns the state of error_flag. */
679 gfc_error_check (void)
681 int rc;
683 rc = error_buffer.flag;
685 if (error_buffer.flag)
687 errors++;
688 fputs (error_buffer.message, stderr);
689 error_buffer.flag = 0;
692 return rc;
696 /* Save the existing error state. */
698 void
699 gfc_push_error (gfc_error_buf * err)
702 err->flag = error_buffer.flag;
703 if (error_buffer.flag)
704 strcpy (err->message, error_buffer.message);
706 error_buffer.flag = 0;
710 /* Restore a previous pushed error state. */
712 void
713 gfc_pop_error (gfc_error_buf * err)
716 error_buffer.flag = err->flag;
717 if (error_buffer.flag)
718 strcpy (error_buffer.message, err->message);
722 /* Debug wrapper for printf. */
724 void
725 gfc_status (const char *format, ...)
727 va_list argp;
729 va_start (argp, format);
731 vprintf (format, argp);
733 va_end (argp);
737 /* Subroutine for outputting a single char so that we don't have to go
738 around creating a lot of 1-character strings. */
740 void
741 gfc_status_char (char c)
743 putchar (c);
747 /* Report the number of warnings and errors that occored to the caller. */
749 void
750 gfc_get_errors (int *w, int *e)
753 if (w != NULL)
754 *w = warnings;
755 if (e != NULL)
756 *e = errors;