diff --git a/lld/Common/ErrorHandler.cpp b/lld/Common/ErrorHandler.cpp --- a/lld/Common/ErrorHandler.cpp +++ b/lld/Common/ErrorHandler.cpp @@ -155,8 +155,11 @@ void ErrorHandler::log(const Twine &msg) { if (!verbose) return; + SmallString<256> buf; + raw_svector_ostream os(buf); + os << logName << ": " << msg << '\n'; std::lock_guard lock(mu); - lld::errs() << logName << ": " << msg << "\n"; + lld::errs() << buf; } void ErrorHandler::message(const Twine &msg) { @@ -171,9 +174,21 @@ return; } - std::lock_guard lock(mu); - lld::errs() << sep << getLocation(msg) << ": " << Colors::MAGENTA - << "warning: " << Colors::RESET << msg << "\n"; + SmallString<256> buf; + raw_svector_ostream os(buf); + os << sep << getLocation(msg) << ": "; + if (lld::errs().colors_enabled()) { + os.enable_colors(true); + os << Colors::MAGENTA << "warning: " << Colors::RESET; + } else { + os << "warning: "; + } + os << msg << '\n'; + + { + std::lock_guard lock(mu); + lld::errs() << buf; + } sep = getSeparator(msg); } @@ -196,16 +211,25 @@ bool exit = false; { - std::lock_guard lock(mu); + if (errorLimit == 0 || errorCount <= errorLimit) { + SmallString<256> buf; + raw_svector_ostream os(buf); + os << sep << getLocation(msg) << ": "; + if (lld::errs().colors_enabled()) { + os.enable_colors(true); + os << Colors::RED << "error: " << Colors::RESET; + } else { + os << "error: "; + } + if (errorLimit == 0 || errorCount < errorLimit) { + os << msg << '\n'; + } else { + os << errorLimitExceededMsg << '\n'; + exit = exitEarly; + } - if (errorLimit == 0 || errorCount < errorLimit) { - lld::errs() << sep << getLocation(msg) << ": " << Colors::RED - << "error: " << Colors::RESET << msg << "\n"; - } else if (errorCount == errorLimit) { - lld::errs() << sep << getLocation(msg) << ": " << Colors::RED - << "error: " << Colors::RESET << errorLimitExceededMsg - << "\n"; - exit = exitEarly; + std::lock_guard lock(mu); + lld::errs() << buf; } sep = getSeparator(msg); diff --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h --- a/llvm/include/llvm/Support/raw_ostream.h +++ b/llvm/include/llvm/Support/raw_ostream.h @@ -301,6 +301,8 @@ // changeColor() has no effect until enable_colors(true) is called. virtual void enable_colors(bool enable) { ColorEnabled = enable; } + bool colors_enabled() const { return ColorEnabled; } + /// Tie this stream to the specified stream. Replaces any existing tied-to /// stream. Specifying a nullptr unties the stream. void tie(raw_ostream *TieTo) { TiedStream = TieTo; }