[clang-tools-extra] r322080 - [clangd] Fix a bug in asynchronous code completion

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 9 06:39:27 PST 2018


Author: ibiryukov
Date: Tue Jan  9 06:39:27 2018
New Revision: 322080

URL: http://llvm.org/viewvc/llvm-project?rev=322080&view=rev
Log:
[clangd] Fix a bug in asynchronous code completion

A StringRef that goes out of scope was copied and used in a handler
running on a separate thread.
We didn't catch it because clangd does not use the async completion
API yet.

Modified:
    clang-tools-extra/trunk/clangd/ClangdServer.cpp

Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.cpp?rev=322080&r1=322079&r2=322080&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdServer.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdServer.cpp Tue Jan  9 06:39:27 2018
@@ -251,10 +251,17 @@ void ClangdServer::codeComplete(
   auto CodeCompleteOpts = Opts;
   if (FileIdx)
     CodeCompleteOpts.Index = FileIdx.get();
+
+  // Copy File, as it is a PathRef that will go out of scope before Task is
+  // executed.
+  Path FileStr = File;
+  // Copy PCHs to avoid accessing this->PCHs concurrently
+  std::shared_ptr<PCHContainerOperations> PCHs = this->PCHs;
   // A task that will be run asynchronously.
   auto Task =
       // 'mutable' to reassign Preamble variable.
-      [=](Context Ctx, CallbackType Callback) mutable {
+      [FileStr, Preamble, Resources, Contents, Pos, CodeCompleteOpts, TaggedFS,
+       PCHs](Context Ctx, CallbackType Callback) mutable {
         if (!Preamble) {
           // Maybe we built some preamble before processing this request.
           Preamble = Resources->getPossiblyStalePreamble();
@@ -263,7 +270,7 @@ void ClangdServer::codeComplete(
         // both the old and the new version in case only one of them matches.
 
         CompletionList Result = clangd::codeComplete(
-            Ctx, File, Resources->getCompileCommand(),
+            Ctx, FileStr, Resources->getCompileCommand(),
             Preamble ? &Preamble->Preamble : nullptr, Contents, Pos,
             TaggedFS.Value, PCHs, CodeCompleteOpts);
 




More information about the cfe-commits mailing list