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