PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/fortranformat/spec/examples/gcc-4.4.0/libgfortran/runtime/error.c

https://bitbucket.org/brendanarnold/py-fortranformat
C | 532 lines | 329 code | 117 blank | 86 comment | 69 complexity | b9d30bc2bb764974f879d32332c102e6 MD5 | raw file
Possible License(s): MIT
  1. /* Copyright (C) 2002, 2003, 2005, 2006, 2007, 2009
  2. Free Software Foundation, Inc.
  3. Contributed by Andy Vaught
  4. This file is part of the GNU Fortran 95 runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #include <assert.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #ifdef HAVE_SIGNAL_H
  25. #include <signal.h>
  26. #endif
  27. #ifdef HAVE_UNISTD_H
  28. #include <unistd.h>
  29. #endif
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. #ifdef HAVE_SYS_TIME_H
  34. #include <sys/time.h>
  35. #endif
  36. /* <sys/time.h> has to be included before <sys/resource.h> to work
  37. around PR 30518; otherwise, MacOS 10.3.9 headers are just broken. */
  38. #ifdef HAVE_SYS_RESOURCE_H
  39. #include <sys/resource.h>
  40. #endif
  41. #ifdef __MINGW32__
  42. #define HAVE_GETPID 1
  43. #include <process.h>
  44. #endif
  45. /* sys_exit()-- Terminate the program with an exit code. */
  46. void
  47. sys_exit (int code)
  48. {
  49. /* Show error backtrace if possible. */
  50. if (code != 0 && code != 4
  51. && (options.backtrace == 1
  52. || (options.backtrace == -1 && compile_options.backtrace == 1)))
  53. show_backtrace ();
  54. /* Dump core if requested. */
  55. if (code != 0
  56. && (options.dump_core == 1
  57. || (options.dump_core == -1 && compile_options.dump_core == 1)))
  58. {
  59. #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
  60. /* Warn if a core file cannot be produced because
  61. of core size limit. */
  62. struct rlimit core_limit;
  63. if (getrlimit (RLIMIT_CORE, &core_limit) == 0 && core_limit.rlim_cur == 0)
  64. st_printf ("** Warning: a core dump was requested, but the core size"
  65. "limit\n** is currently zero.\n\n");
  66. #endif
  67. #if defined(HAVE_KILL) && defined(HAVE_GETPID) && defined(SIGQUIT)
  68. kill (getpid (), SIGQUIT);
  69. #else
  70. st_printf ("Core dump not possible, sorry.");
  71. #endif
  72. }
  73. exit (code);
  74. }
  75. /* Error conditions. The tricky part here is printing a message when
  76. * it is the I/O subsystem that is severely wounded. Our goal is to
  77. * try and print something making the fewest assumptions possible,
  78. * then try to clean up before actually exiting.
  79. *
  80. * The following exit conditions are defined:
  81. * 0 Normal program exit.
  82. * 1 Terminated because of operating system error.
  83. * 2 Error in the runtime library
  84. * 3 Internal error in runtime library
  85. * 4 Error during error processing (very bad)
  86. *
  87. * Other error returns are reserved for the STOP statement with a numeric code.
  88. */
  89. /* gfc_itoa()-- Integer to decimal conversion. */
  90. const char *
  91. gfc_itoa (GFC_INTEGER_LARGEST n, char *buffer, size_t len)
  92. {
  93. int negative;
  94. char *p;
  95. GFC_UINTEGER_LARGEST t;
  96. assert (len >= GFC_ITOA_BUF_SIZE);
  97. if (n == 0)
  98. return "0";
  99. negative = 0;
  100. t = n;
  101. if (n < 0)
  102. {
  103. negative = 1;
  104. t = -n; /*must use unsigned to protect from overflow*/
  105. }
  106. p = buffer + GFC_ITOA_BUF_SIZE - 1;
  107. *p = '\0';
  108. while (t != 0)
  109. {
  110. *--p = '0' + (t % 10);
  111. t /= 10;
  112. }
  113. if (negative)
  114. *--p = '-';
  115. return p;
  116. }
  117. /* xtoa()-- Integer to hexadecimal conversion. */
  118. const char *
  119. xtoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
  120. {
  121. int digit;
  122. char *p;
  123. assert (len >= GFC_XTOA_BUF_SIZE);
  124. if (n == 0)
  125. return "0";
  126. p = buffer + GFC_XTOA_BUF_SIZE - 1;
  127. *p = '\0';
  128. while (n != 0)
  129. {
  130. digit = n & 0xF;
  131. if (digit > 9)
  132. digit += 'A' - '0' - 10;
  133. *--p = '0' + digit;
  134. n >>= 4;
  135. }
  136. return p;
  137. }
  138. /* show_locus()-- Print a line number and filename describing where
  139. * something went wrong */
  140. void
  141. show_locus (st_parameter_common *cmp)
  142. {
  143. static char *filename;
  144. if (!options.locus || cmp == NULL || cmp->filename == NULL)
  145. return;
  146. if (cmp->unit > 0)
  147. {
  148. filename = filename_from_unit (cmp->unit);
  149. if (filename != NULL)
  150. {
  151. st_printf ("At line %d of file %s (unit = %d, file = '%s')\n",
  152. (int) cmp->line, cmp->filename, (int) cmp->unit, filename);
  153. free_mem (filename);
  154. }
  155. else
  156. {
  157. st_printf ("At line %d of file %s (unit = %d)\n",
  158. (int) cmp->line, cmp->filename, (int) cmp->unit);
  159. }
  160. return;
  161. }
  162. st_printf ("At line %d of file %s\n", (int) cmp->line, cmp->filename);
  163. }
  164. /* recursion_check()-- It's possible for additional errors to occur
  165. * during fatal error processing. We detect this condition here and
  166. * exit with code 4 immediately. */
  167. #define MAGIC 0x20DE8101
  168. static void
  169. recursion_check (void)
  170. {
  171. static int magic = 0;
  172. /* Don't even try to print something at this point */
  173. if (magic == MAGIC)
  174. sys_exit (4);
  175. magic = MAGIC;
  176. }
  177. /* os_error()-- Operating system error. We get a message from the
  178. * operating system, show it and leave. Some operating system errors
  179. * are caught and processed by the library. If not, we come here. */
  180. void
  181. os_error (const char *message)
  182. {
  183. recursion_check ();
  184. st_printf ("Operating system error: %s\n%s\n", get_oserror (), message);
  185. sys_exit (1);
  186. }
  187. iexport(os_error);
  188. /* void runtime_error()-- These are errors associated with an
  189. * invalid fortran program. */
  190. void
  191. runtime_error (const char *message, ...)
  192. {
  193. va_list ap;
  194. recursion_check ();
  195. st_printf ("Fortran runtime error: ");
  196. va_start (ap, message);
  197. st_vprintf (message, ap);
  198. va_end (ap);
  199. st_printf ("\n");
  200. sys_exit (2);
  201. }
  202. iexport(runtime_error);
  203. /* void runtime_error_at()-- These are errors associated with a
  204. * run time error generated by the front end compiler. */
  205. void
  206. runtime_error_at (const char *where, const char *message, ...)
  207. {
  208. va_list ap;
  209. recursion_check ();
  210. st_printf ("%s\n", where);
  211. st_printf ("Fortran runtime error: ");
  212. va_start (ap, message);
  213. st_vprintf (message, ap);
  214. va_end (ap);
  215. st_printf ("\n");
  216. sys_exit (2);
  217. }
  218. iexport(runtime_error_at);
  219. void
  220. runtime_warning_at (const char *where, const char *message, ...)
  221. {
  222. va_list ap;
  223. st_printf ("%s\n", where);
  224. st_printf ("Fortran runtime warning: ");
  225. va_start (ap, message);
  226. st_vprintf (message, ap);
  227. va_end (ap);
  228. st_printf ("\n");
  229. }
  230. iexport(runtime_warning_at);
  231. /* void internal_error()-- These are this-can't-happen errors
  232. * that indicate something deeply wrong. */
  233. void
  234. internal_error (st_parameter_common *cmp, const char *message)
  235. {
  236. recursion_check ();
  237. show_locus (cmp);
  238. st_printf ("Internal Error: %s\n", message);
  239. /* This function call is here to get the main.o object file included
  240. when linking statically. This works because error.o is supposed to
  241. be always linked in (and the function call is in internal_error
  242. because hopefully it doesn't happen too often). */
  243. stupid_function_name_for_static_linking();
  244. sys_exit (3);
  245. }
  246. /* translate_error()-- Given an integer error code, return a string
  247. * describing the error. */
  248. const char *
  249. translate_error (int code)
  250. {
  251. const char *p;
  252. switch (code)
  253. {
  254. case LIBERROR_EOR:
  255. p = "End of record";
  256. break;
  257. case LIBERROR_END:
  258. p = "End of file";
  259. break;
  260. case LIBERROR_OK:
  261. p = "Successful return";
  262. break;
  263. case LIBERROR_OS:
  264. p = "Operating system error";
  265. break;
  266. case LIBERROR_BAD_OPTION:
  267. p = "Bad statement option";
  268. break;
  269. case LIBERROR_MISSING_OPTION:
  270. p = "Missing statement option";
  271. break;
  272. case LIBERROR_OPTION_CONFLICT:
  273. p = "Conflicting statement options";
  274. break;
  275. case LIBERROR_ALREADY_OPEN:
  276. p = "File already opened in another unit";
  277. break;
  278. case LIBERROR_BAD_UNIT:
  279. p = "Unattached unit";
  280. break;
  281. case LIBERROR_FORMAT:
  282. p = "FORMAT error";
  283. break;
  284. case LIBERROR_BAD_ACTION:
  285. p = "Incorrect ACTION specified";
  286. break;
  287. case LIBERROR_ENDFILE:
  288. p = "Read past ENDFILE record";
  289. break;
  290. case LIBERROR_BAD_US:
  291. p = "Corrupt unformatted sequential file";
  292. break;
  293. case LIBERROR_READ_VALUE:
  294. p = "Bad value during read";
  295. break;
  296. case LIBERROR_READ_OVERFLOW:
  297. p = "Numeric overflow on read";
  298. break;
  299. case LIBERROR_INTERNAL:
  300. p = "Internal error in run-time library";
  301. break;
  302. case LIBERROR_INTERNAL_UNIT:
  303. p = "Internal unit I/O error";
  304. break;
  305. case LIBERROR_DIRECT_EOR:
  306. p = "Write exceeds length of DIRECT access record";
  307. break;
  308. case LIBERROR_SHORT_RECORD:
  309. p = "I/O past end of record on unformatted file";
  310. break;
  311. case LIBERROR_CORRUPT_FILE:
  312. p = "Unformatted file structure has been corrupted";
  313. break;
  314. default:
  315. p = "Unknown error code";
  316. break;
  317. }
  318. return p;
  319. }
  320. /* generate_error()-- Come here when an error happens. This
  321. * subroutine is called if it is possible to continue on after the error.
  322. * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
  323. * ERR labels are present, we return, otherwise we terminate the program
  324. * after printing a message. The error code is always required but the
  325. * message parameter can be NULL, in which case a string describing
  326. * the most recent operating system error is used. */
  327. void
  328. generate_error (st_parameter_common *cmp, int family, const char *message)
  329. {
  330. /* If there was a previous error, don't mask it with another
  331. error message, EOF or EOR condition. */
  332. if ((cmp->flags & IOPARM_LIBRETURN_MASK) == IOPARM_LIBRETURN_ERROR)
  333. return;
  334. /* Set the error status. */
  335. if ((cmp->flags & IOPARM_HAS_IOSTAT))
  336. *cmp->iostat = (family == LIBERROR_OS) ? errno : family;
  337. if (message == NULL)
  338. message =
  339. (family == LIBERROR_OS) ? get_oserror () : translate_error (family);
  340. if (cmp->flags & IOPARM_HAS_IOMSG)
  341. cf_strcpy (cmp->iomsg, cmp->iomsg_len, message);
  342. /* Report status back to the compiler. */
  343. cmp->flags &= ~IOPARM_LIBRETURN_MASK;
  344. switch (family)
  345. {
  346. case LIBERROR_EOR:
  347. cmp->flags |= IOPARM_LIBRETURN_EOR;
  348. if ((cmp->flags & IOPARM_EOR))
  349. return;
  350. break;
  351. case LIBERROR_END:
  352. cmp->flags |= IOPARM_LIBRETURN_END;
  353. if ((cmp->flags & IOPARM_END))
  354. return;
  355. break;
  356. default:
  357. cmp->flags |= IOPARM_LIBRETURN_ERROR;
  358. if ((cmp->flags & IOPARM_ERR))
  359. return;
  360. break;
  361. }
  362. /* Return if the user supplied an iostat variable. */
  363. if ((cmp->flags & IOPARM_HAS_IOSTAT))
  364. return;
  365. /* Terminate the program */
  366. recursion_check ();
  367. show_locus (cmp);
  368. st_printf ("Fortran runtime error: %s\n", message);
  369. sys_exit (2);
  370. }
  371. iexport(generate_error);
  372. /* Whether, for a feature included in a given standard set (GFC_STD_*),
  373. we should issue an error or a warning, or be quiet. */
  374. notification
  375. notification_std (int std)
  376. {
  377. int warning;
  378. if (!compile_options.pedantic)
  379. return SILENT;
  380. warning = compile_options.warn_std & std;
  381. if ((compile_options.allow_std & std) != 0 && !warning)
  382. return SILENT;
  383. return warning ? WARNING : ERROR;
  384. }
  385. /* Possibly issue a warning/error about use of a nonstandard (or deleted)
  386. feature. An error/warning will be issued if the currently selected
  387. standard does not contain the requested bits. */
  388. try
  389. notify_std (st_parameter_common *cmp, int std, const char * message)
  390. {
  391. int warning;
  392. if (!compile_options.pedantic)
  393. return SUCCESS;
  394. warning = compile_options.warn_std & std;
  395. if ((compile_options.allow_std & std) != 0 && !warning)
  396. return SUCCESS;
  397. if (!warning)
  398. {
  399. recursion_check ();
  400. show_locus (cmp);
  401. st_printf ("Fortran runtime error: %s\n", message);
  402. sys_exit (2);
  403. }
  404. else
  405. {
  406. show_locus (cmp);
  407. st_printf ("Fortran runtime warning: %s\n", message);
  408. }
  409. return FAILURE;
  410. }