blob: 1dd61739f4dd5470fbe5b34baf68145d2f2bbbe4 [file] [log] [blame]
Ilya Biryukov38d79772017-05-16 09:38:59 +00001//===--- 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 Liub99d5e82017-12-14 21:22:03 +000013#include "ClangdUnit.h"
Eric Liub99d5e82017-12-14 21:22:03 +000014#include "CodeComplete.h"
Ilya Biryukov929697b2018-01-25 14:19:21 +000015#include "CompileArgsCache.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000016#include "DraftStore.h"
Eric Liub99d5e82017-12-14 21:22:03 +000017#include "Function.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000018#include "GlobalCompilationDatabase.h"
Eric Liub99d5e82017-12-14 21:22:03 +000019#include "Protocol.h"
Ilya Biryukov75f1dd92018-01-31 08:51:16 +000020#include "TUScheduler.h"
Eric Liubfac8f72017-12-19 18:00:37 +000021#include "index/FileIndex.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000022#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 Biryukovf01af682017-05-23 13:42:59 +000027#include <functional>
Ilya Biryukov44ba9e02018-02-09 10:17:23 +000028#include <future>
Ilya Biryukov38d79772017-05-16 09:38:59 +000029#include <string>
Ilya Biryukov22602992017-05-30 15:11:02 +000030#include <type_traits>
Ilya Biryukov38d79772017-05-16 09:38:59 +000031#include <utility>
32
33namespace clang {
34class PCHContainerOperations;
35
36namespace clangd {
37
38class DiagnosticsConsumer {
39public:
40 virtual ~DiagnosticsConsumer() = default;
41
42 /// Called by ClangdServer when \p Diagnostics for \p File are ready.
Ilya Biryukov71028b82018-03-12 15:28:22 +000043 virtual void onDiagnosticsReady(PathRef File,
Sam McCalla7bb0cc2018-03-12 23:22:35 +000044 std::vector<Diag> Diagnostics) = 0;
Ilya Biryukov38d79772017-05-16 09:38:59 +000045};
46
Ilya Biryukov0f62ed22017-05-26 12:26:51 +000047class FileSystemProvider {
48public:
49 virtual ~FileSystemProvider() = default;
Ilya Biryukovaf0c04b2017-06-14 09:46:44 +000050 /// Called by ClangdServer to obtain a vfs::FileSystem to be used for parsing.
Sam McCalla7bb0cc2018-03-12 23:22:35 +000051 /// Context::current() will be the context passed to the clang entrypoint,
52 /// such as addDocument(), and will also be propagated to result callbacks.
53 /// Embedders may use this to isolate filesystem accesses.
54 virtual IntrusiveRefCntPtr<vfs::FileSystem> getFileSystem() = 0;
Ilya Biryukov0f62ed22017-05-26 12:26:51 +000055};
56
57class RealFileSystemProvider : public FileSystemProvider {
58public:
Sam McCalla7bb0cc2018-03-12 23:22:35 +000059 /// Returns getRealFileSystem().
60 IntrusiveRefCntPtr<vfs::FileSystem> getFileSystem() override;
Ilya Biryukov0f62ed22017-05-26 12:26:51 +000061};
62
Ilya Biryukov38d79772017-05-16 09:38:59 +000063/// Provides API to manage ASTs for a collection of C++ files and request
Ilya Biryukov75337e82017-08-22 09:16:46 +000064/// various language features.
65/// Currently supports async diagnostics, code completion, formatting and goto
66/// definition.
Ilya Biryukov38d79772017-05-16 09:38:59 +000067class ClangdServer {
68public:
Sam McCall7363a2f2018-03-05 17:28:54 +000069 struct Options {
70 /// To process requests asynchronously, ClangdServer spawns worker threads.
71 /// If 0, all requests are processed on the calling thread.
72 unsigned AsyncThreadsCount = getDefaultAsyncThreadsCount();
73
74 /// Cached preambles are potentially large. If false, store them on disk.
75 bool StorePreamblesInMemory = true;
76
77 /// If true, ClangdServer builds a dynamic in-memory index for symbols in
78 /// opened files and uses the index to augment code completion results.
79 bool BuildDynamicSymbolIndex = false;
80
81 /// If set, use this index to augment code completion results.
82 SymbolIndex *StaticIndex = nullptr;
83
84 /// The resource directory is used to find internal headers, overriding
85 /// defaults and -resource-dir compiler flag).
86 /// If None, ClangdServer calls CompilerInvocation::GetResourcePath() to
87 /// obtain the standard resource directory.
88 llvm::Optional<StringRef> ResourceDir = llvm::None;
89
90 /// Time to wait after a new file version before computing diagnostics.
91 std::chrono::steady_clock::duration UpdateDebounce =
92 std::chrono::milliseconds(500);
93 };
94 // Sensible default options for use in tests.
95 // Features like indexing must be enabled if desired.
96 static Options optsForTest();
97
Ilya Biryukov75337e82017-08-22 09:16:46 +000098 /// Creates a new ClangdServer instance.
Ilya Biryukov75337e82017-08-22 09:16:46 +000099 ///
100 /// ClangdServer uses \p CDB to obtain compilation arguments for parsing. Note
101 /// that ClangdServer only obtains compilation arguments once for each newly
102 /// added file (i.e., when processing a first call to addDocument) and reuses
103 /// those arguments for subsequent reparses. However, ClangdServer will check
104 /// if compilation arguments changed on calls to forceReparse().
105 ///
106 /// After each parsing request finishes, ClangdServer reports diagnostics to
107 /// \p DiagConsumer. Note that a callback to \p DiagConsumer happens on a
108 /// worker thread. Therefore, instances of \p DiagConsumer must properly
109 /// synchronize access to shared state.
Sam McCall7363a2f2018-03-05 17:28:54 +0000110 ClangdServer(GlobalCompilationDatabase &CDB, FileSystemProvider &FSProvider,
111 DiagnosticsConsumer &DiagConsumer, const Options &Opts);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000112
Marc-Andre Laperle37de9712017-09-27 15:31:17 +0000113 /// Set the root path of the workspace.
114 void setRootPath(PathRef RootPath);
115
Ilya Biryukov38d79772017-05-16 09:38:59 +0000116 /// Add a \p File to the list of tracked C++ files or update the contents if
117 /// \p File is already tracked. Also schedules parsing of the AST for it on a
118 /// separate thread. When the parsing is complete, DiagConsumer passed in
119 /// constructor will receive onDiagnosticsReady callback.
Ilya Biryukovbec5df22018-03-14 17:08:41 +0000120 /// When \p SkipCache is true, compile commands will always be requested from
121 /// compilation database even if they were cached in previous invocations.
Sam McCall568e17f2018-02-22 13:11:12 +0000122 void addDocument(PathRef File, StringRef Contents,
Ilya Biryukovbec5df22018-03-14 17:08:41 +0000123 WantDiagnostics WD = WantDiagnostics::Auto,
124 bool SkipCache = false);
Sam McCalld1a7a372018-01-31 13:40:48 +0000125
Ilya Biryukov38d79772017-05-16 09:38:59 +0000126 /// Remove \p File from list of tracked files, schedule a request to free
127 /// resources associated with it.
Ilya Biryukov7e5ee262018-02-08 07:37:35 +0000128 void removeDocument(PathRef File);
Sam McCalld1a7a372018-01-31 13:40:48 +0000129
Simon Marchi5178f922018-02-22 14:00:39 +0000130 /// Calls forceReparse() on all currently opened files.
131 /// As a result, this method may be very expensive.
132 /// This method is normally called when the compilation database is changed.
Ilya Biryukovbec5df22018-03-14 17:08:41 +0000133 /// FIXME: this method must be moved to ClangdLSPServer along with DraftMgr.
Simon Marchi5178f922018-02-22 14:00:39 +0000134 void reparseOpenedFiles();
135
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000136 /// Run code completion for \p File at \p Pos.
Sam McCalld1a7a372018-01-31 13:40:48 +0000137 /// Request is processed asynchronously.
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000138 ///
Sam McCalldb3ea4c2018-02-27 17:15:50 +0000139 /// The current draft for \p File will be used. If \p UsedFS is non-null, it
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000140 /// will be overwritten by vfs::FileSystem used for completion.
141 ///
142 /// This method should only be called for currently tracked files. However, it
143 /// is safe to call removeDocument for \p File after this method returns, even
144 /// while returned future is not yet ready.
Ilya Biryukov90bbcfd2017-10-25 09:35:10 +0000145 /// A version of `codeComplete` that runs \p Callback on the processing thread
146 /// when codeComplete results become available.
Sam McCalld1a7a372018-01-31 13:40:48 +0000147 void codeComplete(PathRef File, Position Pos,
148 const clangd::CodeCompleteOptions &Opts,
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000149 Callback<CompletionList> CB);
Sam McCalld1a7a372018-01-31 13:40:48 +0000150
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000151 /// Provide signature help for \p File at \p Pos. If \p OverridenContents is
152 /// not None, they will used only for signature help, i.e. no diagnostics
153 /// update will be scheduled and a draft for \p File will not be updated. If
Sam McCalldb3ea4c2018-02-27 17:15:50 +0000154 /// If \p UsedFS is non-null, it will be overwritten by vfs::FileSystem used
155 /// for signature help. This method should only be called for tracked files.
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000156 void signatureHelp(PathRef File, Position Pos, Callback<SignatureHelp> CB);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000157
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000158 /// Get definition of symbol at a specified \p Line and \p Column in \p File.
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000159 void findDefinitions(PathRef File, Position Pos,
160 Callback<std::vector<Location>> CB);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000161
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000162 /// Helper function that returns a path to the corresponding source file when
163 /// given a header file and vice versa.
164 llvm::Optional<Path> switchSourceHeader(PathRef Path);
165
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000166 /// Get document highlights for a given position.
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000167 void findDocumentHighlights(PathRef File, Position Pos,
168 Callback<std::vector<DocumentHighlight>> CB);
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000169
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000170 /// Get code hover for a given position.
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000171 void findHover(PathRef File, Position Pos, Callback<Hover> CB);
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000172
Raoul Wols212bcf82017-12-12 20:25:06 +0000173 /// Run formatting for \p Rng inside \p File with content \p Code.
174 llvm::Expected<tooling::Replacements> formatRange(StringRef Code,
175 PathRef File, Range Rng);
176
177 /// Run formatting for the whole \p File with content \p Code.
178 llvm::Expected<tooling::Replacements> formatFile(StringRef Code,
179 PathRef File);
180
181 /// Run formatting after a character was typed at \p Pos in \p File with
182 /// content \p Code.
183 llvm::Expected<tooling::Replacements>
184 formatOnType(StringRef Code, PathRef File, Position Pos);
185
Haojian Wu345099c2017-11-09 11:30:04 +0000186 /// Rename all occurrences of the symbol at the \p Pos in \p File to
187 /// \p NewName.
Ilya Biryukov2c5e8e82018-02-15 13:15:47 +0000188 void rename(PathRef File, Position Pos, llvm::StringRef NewName,
Sam McCalla7bb0cc2018-03-12 23:22:35 +0000189 Callback<std::vector<tooling::Replacement>> CB);
Ilya Biryukovafb55542017-05-16 14:40:30 +0000190
Eric Liu6c8e8582018-02-26 08:32:13 +0000191 /// Inserts a new #include into \p File, if it's not present in \p Code.
192 ///
193 /// \p DeclaringHeader The original header corresponding to this insertion
194 /// e.g. the header that declared a symbol. This can be either a URI or a
195 /// literal string quoted with <> or "" that can be #included directly.
196 /// \p InsertedHeader The preferred header to be inserted. This may be
197 /// different from \p DeclaringHeader as a header file can have a different
198 /// canonical include. This can be either a URI or a literal string quoted
199 /// with <> or "" that can be #included directly.
200 ///
201 /// Both OriginalHeader and InsertedHeader will be considered to determine
202 /// whether an include needs to be added.
Eric Liuc5105f92018-02-16 14:15:55 +0000203 Expected<tooling::Replacements> insertInclude(PathRef File, StringRef Code,
Eric Liu6c8e8582018-02-26 08:32:13 +0000204 StringRef DeclaringHeader,
205 StringRef InsertedHeader);
Eric Liuc5105f92018-02-16 14:15:55 +0000206
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000207 /// Gets current document contents for \p File. Returns None if \p File is not
208 /// currently tracked.
Ilya Biryukovafb55542017-05-16 14:40:30 +0000209 /// FIXME(ibiryukov): This function is here to allow offset-to-Position
210 /// conversions in outside code, maybe there's a way to get rid of it.
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000211 llvm::Optional<std::string> getDocument(PathRef File);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000212
Ilya Biryukovf01af682017-05-23 13:42:59 +0000213 /// Only for testing purposes.
214 /// Waits until all requests to worker thread are finished and dumps AST for
215 /// \p File. \p File must be in the list of added documents.
Ilya Biryukov2c5e8e82018-02-15 13:15:47 +0000216 void dumpAST(PathRef File, UniqueFunction<void(std::string)> Callback);
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000217 /// Called when an event occurs for a watched file in the workspace.
218 void onFileEvent(const DidChangeWatchedFilesParams &Params);
Ilya Biryukovf01af682017-05-23 13:42:59 +0000219
Ilya Biryukovdf842342018-01-25 14:32:21 +0000220 /// Returns estimated memory usage for each of the currently open files.
221 /// The order of results is unspecified.
222 /// Overall memory usage of clangd may be significantly more than reported
223 /// here, as this metric does not account (at least) for:
224 /// - memory occupied by static and dynamic index,
225 /// - memory required for in-flight requests,
226 /// FIXME: those metrics might be useful too, we should add them.
227 std::vector<std::pair<Path, std::size_t>> getUsedBytesPerFile() const;
228
Sam McCall0bb24cd2018-02-13 08:59:23 +0000229 // Blocks the main thread until the server is idle. Only for use in tests.
230 // Returns false if the timeout expires.
231 LLVM_NODISCARD bool
232 blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds = 10);
233
Ilya Biryukov38d79772017-05-16 09:38:59 +0000234private:
Raoul Wols212bcf82017-12-12 20:25:06 +0000235 /// FIXME: This stats several files to find a .clang-format file. I/O can be
236 /// slow. Think of a way to cache this.
237 llvm::Expected<tooling::Replacements>
238 formatCode(llvm::StringRef Code, PathRef File,
239 ArrayRef<tooling::Range> Ranges);
240
Ilya Biryukovbec5df22018-03-14 17:08:41 +0000241 void consumeDiagnostics(PathRef File, DocVersion Version,
242 std::vector<Diag> Diags);
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000243
Ilya Biryukov929697b2018-01-25 14:19:21 +0000244 CompileArgsCache CompileArgs;
Ilya Biryukov103c9512017-06-13 15:59:43 +0000245 DiagnosticsConsumer &DiagConsumer;
246 FileSystemProvider &FSProvider;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000247 DraftStore DraftMgr;
Sam McCall0faecf02018-01-15 12:33:00 +0000248 // The index used to look up symbols. This could be:
249 // - null (all index functionality is optional)
250 // - the dynamic index owned by ClangdServer (FileIdx)
251 // - the static index passed to the constructor
252 // - a merged view of a static and dynamic index (MergedIndex)
253 SymbolIndex *Index;
254 // If present, an up-to-date of symbols in open files. Read via Index.
Eric Liubfac8f72017-12-19 18:00:37 +0000255 std::unique_ptr<FileIndex> FileIdx;
Sam McCall0faecf02018-01-15 12:33:00 +0000256 // If present, a merged view of FileIdx and an external index. Read via Index.
257 std::unique_ptr<SymbolIndex> MergedIndex;
Marc-Andre Laperle37de9712017-09-27 15:31:17 +0000258 // If set, this represents the workspace path.
259 llvm::Optional<std::string> RootPath;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000260 std::shared_ptr<PCHContainerOperations> PCHs;
Ilya Biryukov47f22022017-09-20 12:58:55 +0000261 /// Used to serialize diagnostic callbacks.
262 /// FIXME(ibiryukov): get rid of an extra map and put all version counters
263 /// into CppFile.
264 std::mutex DiagnosticsMutex;
265 /// Maps from a filename to the latest version of reported diagnostics.
266 llvm::StringMap<DocVersion> ReportedDiagnosticVersions;
Ilya Biryukovf4e95d72017-09-20 19:32:06 +0000267 // WorkScheduler has to be the last member, because its destructor has to be
268 // called before all other members to stop the worker thread that references
Ilya Biryukov75f1dd92018-01-31 08:51:16 +0000269 // ClangdServer.
270 TUScheduler WorkScheduler;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000271};
272
273} // namespace clangd
274} // namespace clang
275
276#endif