1 //===- ErrorHandler.cpp ---------------------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lld/Common/ErrorHandler.h" 10 11 #include "lld/Common/Threads.h" 12 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" 18 #include <mutex> 19 #include <regex> 20 21 #if !defined(_MSC_VER) && !defined(__MINGW32__) 22 #include <unistd.h> 23 #endif 24 25 using namespace llvm; 26 using namespace lld; 27 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 30 // mutex. 31 static std::mutex mu; 32 33 // We want to separate multi-line messages with a newline. `sep` is "\n" 34 // if the last messages was multi-line. Otherwise "". 35 static StringRef sep; 36 37 static StringRef getSeparator(const Twine &msg) { 38 if (StringRef(msg.str()).contains('\n')) 39 return "\n"; 40 return ""; 41 } 42 43 raw_ostream *lld::stdoutOS; 44 raw_ostream *lld::stderrOS; 45 46 raw_ostream &lld::outs() { return stdoutOS ? *stdoutOS : llvm::outs(); } 47 raw_ostream &lld::errs() { return stderrOS ? *stderrOS : llvm::errs(); } 48 49 ErrorHandler &lld::errorHandler() { 50 static ErrorHandler handler; 51 return handler; 52 } 53 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(); 58 59 // Dealloc/destroy ManagedStatic variables before calling 60 // _exit(). In a non-LTO build, this is a nop. In an LTO 61 // build allows us to get the output of -time-passes. 62 llvm_shutdown(); 63 64 lld::outs().flush(); 65 lld::errs().flush(); 66 _exit(val); 67 } 68 69 void lld::diagnosticHandler(const DiagnosticInfo &di) { 70 SmallString<128> s; 71 raw_svector_ostream os(s); 72 DiagnosticPrinterRawOStream dp(os); 73 di.print(dp); 74 switch (di.getSeverity()) { 75 case DS_Error: 76 error(s); 77 break; 78 case DS_Warning: 79 warn(s); 80 break; 81 case DS_Remark: 82 case DS_Note: 83 message(s); 84 break; 85 } 86 } 87 88 void lld::checkError(Error e) { 89 handleAllErrors(std::move(e), 90 [&](ErrorInfoBase &eib) { error(eib.message()); }); 91 } 92 93 // This is for --vs-diagnostics. 94 // 95 // Normally, lld's error message starts with argv[0]. Therefore, it usually 96 // looks like this: 97 // 98 // ld.lld: error: ... 99 // 100 // This error message style is unfortunately unfriendly to Visual Studio 101 // IDE. VS interprets the first word of the first line as an error location 102 // and make it clickable, thus "ld.lld" in the above message would become a 103 // clickable text. When you click it, VS opens "ld.lld" executable file with 104 // a binary editor. 105 // 106 // As a workaround, we print out an error location instead of "ld.lld" if 107 // lld is running in VS diagnostics mode. As a result, error message will 108 // look like this: 109 // 110 // src/foo.c(35): error: ... 111 // 112 // This function returns an error location string. An error location is 113 // extracted from an error message using regexps. 114 std::string ErrorHandler::getLocation(const Twine &msg) { 115 if (!vsDiagnostics) 116 return logName; 117 118 static std::regex regexes[] = { 119 std::regex( 120 R"(^undefined (?:\S+ )?symbol:.*\n)" 121 R"(>>> referenced by .+\((\S+):(\d+)\))"), 122 std::regex( 123 R"(^undefined (?:\S+ )?symbol:.*\n>>> referenced by (\S+):(\d+))"), 124 std::regex(R"(^undefined symbol:.*\n>>> referenced by (.*):)"), 125 std::regex( 126 R"(^duplicate symbol: .*\n>>> defined in (\S+)\n>>> defined in.*)"), 127 std::regex( 128 R"(^duplicate symbol: .*\n>>> defined at .+\((\S+):(\d+)\))"), 129 std::regex(R"(^duplicate symbol: .*\n>>> defined at (\S+):(\d+))"), 130 std::regex( 131 R"(.*\n>>> defined in .*\n>>> referenced by .+\((\S+):(\d+)\))"), 132 std::regex(R"(.*\n>>> defined in .*\n>>> referenced by (\S+):(\d+))"), 133 std::regex(R"((\S+):(\d+): unclosed quote)"), 134 }; 135 136 std::string str = msg.str(); 137 for (std::regex &re : regexes) { 138 std::smatch m; 139 if (!std::regex_search(str, m, re)) 140 continue; 141 142 assert(m.size() == 2 || m.size() == 3); 143 if (m.size() == 2) 144 return m.str(1); 145 return m.str(1) + "(" + m.str(2) + ")"; 146 } 147 148 return logName; 149 } 150 151 void ErrorHandler::log(const Twine &msg) { 152 if (!verbose) 153 return; 154 std::lock_guard<std::mutex> lock(mu); 155 lld::errs() << logName << ": " << msg << "\n"; 156 } 157 158 void ErrorHandler::message(const Twine &msg) { 159 std::lock_guard<std::mutex> lock(mu); 160 lld::outs() << msg << "\n"; 161 lld::outs().flush(); 162 } 163 164 void ErrorHandler::warn(const Twine &msg) { 165 if (fatalWarnings) { 166 error(msg); 167 return; 168 } 169 170 std::lock_guard<std::mutex> lock(mu); 171 lld::errs() << sep << getLocation(msg) << ": " << Colors::MAGENTA 172 << "warning: " << Colors::RESET << msg << "\n"; 173 sep = getSeparator(msg); 174 } 175 176 void ErrorHandler::error(const Twine &msg) { 177 // If Visual Studio-style error message mode is enabled, 178 // this particular error is printed out as two errors. 179 if (vsDiagnostics) { 180 static std::regex re(R"(^(duplicate symbol: .*))" 181 R"((\n>>> defined at \S+:\d+.*\n>>>.*))" 182 R"((\n>>> defined at \S+:\d+.*\n>>>.*))"); 183 std::string str = msg.str(); 184 std::smatch m; 185 186 if (std::regex_match(str, m, re)) { 187 error(m.str(1) + m.str(2)); 188 error(m.str(1) + m.str(3)); 189 return; 190 } 191 } 192 193 std::lock_guard<std::mutex> lock(mu); 194 195 if (errorLimit == 0 || errorCount < errorLimit) { 196 lld::errs() << sep << getLocation(msg) << ": " << Colors::RED 197 << "error: " << Colors::RESET << msg << "\n"; 198 } else if (errorCount == errorLimit) { 199 lld::errs() << sep << getLocation(msg) << ": " << Colors::RED 200 << "error: " << Colors::RESET << errorLimitExceededMsg << "\n"; 201 if (exitEarly) 202 exitLld(1); 203 } 204 205 sep = getSeparator(msg); 206 ++errorCount; 207 } 208 209 void ErrorHandler::fatal(const Twine &msg) { 210 error(msg); 211 exitLld(1); 212 } 213