diff options
Diffstat (limited to 'lld/Common/ErrorHandler.cpp')
| -rw-r--r-- | lld/Common/ErrorHandler.cpp | 100 |
1 files changed, 41 insertions, 59 deletions
diff --git a/lld/Common/ErrorHandler.cpp b/lld/Common/ErrorHandler.cpp index 68ea0a10bf5..592b5b508da 100644 --- a/lld/Common/ErrorHandler.cpp +++ b/lld/Common/ErrorHandler.cpp @@ -85,54 +85,41 @@ void lld::checkError(Error e) { [&](ErrorInfoBase &eib) { error(eib.message()); }); } -static std::string getLocation(std::string msg, std::string defaultMsg) { - static std::vector<std::regex> Regexes{ - std::regex(R"(^undefined (?:\S+ )?symbol:.*\n>>> referenced by (\S+):(\d+)\n.*)"), +std::string ErrorHandler::getLocation(const Twine &msg) { + if (!vsDiagnostics) + return logName; + + static std::regex regexes[] = { + std::regex( + R"(^undefined (?:\S+ )?symbol:.*\n>>> referenced by (\S+):(\d+)\n.*)"), std::regex(R"(^undefined symbol:.*\n>>> referenced by (.*):)"), std::regex( R"(^duplicate symbol: .*\n>>> defined in (\S+)\n>>> defined in.*)"), - std::regex( - R"(^duplicate symbol: .*\n>>> defined at (\S+):(\d+).*)"), - std::regex( - R"(.*\n>>> defined in .*\n>>> referenced by (\S+):(\d+))"), + std::regex(R"(^duplicate symbol: .*\n>>> defined at (\S+):(\d+).*)"), + std::regex(R"(.*\n>>> defined in .*\n>>> referenced by (\S+):(\d+))"), std::regex(R"((\S+):(\d+): unclosed quote)"), }; - std::smatch Match; - for (std::regex &Re : Regexes) { - if (std::regex_search(msg, Match, Re)) { - return Match.size() > 2 ? Match.str(1) + "(" + Match.str(2) + ")" - : Match.str(1); - } - } - return defaultMsg; -} + std::string str = msg.str(); -void ErrorHandler::printHeader(StringRef s, raw_ostream::Colors c, - const Twine &msg) { + for (std::regex &re : regexes) { + std::smatch match; + if (!std::regex_search(str, match, re)) + continue; - if (vsDiagnostics) { - // A Visual Studio-style error message starts with an error location. - // If a location cannot be extracted then we default to LogName. - *errorOS << getLocation(msg.str(), logName) << ": "; - } else { - *errorOS << logName << ": "; + if (match.size() > 2) + return match.str(1) + "(" + match.str(2) + ")"; + return match.str(1); } - if (colorDiagnostics) { - errorOS->changeColor(c, true); - *errorOS << s; - errorOS->resetColor(); - } else { - *errorOS << s; - } + return logName; } void ErrorHandler::log(const Twine &msg) { - if (verbose) { - std::lock_guard<std::mutex> lock(mu); - *errorOS << logName << ": " << msg << "\n"; - } + if (!verbose) + return; + std::lock_guard<std::mutex> lock(mu); + *errorOS << logName << ": " << msg << "\n"; } void ErrorHandler::message(const Twine &msg) { @@ -149,42 +136,37 @@ void ErrorHandler::warn(const Twine &msg) { std::lock_guard<std::mutex> lock(mu); newline(errorOS, msg); - printHeader("warning: ", raw_ostream::MAGENTA, msg); - *errorOS << msg << "\n"; -} - -void ErrorHandler::printErrorMsg(const Twine &msg) { - newline(errorOS, msg); - printHeader("error: ", raw_ostream::RED, msg); - *errorOS << msg << "\n"; + *errorOS << getLocation(msg) << ": " << Color::MAGENTA + << "warning: " << Color::RESET << msg << "\n"; } -void ErrorHandler::printError(const Twine &msg) { +void ErrorHandler::error(const Twine &msg) { + // If Microsoft Visual Studio-style error message mode is enabled, + // this particular error is printed out as two errors. if (vsDiagnostics) { - static std::regex reDuplicateSymbol( - R"(^(duplicate symbol: .*))" - R"((\n>>> defined at \S+:\d+\n>>>.*))" - R"((\n>>> defined at \S+:\d+\n>>>.*))"); - std::string msgStr = msg.str(); - std::smatch match; - if (std::regex_match(msgStr, match, reDuplicateSymbol)) { - printErrorMsg(match.str(1) + match.str(2)); - printErrorMsg(match.str(1) + match.str(3)); + static std::regex re(R"(^(duplicate symbol: .*))" + R"((\n>>> defined at \S+:\d+\n>>>.*))" + R"((\n>>> defined at \S+:\d+\n>>>.*))"); + std::string str = msg.str(); + std::smatch m; + + if (std::regex_match(str, m, re)) { + error(m.str(1) + m.str(2)); + error(m.str(1) + m.str(3)); return; } } - printErrorMsg(msg); -} -void ErrorHandler::error(const Twine &msg) { std::lock_guard<std::mutex> lock(mu); if (errorLimit == 0 || errorCount < errorLimit) { - printError(msg); + newline(errorOS, msg); + *errorOS << getLocation(msg) << ": " << Color::RED + << "error: " << Color::RESET << msg << "\n"; } else if (errorCount == errorLimit) { newline(errorOS, msg); - printHeader("error: ", raw_ostream::RED, msg); - *errorOS << errorLimitExceededMsg << "\n"; + *errorOS << getLocation(msg) << ": " << Color::RED + << "error: " << Color::RESET << errorLimitExceededMsg << "\n"; if (exitEarly) exitLld(1); } |

