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.value_or(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).has_value();
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.has_value();
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().value_or(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) 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() &&
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       break;
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 0;
1023           case OC::CK_Function:
1024             return 1;
1025           case OC::CK_FunctionType:
1026             return 2;
1027           case OC::CK_FunctionProtoTypeLoc:
1028             return 3;
1029           case OC::CK_FunctionTemplate:
1030             return 4;
1031           case OC::CK_Template:
1032             return 5;
1033           }
1034           llvm_unreachable("Unknown overload candidate type.");
1035         };
1036         return KindPriority(L.Quality.Kind) < KindPriority(R.Quality.Kind);
1037       }
1038       if (L.Signature.label.size() != R.Signature.label.size())
1039         return L.Signature.label.size() < R.Signature.label.size();
1040       return L.Signature.label < R.Signature.label;
1041     });
1042 
1043     for (auto &SS : ScoredSignatures) {
1044       auto IndexDocIt =
1045           SS.IDForDoc ? FetchedDocs.find(SS.IDForDoc) : FetchedDocs.end();
1046       if (IndexDocIt != FetchedDocs.end()) {
1047         markup::Document SignatureComment;
1048         parseDocumentation(IndexDocIt->second, SignatureComment);
1049         SS.Signature.documentation =
1050             renderDoc(SignatureComment, DocumentationFormat);
1051       }
1052 
1053       SigHelp.signatures.push_back(std::move(SS.Signature));
1054     }
1055   }
1056 
1057   GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
1058 
1059   CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
1060 
1061 private:
1062   void processParameterChunk(llvm::StringRef ChunkText,
1063                              SignatureInformation &Signature) const {
1064     // (!) this is O(n), should still be fast compared to building ASTs.
1065     unsigned ParamStartOffset = lspLength(Signature.label);
1066     unsigned ParamEndOffset = ParamStartOffset + lspLength(ChunkText);
1067     // A piece of text that describes the parameter that corresponds to
1068     // the code-completion location within a function call, message send,
1069     // macro invocation, etc.
1070     Signature.label += ChunkText;
1071     ParameterInformation Info;
1072     Info.labelOffsets.emplace(ParamStartOffset, ParamEndOffset);
1073     // FIXME: only set 'labelOffsets' when all clients migrate out of it.
1074     Info.labelString = std::string(ChunkText);
1075 
1076     Signature.parameters.push_back(std::move(Info));
1077   }
1078 
1079   void processOptionalChunk(const CodeCompletionString &CCS,
1080                             SignatureInformation &Signature,
1081                             SignatureQualitySignals &Signal) const {
1082     for (const auto &Chunk : CCS) {
1083       switch (Chunk.Kind) {
1084       case CodeCompletionString::CK_Optional:
1085         assert(Chunk.Optional &&
1086                "Expected the optional code completion string to be non-null.");
1087         processOptionalChunk(*Chunk.Optional, Signature, Signal);
1088         break;
1089       case CodeCompletionString::CK_VerticalSpace:
1090         break;
1091       case CodeCompletionString::CK_CurrentParameter:
1092       case CodeCompletionString::CK_Placeholder:
1093         processParameterChunk(Chunk.Text, Signature);
1094         Signal.NumberOfOptionalParameters++;
1095         break;
1096       default:
1097         Signature.label += Chunk.Text;
1098         break;
1099       }
1100     }
1101   }
1102 
1103   // FIXME(ioeric): consider moving CodeCompletionString logic here to
1104   // CompletionString.h.
1105   ScoredSignature processOverloadCandidate(const OverloadCandidate &Candidate,
1106                                            const CodeCompletionString &CCS,
1107                                            llvm::StringRef DocComment) const {
1108     SignatureInformation Signature;
1109     SignatureQualitySignals Signal;
1110     const char *ReturnType = nullptr;
1111 
1112     markup::Document OverloadComment;
1113     parseDocumentation(formatDocumentation(CCS, DocComment), OverloadComment);
1114     Signature.documentation = renderDoc(OverloadComment, DocumentationFormat);
1115     Signal.Kind = Candidate.getKind();
1116 
1117     for (const auto &Chunk : CCS) {
1118       switch (Chunk.Kind) {
1119       case CodeCompletionString::CK_ResultType:
1120         // A piece of text that describes the type of an entity or,
1121         // for functions and methods, the return type.
1122         assert(!ReturnType && "Unexpected CK_ResultType");
1123         ReturnType = Chunk.Text;
1124         break;
1125       case CodeCompletionString::CK_CurrentParameter:
1126       case CodeCompletionString::CK_Placeholder:
1127         processParameterChunk(Chunk.Text, Signature);
1128         Signal.NumberOfParameters++;
1129         break;
1130       case CodeCompletionString::CK_Optional: {
1131         // The rest of the parameters are defaulted/optional.
1132         assert(Chunk.Optional &&
1133                "Expected the optional code completion string to be non-null.");
1134         processOptionalChunk(*Chunk.Optional, Signature, Signal);
1135         break;
1136       }
1137       case CodeCompletionString::CK_VerticalSpace:
1138         break;
1139       default:
1140         Signature.label += Chunk.Text;
1141         break;
1142       }
1143     }
1144     if (ReturnType) {
1145       Signature.label += " -> ";
1146       Signature.label += ReturnType;
1147     }
1148     dlog("Signal for {0}: {1}", Signature, Signal);
1149     ScoredSignature Result;
1150     Result.Signature = std::move(Signature);
1151     Result.Quality = Signal;
1152     const FunctionDecl *Func = Candidate.getFunction();
1153     if (Func && Result.Signature.documentation.value.empty()) {
1154       // Computing USR caches linkage, which may change after code completion.
1155       if (!hasUnstableLinkage(Func))
1156         Result.IDForDoc = clangd::getSymbolID(Func);
1157     }
1158     return Result;
1159   }
1160 
1161   SignatureHelp &SigHelp;
1162   std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
1163   CodeCompletionTUInfo CCTUInfo;
1164   const SymbolIndex *Index;
1165   MarkupKind DocumentationFormat;
1166 }; // SignatureHelpCollector
1167 
1168 // Used only for completion of C-style comments in function call (i.e.
1169 // /*foo=*/7). Similar to SignatureHelpCollector, but needs to do less work.
1170 class ParamNameCollector final : public CodeCompleteConsumer {
1171 public:
1172   ParamNameCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
1173                      std::set<std::string> &ParamNames)
1174       : CodeCompleteConsumer(CodeCompleteOpts),
1175         Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
1176         CCTUInfo(Allocator), ParamNames(ParamNames) {}
1177 
1178   void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1179                                  OverloadCandidate *Candidates,
1180                                  unsigned NumCandidates,
1181                                  SourceLocation OpenParLoc,
1182                                  bool Braced) override {
1183     assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
1184            "too many arguments");
1185 
1186     for (unsigned I = 0; I < NumCandidates; ++I) {
1187       if (const NamedDecl *ND = Candidates[I].getParamDecl(CurrentArg))
1188         if (const auto *II = ND->getIdentifier())
1189           ParamNames.emplace(II->getName());
1190     }
1191   }
1192 
1193 private:
1194   GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
1195 
1196   CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
1197 
1198   std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
1199   CodeCompletionTUInfo CCTUInfo;
1200   std::set<std::string> &ParamNames;
1201 };
1202 
1203 struct SemaCompleteInput {
1204   PathRef FileName;
1205   size_t Offset;
1206   const PreambleData &Preamble;
1207   const llvm::Optional<PreamblePatch> Patch;
1208   const ParseInputs &ParseInput;
1209 };
1210 
1211 void loadMainFilePreambleMacros(const Preprocessor &PP,
1212                                 const PreambleData &Preamble) {
1213   // The ExternalPreprocessorSource has our macros, if we know where to look.
1214   // We can read all the macros using PreambleMacros->ReadDefinedMacros(),
1215   // but this includes transitively included files, so may deserialize a lot.
1216   ExternalPreprocessorSource *PreambleMacros = PP.getExternalSource();
1217   // As we have the names of the macros, we can look up their IdentifierInfo
1218   // and then use this to load just the macros we want.
1219   const auto &ITable = PP.getIdentifierTable();
1220   IdentifierInfoLookup *PreambleIdentifiers =
1221       ITable.getExternalIdentifierLookup();
1222 
1223   if (!PreambleIdentifiers || !PreambleMacros)
1224     return;
1225   for (const auto &MacroName : Preamble.Macros.Names) {
1226     if (ITable.find(MacroName.getKey()) != ITable.end())
1227       continue;
1228     if (auto *II = PreambleIdentifiers->get(MacroName.getKey()))
1229       if (II->isOutOfDate())
1230         PreambleMacros->updateOutOfDateIdentifier(*II);
1231   }
1232 }
1233 
1234 // Invokes Sema code completion on a file.
1235 // If \p Includes is set, it will be updated based on the compiler invocation.
1236 bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
1237                       const clang::CodeCompleteOptions &Options,
1238                       const SemaCompleteInput &Input,
1239                       IncludeStructure *Includes = nullptr) {
1240   trace::Span Tracer("Sema completion");
1241 
1242   IgnoreDiagnostics IgnoreDiags;
1243   auto CI = buildCompilerInvocation(Input.ParseInput, IgnoreDiags);
1244   if (!CI) {
1245     elog("Couldn't create CompilerInvocation");
1246     return false;
1247   }
1248   auto &FrontendOpts = CI->getFrontendOpts();
1249   FrontendOpts.SkipFunctionBodies = true;
1250   // Disable typo correction in Sema.
1251   CI->getLangOpts()->SpellChecking = false;
1252   // Code completion won't trigger in delayed template bodies.
1253   // This is on-by-default in windows to allow parsing SDK headers; we're only
1254   // disabling it for the main-file (not preamble).
1255   CI->getLangOpts()->DelayedTemplateParsing = false;
1256   // Setup code completion.
1257   FrontendOpts.CodeCompleteOpts = Options;
1258   FrontendOpts.CodeCompletionAt.FileName = std::string(Input.FileName);
1259   std::tie(FrontendOpts.CodeCompletionAt.Line,
1260            FrontendOpts.CodeCompletionAt.Column) =
1261       offsetToClangLineColumn(Input.ParseInput.Contents, Input.Offset);
1262 
1263   std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1264       llvm::MemoryBuffer::getMemBuffer(Input.ParseInput.Contents,
1265                                        Input.FileName);
1266   // The diagnostic options must be set before creating a CompilerInstance.
1267   CI->getDiagnosticOpts().IgnoreWarnings = true;
1268   // We reuse the preamble whether it's valid or not. This is a
1269   // correctness/performance tradeoff: building without a preamble is slow, and
1270   // completion is latency-sensitive.
1271   // However, if we're completing *inside* the preamble section of the draft,
1272   // overriding the preamble will break sema completion. Fortunately we can just
1273   // skip all includes in this case; these completions are really simple.
1274   PreambleBounds PreambleRegion =
1275       ComputePreambleBounds(*CI->getLangOpts(), *ContentsBuffer, 0);
1276   bool CompletingInPreamble = Input.Offset < PreambleRegion.Size ||
1277                               (!PreambleRegion.PreambleEndsAtStartOfLine &&
1278                                Input.Offset == PreambleRegion.Size);
1279   if (Input.Patch)
1280     Input.Patch->apply(*CI);
1281   // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
1282   // the remapped buffers do not get freed.
1283   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
1284       Input.ParseInput.TFS->view(Input.ParseInput.CompileCommand.Directory);
1285   if (Input.Preamble.StatCache)
1286     VFS = Input.Preamble.StatCache->getConsumingFS(std::move(VFS));
1287   auto Clang = prepareCompilerInstance(
1288       std::move(CI), !CompletingInPreamble ? &Input.Preamble.Preamble : nullptr,
1289       std::move(ContentsBuffer), std::move(VFS), IgnoreDiags);
1290   Clang->getPreprocessorOpts().SingleFileParseMode = CompletingInPreamble;
1291   Clang->setCodeCompletionConsumer(Consumer.release());
1292 
1293   SyntaxOnlyAction Action;
1294   if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
1295     log("BeginSourceFile() failed when running codeComplete for {0}",
1296         Input.FileName);
1297     return false;
1298   }
1299   // Macros can be defined within the preamble region of the main file.
1300   // They don't fall nicely into our index/Sema dichotomy:
1301   //  - they're not indexed for completion (they're not available across files)
1302   //  - but Sema code complete won't see them: as part of the preamble, they're
1303   //    deserialized only when mentioned.
1304   // Force them to be deserialized so SemaCodeComplete sees them.
1305   loadMainFilePreambleMacros(Clang->getPreprocessor(), Input.Preamble);
1306   if (Includes)
1307     Includes->collect(*Clang);
1308   if (llvm::Error Err = Action.Execute()) {
1309     log("Execute() failed when running codeComplete for {0}: {1}",
1310         Input.FileName, toString(std::move(Err)));
1311     return false;
1312   }
1313   Action.EndSourceFile();
1314 
1315   return true;
1316 }
1317 
1318 // Should we allow index completions in the specified context?
1319 bool allowIndex(CodeCompletionContext &CC) {
1320   if (!contextAllowsIndex(CC.getKind()))
1321     return false;
1322   // We also avoid ClassName::bar (but allow namespace::bar).
1323   auto Scope = CC.getCXXScopeSpecifier();
1324   if (!Scope)
1325     return true;
1326   NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
1327   if (!NameSpec)
1328     return true;
1329   // We only query the index when qualifier is a namespace.
1330   // If it's a class, we rely solely on sema completions.
1331   switch (NameSpec->getKind()) {
1332   case NestedNameSpecifier::Global:
1333   case NestedNameSpecifier::Namespace:
1334   case NestedNameSpecifier::NamespaceAlias:
1335     return true;
1336   case NestedNameSpecifier::Super:
1337   case NestedNameSpecifier::TypeSpec:
1338   case NestedNameSpecifier::TypeSpecWithTemplate:
1339   // Unresolved inside a template.
1340   case NestedNameSpecifier::Identifier:
1341     return false;
1342   }
1343   llvm_unreachable("invalid NestedNameSpecifier kind");
1344 }
1345 
1346 std::future<SymbolSlab> startAsyncFuzzyFind(const SymbolIndex &Index,
1347                                             const FuzzyFindRequest &Req) {
1348   return runAsync<SymbolSlab>([&Index, Req]() {
1349     trace::Span Tracer("Async fuzzyFind");
1350     SymbolSlab::Builder Syms;
1351     Index.fuzzyFind(Req, [&Syms](const Symbol &Sym) { Syms.insert(Sym); });
1352     return std::move(Syms).build();
1353   });
1354 }
1355 
1356 // Creates a `FuzzyFindRequest` based on the cached index request from the
1357 // last completion, if any, and the speculated completion filter text in the
1358 // source code.
1359 FuzzyFindRequest speculativeFuzzyFindRequestForCompletion(
1360     FuzzyFindRequest CachedReq, const CompletionPrefix &HeuristicPrefix) {
1361   CachedReq.Query = std::string(HeuristicPrefix.Name);
1362   return CachedReq;
1363 }
1364 
1365 // Runs Sema-based (AST) and Index-based completion, returns merged results.
1366 //
1367 // There are a few tricky considerations:
1368 //   - the AST provides information needed for the index query (e.g. which
1369 //     namespaces to search in). So Sema must start first.
1370 //   - we only want to return the top results (Opts.Limit).
1371 //     Building CompletionItems for everything else is wasteful, so we want to
1372 //     preserve the "native" format until we're done with scoring.
1373 //   - the data underlying Sema completion items is owned by the AST and various
1374 //     other arenas, which must stay alive for us to build CompletionItems.
1375 //   - we may get duplicate results from Sema and the Index, we need to merge.
1376 //
1377 // So we start Sema completion first, and do all our work in its callback.
1378 // We use the Sema context information to query the index.
1379 // Then we merge the two result sets, producing items that are Sema/Index/Both.
1380 // These items are scored, and the top N are synthesized into the LSP response.
1381 // Finally, we can clean up the data structures created by Sema completion.
1382 //
1383 // Main collaborators are:
1384 //   - semaCodeComplete sets up the compiler machinery to run code completion.
1385 //   - CompletionRecorder captures Sema completion results, including context.
1386 //   - SymbolIndex (Opts.Index) provides index completion results as Symbols
1387 //   - CompletionCandidates are the result of merging Sema and Index results.
1388 //     Each candidate points to an underlying CodeCompletionResult (Sema), a
1389 //     Symbol (Index), or both. It computes the result quality score.
1390 //     CompletionCandidate also does conversion to CompletionItem (at the end).
1391 //   - FuzzyMatcher scores how the candidate matches the partial identifier.
1392 //     This score is combined with the result quality score for the final score.
1393 //   - TopN determines the results with the best score.
1394 class CodeCompleteFlow {
1395   PathRef FileName;
1396   IncludeStructure Includes;           // Complete once the compiler runs.
1397   SpeculativeFuzzyFind *SpecFuzzyFind; // Can be nullptr.
1398   const CodeCompleteOptions &Opts;
1399 
1400   // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup.
1401   CompletionRecorder *Recorder = nullptr;
1402   CodeCompletionContext::Kind CCContextKind = CodeCompletionContext::CCC_Other;
1403   bool IsUsingDeclaration = false;
1404   // The snippets will not be generated if the token following completion
1405   // location is an opening parenthesis (tok::l_paren) because this would add
1406   // extra parenthesis.
1407   tok::TokenKind NextTokenKind = tok::eof;
1408   // Counters for logging.
1409   int NSema = 0, NIndex = 0, NSemaAndIndex = 0, NIdent = 0;
1410   bool Incomplete = false; // Would more be available with a higher limit?
1411   CompletionPrefix HeuristicPrefix;
1412   llvm::Optional<FuzzyMatcher> Filter; // Initialized once Sema runs.
1413   Range ReplacedRange;
1414   std::vector<std::string> QueryScopes; // Initialized once Sema runs.
1415   // Initialized once QueryScopes is initialized, if there are scopes.
1416   llvm::Optional<ScopeDistance> ScopeProximity;
1417   llvm::Optional<OpaqueType> PreferredType; // Initialized once Sema runs.
1418   // Whether to query symbols from any scope. Initialized once Sema runs.
1419   bool AllScopes = false;
1420   llvm::StringSet<> ContextWords;
1421   // Include-insertion and proximity scoring rely on the include structure.
1422   // This is available after Sema has run.
1423   llvm::Optional<IncludeInserter> Inserter;  // Available during runWithSema.
1424   llvm::Optional<URIDistance> FileProximity; // Initialized once Sema runs.
1425   /// Speculative request based on the cached request and the filter text before
1426   /// the cursor.
1427   /// Initialized right before sema run. This is only set if `SpecFuzzyFind` is
1428   /// set and contains a cached request.
1429   llvm::Optional<FuzzyFindRequest> SpecReq;
1430 
1431 public:
1432   // A CodeCompleteFlow object is only useful for calling run() exactly once.
1433   CodeCompleteFlow(PathRef FileName, const IncludeStructure &Includes,
1434                    SpeculativeFuzzyFind *SpecFuzzyFind,
1435                    const CodeCompleteOptions &Opts)
1436       : FileName(FileName), Includes(Includes), SpecFuzzyFind(SpecFuzzyFind),
1437         Opts(Opts) {}
1438 
1439   CodeCompleteResult run(const SemaCompleteInput &SemaCCInput) && {
1440     trace::Span Tracer("CodeCompleteFlow");
1441     HeuristicPrefix = guessCompletionPrefix(SemaCCInput.ParseInput.Contents,
1442                                             SemaCCInput.Offset);
1443     populateContextWords(SemaCCInput.ParseInput.Contents);
1444     if (Opts.Index && SpecFuzzyFind && SpecFuzzyFind->CachedReq) {
1445       assert(!SpecFuzzyFind->Result.valid());
1446       SpecReq = speculativeFuzzyFindRequestForCompletion(
1447           *SpecFuzzyFind->CachedReq, HeuristicPrefix);
1448       SpecFuzzyFind->Result = startAsyncFuzzyFind(*Opts.Index, *SpecReq);
1449     }
1450 
1451     // We run Sema code completion first. It builds an AST and calculates:
1452     //   - completion results based on the AST.
1453     //   - partial identifier and context. We need these for the index query.
1454     CodeCompleteResult Output;
1455     auto RecorderOwner = std::make_unique<CompletionRecorder>(Opts, [&]() {
1456       assert(Recorder && "Recorder is not set");
1457       CCContextKind = Recorder->CCContext.getKind();
1458       IsUsingDeclaration = Recorder->CCContext.isUsingDeclaration();
1459       auto Style = getFormatStyleForFile(SemaCCInput.FileName,
1460                                          SemaCCInput.ParseInput.Contents,
1461                                          *SemaCCInput.ParseInput.TFS);
1462       const auto NextToken = Lexer::findNextToken(
1463           Recorder->CCSema->getPreprocessor().getCodeCompletionLoc(),
1464           Recorder->CCSema->getSourceManager(), Recorder->CCSema->LangOpts);
1465       if (NextToken)
1466         NextTokenKind = NextToken->getKind();
1467       // If preprocessor was run, inclusions from preprocessor callback should
1468       // already be added to Includes.
1469       Inserter.emplace(
1470           SemaCCInput.FileName, SemaCCInput.ParseInput.Contents, Style,
1471           SemaCCInput.ParseInput.CompileCommand.Directory,
1472           &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo());
1473       for (const auto &Inc : Includes.MainFileIncludes)
1474         Inserter->addExisting(Inc);
1475 
1476       // Most of the cost of file proximity is in initializing the FileDistance
1477       // structures based on the observed includes, once per query. Conceptually
1478       // that happens here (though the per-URI-scheme initialization is lazy).
1479       // The per-result proximity scoring is (amortized) very cheap.
1480       FileDistanceOptions ProxOpts{}; // Use defaults.
1481       const auto &SM = Recorder->CCSema->getSourceManager();
1482       llvm::StringMap<SourceParams> ProxSources;
1483       auto MainFileID =
1484           Includes.getID(SM.getFileEntryForID(SM.getMainFileID()));
1485       assert(MainFileID);
1486       for (auto &HeaderIDAndDepth : Includes.includeDepth(*MainFileID)) {
1487         auto &Source =
1488             ProxSources[Includes.getRealPath(HeaderIDAndDepth.getFirst())];
1489         Source.Cost = HeaderIDAndDepth.getSecond() * ProxOpts.IncludeCost;
1490         // Symbols near our transitive includes are good, but only consider
1491         // things in the same directory or below it. Otherwise there can be
1492         // many false positives.
1493         if (HeaderIDAndDepth.getSecond() > 0)
1494           Source.MaxUpTraversals = 1;
1495       }
1496       FileProximity.emplace(ProxSources, ProxOpts);
1497 
1498       Output = runWithSema();
1499       Inserter.reset(); // Make sure this doesn't out-live Clang.
1500       SPAN_ATTACH(Tracer, "sema_completion_kind",
1501                   getCompletionKindString(CCContextKind));
1502       log("Code complete: sema context {0}, query scopes [{1}] (AnyScope={2}), "
1503           "expected type {3}{4}",
1504           getCompletionKindString(CCContextKind),
1505           llvm::join(QueryScopes.begin(), QueryScopes.end(), ","), AllScopes,
1506           PreferredType ? Recorder->CCContext.getPreferredType().getAsString()
1507                         : "<none>",
1508           IsUsingDeclaration ? ", inside using declaration" : "");
1509     });
1510 
1511     Recorder = RecorderOwner.get();
1512 
1513     semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
1514                      SemaCCInput, &Includes);
1515     logResults(Output, Tracer);
1516     return Output;
1517   }
1518 
1519   void logResults(const CodeCompleteResult &Output, const trace::Span &Tracer) {
1520     SPAN_ATTACH(Tracer, "sema_results", NSema);
1521     SPAN_ATTACH(Tracer, "index_results", NIndex);
1522     SPAN_ATTACH(Tracer, "merged_results", NSemaAndIndex);
1523     SPAN_ATTACH(Tracer, "identifier_results", NIdent);
1524     SPAN_ATTACH(Tracer, "returned_results", int64_t(Output.Completions.size()));
1525     SPAN_ATTACH(Tracer, "incomplete", Output.HasMore);
1526     log("Code complete: {0} results from Sema, {1} from Index, "
1527         "{2} matched, {3} from identifiers, {4} returned{5}.",
1528         NSema, NIndex, NSemaAndIndex, NIdent, Output.Completions.size(),
1529         Output.HasMore ? " (incomplete)" : "");
1530     assert(!Opts.Limit || Output.Completions.size() <= Opts.Limit);
1531     // We don't assert that isIncomplete means we hit a limit.
1532     // Indexes may choose to impose their own limits even if we don't have one.
1533   }
1534 
1535   CodeCompleteResult runWithoutSema(llvm::StringRef Content, size_t Offset,
1536                                     const ThreadsafeFS &TFS) && {
1537     trace::Span Tracer("CodeCompleteWithoutSema");
1538     // Fill in fields normally set by runWithSema()
1539     HeuristicPrefix = guessCompletionPrefix(Content, Offset);
1540     populateContextWords(Content);
1541     CCContextKind = CodeCompletionContext::CCC_Recovery;
1542     IsUsingDeclaration = false;
1543     Filter = FuzzyMatcher(HeuristicPrefix.Name);
1544     auto Pos = offsetToPosition(Content, Offset);
1545     ReplacedRange.start = ReplacedRange.end = Pos;
1546     ReplacedRange.start.character -= HeuristicPrefix.Name.size();
1547 
1548     llvm::StringMap<SourceParams> ProxSources;
1549     ProxSources[FileName].Cost = 0;
1550     FileProximity.emplace(ProxSources);
1551 
1552     auto Style = getFormatStyleForFile(FileName, Content, TFS);
1553     // This will only insert verbatim headers.
1554     Inserter.emplace(FileName, Content, Style,
1555                      /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr);
1556 
1557     auto Identifiers = collectIdentifiers(Content, Style);
1558     std::vector<RawIdentifier> IdentifierResults;
1559     for (const auto &IDAndCount : Identifiers) {
1560       RawIdentifier ID;
1561       ID.Name = IDAndCount.first();
1562       ID.References = IDAndCount.second;
1563       // Avoid treating typed filter as an identifier.
1564       if (ID.Name == HeuristicPrefix.Name)
1565         --ID.References;
1566       if (ID.References > 0)
1567         IdentifierResults.push_back(std::move(ID));
1568     }
1569 
1570     // Simplified version of getQueryScopes():
1571     //  - accessible scopes are determined heuristically.
1572     //  - all-scopes query if no qualifier was typed (and it's allowed).
1573     SpecifiedScope Scopes;
1574     Scopes.AccessibleScopes = visibleNamespaces(
1575         Content.take_front(Offset), format::getFormattingLangOpts(Style));
1576     for (std::string &S : Scopes.AccessibleScopes)
1577       if (!S.empty())
1578         S.append("::"); // visibleNamespaces doesn't include trailing ::.
1579     if (HeuristicPrefix.Qualifier.empty())
1580       AllScopes = Opts.AllScopes;
1581     else if (HeuristicPrefix.Qualifier.startswith("::")) {
1582       Scopes.AccessibleScopes = {""};
1583       Scopes.UnresolvedQualifier =
1584           std::string(HeuristicPrefix.Qualifier.drop_front(2));
1585     } else
1586       Scopes.UnresolvedQualifier = std::string(HeuristicPrefix.Qualifier);
1587     // First scope is the (modified) enclosing scope.
1588     QueryScopes = Scopes.scopesForIndexQuery();
1589     ScopeProximity.emplace(QueryScopes);
1590 
1591     SymbolSlab IndexResults = Opts.Index ? queryIndex() : SymbolSlab();
1592 
1593     CodeCompleteResult Output = toCodeCompleteResult(mergeResults(
1594         /*SemaResults=*/{}, IndexResults, IdentifierResults));
1595     Output.RanParser = false;
1596     logResults(Output, Tracer);
1597     return Output;
1598   }
1599 
1600 private:
1601   void populateContextWords(llvm::StringRef Content) {
1602     // Take last 3 lines before the completion point.
1603     unsigned RangeEnd = HeuristicPrefix.Qualifier.begin() - Content.data(),
1604              RangeBegin = RangeEnd;
1605     for (size_t I = 0; I < 3 && RangeBegin > 0; ++I) {
1606       auto PrevNL = Content.rfind('\n', RangeBegin);
1607       if (PrevNL == StringRef::npos) {
1608         RangeBegin = 0;
1609         break;
1610       }
1611       RangeBegin = PrevNL;
1612     }
1613 
1614     ContextWords = collectWords(Content.slice(RangeBegin, RangeEnd));
1615     dlog("Completion context words: {0}",
1616          llvm::join(ContextWords.keys(), ", "));
1617   }
1618 
1619   // This is called by run() once Sema code completion is done, but before the
1620   // Sema data structures are torn down. It does all the real work.
1621   CodeCompleteResult runWithSema() {
1622     const auto &CodeCompletionRange = CharSourceRange::getCharRange(
1623         Recorder->CCSema->getPreprocessor().getCodeCompletionTokenRange());
1624     // When we are getting completions with an empty identifier, for example
1625     //    std::vector<int> asdf;
1626     //    asdf.^;
1627     // Then the range will be invalid and we will be doing insertion, use
1628     // current cursor position in such cases as range.
1629     if (CodeCompletionRange.isValid()) {
1630       ReplacedRange = halfOpenToRange(Recorder->CCSema->getSourceManager(),
1631                                       CodeCompletionRange);
1632     } else {
1633       const auto &Pos = sourceLocToPosition(
1634           Recorder->CCSema->getSourceManager(),
1635           Recorder->CCSema->getPreprocessor().getCodeCompletionLoc());
1636       ReplacedRange.start = ReplacedRange.end = Pos;
1637     }
1638     Filter = FuzzyMatcher(
1639         Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
1640     std::tie(QueryScopes, AllScopes) = getQueryScopes(
1641         Recorder->CCContext, *Recorder->CCSema, HeuristicPrefix, Opts);
1642     if (!QueryScopes.empty())
1643       ScopeProximity.emplace(QueryScopes);
1644     PreferredType =
1645         OpaqueType::fromType(Recorder->CCSema->getASTContext(),
1646                              Recorder->CCContext.getPreferredType());
1647     // Sema provides the needed context to query the index.
1648     // FIXME: in addition to querying for extra/overlapping symbols, we should
1649     //        explicitly request symbols corresponding to Sema results.
1650     //        We can use their signals even if the index can't suggest them.
1651     // We must copy index results to preserve them, but there are at most Limit.
1652     auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
1653                             ? queryIndex()
1654                             : SymbolSlab();
1655     trace::Span Tracer("Populate CodeCompleteResult");
1656     // Merge Sema and Index results, score them, and pick the winners.
1657     auto Top =
1658         mergeResults(Recorder->Results, IndexResults, /*Identifiers*/ {});
1659     return toCodeCompleteResult(Top);
1660   }
1661 
1662   CodeCompleteResult
1663   toCodeCompleteResult(const std::vector<ScoredBundle> &Scored) {
1664     CodeCompleteResult Output;
1665 
1666     // Convert the results to final form, assembling the expensive strings.
1667     for (auto &C : Scored) {
1668       Output.Completions.push_back(toCodeCompletion(C.first));
1669       Output.Completions.back().Score = C.second;
1670       Output.Completions.back().CompletionTokenRange = ReplacedRange;
1671     }
1672     Output.HasMore = Incomplete;
1673     Output.Context = CCContextKind;
1674     Output.CompletionRange = ReplacedRange;
1675     return Output;
1676   }
1677 
1678   bool includeSymbolFromIndex(const Symbol &Sym) {
1679     if (CCContextKind == CodeCompletionContext::CCC_ObjCProtocolName) {
1680       return Sym.SymInfo.Lang == index::SymbolLanguage::ObjC &&
1681           Sym.SymInfo.Kind == index::SymbolKind::Protocol;
1682     }
1683     return true;
1684   }
1685 
1686   SymbolSlab queryIndex() {
1687     trace::Span Tracer("Query index");
1688     SPAN_ATTACH(Tracer, "limit", int64_t(Opts.Limit));
1689 
1690     // Build the query.
1691     FuzzyFindRequest Req;
1692     if (Opts.Limit)
1693       Req.Limit = Opts.Limit;
1694     Req.Query = std::string(Filter->pattern());
1695     Req.RestrictForCodeCompletion = true;
1696     Req.Scopes = QueryScopes;
1697     Req.AnyScope = AllScopes;
1698     // FIXME: we should send multiple weighted paths here.
1699     Req.ProximityPaths.push_back(std::string(FileName));
1700     if (PreferredType)
1701       Req.PreferredTypes.push_back(std::string(PreferredType->raw()));
1702     vlog("Code complete: fuzzyFind({0:2})", toJSON(Req));
1703 
1704     if (SpecFuzzyFind)
1705       SpecFuzzyFind->NewReq = Req;
1706     if (SpecFuzzyFind && SpecFuzzyFind->Result.valid() && (*SpecReq == Req)) {
1707       vlog("Code complete: speculative fuzzy request matches the actual index "
1708            "request. Waiting for the speculative index results.");
1709       SPAN_ATTACH(Tracer, "Speculative results", true);
1710 
1711       trace::Span WaitSpec("Wait speculative results");
1712       return SpecFuzzyFind->Result.get();
1713     }
1714 
1715     SPAN_ATTACH(Tracer, "Speculative results", false);
1716 
1717     // Run the query against the index.
1718     SymbolSlab::Builder ResultsBuilder;
1719     if (Opts.Index->fuzzyFind(Req, [&](const Symbol &Sym) {
1720           if (includeSymbolFromIndex(Sym))
1721             ResultsBuilder.insert(Sym);
1722         }))
1723       Incomplete = true;
1724     return std::move(ResultsBuilder).build();
1725   }
1726 
1727   // Merges Sema and Index results where possible, to form CompletionCandidates.
1728   // \p Identifiers is raw identifiers that can also be completion candidates.
1729   // Identifiers are not merged with results from index or sema.
1730   // Groups overloads if desired, to form CompletionCandidate::Bundles. The
1731   // bundles are scored and top results are returned, best to worst.
1732   std::vector<ScoredBundle>
1733   mergeResults(const std::vector<CodeCompletionResult> &SemaResults,
1734                const SymbolSlab &IndexResults,
1735                const std::vector<RawIdentifier> &IdentifierResults) {
1736     trace::Span Tracer("Merge and score results");
1737     std::vector<CompletionCandidate::Bundle> Bundles;
1738     llvm::DenseMap<size_t, size_t> BundleLookup;
1739     auto AddToBundles = [&](const CodeCompletionResult *SemaResult,
1740                             const Symbol *IndexResult,
1741                             const RawIdentifier *IdentifierResult) {
1742       CompletionCandidate C;
1743       C.SemaResult = SemaResult;
1744       C.IndexResult = IndexResult;
1745       C.IdentifierResult = IdentifierResult;
1746       if (C.IndexResult) {
1747         C.Name = IndexResult->Name;
1748         C.RankedIncludeHeaders = getRankedIncludes(*C.IndexResult);
1749       } else if (C.SemaResult) {
1750         C.Name = Recorder->getName(*SemaResult);
1751       } else {
1752         assert(IdentifierResult);
1753         C.Name = IdentifierResult->Name;
1754       }
1755       if (auto OverloadSet = C.overloadSet(
1756               Opts, FileName, Inserter ? Inserter.getPointer() : nullptr)) {
1757         auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1758         if (Ret.second)
1759           Bundles.emplace_back();
1760         Bundles[Ret.first->second].push_back(std::move(C));
1761       } else {
1762         Bundles.emplace_back();
1763         Bundles.back().push_back(std::move(C));
1764       }
1765     };
1766     llvm::DenseSet<const Symbol *> UsedIndexResults;
1767     auto CorrespondingIndexResult =
1768         [&](const CodeCompletionResult &SemaResult) -> const Symbol * {
1769       if (auto SymID =
1770               getSymbolID(SemaResult, Recorder->CCSema->getSourceManager())) {
1771         auto I = IndexResults.find(SymID);
1772         if (I != IndexResults.end()) {
1773           UsedIndexResults.insert(&*I);
1774           return &*I;
1775         }
1776       }
1777       return nullptr;
1778     };
1779     // Emit all Sema results, merging them with Index results if possible.
1780     for (auto &SemaResult : SemaResults)
1781       AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult), nullptr);
1782     // Now emit any Index-only results.
1783     for (const auto &IndexResult : IndexResults) {
1784       if (UsedIndexResults.count(&IndexResult))
1785         continue;
1786       AddToBundles(/*SemaResult=*/nullptr, &IndexResult, nullptr);
1787     }
1788     // Emit identifier results.
1789     for (const auto &Ident : IdentifierResults)
1790       AddToBundles(/*SemaResult=*/nullptr, /*IndexResult=*/nullptr, &Ident);
1791     // We only keep the best N results at any time, in "native" format.
1792     TopN<ScoredBundle, ScoredBundleGreater> Top(
1793         Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1794     for (auto &Bundle : Bundles)
1795       addCandidate(Top, std::move(Bundle));
1796     return std::move(Top).items();
1797   }
1798 
1799   llvm::Optional<float> fuzzyScore(const CompletionCandidate &C) {
1800     // Macros can be very spammy, so we only support prefix completion.
1801     if (((C.SemaResult &&
1802           C.SemaResult->Kind == CodeCompletionResult::RK_Macro) ||
1803          (C.IndexResult &&
1804           C.IndexResult->SymInfo.Kind == index::SymbolKind::Macro)) &&
1805         !C.Name.startswith_insensitive(Filter->pattern()))
1806       return None;
1807     return Filter->match(C.Name);
1808   }
1809 
1810   CodeCompletion::Scores
1811   evaluateCompletion(const SymbolQualitySignals &Quality,
1812                      const SymbolRelevanceSignals &Relevance) {
1813     using RM = CodeCompleteOptions::CodeCompletionRankingModel;
1814     CodeCompletion::Scores Scores;
1815     switch (Opts.RankingModel) {
1816     case RM::Heuristics:
1817       Scores.Quality = Quality.evaluateHeuristics();
1818       Scores.Relevance = Relevance.evaluateHeuristics();
1819       Scores.Total =
1820           evaluateSymbolAndRelevance(Scores.Quality, Scores.Relevance);
1821       // NameMatch is in fact a multiplier on total score, so rescoring is
1822       // sound.
1823       Scores.ExcludingName =
1824           Relevance.NameMatch > std::numeric_limits<float>::epsilon()
1825               ? Scores.Total / Relevance.NameMatch
1826               : Scores.Quality;
1827       return Scores;
1828 
1829     case RM::DecisionForest:
1830       DecisionForestScores DFScores = Opts.DecisionForestScorer(
1831           Quality, Relevance, Opts.DecisionForestBase);
1832       Scores.ExcludingName = DFScores.ExcludingName;
1833       Scores.Total = DFScores.Total;
1834       return Scores;
1835     }
1836     llvm_unreachable("Unhandled CodeCompletion ranking model.");
1837   }
1838 
1839   // Scores a candidate and adds it to the TopN structure.
1840   void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
1841                     CompletionCandidate::Bundle Bundle) {
1842     SymbolQualitySignals Quality;
1843     SymbolRelevanceSignals Relevance;
1844     Relevance.Context = CCContextKind;
1845     Relevance.Name = Bundle.front().Name;
1846     Relevance.FilterLength = HeuristicPrefix.Name.size();
1847     Relevance.Query = SymbolRelevanceSignals::CodeComplete;
1848     Relevance.FileProximityMatch = FileProximity.getPointer();
1849     if (ScopeProximity)
1850       Relevance.ScopeProximityMatch = ScopeProximity.getPointer();
1851     if (PreferredType)
1852       Relevance.HadContextType = true;
1853     Relevance.ContextWords = &ContextWords;
1854     Relevance.MainFileSignals = Opts.MainFileSignals;
1855 
1856     auto &First = Bundle.front();
1857     if (auto FuzzyScore = fuzzyScore(First))
1858       Relevance.NameMatch = *FuzzyScore;
1859     else
1860       return;
1861     SymbolOrigin Origin = SymbolOrigin::Unknown;
1862     bool FromIndex = false;
1863     for (const auto &Candidate : Bundle) {
1864       if (Candidate.IndexResult) {
1865         Quality.merge(*Candidate.IndexResult);
1866         Relevance.merge(*Candidate.IndexResult);
1867         Origin |= Candidate.IndexResult->Origin;
1868         FromIndex = true;
1869         if (!Candidate.IndexResult->Type.empty())
1870           Relevance.HadSymbolType |= true;
1871         if (PreferredType &&
1872             PreferredType->raw() == Candidate.IndexResult->Type) {
1873           Relevance.TypeMatchesPreferred = true;
1874         }
1875       }
1876       if (Candidate.SemaResult) {
1877         Quality.merge(*Candidate.SemaResult);
1878         Relevance.merge(*Candidate.SemaResult);
1879         if (PreferredType) {
1880           if (auto CompletionType = OpaqueType::fromCompletionResult(
1881                   Recorder->CCSema->getASTContext(), *Candidate.SemaResult)) {
1882             Relevance.HadSymbolType |= true;
1883             if (PreferredType == CompletionType)
1884               Relevance.TypeMatchesPreferred = true;
1885           }
1886         }
1887         Origin |= SymbolOrigin::AST;
1888       }
1889       if (Candidate.IdentifierResult) {
1890         Quality.References = Candidate.IdentifierResult->References;
1891         Relevance.Scope = SymbolRelevanceSignals::FileScope;
1892         Origin |= SymbolOrigin::Identifier;
1893       }
1894     }
1895 
1896     CodeCompletion::Scores Scores = evaluateCompletion(Quality, Relevance);
1897     if (Opts.RecordCCResult)
1898       Opts.RecordCCResult(toCodeCompletion(Bundle), Quality, Relevance,
1899                           Scores.Total);
1900 
1901     dlog("CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First.Name,
1902          llvm::to_string(Origin), Scores.Total, llvm::to_string(Quality),
1903          llvm::to_string(Relevance));
1904 
1905     NSema += bool(Origin & SymbolOrigin::AST);
1906     NIndex += FromIndex;
1907     NSemaAndIndex += bool(Origin & SymbolOrigin::AST) && FromIndex;
1908     NIdent += bool(Origin & SymbolOrigin::Identifier);
1909     if (Candidates.push({std::move(Bundle), Scores}))
1910       Incomplete = true;
1911   }
1912 
1913   CodeCompletion toCodeCompletion(const CompletionCandidate::Bundle &Bundle) {
1914     llvm::Optional<CodeCompletionBuilder> Builder;
1915     for (const auto &Item : Bundle) {
1916       CodeCompletionString *SemaCCS =
1917           Item.SemaResult ? Recorder->codeCompletionString(*Item.SemaResult)
1918                           : nullptr;
1919       if (!Builder)
1920         Builder.emplace(Recorder ? &Recorder->CCSema->getASTContext() : nullptr,
1921                         Item, SemaCCS, QueryScopes, *Inserter, FileName,
1922                         CCContextKind, Opts, IsUsingDeclaration, NextTokenKind);
1923       else
1924         Builder->add(Item, SemaCCS);
1925     }
1926     return Builder->build();
1927   }
1928 };
1929 
1930 } // namespace
1931 
1932 clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const {
1933   clang::CodeCompleteOptions Result;
1934   Result.IncludeCodePatterns = EnableSnippets;
1935   Result.IncludeMacros = true;
1936   Result.IncludeGlobals = true;
1937   // We choose to include full comments and not do doxygen parsing in
1938   // completion.
1939   // FIXME: ideally, we should support doxygen in some form, e.g. do markdown
1940   // formatting of the comments.
1941   Result.IncludeBriefComments = false;
1942 
1943   // When an is used, Sema is responsible for completing the main file,
1944   // the index can provide results from the preamble.
1945   // Tell Sema not to deserialize the preamble to look for results.
1946   Result.LoadExternal = !Index;
1947   Result.IncludeFixIts = IncludeFixIts;
1948 
1949   return Result;
1950 }
1951 
1952 CompletionPrefix guessCompletionPrefix(llvm::StringRef Content,
1953                                        unsigned Offset) {
1954   assert(Offset <= Content.size());
1955   StringRef Rest = Content.take_front(Offset);
1956   CompletionPrefix Result;
1957 
1958   // Consume the unqualified name. We only handle ASCII characters.
1959   // isAsciiIdentifierContinue will let us match "0invalid", but we don't mind.
1960   while (!Rest.empty() && isAsciiIdentifierContinue(Rest.back()))
1961     Rest = Rest.drop_back();
1962   Result.Name = Content.slice(Rest.size(), Offset);
1963 
1964   // Consume qualifiers.
1965   while (Rest.consume_back("::") && !Rest.endswith(":")) // reject ::::
1966     while (!Rest.empty() && isAsciiIdentifierContinue(Rest.back()))
1967       Rest = Rest.drop_back();
1968   Result.Qualifier =
1969       Content.slice(Rest.size(), Result.Name.begin() - Content.begin());
1970 
1971   return Result;
1972 }
1973 
1974 // Code complete the argument name on "/*" inside function call.
1975 // Offset should be pointing to the start of the comment, i.e.:
1976 // foo(^/*, rather than foo(/*^) where the cursor probably is.
1977 CodeCompleteResult codeCompleteComment(PathRef FileName, unsigned Offset,
1978                                        llvm::StringRef Prefix,
1979                                        const PreambleData *Preamble,
1980                                        const ParseInputs &ParseInput) {
1981   if (Preamble == nullptr) // Can't run without Sema.
1982     return CodeCompleteResult();
1983 
1984   clang::CodeCompleteOptions Options;
1985   Options.IncludeGlobals = false;
1986   Options.IncludeMacros = false;
1987   Options.IncludeCodePatterns = false;
1988   Options.IncludeBriefComments = false;
1989   std::set<std::string> ParamNames;
1990   // We want to see signatures coming from newly introduced includes, hence a
1991   // full patch.
1992   semaCodeComplete(
1993       std::make_unique<ParamNameCollector>(Options, ParamNames), Options,
1994       {FileName, Offset, *Preamble,
1995        PreamblePatch::createFullPatch(FileName, ParseInput, *Preamble),
1996        ParseInput});
1997   if (ParamNames.empty())
1998     return CodeCompleteResult();
1999 
2000   CodeCompleteResult Result;
2001   Result.Context = CodeCompletionContext::CCC_NaturalLanguage;
2002   for (llvm::StringRef Name : ParamNames) {
2003     if (!Name.startswith(Prefix))
2004       continue;
2005     CodeCompletion Item;
2006     Item.Name = Name.str() + "=";
2007     Item.FilterText = Item.Name;
2008     Item.Kind = CompletionItemKind::Text;
2009     Result.Completions.push_back(Item);
2010   }
2011 
2012   return Result;
2013 }
2014 
2015 // If Offset is inside what looks like argument comment (e.g.
2016 // "/*^" or "/* foo^"), returns new offset pointing to the start of the /*
2017 // (place where semaCodeComplete should run).
2018 llvm::Optional<unsigned>
2019 maybeFunctionArgumentCommentStart(llvm::StringRef Content) {
2020   while (!Content.empty() && isAsciiIdentifierContinue(Content.back()))
2021     Content = Content.drop_back();
2022   Content = Content.rtrim();
2023   if (Content.endswith("/*"))
2024     return Content.size() - 2;
2025   return None;
2026 }
2027 
2028 CodeCompleteResult codeComplete(PathRef FileName, Position Pos,
2029                                 const PreambleData *Preamble,
2030                                 const ParseInputs &ParseInput,
2031                                 CodeCompleteOptions Opts,
2032                                 SpeculativeFuzzyFind *SpecFuzzyFind) {
2033   auto Offset = positionToOffset(ParseInput.Contents, Pos);
2034   if (!Offset) {
2035     elog("Code completion position was invalid {0}", Offset.takeError());
2036     return CodeCompleteResult();
2037   }
2038 
2039   auto Content = llvm::StringRef(ParseInput.Contents).take_front(*Offset);
2040   if (auto OffsetBeforeComment = maybeFunctionArgumentCommentStart(Content)) {
2041     // We are doing code completion of a comment, where we currently only
2042     // support completing param names in function calls. To do this, we
2043     // require information from Sema, but Sema's comment completion stops at
2044     // parsing, so we must move back the position before running it, extract
2045     // information we need and construct completion items ourselves.
2046     auto CommentPrefix = Content.substr(*OffsetBeforeComment + 2).trim();
2047     return codeCompleteComment(FileName, *OffsetBeforeComment, CommentPrefix,
2048                                Preamble, ParseInput);
2049   }
2050 
2051   auto Flow = CodeCompleteFlow(
2052       FileName, Preamble ? Preamble->Includes : IncludeStructure(),
2053       SpecFuzzyFind, Opts);
2054   return (!Preamble || Opts.RunParser == CodeCompleteOptions::NeverParse)
2055              ? std::move(Flow).runWithoutSema(ParseInput.Contents, *Offset,
2056                                               *ParseInput.TFS)
2057              : std::move(Flow).run({FileName, *Offset, *Preamble,
2058                                     /*PreamblePatch=*/
2059                                     PreamblePatch::createMacroPatch(
2060                                         FileName, ParseInput, *Preamble),
2061                                     ParseInput});
2062 }
2063 
2064 SignatureHelp signatureHelp(PathRef FileName, Position Pos,
2065                             const PreambleData &Preamble,
2066                             const ParseInputs &ParseInput,
2067                             MarkupKind DocumentationFormat) {
2068   auto Offset = positionToOffset(ParseInput.Contents, Pos);
2069   if (!Offset) {
2070     elog("Signature help position was invalid {0}", Offset.takeError());
2071     return SignatureHelp();
2072   }
2073   SignatureHelp Result;
2074   clang::CodeCompleteOptions Options;
2075   Options.IncludeGlobals = false;
2076   Options.IncludeMacros = false;
2077   Options.IncludeCodePatterns = false;
2078   Options.IncludeBriefComments = false;
2079   semaCodeComplete(
2080       std::make_unique<SignatureHelpCollector>(Options, DocumentationFormat,
2081                                                ParseInput.Index, Result),
2082       Options,
2083       {FileName, *Offset, Preamble,
2084        PreamblePatch::createFullPatch(FileName, ParseInput, Preamble),
2085        ParseInput});
2086   return Result;
2087 }
2088 
2089 bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) {
2090   auto InTopLevelScope = [](const NamedDecl &ND) {
2091     switch (ND.getDeclContext()->getDeclKind()) {
2092     case Decl::TranslationUnit:
2093     case Decl::Namespace:
2094     case Decl::LinkageSpec:
2095       return true;
2096     default:
2097       break;
2098     };
2099     return false;
2100   };
2101   // We only complete symbol's name, which is the same as the name of the
2102   // *primary* template in case of template specializations.
2103   if (isExplicitTemplateSpecialization(&ND))
2104     return false;
2105 
2106   // Category decls are not useful on their own outside the interface or
2107   // implementation blocks. Moreover, sema already provides completion for
2108   // these, even if it requires preamble deserialization. So by excluding them
2109   // from the index, we reduce the noise in all the other completion scopes.
2110   if (llvm::isa<ObjCCategoryDecl>(&ND) || llvm::isa<ObjCCategoryImplDecl>(&ND))
2111     return false;
2112 
2113   if (InTopLevelScope(ND))
2114     return true;
2115 
2116   if (const auto *EnumDecl = dyn_cast<clang::EnumDecl>(ND.getDeclContext()))
2117     return InTopLevelScope(*EnumDecl) && !EnumDecl->isScoped();
2118 
2119   return false;
2120 }
2121 
2122 CompletionItem CodeCompletion::render(const CodeCompleteOptions &Opts) const {
2123   CompletionItem LSP;
2124   const auto *InsertInclude = Includes.empty() ? nullptr : &Includes[0];
2125   LSP.label = ((InsertInclude && InsertInclude->Insertion)
2126                    ? Opts.IncludeIndicator.Insert
2127                    : Opts.IncludeIndicator.NoInsert) +
2128               (Opts.ShowOrigins ? "[" + llvm::to_string(Origin) + "]" : "") +
2129               RequiredQualifier + Name + Signature;
2130 
2131   LSP.kind = Kind;
2132   LSP.detail = BundleSize > 1
2133                    ? std::string(llvm::formatv("[{0} overloads]", BundleSize))
2134                    : ReturnType;
2135   LSP.deprecated = Deprecated;
2136   // Combine header information and documentation in LSP `documentation` field.
2137   // This is not quite right semantically, but tends to display well in editors.
2138   if (InsertInclude || Documentation) {
2139     markup::Document Doc;
2140     if (InsertInclude)
2141       Doc.addParagraph().appendText("From ").appendCode(InsertInclude->Header);
2142     if (Documentation)
2143       Doc.append(*Documentation);
2144     LSP.documentation = renderDoc(Doc, Opts.DocumentationFormat);
2145   }
2146   LSP.sortText = sortText(Score.Total, FilterText);
2147   LSP.filterText = FilterText;
2148   LSP.textEdit = {CompletionTokenRange, RequiredQualifier + Name};
2149   // Merge continuous additionalTextEdits into main edit. The main motivation
2150   // behind this is to help LSP clients, it seems most of them are confused when
2151   // they are provided with additionalTextEdits that are consecutive to main
2152   // edit.
2153   // Note that we store additional text edits from back to front in a line. That
2154   // is mainly to help LSP clients again, so that changes do not effect each
2155   // other.
2156   for (const auto &FixIt : FixIts) {
2157     if (FixIt.range.end == LSP.textEdit->range.start) {
2158       LSP.textEdit->newText = FixIt.newText + LSP.textEdit->newText;
2159       LSP.textEdit->range.start = FixIt.range.start;
2160     } else {
2161       LSP.additionalTextEdits.push_back(FixIt);
2162     }
2163   }
2164   if (Opts.EnableSnippets)
2165     LSP.textEdit->newText += SnippetSuffix;
2166 
2167   // FIXME(kadircet): Do not even fill insertText after making sure textEdit is
2168   // compatible with most of the editors.
2169   LSP.insertText = LSP.textEdit->newText;
2170   // Some clients support snippets but work better with plaintext.
2171   // So if the snippet is trivial, let the client know.
2172   // https://github.com/clangd/clangd/issues/922
2173   LSP.insertTextFormat = (Opts.EnableSnippets && !SnippetSuffix.empty())
2174                              ? InsertTextFormat::Snippet
2175                              : InsertTextFormat::PlainText;
2176   if (InsertInclude && InsertInclude->Insertion)
2177     LSP.additionalTextEdits.push_back(*InsertInclude->Insertion);
2178 
2179   LSP.score = Score.ExcludingName;
2180 
2181   return LSP;
2182 }
2183 
2184 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const CodeCompletion &C) {
2185   // For now just lean on CompletionItem.
2186   return OS << C.render(CodeCompleteOptions());
2187 }
2188 
2189 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
2190                               const CodeCompleteResult &R) {
2191   OS << "CodeCompleteResult: " << R.Completions.size() << (R.HasMore ? "+" : "")
2192      << " (" << getCompletionKindString(R.Context) << ")"
2193      << " items:\n";
2194   for (const auto &C : R.Completions)
2195     OS << C << "\n";
2196   return OS;
2197 }
2198 
2199 // Heuristically detect whether the `Line` is an unterminated include filename.
2200 bool isIncludeFile(llvm::StringRef Line) {
2201   Line = Line.ltrim();
2202   if (!Line.consume_front("#"))
2203     return false;
2204   Line = Line.ltrim();
2205   if (!(Line.consume_front("include_next") || Line.consume_front("include") ||
2206         Line.consume_front("import")))
2207     return false;
2208   Line = Line.ltrim();
2209   if (Line.consume_front("<"))
2210     return Line.count('>') == 0;
2211   if (Line.consume_front("\""))
2212     return Line.count('"') == 0;
2213   return false;
2214 }
2215 
2216 bool allowImplicitCompletion(llvm::StringRef Content, unsigned Offset) {
2217   // Look at last line before completion point only.
2218   Content = Content.take_front(Offset);
2219   auto Pos = Content.rfind('\n');
2220   if (Pos != llvm::StringRef::npos)
2221     Content = Content.substr(Pos + 1);
2222 
2223   // Complete after scope operators.
2224   if (Content.endswith(".") || Content.endswith("->") ||
2225       Content.endswith("::") || Content.endswith("/*"))
2226     return true;
2227   // Complete after `#include <` and #include `<foo/`.
2228   if ((Content.endswith("<") || Content.endswith("\"") ||
2229        Content.endswith("/")) &&
2230       isIncludeFile(Content))
2231     return true;
2232 
2233   // Complete words. Give non-ascii characters the benefit of the doubt.
2234   return !Content.empty() && (isAsciiIdentifierContinue(Content.back()) ||
2235                               !llvm::isASCII(Content.back()));
2236 }
2237 
2238 } // namespace clangd
2239 } // namespace clang
2240