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" |
Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Path.h" |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include <future> |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 28 | |
Ilya Biryukov | 2f31410 | 2017-05-16 10:06:20 +0000 | [diff] [blame] | 29 | using namespace clang; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 30 | using namespace clang::clangd; |
| 31 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 34 | void ignoreError(llvm::Error Err) { |
| 35 | handleAllErrors(std::move(Err), [](const llvm::ErrorInfoBase &) {}); |
| 36 | } |
| 37 | |
Ilya Biryukov | a46f7a9 | 2017-06-28 10:34:50 +0000 | [diff] [blame] | 38 | std::string getStandardResourceDir() { |
| 39 | static int Dummy; // Just an address in this process. |
| 40 | return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy); |
| 41 | } |
| 42 | |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 43 | class RefactoringResultCollector final |
| 44 | : public tooling::RefactoringResultConsumer { |
| 45 | public: |
| 46 | void handleError(llvm::Error Err) override { |
| 47 | assert(!Result.hasValue()); |
| 48 | // FIXME: figure out a way to return better message for DiagnosticError. |
| 49 | // clangd uses llvm::toString to convert the Err to string, however, for |
| 50 | // DiagnosticError, only "clang diagnostic" will be generated. |
| 51 | Result = std::move(Err); |
| 52 | } |
| 53 | |
| 54 | // Using the handle(SymbolOccurrences) from parent class. |
| 55 | using tooling::RefactoringResultConsumer::handle; |
| 56 | |
| 57 | void handle(tooling::AtomicChanges SourceReplacements) override { |
| 58 | assert(!Result.hasValue()); |
| 59 | Result = std::move(SourceReplacements); |
| 60 | } |
| 61 | |
| 62 | Optional<Expected<tooling::AtomicChanges>> Result; |
| 63 | }; |
| 64 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 65 | } // namespace |
| 66 | |
Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 67 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> |
Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 68 | RealFileSystemProvider::getTaggedFileSystem(PathRef File) { |
Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 69 | return make_tagged(vfs::getRealFileSystem(), VFSTag()); |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 72 | ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB, |
| 73 | DiagnosticsConsumer &DiagConsumer, |
| 74 | FileSystemProvider &FSProvider, |
| 75 | unsigned AsyncThreadsCount, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 76 | bool StorePreamblesInMemory, |
Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 77 | bool BuildDynamicSymbolIndex, SymbolIndex *StaticIdx, |
Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 78 | llvm::Optional<StringRef> ResourceDir) |
Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 79 | : CompileArgs(CDB, |
| 80 | ResourceDir ? ResourceDir->str() : getStandardResourceDir()), |
| 81 | DiagConsumer(DiagConsumer), FSProvider(FSProvider), |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 82 | FileIdx(BuildDynamicSymbolIndex ? new FileIndex() : nullptr), |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 83 | PCHs(std::make_shared<PCHContainerOperations>()), |
| 84 | // Pass a callback into `WorkScheduler` to extract symbols from a newly |
| 85 | // parsed file and rebuild the file index synchronously each time an AST |
| 86 | // is parsed. |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 87 | // FIXME(ioeric): this can be slow and we may be able to index on less |
| 88 | // critical paths. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 89 | WorkScheduler(AsyncThreadsCount, StorePreamblesInMemory, |
| 90 | FileIdx |
| 91 | ? [this](PathRef Path, |
| 92 | ParsedAST *AST) { FileIdx->update(Path, AST); } |
| 93 | : ASTParsedCallback()) { |
Sam McCall | 0faecf0 | 2018-01-15 12:33:00 +0000 | [diff] [blame] | 94 | if (FileIdx && StaticIdx) { |
| 95 | MergedIndex = mergeIndex(FileIdx.get(), StaticIdx); |
| 96 | Index = MergedIndex.get(); |
| 97 | } else if (FileIdx) |
| 98 | Index = FileIdx.get(); |
| 99 | else if (StaticIdx) |
| 100 | Index = StaticIdx; |
| 101 | else |
| 102 | Index = nullptr; |
| 103 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 104 | |
Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 105 | void ClangdServer::setRootPath(PathRef RootPath) { |
| 106 | std::string NewRootPath = llvm::sys::path::convert_to_slash( |
| 107 | RootPath, llvm::sys::path::Style::posix); |
| 108 | if (llvm::sys::fs::is_directory(NewRootPath)) |
| 109 | this->RootPath = NewRootPath; |
| 110 | } |
| 111 | |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 112 | void ClangdServer::addDocument(PathRef File, StringRef Contents) { |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 113 | DocVersion Version = DraftMgr.updateDraft(File, Contents); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 114 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 115 | scheduleReparseAndDiags(File, VersionedDraft{Version, Contents.str()}, |
| 116 | std::move(TaggedFS)); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Ilya Biryukov | 7e5ee26 | 2018-02-08 07:37:35 +0000 | [diff] [blame] | 119 | void ClangdServer::removeDocument(PathRef File) { |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 120 | DraftMgr.removeDraft(File); |
Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 121 | CompileArgs.invalidate(File); |
Ilya Biryukov | 7e5ee26 | 2018-02-08 07:37:35 +0000 | [diff] [blame] | 122 | WorkScheduler.remove(File); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 125 | void ClangdServer::forceReparse(PathRef File) { |
Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 126 | auto FileContents = DraftMgr.getDraft(File); |
| 127 | assert(FileContents.Draft && |
| 128 | "forceReparse() was called for non-added document"); |
| 129 | |
Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 130 | // forceReparse promises to request new compilation flags from CDB, so we |
| 131 | // remove any cahced flags. |
| 132 | CompileArgs.invalidate(File); |
| 133 | |
Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 134 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 135 | scheduleReparseAndDiags(File, std::move(FileContents), std::move(TaggedFS)); |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 138 | void ClangdServer::codeComplete( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 139 | PathRef File, Position Pos, const clangd::CodeCompleteOptions &Opts, |
| 140 | UniqueFunction<void(Tagged<CompletionList>)> Callback, |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 141 | llvm::Optional<StringRef> OverridenContents, |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 142 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 143 | using CallbackType = UniqueFunction<void(Tagged<CompletionList>)>; |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 144 | |
Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 145 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Ilya Biryukov | ed99e4c | 2017-07-31 17:09:29 +0000 | [diff] [blame] | 146 | if (UsedFS) |
| 147 | *UsedFS = TaggedFS.Value; |
| 148 | |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 149 | // Copy completion options for passing them to async task handler. |
| 150 | auto CodeCompleteOpts = Opts; |
Sam McCall | 0faecf0 | 2018-01-15 12:33:00 +0000 | [diff] [blame] | 151 | if (!CodeCompleteOpts.Index) // Respect overridden index. |
| 152 | CodeCompleteOpts.Index = Index; |
Ilya Biryukov | f6e2b4c | 2018-01-09 14:39:27 +0000 | [diff] [blame] | 153 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 154 | std::string Contents; |
| 155 | if (OverridenContents) { |
| 156 | Contents = OverridenContents->str(); |
| 157 | } else { |
| 158 | VersionedDraft Latest = DraftMgr.getDraft(File); |
| 159 | assert(Latest.Draft && "codeComplete called for non-added document"); |
| 160 | Contents = *Latest.Draft; |
| 161 | } |
| 162 | |
Ilya Biryukov | f6e2b4c | 2018-01-09 14:39:27 +0000 | [diff] [blame] | 163 | // Copy PCHs to avoid accessing this->PCHs concurrently |
| 164 | std::shared_ptr<PCHContainerOperations> PCHs = this->PCHs; |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 165 | auto Task = [PCHs, Pos, TaggedFS, CodeCompleteOpts]( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 166 | std::string Contents, Path File, CallbackType Callback, |
| 167 | llvm::Expected<InputsAndPreamble> IP) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 168 | assert(IP && "error when trying to read preamble for codeComplete"); |
| 169 | auto PreambleData = IP->Preamble; |
| 170 | auto &Command = IP->Inputs.CompileCommand; |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 171 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 172 | // FIXME(ibiryukov): even if Preamble is non-null, we may want to check |
| 173 | // both the old and the new version in case only one of them matches. |
| 174 | CompletionList Result = clangd::codeComplete( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 175 | File, Command, PreambleData ? &PreambleData->Preamble : nullptr, |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 176 | Contents, Pos, TaggedFS.Value, PCHs, CodeCompleteOpts); |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 177 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 178 | Callback(make_tagged(std::move(Result), std::move(TaggedFS.Tag))); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 181 | WorkScheduler.runWithPreamble(File, BindWithForward(Task, std::move(Contents), |
| 182 | File.str(), |
| 183 | std::move(Callback))); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 184 | } |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 185 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 186 | void ClangdServer::signatureHelp( |
| 187 | PathRef File, Position Pos, |
| 188 | UniqueFunction<void(llvm::Expected<Tagged<SignatureHelp>>)> Callback, |
| 189 | llvm::Optional<StringRef> OverridenContents, |
| 190 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) { |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 191 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 192 | if (UsedFS) |
| 193 | *UsedFS = TaggedFS.Value; |
| 194 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 195 | std::string Contents; |
| 196 | if (OverridenContents) { |
| 197 | Contents = OverridenContents->str(); |
| 198 | } else { |
| 199 | VersionedDraft Latest = DraftMgr.getDraft(File); |
| 200 | if (!Latest.Draft) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 201 | return Callback(llvm::make_error<llvm::StringError>( |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 202 | "signatureHelp is called for non-added document", |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 203 | llvm::errc::invalid_argument)); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 204 | Contents = std::move(*Latest.Draft); |
| 205 | } |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 206 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 207 | auto PCHs = this->PCHs; |
| 208 | auto Action = [Contents, Pos, TaggedFS, |
| 209 | PCHs](Path File, decltype(Callback) Callback, |
| 210 | llvm::Expected<InputsAndPreamble> IP) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 211 | if (!IP) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 212 | return Callback(IP.takeError()); |
| 213 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 214 | auto PreambleData = IP->Preamble; |
| 215 | auto &Command = IP->Inputs.CompileCommand; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 216 | Callback(make_tagged( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 217 | clangd::signatureHelp(File, Command, |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 218 | PreambleData ? &PreambleData->Preamble : nullptr, |
| 219 | Contents, Pos, TaggedFS.Value, PCHs), |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 220 | TaggedFS.Tag)); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 221 | }; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 222 | |
| 223 | WorkScheduler.runWithPreamble( |
| 224 | File, BindWithForward(Action, File.str(), std::move(Callback))); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 227 | llvm::Expected<tooling::Replacements> |
| 228 | ClangdServer::formatRange(StringRef Code, PathRef File, Range Rng) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 229 | size_t Begin = positionToOffset(Code, Rng.start); |
| 230 | size_t Len = positionToOffset(Code, Rng.end) - Begin; |
| 231 | return formatCode(Code, File, {tooling::Range(Begin, Len)}); |
| 232 | } |
| 233 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 234 | llvm::Expected<tooling::Replacements> ClangdServer::formatFile(StringRef Code, |
| 235 | PathRef File) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 236 | // Format everything. |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 237 | return formatCode(Code, File, {tooling::Range(0, Code.size())}); |
| 238 | } |
| 239 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 240 | llvm::Expected<tooling::Replacements> |
| 241 | ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 242 | // Look for the previous opening brace from the character position and |
| 243 | // format starting from there. |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 244 | size_t CursorPos = positionToOffset(Code, Pos); |
| 245 | size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos); |
| 246 | if (PreviousLBracePos == StringRef::npos) |
| 247 | PreviousLBracePos = CursorPos; |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 248 | size_t Len = CursorPos - PreviousLBracePos; |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 249 | |
| 250 | return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)}); |
| 251 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 252 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 253 | void ClangdServer::rename( |
| 254 | PathRef File, Position Pos, llvm::StringRef NewName, |
| 255 | UniqueFunction<void(Expected<std::vector<tooling::Replacement>>)> |
| 256 | Callback) { |
| 257 | auto Action = [Pos](Path File, std::string NewName, |
| 258 | decltype(Callback) Callback, |
| 259 | Expected<InputsAndAST> InpAST) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 260 | if (!InpAST) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 261 | return Callback(InpAST.takeError()); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 262 | auto &AST = InpAST->AST; |
| 263 | |
| 264 | RefactoringResultCollector ResultCollector; |
| 265 | const SourceManager &SourceMgr = AST.getASTContext().getSourceManager(); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 266 | const FileEntry *FE = |
| 267 | SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); |
| 268 | if (!FE) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 269 | return Callback(llvm::make_error<llvm::StringError>( |
| 270 | "rename called for non-added document", |
| 271 | llvm::errc::invalid_argument)); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 272 | SourceLocation SourceLocationBeg = |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 273 | clangd::getBeginningOfIdentifier(AST, Pos, FE); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 274 | tooling::RefactoringRuleContext Context( |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 275 | AST.getASTContext().getSourceManager()); |
| 276 | Context.setASTContext(AST.getASTContext()); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 277 | auto Rename = clang::tooling::RenameOccurrences::initiate( |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 278 | Context, SourceRange(SourceLocationBeg), NewName); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 279 | if (!Rename) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 280 | return Callback(Rename.takeError()); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 281 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 282 | Rename->invoke(ResultCollector, Context); |
| 283 | |
| 284 | assert(ResultCollector.Result.hasValue()); |
| 285 | if (!ResultCollector.Result.getValue()) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 286 | return Callback(ResultCollector.Result->takeError()); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 287 | |
| 288 | std::vector<tooling::Replacement> Replacements; |
| 289 | for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) { |
| 290 | tooling::Replacements ChangeReps = Change.getReplacements(); |
| 291 | for (const auto &Rep : ChangeReps) { |
| 292 | // FIXME: Right now we only support renaming the main file, so we |
| 293 | // drop replacements not for the main file. In the future, we might |
| 294 | // consider to support: |
| 295 | // * rename in any included header |
| 296 | // * rename only in the "main" header |
| 297 | // * provide an error if there are symbols we won't rename (e.g. |
| 298 | // std::vector) |
| 299 | // * rename globally in project |
| 300 | // * rename in open files |
| 301 | if (Rep.getFilePath() == File) |
| 302 | Replacements.push_back(Rep); |
| 303 | } |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 304 | } |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 305 | return Callback(Replacements); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 306 | }; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 307 | |
| 308 | WorkScheduler.runWithAST( |
| 309 | File, |
| 310 | BindWithForward(Action, File.str(), NewName.str(), std::move(Callback))); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 313 | llvm::Optional<std::string> ClangdServer::getDocument(PathRef File) { |
| 314 | auto Latest = DraftMgr.getDraft(File); |
| 315 | if (!Latest.Draft) |
| 316 | return llvm::None; |
| 317 | return std::move(*Latest.Draft); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 320 | void ClangdServer::dumpAST(PathRef File, |
| 321 | UniqueFunction<void(std::string)> Callback) { |
| 322 | auto Action = [](decltype(Callback) Callback, |
| 323 | llvm::Expected<InputsAndAST> InpAST) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 324 | if (!InpAST) { |
| 325 | ignoreError(InpAST.takeError()); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 326 | return Callback("<no-ast>"); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 327 | } |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 328 | std::string Result; |
| 329 | |
| 330 | llvm::raw_string_ostream ResultOS(Result); |
| 331 | clangd::dumpAST(InpAST->AST, ResultOS); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 332 | ResultOS.flush(); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 333 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 334 | Callback(Result); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 335 | }; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 336 | |
| 337 | WorkScheduler.runWithAST(File, BindWithForward(Action, std::move(Callback))); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 338 | } |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 339 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 340 | void ClangdServer::findDefinitions( |
| 341 | PathRef File, Position Pos, |
| 342 | UniqueFunction<void(llvm::Expected<Tagged<std::vector<Location>>>)> |
| 343 | Callback) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 344 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 345 | auto Action = [Pos, TaggedFS](decltype(Callback) Callback, |
| 346 | llvm::Expected<InputsAndAST> InpAST) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 347 | if (!InpAST) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 348 | return Callback(InpAST.takeError()); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 349 | auto Result = clangd::findDefinitions(InpAST->AST, Pos); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 350 | Callback(make_tagged(std::move(Result), TaggedFS.Tag)); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 351 | }; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 352 | |
| 353 | WorkScheduler.runWithAST(File, BindWithForward(Action, std::move(Callback))); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 354 | } |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 355 | |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 356 | llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) { |
| 357 | |
| 358 | StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx", |
| 359 | ".c++", ".m", ".mm"}; |
| 360 | StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"}; |
| 361 | |
| 362 | StringRef PathExt = llvm::sys::path::extension(Path); |
| 363 | |
| 364 | // Lookup in a list of known extensions. |
| 365 | auto SourceIter = |
| 366 | std::find_if(std::begin(SourceExtensions), std::end(SourceExtensions), |
| 367 | [&PathExt](PathRef SourceExt) { |
| 368 | return SourceExt.equals_lower(PathExt); |
| 369 | }); |
| 370 | bool IsSource = SourceIter != std::end(SourceExtensions); |
| 371 | |
| 372 | auto HeaderIter = |
| 373 | std::find_if(std::begin(HeaderExtensions), std::end(HeaderExtensions), |
| 374 | [&PathExt](PathRef HeaderExt) { |
| 375 | return HeaderExt.equals_lower(PathExt); |
| 376 | }); |
| 377 | |
| 378 | bool IsHeader = HeaderIter != std::end(HeaderExtensions); |
| 379 | |
| 380 | // We can only switch between extensions known extensions. |
| 381 | if (!IsSource && !IsHeader) |
| 382 | return llvm::None; |
| 383 | |
| 384 | // Array to lookup extensions for the switch. An opposite of where original |
| 385 | // extension was found. |
| 386 | ArrayRef<StringRef> NewExts; |
| 387 | if (IsSource) |
| 388 | NewExts = HeaderExtensions; |
| 389 | else |
| 390 | NewExts = SourceExtensions; |
| 391 | |
| 392 | // Storage for the new path. |
| 393 | SmallString<128> NewPath = StringRef(Path); |
| 394 | |
| 395 | // Instance of vfs::FileSystem, used for file existence checks. |
| 396 | auto FS = FSProvider.getTaggedFileSystem(Path).Value; |
| 397 | |
| 398 | // Loop through switched extension candidates. |
| 399 | for (StringRef NewExt : NewExts) { |
| 400 | llvm::sys::path::replace_extension(NewPath, NewExt); |
| 401 | if (FS->exists(NewPath)) |
| 402 | return NewPath.str().str(); // First str() to convert from SmallString to |
| 403 | // StringRef, second to convert from StringRef |
| 404 | // to std::string |
Ilya Biryukov | 3333494 | 2017-10-06 14:39:39 +0000 | [diff] [blame] | 405 | |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 406 | // Also check NewExt in upper-case, just in case. |
| 407 | llvm::sys::path::replace_extension(NewPath, NewExt.upper()); |
| 408 | if (FS->exists(NewPath)) |
| 409 | return NewPath.str().str(); |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | return llvm::None; |
| 413 | } |
| 414 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 415 | llvm::Expected<tooling::Replacements> |
| 416 | ClangdServer::formatCode(llvm::StringRef Code, PathRef File, |
| 417 | ArrayRef<tooling::Range> Ranges) { |
| 418 | // Call clang-format. |
| 419 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 420 | auto StyleOrError = |
| 421 | format::getStyle("file", File, "LLVM", Code, TaggedFS.Value.get()); |
| 422 | if (!StyleOrError) { |
| 423 | return StyleOrError.takeError(); |
| 424 | } else { |
| 425 | return format::reformat(StyleOrError.get(), Code, Ranges, File); |
| 426 | } |
| 427 | } |
| 428 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 429 | void ClangdServer::findDocumentHighlights( |
| 430 | PathRef File, Position Pos, |
| 431 | UniqueFunction<void(llvm::Expected<Tagged<std::vector<DocumentHighlight>>>)> |
| 432 | Callback) { |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 433 | auto FileContents = DraftMgr.getDraft(File); |
| 434 | if (!FileContents.Draft) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 435 | return Callback(llvm::make_error<llvm::StringError>( |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 436 | "findDocumentHighlights called on non-added file", |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 437 | llvm::errc::invalid_argument)); |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 438 | |
| 439 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 440 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 441 | auto Action = [TaggedFS, Pos](decltype(Callback) Callback, |
| 442 | llvm::Expected<InputsAndAST> InpAST) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 443 | if (!InpAST) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 444 | return Callback(InpAST.takeError()); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 445 | auto Result = clangd::findDocumentHighlights(InpAST->AST, Pos); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 446 | Callback(make_tagged(std::move(Result), TaggedFS.Tag)); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 447 | }; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame^] | 448 | |
| 449 | WorkScheduler.runWithAST(File, BindWithForward(Action, std::move(Callback))); |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 452 | void ClangdServer::scheduleReparseAndDiags( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 453 | PathRef File, VersionedDraft Contents, |
Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 454 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 455 | tooling::CompileCommand Command = CompileArgs.getCompileCommand(File); |
Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 456 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 457 | using OptDiags = llvm::Optional<std::vector<DiagWithFixIts>>; |
| 458 | |
| 459 | DocVersion Version = Contents.Version; |
| 460 | Path FileStr = File.str(); |
| 461 | VFSTag Tag = std::move(TaggedFS.Tag); |
| 462 | |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 463 | auto Callback = [this, Version, FileStr, Tag](OptDiags Diags) { |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 464 | if (!Diags) |
| 465 | return; // A new reparse was requested before this one completed. |
Ilya Biryukov | 47f2202 | 2017-09-20 12:58:55 +0000 | [diff] [blame] | 466 | |
| 467 | // We need to serialize access to resulting diagnostics to avoid calling |
| 468 | // `onDiagnosticsReady` in the wrong order. |
| 469 | std::lock_guard<std::mutex> DiagsLock(DiagnosticsMutex); |
| 470 | DocVersion &LastReportedDiagsVersion = ReportedDiagnosticVersions[FileStr]; |
| 471 | // FIXME(ibiryukov): get rid of '<' comparison here. In the current |
| 472 | // implementation diagnostics will not be reported after version counters' |
| 473 | // overflow. This should not happen in practice, since DocVersion is a |
| 474 | // 64-bit unsigned integer. |
| 475 | if (Version < LastReportedDiagsVersion) |
| 476 | return; |
| 477 | LastReportedDiagsVersion = Version; |
| 478 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 479 | DiagConsumer.onDiagnosticsReady( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 480 | FileStr, make_tagged(std::move(*Diags), std::move(Tag))); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 481 | }; |
| 482 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 483 | WorkScheduler.update(File, |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 484 | ParseInputs{std::move(Command), |
| 485 | std::move(TaggedFS.Value), |
| 486 | std::move(*Contents.Draft)}, |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 487 | std::move(Callback)); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 488 | } |
Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 489 | |
| 490 | void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) { |
| 491 | // FIXME: Do nothing for now. This will be used for indexing and potentially |
| 492 | // invalidating other caches. |
| 493 | } |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 494 | |
| 495 | std::vector<std::pair<Path, std::size_t>> |
| 496 | ClangdServer::getUsedBytesPerFile() const { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 497 | return WorkScheduler.getUsedBytesPerFile(); |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 498 | } |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 499 | |
| 500 | LLVM_NODISCARD bool |
| 501 | ClangdServer::blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds) { |
| 502 | return WorkScheduler.blockUntilIdle(timeoutSeconds(TimeoutSeconds)); |
| 503 | } |