1 //===- ErrorHandler.cpp ---------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lld/Common/ErrorHandler.h"
11 #include "lld/Common/Threads.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/IR/DiagnosticInfo.h"
15 #include "llvm/IR/DiagnosticPrinter.h"
16 #include "llvm/Support/ManagedStatic.h"
17 #include "llvm/Support/raw_ostream.h"
21 #if !defined(_MSC_VER) && !defined(__MINGW32__)
28 // The functions defined in this file can be called from multiple threads,
29 // but outs() or errs() are not thread-safe. We protect them using a mutex.
32 // Prints "\n" or does nothing, depending on Msg contents of
33 // the previous call of this function.
34 static void newline(raw_ostream *errorOS, const Twine &msg) {
35 // True if the previous error message contained "\n".
36 // We want to separate multi-line error messages with a newline.
41 flag = StringRef(msg.str()).contains('\n');
44 ErrorHandler &lld::errorHandler() {
45 static ErrorHandler handler;
49 void lld::exitLld(int val) {
50 // Delete any temporary file, while keeping the memory mapping open.
51 if (errorHandler().outputBuffer)
52 errorHandler().outputBuffer->discard();
54 // Dealloc/destroy ManagedStatic variables before calling
55 // _exit(). In a non-LTO build, this is a nop. In an LTO
56 // build allows us to get the output of -time-passes.
64 void lld::diagnosticHandler(const DiagnosticInfo &di) {
66 raw_svector_ostream os(s);
67 DiagnosticPrinterRawOStream dp(os);
69 switch (di.getSeverity()) {
83 void lld::checkError(Error e) {
84 handleAllErrors(std::move(e),
85 [&](ErrorInfoBase &eib) { error(eib.message()); });
88 static std::string getLocation(std::string msg, std::string defaultMsg) {
89 static std::vector<std::regex> Regexes{
90 std::regex(R"(^undefined symbol:.*\n>>> referenced by (\S+):(\d+)\n.*)"),
91 std::regex(R"(^undefined symbol:.*\n>>> referenced by (.*):)"),
93 R"(^duplicate symbol: .*\n>>> defined in (\S+)\n>>> defined in.*)"),
95 R"(^duplicate symbol: .*\n>>> defined at (\S+):(\d+).*)"),
97 R"(.*\n>>> defined in .*\n>>> referenced by (\S+):(\d+))"),
99 R"(^undefined (internal|hidden|protected) symbol: .*\n>>> referenced by (\S+):(\d+)\n.*)"),
100 std::regex(R"((\S+):(\d+): unclosed quote)"),
104 for (std::regex &Re : Regexes) {
105 if (std::regex_search(msg, Match, Re)) {
106 return Match.size() > 2 ? Match.str(1) + "(" + Match.str(2) + ")"
113 void ErrorHandler::printHeader(StringRef s, raw_ostream::Colors c,
117 // A Visual Studio-style error message starts with an error location.
118 // If a location cannot be extracted then we default to LogName.
119 *errorOS << getLocation(msg.str(), logName) << ": ";
121 *errorOS << logName << ": ";
124 if (colorDiagnostics) {
125 errorOS->changeColor(c, true);
127 errorOS->resetColor();
133 void ErrorHandler::log(const Twine &msg) {
135 std::lock_guard<std::mutex> lock(mu);
136 *errorOS << logName << ": " << msg << "\n";
140 void ErrorHandler::message(const Twine &msg) {
141 std::lock_guard<std::mutex> lock(mu);
142 outs() << msg << "\n";
146 void ErrorHandler::warn(const Twine &msg) {
152 std::lock_guard<std::mutex> lock(mu);
153 newline(errorOS, msg);
154 printHeader("warning: ", raw_ostream::MAGENTA, msg);
155 *errorOS << msg << "\n";
158 void ErrorHandler::error(const Twine &msg) {
159 std::lock_guard<std::mutex> lock(mu);
160 newline(errorOS, msg);
162 if (errorLimit == 0 || errorCount < errorLimit) {
163 printHeader("error: ", raw_ostream::RED, msg);
164 *errorOS << msg << "\n";
165 } else if (errorCount == errorLimit) {
166 printHeader("error: ", raw_ostream::RED, msg);
167 *errorOS << errorLimitExceededMsg << "\n";
175 void ErrorHandler::fatal(const Twine &msg) {