class ASTWorker;
}
+static const clang::clangd::Key<std::string> kFileBeingProcessed;
+
+llvm::Optional<llvm::StringRef> TUScheduler::getFileBeingProcessedInContext() {
+ if (auto *File = Context::current().get(kFileBeingProcessed))
+ return StringRef(*File);
+ return llvm::None;
+}
+
/// An LRU cache of idle ASTs.
/// Because we want to limit the overall number of these we retain, the cache
/// owns ASTs (and may evict them) while their workers are idle.
{
std::lock_guard<std::mutex> Lock(Mutex);
assert(!Done && "running a task after stop()");
- Requests.push_back({std::move(Task), Name, steady_clock::now(),
- Context::current().clone(), UpdateType});
+ Requests.push_back(
+ {std::move(Task), Name, steady_clock::now(),
+ Context::current().derive(kFileBeingProcessed, FileName), UpdateType});
}
RequestsCV.notify_all();
}
Action(InputsAndPreamble{Contents, Command, Preamble.get()});
};
- PreambleTasks->runAsync("task:" + llvm::sys::path::filename(File),
- Bind(Task, std::string(Name), std::string(File),
- It->second->Contents, It->second->Command,
- Context::current().clone(), std::move(Action)));
+ PreambleTasks->runAsync(
+ "task:" + llvm::sys::path::filename(File),
+ Bind(Task, std::string(Name), std::string(File), It->second->Contents,
+ It->second->Command,
+ Context::current().derive(kFileBeingProcessed, File),
+ std::move(Action)));
}
std::vector<std::pair<Path, std::size_t>>
/// an LRU cache.
class ASTCache;
+ // The file being built/processed in the current thread. This is a hack in
+ // order to get the file name into the index implementations. Do not depend on
+ // this inside clangd.
+ // FIXME: remove this when there is proper index support via build system
+ // integration.
+ static llvm::Optional<llvm::StringRef> getFileBeingProcessedInContext();
+
private:
const bool StorePreamblesInMemory;
const std::shared_ptr<PCHContainerOperations> PCHOps;
llvm::Optional<AsyncTaskRunner> WorkerThreads;
std::chrono::steady_clock::duration UpdateDebounce;
};
+
} // namespace clangd
} // namespace clang
{
WithContextValue WithNonce(NonceKey, ++Nonce);
S.update(File, Inputs, WantDiagnostics::Auto,
- [Nonce, &Mut,
+ [File, Nonce, &Mut,
&TotalUpdates](llvm::Optional<std::vector<Diag>> Diags) {
EXPECT_THAT(Context::current().get(NonceKey),
Pointee(Nonce));
std::lock_guard<std::mutex> Lock(Mut);
++TotalUpdates;
+ EXPECT_EQ(File,
+ *TUScheduler::getFileBeingProcessedInContext());
});
}
{
WithContextValue WithNonce(NonceKey, ++Nonce);
S.runWithAST("CheckAST", File,
- [Inputs, Nonce, &Mut,
+ [File, Inputs, Nonce, &Mut,
&TotalASTReads](llvm::Expected<InputsAndAST> AST) {
EXPECT_THAT(Context::current().get(NonceKey),
Pointee(Nonce));
std::lock_guard<std::mutex> Lock(Mut);
++TotalASTReads;
+ EXPECT_EQ(
+ File,
+ *TUScheduler::getFileBeingProcessedInContext());
});
}
{
WithContextValue WithNonce(NonceKey, ++Nonce);
- S.runWithPreamble("CheckPreamble", File,
- [Inputs, Nonce, &Mut, &TotalPreambleReads](
- llvm::Expected<InputsAndPreamble> Preamble) {
- EXPECT_THAT(Context::current().get(NonceKey),
- Pointee(Nonce));
-
- ASSERT_TRUE((bool)Preamble);
- EXPECT_EQ(Preamble->Contents, Inputs.Contents);
-
- std::lock_guard<std::mutex> Lock(Mut);
- ++TotalPreambleReads;
- });
+ S.runWithPreamble(
+ "CheckPreamble", File,
+ [File, Inputs, Nonce, &Mut, &TotalPreambleReads](
+ llvm::Expected<InputsAndPreamble> Preamble) {
+ EXPECT_THAT(Context::current().get(NonceKey), Pointee(Nonce));
+
+ ASSERT_TRUE((bool)Preamble);
+ EXPECT_EQ(Preamble->Contents, Inputs.Contents);
+
+ std::lock_guard<std::mutex> Lock(Mut);
+ ++TotalPreambleReads;
+ EXPECT_EQ(File, *TUScheduler::getFileBeingProcessedInContext());
+ });
}
}
}