]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Error.h
Update lldb to trunk r290819 and resolve conflicts.
[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 errors in a single run.
19 //
20 // Warn doesn't do anything but printing out a given message.
21 //
22 //===----------------------------------------------------------------------===//
23
24 #ifndef LLD_ELF_ERROR_H
25 #define LLD_ELF_ERROR_H
26
27 #include "lld/Core/LLVM.h"
28
29 #include "llvm/Support/Error.h"
30
31 namespace lld {
32 namespace elf {
33
34 extern uint64_t ErrorCount;
35 extern llvm::raw_ostream *ErrorOS;
36 extern llvm::StringRef Argv0;
37
38 void log(const Twine &Msg);
39 void warn(const Twine &Msg);
40
41 void error(const Twine &Msg);
42 void error(std::error_code EC, const Twine &Prefix);
43
44 LLVM_ATTRIBUTE_NORETURN void exitLld(int Val);
45 LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg);
46 LLVM_ATTRIBUTE_NORETURN void fatal(std::error_code EC, const Twine &Prefix);
47
48 // check() functions are convenient functions to strip errors
49 // from error-or-value objects.
50 template <class T> T check(ErrorOr<T> E) {
51   if (auto EC = E.getError())
52     fatal(EC.message());
53   return std::move(*E);
54 }
55
56 template <class T> T check(Expected<T> E) {
57   if (!E)
58     handleAllErrors(std::move(E.takeError()),
59                     [](llvm::ErrorInfoBase &EIB) -> Error {
60                       fatal(EIB.message());
61                       return Error::success();
62                     });
63   return std::move(*E);
64 }
65
66 template <class T> T check(ErrorOr<T> E, const Twine &Prefix) {
67   if (auto EC = E.getError())
68     fatal(Prefix + ": " + EC.message());
69   return std::move(*E);
70 }
71
72 template <class T> T check(Expected<T> E, const Twine &Prefix) {
73   if (!E)
74     fatal(Prefix + ": " + errorToErrorCode(E.takeError()).message());
75   return std::move(*E);
76 }
77
78 } // namespace elf
79 } // namespace lld
80
81 #endif