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" |
Sam McCall | a66d2cb | 2017-12-19 17:06:07 +0000 | [diff] [blame] | 11 | #include "CodeComplete.h" |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 12 | #include "SourceCode.h" |
Sam McCall | a66d2cb | 2017-12-19 17:06:07 +0000 | [diff] [blame] | 13 | #include "XRefs.h" |
Sam McCall | 0faecf0 | 2018-01-15 12:33:00 +0000 | [diff] [blame] | 14 | #include "index/Merge.h" |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 15 | #include "clang/Format/Format.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/CompilerInstance.h" |
| 17 | #include "clang/Frontend/CompilerInvocation.h" |
| 18 | #include "clang/Tooling/CompilationDatabase.h" |
Ilya Biryukov | 9e11c4c | 2017-11-15 18:04:56 +0000 | [diff] [blame] | 19 | #include "clang/Tooling/Refactoring/RefactoringResultConsumer.h" |
| 20 | #include "clang/Tooling/Refactoring/Rename/RenamingAction.h" |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/ArrayRef.h" |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Errc.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FormatProviders.h" |
| 25 | #include "llvm/Support/FormatVariadic.h" |
Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include <future> |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 29 | |
Ilya Biryukov | 2f31410 | 2017-05-16 10:06:20 +0000 | [diff] [blame] | 30 | using namespace clang; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 31 | using namespace clang::clangd; |
| 32 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame^] | 35 | tooling::CompileCommand getCompileCommand(GlobalCompilationDatabase &CDB, |
| 36 | PathRef File, PathRef ResourceDir) { |
| 37 | llvm::Optional<tooling::CompileCommand> C = CDB.getCompileCommand(File); |
| 38 | if (!C) // FIXME: Suppress diagnostics? Let the user know? |
| 39 | C = CDB.getFallbackCommand(File); |
| 40 | |
| 41 | // Inject the resource dir. |
| 42 | // FIXME: Don't overwrite it if it's already there. |
| 43 | C->CommandLine.push_back("-resource-dir=" + ResourceDir.str()); |
| 44 | return std::move(*C); |
| 45 | } |
| 46 | |
Ilya Biryukov | a46f7a9 | 2017-06-28 10:34:50 +0000 | [diff] [blame] | 47 | std::string getStandardResourceDir() { |
| 48 | static int Dummy; // Just an address in this process. |
| 49 | return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy); |
| 50 | } |
| 51 | |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 52 | class RefactoringResultCollector final |
| 53 | : public tooling::RefactoringResultConsumer { |
| 54 | public: |
| 55 | void handleError(llvm::Error Err) override { |
| 56 | assert(!Result.hasValue()); |
| 57 | // FIXME: figure out a way to return better message for DiagnosticError. |
| 58 | // clangd uses llvm::toString to convert the Err to string, however, for |
| 59 | // DiagnosticError, only "clang diagnostic" will be generated. |
| 60 | Result = std::move(Err); |
| 61 | } |
| 62 | |
| 63 | // Using the handle(SymbolOccurrences) from parent class. |
| 64 | using tooling::RefactoringResultConsumer::handle; |
| 65 | |
| 66 | void handle(tooling::AtomicChanges SourceReplacements) override { |
| 67 | assert(!Result.hasValue()); |
| 68 | Result = std::move(SourceReplacements); |
| 69 | } |
| 70 | |
| 71 | Optional<Expected<tooling::AtomicChanges>> Result; |
| 72 | }; |
| 73 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 74 | } // namespace |
| 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) { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 100 | Workers.push_back(std::thread([this, I]() { |
| 101 | llvm::set_thread_name(llvm::formatv("scheduler/{0}", I)); |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 102 | while (true) { |
Ilya Biryukov | 08e6ccb | 2017-10-09 16:26:26 +0000 | [diff] [blame] | 103 | UniqueFunction<void()> Request; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 104 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 105 | // Pick request from the queue |
| 106 | { |
| 107 | std::unique_lock<std::mutex> Lock(Mutex); |
| 108 | // Wait for more requests. |
| 109 | RequestCV.wait(Lock, |
| 110 | [this] { return !RequestQueue.empty() || Done; }); |
| 111 | if (Done) |
| 112 | return; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 113 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 114 | assert(!RequestQueue.empty() && "RequestQueue was empty"); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 115 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 116 | // We process requests starting from the front of the queue. Users of |
| 117 | // ClangdScheduler have a way to prioritise their requests by putting |
| 118 | // them to the either side of the queue (using either addToEnd or |
| 119 | // addToFront). |
| 120 | Request = std::move(RequestQueue.front()); |
| 121 | RequestQueue.pop_front(); |
| 122 | } // unlock Mutex |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 123 | |
Ilya Biryukov | 08e6ccb | 2017-10-09 16:26:26 +0000 | [diff] [blame] | 124 | Request(); |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 125 | } |
| 126 | })); |
| 127 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | ClangdScheduler::~ClangdScheduler() { |
| 131 | if (RunSynchronously) |
| 132 | return; // no worker thread is running in that case |
| 133 | |
| 134 | { |
| 135 | std::lock_guard<std::mutex> Lock(Mutex); |
| 136 | // Wake up the worker thread |
| 137 | Done = true; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 138 | } // unlock Mutex |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 139 | RequestCV.notify_all(); |
| 140 | |
| 141 | for (auto &Worker : Workers) |
| 142 | Worker.join(); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 145 | ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB, |
| 146 | DiagnosticsConsumer &DiagConsumer, |
| 147 | FileSystemProvider &FSProvider, |
| 148 | unsigned AsyncThreadsCount, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 149 | bool StorePreamblesInMemory, |
Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 150 | bool BuildDynamicSymbolIndex, SymbolIndex *StaticIdx, |
Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 151 | llvm::Optional<StringRef> ResourceDir) |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 152 | : CDB(CDB), DiagConsumer(DiagConsumer), FSProvider(FSProvider), |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 153 | FileIdx(BuildDynamicSymbolIndex ? new FileIndex() : nullptr), |
| 154 | // Pass a callback into `Units` to extract symbols from a newly parsed |
| 155 | // file and rebuild the file index synchronously each time an AST is |
| 156 | // parsed. |
| 157 | // FIXME(ioeric): this can be slow and we may be able to index on less |
| 158 | // critical paths. |
| 159 | Units(FileIdx |
| 160 | ? [this](const Context &Ctx, PathRef Path, |
| 161 | ParsedAST *AST) { FileIdx->update(Ctx, Path, AST); } |
| 162 | : ASTParsedCallback()), |
Ilya Biryukov | a46f7a9 | 2017-06-28 10:34:50 +0000 | [diff] [blame] | 163 | ResourceDir(ResourceDir ? ResourceDir->str() : getStandardResourceDir()), |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 164 | PCHs(std::make_shared<PCHContainerOperations>()), |
Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 165 | StorePreamblesInMemory(StorePreamblesInMemory), |
Sam McCall | 0faecf0 | 2018-01-15 12:33:00 +0000 | [diff] [blame] | 166 | WorkScheduler(AsyncThreadsCount) { |
| 167 | if (FileIdx && StaticIdx) { |
| 168 | MergedIndex = mergeIndex(FileIdx.get(), StaticIdx); |
| 169 | Index = MergedIndex.get(); |
| 170 | } else if (FileIdx) |
| 171 | Index = FileIdx.get(); |
| 172 | else if (StaticIdx) |
| 173 | Index = StaticIdx; |
| 174 | else |
| 175 | Index = nullptr; |
| 176 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 177 | |
Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 178 | void ClangdServer::setRootPath(PathRef RootPath) { |
| 179 | std::string NewRootPath = llvm::sys::path::convert_to_slash( |
| 180 | RootPath, llvm::sys::path::Style::posix); |
| 181 | if (llvm::sys::fs::is_directory(NewRootPath)) |
| 182 | this->RootPath = NewRootPath; |
| 183 | } |
| 184 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 185 | std::future<Context> ClangdServer::addDocument(Context Ctx, PathRef File, |
| 186 | StringRef Contents) { |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 187 | DocVersion Version = DraftMgr.updateDraft(File, Contents); |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 188 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 189 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame^] | 190 | std::shared_ptr<CppFile> Resources = |
| 191 | Units.getOrCreateFile(File, ResourceDir, StorePreamblesInMemory, PCHs); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 192 | return scheduleReparseAndDiags(std::move(Ctx), File, |
| 193 | VersionedDraft{Version, Contents.str()}, |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame^] | 194 | std::move(Resources), std::move(TaggedFS), |
| 195 | /*AllowCachedCompileFlags=*/true); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 198 | std::future<Context> ClangdServer::removeDocument(Context Ctx, PathRef File) { |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 199 | DraftMgr.removeDraft(File); |
| 200 | std::shared_ptr<CppFile> Resources = Units.removeIfPresent(File); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 201 | return scheduleCancelRebuild(std::move(Ctx), std::move(Resources)); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 204 | std::future<Context> ClangdServer::forceReparse(Context Ctx, PathRef File) { |
Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 205 | auto FileContents = DraftMgr.getDraft(File); |
| 206 | assert(FileContents.Draft && |
| 207 | "forceReparse() was called for non-added document"); |
| 208 | |
| 209 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame^] | 210 | std::shared_ptr<CppFile> Resources = |
| 211 | Units.getOrCreateFile(File, ResourceDir, StorePreamblesInMemory, PCHs); |
| 212 | return scheduleReparseAndDiags(std::move(Ctx), File, FileContents, |
| 213 | std::move(Resources), std::move(TaggedFS), |
| 214 | /*AllowCachedCompileFlags=*/false); |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 217 | std::future<std::pair<Context, Tagged<CompletionList>>> |
| 218 | ClangdServer::codeComplete(Context Ctx, PathRef File, Position Pos, |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 219 | const clangd::CodeCompleteOptions &Opts, |
Ilya Biryukov | ed99e4c | 2017-07-31 17:09:29 +0000 | [diff] [blame] | 220 | llvm::Optional<StringRef> OverridenContents, |
| 221 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) { |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 222 | using ResultType = std::pair<Context, Tagged<CompletionList>>; |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 223 | |
| 224 | std::promise<ResultType> ResultPromise; |
| 225 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 226 | auto Callback = [](std::promise<ResultType> ResultPromise, Context Ctx, |
| 227 | Tagged<CompletionList> Result) -> void { |
| 228 | ResultPromise.set_value({std::move(Ctx), std::move(Result)}); |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | std::future<ResultType> ResultFuture = ResultPromise.get_future(); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 232 | codeComplete(std::move(Ctx), File, Pos, Opts, |
| 233 | BindWithForward(Callback, std::move(ResultPromise)), |
| 234 | OverridenContents, UsedFS); |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 235 | return ResultFuture; |
| 236 | } |
| 237 | |
| 238 | void ClangdServer::codeComplete( |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 239 | Context Ctx, PathRef File, Position Pos, |
| 240 | const clangd::CodeCompleteOptions &Opts, |
| 241 | UniqueFunction<void(Context, Tagged<CompletionList>)> Callback, |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 242 | llvm::Optional<StringRef> OverridenContents, |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 243 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) { |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 244 | using CallbackType = UniqueFunction<void(Context, Tagged<CompletionList>)>; |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 245 | |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 246 | std::string Contents; |
| 247 | if (OverridenContents) { |
| 248 | Contents = *OverridenContents; |
| 249 | } else { |
Ilya Biryukov | 0e27ce4 | 2017-06-13 14:15:56 +0000 | [diff] [blame] | 250 | auto FileContents = DraftMgr.getDraft(File); |
| 251 | assert(FileContents.Draft && |
| 252 | "codeComplete is called for non-added document"); |
| 253 | |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 254 | Contents = std::move(*FileContents.Draft); |
Ilya Biryukov | 0e27ce4 | 2017-06-13 14:15:56 +0000 | [diff] [blame] | 255 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 256 | |
Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 257 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Ilya Biryukov | ed99e4c | 2017-07-31 17:09:29 +0000 | [diff] [blame] | 258 | if (UsedFS) |
| 259 | *UsedFS = TaggedFS.Value; |
| 260 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 261 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
| 262 | assert(Resources && "Calling completion on non-added file"); |
| 263 | |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 264 | // Remember the current Preamble and use it when async task starts executing. |
| 265 | // At the point when async task starts executing, we may have a different |
| 266 | // Preamble in Resources. However, we assume the Preamble that we obtain here |
| 267 | // is reusable in completion more often. |
| 268 | std::shared_ptr<const PreambleData> Preamble = |
| 269 | Resources->getPossiblyStalePreamble(); |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 270 | // Copy completion options for passing them to async task handler. |
| 271 | auto CodeCompleteOpts = Opts; |
Sam McCall | 0faecf0 | 2018-01-15 12:33:00 +0000 | [diff] [blame] | 272 | if (!CodeCompleteOpts.Index) // Respect overridden index. |
| 273 | CodeCompleteOpts.Index = Index; |
Ilya Biryukov | f6e2b4c | 2018-01-09 14:39:27 +0000 | [diff] [blame] | 274 | |
| 275 | // Copy File, as it is a PathRef that will go out of scope before Task is |
| 276 | // executed. |
| 277 | Path FileStr = File; |
| 278 | // Copy PCHs to avoid accessing this->PCHs concurrently |
| 279 | std::shared_ptr<PCHContainerOperations> PCHs = this->PCHs; |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame^] | 280 | |
| 281 | assert(Resources->getLastCommand() && |
| 282 | "CppFile is in inconsistent state, missing CompileCommand"); |
| 283 | tooling::CompileCommand CompileCommand = *Resources->getLastCommand(); |
| 284 | |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 285 | // A task that will be run asynchronously. |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 286 | auto Task = |
| 287 | // 'mutable' to reassign Preamble variable. |
Ilya Biryukov | f6e2b4c | 2018-01-09 14:39:27 +0000 | [diff] [blame] | 288 | [FileStr, Preamble, Resources, Contents, Pos, CodeCompleteOpts, TaggedFS, |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame^] | 289 | PCHs, CompileCommand](Context Ctx, CallbackType Callback) mutable { |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 290 | if (!Preamble) { |
| 291 | // Maybe we built some preamble before processing this request. |
| 292 | Preamble = Resources->getPossiblyStalePreamble(); |
| 293 | } |
| 294 | // FIXME(ibiryukov): even if Preamble is non-null, we may want to check |
| 295 | // both the old and the new version in case only one of them matches. |
Sam McCall | a40371b | 2017-11-15 09:16:29 +0000 | [diff] [blame] | 296 | CompletionList Result = clangd::codeComplete( |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame^] | 297 | Ctx, FileStr, CompileCommand, |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 298 | Preamble ? &Preamble->Preamble : nullptr, Contents, Pos, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 299 | TaggedFS.Value, PCHs, CodeCompleteOpts); |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 300 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 301 | Callback(std::move(Ctx), |
| 302 | make_tagged(std::move(Result), std::move(TaggedFS.Tag))); |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 305 | WorkScheduler.addToFront(std::move(Task), std::move(Ctx), |
| 306 | std::move(Callback)); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 307 | } |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 308 | |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 309 | llvm::Expected<Tagged<SignatureHelp>> |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 310 | ClangdServer::signatureHelp(const Context &Ctx, PathRef File, Position Pos, |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 311 | llvm::Optional<StringRef> OverridenContents, |
| 312 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) { |
| 313 | std::string DraftStorage; |
| 314 | if (!OverridenContents) { |
| 315 | auto FileContents = DraftMgr.getDraft(File); |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 316 | if (!FileContents.Draft) |
| 317 | return llvm::make_error<llvm::StringError>( |
| 318 | "signatureHelp is called for non-added document", |
| 319 | llvm::errc::invalid_argument); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 320 | |
| 321 | DraftStorage = std::move(*FileContents.Draft); |
| 322 | OverridenContents = DraftStorage; |
| 323 | } |
| 324 | |
| 325 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 326 | if (UsedFS) |
| 327 | *UsedFS = TaggedFS.Value; |
| 328 | |
| 329 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 330 | if (!Resources) |
| 331 | return llvm::make_error<llvm::StringError>( |
| 332 | "signatureHelp is called for non-added document", |
| 333 | llvm::errc::invalid_argument); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 334 | |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame^] | 335 | assert(Resources->getLastCommand() && |
| 336 | "CppFile is in inconsistent state, missing CompileCommand"); |
| 337 | |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 338 | auto Preamble = Resources->getPossiblyStalePreamble(); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 339 | auto Result = |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame^] | 340 | clangd::signatureHelp(Ctx, File, *Resources->getLastCommand(), |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 341 | Preamble ? &Preamble->Preamble : nullptr, |
| 342 | *OverridenContents, Pos, TaggedFS.Value, PCHs); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 343 | return make_tagged(std::move(Result), TaggedFS.Tag); |
| 344 | } |
| 345 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 346 | llvm::Expected<tooling::Replacements> |
| 347 | ClangdServer::formatRange(StringRef Code, PathRef File, Range Rng) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 348 | size_t Begin = positionToOffset(Code, Rng.start); |
| 349 | size_t Len = positionToOffset(Code, Rng.end) - Begin; |
| 350 | return formatCode(Code, File, {tooling::Range(Begin, Len)}); |
| 351 | } |
| 352 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 353 | llvm::Expected<tooling::Replacements> ClangdServer::formatFile(StringRef Code, |
| 354 | PathRef File) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 355 | // Format everything. |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 356 | return formatCode(Code, File, {tooling::Range(0, Code.size())}); |
| 357 | } |
| 358 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 359 | llvm::Expected<tooling::Replacements> |
| 360 | ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 361 | // Look for the previous opening brace from the character position and |
| 362 | // format starting from there. |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 363 | size_t CursorPos = positionToOffset(Code, Pos); |
| 364 | size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos); |
| 365 | if (PreviousLBracePos == StringRef::npos) |
| 366 | PreviousLBracePos = CursorPos; |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 367 | size_t Len = CursorPos - PreviousLBracePos; |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 368 | |
| 369 | return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)}); |
| 370 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 371 | |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 372 | Expected<std::vector<tooling::Replacement>> |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 373 | ClangdServer::rename(const Context &Ctx, PathRef File, Position Pos, |
| 374 | llvm::StringRef NewName) { |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 375 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
| 376 | RefactoringResultCollector ResultCollector; |
| 377 | Resources->getAST().get()->runUnderLock([&](ParsedAST *AST) { |
| 378 | const SourceManager &SourceMgr = AST->getASTContext().getSourceManager(); |
| 379 | const FileEntry *FE = |
| 380 | SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); |
| 381 | if (!FE) |
| 382 | return; |
| 383 | SourceLocation SourceLocationBeg = |
| 384 | clangd::getBeginningOfIdentifier(*AST, Pos, FE); |
| 385 | tooling::RefactoringRuleContext Context( |
| 386 | AST->getASTContext().getSourceManager()); |
| 387 | Context.setASTContext(AST->getASTContext()); |
| 388 | auto Rename = clang::tooling::RenameOccurrences::initiate( |
| 389 | Context, SourceRange(SourceLocationBeg), NewName.str()); |
| 390 | if (!Rename) { |
| 391 | ResultCollector.Result = Rename.takeError(); |
| 392 | return; |
| 393 | } |
| 394 | Rename->invoke(ResultCollector, Context); |
| 395 | }); |
| 396 | assert(ResultCollector.Result.hasValue()); |
| 397 | if (!ResultCollector.Result.getValue()) |
| 398 | return ResultCollector.Result->takeError(); |
| 399 | |
| 400 | std::vector<tooling::Replacement> Replacements; |
| 401 | for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) { |
| 402 | tooling::Replacements ChangeReps = Change.getReplacements(); |
| 403 | for (const auto &Rep : ChangeReps) { |
| 404 | // FIXME: Right now we only support renaming the main file, so we drop |
| 405 | // replacements not for the main file. In the future, we might consider to |
| 406 | // support: |
| 407 | // * rename in any included header |
| 408 | // * rename only in the "main" header |
| 409 | // * provide an error if there are symbols we won't rename (e.g. |
| 410 | // std::vector) |
| 411 | // * rename globally in project |
| 412 | // * rename in open files |
| 413 | if (Rep.getFilePath() == File) |
| 414 | Replacements.push_back(Rep); |
| 415 | } |
| 416 | } |
| 417 | return Replacements; |
| 418 | } |
| 419 | |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 420 | llvm::Optional<std::string> ClangdServer::getDocument(PathRef File) { |
| 421 | auto Latest = DraftMgr.getDraft(File); |
| 422 | if (!Latest.Draft) |
| 423 | return llvm::None; |
| 424 | return std::move(*Latest.Draft); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 427 | std::string ClangdServer::dumpAST(PathRef File) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 428 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 429 | if (!Resources) |
| 430 | return "<non-added file>"; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 431 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 432 | std::string Result; |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame] | 433 | Resources->getAST().get()->runUnderLock([&Result](ParsedAST *AST) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 434 | llvm::raw_string_ostream ResultOS(Result); |
| 435 | if (AST) { |
| 436 | clangd::dumpAST(*AST, ResultOS); |
| 437 | } else { |
| 438 | ResultOS << "<no-ast>"; |
| 439 | } |
| 440 | ResultOS.flush(); |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 441 | }); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 442 | return Result; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 443 | } |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 444 | |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 445 | llvm::Expected<Tagged<std::vector<Location>>> |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 446 | ClangdServer::findDefinitions(const Context &Ctx, PathRef File, Position Pos) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 447 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 448 | |
| 449 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 450 | if (!Resources) |
| 451 | return llvm::make_error<llvm::StringError>( |
| 452 | "findDefinitions called on non-added file", |
| 453 | llvm::errc::invalid_argument); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 454 | |
| 455 | std::vector<Location> Result; |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 456 | Resources->getAST().get()->runUnderLock([Pos, &Result, &Ctx](ParsedAST *AST) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 457 | if (!AST) |
| 458 | return; |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 459 | Result = clangd::findDefinitions(Ctx, *AST, Pos); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 460 | }); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 461 | return make_tagged(std::move(Result), TaggedFS.Tag); |
| 462 | } |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 463 | |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 464 | llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) { |
| 465 | |
| 466 | StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx", |
| 467 | ".c++", ".m", ".mm"}; |
| 468 | StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"}; |
| 469 | |
| 470 | StringRef PathExt = llvm::sys::path::extension(Path); |
| 471 | |
| 472 | // Lookup in a list of known extensions. |
| 473 | auto SourceIter = |
| 474 | std::find_if(std::begin(SourceExtensions), std::end(SourceExtensions), |
| 475 | [&PathExt](PathRef SourceExt) { |
| 476 | return SourceExt.equals_lower(PathExt); |
| 477 | }); |
| 478 | bool IsSource = SourceIter != std::end(SourceExtensions); |
| 479 | |
| 480 | auto HeaderIter = |
| 481 | std::find_if(std::begin(HeaderExtensions), std::end(HeaderExtensions), |
| 482 | [&PathExt](PathRef HeaderExt) { |
| 483 | return HeaderExt.equals_lower(PathExt); |
| 484 | }); |
| 485 | |
| 486 | bool IsHeader = HeaderIter != std::end(HeaderExtensions); |
| 487 | |
| 488 | // We can only switch between extensions known extensions. |
| 489 | if (!IsSource && !IsHeader) |
| 490 | return llvm::None; |
| 491 | |
| 492 | // Array to lookup extensions for the switch. An opposite of where original |
| 493 | // extension was found. |
| 494 | ArrayRef<StringRef> NewExts; |
| 495 | if (IsSource) |
| 496 | NewExts = HeaderExtensions; |
| 497 | else |
| 498 | NewExts = SourceExtensions; |
| 499 | |
| 500 | // Storage for the new path. |
| 501 | SmallString<128> NewPath = StringRef(Path); |
| 502 | |
| 503 | // Instance of vfs::FileSystem, used for file existence checks. |
| 504 | auto FS = FSProvider.getTaggedFileSystem(Path).Value; |
| 505 | |
| 506 | // Loop through switched extension candidates. |
| 507 | for (StringRef NewExt : NewExts) { |
| 508 | llvm::sys::path::replace_extension(NewPath, NewExt); |
| 509 | if (FS->exists(NewPath)) |
| 510 | return NewPath.str().str(); // First str() to convert from SmallString to |
| 511 | // StringRef, second to convert from StringRef |
| 512 | // to std::string |
Ilya Biryukov | 3333494 | 2017-10-06 14:39:39 +0000 | [diff] [blame] | 513 | |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 514 | // Also check NewExt in upper-case, just in case. |
| 515 | llvm::sys::path::replace_extension(NewPath, NewExt.upper()); |
| 516 | if (FS->exists(NewPath)) |
| 517 | return NewPath.str().str(); |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | return llvm::None; |
| 521 | } |
| 522 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 523 | llvm::Expected<tooling::Replacements> |
| 524 | ClangdServer::formatCode(llvm::StringRef Code, PathRef File, |
| 525 | ArrayRef<tooling::Range> Ranges) { |
| 526 | // Call clang-format. |
| 527 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 528 | auto StyleOrError = |
| 529 | format::getStyle("file", File, "LLVM", Code, TaggedFS.Value.get()); |
| 530 | if (!StyleOrError) { |
| 531 | return StyleOrError.takeError(); |
| 532 | } else { |
| 533 | return format::reformat(StyleOrError.get(), Code, Ranges, File); |
| 534 | } |
| 535 | } |
| 536 | |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 537 | llvm::Expected<Tagged<std::vector<DocumentHighlight>>> |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 538 | ClangdServer::findDocumentHighlights(const Context &Ctx, PathRef File, |
| 539 | Position Pos) { |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 540 | auto FileContents = DraftMgr.getDraft(File); |
| 541 | if (!FileContents.Draft) |
| 542 | return llvm::make_error<llvm::StringError>( |
| 543 | "findDocumentHighlights called on non-added file", |
| 544 | llvm::errc::invalid_argument); |
| 545 | |
| 546 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 547 | |
| 548 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
| 549 | if (!Resources) |
| 550 | return llvm::make_error<llvm::StringError>( |
| 551 | "findDocumentHighlights called on non-added file", |
| 552 | llvm::errc::invalid_argument); |
| 553 | |
| 554 | std::vector<DocumentHighlight> Result; |
| 555 | llvm::Optional<llvm::Error> Err; |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 556 | Resources->getAST().get()->runUnderLock([Pos, &Ctx, &Err, |
| 557 | &Result](ParsedAST *AST) { |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 558 | if (!AST) { |
| 559 | Err = llvm::make_error<llvm::StringError>("Invalid AST", |
| 560 | llvm::errc::invalid_argument); |
| 561 | return; |
| 562 | } |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 563 | Result = clangd::findDocumentHighlights(Ctx, *AST, Pos); |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 564 | }); |
| 565 | |
| 566 | if (Err) |
| 567 | return std::move(*Err); |
| 568 | return make_tagged(Result, TaggedFS.Tag); |
| 569 | } |
| 570 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 571 | std::future<Context> ClangdServer::scheduleReparseAndDiags( |
| 572 | Context Ctx, PathRef File, VersionedDraft Contents, |
| 573 | std::shared_ptr<CppFile> Resources, |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame^] | 574 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS, |
| 575 | bool AllowCachedCompileFlags) { |
| 576 | llvm::Optional<tooling::CompileCommand> ReusedCommand; |
| 577 | if (AllowCachedCompileFlags) |
| 578 | ReusedCommand = Resources->getLastCommand(); |
| 579 | tooling::CompileCommand Command = |
| 580 | ReusedCommand ? std::move(*ReusedCommand) |
| 581 | : getCompileCommand(CDB, File, ResourceDir); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 582 | |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame^] | 583 | ParseInputs Inputs = {std::move(Command), std::move(TaggedFS.Value), |
| 584 | *std::move(Contents.Draft)}; |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 585 | assert(Contents.Draft && "Draft must have contents"); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 586 | UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>(const Context &)> |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame^] | 587 | DeferredRebuild = Resources->deferRebuild(std::move(Inputs)); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 588 | std::promise<Context> DonePromise; |
| 589 | std::future<Context> DoneFuture = DonePromise.get_future(); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 590 | |
| 591 | DocVersion Version = Contents.Version; |
| 592 | Path FileStr = File; |
| 593 | VFSTag Tag = TaggedFS.Tag; |
| 594 | auto ReparseAndPublishDiags = |
| 595 | [this, FileStr, Version, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 596 | Tag](UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>( |
| 597 | const Context &)> |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 598 | DeferredRebuild, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 599 | std::promise<Context> DonePromise, Context Ctx) -> void { |
| 600 | auto Guard = onScopeExit([&]() { DonePromise.set_value(std::move(Ctx)); }); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 601 | |
| 602 | auto CurrentVersion = DraftMgr.getVersion(FileStr); |
| 603 | if (CurrentVersion != Version) |
| 604 | return; // This request is outdated |
| 605 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 606 | auto Diags = DeferredRebuild(Ctx); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 607 | if (!Diags) |
| 608 | return; // A new reparse was requested before this one completed. |
Ilya Biryukov | 47f2202 | 2017-09-20 12:58:55 +0000 | [diff] [blame] | 609 | |
| 610 | // We need to serialize access to resulting diagnostics to avoid calling |
| 611 | // `onDiagnosticsReady` in the wrong order. |
| 612 | std::lock_guard<std::mutex> DiagsLock(DiagnosticsMutex); |
| 613 | DocVersion &LastReportedDiagsVersion = ReportedDiagnosticVersions[FileStr]; |
| 614 | // FIXME(ibiryukov): get rid of '<' comparison here. In the current |
| 615 | // implementation diagnostics will not be reported after version counters' |
| 616 | // overflow. This should not happen in practice, since DocVersion is a |
| 617 | // 64-bit unsigned integer. |
| 618 | if (Version < LastReportedDiagsVersion) |
| 619 | return; |
| 620 | LastReportedDiagsVersion = Version; |
| 621 | |
Ilya Biryukov | 9555839 | 2018-01-10 17:59:27 +0000 | [diff] [blame] | 622 | DiagConsumer.onDiagnosticsReady(Ctx, FileStr, |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 623 | make_tagged(std::move(*Diags), Tag)); |
| 624 | }; |
| 625 | |
| 626 | WorkScheduler.addToFront(std::move(ReparseAndPublishDiags), |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 627 | std::move(DeferredRebuild), std::move(DonePromise), |
| 628 | std::move(Ctx)); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 629 | return DoneFuture; |
| 630 | } |
| 631 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 632 | std::future<Context> |
| 633 | ClangdServer::scheduleCancelRebuild(Context Ctx, |
| 634 | std::shared_ptr<CppFile> Resources) { |
| 635 | std::promise<Context> DonePromise; |
| 636 | std::future<Context> DoneFuture = DonePromise.get_future(); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 637 | if (!Resources) { |
| 638 | // No need to schedule any cleanup. |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 639 | DonePromise.set_value(std::move(Ctx)); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 640 | return DoneFuture; |
| 641 | } |
| 642 | |
Ilya Biryukov | 98a1fd7 | 2017-10-10 16:12:54 +0000 | [diff] [blame] | 643 | UniqueFunction<void()> DeferredCancel = Resources->deferCancelRebuild(); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 644 | auto CancelReparses = [Resources](std::promise<Context> DonePromise, |
| 645 | UniqueFunction<void()> DeferredCancel, |
| 646 | Context Ctx) { |
Ilya Biryukov | 98a1fd7 | 2017-10-10 16:12:54 +0000 | [diff] [blame] | 647 | DeferredCancel(); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 648 | DonePromise.set_value(std::move(Ctx)); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 649 | }; |
| 650 | WorkScheduler.addToFront(std::move(CancelReparses), std::move(DonePromise), |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 651 | std::move(DeferredCancel), std::move(Ctx)); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 652 | return DoneFuture; |
| 653 | } |
Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 654 | |
| 655 | void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) { |
| 656 | // FIXME: Do nothing for now. This will be used for indexing and potentially |
| 657 | // invalidating other caches. |
| 658 | } |