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