[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.

llvm-svn: 322080
This commit is contained in:
Ilya Biryukov 2018-01-09 14:39:27 +00:00
parent 47dbaf6c8e
commit f6e2b4cb4b
1 changed files with 9 additions and 2 deletions

View File

@ -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);