]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Error.h
Merge ^/head r318560 through r318657.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / Error.h
1 //===- Error.h --------------------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // In LLD, we have three levels of errors: fatal, error or warn.
11 //
12 // Fatal makes the program exit immediately with an error message.
13 // You shouldn't use it except for reporting a corrupted input file.
14 //
15 // Error prints out an error message and increment a global variable
16 // ErrorCount to record the fact that we met an error condition. It does
17 // not exit, so it is safe for a lld-as-a-library use case. It is generally
18 // useful because it can report more than one error in a single run.
19 //
20 // Warn doesn't do anything but printing out a given message.
21 //
22 // It is not recommended to use llvm::outs() or llvm::errs() directly
23 // in LLD because they are not thread-safe. The functions declared in
24 // this file are mutually excluded, so you want to use them instead.
25 //
26 //===----------------------------------------------------------------------===//
27
28 #ifndef LLD_ELF_ERROR_H
29 #define LLD_ELF_ERROR_H
30
31 #include "lld/Core/LLVM.h"
32
33 #include "llvm/Support/Error.h"
34
35 namespace lld {
36 namespace elf {
37
38 extern uint64_t ErrorCount;
39 extern llvm::raw_ostream *ErrorOS;
40 extern llvm::StringRef Argv0;
41
42 void log(const Twine &Msg);
43 void message(const Twine &Msg);
44 void warn(const Twine &Msg);
45 void error(const Twine &Msg);
46 LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg);
47
48 LLVM_ATTRIBUTE_NORETURN void exitLld(int Val);
49
50 // check() functions are convenient functions to strip errors
51 // from error-or-value objects.
52 template <class T> T check(ErrorOr<T> E) {
53   if (auto EC = E.getError())
54     fatal(EC.message());
55   return std::move(*E);
56 }
57
58 template <class T> T check(Expected<T> E) {
59   if (!E)
60     fatal(llvm::toString(E.takeError()));
61   return std::move(*E);
62 }
63
64 template <class T> T check(ErrorOr<T> E, const Twine &Prefix) {
65   if (auto EC = E.getError())
66     fatal(Prefix + ": " + EC.message());
67   return std::move(*E);
68 }
69
70 template <class T> T check(Expected<T> E, const Twine &Prefix) {
71   if (!E)
72     fatal(Prefix + ": " + toString(E.takeError()));
73   return std::move(*E);
74 }
75
76 } // namespace elf
77 } // namespace lld
78
79 #endif