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 "CodeCompletionStrings.h"
24 #include "Compiler.h"
25 #include "FuzzyMatch.h"
26 #include "Headers.h"
27 #include "Logger.h"
28 #include "Quality.h"
29 #include "SourceCode.h"
30 #include "Trace.h"
31 #include "URI.h"
32 #include "index/Index.h"
33 #include "clang/ASTMatchers/ASTMatchFinder.h"
34 #include "clang/Basic/LangOptions.h"
35 #include "clang/Format/Format.h"
36 #include "clang/Frontend/CompilerInstance.h"
37 #include "clang/Frontend/FrontendActions.h"
38 #include "clang/Index/USRGeneration.h"
39 #include "clang/Sema/CodeCompleteConsumer.h"
40 #include "clang/Sema/Sema.h"
41 #include "clang/Tooling/Core/Replacement.h"
42 #include "llvm/Support/Format.h"
43 #include <queue>
44 
45 // We log detailed candidate here if you run with -debug-only=codecomplete.
46 #define DEBUG_TYPE "codecomplete"
47 
48 namespace clang {
49 namespace clangd {
50 namespace {
51 
52 CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) {
53   using SK = index::SymbolKind;
54   switch (Kind) {
55   case SK::Unknown:
56     return CompletionItemKind::Missing;
57   case SK::Module:
58   case SK::Namespace:
59   case SK::NamespaceAlias:
60     return CompletionItemKind::Module;
61   case SK::Macro:
62     return CompletionItemKind::Text;
63   case SK::Enum:
64     return CompletionItemKind::Enum;
65   // FIXME(ioeric): use LSP struct instead of class when it is suppoted in the
66   // protocol.
67   case SK::Struct:
68   case SK::Class:
69   case SK::Protocol:
70   case SK::Extension:
71   case SK::Union:
72     return CompletionItemKind::Class;
73   // FIXME(ioeric): figure out whether reference is the right type for aliases.
74   case SK::TypeAlias:
75   case SK::Using:
76     return CompletionItemKind::Reference;
77   case SK::Function:
78   // FIXME(ioeric): this should probably be an operator. This should be fixed
79   // when `Operator` is support type in the protocol.
80   case SK::ConversionFunction:
81     return CompletionItemKind::Function;
82   case SK::Variable:
83   case SK::Parameter:
84     return CompletionItemKind::Variable;
85   case SK::Field:
86     return CompletionItemKind::Field;
87   // FIXME(ioeric): use LSP enum constant when it is supported in the protocol.
88   case SK::EnumConstant:
89     return CompletionItemKind::Value;
90   case SK::InstanceMethod:
91   case SK::ClassMethod:
92   case SK::StaticMethod:
93   case SK::Destructor:
94     return CompletionItemKind::Method;
95   case SK::InstanceProperty:
96   case SK::ClassProperty:
97   case SK::StaticProperty:
98     return CompletionItemKind::Property;
99   case SK::Constructor:
100     return CompletionItemKind::Constructor;
101   }
102   llvm_unreachable("Unhandled clang::index::SymbolKind.");
103 }
104 
105 CompletionItemKind
106 toCompletionItemKind(CodeCompletionResult::ResultKind ResKind,
107                      const NamedDecl *Decl) {
108   if (Decl)
109     return toCompletionItemKind(index::getSymbolInfo(Decl).Kind);
110   switch (ResKind) {
111   case CodeCompletionResult::RK_Declaration:
112     llvm_unreachable("RK_Declaration without Decl");
113   case CodeCompletionResult::RK_Keyword:
114     return CompletionItemKind::Keyword;
115   case CodeCompletionResult::RK_Macro:
116     return CompletionItemKind::Text; // unfortunately, there's no 'Macro'
117                                      // completion items in LSP.
118   case CodeCompletionResult::RK_Pattern:
119     return CompletionItemKind::Snippet;
120   }
121   llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
122 }
123 
124 /// Get the optional chunk as a string. This function is possibly recursive.
125 ///
126 /// The parameter info for each parameter is appended to the Parameters.
127 std::string
128 getOptionalParameters(const CodeCompletionString &CCS,
129                       std::vector<ParameterInformation> &Parameters) {
130   std::string Result;
131   for (const auto &Chunk : CCS) {
132     switch (Chunk.Kind) {
133     case CodeCompletionString::CK_Optional:
134       assert(Chunk.Optional &&
135              "Expected the optional code completion string to be non-null.");
136       Result += getOptionalParameters(*Chunk.Optional, Parameters);
137       break;
138     case CodeCompletionString::CK_VerticalSpace:
139       break;
140     case CodeCompletionString::CK_Placeholder:
141       // A string that acts as a placeholder for, e.g., a function call
142       // argument.
143       // Intentional fallthrough here.
144     case CodeCompletionString::CK_CurrentParameter: {
145       // A piece of text that describes the parameter that corresponds to
146       // the code-completion location within a function call, message send,
147       // macro invocation, etc.
148       Result += Chunk.Text;
149       ParameterInformation Info;
150       Info.label = Chunk.Text;
151       Parameters.push_back(std::move(Info));
152       break;
153     }
154     default:
155       Result += Chunk.Text;
156       break;
157     }
158   }
159   return Result;
160 }
161 
162 /// Creates a `HeaderFile` from \p Header which can be either a URI or a literal
163 /// include.
164 static llvm::Expected<HeaderFile> toHeaderFile(StringRef Header,
165                                                llvm::StringRef HintPath) {
166   if (isLiteralInclude(Header))
167     return HeaderFile{Header.str(), /*Verbatim=*/true};
168   auto U = URI::parse(Header);
169   if (!U)
170     return U.takeError();
171 
172   auto IncludePath = URI::includeSpelling(*U);
173   if (!IncludePath)
174     return IncludePath.takeError();
175   if (!IncludePath->empty())
176     return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true};
177 
178   auto Resolved = URI::resolve(*U, HintPath);
179   if (!Resolved)
180     return Resolved.takeError();
181   return HeaderFile{std::move(*Resolved), /*Verbatim=*/false};
182 }
183 
184 /// A code completion result, in clang-native form.
185 /// It may be promoted to a CompletionItem if it's among the top-ranked results.
186 struct CompletionCandidate {
187   llvm::StringRef Name; // Used for filtering and sorting.
188   // We may have a result from Sema, from the index, or both.
189   const CodeCompletionResult *SemaResult = nullptr;
190   const Symbol *IndexResult = nullptr;
191 
192   // Returns a token identifying the overload set this is part of.
193   // 0 indicates it's not part of any overload set.
194   size_t overloadSet() const {
195     SmallString<256> Scratch;
196     if (IndexResult) {
197       switch (IndexResult->SymInfo.Kind) {
198       case index::SymbolKind::ClassMethod:
199       case index::SymbolKind::InstanceMethod:
200       case index::SymbolKind::StaticMethod:
201         assert(false && "Don't expect members from index in code completion");
202         // fall through
203       case index::SymbolKind::Function:
204         // We can't group overloads together that need different #includes.
205         // This could break #include insertion.
206         return hash_combine(
207             (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch),
208             headerToInsertIfNotPresent().getValueOr(""));
209       default:
210         return 0;
211       }
212     }
213     assert(SemaResult);
214     // We need to make sure we're consistent with the IndexResult case!
215     const NamedDecl *D = SemaResult->Declaration;
216     if (!D || !D->isFunctionOrFunctionTemplate())
217       return 0;
218     {
219       llvm::raw_svector_ostream OS(Scratch);
220       D->printQualifiedName(OS);
221     }
222     return hash_combine(Scratch, headerToInsertIfNotPresent().getValueOr(""));
223   }
224 
225   llvm::Optional<llvm::StringRef> headerToInsertIfNotPresent() const {
226     if (!IndexResult || !IndexResult->Detail ||
227         IndexResult->Detail->IncludeHeader.empty())
228       return llvm::None;
229     if (SemaResult && SemaResult->Declaration) {
230       // Avoid inserting new #include if the declaration is found in the current
231       // file e.g. the symbol is forward declared.
232       auto &SM = SemaResult->Declaration->getASTContext().getSourceManager();
233       for (const Decl *RD : SemaResult->Declaration->redecls())
234         if (SM.isInMainFile(SM.getExpansionLoc(RD->getLocStart())))
235           return llvm::None;
236     }
237     return IndexResult->Detail->IncludeHeader;
238   }
239 
240   // Builds an LSP completion item.
241   CompletionItem build(StringRef FileName, const CompletionItemScores &Scores,
242                        const CodeCompleteOptions &Opts,
243                        CodeCompletionString *SemaCCS,
244                        const IncludeInserter &Includes,
245                        llvm::StringRef SemaDocComment) const {
246     assert(bool(SemaResult) == bool(SemaCCS));
247     assert(SemaResult || IndexResult);
248 
249     CompletionItem I;
250     bool InsertingInclude = false; // Whether a new #include will be added.
251     if (SemaResult) {
252       llvm::StringRef Name(SemaCCS->getTypedText());
253       std::string Signature, SnippetSuffix, Qualifiers;
254       getSignature(*SemaCCS, &Signature, &SnippetSuffix, &Qualifiers);
255       I.label = (Qualifiers + Name + Signature).str();
256       I.filterText = Name;
257       I.insertText = (Qualifiers + Name).str();
258       if (Opts.EnableSnippets)
259         I.insertText += SnippetSuffix;
260       I.documentation = formatDocumentation(*SemaCCS, SemaDocComment);
261       I.detail = getReturnType(*SemaCCS);
262       if (SemaResult->Kind == CodeCompletionResult::RK_Declaration)
263         if (const auto *D = SemaResult->getDeclaration())
264           if (const auto *ND = llvm::dyn_cast<NamedDecl>(D))
265             I.SymbolScope = splitQualifiedName(printQualifiedName(*ND)).first;
266       I.kind = toCompletionItemKind(SemaResult->Kind, SemaResult->Declaration);
267     }
268     if (IndexResult) {
269       if (I.SymbolScope.empty())
270         I.SymbolScope = IndexResult->Scope;
271       if (I.kind == CompletionItemKind::Missing)
272         I.kind = toCompletionItemKind(IndexResult->SymInfo.Kind);
273       // FIXME: reintroduce a way to show the index source for debugging.
274       if (I.label.empty())
275         I.label = (IndexResult->Name + IndexResult->Signature).str();
276       if (I.filterText.empty())
277         I.filterText = IndexResult->Name;
278 
279       // FIXME(ioeric): support inserting/replacing scope qualifiers.
280       if (I.insertText.empty()) {
281         I.insertText = IndexResult->Name;
282         if (Opts.EnableSnippets)
283           I.insertText += IndexResult->CompletionSnippetSuffix;
284       }
285 
286       if (auto *D = IndexResult->Detail) {
287         if (I.documentation.empty())
288           I.documentation = D->Documentation;
289         if (I.detail.empty())
290           I.detail = D->ReturnType;
291         if (auto Inserted = headerToInsertIfNotPresent()) {
292           auto IncludePath = [&]() -> Expected<std::string> {
293             auto ResolvedDeclaring = toHeaderFile(
294                 IndexResult->CanonicalDeclaration.FileURI, FileName);
295             if (!ResolvedDeclaring)
296               return ResolvedDeclaring.takeError();
297             auto ResolvedInserted = toHeaderFile(*Inserted, FileName);
298             if (!ResolvedInserted)
299               return ResolvedInserted.takeError();
300             if (!Includes.shouldInsertInclude(*ResolvedDeclaring,
301                                               *ResolvedInserted))
302               return "";
303             return Includes.calculateIncludePath(*ResolvedDeclaring,
304                                                  *ResolvedInserted);
305           }();
306           if (!IncludePath) {
307             std::string ErrMsg =
308                 ("Failed to generate include insertion edits for adding header "
309                  "(FileURI=\"" +
310                  IndexResult->CanonicalDeclaration.FileURI +
311                  "\", IncludeHeader=\"" + D->IncludeHeader + "\") into " +
312                  FileName)
313                     .str();
314             log(ErrMsg + ":" + llvm::toString(IncludePath.takeError()));
315           } else if (!IncludePath->empty()) {
316             // FIXME: consider what to show when there is no #include insertion,
317             // and for sema results, for consistency.
318             if (auto Edit = Includes.insert(*IncludePath)) {
319               I.detail += ("\n" + StringRef(*IncludePath).trim('"')).str();
320               I.additionalTextEdits = {std::move(*Edit)};
321               InsertingInclude = true;
322             }
323           }
324         }
325       }
326     }
327     I.label = (InsertingInclude ? Opts.IncludeIndicator.Insert
328                                 : Opts.IncludeIndicator.NoInsert) +
329               I.label;
330     I.scoreInfo = Scores;
331     I.sortText = sortText(Scores.finalScore, Name);
332     I.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet
333                                              : InsertTextFormat::PlainText;
334     return I;
335   }
336 
337   using Bundle = llvm::SmallVector<CompletionCandidate, 4>;
338 
339   static CompletionItem build(const Bundle &Bundle, CompletionItem First,
340                               const clangd::CodeCompleteOptions &Opts) {
341     if (Bundle.size() == 1)
342       return First;
343     // Patch up the completion item to make it look like a bundle.
344     // This is a bit of a hack but most things are the same.
345 
346     // Need to erase the signature. All bundles are function calls.
347     llvm::StringRef Name = Bundle.front().Name;
348     First.insertText =
349         Opts.EnableSnippets ? (Name + "(${0})").str() : Name.str();
350     // Keep the visual indicator of the original label.
351     bool InsertingInclude =
352         StringRef(First.label).startswith(Opts.IncludeIndicator.Insert);
353     First.label = (Twine(InsertingInclude ? Opts.IncludeIndicator.Insert
354                                           : Opts.IncludeIndicator.NoInsert) +
355                    Name + "(…)")
356                       .str();
357     First.detail = llvm::formatv("[{0} overloads]", Bundle.size());
358     return First;
359   }
360 };
361 using ScoredBundle =
362     std::pair<CompletionCandidate::Bundle, CompletionItemScores>;
363 struct ScoredBundleGreater {
364   bool operator()(const ScoredBundle &L, const ScoredBundle &R) {
365     if (L.second.finalScore != R.second.finalScore)
366       return L.second.finalScore > R.second.finalScore;
367     return L.first.front().Name <
368            R.first.front().Name; // Earlier name is better.
369   }
370 };
371 
372 // Determine the symbol ID for a Sema code completion result, if possible.
373 llvm::Optional<SymbolID> getSymbolID(const CodeCompletionResult &R) {
374   switch (R.Kind) {
375   case CodeCompletionResult::RK_Declaration:
376   case CodeCompletionResult::RK_Pattern: {
377     llvm::SmallString<128> USR;
378     if (/*Ignore=*/clang::index::generateUSRForDecl(R.Declaration, USR))
379       return None;
380     return SymbolID(USR);
381   }
382   case CodeCompletionResult::RK_Macro:
383     // FIXME: Macros do have USRs, but the CCR doesn't contain enough info.
384   case CodeCompletionResult::RK_Keyword:
385     return None;
386   }
387   llvm_unreachable("unknown CodeCompletionResult kind");
388 }
389 
390 // Scopes of the paritial identifier we're trying to complete.
391 // It is used when we query the index for more completion results.
392 struct SpecifiedScope {
393   // The scopes we should look in, determined by Sema.
394   //
395   // If the qualifier was fully resolved, we look for completions in these
396   // scopes; if there is an unresolved part of the qualifier, it should be
397   // resolved within these scopes.
398   //
399   // Examples of qualified completion:
400   //
401   //   "::vec"                                      => {""}
402   //   "using namespace std; ::vec^"                => {"", "std::"}
403   //   "namespace ns {using namespace std;} ns::^"  => {"ns::", "std::"}
404   //   "std::vec^"                                  => {""}  // "std" unresolved
405   //
406   // Examples of unqualified completion:
407   //
408   //   "vec^"                                       => {""}
409   //   "using namespace std; vec^"                  => {"", "std::"}
410   //   "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""}
411   //
412   // "" for global namespace, "ns::" for normal namespace.
413   std::vector<std::string> AccessibleScopes;
414   // The full scope qualifier as typed by the user (without the leading "::").
415   // Set if the qualifier is not fully resolved by Sema.
416   llvm::Optional<std::string> UnresolvedQualifier;
417 
418   // Construct scopes being queried in indexes.
419   // This method format the scopes to match the index request representation.
420   std::vector<std::string> scopesForIndexQuery() {
421     std::vector<std::string> Results;
422     for (llvm::StringRef AS : AccessibleScopes) {
423       Results.push_back(AS);
424       if (UnresolvedQualifier)
425         Results.back() += *UnresolvedQualifier;
426     }
427     return Results;
428   }
429 };
430 
431 // Get all scopes that will be queried in indexes.
432 std::vector<std::string> getQueryScopes(CodeCompletionContext &CCContext,
433                                         const SourceManager &SM) {
434   auto GetAllAccessibleScopes = [](CodeCompletionContext &CCContext) {
435     SpecifiedScope Info;
436     for (auto *Context : CCContext.getVisitedContexts()) {
437       if (isa<TranslationUnitDecl>(Context))
438         Info.AccessibleScopes.push_back(""); // global namespace
439       else if (const auto *NS = dyn_cast<NamespaceDecl>(Context))
440         Info.AccessibleScopes.push_back(NS->getQualifiedNameAsString() + "::");
441     }
442     return Info;
443   };
444 
445   auto SS = CCContext.getCXXScopeSpecifier();
446 
447   // Unqualified completion (e.g. "vec^").
448   if (!SS) {
449     // FIXME: Once we can insert namespace qualifiers and use the in-scope
450     //        namespaces for scoring, search in all namespaces.
451     // FIXME: Capture scopes and use for scoring, for example,
452     //        "using namespace std; namespace foo {v^}" =>
453     //        foo::value > std::vector > boost::variant
454     return GetAllAccessibleScopes(CCContext).scopesForIndexQuery();
455   }
456 
457   // Qualified completion ("std::vec^"), we have two cases depending on whether
458   // the qualifier can be resolved by Sema.
459   if ((*SS)->isValid()) { // Resolved qualifier.
460     return GetAllAccessibleScopes(CCContext).scopesForIndexQuery();
461   }
462 
463   // Unresolved qualifier.
464   // FIXME: When Sema can resolve part of a scope chain (e.g.
465   // "known::unknown::id"), we should expand the known part ("known::") rather
466   // than treating the whole thing as unknown.
467   SpecifiedScope Info;
468   Info.AccessibleScopes.push_back(""); // global namespace
469 
470   Info.UnresolvedQualifier =
471       Lexer::getSourceText(CharSourceRange::getCharRange((*SS)->getRange()), SM,
472                            clang::LangOptions())
473           .ltrim("::");
474   // Sema excludes the trailing "::".
475   if (!Info.UnresolvedQualifier->empty())
476     *Info.UnresolvedQualifier += "::";
477 
478   return Info.scopesForIndexQuery();
479 }
480 
481 // Should we perform index-based completion in a context of the specified kind?
482 // FIXME: consider allowing completion, but restricting the result types.
483 bool contextAllowsIndex(enum CodeCompletionContext::Kind K) {
484   switch (K) {
485   case CodeCompletionContext::CCC_TopLevel:
486   case CodeCompletionContext::CCC_ObjCInterface:
487   case CodeCompletionContext::CCC_ObjCImplementation:
488   case CodeCompletionContext::CCC_ObjCIvarList:
489   case CodeCompletionContext::CCC_ClassStructUnion:
490   case CodeCompletionContext::CCC_Statement:
491   case CodeCompletionContext::CCC_Expression:
492   case CodeCompletionContext::CCC_ObjCMessageReceiver:
493   case CodeCompletionContext::CCC_EnumTag:
494   case CodeCompletionContext::CCC_UnionTag:
495   case CodeCompletionContext::CCC_ClassOrStructTag:
496   case CodeCompletionContext::CCC_ObjCProtocolName:
497   case CodeCompletionContext::CCC_Namespace:
498   case CodeCompletionContext::CCC_Type:
499   case CodeCompletionContext::CCC_Name: // FIXME: why does ns::^ give this?
500   case CodeCompletionContext::CCC_PotentiallyQualifiedName:
501   case CodeCompletionContext::CCC_ParenthesizedExpression:
502   case CodeCompletionContext::CCC_ObjCInterfaceName:
503   case CodeCompletionContext::CCC_ObjCCategoryName:
504     return true;
505   case CodeCompletionContext::CCC_Other: // Be conservative.
506   case CodeCompletionContext::CCC_OtherWithMacros:
507   case CodeCompletionContext::CCC_DotMemberAccess:
508   case CodeCompletionContext::CCC_ArrowMemberAccess:
509   case CodeCompletionContext::CCC_ObjCPropertyAccess:
510   case CodeCompletionContext::CCC_MacroName:
511   case CodeCompletionContext::CCC_MacroNameUse:
512   case CodeCompletionContext::CCC_PreprocessorExpression:
513   case CodeCompletionContext::CCC_PreprocessorDirective:
514   case CodeCompletionContext::CCC_NaturalLanguage:
515   case CodeCompletionContext::CCC_SelectorName:
516   case CodeCompletionContext::CCC_TypeQualifiers:
517   case CodeCompletionContext::CCC_ObjCInstanceMessage:
518   case CodeCompletionContext::CCC_ObjCClassMessage:
519   case CodeCompletionContext::CCC_Recovery:
520     return false;
521   }
522   llvm_unreachable("unknown code completion context");
523 }
524 
525 // Some member calls are blacklisted because they're so rarely useful.
526 static bool isBlacklistedMember(const NamedDecl &D) {
527   // Destructor completion is rarely useful, and works inconsistently.
528   // (s.^ completes ~string, but s.~st^ is an error).
529   if (D.getKind() == Decl::CXXDestructor)
530     return true;
531   // Injected name may be useful for A::foo(), but who writes A::A::foo()?
532   if (auto *R = dyn_cast_or_null<RecordDecl>(&D))
533     if (R->isInjectedClassName())
534       return true;
535   // Explicit calls to operators are also rare.
536   auto NameKind = D.getDeclName().getNameKind();
537   if (NameKind == DeclarationName::CXXOperatorName ||
538       NameKind == DeclarationName::CXXLiteralOperatorName ||
539       NameKind == DeclarationName::CXXConversionFunctionName)
540     return true;
541   return false;
542 }
543 
544 // The CompletionRecorder captures Sema code-complete output, including context.
545 // It filters out ignored results (but doesn't apply fuzzy-filtering yet).
546 // It doesn't do scoring or conversion to CompletionItem yet, as we want to
547 // merge with index results first.
548 // Generally the fields and methods of this object should only be used from
549 // within the callback.
550 struct CompletionRecorder : public CodeCompleteConsumer {
551   CompletionRecorder(const CodeCompleteOptions &Opts,
552                      UniqueFunction<void()> ResultsCallback)
553       : CodeCompleteConsumer(Opts.getClangCompleteOpts(),
554                              /*OutputIsBinary=*/false),
555         CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
556         CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
557         CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) {
558     assert(this->ResultsCallback);
559   }
560 
561   std::vector<CodeCompletionResult> Results;
562   CodeCompletionContext CCContext;
563   Sema *CCSema = nullptr; // Sema that created the results.
564   // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead?
565 
566   void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context,
567                                   CodeCompletionResult *InResults,
568                                   unsigned NumResults) override final {
569     // If a callback is called without any sema result and the context does not
570     // support index-based completion, we simply skip it to give way to
571     // potential future callbacks with results.
572     if (NumResults == 0 && !contextAllowsIndex(Context.getKind()))
573       return;
574     if (CCSema) {
575       log(llvm::formatv(
576           "Multiple code complete callbacks (parser backtracked?). "
577           "Dropping results from context {0}, keeping results from {1}.",
578           getCompletionKindString(Context.getKind()),
579           getCompletionKindString(this->CCContext.getKind())));
580       return;
581     }
582     // Record the completion context.
583     CCSema = &S;
584     CCContext = Context;
585 
586     // Retain the results we might want.
587     for (unsigned I = 0; I < NumResults; ++I) {
588       auto &Result = InResults[I];
589       // Drop hidden items which cannot be found by lookup after completion.
590       // Exception: some items can be named by using a qualifier.
591       if (Result.Hidden && (!Result.Qualifier || Result.QualifierIsInformative))
592         continue;
593       if (!Opts.IncludeIneligibleResults &&
594           (Result.Availability == CXAvailability_NotAvailable ||
595            Result.Availability == CXAvailability_NotAccessible))
596         continue;
597       if (Result.Declaration &&
598           !Context.getBaseType().isNull() // is this a member-access context?
599           && isBlacklistedMember(*Result.Declaration))
600         continue;
601       // We choose to never append '::' to completion results in clangd.
602       Result.StartsNestedNameSpecifier = false;
603       Results.push_back(Result);
604     }
605     ResultsCallback();
606   }
607 
608   CodeCompletionAllocator &getAllocator() override { return *CCAllocator; }
609   CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
610 
611   // Returns the filtering/sorting name for Result, which must be from Results.
612   // Returned string is owned by this recorder (or the AST).
613   llvm::StringRef getName(const CodeCompletionResult &Result) {
614     switch (Result.Kind) {
615     case CodeCompletionResult::RK_Declaration:
616       if (auto *ID = Result.Declaration->getIdentifier())
617         return ID->getName();
618       break;
619     case CodeCompletionResult::RK_Keyword:
620       return Result.Keyword;
621     case CodeCompletionResult::RK_Macro:
622       return Result.Macro->getName();
623     case CodeCompletionResult::RK_Pattern:
624       return Result.Pattern->getTypedText();
625     }
626     auto *CCS = codeCompletionString(Result);
627     return CCS->getTypedText();
628   }
629 
630   // Build a CodeCompletion string for R, which must be from Results.
631   // The CCS will be owned by this recorder.
632   CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) {
633     // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway.
634     return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString(
635         *CCSema, CCContext, *CCAllocator, CCTUInfo,
636         /*IncludeBriefComments=*/false);
637   }
638 
639 private:
640   CodeCompleteOptions Opts;
641   std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
642   CodeCompletionTUInfo CCTUInfo;
643   UniqueFunction<void()> ResultsCallback;
644 };
645 
646 class SignatureHelpCollector final : public CodeCompleteConsumer {
647 
648 public:
649   SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
650                          SignatureHelp &SigHelp)
651       : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
652         SigHelp(SigHelp),
653         Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
654         CCTUInfo(Allocator) {}
655 
656   void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
657                                  OverloadCandidate *Candidates,
658                                  unsigned NumCandidates) override {
659     SigHelp.signatures.reserve(NumCandidates);
660     // FIXME(rwols): How can we determine the "active overload candidate"?
661     // Right now the overloaded candidates seem to be provided in a "best fit"
662     // order, so I'm not too worried about this.
663     SigHelp.activeSignature = 0;
664     assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
665            "too many arguments");
666     SigHelp.activeParameter = static_cast<int>(CurrentArg);
667     for (unsigned I = 0; I < NumCandidates; ++I) {
668       const auto &Candidate = Candidates[I];
669       const auto *CCS = Candidate.CreateSignatureString(
670           CurrentArg, S, *Allocator, CCTUInfo, true);
671       assert(CCS && "Expected the CodeCompletionString to be non-null");
672       // FIXME: for headers, we need to get a comment from the index.
673       SigHelp.signatures.push_back(ProcessOverloadCandidate(
674           Candidate, *CCS,
675           getParameterDocComment(S.getASTContext(), Candidate, CurrentArg,
676                                  /*CommentsFromHeaders=*/false)));
677     }
678   }
679 
680   GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
681 
682   CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
683 
684 private:
685   // FIXME(ioeric): consider moving CodeCompletionString logic here to
686   // CompletionString.h.
687   SignatureInformation
688   ProcessOverloadCandidate(const OverloadCandidate &Candidate,
689                            const CodeCompletionString &CCS,
690                            llvm::StringRef DocComment) const {
691     SignatureInformation Result;
692     const char *ReturnType = nullptr;
693 
694     Result.documentation = formatDocumentation(CCS, DocComment);
695 
696     for (const auto &Chunk : CCS) {
697       switch (Chunk.Kind) {
698       case CodeCompletionString::CK_ResultType:
699         // A piece of text that describes the type of an entity or,
700         // for functions and methods, the return type.
701         assert(!ReturnType && "Unexpected CK_ResultType");
702         ReturnType = Chunk.Text;
703         break;
704       case CodeCompletionString::CK_Placeholder:
705         // A string that acts as a placeholder for, e.g., a function call
706         // argument.
707         // Intentional fallthrough here.
708       case CodeCompletionString::CK_CurrentParameter: {
709         // A piece of text that describes the parameter that corresponds to
710         // the code-completion location within a function call, message send,
711         // macro invocation, etc.
712         Result.label += Chunk.Text;
713         ParameterInformation Info;
714         Info.label = Chunk.Text;
715         Result.parameters.push_back(std::move(Info));
716         break;
717       }
718       case CodeCompletionString::CK_Optional: {
719         // The rest of the parameters are defaulted/optional.
720         assert(Chunk.Optional &&
721                "Expected the optional code completion string to be non-null.");
722         Result.label +=
723             getOptionalParameters(*Chunk.Optional, Result.parameters);
724         break;
725       }
726       case CodeCompletionString::CK_VerticalSpace:
727         break;
728       default:
729         Result.label += Chunk.Text;
730         break;
731       }
732     }
733     if (ReturnType) {
734       Result.label += " -> ";
735       Result.label += ReturnType;
736     }
737     return Result;
738   }
739 
740   SignatureHelp &SigHelp;
741   std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
742   CodeCompletionTUInfo CCTUInfo;
743 
744 }; // SignatureHelpCollector
745 
746 struct SemaCompleteInput {
747   PathRef FileName;
748   const tooling::CompileCommand &Command;
749   PrecompiledPreamble const *Preamble;
750   const std::vector<Inclusion> &PreambleInclusions;
751   StringRef Contents;
752   Position Pos;
753   IntrusiveRefCntPtr<vfs::FileSystem> VFS;
754   std::shared_ptr<PCHContainerOperations> PCHs;
755 };
756 
757 // Invokes Sema code completion on a file.
758 // If \p Includes is set, it will be initialized after a compiler instance has
759 // been set up.
760 bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
761                       const clang::CodeCompleteOptions &Options,
762                       const SemaCompleteInput &Input,
763                       std::unique_ptr<IncludeInserter> *Includes = nullptr) {
764   trace::Span Tracer("Sema completion");
765   std::vector<const char *> ArgStrs;
766   for (const auto &S : Input.Command.CommandLine)
767     ArgStrs.push_back(S.c_str());
768 
769   if (Input.VFS->setCurrentWorkingDirectory(Input.Command.Directory)) {
770     log("Couldn't set working directory");
771     // We run parsing anyway, our lit-tests rely on results for non-existing
772     // working dirs.
773   }
774 
775   IgnoreDiagnostics DummyDiagsConsumer;
776   auto CI = createInvocationFromCommandLine(
777       ArgStrs,
778       CompilerInstance::createDiagnostics(new DiagnosticOptions,
779                                           &DummyDiagsConsumer, false),
780       Input.VFS);
781   if (!CI) {
782     log("Couldn't create CompilerInvocation");
783     return false;
784   }
785   auto &FrontendOpts = CI->getFrontendOpts();
786   FrontendOpts.DisableFree = false;
787   FrontendOpts.SkipFunctionBodies = true;
788   CI->getLangOpts()->CommentOpts.ParseAllComments = true;
789   // Disable typo correction in Sema.
790   CI->getLangOpts()->SpellChecking = false;
791   // Setup code completion.
792   FrontendOpts.CodeCompleteOpts = Options;
793   FrontendOpts.CodeCompletionAt.FileName = Input.FileName;
794   auto Offset = positionToOffset(Input.Contents, Input.Pos);
795   if (!Offset) {
796     log("Code completion position was invalid " +
797         llvm::toString(Offset.takeError()));
798     return false;
799   }
800   std::tie(FrontendOpts.CodeCompletionAt.Line,
801            FrontendOpts.CodeCompletionAt.Column) =
802       offsetToClangLineColumn(Input.Contents, *Offset);
803 
804   std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
805       llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName);
806   // The diagnostic options must be set before creating a CompilerInstance.
807   CI->getDiagnosticOpts().IgnoreWarnings = true;
808   // We reuse the preamble whether it's valid or not. This is a
809   // correctness/performance tradeoff: building without a preamble is slow, and
810   // completion is latency-sensitive.
811   // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
812   // the remapped buffers do not get freed.
813   auto Clang = prepareCompilerInstance(
814       std::move(CI), Input.Preamble, std::move(ContentsBuffer),
815       std::move(Input.PCHs), std::move(Input.VFS), DummyDiagsConsumer);
816   Clang->setCodeCompletionConsumer(Consumer.release());
817 
818   SyntaxOnlyAction Action;
819   if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
820     log("BeginSourceFile() failed when running codeComplete for " +
821         Input.FileName);
822     return false;
823   }
824   if (Includes) {
825     // Initialize Includes if provided.
826 
827     // FIXME(ioeric): needs more consistent style support in clangd server.
828     auto Style = format::getStyle("file", Input.FileName, "LLVM",
829                                   Input.Contents, Input.VFS.get());
830     if (!Style) {
831       log("Failed to get FormatStyle for file" + Input.FileName +
832           ". Fall back to use LLVM style. Error: " +
833           llvm::toString(Style.takeError()));
834       Style = format::getLLVMStyle();
835     }
836     *Includes = llvm::make_unique<IncludeInserter>(
837         Input.FileName, Input.Contents, *Style, Input.Command.Directory,
838         Clang->getPreprocessor().getHeaderSearchInfo());
839     for (const auto &Inc : Input.PreambleInclusions)
840       Includes->get()->addExisting(Inc);
841     Clang->getPreprocessor().addPPCallbacks(collectInclusionsInMainFileCallback(
842         Clang->getSourceManager(), [Includes](Inclusion Inc) {
843           Includes->get()->addExisting(std::move(Inc));
844         }));
845   }
846   if (!Action.Execute()) {
847     log("Execute() failed when running codeComplete for " + Input.FileName);
848     return false;
849   }
850   Action.EndSourceFile();
851 
852   return true;
853 }
854 
855 // Should we allow index completions in the specified context?
856 bool allowIndex(CodeCompletionContext &CC) {
857   if (!contextAllowsIndex(CC.getKind()))
858     return false;
859   // We also avoid ClassName::bar (but allow namespace::bar).
860   auto Scope = CC.getCXXScopeSpecifier();
861   if (!Scope)
862     return true;
863   NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
864   if (!NameSpec)
865     return true;
866   // We only query the index when qualifier is a namespace.
867   // If it's a class, we rely solely on sema completions.
868   switch (NameSpec->getKind()) {
869   case NestedNameSpecifier::Global:
870   case NestedNameSpecifier::Namespace:
871   case NestedNameSpecifier::NamespaceAlias:
872     return true;
873   case NestedNameSpecifier::Super:
874   case NestedNameSpecifier::TypeSpec:
875   case NestedNameSpecifier::TypeSpecWithTemplate:
876   // Unresolved inside a template.
877   case NestedNameSpecifier::Identifier:
878     return false;
879   }
880   llvm_unreachable("invalid NestedNameSpecifier kind");
881 }
882 
883 } // namespace
884 
885 clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const {
886   clang::CodeCompleteOptions Result;
887   Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns;
888   Result.IncludeMacros = IncludeMacros;
889   Result.IncludeGlobals = true;
890   // We choose to include full comments and not do doxygen parsing in
891   // completion.
892   // FIXME: ideally, we should support doxygen in some form, e.g. do markdown
893   // formatting of the comments.
894   Result.IncludeBriefComments = false;
895 
896   // When an is used, Sema is responsible for completing the main file,
897   // the index can provide results from the preamble.
898   // Tell Sema not to deserialize the preamble to look for results.
899   Result.LoadExternal = !Index;
900 
901   return Result;
902 }
903 
904 // Runs Sema-based (AST) and Index-based completion, returns merged results.
905 //
906 // There are a few tricky considerations:
907 //   - the AST provides information needed for the index query (e.g. which
908 //     namespaces to search in). So Sema must start first.
909 //   - we only want to return the top results (Opts.Limit).
910 //     Building CompletionItems for everything else is wasteful, so we want to
911 //     preserve the "native" format until we're done with scoring.
912 //   - the data underlying Sema completion items is owned by the AST and various
913 //     other arenas, which must stay alive for us to build CompletionItems.
914 //   - we may get duplicate results from Sema and the Index, we need to merge.
915 //
916 // So we start Sema completion first, and do all our work in its callback.
917 // We use the Sema context information to query the index.
918 // Then we merge the two result sets, producing items that are Sema/Index/Both.
919 // These items are scored, and the top N are synthesized into the LSP response.
920 // Finally, we can clean up the data structures created by Sema completion.
921 //
922 // Main collaborators are:
923 //   - semaCodeComplete sets up the compiler machinery to run code completion.
924 //   - CompletionRecorder captures Sema completion results, including context.
925 //   - SymbolIndex (Opts.Index) provides index completion results as Symbols
926 //   - CompletionCandidates are the result of merging Sema and Index results.
927 //     Each candidate points to an underlying CodeCompletionResult (Sema), a
928 //     Symbol (Index), or both. It computes the result quality score.
929 //     CompletionCandidate also does conversion to CompletionItem (at the end).
930 //   - FuzzyMatcher scores how the candidate matches the partial identifier.
931 //     This score is combined with the result quality score for the final score.
932 //   - TopN determines the results with the best score.
933 class CodeCompleteFlow {
934   PathRef FileName;
935   const CodeCompleteOptions &Opts;
936   // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup.
937   CompletionRecorder *Recorder = nullptr;
938   int NSema = 0, NIndex = 0, NBoth = 0; // Counters for logging.
939   bool Incomplete = false; // Would more be available with a higher limit?
940   llvm::Optional<FuzzyMatcher> Filter;       // Initialized once Sema runs.
941   std::unique_ptr<IncludeInserter> Includes; // Initialized once compiler runs.
942   FileProximityMatcher FileProximityMatch;
943 
944 public:
945   // A CodeCompleteFlow object is only useful for calling run() exactly once.
946   CodeCompleteFlow(PathRef FileName, const CodeCompleteOptions &Opts)
947       : FileName(FileName), Opts(Opts),
948         // FIXME: also use path of the main header corresponding to FileName to
949         // calculate the file proximity, which would capture include/ and src/
950         // project setup where headers and implementations are not in the same
951         // directory.
952         FileProximityMatch(ArrayRef<StringRef>({FileName})) {}
953 
954   CompletionList run(const SemaCompleteInput &SemaCCInput) && {
955     trace::Span Tracer("CodeCompleteFlow");
956 
957     // We run Sema code completion first. It builds an AST and calculates:
958     //   - completion results based on the AST.
959     //   - partial identifier and context. We need these for the index query.
960     CompletionList Output;
961     auto RecorderOwner = llvm::make_unique<CompletionRecorder>(Opts, [&]() {
962       assert(Recorder && "Recorder is not set");
963       assert(Includes && "Includes is not set");
964       // If preprocessor was run, inclusions from preprocessor callback should
965       // already be added to Inclusions.
966       Output = runWithSema();
967       Includes.reset(); // Make sure this doesn't out-live Clang.
968       SPAN_ATTACH(Tracer, "sema_completion_kind",
969                   getCompletionKindString(Recorder->CCContext.getKind()));
970     });
971 
972     Recorder = RecorderOwner.get();
973     semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
974                      SemaCCInput, &Includes);
975 
976     SPAN_ATTACH(Tracer, "sema_results", NSema);
977     SPAN_ATTACH(Tracer, "index_results", NIndex);
978     SPAN_ATTACH(Tracer, "merged_results", NBoth);
979     SPAN_ATTACH(Tracer, "returned_results", Output.items.size());
980     SPAN_ATTACH(Tracer, "incomplete", Output.isIncomplete);
981     log(llvm::formatv("Code complete: {0} results from Sema, {1} from Index, "
982                       "{2} matched, {3} returned{4}.",
983                       NSema, NIndex, NBoth, Output.items.size(),
984                       Output.isIncomplete ? " (incomplete)" : ""));
985     assert(!Opts.Limit || Output.items.size() <= Opts.Limit);
986     // We don't assert that isIncomplete means we hit a limit.
987     // Indexes may choose to impose their own limits even if we don't have one.
988     return Output;
989   }
990 
991 private:
992   // This is called by run() once Sema code completion is done, but before the
993   // Sema data structures are torn down. It does all the real work.
994   CompletionList runWithSema() {
995     Filter = FuzzyMatcher(
996         Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
997     // Sema provides the needed context to query the index.
998     // FIXME: in addition to querying for extra/overlapping symbols, we should
999     //        explicitly request symbols corresponding to Sema results.
1000     //        We can use their signals even if the index can't suggest them.
1001     // We must copy index results to preserve them, but there are at most Limit.
1002     auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
1003                             ? queryIndex()
1004                             : SymbolSlab();
1005     // Merge Sema and Index results, score them, and pick the winners.
1006     auto Top = mergeResults(Recorder->Results, IndexResults);
1007     // Convert the results to the desired LSP structs.
1008     CompletionList Output;
1009     for (auto &C : Top)
1010       Output.items.push_back(toCompletionItem(C.first, C.second));
1011     Output.isIncomplete = Incomplete;
1012     return Output;
1013   }
1014 
1015   SymbolSlab queryIndex() {
1016     trace::Span Tracer("Query index");
1017     SPAN_ATTACH(Tracer, "limit", Opts.Limit);
1018 
1019     SymbolSlab::Builder ResultsBuilder;
1020     // Build the query.
1021     FuzzyFindRequest Req;
1022     if (Opts.Limit)
1023       Req.MaxCandidateCount = Opts.Limit;
1024     Req.Query = Filter->pattern();
1025     Req.RestrictForCodeCompletion = true;
1026     Req.Scopes = getQueryScopes(Recorder->CCContext,
1027                                 Recorder->CCSema->getSourceManager());
1028     Req.ProximityPaths.push_back(FileName);
1029     log(llvm::formatv("Code complete: fuzzyFind(\"{0}\", scopes=[{1}])",
1030                       Req.Query,
1031                       llvm::join(Req.Scopes.begin(), Req.Scopes.end(), ",")));
1032     // Run the query against the index.
1033     if (Opts.Index->fuzzyFind(
1034             Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); }))
1035       Incomplete = true;
1036     return std::move(ResultsBuilder).build();
1037   }
1038 
1039   // Merges Sema and Index results where possible, to form CompletionCandidates.
1040   // Groups overloads if desired, to form CompletionCandidate::Bundles.
1041   // The bundles are scored and top results are returned, best to worst.
1042   std::vector<ScoredBundle>
1043   mergeResults(const std::vector<CodeCompletionResult> &SemaResults,
1044                const SymbolSlab &IndexResults) {
1045     trace::Span Tracer("Merge and score results");
1046     std::vector<CompletionCandidate::Bundle> Bundles;
1047     llvm::DenseMap<size_t, size_t> BundleLookup;
1048     auto AddToBundles = [&](const CodeCompletionResult *SemaResult,
1049                             const Symbol *IndexResult) {
1050       CompletionCandidate C;
1051       C.SemaResult = SemaResult;
1052       C.IndexResult = IndexResult;
1053       C.Name = IndexResult ? IndexResult->Name : Recorder->getName(*SemaResult);
1054       if (auto OverloadSet = Opts.BundleOverloads ? C.overloadSet() : 0) {
1055         auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1056         if (Ret.second)
1057           Bundles.emplace_back();
1058         Bundles[Ret.first->second].push_back(std::move(C));
1059       } else {
1060         Bundles.emplace_back();
1061         Bundles.back().push_back(std::move(C));
1062       }
1063     };
1064     llvm::DenseSet<const Symbol *> UsedIndexResults;
1065     auto CorrespondingIndexResult =
1066         [&](const CodeCompletionResult &SemaResult) -> const Symbol * {
1067       if (auto SymID = getSymbolID(SemaResult)) {
1068         auto I = IndexResults.find(*SymID);
1069         if (I != IndexResults.end()) {
1070           UsedIndexResults.insert(&*I);
1071           return &*I;
1072         }
1073       }
1074       return nullptr;
1075     };
1076     // Emit all Sema results, merging them with Index results if possible.
1077     for (auto &SemaResult : Recorder->Results)
1078       AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult));
1079     // Now emit any Index-only results.
1080     for (const auto &IndexResult : IndexResults) {
1081       if (UsedIndexResults.count(&IndexResult))
1082         continue;
1083       AddToBundles(/*SemaResult=*/nullptr, &IndexResult);
1084     }
1085     // We only keep the best N results at any time, in "native" format.
1086     TopN<ScoredBundle, ScoredBundleGreater> Top(
1087         Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1088     for (auto &Bundle : Bundles)
1089       addCandidate(Top, std::move(Bundle));
1090     return std::move(Top).items();
1091   }
1092 
1093   Optional<float> fuzzyScore(const CompletionCandidate &C) {
1094     // Macros can be very spammy, so we only support prefix completion.
1095     // We won't end up with underfull index results, as macros are sema-only.
1096     if (C.SemaResult && C.SemaResult->Kind == CodeCompletionResult::RK_Macro &&
1097         !C.Name.startswith_lower(Filter->pattern()))
1098       return None;
1099     return Filter->match(C.Name);
1100   }
1101 
1102   // Scores a candidate and adds it to the TopN structure.
1103   void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
1104                     CompletionCandidate::Bundle Bundle) {
1105     SymbolQualitySignals Quality;
1106     SymbolRelevanceSignals Relevance;
1107     Relevance.Query = SymbolRelevanceSignals::CodeComplete;
1108     Relevance.FileProximityMatch = &FileProximityMatch;
1109     auto &First = Bundle.front();
1110     if (auto FuzzyScore = fuzzyScore(First))
1111       Relevance.NameMatch = *FuzzyScore;
1112     else
1113       return;
1114     unsigned SemaResult = 0, IndexResult = 0;
1115     for (const auto &Candidate : Bundle) {
1116       if (Candidate.IndexResult) {
1117         Quality.merge(*Candidate.IndexResult);
1118         Relevance.merge(*Candidate.IndexResult);
1119         ++IndexResult;
1120       }
1121       if (Candidate.SemaResult) {
1122         Quality.merge(*Candidate.SemaResult);
1123         Relevance.merge(*Candidate.SemaResult);
1124         ++SemaResult;
1125       }
1126     }
1127 
1128     float QualScore = Quality.evaluate(), RelScore = Relevance.evaluate();
1129     CompletionItemScores Scores;
1130     Scores.finalScore = evaluateSymbolAndRelevance(QualScore, RelScore);
1131     // The purpose of exporting component scores is to allow NameMatch to be
1132     // replaced on the client-side. So we export (NameMatch, final/NameMatch)
1133     // rather than (RelScore, QualScore).
1134     Scores.filterScore = Relevance.NameMatch;
1135     Scores.symbolScore =
1136         Scores.filterScore ? Scores.finalScore / Scores.filterScore : QualScore;
1137 
1138     LLVM_DEBUG(llvm::dbgs() << "CodeComplete: " << First.Name << "("
1139                             << IndexResult << " index) "
1140                             << "(" << SemaResult << " sema)"
1141                             << " = " << Scores.finalScore << "\n"
1142                             << Quality << Relevance << "\n");
1143 
1144     NSema += bool(SemaResult);
1145     NIndex += bool(IndexResult);
1146     NBoth += SemaResult && IndexResult;
1147     if (Candidates.push({std::move(Bundle), Scores}))
1148       Incomplete = true;
1149   }
1150 
1151   CompletionItem toCompletionItem(const CompletionCandidate::Bundle &Bundle,
1152                                   const CompletionItemScores &Scores) {
1153     CodeCompletionString *SemaCCS = nullptr;
1154     std::string FrontDocComment;
1155     if (auto *SR = Bundle.front().SemaResult) {
1156       SemaCCS = Recorder->codeCompletionString(*SR);
1157       if (Opts.IncludeComments) {
1158         assert(Recorder->CCSema);
1159         FrontDocComment = getDocComment(Recorder->CCSema->getASTContext(), *SR,
1160                                         /*CommentsFromHeader=*/false);
1161       }
1162     }
1163     return CompletionCandidate::build(
1164         Bundle,
1165         Bundle.front().build(FileName, Scores, Opts, SemaCCS, *Includes,
1166                              FrontDocComment),
1167         Opts);
1168   }
1169 };
1170 
1171 CompletionList codeComplete(PathRef FileName,
1172                             const tooling::CompileCommand &Command,
1173                             PrecompiledPreamble const *Preamble,
1174                             const std::vector<Inclusion> &PreambleInclusions,
1175                             StringRef Contents, Position Pos,
1176                             IntrusiveRefCntPtr<vfs::FileSystem> VFS,
1177                             std::shared_ptr<PCHContainerOperations> PCHs,
1178                             CodeCompleteOptions Opts) {
1179   return CodeCompleteFlow(FileName, Opts)
1180       .run({FileName, Command, Preamble, PreambleInclusions, Contents, Pos, VFS,
1181             PCHs});
1182 }
1183 
1184 SignatureHelp signatureHelp(PathRef FileName,
1185                             const tooling::CompileCommand &Command,
1186                             PrecompiledPreamble const *Preamble,
1187                             StringRef Contents, Position Pos,
1188                             IntrusiveRefCntPtr<vfs::FileSystem> VFS,
1189                             std::shared_ptr<PCHContainerOperations> PCHs) {
1190   SignatureHelp Result;
1191   clang::CodeCompleteOptions Options;
1192   Options.IncludeGlobals = false;
1193   Options.IncludeMacros = false;
1194   Options.IncludeCodePatterns = false;
1195   Options.IncludeBriefComments = false;
1196   std::vector<Inclusion> PreambleInclusions = {}; // Unused for signatureHelp
1197   semaCodeComplete(llvm::make_unique<SignatureHelpCollector>(Options, Result),
1198                    Options,
1199                    {FileName, Command, Preamble, PreambleInclusions, Contents,
1200                     Pos, std::move(VFS), std::move(PCHs)});
1201   return Result;
1202 }
1203 
1204 bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) {
1205   using namespace clang::ast_matchers;
1206   auto InTopLevelScope = hasDeclContext(
1207       anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl()));
1208   return !match(decl(anyOf(InTopLevelScope,
1209                            hasDeclContext(
1210                                enumDecl(InTopLevelScope, unless(isScoped()))))),
1211                 ND, ASTCtx)
1212               .empty();
1213 }
1214 
1215 } // namespace clangd
1216 } // namespace clang
1217