| 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" |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 14 | #include "ClangdUnitStore.h" |
| Eric Liu | b99d5e8 | 2017-12-14 21:22:03 +0000 | [diff] [blame] | 15 | #include "CodeComplete.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" |
| Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 20 | #include "index/FileIndex.h" |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 21 | #include "clang/Tooling/CompilationDatabase.h" |
| 22 | #include "clang/Tooling/Core/Replacement.h" |
| 23 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 24 | #include "llvm/ADT/Optional.h" |
| 25 | #include "llvm/ADT/StringRef.h" |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 26 | #include <condition_variable> |
| Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 27 | #include <functional> |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 28 | #include <mutex> |
| 29 | #include <string> |
| 30 | #include <thread> |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 31 | #include <type_traits> |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 32 | #include <utility> |
| 33 | |
| 34 | namespace clang { |
| 35 | class PCHContainerOperations; |
| 36 | |
| 37 | namespace clangd { |
| 38 | |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 39 | /// A tag supplied by the FileSytemProvider. |
| Ilya Biryukov | e36035b | 2017-06-13 08:24:48 +0000 | [diff] [blame] | 40 | typedef std::string VFSTag; |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 41 | |
| 42 | /// A value of an arbitrary type and VFSTag that was supplied by the |
| 43 | /// FileSystemProvider when this value was computed. |
| 44 | template <class T> class Tagged { |
| 45 | public: |
| Ilya Biryukov | e81b762 | 2017-10-05 22:15:15 +0000 | [diff] [blame] | 46 | // MSVC requires future<> arguments to be default-constructible. |
| 47 | Tagged() = default; |
| 48 | |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 49 | template <class U> |
| Ilya Biryukov | e36035b | 2017-06-13 08:24:48 +0000 | [diff] [blame] | 50 | Tagged(U &&Value, VFSTag Tag) |
| 51 | : Value(std::forward<U>(Value)), Tag(std::move(Tag)) {} |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 52 | |
| 53 | template <class U> |
| 54 | Tagged(const Tagged<U> &Other) : Value(Other.Value), Tag(Other.Tag) {} |
| 55 | |
| 56 | template <class U> |
| Ilya Biryukov | e36035b | 2017-06-13 08:24:48 +0000 | [diff] [blame] | 57 | Tagged(Tagged<U> &&Other) |
| 58 | : Value(std::move(Other.Value)), Tag(std::move(Other.Tag)) {} |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 59 | |
| Ilya Biryukov | e81b762 | 2017-10-05 22:15:15 +0000 | [diff] [blame] | 60 | T Value = T(); |
| 61 | VFSTag Tag = VFSTag(); |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | template <class T> |
| 65 | Tagged<typename std::decay<T>::type> make_tagged(T &&Value, VFSTag Tag) { |
| Ilya Biryukov | c22824a | 2017-08-09 12:55:13 +0000 | [diff] [blame] | 66 | return Tagged<typename std::decay<T>::type>(std::forward<T>(Value), Tag); |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 69 | class DiagnosticsConsumer { |
| 70 | public: |
| 71 | virtual ~DiagnosticsConsumer() = default; |
| 72 | |
| 73 | /// Called by ClangdServer when \p Diagnostics for \p File are ready. |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 74 | virtual void |
| Ilya Biryukov | 9555839 | 2018-01-10 17:59:27 +0000 | [diff] [blame] | 75 | onDiagnosticsReady(const Context &Ctx, PathRef File, |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 76 | Tagged<std::vector<DiagWithFixIts>> Diagnostics) = 0; |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 79 | class FileSystemProvider { |
| 80 | public: |
| 81 | virtual ~FileSystemProvider() = default; |
| Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 82 | /// Called by ClangdServer to obtain a vfs::FileSystem to be used for parsing. |
| 83 | /// Name of the file that will be parsed is passed in \p File. |
| 84 | /// |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 85 | /// \return A filesystem that will be used for all file accesses in clangd. |
| 86 | /// A Tag returned by this method will be propagated to all results of clangd |
| 87 | /// that will use this filesystem. |
| Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 88 | virtual Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> |
| 89 | getTaggedFileSystem(PathRef File) = 0; |
| Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | class RealFileSystemProvider : public FileSystemProvider { |
| 93 | public: |
| Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 94 | /// \return getRealFileSystem() tagged with default tag, i.e. VFSTag() |
| Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 95 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> |
| 96 | getTaggedFileSystem(PathRef File) override; |
| Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 99 | class ClangdServer; |
| 100 | |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 101 | /// Returns a number of a default async threads to use for ClangdScheduler. |
| 102 | /// Returned value is always >= 1 (i.e. will not cause requests to be processed |
| 103 | /// synchronously). |
| 104 | unsigned getDefaultAsyncThreadsCount(); |
| 105 | |
| 106 | /// Handles running WorkerRequests of ClangdServer on a number of worker |
| 107 | /// threads. |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 108 | class ClangdScheduler { |
| 109 | public: |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 110 | /// If \p AsyncThreadsCount is 0, requests added using addToFront and addToEnd |
| 111 | /// will be processed synchronously on the calling thread. |
| 112 | // Otherwise, \p AsyncThreadsCount threads will be created to schedule the |
| 113 | // requests. |
| 114 | ClangdScheduler(unsigned AsyncThreadsCount); |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 115 | ~ClangdScheduler(); |
| 116 | |
| Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 117 | /// Add a new request to run function \p F with args \p As to the start of the |
| 118 | /// queue. The request will be run on a separate thread. |
| 119 | template <class Func, class... Args> |
| 120 | void addToFront(Func &&F, Args &&... As) { |
| 121 | if (RunSynchronously) { |
| 122 | std::forward<Func>(F)(std::forward<Args>(As)...); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | { |
| 127 | std::lock_guard<std::mutex> Lock(Mutex); |
| Ilya Biryukov | 08e6ccb | 2017-10-09 16:26:26 +0000 | [diff] [blame] | 128 | RequestQueue.push_front( |
| 129 | BindWithForward(std::forward<Func>(F), std::forward<Args>(As)...)); |
| Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 130 | } |
| 131 | RequestCV.notify_one(); |
| 132 | } |
| 133 | |
| 134 | /// Add a new request to run function \p F with args \p As to the end of the |
| 135 | /// queue. The request will be run on a separate thread. |
| 136 | template <class Func, class... Args> void addToEnd(Func &&F, Args &&... As) { |
| 137 | if (RunSynchronously) { |
| 138 | std::forward<Func>(F)(std::forward<Args>(As)...); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | { |
| 143 | std::lock_guard<std::mutex> Lock(Mutex); |
| Ilya Biryukov | 08e6ccb | 2017-10-09 16:26:26 +0000 | [diff] [blame] | 144 | RequestQueue.push_back( |
| 145 | BindWithForward(std::forward<Func>(F), std::forward<Args>(As)...)); |
| Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 146 | } |
| 147 | RequestCV.notify_one(); |
| 148 | } |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 149 | |
| 150 | private: |
| 151 | bool RunSynchronously; |
| 152 | std::mutex Mutex; |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 153 | /// We run some tasks on separate threads(parsing, CppFile cleanup). |
| 154 | /// These threads looks into RequestQueue to find requests to handle and |
| 155 | /// terminate when Done is set to true. |
| 156 | std::vector<std::thread> Workers; |
| 157 | /// Setting Done to true will make the worker threads terminate. |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 158 | bool Done = false; |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 159 | /// A queue of requests. Elements of this vector are async computations (i.e. |
| 160 | /// results of calling std::async(std::launch::deferred, ...)). |
| Ilya Biryukov | 08e6ccb | 2017-10-09 16:26:26 +0000 | [diff] [blame] | 161 | std::deque<UniqueFunction<void()>> RequestQueue; |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 162 | /// Condition variable to wake up worker threads. |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 163 | std::condition_variable RequestCV; |
| 164 | }; |
| 165 | |
| 166 | /// 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] | 167 | /// various language features. |
| 168 | /// Currently supports async diagnostics, code completion, formatting and goto |
| 169 | /// definition. |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 170 | class ClangdServer { |
| 171 | public: |
| Ilya Biryukov | 75337e8 | 2017-08-22 09:16:46 +0000 | [diff] [blame] | 172 | /// Creates a new ClangdServer instance. |
| 173 | /// To process parsing requests asynchronously, ClangdServer will spawn \p |
| 174 | /// AsyncThreadsCount worker threads. However, if \p AsyncThreadsCount is 0, |
| 175 | /// all requests will be processed on the calling thread. |
| 176 | /// |
| 177 | /// ClangdServer uses \p FSProvider to get an instance of vfs::FileSystem for |
| 178 | /// each parsing request. Results of code completion and diagnostics also |
| 179 | /// include a tag, that \p FSProvider returns along with the vfs::FileSystem. |
| 180 | /// |
| 181 | /// The value of \p ResourceDir will be used to search for internal headers |
| 182 | /// (overriding defaults and -resource-dir compiler flag). If \p ResourceDir |
| 183 | /// is None, ClangdServer will call CompilerInvocation::GetResourcePath() to |
| 184 | /// obtain the standard resource directory. |
| 185 | /// |
| 186 | /// ClangdServer uses \p CDB to obtain compilation arguments for parsing. Note |
| 187 | /// that ClangdServer only obtains compilation arguments once for each newly |
| 188 | /// added file (i.e., when processing a first call to addDocument) and reuses |
| 189 | /// those arguments for subsequent reparses. However, ClangdServer will check |
| 190 | /// if compilation arguments changed on calls to forceReparse(). |
| 191 | /// |
| 192 | /// After each parsing request finishes, ClangdServer reports diagnostics to |
| 193 | /// \p DiagConsumer. Note that a callback to \p DiagConsumer happens on a |
| 194 | /// worker thread. Therefore, instances of \p DiagConsumer must properly |
| 195 | /// synchronize access to shared state. |
| Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 196 | /// |
| Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 197 | /// \p StorePreamblesInMemory defines whether the Preambles generated by |
| 198 | /// clangd are stored in-memory or on disk. |
| Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 199 | /// |
| 200 | /// If \p BuildDynamicSymbolIndex is true, ClangdServer builds a dynamic |
| 201 | /// in-memory index for symbols in all opened files and uses the index to |
| 202 | /// augment code completion results. |
| Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 203 | /// |
| 204 | /// If \p StaticIdx is set, ClangdServer uses the index for global code |
| 205 | /// completion. |
| Ilya Biryukov | 103c951 | 2017-06-13 15:59:43 +0000 | [diff] [blame] | 206 | ClangdServer(GlobalCompilationDatabase &CDB, |
| 207 | DiagnosticsConsumer &DiagConsumer, |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 208 | FileSystemProvider &FSProvider, unsigned AsyncThreadsCount, |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 209 | bool StorePreamblesInMemory, |
| Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 210 | bool BuildDynamicSymbolIndex = false, |
| Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 211 | SymbolIndex *StaticIdx = nullptr, |
| Ilya Biryukov | a46f7a9 | 2017-06-28 10:34:50 +0000 | [diff] [blame] | 212 | llvm::Optional<StringRef> ResourceDir = llvm::None); |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 213 | |
| Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 214 | /// Set the root path of the workspace. |
| 215 | void setRootPath(PathRef RootPath); |
| 216 | |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 217 | /// Add a \p File to the list of tracked C++ files or update the contents if |
| 218 | /// \p File is already tracked. Also schedules parsing of the AST for it on a |
| 219 | /// separate thread. When the parsing is complete, DiagConsumer passed in |
| 220 | /// constructor will receive onDiagnosticsReady callback. |
| Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 221 | /// \return A future that will become ready when the rebuild (including |
| 222 | /// diagnostics) is finished. |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 223 | std::future<Context> addDocument(Context Ctx, PathRef File, |
| 224 | StringRef Contents); |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 225 | /// Remove \p File from list of tracked files, schedule a request to free |
| 226 | /// resources associated with it. |
| Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 227 | /// \return A future that will become ready when the file is removed and all |
| 228 | /// associated resources are freed. |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 229 | std::future<Context> removeDocument(Context Ctx, PathRef File); |
| Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 230 | /// Force \p File to be reparsed using the latest contents. |
| Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 231 | /// Will also check if CompileCommand, provided by GlobalCompilationDatabase |
| 232 | /// for \p File has changed. If it has, will remove currently stored Preamble |
| 233 | /// and AST and rebuild them from scratch. |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 234 | std::future<Context> forceReparse(Context Ctx, PathRef File); |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 235 | |
| Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 236 | /// DEPRECATED. Please use a callback-based version, this API is deprecated |
| 237 | /// and will soon be removed. |
| 238 | /// |
| Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 239 | /// Run code completion for \p File at \p Pos. |
| 240 | /// |
| 241 | /// Request is processed asynchronously. You can use the returned future to |
| 242 | /// wait for the results of the async request. |
| 243 | /// |
| 244 | /// If \p OverridenContents is not None, they will used only for code |
| 245 | /// completion, i.e. no diagnostics update will be scheduled and a draft for |
| 246 | /// \p File will not be updated. If \p OverridenContents is None, contents of |
| 247 | /// the current draft for \p File will be used. If \p UsedFS is non-null, it |
| 248 | /// will be overwritten by vfs::FileSystem used for completion. |
| 249 | /// |
| 250 | /// This method should only be called for currently tracked files. However, it |
| 251 | /// is safe to call removeDocument for \p File after this method returns, even |
| 252 | /// while returned future is not yet ready. |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 253 | std::future<std::pair<Context, Tagged<CompletionList>>> |
| 254 | codeComplete(Context Ctx, PathRef File, Position Pos, |
| Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 255 | const clangd::CodeCompleteOptions &Opts, |
| Ilya Biryukov | ed99e4c | 2017-07-31 17:09:29 +0000 | [diff] [blame] | 256 | llvm::Optional<StringRef> OverridenContents = llvm::None, |
| 257 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS = nullptr); |
| Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 258 | |
| Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 259 | /// A version of `codeComplete` that runs \p Callback on the processing thread |
| 260 | /// when codeComplete results become available. |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 261 | void |
| 262 | codeComplete(Context Ctx, PathRef File, Position Pos, |
| 263 | const clangd::CodeCompleteOptions &Opts, |
| 264 | UniqueFunction<void(Context, Tagged<CompletionList>)> Callback, |
| 265 | llvm::Optional<StringRef> OverridenContents = llvm::None, |
| 266 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS = nullptr); |
| Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 267 | |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 268 | /// Provide signature help for \p File at \p Pos. If \p OverridenContents is |
| 269 | /// not None, they will used only for signature help, i.e. no diagnostics |
| 270 | /// update will be scheduled and a draft for \p File will not be updated. If |
| 271 | /// \p OverridenContents is None, contents of the current draft for \p File |
| 272 | /// will be used. If \p UsedFS is non-null, it will be overwritten by |
| 273 | /// vfs::FileSystem used for signature help. This method should only be called |
| 274 | /// for currently tracked files. |
| Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 275 | llvm::Expected<Tagged<SignatureHelp>> |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 276 | signatureHelp(const Context &Ctx, PathRef File, Position Pos, |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 277 | llvm::Optional<StringRef> OverridenContents = llvm::None, |
| 278 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS = nullptr); |
| 279 | |
| Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 280 | /// Get definition of symbol at a specified \p Line and \p Column in \p File. |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 281 | llvm::Expected<Tagged<std::vector<Location>>> |
| 282 | findDefinitions(const Context &Ctx, PathRef File, Position Pos); |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 283 | |
| Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 284 | /// Helper function that returns a path to the corresponding source file when |
| 285 | /// given a header file and vice versa. |
| 286 | llvm::Optional<Path> switchSourceHeader(PathRef Path); |
| 287 | |
| Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 288 | /// Get document highlights for a given position. |
| 289 | llvm::Expected<Tagged<std::vector<DocumentHighlight>>> |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 290 | findDocumentHighlights(const Context &Ctx, PathRef File, Position Pos); |
| Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 291 | |
| Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 292 | /// Run formatting for \p Rng inside \p File with content \p Code. |
| 293 | llvm::Expected<tooling::Replacements> formatRange(StringRef Code, |
| 294 | PathRef File, Range Rng); |
| 295 | |
| 296 | /// Run formatting for the whole \p File with content \p Code. |
| 297 | llvm::Expected<tooling::Replacements> formatFile(StringRef Code, |
| 298 | PathRef File); |
| 299 | |
| 300 | /// Run formatting after a character was typed at \p Pos in \p File with |
| 301 | /// content \p Code. |
| 302 | llvm::Expected<tooling::Replacements> |
| 303 | formatOnType(StringRef Code, PathRef File, Position Pos); |
| 304 | |
| Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 305 | /// Rename all occurrences of the symbol at the \p Pos in \p File to |
| 306 | /// \p NewName. |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 307 | Expected<std::vector<tooling::Replacement>> rename(const Context &Ctx, |
| 308 | PathRef File, Position Pos, |
| Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 309 | llvm::StringRef NewName); |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 310 | |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 311 | /// Gets current document contents for \p File. \p File must point to a |
| 312 | /// currently tracked file. |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 313 | /// FIXME(ibiryukov): This function is here to allow offset-to-Position |
| 314 | /// conversions in outside code, maybe there's a way to get rid of it. |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 315 | std::string getDocument(PathRef File); |
| 316 | |
| Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 317 | /// Only for testing purposes. |
| 318 | /// Waits until all requests to worker thread are finished and dumps AST for |
| 319 | /// \p File. \p File must be in the list of added documents. |
| 320 | std::string dumpAST(PathRef File); |
| Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 321 | /// Called when an event occurs for a watched file in the workspace. |
| 322 | void onFileEvent(const DidChangeWatchedFilesParams &Params); |
| Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 323 | |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 324 | private: |
| Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 325 | /// FIXME: This stats several files to find a .clang-format file. I/O can be |
| 326 | /// slow. Think of a way to cache this. |
| 327 | llvm::Expected<tooling::Replacements> |
| 328 | formatCode(llvm::StringRef Code, PathRef File, |
| 329 | ArrayRef<tooling::Range> Ranges); |
| 330 | |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 331 | std::future<Context> |
| 332 | scheduleReparseAndDiags(Context Ctx, PathRef File, VersionedDraft Contents, |
| Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 333 | std::shared_ptr<CppFile> Resources, |
| 334 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS); |
| 335 | |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 336 | std::future<Context> |
| 337 | scheduleCancelRebuild(Context Ctx, std::shared_ptr<CppFile> Resources); |
| Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 338 | |
| Ilya Biryukov | 103c951 | 2017-06-13 15:59:43 +0000 | [diff] [blame] | 339 | GlobalCompilationDatabase &CDB; |
| 340 | DiagnosticsConsumer &DiagConsumer; |
| 341 | FileSystemProvider &FSProvider; |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 342 | DraftStore DraftMgr; |
| Sam McCall | 0faecf0 | 2018-01-15 12:33:00 +0000 | [diff] [blame^] | 343 | // The index used to look up symbols. This could be: |
| 344 | // - null (all index functionality is optional) |
| 345 | // - the dynamic index owned by ClangdServer (FileIdx) |
| 346 | // - the static index passed to the constructor |
| 347 | // - a merged view of a static and dynamic index (MergedIndex) |
| 348 | SymbolIndex *Index; |
| 349 | // 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] | 350 | std::unique_ptr<FileIndex> FileIdx; |
| Sam McCall | 0faecf0 | 2018-01-15 12:33:00 +0000 | [diff] [blame^] | 351 | // If present, a merged view of FileIdx and an external index. Read via Index. |
| 352 | std::unique_ptr<SymbolIndex> MergedIndex; |
| Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 353 | CppFileCollection Units; |
| Ilya Biryukov | a46f7a9 | 2017-06-28 10:34:50 +0000 | [diff] [blame] | 354 | std::string ResourceDir; |
| Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 355 | // If set, this represents the workspace path. |
| 356 | llvm::Optional<std::string> RootPath; |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 357 | std::shared_ptr<PCHContainerOperations> PCHs; |
| Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 358 | bool StorePreamblesInMemory; |
| Ilya Biryukov | 47f2202 | 2017-09-20 12:58:55 +0000 | [diff] [blame] | 359 | /// Used to serialize diagnostic callbacks. |
| 360 | /// FIXME(ibiryukov): get rid of an extra map and put all version counters |
| 361 | /// into CppFile. |
| 362 | std::mutex DiagnosticsMutex; |
| 363 | /// Maps from a filename to the latest version of reported diagnostics. |
| 364 | llvm::StringMap<DocVersion> ReportedDiagnosticVersions; |
| Ilya Biryukov | f4e95d7 | 2017-09-20 19:32:06 +0000 | [diff] [blame] | 365 | // WorkScheduler has to be the last member, because its destructor has to be |
| 366 | // called before all other members to stop the worker thread that references |
| 367 | // ClangdServer |
| 368 | ClangdScheduler WorkScheduler; |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 369 | }; |
| 370 | |
| 371 | } // namespace clangd |
| 372 | } // namespace clang |
| 373 | |
| 374 | #endif |