Author: sammccall Date: Wed Jun 19 00:29:05 2019 New Revision: 363765 URL: http://llvm.org/viewvc/llvm-project?rev=363765&view=rev Log: [clangd] Add ClangdServer accessor for buffer contents
Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp clang-tools-extra/trunk/clangd/ClangdServer.h clang-tools-extra/trunk/clangd/TUScheduler.cpp clang-tools-extra/trunk/clangd/TUScheduler.h clang-tools-extra/trunk/clangd/unittests/TUSchedulerTests.cpp Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.cpp?rev=363765&r1=363764&r2=363765&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/ClangdServer.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdServer.cpp Wed Jun 19 00:29:05 2019 @@ -150,6 +150,10 @@ void ClangdServer::addDocument(PathRef F void ClangdServer::removeDocument(PathRef File) { WorkScheduler.remove(File); } +llvm::StringRef ClangdServer::getDocument(PathRef File) const { + return WorkScheduler.getContents(File); +} + void ClangdServer::codeComplete(PathRef File, Position Pos, const clangd::CodeCompleteOptions &Opts, Callback<CodeCompleteResult> CB) { Modified: clang-tools-extra/trunk/clangd/ClangdServer.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.h?rev=363765&r1=363764&r2=363765&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/ClangdServer.h (original) +++ clang-tools-extra/trunk/clangd/ClangdServer.h Wed Jun 19 00:29:05 2019 @@ -163,6 +163,9 @@ public: void addDocument(PathRef File, StringRef Contents, WantDiagnostics WD = WantDiagnostics::Auto); + /// Get the contents of \p File, which should have been added. + llvm::StringRef getDocument(PathRef File) const; + /// Remove \p File from list of tracked files, schedule a request to free /// resources associated with it. Pending diagnostics for closed files may not /// be delivered, even if requested with WantDiags::Auto or WantDiags::Yes. Modified: clang-tools-extra/trunk/clangd/TUScheduler.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/TUScheduler.cpp?rev=363765&r1=363764&r2=363765&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/TUScheduler.cpp (original) +++ clang-tools-extra/trunk/clangd/TUScheduler.cpp Wed Jun 19 00:29:05 2019 @@ -884,6 +884,15 @@ void TUScheduler::remove(PathRef File) { File); } +llvm::StringRef TUScheduler::getContents(PathRef File) const { + auto It = Files.find(File); + if (It == Files.end()) { + elog("getContents() for untracked file: {0}", File); + return ""; + } + return It->second->Contents; +} + void TUScheduler::run(llvm::StringRef Name, llvm::unique_function<void()> Action) { if (!PreambleTasks) Modified: clang-tools-extra/trunk/clangd/TUScheduler.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/TUScheduler.h?rev=363765&r1=363764&r2=363765&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/TUScheduler.h (original) +++ clang-tools-extra/trunk/clangd/TUScheduler.h Wed Jun 19 00:29:05 2019 @@ -155,6 +155,10 @@ public: /// if requested with WantDiags::Auto or WantDiags::Yes. void remove(PathRef File); + /// Returns the current contents of the buffer for File, per last update(). + /// The returned StringRef may be invalidated by any write to TUScheduler. + llvm::StringRef getContents(PathRef File) const; + /// Schedule an async task with no dependencies. void run(llvm::StringRef Name, llvm::unique_function<void()> Action); Modified: clang-tools-extra/trunk/clangd/unittests/TUSchedulerTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TUSchedulerTests.cpp?rev=363765&r1=363764&r2=363765&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/unittests/TUSchedulerTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/TUSchedulerTests.cpp Wed Jun 19 00:29:05 2019 @@ -107,12 +107,14 @@ TEST_F(TUSchedulerTests, MissingFiles) { ASTRetentionPolicy()); auto Added = testPath("added.cpp"); - Files[Added] = ""; + Files[Added] = "x"; auto Missing = testPath("missing.cpp"); Files[Missing] = ""; - S.update(Added, getInputs(Added, ""), WantDiagnostics::No); + EXPECT_EQ(S.getContents(Added), ""); + S.update(Added, getInputs(Added, "x"), WantDiagnostics::No); + EXPECT_EQ(S.getContents(Added), "x"); // Assert each operation for missing file is an error (even if it's available // in VFS). @@ -131,7 +133,9 @@ TEST_F(TUSchedulerTests, MissingFiles) { [&](Expected<InputsAndPreamble> Preamble) { EXPECT_TRUE(bool(Preamble)); }); + EXPECT_EQ(S.getContents(Added), "x"); S.remove(Added); + EXPECT_EQ(S.getContents(Added), ""); // Assert that all operations fail after removing the file. S.runWithAST("", Added, _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits