Index: clang-tools-extra/trunk/clangd/TUScheduler.h =================================================================== --- clang-tools-extra/trunk/clangd/TUScheduler.h +++ clang-tools-extra/trunk/clangd/TUScheduler.h @@ -122,6 +122,13 @@ /// 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 getFileBeingProcessedInContext(); + private: const bool StorePreamblesInMemory; const std::shared_ptr PCHOps; @@ -135,6 +142,7 @@ llvm::Optional WorkerThreads; std::chrono::steady_clock::duration UpdateDebounce; }; + } // namespace clangd } // namespace clang Index: clang-tools-extra/trunk/clangd/TUScheduler.cpp =================================================================== --- clang-tools-extra/trunk/clangd/TUScheduler.cpp +++ clang-tools-extra/trunk/clangd/TUScheduler.cpp @@ -63,6 +63,14 @@ class ASTWorker; } +static const clang::clangd::Key kFileBeingProcessed; + +llvm::Optional 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. @@ -491,8 +499,9 @@ { std::lock_guard 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(); } @@ -734,10 +743,12 @@ 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> Index: clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp =================================================================== --- clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp +++ clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp @@ -197,20 +197,22 @@ { WithContextValue WithNonce(NonceKey, ++Nonce); S.update(File, Inputs, WantDiagnostics::Auto, - [Nonce, &Mut, + [File, Nonce, &Mut, &TotalUpdates](llvm::Optional> Diags) { EXPECT_THAT(Context::current().get(NonceKey), Pointee(Nonce)); std::lock_guard 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 AST) { EXPECT_THAT(Context::current().get(NonceKey), Pointee(Nonce)); @@ -221,23 +223,27 @@ std::lock_guard Lock(Mut); ++TotalASTReads; + EXPECT_EQ( + File, + *TUScheduler::getFileBeingProcessedInContext()); }); } { WithContextValue WithNonce(NonceKey, ++Nonce); - S.runWithPreamble("CheckPreamble", File, - [Inputs, Nonce, &Mut, &TotalPreambleReads]( - llvm::Expected Preamble) { - EXPECT_THAT(Context::current().get(NonceKey), - Pointee(Nonce)); - - ASSERT_TRUE((bool)Preamble); - EXPECT_EQ(Preamble->Contents, Inputs.Contents); - - std::lock_guard Lock(Mut); - ++TotalPreambleReads; - }); + S.runWithPreamble( + "CheckPreamble", File, + [File, Inputs, Nonce, &Mut, &TotalPreambleReads]( + llvm::Expected Preamble) { + EXPECT_THAT(Context::current().get(NonceKey), Pointee(Nonce)); + + ASSERT_TRUE((bool)Preamble); + EXPECT_EQ(Preamble->Contents, Inputs.Contents); + + std::lock_guard Lock(Mut); + ++TotalPreambleReads; + EXPECT_EQ(File, *TUScheduler::getFileBeingProcessedInContext()); + }); } } }