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