aboutsummaryrefslogtreecommitdiff
path: root/COFF/Error.h
blob: a4f44fb1e36c7f24e4acc4017ecbd335618cf81e (plain)
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
//===- Error.h --------------------------------------------------*- C++ -*-===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLD_COFF_ERROR_H
#define LLD_COFF_ERROR_H

#include "lld/Core/LLVM.h"
#include "llvm/Support/Error.h"

namespace lld {
namespace coff {

extern uint64_t ErrorCount;
extern llvm::raw_ostream *ErrorOS;
extern llvm::StringRef Argv0;

void log(const Twine &Msg);
void message(const Twine &Msg);
void warn(const Twine &Msg);
void error(const Twine &Msg);
LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg);
LLVM_ATTRIBUTE_NORETURN void fatal(std::error_code EC, const Twine &Prefix);
LLVM_ATTRIBUTE_NORETURN void fatal(llvm::Error &Err, const Twine &Prefix);

template <class T> T check(ErrorOr<T> V, const Twine &Prefix) {
  if (auto EC = V.getError())
    fatal(EC, Prefix);
  return std::move(*V);
}

template <class T> T check(Expected<T> E, const Twine &Prefix) {
  if (llvm::Error Err = E.takeError())
    fatal(Err, Prefix);
  return std::move(*E);
}

template <class T> T check(ErrorOr<T> EO) {
  if (!EO)
    fatal(EO.getError().message());
  return std::move(*EO);
}

template <class T> T check(Expected<T> E) {
  if (!E) {
    std::string Buf;
    llvm::raw_string_ostream OS(Buf);
    logAllUnhandledErrors(E.takeError(), OS, "");
    OS.flush();
    fatal(Buf);
  }
  return std::move(*E);
}

} // namespace coff
} // namespace lld

#endif