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