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 "ClangdUnit.h"
23 #include "CodeCompletionStrings.h"
24 #include "Compiler.h"
25 #include "Diagnostics.h"
26 #include "ExpectedTypes.h"
27 #include "FileDistance.h"
28 #include "FuzzyMatch.h"
29 #include "Headers.h"
30 #include "Logger.h"
31 #include "Quality.h"
32 #include "SourceCode.h"
33 #include "TUScheduler.h"
34 #include "Trace.h"
35 #include "URI.h"
36 #include "index/Index.h"
37 #include "clang/AST/Decl.h"
38 #include "clang/AST/DeclBase.h"
39 #include "clang/Basic/LangOptions.h"
40 #include "clang/Basic/SourceLocation.h"
41 #include "clang/Format/Format.h"
42 #include "clang/Frontend/CompilerInstance.h"
43 #include "clang/Frontend/FrontendActions.h"
44 #include "clang/Lex/PreprocessorOptions.h"
45 #include "clang/Sema/CodeCompleteConsumer.h"
46 #include "clang/Sema/Sema.h"
47 #include "llvm/ADT/ArrayRef.h"
48 #include "llvm/ADT/None.h"
49 #include "llvm/ADT/Optional.h"
50 #include "llvm/ADT/SmallVector.h"
51 #include "llvm/Support/Error.h"
52 #include "llvm/Support/Format.h"
53 #include "llvm/Support/FormatVariadic.h"
54 #include "llvm/Support/ScopedPrinter.h"
55 #include <algorithm>
56 #include <iterator>
57 
58 // We log detailed candidate here if you run with -debug-only=codecomplete.
59 #define DEBUG_TYPE "CodeComplete"
60 
61 namespace clang {
62 namespace clangd {
63 namespace {
64 
65 CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) {
66   using SK = index::SymbolKind;
67   switch (Kind) {
68   case SK::Unknown:
69     return CompletionItemKind::Missing;
70   case SK::Module:
71   case SK::Namespace:
72   case SK::NamespaceAlias:
73     return CompletionItemKind::Module;
74   case SK::Macro:
75     return CompletionItemKind::Text;
76   case SK::Enum:
77     return CompletionItemKind::Enum;
78   // FIXME(ioeric): use LSP struct instead of class when it is suppoted in the
79   // protocol.
80   case SK::Struct:
81   case SK::Class:
82   case SK::Protocol:
83   case SK::Extension:
84   case SK::Union:
85     return CompletionItemKind::Class;
86   // FIXME(ioeric): figure out whether reference is the right type for aliases.
87   case SK::TypeAlias:
88   case SK::Using:
89     return CompletionItemKind::Reference;
90   case SK::Function:
91   // FIXME(ioeric): this should probably be an operator. This should be fixed
92   // when `Operator` is support type in the protocol.
93   case SK::ConversionFunction:
94     return CompletionItemKind::Function;
95   case SK::Variable:
96   case SK::Parameter:
97     return CompletionItemKind::Variable;
98   case SK::Field:
99     return CompletionItemKind::Field;
100   // FIXME(ioeric): use LSP enum constant when it is supported in the protocol.
101   case SK::EnumConstant:
102     return CompletionItemKind::Value;
103   case SK::InstanceMethod:
104   case SK::ClassMethod:
105   case SK::StaticMethod:
106   case SK::Destructor:
107     return CompletionItemKind::Method;
108   case SK::InstanceProperty:
109   case SK::ClassProperty:
110   case SK::StaticProperty:
111     return CompletionItemKind::Property;
112   case SK::Constructor:
113     return CompletionItemKind::Constructor;
114   }
115   llvm_unreachable("Unhandled clang::index::SymbolKind.");
116 }
117 
118 CompletionItemKind
119 toCompletionItemKind(CodeCompletionResult::ResultKind ResKind,
120                      const NamedDecl *Decl,
121                      CodeCompletionContext::Kind CtxKind) {
122   if (Decl)
123     return toCompletionItemKind(index::getSymbolInfo(Decl).Kind);
124   if (CtxKind == CodeCompletionContext::CCC_IncludedFile)
125     return CompletionItemKind::File;
126   switch (ResKind) {
127   case CodeCompletionResult::RK_Declaration:
128     llvm_unreachable("RK_Declaration without Decl");
129   case CodeCompletionResult::RK_Keyword:
130     return CompletionItemKind::Keyword;
131   case CodeCompletionResult::RK_Macro:
132     return CompletionItemKind::Text; // unfortunately, there's no 'Macro'
133                                      // completion items in LSP.
134   case CodeCompletionResult::RK_Pattern:
135     return CompletionItemKind::Snippet;
136   }
137   llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
138 }
139 
140 /// Get the optional chunk as a string. This function is possibly recursive.
141 ///
142 /// The parameter info for each parameter is appended to the Parameters.
143 std::string getOptionalParameters(const CodeCompletionString &CCS,
144                                   std::vector<ParameterInformation> &Parameters,
145                                   SignatureQualitySignals &Signal) {
146   std::string Result;
147   for (const auto &Chunk : CCS) {
148     switch (Chunk.Kind) {
149     case CodeCompletionString::CK_Optional:
150       assert(Chunk.Optional &&
151              "Expected the optional code completion string to be non-null.");
152       Result += getOptionalParameters(*Chunk.Optional, Parameters, Signal);
153       break;
154     case CodeCompletionString::CK_VerticalSpace:
155       break;
156     case CodeCompletionString::CK_Placeholder:
157       // A string that acts as a placeholder for, e.g., a function call
158       // argument.
159       // Intentional fallthrough here.
160     case CodeCompletionString::CK_CurrentParameter: {
161       // A piece of text that describes the parameter that corresponds to
162       // the code-completion location within a function call, message send,
163       // macro invocation, etc.
164       Result += Chunk.Text;
165       ParameterInformation Info;
166       Info.label = Chunk.Text;
167       Parameters.push_back(std::move(Info));
168       Signal.ContainsActiveParameter = true;
169       Signal.NumberOfOptionalParameters++;
170       break;
171     }
172     default:
173       Result += Chunk.Text;
174       break;
175     }
176   }
177   return Result;
178 }
179 
180 /// A code completion result, in clang-native form.
181 /// It may be promoted to a CompletionItem if it's among the top-ranked results.
182 struct CompletionCandidate {
183   llvm::StringRef Name; // Used for filtering and sorting.
184   // We may have a result from Sema, from the index, or both.
185   const CodeCompletionResult *SemaResult = nullptr;
186   const Symbol *IndexResult = nullptr;
187   llvm::SmallVector<llvm::StringRef, 1> RankedIncludeHeaders;
188 
189   // Returns a token identifying the overload set this is part of.
190   // 0 indicates it's not part of any overload set.
191   size_t overloadSet() const {
192     llvm::SmallString<256> Scratch;
193     if (IndexResult) {
194       switch (IndexResult->SymInfo.Kind) {
195       case index::SymbolKind::ClassMethod:
196       case index::SymbolKind::InstanceMethod:
197       case index::SymbolKind::StaticMethod:
198 #ifndef NDEBUG
199         llvm_unreachable("Don't expect members from index in code completion");
200 #else
201         LLVM_FALLTHROUGH;
202 #endif
203       case index::SymbolKind::Function:
204         // We can't group overloads together that need different #includes.
205         // This could break #include insertion.
206         return llvm::hash_combine(
207             (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch),
208             headerToInsertIfAllowed().getValueOr(""));
209       default:
210         return 0;
211       }
212     }
213     assert(SemaResult);
214     // We need to make sure we're consistent with the IndexResult case!
215     const NamedDecl *D = SemaResult->Declaration;
216     if (!D || !D->isFunctionOrFunctionTemplate())
217       return 0;
218     {
219       llvm::raw_svector_ostream OS(Scratch);
220       D->printQualifiedName(OS);
221     }
222     return llvm::hash_combine(Scratch,
223                               headerToInsertIfAllowed().getValueOr(""));
224   }
225 
226   // The best header to include if include insertion is allowed.
227   llvm::Optional<llvm::StringRef> headerToInsertIfAllowed() const {
228     if (RankedIncludeHeaders.empty())
229       return None;
230     if (SemaResult && SemaResult->Declaration) {
231       // Avoid inserting new #include if the declaration is found in the current
232       // file e.g. the symbol is forward declared.
233       auto &SM = SemaResult->Declaration->getASTContext().getSourceManager();
234       for (const Decl *RD : SemaResult->Declaration->redecls())
235         if (SM.isInMainFile(SM.getExpansionLoc(RD->getBeginLoc())))
236           return None;
237     }
238     return RankedIncludeHeaders[0];
239   }
240 
241   using Bundle = llvm::SmallVector<CompletionCandidate, 4>;
242 };
243 using ScoredBundle =
244     std::pair<CompletionCandidate::Bundle, CodeCompletion::Scores>;
245 struct ScoredBundleGreater {
246   bool operator()(const ScoredBundle &L, const ScoredBundle &R) {
247     if (L.second.Total != R.second.Total)
248       return L.second.Total > R.second.Total;
249     return L.first.front().Name <
250            R.first.front().Name; // Earlier name is better.
251   }
252 };
253 
254 // Assembles a code completion out of a bundle of >=1 completion candidates.
255 // Many of the expensive strings are only computed at this point, once we know
256 // the candidate bundle is going to be returned.
257 //
258 // Many fields are the same for all candidates in a bundle (e.g. name), and are
259 // computed from the first candidate, in the constructor.
260 // Others vary per candidate, so add() must be called for remaining candidates.
261 struct CodeCompletionBuilder {
262   CodeCompletionBuilder(ASTContext &ASTCtx, const CompletionCandidate &C,
263                         CodeCompletionString *SemaCCS,
264                         llvm::ArrayRef<std::string> QueryScopes,
265                         const IncludeInserter &Includes,
266                         llvm::StringRef FileName,
267                         CodeCompletionContext::Kind ContextKind,
268                         const CodeCompleteOptions &Opts)
269       : ASTCtx(ASTCtx), ExtractDocumentation(Opts.IncludeComments),
270         EnableFunctionArgSnippets(Opts.EnableFunctionArgSnippets) {
271     add(C, SemaCCS);
272     if (C.SemaResult) {
273       Completion.Origin |= SymbolOrigin::AST;
274       Completion.Name = llvm::StringRef(SemaCCS->getTypedText());
275       if (Completion.Scope.empty()) {
276         if ((C.SemaResult->Kind == CodeCompletionResult::RK_Declaration) ||
277             (C.SemaResult->Kind == CodeCompletionResult::RK_Pattern))
278           if (const auto *D = C.SemaResult->getDeclaration())
279             if (const auto *ND = dyn_cast<NamedDecl>(D))
280               Completion.Scope =
281                   splitQualifiedName(printQualifiedName(*ND)).first;
282       }
283       Completion.Kind = toCompletionItemKind(
284           C.SemaResult->Kind, C.SemaResult->Declaration, ContextKind);
285       // Sema could provide more info on whether the completion was a file or
286       // folder.
287       if (Completion.Kind == CompletionItemKind::File &&
288           Completion.Name.back() == '/')
289         Completion.Kind = CompletionItemKind::Folder;
290       for (const auto &FixIt : C.SemaResult->FixIts) {
291         Completion.FixIts.push_back(
292             toTextEdit(FixIt, ASTCtx.getSourceManager(), ASTCtx.getLangOpts()));
293       }
294       llvm::sort(Completion.FixIts, [](const TextEdit &X, const TextEdit &Y) {
295         return std::tie(X.range.start.line, X.range.start.character) <
296                std::tie(Y.range.start.line, Y.range.start.character);
297       });
298       Completion.Deprecated |=
299           (C.SemaResult->Availability == CXAvailability_Deprecated);
300     }
301     if (C.IndexResult) {
302       Completion.Origin |= C.IndexResult->Origin;
303       if (Completion.Scope.empty())
304         Completion.Scope = C.IndexResult->Scope;
305       if (Completion.Kind == CompletionItemKind::Missing)
306         Completion.Kind = toCompletionItemKind(C.IndexResult->SymInfo.Kind);
307       if (Completion.Name.empty())
308         Completion.Name = C.IndexResult->Name;
309       // If the completion was visible to Sema, no qualifier is needed. This
310       // avoids unneeded qualifiers in cases like with `using ns::X`.
311       if (Completion.RequiredQualifier.empty() && !C.SemaResult) {
312         llvm::StringRef ShortestQualifier = C.IndexResult->Scope;
313         for (llvm::StringRef Scope : QueryScopes) {
314           llvm::StringRef Qualifier = C.IndexResult->Scope;
315           if (Qualifier.consume_front(Scope) &&
316               Qualifier.size() < ShortestQualifier.size())
317             ShortestQualifier = Qualifier;
318         }
319         Completion.RequiredQualifier = ShortestQualifier;
320       }
321       Completion.Deprecated |= (C.IndexResult->Flags & Symbol::Deprecated);
322     }
323 
324     // Turn absolute path into a literal string that can be #included.
325     auto Inserted = [&](llvm::StringRef Header)
326         -> llvm::Expected<std::pair<std::string, bool>> {
327       auto ResolvedDeclaring =
328           toHeaderFile(C.IndexResult->CanonicalDeclaration.FileURI, FileName);
329       if (!ResolvedDeclaring)
330         return ResolvedDeclaring.takeError();
331       auto ResolvedInserted = toHeaderFile(Header, FileName);
332       if (!ResolvedInserted)
333         return ResolvedInserted.takeError();
334       return std::make_pair(
335           Includes.calculateIncludePath(*ResolvedDeclaring, *ResolvedInserted),
336           Includes.shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
337     };
338     bool ShouldInsert = C.headerToInsertIfAllowed().hasValue();
339     // Calculate include paths and edits for all possible headers.
340     for (const auto &Inc : C.RankedIncludeHeaders) {
341       if (auto ToInclude = Inserted(Inc)) {
342         CodeCompletion::IncludeCandidate Include;
343         Include.Header = ToInclude->first;
344         if (ToInclude->second && ShouldInsert)
345           Include.Insertion = Includes.insert(ToInclude->first);
346         Completion.Includes.push_back(std::move(Include));
347       } else
348         log("Failed to generate include insertion edits for adding header "
349             "(FileURI='{0}', IncludeHeader='{1}') into {2}",
350             C.IndexResult->CanonicalDeclaration.FileURI, Inc, FileName);
351     }
352     // Prefer includes that do not need edits (i.e. already exist).
353     std::stable_partition(Completion.Includes.begin(),
354                           Completion.Includes.end(),
355                           [](const CodeCompletion::IncludeCandidate &I) {
356                             return !I.Insertion.hasValue();
357                           });
358   }
359 
360   void add(const CompletionCandidate &C, CodeCompletionString *SemaCCS) {
361     assert(bool(C.SemaResult) == bool(SemaCCS));
362     Bundled.emplace_back();
363     BundledEntry &S = Bundled.back();
364     if (C.SemaResult) {
365       getSignature(*SemaCCS, &S.Signature, &S.SnippetSuffix,
366                    &Completion.RequiredQualifier);
367       S.ReturnType = getReturnType(*SemaCCS);
368     } else if (C.IndexResult) {
369       S.Signature = C.IndexResult->Signature;
370       S.SnippetSuffix = C.IndexResult->CompletionSnippetSuffix;
371       S.ReturnType = C.IndexResult->ReturnType;
372     }
373     if (ExtractDocumentation && Completion.Documentation.empty()) {
374       if (C.IndexResult)
375         Completion.Documentation = C.IndexResult->Documentation;
376       else if (C.SemaResult)
377         Completion.Documentation = getDocComment(ASTCtx, *C.SemaResult,
378                                                  /*CommentsFromHeader=*/false);
379     }
380   }
381 
382   CodeCompletion build() {
383     Completion.ReturnType = summarizeReturnType();
384     Completion.Signature = summarizeSignature();
385     Completion.SnippetSuffix = summarizeSnippet();
386     Completion.BundleSize = Bundled.size();
387     return std::move(Completion);
388   }
389 
390 private:
391   struct BundledEntry {
392     std::string SnippetSuffix;
393     std::string Signature;
394     std::string ReturnType;
395   };
396 
397   // If all BundledEntrys have the same value for a property, return it.
398   template <std::string BundledEntry::*Member>
399   const std::string *onlyValue() const {
400     auto B = Bundled.begin(), E = Bundled.end();
401     for (auto I = B + 1; I != E; ++I)
402       if (I->*Member != B->*Member)
403         return nullptr;
404     return &(B->*Member);
405   }
406 
407   template <bool BundledEntry::*Member> const bool *onlyValue() const {
408     auto B = Bundled.begin(), E = Bundled.end();
409     for (auto I = B + 1; I != E; ++I)
410       if (I->*Member != B->*Member)
411         return nullptr;
412     return &(B->*Member);
413   }
414 
415   std::string summarizeReturnType() const {
416     if (auto *RT = onlyValue<&BundledEntry::ReturnType>())
417       return *RT;
418     return "";
419   }
420 
421   std::string summarizeSnippet() const {
422     auto *Snippet = onlyValue<&BundledEntry::SnippetSuffix>();
423     if (!Snippet)
424       // All bundles are function calls.
425       // FIXME(ibiryukov): sometimes add template arguments to a snippet, e.g.
426       // we need to complete 'forward<$1>($0)'.
427       return "($0)";
428     if (EnableFunctionArgSnippets)
429       return *Snippet;
430 
431     // Replace argument snippets with a simplified pattern.
432     if (Snippet->empty())
433       return "";
434     if (Completion.Kind == CompletionItemKind::Function ||
435         Completion.Kind == CompletionItemKind::Method) {
436       // Functions snippets can be of 2 types:
437       // - containing only function arguments, e.g.
438       //   foo(${1:int p1}, ${2:int p2});
439       //   We transform this pattern to '($0)' or '()'.
440       // - template arguments and function arguments, e.g.
441       //   foo<${1:class}>(${2:int p1}).
442       //   We transform this pattern to '<$1>()$0' or '<$0>()'.
443 
444       bool EmptyArgs = llvm::StringRef(*Snippet).endswith("()");
445       if (Snippet->front() == '<')
446         return EmptyArgs ? "<$1>()$0" : "<$1>($0)";
447       if (Snippet->front() == '(')
448         return EmptyArgs ? "()" : "($0)";
449       return *Snippet; // Not an arg snippet?
450     }
451     if (Completion.Kind == CompletionItemKind::Reference ||
452         Completion.Kind == CompletionItemKind::Class) {
453       if (Snippet->front() != '<')
454         return *Snippet; // Not an arg snippet?
455 
456       // Classes and template using aliases can only have template arguments,
457       // e.g. Foo<${1:class}>.
458       if (llvm::StringRef(*Snippet).endswith("<>"))
459         return "<>"; // can happen with defaulted template arguments.
460       return "<$0>";
461     }
462     return *Snippet;
463   }
464 
465   std::string summarizeSignature() const {
466     if (auto *Signature = onlyValue<&BundledEntry::Signature>())
467       return *Signature;
468     // All bundles are function calls.
469     return "(…)";
470   }
471 
472   ASTContext &ASTCtx;
473   CodeCompletion Completion;
474   llvm::SmallVector<BundledEntry, 1> Bundled;
475   bool ExtractDocumentation;
476   bool EnableFunctionArgSnippets;
477 };
478 
479 // Determine the symbol ID for a Sema code completion result, if possible.
480 llvm::Optional<SymbolID> getSymbolID(const CodeCompletionResult &R,
481                                      const SourceManager &SM) {
482   switch (R.Kind) {
483   case CodeCompletionResult::RK_Declaration:
484   case CodeCompletionResult::RK_Pattern: {
485     return clang::clangd::getSymbolID(R.Declaration);
486   }
487   case CodeCompletionResult::RK_Macro:
488     return clang::clangd::getSymbolID(*R.Macro, R.MacroDefInfo, SM);
489   case CodeCompletionResult::RK_Keyword:
490     return None;
491   }
492   llvm_unreachable("unknown CodeCompletionResult kind");
493 }
494 
495 // Scopes of the paritial identifier we're trying to complete.
496 // It is used when we query the index for more completion results.
497 struct SpecifiedScope {
498   // The scopes we should look in, determined by Sema.
499   //
500   // If the qualifier was fully resolved, we look for completions in these
501   // scopes; if there is an unresolved part of the qualifier, it should be
502   // resolved within these scopes.
503   //
504   // Examples of qualified completion:
505   //
506   //   "::vec"                                      => {""}
507   //   "using namespace std; ::vec^"                => {"", "std::"}
508   //   "namespace ns {using namespace std;} ns::^"  => {"ns::", "std::"}
509   //   "std::vec^"                                  => {""}  // "std" unresolved
510   //
511   // Examples of unqualified completion:
512   //
513   //   "vec^"                                       => {""}
514   //   "using namespace std; vec^"                  => {"", "std::"}
515   //   "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""}
516   //
517   // "" for global namespace, "ns::" for normal namespace.
518   std::vector<std::string> AccessibleScopes;
519   // The full scope qualifier as typed by the user (without the leading "::").
520   // Set if the qualifier is not fully resolved by Sema.
521   llvm::Optional<std::string> UnresolvedQualifier;
522 
523   // Construct scopes being queried in indexes. The results are deduplicated.
524   // This method format the scopes to match the index request representation.
525   std::vector<std::string> scopesForIndexQuery() {
526     std::set<std::string> Results;
527     for (llvm::StringRef AS : AccessibleScopes)
528       Results.insert(
529           ((UnresolvedQualifier ? *UnresolvedQualifier : "") + AS).str());
530     return {Results.begin(), Results.end()};
531   }
532 };
533 
534 // Get all scopes that will be queried in indexes and whether symbols from
535 // any scope is allowed. The first scope in the list is the preferred scope
536 // (e.g. enclosing namespace).
537 std::pair<std::vector<std::string>, bool>
538 getQueryScopes(CodeCompletionContext &CCContext, const Sema &CCSema,
539                const CodeCompleteOptions &Opts) {
540   auto GetAllAccessibleScopes = [](CodeCompletionContext &CCContext) {
541     SpecifiedScope Info;
542     for (auto *Context : CCContext.getVisitedContexts()) {
543       if (isa<TranslationUnitDecl>(Context))
544         Info.AccessibleScopes.push_back(""); // global namespace
545       else if (isa<NamespaceDecl>(Context))
546         Info.AccessibleScopes.push_back(printNamespaceScope(*Context));
547     }
548     return Info;
549   };
550 
551   auto SS = CCContext.getCXXScopeSpecifier();
552 
553   // Unqualified completion (e.g. "vec^").
554   if (!SS) {
555     std::vector<std::string> Scopes;
556     std::string EnclosingScope = printNamespaceScope(*CCSema.CurContext);
557     Scopes.push_back(EnclosingScope);
558     for (auto &S : GetAllAccessibleScopes(CCContext).scopesForIndexQuery()) {
559       if (EnclosingScope != S)
560         Scopes.push_back(std::move(S));
561     }
562     // Allow AllScopes completion only for there is no explicit scope qualifier.
563     return {Scopes, Opts.AllScopes};
564   }
565 
566   // Qualified completion ("std::vec^"), we have two cases depending on whether
567   // the qualifier can be resolved by Sema.
568   if ((*SS)->isValid()) { // Resolved qualifier.
569     return {GetAllAccessibleScopes(CCContext).scopesForIndexQuery(), false};
570   }
571 
572   // Unresolved qualifier.
573   // FIXME: When Sema can resolve part of a scope chain (e.g.
574   // "known::unknown::id"), we should expand the known part ("known::") rather
575   // than treating the whole thing as unknown.
576   SpecifiedScope Info;
577   Info.AccessibleScopes.push_back(""); // global namespace
578 
579   Info.UnresolvedQualifier =
580       Lexer::getSourceText(CharSourceRange::getCharRange((*SS)->getRange()),
581                            CCSema.SourceMgr, clang::LangOptions())
582           .ltrim("::");
583   // Sema excludes the trailing "::".
584   if (!Info.UnresolvedQualifier->empty())
585     *Info.UnresolvedQualifier += "::";
586 
587   return {Info.scopesForIndexQuery(), false};
588 }
589 
590 // Should we perform index-based completion in a context of the specified kind?
591 // FIXME: consider allowing completion, but restricting the result types.
592 bool contextAllowsIndex(enum CodeCompletionContext::Kind K) {
593   switch (K) {
594   case CodeCompletionContext::CCC_TopLevel:
595   case CodeCompletionContext::CCC_ObjCInterface:
596   case CodeCompletionContext::CCC_ObjCImplementation:
597   case CodeCompletionContext::CCC_ObjCIvarList:
598   case CodeCompletionContext::CCC_ClassStructUnion:
599   case CodeCompletionContext::CCC_Statement:
600   case CodeCompletionContext::CCC_Expression:
601   case CodeCompletionContext::CCC_ObjCMessageReceiver:
602   case CodeCompletionContext::CCC_EnumTag:
603   case CodeCompletionContext::CCC_UnionTag:
604   case CodeCompletionContext::CCC_ClassOrStructTag:
605   case CodeCompletionContext::CCC_ObjCProtocolName:
606   case CodeCompletionContext::CCC_Namespace:
607   case CodeCompletionContext::CCC_Type:
608   case CodeCompletionContext::CCC_ParenthesizedExpression:
609   case CodeCompletionContext::CCC_ObjCInterfaceName:
610   case CodeCompletionContext::CCC_ObjCCategoryName:
611   case CodeCompletionContext::CCC_Symbol:
612   case CodeCompletionContext::CCC_SymbolOrNewName:
613     return true;
614   case CodeCompletionContext::CCC_OtherWithMacros:
615   case CodeCompletionContext::CCC_DotMemberAccess:
616   case CodeCompletionContext::CCC_ArrowMemberAccess:
617   case CodeCompletionContext::CCC_ObjCPropertyAccess:
618   case CodeCompletionContext::CCC_MacroName:
619   case CodeCompletionContext::CCC_MacroNameUse:
620   case CodeCompletionContext::CCC_PreprocessorExpression:
621   case CodeCompletionContext::CCC_PreprocessorDirective:
622   case CodeCompletionContext::CCC_SelectorName:
623   case CodeCompletionContext::CCC_TypeQualifiers:
624   case CodeCompletionContext::CCC_ObjCInstanceMessage:
625   case CodeCompletionContext::CCC_ObjCClassMessage:
626   case CodeCompletionContext::CCC_IncludedFile:
627   // FIXME: Provide identifier based completions for the following contexts:
628   case CodeCompletionContext::CCC_Other: // Be conservative.
629   case CodeCompletionContext::CCC_NaturalLanguage:
630   case CodeCompletionContext::CCC_Recovery:
631   case CodeCompletionContext::CCC_NewName:
632     return false;
633   }
634   llvm_unreachable("unknown code completion context");
635 }
636 
637 static bool isInjectedClass(const NamedDecl &D) {
638   if (auto *R = dyn_cast_or_null<RecordDecl>(&D))
639     if (R->isInjectedClassName())
640       return true;
641   return false;
642 }
643 
644 // Some member calls are blacklisted because they're so rarely useful.
645 static bool isBlacklistedMember(const NamedDecl &D) {
646   // Destructor completion is rarely useful, and works inconsistently.
647   // (s.^ completes ~string, but s.~st^ is an error).
648   if (D.getKind() == Decl::CXXDestructor)
649     return true;
650   // Injected name may be useful for A::foo(), but who writes A::A::foo()?
651   if (isInjectedClass(D))
652     return true;
653   // Explicit calls to operators are also rare.
654   auto NameKind = D.getDeclName().getNameKind();
655   if (NameKind == DeclarationName::CXXOperatorName ||
656       NameKind == DeclarationName::CXXLiteralOperatorName ||
657       NameKind == DeclarationName::CXXConversionFunctionName)
658     return true;
659   return false;
660 }
661 
662 // The CompletionRecorder captures Sema code-complete output, including context.
663 // It filters out ignored results (but doesn't apply fuzzy-filtering yet).
664 // It doesn't do scoring or conversion to CompletionItem yet, as we want to
665 // merge with index results first.
666 // Generally the fields and methods of this object should only be used from
667 // within the callback.
668 struct CompletionRecorder : public CodeCompleteConsumer {
669   CompletionRecorder(const CodeCompleteOptions &Opts,
670                      llvm::unique_function<void()> ResultsCallback)
671       : CodeCompleteConsumer(Opts.getClangCompleteOpts(),
672                              /*OutputIsBinary=*/false),
673         CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
674         CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
675         CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) {
676     assert(this->ResultsCallback);
677   }
678 
679   std::vector<CodeCompletionResult> Results;
680   CodeCompletionContext CCContext;
681   Sema *CCSema = nullptr; // Sema that created the results.
682   // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead?
683 
684   void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context,
685                                   CodeCompletionResult *InResults,
686                                   unsigned NumResults) override final {
687     // Results from recovery mode are generally useless, and the callback after
688     // recovery (if any) is usually more interesting. To make sure we handle the
689     // future callback from sema, we just ignore all callbacks in recovery mode,
690     // as taking only results from recovery mode results in poor completion
691     // results.
692     // FIXME: in case there is no future sema completion callback after the
693     // recovery mode, we might still want to provide some results (e.g. trivial
694     // identifier-based completion).
695     if (Context.getKind() == CodeCompletionContext::CCC_Recovery) {
696       log("Code complete: Ignoring sema code complete callback with Recovery "
697           "context.");
698       return;
699     }
700     // If a callback is called without any sema result and the context does not
701     // support index-based completion, we simply skip it to give way to
702     // potential future callbacks with results.
703     if (NumResults == 0 && !contextAllowsIndex(Context.getKind()))
704       return;
705     if (CCSema) {
706       log("Multiple code complete callbacks (parser backtracked?). "
707           "Dropping results from context {0}, keeping results from {1}.",
708           getCompletionKindString(Context.getKind()),
709           getCompletionKindString(this->CCContext.getKind()));
710       return;
711     }
712     // Record the completion context.
713     CCSema = &S;
714     CCContext = Context;
715 
716     // Retain the results we might want.
717     for (unsigned I = 0; I < NumResults; ++I) {
718       auto &Result = InResults[I];
719       // Class members that are shadowed by subclasses are usually noise.
720       if (Result.Hidden && Result.Declaration &&
721           Result.Declaration->isCXXClassMember())
722         continue;
723       if (!Opts.IncludeIneligibleResults &&
724           (Result.Availability == CXAvailability_NotAvailable ||
725            Result.Availability == CXAvailability_NotAccessible))
726         continue;
727       if (Result.Declaration &&
728           !Context.getBaseType().isNull() // is this a member-access context?
729           && isBlacklistedMember(*Result.Declaration))
730         continue;
731       // Skip injected class name when no class scope is not explicitly set.
732       // E.g. show injected A::A in `using A::A^` but not in "A^".
733       if (Result.Declaration && !Context.getCXXScopeSpecifier().hasValue() &&
734           isInjectedClass(*Result.Declaration))
735         continue;
736       // We choose to never append '::' to completion results in clangd.
737       Result.StartsNestedNameSpecifier = false;
738       Results.push_back(Result);
739     }
740     ResultsCallback();
741   }
742 
743   CodeCompletionAllocator &getAllocator() override { return *CCAllocator; }
744   CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
745 
746   // Returns the filtering/sorting name for Result, which must be from Results.
747   // Returned string is owned by this recorder (or the AST).
748   llvm::StringRef getName(const CodeCompletionResult &Result) {
749     switch (Result.Kind) {
750     case CodeCompletionResult::RK_Declaration:
751       if (auto *ID = Result.Declaration->getIdentifier())
752         return ID->getName();
753       break;
754     case CodeCompletionResult::RK_Keyword:
755       return Result.Keyword;
756     case CodeCompletionResult::RK_Macro:
757       return Result.Macro->getName();
758     case CodeCompletionResult::RK_Pattern:
759       return Result.Pattern->getTypedText();
760     }
761     auto *CCS = codeCompletionString(Result);
762     return CCS->getTypedText();
763   }
764 
765   // Build a CodeCompletion string for R, which must be from Results.
766   // The CCS will be owned by this recorder.
767   CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) {
768     // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway.
769     return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString(
770         *CCSema, CCContext, *CCAllocator, CCTUInfo,
771         /*IncludeBriefComments=*/false);
772   }
773 
774 private:
775   CodeCompleteOptions Opts;
776   std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
777   CodeCompletionTUInfo CCTUInfo;
778   llvm::unique_function<void()> ResultsCallback;
779 };
780 
781 struct ScoredSignature {
782   // When set, requires documentation to be requested from the index with this
783   // ID.
784   llvm::Optional<SymbolID> IDForDoc;
785   SignatureInformation Signature;
786   SignatureQualitySignals Quality;
787 };
788 
789 class SignatureHelpCollector final : public CodeCompleteConsumer {
790 public:
791   SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
792                          const SymbolIndex *Index, SignatureHelp &SigHelp)
793       : CodeCompleteConsumer(CodeCompleteOpts,
794                              /*OutputIsBinary=*/false),
795         SigHelp(SigHelp),
796         Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
797         CCTUInfo(Allocator), Index(Index) {}
798 
799   void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
800                                  OverloadCandidate *Candidates,
801                                  unsigned NumCandidates,
802                                  SourceLocation OpenParLoc) override {
803     assert(!OpenParLoc.isInvalid());
804     SourceManager &SrcMgr = S.getSourceManager();
805     OpenParLoc = SrcMgr.getFileLoc(OpenParLoc);
806     if (SrcMgr.isInMainFile(OpenParLoc))
807       SigHelp.argListStart = sourceLocToPosition(SrcMgr, OpenParLoc);
808     else
809       elog("Location oustide main file in signature help: {0}",
810            OpenParLoc.printToString(SrcMgr));
811 
812     std::vector<ScoredSignature> ScoredSignatures;
813     SigHelp.signatures.reserve(NumCandidates);
814     ScoredSignatures.reserve(NumCandidates);
815     // FIXME(rwols): How can we determine the "active overload candidate"?
816     // Right now the overloaded candidates seem to be provided in a "best fit"
817     // order, so I'm not too worried about this.
818     SigHelp.activeSignature = 0;
819     assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
820            "too many arguments");
821     SigHelp.activeParameter = static_cast<int>(CurrentArg);
822     for (unsigned I = 0; I < NumCandidates; ++I) {
823       OverloadCandidate Candidate = Candidates[I];
824       // We want to avoid showing instantiated signatures, because they may be
825       // long in some cases (e.g. when 'T' is substituted with 'std::string', we
826       // would get 'std::basic_string<char>').
827       if (auto *Func = Candidate.getFunction()) {
828         if (auto *Pattern = Func->getTemplateInstantiationPattern())
829           Candidate = OverloadCandidate(Pattern);
830       }
831 
832       const auto *CCS = Candidate.CreateSignatureString(
833           CurrentArg, S, *Allocator, CCTUInfo, true);
834       assert(CCS && "Expected the CodeCompletionString to be non-null");
835       ScoredSignatures.push_back(processOverloadCandidate(
836           Candidate, *CCS,
837           Candidate.getFunction()
838               ? getDeclComment(S.getASTContext(), *Candidate.getFunction())
839               : ""));
840     }
841 
842     // Sema does not load the docs from the preamble, so we need to fetch extra
843     // docs from the index instead.
844     llvm::DenseMap<SymbolID, std::string> FetchedDocs;
845     if (Index) {
846       LookupRequest IndexRequest;
847       for (const auto &S : ScoredSignatures) {
848         if (!S.IDForDoc)
849           continue;
850         IndexRequest.IDs.insert(*S.IDForDoc);
851       }
852       Index->lookup(IndexRequest, [&](const Symbol &S) {
853         if (!S.Documentation.empty())
854           FetchedDocs[S.ID] = S.Documentation;
855       });
856       log("SigHelp: requested docs for {0} symbols from the index, got {1} "
857           "symbols with non-empty docs in the response",
858           IndexRequest.IDs.size(), FetchedDocs.size());
859     }
860 
861     llvm::sort(ScoredSignatures, [](const ScoredSignature &L,
862                                     const ScoredSignature &R) {
863       // Ordering follows:
864       // - Less number of parameters is better.
865       // - Function is better than FunctionType which is better than
866       // Function Template.
867       // - High score is better.
868       // - Shorter signature is better.
869       // - Alphebatically smaller is better.
870       if (L.Quality.NumberOfParameters != R.Quality.NumberOfParameters)
871         return L.Quality.NumberOfParameters < R.Quality.NumberOfParameters;
872       if (L.Quality.NumberOfOptionalParameters !=
873           R.Quality.NumberOfOptionalParameters)
874         return L.Quality.NumberOfOptionalParameters <
875                R.Quality.NumberOfOptionalParameters;
876       if (L.Quality.Kind != R.Quality.Kind) {
877         using OC = CodeCompleteConsumer::OverloadCandidate;
878         switch (L.Quality.Kind) {
879         case OC::CK_Function:
880           return true;
881         case OC::CK_FunctionType:
882           return R.Quality.Kind != OC::CK_Function;
883         case OC::CK_FunctionTemplate:
884           return false;
885         }
886         llvm_unreachable("Unknown overload candidate type.");
887       }
888       if (L.Signature.label.size() != R.Signature.label.size())
889         return L.Signature.label.size() < R.Signature.label.size();
890       return L.Signature.label < R.Signature.label;
891     });
892 
893     for (auto &SS : ScoredSignatures) {
894       auto IndexDocIt =
895           SS.IDForDoc ? FetchedDocs.find(*SS.IDForDoc) : FetchedDocs.end();
896       if (IndexDocIt != FetchedDocs.end())
897         SS.Signature.documentation = IndexDocIt->second;
898 
899       SigHelp.signatures.push_back(std::move(SS.Signature));
900     }
901   }
902 
903   GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
904 
905   CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
906 
907 private:
908   // FIXME(ioeric): consider moving CodeCompletionString logic here to
909   // CompletionString.h.
910   ScoredSignature processOverloadCandidate(const OverloadCandidate &Candidate,
911                                            const CodeCompletionString &CCS,
912                                            llvm::StringRef DocComment) const {
913     SignatureInformation Signature;
914     SignatureQualitySignals Signal;
915     const char *ReturnType = nullptr;
916 
917     Signature.documentation = formatDocumentation(CCS, DocComment);
918     Signal.Kind = Candidate.getKind();
919 
920     for (const auto &Chunk : CCS) {
921       switch (Chunk.Kind) {
922       case CodeCompletionString::CK_ResultType:
923         // A piece of text that describes the type of an entity or,
924         // for functions and methods, the return type.
925         assert(!ReturnType && "Unexpected CK_ResultType");
926         ReturnType = Chunk.Text;
927         break;
928       case CodeCompletionString::CK_Placeholder:
929         // A string that acts as a placeholder for, e.g., a function call
930         // argument.
931         // Intentional fallthrough here.
932       case CodeCompletionString::CK_CurrentParameter: {
933         // A piece of text that describes the parameter that corresponds to
934         // the code-completion location within a function call, message send,
935         // macro invocation, etc.
936         Signature.label += Chunk.Text;
937         ParameterInformation Info;
938         Info.label = Chunk.Text;
939         Signature.parameters.push_back(std::move(Info));
940         Signal.NumberOfParameters++;
941         Signal.ContainsActiveParameter = true;
942         break;
943       }
944       case CodeCompletionString::CK_Optional: {
945         // The rest of the parameters are defaulted/optional.
946         assert(Chunk.Optional &&
947                "Expected the optional code completion string to be non-null.");
948         Signature.label += getOptionalParameters(*Chunk.Optional,
949                                                  Signature.parameters, Signal);
950         break;
951       }
952       case CodeCompletionString::CK_VerticalSpace:
953         break;
954       default:
955         Signature.label += Chunk.Text;
956         break;
957       }
958     }
959     if (ReturnType) {
960       Signature.label += " -> ";
961       Signature.label += ReturnType;
962     }
963     dlog("Signal for {0}: {1}", Signature, Signal);
964     ScoredSignature Result;
965     Result.Signature = std::move(Signature);
966     Result.Quality = Signal;
967     Result.IDForDoc =
968         Result.Signature.documentation.empty() && Candidate.getFunction()
969             ? clangd::getSymbolID(Candidate.getFunction())
970             : None;
971     return Result;
972   }
973 
974   SignatureHelp &SigHelp;
975   std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
976   CodeCompletionTUInfo CCTUInfo;
977   const SymbolIndex *Index;
978 }; // SignatureHelpCollector
979 
980 struct SemaCompleteInput {
981   PathRef FileName;
982   const tooling::CompileCommand &Command;
983   const PreambleData *Preamble;
984   llvm::StringRef Contents;
985   Position Pos;
986   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS;
987   std::shared_ptr<PCHContainerOperations> PCHs;
988 };
989 
990 // Invokes Sema code completion on a file.
991 // If \p Includes is set, it will be updated based on the compiler invocation.
992 bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
993                       const clang::CodeCompleteOptions &Options,
994                       const SemaCompleteInput &Input,
995                       IncludeStructure *Includes = nullptr) {
996   trace::Span Tracer("Sema completion");
997   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = Input.VFS;
998   if (Input.Preamble && Input.Preamble->StatCache)
999     VFS = Input.Preamble->StatCache->getConsumingFS(std::move(VFS));
1000   ParseInputs ParseInput;
1001   ParseInput.CompileCommand = Input.Command;
1002   ParseInput.FS = VFS;
1003   ParseInput.Contents = Input.Contents;
1004   ParseInput.Opts = ParseOptions();
1005   auto CI = buildCompilerInvocation(ParseInput);
1006   if (!CI) {
1007     elog("Couldn't create CompilerInvocation");
1008     return false;
1009   }
1010   auto &FrontendOpts = CI->getFrontendOpts();
1011   FrontendOpts.SkipFunctionBodies = true;
1012   // Disable typo correction in Sema.
1013   CI->getLangOpts()->SpellChecking = false;
1014   // Setup code completion.
1015   FrontendOpts.CodeCompleteOpts = Options;
1016   FrontendOpts.CodeCompletionAt.FileName = Input.FileName;
1017   auto Offset = positionToOffset(Input.Contents, Input.Pos);
1018   if (!Offset) {
1019     elog("Code completion position was invalid {0}", Offset.takeError());
1020     return false;
1021   }
1022   std::tie(FrontendOpts.CodeCompletionAt.Line,
1023            FrontendOpts.CodeCompletionAt.Column) =
1024       offsetToClangLineColumn(Input.Contents, *Offset);
1025 
1026   std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1027       llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName);
1028   // The diagnostic options must be set before creating a CompilerInstance.
1029   CI->getDiagnosticOpts().IgnoreWarnings = true;
1030   // We reuse the preamble whether it's valid or not. This is a
1031   // correctness/performance tradeoff: building without a preamble is slow, and
1032   // completion is latency-sensitive.
1033   // However, if we're completing *inside* the preamble section of the draft,
1034   // overriding the preamble will break sema completion. Fortunately we can just
1035   // skip all includes in this case; these completions are really simple.
1036   bool CompletingInPreamble =
1037       ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0).Size >
1038       *Offset;
1039   // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
1040   // the remapped buffers do not get freed.
1041   IgnoreDiagnostics DummyDiagsConsumer;
1042   auto Clang = prepareCompilerInstance(
1043       std::move(CI),
1044       (Input.Preamble && !CompletingInPreamble) ? &Input.Preamble->Preamble
1045                                                 : nullptr,
1046       std::move(ContentsBuffer), std::move(Input.PCHs), std::move(VFS),
1047       DummyDiagsConsumer);
1048   Clang->getPreprocessorOpts().SingleFileParseMode = CompletingInPreamble;
1049   Clang->setCodeCompletionConsumer(Consumer.release());
1050 
1051   SyntaxOnlyAction Action;
1052   if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
1053     log("BeginSourceFile() failed when running codeComplete for {0}",
1054         Input.FileName);
1055     return false;
1056   }
1057   if (Includes)
1058     Clang->getPreprocessor().addPPCallbacks(
1059         collectIncludeStructureCallback(Clang->getSourceManager(), Includes));
1060   if (!Action.Execute()) {
1061     log("Execute() failed when running codeComplete for {0}", Input.FileName);
1062     return false;
1063   }
1064   Action.EndSourceFile();
1065 
1066   return true;
1067 }
1068 
1069 // Should we allow index completions in the specified context?
1070 bool allowIndex(CodeCompletionContext &CC) {
1071   if (!contextAllowsIndex(CC.getKind()))
1072     return false;
1073   // We also avoid ClassName::bar (but allow namespace::bar).
1074   auto Scope = CC.getCXXScopeSpecifier();
1075   if (!Scope)
1076     return true;
1077   NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
1078   if (!NameSpec)
1079     return true;
1080   // We only query the index when qualifier is a namespace.
1081   // If it's a class, we rely solely on sema completions.
1082   switch (NameSpec->getKind()) {
1083   case NestedNameSpecifier::Global:
1084   case NestedNameSpecifier::Namespace:
1085   case NestedNameSpecifier::NamespaceAlias:
1086     return true;
1087   case NestedNameSpecifier::Super:
1088   case NestedNameSpecifier::TypeSpec:
1089   case NestedNameSpecifier::TypeSpecWithTemplate:
1090   // Unresolved inside a template.
1091   case NestedNameSpecifier::Identifier:
1092     return false;
1093   }
1094   llvm_unreachable("invalid NestedNameSpecifier kind");
1095 }
1096 
1097 std::future<SymbolSlab> startAsyncFuzzyFind(const SymbolIndex &Index,
1098                                             const FuzzyFindRequest &Req) {
1099   return runAsync<SymbolSlab>([&Index, Req]() {
1100     trace::Span Tracer("Async fuzzyFind");
1101     SymbolSlab::Builder Syms;
1102     Index.fuzzyFind(Req, [&Syms](const Symbol &Sym) { Syms.insert(Sym); });
1103     return std::move(Syms).build();
1104   });
1105 }
1106 
1107 // Creates a `FuzzyFindRequest` based on the cached index request from the
1108 // last completion, if any, and the speculated completion filter text in the
1109 // source code.
1110 llvm::Optional<FuzzyFindRequest>
1111 speculativeFuzzyFindRequestForCompletion(FuzzyFindRequest CachedReq,
1112                                          PathRef File, llvm::StringRef Content,
1113                                          Position Pos) {
1114   auto Filter = speculateCompletionFilter(Content, Pos);
1115   if (!Filter) {
1116     elog("Failed to speculate filter text for code completion at Pos "
1117          "{0}:{1}: {2}",
1118          Pos.line, Pos.character, Filter.takeError());
1119     return None;
1120   }
1121   CachedReq.Query = *Filter;
1122   return CachedReq;
1123 }
1124 
1125 // Runs Sema-based (AST) and Index-based completion, returns merged results.
1126 //
1127 // There are a few tricky considerations:
1128 //   - the AST provides information needed for the index query (e.g. which
1129 //     namespaces to search in). So Sema must start first.
1130 //   - we only want to return the top results (Opts.Limit).
1131 //     Building CompletionItems for everything else is wasteful, so we want to
1132 //     preserve the "native" format until we're done with scoring.
1133 //   - the data underlying Sema completion items is owned by the AST and various
1134 //     other arenas, which must stay alive for us to build CompletionItems.
1135 //   - we may get duplicate results from Sema and the Index, we need to merge.
1136 //
1137 // So we start Sema completion first, and do all our work in its callback.
1138 // We use the Sema context information to query the index.
1139 // Then we merge the two result sets, producing items that are Sema/Index/Both.
1140 // These items are scored, and the top N are synthesized into the LSP response.
1141 // Finally, we can clean up the data structures created by Sema completion.
1142 //
1143 // Main collaborators are:
1144 //   - semaCodeComplete sets up the compiler machinery to run code completion.
1145 //   - CompletionRecorder captures Sema completion results, including context.
1146 //   - SymbolIndex (Opts.Index) provides index completion results as Symbols
1147 //   - CompletionCandidates are the result of merging Sema and Index results.
1148 //     Each candidate points to an underlying CodeCompletionResult (Sema), a
1149 //     Symbol (Index), or both. It computes the result quality score.
1150 //     CompletionCandidate also does conversion to CompletionItem (at the end).
1151 //   - FuzzyMatcher scores how the candidate matches the partial identifier.
1152 //     This score is combined with the result quality score for the final score.
1153 //   - TopN determines the results with the best score.
1154 class CodeCompleteFlow {
1155   PathRef FileName;
1156   IncludeStructure Includes;           // Complete once the compiler runs.
1157   SpeculativeFuzzyFind *SpecFuzzyFind; // Can be nullptr.
1158   const CodeCompleteOptions &Opts;
1159 
1160   // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup.
1161   CompletionRecorder *Recorder = nullptr;
1162   int NSema = 0, NIndex = 0, NBoth = 0; // Counters for logging.
1163   bool Incomplete = false;       // Would more be available with a higher limit?
1164   llvm::Optional<FuzzyMatcher> Filter;  // Initialized once Sema runs.
1165   std::vector<std::string> QueryScopes; // Initialized once Sema runs.
1166   // Initialized once QueryScopes is initialized, if there are scopes.
1167   llvm::Optional<ScopeDistance> ScopeProximity;
1168   llvm::Optional<OpaqueType> PreferredType; // Initialized once Sema runs.
1169   // Whether to query symbols from any scope. Initialized once Sema runs.
1170   bool AllScopes = false;
1171   // Include-insertion and proximity scoring rely on the include structure.
1172   // This is available after Sema has run.
1173   llvm::Optional<IncludeInserter> Inserter;  // Available during runWithSema.
1174   llvm::Optional<URIDistance> FileProximity; // Initialized once Sema runs.
1175   /// Speculative request based on the cached request and the filter text before
1176   /// the cursor.
1177   /// Initialized right before sema run. This is only set if `SpecFuzzyFind` is
1178   /// set and contains a cached request.
1179   llvm::Optional<FuzzyFindRequest> SpecReq;
1180 
1181 public:
1182   // A CodeCompleteFlow object is only useful for calling run() exactly once.
1183   CodeCompleteFlow(PathRef FileName, const IncludeStructure &Includes,
1184                    SpeculativeFuzzyFind *SpecFuzzyFind,
1185                    const CodeCompleteOptions &Opts)
1186       : FileName(FileName), Includes(Includes), SpecFuzzyFind(SpecFuzzyFind),
1187         Opts(Opts) {}
1188 
1189   CodeCompleteResult run(const SemaCompleteInput &SemaCCInput) && {
1190     trace::Span Tracer("CodeCompleteFlow");
1191     if (Opts.Index && SpecFuzzyFind && SpecFuzzyFind->CachedReq.hasValue()) {
1192       assert(!SpecFuzzyFind->Result.valid());
1193       if ((SpecReq = speculativeFuzzyFindRequestForCompletion(
1194                *SpecFuzzyFind->CachedReq, SemaCCInput.FileName,
1195                SemaCCInput.Contents, SemaCCInput.Pos)))
1196         SpecFuzzyFind->Result = startAsyncFuzzyFind(*Opts.Index, *SpecReq);
1197     }
1198 
1199     // We run Sema code completion first. It builds an AST and calculates:
1200     //   - completion results based on the AST.
1201     //   - partial identifier and context. We need these for the index query.
1202     CodeCompleteResult Output;
1203     auto RecorderOwner = llvm::make_unique<CompletionRecorder>(Opts, [&]() {
1204       assert(Recorder && "Recorder is not set");
1205       auto Style = getFormatStyleForFile(
1206           SemaCCInput.FileName, SemaCCInput.Contents, SemaCCInput.VFS.get());
1207       // If preprocessor was run, inclusions from preprocessor callback should
1208       // already be added to Includes.
1209       Inserter.emplace(
1210           SemaCCInput.FileName, SemaCCInput.Contents, Style,
1211           SemaCCInput.Command.Directory,
1212           Recorder->CCSema->getPreprocessor().getHeaderSearchInfo());
1213       for (const auto &Inc : Includes.MainFileIncludes)
1214         Inserter->addExisting(Inc);
1215 
1216       // Most of the cost of file proximity is in initializing the FileDistance
1217       // structures based on the observed includes, once per query. Conceptually
1218       // that happens here (though the per-URI-scheme initialization is lazy).
1219       // The per-result proximity scoring is (amortized) very cheap.
1220       FileDistanceOptions ProxOpts{}; // Use defaults.
1221       const auto &SM = Recorder->CCSema->getSourceManager();
1222       llvm::StringMap<SourceParams> ProxSources;
1223       for (auto &Entry : Includes.includeDepth(
1224                SM.getFileEntryForID(SM.getMainFileID())->getName())) {
1225         auto &Source = ProxSources[Entry.getKey()];
1226         Source.Cost = Entry.getValue() * ProxOpts.IncludeCost;
1227         // Symbols near our transitive includes are good, but only consider
1228         // things in the same directory or below it. Otherwise there can be
1229         // many false positives.
1230         if (Entry.getValue() > 0)
1231           Source.MaxUpTraversals = 1;
1232       }
1233       FileProximity.emplace(ProxSources, ProxOpts);
1234 
1235       Output = runWithSema();
1236       Inserter.reset(); // Make sure this doesn't out-live Clang.
1237       SPAN_ATTACH(Tracer, "sema_completion_kind",
1238                   getCompletionKindString(Recorder->CCContext.getKind()));
1239       log("Code complete: sema context {0}, query scopes [{1}] (AnyScope={2}), "
1240           "expected type {3}",
1241           getCompletionKindString(Recorder->CCContext.getKind()),
1242           llvm::join(QueryScopes.begin(), QueryScopes.end(), ","), AllScopes,
1243           PreferredType ? Recorder->CCContext.getPreferredType().getAsString()
1244                         : "<none>");
1245     });
1246 
1247     Recorder = RecorderOwner.get();
1248 
1249     semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
1250                      SemaCCInput, &Includes);
1251 
1252     SPAN_ATTACH(Tracer, "sema_results", NSema);
1253     SPAN_ATTACH(Tracer, "index_results", NIndex);
1254     SPAN_ATTACH(Tracer, "merged_results", NBoth);
1255     SPAN_ATTACH(Tracer, "returned_results", int64_t(Output.Completions.size()));
1256     SPAN_ATTACH(Tracer, "incomplete", Output.HasMore);
1257     log("Code complete: {0} results from Sema, {1} from Index, "
1258         "{2} matched, {3} returned{4}.",
1259         NSema, NIndex, NBoth, Output.Completions.size(),
1260         Output.HasMore ? " (incomplete)" : "");
1261     assert(!Opts.Limit || Output.Completions.size() <= Opts.Limit);
1262     // We don't assert that isIncomplete means we hit a limit.
1263     // Indexes may choose to impose their own limits even if we don't have one.
1264     return Output;
1265   }
1266 
1267 private:
1268   // This is called by run() once Sema code completion is done, but before the
1269   // Sema data structures are torn down. It does all the real work.
1270   CodeCompleteResult runWithSema() {
1271     const auto &CodeCompletionRange = CharSourceRange::getCharRange(
1272         Recorder->CCSema->getPreprocessor().getCodeCompletionTokenRange());
1273     Range TextEditRange;
1274     // When we are getting completions with an empty identifier, for example
1275     //    std::vector<int> asdf;
1276     //    asdf.^;
1277     // Then the range will be invalid and we will be doing insertion, use
1278     // current cursor position in such cases as range.
1279     if (CodeCompletionRange.isValid()) {
1280       TextEditRange = halfOpenToRange(Recorder->CCSema->getSourceManager(),
1281                                       CodeCompletionRange);
1282     } else {
1283       const auto &Pos = sourceLocToPosition(
1284           Recorder->CCSema->getSourceManager(),
1285           Recorder->CCSema->getPreprocessor().getCodeCompletionLoc());
1286       TextEditRange.start = TextEditRange.end = Pos;
1287     }
1288     Filter = FuzzyMatcher(
1289         Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
1290     std::tie(QueryScopes, AllScopes) =
1291         getQueryScopes(Recorder->CCContext, *Recorder->CCSema, Opts);
1292     if (!QueryScopes.empty())
1293       ScopeProximity.emplace(QueryScopes);
1294     PreferredType =
1295         OpaqueType::fromType(Recorder->CCSema->getASTContext(),
1296                              Recorder->CCContext.getPreferredType());
1297     // Sema provides the needed context to query the index.
1298     // FIXME: in addition to querying for extra/overlapping symbols, we should
1299     //        explicitly request symbols corresponding to Sema results.
1300     //        We can use their signals even if the index can't suggest them.
1301     // We must copy index results to preserve them, but there are at most Limit.
1302     auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
1303                             ? queryIndex()
1304                             : SymbolSlab();
1305     trace::Span Tracer("Populate CodeCompleteResult");
1306     // Merge Sema and Index results, score them, and pick the winners.
1307     auto Top = mergeResults(Recorder->Results, IndexResults);
1308     CodeCompleteResult Output;
1309 
1310     // Convert the results to final form, assembling the expensive strings.
1311     for (auto &C : Top) {
1312       Output.Completions.push_back(toCodeCompletion(C.first));
1313       Output.Completions.back().Score = C.second;
1314       Output.Completions.back().CompletionTokenRange = TextEditRange;
1315     }
1316     Output.HasMore = Incomplete;
1317     Output.Context = Recorder->CCContext.getKind();
1318 
1319     return Output;
1320   }
1321 
1322   SymbolSlab queryIndex() {
1323     trace::Span Tracer("Query index");
1324     SPAN_ATTACH(Tracer, "limit", int64_t(Opts.Limit));
1325 
1326     // Build the query.
1327     FuzzyFindRequest Req;
1328     if (Opts.Limit)
1329       Req.Limit = Opts.Limit;
1330     Req.Query = Filter->pattern();
1331     Req.RestrictForCodeCompletion = true;
1332     Req.Scopes = QueryScopes;
1333     Req.AnyScope = AllScopes;
1334     // FIXME: we should send multiple weighted paths here.
1335     Req.ProximityPaths.push_back(FileName);
1336     if (PreferredType)
1337       Req.PreferredTypes.push_back(PreferredType->raw());
1338     vlog("Code complete: fuzzyFind({0:2})", toJSON(Req));
1339 
1340     if (SpecFuzzyFind)
1341       SpecFuzzyFind->NewReq = Req;
1342     if (SpecFuzzyFind && SpecFuzzyFind->Result.valid() && (*SpecReq == Req)) {
1343       vlog("Code complete: speculative fuzzy request matches the actual index "
1344            "request. Waiting for the speculative index results.");
1345       SPAN_ATTACH(Tracer, "Speculative results", true);
1346 
1347       trace::Span WaitSpec("Wait speculative results");
1348       return SpecFuzzyFind->Result.get();
1349     }
1350 
1351     SPAN_ATTACH(Tracer, "Speculative results", false);
1352 
1353     // Run the query against the index.
1354     SymbolSlab::Builder ResultsBuilder;
1355     if (Opts.Index->fuzzyFind(
1356             Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); }))
1357       Incomplete = true;
1358     return std::move(ResultsBuilder).build();
1359   }
1360 
1361   // Merges Sema and Index results where possible, to form CompletionCandidates.
1362   // Groups overloads if desired, to form CompletionCandidate::Bundles. The
1363   // bundles are scored and top results are returned, best to worst.
1364   std::vector<ScoredBundle>
1365   mergeResults(const std::vector<CodeCompletionResult> &SemaResults,
1366                const SymbolSlab &IndexResults) {
1367     trace::Span Tracer("Merge and score results");
1368     std::vector<CompletionCandidate::Bundle> Bundles;
1369     llvm::DenseMap<size_t, size_t> BundleLookup;
1370     auto AddToBundles = [&](const CodeCompletionResult *SemaResult,
1371                             const Symbol *IndexResult) {
1372       CompletionCandidate C;
1373       C.SemaResult = SemaResult;
1374       C.IndexResult = IndexResult;
1375       if (C.IndexResult)
1376         C.RankedIncludeHeaders = getRankedIncludes(*C.IndexResult);
1377       C.Name = IndexResult ? IndexResult->Name : Recorder->getName(*SemaResult);
1378       if (auto OverloadSet = Opts.BundleOverloads ? C.overloadSet() : 0) {
1379         auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1380         if (Ret.second)
1381           Bundles.emplace_back();
1382         Bundles[Ret.first->second].push_back(std::move(C));
1383       } else {
1384         Bundles.emplace_back();
1385         Bundles.back().push_back(std::move(C));
1386       }
1387     };
1388     llvm::DenseSet<const Symbol *> UsedIndexResults;
1389     auto CorrespondingIndexResult =
1390         [&](const CodeCompletionResult &SemaResult) -> const Symbol * {
1391       if (auto SymID =
1392               getSymbolID(SemaResult, Recorder->CCSema->getSourceManager())) {
1393         auto I = IndexResults.find(*SymID);
1394         if (I != IndexResults.end()) {
1395           UsedIndexResults.insert(&*I);
1396           return &*I;
1397         }
1398       }
1399       return nullptr;
1400     };
1401     // Emit all Sema results, merging them with Index results if possible.
1402     for (auto &SemaResult : Recorder->Results)
1403       AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult));
1404     // Now emit any Index-only results.
1405     for (const auto &IndexResult : IndexResults) {
1406       if (UsedIndexResults.count(&IndexResult))
1407         continue;
1408       AddToBundles(/*SemaResult=*/nullptr, &IndexResult);
1409     }
1410     // We only keep the best N results at any time, in "native" format.
1411     TopN<ScoredBundle, ScoredBundleGreater> Top(
1412         Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1413     for (auto &Bundle : Bundles)
1414       addCandidate(Top, std::move(Bundle));
1415     return std::move(Top).items();
1416   }
1417 
1418   llvm::Optional<float> fuzzyScore(const CompletionCandidate &C) {
1419     // Macros can be very spammy, so we only support prefix completion.
1420     // We won't end up with underfull index results, as macros are sema-only.
1421     if (C.SemaResult && C.SemaResult->Kind == CodeCompletionResult::RK_Macro &&
1422         !C.Name.startswith_lower(Filter->pattern()))
1423       return None;
1424     return Filter->match(C.Name);
1425   }
1426 
1427   // Scores a candidate and adds it to the TopN structure.
1428   void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
1429                     CompletionCandidate::Bundle Bundle) {
1430     SymbolQualitySignals Quality;
1431     SymbolRelevanceSignals Relevance;
1432     Relevance.Context = Recorder->CCContext.getKind();
1433     Relevance.Query = SymbolRelevanceSignals::CodeComplete;
1434     Relevance.FileProximityMatch = FileProximity.getPointer();
1435     if (ScopeProximity)
1436       Relevance.ScopeProximityMatch = ScopeProximity.getPointer();
1437     if (PreferredType)
1438       Relevance.HadContextType = true;
1439 
1440     auto &First = Bundle.front();
1441     if (auto FuzzyScore = fuzzyScore(First))
1442       Relevance.NameMatch = *FuzzyScore;
1443     else
1444       return;
1445     SymbolOrigin Origin = SymbolOrigin::Unknown;
1446     bool FromIndex = false;
1447     for (const auto &Candidate : Bundle) {
1448       if (Candidate.IndexResult) {
1449         Quality.merge(*Candidate.IndexResult);
1450         Relevance.merge(*Candidate.IndexResult);
1451         Origin |= Candidate.IndexResult->Origin;
1452         FromIndex = true;
1453         if (!Candidate.IndexResult->Type.empty())
1454           Relevance.HadSymbolType |= true;
1455         if (PreferredType &&
1456             PreferredType->raw() == Candidate.IndexResult->Type) {
1457           Relevance.TypeMatchesPreferred = true;
1458         }
1459       }
1460       if (Candidate.SemaResult) {
1461         Quality.merge(*Candidate.SemaResult);
1462         Relevance.merge(*Candidate.SemaResult);
1463         if (PreferredType) {
1464           if (auto CompletionType = OpaqueType::fromCompletionResult(
1465                   Recorder->CCSema->getASTContext(), *Candidate.SemaResult)) {
1466             Relevance.HadSymbolType |= true;
1467             if (PreferredType == CompletionType)
1468               Relevance.TypeMatchesPreferred = true;
1469           }
1470         }
1471         Origin |= SymbolOrigin::AST;
1472       }
1473     }
1474 
1475     CodeCompletion::Scores Scores;
1476     Scores.Quality = Quality.evaluate();
1477     Scores.Relevance = Relevance.evaluate();
1478     Scores.Total = evaluateSymbolAndRelevance(Scores.Quality, Scores.Relevance);
1479     // NameMatch is in fact a multiplier on total score, so rescoring is sound.
1480     Scores.ExcludingName = Relevance.NameMatch
1481                                ? Scores.Total / Relevance.NameMatch
1482                                : Scores.Quality;
1483 
1484     dlog("CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First.Name,
1485          llvm::to_string(Origin), Scores.Total, llvm::to_string(Quality),
1486          llvm::to_string(Relevance));
1487 
1488     NSema += bool(Origin & SymbolOrigin::AST);
1489     NIndex += FromIndex;
1490     NBoth += bool(Origin & SymbolOrigin::AST) && FromIndex;
1491     if (Candidates.push({std::move(Bundle), Scores}))
1492       Incomplete = true;
1493   }
1494 
1495   CodeCompletion toCodeCompletion(const CompletionCandidate::Bundle &Bundle) {
1496     llvm::Optional<CodeCompletionBuilder> Builder;
1497     for (const auto &Item : Bundle) {
1498       CodeCompletionString *SemaCCS =
1499           Item.SemaResult ? Recorder->codeCompletionString(*Item.SemaResult)
1500                           : nullptr;
1501       if (!Builder)
1502         Builder.emplace(Recorder->CCSema->getASTContext(), Item, SemaCCS,
1503                         QueryScopes, *Inserter, FileName,
1504                         Recorder->CCContext.getKind(), Opts);
1505       else
1506         Builder->add(Item, SemaCCS);
1507     }
1508     return Builder->build();
1509   }
1510 };
1511 
1512 } // namespace
1513 
1514 clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const {
1515   clang::CodeCompleteOptions Result;
1516   Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns;
1517   Result.IncludeMacros = IncludeMacros;
1518   Result.IncludeGlobals = true;
1519   // We choose to include full comments and not do doxygen parsing in
1520   // completion.
1521   // FIXME: ideally, we should support doxygen in some form, e.g. do markdown
1522   // formatting of the comments.
1523   Result.IncludeBriefComments = false;
1524 
1525   // When an is used, Sema is responsible for completing the main file,
1526   // the index can provide results from the preamble.
1527   // Tell Sema not to deserialize the preamble to look for results.
1528   Result.LoadExternal = !Index;
1529   Result.IncludeFixIts = IncludeFixIts;
1530 
1531   return Result;
1532 }
1533 
1534 llvm::Expected<llvm::StringRef>
1535 speculateCompletionFilter(llvm::StringRef Content, Position Pos) {
1536   auto Offset = positionToOffset(Content, Pos);
1537   if (!Offset)
1538     return llvm::make_error<llvm::StringError>(
1539         "Failed to convert position to offset in content.",
1540         llvm::inconvertibleErrorCode());
1541   if (*Offset == 0)
1542     return "";
1543 
1544   // Start from the character before the cursor.
1545   int St = *Offset - 1;
1546   // FIXME(ioeric): consider UTF characters?
1547   auto IsValidIdentifierChar = [](char C) {
1548     return ((C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') ||
1549             (C >= '0' && C <= '9') || (C == '_'));
1550   };
1551   size_t Len = 0;
1552   for (; (St >= 0) && IsValidIdentifierChar(Content[St]); --St, ++Len) {
1553   }
1554   if (Len > 0)
1555     St++; // Shift to the first valid character.
1556   return Content.substr(St, Len);
1557 }
1558 
1559 CodeCompleteResult
1560 codeComplete(PathRef FileName, const tooling::CompileCommand &Command,
1561              const PreambleData *Preamble, llvm::StringRef Contents,
1562              Position Pos, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
1563              std::shared_ptr<PCHContainerOperations> PCHs,
1564              CodeCompleteOptions Opts, SpeculativeFuzzyFind *SpecFuzzyFind) {
1565   return CodeCompleteFlow(FileName,
1566                           Preamble ? Preamble->Includes : IncludeStructure(),
1567                           SpecFuzzyFind, Opts)
1568       .run({FileName, Command, Preamble, Contents, Pos, VFS, PCHs});
1569 }
1570 
1571 SignatureHelp signatureHelp(PathRef FileName,
1572                             const tooling::CompileCommand &Command,
1573                             const PreambleData *Preamble,
1574                             llvm::StringRef Contents, Position Pos,
1575                             llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
1576                             std::shared_ptr<PCHContainerOperations> PCHs,
1577                             const SymbolIndex *Index) {
1578   SignatureHelp Result;
1579   clang::CodeCompleteOptions Options;
1580   Options.IncludeGlobals = false;
1581   Options.IncludeMacros = false;
1582   Options.IncludeCodePatterns = false;
1583   Options.IncludeBriefComments = false;
1584   IncludeStructure PreambleInclusions; // Unused for signatureHelp
1585   semaCodeComplete(
1586       llvm::make_unique<SignatureHelpCollector>(Options, Index, Result),
1587       Options,
1588       {FileName, Command, Preamble, Contents, Pos, std::move(VFS),
1589        std::move(PCHs)});
1590   return Result;
1591 }
1592 
1593 bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) {
1594   auto InTopLevelScope = [](const NamedDecl &ND) {
1595     switch (ND.getDeclContext()->getDeclKind()) {
1596     case Decl::TranslationUnit:
1597     case Decl::Namespace:
1598     case Decl::LinkageSpec:
1599       return true;
1600     default:
1601       break;
1602     };
1603     return false;
1604   };
1605   if (InTopLevelScope(ND))
1606     return true;
1607 
1608   if (const auto *EnumDecl = dyn_cast<clang::EnumDecl>(ND.getDeclContext()))
1609     return InTopLevelScope(*EnumDecl) && !EnumDecl->isScoped();
1610 
1611   return false;
1612 }
1613 
1614 CompletionItem CodeCompletion::render(const CodeCompleteOptions &Opts) const {
1615   CompletionItem LSP;
1616   const auto *InsertInclude = Includes.empty() ? nullptr : &Includes[0];
1617   LSP.label = ((InsertInclude && InsertInclude->Insertion)
1618                    ? Opts.IncludeIndicator.Insert
1619                    : Opts.IncludeIndicator.NoInsert) +
1620               (Opts.ShowOrigins ? "[" + llvm::to_string(Origin) + "]" : "") +
1621               RequiredQualifier + Name + Signature;
1622 
1623   LSP.kind = Kind;
1624   LSP.detail = BundleSize > 1 ? llvm::formatv("[{0} overloads]", BundleSize)
1625                               : ReturnType;
1626   LSP.deprecated = Deprecated;
1627   if (InsertInclude)
1628     LSP.detail += "\n" + InsertInclude->Header;
1629   LSP.documentation = Documentation;
1630   LSP.sortText = sortText(Score.Total, Name);
1631   LSP.filterText = Name;
1632   LSP.textEdit = {CompletionTokenRange, RequiredQualifier + Name};
1633   // Merge continuous additionalTextEdits into main edit. The main motivation
1634   // behind this is to help LSP clients, it seems most of them are confused when
1635   // they are provided with additionalTextEdits that are consecutive to main
1636   // edit.
1637   // Note that we store additional text edits from back to front in a line. That
1638   // is mainly to help LSP clients again, so that changes do not effect each
1639   // other.
1640   for (const auto &FixIt : FixIts) {
1641     if (isRangeConsecutive(FixIt.range, LSP.textEdit->range)) {
1642       LSP.textEdit->newText = FixIt.newText + LSP.textEdit->newText;
1643       LSP.textEdit->range.start = FixIt.range.start;
1644     } else {
1645       LSP.additionalTextEdits.push_back(FixIt);
1646     }
1647   }
1648   if (Opts.EnableSnippets)
1649     LSP.textEdit->newText += SnippetSuffix;
1650 
1651   // FIXME(kadircet): Do not even fill insertText after making sure textEdit is
1652   // compatible with most of the editors.
1653   LSP.insertText = LSP.textEdit->newText;
1654   LSP.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet
1655                                              : InsertTextFormat::PlainText;
1656   if (InsertInclude && InsertInclude->Insertion)
1657     LSP.additionalTextEdits.push_back(*InsertInclude->Insertion);
1658 
1659   return LSP;
1660 }
1661 
1662 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const CodeCompletion &C) {
1663   // For now just lean on CompletionItem.
1664   return OS << C.render(CodeCompleteOptions());
1665 }
1666 
1667 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
1668                               const CodeCompleteResult &R) {
1669   OS << "CodeCompleteResult: " << R.Completions.size() << (R.HasMore ? "+" : "")
1670      << " (" << getCompletionKindString(R.Context) << ")"
1671      << " items:\n";
1672   for (const auto &C : R.Completions)
1673     OS << C << "\n";
1674   return OS;
1675 }
1676 
1677 } // namespace clangd
1678 } // namespace clang
1679