[clangd] Add error() function for creating formatv-style llvm::Errors. NFC
Summary:
This is considerably terser than the makeStringError and friends, and
avoids verbosity cliffs that discourage adding log information.
It follows the syntax used in log/elog/vlog/dlog that have been successful.
The main caveats are:
- it's strictly out-of-place in logger.h, though kind of fits thematically and
in implementation
- it claims the "error" identifier, which seems a bit too opinionated
to put higher up in llvm
I've updated some users of StringError mostly at random - there are lots
more mechanical changes but I'd like to get this reviewed before making
them all.
Reviewers: kbobyrev, hokein
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D83419
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index 6ebb71c..4cc1fea 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -147,13 +147,9 @@
if (!InvalidFileCount)
return llvm::Error::success();
if (InvalidFileCount == 1)
- return llvm::createStringError(llvm::inconvertibleErrorCode(),
- "File must be saved first: " +
- LastInvalidFile);
- return llvm::createStringError(
- llvm::inconvertibleErrorCode(),
- "Files must be saved first: " + LastInvalidFile + " (and " +
- llvm::to_string(InvalidFileCount - 1) + " others)");
+ return error("File must be saved first: {0}", LastInvalidFile);
+ return error("Files must be saved first: {0} (and {1} others)",
+ LastInvalidFile, InvalidFileCount - 1);
}
} // namespace
@@ -284,10 +280,9 @@
}
}
if (OldestCB)
- OldestCB->second(llvm::createStringError(
- llvm::inconvertibleErrorCode(),
- llvm::formatv("failed to receive a client reply for request ({0})",
- OldestCB->first)));
+ OldestCB->second(
+ error("failed to receive a client reply for request ({0})",
+ OldestCB->first));
return ID;
}
@@ -661,8 +656,7 @@
if (Server->blockUntilIdleForTest(/*TimeoutSeconds=*/60))
Reply(nullptr);
else
- Reply(llvm::createStringError(llvm::inconvertibleErrorCode(),
- "Not idle after a minute"));
+ Reply(error("Not idle after a minute"));
}
void ClangdLSPServer::onDocumentDidOpen(
@@ -729,9 +723,7 @@
std::string Reason = Response->failureReason
? *Response->failureReason
: "unknown reason";
- return Reply(llvm::createStringError(
- llvm::inconvertibleErrorCode(),
- ("edits were not applied: " + Reason).c_str()));
+ return Reply(error("edits were not applied: {0}", Reason));
}
return Reply(SuccessMessage);
});
@@ -752,9 +744,7 @@
Params.tweakArgs) {
auto Code = DraftMgr.getDraft(Params.tweakArgs->file.file());
if (!Code)
- return Reply(llvm::createStringError(
- llvm::inconvertibleErrorCode(),
- "trying to apply a code action for a non-added file"));
+ return Reply(error("trying to apply a code action for a non-added file"));
auto Action = [this, ApplyEdit, Reply = std::move(Reply),
File = Params.tweakArgs->file, Code = std::move(*Code)](