1 //===-- runtime/terminate.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 "terminator.h"
13 namespace Fortran::runtime
{
15 #if !defined(RT_DEVICE_COMPILATION)
16 [[maybe_unused
]] static void (*crashHandler
)(
17 const char *, int, const char *, va_list &){nullptr};
19 void Terminator::RegisterCrashHandler(
20 void (*handler
)(const char *, int, const char *, va_list &)) {
21 crashHandler
= handler
;
24 void Terminator::InvokeCrashHandler(const char *message
, ...) const {
27 va_start(ap
, message
);
28 crashHandler(sourceFileName_
, sourceLine_
, message
, ap
);
33 [[noreturn
]] void Terminator::CrashArgs(
34 const char *message
, va_list &ap
) const {
36 std::vfprintf(stderr
, message
, ap
);
42 RT_OFFLOAD_API_GROUP_BEGIN
44 RT_API_ATTRS
void Terminator::CrashHeader() const {
45 #if defined(RT_DEVICE_COMPILATION)
46 std::printf("\nfatal Fortran runtime error");
47 if (sourceFileName_
) {
48 std::printf("(%s", sourceFileName_
);
50 std::printf(":%d", sourceLine_
);
56 std::fputs("\nfatal Fortran runtime error", stderr
);
57 if (sourceFileName_
) {
58 std::fprintf(stderr
, "(%s", sourceFileName_
);
60 std::fprintf(stderr
, ":%d", sourceLine_
);
64 std::fputs(": ", stderr
);
68 [[noreturn
]] RT_API_ATTRS
void Terminator::CrashFooter() const {
69 #if defined(RT_DEVICE_COMPILATION)
73 // FIXME: re-enable the flush along with the IO enabling.
74 io::FlushOutputOnCrash(*this);
76 NotifyOtherImagesOfErrorTermination();
77 #if defined(RT_DEVICE_COMPILATION)
78 #if defined(__CUDACC__)
79 // NVCC supports __trap().
81 #elif defined(__clang__)
82 // Clang supports __builtin_trap().
85 #error "unsupported compiler"
92 [[noreturn
]] RT_API_ATTRS
void Terminator::CheckFailed(
93 const char *predicate
, const char *file
, int line
) const {
94 Crash("Internal error: RUNTIME_CHECK(%s) failed at %s(%d)", predicate
, file
,
98 [[noreturn
]] RT_API_ATTRS
void Terminator::CheckFailed(
99 const char *predicate
) const {
100 Crash("Internal error: RUNTIME_CHECK(%s) failed at %s(%d)", predicate
,
101 sourceFileName_
, sourceLine_
);
104 // TODO: These will be defined in the coarray runtime library
105 RT_API_ATTRS
void NotifyOtherImagesOfNormalEnd() {}
106 RT_API_ATTRS
void NotifyOtherImagesOfFailImageStatement() {}
107 RT_API_ATTRS
void NotifyOtherImagesOfErrorTermination() {}
109 RT_OFFLOAD_API_GROUP_END
111 } // namespace Fortran::runtime