1 //===-- runtime/terminator.h ------------------------------------*- 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 // Termination of the image
10 
11 #ifndef FORTRAN_RUNTIME_TERMINATOR_H_
12 #define FORTRAN_RUNTIME_TERMINATOR_H_
13 
14 #include "entry-names.h"
15 #include <cstdarg>
16 
17 namespace Fortran::runtime {
18 
19 // A mixin class for statement-specific image error termination
20 // for errors detected in the runtime library
21 class Terminator {
22 public:
23   Terminator() {}
24   Terminator(const Terminator &) = default;
25   explicit Terminator(const char *sourceFileName, int sourceLine = 0)
26       : sourceFileName_{sourceFileName}, sourceLine_{sourceLine} {}
27   void SetLocation(const char *sourceFileName = nullptr, int sourceLine = 0) {
28     sourceFileName_ = sourceFileName;
29     sourceLine_ = sourceLine;
30   }
31   [[noreturn]] void Crash(const char *message, ...) const;
32   [[noreturn]] void CrashArgs(const char *message, va_list &) const;
33   [[noreturn]] void CheckFailed(
34       const char *predicate, const char *file, int line) const;
35 
36   // For test harnessing - overrides CrashArgs().
37   static void RegisterCrashHandler(void (*)(const char *, va_list &));
38 
39 private:
40   const char *sourceFileName_{nullptr};
41   int sourceLine_{0};
42 };
43 
44 // RUNTIME_CHECK() guarantees evaluation of its predicate.
45 #define RUNTIME_CHECK(terminator, pred) \
46   if (pred) \
47     ; \
48   else \
49     (terminator).CheckFailed(#pred, __FILE__, __LINE__)
50 
51 void NotifyOtherImagesOfNormalEnd();
52 void NotifyOtherImagesOfFailImageStatement();
53 void NotifyOtherImagesOfErrorTermination();
54 } // namespace Fortran::runtime
55 
56 namespace Fortran::runtime::io {
57 void FlushOutputOnCrash(const Terminator &);
58 }
59 
60 #endif // FORTRAN_RUNTIME_TERMINATOR_H_
61