Author: ioeric Date: Thu Aug 9 02:05:45 2018 New Revision: 339320 URL: http://llvm.org/viewvc/llvm-project?rev=339320&view=rev Log: [clangd] Record the file being processed in a TUScheduler thread in context.
Summary: This allows implementations like different symbol indexes to know what the current active file is. For example, some customized index implementation might decide to only return results for some files. Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: javed.absar, MaskRay, jkorous, arphaman, cfe-commits Differential Revision: https://reviews.llvm.org/D50446 Modified: clang-tools-extra/trunk/clangd/TUScheduler.cpp clang-tools-extra/trunk/clangd/TUScheduler.h clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp Modified: clang-tools-extra/trunk/clangd/TUScheduler.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/TUScheduler.cpp?rev=339320&r1=339319&r2=339320&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/TUScheduler.cpp (original) +++ clang-tools-extra/trunk/clangd/TUScheduler.cpp Thu Aug 9 02:05:45 2018 @@ -63,6 +63,14 @@ namespace { 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. @@ -491,8 +499,9 @@ void ASTWorker::startTask(llvm::StringRe { 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(); } @@ -734,10 +743,12 @@ void TUScheduler::runWithPreamble( 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>> Modified: clang-tools-extra/trunk/clangd/TUScheduler.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/TUScheduler.h?rev=339320&r1=339319&r2=339320&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/TUScheduler.h (original) +++ clang-tools-extra/trunk/clangd/TUScheduler.h Thu Aug 9 02:05:45 2018 @@ -122,6 +122,13 @@ public: /// 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; @@ -135,6 +142,7 @@ private: llvm::Optional<AsyncTaskRunner> WorkerThreads; std::chrono::steady_clock::duration UpdateDebounce; }; + } // namespace clangd } // namespace clang Modified: clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp?rev=339320&r1=339319&r2=339320&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/TUSchedulerTests.cpp Thu Aug 9 02:05:45 2018 @@ -197,20 +197,22 @@ TEST_F(TUSchedulerTests, ManyUpdates) { { 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)); @@ -221,23 +223,27 @@ TEST_F(TUSchedulerTests, ManyUpdates) { 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()); + }); } } } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits