clang-tools  10.0.0git
ClangdServer.cpp
Go to the documentation of this file.
1 //===--- ClangdServer.cpp - 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 #include "ClangdServer.h"
10 #include "CodeComplete.h"
11 #include "FindSymbols.h"
12 #include "Format.h"
13 #include "FormattedString.h"
14 #include "HeaderSourceSwitch.h"
15 #include "Headers.h"
16 #include "Logger.h"
17 #include "ParsedAST.h"
18 #include "Preamble.h"
19 #include "Protocol.h"
20 #include "SemanticHighlighting.h"
21 #include "SemanticSelection.h"
22 #include "SourceCode.h"
23 #include "TUScheduler.h"
24 #include "Trace.h"
25 #include "XRefs.h"
27 #include "index/FileIndex.h"
28 #include "index/Merge.h"
29 #include "refactor/Rename.h"
30 #include "refactor/Tweak.h"
31 #include "clang/Format/Format.h"
32 #include "clang/Frontend/CompilerInstance.h"
33 #include "clang/Frontend/CompilerInvocation.h"
34 #include "clang/Lex/Preprocessor.h"
35 #include "clang/Tooling/CompilationDatabase.h"
36 #include "clang/Tooling/Core/Replacement.h"
37 #include "llvm/ADT/ArrayRef.h"
38 #include "llvm/ADT/Optional.h"
39 #include "llvm/ADT/STLExtras.h"
40 #include "llvm/ADT/ScopeExit.h"
41 #include "llvm/ADT/StringRef.h"
42 #include "llvm/Support/Errc.h"
43 #include "llvm/Support/Error.h"
44 #include "llvm/Support/FileSystem.h"
45 #include "llvm/Support/Path.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include <algorithm>
48 #include <future>
49 #include <memory>
50 #include <mutex>
51 #include <type_traits>
52 
53 namespace clang {
54 namespace clangd {
55 namespace {
56 
57 // Update the FileIndex with new ASTs and plumb the diagnostics responses.
58 struct UpdateIndexCallbacks : public ParsingCallbacks {
59  UpdateIndexCallbacks(FileIndex *FIndex, DiagnosticsConsumer &DiagConsumer,
60  bool SemanticHighlighting)
61  : FIndex(FIndex), DiagConsumer(DiagConsumer),
62  SemanticHighlighting(SemanticHighlighting) {}
63 
64  void onPreambleAST(PathRef Path, ASTContext &Ctx,
65  std::shared_ptr<clang::Preprocessor> PP,
66  const CanonicalIncludes &CanonIncludes) override {
67  if (FIndex)
68  FIndex->updatePreamble(Path, Ctx, std::move(PP), CanonIncludes);
69  }
70 
71  void onMainAST(PathRef Path, ParsedAST &AST, PublishFn Publish) override {
72  if (FIndex)
73  FIndex->updateMain(Path, AST);
74 
75  std::vector<Diag> Diagnostics = AST.getDiagnostics();
76  std::vector<HighlightingToken> Highlightings;
77  if (SemanticHighlighting)
78  Highlightings = getSemanticHighlightings(AST);
79 
80  Publish([&]() {
81  DiagConsumer.onDiagnosticsReady(Path, std::move(Diagnostics));
82  if (SemanticHighlighting)
83  DiagConsumer.onHighlightingsReady(Path, std::move(Highlightings));
84  });
85  }
86 
87  void onFailedAST(PathRef Path, std::vector<Diag> Diags,
88  PublishFn Publish) override {
89  Publish([&]() { DiagConsumer.onDiagnosticsReady(Path, Diags); });
90  }
91 
92  void onFileUpdated(PathRef File, const TUStatus &Status) override {
93  DiagConsumer.onFileUpdated(File, Status);
94  }
95 
96 private:
97  FileIndex *FIndex;
98  DiagnosticsConsumer &DiagConsumer;
99  bool SemanticHighlighting;
100 };
101 } // namespace
102 
105  Opts.UpdateDebounce = std::chrono::steady_clock::duration::zero(); // Faster!
106  Opts.StorePreamblesInMemory = true;
107  Opts.AsyncThreadsCount = 4; // Consistent!
108  Opts.SemanticHighlighting = true;
109  return Opts;
110 }
111 
115  const Options &Opts)
116  : FSProvider(FSProvider),
117  DynamicIdx(Opts.BuildDynamicSymbolIndex
118  ? new FileIndex(Opts.HeavyweightDynamicSymbolIndex)
119  : nullptr),
120  GetClangTidyOptions(Opts.GetClangTidyOptions),
121  SuggestMissingIncludes(Opts.SuggestMissingIncludes),
122  CrossFileRename(Opts.CrossFileRename), TweakFilter(Opts.TweakFilter),
123  WorkspaceRoot(Opts.WorkspaceRoot),
124  // Pass a callback into `WorkScheduler` to extract symbols from a newly
125  // parsed file and rebuild the file index synchronously each time an AST
126  // is parsed.
127  // FIXME(ioeric): this can be slow and we may be able to index on less
128  // critical paths.
129  WorkScheduler(
130  CDB, Opts.AsyncThreadsCount, Opts.StorePreamblesInMemory,
131  std::make_unique<UpdateIndexCallbacks>(DynamicIdx.get(), DiagConsumer,
132  Opts.SemanticHighlighting),
133  Opts.UpdateDebounce, Opts.RetentionPolicy) {
134  // Adds an index to the stack, at higher priority than existing indexes.
135  auto AddIndex = [&](SymbolIndex *Idx) {
136  if (this->Index != nullptr) {
137  MergedIdx.push_back(std::make_unique<MergedIndex>(Idx, this->Index));
138  this->Index = MergedIdx.back().get();
139  } else {
140  this->Index = Idx;
141  }
142  };
143  if (Opts.StaticIndex)
144  AddIndex(Opts.StaticIndex);
145  if (Opts.BackgroundIndex) {
146  BackgroundIdx = std::make_unique<BackgroundIndex>(
147  Context::current().clone(), FSProvider, CDB,
149  [&CDB](llvm::StringRef File) { return CDB.getProjectInfo(File); }),
150  std::max(Opts.AsyncThreadsCount, 1u));
151  AddIndex(BackgroundIdx.get());
152  }
153  if (DynamicIdx)
154  AddIndex(DynamicIdx.get());
155 }
156 
158  WantDiagnostics WantDiags) {
159  auto FS = FSProvider.getFileSystem();
160 
161  ParseOptions Opts;
163  // FIXME: call tidy options builder on the worker thread, it can do IO.
164  if (GetClangTidyOptions)
165  Opts.ClangTidyOpts = GetClangTidyOptions(*FS, File);
166  Opts.SuggestMissingIncludes = SuggestMissingIncludes;
167 
168  // Compile command is set asynchronously during update, as it can be slow.
169  ParseInputs Inputs;
170  Inputs.FS = FS;
171  Inputs.Contents = Contents;
172  Inputs.Opts = std::move(Opts);
173  Inputs.Index = Index;
174  bool NewFile = WorkScheduler.update(File, Inputs, WantDiags);
175  // If we loaded Foo.h, we want to make sure Foo.cpp is indexed.
176  if (NewFile && BackgroundIdx)
177  BackgroundIdx->boostRelated(File);
178 }
179 
180 void ClangdServer::removeDocument(PathRef File) { WorkScheduler.remove(File); }
181 
182 llvm::StringRef ClangdServer::getDocument(PathRef File) const {
183  return WorkScheduler.getContents(File);
184 }
185 
187  const clangd::CodeCompleteOptions &Opts,
189  // Copy completion options for passing them to async task handler.
190  auto CodeCompleteOpts = Opts;
191  if (!CodeCompleteOpts.Index) // Respect overridden index.
192  CodeCompleteOpts.Index = Index;
193 
194  auto Task = [Pos, FS = FSProvider.getFileSystem(), CodeCompleteOpts,
195  File = File.str(), CB = std::move(CB),
196  this](llvm::Expected<InputsAndPreamble> IP) mutable {
197  if (!IP)
198  return CB(IP.takeError());
199  if (isCancelled())
200  return CB(llvm::make_error<CancelledError>());
201 
202  llvm::Optional<SpeculativeFuzzyFind> SpecFuzzyFind;
203  if (!IP->Preamble) {
204  // No speculation in Fallback mode, as it's supposed to be much faster
205  // without compiling.
206  vlog("Build for file {0} is not ready. Enter fallback mode.", File);
207  } else {
208  if (CodeCompleteOpts.Index && CodeCompleteOpts.SpeculativeIndexRequest) {
209  SpecFuzzyFind.emplace();
210  {
211  std::lock_guard<std::mutex> Lock(
212  CachedCompletionFuzzyFindRequestMutex);
213  SpecFuzzyFind->CachedReq =
214  CachedCompletionFuzzyFindRequestByFile[File];
215  }
216  }
217  }
218  // FIXME(ibiryukov): even if Preamble is non-null, we may want to check
219  // both the old and the new version in case only one of them matches.
221  File, IP->Command, IP->Preamble, IP->Contents, Pos, FS,
222  CodeCompleteOpts, SpecFuzzyFind ? SpecFuzzyFind.getPointer() : nullptr);
223  {
224  clang::clangd::trace::Span Tracer("Completion results callback");
225  CB(std::move(Result));
226  }
227  if (SpecFuzzyFind && SpecFuzzyFind->NewReq.hasValue()) {
228  std::lock_guard<std::mutex> Lock(CachedCompletionFuzzyFindRequestMutex);
229  CachedCompletionFuzzyFindRequestByFile[File] =
230  SpecFuzzyFind->NewReq.getValue();
231  }
232  // SpecFuzzyFind is only destroyed after speculative fuzzy find finishes.
233  // We don't want `codeComplete` to wait for the async call if it doesn't use
234  // the result (e.g. non-index completion, speculation fails), so that `CB`
235  // is called as soon as results are available.
236  };
237 
238  // We use a potentially-stale preamble because latency is critical here.
239  WorkScheduler.runWithPreamble(
240  "CodeComplete", File,
244  std::move(Task));
245 }
246 
249 
250  auto Action = [Pos, FS = FSProvider.getFileSystem(), File = File.str(),
251  CB = std::move(CB),
252  this](llvm::Expected<InputsAndPreamble> IP) mutable {
253  if (!IP)
254  return CB(IP.takeError());
255 
256  auto PreambleData = IP->Preamble;
257  CB(clangd::signatureHelp(File, IP->Command, PreambleData, IP->Contents, Pos,
258  FS, Index));
259  };
260 
261  // Unlike code completion, we wait for an up-to-date preamble here.
262  // Signature help is often triggered after code completion. If the code
263  // completion inserted a header to make the symbol available, then using
264  // the old preamble would yield useless results.
265  WorkScheduler.runWithPreamble("SignatureHelp", File, TUScheduler::Consistent,
266  std::move(Action));
267 }
268 
269 llvm::Expected<tooling::Replacements>
271  llvm::Expected<size_t> Begin = positionToOffset(Code, Rng.start);
272  if (!Begin)
273  return Begin.takeError();
274  llvm::Expected<size_t> End = positionToOffset(Code, Rng.end);
275  if (!End)
276  return End.takeError();
277  return formatCode(Code, File, {tooling::Range(*Begin, *End - *Begin)});
278 }
279 
280 llvm::Expected<tooling::Replacements>
282  // Format everything.
283  return formatCode(Code, File, {tooling::Range(0, Code.size())});
284 }
285 
286 llvm::Expected<std::vector<TextEdit>>
288  StringRef TriggerText) {
289  llvm::Expected<size_t> CursorPos = positionToOffset(Code, Pos);
290  if (!CursorPos)
291  return CursorPos.takeError();
292  auto FS = FSProvider.getFileSystem();
293  auto Style = format::getStyle(format::DefaultFormatStyle, File,
294  format::DefaultFallbackStyle, Code, FS.get());
295  if (!Style)
296  return Style.takeError();
297 
298  std::vector<TextEdit> Result;
299  for (const tooling::Replacement &R :
300  formatIncremental(Code, *CursorPos, TriggerText, *Style))
301  Result.push_back(replacementToEdit(Code, R));
302  return Result;
303 }
304 
306  Callback<llvm::Optional<Range>> CB) {
307  auto Action = [Pos, File = File.str(), CB = std::move(CB),
308  this](llvm::Expected<InputsAndAST> InpAST) mutable {
309  if (!InpAST)
310  return CB(InpAST.takeError());
311  auto &AST = InpAST->AST;
312  const auto &SM = AST.getSourceManager();
313  SourceLocation Loc =
314  SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
315  Pos, AST.getSourceManager(), AST.getLangOpts()));
316  auto Range = getTokenRange(SM, AST.getLangOpts(), Loc);
317  if (!Range)
318  return CB(llvm::None); // "rename" is not valid at the position.
319 
320  if (CrossFileRename)
321  // FIXME: we now assume cross-file rename always succeeds, revisit this.
322  return CB(*Range);
323 
324  // Performing the local rename isn't substantially more expensive than
325  // doing an AST-based check, so we just rename and throw away the results.
326  auto Changes = clangd::rename({Pos, "dummy", AST, File, Index,
327  /*AllowCrossFile=*/false,
328  /*GetDirtyBuffer=*/nullptr});
329  if (!Changes) {
330  // LSP says to return null on failure, but that will result in a generic
331  // failure message. If we send an LSP error response, clients can surface
332  // the message to users (VSCode does).
333  return CB(Changes.takeError());
334  }
335  return CB(*Range);
336  };
337  WorkScheduler.runWithAST("PrepareRename", File, std::move(Action));
338 }
339 
340 void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
341  bool WantFormat, Callback<FileEdits> CB) {
342  // A snapshot of all file dirty buffers.
343  llvm::StringMap<std::string> Snapshot = WorkScheduler.getAllFileContents();
344  auto Action = [File = File.str(), NewName = NewName.str(), Pos, WantFormat,
345  CB = std::move(CB), Snapshot = std::move(Snapshot),
346  this](llvm::Expected<InputsAndAST> InpAST) mutable {
347  if (!InpAST)
348  return CB(InpAST.takeError());
349  auto GetDirtyBuffer =
350  [&Snapshot](PathRef AbsPath) -> llvm::Optional<std::string> {
351  auto It = Snapshot.find(AbsPath);
352  if (It == Snapshot.end())
353  return llvm::None;
354  return It->second;
355  };
356  auto Edits = clangd::rename({Pos, NewName, InpAST->AST, File, Index,
357  CrossFileRename, GetDirtyBuffer});
358  if (!Edits)
359  return CB(Edits.takeError());
360 
361  if (WantFormat) {
362  auto Style = getFormatStyleForFile(File, InpAST->Inputs.Contents,
363  InpAST->Inputs.FS.get());
364  llvm::Error Err = llvm::Error::success();
365  for (auto &E : *Edits)
366  Err =
367  llvm::joinErrors(reformatEdit(E.getValue(), Style), std::move(Err));
368 
369  if (Err)
370  return CB(std::move(Err));
371  }
372  return CB(std::move(*Edits));
373  };
374  WorkScheduler.runWithAST("Rename", File, std::move(Action));
375 }
376 
377 static llvm::Expected<Tweak::Selection>
378 tweakSelection(const Range &Sel, const InputsAndAST &AST) {
379  auto Begin = positionToOffset(AST.Inputs.Contents, Sel.start);
380  if (!Begin)
381  return Begin.takeError();
382  auto End = positionToOffset(AST.Inputs.Contents, Sel.end);
383  if (!End)
384  return End.takeError();
385  return Tweak::Selection(AST.Inputs.Index, AST.AST, *Begin, *End);
386 }
387 
389  Callback<std::vector<TweakRef>> CB) {
390  auto Action = [File = File.str(), Sel, CB = std::move(CB),
391  this](Expected<InputsAndAST> InpAST) mutable {
392  if (!InpAST)
393  return CB(InpAST.takeError());
394  auto Selection = tweakSelection(Sel, *InpAST);
395  if (!Selection)
396  return CB(Selection.takeError());
397  std::vector<TweakRef> Res;
398  for (auto &T : prepareTweaks(*Selection, TweakFilter))
399  Res.push_back({T->id(), T->title(), T->intent()});
400 
401  CB(std::move(Res));
402  };
403 
404  WorkScheduler.runWithAST("EnumerateTweaks", File, std::move(Action));
405 }
406 
407 void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
409  auto Action =
410  [File = File.str(), Sel, TweakID = TweakID.str(), CB = std::move(CB),
411  FS = FSProvider.getFileSystem()](Expected<InputsAndAST> InpAST) mutable {
412  if (!InpAST)
413  return CB(InpAST.takeError());
414  auto Selection = tweakSelection(Sel, *InpAST);
415  if (!Selection)
416  return CB(Selection.takeError());
417  auto A = prepareTweak(TweakID, *Selection);
418  if (!A)
419  return CB(A.takeError());
420  auto Effect = (*A)->apply(*Selection);
421  if (!Effect)
422  return CB(Effect.takeError());
423  for (auto &It : Effect->ApplyEdits) {
424  Edit &E = It.second;
425  format::FormatStyle Style =
426  getFormatStyleForFile(File, E.InitialCode, FS.get());
427  if (llvm::Error Err = reformatEdit(E, Style))
428  elog("Failed to format {0}: {1}", It.first(), std::move(Err));
429  }
430  return CB(std::move(*Effect));
431  };
432  WorkScheduler.runWithAST("ApplyTweak", File, std::move(Action));
433 }
434 
436  llvm::unique_function<void(std::string)> Callback) {
437  auto Action = [Callback = std::move(Callback)](
438  llvm::Expected<InputsAndAST> InpAST) mutable {
439  if (!InpAST) {
440  llvm::consumeError(InpAST.takeError());
441  return Callback("<no-ast>");
442  }
443  std::string Result;
444 
445  llvm::raw_string_ostream ResultOS(Result);
446  clangd::dumpAST(InpAST->AST, ResultOS);
447  ResultOS.flush();
448 
449  Callback(Result);
450  };
451 
452  WorkScheduler.runWithAST("DumpAST", File, std::move(Action));
453 }
454 
456  Callback<std::vector<LocatedSymbol>> CB) {
457  auto Action = [Pos, CB = std::move(CB),
458  this](llvm::Expected<InputsAndAST> InpAST) mutable {
459  if (!InpAST)
460  return CB(InpAST.takeError());
461  CB(clangd::locateSymbolAt(InpAST->AST, Pos, Index));
462  };
463 
464  WorkScheduler.runWithAST("Definitions", File, std::move(Action));
465 }
466 
468  PathRef Path, Callback<llvm::Optional<clangd::Path>> CB) {
469  // We want to return the result as fast as possible, strategy is:
470  // 1) use the file-only heuristic, it requires some IO but it is much
471  // faster than building AST, but it only works when .h/.cc files are in
472  // the same directory.
473  // 2) if 1) fails, we use the AST&Index approach, it is slower but supports
474  // different code layout.
475  if (auto CorrespondingFile =
476  getCorrespondingHeaderOrSource(Path, FSProvider.getFileSystem()))
477  return CB(std::move(CorrespondingFile));
478  auto Action = [Path = Path.str(), CB = std::move(CB),
479  this](llvm::Expected<InputsAndAST> InpAST) mutable {
480  if (!InpAST)
481  return CB(InpAST.takeError());
482  CB(getCorrespondingHeaderOrSource(Path, InpAST->AST, Index));
483  };
484  WorkScheduler.runWithAST("SwitchHeaderSource", Path, std::move(Action));
485 }
486 
487 llvm::Expected<tooling::Replacements>
488 ClangdServer::formatCode(llvm::StringRef Code, PathRef File,
489  llvm::ArrayRef<tooling::Range> Ranges) {
490  // Call clang-format.
491  format::FormatStyle Style =
492  getFormatStyleForFile(File, Code, FSProvider.getFileSystem().get());
493  tooling::Replacements IncludeReplaces =
494  format::sortIncludes(Style, Code, Ranges, File);
495  auto Changed = tooling::applyAllReplacements(Code, IncludeReplaces);
496  if (!Changed)
497  return Changed.takeError();
498 
499  return IncludeReplaces.merge(format::reformat(
500  Style, *Changed,
501  tooling::calculateRangesAfterReplacements(IncludeReplaces, Ranges),
502  File));
503 }
504 
506  PathRef File, Position Pos, Callback<std::vector<DocumentHighlight>> CB) {
507  auto Action =
508  [Pos, CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
509  if (!InpAST)
510  return CB(InpAST.takeError());
511  CB(clangd::findDocumentHighlights(InpAST->AST, Pos));
512  };
513 
514  WorkScheduler.runWithAST("Highlights", File, std::move(Action));
515 }
516 
518  Callback<llvm::Optional<HoverInfo>> CB) {
519  auto Action = [File = File.str(), Pos, CB = std::move(CB),
520  this](llvm::Expected<InputsAndAST> InpAST) mutable {
521  if (!InpAST)
522  return CB(InpAST.takeError());
524  File, InpAST->Inputs.Contents, InpAST->Inputs.FS.get());
525  CB(clangd::getHover(InpAST->AST, Pos, std::move(Style), Index));
526  };
527 
528  WorkScheduler.runWithAST("Hover", File, std::move(Action));
529 }
530 
532  TypeHierarchyDirection Direction,
533  Callback<Optional<TypeHierarchyItem>> CB) {
534  auto Action = [File = File.str(), Pos, Resolve, Direction, CB = std::move(CB),
535  this](Expected<InputsAndAST> InpAST) mutable {
536  if (!InpAST)
537  return CB(InpAST.takeError());
538  CB(clangd::getTypeHierarchy(InpAST->AST, Pos, Resolve, Direction, Index,
539  File));
540  };
541 
542  WorkScheduler.runWithAST("Type Hierarchy", File, std::move(Action));
543 }
544 
546  TypeHierarchyItem Item, int Resolve, TypeHierarchyDirection Direction,
547  Callback<llvm::Optional<TypeHierarchyItem>> CB) {
548  clangd::resolveTypeHierarchy(Item, Resolve, Direction, Index);
549  CB(Item);
550 }
551 
553  // FIXME: Do nothing for now. This will be used for indexing and potentially
554  // invalidating other caches.
555 }
556 
558  llvm::StringRef Query, int Limit,
559  Callback<std::vector<SymbolInformation>> CB) {
560  WorkScheduler.run(
561  "getWorkspaceSymbols",
562  [Query = Query.str(), Limit, CB = std::move(CB), this]() mutable {
563  CB(clangd::getWorkspaceSymbols(Query, Limit, Index,
564  WorkspaceRoot.getValueOr("")));
565  });
566 }
567 
568 void ClangdServer::documentSymbols(llvm::StringRef File,
569  Callback<std::vector<DocumentSymbol>> CB) {
570  auto Action =
571  [CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
572  if (!InpAST)
573  return CB(InpAST.takeError());
574  CB(clangd::getDocumentSymbols(InpAST->AST));
575  };
576  WorkScheduler.runWithAST("documentSymbols", File, std::move(Action));
577 }
578 
581  auto Action = [Pos, Limit, CB = std::move(CB),
582  this](llvm::Expected<InputsAndAST> InpAST) mutable {
583  if (!InpAST)
584  return CB(InpAST.takeError());
585  CB(clangd::findReferences(InpAST->AST, Pos, Limit, Index));
586  };
587 
588  WorkScheduler.runWithAST("References", File, std::move(Action));
589 }
590 
592  Callback<std::vector<SymbolDetails>> CB) {
593  auto Action =
594  [Pos, CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
595  if (!InpAST)
596  return CB(InpAST.takeError());
597  CB(clangd::getSymbolInfo(InpAST->AST, Pos));
598  };
599 
600  WorkScheduler.runWithAST("SymbolInfo", File, std::move(Action));
601 }
602 
604  Callback<std::vector<Range>> CB) {
605  auto Action =
606  [Pos, CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
607  if (!InpAST)
608  return CB(InpAST.takeError());
609  CB(clangd::getSemanticRanges(InpAST->AST, Pos));
610  };
611  WorkScheduler.runWithAST("SemanticRanges", File, std::move(Action));
612 }
613 
615  Callback<std::vector<DocumentLink>> CB) {
616  auto Action =
617  [CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
618  if (!InpAST)
619  return CB(InpAST.takeError());
620  CB(clangd::getDocumentLinks(InpAST->AST));
621  };
622  WorkScheduler.runWithAST("DocumentLinks", File, std::move(Action));
623 }
624 
625 std::vector<std::pair<Path, std::size_t>>
627  return WorkScheduler.getUsedBytesPerFile();
628 }
629 
630 LLVM_NODISCARD bool
631 ClangdServer::blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds) {
632  return WorkScheduler.blockUntilIdle(timeoutSeconds(TimeoutSeconds)) &&
633  (!BackgroundIdx ||
634  BackgroundIdx->blockUntilIdleForTest(TimeoutSeconds));
635 }
636 
637 } // namespace clangd
638 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
int Limit
std::string Code
WantDiagnostics
Determines whether diagnostics should be generated for a file snapshot.
Definition: TUScheduler.h:47
std::vector< tooling::Replacement > formatIncremental(llvm::StringRef OriginalCode, unsigned OriginalCursor, llvm::StringRef InsertedText, format::FormatStyle Style)
Applies limited formatting around new InsertedText.
Definition: Format.cpp:246
void addDocument(PathRef File, StringRef Contents, WantDiagnostics WD=WantDiagnostics::Auto)
Add a File to the list of tracked C++ files or update the contents if File is already tracked...
llvm::Expected< std::vector< SymbolInformation > > getWorkspaceSymbols(llvm::StringRef Query, int Limit, const SymbolIndex *const Index, llvm::StringRef HintPath)
Searches for the symbols matching Query.
Definition: FindSymbols.cpp:67
llvm::StringRef Contents
void codeComplete(PathRef File, Position Pos, const clangd::CodeCompleteOptions &Opts, Callback< CodeCompleteResult > CB)
Run code completion for File at Pos.
Position start
The range&#39;s start position.
Definition: Protocol.h:158
The preamble may be generated from an older version of the file.
Definition: TUScheduler.h:214
void typeHierarchy(PathRef File, Position Pos, int Resolve, TypeHierarchyDirection Direction, Callback< llvm::Optional< TypeHierarchyItem >> CB)
Get information about type hierarchy for a given position.
bool BackgroundIndex
If true, ClangdServer automatically indexes files in the current project on background threads...
Definition: ClangdServer.h:101
tooling::Replacements Changes
Definition: Format.cpp:108
llvm::Expected< std::unique_ptr< Tweak > > prepareTweak(StringRef ID, const Tweak::Selection &S)
Definition: Tweak.cpp:77
bool blockUntilIdle(Deadline D) const
Wait until there are no scheduled or running tasks.
std::vector< DocumentLink > getDocumentLinks(ParsedAST &AST)
Get all document links.
Definition: XRefs.cpp:170
void remove(PathRef File)
Remove File from the list of tracked files and schedule removal of its resources. ...
void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS)
For testing/debugging purposes.
Definition: ParsedAST.cpp:213
void onFileEvent(const DidChangeWatchedFilesParams &Params)
Called when an event occurs for a watched file in the workspace.
void resolveTypeHierarchy(TypeHierarchyItem &Item, int ResolveLevels, TypeHierarchyDirection Direction, const SymbolIndex *Index)
Definition: XRefs.cpp:807
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
static Factory createDiskBackedStorageFactory(std::function< llvm::Optional< ProjectInfo >(PathRef)> GetProjectInfo)
ClangdServer(const GlobalCompilationDatabase &CDB, const FileSystemProvider &FSProvider, DiagnosticsConsumer &DiagConsumer, const Options &Opts)
Creates a new ClangdServer instance.
This manages symbols from files and an in-memory index on all symbols.
Definition: FileIndex.h:95
Besides accepting stale preamble, this also allow preamble to be absent (not ready or failed to build...
Definition: TUScheduler.h:217
SourceLocation getBeginningOfIdentifier(const Position &Pos, const SourceManager &SM, const LangOptions &LangOpts)
Get the beginning SourceLocation at a specified Pos in the main file.
Definition: SourceCode.cpp:277
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
LLVM_NODISCARD bool blockUntilIdleForTest(llvm::Optional< double > TimeoutSeconds=10)
MockFSProvider FS
ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit, const SymbolIndex *Index)
Returns references of the symbol at a specified Pos.
Definition: XRefs.cpp:423
static Options optsForTest()
Documents should not be synced at all.
tidy::ClangTidyOptions ClangTidyOpts
Definition: Compiler.h:39
void findDocumentHighlights(PathRef File, Position Pos, Callback< std::vector< DocumentHighlight >> CB)
Get document highlights for a given position.
unsigned AsyncThreadsCount
To process requests asynchronously, ClangdServer spawns worker threads.
Definition: ClangdServer.h:86
void vlog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:67
bool update(PathRef File, ParseInputs Inputs, WantDiagnostics WD)
Schedule an update for File.
std::string InitialCode
Definition: SourceCode.h:206
void elog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:56
void applyTweak(PathRef File, Range Sel, StringRef ID, Callback< Tweak::Effect > CB)
Apply the code tweak with a specified ID.
MockFSProvider FSProvider
static llvm::Expected< Tweak::Selection > tweakSelection(const Range &Sel, const InputsAndAST &AST)
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)
bool isCancelled(const Context &Ctx)
True if the current context is within a cancelable task which was cancelled.
Provides compilation arguments used for parsing C and C++ files.
Context Ctx
std::vector< SymbolDetails > getSymbolInfo(ParsedAST &AST, Position Pos)
Get info about symbols at Pos.
Definition: XRefs.cpp:519
void documentLinks(PathRef File, Callback< std::vector< DocumentLink >> CB)
Get all document links in a file.
llvm::Optional< HoverInfo > getHover(ParsedAST &AST, Position Pos, format::FormatStyle Style, const SymbolIndex *Index)
Get the hover information when hovering at Pos.
Definition: Hover.cpp:477
Context clone() const
Clone this context object.
Definition: Context.cpp:20
void resolveTypeHierarchy(TypeHierarchyItem Item, int Resolve, TypeHierarchyDirection Direction, Callback< llvm::Optional< TypeHierarchyItem >> CB)
Resolve type hierarchy item in the given direction.
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength)
Turn a [line, column] pair into an offset in Code.
Definition: SourceCode.cpp:155
llvm::Expected< std::vector< TextEdit > > formatOnType(StringRef Code, PathRef File, Position Pos, StringRef TriggerText)
Run formatting after TriggerText was typed at Pos in File with content Code.
llvm::unique_function< void()> Action
std::string Path
A typedef to represent a file path.
Definition: Path.h:20
static const Context & current()
Returns the context for the current thread, creating it if needed.
Definition: Context.cpp:27
const ParseInputs & Inputs
Definition: TUScheduler.h:35
std::vector< std::pair< Path, std::size_t > > getUsedBytesPerFile() const
Returns estimated memory usage for each of the currently open files.
void runWithPreamble(llvm::StringRef Name, PathRef File, PreambleConsistency Consistency, Callback< InputsAndPreamble > Action)
Schedule an async read of the preamble.
enum clang::clangd::CodeCompleteOptions::CodeCompletionParse RunParser
void switchSourceHeader(PathRef Path, Callback< llvm::Optional< clangd::Path >> CB)
Switch to a corresponding source file when given a header file, and vice versa.
std::chrono::steady_clock::duration UpdateDebounce
Time to wait after a new file version before computing diagnostics.
Definition: ClangdServer.h:125
llvm::Expected< std::vector< Range > > getSemanticRanges(ParsedAST &AST, Position Pos)
Returns the list of all interesting ranges around the Position Pos.
void dumpAST(PathRef File, llvm::unique_function< void(std::string)> Callback)
Only for testing purposes.
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
Definition: SourceCode.cpp:227
void documentSymbols(StringRef File, Callback< std::vector< DocumentSymbol >> CB)
Retrieve the symbols within the specified file.
Position Pos
Definition: SourceCode.cpp:772
std::vector< DocumentHighlight > findDocumentHighlights(ParsedAST &AST, Position Pos)
Returns highlights for all usages of a symbol at Pos.
Definition: XRefs.cpp:390
Input to prepare and apply tweaks.
Definition: Tweak.h:49
std::vector< std::pair< Path, std::size_t > > getUsedBytesPerFile() const
Returns estimated memory usage for each of the currently open files.
format::FormatStyle getFormatStyleForFile(llvm::StringRef File, llvm::StringRef Content, llvm::vfs::FileSystem *FS)
Choose the clang-format style we should apply to a certain file.
Definition: SourceCode.cpp:693
void enumerateTweaks(PathRef File, Range Sel, Callback< std::vector< TweakRef >> CB)
Enumerate the code tweaks available to the user at a specified point.
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)
void findHover(PathRef File, Position Pos, Callback< llvm::Optional< HoverInfo >> CB)
Get code hover for a given position.
const SymbolIndex * Index
Definition: Compiler.h:49
const SymbolIndex * Index
If Index is set, it is used to augment the code completion results.
Definition: CodeComplete.h:103
llvm::Expected< tooling::Replacements > formatRange(StringRef Code, PathRef File, Range Rng)
Run formatting for Rng inside File with content Code.
Block until we can run the parser (e.g.
Definition: CodeComplete.h:125
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:44
llvm::Optional< Path > getCorrespondingHeaderOrSource(const Path &OriginalFile, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS)
Given a header file, returns the best matching source file, and vice visa.
bool StorePreamblesInMemory
Cached preambles are potentially large. If false, store them on disk.
Definition: ClangdServer.h:92
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Deadline timeoutSeconds(llvm::Optional< double > Seconds)
Makes a deadline from a timeout in seconds. None means wait forever.
Definition: Threading.cpp:99
TextEdit replacementToEdit(llvm::StringRef Code, const tooling::Replacement &R)
Definition: SourceCode.cpp:606
llvm::Expected< std::vector< DocumentSymbol > > getDocumentSymbols(ParsedAST &AST)
Retrieves the symbols contained in the "main file" section of an AST in the same order that they appe...
CharSourceRange Range
SourceRange for the file name.
llvm::Optional< TypeHierarchyItem > getTypeHierarchy(ParsedAST &AST, Position Pos, int ResolveLevels, TypeHierarchyDirection Direction, const SymbolIndex *Index, PathRef TUPath)
Get type hierarchy information at Pos.
Definition: XRefs.cpp:768
void removeDocument(PathRef File)
Remove File from list of tracked files, schedule a request to free resources associated with it...
void runWithAST(llvm::StringRef Name, PathRef File, Callback< InputsAndAST > Action)
Schedule an async read of the AST.
void prepareRename(PathRef File, Position Pos, Callback< llvm::Optional< Range >> CB)
Test the validity of a rename operation.
std::vector< HighlightingToken > getSemanticHighlightings(ParsedAST &AST)
llvm::StringRef getDocument(PathRef File) const
Get the contents of File, which should have been added.
const Expr * E
void symbolInfo(PathRef File, Position Pos, Callback< std::vector< SymbolDetails >> CB)
Get symbol info for given position.
IgnoreDiagnostics DiagConsumer
void findReferences(PathRef File, Position Pos, uint32_t Limit, Callback< ReferencesResult > CB)
Retrieve locations for symbol references.
void locateSymbolAt(PathRef File, Position Pos, Callback< std::vector< LocatedSymbol >> CB)
Find declaration/definition locations of symbol at a specified position.
std::vector< LocatedSymbol > locateSymbolAt(ParsedAST &AST, Position Pos, const SymbolIndex *Index)
Get definition of symbol at a specified Pos.
Definition: XRefs.cpp:190
void semanticRanges(PathRef File, Position Pos, Callback< std::vector< Range >> CB)
Get semantic ranges around a specified position in a file.
void rename(PathRef File, Position Pos, llvm::StringRef NewName, bool WantFormat, Callback< FileEdits > CB)
Rename all occurrences of the symbol at the Pos in File to NewName.
IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS
Definition: Compiler.h:46
PrecompiledPreamble Preamble
Definition: Preamble.h:52
static ClangTidyOptions getDefaults()
These options are used for all settings that haven&#39;t been overridden by the OptionsProvider.
llvm::Expected< FileEdits > rename(const RenameInputs &RInputs)
Renames all occurrences of the symbol.
Definition: Rename.cpp:427
Position end
The range&#39;s end position.
Definition: Protocol.h:161
llvm::StringRef getContents(PathRef File) const
Returns the current contents of the buffer for File, per last update().
The parsed preamble and associated data.
Definition: Preamble.h:45
Records an event whose duration is the lifetime of the Span object.
Definition: Trace.h:81
SymbolIndex * StaticIndex
If set, use this index to augment code completion results.
Definition: ClangdServer.h:104
void workspaceSymbols(StringRef Query, int Limit, Callback< std::vector< SymbolInformation >> CB)
Retrieve the top symbols from the workspace matching a query.
llvm::Expected< tooling::Replacements > formatFile(StringRef Code, PathRef File)
Run formatting for the whole File with content Code.
void signatureHelp(PathRef File, Position Pos, Callback< SignatureHelp > CB)
Provide signature help for File at Pos.
A set of edits generated for a single file.
Definition: SourceCode.h:204
The preamble is generated from the current version of the file.
Definition: TUScheduler.h:206
llvm::StringMap< std::string > getAllFileContents() const
Returns a snapshot of all file buffer contents, per last update().
llvm::Error reformatEdit(Edit &E, const format::FormatStyle &Style)
Formats the edits and code around it according to Style.
bool SemanticHighlighting
Enable semantic highlighting features.
Definition: ClangdServer.h:135
std::vector< std::unique_ptr< Tweak > > prepareTweaks(const Tweak::Selection &S, llvm::function_ref< bool(const Tweak &)> Filter)
Calls prepare() on all tweaks that satisfy the filter, returning those that can run on the selection...
Definition: Tweak.cpp:59
void run(llvm::StringRef Name, llvm::unique_function< void()> Action)
Schedule an async task with no dependencies.
static cl::opt< std::string > FormatStyle("format-style", cl::desc(R"( Style for formatting code around applied fixes: - 'none' (default) turns off formatting - 'file' (literally 'file', not a placeholder) uses .clang-format file in the closest parent directory - '{ <json> }' specifies options inline, e.g. -format-style='{BasedOnStyle: llvm, IndentWidth: 8}' - 'llvm', 'google', 'webkit', 'mozilla' See clang-format documentation for the up-to-date information about formatting styles and options. This option overrides the 'FormatStyle` option in .clang-tidy file, if any. )"), cl::init("none"), cl::cat(ClangTidyCategory))
virtual llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > getFileSystem() const =0
Called by ClangdServer to obtain a vfs::FileSystem to be used for parsing.