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