blob: bd9c257ed801ca8fd77949133ad83c8da2173b67 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- ErrorHandler.h -------------------------------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
Andrew Scullcdfcccc2018-10-05 20:58:37 +01009// We designed lld's error handlers with the following goals in mind:
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010010//
Andrew Scullcdfcccc2018-10-05 20:58:37 +010011// - Errors can occur at any place where we handle user input, but we don't
12// want them to affect the normal execution path too much. Ideally,
13// handling errors should be as simple as reporting them and exit (but
14// without actually doing exit).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010015//
Andrew Scullcdfcccc2018-10-05 20:58:37 +010016// In particular, the design to wrap all functions that could fail with
17// ErrorOr<T> is rejected because otherwise we would have to wrap a large
18// number of functions in lld with ErrorOr. With that approach, if some
19// function F can fail, not only F but all functions that transitively call
20// F have to be wrapped with ErrorOr. That seemed too much.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010021//
Andrew Scullcdfcccc2018-10-05 20:58:37 +010022// - Finding only one error at a time is not sufficient. We want to find as
23// many errors as possible with one execution of the linker. That means the
24// linker needs to keep running after a first error and give up at some
25// checkpoint (beyond which it would find cascading, false errors caused by
26// the previous errors).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027//
Andrew Scullcdfcccc2018-10-05 20:58:37 +010028// - We want a simple interface to report errors. Unlike Clang, the data we
29// handle is compiled binary, so we don't need an error reporting mechanism
30// that's as sophisticated as the one that Clang has.
31//
32// The current lld's error handling mechanism is simple:
33//
34// - When you find an error, report it using error() and continue as far as
35// you can. An internal error counter is incremented by one every time you
36// call error().
37//
38// A common idiom to handle an error is calling error() and then returning
39// a reasonable default value. For example, if your function handles a
40// user-supplied alignment value, and if you find an invalid alignment
41// (e.g. 17 which is not 2^n), you may report it using error() and continue
42// as if it were alignment 1 (which is the simplest reasonable value).
43//
44// Note that you should not continue with an invalid value; that breaks the
45// internal consistency. You need to maintain all variables have some sane
46// value even after an error occurred. So, when you have to continue with
47// some value, always use a dummy value.
48//
49// - Find a reasonable checkpoint at where you want to stop the linker, and
50// add code to return from the function if errorCount() > 0. In most cases,
51// a checkpoint already exists, so you don't need to do anything for this.
52//
53// This interface satisfies all the goals that we mentioned above.
54//
55// You should never call fatal() except for reporting a corrupted input file.
56// fatal() immediately terminates the linker, so the function is not desirable
57// if you are using lld as a subroutine in other program, and with that you
58// can find only one error at a time.
59//
60// warn() doesn't do anything but printing out a given message.
61//
62// It is not recommended to use llvm::outs() or llvm::errs() directly in lld
63// because they are not thread-safe. The functions declared in this file are
64// thread-safe.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010065//
66//===----------------------------------------------------------------------===//
67
68#ifndef LLD_COMMON_ERRORHANDLER_H
69#define LLD_COMMON_ERRORHANDLER_H
70
71#include "lld/Common/LLVM.h"
72
73#include "llvm/ADT/STLExtras.h"
74#include "llvm/Support/Error.h"
75#include "llvm/Support/FileOutputBuffer.h"
76
Andrew Scullcdfcccc2018-10-05 20:58:37 +010077namespace llvm {
78class DiagnosticInfo;
79}
80
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010081namespace lld {
82
83class ErrorHandler {
84public:
85 uint64_t ErrorCount = 0;
86 uint64_t ErrorLimit = 20;
87 StringRef ErrorLimitExceededMsg = "too many errors emitted, stopping now";
88 StringRef LogName = "lld";
89 llvm::raw_ostream *ErrorOS = &llvm::errs();
90 bool ColorDiagnostics = llvm::errs().has_colors();
91 bool ExitEarly = true;
92 bool FatalWarnings = false;
93 bool Verbose = false;
94
95 void error(const Twine &Msg);
96 LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg);
97 void log(const Twine &Msg);
98 void message(const Twine &Msg);
99 void warn(const Twine &Msg);
100
101 std::unique_ptr<llvm::FileOutputBuffer> OutputBuffer;
102
103private:
104 void print(StringRef S, raw_ostream::Colors C);
105};
106
107/// Returns the default error handler.
108ErrorHandler &errorHandler();
109
110inline void error(const Twine &Msg) { errorHandler().error(Msg); }
111inline LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg) {
112 errorHandler().fatal(Msg);
113}
114inline void log(const Twine &Msg) { errorHandler().log(Msg); }
115inline void message(const Twine &Msg) { errorHandler().message(Msg); }
116inline void warn(const Twine &Msg) { errorHandler().warn(Msg); }
117inline uint64_t errorCount() { return errorHandler().ErrorCount; }
118
119LLVM_ATTRIBUTE_NORETURN void exitLld(int Val);
120
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100121void diagnosticHandler(const llvm::DiagnosticInfo &DI);
122void checkError(Error E);
123
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100124// check functions are convenient functions to strip errors
125// from error-or-value objects.
126template <class T> T check(ErrorOr<T> E) {
127 if (auto EC = E.getError())
128 fatal(EC.message());
129 return std::move(*E);
130}
131
132template <class T> T check(Expected<T> E) {
133 if (!E)
134 fatal(llvm::toString(E.takeError()));
135 return std::move(*E);
136}
137
138template <class T>
139T check2(ErrorOr<T> E, llvm::function_ref<std::string()> Prefix) {
140 if (auto EC = E.getError())
141 fatal(Prefix() + ": " + EC.message());
142 return std::move(*E);
143}
144
145template <class T>
146T check2(Expected<T> E, llvm::function_ref<std::string()> Prefix) {
147 if (!E)
148 fatal(Prefix() + ": " + toString(E.takeError()));
149 return std::move(*E);
150}
151
152inline std::string toString(const Twine &S) { return S.str(); }
153
154// To evaluate the second argument lazily, we use C macro.
Andrew Scull0372a572018-11-16 15:47:06 +0000155#define CHECK(E, S) check2((E), [&] { return toString(S); })
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100156
157} // namespace lld
158
159#endif