clang-tools  14.0.0git
CodeComplete.cpp
Go to the documentation of this file.
1 //===--- CodeComplete.cpp ----------------------------------------*- 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 // Code completion has several moving parts:
10 // - AST-based completions are provided using the completion hooks in Sema.
11 // - external completions are retrieved from the index (using hints from Sema)
12 // - the two sources overlap, and must be merged and overloads bundled
13 // - results must be scored and ranked (see Quality.h) before rendering
14 //
15 // Signature help works in a similar way as code completion, but it is simpler:
16 // it's purely AST-based, and there are few candidates.
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "CodeComplete.h"
21 #include "AST.h"
22 #include "CodeCompletionStrings.h"
23 #include "Compiler.h"
24 #include "Diagnostics.h"
25 #include "ExpectedTypes.h"
26 #include "FileDistance.h"
27 #include "FuzzyMatch.h"
28 #include "Headers.h"
29 #include "Hover.h"
30 #include "Preamble.h"
31 #include "Protocol.h"
32 #include "Quality.h"
33 #include "SourceCode.h"
34 #include "TUScheduler.h"
35 #include "URI.h"
36 #include "index/Index.h"
37 #include "index/Symbol.h"
38 #include "index/SymbolOrigin.h"
39 #include "support/Logger.h"
40 #include "support/Threading.h"
41 #include "support/ThreadsafeFS.h"
42 #include "support/Trace.h"
43 #include "clang/AST/Decl.h"
44 #include "clang/AST/DeclBase.h"
45 #include "clang/Basic/CharInfo.h"
46 #include "clang/Basic/LangOptions.h"
47 #include "clang/Basic/SourceLocation.h"
48 #include "clang/Basic/TokenKinds.h"
49 #include "clang/Format/Format.h"
50 #include "clang/Frontend/CompilerInstance.h"
51 #include "clang/Frontend/FrontendActions.h"
52 #include "clang/Lex/ExternalPreprocessorSource.h"
53 #include "clang/Lex/Lexer.h"
54 #include "clang/Lex/Preprocessor.h"
55 #include "clang/Lex/PreprocessorOptions.h"
56 #include "clang/Sema/CodeCompleteConsumer.h"
57 #include "clang/Sema/DeclSpec.h"
58 #include "clang/Sema/Sema.h"
59 #include "llvm/ADT/ArrayRef.h"
60 #include "llvm/ADT/None.h"
61 #include "llvm/ADT/Optional.h"
62 #include "llvm/ADT/SmallVector.h"
63 #include "llvm/ADT/StringExtras.h"
64 #include "llvm/ADT/StringRef.h"
65 #include "llvm/Support/Casting.h"
66 #include "llvm/Support/Compiler.h"
67 #include "llvm/Support/Debug.h"
68 #include "llvm/Support/Error.h"
69 #include "llvm/Support/Format.h"
70 #include "llvm/Support/FormatVariadic.h"
71 #include "llvm/Support/ScopedPrinter.h"
72 #include <algorithm>
73 #include <iterator>
74 #include <limits>
75 
76 // We log detailed candidate here if you run with -debug-only=codecomplete.
77 #define DEBUG_TYPE "CodeComplete"
78 
79 namespace clang {
80 namespace clangd {
81 namespace {
82 
83 CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) {
84  using SK = index::SymbolKind;
85  switch (Kind) {
86  case SK::Unknown:
88  case SK::Module:
89  case SK::Namespace:
90  case SK::NamespaceAlias:
92  case SK::Macro:
94  case SK::Enum:
96  case SK::Struct:
98  case SK::Class:
99  case SK::Protocol:
100  case SK::Extension:
101  case SK::Union:
103  case SK::TypeAlias:
104  // We use the same kind as the VSCode C++ extension.
105  // FIXME: pick a better option when we have one.
107  case SK::Using:
109  case SK::Function:
110  case SK::ConversionFunction:
112  case SK::Variable:
113  case SK::Parameter:
114  case SK::NonTypeTemplateParm:
116  case SK::Field:
118  case SK::EnumConstant:
120  case SK::InstanceMethod:
121  case SK::ClassMethod:
122  case SK::StaticMethod:
123  case SK::Destructor:
125  case SK::InstanceProperty:
126  case SK::ClassProperty:
127  case SK::StaticProperty:
129  case SK::Constructor:
131  case SK::TemplateTypeParm:
132  case SK::TemplateTemplateParm:
134  }
135  llvm_unreachable("Unhandled clang::index::SymbolKind.");
136 }
137 
139 toCompletionItemKind(CodeCompletionResult::ResultKind ResKind,
140  const NamedDecl *Decl,
141  CodeCompletionContext::Kind CtxKind) {
142  if (Decl)
143  return toCompletionItemKind(index::getSymbolInfo(Decl).Kind);
144  if (CtxKind == CodeCompletionContext::CCC_IncludedFile)
146  switch (ResKind) {
147  case CodeCompletionResult::RK_Declaration:
148  llvm_unreachable("RK_Declaration without Decl");
149  case CodeCompletionResult::RK_Keyword:
151  case CodeCompletionResult::RK_Macro:
152  return CompletionItemKind::Text; // unfortunately, there's no 'Macro'
153  // completion items in LSP.
154  case CodeCompletionResult::RK_Pattern:
156  }
157  llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
158 }
159 
160 // Identifier code completion result.
161 struct RawIdentifier {
162  llvm::StringRef Name;
163  unsigned References; // # of usages in file.
164 };
165 
166 /// A code completion result, in clang-native form.
167 /// It may be promoted to a CompletionItem if it's among the top-ranked results.
168 struct CompletionCandidate {
169  llvm::StringRef Name; // Used for filtering and sorting.
170  // We may have a result from Sema, from the index, or both.
171  const CodeCompletionResult *SemaResult = nullptr;
172  const Symbol *IndexResult = nullptr;
173  const RawIdentifier *IdentifierResult = nullptr;
174  llvm::SmallVector<llvm::StringRef, 1> RankedIncludeHeaders;
175 
176  // Returns a token identifying the overload set this is part of.
177  // 0 indicates it's not part of any overload set.
178  size_t overloadSet(const CodeCompleteOptions &Opts, llvm::StringRef FileName,
179  IncludeInserter *Inserter) const {
180  if (!Opts.BundleOverloads.getValueOr(false))
181  return 0;
182 
183  // Depending on the index implementation, we can see different header
184  // strings (literal or URI) mapping to the same file. We still want to
185  // bundle those, so we must resolve the header to be included here.
186  std::string HeaderForHash;
187  if (Inserter) {
188  if (auto Header = headerToInsertIfAllowed(Opts)) {
189  if (auto HeaderFile = toHeaderFile(*Header, FileName)) {
190  if (auto Spelled =
191  Inserter->calculateIncludePath(*HeaderFile, FileName))
192  HeaderForHash = *Spelled;
193  } else {
194  vlog("Code completion header path manipulation failed {0}",
195  HeaderFile.takeError());
196  }
197  }
198  }
199 
200  llvm::SmallString<256> Scratch;
201  if (IndexResult) {
202  switch (IndexResult->SymInfo.Kind) {
203  case index::SymbolKind::ClassMethod:
204  case index::SymbolKind::InstanceMethod:
205  case index::SymbolKind::StaticMethod:
206 #ifndef NDEBUG
207  llvm_unreachable("Don't expect members from index in code completion");
208 #else
209  LLVM_FALLTHROUGH;
210 #endif
211  case index::SymbolKind::Function:
212  // We can't group overloads together that need different #includes.
213  // This could break #include insertion.
214  return llvm::hash_combine(
215  (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch),
216  HeaderForHash);
217  default:
218  return 0;
219  }
220  }
221  if (SemaResult) {
222  // We need to make sure we're consistent with the IndexResult case!
223  const NamedDecl *D = SemaResult->Declaration;
224  if (!D || !D->isFunctionOrFunctionTemplate())
225  return 0;
226  {
227  llvm::raw_svector_ostream OS(Scratch);
228  D->printQualifiedName(OS);
229  }
230  return llvm::hash_combine(Scratch, HeaderForHash);
231  }
232  assert(IdentifierResult);
233  return 0;
234  }
235 
236  // The best header to include if include insertion is allowed.
237  llvm::Optional<llvm::StringRef>
238  headerToInsertIfAllowed(const CodeCompleteOptions &Opts) const {
239  if (Opts.InsertIncludes == CodeCompleteOptions::NeverInsert ||
240  RankedIncludeHeaders.empty())
241  return None;
242  if (SemaResult && SemaResult->Declaration) {
243  // Avoid inserting new #include if the declaration is found in the current
244  // file e.g. the symbol is forward declared.
245  auto &SM = SemaResult->Declaration->getASTContext().getSourceManager();
246  for (const Decl *RD : SemaResult->Declaration->redecls())
247  if (SM.isInMainFile(SM.getExpansionLoc(RD->getBeginLoc())))
248  return None;
249  }
250  return RankedIncludeHeaders[0];
251  }
252 
253  using Bundle = llvm::SmallVector<CompletionCandidate, 4>;
254 };
255 using ScoredBundle =
256  std::pair<CompletionCandidate::Bundle, CodeCompletion::Scores>;
257 struct ScoredBundleGreater {
258  bool operator()(const ScoredBundle &L, const ScoredBundle &R) {
259  if (L.second.Total != R.second.Total)
260  return L.second.Total > R.second.Total;
261  return L.first.front().Name <
262  R.first.front().Name; // Earlier name is better.
263  }
264 };
265 
266 // Assembles a code completion out of a bundle of >=1 completion candidates.
267 // Many of the expensive strings are only computed at this point, once we know
268 // the candidate bundle is going to be returned.
269 //
270 // Many fields are the same for all candidates in a bundle (e.g. name), and are
271 // computed from the first candidate, in the constructor.
272 // Others vary per candidate, so add() must be called for remaining candidates.
273 struct CodeCompletionBuilder {
274  CodeCompletionBuilder(ASTContext *ASTCtx, const CompletionCandidate &C,
275  CodeCompletionString *SemaCCS,
276  llvm::ArrayRef<std::string> QueryScopes,
277  const IncludeInserter &Includes,
278  llvm::StringRef FileName,
279  CodeCompletionContext::Kind ContextKind,
280  const CodeCompleteOptions &Opts,
281  bool IsUsingDeclaration, tok::TokenKind NextTokenKind)
282  : ASTCtx(ASTCtx),
283  EnableFunctionArgSnippets(Opts.EnableFunctionArgSnippets),
284  IsUsingDeclaration(IsUsingDeclaration), NextTokenKind(NextTokenKind) {
285  Completion.Deprecated = true; // cleared by any non-deprecated overload.
286  add(C, SemaCCS);
287  if (C.SemaResult) {
288  assert(ASTCtx);
289  Completion.Origin |= SymbolOrigin::AST;
290  Completion.Name = std::string(llvm::StringRef(SemaCCS->getTypedText()));
291  if (Completion.Scope.empty()) {
292  if ((C.SemaResult->Kind == CodeCompletionResult::RK_Declaration) ||
293  (C.SemaResult->Kind == CodeCompletionResult::RK_Pattern))
294  if (const auto *D = C.SemaResult->getDeclaration())
295  if (const auto *ND = dyn_cast<NamedDecl>(D))
296  Completion.Scope = std::string(
298  }
299  Completion.Kind = toCompletionItemKind(
300  C.SemaResult->Kind, C.SemaResult->Declaration, ContextKind);
301  // Sema could provide more info on whether the completion was a file or
302  // folder.
303  if (Completion.Kind == CompletionItemKind::File &&
304  Completion.Name.back() == '/')
305  Completion.Kind = CompletionItemKind::Folder;
306  for (const auto &FixIt : C.SemaResult->FixIts) {
307  Completion.FixIts.push_back(toTextEdit(
308  FixIt, ASTCtx->getSourceManager(), ASTCtx->getLangOpts()));
309  }
310  llvm::sort(Completion.FixIts, [](const TextEdit &X, const TextEdit &Y) {
311  return std::tie(X.range.start.line, X.range.start.character) <
312  std::tie(Y.range.start.line, Y.range.start.character);
313  });
314  }
315  if (C.IndexResult) {
316  Completion.Origin |= C.IndexResult->Origin;
317  if (Completion.Scope.empty())
318  Completion.Scope = std::string(C.IndexResult->Scope);
319  if (Completion.Kind == CompletionItemKind::Missing)
320  Completion.Kind = toCompletionItemKind(C.IndexResult->SymInfo.Kind);
321  if (Completion.Name.empty())
322  Completion.Name = std::string(C.IndexResult->Name);
323  // If the completion was visible to Sema, no qualifier is needed. This
324  // avoids unneeded qualifiers in cases like with `using ns::X`.
325  if (Completion.RequiredQualifier.empty() && !C.SemaResult) {
326  llvm::StringRef ShortestQualifier = C.IndexResult->Scope;
327  for (llvm::StringRef Scope : QueryScopes) {
328  llvm::StringRef Qualifier = C.IndexResult->Scope;
329  if (Qualifier.consume_front(Scope) &&
330  Qualifier.size() < ShortestQualifier.size())
331  ShortestQualifier = Qualifier;
332  }
333  Completion.RequiredQualifier = std::string(ShortestQualifier);
334  }
335  }
336  if (C.IdentifierResult) {
337  Completion.Origin |= SymbolOrigin::Identifier;
338  Completion.Kind = CompletionItemKind::Text;
339  Completion.Name = std::string(C.IdentifierResult->Name);
340  }
341 
342  // Turn absolute path into a literal string that can be #included.
343  auto Inserted = [&](llvm::StringRef Header)
344  -> llvm::Expected<std::pair<std::string, bool>> {
345  auto ResolvedDeclaring =
346  URI::resolve(C.IndexResult->CanonicalDeclaration.FileURI, FileName);
347  if (!ResolvedDeclaring)
348  return ResolvedDeclaring.takeError();
349  auto ResolvedInserted = toHeaderFile(Header, FileName);
350  if (!ResolvedInserted)
351  return ResolvedInserted.takeError();
352  auto Spelled = Includes.calculateIncludePath(*ResolvedInserted, FileName);
353  if (!Spelled)
354  return error("Header not on include path");
355  return std::make_pair(
356  std::move(*Spelled),
357  Includes.shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
358  };
359  bool ShouldInsert = C.headerToInsertIfAllowed(Opts).hasValue();
360  // Calculate include paths and edits for all possible headers.
361  for (const auto &Inc : C.RankedIncludeHeaders) {
362  if (auto ToInclude = Inserted(Inc)) {
363  CodeCompletion::IncludeCandidate Include;
364  Include.Header = ToInclude->first;
365  if (ToInclude->second && ShouldInsert)
366  Include.Insertion = Includes.insert(ToInclude->first);
367  Completion.Includes.push_back(std::move(Include));
368  } else
369  log("Failed to generate include insertion edits for adding header "
370  "(FileURI='{0}', IncludeHeader='{1}') into {2}: {3}",
371  C.IndexResult->CanonicalDeclaration.FileURI, Inc, FileName,
372  ToInclude.takeError());
373  }
374  // Prefer includes that do not need edits (i.e. already exist).
375  std::stable_partition(Completion.Includes.begin(),
376  Completion.Includes.end(),
377  [](const CodeCompletion::IncludeCandidate &I) {
378  return !I.Insertion.hasValue();
379  });
380  }
381 
382  void add(const CompletionCandidate &C, CodeCompletionString *SemaCCS) {
383  assert(bool(C.SemaResult) == bool(SemaCCS));
384  Bundled.emplace_back();
385  BundledEntry &S = Bundled.back();
386  if (C.SemaResult) {
387  bool IsPattern = C.SemaResult->Kind == CodeCompletionResult::RK_Pattern;
388  getSignature(*SemaCCS, &S.Signature, &S.SnippetSuffix,
389  &Completion.RequiredQualifier, IsPattern);
390  S.ReturnType = getReturnType(*SemaCCS);
391  } else if (C.IndexResult) {
392  S.Signature = std::string(C.IndexResult->Signature);
393  S.SnippetSuffix = std::string(C.IndexResult->CompletionSnippetSuffix);
394  S.ReturnType = std::string(C.IndexResult->ReturnType);
395  }
396  if (!Completion.Documentation) {
397  auto SetDoc = [&](llvm::StringRef Doc) {
398  if (!Doc.empty()) {
399  Completion.Documentation.emplace();
400  parseDocumentation(Doc, *Completion.Documentation);
401  }
402  };
403  if (C.IndexResult) {
404  SetDoc(C.IndexResult->Documentation);
405  } else if (C.SemaResult) {
406  const auto DocComment = getDocComment(*ASTCtx, *C.SemaResult,
407  /*CommentsFromHeader=*/false);
408  SetDoc(formatDocumentation(*SemaCCS, DocComment));
409  }
410  }
411  if (Completion.Deprecated) {
412  if (C.SemaResult)
413  Completion.Deprecated &=
414  C.SemaResult->Availability == CXAvailability_Deprecated;
415  if (C.IndexResult)
416  Completion.Deprecated &=
417  bool(C.IndexResult->Flags & Symbol::Deprecated);
418  }
419  }
420 
421  CodeCompletion build() {
422  Completion.ReturnType = summarizeReturnType();
423  Completion.Signature = summarizeSignature();
424  Completion.SnippetSuffix = summarizeSnippet();
425  Completion.BundleSize = Bundled.size();
426  return std::move(Completion);
427  }
428 
429 private:
430  struct BundledEntry {
431  std::string SnippetSuffix;
432  std::string Signature;
433  std::string ReturnType;
434  };
435 
436  // If all BundledEntries have the same value for a property, return it.
437  template <std::string BundledEntry::*Member>
438  const std::string *onlyValue() const {
439  auto B = Bundled.begin(), E = Bundled.end();
440  for (auto I = B + 1; I != E; ++I)
441  if (I->*Member != B->*Member)
442  return nullptr;
443  return &(B->*Member);
444  }
445 
446  template <bool BundledEntry::*Member> const bool *onlyValue() const {
447  auto B = Bundled.begin(), E = Bundled.end();
448  for (auto I = B + 1; I != E; ++I)
449  if (I->*Member != B->*Member)
450  return nullptr;
451  return &(B->*Member);
452  }
453 
454  std::string summarizeReturnType() const {
455  if (auto *RT = onlyValue<&BundledEntry::ReturnType>())
456  return *RT;
457  return "";
458  }
459 
460  std::string summarizeSnippet() const {
461  if (IsUsingDeclaration)
462  return "";
463  auto *Snippet = onlyValue<&BundledEntry::SnippetSuffix>();
464  if (!Snippet)
465  // All bundles are function calls.
466  // FIXME(ibiryukov): sometimes add template arguments to a snippet, e.g.
467  // we need to complete 'forward<$1>($0)'.
468  return "($0)";
469  // Suppress function argument snippets cursor is followed by left
470  // parenthesis (and potentially arguments) or if there are potentially
471  // template arguments. There are cases where it would be wrong (e.g. next
472  // '<' token is a comparison rather than template argument list start) but
473  // it is less common and suppressing snippet provides better UX.
474  if (Completion.Kind == CompletionItemKind::Function ||
475  Completion.Kind == CompletionItemKind::Method ||
476  Completion.Kind == CompletionItemKind::Constructor) {
477  // If there is a potential template argument list, drop snippet and just
478  // complete symbol name. Ideally, this could generate an edit that would
479  // paste function arguments after template argument list but it would be
480  // complicated. Example:
481  //
482  // fu^<int> -> function<int>
483  if (NextTokenKind == tok::less && Snippet->front() == '<')
484  return "";
485  // Potentially followed by argument list.
486  if (NextTokenKind == tok::l_paren) {
487  // If snippet contains template arguments we will emit them and drop
488  // function arguments. Example:
489  //
490  // fu^(42) -> function<int>(42);
491  if (Snippet->front() == '<') {
492  // Find matching '>'. Snippet->find('>') will not work in cases like
493  // template <typename T=std::vector<int>>. Hence, iterate through
494  // the snippet until the angle bracket balance reaches zero.
495  int Balance = 0;
496  size_t I = 0;
497  do {
498  if (Snippet->at(I) == '>')
499  --Balance;
500  else if (Snippet->at(I) == '<')
501  ++Balance;
502  ++I;
503  } while (Balance > 0);
504  return Snippet->substr(0, I);
505  }
506  return "";
507  }
508  }
509  if (EnableFunctionArgSnippets)
510  return *Snippet;
511 
512  // Replace argument snippets with a simplified pattern.
513  if (Snippet->empty())
514  return "";
515  if (Completion.Kind == CompletionItemKind::Function ||
516  Completion.Kind == CompletionItemKind::Method) {
517  // Functions snippets can be of 2 types:
518  // - containing only function arguments, e.g.
519  // foo(${1:int p1}, ${2:int p2});
520  // We transform this pattern to '($0)' or '()'.
521  // - template arguments and function arguments, e.g.
522  // foo<${1:class}>(${2:int p1}).
523  // We transform this pattern to '<$1>()$0' or '<$0>()'.
524 
525  bool EmptyArgs = llvm::StringRef(*Snippet).endswith("()");
526  if (Snippet->front() == '<')
527  return EmptyArgs ? "<$1>()$0" : "<$1>($0)";
528  if (Snippet->front() == '(')
529  return EmptyArgs ? "()" : "($0)";
530  return *Snippet; // Not an arg snippet?
531  }
532  // 'CompletionItemKind::Interface' matches template type aliases.
533  if (Completion.Kind == CompletionItemKind::Interface ||
534  Completion.Kind == CompletionItemKind::Class) {
535  if (Snippet->front() != '<')
536  return *Snippet; // Not an arg snippet?
537 
538  // Classes and template using aliases can only have template arguments,
539  // e.g. Foo<${1:class}>.
540  if (llvm::StringRef(*Snippet).endswith("<>"))
541  return "<>"; // can happen with defaulted template arguments.
542  return "<$0>";
543  }
544  return *Snippet;
545  }
546 
547  std::string summarizeSignature() const {
548  if (auto *Signature = onlyValue<&BundledEntry::Signature>())
549  return *Signature;
550  // All bundles are function calls.
551  return "(…)";
552  }
553 
554  // ASTCtx can be nullptr if not run with sema.
555  ASTContext *ASTCtx;
556  CodeCompletion Completion;
557  llvm::SmallVector<BundledEntry, 1> Bundled;
558  bool EnableFunctionArgSnippets;
559  // No snippets will be generated for using declarations and when the function
560  // arguments are already present.
561  bool IsUsingDeclaration;
562  tok::TokenKind NextTokenKind;
563 };
564 
565 // Determine the symbol ID for a Sema code completion result, if possible.
566 SymbolID getSymbolID(const CodeCompletionResult &R, const SourceManager &SM) {
567  switch (R.Kind) {
568  case CodeCompletionResult::RK_Declaration:
569  case CodeCompletionResult::RK_Pattern: {
570  // Computing USR caches linkage, which may change after code completion.
571  if (hasUnstableLinkage(R.Declaration))
572  return {};
573  return clang::clangd::getSymbolID(R.Declaration);
574  }
575  case CodeCompletionResult::RK_Macro:
576  return clang::clangd::getSymbolID(R.Macro->getName(), R.MacroDefInfo, SM);
577  case CodeCompletionResult::RK_Keyword:
578  return {};
579  }
580  llvm_unreachable("unknown CodeCompletionResult kind");
581 }
582 
583 // Scopes of the partial identifier we're trying to complete.
584 // It is used when we query the index for more completion results.
585 struct SpecifiedScope {
586  // The scopes we should look in, determined by Sema.
587  //
588  // If the qualifier was fully resolved, we look for completions in these
589  // scopes; if there is an unresolved part of the qualifier, it should be
590  // resolved within these scopes.
591  //
592  // Examples of qualified completion:
593  //
594  // "::vec" => {""}
595  // "using namespace std; ::vec^" => {"", "std::"}
596  // "namespace ns {using namespace std;} ns::^" => {"ns::", "std::"}
597  // "std::vec^" => {""} // "std" unresolved
598  //
599  // Examples of unqualified completion:
600  //
601  // "vec^" => {""}
602  // "using namespace std; vec^" => {"", "std::"}
603  // "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""}
604  //
605  // "" for global namespace, "ns::" for normal namespace.
606  std::vector<std::string> AccessibleScopes;
607  // The full scope qualifier as typed by the user (without the leading "::").
608  // Set if the qualifier is not fully resolved by Sema.
609  llvm::Optional<std::string> UnresolvedQualifier;
610 
611  // Construct scopes being queried in indexes. The results are deduplicated.
612  // This method format the scopes to match the index request representation.
613  std::vector<std::string> scopesForIndexQuery() {
614  std::set<std::string> Results;
615  for (llvm::StringRef AS : AccessibleScopes)
616  Results.insert(
617  (AS + (UnresolvedQualifier ? *UnresolvedQualifier : "")).str());
618  return {Results.begin(), Results.end()};
619  }
620 };
621 
622 // Get all scopes that will be queried in indexes and whether symbols from
623 // any scope is allowed. The first scope in the list is the preferred scope
624 // (e.g. enclosing namespace).
625 std::pair<std::vector<std::string>, bool>
626 getQueryScopes(CodeCompletionContext &CCContext, const Sema &CCSema,
627  const CompletionPrefix &HeuristicPrefix,
628  const CodeCompleteOptions &Opts) {
629  SpecifiedScope Scopes;
630  for (auto *Context : CCContext.getVisitedContexts()) {
631  if (isa<TranslationUnitDecl>(Context))
632  Scopes.AccessibleScopes.push_back(""); // global namespace
633  else if (isa<NamespaceDecl>(Context))
634  Scopes.AccessibleScopes.push_back(printNamespaceScope(*Context));
635  }
636 
637  const CXXScopeSpec *SemaSpecifier =
638  CCContext.getCXXScopeSpecifier().getValueOr(nullptr);
639  // Case 1: unqualified completion.
640  if (!SemaSpecifier) {
641  // Case 2 (exception): sema saw no qualifier, but there appears to be one!
642  // This can happen e.g. in incomplete macro expansions. Use heuristics.
643  if (!HeuristicPrefix.Qualifier.empty()) {
644  vlog("Sema said no scope specifier, but we saw {0} in the source code",
645  HeuristicPrefix.Qualifier);
646  StringRef SpelledSpecifier = HeuristicPrefix.Qualifier;
647  if (SpelledSpecifier.consume_front("::"))
648  Scopes.AccessibleScopes = {""};
649  Scopes.UnresolvedQualifier = std::string(SpelledSpecifier);
650  return {Scopes.scopesForIndexQuery(), false};
651  }
652  // The enclosing namespace must be first, it gets a quality boost.
653  std::vector<std::string> EnclosingAtFront;
654  std::string EnclosingScope = printNamespaceScope(*CCSema.CurContext);
655  EnclosingAtFront.push_back(EnclosingScope);
656  for (auto &S : Scopes.scopesForIndexQuery()) {
657  if (EnclosingScope != S)
658  EnclosingAtFront.push_back(std::move(S));
659  }
660  // Allow AllScopes completion as there is no explicit scope qualifier.
661  return {EnclosingAtFront, Opts.AllScopes};
662  }
663  // Case 3: sema saw and resolved a scope qualifier.
664  if (SemaSpecifier && SemaSpecifier->isValid())
665  return {Scopes.scopesForIndexQuery(), false};
666 
667  // Case 4: There was a qualifier, and Sema didn't resolve it.
668  Scopes.AccessibleScopes.push_back(""); // Make sure global scope is included.
669  llvm::StringRef SpelledSpecifier = Lexer::getSourceText(
670  CharSourceRange::getCharRange(SemaSpecifier->getRange()),
671  CCSema.SourceMgr, clang::LangOptions());
672  if (SpelledSpecifier.consume_front("::"))
673  Scopes.AccessibleScopes = {""};
674  Scopes.UnresolvedQualifier = std::string(SpelledSpecifier);
675  // Sema excludes the trailing "::".
676  if (!Scopes.UnresolvedQualifier->empty())
677  *Scopes.UnresolvedQualifier += "::";
678 
679  return {Scopes.scopesForIndexQuery(), false};
680 }
681 
682 // Should we perform index-based completion in a context of the specified kind?
683 // FIXME: consider allowing completion, but restricting the result types.
684 bool contextAllowsIndex(enum CodeCompletionContext::Kind K) {
685  switch (K) {
686  case CodeCompletionContext::CCC_TopLevel:
687  case CodeCompletionContext::CCC_ObjCInterface:
688  case CodeCompletionContext::CCC_ObjCImplementation:
689  case CodeCompletionContext::CCC_ObjCIvarList:
690  case CodeCompletionContext::CCC_ClassStructUnion:
691  case CodeCompletionContext::CCC_Statement:
692  case CodeCompletionContext::CCC_Expression:
693  case CodeCompletionContext::CCC_ObjCMessageReceiver:
694  case CodeCompletionContext::CCC_EnumTag:
695  case CodeCompletionContext::CCC_UnionTag:
696  case CodeCompletionContext::CCC_ClassOrStructTag:
697  case CodeCompletionContext::CCC_ObjCProtocolName:
698  case CodeCompletionContext::CCC_Namespace:
699  case CodeCompletionContext::CCC_Type:
700  case CodeCompletionContext::CCC_ParenthesizedExpression:
701  case CodeCompletionContext::CCC_ObjCInterfaceName:
702  case CodeCompletionContext::CCC_ObjCCategoryName:
703  case CodeCompletionContext::CCC_Symbol:
704  case CodeCompletionContext::CCC_SymbolOrNewName:
705  return true;
706  case CodeCompletionContext::CCC_OtherWithMacros:
707  case CodeCompletionContext::CCC_DotMemberAccess:
708  case CodeCompletionContext::CCC_ArrowMemberAccess:
709  case CodeCompletionContext::CCC_ObjCPropertyAccess:
710  case CodeCompletionContext::CCC_MacroName:
711  case CodeCompletionContext::CCC_MacroNameUse:
712  case CodeCompletionContext::CCC_PreprocessorExpression:
713  case CodeCompletionContext::CCC_PreprocessorDirective:
714  case CodeCompletionContext::CCC_SelectorName:
715  case CodeCompletionContext::CCC_TypeQualifiers:
716  case CodeCompletionContext::CCC_ObjCInstanceMessage:
717  case CodeCompletionContext::CCC_ObjCClassMessage:
718  case CodeCompletionContext::CCC_IncludedFile:
719  case CodeCompletionContext::CCC_Attribute:
720  // FIXME: Provide identifier based completions for the following contexts:
721  case CodeCompletionContext::CCC_Other: // Be conservative.
722  case CodeCompletionContext::CCC_NaturalLanguage:
723  case CodeCompletionContext::CCC_Recovery:
724  case CodeCompletionContext::CCC_NewName:
725  return false;
726  }
727  llvm_unreachable("unknown code completion context");
728 }
729 
730 static bool isInjectedClass(const NamedDecl &D) {
731  if (auto *R = dyn_cast_or_null<RecordDecl>(&D))
732  if (R->isInjectedClassName())
733  return true;
734  return false;
735 }
736 
737 // Some member calls are excluded because they're so rarely useful.
738 static bool isExcludedMember(const NamedDecl &D) {
739  // Destructor completion is rarely useful, and works inconsistently.
740  // (s.^ completes ~string, but s.~st^ is an error).
741  if (D.getKind() == Decl::CXXDestructor)
742  return true;
743  // Injected name may be useful for A::foo(), but who writes A::A::foo()?
744  if (isInjectedClass(D))
745  return true;
746  // Explicit calls to operators are also rare.
747  auto NameKind = D.getDeclName().getNameKind();
748  if (NameKind == DeclarationName::CXXOperatorName ||
749  NameKind == DeclarationName::CXXLiteralOperatorName ||
750  NameKind == DeclarationName::CXXConversionFunctionName)
751  return true;
752  return false;
753 }
754 
755 // The CompletionRecorder captures Sema code-complete output, including context.
756 // It filters out ignored results (but doesn't apply fuzzy-filtering yet).
757 // It doesn't do scoring or conversion to CompletionItem yet, as we want to
758 // merge with index results first.
759 // Generally the fields and methods of this object should only be used from
760 // within the callback.
761 struct CompletionRecorder : public CodeCompleteConsumer {
762  CompletionRecorder(const CodeCompleteOptions &Opts,
763  llvm::unique_function<void()> ResultsCallback)
764  : CodeCompleteConsumer(Opts.getClangCompleteOpts()),
765  CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
766  CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
767  CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) {
768  assert(this->ResultsCallback);
769  }
770 
771  std::vector<CodeCompletionResult> Results;
772  CodeCompletionContext CCContext;
773  Sema *CCSema = nullptr; // Sema that created the results.
774  // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead?
775 
776  void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context,
777  CodeCompletionResult *InResults,
778  unsigned NumResults) override final {
779  // Results from recovery mode are generally useless, and the callback after
780  // recovery (if any) is usually more interesting. To make sure we handle the
781  // future callback from sema, we just ignore all callbacks in recovery mode,
782  // as taking only results from recovery mode results in poor completion
783  // results.
784  // FIXME: in case there is no future sema completion callback after the
785  // recovery mode, we might still want to provide some results (e.g. trivial
786  // identifier-based completion).
787  if (Context.getKind() == CodeCompletionContext::CCC_Recovery) {
788  log("Code complete: Ignoring sema code complete callback with Recovery "
789  "context.");
790  return;
791  }
792  // If a callback is called without any sema result and the context does not
793  // support index-based completion, we simply skip it to give way to
794  // potential future callbacks with results.
795  if (NumResults == 0 && !contextAllowsIndex(Context.getKind()))
796  return;
797  if (CCSema) {
798  log("Multiple code complete callbacks (parser backtracked?). "
799  "Dropping results from context {0}, keeping results from {1}.",
800  getCompletionKindString(Context.getKind()),
801  getCompletionKindString(this->CCContext.getKind()));
802  return;
803  }
804  // Record the completion context.
805  CCSema = &S;
806  CCContext = Context;
807 
808  // Retain the results we might want.
809  for (unsigned I = 0; I < NumResults; ++I) {
810  auto &Result = InResults[I];
811  // Class members that are shadowed by subclasses are usually noise.
812  if (Result.Hidden && Result.Declaration &&
813  Result.Declaration->isCXXClassMember())
814  continue;
815  if (!Opts.IncludeIneligibleResults &&
816  (Result.Availability == CXAvailability_NotAvailable ||
817  Result.Availability == CXAvailability_NotAccessible))
818  continue;
819  if (Result.Declaration &&
820  !Context.getBaseType().isNull() // is this a member-access context?
821  && isExcludedMember(*Result.Declaration))
822  continue;
823  // Skip injected class name when no class scope is not explicitly set.
824  // E.g. show injected A::A in `using A::A^` but not in "A^".
825  if (Result.Declaration && !Context.getCXXScopeSpecifier().hasValue() &&
826  isInjectedClass(*Result.Declaration))
827  continue;
828  // We choose to never append '::' to completion results in clangd.
829  Result.StartsNestedNameSpecifier = false;
830  Results.push_back(Result);
831  }
832  ResultsCallback();
833  }
834 
835  CodeCompletionAllocator &getAllocator() override { return *CCAllocator; }
836  CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
837 
838  // Returns the filtering/sorting name for Result, which must be from Results.
839  // Returned string is owned by this recorder (or the AST).
840  llvm::StringRef getName(const CodeCompletionResult &Result) {
841  switch (Result.Kind) {
842  case CodeCompletionResult::RK_Declaration:
843  if (auto *ID = Result.Declaration->getIdentifier())
844  return ID->getName();
845  break;
846  case CodeCompletionResult::RK_Keyword:
847  return Result.Keyword;
848  case CodeCompletionResult::RK_Macro:
849  return Result.Macro->getName();
850  case CodeCompletionResult::RK_Pattern:
851  return Result.Pattern->getTypedText();
852  }
853  auto *CCS = codeCompletionString(Result);
854  return CCS->getTypedText();
855  }
856 
857  // Build a CodeCompletion string for R, which must be from Results.
858  // The CCS will be owned by this recorder.
859  CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) {
860  // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway.
861  return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString(
862  *CCSema, CCContext, *CCAllocator, CCTUInfo,
863  /*IncludeBriefComments=*/false);
864  }
865 
866 private:
867  CodeCompleteOptions Opts;
868  std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
869  CodeCompletionTUInfo CCTUInfo;
870  llvm::unique_function<void()> ResultsCallback;
871 };
872 
873 struct ScoredSignature {
874  // When not null, requires documentation to be requested from the index with
875  // this ID.
877  SignatureInformation Signature;
878  SignatureQualitySignals Quality;
879 };
880 
881 class SignatureHelpCollector final : public CodeCompleteConsumer {
882 public:
883  SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
884  const SymbolIndex *Index, SignatureHelp &SigHelp)
885  : CodeCompleteConsumer(CodeCompleteOpts), SigHelp(SigHelp),
886  Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
888 
889  void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
890  OverloadCandidate *Candidates,
891  unsigned NumCandidates,
892  SourceLocation OpenParLoc) override {
893  assert(!OpenParLoc.isInvalid());
894  SourceManager &SrcMgr = S.getSourceManager();
895  OpenParLoc = SrcMgr.getFileLoc(OpenParLoc);
896  if (SrcMgr.isInMainFile(OpenParLoc))
897  SigHelp.argListStart = sourceLocToPosition(SrcMgr, OpenParLoc);
898  else
899  elog("Location oustide main file in signature help: {0}",
900  OpenParLoc.printToString(SrcMgr));
901 
902  std::vector<ScoredSignature> ScoredSignatures;
903  SigHelp.signatures.reserve(NumCandidates);
904  ScoredSignatures.reserve(NumCandidates);
905  // FIXME(rwols): How can we determine the "active overload candidate"?
906  // Right now the overloaded candidates seem to be provided in a "best fit"
907  // order, so I'm not too worried about this.
908  SigHelp.activeSignature = 0;
909  assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
910  "too many arguments");
911  SigHelp.activeParameter = static_cast<int>(CurrentArg);
912  for (unsigned I = 0; I < NumCandidates; ++I) {
913  OverloadCandidate Candidate = Candidates[I];
914  // We want to avoid showing instantiated signatures, because they may be
915  // long in some cases (e.g. when 'T' is substituted with 'std::string', we
916  // would get 'std::basic_string<char>').
917  if (auto *Func = Candidate.getFunction()) {
918  if (auto *Pattern = Func->getTemplateInstantiationPattern())
919  Candidate = OverloadCandidate(Pattern);
920  }
921 
922  const auto *CCS = Candidate.CreateSignatureString(
923  CurrentArg, S, *Allocator, CCTUInfo, true);
924  assert(CCS && "Expected the CodeCompletionString to be non-null");
925  ScoredSignatures.push_back(processOverloadCandidate(
926  Candidate, *CCS,
927  Candidate.getFunction()
928  ? getDeclComment(S.getASTContext(), *Candidate.getFunction())
929  : ""));
930  }
931 
932  // Sema does not load the docs from the preamble, so we need to fetch extra
933  // docs from the index instead.
934  llvm::DenseMap<SymbolID, std::string> FetchedDocs;
935  if (Index) {
936  LookupRequest IndexRequest;
937  for (const auto &S : ScoredSignatures) {
938  if (!S.IDForDoc)
939  continue;
940  IndexRequest.IDs.insert(S.IDForDoc);
941  }
942  Index->lookup(IndexRequest, [&](const Symbol &S) {
943  if (!S.Documentation.empty())
944  FetchedDocs[S.ID] = std::string(S.Documentation);
945  });
946  log("SigHelp: requested docs for {0} symbols from the index, got {1} "
947  "symbols with non-empty docs in the response",
948  IndexRequest.IDs.size(), FetchedDocs.size());
949  }
950 
951  llvm::sort(ScoredSignatures, [](const ScoredSignature &L,
952  const ScoredSignature &R) {
953  // Ordering follows:
954  // - Less number of parameters is better.
955  // - Function is better than FunctionType which is better than
956  // Function Template.
957  // - High score is better.
958  // - Shorter signature is better.
959  // - Alphabetically smaller is better.
960  if (L.Quality.NumberOfParameters != R.Quality.NumberOfParameters)
961  return L.Quality.NumberOfParameters < R.Quality.NumberOfParameters;
962  if (L.Quality.NumberOfOptionalParameters !=
963  R.Quality.NumberOfOptionalParameters)
964  return L.Quality.NumberOfOptionalParameters <
965  R.Quality.NumberOfOptionalParameters;
966  if (L.Quality.Kind != R.Quality.Kind) {
967  using OC = CodeCompleteConsumer::OverloadCandidate;
968  switch (L.Quality.Kind) {
969  case OC::CK_Function:
970  return true;
971  case OC::CK_FunctionType:
972  return R.Quality.Kind != OC::CK_Function;
973  case OC::CK_FunctionTemplate:
974  return false;
975  }
976  llvm_unreachable("Unknown overload candidate type.");
977  }
978  if (L.Signature.label.size() != R.Signature.label.size())
979  return L.Signature.label.size() < R.Signature.label.size();
980  return L.Signature.label < R.Signature.label;
981  });
982 
983  for (auto &SS : ScoredSignatures) {
984  auto IndexDocIt =
985  SS.IDForDoc ? FetchedDocs.find(SS.IDForDoc) : FetchedDocs.end();
986  if (IndexDocIt != FetchedDocs.end())
987  SS.Signature.documentation = IndexDocIt->second;
988 
989  SigHelp.signatures.push_back(std::move(SS.Signature));
990  }
991  }
992 
993  GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
994 
995  CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
996 
997 private:
998  void processParameterChunk(llvm::StringRef ChunkText,
999  SignatureInformation &Signature) const {
1000  // (!) this is O(n), should still be fast compared to building ASTs.
1001  unsigned ParamStartOffset = lspLength(Signature.label);
1002  unsigned ParamEndOffset = ParamStartOffset + lspLength(ChunkText);
1003  // A piece of text that describes the parameter that corresponds to
1004  // the code-completion location within a function call, message send,
1005  // macro invocation, etc.
1006  Signature.label += ChunkText;
1007  ParameterInformation Info;
1008  Info.labelOffsets.emplace(ParamStartOffset, ParamEndOffset);
1009  // FIXME: only set 'labelOffsets' when all clients migrate out of it.
1010  Info.labelString = std::string(ChunkText);
1011 
1012  Signature.parameters.push_back(std::move(Info));
1013  }
1014 
1015  void processOptionalChunk(const CodeCompletionString &CCS,
1016  SignatureInformation &Signature,
1017  SignatureQualitySignals &Signal) const {
1018  for (const auto &Chunk : CCS) {
1019  switch (Chunk.Kind) {
1020  case CodeCompletionString::CK_Optional:
1021  assert(Chunk.Optional &&
1022  "Expected the optional code completion string to be non-null.");
1023  processOptionalChunk(*Chunk.Optional, Signature, Signal);
1024  break;
1025  case CodeCompletionString::CK_VerticalSpace:
1026  break;
1027  case CodeCompletionString::CK_CurrentParameter:
1028  case CodeCompletionString::CK_Placeholder:
1029  processParameterChunk(Chunk.Text, Signature);
1030  Signal.NumberOfOptionalParameters++;
1031  break;
1032  default:
1033  Signature.label += Chunk.Text;
1034  break;
1035  }
1036  }
1037  }
1038 
1039  // FIXME(ioeric): consider moving CodeCompletionString logic here to
1040  // CompletionString.h.
1041  ScoredSignature processOverloadCandidate(const OverloadCandidate &Candidate,
1042  const CodeCompletionString &CCS,
1043  llvm::StringRef DocComment) const {
1044  SignatureInformation Signature;
1045  SignatureQualitySignals Signal;
1046  const char *ReturnType = nullptr;
1047 
1048  Signature.documentation = formatDocumentation(CCS, DocComment);
1049  Signal.Kind = Candidate.getKind();
1050 
1051  for (const auto &Chunk : CCS) {
1052  switch (Chunk.Kind) {
1053  case CodeCompletionString::CK_ResultType:
1054  // A piece of text that describes the type of an entity or,
1055  // for functions and methods, the return type.
1056  assert(!ReturnType && "Unexpected CK_ResultType");
1057  ReturnType = Chunk.Text;
1058  break;
1059  case CodeCompletionString::CK_CurrentParameter:
1060  case CodeCompletionString::CK_Placeholder:
1061  processParameterChunk(Chunk.Text, Signature);
1062  Signal.NumberOfParameters++;
1063  break;
1064  case CodeCompletionString::CK_Optional: {
1065  // The rest of the parameters are defaulted/optional.
1066  assert(Chunk.Optional &&
1067  "Expected the optional code completion string to be non-null.");
1068  processOptionalChunk(*Chunk.Optional, Signature, Signal);
1069  break;
1070  }
1071  case CodeCompletionString::CK_VerticalSpace:
1072  break;
1073  default:
1074  Signature.label += Chunk.Text;
1075  break;
1076  }
1077  }
1078  if (ReturnType) {
1079  Signature.label += " -> ";
1080  Signature.label += ReturnType;
1081  }
1082  dlog("Signal for {0}: {1}", Signature, Signal);
1083  ScoredSignature Result;
1084  Result.Signature = std::move(Signature);
1085  Result.Quality = Signal;
1086  const FunctionDecl *Func = Candidate.getFunction();
1087  if (Func && Result.Signature.documentation.empty()) {
1088  // Computing USR caches linkage, which may change after code completion.
1089  if (!hasUnstableLinkage(Func))
1090  Result.IDForDoc = clangd::getSymbolID(Func);
1091  }
1092  return Result;
1093  }
1094 
1095  SignatureHelp &SigHelp;
1096  std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
1097  CodeCompletionTUInfo CCTUInfo;
1098  const SymbolIndex *Index;
1099 }; // SignatureHelpCollector
1100 
1101 struct SemaCompleteInput {
1103  size_t Offset;
1104  const PreambleData &Preamble;
1105  const llvm::Optional<PreamblePatch> Patch;
1106  const ParseInputs &ParseInput;
1107 };
1108 
1109 void loadMainFilePreambleMacros(const Preprocessor &PP,
1110  const PreambleData &Preamble) {
1111  // The ExternalPreprocessorSource has our macros, if we know where to look.
1112  // We can read all the macros using PreambleMacros->ReadDefinedMacros(),
1113  // but this includes transitively included files, so may deserialize a lot.
1114  ExternalPreprocessorSource *PreambleMacros = PP.getExternalSource();
1115  // As we have the names of the macros, we can look up their IdentifierInfo
1116  // and then use this to load just the macros we want.
1117  const auto &ITable = PP.getIdentifierTable();
1118  IdentifierInfoLookup *PreambleIdentifiers =
1119  ITable.getExternalIdentifierLookup();
1120 
1121  if (!PreambleIdentifiers || !PreambleMacros)
1122  return;
1123  for (const auto &MacroName : Preamble.Macros.Names) {
1124  if (ITable.find(MacroName.getKey()) != ITable.end())
1125  continue;
1126  if (auto *II = PreambleIdentifiers->get(MacroName.getKey()))
1127  if (II->isOutOfDate())
1128  PreambleMacros->updateOutOfDateIdentifier(*II);
1129  }
1130 }
1131 
1132 // Invokes Sema code completion on a file.
1133 // If \p Includes is set, it will be updated based on the compiler invocation.
1134 bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
1135  const clang::CodeCompleteOptions &Options,
1136  const SemaCompleteInput &Input,
1137  IncludeStructure *Includes = nullptr) {
1138  trace::Span Tracer("Sema completion");
1139 
1140  IgnoreDiagnostics IgnoreDiags;
1141  auto CI = buildCompilerInvocation(Input.ParseInput, IgnoreDiags);
1142  if (!CI) {
1143  elog("Couldn't create CompilerInvocation");
1144  return false;
1145  }
1146  auto &FrontendOpts = CI->getFrontendOpts();
1147  FrontendOpts.SkipFunctionBodies = true;
1148  // Disable typo correction in Sema.
1149  CI->getLangOpts()->SpellChecking = false;
1150  // Code completion won't trigger in delayed template bodies.
1151  // This is on-by-default in windows to allow parsing SDK headers; we're only
1152  // disabling it for the main-file (not preamble).
1153  CI->getLangOpts()->DelayedTemplateParsing = false;
1154  // Setup code completion.
1155  FrontendOpts.CodeCompleteOpts = Options;
1156  FrontendOpts.CodeCompletionAt.FileName = std::string(Input.FileName);
1157  std::tie(FrontendOpts.CodeCompletionAt.Line,
1158  FrontendOpts.CodeCompletionAt.Column) =
1159  offsetToClangLineColumn(Input.ParseInput.Contents, Input.Offset);
1160 
1161  std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1162  llvm::MemoryBuffer::getMemBuffer(Input.ParseInput.Contents,
1163  Input.FileName);
1164  // The diagnostic options must be set before creating a CompilerInstance.
1165  CI->getDiagnosticOpts().IgnoreWarnings = true;
1166  // We reuse the preamble whether it's valid or not. This is a
1167  // correctness/performance tradeoff: building without a preamble is slow, and
1168  // completion is latency-sensitive.
1169  // However, if we're completing *inside* the preamble section of the draft,
1170  // overriding the preamble will break sema completion. Fortunately we can just
1171  // skip all includes in this case; these completions are really simple.
1172  PreambleBounds PreambleRegion =
1173  ComputePreambleBounds(*CI->getLangOpts(), *ContentsBuffer, 0);
1174  bool CompletingInPreamble = Input.Offset < PreambleRegion.Size ||
1175  (!PreambleRegion.PreambleEndsAtStartOfLine &&
1176  Input.Offset == PreambleRegion.Size);
1177  if (Input.Patch)
1178  Input.Patch->apply(*CI);
1179  // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
1180  // the remapped buffers do not get freed.
1181  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
1182  Input.ParseInput.TFS->view(Input.ParseInput.CompileCommand.Directory);
1183  if (Input.Preamble.StatCache)
1184  VFS = Input.Preamble.StatCache->getConsumingFS(std::move(VFS));
1185  auto Clang = prepareCompilerInstance(
1186  std::move(CI), !CompletingInPreamble ? &Input.Preamble.Preamble : nullptr,
1187  std::move(ContentsBuffer), std::move(VFS), IgnoreDiags);
1188  Clang->getPreprocessorOpts().SingleFileParseMode = CompletingInPreamble;
1189  Clang->setCodeCompletionConsumer(Consumer.release());
1190 
1192  if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
1193  log("BeginSourceFile() failed when running codeComplete for {0}",
1194  Input.FileName);
1195  return false;
1196  }
1197  // Macros can be defined within the preamble region of the main file.
1198  // They don't fall nicely into our index/Sema dichotomy:
1199  // - they're not indexed for completion (they're not available across files)
1200  // - but Sema code complete won't see them: as part of the preamble, they're
1201  // deserialized only when mentioned.
1202  // Force them to be deserialized so SemaCodeComplete sees them.
1203  loadMainFilePreambleMacros(Clang->getPreprocessor(), Input.Preamble);
1204  if (Includes)
1205  Clang->getPreprocessor().addPPCallbacks(
1206  collectIncludeStructureCallback(Clang->getSourceManager(), Includes));
1207  if (llvm::Error Err = Action.Execute()) {
1208  log("Execute() failed when running codeComplete for {0}: {1}",
1209  Input.FileName, toString(std::move(Err)));
1210  return false;
1211  }
1212  Action.EndSourceFile();
1213 
1214  return true;
1215 }
1216 
1217 // Should we allow index completions in the specified context?
1218 bool allowIndex(CodeCompletionContext &CC) {
1219  if (!contextAllowsIndex(CC.getKind()))
1220  return false;
1221  // We also avoid ClassName::bar (but allow namespace::bar).
1222  auto Scope = CC.getCXXScopeSpecifier();
1223  if (!Scope)
1224  return true;
1225  NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
1226  if (!NameSpec)
1227  return true;
1228  // We only query the index when qualifier is a namespace.
1229  // If it's a class, we rely solely on sema completions.
1230  switch (NameSpec->getKind()) {
1231  case NestedNameSpecifier::Global:
1232  case NestedNameSpecifier::Namespace:
1233  case NestedNameSpecifier::NamespaceAlias:
1234  return true;
1235  case NestedNameSpecifier::Super:
1236  case NestedNameSpecifier::TypeSpec:
1237  case NestedNameSpecifier::TypeSpecWithTemplate:
1238  // Unresolved inside a template.
1239  case NestedNameSpecifier::Identifier:
1240  return false;
1241  }
1242  llvm_unreachable("invalid NestedNameSpecifier kind");
1243 }
1244 
1245 std::future<SymbolSlab> startAsyncFuzzyFind(const SymbolIndex &Index,
1246  const FuzzyFindRequest &Req) {
1247  return runAsync<SymbolSlab>([&Index, Req]() {
1248  trace::Span Tracer("Async fuzzyFind");
1249  SymbolSlab::Builder Syms;
1250  Index.fuzzyFind(Req, [&Syms](const Symbol &Sym) { Syms.insert(Sym); });
1251  return std::move(Syms).build();
1252  });
1253 }
1254 
1255 // Creates a `FuzzyFindRequest` based on the cached index request from the
1256 // last completion, if any, and the speculated completion filter text in the
1257 // source code.
1258 FuzzyFindRequest speculativeFuzzyFindRequestForCompletion(
1259  FuzzyFindRequest CachedReq, const CompletionPrefix &HeuristicPrefix) {
1260  CachedReq.Query = std::string(HeuristicPrefix.Name);
1261  return CachedReq;
1262 }
1263 
1264 // Runs Sema-based (AST) and Index-based completion, returns merged results.
1265 //
1266 // There are a few tricky considerations:
1267 // - the AST provides information needed for the index query (e.g. which
1268 // namespaces to search in). So Sema must start first.
1269 // - we only want to return the top results (Opts.Limit).
1270 // Building CompletionItems for everything else is wasteful, so we want to
1271 // preserve the "native" format until we're done with scoring.
1272 // - the data underlying Sema completion items is owned by the AST and various
1273 // other arenas, which must stay alive for us to build CompletionItems.
1274 // - we may get duplicate results from Sema and the Index, we need to merge.
1275 //
1276 // So we start Sema completion first, and do all our work in its callback.
1277 // We use the Sema context information to query the index.
1278 // Then we merge the two result sets, producing items that are Sema/Index/Both.
1279 // These items are scored, and the top N are synthesized into the LSP response.
1280 // Finally, we can clean up the data structures created by Sema completion.
1281 //
1282 // Main collaborators are:
1283 // - semaCodeComplete sets up the compiler machinery to run code completion.
1284 // - CompletionRecorder captures Sema completion results, including context.
1285 // - SymbolIndex (Opts.Index) provides index completion results as Symbols
1286 // - CompletionCandidates are the result of merging Sema and Index results.
1287 // Each candidate points to an underlying CodeCompletionResult (Sema), a
1288 // Symbol (Index), or both. It computes the result quality score.
1289 // CompletionCandidate also does conversion to CompletionItem (at the end).
1290 // - FuzzyMatcher scores how the candidate matches the partial identifier.
1291 // This score is combined with the result quality score for the final score.
1292 // - TopN determines the results with the best score.
1293 class CodeCompleteFlow {
1294  PathRef FileName;
1295  IncludeStructure Includes; // Complete once the compiler runs.
1296  SpeculativeFuzzyFind *SpecFuzzyFind; // Can be nullptr.
1297  const CodeCompleteOptions &Opts;
1298 
1299  // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup.
1300  CompletionRecorder *Recorder = nullptr;
1301  CodeCompletionContext::Kind CCContextKind = CodeCompletionContext::CCC_Other;
1302  bool IsUsingDeclaration = false;
1303  // The snippets will not be generated if the token following completion
1304  // location is an opening parenthesis (tok::l_paren) because this would add
1305  // extra parenthesis.
1306  tok::TokenKind NextTokenKind = tok::eof;
1307  // Counters for logging.
1308  int NSema = 0, NIndex = 0, NSemaAndIndex = 0, NIdent = 0;
1309  bool Incomplete = false; // Would more be available with a higher limit?
1310  CompletionPrefix HeuristicPrefix;
1311  llvm::Optional<FuzzyMatcher> Filter; // Initialized once Sema runs.
1312  Range ReplacedRange;
1313  std::vector<std::string> QueryScopes; // Initialized once Sema runs.
1314  // Initialized once QueryScopes is initialized, if there are scopes.
1315  llvm::Optional<ScopeDistance> ScopeProximity;
1316  llvm::Optional<OpaqueType> PreferredType; // Initialized once Sema runs.
1317  // Whether to query symbols from any scope. Initialized once Sema runs.
1318  bool AllScopes = false;
1319  llvm::StringSet<> ContextWords;
1320  // Include-insertion and proximity scoring rely on the include structure.
1321  // This is available after Sema has run.
1322  llvm::Optional<IncludeInserter> Inserter; // Available during runWithSema.
1323  llvm::Optional<URIDistance> FileProximity; // Initialized once Sema runs.
1324  /// Speculative request based on the cached request and the filter text before
1325  /// the cursor.
1326  /// Initialized right before sema run. This is only set if `SpecFuzzyFind` is
1327  /// set and contains a cached request.
1328  llvm::Optional<FuzzyFindRequest> SpecReq;
1329 
1330 public:
1331  // A CodeCompleteFlow object is only useful for calling run() exactly once.
1332  CodeCompleteFlow(PathRef FileName, const IncludeStructure &Includes,
1333  SpeculativeFuzzyFind *SpecFuzzyFind,
1334  const CodeCompleteOptions &Opts)
1335  : FileName(FileName), Includes(Includes), SpecFuzzyFind(SpecFuzzyFind),
1336  Opts(Opts) {}
1337 
1338  CodeCompleteResult run(const SemaCompleteInput &SemaCCInput) && {
1339  trace::Span Tracer("CodeCompleteFlow");
1340  HeuristicPrefix = guessCompletionPrefix(SemaCCInput.ParseInput.Contents,
1341  SemaCCInput.Offset);
1342  populateContextWords(SemaCCInput.ParseInput.Contents);
1343  if (Opts.Index && SpecFuzzyFind && SpecFuzzyFind->CachedReq.hasValue()) {
1344  assert(!SpecFuzzyFind->Result.valid());
1345  SpecReq = speculativeFuzzyFindRequestForCompletion(
1346  *SpecFuzzyFind->CachedReq, HeuristicPrefix);
1347  SpecFuzzyFind->Result = startAsyncFuzzyFind(*Opts.Index, *SpecReq);
1348  }
1349 
1350  // We run Sema code completion first. It builds an AST and calculates:
1351  // - completion results based on the AST.
1352  // - partial identifier and context. We need these for the index query.
1353  CodeCompleteResult Output;
1354  auto RecorderOwner = std::make_unique<CompletionRecorder>(Opts, [&]() {
1355  assert(Recorder && "Recorder is not set");
1356  CCContextKind = Recorder->CCContext.getKind();
1357  IsUsingDeclaration = Recorder->CCContext.isUsingDeclaration();
1358  auto Style = getFormatStyleForFile(SemaCCInput.FileName,
1359  SemaCCInput.ParseInput.Contents,
1360  *SemaCCInput.ParseInput.TFS);
1361  const auto NextToken = Lexer::findNextToken(
1362  Recorder->CCSema->getPreprocessor().getCodeCompletionLoc(),
1363  Recorder->CCSema->getSourceManager(), Recorder->CCSema->LangOpts);
1364  if (NextToken)
1365  NextTokenKind = NextToken->getKind();
1366  // If preprocessor was run, inclusions from preprocessor callback should
1367  // already be added to Includes.
1368  Inserter.emplace(
1369  SemaCCInput.FileName, SemaCCInput.ParseInput.Contents, Style,
1370  SemaCCInput.ParseInput.CompileCommand.Directory,
1371  &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo());
1372  for (const auto &Inc : Includes.MainFileIncludes)
1373  Inserter->addExisting(Inc);
1374 
1375  // Most of the cost of file proximity is in initializing the FileDistance
1376  // structures based on the observed includes, once per query. Conceptually
1377  // that happens here (though the per-URI-scheme initialization is lazy).
1378  // The per-result proximity scoring is (amortized) very cheap.
1379  FileDistanceOptions ProxOpts{}; // Use defaults.
1380  const auto &SM = Recorder->CCSema->getSourceManager();
1381  llvm::StringMap<SourceParams> ProxSources;
1382  for (auto &Entry : Includes.includeDepth(
1383  SM.getFileEntryForID(SM.getMainFileID())->getName())) {
1384  auto &Source = ProxSources[Entry.getKey()];
1385  Source.Cost = Entry.getValue() * ProxOpts.IncludeCost;
1386  // Symbols near our transitive includes are good, but only consider
1387  // things in the same directory or below it. Otherwise there can be
1388  // many false positives.
1389  if (Entry.getValue() > 0)
1390  Source.MaxUpTraversals = 1;
1391  }
1392  FileProximity.emplace(ProxSources, ProxOpts);
1393 
1394  Output = runWithSema();
1395  Inserter.reset(); // Make sure this doesn't out-live Clang.
1396  SPAN_ATTACH(Tracer, "sema_completion_kind",
1397  getCompletionKindString(CCContextKind));
1398  log("Code complete: sema context {0}, query scopes [{1}] (AnyScope={2}), "
1399  "expected type {3}{4}",
1400  getCompletionKindString(CCContextKind),
1401  llvm::join(QueryScopes.begin(), QueryScopes.end(), ","), AllScopes,
1402  PreferredType ? Recorder->CCContext.getPreferredType().getAsString()
1403  : "<none>",
1404  IsUsingDeclaration ? ", inside using declaration" : "");
1405  });
1406 
1407  Recorder = RecorderOwner.get();
1408 
1409  semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
1410  SemaCCInput, &Includes);
1411  logResults(Output, Tracer);
1412  return Output;
1413  }
1414 
1415  void logResults(const CodeCompleteResult &Output, const trace::Span &Tracer) {
1416  SPAN_ATTACH(Tracer, "sema_results", NSema);
1417  SPAN_ATTACH(Tracer, "index_results", NIndex);
1418  SPAN_ATTACH(Tracer, "merged_results", NSemaAndIndex);
1419  SPAN_ATTACH(Tracer, "identifier_results", NIdent);
1420  SPAN_ATTACH(Tracer, "returned_results", int64_t(Output.Completions.size()));
1421  SPAN_ATTACH(Tracer, "incomplete", Output.HasMore);
1422  log("Code complete: {0} results from Sema, {1} from Index, "
1423  "{2} matched, {3} from identifiers, {4} returned{5}.",
1424  NSema, NIndex, NSemaAndIndex, NIdent, Output.Completions.size(),
1425  Output.HasMore ? " (incomplete)" : "");
1426  assert(!Opts.Limit || Output.Completions.size() <= Opts.Limit);
1427  // We don't assert that isIncomplete means we hit a limit.
1428  // Indexes may choose to impose their own limits even if we don't have one.
1429  }
1430 
1431  CodeCompleteResult runWithoutSema(llvm::StringRef Content, size_t Offset,
1432  const ThreadsafeFS &TFS) && {
1433  trace::Span Tracer("CodeCompleteWithoutSema");
1434  // Fill in fields normally set by runWithSema()
1435  HeuristicPrefix = guessCompletionPrefix(Content, Offset);
1436  populateContextWords(Content);
1437  CCContextKind = CodeCompletionContext::CCC_Recovery;
1438  IsUsingDeclaration = false;
1439  Filter = FuzzyMatcher(HeuristicPrefix.Name);
1440  auto Pos = offsetToPosition(Content, Offset);
1441  ReplacedRange.start = ReplacedRange.end = Pos;
1442  ReplacedRange.start.character -= HeuristicPrefix.Name.size();
1443 
1444  llvm::StringMap<SourceParams> ProxSources;
1445  ProxSources[FileName].Cost = 0;
1446  FileProximity.emplace(ProxSources);
1447 
1448  auto Style = getFormatStyleForFile(FileName, Content, TFS);
1449  // This will only insert verbatim headers.
1450  Inserter.emplace(FileName, Content, Style,
1451  /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr);
1452 
1453  auto Identifiers = collectIdentifiers(Content, Style);
1454  std::vector<RawIdentifier> IdentifierResults;
1455  for (const auto &IDAndCount : Identifiers) {
1456  RawIdentifier ID;
1457  ID.Name = IDAndCount.first();
1458  ID.References = IDAndCount.second;
1459  // Avoid treating typed filter as an identifier.
1460  if (ID.Name == HeuristicPrefix.Name)
1461  --ID.References;
1462  if (ID.References > 0)
1463  IdentifierResults.push_back(std::move(ID));
1464  }
1465 
1466  // Simplified version of getQueryScopes():
1467  // - accessible scopes are determined heuristically.
1468  // - all-scopes query if no qualifier was typed (and it's allowed).
1469  SpecifiedScope Scopes;
1470  Scopes.AccessibleScopes = visibleNamespaces(
1471  Content.take_front(Offset), format::getFormattingLangOpts(Style));
1472  for (std::string &S : Scopes.AccessibleScopes)
1473  if (!S.empty())
1474  S.append("::"); // visibleNamespaces doesn't include trailing ::.
1475  if (HeuristicPrefix.Qualifier.empty())
1476  AllScopes = Opts.AllScopes;
1477  else if (HeuristicPrefix.Qualifier.startswith("::")) {
1478  Scopes.AccessibleScopes = {""};
1479  Scopes.UnresolvedQualifier =
1480  std::string(HeuristicPrefix.Qualifier.drop_front(2));
1481  } else
1482  Scopes.UnresolvedQualifier = std::string(HeuristicPrefix.Qualifier);
1483  // First scope is the (modified) enclosing scope.
1484  QueryScopes = Scopes.scopesForIndexQuery();
1485  ScopeProximity.emplace(QueryScopes);
1486 
1487  SymbolSlab IndexResults = Opts.Index ? queryIndex() : SymbolSlab();
1488 
1489  CodeCompleteResult Output = toCodeCompleteResult(mergeResults(
1490  /*SemaResults=*/{}, IndexResults, IdentifierResults));
1491  Output.RanParser = false;
1492  logResults(Output, Tracer);
1493  return Output;
1494  }
1495 
1496 private:
1497  void populateContextWords(llvm::StringRef Content) {
1498  // Take last 3 lines before the completion point.
1499  unsigned RangeEnd = HeuristicPrefix.Qualifier.begin() - Content.data(),
1500  RangeBegin = RangeEnd;
1501  for (size_t I = 0; I < 3 && RangeBegin > 0; ++I) {
1502  auto PrevNL = Content.rfind('\n', RangeBegin);
1503  if (PrevNL == StringRef::npos) {
1504  RangeBegin = 0;
1505  break;
1506  }
1507  RangeBegin = PrevNL;
1508  }
1509 
1510  ContextWords = collectWords(Content.slice(RangeBegin, RangeEnd));
1511  dlog("Completion context words: {0}",
1512  llvm::join(ContextWords.keys(), ", "));
1513  }
1514 
1515  // This is called by run() once Sema code completion is done, but before the
1516  // Sema data structures are torn down. It does all the real work.
1517  CodeCompleteResult runWithSema() {
1518  const auto &CodeCompletionRange = CharSourceRange::getCharRange(
1519  Recorder->CCSema->getPreprocessor().getCodeCompletionTokenRange());
1520  // When we are getting completions with an empty identifier, for example
1521  // std::vector<int> asdf;
1522  // asdf.^;
1523  // Then the range will be invalid and we will be doing insertion, use
1524  // current cursor position in such cases as range.
1525  if (CodeCompletionRange.isValid()) {
1526  ReplacedRange = halfOpenToRange(Recorder->CCSema->getSourceManager(),
1527  CodeCompletionRange);
1528  } else {
1529  const auto &Pos = sourceLocToPosition(
1530  Recorder->CCSema->getSourceManager(),
1531  Recorder->CCSema->getPreprocessor().getCodeCompletionLoc());
1532  ReplacedRange.start = ReplacedRange.end = Pos;
1533  }
1534  Filter = FuzzyMatcher(
1535  Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
1536  std::tie(QueryScopes, AllScopes) = getQueryScopes(
1537  Recorder->CCContext, *Recorder->CCSema, HeuristicPrefix, Opts);
1538  if (!QueryScopes.empty())
1539  ScopeProximity.emplace(QueryScopes);
1540  PreferredType =
1541  OpaqueType::fromType(Recorder->CCSema->getASTContext(),
1542  Recorder->CCContext.getPreferredType());
1543  // Sema provides the needed context to query the index.
1544  // FIXME: in addition to querying for extra/overlapping symbols, we should
1545  // explicitly request symbols corresponding to Sema results.
1546  // We can use their signals even if the index can't suggest them.
1547  // We must copy index results to preserve them, but there are at most Limit.
1548  auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
1549  ? queryIndex()
1550  : SymbolSlab();
1551  trace::Span Tracer("Populate CodeCompleteResult");
1552  // Merge Sema and Index results, score them, and pick the winners.
1553  auto Top =
1554  mergeResults(Recorder->Results, IndexResults, /*Identifiers*/ {});
1555  return toCodeCompleteResult(Top);
1556  }
1557 
1558  CodeCompleteResult
1559  toCodeCompleteResult(const std::vector<ScoredBundle> &Scored) {
1560  CodeCompleteResult Output;
1561 
1562  // Convert the results to final form, assembling the expensive strings.
1563  for (auto &C : Scored) {
1564  Output.Completions.push_back(toCodeCompletion(C.first));
1565  Output.Completions.back().Score = C.second;
1566  Output.Completions.back().CompletionTokenRange = ReplacedRange;
1567  }
1568  Output.HasMore = Incomplete;
1569  Output.Context = CCContextKind;
1570  Output.CompletionRange = ReplacedRange;
1571  return Output;
1572  }
1573 
1574  SymbolSlab queryIndex() {
1575  trace::Span Tracer("Query index");
1576  SPAN_ATTACH(Tracer, "limit", int64_t(Opts.Limit));
1577 
1578  // Build the query.
1579  FuzzyFindRequest Req;
1580  if (Opts.Limit)
1581  Req.Limit = Opts.Limit;
1582  Req.Query = std::string(Filter->pattern());
1583  Req.RestrictForCodeCompletion = true;
1584  Req.Scopes = QueryScopes;
1585  Req.AnyScope = AllScopes;
1586  // FIXME: we should send multiple weighted paths here.
1587  Req.ProximityPaths.push_back(std::string(FileName));
1588  if (PreferredType)
1589  Req.PreferredTypes.push_back(std::string(PreferredType->raw()));
1590  vlog("Code complete: fuzzyFind({0:2})", toJSON(Req));
1591 
1592  if (SpecFuzzyFind)
1593  SpecFuzzyFind->NewReq = Req;
1594  if (SpecFuzzyFind && SpecFuzzyFind->Result.valid() && (*SpecReq == Req)) {
1595  vlog("Code complete: speculative fuzzy request matches the actual index "
1596  "request. Waiting for the speculative index results.");
1597  SPAN_ATTACH(Tracer, "Speculative results", true);
1598 
1599  trace::Span WaitSpec("Wait speculative results");
1600  return SpecFuzzyFind->Result.get();
1601  }
1602 
1603  SPAN_ATTACH(Tracer, "Speculative results", false);
1604 
1605  // Run the query against the index.
1606  SymbolSlab::Builder ResultsBuilder;
1607  if (Opts.Index->fuzzyFind(
1608  Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); }))
1609  Incomplete = true;
1610  return std::move(ResultsBuilder).build();
1611  }
1612 
1613  // Merges Sema and Index results where possible, to form CompletionCandidates.
1614  // \p Identifiers is raw identifiers that can also be completion candidates.
1615  // Identifiers are not merged with results from index or sema.
1616  // Groups overloads if desired, to form CompletionCandidate::Bundles. The
1617  // bundles are scored and top results are returned, best to worst.
1618  std::vector<ScoredBundle>
1619  mergeResults(const std::vector<CodeCompletionResult> &SemaResults,
1620  const SymbolSlab &IndexResults,
1621  const std::vector<RawIdentifier> &IdentifierResults) {
1622  trace::Span Tracer("Merge and score results");
1623  std::vector<CompletionCandidate::Bundle> Bundles;
1624  llvm::DenseMap<size_t, size_t> BundleLookup;
1625  auto AddToBundles = [&](const CodeCompletionResult *SemaResult,
1626  const Symbol *IndexResult,
1627  const RawIdentifier *IdentifierResult) {
1628  CompletionCandidate C;
1629  C.SemaResult = SemaResult;
1630  C.IndexResult = IndexResult;
1631  C.IdentifierResult = IdentifierResult;
1632  if (C.IndexResult) {
1633  C.Name = IndexResult->Name;
1634  C.RankedIncludeHeaders = getRankedIncludes(*C.IndexResult);
1635  } else if (C.SemaResult) {
1636  C.Name = Recorder->getName(*SemaResult);
1637  } else {
1638  assert(IdentifierResult);
1639  C.Name = IdentifierResult->Name;
1640  }
1641  if (auto OverloadSet = C.overloadSet(
1642  Opts, FileName, Inserter ? Inserter.getPointer() : nullptr)) {
1643  auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1644  if (Ret.second)
1645  Bundles.emplace_back();
1646  Bundles[Ret.first->second].push_back(std::move(C));
1647  } else {
1648  Bundles.emplace_back();
1649  Bundles.back().push_back(std::move(C));
1650  }
1651  };
1652  llvm::DenseSet<const Symbol *> UsedIndexResults;
1653  auto CorrespondingIndexResult =
1654  [&](const CodeCompletionResult &SemaResult) -> const Symbol * {
1655  if (auto SymID =
1656  getSymbolID(SemaResult, Recorder->CCSema->getSourceManager())) {
1657  auto I = IndexResults.find(SymID);
1658  if (I != IndexResults.end()) {
1659  UsedIndexResults.insert(&*I);
1660  return &*I;
1661  }
1662  }
1663  return nullptr;
1664  };
1665  // Emit all Sema results, merging them with Index results if possible.
1666  for (auto &SemaResult : SemaResults)
1667  AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult), nullptr);
1668  // Now emit any Index-only results.
1669  for (const auto &IndexResult : IndexResults) {
1670  if (UsedIndexResults.count(&IndexResult))
1671  continue;
1672  AddToBundles(/*SemaResult=*/nullptr, &IndexResult, nullptr);
1673  }
1674  // Emit identifier results.
1675  for (const auto &Ident : IdentifierResults)
1676  AddToBundles(/*SemaResult=*/nullptr, /*IndexResult=*/nullptr, &Ident);
1677  // We only keep the best N results at any time, in "native" format.
1678  TopN<ScoredBundle, ScoredBundleGreater> Top(
1679  Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1680  for (auto &Bundle : Bundles)
1681  addCandidate(Top, std::move(Bundle));
1682  return std::move(Top).items();
1683  }
1684 
1685  llvm::Optional<float> fuzzyScore(const CompletionCandidate &C) {
1686  // Macros can be very spammy, so we only support prefix completion.
1687  if (((C.SemaResult &&
1688  C.SemaResult->Kind == CodeCompletionResult::RK_Macro) ||
1689  (C.IndexResult &&
1690  C.IndexResult->SymInfo.Kind == index::SymbolKind::Macro)) &&
1691  !C.Name.startswith_insensitive(Filter->pattern()))
1692  return None;
1693  return Filter->match(C.Name);
1694  }
1695 
1696  CodeCompletion::Scores
1697  evaluateCompletion(const SymbolQualitySignals &Quality,
1698  const SymbolRelevanceSignals &Relevance) {
1700  CodeCompletion::Scores Scores;
1701  switch (Opts.RankingModel) {
1702  case RM::Heuristics:
1703  Scores.Quality = Quality.evaluateHeuristics();
1704  Scores.Relevance = Relevance.evaluateHeuristics();
1705  Scores.Total =
1706  evaluateSymbolAndRelevance(Scores.Quality, Scores.Relevance);
1707  // NameMatch is in fact a multiplier on total score, so rescoring is
1708  // sound.
1709  Scores.ExcludingName =
1710  Relevance.NameMatch > std::numeric_limits<float>::epsilon()
1711  ? Scores.Total / Relevance.NameMatch
1712  : Scores.Quality;
1713  return Scores;
1714 
1715  case RM::DecisionForest:
1716  DecisionForestScores DFScores = Opts.DecisionForestScorer(
1717  Quality, Relevance, Opts.DecisionForestBase);
1718  Scores.ExcludingName = DFScores.ExcludingName;
1719  Scores.Total = DFScores.Total;
1720  return Scores;
1721  }
1722  llvm_unreachable("Unhandled CodeCompletion ranking model.");
1723  }
1724 
1725  // Scores a candidate and adds it to the TopN structure.
1726  void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
1727  CompletionCandidate::Bundle Bundle) {
1728  SymbolQualitySignals Quality;
1729  SymbolRelevanceSignals Relevance;
1730  Relevance.Context = CCContextKind;
1731  Relevance.Name = Bundle.front().Name;
1732  Relevance.FilterLength = HeuristicPrefix.Name.size();
1733  Relevance.Query = SymbolRelevanceSignals::CodeComplete;
1734  Relevance.FileProximityMatch = FileProximity.getPointer();
1735  if (ScopeProximity)
1736  Relevance.ScopeProximityMatch = ScopeProximity.getPointer();
1737  if (PreferredType)
1738  Relevance.HadContextType = true;
1739  Relevance.ContextWords = &ContextWords;
1740  Relevance.MainFileSignals = Opts.MainFileSignals;
1741 
1742  auto &First = Bundle.front();
1743  if (auto FuzzyScore = fuzzyScore(First))
1744  Relevance.NameMatch = *FuzzyScore;
1745  else
1746  return;
1748  bool FromIndex = false;
1749  for (const auto &Candidate : Bundle) {
1750  if (Candidate.IndexResult) {
1751  Quality.merge(*Candidate.IndexResult);
1752  Relevance.merge(*Candidate.IndexResult);
1753  Origin |= Candidate.IndexResult->Origin;
1754  FromIndex = true;
1755  if (!Candidate.IndexResult->Type.empty())
1756  Relevance.HadSymbolType |= true;
1757  if (PreferredType &&
1758  PreferredType->raw() == Candidate.IndexResult->Type) {
1759  Relevance.TypeMatchesPreferred = true;
1760  }
1761  }
1762  if (Candidate.SemaResult) {
1763  Quality.merge(*Candidate.SemaResult);
1764  Relevance.merge(*Candidate.SemaResult);
1765  if (PreferredType) {
1766  if (auto CompletionType = OpaqueType::fromCompletionResult(
1767  Recorder->CCSema->getASTContext(), *Candidate.SemaResult)) {
1768  Relevance.HadSymbolType |= true;
1769  if (PreferredType == CompletionType)
1770  Relevance.TypeMatchesPreferred = true;
1771  }
1772  }
1773  Origin |= SymbolOrigin::AST;
1774  }
1775  if (Candidate.IdentifierResult) {
1776  Quality.References = Candidate.IdentifierResult->References;
1777  Relevance.Scope = SymbolRelevanceSignals::FileScope;
1778  Origin |= SymbolOrigin::Identifier;
1779  }
1780  }
1781 
1782  CodeCompletion::Scores Scores = evaluateCompletion(Quality, Relevance);
1783  if (Opts.RecordCCResult)
1784  Opts.RecordCCResult(toCodeCompletion(Bundle), Quality, Relevance,
1785  Scores.Total);
1786 
1787  dlog("CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First.Name,
1788  llvm::to_string(Origin), Scores.Total, llvm::to_string(Quality),
1789  llvm::to_string(Relevance));
1790 
1791  NSema += bool(Origin & SymbolOrigin::AST);
1792  NIndex += FromIndex;
1793  NSemaAndIndex += bool(Origin & SymbolOrigin::AST) && FromIndex;
1794  NIdent += bool(Origin & SymbolOrigin::Identifier);
1795  if (Candidates.push({std::move(Bundle), Scores}))
1796  Incomplete = true;
1797  }
1798 
1799  CodeCompletion toCodeCompletion(const CompletionCandidate::Bundle &Bundle) {
1800  llvm::Optional<CodeCompletionBuilder> Builder;
1801  for (const auto &Item : Bundle) {
1802  CodeCompletionString *SemaCCS =
1803  Item.SemaResult ? Recorder->codeCompletionString(*Item.SemaResult)
1804  : nullptr;
1805  if (!Builder)
1806  Builder.emplace(Recorder ? &Recorder->CCSema->getASTContext() : nullptr,
1807  Item, SemaCCS, QueryScopes, *Inserter, FileName,
1808  CCContextKind, Opts, IsUsingDeclaration, NextTokenKind);
1809  else
1810  Builder->add(Item, SemaCCS);
1811  }
1812  return Builder->build();
1813  }
1814 };
1815 
1816 } // namespace
1817 
1818 clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const {
1819  clang::CodeCompleteOptions Result;
1820  Result.IncludeCodePatterns = EnableSnippets;
1821  Result.IncludeMacros = true;
1822  Result.IncludeGlobals = true;
1823  // We choose to include full comments and not do doxygen parsing in
1824  // completion.
1825  // FIXME: ideally, we should support doxygen in some form, e.g. do markdown
1826  // formatting of the comments.
1827  Result.IncludeBriefComments = false;
1828 
1829  // When an is used, Sema is responsible for completing the main file,
1830  // the index can provide results from the preamble.
1831  // Tell Sema not to deserialize the preamble to look for results.
1832  Result.LoadExternal = !Index;
1833  Result.IncludeFixIts = IncludeFixIts;
1834 
1835  return Result;
1836 }
1837 
1838 CompletionPrefix guessCompletionPrefix(llvm::StringRef Content,
1839  unsigned Offset) {
1840  assert(Offset <= Content.size());
1841  StringRef Rest = Content.take_front(Offset);
1842  CompletionPrefix Result;
1843 
1844  // Consume the unqualified name. We only handle ASCII characters.
1845  // isAsciiIdentifierContinue will let us match "0invalid", but we don't mind.
1846  while (!Rest.empty() && isAsciiIdentifierContinue(Rest.back()))
1847  Rest = Rest.drop_back();
1848  Result.Name = Content.slice(Rest.size(), Offset);
1849 
1850  // Consume qualifiers.
1851  while (Rest.consume_back("::") && !Rest.endswith(":")) // reject ::::
1852  while (!Rest.empty() && isAsciiIdentifierContinue(Rest.back()))
1853  Rest = Rest.drop_back();
1854  Result.Qualifier =
1855  Content.slice(Rest.size(), Result.Name.begin() - Content.begin());
1856 
1857  return Result;
1858 }
1859 
1861  const PreambleData *Preamble,
1862  const ParseInputs &ParseInput,
1863  CodeCompleteOptions Opts,
1864  SpeculativeFuzzyFind *SpecFuzzyFind) {
1865  auto Offset = positionToOffset(ParseInput.Contents, Pos);
1866  if (!Offset) {
1867  elog("Code completion position was invalid {0}", Offset.takeError());
1868  return CodeCompleteResult();
1869  }
1870  auto Flow = CodeCompleteFlow(
1871  FileName, Preamble ? Preamble->Includes : IncludeStructure(),
1872  SpecFuzzyFind, Opts);
1873  return (!Preamble || Opts.RunParser == CodeCompleteOptions::NeverParse)
1874  ? std::move(Flow).runWithoutSema(ParseInput.Contents, *Offset,
1875  *ParseInput.TFS)
1876  : std::move(Flow).run({FileName, *Offset, *Preamble,
1877  /*PreamblePatch=*/
1878  PreamblePatch::createMacroPatch(
1880  ParseInput});
1881 }
1882 
1884  const PreambleData &Preamble,
1885  const ParseInputs &ParseInput) {
1886  auto Offset = positionToOffset(ParseInput.Contents, Pos);
1887  if (!Offset) {
1888  elog("Signature help position was invalid {0}", Offset.takeError());
1889  return SignatureHelp();
1890  }
1891  SignatureHelp Result;
1892  clang::CodeCompleteOptions Options;
1893  Options.IncludeGlobals = false;
1894  Options.IncludeMacros = false;
1895  Options.IncludeCodePatterns = false;
1896  Options.IncludeBriefComments = false;
1897  semaCodeComplete(
1898  std::make_unique<SignatureHelpCollector>(Options, ParseInput.Index,
1899  Result),
1900  Options,
1901  {FileName, *Offset, Preamble,
1902  PreamblePatch::createFullPatch(FileName, ParseInput, Preamble),
1903  ParseInput});
1904  return Result;
1905 }
1906 
1907 bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) {
1908  auto InTopLevelScope = [](const NamedDecl &ND) {
1909  switch (ND.getDeclContext()->getDeclKind()) {
1910  case Decl::TranslationUnit:
1911  case Decl::Namespace:
1912  case Decl::LinkageSpec:
1913  return true;
1914  default:
1915  break;
1916  };
1917  return false;
1918  };
1919  // We only complete symbol's name, which is the same as the name of the
1920  // *primary* template in case of template specializations.
1922  return false;
1923 
1924  // Category decls are not useful on their own outside the interface or
1925  // implementation blocks. Moreover, sema already provides completion for
1926  // these, even if it requires preamble deserialization. So by excluding them
1927  // from the index, we reduce the noise in all the other completion scopes.
1928  if (llvm::isa<ObjCCategoryDecl>(&ND) || llvm::isa<ObjCCategoryImplDecl>(&ND))
1929  return false;
1930 
1931  if (InTopLevelScope(ND))
1932  return true;
1933 
1934  if (const auto *EnumDecl = dyn_cast<clang::EnumDecl>(ND.getDeclContext()))
1935  return InTopLevelScope(*EnumDecl) && !EnumDecl->isScoped();
1936 
1937  return false;
1938 }
1939 
1940 // FIXME: find a home for this (that can depend on both markup and Protocol).
1942  MarkupContent Result;
1943  Result.kind = Kind;
1944  switch (Kind) {
1945  case MarkupKind::PlainText:
1946  Result.value.append(Doc.asPlainText());
1947  break;
1948  case MarkupKind::Markdown:
1949  Result.value.append(Doc.asMarkdown());
1950  break;
1951  }
1952  return Result;
1953 }
1954 
1955 CompletionItem CodeCompletion::render(const CodeCompleteOptions &Opts) const {
1956  CompletionItem LSP;
1957  const auto *InsertInclude = Includes.empty() ? nullptr : &Includes[0];
1958  LSP.label = ((InsertInclude && InsertInclude->Insertion)
1959  ? Opts.IncludeIndicator.Insert
1960  : Opts.IncludeIndicator.NoInsert) +
1961  (Opts.ShowOrigins ? "[" + llvm::to_string(Origin) + "]" : "") +
1962  RequiredQualifier + Name + Signature;
1963 
1964  LSP.kind = Kind;
1965  LSP.detail = BundleSize > 1
1966  ? std::string(llvm::formatv("[{0} overloads]", BundleSize))
1967  : ReturnType;
1968  LSP.deprecated = Deprecated;
1969  // Combine header information and documentation in LSP `documentation` field.
1970  // This is not quite right semantically, but tends to display well in editors.
1971  if (InsertInclude || Documentation) {
1972  markup::Document Doc;
1973  if (InsertInclude)
1974  Doc.addParagraph().appendText("From ").appendCode(InsertInclude->Header);
1975  if (Documentation)
1976  Doc.append(*Documentation);
1977  LSP.documentation = renderDoc(Doc, Opts.DocumentationFormat);
1978  }
1979  LSP.sortText = sortText(Score.Total, Name);
1980  LSP.filterText = Name;
1981  LSP.textEdit = {CompletionTokenRange, RequiredQualifier + Name};
1982  // Merge continuous additionalTextEdits into main edit. The main motivation
1983  // behind this is to help LSP clients, it seems most of them are confused when
1984  // they are provided with additionalTextEdits that are consecutive to main
1985  // edit.
1986  // Note that we store additional text edits from back to front in a line. That
1987  // is mainly to help LSP clients again, so that changes do not effect each
1988  // other.
1989  for (const auto &FixIt : FixIts) {
1990  if (FixIt.range.end == LSP.textEdit->range.start) {
1991  LSP.textEdit->newText = FixIt.newText + LSP.textEdit->newText;
1992  LSP.textEdit->range.start = FixIt.range.start;
1993  } else {
1994  LSP.additionalTextEdits.push_back(FixIt);
1995  }
1996  }
1997  if (Opts.EnableSnippets)
1998  LSP.textEdit->newText += SnippetSuffix;
1999 
2000  // FIXME(kadircet): Do not even fill insertText after making sure textEdit is
2001  // compatible with most of the editors.
2002  LSP.insertText = LSP.textEdit->newText;
2003  LSP.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet
2004  : InsertTextFormat::PlainText;
2005  if (InsertInclude && InsertInclude->Insertion)
2006  LSP.additionalTextEdits.push_back(*InsertInclude->Insertion);
2007 
2008  LSP.score = Score.ExcludingName;
2009 
2010  return LSP;
2011 }
2012 
2013 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const CodeCompletion &C) {
2014  // For now just lean on CompletionItem.
2015  return OS << C.render(CodeCompleteOptions());
2016 }
2017 
2018 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
2019  const CodeCompleteResult &R) {
2020  OS << "CodeCompleteResult: " << R.Completions.size() << (R.HasMore ? "+" : "")
2021  << " (" << getCompletionKindString(R.Context) << ")"
2022  << " items:\n";
2023  for (const auto &C : R.Completions)
2024  OS << C << "\n";
2025  return OS;
2026 }
2027 
2028 // Heuristically detect whether the `Line` is an unterminated include filename.
2029 bool isIncludeFile(llvm::StringRef Line) {
2030  Line = Line.ltrim();
2031  if (!Line.consume_front("#"))
2032  return false;
2033  Line = Line.ltrim();
2034  if (!(Line.consume_front("include_next") || Line.consume_front("include") ||
2035  Line.consume_front("import")))
2036  return false;
2037  Line = Line.ltrim();
2038  if (Line.consume_front("<"))
2039  return Line.count('>') == 0;
2040  if (Line.consume_front("\""))
2041  return Line.count('"') == 0;
2042  return false;
2043 }
2044 
2045 bool allowImplicitCompletion(llvm::StringRef Content, unsigned Offset) {
2046  // Look at last line before completion point only.
2047  Content = Content.take_front(Offset);
2048  auto Pos = Content.rfind('\n');
2049  if (Pos != llvm::StringRef::npos)
2050  Content = Content.substr(Pos + 1);
2051 
2052  // Complete after scope operators.
2053  if (Content.endswith(".") || Content.endswith("->") || Content.endswith("::"))
2054  return true;
2055  // Complete after `#include <` and #include `<foo/`.
2056  if ((Content.endswith("<") || Content.endswith("\"") ||
2057  Content.endswith("/")) &&
2058  isIncludeFile(Content))
2059  return true;
2060 
2061  // Complete words. Give non-ascii characters the benefit of the doubt.
2062  return !Content.empty() && (isAsciiIdentifierContinue(Content.back()) ||
2063  !llvm::isASCII(Content.back()));
2064 }
2065 
2066 } // namespace clangd
2067 } // namespace clang
dlog
#define dlog(...)
Definition: Logger.h:102
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
clang::find_all_symbols::SymbolInfo
Describes a named symbol from a header.
Definition: SymbolInfo.h:27
clang::clangd::getDeclComment
std::string getDeclComment(const ASTContext &Ctx, const NamedDecl &Decl)
Similar to getDocComment, but returns the comment for a NamedDecl.
Definition: CodeCompletionStrings.cpp:74
clang::clangd::markup::Document::asPlainText
std::string asPlainText() const
Doesn't contain any trailing newlines.
Definition: Markup.cpp:488
SyntaxOnlyAction
set
set(CLANGD_XPC_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") set(CLANGD_XPC_BINARY_DIR "$
Definition: clangd/xpc/CMakeLists.txt:1
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:28
Headers.h
clang::clangd::collectIdentifiers
llvm::StringMap< unsigned > collectIdentifiers(llvm::StringRef Content, const format::FormatStyle &Style)
Collects identifiers with counts in the source code.
Definition: SourceCode.cpp:608
References
unsigned References
Definition: CodeComplete.cpp:163
clang::tidy::bugprone::getName
static SmallString< 64 > getName(const NamedDecl *ND)
Returns the diagnostic-friendly name of the node, or empty string.
Definition: EasilySwappableParametersCheck.cpp:1907
clang::clangd::signatureHelp
SignatureHelp signatureHelp(PathRef FileName, Position Pos, const PreambleData &Preamble, const ParseInputs &ParseInput)
Get signature help at a specified Pos in FileName.
Definition: CodeComplete.cpp:1883
clang::clangd::CodeCompleteResult::Completions
std::vector< CodeCompletion > Completions
Definition: CodeComplete.h:239
SymbolOrigin.h
clang::clangd::hasUnstableLinkage
bool hasUnstableLinkage(const Decl *D)
Whether we must avoid computing linkage for D during code completion.
Definition: AST.cpp:549
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
clang::clangd::CodeCompleteResult::Context
CodeCompletionContext::Kind Context
Definition: CodeComplete.h:241
clang::clangd::prepareCompilerInstance
std::unique_ptr< CompilerInstance > prepareCompilerInstance(std::unique_ptr< clang::CompilerInvocation > CI, const PrecompiledPreamble *Preamble, std::unique_ptr< llvm::MemoryBuffer > Buffer, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, DiagnosticConsumer &DiagsClient)
Definition: Compiler.cpp:110
clang::clangd::CodeCompleteResult::HasMore
bool HasMore
Definition: CodeComplete.h:240
CodeComplete.h
if
if(CMAKE_CFG_INTDIR STREQUAL ".") set(LLVM_BUILD_MODE ".") else() set(LLVM_BUILD_MODE "%(build_mode)s") endif() string(REPLACE $
Definition: clangd/test/CMakeLists.txt:4
clang::clangd::CompletionItemKind::Class
@ Class
CCSema
Sema * CCSema
Definition: CodeComplete.cpp:773
Expected
std::vector< const char * > Expected
Definition: PrintASTTests.cpp:27
clang::clangd::CodeCompletion
Definition: CodeComplete.h:159
Tracer
std::unique_ptr< trace::EventTracer > Tracer
Definition: TraceTests.cpp:164
clang::clangd::error
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
Definition: Logger.h:80
IndexResult
const Symbol * IndexResult
Definition: CodeComplete.cpp:172
clang::clangd::CompletionItem::deprecated
bool deprecated
Indicates if this item is deprecated.
Definition: Protocol.h:1224
clang::clangd::evaluateSymbolAndRelevance
float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance)
Combine symbol quality and relevance into a single score.
Definition: Quality.cpp:536
ParseInput
const ParseInputs & ParseInput
Definition: CodeComplete.cpp:1106
clang::clangd::CompletionItemKind::Field
@ Field
clang::clangd::splitQualifiedName
std::pair< StringRef, StringRef > splitQualifiedName(StringRef QName)
Definition: SourceCode.cpp:484
clang::clangd::visibleNamespaces
std::vector< std::string > visibleNamespaces(llvm::StringRef Code, const LangOptions &LangOpts)
Heuristically determine namespaces visible at a point, without parsing Code.
Definition: SourceCode.cpp:793
clang::clangd::CompletionPrefix::Name
llvm::StringRef Name
Definition: CodeComplete.h:308
clang::clangd::markup::Paragraph::appendText
Paragraph & appendText(llvm::StringRef Text)
Append plain text to the end of the string.
Definition: Markup.cpp:422
clang::clangd::PreambleData::Macros
MainFileMacros Macros
Definition: Preamble.h:63
clang::clangd::lspLength
size_t lspLength(llvm::StringRef Code)
Definition: SourceCode.cpp:151
clang::clangd::collectWords
llvm::StringSet collectWords(llvm::StringRef Content)
Collects words from the source code.
Definition: SourceCode.cpp:841
Index.h
CI
std::unique_ptr< CompilerInvocation > CI
Definition: TUScheduler.cpp:450
clang::clangd::OpaqueType::fromCompletionResult
static llvm::Optional< OpaqueType > fromCompletionResult(ASTContext &Ctx, const CodeCompletionResult &R)
Create a type from a code completion result.
Definition: ExpectedTypes.cpp:87
Kind
BindArgumentKind Kind
Definition: AvoidBindCheck.cpp:59
clang::clangd::CompletionItem::label
std::string label
The label of this completion item.
Definition: Protocol.h:1182
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompleteResult &R)
Definition: CodeComplete.cpp:2018
clang::clangd::CompletionItemKind::Reference
@ Reference
clang::clangd::Symbol::Deprecated
@ Deprecated
Indicates if the symbol is deprecated.
Definition: Symbol.h:121
clang::tidy::cppcoreguidelines::getSourceText
static std::string getSourceText(const CXXDestructorDecl &Destructor)
Definition: VirtualClassDestructorCheck.cpp:96
clang::clangd::X
static URISchemeRegistry::Add< TestScheme > X(TestScheme::Scheme, "Test schema")
Preamble.h
clang::clangd::CompletionItemKind::Folder
@ Folder
clang::clangd::toTextEdit
TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M, const LangOptions &L)
Definition: SourceCode.cpp:546
clang::clangd::markup::Document::addParagraph
Paragraph & addParagraph()
Adds a semantical block that will be separate from others.
Definition: Markup.cpp:472
include_directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR}) include_directories($
Definition: clangd/CMakeLists.txt:2
Trace.h
IDForDoc
SymbolID IDForDoc
Definition: CodeComplete.cpp:876
add_clang_tool
add_clang_tool(clangd ClangdMain.cpp Check.cpp $< TARGET_OBJECTS:obj.clangDaemonTweaks >) set(LLVM_LINK_COMPONENTS support) set(CLANGD_XPC_LIBS "") if(CLANGD_BUILD_XPC) list(APPEND CLANGD_XPC_LIBS "clangdXpcJsonConversions" "clangdXpcTransport") endif() clang_target_link_libraries(clangd PRIVATE clangAST clangBasic clangFormat clangFrontend clangLex clangSema clangTooling clangToolingCore clangToolingRefactoring clangToolingSyntax) target_link_libraries(clangd PRIVATE clangTidy clangDaemon clangdRemoteIndex clangdSupport $
Definition: clangd/tool/CMakeLists.txt:1
clang::clangd::markup::Document
A format-agnostic representation for structured text.
Definition: Markup.h:94
clang::clangd::printNamespaceScope
std::string printNamespaceScope(const DeclContext &DC)
Returns the first enclosing namespace scope starting from DC.
Definition: AST.cpp:283
clang::tidy::cppcoreguidelines::join
static std::string join(ArrayRef< SpecialMemberFunctionsCheck::SpecialMemberFunctionKind > SMFS, llvm::StringRef AndOr)
Definition: SpecialMemberFunctionsCheck.cpp:78
clang::clangd::CompletionItem::score
float score
The score that clangd calculates to rank the returned completions.
Definition: Protocol.h:1231
set
MATCHES Darwin set(CLANGD_BUILD_XPC_DEFAULT ON) else() set(CLANGD_BUILD_XPC_DEFAULT OFF) endif() llvm_canonicalize_cmake_booleans(CLANGD_BUILD_XPC_DEFAULT) set(CLANGD_BUILD_XPC $
Definition: clangd/CMakeLists.txt:10
CCContext
CodeCompletionContext CCContext
Definition: CodeComplete.cpp:772
clang::clangd::CompletionItemKind::Property
@ Property
CCTUInfo
CodeCompletionTUInfo CCTUInfo
Definition: CodeCompletionStringsTests.cpp:35
clang::clangd::CompletionItemKind::Text
@ Text
clang::tidy::clangTidyMain
int clangTidyMain(int argc, const char **argv)
Definition: ClangTidyMain.cpp:248
Snippet
std::string Snippet
Definition: CodeCompletionStringsTests.cpp:38
clang::clangd::SpeculativeFuzzyFind
A speculative and asynchronous fuzzy find index request (based on cached request) that can be sent be...
Definition: CodeComplete.h:256
Action
llvm::unique_function< void()> Action
Definition: TUScheduler.cpp:596
clang::clangd::sourceLocToPosition
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
Definition: SourceCode.cpp:216
RankedIncludeHeaders
llvm::SmallVector< llvm::StringRef, 1 > RankedIncludeHeaders
Definition: CodeComplete.cpp:174
clang::clangd::CompletionItem::filterText
std::string filterText
A string that should be used when filtering a set of completion items.
Definition: Protocol.h:1201
clang::clangd::getDocComment
std::string getDocComment(const ASTContext &Ctx, const CodeCompletionResult &Result, bool CommentsFromHeaders)
Gets a minimally formatted documentation comment of Result, with comment markers stripped.
Definition: CodeCompletionStrings.cpp:62
clang::clangd::CompletionItemKind::Keyword
@ Keyword
AccessibleScopes
std::vector< std::string > AccessibleScopes
Definition: CodeComplete.cpp:606
clang::clangd::CompletionItem
Definition: Protocol.h:1179
clang::clangd::MainFileMacros::Names
llvm::StringSet Names
Definition: CollectMacros.h:32
clang::clangd::CompletionItem::textEdit
llvm::Optional< TextEdit > textEdit
An edit which is applied to a document when selecting this completion.
Definition: Protocol.h:1216
clang::clangd::CodeCompleteOptions::AllScopes
bool AllScopes
Whether to include index symbols that are not defined in the scopes visible from the code completion ...
Definition: CodeComplete.h:107
Preamble
const PreambleData & Preamble
Definition: CodeComplete.cpp:1104
clang::clangd::CodeCompleteOptions::CodeCompletionRankingModel
CodeCompletionRankingModel
Model to use for ranking code completion candidates.
Definition: CodeComplete.h:133
clang::clangd::CompletionItemKind::Enum
@ Enum
clang::clangd::markup::Document::asMarkdown
std::string asMarkdown() const
Doesn't contain any trailing newlines.
Definition: Markup.cpp:484
FixIt
llvm::Optional< FixItHint > FixIt
Definition: UppercaseLiteralSuffixCheck.cpp:62
test
Definition: test.py:1
Protocol.h
ThreadsafeFS.h
clang::clangd::IncludeStructure
Definition: Headers.h:113
Hover.h
clang::clangd::getRankedIncludes
llvm::SmallVector< llvm::StringRef, 1 > getRankedIncludes(const Symbol &Sym)
Definition: Headers.cpp:136
clang::clangd::CompletionPrefix
Definition: CodeComplete.h:305
clang::clangd::formatDocumentation
std::string formatDocumentation(const CodeCompletionString &CCS, llvm::StringRef DocComment)
Assembles formatted documentation for a completion result.
Definition: CodeCompletionStrings.cpp:242
clang::clangd::URI::resolve
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
Definition: URI.cpp:247
FileDistance.h
Offset
size_t Offset
Definition: CodeComplete.cpp:1103
clang::clangd::halfOpenToRange
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
Definition: SourceCode.cpp:466
clang::clangd::Position
Definition: Protocol.h:148
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:47
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
FuzzyMatch.h
Name
llvm::StringRef Name
Definition: CodeComplete.cpp:162
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
CodeCompletionStrings.h
IdentifierResult
const RawIdentifier * IdentifierResult
Definition: CodeComplete.cpp:173
clang::clangd::Unknown
@ Unknown
Definition: FuzzyMatch.h:56
Line
int Line
Definition: PreprocessorTracker.cpp:514
clang::clangd::PreambleData
The parsed preamble and associated data.
Definition: Preamble.h:49
clang::clangd::CompletionItemKind::Interface
@ Interface
clang::clangd::CompletionItemKind::Function
@ Function
clang::clangd::CompletionItem::insertText
std::string insertText
A string that should be inserted to a document when selecting this completion.
Definition: Protocol.h:1205
Quality
SignatureQualitySignals Quality
Definition: CodeComplete.cpp:878
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:36
clang::clangd::printQualifiedName
std::string printQualifiedName(const NamedDecl &ND)
Returns the qualified name of ND.
Definition: AST.cpp:174
clang::clangd::toJSON
llvm::json::Value toJSON(const FuzzyFindRequest &Request)
Definition: Index.cpp:49
SemaResult
const CodeCompletionResult * SemaResult
Definition: CodeComplete.cpp:171
Logger.h
clang::clangd::getSymbolInfo
std::vector< SymbolDetails > getSymbolInfo(ParsedAST &AST, Position Pos)
Get info about symbols at Pos.
Definition: XRefs.cpp:1509
clang::clangd::codeComplete
CodeCompleteResult codeComplete(PathRef FileName, Position Pos, const PreambleData *Preamble, const ParseInputs &ParseInput, CodeCompleteOptions Opts, SpeculativeFuzzyFind *SpecFuzzyFind)
Gets code completions at a specified Pos in FileName.
Definition: CodeComplete.cpp:1860
clang::clangd::CompletionItem::kind
CompletionItemKind kind
The kind of this completion item.
Definition: Protocol.h:1186
Threading.h
clang::clangd::positionToOffset
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:175
if
if(CLANGD_ENABLE_REMOTE) generate_protos(RemoteIndexProto "Index.proto") generate_protos(MonitoringServiceProto "MonitoringService.proto" GRPC) generate_protos(RemoteIndexServiceProto "Service.proto" DEPENDS "Index.proto" GRPC) target_link_libraries(RemoteIndexServiceProto PRIVATE RemoteIndexProto MonitoringServiceProto) include_directories($
Definition: clangd/index/remote/CMakeLists.txt:1
ExpectedTypes.h
Patch
const llvm::Optional< PreamblePatch > Patch
Definition: CodeComplete.cpp:1105
SnippetSuffix
std::string SnippetSuffix
Definition: CodeComplete.cpp:431
clang::clangd::CompletionItem::sortText
std::string sortText
A string that should be used when comparing this item with other items.
Definition: Protocol.h:1197
SPAN_ATTACH
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
Definition: Trace.h:164
clang::clangd::toString
static const char * toString(OffsetEncoding OE)
Definition: Protocol.cpp:1338
CC
CognitiveComplexity CC
Definition: FunctionCognitiveComplexityCheck.cpp:496
clang::clangd::CompletionItemKind::Struct
@ Struct
clang::clangd::vlog
void vlog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:73
clang_target_link_libraries
DEPENDS omp_gen clang_target_link_libraries(clangTidyMain PRIVATE clangAST clangASTMatchers clangBasic clangTooling clangToolingCore) add_clang_tool(clang-tidy ClangTidyToolMain.cpp) add_dependencies(clang-tidy clang-resource-headers) clang_target_link_libraries(clang-tidy PRIVATE clangAST clangASTMatchers clangBasic clangTooling clangToolingCore) target_link_libraries(clang-tidy PRIVATE clangTidy clangTidyMain $
Definition: clang-tidy/tool/CMakeLists.txt:23
Diagnostics.h
Results
std::vector< CodeCompletionResult > Results
Definition: CodeComplete.cpp:771
clang::doc::SymbolID
std::array< uint8_t, 20 > SymbolID
Definition: Representation.h:30
clang::clangd::collectIncludeStructureCallback
std::unique_ptr< PPCallbacks > collectIncludeStructureCallback(const SourceManager &SM, IncludeStructure *Out)
Returns a PPCallback that visits all inclusions in the main file.
Definition: Headers.cpp:152
set
set(LLVM_LINK_COMPONENTS FrontendOpenMP Support) configure_file($
Definition: clang-tidy/CMakeLists.txt:1
clang::clangd::offsetToPosition
Position offsetToPosition(llvm::StringRef Code, size_t Offset)
Turn an offset in Code into a [line, column] pair.
Definition: SourceCode.cpp:204
clang::clangd::CompletionItemKind::Method
@ Method
clang::clangd::toHeaderFile
llvm::Expected< HeaderFile > toHeaderFile(llvm::StringRef Header, llvm::StringRef HintPath)
Creates a HeaderFile from Header which can be either a URI or a literal include.
Definition: Headers.cpp:116
clang::clangd::SymbolOrigin
SymbolOrigin
Definition: SymbolOrigin.h:21
clang::clangd::isIndexedForCodeCompletion
bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx)
Definition: CodeComplete.cpp:1907
clang::clangd::CompletionItemKind
CompletionItemKind
The kind of a completion entry.
Definition: Protocol.h:286
clang::clangd::markup::Document::append
void append(Document Other)
Definition: Markup.cpp:467
clang::clangd::SymbolOrigin::AST
@ AST
clang::clangd::isIncludeFile
bool isIncludeFile(llvm::StringRef Line)
Definition: CodeComplete.cpp:2029
Output
std::string Output
Definition: TraceTests.cpp:162
clang::clangd::getFormatStyleForFile
format::FormatStyle getFormatStyleForFile(llvm::StringRef File, llvm::StringRef Content, const ThreadsafeFS &TFS)
Choose the clang-format style we should apply to a certain file.
Definition: SourceCode.cpp:573
clang::clangd::CodeCompleteOptions
Definition: CodeComplete.h:45
SymbolKind
clang::find_all_symbols::SymbolInfo::SymbolKind SymbolKind
Definition: SymbolInfo.cpp:21
Symbol.h
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
clang::clangd::check
bool check(llvm::StringRef File, llvm::function_ref< bool(const Position &)> ShouldCheckLine, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:253
clang::clangd::log
void log(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:68
clang::clangd::markup::Paragraph::appendCode
Paragraph & appendCode(llvm::StringRef Code, bool Preserve=false)
Append inline code, this translates to the ` block in markdown.
Definition: Markup.cpp:435
clang::clangd::OpaqueType::fromType
static llvm::Optional< OpaqueType > fromType(ASTContext &Ctx, QualType Type)
Construct an instance from a clang::QualType.
Definition: ExpectedTypes.cpp:81
clang::clangd::buildCompilerInvocation
std::unique_ptr< CompilerInvocation > buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, std::vector< std::string > *CC1Args)
Builds compiler invocation that could be used to build AST or preamble.
Definition: Compiler.cpp:84
clang::clangd::Deprecated
@ Deprecated
Deprecated or obsolete code.
Definition: Protocol.h:824
clang::clangd::CodeCompleteOptions::NeverInsert
@ NeverInsert
Definition: CodeComplete.h:73
clang::clangd::allowImplicitCompletion
bool allowImplicitCompletion(llvm::StringRef Content, unsigned Offset)
Definition: CodeComplete.cpp:2045
Entry
Definition: Modularize.cpp:428
clang::clangd::getSymbolID
SymbolID getSymbolID(const Decl *D)
Gets the symbol ID for a declaration. Returned SymbolID might be null.
Definition: AST.cpp:337
SourceCode.h
Index
const SymbolIndex * Index
Definition: Dexp.cpp:99
clang::clangd::CompletionItemKind::File
@ File
gen_decision_forest
quality CompletionModel cmake gen_decision_forest(${CMAKE_CURRENT_SOURCE_DIR}/decision_forest_model DecisionForestRuntimeTest ::ns1::ns2::test::Example) add_custom_target(ClangdUnitTests) add_unittest(ClangdUnitTests ClangdTests Annotations.cpp ASTTests.cpp ASTSignalsTests.cpp BackgroundIndexTests.cpp CallHierarchyTests.cpp CanonicalIncludesTests.cpp ClangdTests.cpp ClangdLSPServerTests.cpp CodeCompleteTests.cpp CodeCompletionStringsTests.cpp CollectMacrosTests.cpp CompileCommandsTests.cpp CompilerTests.cpp ConfigCompileTests.cpp ConfigProviderTests.cpp ConfigYAMLTests.cpp DecisionForestTests.cpp DexTests.cpp DiagnosticsTests.cpp DraftStoreTests.cpp DumpASTTests.cpp ExpectedTypeTest.cpp FeatureModulesTests.cpp FileDistanceTests.cpp FileIndexTests.cpp FindSymbolsTests.cpp FindTargetTests.cpp FormatTests.cpp FSTests.cpp FuzzyMatchTests.cpp GlobalCompilationDatabaseTests.cpp HeadersTests.cpp HeaderSourceSwitchTests.cpp HoverTests.cpp IncludeCleanerTests.cpp IndexActionTests.cpp IndexTests.cpp InlayHintTests.cpp JSONTransportTests.cpp LoggerTests.cpp LSPBinderTests.cpp LSPClient.cpp ModulesTests.cpp ParsedASTTests.cpp PathMappingTests.cpp PreambleTests.cpp PrintASTTests.cpp ProjectAwareIndexTests.cpp QualityTests.cpp RenameTests.cpp RIFFTests.cpp SelectionTests.cpp SemanticHighlightingTests.cpp SemanticSelectionTests.cpp SerializationTests.cpp SourceCodeTests.cpp SymbolCollectorTests.cpp SymbolInfoTests.cpp SyncAPI.cpp TUSchedulerTests.cpp TestFS.cpp TestIndex.cpp TestTU.cpp TestWorkspace.cpp TidyProviderTests.cpp TypeHierarchyTests.cpp URITests.cpp XRefsTests.cpp $
Definition: clangd/unittests/CMakeLists.txt:23
clang::clangd::CompletionItem::insertTextFormat
InsertTextFormat insertTextFormat
The format of the insert text.
Definition: Protocol.h:1209
clang::clangd::MarkupKind
MarkupKind
Definition: Protocol.h:387
clang::clangd::CompletionItem::additionalTextEdits
std::vector< TextEdit > additionalTextEdits
An optional array of additional text edits that are applied when selecting this completion.
Definition: Protocol.h:1221
unset
CACHE BOOL Build XPC Support For Clangd FORCE unset(CLANGD_BUILD_XPC_DEFAULT) endif() option(CLANGD_MALLOC_TRIM "Call malloc_trim(3) periodically in Clangd. (only takes effect when using glibc)" ON) option(CLANGD_TIDY_CHECKS "Link all clang-tidy checks into clangd" ON) llvm_canonicalize_cmake_booleans(CLANGD_BUILD_XPC CLANGD_ENABLE_REMOTE ENABLE_GRPC_REFLECTION CLANGD_MALLOC_TRIM CLANGD_TIDY_CHECKS LLVM_ENABLE_ZLIB) configure_file($
Definition: clangd/CMakeLists.txt:18
Compiler.h
clang::clangd::RefKind::Spelled
@ Spelled
Info
FunctionInfo Info
Definition: FunctionSizeCheck.cpp:120
ID
static char ID
Definition: Logger.cpp:74
Score
llvm::Optional< float > Score
Definition: FuzzyMatchTests.cpp:48
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
clang::clangd::CompletionItemKind::EnumMember
@ EnumMember
Candidate
ExpectedMatch Candidate
Definition: FuzzyMatchTests.cpp:47
clang::clangd::sortText
std::string sortText(float Score, llvm::StringRef Name)
Returns a string that sorts in the same order as (-Score, Tiebreak), for LSP.
Definition: Quality.cpp:613
clang::clangd::getSignature
void getSignature(const CodeCompletionString &CCS, std::string *Signature, std::string *Snippet, std::string *RequiredQualifiers, bool CompletingPattern)
Formats the signature for an item, as a display string and snippet.
Definition: CodeCompletionStrings.cpp:98
clang::clangd::CompletionItemKind::Snippet
@ Snippet
set
set(SOURCES ClangdXPC.cpp) add_clang_library(ClangdXPCLib SHARED $
Definition: clangd/xpc/framework/CMakeLists.txt:2
clang::clangd::CompletionItem::detail
std::string detail
A human-readable string with additional information about this item, like type or symbol information.
Definition: Protocol.h:1190
clang::tidy::bugprone::model::MixFlags::None
@ None
Mix between the two parameters is not possible.
clang::clangd::SignatureHelp
Represents the signature of a callable.
Definition: Protocol.h:1291
C
const Criteria C
Definition: FunctionCognitiveComplexityCheck.cpp:93
clang::clangd::renderDoc
static MarkupContent renderDoc(const markup::Document &Doc, MarkupKind Kind)
Definition: CodeComplete.cpp:1941
set
set(LLVM_LINK_COMPONENTS Support) add_clang_library(clangApplyReplacements lib/Tooling/ApplyReplacements.cpp) clang_target_link_libraries(clangApplyReplacements PRIVATE clangAST clangBasic clangRewrite clangToolingCore clangToolingRefactoring) include_directories($
Definition: clang-apply-replacements/CMakeLists.txt:1
clang::clangd::CodeCompleteResult
Definition: CodeComplete.h:238
clang::clangd::CompletionItemKind::Constructor
@ Constructor
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:163
Modularize
Definition: CoverageChecker.h:32
TUScheduler.h
set
set(LLVM_LINK_COMPONENTS Support) add_clang_executable(clangd-index-server Server.cpp DEPENDS RemoteIndexProto RemoteIndexServiceProto) target_link_libraries(clangd-index-server PRIVATE clangDaemon clangdSupport MonitoringServiceProto RemoteIndexProto RemoteIndexServiceProto clangdRemoteMarshalling $
Definition: clangd/index/remote/server/CMakeLists.txt:1
add_definitions
index remote add_definitions(-DGOOGLE_PROTOBUF_NO_RTTI=1) set(REMOTE_TEST_SOURCES remote/MarshallingTests.cpp) endif() include($
Definition: clangd/unittests/CMakeLists.txt:18
set
set(LLVM_LINK_COMPONENTS support AllTargetsInfos FrontendOpenMP) if(CLANG_BUILT_STANDALONE) if(EXISTS $
Definition: clangd/unittests/CMakeLists.txt:1
clang::clangd::isExplicitTemplateSpecialization
bool isExplicitTemplateSpecialization(const NamedDecl *D)
Indicates if D is an explicit template specialization, e.g.
Definition: AST.cpp:158
clang::clangd::parseDocumentation
void parseDocumentation(llvm::StringRef Input, markup::Document &Output)
Definition: Hover.cpp:1179
add_subdirectory
lib Testing Support AND NOT TARGET LLVMTestingSupport add_subdirectory(${LLVM_MAIN_SRC_DIR}/lib/Testing/Support lib/Testing/Support) endif() endif() if(CLANGD_ENABLE_REMOTE) include_directories($
Definition: clangd/unittests/CMakeLists.txt:11
clang::clangd::guessCompletionPrefix
CompletionPrefix guessCompletionPrefix(llvm::StringRef Content, unsigned Offset)
Definition: CodeComplete.cpp:1838
clang::clangd::getReturnType
std::string getReturnType(const CodeCompletionString &CCS)
Gets detail to be used as the detail field in an LSP completion item.
Definition: CodeCompletionStrings.cpp:272
clang::clangd::SymbolOrigin::Unknown
@ Unknown
clang::clangd::CompletionItemKind::Missing
@ Missing
clang::clangd::CompletionItemKind::Module
@ Module
ReturnType
std::string ReturnType
Definition: CodeComplete.cpp:433
Pos
Position Pos
Definition: SourceCode.cpp:650
URI.h
Allocator
std::shared_ptr< clang::GlobalCodeCompletionAllocator > Allocator
Definition: CodeCompletionStringsTests.cpp:34
add_subdirectory
add_subdirectory(support) if(NOT DEFINED CLANGD_BUILD_XPC) if($
Definition: clangd/CMakeLists.txt:5
Quality.h
ns1::ns2::B
@ B
Definition: CategoricalFeature.h:3
clang::clangd::elog
void elog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:62
clang::clangd::MarkupContent
Definition: Protocol.h:1145
clang::clangd::SymbolRelevanceSignals::CodeComplete
@ CodeComplete
Definition: Quality.h:124
clang::clangd::SymbolOrigin::Identifier
@ Identifier
clang::clangd::CompletionPrefix::Qualifier
llvm::StringRef Qualifier
Definition: CodeComplete.h:311
FileName
PathRef FileName
Definition: CodeComplete.cpp:1102
clang::clangd::CompletionItemKind::TypeParameter
@ TypeParameter
set
set(LLVM_LINK_COMPONENTS AllTargetsAsmParsers AllTargetsDescs AllTargetsInfos FrontendOpenMP support) set(LLVM_OPTIONAL_SOURCES ClangTidyMain.cpp ClangTidyToolMain.cpp) add_clang_library(clangTidyMain ClangTidyMain.cpp LINK_LIBS clangTidy $
Definition: clang-tidy/tool/CMakeLists.txt:1
clang::clangd::CompletionItemKind::Variable
@ Variable
IgnoreDiags
IgnoringDiagConsumer IgnoreDiags
Definition: HeadersTests.cpp:128
K
Kind K
Definition: Rename.cpp:442
clang::clangd::quality
float quality(const Symbol &S)
Computes query-independent quality score for a Symbol.
Definition: Symbol.cpp:29
clang::clangd::SymbolIndex::fuzzyFind
virtual bool fuzzyFind(const FuzzyFindRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const =0
Matches symbols in the index fuzzily and applies Callback on each matched symbol before returning.
set
set(LLVM_LINK_COMPONENTS Support) if(CLANG_BUILT_STANDALONE) include(CheckAtomic) endif() set(CLANGD_ATOMIC_LIB "") if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB OR NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB) list(APPEND CLANGD_ATOMIC_LIB "atomic") endif() add_clang_library(clangdSupport Cancellation.cpp Context.cpp FileCache.cpp Logger.cpp Markup.cpp MemoryTree.cpp Path.cpp Shutdown.cpp Threading.cpp ThreadsafeFS.cpp Trace.cpp LINK_LIBS $
Definition: clangd/support/CMakeLists.txt:4
clang::clangd::SymbolRelevanceSignals::FileScope
@ FileScope
Definition: Quality.h:119
Signature
std::string Signature
Definition: CodeComplete.cpp:432
Path
std::vector< HeaderHandle > Path
Definition: PreprocessorTracker.cpp:525
AST.h
UnresolvedQualifier
llvm::Optional< std::string > UnresolvedQualifier
Definition: CodeComplete.cpp:609
clang::clangd::offsetToClangLineColumn
std::pair< size_t, size_t > offsetToClangLineColumn(llvm::StringRef Code, size_t Offset)
Definition: SourceCode.cpp:474
clang::clangd::CompletionItem::documentation
llvm::Optional< MarkupContent > documentation
A human-readable string that represents a doc-comment.
Definition: Protocol.h:1193
lit
Definition: test/lit.cfg.py:1