[PATCH] D117456: [clangd] Avoid a code completion crash

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 17 00:11:43 PST 2022


hokein created this revision.
hokein added a reviewer: sammccall.
Herald added subscribers: usaxena95, kadircet, arphaman.
hokein requested review of this revision.
Herald added subscribers: MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117456

Files:
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp


Index: clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -2474,6 +2474,26 @@
   )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]]^",
Index: clang-tools-extra/clangd/ClangdServer.cpp
===================================================================
--- clang-tools-extra/clangd/ClangdServer.cpp
+++ clang-tools-extra/clangd/ClangdServer.cpp
@@ -369,6 +369,10 @@
       }
     }
     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 @@
       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));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117456.400453.patch
Type: text/x-patch
Size: 2305 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220117/8400052b/attachment.bin>


More information about the cfe-commits mailing list