blob: 8ae6f46ac59e1c4a9af60f2dee7a68ff3e1fb4bb [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- ErrorHandler.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_COMMON_ERRORHANDLER_H
29#define LLD_COMMON_ERRORHANDLER_H
30
31#include "lld/Common/LLVM.h"
32
33#include "llvm/ADT/STLExtras.h"
34#include "llvm/Support/Error.h"
35#include "llvm/Support/FileOutputBuffer.h"
36
37namespace lld {
38
39class ErrorHandler {
40public:
41 uint64_t ErrorCount = 0;
42 uint64_t ErrorLimit = 20;
43 StringRef ErrorLimitExceededMsg = "too many errors emitted, stopping now";
44 StringRef LogName = "lld";
45 llvm::raw_ostream *ErrorOS = &llvm::errs();
46 bool ColorDiagnostics = llvm::errs().has_colors();
47 bool ExitEarly = true;
48 bool FatalWarnings = false;
49 bool Verbose = false;
50
51 void error(const Twine &Msg);
52 LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg);
53 void log(const Twine &Msg);
54 void message(const Twine &Msg);
55 void warn(const Twine &Msg);
56
57 std::unique_ptr<llvm::FileOutputBuffer> OutputBuffer;
58
59private:
60 void print(StringRef S, raw_ostream::Colors C);
61};
62
63/// Returns the default error handler.
64ErrorHandler &errorHandler();
65
66inline void error(const Twine &Msg) { errorHandler().error(Msg); }
67inline LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg) {
68 errorHandler().fatal(Msg);
69}
70inline void log(const Twine &Msg) { errorHandler().log(Msg); }
71inline void message(const Twine &Msg) { errorHandler().message(Msg); }
72inline void warn(const Twine &Msg) { errorHandler().warn(Msg); }
73inline uint64_t errorCount() { return errorHandler().ErrorCount; }
74
75LLVM_ATTRIBUTE_NORETURN void exitLld(int Val);
76
77// check functions are convenient functions to strip errors
78// from error-or-value objects.
79template <class T> T check(ErrorOr<T> E) {
80 if (auto EC = E.getError())
81 fatal(EC.message());
82 return std::move(*E);
83}
84
85template <class T> T check(Expected<T> E) {
86 if (!E)
87 fatal(llvm::toString(E.takeError()));
88 return std::move(*E);
89}
90
91template <class T>
92T check2(ErrorOr<T> E, llvm::function_ref<std::string()> Prefix) {
93 if (auto EC = E.getError())
94 fatal(Prefix() + ": " + EC.message());
95 return std::move(*E);
96}
97
98template <class T>
99T check2(Expected<T> E, llvm::function_ref<std::string()> Prefix) {
100 if (!E)
101 fatal(Prefix() + ": " + toString(E.takeError()));
102 return std::move(*E);
103}
104
105inline std::string toString(const Twine &S) { return S.str(); }
106
107// To evaluate the second argument lazily, we use C macro.
108#define CHECK(E, S) check2(E, [&] { return toString(S); })
109
110} // namespace lld
111
112#endif