Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1 | //===--- ClangdServer.cpp - Main clangd server code --------------*- C++-*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===-------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ClangdServer.h" |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 11 | #include "clang/Format/Format.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 12 | #include "clang/Frontend/ASTUnit.h" |
| 13 | #include "clang/Frontend/CompilerInstance.h" |
| 14 | #include "clang/Frontend/CompilerInvocation.h" |
| 15 | #include "clang/Tooling/CompilationDatabase.h" |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FileSystem.h" |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | #include <future> |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 20 | |
Ilya Biryukov | 2f31410 | 2017-05-16 10:06:20 +0000 | [diff] [blame] | 21 | using namespace clang; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 22 | using namespace clang::clangd; |
| 23 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 26 | class FulfillPromiseGuard { |
| 27 | public: |
| 28 | FulfillPromiseGuard(std::promise<void> &Promise) : Promise(Promise) {} |
| 29 | |
| 30 | ~FulfillPromiseGuard() { Promise.set_value(); } |
| 31 | |
| 32 | private: |
| 33 | std::promise<void> &Promise; |
| 34 | }; |
| 35 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 36 | std::vector<tooling::Replacement> formatCode(StringRef Code, StringRef Filename, |
| 37 | ArrayRef<tooling::Range> Ranges) { |
| 38 | // Call clang-format. |
| 39 | // FIXME: Don't ignore style. |
| 40 | format::FormatStyle Style = format::getLLVMStyle(); |
| 41 | auto Result = format::reformat(Style, Code, Ranges, Filename); |
| 42 | |
| 43 | return std::vector<tooling::Replacement>(Result.begin(), Result.end()); |
| 44 | } |
| 45 | |
Ilya Biryukov | a46f7a9 | 2017-06-28 10:34:50 +0000 | [diff] [blame] | 46 | std::string getStandardResourceDir() { |
| 47 | static int Dummy; // Just an address in this process. |
| 48 | return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy); |
| 49 | } |
| 50 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 51 | } // namespace |
| 52 | |
| 53 | size_t clangd::positionToOffset(StringRef Code, Position P) { |
| 54 | size_t Offset = 0; |
| 55 | for (int I = 0; I != P.line; ++I) { |
| 56 | // FIXME: \r\n |
| 57 | // FIXME: UTF-8 |
| 58 | size_t F = Code.find('\n', Offset); |
| 59 | if (F == StringRef::npos) |
| 60 | return 0; // FIXME: Is this reasonable? |
| 61 | Offset = F + 1; |
| 62 | } |
| 63 | return (Offset == 0 ? 0 : (Offset - 1)) + P.character; |
| 64 | } |
| 65 | |
| 66 | /// Turn an offset in Code into a [line, column] pair. |
| 67 | Position clangd::offsetToPosition(StringRef Code, size_t Offset) { |
| 68 | StringRef JustBefore = Code.substr(0, Offset); |
| 69 | // FIXME: \r\n |
| 70 | // FIXME: UTF-8 |
| 71 | int Lines = JustBefore.count('\n'); |
| 72 | int Cols = JustBefore.size() - JustBefore.rfind('\n') - 1; |
| 73 | return {Lines, Cols}; |
| 74 | } |
| 75 | |
Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 76 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> |
Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 77 | RealFileSystemProvider::getTaggedFileSystem(PathRef File) { |
Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 78 | return make_tagged(vfs::getRealFileSystem(), VFSTag()); |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 81 | unsigned clangd::getDefaultAsyncThreadsCount() { |
| 82 | unsigned HardwareConcurrency = std::thread::hardware_concurrency(); |
| 83 | // C++ standard says that hardware_concurrency() |
| 84 | // may return 0, fallback to 1 worker thread in |
| 85 | // that case. |
| 86 | if (HardwareConcurrency == 0) |
| 87 | return 1; |
| 88 | return HardwareConcurrency; |
| 89 | } |
| 90 | |
| 91 | ClangdScheduler::ClangdScheduler(unsigned AsyncThreadsCount) |
| 92 | : RunSynchronously(AsyncThreadsCount == 0) { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 93 | if (RunSynchronously) { |
| 94 | // Don't start the worker thread if we're running synchronously |
| 95 | return; |
| 96 | } |
| 97 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 98 | Workers.reserve(AsyncThreadsCount); |
| 99 | for (unsigned I = 0; I < AsyncThreadsCount; ++I) { |
| 100 | Workers.push_back(std::thread([this]() { |
| 101 | while (true) { |
| 102 | std::future<void> Request; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 103 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 104 | // Pick request from the queue |
| 105 | { |
| 106 | std::unique_lock<std::mutex> Lock(Mutex); |
| 107 | // Wait for more requests. |
| 108 | RequestCV.wait(Lock, |
| 109 | [this] { return !RequestQueue.empty() || Done; }); |
| 110 | if (Done) |
| 111 | return; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 112 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 113 | assert(!RequestQueue.empty() && "RequestQueue was empty"); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 114 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 115 | // We process requests starting from the front of the queue. Users of |
| 116 | // ClangdScheduler have a way to prioritise their requests by putting |
| 117 | // them to the either side of the queue (using either addToEnd or |
| 118 | // addToFront). |
| 119 | Request = std::move(RequestQueue.front()); |
| 120 | RequestQueue.pop_front(); |
| 121 | } // unlock Mutex |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 122 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 123 | Request.get(); |
| 124 | } |
| 125 | })); |
| 126 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | ClangdScheduler::~ClangdScheduler() { |
| 130 | if (RunSynchronously) |
| 131 | return; // no worker thread is running in that case |
| 132 | |
| 133 | { |
| 134 | std::lock_guard<std::mutex> Lock(Mutex); |
| 135 | // Wake up the worker thread |
| 136 | Done = true; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 137 | } // unlock Mutex |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 138 | RequestCV.notify_all(); |
| 139 | |
| 140 | for (auto &Worker : Workers) |
| 141 | Worker.join(); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Ilya Biryukov | 103c951 | 2017-06-13 15:59:43 +0000 | [diff] [blame] | 144 | ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB, |
| 145 | DiagnosticsConsumer &DiagConsumer, |
| 146 | FileSystemProvider &FSProvider, |
Ilya Biryukov | b33c157 | 2017-09-12 13:57:14 +0000 | [diff] [blame^] | 147 | unsigned AsyncThreadsCount, bool SnippetCompletions, |
Ilya Biryukov | a46f7a9 | 2017-06-28 10:34:50 +0000 | [diff] [blame] | 148 | llvm::Optional<StringRef> ResourceDir) |
Ilya Biryukov | 103c951 | 2017-06-13 15:59:43 +0000 | [diff] [blame] | 149 | : CDB(CDB), DiagConsumer(DiagConsumer), FSProvider(FSProvider), |
Ilya Biryukov | a46f7a9 | 2017-06-28 10:34:50 +0000 | [diff] [blame] | 150 | ResourceDir(ResourceDir ? ResourceDir->str() : getStandardResourceDir()), |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 151 | PCHs(std::make_shared<PCHContainerOperations>()), |
Ilya Biryukov | b33c157 | 2017-09-12 13:57:14 +0000 | [diff] [blame^] | 152 | WorkScheduler(AsyncThreadsCount), SnippetCompletions(SnippetCompletions) { |
| 153 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 154 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 155 | std::future<void> ClangdServer::addDocument(PathRef File, StringRef Contents) { |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 156 | DocVersion Version = DraftMgr.updateDraft(File, Contents); |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 157 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 158 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 159 | std::shared_ptr<CppFile> Resources = |
| 160 | Units.getOrCreateFile(File, ResourceDir, CDB, PCHs, TaggedFS.Value); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 161 | return scheduleReparseAndDiags(File, VersionedDraft{Version, Contents.str()}, |
| 162 | std::move(Resources), std::move(TaggedFS)); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 165 | std::future<void> ClangdServer::removeDocument(PathRef File) { |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 166 | DraftMgr.removeDraft(File); |
| 167 | std::shared_ptr<CppFile> Resources = Units.removeIfPresent(File); |
| 168 | return scheduleCancelRebuild(std::move(Resources)); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 171 | std::future<void> ClangdServer::forceReparse(PathRef File) { |
Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 172 | auto FileContents = DraftMgr.getDraft(File); |
| 173 | assert(FileContents.Draft && |
| 174 | "forceReparse() was called for non-added document"); |
| 175 | |
| 176 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 177 | auto Recreated = Units.recreateFileIfCompileCommandChanged( |
| 178 | File, ResourceDir, CDB, PCHs, TaggedFS.Value); |
| 179 | |
| 180 | // Note that std::future from this cleanup action is ignored. |
| 181 | scheduleCancelRebuild(std::move(Recreated.RemovedFile)); |
| 182 | // Schedule a reparse. |
| 183 | return scheduleReparseAndDiags(File, std::move(FileContents), |
| 184 | std::move(Recreated.FileInCollection), |
| 185 | std::move(TaggedFS)); |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Ilya Biryukov | 0e27ce4 | 2017-06-13 14:15:56 +0000 | [diff] [blame] | 188 | Tagged<std::vector<CompletionItem>> |
| 189 | ClangdServer::codeComplete(PathRef File, Position Pos, |
Ilya Biryukov | ed99e4c | 2017-07-31 17:09:29 +0000 | [diff] [blame] | 190 | llvm::Optional<StringRef> OverridenContents, |
| 191 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) { |
Ilya Biryukov | 0e27ce4 | 2017-06-13 14:15:56 +0000 | [diff] [blame] | 192 | std::string DraftStorage; |
| 193 | if (!OverridenContents) { |
| 194 | auto FileContents = DraftMgr.getDraft(File); |
| 195 | assert(FileContents.Draft && |
| 196 | "codeComplete is called for non-added document"); |
| 197 | |
| 198 | DraftStorage = std::move(*FileContents.Draft); |
| 199 | OverridenContents = DraftStorage; |
| 200 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 201 | |
Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 202 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Ilya Biryukov | ed99e4c | 2017-07-31 17:09:29 +0000 | [diff] [blame] | 203 | if (UsedFS) |
| 204 | *UsedFS = TaggedFS.Value; |
| 205 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 206 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
| 207 | assert(Resources && "Calling completion on non-added file"); |
| 208 | |
| 209 | auto Preamble = Resources->getPossiblyStalePreamble(); |
Ilya Biryukov | b33c157 | 2017-09-12 13:57:14 +0000 | [diff] [blame^] | 210 | std::vector<CompletionItem> Result = clangd::codeComplete( |
| 211 | File, Resources->getCompileCommand(), |
| 212 | Preamble ? &Preamble->Preamble : nullptr, *OverridenContents, Pos, |
| 213 | TaggedFS.Value, PCHs, SnippetCompletions); |
Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 214 | return make_tagged(std::move(Result), TaggedFS.Tag); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 215 | } |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 216 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 217 | std::vector<tooling::Replacement> ClangdServer::formatRange(PathRef File, |
| 218 | Range Rng) { |
| 219 | std::string Code = getDocument(File); |
| 220 | |
| 221 | size_t Begin = positionToOffset(Code, Rng.start); |
| 222 | size_t Len = positionToOffset(Code, Rng.end) - Begin; |
| 223 | return formatCode(Code, File, {tooling::Range(Begin, Len)}); |
| 224 | } |
| 225 | |
| 226 | std::vector<tooling::Replacement> ClangdServer::formatFile(PathRef File) { |
| 227 | // Format everything. |
| 228 | std::string Code = getDocument(File); |
| 229 | return formatCode(Code, File, {tooling::Range(0, Code.size())}); |
| 230 | } |
| 231 | |
| 232 | std::vector<tooling::Replacement> ClangdServer::formatOnType(PathRef File, |
| 233 | Position Pos) { |
| 234 | // Look for the previous opening brace from the character position and |
| 235 | // format starting from there. |
| 236 | std::string Code = getDocument(File); |
| 237 | size_t CursorPos = positionToOffset(Code, Pos); |
| 238 | size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos); |
| 239 | if (PreviousLBracePos == StringRef::npos) |
| 240 | PreviousLBracePos = CursorPos; |
| 241 | size_t Len = 1 + CursorPos - PreviousLBracePos; |
| 242 | |
| 243 | return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)}); |
| 244 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 245 | |
| 246 | std::string ClangdServer::getDocument(PathRef File) { |
| 247 | auto draft = DraftMgr.getDraft(File); |
| 248 | assert(draft.Draft && "File is not tracked, cannot get contents"); |
| 249 | return *draft.Draft; |
| 250 | } |
| 251 | |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 252 | std::string ClangdServer::dumpAST(PathRef File) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 253 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
| 254 | assert(Resources && "dumpAST is called for non-added document"); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 255 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 256 | std::string Result; |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame] | 257 | Resources->getAST().get()->runUnderLock([&Result](ParsedAST *AST) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 258 | llvm::raw_string_ostream ResultOS(Result); |
| 259 | if (AST) { |
| 260 | clangd::dumpAST(*AST, ResultOS); |
| 261 | } else { |
| 262 | ResultOS << "<no-ast>"; |
| 263 | } |
| 264 | ResultOS.flush(); |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 265 | }); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 266 | return Result; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 267 | } |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 268 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 269 | Tagged<std::vector<Location>> ClangdServer::findDefinitions(PathRef File, |
| 270 | Position Pos) { |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 271 | auto FileContents = DraftMgr.getDraft(File); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 272 | assert(FileContents.Draft && |
| 273 | "findDefinitions is called for non-added document"); |
| 274 | |
| 275 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 276 | |
| 277 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
| 278 | assert(Resources && "Calling findDefinitions on non-added file"); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 279 | |
| 280 | std::vector<Location> Result; |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame] | 281 | Resources->getAST().get()->runUnderLock([Pos, &Result](ParsedAST *AST) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 282 | if (!AST) |
| 283 | return; |
| 284 | Result = clangd::findDefinitions(*AST, Pos); |
| 285 | }); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 286 | return make_tagged(std::move(Result), TaggedFS.Tag); |
| 287 | } |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 288 | |
| 289 | std::future<void> ClangdServer::scheduleReparseAndDiags( |
| 290 | PathRef File, VersionedDraft Contents, std::shared_ptr<CppFile> Resources, |
| 291 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS) { |
| 292 | |
| 293 | assert(Contents.Draft && "Draft must have contents"); |
| 294 | std::future<llvm::Optional<std::vector<DiagWithFixIts>>> DeferredRebuild = |
| 295 | Resources->deferRebuild(*Contents.Draft, TaggedFS.Value); |
| 296 | std::promise<void> DonePromise; |
| 297 | std::future<void> DoneFuture = DonePromise.get_future(); |
| 298 | |
| 299 | DocVersion Version = Contents.Version; |
| 300 | Path FileStr = File; |
| 301 | VFSTag Tag = TaggedFS.Tag; |
| 302 | auto ReparseAndPublishDiags = |
| 303 | [this, FileStr, Version, |
| 304 | Tag](std::future<llvm::Optional<std::vector<DiagWithFixIts>>> |
| 305 | DeferredRebuild, |
| 306 | std::promise<void> DonePromise) -> void { |
| 307 | FulfillPromiseGuard Guard(DonePromise); |
| 308 | |
| 309 | auto CurrentVersion = DraftMgr.getVersion(FileStr); |
| 310 | if (CurrentVersion != Version) |
| 311 | return; // This request is outdated |
| 312 | |
| 313 | auto Diags = DeferredRebuild.get(); |
| 314 | if (!Diags) |
| 315 | return; // A new reparse was requested before this one completed. |
| 316 | DiagConsumer.onDiagnosticsReady(FileStr, |
| 317 | make_tagged(std::move(*Diags), Tag)); |
| 318 | }; |
| 319 | |
| 320 | WorkScheduler.addToFront(std::move(ReparseAndPublishDiags), |
| 321 | std::move(DeferredRebuild), std::move(DonePromise)); |
| 322 | return DoneFuture; |
| 323 | } |
| 324 | |
| 325 | std::future<void> |
| 326 | ClangdServer::scheduleCancelRebuild(std::shared_ptr<CppFile> Resources) { |
| 327 | std::promise<void> DonePromise; |
| 328 | std::future<void> DoneFuture = DonePromise.get_future(); |
| 329 | if (!Resources) { |
| 330 | // No need to schedule any cleanup. |
| 331 | DonePromise.set_value(); |
| 332 | return DoneFuture; |
| 333 | } |
| 334 | |
| 335 | std::future<void> DeferredCancel = Resources->deferCancelRebuild(); |
| 336 | auto CancelReparses = [Resources](std::promise<void> DonePromise, |
| 337 | std::future<void> DeferredCancel) { |
| 338 | FulfillPromiseGuard Guard(DonePromise); |
| 339 | DeferredCancel.get(); |
| 340 | }; |
| 341 | WorkScheduler.addToFront(std::move(CancelReparses), std::move(DonePromise), |
| 342 | std::move(DeferredCancel)); |
| 343 | return DoneFuture; |
| 344 | } |