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