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 lld::outs() or lld::errs() are not thread-safe. We protect them using a
33 // We want to separate multi-line messages with a newline. `sep` is "\n"
34 // if the last messages was multi-line. Otherwise "".
37 static StringRef getSeparator(const Twine &msg) {
38 if (StringRef(msg.str()).contains('\n'))
43 raw_ostream *lld::stdoutOS;
44 raw_ostream *lld::stderrOS;
46 raw_ostream &lld::outs() { return stdoutOS ? *stdoutOS : llvm::outs(); }
47 raw_ostream &lld::errs() { return stderrOS ? *stderrOS : llvm::errs(); }
49 ErrorHandler &lld::errorHandler() {
50 static ErrorHandler handler;
54 void lld::exitLld(int val) {
55 // Delete any temporary file, while keeping the memory mapping open.
56 if (errorHandler().outputBuffer)
57 errorHandler().outputBuffer->discard();
59 // Dealloc/destroy ManagedStatic variables before calling _exit().
60 // In an LTO build, allows us to get the output of -time-passes.
61 // Ensures that the thread pool for the parallel algorithms is stopped to
62 // avoid intermittent crashes on Windows when exiting.
70 void lld::diagnosticHandler(const DiagnosticInfo &di) {
72 raw_svector_ostream os(s);
73 DiagnosticPrinterRawOStream dp(os);
75 switch (di.getSeverity()) {
89 void lld::checkError(Error e) {
90 handleAllErrors(std::move(e),
91 [&](ErrorInfoBase &eib) { error(eib.message()); });
94 // This is for --vs-diagnostics.
96 // Normally, lld's error message starts with argv[0]. Therefore, it usually
101 // This error message style is unfortunately unfriendly to Visual Studio
102 // IDE. VS interprets the first word of the first line as an error location
103 // and make it clickable, thus "ld.lld" in the above message would become a
104 // clickable text. When you click it, VS opens "ld.lld" executable file with
107 // As a workaround, we print out an error location instead of "ld.lld" if
108 // lld is running in VS diagnostics mode. As a result, error message will
111 // src/foo.c(35): error: ...
113 // This function returns an error location string. An error location is
114 // extracted from an error message using regexps.
115 std::string ErrorHandler::getLocation(const Twine &msg) {
119 static std::regex regexes[] = {
121 R"(^undefined (?:\S+ )?symbol:.*\n)"
122 R"(>>> referenced by .+\((\S+):(\d+)\))"),
124 R"(^undefined (?:\S+ )?symbol:.*\n>>> referenced by (\S+):(\d+))"),
125 std::regex(R"(^undefined symbol:.*\n>>> referenced by (.*):)"),
127 R"(^duplicate symbol: .*\n>>> defined in (\S+)\n>>> defined in.*)"),
129 R"(^duplicate symbol: .*\n>>> defined at .+\((\S+):(\d+)\))"),
130 std::regex(R"(^duplicate symbol: .*\n>>> defined at (\S+):(\d+))"),
132 R"(.*\n>>> defined in .*\n>>> referenced by .+\((\S+):(\d+)\))"),
133 std::regex(R"(.*\n>>> defined in .*\n>>> referenced by (\S+):(\d+))"),
134 std::regex(R"((\S+):(\d+): unclosed quote)"),
137 std::string str = msg.str();
138 for (std::regex &re : regexes) {
140 if (!std::regex_search(str, m, re))
143 assert(m.size() == 2 || m.size() == 3);
146 return m.str(1) + "(" + m.str(2) + ")";
152 void ErrorHandler::log(const Twine &msg) {
155 std::lock_guard<std::mutex> lock(mu);
156 lld::errs() << logName << ": " << msg << "\n";
159 void ErrorHandler::message(const Twine &msg) {
160 std::lock_guard<std::mutex> lock(mu);
161 lld::outs() << msg << "\n";
165 void ErrorHandler::warn(const Twine &msg) {
171 std::lock_guard<std::mutex> lock(mu);
172 lld::errs() << sep << getLocation(msg) << ": " << Colors::MAGENTA
173 << "warning: " << Colors::RESET << msg << "\n";
174 sep = getSeparator(msg);
177 void ErrorHandler::error(const Twine &msg) {
178 // If Visual Studio-style error message mode is enabled,
179 // this particular error is printed out as two errors.
181 static std::regex re(R"(^(duplicate symbol: .*))"
182 R"((\n>>> defined at \S+:\d+.*\n>>>.*))"
183 R"((\n>>> defined at \S+:\d+.*\n>>>.*))");
184 std::string str = msg.str();
187 if (std::regex_match(str, m, re)) {
188 error(m.str(1) + m.str(2));
189 error(m.str(1) + m.str(3));
194 std::lock_guard<std::mutex> lock(mu);
196 if (errorLimit == 0 || errorCount < errorLimit) {
197 lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
198 << "error: " << Colors::RESET << msg << "\n";
199 } else if (errorCount == errorLimit) {
200 lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
201 << "error: " << Colors::RESET << errorLimitExceededMsg << "\n";
206 sep = getSeparator(msg);
210 void ErrorHandler::fatal(const Twine &msg) {