1 //===-- runtime/stop.cpp ----------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "stop.h" 10 #include "file.h" 11 #include "io-error.h" 12 #include "terminator.h" 13 #include "unit.h" 14 #include <cfenv> 15 #include <cstdio> 16 #include <cstdlib> 17 18 extern "C" { 19 20 static void DescribeIEEESignaledExceptions() { 21 #ifdef fetestexcept // a macro in some environments; omit std:: 22 auto excepts{fetestexcept(FE_ALL_EXCEPT)}; 23 #else 24 auto excepts{std::fetestexcept(FE_ALL_EXCEPT)}; 25 #endif 26 if (excepts) { 27 std::fputs("IEEE arithmetic exceptions signaled:", stderr); 28 if (excepts & FE_DIVBYZERO) { 29 std::fputs(" DIVBYZERO", stderr); 30 } 31 if (excepts & FE_INEXACT) { 32 std::fputs(" INEXACT", stderr); 33 } 34 if (excepts & FE_INVALID) { 35 std::fputs(" INVALID", stderr); 36 } 37 if (excepts & FE_OVERFLOW) { 38 std::fputs(" OVERFLOW", stderr); 39 } 40 if (excepts & FE_UNDERFLOW) { 41 std::fputs(" UNDERFLOW", stderr); 42 } 43 std::fputc('\n', stderr); 44 } 45 } 46 47 static void CloseAllExternalUnits(const char *why) { 48 Fortran::runtime::io::IoErrorHandler handler{why}; 49 Fortran::runtime::io::ExternalFileUnit::CloseAll(handler); 50 } 51 52 [[noreturn]] void RTNAME(StopStatement)( 53 int code, bool isErrorStop, bool quiet) { 54 CloseAllExternalUnits("STOP statement"); 55 if (!quiet) { 56 std::fprintf(stderr, "Fortran %s", isErrorStop ? "ERROR STOP" : "STOP"); 57 if (code != EXIT_SUCCESS) { 58 std::fprintf(stderr, ": code %d\n", code); 59 } 60 std::fputc('\n', stderr); 61 DescribeIEEESignaledExceptions(); 62 } 63 std::exit(code); 64 } 65 66 [[noreturn]] void RTNAME(StopStatementText)( 67 const char *code, bool isErrorStop, bool quiet) { 68 CloseAllExternalUnits("STOP statement"); 69 if (!quiet) { 70 std::fprintf( 71 stderr, "Fortran %s: %s\n", isErrorStop ? "ERROR STOP" : "STOP", code); 72 DescribeIEEESignaledExceptions(); 73 } 74 std::exit(EXIT_FAILURE); 75 } 76 77 void RTNAME(PauseStatement)() { 78 if (Fortran::runtime::io::IsATerminal(0)) { 79 Fortran::runtime::io::IoErrorHandler handler{"PAUSE statement"}; 80 Fortran::runtime::io::ExternalFileUnit::FlushAll(handler); 81 std::fputs("Fortran PAUSE: hit RETURN to continue:", stderr); 82 std::fflush(nullptr); 83 if (std::fgetc(stdin) == EOF) { 84 CloseAllExternalUnits("PAUSE statement"); 85 std::exit(EXIT_SUCCESS); 86 } 87 } 88 } 89 90 [[noreturn]] void RTNAME(FailImageStatement)() { 91 Fortran::runtime::NotifyOtherImagesOfFailImageStatement(); 92 CloseAllExternalUnits("FAIL IMAGE statement"); 93 std::exit(EXIT_FAILURE); 94 } 95 96 [[noreturn]] void RTNAME(ProgramEndStatement)() { 97 CloseAllExternalUnits("END statement"); 98 std::exit(EXIT_SUCCESS); 99 } 100 } 101