1 //===-- runtime/stop.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "flang/Runtime/stop.h"
10 #include "environment.h"
13 #include "terminator.h"
21 static void DescribeIEEESignaledExceptions() {
22 #ifdef fetestexcept // a macro in some environments; omit std::
23 auto excepts
{fetestexcept(FE_ALL_EXCEPT
)};
25 auto excepts
{std::fetestexcept(FE_ALL_EXCEPT
)};
28 std::fputs("IEEE arithmetic exceptions signaled:", stderr
);
30 if (excepts
& FE_DIVBYZERO
) {
31 std::fputs(" DIVBYZERO", stderr
);
35 if (excepts
& FE_INEXACT
) {
36 std::fputs(" INEXACT", stderr
);
40 if (excepts
& FE_INVALID
) {
41 std::fputs(" INVALID", stderr
);
45 if (excepts
& FE_OVERFLOW
) {
46 std::fputs(" OVERFLOW", stderr
);
50 if (excepts
& FE_UNDERFLOW
) {
51 std::fputs(" UNDERFLOW", stderr
);
54 std::fputc('\n', stderr
);
58 static void CloseAllExternalUnits(const char *why
) {
59 Fortran::runtime::io::IoErrorHandler handler
{why
};
60 Fortran::runtime::io::ExternalFileUnit::CloseAll(handler
);
63 [[noreturn
]] void RTNAME(StopStatement
)(
64 int code
, bool isErrorStop
, bool quiet
) {
65 CloseAllExternalUnits("STOP statement");
66 if (Fortran::runtime::executionEnvironment
.noStopMessage
&& code
== 0) {
70 std::fprintf(stderr
, "Fortran %s", isErrorStop
? "ERROR STOP" : "STOP");
71 if (code
!= EXIT_SUCCESS
) {
72 std::fprintf(stderr
, ": code %d\n", code
);
74 std::fputc('\n', stderr
);
75 DescribeIEEESignaledExceptions();
80 [[noreturn
]] void RTNAME(StopStatementText
)(
81 const char *code
, std::size_t length
, bool isErrorStop
, bool quiet
) {
82 CloseAllExternalUnits("STOP statement");
84 if (Fortran::runtime::executionEnvironment
.noStopMessage
&& !isErrorStop
) {
85 std::fprintf(stderr
, "%.*s\n", static_cast<int>(length
), code
);
87 std::fprintf(stderr
, "Fortran %s: %.*s\n",
88 isErrorStop
? "ERROR STOP" : "STOP", static_cast<int>(length
), code
);
90 DescribeIEEESignaledExceptions();
93 std::exit(EXIT_FAILURE
);
95 std::exit(EXIT_SUCCESS
);
99 static bool StartPause() {
100 if (Fortran::runtime::io::IsATerminal(0)) {
101 Fortran::runtime::io::IoErrorHandler handler
{"PAUSE statement"};
102 Fortran::runtime::io::ExternalFileUnit::FlushAll(handler
);
108 static void EndPause() {
109 std::fflush(nullptr);
110 if (std::fgetc(stdin
) == EOF
) {
111 CloseAllExternalUnits("PAUSE statement");
112 std::exit(EXIT_SUCCESS
);
116 void RTNAME(PauseStatement
)() {
118 std::fputs("Fortran PAUSE: hit RETURN to continue:", stderr
);
123 void RTNAME(PauseStatementInt
)(int code
) {
125 std::fprintf(stderr
, "Fortran PAUSE %d: hit RETURN to continue:", code
);
130 void RTNAME(PauseStatementText
)(const char *code
, std::size_t length
) {
133 "Fortran PAUSE %.*s: hit RETURN to continue:", static_cast<int>(length
),
139 [[noreturn
]] void RTNAME(FailImageStatement
)() {
140 Fortran::runtime::NotifyOtherImagesOfFailImageStatement();
141 CloseAllExternalUnits("FAIL IMAGE statement");
142 std::exit(EXIT_FAILURE
);
145 [[noreturn
]] void RTNAME(ProgramEndStatement
)() {
146 CloseAllExternalUnits("END statement");
147 std::exit(EXIT_SUCCESS
);
150 [[noreturn
]] void RTNAME(Exit
)(int status
) {
151 CloseAllExternalUnits("CALL EXIT()");
155 [[noreturn
]] void RTNAME(Abort
)() {
156 // TODO: Add backtrace call, unless with `-fno-backtrace`.
160 [[noreturn
]] void RTNAME(ReportFatalUserError
)(
161 const char *message
, const char *source
, int line
) {
162 Fortran::runtime::Terminator
{source
, line
}.Crash(message
);