[clang-tools-extra] 8848324 - [clangd] Avoid a code completion crash
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 17 01:40:49 PST 2022
Author: Haojian Wu
Date: 2022-01-17T10:35:55+01:00
New Revision: 884832407e47579a28e8c363492a620901f2eab4
URL: https://github.com/llvm/llvm-project/commit/884832407e47579a28e8c363492a620901f2eab4
DIFF: https://github.com/llvm/llvm-project/commit/884832407e47579a28e8c363492a620901f2eab4.diff
LOG: [clangd] Avoid a code completion crash
This is a workaround (adding a newline to the eof) in clangd to avoid the code
completion crash, see https://github.com/clangd/clangd/issues/332.
In principle, this is a clang bug, we should fix it in clang, but it is not
trivial.
Reviewed By: sammccall
Differential Revision: https://reviews.llvm.org/D117456
Added:
Modified:
clang-tools-extra/clangd/ClangdServer.cpp
clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp
index 29d5d9dfe1d1..34fc71f70bb8 100644
--- a/clang-tools-extra/clangd/ClangdServer.cpp
+++ b/clang-tools-extra/clangd/ClangdServer.cpp
@@ -369,6 +369,10 @@ void ClangdServer::codeComplete(PathRef File, Position Pos,
}
}
ParseInputs ParseInput{IP->Command, &TFS, IP->Contents.str()};
+ // FIXME: Add traling new line if there is none at eof, workaround a crash,
+ // see https://github.com/clangd/clangd/issues/332
+ if (!IP->Contents.endswith("\n"))
+ ParseInput.Contents.append("\n");
ParseInput.Index = Index;
CodeCompleteOpts.MainFileSignals = IP->Signals;
@@ -417,6 +421,10 @@ void ClangdServer::signatureHelp(PathRef File, Position Pos,
return CB(error("Failed to parse includes"));
ParseInputs ParseInput{IP->Command, &TFS, IP->Contents.str()};
+ // FIXME: Add traling new line if there is none at eof, workaround a crash,
+ // see https://github.com/clangd/clangd/issues/332
+ if (!IP->Contents.endswith("\n"))
+ ParseInput.Contents.append("\n");
ParseInput.Index = Index;
CB(clangd::signatureHelp(File, Pos, *PreambleData, ParseInput,
DocumentationFormat));
diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
index e3bf5ebdb0a7..c602627c3595 100644
--- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -2474,6 +2474,26 @@ TEST(CompletionTest, OverridesNonIdentName) {
)cpp");
}
+TEST(CompletionTest, NoCrashOnMissingNewLineAtEOF) {
+ auto FooCpp = testPath("foo.cpp");
+
+ MockCompilationDatabase CDB;
+ MockFS FS;
+ Annotations F("#pragma ^ // no new line");
+ FS.Files[FooCpp] = F.code().str();
+ ClangdServer Server(CDB, FS, ClangdServer::optsForTest());
+ runAddDocument(Server, FooCpp, F.code());
+ // Run completion outside the file range.
+ EXPECT_THAT(cantFail(runCodeComplete(Server, FooCpp, F.point(),
+ clangd::CodeCompleteOptions()))
+ .Completions,
+ IsEmpty());
+ EXPECT_THAT(cantFail(runSignatureHelp(Server, FooCpp, F.point(),
+ MarkupKind::PlainText))
+ .signatures,
+ IsEmpty());
+}
+
TEST(GuessCompletionPrefix, Filters) {
for (llvm::StringRef Case : {
"[[scope::]][[ident]]^",
More information about the cfe-commits
mailing list