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
);
29 if (excepts
& FE_DIVBYZERO
) {
30 std::fputs(" DIVBYZERO", stderr
);
32 if (excepts
& FE_INEXACT
) {
33 std::fputs(" INEXACT", stderr
);
35 if (excepts
& FE_INVALID
) {
36 std::fputs(" INVALID", stderr
);
38 if (excepts
& FE_OVERFLOW
) {
39 std::fputs(" OVERFLOW", stderr
);
41 if (excepts
& FE_UNDERFLOW
) {
42 std::fputs(" UNDERFLOW", stderr
);
44 std::fputc('\n', stderr
);
48 static void CloseAllExternalUnits(const char *why
) {
49 Fortran::runtime::io::IoErrorHandler handler
{why
};
50 Fortran::runtime::io::ExternalFileUnit::CloseAll(handler
);
53 [[noreturn
]] void RTNAME(StopStatement
)(
54 int code
, bool isErrorStop
, bool quiet
) {
55 CloseAllExternalUnits("STOP statement");
56 if (Fortran::runtime::executionEnvironment
.noStopMessage
&& code
== 0) {
60 std::fprintf(stderr
, "Fortran %s", isErrorStop
? "ERROR STOP" : "STOP");
61 if (code
!= EXIT_SUCCESS
) {
62 std::fprintf(stderr
, ": code %d\n", code
);
64 std::fputc('\n', stderr
);
65 DescribeIEEESignaledExceptions();
70 [[noreturn
]] void RTNAME(StopStatementText
)(
71 const char *code
, std::size_t length
, bool isErrorStop
, bool quiet
) {
72 CloseAllExternalUnits("STOP statement");
74 if (Fortran::runtime::executionEnvironment
.noStopMessage
&& !isErrorStop
) {
75 std::fprintf(stderr
, "%.*s\n", static_cast<int>(length
), code
);
77 std::fprintf(stderr
, "Fortran %s: %.*s\n",
78 isErrorStop
? "ERROR STOP" : "STOP", static_cast<int>(length
), code
);
80 DescribeIEEESignaledExceptions();
83 std::exit(EXIT_FAILURE
);
85 std::exit(EXIT_SUCCESS
);
89 static bool StartPause() {
90 if (Fortran::runtime::io::IsATerminal(0)) {
91 Fortran::runtime::io::IoErrorHandler handler
{"PAUSE statement"};
92 Fortran::runtime::io::ExternalFileUnit::FlushAll(handler
);
98 static void EndPause() {
100 if (std::fgetc(stdin
) == EOF
) {
101 CloseAllExternalUnits("PAUSE statement");
102 std::exit(EXIT_SUCCESS
);
106 void RTNAME(PauseStatement
)() {
108 std::fputs("Fortran PAUSE: hit RETURN to continue:", stderr
);
113 void RTNAME(PauseStatementInt
)(int code
) {
115 std::fprintf(stderr
, "Fortran PAUSE %d: hit RETURN to continue:", code
);
120 void RTNAME(PauseStatementText
)(const char *code
, std::size_t length
) {
123 "Fortran PAUSE %.*s: hit RETURN to continue:", static_cast<int>(length
),
129 [[noreturn
]] void RTNAME(FailImageStatement
)() {
130 Fortran::runtime::NotifyOtherImagesOfFailImageStatement();
131 CloseAllExternalUnits("FAIL IMAGE statement");
132 std::exit(EXIT_FAILURE
);
135 [[noreturn
]] void RTNAME(ProgramEndStatement
)() {
136 CloseAllExternalUnits("END statement");
137 std::exit(EXIT_SUCCESS
);
140 [[noreturn
]] void RTNAME(Exit
)(int status
) {
141 CloseAllExternalUnits("CALL EXIT()");
145 [[noreturn
]] void RTNAME(Abort
)() {
146 // TODO: Add backtrace call, unless with `-fno-backtrace`.
150 [[noreturn
]] void RTNAME(ReportFatalUserError
)(
151 const char *message
, const char *source
, int line
) {
152 Fortran::runtime::Terminator
{source
, line
}.Crash(message
);