File: terminator.h

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (124 lines) | stat: -rw-r--r-- 4,055 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//===-- runtime/terminator.h ------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// Termination of the image

#ifndef FORTRAN_RUNTIME_TERMINATOR_H_
#define FORTRAN_RUNTIME_TERMINATOR_H_

#include "flang/Common/api-attrs.h"
#include <cstdarg>
#include <cstdio>
#include <cstdlib>

namespace Fortran::runtime {

// A mixin class for statement-specific image error termination
// for errors detected in the runtime library
class Terminator {
public:
  RT_API_ATTRS Terminator() {}
  Terminator(const Terminator &) = default;
  explicit RT_API_ATTRS Terminator(
      const char *sourceFileName, int sourceLine = 0)
      : sourceFileName_{sourceFileName}, sourceLine_{sourceLine} {}

  RT_API_ATTRS const char *sourceFileName() const { return sourceFileName_; }
  RT_API_ATTRS int sourceLine() const { return sourceLine_; }

  RT_API_ATTRS void SetLocation(
      const char *sourceFileName = nullptr, int sourceLine = 0) {
    sourceFileName_ = sourceFileName;
    sourceLine_ = sourceLine;
  }

  // Silence compiler warnings about the format string being
  // non-literal. A more precise control would be
  // __attribute__((format_arg(2))), but it requires the function
  // to return 'char *', which does not work well with noreturn.
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-security"
#elif defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
#endif

  // Device offload compilers do not normally support varargs and va_list,
  // so use C++ variadic templates to forward the crash arguments
  // to regular printf for the device compilation.
  // Try to keep the inline implementations as small as possible.
  template <typename... Args>
  [[noreturn]] RT_DEVICE_NOINLINE RT_API_ATTRS const char *Crash(
      const char *message, Args... args) const {
#if !defined(RT_DEVICE_COMPILATION)
    // Invoke handler set up by the test harness.
    InvokeCrashHandler(message, args...);
#endif
    CrashHeader();
    PrintCrashArgs(message, args...);
    CrashFooter();
  }

  template <typename... Args>
  RT_API_ATTRS void PrintCrashArgs(const char *message, Args... args) const {
#if defined(RT_DEVICE_COMPILATION)
    std::printf(message, args...);
#else
    std::fprintf(stderr, message, args...);
#endif
  }

#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif

  RT_API_ATTRS void CrashHeader() const;
  [[noreturn]] RT_API_ATTRS void CrashFooter() const;
#if !defined(RT_DEVICE_COMPILATION)
  void InvokeCrashHandler(const char *message, ...) const;
  [[noreturn]] void CrashArgs(const char *message, va_list &) const;
#endif
  [[noreturn]] RT_API_ATTRS void CheckFailed(
      const char *predicate, const char *file, int line) const;
  [[noreturn]] RT_API_ATTRS void CheckFailed(const char *predicate) const;

  // For test harnessing - overrides CrashArgs().
  static void RegisterCrashHandler(void (*)(const char *sourceFile,
      int sourceLine, const char *message, va_list &ap));

private:
  const char *sourceFileName_{nullptr};
  int sourceLine_{0};
};

// RUNTIME_CHECK() guarantees evaluation of its predicate.
#define RUNTIME_CHECK(terminator, pred) \
  if (pred) \
    ; \
  else \
    (terminator).CheckFailed(#pred, __FILE__, __LINE__)

#define INTERNAL_CHECK(pred) \
  if (pred) \
    ; \
  else \
    Terminator{__FILE__, __LINE__}.CheckFailed(#pred)

RT_API_ATTRS void NotifyOtherImagesOfNormalEnd();
RT_API_ATTRS void NotifyOtherImagesOfFailImageStatement();
RT_API_ATTRS void NotifyOtherImagesOfErrorTermination();
} // namespace Fortran::runtime

namespace Fortran::runtime::io {
RT_API_ATTRS void FlushOutputOnCrash(const Terminator &);
}

#endif // FORTRAN_RUNTIME_TERMINATOR_H_