Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- ErrorHandler.h -------------------------------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 9 | // We designed lld's error handlers with the following goals in mind: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 10 | // |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 11 | // - 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 15 | // |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 16 | // 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 21 | // |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 22 | // - 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 27 | // |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 28 | // - 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 | // |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 62 | // It is not recommended to use llvm::outs() or lld::errs() directly in lld |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 63 | // because they are not thread-safe. The functions declared in this file are |
| 64 | // thread-safe. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 65 | // |
| 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 77 | namespace llvm { |
| 78 | class DiagnosticInfo; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 79 | class raw_ostream; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 82 | namespace lld { |
| 83 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 84 | // We wrap stdout and stderr so that you can pass alternative stdout/stderr as |
| 85 | // arguments to lld::*::link() functions. |
| 86 | extern llvm::raw_ostream *stdoutOS; |
| 87 | extern llvm::raw_ostream *stderrOS; |
| 88 | |
| 89 | llvm::raw_ostream &outs(); |
| 90 | llvm::raw_ostream &errs(); |
| 91 | |
| 92 | enum class ErrorTag { LibNotFound, SymbolNotFound }; |
| 93 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 94 | class ErrorHandler { |
| 95 | public: |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 96 | uint64_t errorCount = 0; |
| 97 | uint64_t errorLimit = 20; |
| 98 | StringRef errorLimitExceededMsg = "too many errors emitted, stopping now"; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 99 | StringRef errorHandlingScript; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 100 | StringRef logName = "lld"; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 101 | bool exitEarly = true; |
| 102 | bool fatalWarnings = false; |
| 103 | bool verbose = false; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 104 | bool vsDiagnostics = false; |
| 105 | bool disableOutput = false; |
| 106 | std::function<void()> cleanupCallback; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 107 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 108 | void error(const Twine &msg); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 109 | void error(const Twine &msg, ErrorTag tag, ArrayRef<StringRef> args); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 110 | LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &msg); |
| 111 | void log(const Twine &msg); |
| 112 | void message(const Twine &msg); |
| 113 | void warn(const Twine &msg); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 114 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 115 | void reset() { |
| 116 | if (cleanupCallback) |
| 117 | cleanupCallback(); |
| 118 | *this = ErrorHandler(); |
| 119 | } |
| 120 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 121 | std::unique_ptr<llvm::FileOutputBuffer> outputBuffer; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 122 | |
| 123 | private: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 124 | using Colors = raw_ostream::Colors; |
| 125 | |
| 126 | std::string getLocation(const Twine &msg); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | /// Returns the default error handler. |
| 130 | ErrorHandler &errorHandler(); |
| 131 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 132 | inline void error(const Twine &msg) { errorHandler().error(msg); } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 133 | inline void error(const Twine &msg, ErrorTag tag, ArrayRef<StringRef> args) { |
| 134 | errorHandler().error(msg, tag, args); |
| 135 | } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 136 | inline LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &msg) { |
| 137 | errorHandler().fatal(msg); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 138 | } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 139 | inline void log(const Twine &msg) { errorHandler().log(msg); } |
| 140 | inline void message(const Twine &msg) { errorHandler().message(msg); } |
| 141 | inline void warn(const Twine &msg) { errorHandler().warn(msg); } |
| 142 | inline uint64_t errorCount() { return errorHandler().errorCount; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 143 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 144 | LLVM_ATTRIBUTE_NORETURN void exitLld(int val); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 145 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 146 | void diagnosticHandler(const llvm::DiagnosticInfo &di); |
| 147 | void checkError(Error e); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 148 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 149 | // check functions are convenient functions to strip errors |
| 150 | // from error-or-value objects. |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 151 | template <class T> T check(ErrorOr<T> e) { |
| 152 | if (auto ec = e.getError()) |
| 153 | fatal(ec.message()); |
| 154 | return std::move(*e); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 157 | template <class T> T check(Expected<T> e) { |
| 158 | if (!e) |
| 159 | fatal(llvm::toString(e.takeError())); |
| 160 | return std::move(*e); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 163 | // Don't move from Expected wrappers around references. |
| 164 | template <class T> T &check(Expected<T &> e) { |
| 165 | if (!e) |
| 166 | fatal(llvm::toString(e.takeError())); |
| 167 | return *e; |
| 168 | } |
| 169 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 170 | template <class T> |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 171 | T check2(ErrorOr<T> e, llvm::function_ref<std::string()> prefix) { |
| 172 | if (auto ec = e.getError()) |
| 173 | fatal(prefix() + ": " + ec.message()); |
| 174 | return std::move(*e); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | template <class T> |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 178 | T check2(Expected<T> e, llvm::function_ref<std::string()> prefix) { |
| 179 | if (!e) |
| 180 | fatal(prefix() + ": " + toString(e.takeError())); |
| 181 | return std::move(*e); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 184 | inline std::string toString(const Twine &s) { return s.str(); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 185 | |
| 186 | // To evaluate the second argument lazily, we use C macro. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 187 | #define CHECK(E, S) check2((E), [&] { return toString(S); }) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 188 | |
| 189 | } // namespace lld |
| 190 | |
| 191 | #endif |