1 //===-- runtime/terminate.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 "terminator.h"
10 #include <cstdio>
11 #include <cstdlib>
12 
13 namespace Fortran::runtime {
14 
15 [[noreturn]] void Terminator::Crash(const char *message, ...) const {
16   va_list ap;
17   va_start(ap, message);
18   CrashArgs(message, ap);
19 }
20 
21 static void (*crashHandler)(const char *, va_list &){nullptr};
22 
23 void Terminator::RegisterCrashHandler(
24     void (*handler)(const char *, va_list &)) {
25   crashHandler = handler;
26 }
27 
28 [[noreturn]] void Terminator::CrashArgs(
29     const char *message, va_list &ap) const {
30   if (crashHandler) {
31     crashHandler(message, ap);
32   }
33   std::fputs("\nfatal Fortran runtime error", stderr);
34   if (sourceFileName_) {
35     std::fprintf(stderr, "(%s", sourceFileName_);
36     if (sourceLine_) {
37       std::fprintf(stderr, ":%d", sourceLine_);
38     }
39     fputc(')', stderr);
40   }
41   std::fputs(": ", stderr);
42   std::vfprintf(stderr, message, ap);
43   fputc('\n', stderr);
44   va_end(ap);
45   io::FlushOutputOnCrash(*this);
46   NotifyOtherImagesOfErrorTermination();
47   std::abort();
48 }
49 
50 [[noreturn]] void Terminator::CheckFailed(
51     const char *predicate, const char *file, int line) const {
52   Crash("Internal error: RUNTIME_CHECK(%s) failed at %s(%d)", predicate, file,
53       line);
54 }
55 
56 // TODO: These will be defined in the coarray runtime library
57 void NotifyOtherImagesOfNormalEnd() {}
58 void NotifyOtherImagesOfFailImageStatement() {}
59 void NotifyOtherImagesOfErrorTermination() {}
60 } // namespace Fortran::runtime
61