1 //===--- CodeComplete.cpp ----------------------------------------*- C++-*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Code completion has several moving parts:
11 //  - AST-based completions are provided using the completion hooks in Sema.
12 //  - external completions are retrieved from the index (using hints from Sema)
13 //  - the two sources overlap, and must be merged and overloads bundled
14 //  - results must be scored and ranked (see Quality.h) before rendering
15 //
16 // Signature help works in a similar way as code completion, but it is simpler:
17 // it's purely AST-based, and there are few candidates.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #include "CodeComplete.h"
22 #include "AST.h"
23 #include "CodeCompletionStrings.h"
24 #include "Compiler.h"
25 #include "Diagnostics.h"
26 #include "FileDistance.h"
27 #include "FuzzyMatch.h"
28 #include "Headers.h"
29 #include "Logger.h"
30 #include "Quality.h"
31 #include "SourceCode.h"
32 #include "TUScheduler.h"
33 #include "Trace.h"
34 #include "URI.h"
35 #include "index/Index.h"
36 #include "clang/ASTMatchers/ASTMatchFinder.h"
37 #include "clang/Basic/LangOptions.h"
38 #include "clang/Basic/SourceLocation.h"
39 #include "clang/Format/Format.h"
40 #include "clang/Frontend/CompilerInstance.h"
41 #include "clang/Frontend/FrontendActions.h"
42 #include "clang/Index/USRGeneration.h"
43 #include "clang/Lex/PreprocessorOptions.h"
44 #include "clang/Sema/CodeCompleteConsumer.h"
45 #include "clang/Sema/Sema.h"
46 #include "clang/Tooling/Core/Replacement.h"
47 #include "llvm/ADT/Optional.h"
48 #include "llvm/ADT/SmallVector.h"
49 #include "llvm/Support/Error.h"
50 #include "llvm/Support/Format.h"
51 #include "llvm/Support/FormatVariadic.h"
52 #include "llvm/Support/ScopedPrinter.h"
53 #include <algorithm>
54 #include <iterator>
55 #include <queue>
56 
57 // We log detailed candidate here if you run with -debug-only=codecomplete.
58 #define DEBUG_TYPE "CodeComplete"
59 
60 namespace clang {
61 namespace clangd {
62 namespace {
63 
64 CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) {
65   using SK = index::SymbolKind;
66   switch (Kind) {
67   case SK::Unknown:
68     return CompletionItemKind::Missing;
69   case SK::Module:
70   case SK::Namespace:
71   case SK::NamespaceAlias:
72     return CompletionItemKind::Module;
73   case SK::Macro:
74     return CompletionItemKind::Text;
75   case SK::Enum:
76     return CompletionItemKind::Enum;
77   // FIXME(ioeric): use LSP struct instead of class when it is suppoted in the
78   // protocol.
79   case SK::Struct:
80   case SK::Class:
81   case SK::Protocol:
82   case SK::Extension:
83   case SK::Union:
84     return CompletionItemKind::Class;
85   // FIXME(ioeric): figure out whether reference is the right type for aliases.
86   case SK::TypeAlias:
87   case SK::Using:
88     return CompletionItemKind::Reference;
89   case SK::Function:
90   // FIXME(ioeric): this should probably be an operator. This should be fixed
91   // when `Operator` is support type in the protocol.
92   case SK::ConversionFunction:
93     return CompletionItemKind::Function;
94   case SK::Variable:
95   case SK::Parameter:
96     return CompletionItemKind::Variable;
97   case SK::Field:
98     return CompletionItemKind::Field;
99   // FIXME(ioeric): use LSP enum constant when it is supported in the protocol.
100   case SK::EnumConstant:
101     return CompletionItemKind::Value;
102   case SK::InstanceMethod:
103   case SK::ClassMethod:
104   case SK::StaticMethod:
105   case SK::Destructor:
106     return CompletionItemKind::Method;
107   case SK::InstanceProperty:
108   case SK::ClassProperty:
109   case SK::StaticProperty:
110     return CompletionItemKind::Property;
111   case SK::Constructor:
112     return CompletionItemKind::Constructor;
113   }
114   llvm_unreachable("Unhandled clang::index::SymbolKind.");
115 }
116 
117 CompletionItemKind
118 toCompletionItemKind(CodeCompletionResult::ResultKind ResKind,
119                      const NamedDecl *Decl,
120                      CodeCompletionContext::Kind CtxKind) {
121   if (Decl)
122     return toCompletionItemKind(index::getSymbolInfo(Decl).Kind);
123   if (CtxKind == CodeCompletionContext::CCC_IncludedFile)
124     return CompletionItemKind::File;
125   switch (ResKind) {
126   case CodeCompletionResult::RK_Declaration:
127     llvm_unreachable("RK_Declaration without Decl");
128   case CodeCompletionResult::RK_Keyword:
129     return CompletionItemKind::Keyword;
130   case CodeCompletionResult::RK_Macro:
131     return CompletionItemKind::Text; // unfortunately, there's no 'Macro'
132                                      // completion items in LSP.
133   case CodeCompletionResult::RK_Pattern:
134     return CompletionItemKind::Snippet;
135   }
136   llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
137 }
138 
139 /// Get the optional chunk as a string. This function is possibly recursive.
140 ///
141 /// The parameter info for each parameter is appended to the Parameters.
142 std::string getOptionalParameters(const CodeCompletionString &CCS,
143                                   std::vector<ParameterInformation> &Parameters,
144                                   SignatureQualitySignals &Signal) {
145   std::string Result;
146   for (const auto &Chunk : CCS) {
147     switch (Chunk.Kind) {
148     case CodeCompletionString::CK_Optional:
149       assert(Chunk.Optional &&
150              "Expected the optional code completion string to be non-null.");
151       Result += getOptionalParameters(*Chunk.Optional, Parameters, Signal);
152       break;
153     case CodeCompletionString::CK_VerticalSpace:
154       break;
155     case CodeCompletionString::CK_Placeholder:
156       // A string that acts as a placeholder for, e.g., a function call
157       // argument.
158       // Intentional fallthrough here.
159     case CodeCompletionString::CK_CurrentParameter: {
160       // A piece of text that describes the parameter that corresponds to
161       // the code-completion location within a function call, message send,
162       // macro invocation, etc.
163       Result += Chunk.Text;
164       ParameterInformation Info;
165       Info.label = Chunk.Text;
166       Parameters.push_back(std::move(Info));
167       Signal.ContainsActiveParameter = true;
168       Signal.NumberOfOptionalParameters++;
169       break;
170     }
171     default:
172       Result += Chunk.Text;
173       break;
174     }
175   }
176   return Result;
177 }
178 
179 /// Creates a `HeaderFile` from \p Header which can be either a URI or a literal
180 /// include.
181 static llvm::Expected<HeaderFile> toHeaderFile(StringRef Header,
182                                                llvm::StringRef HintPath) {
183   if (isLiteralInclude(Header))
184     return HeaderFile{Header.str(), /*Verbatim=*/true};
185   auto U = URI::parse(Header);
186   if (!U)
187     return U.takeError();
188 
189   auto IncludePath = URI::includeSpelling(*U);
190   if (!IncludePath)
191     return IncludePath.takeError();
192   if (!IncludePath->empty())
193     return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true};
194 
195   auto Resolved = URI::resolve(*U, HintPath);
196   if (!Resolved)
197     return Resolved.takeError();
198   return HeaderFile{std::move(*Resolved), /*Verbatim=*/false};
199 }
200 
201 // First traverses all method definitions inside current class/struct/union
202 // definition. Than traverses base classes to find virtual methods that haven't
203 // been overriden within current context.
204 // FIXME(kadircet): Currently we cannot see declarations below completion point.
205 // It is because Sema gets run only upto completion point. Need to find a
206 // solution to run it for the whole class/struct/union definition.
207 static std::vector<CodeCompletionResult>
208 getNonOverridenMethodCompletionResults(const DeclContext *DC, Sema *S) {
209   const auto *CR = llvm::dyn_cast<CXXRecordDecl>(DC);
210   // If not inside a class/struct/union return empty.
211   if (!CR)
212     return {};
213   // First store overrides within current class.
214   // These are stored by name to make querying fast in the later step.
215   llvm::StringMap<std::vector<FunctionDecl *>> Overrides;
216   for (auto *Method : CR->methods()) {
217     if (!Method->isVirtual() || !Method->getIdentifier())
218       continue;
219     Overrides[Method->getName()].push_back(Method);
220   }
221 
222   std::vector<CodeCompletionResult> Results;
223   for (const auto &Base : CR->bases()) {
224     const auto *BR = Base.getType().getTypePtr()->getAsCXXRecordDecl();
225     if (!BR)
226       continue;
227     for (auto *Method : BR->methods()) {
228       if (!Method->isVirtual() || !Method->getIdentifier())
229         continue;
230       const auto it = Overrides.find(Method->getName());
231       bool IsOverriden = false;
232       if (it != Overrides.end()) {
233         for (auto *MD : it->second) {
234           // If the method in current body is not an overload of this virtual
235           // function, then it overrides this one.
236           if (!S->IsOverload(MD, Method, false)) {
237             IsOverriden = true;
238             break;
239           }
240         }
241       }
242       if (!IsOverriden)
243         Results.emplace_back(Method, 0);
244     }
245   }
246 
247   return Results;
248 }
249 
250 /// A code completion result, in clang-native form.
251 /// It may be promoted to a CompletionItem if it's among the top-ranked results.
252 struct CompletionCandidate {
253   llvm::StringRef Name; // Used for filtering and sorting.
254   // We may have a result from Sema, from the index, or both.
255   const CodeCompletionResult *SemaResult = nullptr;
256   const Symbol *IndexResult = nullptr;
257   llvm::SmallVector<StringRef, 1> RankedIncludeHeaders;
258 
259   // States whether this item is an override suggestion.
260   bool IsOverride = false;
261 
262   // Returns a token identifying the overload set this is part of.
263   // 0 indicates it's not part of any overload set.
264   size_t overloadSet() const {
265     SmallString<256> Scratch;
266     if (IndexResult) {
267       switch (IndexResult->SymInfo.Kind) {
268       case index::SymbolKind::ClassMethod:
269       case index::SymbolKind::InstanceMethod:
270       case index::SymbolKind::StaticMethod:
271         assert(false && "Don't expect members from index in code completion");
272         // fall through
273       case index::SymbolKind::Function:
274         // We can't group overloads together that need different #includes.
275         // This could break #include insertion.
276         return hash_combine(
277             (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch),
278             headerToInsertIfAllowed().getValueOr(""));
279       default:
280         return 0;
281       }
282     }
283     assert(SemaResult);
284     // We need to make sure we're consistent with the IndexResult case!
285     const NamedDecl *D = SemaResult->Declaration;
286     if (!D || !D->isFunctionOrFunctionTemplate())
287       return 0;
288     {
289       llvm::raw_svector_ostream OS(Scratch);
290       D->printQualifiedName(OS);
291     }
292     return hash_combine(Scratch, headerToInsertIfAllowed().getValueOr(""));
293   }
294 
295   // The best header to include if include insertion is allowed.
296   llvm::Optional<llvm::StringRef> headerToInsertIfAllowed() const {
297     if (RankedIncludeHeaders.empty())
298       return llvm::None;
299     if (SemaResult && SemaResult->Declaration) {
300       // Avoid inserting new #include if the declaration is found in the current
301       // file e.g. the symbol is forward declared.
302       auto &SM = SemaResult->Declaration->getASTContext().getSourceManager();
303       for (const Decl *RD : SemaResult->Declaration->redecls())
304         if (SM.isInMainFile(SM.getExpansionLoc(RD->getBeginLoc())))
305           return llvm::None;
306     }
307     return RankedIncludeHeaders[0];
308   }
309 
310   using Bundle = llvm::SmallVector<CompletionCandidate, 4>;
311 };
312 using ScoredBundle =
313     std::pair<CompletionCandidate::Bundle, CodeCompletion::Scores>;
314 struct ScoredBundleGreater {
315   bool operator()(const ScoredBundle &L, const ScoredBundle &R) {
316     if (L.second.Total != R.second.Total)
317       return L.second.Total > R.second.Total;
318     return L.first.front().Name <
319            R.first.front().Name; // Earlier name is better.
320   }
321 };
322 
323 // Assembles a code completion out of a bundle of >=1 completion candidates.
324 // Many of the expensive strings are only computed at this point, once we know
325 // the candidate bundle is going to be returned.
326 //
327 // Many fields are the same for all candidates in a bundle (e.g. name), and are
328 // computed from the first candidate, in the constructor.
329 // Others vary per candidate, so add() must be called for remaining candidates.
330 struct CodeCompletionBuilder {
331   CodeCompletionBuilder(ASTContext &ASTCtx, const CompletionCandidate &C,
332                         CodeCompletionString *SemaCCS,
333                         const IncludeInserter &Includes, StringRef FileName,
334                         CodeCompletionContext::Kind ContextKind,
335                         const CodeCompleteOptions &Opts)
336       : ASTCtx(ASTCtx), ExtractDocumentation(Opts.IncludeComments),
337         EnableFunctionArgSnippets(Opts.EnableFunctionArgSnippets) {
338     add(C, SemaCCS);
339     if (C.SemaResult) {
340       Completion.Origin |= SymbolOrigin::AST;
341       Completion.Name = llvm::StringRef(SemaCCS->getTypedText());
342       if (Completion.Scope.empty()) {
343         if ((C.SemaResult->Kind == CodeCompletionResult::RK_Declaration) ||
344             (C.SemaResult->Kind == CodeCompletionResult::RK_Pattern))
345           if (const auto *D = C.SemaResult->getDeclaration())
346             if (const auto *ND = llvm::dyn_cast<NamedDecl>(D))
347               Completion.Scope =
348                   splitQualifiedName(printQualifiedName(*ND)).first;
349       }
350       Completion.Kind = toCompletionItemKind(
351           C.SemaResult->Kind, C.SemaResult->Declaration, ContextKind);
352       for (const auto &FixIt : C.SemaResult->FixIts) {
353         Completion.FixIts.push_back(
354             toTextEdit(FixIt, ASTCtx.getSourceManager(), ASTCtx.getLangOpts()));
355       }
356       std::sort(Completion.FixIts.begin(), Completion.FixIts.end(),
357                 [](const TextEdit &X, const TextEdit &Y) {
358                   return std::tie(X.range.start.line, X.range.start.character) <
359                          std::tie(Y.range.start.line, Y.range.start.character);
360                 });
361       Completion.Deprecated |=
362           (C.SemaResult->Availability == CXAvailability_Deprecated);
363     }
364     if (C.IndexResult) {
365       Completion.Origin |= C.IndexResult->Origin;
366       if (Completion.Scope.empty())
367         Completion.Scope = C.IndexResult->Scope;
368       if (Completion.Kind == CompletionItemKind::Missing)
369         Completion.Kind = toCompletionItemKind(C.IndexResult->SymInfo.Kind);
370       if (Completion.Name.empty())
371         Completion.Name = C.IndexResult->Name;
372       Completion.Deprecated |= (C.IndexResult->Flags & Symbol::Deprecated);
373     }
374 
375     // Turn absolute path into a literal string that can be #included.
376     auto Inserted =
377         [&](StringRef Header) -> Expected<std::pair<std::string, bool>> {
378       auto ResolvedDeclaring =
379           toHeaderFile(C.IndexResult->CanonicalDeclaration.FileURI, FileName);
380       if (!ResolvedDeclaring)
381         return ResolvedDeclaring.takeError();
382       auto ResolvedInserted = toHeaderFile(Header, FileName);
383       if (!ResolvedInserted)
384         return ResolvedInserted.takeError();
385       return std::make_pair(
386           Includes.calculateIncludePath(*ResolvedDeclaring, *ResolvedInserted),
387           Includes.shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
388     };
389     bool ShouldInsert = C.headerToInsertIfAllowed().hasValue();
390     // Calculate include paths and edits for all possible headers.
391     for (const auto &Inc : C.RankedIncludeHeaders) {
392       if (auto ToInclude = Inserted(Inc)) {
393         CodeCompletion::IncludeCandidate Include;
394         Include.Header = ToInclude->first;
395         if (ToInclude->second && ShouldInsert)
396           Include.Insertion = Includes.insert(ToInclude->first);
397         Completion.Includes.push_back(std::move(Include));
398       } else
399         log("Failed to generate include insertion edits for adding header "
400             "(FileURI='{0}', IncludeHeader='{1}') into {2}",
401             C.IndexResult->CanonicalDeclaration.FileURI, Inc, FileName);
402     }
403     // Prefer includes that do not need edits (i.e. already exist).
404     std::stable_partition(Completion.Includes.begin(),
405                           Completion.Includes.end(),
406                           [](const CodeCompletion::IncludeCandidate &I) {
407                             return !I.Insertion.hasValue();
408                           });
409   }
410 
411   void add(const CompletionCandidate &C, CodeCompletionString *SemaCCS) {
412     assert(bool(C.SemaResult) == bool(SemaCCS));
413     Bundled.emplace_back();
414     BundledEntry &S = Bundled.back();
415     if (C.SemaResult) {
416       getSignature(*SemaCCS, &S.Signature, &S.SnippetSuffix,
417                    &Completion.RequiredQualifier);
418       S.ReturnType = getReturnType(*SemaCCS);
419     } else if (C.IndexResult) {
420       S.Signature = C.IndexResult->Signature;
421       S.SnippetSuffix = C.IndexResult->CompletionSnippetSuffix;
422       S.ReturnType = C.IndexResult->ReturnType;
423     }
424     if (ExtractDocumentation && Completion.Documentation.empty()) {
425       if (C.IndexResult)
426         Completion.Documentation = C.IndexResult->Documentation;
427       else if (C.SemaResult)
428         Completion.Documentation = getDocComment(ASTCtx, *C.SemaResult,
429                                                  /*CommentsFromHeader=*/false);
430     }
431     if (C.IsOverride)
432       S.OverrideSuffix = true;
433   }
434 
435   CodeCompletion build() {
436     Completion.ReturnType = summarizeReturnType();
437     Completion.Signature = summarizeSignature();
438     Completion.SnippetSuffix = summarizeSnippet();
439     Completion.BundleSize = Bundled.size();
440     if (summarizeOverride()) {
441       Completion.Name = Completion.ReturnType + ' ' +
442                         std::move(Completion.Name) +
443                         std::move(Completion.Signature) + " override";
444       Completion.Signature.clear();
445     }
446     return std::move(Completion);
447   }
448 
449 private:
450   struct BundledEntry {
451     std::string SnippetSuffix;
452     std::string Signature;
453     std::string ReturnType;
454     bool OverrideSuffix;
455   };
456 
457   // If all BundledEntrys have the same value for a property, return it.
458   template <std::string BundledEntry::*Member>
459   const std::string *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   template <bool BundledEntry::*Member> const bool *onlyValue() const {
468     auto B = Bundled.begin(), E = Bundled.end();
469     for (auto I = B + 1; I != E; ++I)
470       if (I->*Member != B->*Member)
471         return nullptr;
472     return &(B->*Member);
473   }
474 
475   std::string summarizeReturnType() const {
476     if (auto *RT = onlyValue<&BundledEntry::ReturnType>())
477       return *RT;
478     return "";
479   }
480 
481   std::string summarizeSnippet() const {
482     auto *Snippet = onlyValue<&BundledEntry::SnippetSuffix>();
483     if (!Snippet)
484       // All bundles are function calls.
485       return "($0)";
486     if (!Snippet->empty() && !EnableFunctionArgSnippets &&
487         ((Completion.Kind == CompletionItemKind::Function) ||
488          (Completion.Kind == CompletionItemKind::Method)) &&
489         (Snippet->front() == '(') && (Snippet->back() == ')'))
490       // Check whether function has any parameters or not.
491       return Snippet->size() > 2 ? "($0)" : "()";
492     return *Snippet;
493   }
494 
495   std::string summarizeSignature() const {
496     if (auto *Signature = onlyValue<&BundledEntry::Signature>())
497       return *Signature;
498     // All bundles are function calls.
499     return "(…)";
500   }
501 
502   bool summarizeOverride() const {
503     if (auto *OverrideSuffix = onlyValue<&BundledEntry::OverrideSuffix>())
504       return *OverrideSuffix;
505     return false;
506   }
507 
508   ASTContext &ASTCtx;
509   CodeCompletion Completion;
510   SmallVector<BundledEntry, 1> Bundled;
511   bool ExtractDocumentation;
512   bool EnableFunctionArgSnippets;
513 };
514 
515 // Determine the symbol ID for a Sema code completion result, if possible.
516 llvm::Optional<SymbolID> getSymbolID(const CodeCompletionResult &R,
517                                      const SourceManager &SM) {
518   switch (R.Kind) {
519   case CodeCompletionResult::RK_Declaration:
520   case CodeCompletionResult::RK_Pattern: {
521     return clang::clangd::getSymbolID(R.Declaration);
522   }
523   case CodeCompletionResult::RK_Macro:
524     return clang::clangd::getSymbolID(*R.Macro, R.MacroDefInfo, SM);
525   case CodeCompletionResult::RK_Keyword:
526     return None;
527   }
528   llvm_unreachable("unknown CodeCompletionResult kind");
529 }
530 
531 // Scopes of the paritial identifier we're trying to complete.
532 // It is used when we query the index for more completion results.
533 struct SpecifiedScope {
534   // The scopes we should look in, determined by Sema.
535   //
536   // If the qualifier was fully resolved, we look for completions in these
537   // scopes; if there is an unresolved part of the qualifier, it should be
538   // resolved within these scopes.
539   //
540   // Examples of qualified completion:
541   //
542   //   "::vec"                                      => {""}
543   //   "using namespace std; ::vec^"                => {"", "std::"}
544   //   "namespace ns {using namespace std;} ns::^"  => {"ns::", "std::"}
545   //   "std::vec^"                                  => {""}  // "std" unresolved
546   //
547   // Examples of unqualified completion:
548   //
549   //   "vec^"                                       => {""}
550   //   "using namespace std; vec^"                  => {"", "std::"}
551   //   "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""}
552   //
553   // "" for global namespace, "ns::" for normal namespace.
554   std::vector<std::string> AccessibleScopes;
555   // The full scope qualifier as typed by the user (without the leading "::").
556   // Set if the qualifier is not fully resolved by Sema.
557   llvm::Optional<std::string> UnresolvedQualifier;
558 
559   // Construct scopes being queried in indexes.
560   // This method format the scopes to match the index request representation.
561   std::vector<std::string> scopesForIndexQuery() {
562     std::vector<std::string> Results;
563     for (llvm::StringRef AS : AccessibleScopes) {
564       Results.push_back(AS);
565       if (UnresolvedQualifier)
566         Results.back() += *UnresolvedQualifier;
567     }
568     return Results;
569   }
570 };
571 
572 // Get all scopes that will be queried in indexes.
573 std::vector<std::string> getQueryScopes(CodeCompletionContext &CCContext,
574                                         const SourceManager &SM) {
575   auto GetAllAccessibleScopes = [](CodeCompletionContext &CCContext) {
576     SpecifiedScope Info;
577     for (auto *Context : CCContext.getVisitedContexts()) {
578       if (isa<TranslationUnitDecl>(Context))
579         Info.AccessibleScopes.push_back(""); // global namespace
580       else if (const auto *NS = dyn_cast<NamespaceDecl>(Context))
581         Info.AccessibleScopes.push_back(NS->getQualifiedNameAsString() + "::");
582     }
583     return Info;
584   };
585 
586   auto SS = CCContext.getCXXScopeSpecifier();
587 
588   // Unqualified completion (e.g. "vec^").
589   if (!SS) {
590     // FIXME: Once we can insert namespace qualifiers and use the in-scope
591     //        namespaces for scoring, search in all namespaces.
592     // FIXME: Capture scopes and use for scoring, for example,
593     //        "using namespace std; namespace foo {v^}" =>
594     //        foo::value > std::vector > boost::variant
595     return GetAllAccessibleScopes(CCContext).scopesForIndexQuery();
596   }
597 
598   // Qualified completion ("std::vec^"), we have two cases depending on whether
599   // the qualifier can be resolved by Sema.
600   if ((*SS)->isValid()) { // Resolved qualifier.
601     return GetAllAccessibleScopes(CCContext).scopesForIndexQuery();
602   }
603 
604   // Unresolved qualifier.
605   // FIXME: When Sema can resolve part of a scope chain (e.g.
606   // "known::unknown::id"), we should expand the known part ("known::") rather
607   // than treating the whole thing as unknown.
608   SpecifiedScope Info;
609   Info.AccessibleScopes.push_back(""); // global namespace
610 
611   Info.UnresolvedQualifier =
612       Lexer::getSourceText(CharSourceRange::getCharRange((*SS)->getRange()), SM,
613                            clang::LangOptions())
614           .ltrim("::");
615   // Sema excludes the trailing "::".
616   if (!Info.UnresolvedQualifier->empty())
617     *Info.UnresolvedQualifier += "::";
618 
619   return Info.scopesForIndexQuery();
620 }
621 
622 // Should we perform index-based completion in a context of the specified kind?
623 // FIXME: consider allowing completion, but restricting the result types.
624 bool contextAllowsIndex(enum CodeCompletionContext::Kind K) {
625   switch (K) {
626   case CodeCompletionContext::CCC_TopLevel:
627   case CodeCompletionContext::CCC_ObjCInterface:
628   case CodeCompletionContext::CCC_ObjCImplementation:
629   case CodeCompletionContext::CCC_ObjCIvarList:
630   case CodeCompletionContext::CCC_ClassStructUnion:
631   case CodeCompletionContext::CCC_Statement:
632   case CodeCompletionContext::CCC_Expression:
633   case CodeCompletionContext::CCC_ObjCMessageReceiver:
634   case CodeCompletionContext::CCC_EnumTag:
635   case CodeCompletionContext::CCC_UnionTag:
636   case CodeCompletionContext::CCC_ClassOrStructTag:
637   case CodeCompletionContext::CCC_ObjCProtocolName:
638   case CodeCompletionContext::CCC_Namespace:
639   case CodeCompletionContext::CCC_Type:
640   case CodeCompletionContext::CCC_Name: // FIXME: why does ns::^ give this?
641   case CodeCompletionContext::CCC_PotentiallyQualifiedName:
642   case CodeCompletionContext::CCC_ParenthesizedExpression:
643   case CodeCompletionContext::CCC_ObjCInterfaceName:
644   case CodeCompletionContext::CCC_ObjCCategoryName:
645     return true;
646   case CodeCompletionContext::CCC_Other: // Be conservative.
647   case CodeCompletionContext::CCC_OtherWithMacros:
648   case CodeCompletionContext::CCC_DotMemberAccess:
649   case CodeCompletionContext::CCC_ArrowMemberAccess:
650   case CodeCompletionContext::CCC_ObjCPropertyAccess:
651   case CodeCompletionContext::CCC_MacroName:
652   case CodeCompletionContext::CCC_MacroNameUse:
653   case CodeCompletionContext::CCC_PreprocessorExpression:
654   case CodeCompletionContext::CCC_PreprocessorDirective:
655   case CodeCompletionContext::CCC_NaturalLanguage:
656   case CodeCompletionContext::CCC_SelectorName:
657   case CodeCompletionContext::CCC_TypeQualifiers:
658   case CodeCompletionContext::CCC_ObjCInstanceMessage:
659   case CodeCompletionContext::CCC_ObjCClassMessage:
660   case CodeCompletionContext::CCC_IncludedFile:
661   case CodeCompletionContext::CCC_Recovery:
662     return false;
663   }
664   llvm_unreachable("unknown code completion context");
665 }
666 
667 // Some member calls are blacklisted because they're so rarely useful.
668 static bool isBlacklistedMember(const NamedDecl &D) {
669   // Destructor completion is rarely useful, and works inconsistently.
670   // (s.^ completes ~string, but s.~st^ is an error).
671   if (D.getKind() == Decl::CXXDestructor)
672     return true;
673   // Injected name may be useful for A::foo(), but who writes A::A::foo()?
674   if (auto *R = dyn_cast_or_null<RecordDecl>(&D))
675     if (R->isInjectedClassName())
676       return true;
677   // Explicit calls to operators are also rare.
678   auto NameKind = D.getDeclName().getNameKind();
679   if (NameKind == DeclarationName::CXXOperatorName ||
680       NameKind == DeclarationName::CXXLiteralOperatorName ||
681       NameKind == DeclarationName::CXXConversionFunctionName)
682     return true;
683   return false;
684 }
685 
686 // The CompletionRecorder captures Sema code-complete output, including context.
687 // It filters out ignored results (but doesn't apply fuzzy-filtering yet).
688 // It doesn't do scoring or conversion to CompletionItem yet, as we want to
689 // merge with index results first.
690 // Generally the fields and methods of this object should only be used from
691 // within the callback.
692 struct CompletionRecorder : public CodeCompleteConsumer {
693   CompletionRecorder(const CodeCompleteOptions &Opts,
694                      llvm::unique_function<void()> ResultsCallback)
695       : CodeCompleteConsumer(Opts.getClangCompleteOpts(),
696                              /*OutputIsBinary=*/false),
697         CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
698         CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
699         CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) {
700     assert(this->ResultsCallback);
701   }
702 
703   std::vector<CodeCompletionResult> Results;
704   CodeCompletionContext CCContext;
705   Sema *CCSema = nullptr; // Sema that created the results.
706   // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead?
707 
708   void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context,
709                                   CodeCompletionResult *InResults,
710                                   unsigned NumResults) override final {
711     // Results from recovery mode are generally useless, and the callback after
712     // recovery (if any) is usually more interesting. To make sure we handle the
713     // future callback from sema, we just ignore all callbacks in recovery mode,
714     // as taking only results from recovery mode results in poor completion
715     // results.
716     // FIXME: in case there is no future sema completion callback after the
717     // recovery mode, we might still want to provide some results (e.g. trivial
718     // identifier-based completion).
719     if (Context.getKind() == CodeCompletionContext::CCC_Recovery) {
720       log("Code complete: Ignoring sema code complete callback with Recovery "
721           "context.");
722       return;
723     }
724     // If a callback is called without any sema result and the context does not
725     // support index-based completion, we simply skip it to give way to
726     // potential future callbacks with results.
727     if (NumResults == 0 && !contextAllowsIndex(Context.getKind()))
728       return;
729     if (CCSema) {
730       log("Multiple code complete callbacks (parser backtracked?). "
731           "Dropping results from context {0}, keeping results from {1}.",
732           getCompletionKindString(Context.getKind()),
733           getCompletionKindString(this->CCContext.getKind()));
734       return;
735     }
736     // Record the completion context.
737     CCSema = &S;
738     CCContext = Context;
739 
740     // Retain the results we might want.
741     for (unsigned I = 0; I < NumResults; ++I) {
742       auto &Result = InResults[I];
743       // Drop hidden items which cannot be found by lookup after completion.
744       // Exception: some items can be named by using a qualifier.
745       if (Result.Hidden && (!Result.Qualifier || Result.QualifierIsInformative))
746         continue;
747       if (!Opts.IncludeIneligibleResults &&
748           (Result.Availability == CXAvailability_NotAvailable ||
749            Result.Availability == CXAvailability_NotAccessible))
750         continue;
751       if (Result.Declaration &&
752           !Context.getBaseType().isNull() // is this a member-access context?
753           && isBlacklistedMember(*Result.Declaration))
754         continue;
755       // We choose to never append '::' to completion results in clangd.
756       Result.StartsNestedNameSpecifier = false;
757       Results.push_back(Result);
758     }
759     ResultsCallback();
760   }
761 
762   CodeCompletionAllocator &getAllocator() override { return *CCAllocator; }
763   CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
764 
765   // Returns the filtering/sorting name for Result, which must be from Results.
766   // Returned string is owned by this recorder (or the AST).
767   llvm::StringRef getName(const CodeCompletionResult &Result) {
768     switch (Result.Kind) {
769     case CodeCompletionResult::RK_Declaration:
770       if (auto *ID = Result.Declaration->getIdentifier())
771         return ID->getName();
772       break;
773     case CodeCompletionResult::RK_Keyword:
774       return Result.Keyword;
775     case CodeCompletionResult::RK_Macro:
776       return Result.Macro->getName();
777     case CodeCompletionResult::RK_Pattern:
778       return Result.Pattern->getTypedText();
779     }
780     auto *CCS = codeCompletionString(Result);
781     return CCS->getTypedText();
782   }
783 
784   // Build a CodeCompletion string for R, which must be from Results.
785   // The CCS will be owned by this recorder.
786   CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) {
787     // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway.
788     return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString(
789         *CCSema, CCContext, *CCAllocator, CCTUInfo,
790         /*IncludeBriefComments=*/false);
791   }
792 
793 private:
794   CodeCompleteOptions Opts;
795   std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
796   CodeCompletionTUInfo CCTUInfo;
797   llvm::unique_function<void()> ResultsCallback;
798 };
799 
800 struct ScoredSignature {
801   // When set, requires documentation to be requested from the index with this
802   // ID.
803   llvm::Optional<SymbolID> IDForDoc;
804   SignatureInformation Signature;
805   SignatureQualitySignals Quality;
806 };
807 
808 class SignatureHelpCollector final : public CodeCompleteConsumer {
809 public:
810   SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
811                          const SymbolIndex *Index, SignatureHelp &SigHelp)
812       : CodeCompleteConsumer(CodeCompleteOpts,
813                              /*OutputIsBinary=*/false),
814         SigHelp(SigHelp),
815         Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
816         CCTUInfo(Allocator), Index(Index) {}
817 
818   void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
819                                  OverloadCandidate *Candidates,
820                                  unsigned NumCandidates,
821                                  SourceLocation OpenParLoc) override {
822     assert(!OpenParLoc.isInvalid());
823     SourceManager &SrcMgr = S.getSourceManager();
824     OpenParLoc = SrcMgr.getFileLoc(OpenParLoc);
825     if (SrcMgr.isInMainFile(OpenParLoc))
826       SigHelp.argListStart = sourceLocToPosition(SrcMgr, OpenParLoc);
827     else
828       elog("Location oustide main file in signature help: {0}",
829            OpenParLoc.printToString(SrcMgr));
830 
831     std::vector<ScoredSignature> ScoredSignatures;
832     SigHelp.signatures.reserve(NumCandidates);
833     ScoredSignatures.reserve(NumCandidates);
834     // FIXME(rwols): How can we determine the "active overload candidate"?
835     // Right now the overloaded candidates seem to be provided in a "best fit"
836     // order, so I'm not too worried about this.
837     SigHelp.activeSignature = 0;
838     assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
839            "too many arguments");
840     SigHelp.activeParameter = static_cast<int>(CurrentArg);
841     for (unsigned I = 0; I < NumCandidates; ++I) {
842       OverloadCandidate Candidate = Candidates[I];
843       // We want to avoid showing instantiated signatures, because they may be
844       // long in some cases (e.g. when 'T' is substituted with 'std::string', we
845       // would get 'std::basic_string<char>').
846       if (auto *Func = Candidate.getFunction()) {
847         if (auto *Pattern = Func->getTemplateInstantiationPattern())
848           Candidate = OverloadCandidate(Pattern);
849       }
850 
851       const auto *CCS = Candidate.CreateSignatureString(
852           CurrentArg, S, *Allocator, CCTUInfo, true);
853       assert(CCS && "Expected the CodeCompletionString to be non-null");
854       ScoredSignatures.push_back(processOverloadCandidate(
855           Candidate, *CCS,
856           Candidate.getFunction()
857               ? getDeclComment(S.getASTContext(), *Candidate.getFunction())
858               : ""));
859     }
860 
861     // Sema does not load the docs from the preamble, so we need to fetch extra
862     // docs from the index instead.
863     llvm::DenseMap<SymbolID, std::string> FetchedDocs;
864     if (Index) {
865       LookupRequest IndexRequest;
866       for (const auto &S : ScoredSignatures) {
867         if (!S.IDForDoc)
868           continue;
869         IndexRequest.IDs.insert(*S.IDForDoc);
870       }
871       Index->lookup(IndexRequest, [&](const Symbol &S) {
872         if (!S.Documentation.empty())
873           FetchedDocs[S.ID] = S.Documentation;
874       });
875       log("SigHelp: requested docs for {0} symbols from the index, got {1} "
876           "symbols with non-empty docs in the response",
877           IndexRequest.IDs.size(), FetchedDocs.size());
878     }
879 
880     std::sort(
881         ScoredSignatures.begin(), ScoredSignatures.end(),
882         [](const ScoredSignature &L, const ScoredSignature &R) {
883           // Ordering follows:
884           // - Less number of parameters is better.
885           // - Function is better than FunctionType which is better than
886           // Function Template.
887           // - High score is better.
888           // - Shorter signature is better.
889           // - Alphebatically smaller is better.
890           if (L.Quality.NumberOfParameters != R.Quality.NumberOfParameters)
891             return L.Quality.NumberOfParameters < R.Quality.NumberOfParameters;
892           if (L.Quality.NumberOfOptionalParameters !=
893               R.Quality.NumberOfOptionalParameters)
894             return L.Quality.NumberOfOptionalParameters <
895                    R.Quality.NumberOfOptionalParameters;
896           if (L.Quality.Kind != R.Quality.Kind) {
897             using OC = CodeCompleteConsumer::OverloadCandidate;
898             switch (L.Quality.Kind) {
899             case OC::CK_Function:
900               return true;
901             case OC::CK_FunctionType:
902               return R.Quality.Kind != OC::CK_Function;
903             case OC::CK_FunctionTemplate:
904               return false;
905             }
906             llvm_unreachable("Unknown overload candidate type.");
907           }
908           if (L.Signature.label.size() != R.Signature.label.size())
909             return L.Signature.label.size() < R.Signature.label.size();
910           return L.Signature.label < R.Signature.label;
911         });
912 
913     for (auto &SS : ScoredSignatures) {
914       auto IndexDocIt =
915           SS.IDForDoc ? FetchedDocs.find(*SS.IDForDoc) : FetchedDocs.end();
916       if (IndexDocIt != FetchedDocs.end())
917         SS.Signature.documentation = IndexDocIt->second;
918 
919       SigHelp.signatures.push_back(std::move(SS.Signature));
920     }
921   }
922 
923   GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
924 
925   CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
926 
927 private:
928   // FIXME(ioeric): consider moving CodeCompletionString logic here to
929   // CompletionString.h.
930   ScoredSignature processOverloadCandidate(const OverloadCandidate &Candidate,
931                                            const CodeCompletionString &CCS,
932                                            llvm::StringRef DocComment) const {
933     SignatureInformation Signature;
934     SignatureQualitySignals Signal;
935     const char *ReturnType = nullptr;
936 
937     Signature.documentation = formatDocumentation(CCS, DocComment);
938     Signal.Kind = Candidate.getKind();
939 
940     for (const auto &Chunk : CCS) {
941       switch (Chunk.Kind) {
942       case CodeCompletionString::CK_ResultType:
943         // A piece of text that describes the type of an entity or,
944         // for functions and methods, the return type.
945         assert(!ReturnType && "Unexpected CK_ResultType");
946         ReturnType = Chunk.Text;
947         break;
948       case CodeCompletionString::CK_Placeholder:
949         // A string that acts as a placeholder for, e.g., a function call
950         // argument.
951         // Intentional fallthrough here.
952       case CodeCompletionString::CK_CurrentParameter: {
953         // A piece of text that describes the parameter that corresponds to
954         // the code-completion location within a function call, message send,
955         // macro invocation, etc.
956         Signature.label += Chunk.Text;
957         ParameterInformation Info;
958         Info.label = Chunk.Text;
959         Signature.parameters.push_back(std::move(Info));
960         Signal.NumberOfParameters++;
961         Signal.ContainsActiveParameter = true;
962         break;
963       }
964       case CodeCompletionString::CK_Optional: {
965         // The rest of the parameters are defaulted/optional.
966         assert(Chunk.Optional &&
967                "Expected the optional code completion string to be non-null.");
968         Signature.label += getOptionalParameters(*Chunk.Optional,
969                                                  Signature.parameters, Signal);
970         break;
971       }
972       case CodeCompletionString::CK_VerticalSpace:
973         break;
974       default:
975         Signature.label += Chunk.Text;
976         break;
977       }
978     }
979     if (ReturnType) {
980       Signature.label += " -> ";
981       Signature.label += ReturnType;
982     }
983     dlog("Signal for {0}: {1}", Signature, Signal);
984     ScoredSignature Result;
985     Result.Signature = std::move(Signature);
986     Result.Quality = Signal;
987     Result.IDForDoc =
988         Result.Signature.documentation.empty() && Candidate.getFunction()
989             ? clangd::getSymbolID(Candidate.getFunction())
990             : llvm::None;
991     return Result;
992   }
993 
994   SignatureHelp &SigHelp;
995   std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
996   CodeCompletionTUInfo CCTUInfo;
997   const SymbolIndex *Index;
998 }; // SignatureHelpCollector
999 
1000 struct SemaCompleteInput {
1001   PathRef FileName;
1002   const tooling::CompileCommand &Command;
1003   PrecompiledPreamble const *Preamble;
1004   StringRef Contents;
1005   Position Pos;
1006   IntrusiveRefCntPtr<vfs::FileSystem> VFS;
1007   std::shared_ptr<PCHContainerOperations> PCHs;
1008 };
1009 
1010 // Invokes Sema code completion on a file.
1011 // If \p Includes is set, it will be updated based on the compiler invocation.
1012 bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
1013                       const clang::CodeCompleteOptions &Options,
1014                       const SemaCompleteInput &Input,
1015                       IncludeStructure *Includes = nullptr) {
1016   trace::Span Tracer("Sema completion");
1017   std::vector<const char *> ArgStrs;
1018   for (const auto &S : Input.Command.CommandLine)
1019     ArgStrs.push_back(S.c_str());
1020 
1021   if (Input.VFS->setCurrentWorkingDirectory(Input.Command.Directory)) {
1022     log("Couldn't set working directory");
1023     // We run parsing anyway, our lit-tests rely on results for non-existing
1024     // working dirs.
1025   }
1026 
1027   IgnoreDiagnostics DummyDiagsConsumer;
1028   auto CI = createInvocationFromCommandLine(
1029       ArgStrs,
1030       CompilerInstance::createDiagnostics(new DiagnosticOptions,
1031                                           &DummyDiagsConsumer, false),
1032       Input.VFS);
1033   if (!CI) {
1034     elog("Couldn't create CompilerInvocation");
1035     return false;
1036   }
1037   auto &FrontendOpts = CI->getFrontendOpts();
1038   FrontendOpts.DisableFree = false;
1039   FrontendOpts.SkipFunctionBodies = true;
1040   CI->getLangOpts()->CommentOpts.ParseAllComments = true;
1041   // Disable typo correction in Sema.
1042   CI->getLangOpts()->SpellChecking = false;
1043   // Setup code completion.
1044   FrontendOpts.CodeCompleteOpts = Options;
1045   FrontendOpts.CodeCompletionAt.FileName = Input.FileName;
1046   auto Offset = positionToOffset(Input.Contents, Input.Pos);
1047   if (!Offset) {
1048     elog("Code completion position was invalid {0}", Offset.takeError());
1049     return false;
1050   }
1051   std::tie(FrontendOpts.CodeCompletionAt.Line,
1052            FrontendOpts.CodeCompletionAt.Column) =
1053       offsetToClangLineColumn(Input.Contents, *Offset);
1054 
1055   std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1056       llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName);
1057   // The diagnostic options must be set before creating a CompilerInstance.
1058   CI->getDiagnosticOpts().IgnoreWarnings = true;
1059   // We reuse the preamble whether it's valid or not. This is a
1060   // correctness/performance tradeoff: building without a preamble is slow, and
1061   // completion is latency-sensitive.
1062   // However, if we're completing *inside* the preamble section of the draft,
1063   // overriding the preamble will break sema completion. Fortunately we can just
1064   // skip all includes in this case; these completions are really simple.
1065   bool CompletingInPreamble =
1066       ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0).Size >
1067       *Offset;
1068   // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
1069   // the remapped buffers do not get freed.
1070   auto Clang = prepareCompilerInstance(
1071       std::move(CI), CompletingInPreamble ? nullptr : Input.Preamble,
1072       std::move(ContentsBuffer), std::move(Input.PCHs), std::move(Input.VFS),
1073       DummyDiagsConsumer);
1074   Clang->getPreprocessorOpts().SingleFileParseMode = CompletingInPreamble;
1075   Clang->setCodeCompletionConsumer(Consumer.release());
1076 
1077   SyntaxOnlyAction Action;
1078   if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
1079     log("BeginSourceFile() failed when running codeComplete for {0}",
1080         Input.FileName);
1081     return false;
1082   }
1083   if (Includes)
1084     Clang->getPreprocessor().addPPCallbacks(
1085         collectIncludeStructureCallback(Clang->getSourceManager(), Includes));
1086   if (!Action.Execute()) {
1087     log("Execute() failed when running codeComplete for {0}", Input.FileName);
1088     return false;
1089   }
1090   Action.EndSourceFile();
1091 
1092   return true;
1093 }
1094 
1095 // Should we allow index completions in the specified context?
1096 bool allowIndex(CodeCompletionContext &CC) {
1097   if (!contextAllowsIndex(CC.getKind()))
1098     return false;
1099   // We also avoid ClassName::bar (but allow namespace::bar).
1100   auto Scope = CC.getCXXScopeSpecifier();
1101   if (!Scope)
1102     return true;
1103   NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
1104   if (!NameSpec)
1105     return true;
1106   // We only query the index when qualifier is a namespace.
1107   // If it's a class, we rely solely on sema completions.
1108   switch (NameSpec->getKind()) {
1109   case NestedNameSpecifier::Global:
1110   case NestedNameSpecifier::Namespace:
1111   case NestedNameSpecifier::NamespaceAlias:
1112     return true;
1113   case NestedNameSpecifier::Super:
1114   case NestedNameSpecifier::TypeSpec:
1115   case NestedNameSpecifier::TypeSpecWithTemplate:
1116   // Unresolved inside a template.
1117   case NestedNameSpecifier::Identifier:
1118     return false;
1119   }
1120   llvm_unreachable("invalid NestedNameSpecifier kind");
1121 }
1122 
1123 std::future<SymbolSlab> startAsyncFuzzyFind(const SymbolIndex &Index,
1124                                             const FuzzyFindRequest &Req) {
1125   return runAsync<SymbolSlab>([&Index, Req]() {
1126     trace::Span Tracer("Async fuzzyFind");
1127     SymbolSlab::Builder Syms;
1128     Index.fuzzyFind(Req, [&Syms](const Symbol &Sym) { Syms.insert(Sym); });
1129     return std::move(Syms).build();
1130   });
1131 }
1132 
1133 // Creates a `FuzzyFindRequest` based on the cached index request from the
1134 // last completion, if any, and the speculated completion filter text in the
1135 // source code.
1136 llvm::Optional<FuzzyFindRequest> speculativeFuzzyFindRequestForCompletion(
1137     FuzzyFindRequest CachedReq, PathRef File, StringRef Content, Position Pos) {
1138   auto Filter = speculateCompletionFilter(Content, Pos);
1139   if (!Filter) {
1140     elog("Failed to speculate filter text for code completion at Pos "
1141          "{0}:{1}: {2}",
1142          Pos.line, Pos.character, Filter.takeError());
1143     return llvm::None;
1144   }
1145   CachedReq.Query = *Filter;
1146   return CachedReq;
1147 }
1148 
1149 } // namespace
1150 
1151 clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const {
1152   clang::CodeCompleteOptions Result;
1153   Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns;
1154   Result.IncludeMacros = IncludeMacros;
1155   Result.IncludeGlobals = true;
1156   // We choose to include full comments and not do doxygen parsing in
1157   // completion.
1158   // FIXME: ideally, we should support doxygen in some form, e.g. do markdown
1159   // formatting of the comments.
1160   Result.IncludeBriefComments = false;
1161 
1162   // When an is used, Sema is responsible for completing the main file,
1163   // the index can provide results from the preamble.
1164   // Tell Sema not to deserialize the preamble to look for results.
1165   Result.LoadExternal = !Index;
1166   Result.IncludeFixIts = IncludeFixIts;
1167 
1168   return Result;
1169 }
1170 
1171 // Returns the most popular include header for \p Sym. If two headers are
1172 // equally popular, prefer the shorter one. Returns empty string if \p Sym has
1173 // no include header.
1174 llvm::SmallVector<StringRef, 1>
1175 getRankedIncludes(const Symbol &Sym) {
1176   auto Includes = Sym.IncludeHeaders;
1177   // Sort in descending order by reference count and header length.
1178   std::sort(Includes.begin(), Includes.end(),
1179             [](const Symbol::IncludeHeaderWithReferences &LHS,
1180                const Symbol::IncludeHeaderWithReferences &RHS) {
1181               if (LHS.References == RHS.References)
1182                 return LHS.IncludeHeader.size() < RHS.IncludeHeader.size();
1183               return LHS.References > RHS.References;
1184             });
1185   llvm::SmallVector<StringRef, 1> Headers;
1186   for (const auto &Include : Includes)
1187     Headers.push_back(Include.IncludeHeader);
1188   return Headers;
1189 }
1190 
1191 // Runs Sema-based (AST) and Index-based completion, returns merged results.
1192 //
1193 // There are a few tricky considerations:
1194 //   - the AST provides information needed for the index query (e.g. which
1195 //     namespaces to search in). So Sema must start first.
1196 //   - we only want to return the top results (Opts.Limit).
1197 //     Building CompletionItems for everything else is wasteful, so we want to
1198 //     preserve the "native" format until we're done with scoring.
1199 //   - the data underlying Sema completion items is owned by the AST and various
1200 //     other arenas, which must stay alive for us to build CompletionItems.
1201 //   - we may get duplicate results from Sema and the Index, we need to merge.
1202 //
1203 // So we start Sema completion first, and do all our work in its callback.
1204 // We use the Sema context information to query the index.
1205 // Then we merge the two result sets, producing items that are Sema/Index/Both.
1206 // These items are scored, and the top N are synthesized into the LSP response.
1207 // Finally, we can clean up the data structures created by Sema completion.
1208 //
1209 // Main collaborators are:
1210 //   - semaCodeComplete sets up the compiler machinery to run code completion.
1211 //   - CompletionRecorder captures Sema completion results, including context.
1212 //   - SymbolIndex (Opts.Index) provides index completion results as Symbols
1213 //   - CompletionCandidates are the result of merging Sema and Index results.
1214 //     Each candidate points to an underlying CodeCompletionResult (Sema), a
1215 //     Symbol (Index), or both. It computes the result quality score.
1216 //     CompletionCandidate also does conversion to CompletionItem (at the end).
1217 //   - FuzzyMatcher scores how the candidate matches the partial identifier.
1218 //     This score is combined with the result quality score for the final score.
1219 //   - TopN determines the results with the best score.
1220 class CodeCompleteFlow {
1221   PathRef FileName;
1222   IncludeStructure Includes; // Complete once the compiler runs.
1223   SpeculativeFuzzyFind *SpecFuzzyFind; // Can be nullptr.
1224   const CodeCompleteOptions &Opts;
1225 
1226   // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup.
1227   CompletionRecorder *Recorder = nullptr;
1228   int NSema = 0, NIndex = 0, NBoth = 0; // Counters for logging.
1229   bool Incomplete = false; // Would more be available with a higher limit?
1230   llvm::Optional<FuzzyMatcher> Filter;       // Initialized once Sema runs.
1231   std::vector<std::string> QueryScopes;      // Initialized once Sema runs.
1232   // Include-insertion and proximity scoring rely on the include structure.
1233   // This is available after Sema has run.
1234   llvm::Optional<IncludeInserter> Inserter;  // Available during runWithSema.
1235   llvm::Optional<URIDistance> FileProximity; // Initialized once Sema runs.
1236   /// Speculative request based on the cached request and the filter text before
1237   /// the cursor.
1238   /// Initialized right before sema run. This is only set if `SpecFuzzyFind` is
1239   /// set and contains a cached request.
1240   llvm::Optional<FuzzyFindRequest> SpecReq;
1241 
1242 public:
1243   // A CodeCompleteFlow object is only useful for calling run() exactly once.
1244   CodeCompleteFlow(PathRef FileName, const IncludeStructure &Includes,
1245                    SpeculativeFuzzyFind *SpecFuzzyFind,
1246                    const CodeCompleteOptions &Opts)
1247       : FileName(FileName), Includes(Includes), SpecFuzzyFind(SpecFuzzyFind),
1248         Opts(Opts) {}
1249 
1250   CodeCompleteResult run(const SemaCompleteInput &SemaCCInput) && {
1251     trace::Span Tracer("CodeCompleteFlow");
1252     if (Opts.Index && SpecFuzzyFind && SpecFuzzyFind->CachedReq.hasValue()) {
1253       assert(!SpecFuzzyFind->Result.valid());
1254       if ((SpecReq = speculativeFuzzyFindRequestForCompletion(
1255                *SpecFuzzyFind->CachedReq, SemaCCInput.FileName,
1256                SemaCCInput.Contents, SemaCCInput.Pos)))
1257         SpecFuzzyFind->Result = startAsyncFuzzyFind(*Opts.Index, *SpecReq);
1258     }
1259 
1260     // We run Sema code completion first. It builds an AST and calculates:
1261     //   - completion results based on the AST.
1262     //   - partial identifier and context. We need these for the index query.
1263     CodeCompleteResult Output;
1264     auto RecorderOwner = llvm::make_unique<CompletionRecorder>(Opts, [&]() {
1265       assert(Recorder && "Recorder is not set");
1266       auto Style =
1267           format::getStyle(format::DefaultFormatStyle, SemaCCInput.FileName,
1268                            format::DefaultFallbackStyle, SemaCCInput.Contents,
1269                            SemaCCInput.VFS.get());
1270       if (!Style) {
1271         log("getStyle() failed for file {0}: {1}. Fallback is LLVM style.",
1272             SemaCCInput.FileName, Style.takeError());
1273         Style = format::getLLVMStyle();
1274       }
1275       // If preprocessor was run, inclusions from preprocessor callback should
1276       // already be added to Includes.
1277       Inserter.emplace(
1278           SemaCCInput.FileName, SemaCCInput.Contents, *Style,
1279           SemaCCInput.Command.Directory,
1280           Recorder->CCSema->getPreprocessor().getHeaderSearchInfo());
1281       for (const auto &Inc : Includes.MainFileIncludes)
1282         Inserter->addExisting(Inc);
1283 
1284       // Most of the cost of file proximity is in initializing the FileDistance
1285       // structures based on the observed includes, once per query. Conceptually
1286       // that happens here (though the per-URI-scheme initialization is lazy).
1287       // The per-result proximity scoring is (amortized) very cheap.
1288       FileDistanceOptions ProxOpts{}; // Use defaults.
1289       const auto &SM = Recorder->CCSema->getSourceManager();
1290       llvm::StringMap<SourceParams> ProxSources;
1291       for (auto &Entry : Includes.includeDepth(
1292                SM.getFileEntryForID(SM.getMainFileID())->getName())) {
1293         auto &Source = ProxSources[Entry.getKey()];
1294         Source.Cost = Entry.getValue() * ProxOpts.IncludeCost;
1295         // Symbols near our transitive includes are good, but only consider
1296         // things in the same directory or below it. Otherwise there can be
1297         // many false positives.
1298         if (Entry.getValue() > 0)
1299           Source.MaxUpTraversals = 1;
1300       }
1301       FileProximity.emplace(ProxSources, ProxOpts);
1302 
1303       Output = runWithSema();
1304       Inserter.reset(); // Make sure this doesn't out-live Clang.
1305       SPAN_ATTACH(Tracer, "sema_completion_kind",
1306                   getCompletionKindString(Recorder->CCContext.getKind()));
1307       log("Code complete: sema context {0}, query scopes [{1}]",
1308           getCompletionKindString(Recorder->CCContext.getKind()),
1309           llvm::join(QueryScopes.begin(), QueryScopes.end(), ","));
1310     });
1311 
1312     Recorder = RecorderOwner.get();
1313 
1314     semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
1315                      SemaCCInput, &Includes);
1316 
1317     SPAN_ATTACH(Tracer, "sema_results", NSema);
1318     SPAN_ATTACH(Tracer, "index_results", NIndex);
1319     SPAN_ATTACH(Tracer, "merged_results", NBoth);
1320     SPAN_ATTACH(Tracer, "returned_results", int64_t(Output.Completions.size()));
1321     SPAN_ATTACH(Tracer, "incomplete", Output.HasMore);
1322     log("Code complete: {0} results from Sema, {1} from Index, "
1323         "{2} matched, {3} returned{4}.",
1324         NSema, NIndex, NBoth, Output.Completions.size(),
1325         Output.HasMore ? " (incomplete)" : "");
1326     assert(!Opts.Limit || Output.Completions.size() <= Opts.Limit);
1327     // We don't assert that isIncomplete means we hit a limit.
1328     // Indexes may choose to impose their own limits even if we don't have one.
1329     return Output;
1330   }
1331 
1332 private:
1333   // This is called by run() once Sema code completion is done, but before the
1334   // Sema data structures are torn down. It does all the real work.
1335   CodeCompleteResult runWithSema() {
1336     const auto &CodeCompletionRange = CharSourceRange::getCharRange(
1337         Recorder->CCSema->getPreprocessor().getCodeCompletionTokenRange());
1338     Range TextEditRange;
1339     // When we are getting completions with an empty identifier, for example
1340     //    std::vector<int> asdf;
1341     //    asdf.^;
1342     // Then the range will be invalid and we will be doing insertion, use
1343     // current cursor position in such cases as range.
1344     if (CodeCompletionRange.isValid()) {
1345       TextEditRange = halfOpenToRange(Recorder->CCSema->getSourceManager(),
1346                                       CodeCompletionRange);
1347     } else {
1348       const auto &Pos = sourceLocToPosition(
1349           Recorder->CCSema->getSourceManager(),
1350           Recorder->CCSema->getPreprocessor().getCodeCompletionLoc());
1351       TextEditRange.start = TextEditRange.end = Pos;
1352     }
1353     Filter = FuzzyMatcher(
1354         Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
1355     QueryScopes = getQueryScopes(Recorder->CCContext,
1356                                  Recorder->CCSema->getSourceManager());
1357     // Sema provides the needed context to query the index.
1358     // FIXME: in addition to querying for extra/overlapping symbols, we should
1359     //        explicitly request symbols corresponding to Sema results.
1360     //        We can use their signals even if the index can't suggest them.
1361     // We must copy index results to preserve them, but there are at most Limit.
1362     auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
1363                             ? queryIndex()
1364                             : SymbolSlab();
1365     trace::Span Tracer("Populate CodeCompleteResult");
1366     // Merge Sema, Index and Override results, score them, and pick the
1367     // winners.
1368     const auto Overrides = getNonOverridenMethodCompletionResults(
1369         Recorder->CCSema->CurContext, Recorder->CCSema);
1370     auto Top = mergeResults(Recorder->Results, IndexResults, Overrides);
1371     CodeCompleteResult Output;
1372 
1373     // Convert the results to final form, assembling the expensive strings.
1374     for (auto &C : Top) {
1375       Output.Completions.push_back(toCodeCompletion(C.first));
1376       Output.Completions.back().Score = C.second;
1377       Output.Completions.back().CompletionTokenRange = TextEditRange;
1378     }
1379     Output.HasMore = Incomplete;
1380     Output.Context = Recorder->CCContext.getKind();
1381 
1382     return Output;
1383   }
1384 
1385   SymbolSlab queryIndex() {
1386     trace::Span Tracer("Query index");
1387     SPAN_ATTACH(Tracer, "limit", int64_t(Opts.Limit));
1388 
1389     // Build the query.
1390     FuzzyFindRequest Req;
1391     if (Opts.Limit)
1392       Req.Limit = Opts.Limit;
1393     Req.Query = Filter->pattern();
1394     Req.RestrictForCodeCompletion = true;
1395     Req.Scopes = QueryScopes;
1396     // FIXME: we should send multiple weighted paths here.
1397     Req.ProximityPaths.push_back(FileName);
1398     vlog("Code complete: fuzzyFind({0:2})", toJSON(Req));
1399 
1400     if (SpecFuzzyFind)
1401       SpecFuzzyFind->NewReq = Req;
1402     if (SpecFuzzyFind && SpecFuzzyFind->Result.valid() && (*SpecReq == Req)) {
1403       vlog("Code complete: speculative fuzzy request matches the actual index "
1404            "request. Waiting for the speculative index results.");
1405       SPAN_ATTACH(Tracer, "Speculative results", true);
1406 
1407       trace::Span WaitSpec("Wait speculative results");
1408       return SpecFuzzyFind->Result.get();
1409     }
1410 
1411     SPAN_ATTACH(Tracer, "Speculative results", false);
1412 
1413     // Run the query against the index.
1414     SymbolSlab::Builder ResultsBuilder;
1415     if (Opts.Index->fuzzyFind(
1416             Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); }))
1417       Incomplete = true;
1418     return std::move(ResultsBuilder).build();
1419   }
1420 
1421   // Merges Sema, Index and Override results where possible, to form
1422   // CompletionCandidates. Groups overloads if desired, to form
1423   // CompletionCandidate::Bundles. The bundles are scored and top results are
1424   // returned, best to worst.
1425   std::vector<ScoredBundle>
1426   mergeResults(const std::vector<CodeCompletionResult> &SemaResults,
1427                const SymbolSlab &IndexResults,
1428                const std::vector<CodeCompletionResult> &OverrideResults) {
1429     trace::Span Tracer("Merge and score results");
1430     std::vector<CompletionCandidate::Bundle> Bundles;
1431     llvm::DenseMap<size_t, size_t> BundleLookup;
1432     auto AddToBundles = [&](const CodeCompletionResult *SemaResult,
1433                             const Symbol *IndexResult,
1434                             bool IsOverride) {
1435       CompletionCandidate C;
1436       C.SemaResult = SemaResult;
1437       C.IndexResult = IndexResult;
1438       if (C.IndexResult)
1439         C.RankedIncludeHeaders = getRankedIncludes(*C.IndexResult);
1440       C.IsOverride = IsOverride;
1441       C.Name = IndexResult ? IndexResult->Name : Recorder->getName(*SemaResult);
1442       if (auto OverloadSet = Opts.BundleOverloads ? C.overloadSet() : 0) {
1443         auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1444         if (Ret.second)
1445           Bundles.emplace_back();
1446         Bundles[Ret.first->second].push_back(std::move(C));
1447       } else {
1448         Bundles.emplace_back();
1449         Bundles.back().push_back(std::move(C));
1450       }
1451     };
1452     llvm::DenseSet<const Symbol *> UsedIndexResults;
1453     auto CorrespondingIndexResult =
1454         [&](const CodeCompletionResult &SemaResult) -> const Symbol * {
1455       if (auto SymID =
1456               getSymbolID(SemaResult, Recorder->CCSema->getSourceManager())) {
1457         auto I = IndexResults.find(*SymID);
1458         if (I != IndexResults.end()) {
1459           UsedIndexResults.insert(&*I);
1460           return &*I;
1461         }
1462       }
1463       return nullptr;
1464     };
1465     // Emit all Sema results, merging them with Index results if possible.
1466     for (auto &SemaResult : Recorder->Results)
1467       AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult), false);
1468     // Handle OverrideResults the same way we deal with SemaResults. Since these
1469     // results use the same structs as a SemaResult it is safe to do that, but
1470     // we need to make sure we dont' duplicate things in future if Sema starts
1471     // to provide them as well.
1472     for (auto &OverrideResult : OverrideResults)
1473       AddToBundles(&OverrideResult, CorrespondingIndexResult(OverrideResult),
1474                    true);
1475     // Now emit any Index-only results.
1476     for (const auto &IndexResult : IndexResults) {
1477       if (UsedIndexResults.count(&IndexResult))
1478         continue;
1479       AddToBundles(/*SemaResult=*/nullptr, &IndexResult, false);
1480     }
1481     // We only keep the best N results at any time, in "native" format.
1482     TopN<ScoredBundle, ScoredBundleGreater> Top(
1483         Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1484     for (auto &Bundle : Bundles)
1485       addCandidate(Top, std::move(Bundle));
1486     return std::move(Top).items();
1487   }
1488 
1489   Optional<float> fuzzyScore(const CompletionCandidate &C) {
1490     // Macros can be very spammy, so we only support prefix completion.
1491     // We won't end up with underfull index results, as macros are sema-only.
1492     if (C.SemaResult && C.SemaResult->Kind == CodeCompletionResult::RK_Macro &&
1493         !C.Name.startswith_lower(Filter->pattern()))
1494       return None;
1495     return Filter->match(C.Name);
1496   }
1497 
1498   // Scores a candidate and adds it to the TopN structure.
1499   void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
1500                     CompletionCandidate::Bundle Bundle) {
1501     SymbolQualitySignals Quality;
1502     SymbolRelevanceSignals Relevance;
1503     Relevance.Context = Recorder->CCContext.getKind();
1504     Relevance.Query = SymbolRelevanceSignals::CodeComplete;
1505     Relevance.FileProximityMatch = FileProximity.getPointer();
1506     auto &First = Bundle.front();
1507     if (auto FuzzyScore = fuzzyScore(First))
1508       Relevance.NameMatch = *FuzzyScore;
1509     else
1510       return;
1511     SymbolOrigin Origin = SymbolOrigin::Unknown;
1512     bool FromIndex = false;
1513     for (const auto &Candidate : Bundle) {
1514       if (Candidate.IndexResult) {
1515         Quality.merge(*Candidate.IndexResult);
1516         Relevance.merge(*Candidate.IndexResult);
1517         Origin |= Candidate.IndexResult->Origin;
1518         FromIndex = true;
1519       }
1520       if (Candidate.SemaResult) {
1521         Quality.merge(*Candidate.SemaResult);
1522         Relevance.merge(*Candidate.SemaResult);
1523         Origin |= SymbolOrigin::AST;
1524       }
1525     }
1526 
1527     CodeCompletion::Scores Scores;
1528     Scores.Quality = Quality.evaluate();
1529     Scores.Relevance = Relevance.evaluate();
1530     Scores.Total = evaluateSymbolAndRelevance(Scores.Quality, Scores.Relevance);
1531     // NameMatch is in fact a multiplier on total score, so rescoring is sound.
1532     Scores.ExcludingName = Relevance.NameMatch
1533                                ? Scores.Total / Relevance.NameMatch
1534                                : Scores.Quality;
1535 
1536     dlog("CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First.Name,
1537          llvm::to_string(Origin), Scores.Total, llvm::to_string(Quality),
1538          llvm::to_string(Relevance));
1539 
1540     NSema += bool(Origin & SymbolOrigin::AST);
1541     NIndex += FromIndex;
1542     NBoth += bool(Origin & SymbolOrigin::AST) && FromIndex;
1543     if (Candidates.push({std::move(Bundle), Scores}))
1544       Incomplete = true;
1545   }
1546 
1547   CodeCompletion toCodeCompletion(const CompletionCandidate::Bundle &Bundle) {
1548     llvm::Optional<CodeCompletionBuilder> Builder;
1549     for (const auto &Item : Bundle) {
1550       CodeCompletionString *SemaCCS =
1551           Item.SemaResult ? Recorder->codeCompletionString(*Item.SemaResult)
1552                           : nullptr;
1553       if (!Builder)
1554         Builder.emplace(Recorder->CCSema->getASTContext(), Item, SemaCCS,
1555                         *Inserter, FileName, Recorder->CCContext.getKind(),
1556                         Opts);
1557       else
1558         Builder->add(Item, SemaCCS);
1559     }
1560     return Builder->build();
1561   }
1562 };
1563 
1564 llvm::Expected<llvm::StringRef>
1565 speculateCompletionFilter(llvm::StringRef Content, Position Pos) {
1566   auto Offset = positionToOffset(Content, Pos);
1567   if (!Offset)
1568     return llvm::make_error<llvm::StringError>(
1569         "Failed to convert position to offset in content.",
1570         llvm::inconvertibleErrorCode());
1571   if (*Offset == 0)
1572     return "";
1573 
1574   // Start from the character before the cursor.
1575   int St = *Offset - 1;
1576   // FIXME(ioeric): consider UTF characters?
1577   auto IsValidIdentifierChar = [](char c) {
1578     return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
1579             (c >= '0' && c <= '9') || (c == '_'));
1580   };
1581   size_t Len = 0;
1582   for (; (St >= 0) && IsValidIdentifierChar(Content[St]); --St, ++Len) {
1583   }
1584   if (Len > 0)
1585     St++; // Shift to the first valid character.
1586   return Content.substr(St, Len);
1587 }
1588 
1589 CodeCompleteResult
1590 codeComplete(PathRef FileName, const tooling::CompileCommand &Command,
1591              PrecompiledPreamble const *Preamble,
1592              const IncludeStructure &PreambleInclusions, StringRef Contents,
1593              Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
1594              std::shared_ptr<PCHContainerOperations> PCHs,
1595              CodeCompleteOptions Opts, SpeculativeFuzzyFind *SpecFuzzyFind) {
1596   return CodeCompleteFlow(FileName, PreambleInclusions, SpecFuzzyFind, Opts)
1597       .run({FileName, Command, Preamble, Contents, Pos, VFS, PCHs});
1598 }
1599 
1600 SignatureHelp signatureHelp(PathRef FileName,
1601                             const tooling::CompileCommand &Command,
1602                             PrecompiledPreamble const *Preamble,
1603                             StringRef Contents, Position Pos,
1604                             IntrusiveRefCntPtr<vfs::FileSystem> VFS,
1605                             std::shared_ptr<PCHContainerOperations> PCHs,
1606                             const SymbolIndex *Index) {
1607   SignatureHelp Result;
1608   clang::CodeCompleteOptions Options;
1609   Options.IncludeGlobals = false;
1610   Options.IncludeMacros = false;
1611   Options.IncludeCodePatterns = false;
1612   Options.IncludeBriefComments = false;
1613   IncludeStructure PreambleInclusions; // Unused for signatureHelp
1614   semaCodeComplete(
1615       llvm::make_unique<SignatureHelpCollector>(Options, Index, Result),
1616       Options,
1617       {FileName, Command, Preamble, Contents, Pos, std::move(VFS),
1618        std::move(PCHs)});
1619   return Result;
1620 }
1621 
1622 bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) {
1623   using namespace clang::ast_matchers;
1624   auto InTopLevelScope = hasDeclContext(
1625       anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl()));
1626   return !match(decl(anyOf(InTopLevelScope,
1627                            hasDeclContext(
1628                                enumDecl(InTopLevelScope, unless(isScoped()))))),
1629                 ND, ASTCtx)
1630               .empty();
1631 }
1632 
1633 CompletionItem CodeCompletion::render(const CodeCompleteOptions &Opts) const {
1634   CompletionItem LSP;
1635   const auto *InsertInclude = Includes.empty() ? nullptr : &Includes[0];
1636   LSP.label = ((InsertInclude && InsertInclude->Insertion)
1637                    ? Opts.IncludeIndicator.Insert
1638                    : Opts.IncludeIndicator.NoInsert) +
1639               (Opts.ShowOrigins ? "[" + llvm::to_string(Origin) + "]" : "") +
1640               RequiredQualifier + Name + Signature;
1641 
1642   LSP.kind = Kind;
1643   LSP.detail = BundleSize > 1 ? llvm::formatv("[{0} overloads]", BundleSize)
1644                               : ReturnType;
1645   LSP.deprecated = Deprecated;
1646   if (InsertInclude)
1647     LSP.detail += "\n" + InsertInclude->Header;
1648   LSP.documentation = Documentation;
1649   LSP.sortText = sortText(Score.Total, Name);
1650   LSP.filterText = Name;
1651   LSP.textEdit = {CompletionTokenRange, RequiredQualifier + Name};
1652   // Merge continuous additionalTextEdits into main edit. The main motivation
1653   // behind this is to help LSP clients, it seems most of them are confused when
1654   // they are provided with additionalTextEdits that are consecutive to main
1655   // edit.
1656   // Note that we store additional text edits from back to front in a line. That
1657   // is mainly to help LSP clients again, so that changes do not effect each
1658   // other.
1659   for (const auto &FixIt : FixIts) {
1660     if (IsRangeConsecutive(FixIt.range, LSP.textEdit->range)) {
1661       LSP.textEdit->newText = FixIt.newText + LSP.textEdit->newText;
1662       LSP.textEdit->range.start = FixIt.range.start;
1663     } else {
1664       LSP.additionalTextEdits.push_back(FixIt);
1665     }
1666   }
1667   if (Opts.EnableSnippets)
1668     LSP.textEdit->newText += SnippetSuffix;
1669 
1670   // FIXME(kadircet): Do not even fill insertText after making sure textEdit is
1671   // compatible with most of the editors.
1672   LSP.insertText = LSP.textEdit->newText;
1673   LSP.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet
1674                                              : InsertTextFormat::PlainText;
1675   if (InsertInclude && InsertInclude->Insertion)
1676     LSP.additionalTextEdits.push_back(*InsertInclude->Insertion);
1677 
1678   return LSP;
1679 }
1680 
1681 raw_ostream &operator<<(raw_ostream &OS, const CodeCompletion &C) {
1682   // For now just lean on CompletionItem.
1683   return OS << C.render(CodeCompleteOptions());
1684 }
1685 
1686 raw_ostream &operator<<(raw_ostream &OS, const CodeCompleteResult &R) {
1687   OS << "CodeCompleteResult: " << R.Completions.size() << (R.HasMore ? "+" : "")
1688      << " (" << getCompletionKindString(R.Context) << ")"
1689      << " items:\n";
1690   for (const auto &C : R.Completions)
1691     OS << C << "\n";
1692   return OS;
1693 }
1694 
1695 } // namespace clangd
1696 } // namespace clang
1697