Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / fortran / error.c
blobdd56aebac6e53545e1d2eeb785b57e7f80aaa268
1 /* Handle errors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 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"
31 #include "flags.h"
32 #include "gfortran.h"
34 int gfc_suppress_error = 0;
36 static int terminal_width, buffer_flag, errors,
37 use_warning_buffer, warnings;
39 static char *error_ptr, *warning_ptr;
41 static gfc_error_buf error_buffer, warning_buffer;
44 /* Per-file error initialization. */
46 void
47 gfc_error_init_1 (void)
49 terminal_width = gfc_terminal_width ();
50 errors = 0;
51 warnings = 0;
52 buffer_flag = 0;
56 /* Set the flag for buffering errors or not. */
58 void
59 gfc_buffer_error (int flag)
61 buffer_flag = flag;
65 /* Add a single character to the error buffer or output depending on
66 buffer_flag. */
68 static void
69 error_char (char c)
71 if (buffer_flag)
73 if (use_warning_buffer)
75 *warning_ptr++ = c;
76 if (warning_ptr - warning_buffer.message >= MAX_ERROR_MESSAGE)
77 gfc_internal_error ("error_char(): Warning buffer overflow");
79 else
81 *error_ptr++ = c;
82 if (error_ptr - error_buffer.message >= MAX_ERROR_MESSAGE)
83 gfc_internal_error ("error_char(): Error buffer overflow");
86 else
88 if (c != 0)
90 /* We build up complete lines before handing things
91 over to the library in order to speed up error printing. */
92 static char line[MAX_ERROR_MESSAGE + 1];
93 static int index = 0;
95 line[index++] = c;
96 if (c == '\n' || index == MAX_ERROR_MESSAGE)
98 line[index] = '\0';
99 fputs (line, stderr);
100 index = 0;
107 /* Copy a string to wherever it needs to go. */
109 static void
110 error_string (const char *p)
112 while (*p)
113 error_char (*p++);
117 /* Show the file, where it was included and the source line, give a
118 locus. Calls error_printf() recursively, but the recursion is at
119 most one level deep. */
121 static void error_printf (const char *, ...) ATTRIBUTE_PRINTF_1;
123 static void
124 show_locus (int offset, locus * loc)
126 gfc_linebuf *lb;
127 gfc_file *f;
128 char c, *p;
129 int i, m;
131 /* TODO: Either limit the total length and number of included files
132 displayed or add buffering of arbitrary number of characters in
133 error messages. */
135 lb = loc->lb;
136 f = lb->file;
137 error_printf ("In file %s:%d\n", f->filename,
138 #ifdef USE_MAPPED_LOCATION
139 LOCATION_LINE (lb->location)
140 #else
141 lb->linenum
142 #endif
145 for (;;)
147 i = f->inclusion_line;
149 f = f->included_by;
150 if (f == NULL) break;
152 error_printf (" Included at %s:%d\n", f->filename, i);
155 /* Show the line itself, taking care not to print more than what can
156 show up on the terminal. Tabs are converted to spaces. */
158 p = lb->line + offset;
159 i = strlen (p);
160 if (i > terminal_width)
161 i = terminal_width - 1;
163 for (; i > 0; i--)
165 c = *p++;
166 if (c == '\t')
167 c = ' ';
169 if (ISPRINT (c))
170 error_char (c);
171 else
173 error_char ('\\');
174 error_char ('x');
176 m = ((c >> 4) & 0x0F) + '0';
177 if (m > '9')
178 m += 'A' - '9' - 1;
179 error_char (m);
181 m = (c & 0x0F) + '0';
182 if (m > '9')
183 m += 'A' - '9' - 1;
184 error_char (m);
188 error_char ('\n');
192 /* As part of printing an error, we show the source lines that caused
193 the problem. We show at least one, possibly two loci. If we're
194 showing two loci and they both refer to the same file and line, we
195 only print the line once. */
197 static void
198 show_loci (locus * l1, locus * l2)
200 int offset, flag, i, m, c1, c2, cmax;
202 if (l1 == NULL)
204 error_printf ("<During initialization>\n");
205 return;
208 c1 = l1->nextc - l1->lb->line;
209 c2 = 0;
210 if (l2 == NULL)
211 goto separate;
213 c2 = l2->nextc - l2->lb->line;
215 if (c1 < c2)
216 m = c2 - c1;
217 else
218 m = c1 - c2;
221 if (l1->lb != l2->lb || m > terminal_width - 10)
222 goto separate;
224 offset = 0;
225 cmax = (c1 < c2) ? c2 : c1;
226 if (cmax > terminal_width - 5)
227 offset = cmax - terminal_width + 5;
229 if (offset < 0)
230 offset = 0;
232 c1 -= offset;
233 c2 -= offset;
235 show_locus (offset, l1);
237 /* Arrange that '1' and '2' will show up even if the two columns are equal. */
238 for (i = 1; i <= cmax; i++)
240 flag = 0;
241 if (i == c1)
243 error_char ('1');
244 flag = 1;
246 if (i == c2)
248 error_char ('2');
249 flag = 1;
251 if (flag == 0)
252 error_char (' ');
255 error_char ('\n');
257 return;
259 separate:
260 offset = 0;
262 if (c1 > terminal_width - 5)
264 offset = c1 - 5;
265 if (offset < 0)
266 offset = 0;
267 c1 = c1 - offset;
270 show_locus (offset, l1);
271 for (i = 1; i < c1; i++)
272 error_char (' ');
274 error_char ('1');
275 error_char ('\n');
277 if (l2 != NULL)
279 offset = 0;
281 if (c2 > terminal_width - 20)
283 offset = c2 - 20;
284 if (offset < 0)
285 offset = 0;
286 c2 = c2 - offset;
289 show_locus (offset, l2);
291 for (i = 1; i < c2; i++)
292 error_char (' ');
294 error_char ('2');
295 error_char ('\n');
300 /* Workhorse for the error printing subroutines. This subroutine is
301 inspired by g77's error handling and is similar to printf() with
302 the following %-codes:
304 %c Character, %d Integer, %s String, %% Percent
305 %L Takes locus argument
306 %C Current locus (no argument)
308 If a locus pointer is given, the actual source line is printed out
309 and the column is indicated. Since we want the error message at
310 the bottom of any source file information, we must scan the
311 argument list twice. A maximum of two locus arguments are
312 permitted. */
314 #define IBUF_LEN 30
315 #define MAX_ARGS 10
317 static void
318 error_print (const char *type, const char *format0, va_list argp)
320 char c, *p, int_buf[IBUF_LEN], c_arg[MAX_ARGS], *cp_arg[MAX_ARGS];
321 int i, n, have_l1, i_arg[MAX_ARGS];
322 locus *l1, *l2, *loc;
323 const char *format;
325 l1 = l2 = loc = NULL;
327 have_l1 = 0;
329 n = 0;
330 format = format0;
332 while (*format)
334 c = *format++;
335 if (c == '%')
337 c = *format++;
339 switch (c)
341 case '%':
342 break;
344 case 'L':
345 loc = va_arg (argp, locus *);
346 /* Fall through */
348 case 'C':
349 if (c == 'C')
350 loc = &gfc_current_locus;
352 if (have_l1)
354 l2 = loc;
356 else
358 l1 = loc;
359 have_l1 = 1;
361 break;
363 case 'd':
364 case 'i':
365 i_arg[n++] = va_arg (argp, int);
366 break;
368 case 'c':
369 c_arg[n++] = va_arg (argp, int);
370 break;
372 case 's':
373 cp_arg[n++] = va_arg (argp, char *);
374 break;
379 /* Show the current loci if we have to. */
380 if (have_l1)
381 show_loci (l1, l2);
382 error_string (type);
383 error_char (' ');
385 have_l1 = 0;
386 format = format0;
387 n = 0;
389 for (; *format; format++)
391 if (*format != '%')
393 error_char (*format);
394 continue;
397 format++;
398 switch (*format)
400 case '%':
401 error_char ('%');
402 break;
404 case 'c':
405 error_char (c_arg[n++]);
406 break;
408 case 's':
409 error_string (cp_arg[n++]);
410 break;
412 case 'i':
413 case 'd':
414 i = i_arg[n++];
416 if (i < 0)
418 i = -i;
419 error_char ('-');
422 p = int_buf + IBUF_LEN - 1;
423 *p-- = '\0';
425 if (i == 0)
426 *p-- = '0';
428 while (i > 0)
430 *p-- = i % 10 + '0';
431 i = i / 10;
434 error_string (p + 1);
435 break;
437 case 'C': /* Current locus */
438 case 'L': /* Specified locus */
439 error_string (have_l1 ? "(2)" : "(1)");
440 have_l1 = 1;
441 break;
445 error_char ('\n');
449 /* Wrapper for error_print(). */
451 static void
452 error_printf (const char *format, ...)
454 va_list argp;
456 va_start (argp, format);
457 error_print ("", format, argp);
458 va_end (argp);
462 /* Issue a warning. */
464 void
465 gfc_warning (const char *format, ...)
467 va_list argp;
469 if (inhibit_warnings)
470 return;
472 warning_buffer.flag = 1;
473 warning_ptr = warning_buffer.message;
474 use_warning_buffer = 1;
476 va_start (argp, format);
477 if (buffer_flag == 0)
478 warnings++;
479 error_print ("Warning:", format, argp);
480 va_end (argp);
482 error_char ('\0');
486 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
487 feature. An error/warning will be issued if the currently selected
488 standard does not contain the requested bits. Return FAILURE if
489 and error is generated. */
492 gfc_notify_std (int std, const char *format, ...)
494 va_list argp;
495 bool warning;
497 warning = ((gfc_option.warn_std & std) != 0)
498 && !inhibit_warnings;
499 if ((gfc_option.allow_std & std) != 0
500 && !warning)
501 return SUCCESS;
503 if (gfc_suppress_error)
504 return warning ? SUCCESS : FAILURE;
506 if (warning)
508 warning_buffer.flag = 1;
509 warning_ptr = warning_buffer.message;
510 use_warning_buffer = 1;
512 else
514 error_buffer.flag = 1;
515 error_ptr = error_buffer.message;
516 use_warning_buffer = 0;
519 if (buffer_flag == 0)
521 if (warning)
522 warnings++;
523 else
524 errors++;
526 va_start (argp, format);
527 if (warning)
528 error_print ("Warning:", format, argp);
529 else
530 error_print ("Error:", format, argp);
531 va_end (argp);
533 error_char ('\0');
534 return warning ? SUCCESS : FAILURE;
538 /* Immediate warning (i.e. do not buffer the warning). */
540 void
541 gfc_warning_now (const char *format, ...)
543 va_list argp;
544 int i;
546 if (inhibit_warnings)
547 return;
549 i = buffer_flag;
550 buffer_flag = 0;
551 warnings++;
553 va_start (argp, format);
554 error_print ("Warning:", format, argp);
555 va_end (argp);
557 error_char ('\0');
558 buffer_flag = i;
562 /* Clear the warning flag. */
564 void
565 gfc_clear_warning (void)
567 warning_buffer.flag = 0;
571 /* Check to see if any warnings have been saved.
572 If so, print the warning. */
574 void
575 gfc_warning_check (void)
577 if (warning_buffer.flag)
579 warnings++;
580 fputs (warning_buffer.message, stderr);
581 warning_buffer.flag = 0;
586 /* Issue an error. */
588 void
589 gfc_error (const char *format, ...)
591 va_list argp;
593 if (gfc_suppress_error)
594 return;
596 error_buffer.flag = 1;
597 error_ptr = error_buffer.message;
598 use_warning_buffer = 0;
600 va_start (argp, format);
601 if (buffer_flag == 0)
602 errors++;
603 error_print ("Error:", format, argp);
604 va_end (argp);
606 error_char ('\0');
610 /* Immediate error. */
612 void
613 gfc_error_now (const char *format, ...)
615 va_list argp;
616 int i;
618 error_buffer.flag = 1;
619 error_ptr = error_buffer.message;
621 i = buffer_flag;
622 buffer_flag = 0;
623 errors++;
625 va_start (argp, format);
626 error_print ("Error:", format, argp);
627 va_end (argp);
629 error_char ('\0');
630 buffer_flag = i;
634 /* Fatal error, never returns. */
636 void
637 gfc_fatal_error (const char *format, ...)
639 va_list argp;
641 buffer_flag = 0;
643 va_start (argp, format);
644 error_print ("Fatal Error:", format, argp);
645 va_end (argp);
647 exit (3);
651 /* This shouldn't happen... but sometimes does. */
653 void
654 gfc_internal_error (const char *format, ...)
656 va_list argp;
658 buffer_flag = 0;
660 va_start (argp, format);
662 show_loci (&gfc_current_locus, NULL);
663 error_printf ("Internal Error at (1):");
665 error_print ("", format, argp);
666 va_end (argp);
668 exit (4);
672 /* Clear the error flag when we start to compile a source line. */
674 void
675 gfc_clear_error (void)
677 error_buffer.flag = 0;
681 /* Check to see if any errors have been saved.
682 If so, print the error. Returns the state of error_flag. */
685 gfc_error_check (void)
687 int rc;
689 rc = error_buffer.flag;
691 if (error_buffer.flag)
693 errors++;
694 fputs (error_buffer.message, stderr);
695 error_buffer.flag = 0;
698 return rc;
702 /* Save the existing error state. */
704 void
705 gfc_push_error (gfc_error_buf * err)
707 err->flag = error_buffer.flag;
708 if (error_buffer.flag)
709 strcpy (err->message, error_buffer.message);
711 error_buffer.flag = 0;
715 /* Restore a previous pushed error state. */
717 void
718 gfc_pop_error (gfc_error_buf * err)
720 error_buffer.flag = err->flag;
721 if (error_buffer.flag)
722 strcpy (error_buffer.message, err->message);
726 /* Debug wrapper for printf. */
728 void
729 gfc_status (const char *format, ...)
731 va_list argp;
733 va_start (argp, format);
735 vprintf (format, argp);
737 va_end (argp);
741 /* Subroutine for outputting a single char so that we don't have to go
742 around creating a lot of 1-character strings. */
744 void
745 gfc_status_char (char c)
747 putchar (c);
751 /* Report the number of warnings and errors that occurred to the caller. */
753 void
754 gfc_get_errors (int *w, int *e)
756 if (w != NULL)
757 *w = warnings;
758 if (e != NULL)
759 *e = errors;