]>
gcc.gnu.org Git - gcc.git/blob - libgfortran/runtime/error.c
1 /* Copyright (C) 2002, 2003, 2005, 2006 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
= "Internal error in run-time library";
430 case ERROR_INTERNAL_UNIT
:
431 p
= "Internal unit I/O error";
434 case ERROR_DIRECT_EOR
:
435 p
= "Write exceeds length of DIRECT access record";
439 p
= "Unknown error code";
447 /* generate_error()-- Come here when an error happens. This
448 * subroutine is called if it is possible to continue on after the error.
449 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
450 * ERR labels are present, we return, otherwise we terminate the program
451 * after printing a message. The error code is always required but the
452 * message parameter can be NULL, in which case a string describing
453 * the most recent operating system error is used. */
456 generate_error (st_parameter_common
*cmp
, int family
, const char *message
)
458 /* Set the error status. */
459 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
460 *cmp
->iostat
= family
;
464 (family
== ERROR_OS
) ? get_oserror () : translate_error (family
);
466 if (cmp
->flags
& IOPARM_HAS_IOMSG
)
467 cf_strcpy (cmp
->iomsg
, cmp
->iomsg_len
, message
);
469 /* Report status back to the compiler. */
470 cmp
->flags
&= ~IOPARM_LIBRETURN_MASK
;
474 cmp
->flags
|= IOPARM_LIBRETURN_EOR
;
475 if ((cmp
->flags
& IOPARM_EOR
))
480 cmp
->flags
|= IOPARM_LIBRETURN_END
;
481 if ((cmp
->flags
& IOPARM_END
))
486 cmp
->flags
|= IOPARM_LIBRETURN_ERROR
;
487 if ((cmp
->flags
& IOPARM_ERR
))
492 /* Return if the user supplied an iostat variable. */
493 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
496 /* Terminate the program */
500 st_printf ("Fortran runtime error: %s\n", message
);
505 /* Whether, for a feature included in a given standard set (GFC_STD_*),
506 we should issue an error or a warning, or be quiet. */
509 notification_std (int std
)
513 if (!compile_options
.pedantic
)
516 warning
= compile_options
.warn_std
& std
;
517 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
520 return warning
? WARNING
: ERROR
;
525 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
526 feature. An error/warning will be issued if the currently selected
527 standard does not contain the requested bits. */
530 notify_std (int std
, const char * message
)
534 if (!compile_options
.pedantic
)
537 warning
= compile_options
.warn_std
& std
;
538 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
543 st_printf ("Fortran runtime error: %s\n", message
);
547 st_printf ("Fortran runtime warning: %s\n", message
);
This page took 0.055697 seconds and 5 git commands to generate.