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:
Terminator()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   [[noreturn]] void CheckFailed(const char *predicate) const;
36 
37   // For test harnessing - overrides CrashArgs().
38   static void RegisterCrashHandler(void (*)(const char *sourceFile,
39       int sourceLine, const char *message, va_list &ap));
40 
41 private:
42   const char *sourceFileName_{nullptr};
43   int sourceLine_{0};
44 };
45 
46 // RUNTIME_CHECK() guarantees evaluation of its predicate.
47 #define RUNTIME_CHECK(terminator, pred) \
48   if (pred) \
49     ; \
50   else \
51     (terminator).CheckFailed(#pred, __FILE__, __LINE__)
52 
53 #define INTERNAL_CHECK(pred) \
54   if (pred) \
55     ; \
56   else \
57     Terminator{__FILE__, __LINE__}.CheckFailed(#pred)
58 
59 void NotifyOtherImagesOfNormalEnd();
60 void NotifyOtherImagesOfFailImageStatement();
61 void NotifyOtherImagesOfErrorTermination();
62 } // namespace Fortran::runtime
63 
64 namespace Fortran::runtime::io {
65 void FlushOutputOnCrash(const Terminator &);
66 }
67 
68 #endif // FORTRAN_RUNTIME_TERMINATOR_H_
69