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