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