| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1 | //===--- ClangdServer.h - 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 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDSERVER_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDSERVER_H |
| 12 | |
| Eric Liu | b99d5e8 | 2017-12-14 21:22:03 +0000 | [diff] [blame] | 13 | #include "ClangdUnit.h" |
| Eric Liu | b99d5e8 | 2017-12-14 21:22:03 +0000 | [diff] [blame] | 14 | #include "CodeComplete.h" |
| Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 15 | #include "CompileArgsCache.h" |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 16 | #include "DraftStore.h" |
| Eric Liu | b99d5e8 | 2017-12-14 21:22:03 +0000 | [diff] [blame] | 17 | #include "Function.h" |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 18 | #include "GlobalCompilationDatabase.h" |
| Eric Liu | b99d5e8 | 2017-12-14 21:22:03 +0000 | [diff] [blame] | 19 | #include "Protocol.h" |
| Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 20 | #include "TUScheduler.h" |
| Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 21 | #include "index/FileIndex.h" |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 22 | #include "clang/Tooling/CompilationDatabase.h" |
| 23 | #include "clang/Tooling/Core/Replacement.h" |
| 24 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 25 | #include "llvm/ADT/Optional.h" |
| 26 | #include "llvm/ADT/StringRef.h" |
| Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 27 | #include <functional> |
| Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 28 | #include <future> |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 29 | #include <string> |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 30 | #include <type_traits> |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 31 | #include <utility> |
| 32 | |
| 33 | namespace clang { |
| 34 | class PCHContainerOperations; |
| 35 | |
| 36 | namespace clangd { |
| 37 | |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 38 | /// A tag supplied by the FileSytemProvider. |
| Ilya Biryukov | e36035b | 2017-06-13 08:24:48 +0000 | [diff] [blame] | 39 | typedef std::string VFSTag; |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 40 | |
| 41 | /// A value of an arbitrary type and VFSTag that was supplied by the |
| 42 | /// FileSystemProvider when this value was computed. |
| 43 | template <class T> class Tagged { |
| 44 | public: |
| Ilya Biryukov | e81b762 | 2017-10-05 22:15:15 +0000 | [diff] [blame] | 45 | // MSVC requires future<> arguments to be default-constructible. |
| 46 | Tagged() = default; |
| 47 | |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 48 | template <class U> |
| Ilya Biryukov | e36035b | 2017-06-13 08:24:48 +0000 | [diff] [blame] | 49 | Tagged(U &&Value, VFSTag Tag) |
| 50 | : Value(std::forward<U>(Value)), Tag(std::move(Tag)) {} |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 51 | |
| 52 | template <class U> |
| 53 | Tagged(const Tagged<U> &Other) : Value(Other.Value), Tag(Other.Tag) {} |
| 54 | |
| 55 | template <class U> |
| Ilya Biryukov | e36035b | 2017-06-13 08:24:48 +0000 | [diff] [blame] | 56 | Tagged(Tagged<U> &&Other) |
| 57 | : Value(std::move(Other.Value)), Tag(std::move(Other.Tag)) {} |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 58 | |
| Ilya Biryukov | e81b762 | 2017-10-05 22:15:15 +0000 | [diff] [blame] | 59 | T Value = T(); |
| 60 | VFSTag Tag = VFSTag(); |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | template <class T> |
| 64 | Tagged<typename std::decay<T>::type> make_tagged(T &&Value, VFSTag Tag) { |
| Ilya Biryukov | c22824a | 2017-08-09 12:55:13 +0000 | [diff] [blame] | 65 | return Tagged<typename std::decay<T>::type>(std::forward<T>(Value), Tag); |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 68 | class DiagnosticsConsumer { |
| 69 | public: |
| 70 | virtual ~DiagnosticsConsumer() = default; |
| 71 | |
| 72 | /// Called by ClangdServer when \p Diagnostics for \p File are ready. |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 73 | virtual void |
| Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 74 | onDiagnosticsReady(PathRef File, |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 75 | Tagged<std::vector<DiagWithFixIts>> Diagnostics) = 0; |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 78 | class FileSystemProvider { |
| 79 | public: |
| 80 | virtual ~FileSystemProvider() = default; |
| Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 81 | /// Called by ClangdServer to obtain a vfs::FileSystem to be used for parsing. |
| 82 | /// Name of the file that will be parsed is passed in \p File. |
| 83 | /// |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 84 | /// \return A filesystem that will be used for all file accesses in clangd. |
| 85 | /// A Tag returned by this method will be propagated to all results of clangd |
| 86 | /// that will use this filesystem. |
| Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 87 | virtual Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> |
| 88 | getTaggedFileSystem(PathRef File) = 0; |
| Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | class RealFileSystemProvider : public FileSystemProvider { |
| 92 | public: |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 93 | /// \return getRealFileSystem() tagged with default tag, i.e. VFSTag() |
| Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 94 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> |
| 95 | getTaggedFileSystem(PathRef File) override; |
| Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 98 | /// Provides API to manage ASTs for a collection of C++ files and request |
| Ilya Biryukov | 75337e8 | 2017-08-22 09:16:46 +0000 | [diff] [blame] | 99 | /// various language features. |
| 100 | /// Currently supports async diagnostics, code completion, formatting and goto |
| 101 | /// definition. |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 102 | class ClangdServer { |
| 103 | public: |
| Ilya Biryukov | 75337e8 | 2017-08-22 09:16:46 +0000 | [diff] [blame] | 104 | /// Creates a new ClangdServer instance. |
| 105 | /// To process parsing requests asynchronously, ClangdServer will spawn \p |
| 106 | /// AsyncThreadsCount worker threads. However, if \p AsyncThreadsCount is 0, |
| 107 | /// all requests will be processed on the calling thread. |
| 108 | /// |
| 109 | /// ClangdServer uses \p FSProvider to get an instance of vfs::FileSystem for |
| 110 | /// each parsing request. Results of code completion and diagnostics also |
| 111 | /// include a tag, that \p FSProvider returns along with the vfs::FileSystem. |
| 112 | /// |
| 113 | /// The value of \p ResourceDir will be used to search for internal headers |
| 114 | /// (overriding defaults and -resource-dir compiler flag). If \p ResourceDir |
| 115 | /// is None, ClangdServer will call CompilerInvocation::GetResourcePath() to |
| 116 | /// obtain the standard resource directory. |
| 117 | /// |
| 118 | /// ClangdServer uses \p CDB to obtain compilation arguments for parsing. Note |
| 119 | /// that ClangdServer only obtains compilation arguments once for each newly |
| 120 | /// added file (i.e., when processing a first call to addDocument) and reuses |
| 121 | /// those arguments for subsequent reparses. However, ClangdServer will check |
| 122 | /// if compilation arguments changed on calls to forceReparse(). |
| 123 | /// |
| 124 | /// After each parsing request finishes, ClangdServer reports diagnostics to |
| 125 | /// \p DiagConsumer. Note that a callback to \p DiagConsumer happens on a |
| 126 | /// worker thread. Therefore, instances of \p DiagConsumer must properly |
| 127 | /// synchronize access to shared state. |
| Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 128 | /// |
| Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 129 | /// \p StorePreamblesInMemory defines whether the Preambles generated by |
| 130 | /// clangd are stored in-memory or on disk. |
| Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 131 | /// |
| 132 | /// If \p BuildDynamicSymbolIndex is true, ClangdServer builds a dynamic |
| 133 | /// in-memory index for symbols in all opened files and uses the index to |
| 134 | /// augment code completion results. |
| Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 135 | /// |
| 136 | /// If \p StaticIdx is set, ClangdServer uses the index for global code |
| 137 | /// completion. |
| Ilya Biryukov | 103c951 | 2017-06-13 15:59:43 +0000 | [diff] [blame] | 138 | ClangdServer(GlobalCompilationDatabase &CDB, |
| 139 | DiagnosticsConsumer &DiagConsumer, |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 140 | FileSystemProvider &FSProvider, unsigned AsyncThreadsCount, |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 141 | bool StorePreamblesInMemory, |
| Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 142 | bool BuildDynamicSymbolIndex = false, |
| Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 143 | SymbolIndex *StaticIdx = nullptr, |
| Ilya Biryukov | a46f7a9 | 2017-06-28 10:34:50 +0000 | [diff] [blame] | 144 | llvm::Optional<StringRef> ResourceDir = llvm::None); |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 145 | |
| Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 146 | /// Set the root path of the workspace. |
| 147 | void setRootPath(PathRef RootPath); |
| 148 | |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 149 | /// Add a \p File to the list of tracked C++ files or update the contents if |
| 150 | /// \p File is already tracked. Also schedules parsing of the AST for it on a |
| 151 | /// separate thread. When the parsing is complete, DiagConsumer passed in |
| 152 | /// constructor will receive onDiagnosticsReady callback. |
| Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame^] | 153 | void addDocument(PathRef File, StringRef Contents, |
| 154 | WantDiagnostics WD = WantDiagnostics::Auto); |
| Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 155 | |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 156 | /// Remove \p File from list of tracked files, schedule a request to free |
| 157 | /// resources associated with it. |
| Ilya Biryukov | 7e5ee26 | 2018-02-08 07:37:35 +0000 | [diff] [blame] | 158 | void removeDocument(PathRef File); |
| Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 159 | |
| Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 160 | /// Force \p File to be reparsed using the latest contents. |
| Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 161 | /// Will also check if CompileCommand, provided by GlobalCompilationDatabase |
| 162 | /// for \p File has changed. If it has, will remove currently stored Preamble |
| 163 | /// and AST and rebuild them from scratch. |
| Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 164 | void forceReparse(PathRef File); |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 165 | |
| Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 166 | /// Run code completion for \p File at \p Pos. |
| Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 167 | /// Request is processed asynchronously. |
| Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 168 | /// |
| 169 | /// If \p OverridenContents is not None, they will used only for code |
| 170 | /// completion, i.e. no diagnostics update will be scheduled and a draft for |
| 171 | /// \p File will not be updated. If \p OverridenContents is None, contents of |
| 172 | /// the current draft for \p File will be used. If \p UsedFS is non-null, it |
| 173 | /// will be overwritten by vfs::FileSystem used for completion. |
| 174 | /// |
| 175 | /// This method should only be called for currently tracked files. However, it |
| 176 | /// is safe to call removeDocument for \p File after this method returns, even |
| 177 | /// while returned future is not yet ready. |
| Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 178 | /// A version of `codeComplete` that runs \p Callback on the processing thread |
| 179 | /// when codeComplete results become available. |
| Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 180 | void codeComplete(PathRef File, Position Pos, |
| 181 | const clangd::CodeCompleteOptions &Opts, |
| 182 | UniqueFunction<void(Tagged<CompletionList>)> Callback, |
| 183 | llvm::Optional<StringRef> OverridenContents = llvm::None, |
| 184 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS = nullptr); |
| 185 | |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 186 | /// Provide signature help for \p File at \p Pos. If \p OverridenContents is |
| 187 | /// not None, they will used only for signature help, i.e. no diagnostics |
| 188 | /// update will be scheduled and a draft for \p File will not be updated. If |
| 189 | /// \p OverridenContents is None, contents of the current draft for \p File |
| 190 | /// will be used. If \p UsedFS is non-null, it will be overwritten by |
| 191 | /// vfs::FileSystem used for signature help. This method should only be called |
| 192 | /// for currently tracked files. |
| Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 193 | void signatureHelp( |
| 194 | PathRef File, Position Pos, |
| 195 | UniqueFunction<void(llvm::Expected<Tagged<SignatureHelp>>)> Callback, |
| 196 | llvm::Optional<StringRef> OverridenContents = llvm::None, |
| 197 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS = nullptr); |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 198 | |
| Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 199 | /// Get definition of symbol at a specified \p Line and \p Column in \p File. |
| Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 200 | void findDefinitions( |
| 201 | PathRef File, Position Pos, |
| 202 | UniqueFunction<void(llvm::Expected<Tagged<std::vector<Location>>>)> |
| 203 | Callback); |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 204 | |
| Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 205 | /// Helper function that returns a path to the corresponding source file when |
| 206 | /// given a header file and vice versa. |
| 207 | llvm::Optional<Path> switchSourceHeader(PathRef Path); |
| 208 | |
| Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 209 | /// Get document highlights for a given position. |
| Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 210 | void findDocumentHighlights( |
| 211 | PathRef File, Position Pos, |
| 212 | UniqueFunction< |
| 213 | void(llvm::Expected<Tagged<std::vector<DocumentHighlight>>>)> |
| 214 | Callback); |
| Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 215 | |
| Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 216 | /// Get code hover for a given position. |
| 217 | void findHover(PathRef File, Position Pos, |
| 218 | UniqueFunction<void(llvm::Expected<Tagged<Hover>>)> Callback); |
| 219 | |
| Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 220 | /// Run formatting for \p Rng inside \p File with content \p Code. |
| 221 | llvm::Expected<tooling::Replacements> formatRange(StringRef Code, |
| 222 | PathRef File, Range Rng); |
| 223 | |
| 224 | /// Run formatting for the whole \p File with content \p Code. |
| 225 | llvm::Expected<tooling::Replacements> formatFile(StringRef Code, |
| 226 | PathRef File); |
| 227 | |
| 228 | /// Run formatting after a character was typed at \p Pos in \p File with |
| 229 | /// content \p Code. |
| 230 | llvm::Expected<tooling::Replacements> |
| 231 | formatOnType(StringRef Code, PathRef File, Position Pos); |
| 232 | |
| Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 233 | /// Rename all occurrences of the symbol at the \p Pos in \p File to |
| 234 | /// \p NewName. |
| Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 235 | void rename(PathRef File, Position Pos, llvm::StringRef NewName, |
| 236 | UniqueFunction<void(Expected<std::vector<tooling::Replacement>>)> |
| 237 | Callback); |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 238 | |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 239 | /// Inserts a new #include of \p Header into \p File, if it's not present. |
| 240 | /// \p Header is either an URI that can be resolved to an #include path that |
| 241 | /// is suitable to be inserted or a literal string quoted with <> or "" that |
| 242 | /// can be #included directly. |
| 243 | Expected<tooling::Replacements> insertInclude(PathRef File, StringRef Code, |
| 244 | StringRef Header); |
| 245 | |
| Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 246 | /// Gets current document contents for \p File. Returns None if \p File is not |
| 247 | /// currently tracked. |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 248 | /// FIXME(ibiryukov): This function is here to allow offset-to-Position |
| 249 | /// conversions in outside code, maybe there's a way to get rid of it. |
| Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 250 | llvm::Optional<std::string> getDocument(PathRef File); |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 251 | |
| Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 252 | /// Only for testing purposes. |
| 253 | /// Waits until all requests to worker thread are finished and dumps AST for |
| 254 | /// \p File. \p File must be in the list of added documents. |
| Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 255 | void dumpAST(PathRef File, UniqueFunction<void(std::string)> Callback); |
| Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 256 | /// Called when an event occurs for a watched file in the workspace. |
| 257 | void onFileEvent(const DidChangeWatchedFilesParams &Params); |
| Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 258 | |
| Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 259 | /// Returns estimated memory usage for each of the currently open files. |
| 260 | /// The order of results is unspecified. |
| 261 | /// Overall memory usage of clangd may be significantly more than reported |
| 262 | /// here, as this metric does not account (at least) for: |
| 263 | /// - memory occupied by static and dynamic index, |
| 264 | /// - memory required for in-flight requests, |
| 265 | /// FIXME: those metrics might be useful too, we should add them. |
| 266 | std::vector<std::pair<Path, std::size_t>> getUsedBytesPerFile() const; |
| 267 | |
| Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 268 | // Blocks the main thread until the server is idle. Only for use in tests. |
| 269 | // Returns false if the timeout expires. |
| 270 | LLVM_NODISCARD bool |
| 271 | blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds = 10); |
| 272 | |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 273 | private: |
| Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 274 | /// FIXME: This stats several files to find a .clang-format file. I/O can be |
| 275 | /// slow. Think of a way to cache this. |
| 276 | llvm::Expected<tooling::Replacements> |
| 277 | formatCode(llvm::StringRef Code, PathRef File, |
| 278 | ArrayRef<tooling::Range> Ranges); |
| 279 | |
| Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 280 | void |
| Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 281 | scheduleReparseAndDiags(PathRef File, VersionedDraft Contents, |
| Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame^] | 282 | WantDiagnostics WD, |
| Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 283 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS); |
| Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 284 | |
| Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 285 | CompileArgsCache CompileArgs; |
| Ilya Biryukov | 103c951 | 2017-06-13 15:59:43 +0000 | [diff] [blame] | 286 | DiagnosticsConsumer &DiagConsumer; |
| 287 | FileSystemProvider &FSProvider; |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 288 | DraftStore DraftMgr; |
| Sam McCall | 0faecf0 | 2018-01-15 12:33:00 +0000 | [diff] [blame] | 289 | // The index used to look up symbols. This could be: |
| 290 | // - null (all index functionality is optional) |
| 291 | // - the dynamic index owned by ClangdServer (FileIdx) |
| 292 | // - the static index passed to the constructor |
| 293 | // - a merged view of a static and dynamic index (MergedIndex) |
| 294 | SymbolIndex *Index; |
| 295 | // If present, an up-to-date of symbols in open files. Read via Index. |
| Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 296 | std::unique_ptr<FileIndex> FileIdx; |
| Sam McCall | 0faecf0 | 2018-01-15 12:33:00 +0000 | [diff] [blame] | 297 | // If present, a merged view of FileIdx and an external index. Read via Index. |
| 298 | std::unique_ptr<SymbolIndex> MergedIndex; |
| Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 299 | // If set, this represents the workspace path. |
| 300 | llvm::Optional<std::string> RootPath; |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 301 | std::shared_ptr<PCHContainerOperations> PCHs; |
| Ilya Biryukov | 47f2202 | 2017-09-20 12:58:55 +0000 | [diff] [blame] | 302 | /// Used to serialize diagnostic callbacks. |
| 303 | /// FIXME(ibiryukov): get rid of an extra map and put all version counters |
| 304 | /// into CppFile. |
| 305 | std::mutex DiagnosticsMutex; |
| 306 | /// Maps from a filename to the latest version of reported diagnostics. |
| 307 | llvm::StringMap<DocVersion> ReportedDiagnosticVersions; |
| Ilya Biryukov | f4e95d7 | 2017-09-20 19:32:06 +0000 | [diff] [blame] | 308 | // WorkScheduler has to be the last member, because its destructor has to be |
| 309 | // called before all other members to stop the worker thread that references |
| Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 310 | // ClangdServer. |
| 311 | TUScheduler WorkScheduler; |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | } // namespace clangd |
| 315 | } // namespace clang |
| 316 | |
| 317 | #endif |