]>
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. */
38 #include "libgfortran.h"
40 #include "../io/unix.h"
42 /* Error conditions. The tricky part here is printing a message when
43 * it is the I/O subsystem that is severely wounded. Our goal is to
44 * try and print something making the fewest assumptions possible,
45 * then try to clean up before actually exiting.
47 * The following exit conditions are defined:
48 * 0 Normal program exit.
49 * 1 Terminated because of operating system error.
50 * 2 Error in the runtime library
51 * 3 Internal error in runtime library
52 * 4 Error during error processing (very bad)
54 * Other error returns are reserved for the STOP statement with a numeric code.
57 /* gfc_itoa()-- Integer to decimal conversion. */
60 gfc_itoa (GFC_INTEGER_LARGEST n
, char *buffer
, size_t len
)
64 GFC_UINTEGER_LARGEST t
;
66 assert (len
>= GFC_ITOA_BUF_SIZE
);
76 t
= -n
; /*must use unsigned to protect from overflow*/
79 p
= buffer
+ GFC_ITOA_BUF_SIZE
- 1;
84 *--p
= '0' + (t
% 10);
94 /* xtoa()-- Integer to hexadecimal conversion. */
97 xtoa (GFC_UINTEGER_LARGEST n
, char *buffer
, size_t len
)
102 assert (len
>= GFC_XTOA_BUF_SIZE
);
107 p
= buffer
+ GFC_XTOA_BUF_SIZE
- 1;
114 digit
+= 'A' - '0' - 10;
124 /* st_printf()-- simple printf() function for streams that handles the
125 * formats %d, %s and %c. This function handles printing of error
126 * messages that originate within the library itself, not from a user
130 st_printf (const char *format
, ...)
137 char itoa_buf
[GFC_ITOA_BUF_SIZE
];
138 unix_stream err_stream
;
141 s
= init_error_stream (&err_stream
);
142 va_start (arg
, format
);
148 while (format
[count
] != '%' && format
[count
] != '\0')
153 p
= salloc_w (s
, &count
);
154 memmove (p
, format
, count
);
160 if (*format
++ == '\0')
168 p
= salloc_w (s
, &count
);
169 *p
= (char) va_arg (arg
, int);
175 q
= gfc_itoa (va_arg (arg
, int), itoa_buf
, sizeof (itoa_buf
));
178 p
= salloc_w (s
, &count
);
179 memmove (p
, q
, count
);
184 q
= xtoa (va_arg (arg
, unsigned), itoa_buf
, sizeof (itoa_buf
));
187 p
= salloc_w (s
, &count
);
188 memmove (p
, q
, count
);
193 q
= va_arg (arg
, char *);
196 p
= salloc_w (s
, &count
);
197 memmove (p
, q
, count
);
206 p
= salloc_w (s
, &count
);
222 /* st_sprintf()-- Simple sprintf() for formatting memory buffers. */
225 st_sprintf (char *buffer
, const char *format
, ...)
231 char itoa_buf
[GFC_ITOA_BUF_SIZE
];
233 va_start (arg
, format
);
250 *buffer
++ = (char) va_arg (arg
, int);
254 p
= gfc_itoa (va_arg (arg
, int), itoa_buf
, sizeof (itoa_buf
));
257 memcpy (buffer
, p
, count
);
262 p
= va_arg (arg
, char *);
265 memcpy (buffer
, p
, count
);
278 /* show_locus()-- Print a line number and filename describing where
279 * something went wrong */
282 show_locus (st_parameter_common
*cmp
)
284 if (!options
.locus
|| cmp
== NULL
|| cmp
->filename
== NULL
)
287 st_printf ("At line %d of file %s\n", cmp
->line
, cmp
->filename
);
291 /* recursion_check()-- It's possible for additional errors to occur
292 * during fatal error processing. We detect this condition here and
293 * exit with code 4 immediately. */
295 #define MAGIC 0x20DE8101
298 recursion_check (void)
300 static int magic
= 0;
302 /* Don't even try to print something at this point */
310 /* os_error()-- Operating system error. We get a message from the
311 * operating system, show it and leave. Some operating system errors
312 * are caught and processed by the library. If not, we come here. */
315 os_error (const char *message
)
318 st_printf ("Operating system error: %s\n%s\n", get_oserror (), message
);
323 /* void runtime_error()-- These are errors associated with an
324 * invalid fortran program. */
327 runtime_error (const char *message
)
330 st_printf ("Fortran runtime error: %s\n", message
);
333 iexport(runtime_error
);
336 /* void internal_error()-- These are this-can't-happen errors
337 * that indicate something deeply wrong. */
340 internal_error (st_parameter_common
*cmp
, const char *message
)
344 st_printf ("Internal Error: %s\n", message
);
346 /* This function call is here to get the main.o object file included
347 when linking statically. This works because error.o is supposed to
348 be always linked in (and the function call is in internal_error
349 because hopefully it doesn't happen too often). */
350 stupid_function_name_for_static_linking();
356 /* translate_error()-- Given an integer error code, return a string
357 * describing the error. */
360 translate_error (int code
)
375 p
= "Successful return";
379 p
= "Operating system error";
382 case ERROR_BAD_OPTION
:
383 p
= "Bad statement option";
386 case ERROR_MISSING_OPTION
:
387 p
= "Missing statement option";
390 case ERROR_OPTION_CONFLICT
:
391 p
= "Conflicting statement options";
394 case ERROR_ALREADY_OPEN
:
395 p
= "File already opened in another unit";
399 p
= "Unattached unit";
406 case ERROR_BAD_ACTION
:
407 p
= "Incorrect ACTION specified";
411 p
= "Read past ENDFILE record";
415 p
= "Corrupt unformatted sequential file";
418 case ERROR_READ_VALUE
:
419 p
= "Bad value during read";
422 case ERROR_READ_OVERFLOW
:
423 p
= "Numeric overflow on read";
427 p
= "Unknown error code";
435 /* generate_error()-- Come here when an error happens. This
436 * subroutine is called if it is possible to continue on after the error.
437 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
438 * ERR labels are present, we return, otherwise we terminate the program
439 * after printing a message. The error code is always required but the
440 * message parameter can be NULL, in which case a string describing
441 * the most recent operating system error is used. */
444 generate_error (st_parameter_common
*cmp
, int family
, const char *message
)
446 /* Set the error status. */
447 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
448 *cmp
->iostat
= family
;
452 (family
== ERROR_OS
) ? get_oserror () : translate_error (family
);
454 if (cmp
->flags
& IOPARM_HAS_IOMSG
)
455 cf_strcpy (cmp
->iomsg
, cmp
->iomsg_len
, message
);
457 /* Report status back to the compiler. */
458 cmp
->flags
&= ~IOPARM_LIBRETURN_MASK
;
462 cmp
->flags
|= IOPARM_LIBRETURN_EOR
;
463 if ((cmp
->flags
& IOPARM_EOR
))
468 cmp
->flags
|= IOPARM_LIBRETURN_END
;
469 if ((cmp
->flags
& IOPARM_END
))
474 cmp
->flags
|= IOPARM_LIBRETURN_ERROR
;
475 if ((cmp
->flags
& IOPARM_ERR
))
480 /* Return if the user supplied an iostat variable. */
481 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
484 /* Terminate the program */
488 st_printf ("Fortran runtime error: %s\n", message
);
494 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
495 feature. An error/warning will be issued if the currently selected
496 standard does not contain the requested bits. */
499 notify_std (int std
, const char * message
)
503 warning
= compile_options
.warn_std
& std
;
504 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
509 st_printf ("Fortran runtime error: %s\n", message
);
513 st_printf ("Fortran runtime warning: %s\n", message
);