Skip to content

Commit

Permalink
Simplify error message output. NFC.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D65855

llvm-svn: 368144
  • Loading branch information
rui314 committed Aug 7, 2019
1 parent efd0e11 commit 6c5fc94
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions lld/Common/ErrorHandler.cpp
Expand Up @@ -29,16 +29,14 @@ using namespace lld;
// but outs() or errs() are not thread-safe. We protect them using a mutex.
static std::mutex mu;

// Prints "\n" or does nothing, depending on Msg contents of
// the previous call of this function.
static void newline(raw_ostream *errorOS, const Twine &msg) {
// True if the previous error message contained "\n".
// We want to separate multi-line error messages with a newline.
static bool flag;

if (flag)
*errorOS << "\n";
flag = StringRef(msg.str()).contains('\n');
// We want to separate multi-line messages with a newline. `sep` is "\n"
// if the last messages was multi-line. Otherwise "".
static StringRef sep;

static StringRef getSeparator(const Twine &msg) {
if (StringRef(msg.str()).contains('\n'))
return "\n";
return "";
}

ErrorHandler &lld::errorHandler() {
Expand Down Expand Up @@ -160,9 +158,9 @@ void ErrorHandler::warn(const Twine &msg) {
}

std::lock_guard<std::mutex> lock(mu);
newline(errorOS, msg);
*errorOS << getLocation(msg) << ": " << Colors::MAGENTA
*errorOS << sep << getLocation(msg) << ": " << Colors::MAGENTA
<< "warning: " << Colors::RESET << msg << "\n";
sep = getSeparator(msg);
}

void ErrorHandler::error(const Twine &msg) {
Expand All @@ -185,17 +183,16 @@ void ErrorHandler::error(const Twine &msg) {
std::lock_guard<std::mutex> lock(mu);

if (errorLimit == 0 || errorCount < errorLimit) {
newline(errorOS, msg);
*errorOS << getLocation(msg) << ": " << Colors::RED
*errorOS << sep << getLocation(msg) << ": " << Colors::RED
<< "error: " << Colors::RESET << msg << "\n";
} else if (errorCount == errorLimit) {
newline(errorOS, msg);
*errorOS << getLocation(msg) << ": " << Colors::RED
*errorOS << sep << getLocation(msg) << ": " << Colors::RED
<< "error: " << Colors::RESET << errorLimitExceededMsg << "\n";
if (exitEarly)
exitLld(1);
}

sep = getSeparator(msg);
++errorCount;
}

Expand Down

0 comments on commit 6c5fc94

Please sign in to comment.