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