]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/runtime/error.c
1 /* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
37 #include "libgfortran.h"
40 /* Error conditions. The tricky part here is printing a message when
41 * it is the I/O subsystem that is severely wounded. Our goal is to
42 * try and print something making the fewest assumptions possible,
43 * then try to clean up before actually exiting.
45 * The following exit conditions are defined:
46 * 0 Normal program exit.
47 * 1 Terminated because of operating system error.
48 * 2 Error in the runtime library
49 * 3 Internal error in runtime library
50 * 4 Error during error processing (very bad)
52 * Other error returns are reserved for the STOP statement with a numeric code.
55 /* locus variables. These are optionally set by a caller before a
56 * library subroutine is called. They are always cleared on exit so
57 * that files that report loci and those that do not can be linked
58 * together without reporting an erroneous position. */
61 iexport_data(filename
);
66 /* buffer for integer/ascii conversions. */
67 static char buffer
[sizeof (GFC_UINTEGER_LARGEST
) * 8 + 1];
70 /* Returns a pointer to a static buffer. */
73 gfc_itoa (GFC_INTEGER_LARGEST n
)
77 GFC_UINTEGER_LARGEST t
;
91 t
= -n
; /*must use unsigned to protect from overflow*/
94 p
= buffer
+ sizeof (buffer
) - 1;
99 *p
-- = '0' + (t
% 10);
109 /* xtoa()-- Integer to hexadecimal conversion. Returns a pointer to a
113 xtoa (GFC_UINTEGER_LARGEST n
)
125 p
= buffer
+ sizeof (buffer
) - 1;
132 digit
+= 'A' - '0' - 10;
142 /* st_printf()-- simple printf() function for streams that handles the
143 * formats %d, %s and %c. This function handles printing of error
144 * messages that originate within the library itself, not from a user
148 st_printf (const char *format
, ...)
156 s
= init_error_stream ();
157 va_start (arg
, format
);
163 while (format
[count
] != '%' && format
[count
] != '\0')
168 p
= salloc_w (s
, &count
);
169 memmove (p
, format
, count
);
175 if (*format
++ == '\0')
183 p
= salloc_w (s
, &count
);
184 *p
= (char) va_arg (arg
, int);
190 q
= gfc_itoa (va_arg (arg
, int));
193 p
= salloc_w (s
, &count
);
194 memmove (p
, q
, count
);
199 q
= xtoa (va_arg (arg
, unsigned));
202 p
= salloc_w (s
, &count
);
203 memmove (p
, q
, count
);
208 q
= va_arg (arg
, char *);
211 p
= salloc_w (s
, &count
);
212 memmove (p
, q
, count
);
221 p
= salloc_w (s
, &count
);
237 /* st_sprintf()-- Simple sprintf() for formatting memory buffers. */
240 st_sprintf (char *buffer
, const char *format
, ...)
246 va_start (arg
, format
);
263 *buffer
++ = (char) va_arg (arg
, int);
267 p
= gfc_itoa (va_arg (arg
, int));
270 memcpy (buffer
, p
, count
);
275 p
= va_arg (arg
, char *);
278 memcpy (buffer
, p
, count
);
291 /* show_locus()-- Print a line number and filename describing where
292 * something went wrong */
297 if (!options
.locus
|| filename
== NULL
)
300 st_printf ("At line %d of file %s\n", line
, filename
);
304 /* recursion_check()-- It's possible for additional errors to occur
305 * during fatal error processing. We detect this condition here and
306 * exit with code 4 immediately. */
308 #define MAGIC 0x20DE8101
311 recursion_check (void)
313 static int magic
= 0;
315 /* Don't even try to print something at this point */
323 /* os_error()-- Operating system error. We get a message from the
324 * operating system, show it and leave. Some operating system errors
325 * are caught and processed by the library. If not, we come here. */
328 os_error (const char *message
)
332 st_printf ("Operating system error: %s\n%s\n", get_oserror (), message
);
337 /* void runtime_error()-- These are errors associated with an
338 * invalid fortran program. */
341 runtime_error (const char *message
)
345 st_printf ("Fortran runtime error: %s\n", message
);
348 iexport(runtime_error
);
351 /* void internal_error()-- These are this-can't-happen errors
352 * that indicate something deeply wrong. */
355 internal_error (const char *message
)
359 st_printf ("Internal Error: %s\n", message
);
364 /* translate_error()-- Given an integer error code, return a string
365 * describing the error. */
368 translate_error (int code
)
383 p
= "Successful return";
387 p
= "Operating system error";
390 case ERROR_BAD_OPTION
:
391 p
= "Bad statement option";
394 case ERROR_MISSING_OPTION
:
395 p
= "Missing statement option";
398 case ERROR_OPTION_CONFLICT
:
399 p
= "Conflicting statement options";
402 case ERROR_ALREADY_OPEN
:
403 p
= "File already opened in another unit";
407 p
= "Unattached unit";
414 case ERROR_BAD_ACTION
:
415 p
= "Incorrect ACTION specified";
419 p
= "Read past ENDFILE record";
423 p
= "Corrupt unformatted sequential file";
426 case ERROR_READ_VALUE
:
427 p
= "Bad value during read";
430 case ERROR_READ_OVERFLOW
:
431 p
= "Numeric overflow on read";
435 p
= "Unknown error code";
443 /* generate_error()-- Come here when an error happens. This
444 * subroutine is called if it is possible to continue on after the error.
445 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
446 * ERR labels are present, we return, otherwise we terminate the program
447 * after printing a message. The error code is always required but the
448 * message parameter can be NULL, in which case a string describing
449 * the most recent operating system error is used. */
452 generate_error (int family
, const char *message
)
454 /* Set the error status. */
455 if (ioparm
.iostat
!= NULL
)
456 *ioparm
.iostat
= family
;
460 (family
== ERROR_OS
) ? get_oserror () : translate_error (family
);
463 cf_strcpy (ioparm
.iomsg
, ioparm
.iomsg_len
, message
);
465 /* Report status back to the compiler. */
469 ioparm
.library_return
= LIBRARY_EOR
;
475 ioparm
.library_return
= LIBRARY_END
;
481 ioparm
.library_return
= LIBRARY_ERROR
;
487 /* Return if the user supplied an iostat variable. */
488 if (ioparm
.iostat
!= NULL
)
491 /* Terminate the program */
493 runtime_error (message
);
498 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
499 feature. An error/warning will be issued if the currently selected
500 standard does not contain the requested bits. */
503 notify_std (int std
, const char * message
)
507 warning
= compile_options
.warn_std
& std
;
508 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
514 st_printf ("Fortran runtime error: %s\n", message
);
518 st_printf ("Fortran runtime warning: %s\n", message
);