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