clang-tools  9.0.0
ClangdServer.h
Go to the documentation of this file.
1 //===--- ClangdServer.h - Main clangd server code ----------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDSERVER_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDSERVER_H
11 
12 #include "../clang-tidy/ClangTidyOptions.h"
13 #include "Cancellation.h"
14 #include "ClangdUnit.h"
15 #include "CodeComplete.h"
16 #include "FSProvider.h"
17 #include "FormattedString.h"
18 #include "Function.h"
20 #include "Protocol.h"
21 #include "SemanticHighlighting.h"
22 #include "TUScheduler.h"
23 #include "XRefs.h"
24 #include "index/Background.h"
25 #include "index/FileIndex.h"
26 #include "index/Index.h"
27 #include "refactor/Tweak.h"
28 #include "clang/Tooling/CompilationDatabase.h"
29 #include "clang/Tooling/Core/Replacement.h"
30 #include "llvm/ADT/FunctionExtras.h"
31 #include "llvm/ADT/IntrusiveRefCntPtr.h"
32 #include "llvm/ADT/Optional.h"
33 #include "llvm/ADT/StringRef.h"
34 #include <functional>
35 #include <future>
36 #include <string>
37 #include <type_traits>
38 #include <utility>
39 
40 namespace clang {
41 namespace clangd {
42 
43 // FIXME: find a better name.
45 public:
46  virtual ~DiagnosticsConsumer() = default;
47 
48  /// Called by ClangdServer when \p Diagnostics for \p File are ready.
49  virtual void onDiagnosticsReady(PathRef File,
50  std::vector<Diag> Diagnostics) = 0;
51  /// Called whenever the file status is updated.
52  virtual void onFileUpdated(PathRef File, const TUStatus &Status){};
53 
54  /// Called by ClangdServer when some \p Highlightings for \p File are ready.
55  virtual void
57  std::vector<HighlightingToken> Highlightings) {}
58 };
59 
60 /// When set, used by ClangdServer to get clang-tidy options for each particular
61 /// file. Must be thread-safe. We use this instead of ClangTidyOptionsProvider
62 /// to allow reading tidy configs from the VFS used for parsing.
64  llvm::vfs::FileSystem &, llvm::StringRef /*File*/)>;
65 
66 /// Manages a collection of source files and derived data (ASTs, indexes),
67 /// and provides language-aware features such as code completion.
68 ///
69 /// The primary client is ClangdLSPServer which exposes these features via
70 /// the Language Server protocol. ClangdServer may also be embedded directly,
71 /// though its API is not stable over time.
72 ///
73 /// ClangdServer should be used from a single thread. Many potentially-slow
74 /// operations have asynchronous APIs and deliver their results on another
75 /// thread.
76 /// Such operations support cancellation: if the caller sets up a cancelable
77 /// context, many operations will notice cancellation and fail early.
78 /// (ClangdLSPServer uses this to implement $/cancelRequest).
79 class ClangdServer {
80 public:
81  struct Options {
82  /// To process requests asynchronously, ClangdServer spawns worker threads.
83  /// If this is zero, no threads are spawned. All work is done on the calling
84  /// thread, and callbacks are invoked before "async" functions return.
85  unsigned AsyncThreadsCount = getDefaultAsyncThreadsCount();
86 
87  /// AST caching policy. The default is to keep up to 3 ASTs in memory.
89 
90  /// Cached preambles are potentially large. If false, store them on disk.
91  bool StorePreamblesInMemory = true;
92 
93  /// If true, ClangdServer builds a dynamic in-memory index for symbols in
94  /// opened files and uses the index to augment code completion results.
95  bool BuildDynamicSymbolIndex = false;
96  /// Use a heavier and faster in-memory index implementation.
97  bool HeavyweightDynamicSymbolIndex = true;
98  /// If true, ClangdServer automatically indexes files in the current project
99  /// on background threads. The index is stored in the project root.
100  bool BackgroundIndex = false;
101 
102  /// If set, use this index to augment code completion results.
103  SymbolIndex *StaticIndex = nullptr;
104 
105  /// If set, enable clang-tidy in clangd and use to it get clang-tidy
106  /// configurations for a particular file.
107  /// Clangd supports only a small subset of ClangTidyOptions, these options
108  /// (Checks, CheckOptions) are about which clang-tidy checks will be
109  /// enabled.
111 
112  /// Clangd's workspace root. Relevant for "workspace" operations not bound
113  /// to a particular file.
114  /// FIXME: If not set, should use the current working directory.
115  llvm::Optional<std::string> WorkspaceRoot;
116 
117  /// The resource directory is used to find internal headers, overriding
118  /// defaults and -resource-dir compiler flag).
119  /// If None, ClangdServer calls CompilerInvocation::GetResourcePath() to
120  /// obtain the standard resource directory.
121  llvm::Optional<std::string> ResourceDir = llvm::None;
122 
123  /// Time to wait after a new file version before computing diagnostics.
124  std::chrono::steady_clock::duration UpdateDebounce =
125  std::chrono::milliseconds(500);
126 
127  bool SuggestMissingIncludes = false;
128 
129  /// Clangd will execute compiler drivers matching one of these globs to
130  /// fetch system include path.
131  std::vector<std::string> QueryDriverGlobs;
132 
133  /// Enable semantic highlighting features.
134  bool SemanticHighlighting = false;
135 
136  /// Returns true if the tweak should be enabled.
137  std::function<bool(const Tweak &)> TweakFilter = [](const Tweak &T) {
138  return !T.hidden(); // only enable non-hidden tweaks.
139  };
140  };
141  // Sensible default options for use in tests.
142  // Features like indexing must be enabled if desired.
143  static Options optsForTest();
144 
145  /// Creates a new ClangdServer instance.
146  ///
147  /// ClangdServer uses \p CDB to obtain compilation arguments for parsing. Note
148  /// that ClangdServer only obtains compilation arguments once for each newly
149  /// added file (i.e., when processing a first call to addDocument) and reuses
150  /// those arguments for subsequent reparses. However, ClangdServer will check
151  /// if compilation arguments changed on calls to forceReparse().
152  ///
153  /// After each parsing request finishes, ClangdServer reports diagnostics to
154  /// \p DiagConsumer. Note that a callback to \p DiagConsumer happens on a
155  /// worker thread. Therefore, instances of \p DiagConsumer must properly
156  /// synchronize access to shared state.
160 
161  /// Add a \p File to the list of tracked C++ files or update the contents if
162  /// \p File is already tracked. Also schedules parsing of the AST for it on a
163  /// separate thread. When the parsing is complete, DiagConsumer passed in
164  /// constructor will receive onDiagnosticsReady callback.
165  void addDocument(PathRef File, StringRef Contents,
167 
168  /// Get the contents of \p File, which should have been added.
169  llvm::StringRef getDocument(PathRef File) const;
170 
171  /// Remove \p File from list of tracked files, schedule a request to free
172  /// resources associated with it. Pending diagnostics for closed files may not
173  /// be delivered, even if requested with WantDiags::Auto or WantDiags::Yes.
174  void removeDocument(PathRef File);
175 
176  /// Run code completion for \p File at \p Pos.
177  /// Request is processed asynchronously.
178  ///
179  /// This method should only be called for currently tracked files. However, it
180  /// is safe to call removeDocument for \p File after this method returns, even
181  /// while returned future is not yet ready.
182  /// A version of `codeComplete` that runs \p Callback on the processing thread
183  /// when codeComplete results become available.
184  void codeComplete(PathRef File, Position Pos,
185  const clangd::CodeCompleteOptions &Opts,
187 
188  /// Provide signature help for \p File at \p Pos. This method should only be
189  /// called for tracked files.
191 
192  /// Find declaration/definition locations of symbol at a specified position.
193  void locateSymbolAt(PathRef File, Position Pos,
194  Callback<std::vector<LocatedSymbol>> CB);
195 
196  /// Helper function that returns a path to the corresponding source file when
197  /// given a header file and vice versa.
198  llvm::Optional<Path> switchSourceHeader(PathRef Path);
199 
200  /// Get document highlights for a given position.
201  void findDocumentHighlights(PathRef File, Position Pos,
202  Callback<std::vector<DocumentHighlight>> CB);
203 
204  /// Get code hover for a given position.
205  void findHover(PathRef File, Position Pos,
206  Callback<llvm::Optional<HoverInfo>> CB);
207 
208  /// Get information about type hierarchy for a given position.
209  void typeHierarchy(PathRef File, Position Pos, int Resolve,
210  TypeHierarchyDirection Direction,
211  Callback<llvm::Optional<TypeHierarchyItem>> CB);
212 
213  /// Resolve type hierarchy item in the given direction.
214  void resolveTypeHierarchy(TypeHierarchyItem Item, int Resolve,
215  TypeHierarchyDirection Direction,
216  Callback<llvm::Optional<TypeHierarchyItem>> CB);
217 
218  /// Retrieve the top symbols from the workspace matching a query.
219  void workspaceSymbols(StringRef Query, int Limit,
220  Callback<std::vector<SymbolInformation>> CB);
221 
222  /// Retrieve the symbols within the specified file.
223  void documentSymbols(StringRef File,
224  Callback<std::vector<DocumentSymbol>> CB);
225 
226  /// Retrieve locations for symbol references.
227  void findReferences(PathRef File, Position Pos, uint32_t Limit,
228  Callback<std::vector<Location>> CB);
229 
230  /// Run formatting for \p Rng inside \p File with content \p Code.
231  llvm::Expected<tooling::Replacements> formatRange(StringRef Code,
232  PathRef File, Range Rng);
233 
234  /// Run formatting for the whole \p File with content \p Code.
235  llvm::Expected<tooling::Replacements> formatFile(StringRef Code,
236  PathRef File);
237 
238  /// Run formatting after \p TriggerText was typed at \p Pos in \p File with
239  /// content \p Code.
240  llvm::Expected<std::vector<TextEdit>> formatOnType(StringRef Code,
241  PathRef File, Position Pos,
242  StringRef TriggerText);
243 
244  /// Rename all occurrences of the symbol at the \p Pos in \p File to
245  /// \p NewName.
246  /// If WantFormat is false, the final TextEdit will be not formatted,
247  /// embedders could use this method to get all occurrences of the symbol (e.g.
248  /// highlighting them in prepare stage).
249  void rename(PathRef File, Position Pos, llvm::StringRef NewName,
250  bool WantFormat, Callback<std::vector<TextEdit>> CB);
251 
252  struct TweakRef {
253  std::string ID; /// ID to pass for applyTweak.
254  std::string Title; /// A single-line message to show in the UI.
256  };
257  /// Enumerate the code tweaks available to the user at a specified point.
258  void enumerateTweaks(PathRef File, Range Sel,
259  Callback<std::vector<TweakRef>> CB);
260 
261  /// Apply the code tweak with a specified \p ID.
262  void applyTweak(PathRef File, Range Sel, StringRef ID,
264 
265  /// Only for testing purposes.
266  /// Waits until all requests to worker thread are finished and dumps AST for
267  /// \p File. \p File must be in the list of added documents.
268  void dumpAST(PathRef File, llvm::unique_function<void(std::string)> Callback);
269  /// Called when an event occurs for a watched file in the workspace.
270  void onFileEvent(const DidChangeWatchedFilesParams &Params);
271 
272  /// Get symbol info for given position.
273  /// Clangd extension - not part of official LSP.
274  void symbolInfo(PathRef File, Position Pos,
275  Callback<std::vector<SymbolDetails>> CB);
276 
277  /// Returns estimated memory usage for each of the currently open files.
278  /// The order of results is unspecified.
279  /// Overall memory usage of clangd may be significantly more than reported
280  /// here, as this metric does not account (at least) for:
281  /// - memory occupied by static and dynamic index,
282  /// - memory required for in-flight requests,
283  /// FIXME: those metrics might be useful too, we should add them.
284  std::vector<std::pair<Path, std::size_t>> getUsedBytesPerFile() const;
285 
286  // Blocks the main thread until the server is idle. Only for use in tests.
287  // Returns false if the timeout expires.
288  LLVM_NODISCARD bool
289  blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds = 10);
290 
291 private:
292  /// FIXME: This stats several files to find a .clang-format file. I/O can be
293  /// slow. Think of a way to cache this.
294  llvm::Expected<tooling::Replacements>
295  formatCode(llvm::StringRef Code, PathRef File,
296  ArrayRef<tooling::Range> Ranges);
297 
299 
300  Path ResourceDir;
301  // The index used to look up symbols. This could be:
302  // - null (all index functionality is optional)
303  // - the dynamic index owned by ClangdServer (DynamicIdx)
304  // - the static index passed to the constructor
305  // - a merged view of a static and dynamic index (MergedIndex)
306  const SymbolIndex *Index = nullptr;
307  // If present, an index of symbols in open files. Read via *Index.
308  std::unique_ptr<FileIndex> DynamicIdx;
309  // If present, the new "auto-index" maintained in background threads.
310  std::unique_ptr<BackgroundIndex> BackgroundIdx;
311  // Storage for merged views of the various indexes.
312  std::vector<std::unique_ptr<SymbolIndex>> MergedIdx;
313 
314  // When set, provides clang-tidy options for a specific file.
315  ClangTidyOptionsBuilder GetClangTidyOptions;
316 
317  // If this is true, suggest include insertion fixes for diagnostic errors that
318  // can be caused by missing includes (e.g. member access in incomplete type).
319  bool SuggestMissingIncludes = false;
320  bool EnableHiddenFeatures = false;
321 
322  std::function<bool(const Tweak &)> TweakFilter;
323 
324  // GUARDED_BY(CachedCompletionFuzzyFindRequestMutex)
325  llvm::StringMap<llvm::Optional<FuzzyFindRequest>>
326  CachedCompletionFuzzyFindRequestByFile;
327  mutable std::mutex CachedCompletionFuzzyFindRequestMutex;
328 
329  llvm::Optional<std::string> WorkspaceRoot;
330  // WorkScheduler has to be the last member, because its destructor has to be
331  // called before all other members to stop the worker thread that references
332  // ClangdServer.
333  TUScheduler WorkScheduler;
334 };
335 
336 } // namespace clangd
337 } // namespace clang
338 
339 #endif
int Limit
WantDiagnostics
Determines whether diagnostics should be generated for a file snapshot.
Definition: TUScheduler.h:43
llvm::StringRef Contents
void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS)
For testing/debugging purposes.
Definition: ClangdUnit.cpp:282
void resolveTypeHierarchy(TypeHierarchyItem &Item, int ResolveLevels, TypeHierarchyDirection Direction, const SymbolIndex *Index)
Definition: XRefs.cpp:1255
Intent
Output of a tweak.
Definition: Tweak.h:61
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:28
Contains options for clang-tidy.
Tweak::Intent Intent
A single-line message to show in the UI.
Definition: ClangdServer.h:255
Documents should not be synced at all.
ASTRetentionPolicy RetentionPolicy
AST caching policy. The default is to keep up to 3 ASTs in memory.
Definition: ClangdServer.h:88
MockFSProvider FSProvider
Configuration of the AST retention policy.
Definition: TUScheduler.h:53
std::string Title
ID to pass for applyTweak.
Definition: ClangdServer.h:254
SignatureHelp signatureHelp(PathRef FileName, const tooling::CompileCommand &Command, const PreambleData *Preamble, llvm::StringRef Contents, Position Pos, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, const SymbolIndex *Index)
Provides compilation arguments used for parsing C and C++ files.
std::string Path
A typedef to represent a file path.
Definition: Path.h:20
virtual void onDiagnosticsReady(PathRef File, std::vector< Diag > Diagnostics)=0
Called by ClangdServer when Diagnostics for File are ready.
virtual ~DiagnosticsConsumer()=default
std::function< tidy::ClangTidyOptions(llvm::vfs::FileSystem &, llvm::StringRef)> ClangTidyOptionsBuilder
When set, used by ClangdServer to get clang-tidy options for each particular file.
Definition: ClangdServer.h:64
std::vector< DocumentHighlight > findDocumentHighlights(ParsedAST &AST, Position Pos)
Returns highlights for all usages of a symbol at Pos.
Definition: XRefs.cpp:436
llvm::Optional< std::string > WorkspaceRoot
Clangd&#39;s workspace root.
Definition: ClangdServer.h:115
CodeCompleteResult codeComplete(PathRef FileName, const tooling::CompileCommand &Command, const PreambleData *Preamble, llvm::StringRef Contents, Position Pos, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, CodeCompleteOptions Opts, SpeculativeFuzzyFind *SpecFuzzyFind)
unsigned getDefaultAsyncThreadsCount()
Returns a number of a default async threads to use for TUScheduler.
std::vector< Location > findReferences(ParsedAST &AST, Position Pos, uint32_t Limit, const SymbolIndex *Index)
Returns reference locations of the symbol at a specified Pos.
Definition: XRefs.cpp:935
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::vector< std::string > QueryDriverGlobs
Clangd will execute compiler drivers matching one of these globs to fetch system include path...
Definition: ClangdServer.h:131
An interface base for small context-sensitive refactoring actions.
Definition: Tweak.h:39
IgnoreDiagnostics DiagConsumer
Manages a collection of source files and derived data (ASTs, indexes), and provides language-aware fe...
Definition: ClangdServer.h:79
std::vector< LocatedSymbol > locateSymbolAt(ParsedAST &AST, Position Pos, const SymbolIndex *Index)
Get definition of symbol at a specified Pos.
Definition: XRefs.cpp:261
ClangTidyOptionsBuilder GetClangTidyOptions
If set, enable clang-tidy in clangd and use to it get clang-tidy configurations for a particular file...
Definition: ClangdServer.h:110
Handles running tasks for ClangdServer and managing the resources (e.g., preambles and ASTs) for open...
Definition: TUScheduler.h:127
Diagnostics must not be generated for this snapshot.
virtual void onHighlightingsReady(PathRef File, std::vector< HighlightingToken > Highlightings)
Called by ClangdServer when some Highlightings for File are ready.
Definition: ClangdServer.h:56
virtual void onFileUpdated(PathRef File, const TUStatus &Status)
Called whenever the file status is updated.
Definition: ClangdServer.h:52
const SymbolIndex * Index
Definition: Dexp.cpp:84