1 //===--- CodeComplete.cpp ----------------------------------------*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
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
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.
18 //===----------------------------------------------------------------------===//
20 #include "CodeComplete.h"
22 #include "CodeCompletionStrings.h"
25 #include "ExpectedTypes.h"
27 #include "FileDistance.h"
28 #include "FuzzyMatch.h"
34 #include "SourceCode.h"
36 #include "index/Index.h"
37 #include "index/Symbol.h"
38 #include "index/SymbolOrigin.h"
39 #include "support/Logger.h"
40 #include "support/Markup.h"
41 #include "support/Threading.h"
42 #include "support/ThreadsafeFS.h"
43 #include "support/Trace.h"
44 #include "clang/AST/Decl.h"
45 #include "clang/AST/DeclBase.h"
46 #include "clang/Basic/CharInfo.h"
47 #include "clang/Basic/LangOptions.h"
48 #include "clang/Basic/SourceLocation.h"
49 #include "clang/Basic/TokenKinds.h"
50 #include "clang/Format/Format.h"
51 #include "clang/Frontend/CompilerInstance.h"
52 #include "clang/Frontend/FrontendActions.h"
53 #include "clang/Lex/ExternalPreprocessorSource.h"
54 #include "clang/Lex/Lexer.h"
55 #include "clang/Lex/Preprocessor.h"
56 #include "clang/Lex/PreprocessorOptions.h"
57 #include "clang/Sema/CodeCompleteConsumer.h"
58 #include "clang/Sema/DeclSpec.h"
59 #include "clang/Sema/Sema.h"
60 #include "llvm/ADT/ArrayRef.h"
61 #include "llvm/ADT/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"
76 // We log detailed candidate here if you run with -debug-only=codecomplete.
77 #define DEBUG_TYPE "CodeComplete"
82 #if CLANGD_DECISION_FOREST
83 const CodeCompleteOptions::CodeCompletionRankingModel
84 CodeCompleteOptions::DefaultRankingModel
=
85 CodeCompleteOptions::DecisionForest
;
87 const CodeCompleteOptions::CodeCompletionRankingModel
88 CodeCompleteOptions::DefaultRankingModel
= CodeCompleteOptions::Heuristics
;
93 // Note: changes to this function should also be reflected in the
94 // CodeCompletionResult overload where appropriate.
96 toCompletionItemKind(index::SymbolKind Kind
,
97 const llvm::StringRef
*Signature
= nullptr) {
98 using SK
= index::SymbolKind
;
101 return CompletionItemKind::Missing
;
104 case SK::NamespaceAlias
:
105 return CompletionItemKind::Module
;
107 // Use macro signature (if provided) to tell apart function-like and
108 // object-like macros.
109 return Signature
&& Signature
->contains('(') ? CompletionItemKind::Function
110 : CompletionItemKind::Constant
;
112 return CompletionItemKind::Enum
;
114 return CompletionItemKind::Struct
;
118 return CompletionItemKind::Class
;
120 // Use interface instead of class for differentiation of classes and
121 // protocols with the same name (e.g. @interface NSObject vs. @protocol
123 return CompletionItemKind::Interface
;
125 // We use the same kind as the VSCode C++ extension.
126 // FIXME: pick a better option when we have one.
127 return CompletionItemKind::Interface
;
129 return CompletionItemKind::Reference
;
131 case SK::ConversionFunction
:
132 return CompletionItemKind::Function
;
135 case SK::NonTypeTemplateParm
:
136 return CompletionItemKind::Variable
;
138 return CompletionItemKind::Field
;
139 case SK::EnumConstant
:
140 return CompletionItemKind::EnumMember
;
141 case SK::InstanceMethod
:
142 case SK::ClassMethod
:
143 case SK::StaticMethod
:
145 return CompletionItemKind::Method
;
146 case SK::InstanceProperty
:
147 case SK::ClassProperty
:
148 case SK::StaticProperty
:
149 return CompletionItemKind::Property
;
150 case SK::Constructor
:
151 return CompletionItemKind::Constructor
;
152 case SK::TemplateTypeParm
:
153 case SK::TemplateTemplateParm
:
154 return CompletionItemKind::TypeParameter
;
156 return CompletionItemKind::Interface
;
158 llvm_unreachable("Unhandled clang::index::SymbolKind.");
161 // Note: changes to this function should also be reflected in the
162 // index::SymbolKind overload where appropriate.
163 CompletionItemKind
toCompletionItemKind(const CodeCompletionResult
&Res
,
164 CodeCompletionContext::Kind CtxKind
) {
166 return toCompletionItemKind(index::getSymbolInfo(Res
.Declaration
).Kind
);
167 if (CtxKind
== CodeCompletionContext::CCC_IncludedFile
)
168 return CompletionItemKind::File
;
170 case CodeCompletionResult::RK_Declaration
:
171 llvm_unreachable("RK_Declaration without Decl");
172 case CodeCompletionResult::RK_Keyword
:
173 return CompletionItemKind::Keyword
;
174 case CodeCompletionResult::RK_Macro
:
175 // There is no 'Macro' kind in LSP.
176 // Avoid using 'Text' to avoid confusion with client-side word-based
177 // completion proposals.
178 return Res
.MacroDefInfo
&& Res
.MacroDefInfo
->isFunctionLike()
179 ? CompletionItemKind::Function
180 : CompletionItemKind::Constant
;
181 case CodeCompletionResult::RK_Pattern
:
182 return CompletionItemKind::Snippet
;
184 llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
187 // FIXME: find a home for this (that can depend on both markup and Protocol).
188 MarkupContent
renderDoc(const markup::Document
&Doc
, MarkupKind Kind
) {
189 MarkupContent Result
;
192 case MarkupKind::PlainText
:
193 Result
.value
.append(Doc
.asPlainText());
195 case MarkupKind::Markdown
:
196 Result
.value
.append(Doc
.asMarkdown());
202 Symbol::IncludeDirective
insertionDirective(const CodeCompleteOptions
&Opts
) {
203 if (!Opts
.ImportInsertions
|| !Opts
.MainFileSignals
)
204 return Symbol::IncludeDirective::Include
;
205 return Opts
.MainFileSignals
->InsertionDirective
;
208 // Identifier code completion result.
209 struct RawIdentifier
{
210 llvm::StringRef Name
;
211 unsigned References
; // # of usages in file.
214 /// A code completion result, in clang-native form.
215 /// It may be promoted to a CompletionItem if it's among the top-ranked results.
216 struct CompletionCandidate
{
217 llvm::StringRef Name
; // Used for filtering and sorting.
218 // We may have a result from Sema, from the index, or both.
219 const CodeCompletionResult
*SemaResult
= nullptr;
220 const Symbol
*IndexResult
= nullptr;
221 const RawIdentifier
*IdentifierResult
= nullptr;
222 llvm::SmallVector
<SymbolInclude
, 1> RankedIncludeHeaders
;
224 // Returns a token identifying the overload set this is part of.
225 // 0 indicates it's not part of any overload set.
226 size_t overloadSet(const CodeCompleteOptions
&Opts
, llvm::StringRef FileName
,
227 IncludeInserter
*Inserter
,
228 CodeCompletionContext::Kind CCContextKind
) const {
229 if (!Opts
.BundleOverloads
.value_or(false))
232 // Depending on the index implementation, we can see different header
233 // strings (literal or URI) mapping to the same file. We still want to
234 // bundle those, so we must resolve the header to be included here.
235 std::string HeaderForHash
;
237 if (auto Header
= headerToInsertIfAllowed(Opts
, CCContextKind
)) {
238 if (auto HeaderFile
= toHeaderFile(*Header
, FileName
)) {
240 Inserter
->calculateIncludePath(*HeaderFile
, FileName
))
241 HeaderForHash
= *Spelled
;
243 vlog("Code completion header path manipulation failed {0}",
244 HeaderFile
.takeError());
249 llvm::SmallString
<256> Scratch
;
251 switch (IndexResult
->SymInfo
.Kind
) {
252 case index::SymbolKind::ClassMethod
:
253 case index::SymbolKind::InstanceMethod
:
254 case index::SymbolKind::StaticMethod
:
256 llvm_unreachable("Don't expect members from index in code completion");
260 case index::SymbolKind::Function
:
261 // We can't group overloads together that need different #includes.
262 // This could break #include insertion.
263 return llvm::hash_combine(
264 (IndexResult
->Scope
+ IndexResult
->Name
).toStringRef(Scratch
),
271 // We need to make sure we're consistent with the IndexResult case!
272 const NamedDecl
*D
= SemaResult
->Declaration
;
273 if (!D
|| !D
->isFunctionOrFunctionTemplate())
276 llvm::raw_svector_ostream
OS(Scratch
);
277 D
->printQualifiedName(OS
);
279 return llvm::hash_combine(Scratch
, HeaderForHash
);
281 assert(IdentifierResult
);
285 bool contextAllowsHeaderInsertion(CodeCompletionContext::Kind Kind
) const {
286 // Explicitly disable insertions for forward declarations since they don't
287 // reference the declaration.
288 if (Kind
== CodeCompletionContext::CCC_ObjCClassForwardDecl
)
293 // The best header to include if include insertion is allowed.
294 std::optional
<llvm::StringRef
>
295 headerToInsertIfAllowed(const CodeCompleteOptions
&Opts
,
296 CodeCompletionContext::Kind ContextKind
) const {
297 if (Opts
.InsertIncludes
== CodeCompleteOptions::NeverInsert
||
298 RankedIncludeHeaders
.empty() ||
299 !contextAllowsHeaderInsertion(ContextKind
))
301 if (SemaResult
&& SemaResult
->Declaration
) {
302 // Avoid inserting new #include if the declaration is found in the current
303 // file e.g. the symbol is forward declared.
304 auto &SM
= SemaResult
->Declaration
->getASTContext().getSourceManager();
305 for (const Decl
*RD
: SemaResult
->Declaration
->redecls())
306 if (SM
.isInMainFile(SM
.getExpansionLoc(RD
->getBeginLoc())))
309 Symbol::IncludeDirective Directive
= insertionDirective(Opts
);
310 for (const auto &Inc
: RankedIncludeHeaders
)
311 if ((Inc
.Directive
& Directive
) != 0)
316 using Bundle
= llvm::SmallVector
<CompletionCandidate
, 4>;
319 std::pair
<CompletionCandidate::Bundle
, CodeCompletion::Scores
>;
320 struct ScoredBundleGreater
{
321 bool operator()(const ScoredBundle
&L
, const ScoredBundle
&R
) {
322 if (L
.second
.Total
!= R
.second
.Total
)
323 return L
.second
.Total
> R
.second
.Total
;
324 return L
.first
.front().Name
<
325 R
.first
.front().Name
; // Earlier name is better.
329 // Remove the first template argument from Signature.
330 // If Signature only contains a single argument an empty string is returned.
331 std::string
removeFirstTemplateArg(llvm::StringRef Signature
) {
332 auto Rest
= Signature
.split(",").second
;
335 return ("<" + Rest
.ltrim()).str();
338 // Assembles a code completion out of a bundle of >=1 completion candidates.
339 // Many of the expensive strings are only computed at this point, once we know
340 // the candidate bundle is going to be returned.
342 // Many fields are the same for all candidates in a bundle (e.g. name), and are
343 // computed from the first candidate, in the constructor.
344 // Others vary per candidate, so add() must be called for remaining candidates.
345 struct CodeCompletionBuilder
{
346 CodeCompletionBuilder(ASTContext
*ASTCtx
, const CompletionCandidate
&C
,
347 CodeCompletionString
*SemaCCS
,
348 llvm::ArrayRef
<std::string
> AccessibleScopes
,
349 const IncludeInserter
&Includes
,
350 llvm::StringRef FileName
,
351 CodeCompletionContext::Kind ContextKind
,
352 const CodeCompleteOptions
&Opts
,
353 bool IsUsingDeclaration
, tok::TokenKind NextTokenKind
)
354 : ASTCtx(ASTCtx
), ArgumentLists(Opts
.ArgumentLists
),
355 IsUsingDeclaration(IsUsingDeclaration
), NextTokenKind(NextTokenKind
) {
356 Completion
.Deprecated
= true; // cleared by any non-deprecated overload.
357 add(C
, SemaCCS
, ContextKind
);
360 Completion
.Origin
|= SymbolOrigin::AST
;
361 Completion
.Name
= std::string(llvm::StringRef(SemaCCS
->getTypedText()));
362 Completion
.FilterText
= SemaCCS
->getAllTypedText();
363 if (Completion
.Scope
.empty()) {
364 if ((C
.SemaResult
->Kind
== CodeCompletionResult::RK_Declaration
) ||
365 (C
.SemaResult
->Kind
== CodeCompletionResult::RK_Pattern
))
366 if (const auto *D
= C
.SemaResult
->getDeclaration())
367 if (const auto *ND
= dyn_cast
<NamedDecl
>(D
))
368 Completion
.Scope
= std::string(
369 splitQualifiedName(printQualifiedName(*ND
)).first
);
371 Completion
.Kind
= toCompletionItemKind(*C
.SemaResult
, ContextKind
);
372 // Sema could provide more info on whether the completion was a file or
374 if (Completion
.Kind
== CompletionItemKind::File
&&
375 Completion
.Name
.back() == '/')
376 Completion
.Kind
= CompletionItemKind::Folder
;
377 for (const auto &FixIt
: C
.SemaResult
->FixIts
) {
378 Completion
.FixIts
.push_back(toTextEdit(
379 FixIt
, ASTCtx
->getSourceManager(), ASTCtx
->getLangOpts()));
381 llvm::sort(Completion
.FixIts
, [](const TextEdit
&X
, const TextEdit
&Y
) {
382 return std::tie(X
.range
.start
.line
, X
.range
.start
.character
) <
383 std::tie(Y
.range
.start
.line
, Y
.range
.start
.character
);
387 Completion
.Origin
|= C
.IndexResult
->Origin
;
388 if (Completion
.Scope
.empty())
389 Completion
.Scope
= std::string(C
.IndexResult
->Scope
);
390 if (Completion
.Kind
== CompletionItemKind::Missing
)
391 Completion
.Kind
= toCompletionItemKind(C
.IndexResult
->SymInfo
.Kind
,
392 &C
.IndexResult
->Signature
);
393 if (Completion
.Name
.empty())
394 Completion
.Name
= std::string(C
.IndexResult
->Name
);
395 if (Completion
.FilterText
.empty())
396 Completion
.FilterText
= Completion
.Name
;
397 // If the completion was visible to Sema, no qualifier is needed. This
398 // avoids unneeded qualifiers in cases like with `using ns::X`.
399 if (Completion
.RequiredQualifier
.empty() && !C
.SemaResult
) {
400 llvm::StringRef ShortestQualifier
= C
.IndexResult
->Scope
;
401 for (llvm::StringRef Scope
: AccessibleScopes
) {
402 llvm::StringRef Qualifier
= C
.IndexResult
->Scope
;
403 if (Qualifier
.consume_front(Scope
) &&
404 Qualifier
.size() < ShortestQualifier
.size())
405 ShortestQualifier
= Qualifier
;
407 Completion
.RequiredQualifier
= std::string(ShortestQualifier
);
410 if (C
.IdentifierResult
) {
411 Completion
.Origin
|= SymbolOrigin::Identifier
;
412 Completion
.Kind
= CompletionItemKind::Text
;
413 Completion
.Name
= std::string(C
.IdentifierResult
->Name
);
414 Completion
.FilterText
= Completion
.Name
;
417 // Turn absolute path into a literal string that can be #included.
418 auto Inserted
= [&](llvm::StringRef Header
)
419 -> llvm::Expected
<std::pair
<std::string
, bool>> {
420 auto ResolvedDeclaring
=
421 URI::resolve(C
.IndexResult
->CanonicalDeclaration
.FileURI
, FileName
);
422 if (!ResolvedDeclaring
)
423 return ResolvedDeclaring
.takeError();
424 auto ResolvedInserted
= toHeaderFile(Header
, FileName
);
425 if (!ResolvedInserted
)
426 return ResolvedInserted
.takeError();
427 auto Spelled
= Includes
.calculateIncludePath(*ResolvedInserted
, FileName
);
429 return error("Header not on include path");
430 return std::make_pair(
432 Includes
.shouldInsertInclude(*ResolvedDeclaring
, *ResolvedInserted
));
435 C
.headerToInsertIfAllowed(Opts
, ContextKind
).has_value();
436 Symbol::IncludeDirective Directive
= insertionDirective(Opts
);
437 // Calculate include paths and edits for all possible headers.
438 for (const auto &Inc
: C
.RankedIncludeHeaders
) {
439 if ((Inc
.Directive
& Directive
) == 0)
442 if (auto ToInclude
= Inserted(Inc
.Header
)) {
443 CodeCompletion::IncludeCandidate Include
;
444 Include
.Header
= ToInclude
->first
;
445 if (ToInclude
->second
&& ShouldInsert
)
446 Include
.Insertion
= Includes
.insert(
447 ToInclude
->first
, Directive
== Symbol::Import
448 ? tooling::IncludeDirective::Import
449 : tooling::IncludeDirective::Include
);
450 Completion
.Includes
.push_back(std::move(Include
));
452 log("Failed to generate include insertion edits for adding header "
453 "(FileURI='{0}', IncludeHeader='{1}') into {2}: {3}",
454 C
.IndexResult
->CanonicalDeclaration
.FileURI
, Inc
.Header
, FileName
,
455 ToInclude
.takeError());
457 // Prefer includes that do not need edits (i.e. already exist).
458 std::stable_partition(Completion
.Includes
.begin(),
459 Completion
.Includes
.end(),
460 [](const CodeCompletion::IncludeCandidate
&I
) {
461 return !I
.Insertion
.has_value();
465 void add(const CompletionCandidate
&C
, CodeCompletionString
*SemaCCS
,
466 CodeCompletionContext::Kind ContextKind
) {
467 assert(bool(C
.SemaResult
) == bool(SemaCCS
));
468 Bundled
.emplace_back();
469 BundledEntry
&S
= Bundled
.back();
470 bool IsConcept
= false;
472 getSignature(*SemaCCS
, &S
.Signature
, &S
.SnippetSuffix
, C
.SemaResult
->Kind
,
473 C
.SemaResult
->CursorKind
,
474 /*IncludeFunctionArguments=*/C
.SemaResult
->FunctionCanBeCall
,
475 /*RequiredQualifiers=*/&Completion
.RequiredQualifier
);
476 S
.ReturnType
= getReturnType(*SemaCCS
);
477 if (C
.SemaResult
->Kind
== CodeCompletionResult::RK_Declaration
)
478 if (const auto *D
= C
.SemaResult
->getDeclaration())
479 if (isa
<ConceptDecl
>(D
))
481 } else if (C
.IndexResult
) {
482 S
.Signature
= std::string(C
.IndexResult
->Signature
);
483 S
.SnippetSuffix
= std::string(C
.IndexResult
->CompletionSnippetSuffix
);
484 S
.ReturnType
= std::string(C
.IndexResult
->ReturnType
);
485 if (C
.IndexResult
->SymInfo
.Kind
== index::SymbolKind::Concept
)
489 /// When a concept is used as a type-constraint (e.g. `Iterator auto x`),
490 /// and in some other contexts, its first type argument is not written.
491 /// Drop the parameter from the signature.
492 if (IsConcept
&& ContextKind
== CodeCompletionContext::CCC_TopLevel
) {
493 S
.Signature
= removeFirstTemplateArg(S
.Signature
);
494 // Dropping the first placeholder from the suffix will leave a $2
496 S
.SnippetSuffix
= removeFirstTemplateArg(S
.SnippetSuffix
);
499 if (!Completion
.Documentation
) {
500 auto SetDoc
= [&](llvm::StringRef Doc
) {
502 Completion
.Documentation
.emplace();
503 parseDocumentation(Doc
, *Completion
.Documentation
);
507 SetDoc(C
.IndexResult
->Documentation
);
508 } else if (C
.SemaResult
) {
509 const auto DocComment
= getDocComment(*ASTCtx
, *C
.SemaResult
,
510 /*CommentsFromHeaders=*/false);
511 SetDoc(formatDocumentation(*SemaCCS
, DocComment
));
514 if (Completion
.Deprecated
) {
516 Completion
.Deprecated
&=
517 C
.SemaResult
->Availability
== CXAvailability_Deprecated
;
519 Completion
.Deprecated
&=
520 bool(C
.IndexResult
->Flags
& Symbol::Deprecated
);
524 CodeCompletion
build() {
525 Completion
.ReturnType
= summarizeReturnType();
526 Completion
.Signature
= summarizeSignature();
527 Completion
.SnippetSuffix
= summarizeSnippet();
528 Completion
.BundleSize
= Bundled
.size();
529 return std::move(Completion
);
533 struct BundledEntry
{
534 std::string SnippetSuffix
;
535 std::string Signature
;
536 std::string ReturnType
;
539 // If all BundledEntries have the same value for a property, return it.
540 template <std::string
BundledEntry::*Member
>
541 const std::string
*onlyValue() const {
542 auto B
= Bundled
.begin(), E
= Bundled
.end();
543 for (auto *I
= B
+ 1; I
!= E
; ++I
)
544 if (I
->*Member
!= B
->*Member
)
546 return &(B
->*Member
);
549 template <bool BundledEntry::*Member
> const bool *onlyValue() const {
550 auto B
= Bundled
.begin(), E
= Bundled
.end();
551 for (auto *I
= B
+ 1; I
!= E
; ++I
)
552 if (I
->*Member
!= B
->*Member
)
554 return &(B
->*Member
);
557 std::string
summarizeReturnType() const {
558 if (auto *RT
= onlyValue
<&BundledEntry::ReturnType
>())
563 std::string
summarizeSnippet() const {
564 /// localize ArgumentLists tests for better readability
565 const bool None
= ArgumentLists
== Config::ArgumentListsPolicy::None
;
567 ArgumentLists
== Config::ArgumentListsPolicy::OpenDelimiter
;
568 const bool Delim
= ArgumentLists
== Config::ArgumentListsPolicy::Delimiters
;
570 ArgumentLists
== Config::ArgumentListsPolicy::FullPlaceholders
||
571 (!None
&& !Open
&& !Delim
); // <-- failsafe: Full is default
573 if (IsUsingDeclaration
)
575 auto *Snippet
= onlyValue
<&BundledEntry::SnippetSuffix
>();
577 // All bundles are function calls.
578 // FIXME(ibiryukov): sometimes add template arguments to a snippet, e.g.
579 // we need to complete 'forward<$1>($0)'.
580 return None
? "" : (Open
? "(" : "($0)");
582 if (Snippet
->empty())
585 bool MayHaveArgList
= Completion
.Kind
== CompletionItemKind::Function
||
586 Completion
.Kind
== CompletionItemKind::Method
||
587 Completion
.Kind
== CompletionItemKind::Constructor
||
588 Completion
.Kind
== CompletionItemKind::Text
/*Macro*/;
589 // If likely arg list already exists, don't add new parens & placeholders.
590 // Snippet: function(int x, int y)
591 // func^(1,2) -> function(1, 2)
592 // NOT function(int x, int y)(1, 2)
593 if (MayHaveArgList
) {
594 // Check for a template argument list in the code.
595 // Snippet: function<class T>(int x)
596 // fu^<int>(1) -> function<int>(1)
597 if (NextTokenKind
== tok::less
&& Snippet
->front() == '<')
599 // Potentially followed by regular argument list.
600 if (NextTokenKind
== tok::l_paren
) {
601 // Snippet: function<class T>(int x)
602 // fu^(1,2) -> function<class T>(1, 2)
603 if (Snippet
->front() == '<') {
604 // Find matching '>', handling nested brackets.
608 if (Snippet
->at(I
) == '>')
610 else if (Snippet
->at(I
) == '<')
613 } while (Balance
> 0);
614 return Snippet
->substr(0, I
);
622 // Replace argument snippets with a simplified pattern.
623 if (MayHaveArgList
) {
624 // Functions snippets can be of 2 types:
625 // - containing only function arguments, e.g.
626 // foo(${1:int p1}, ${2:int p2});
627 // We transform this pattern to '($0)' or '()'.
628 // - template arguments and function arguments, e.g.
629 // foo<${1:class}>(${2:int p1}).
630 // We transform this pattern to '<$1>()$0' or '<$0>()'.
632 bool EmptyArgs
= llvm::StringRef(*Snippet
).ends_with("()");
633 if (Snippet
->front() == '<')
634 return None
? "" : (Open
? "<" : (EmptyArgs
? "<$1>()$0" : "<$1>($0)"));
635 if (Snippet
->front() == '(')
636 return None
? "" : (Open
? "(" : (EmptyArgs
? "()" : "($0)"));
637 return *Snippet
; // Not an arg snippet?
639 // 'CompletionItemKind::Interface' matches template type aliases.
640 if (Completion
.Kind
== CompletionItemKind::Interface
||
641 Completion
.Kind
== CompletionItemKind::Class
||
642 Completion
.Kind
== CompletionItemKind::Variable
) {
643 if (Snippet
->front() != '<')
644 return *Snippet
; // Not an arg snippet?
646 // Classes and template using aliases can only have template arguments,
647 // e.g. Foo<${1:class}>.
648 if (llvm::StringRef(*Snippet
).ends_with("<>"))
649 return "<>"; // can happen with defaulted template arguments.
650 return None
? "" : (Open
? "<" : "<$0>");
655 std::string
summarizeSignature() const {
656 if (auto *Signature
= onlyValue
<&BundledEntry::Signature
>())
658 // All bundles are function calls.
662 // ASTCtx can be nullptr if not run with sema.
664 CodeCompletion Completion
;
665 llvm::SmallVector
<BundledEntry
, 1> Bundled
;
666 /// the way argument lists are handled.
667 Config::ArgumentListsPolicy ArgumentLists
;
668 // No snippets will be generated for using declarations and when the function
669 // arguments are already present.
670 bool IsUsingDeclaration
;
671 tok::TokenKind NextTokenKind
;
674 // Determine the symbol ID for a Sema code completion result, if possible.
675 SymbolID
getSymbolID(const CodeCompletionResult
&R
, const SourceManager
&SM
) {
677 case CodeCompletionResult::RK_Declaration
:
678 case CodeCompletionResult::RK_Pattern
: {
679 // Computing USR caches linkage, which may change after code completion.
680 if (hasUnstableLinkage(R
.Declaration
))
682 return clang::clangd::getSymbolID(R
.Declaration
);
684 case CodeCompletionResult::RK_Macro
:
685 return clang::clangd::getSymbolID(R
.Macro
->getName(), R
.MacroDefInfo
, SM
);
686 case CodeCompletionResult::RK_Keyword
:
689 llvm_unreachable("unknown CodeCompletionResult kind");
692 // Scopes of the partial identifier we're trying to complete.
693 // It is used when we query the index for more completion results.
694 struct SpecifiedScope
{
695 // The scopes we should look in, determined by Sema.
697 // If the qualifier was fully resolved, we look for completions in these
698 // scopes; if there is an unresolved part of the qualifier, it should be
699 // resolved within these scopes.
701 // Examples of qualified completion:
704 // "using namespace std; ::vec^" => {"", "std::"}
705 // "namespace ns {using namespace std;} ns::^" => {"ns::", "std::"}
706 // "std::vec^" => {""} // "std" unresolved
708 // Examples of unqualified completion:
711 // "using namespace std; vec^" => {"", "std::"}
712 // "namespace ns {inline namespace ni { struct Foo {}}}
713 // using namespace ns::ni; Fo^ " => {"", "ns::ni::"}
714 // "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""}
716 // "" for global namespace, "ns::" for normal namespace.
717 std::vector
<std::string
> AccessibleScopes
;
718 // This is an overestimate of AccessibleScopes, e.g. it ignores inline
719 // namespaces, to fetch more relevant symbols from index.
720 std::vector
<std::string
> QueryScopes
;
721 // The full scope qualifier as typed by the user (without the leading "::").
722 // Set if the qualifier is not fully resolved by Sema.
723 std::optional
<std::string
> UnresolvedQualifier
;
725 std::optional
<std::string
> EnclosingNamespace
;
727 bool AllowAllScopes
= false;
729 // Scopes that are accessible from current context. Used for dropping
730 // unnecessary namespecifiers.
731 std::vector
<std::string
> scopesForQualification() {
732 std::set
<std::string
> Results
;
733 for (llvm::StringRef AS
: AccessibleScopes
)
735 (AS
+ (UnresolvedQualifier
? *UnresolvedQualifier
: "")).str());
736 return {Results
.begin(), Results
.end()};
739 // Construct scopes being queried in indexes. The results are deduplicated.
740 // This method formats the scopes to match the index request representation.
741 std::vector
<std::string
> scopesForIndexQuery() {
742 // The enclosing namespace must be first, it gets a quality boost.
743 std::vector
<std::string
> EnclosingAtFront
;
744 if (EnclosingNamespace
.has_value())
745 EnclosingAtFront
.push_back(*EnclosingNamespace
);
746 std::set
<std::string
> Deduplicated
;
747 for (llvm::StringRef S
: QueryScopes
)
748 if (S
!= EnclosingNamespace
)
749 Deduplicated
.insert((S
+ UnresolvedQualifier
.value_or("")).str());
751 EnclosingAtFront
.reserve(EnclosingAtFront
.size() + Deduplicated
.size());
752 llvm::copy(Deduplicated
, std::back_inserter(EnclosingAtFront
));
754 return EnclosingAtFront
;
758 // Get all scopes that will be queried in indexes and whether symbols from
759 // any scope is allowed. The first scope in the list is the preferred scope
760 // (e.g. enclosing namespace).
761 SpecifiedScope
getQueryScopes(CodeCompletionContext
&CCContext
,
763 const CompletionPrefix
&HeuristicPrefix
,
764 const CodeCompleteOptions
&Opts
) {
765 SpecifiedScope Scopes
;
766 for (auto *Context
: CCContext
.getVisitedContexts()) {
767 if (isa
<TranslationUnitDecl
>(Context
)) {
768 Scopes
.QueryScopes
.push_back("");
769 Scopes
.AccessibleScopes
.push_back("");
770 } else if (const auto *ND
= dyn_cast
<NamespaceDecl
>(Context
)) {
771 Scopes
.QueryScopes
.push_back(printNamespaceScope(*Context
));
772 Scopes
.AccessibleScopes
.push_back(printQualifiedName(*ND
) + "::");
776 const CXXScopeSpec
*SemaSpecifier
=
777 CCContext
.getCXXScopeSpecifier().value_or(nullptr);
778 // Case 1: unqualified completion.
779 if (!SemaSpecifier
) {
780 // Case 2 (exception): sema saw no qualifier, but there appears to be one!
781 // This can happen e.g. in incomplete macro expansions. Use heuristics.
782 if (!HeuristicPrefix
.Qualifier
.empty()) {
783 vlog("Sema said no scope specifier, but we saw {0} in the source code",
784 HeuristicPrefix
.Qualifier
);
785 StringRef SpelledSpecifier
= HeuristicPrefix
.Qualifier
;
786 if (SpelledSpecifier
.consume_front("::")) {
787 Scopes
.AccessibleScopes
= {""};
788 Scopes
.QueryScopes
= {""};
790 Scopes
.UnresolvedQualifier
= std::string(SpelledSpecifier
);
793 /// FIXME: When the enclosing namespace contains an inline namespace,
794 /// it's dropped here. This leads to a behavior similar to
795 /// https://github.com/clangd/clangd/issues/1451
796 Scopes
.EnclosingNamespace
= printNamespaceScope(*CCSema
.CurContext
);
797 // Allow AllScopes completion as there is no explicit scope qualifier.
798 Scopes
.AllowAllScopes
= Opts
.AllScopes
;
801 // Case 3: sema saw and resolved a scope qualifier.
802 if (SemaSpecifier
&& SemaSpecifier
->isValid())
805 // Case 4: There was a qualifier, and Sema didn't resolve it.
806 Scopes
.QueryScopes
.push_back(""); // Make sure global scope is included.
807 llvm::StringRef SpelledSpecifier
= Lexer::getSourceText(
808 CharSourceRange::getCharRange(SemaSpecifier
->getRange()),
809 CCSema
.SourceMgr
, clang::LangOptions());
810 if (SpelledSpecifier
.consume_front("::"))
811 Scopes
.QueryScopes
= {""};
812 Scopes
.UnresolvedQualifier
= std::string(SpelledSpecifier
);
813 // Sema excludes the trailing "::".
814 if (!Scopes
.UnresolvedQualifier
->empty())
815 *Scopes
.UnresolvedQualifier
+= "::";
817 Scopes
.AccessibleScopes
= Scopes
.QueryScopes
;
822 // Should we perform index-based completion in a context of the specified kind?
823 // FIXME: consider allowing completion, but restricting the result types.
824 bool contextAllowsIndex(enum CodeCompletionContext::Kind K
) {
826 case CodeCompletionContext::CCC_TopLevel
:
827 case CodeCompletionContext::CCC_ObjCInterface
:
828 case CodeCompletionContext::CCC_ObjCImplementation
:
829 case CodeCompletionContext::CCC_ObjCIvarList
:
830 case CodeCompletionContext::CCC_ClassStructUnion
:
831 case CodeCompletionContext::CCC_Statement
:
832 case CodeCompletionContext::CCC_Expression
:
833 case CodeCompletionContext::CCC_ObjCMessageReceiver
:
834 case CodeCompletionContext::CCC_EnumTag
:
835 case CodeCompletionContext::CCC_UnionTag
:
836 case CodeCompletionContext::CCC_ClassOrStructTag
:
837 case CodeCompletionContext::CCC_ObjCProtocolName
:
838 case CodeCompletionContext::CCC_Namespace
:
839 case CodeCompletionContext::CCC_Type
:
840 case CodeCompletionContext::CCC_ParenthesizedExpression
:
841 case CodeCompletionContext::CCC_ObjCInterfaceName
:
842 case CodeCompletionContext::CCC_Symbol
:
843 case CodeCompletionContext::CCC_SymbolOrNewName
:
844 case CodeCompletionContext::CCC_ObjCClassForwardDecl
:
845 case CodeCompletionContext::CCC_TopLevelOrExpression
:
847 case CodeCompletionContext::CCC_OtherWithMacros
:
848 case CodeCompletionContext::CCC_DotMemberAccess
:
849 case CodeCompletionContext::CCC_ArrowMemberAccess
:
850 case CodeCompletionContext::CCC_ObjCCategoryName
:
851 case CodeCompletionContext::CCC_ObjCPropertyAccess
:
852 case CodeCompletionContext::CCC_MacroName
:
853 case CodeCompletionContext::CCC_MacroNameUse
:
854 case CodeCompletionContext::CCC_PreprocessorExpression
:
855 case CodeCompletionContext::CCC_PreprocessorDirective
:
856 case CodeCompletionContext::CCC_SelectorName
:
857 case CodeCompletionContext::CCC_TypeQualifiers
:
858 case CodeCompletionContext::CCC_ObjCInstanceMessage
:
859 case CodeCompletionContext::CCC_ObjCClassMessage
:
860 case CodeCompletionContext::CCC_IncludedFile
:
861 case CodeCompletionContext::CCC_Attribute
:
862 // FIXME: Provide identifier based completions for the following contexts:
863 case CodeCompletionContext::CCC_Other
: // Be conservative.
864 case CodeCompletionContext::CCC_NaturalLanguage
:
865 case CodeCompletionContext::CCC_Recovery
:
866 case CodeCompletionContext::CCC_NewName
:
869 llvm_unreachable("unknown code completion context");
872 static bool isInjectedClass(const NamedDecl
&D
) {
873 if (auto *R
= dyn_cast_or_null
<RecordDecl
>(&D
))
874 if (R
->isInjectedClassName())
879 // Some member calls are excluded because they're so rarely useful.
880 static bool isExcludedMember(const NamedDecl
&D
) {
881 // Destructor completion is rarely useful, and works inconsistently.
882 // (s.^ completes ~string, but s.~st^ is an error).
883 if (D
.getKind() == Decl::CXXDestructor
)
885 // Injected name may be useful for A::foo(), but who writes A::A::foo()?
886 if (isInjectedClass(D
))
888 // Explicit calls to operators are also rare.
889 auto NameKind
= D
.getDeclName().getNameKind();
890 if (NameKind
== DeclarationName::CXXOperatorName
||
891 NameKind
== DeclarationName::CXXLiteralOperatorName
||
892 NameKind
== DeclarationName::CXXConversionFunctionName
)
897 // The CompletionRecorder captures Sema code-complete output, including context.
898 // It filters out ignored results (but doesn't apply fuzzy-filtering yet).
899 // It doesn't do scoring or conversion to CompletionItem yet, as we want to
900 // merge with index results first.
901 // Generally the fields and methods of this object should only be used from
902 // within the callback.
903 struct CompletionRecorder
: public CodeCompleteConsumer
{
904 CompletionRecorder(const CodeCompleteOptions
&Opts
,
905 llvm::unique_function
<void()> ResultsCallback
)
906 : CodeCompleteConsumer(Opts
.getClangCompleteOpts()),
907 CCContext(CodeCompletionContext::CCC_Other
), Opts(Opts
),
908 CCAllocator(std::make_shared
<GlobalCodeCompletionAllocator
>()),
909 CCTUInfo(CCAllocator
), ResultsCallback(std::move(ResultsCallback
)) {
910 assert(this->ResultsCallback
);
913 std::vector
<CodeCompletionResult
> Results
;
914 CodeCompletionContext CCContext
;
915 Sema
*CCSema
= nullptr; // Sema that created the results.
916 // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead?
918 void ProcessCodeCompleteResults(class Sema
&S
, CodeCompletionContext Context
,
919 CodeCompletionResult
*InResults
,
920 unsigned NumResults
) final
{
921 // Results from recovery mode are generally useless, and the callback after
922 // recovery (if any) is usually more interesting. To make sure we handle the
923 // future callback from sema, we just ignore all callbacks in recovery mode,
924 // as taking only results from recovery mode results in poor completion
926 // FIXME: in case there is no future sema completion callback after the
927 // recovery mode, we might still want to provide some results (e.g. trivial
928 // identifier-based completion).
929 if (Context
.getKind() == CodeCompletionContext::CCC_Recovery
) {
930 log("Code complete: Ignoring sema code complete callback with Recovery "
934 // If a callback is called without any sema result and the context does not
935 // support index-based completion, we simply skip it to give way to
936 // potential future callbacks with results.
937 if (NumResults
== 0 && !contextAllowsIndex(Context
.getKind()))
940 log("Multiple code complete callbacks (parser backtracked?). "
941 "Dropping results from context {0}, keeping results from {1}.",
942 getCompletionKindString(Context
.getKind()),
943 getCompletionKindString(this->CCContext
.getKind()));
946 // Record the completion context.
950 // Retain the results we might want.
951 for (unsigned I
= 0; I
< NumResults
; ++I
) {
952 auto &Result
= InResults
[I
];
953 // Class members that are shadowed by subclasses are usually noise.
954 if (Result
.Hidden
&& Result
.Declaration
&&
955 Result
.Declaration
->isCXXClassMember())
957 if (!Opts
.IncludeIneligibleResults
&&
958 (Result
.Availability
== CXAvailability_NotAvailable
||
959 Result
.Availability
== CXAvailability_NotAccessible
))
961 if (Result
.Declaration
&&
962 !Context
.getBaseType().isNull() // is this a member-access context?
963 && isExcludedMember(*Result
.Declaration
))
965 // Skip injected class name when no class scope is not explicitly set.
966 // E.g. show injected A::A in `using A::A^` but not in "A^".
967 if (Result
.Declaration
&& !Context
.getCXXScopeSpecifier() &&
968 isInjectedClass(*Result
.Declaration
))
970 // We choose to never append '::' to completion results in clangd.
971 Result
.StartsNestedNameSpecifier
= false;
972 Results
.push_back(Result
);
977 CodeCompletionAllocator
&getAllocator() override
{ return *CCAllocator
; }
978 CodeCompletionTUInfo
&getCodeCompletionTUInfo() override
{ return CCTUInfo
; }
980 // Returns the filtering/sorting name for Result, which must be from Results.
981 // Returned string is owned by this recorder (or the AST).
982 llvm::StringRef
getName(const CodeCompletionResult
&Result
) {
983 switch (Result
.Kind
) {
984 case CodeCompletionResult::RK_Declaration
:
985 if (auto *ID
= Result
.Declaration
->getIdentifier())
986 return ID
->getName();
988 case CodeCompletionResult::RK_Keyword
:
989 return Result
.Keyword
;
990 case CodeCompletionResult::RK_Macro
:
991 return Result
.Macro
->getName();
992 case CodeCompletionResult::RK_Pattern
:
995 auto *CCS
= codeCompletionString(Result
);
996 const CodeCompletionString::Chunk
*OnlyText
= nullptr;
997 for (auto &C
: *CCS
) {
998 if (C
.Kind
!= CodeCompletionString::CK_TypedText
)
1001 return CCAllocator
->CopyString(CCS
->getAllTypedText());
1004 return OnlyText
? OnlyText
->Text
: llvm::StringRef();
1007 // Build a CodeCompletion string for R, which must be from Results.
1008 // The CCS will be owned by this recorder.
1009 CodeCompletionString
*codeCompletionString(const CodeCompletionResult
&R
) {
1010 // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway.
1011 return const_cast<CodeCompletionResult
&>(R
).CreateCodeCompletionString(
1012 *CCSema
, CCContext
, *CCAllocator
, CCTUInfo
,
1013 /*IncludeBriefComments=*/false);
1017 CodeCompleteOptions Opts
;
1018 std::shared_ptr
<GlobalCodeCompletionAllocator
> CCAllocator
;
1019 CodeCompletionTUInfo CCTUInfo
;
1020 llvm::unique_function
<void()> ResultsCallback
;
1023 struct ScoredSignature
{
1024 // When not null, requires documentation to be requested from the index with
1027 SignatureInformation Signature
;
1028 SignatureQualitySignals Quality
;
1031 // Returns the index of the parameter matching argument number "Arg.
1032 // This is usually just "Arg", except for variadic functions/templates, where
1033 // "Arg" might be higher than the number of parameters. When that happens, we
1034 // assume the last parameter is variadic and assume all further args are
1036 int paramIndexForArg(const CodeCompleteConsumer::OverloadCandidate
&Candidate
,
1038 int NumParams
= Candidate
.getNumParams();
1039 if (auto *T
= Candidate
.getFunctionType()) {
1040 if (auto *Proto
= T
->getAs
<FunctionProtoType
>()) {
1041 if (Proto
->isVariadic())
1045 return std::min(Arg
, std::max(NumParams
- 1, 0));
1048 class SignatureHelpCollector final
: public CodeCompleteConsumer
{
1050 SignatureHelpCollector(const clang::CodeCompleteOptions
&CodeCompleteOpts
,
1051 MarkupKind DocumentationFormat
,
1052 const SymbolIndex
*Index
, SignatureHelp
&SigHelp
)
1053 : CodeCompleteConsumer(CodeCompleteOpts
), SigHelp(SigHelp
),
1054 Allocator(std::make_shared
<clang::GlobalCodeCompletionAllocator
>()),
1055 CCTUInfo(Allocator
), Index(Index
),
1056 DocumentationFormat(DocumentationFormat
) {}
1058 void ProcessOverloadCandidates(Sema
&S
, unsigned CurrentArg
,
1059 OverloadCandidate
*Candidates
,
1060 unsigned NumCandidates
,
1061 SourceLocation OpenParLoc
,
1062 bool Braced
) override
{
1063 assert(!OpenParLoc
.isInvalid());
1064 SourceManager
&SrcMgr
= S
.getSourceManager();
1065 OpenParLoc
= SrcMgr
.getFileLoc(OpenParLoc
);
1066 if (SrcMgr
.isInMainFile(OpenParLoc
))
1067 SigHelp
.argListStart
= sourceLocToPosition(SrcMgr
, OpenParLoc
);
1069 elog("Location oustide main file in signature help: {0}",
1070 OpenParLoc
.printToString(SrcMgr
));
1072 std::vector
<ScoredSignature
> ScoredSignatures
;
1073 SigHelp
.signatures
.reserve(NumCandidates
);
1074 ScoredSignatures
.reserve(NumCandidates
);
1075 // FIXME(rwols): How can we determine the "active overload candidate"?
1076 // Right now the overloaded candidates seem to be provided in a "best fit"
1077 // order, so I'm not too worried about this.
1078 SigHelp
.activeSignature
= 0;
1079 assert(CurrentArg
<= (unsigned)std::numeric_limits
<int>::max() &&
1080 "too many arguments");
1082 SigHelp
.activeParameter
= static_cast<int>(CurrentArg
);
1084 for (unsigned I
= 0; I
< NumCandidates
; ++I
) {
1085 OverloadCandidate Candidate
= Candidates
[I
];
1086 // We want to avoid showing instantiated signatures, because they may be
1087 // long in some cases (e.g. when 'T' is substituted with 'std::string', we
1088 // would get 'std::basic_string<char>').
1089 if (auto *Func
= Candidate
.getFunction()) {
1090 if (auto *Pattern
= Func
->getTemplateInstantiationPattern())
1091 Candidate
= OverloadCandidate(Pattern
);
1093 if (static_cast<int>(I
) == SigHelp
.activeSignature
) {
1094 // The activeParameter in LSP relates to the activeSignature. There is
1095 // another, per-signature field, but we currently do not use it and not
1096 // all clients might support it.
1097 // FIXME: Add support for per-signature activeParameter field.
1098 SigHelp
.activeParameter
=
1099 paramIndexForArg(Candidate
, SigHelp
.activeParameter
);
1102 const auto *CCS
= Candidate
.CreateSignatureString(
1103 CurrentArg
, S
, *Allocator
, CCTUInfo
,
1104 /*IncludeBriefComments=*/true, Braced
);
1105 assert(CCS
&& "Expected the CodeCompletionString to be non-null");
1106 ScoredSignatures
.push_back(processOverloadCandidate(
1108 Candidate
.getFunction()
1109 ? getDeclComment(S
.getASTContext(), *Candidate
.getFunction())
1113 // Sema does not load the docs from the preamble, so we need to fetch extra
1114 // docs from the index instead.
1115 llvm::DenseMap
<SymbolID
, std::string
> FetchedDocs
;
1117 LookupRequest IndexRequest
;
1118 for (const auto &S
: ScoredSignatures
) {
1121 IndexRequest
.IDs
.insert(S
.IDForDoc
);
1123 Index
->lookup(IndexRequest
, [&](const Symbol
&S
) {
1124 if (!S
.Documentation
.empty())
1125 FetchedDocs
[S
.ID
] = std::string(S
.Documentation
);
1127 vlog("SigHelp: requested docs for {0} symbols from the index, got {1} "
1128 "symbols with non-empty docs in the response",
1129 IndexRequest
.IDs
.size(), FetchedDocs
.size());
1132 llvm::sort(ScoredSignatures
, [](const ScoredSignature
&L
,
1133 const ScoredSignature
&R
) {
1134 // Ordering follows:
1135 // - Less number of parameters is better.
1136 // - Aggregate > Function > FunctionType > FunctionTemplate
1137 // - High score is better.
1138 // - Shorter signature is better.
1139 // - Alphabetically smaller is better.
1140 if (L
.Quality
.NumberOfParameters
!= R
.Quality
.NumberOfParameters
)
1141 return L
.Quality
.NumberOfParameters
< R
.Quality
.NumberOfParameters
;
1142 if (L
.Quality
.NumberOfOptionalParameters
!=
1143 R
.Quality
.NumberOfOptionalParameters
)
1144 return L
.Quality
.NumberOfOptionalParameters
<
1145 R
.Quality
.NumberOfOptionalParameters
;
1146 if (L
.Quality
.Kind
!= R
.Quality
.Kind
) {
1147 using OC
= CodeCompleteConsumer::OverloadCandidate
;
1148 auto KindPriority
= [&](OC::CandidateKind K
) {
1150 case OC::CK_Aggregate
:
1152 case OC::CK_Function
:
1154 case OC::CK_FunctionType
:
1156 case OC::CK_FunctionProtoTypeLoc
:
1158 case OC::CK_FunctionTemplate
:
1160 case OC::CK_Template
:
1163 llvm_unreachable("Unknown overload candidate type.");
1165 return KindPriority(L
.Quality
.Kind
) < KindPriority(R
.Quality
.Kind
);
1167 if (L
.Signature
.label
.size() != R
.Signature
.label
.size())
1168 return L
.Signature
.label
.size() < R
.Signature
.label
.size();
1169 return L
.Signature
.label
< R
.Signature
.label
;
1172 for (auto &SS
: ScoredSignatures
) {
1174 SS
.IDForDoc
? FetchedDocs
.find(SS
.IDForDoc
) : FetchedDocs
.end();
1175 if (IndexDocIt
!= FetchedDocs
.end()) {
1176 markup::Document SignatureComment
;
1177 parseDocumentation(IndexDocIt
->second
, SignatureComment
);
1178 SS
.Signature
.documentation
=
1179 renderDoc(SignatureComment
, DocumentationFormat
);
1182 SigHelp
.signatures
.push_back(std::move(SS
.Signature
));
1186 GlobalCodeCompletionAllocator
&getAllocator() override
{ return *Allocator
; }
1188 CodeCompletionTUInfo
&getCodeCompletionTUInfo() override
{ return CCTUInfo
; }
1191 void processParameterChunk(llvm::StringRef ChunkText
,
1192 SignatureInformation
&Signature
) const {
1193 // (!) this is O(n), should still be fast compared to building ASTs.
1194 unsigned ParamStartOffset
= lspLength(Signature
.label
);
1195 unsigned ParamEndOffset
= ParamStartOffset
+ lspLength(ChunkText
);
1196 // A piece of text that describes the parameter that corresponds to
1197 // the code-completion location within a function call, message send,
1198 // macro invocation, etc.
1199 Signature
.label
+= ChunkText
;
1200 ParameterInformation Info
;
1201 Info
.labelOffsets
.emplace(ParamStartOffset
, ParamEndOffset
);
1202 // FIXME: only set 'labelOffsets' when all clients migrate out of it.
1203 Info
.labelString
= std::string(ChunkText
);
1205 Signature
.parameters
.push_back(std::move(Info
));
1208 void processOptionalChunk(const CodeCompletionString
&CCS
,
1209 SignatureInformation
&Signature
,
1210 SignatureQualitySignals
&Signal
) const {
1211 for (const auto &Chunk
: CCS
) {
1212 switch (Chunk
.Kind
) {
1213 case CodeCompletionString::CK_Optional
:
1214 assert(Chunk
.Optional
&&
1215 "Expected the optional code completion string to be non-null.");
1216 processOptionalChunk(*Chunk
.Optional
, Signature
, Signal
);
1218 case CodeCompletionString::CK_VerticalSpace
:
1220 case CodeCompletionString::CK_CurrentParameter
:
1221 case CodeCompletionString::CK_Placeholder
:
1222 processParameterChunk(Chunk
.Text
, Signature
);
1223 Signal
.NumberOfOptionalParameters
++;
1226 Signature
.label
+= Chunk
.Text
;
1232 // FIXME(ioeric): consider moving CodeCompletionString logic here to
1233 // CompletionString.h.
1234 ScoredSignature
processOverloadCandidate(const OverloadCandidate
&Candidate
,
1235 const CodeCompletionString
&CCS
,
1236 llvm::StringRef DocComment
) const {
1237 SignatureInformation Signature
;
1238 SignatureQualitySignals Signal
;
1239 const char *ReturnType
= nullptr;
1241 markup::Document OverloadComment
;
1242 parseDocumentation(formatDocumentation(CCS
, DocComment
), OverloadComment
);
1243 Signature
.documentation
= renderDoc(OverloadComment
, DocumentationFormat
);
1244 Signal
.Kind
= Candidate
.getKind();
1246 for (const auto &Chunk
: CCS
) {
1247 switch (Chunk
.Kind
) {
1248 case CodeCompletionString::CK_ResultType
:
1249 // A piece of text that describes the type of an entity or,
1250 // for functions and methods, the return type.
1251 assert(!ReturnType
&& "Unexpected CK_ResultType");
1252 ReturnType
= Chunk
.Text
;
1254 case CodeCompletionString::CK_CurrentParameter
:
1255 case CodeCompletionString::CK_Placeholder
:
1256 processParameterChunk(Chunk
.Text
, Signature
);
1257 Signal
.NumberOfParameters
++;
1259 case CodeCompletionString::CK_Optional
: {
1260 // The rest of the parameters are defaulted/optional.
1261 assert(Chunk
.Optional
&&
1262 "Expected the optional code completion string to be non-null.");
1263 processOptionalChunk(*Chunk
.Optional
, Signature
, Signal
);
1266 case CodeCompletionString::CK_VerticalSpace
:
1269 Signature
.label
+= Chunk
.Text
;
1274 Signature
.label
+= " -> ";
1275 Signature
.label
+= ReturnType
;
1277 dlog("Signal for {0}: {1}", Signature
, Signal
);
1278 ScoredSignature Result
;
1279 Result
.Signature
= std::move(Signature
);
1280 Result
.Quality
= Signal
;
1281 const FunctionDecl
*Func
= Candidate
.getFunction();
1282 if (Func
&& Result
.Signature
.documentation
.value
.empty()) {
1283 // Computing USR caches linkage, which may change after code completion.
1284 if (!hasUnstableLinkage(Func
))
1285 Result
.IDForDoc
= clangd::getSymbolID(Func
);
1290 SignatureHelp
&SigHelp
;
1291 std::shared_ptr
<clang::GlobalCodeCompletionAllocator
> Allocator
;
1292 CodeCompletionTUInfo CCTUInfo
;
1293 const SymbolIndex
*Index
;
1294 MarkupKind DocumentationFormat
;
1295 }; // SignatureHelpCollector
1297 // Used only for completion of C-style comments in function call (i.e.
1298 // /*foo=*/7). Similar to SignatureHelpCollector, but needs to do less work.
1299 class ParamNameCollector final
: public CodeCompleteConsumer
{
1301 ParamNameCollector(const clang::CodeCompleteOptions
&CodeCompleteOpts
,
1302 std::set
<std::string
> &ParamNames
)
1303 : CodeCompleteConsumer(CodeCompleteOpts
),
1304 Allocator(std::make_shared
<clang::GlobalCodeCompletionAllocator
>()),
1305 CCTUInfo(Allocator
), ParamNames(ParamNames
) {}
1307 void ProcessOverloadCandidates(Sema
&S
, unsigned CurrentArg
,
1308 OverloadCandidate
*Candidates
,
1309 unsigned NumCandidates
,
1310 SourceLocation OpenParLoc
,
1311 bool Braced
) override
{
1312 assert(CurrentArg
<= (unsigned)std::numeric_limits
<int>::max() &&
1313 "too many arguments");
1315 for (unsigned I
= 0; I
< NumCandidates
; ++I
) {
1316 if (const NamedDecl
*ND
= Candidates
[I
].getParamDecl(CurrentArg
))
1317 if (const auto *II
= ND
->getIdentifier())
1318 ParamNames
.emplace(II
->getName());
1323 GlobalCodeCompletionAllocator
&getAllocator() override
{ return *Allocator
; }
1325 CodeCompletionTUInfo
&getCodeCompletionTUInfo() override
{ return CCTUInfo
; }
1327 std::shared_ptr
<clang::GlobalCodeCompletionAllocator
> Allocator
;
1328 CodeCompletionTUInfo CCTUInfo
;
1329 std::set
<std::string
> &ParamNames
;
1332 struct SemaCompleteInput
{
1335 const PreambleData
&Preamble
;
1336 const std::optional
<PreamblePatch
> Patch
;
1337 const ParseInputs
&ParseInput
;
1340 void loadMainFilePreambleMacros(const Preprocessor
&PP
,
1341 const PreambleData
&Preamble
) {
1342 // The ExternalPreprocessorSource has our macros, if we know where to look.
1343 // We can read all the macros using PreambleMacros->ReadDefinedMacros(),
1344 // but this includes transitively included files, so may deserialize a lot.
1345 ExternalPreprocessorSource
*PreambleMacros
= PP
.getExternalSource();
1346 // As we have the names of the macros, we can look up their IdentifierInfo
1347 // and then use this to load just the macros we want.
1348 const auto &ITable
= PP
.getIdentifierTable();
1349 IdentifierInfoLookup
*PreambleIdentifiers
=
1350 ITable
.getExternalIdentifierLookup();
1352 if (!PreambleIdentifiers
|| !PreambleMacros
)
1354 for (const auto &MacroName
: Preamble
.Macros
.Names
) {
1355 if (ITable
.find(MacroName
.getKey()) != ITable
.end())
1357 if (auto *II
= PreambleIdentifiers
->get(MacroName
.getKey()))
1358 if (II
->isOutOfDate())
1359 PreambleMacros
->updateOutOfDateIdentifier(*II
);
1363 // Invokes Sema code completion on a file.
1364 // If \p Includes is set, it will be updated based on the compiler invocation.
1365 bool semaCodeComplete(std::unique_ptr
<CodeCompleteConsumer
> Consumer
,
1366 const clang::CodeCompleteOptions
&Options
,
1367 const SemaCompleteInput
&Input
,
1368 IncludeStructure
*Includes
= nullptr) {
1369 trace::Span
Tracer("Sema completion");
1371 IgnoreDiagnostics IgnoreDiags
;
1372 auto CI
= buildCompilerInvocation(Input
.ParseInput
, IgnoreDiags
);
1374 elog("Couldn't create CompilerInvocation");
1377 auto &FrontendOpts
= CI
->getFrontendOpts();
1378 FrontendOpts
.SkipFunctionBodies
= true;
1379 // Disable typo correction in Sema.
1380 CI
->getLangOpts().SpellChecking
= false;
1381 // Code completion won't trigger in delayed template bodies.
1382 // This is on-by-default in windows to allow parsing SDK headers; we're only
1383 // disabling it for the main-file (not preamble).
1384 CI
->getLangOpts().DelayedTemplateParsing
= false;
1385 // Setup code completion.
1386 FrontendOpts
.CodeCompleteOpts
= Options
;
1387 FrontendOpts
.CodeCompletionAt
.FileName
= std::string(Input
.FileName
);
1388 std::tie(FrontendOpts
.CodeCompletionAt
.Line
,
1389 FrontendOpts
.CodeCompletionAt
.Column
) =
1390 offsetToClangLineColumn(Input
.ParseInput
.Contents
, Input
.Offset
);
1392 std::unique_ptr
<llvm::MemoryBuffer
> ContentsBuffer
=
1393 llvm::MemoryBuffer::getMemBuffer(Input
.ParseInput
.Contents
,
1395 // The diagnostic options must be set before creating a CompilerInstance.
1396 CI
->getDiagnosticOpts().IgnoreWarnings
= true;
1397 // We reuse the preamble whether it's valid or not. This is a
1398 // correctness/performance tradeoff: building without a preamble is slow, and
1399 // completion is latency-sensitive.
1400 // However, if we're completing *inside* the preamble section of the draft,
1401 // overriding the preamble will break sema completion. Fortunately we can just
1402 // skip all includes in this case; these completions are really simple.
1403 PreambleBounds PreambleRegion
=
1404 ComputePreambleBounds(CI
->getLangOpts(), *ContentsBuffer
, 0);
1405 bool CompletingInPreamble
= Input
.Offset
< PreambleRegion
.Size
||
1406 (!PreambleRegion
.PreambleEndsAtStartOfLine
&&
1407 Input
.Offset
== PreambleRegion
.Size
);
1409 Input
.Patch
->apply(*CI
);
1410 // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
1411 // the remapped buffers do not get freed.
1412 llvm::IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> VFS
=
1413 Input
.ParseInput
.TFS
->view(Input
.ParseInput
.CompileCommand
.Directory
);
1414 if (Input
.Preamble
.StatCache
)
1415 VFS
= Input
.Preamble
.StatCache
->getConsumingFS(std::move(VFS
));
1416 auto Clang
= prepareCompilerInstance(
1417 std::move(CI
), !CompletingInPreamble
? &Input
.Preamble
.Preamble
: nullptr,
1418 std::move(ContentsBuffer
), std::move(VFS
), IgnoreDiags
);
1419 Clang
->getPreprocessorOpts().SingleFileParseMode
= CompletingInPreamble
;
1420 Clang
->setCodeCompletionConsumer(Consumer
.release());
1422 if (Input
.Preamble
.RequiredModules
)
1423 Input
.Preamble
.RequiredModules
->adjustHeaderSearchOptions(Clang
->getHeaderSearchOpts());
1425 SyntaxOnlyAction Action
;
1426 if (!Action
.BeginSourceFile(*Clang
, Clang
->getFrontendOpts().Inputs
[0])) {
1427 log("BeginSourceFile() failed when running codeComplete for {0}",
1431 // Macros can be defined within the preamble region of the main file.
1432 // They don't fall nicely into our index/Sema dichotomy:
1433 // - they're not indexed for completion (they're not available across files)
1434 // - but Sema code complete won't see them: as part of the preamble, they're
1435 // deserialized only when mentioned.
1436 // Force them to be deserialized so SemaCodeComplete sees them.
1437 loadMainFilePreambleMacros(Clang
->getPreprocessor(), Input
.Preamble
);
1439 Includes
->collect(*Clang
);
1440 if (llvm::Error Err
= Action
.Execute()) {
1441 log("Execute() failed when running codeComplete for {0}: {1}",
1442 Input
.FileName
, toString(std::move(Err
)));
1445 Action
.EndSourceFile();
1450 // Should we allow index completions in the specified context?
1451 bool allowIndex(CodeCompletionContext
&CC
) {
1452 if (!contextAllowsIndex(CC
.getKind()))
1454 // We also avoid ClassName::bar (but allow namespace::bar).
1455 auto Scope
= CC
.getCXXScopeSpecifier();
1458 NestedNameSpecifier
*NameSpec
= (*Scope
)->getScopeRep();
1461 // We only query the index when qualifier is a namespace.
1462 // If it's a class, we rely solely on sema completions.
1463 switch (NameSpec
->getKind()) {
1464 case NestedNameSpecifier::Global
:
1465 case NestedNameSpecifier::Namespace
:
1466 case NestedNameSpecifier::NamespaceAlias
:
1468 case NestedNameSpecifier::Super
:
1469 case NestedNameSpecifier::TypeSpec
:
1470 case NestedNameSpecifier::TypeSpecWithTemplate
:
1471 // Unresolved inside a template.
1472 case NestedNameSpecifier::Identifier
:
1475 llvm_unreachable("invalid NestedNameSpecifier kind");
1478 // Should we include a symbol from the index given the completion kind?
1479 // FIXME: Ideally we can filter in the fuzzy find request itself.
1480 bool includeSymbolFromIndex(CodeCompletionContext::Kind Kind
,
1481 const Symbol
&Sym
) {
1482 // Objective-C protocols are only useful in ObjC protocol completions,
1483 // in other places they're confusing, especially when they share the same
1484 // identifier with a class.
1485 if (Sym
.SymInfo
.Kind
== index::SymbolKind::Protocol
&&
1486 Sym
.SymInfo
.Lang
== index::SymbolLanguage::ObjC
)
1487 return Kind
== CodeCompletionContext::CCC_ObjCProtocolName
;
1488 else if (Kind
== CodeCompletionContext::CCC_ObjCProtocolName
)
1489 // Don't show anything else in ObjC protocol completions.
1492 if (Kind
== CodeCompletionContext::CCC_ObjCClassForwardDecl
)
1493 return Sym
.SymInfo
.Kind
== index::SymbolKind::Class
&&
1494 Sym
.SymInfo
.Lang
== index::SymbolLanguage::ObjC
;
1498 std::future
<std::pair
<bool, SymbolSlab
>>
1499 startAsyncFuzzyFind(const SymbolIndex
&Index
, const FuzzyFindRequest
&Req
) {
1500 return runAsync
<std::pair
<bool, SymbolSlab
>>([&Index
, Req
]() {
1501 trace::Span
Tracer("Async fuzzyFind");
1502 SymbolSlab::Builder Syms
;
1504 Index
.fuzzyFind(Req
, [&Syms
](const Symbol
&Sym
) { Syms
.insert(Sym
); });
1505 return std::make_pair(Incomplete
, std::move(Syms
).build());
1509 // Creates a `FuzzyFindRequest` based on the cached index request from the
1510 // last completion, if any, and the speculated completion filter text in the
1512 FuzzyFindRequest
speculativeFuzzyFindRequestForCompletion(
1513 FuzzyFindRequest CachedReq
, const CompletionPrefix
&HeuristicPrefix
) {
1514 CachedReq
.Query
= std::string(HeuristicPrefix
.Name
);
1518 // This function is similar to Lexer::findNextToken(), but assumes
1519 // that the input SourceLocation is the completion point (which is
1520 // a case findNextToken() does not handle).
1521 std::optional
<Token
>
1522 findTokenAfterCompletionPoint(SourceLocation CompletionPoint
,
1523 const SourceManager
&SM
,
1524 const LangOptions
&LangOpts
) {
1525 SourceLocation Loc
= CompletionPoint
;
1526 if (Loc
.isMacroID()) {
1527 if (!Lexer::isAtEndOfMacroExpansion(Loc
, SM
, LangOpts
, &Loc
))
1528 return std::nullopt
;
1531 // Advance to the next SourceLocation after the completion point.
1532 // Lexer::findNextToken() would call MeasureTokenLength() here,
1533 // which does not handle the completion point (and can't, because
1534 // the Lexer instance it constructs internally doesn't have a
1535 // Preprocessor and so doesn't know about the completion point).
1536 Loc
= Loc
.getLocWithOffset(1);
1538 // Break down the source location.
1539 std::pair
<FileID
, unsigned> LocInfo
= SM
.getDecomposedLoc(Loc
);
1541 // Try to load the file buffer.
1542 bool InvalidTemp
= false;
1543 StringRef File
= SM
.getBufferData(LocInfo
.first
, &InvalidTemp
);
1545 return std::nullopt
;
1547 const char *TokenBegin
= File
.data() + LocInfo
.second
;
1549 // Lex from the start of the given location.
1550 Lexer
TheLexer(SM
.getLocForStartOfFile(LocInfo
.first
), LangOpts
, File
.begin(),
1551 TokenBegin
, File
.end());
1554 TheLexer
.LexFromRawLexer(Tok
);
1558 // Runs Sema-based (AST) and Index-based completion, returns merged results.
1560 // There are a few tricky considerations:
1561 // - the AST provides information needed for the index query (e.g. which
1562 // namespaces to search in). So Sema must start first.
1563 // - we only want to return the top results (Opts.Limit).
1564 // Building CompletionItems for everything else is wasteful, so we want to
1565 // preserve the "native" format until we're done with scoring.
1566 // - the data underlying Sema completion items is owned by the AST and various
1567 // other arenas, which must stay alive for us to build CompletionItems.
1568 // - we may get duplicate results from Sema and the Index, we need to merge.
1570 // So we start Sema completion first, and do all our work in its callback.
1571 // We use the Sema context information to query the index.
1572 // Then we merge the two result sets, producing items that are Sema/Index/Both.
1573 // These items are scored, and the top N are synthesized into the LSP response.
1574 // Finally, we can clean up the data structures created by Sema completion.
1576 // Main collaborators are:
1577 // - semaCodeComplete sets up the compiler machinery to run code completion.
1578 // - CompletionRecorder captures Sema completion results, including context.
1579 // - SymbolIndex (Opts.Index) provides index completion results as Symbols
1580 // - CompletionCandidates are the result of merging Sema and Index results.
1581 // Each candidate points to an underlying CodeCompletionResult (Sema), a
1582 // Symbol (Index), or both. It computes the result quality score.
1583 // CompletionCandidate also does conversion to CompletionItem (at the end).
1584 // - FuzzyMatcher scores how the candidate matches the partial identifier.
1585 // This score is combined with the result quality score for the final score.
1586 // - TopN determines the results with the best score.
1587 class CodeCompleteFlow
{
1589 IncludeStructure Includes
; // Complete once the compiler runs.
1590 SpeculativeFuzzyFind
*SpecFuzzyFind
; // Can be nullptr.
1591 const CodeCompleteOptions
&Opts
;
1593 // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup.
1594 CompletionRecorder
*Recorder
= nullptr;
1595 CodeCompletionContext::Kind CCContextKind
= CodeCompletionContext::CCC_Other
;
1596 bool IsUsingDeclaration
= false;
1597 // The snippets will not be generated if the token following completion
1598 // location is an opening parenthesis (tok::l_paren) because this would add
1599 // extra parenthesis.
1600 tok::TokenKind NextTokenKind
= tok::eof
;
1601 // Counters for logging.
1602 int NSema
= 0, NIndex
= 0, NSemaAndIndex
= 0, NIdent
= 0;
1603 bool Incomplete
= false; // Would more be available with a higher limit?
1604 CompletionPrefix HeuristicPrefix
;
1605 std::optional
<FuzzyMatcher
> Filter
; // Initialized once Sema runs.
1606 Range ReplacedRange
;
1607 std::vector
<std::string
> QueryScopes
; // Initialized once Sema runs.
1608 std::vector
<std::string
> AccessibleScopes
; // Initialized once Sema runs.
1609 // Initialized once QueryScopes is initialized, if there are scopes.
1610 std::optional
<ScopeDistance
> ScopeProximity
;
1611 std::optional
<OpaqueType
> PreferredType
; // Initialized once Sema runs.
1612 // Whether to query symbols from any scope. Initialized once Sema runs.
1613 bool AllScopes
= false;
1614 llvm::StringSet
<> ContextWords
;
1615 // Include-insertion and proximity scoring rely on the include structure.
1616 // This is available after Sema has run.
1617 std::optional
<IncludeInserter
> Inserter
; // Available during runWithSema.
1618 std::optional
<URIDistance
> FileProximity
; // Initialized once Sema runs.
1619 /// Speculative request based on the cached request and the filter text before
1621 /// Initialized right before sema run. This is only set if `SpecFuzzyFind` is
1622 /// set and contains a cached request.
1623 std::optional
<FuzzyFindRequest
> SpecReq
;
1626 // A CodeCompleteFlow object is only useful for calling run() exactly once.
1627 CodeCompleteFlow(PathRef FileName
, const IncludeStructure
&Includes
,
1628 SpeculativeFuzzyFind
*SpecFuzzyFind
,
1629 const CodeCompleteOptions
&Opts
)
1630 : FileName(FileName
), Includes(Includes
), SpecFuzzyFind(SpecFuzzyFind
),
1633 CodeCompleteResult
run(const SemaCompleteInput
&SemaCCInput
) && {
1634 trace::Span
Tracer("CodeCompleteFlow");
1635 HeuristicPrefix
= guessCompletionPrefix(SemaCCInput
.ParseInput
.Contents
,
1636 SemaCCInput
.Offset
);
1637 populateContextWords(SemaCCInput
.ParseInput
.Contents
);
1638 if (Opts
.Index
&& SpecFuzzyFind
&& SpecFuzzyFind
->CachedReq
) {
1639 assert(!SpecFuzzyFind
->Result
.valid());
1640 SpecReq
= speculativeFuzzyFindRequestForCompletion(
1641 *SpecFuzzyFind
->CachedReq
, HeuristicPrefix
);
1642 SpecFuzzyFind
->Result
= startAsyncFuzzyFind(*Opts
.Index
, *SpecReq
);
1645 // We run Sema code completion first. It builds an AST and calculates:
1646 // - completion results based on the AST.
1647 // - partial identifier and context. We need these for the index query.
1648 CodeCompleteResult Output
;
1649 auto RecorderOwner
= std::make_unique
<CompletionRecorder
>(Opts
, [&]() {
1650 assert(Recorder
&& "Recorder is not set");
1651 CCContextKind
= Recorder
->CCContext
.getKind();
1652 IsUsingDeclaration
= Recorder
->CCContext
.isUsingDeclaration();
1653 auto Style
= getFormatStyleForFile(SemaCCInput
.FileName
,
1654 SemaCCInput
.ParseInput
.Contents
,
1655 *SemaCCInput
.ParseInput
.TFS
, false);
1656 const auto NextToken
= findTokenAfterCompletionPoint(
1657 Recorder
->CCSema
->getPreprocessor().getCodeCompletionLoc(),
1658 Recorder
->CCSema
->getSourceManager(), Recorder
->CCSema
->LangOpts
);
1660 NextTokenKind
= NextToken
->getKind();
1661 // If preprocessor was run, inclusions from preprocessor callback should
1662 // already be added to Includes.
1664 SemaCCInput
.FileName
, SemaCCInput
.ParseInput
.Contents
, Style
,
1665 SemaCCInput
.ParseInput
.CompileCommand
.Directory
,
1666 &Recorder
->CCSema
->getPreprocessor().getHeaderSearchInfo());
1667 for (const auto &Inc
: Includes
.MainFileIncludes
)
1668 Inserter
->addExisting(Inc
);
1670 // Most of the cost of file proximity is in initializing the FileDistance
1671 // structures based on the observed includes, once per query. Conceptually
1672 // that happens here (though the per-URI-scheme initialization is lazy).
1673 // The per-result proximity scoring is (amortized) very cheap.
1674 FileDistanceOptions ProxOpts
{}; // Use defaults.
1675 const auto &SM
= Recorder
->CCSema
->getSourceManager();
1676 llvm::StringMap
<SourceParams
> ProxSources
;
1678 Includes
.getID(SM
.getFileEntryForID(SM
.getMainFileID()));
1680 for (auto &HeaderIDAndDepth
: Includes
.includeDepth(*MainFileID
)) {
1682 ProxSources
[Includes
.getRealPath(HeaderIDAndDepth
.getFirst())];
1683 Source
.Cost
= HeaderIDAndDepth
.getSecond() * ProxOpts
.IncludeCost
;
1684 // Symbols near our transitive includes are good, but only consider
1685 // things in the same directory or below it. Otherwise there can be
1686 // many false positives.
1687 if (HeaderIDAndDepth
.getSecond() > 0)
1688 Source
.MaxUpTraversals
= 1;
1690 FileProximity
.emplace(ProxSources
, ProxOpts
);
1692 Output
= runWithSema();
1693 Inserter
.reset(); // Make sure this doesn't out-live Clang.
1694 SPAN_ATTACH(Tracer
, "sema_completion_kind",
1695 getCompletionKindString(CCContextKind
));
1696 log("Code complete: sema context {0}, query scopes [{1}] (AnyScope={2}), "
1697 "expected type {3}{4}",
1698 getCompletionKindString(CCContextKind
),
1699 llvm::join(QueryScopes
.begin(), QueryScopes
.end(), ","), AllScopes
,
1700 PreferredType
? Recorder
->CCContext
.getPreferredType().getAsString()
1702 IsUsingDeclaration
? ", inside using declaration" : "");
1705 Recorder
= RecorderOwner
.get();
1707 semaCodeComplete(std::move(RecorderOwner
), Opts
.getClangCompleteOpts(),
1708 SemaCCInput
, &Includes
);
1709 logResults(Output
, Tracer
);
1713 void logResults(const CodeCompleteResult
&Output
, const trace::Span
&Tracer
) {
1714 SPAN_ATTACH(Tracer
, "sema_results", NSema
);
1715 SPAN_ATTACH(Tracer
, "index_results", NIndex
);
1716 SPAN_ATTACH(Tracer
, "merged_results", NSemaAndIndex
);
1717 SPAN_ATTACH(Tracer
, "identifier_results", NIdent
);
1718 SPAN_ATTACH(Tracer
, "returned_results", int64_t(Output
.Completions
.size()));
1719 SPAN_ATTACH(Tracer
, "incomplete", Output
.HasMore
);
1720 log("Code complete: {0} results from Sema, {1} from Index, "
1721 "{2} matched, {3} from identifiers, {4} returned{5}.",
1722 NSema
, NIndex
, NSemaAndIndex
, NIdent
, Output
.Completions
.size(),
1723 Output
.HasMore
? " (incomplete)" : "");
1724 assert(!Opts
.Limit
|| Output
.Completions
.size() <= Opts
.Limit
);
1725 // We don't assert that isIncomplete means we hit a limit.
1726 // Indexes may choose to impose their own limits even if we don't have one.
1729 CodeCompleteResult
runWithoutSema(llvm::StringRef Content
, size_t Offset
,
1730 const ThreadsafeFS
&TFS
) && {
1731 trace::Span
Tracer("CodeCompleteWithoutSema");
1732 // Fill in fields normally set by runWithSema()
1733 HeuristicPrefix
= guessCompletionPrefix(Content
, Offset
);
1734 populateContextWords(Content
);
1735 CCContextKind
= CodeCompletionContext::CCC_Recovery
;
1736 IsUsingDeclaration
= false;
1737 Filter
= FuzzyMatcher(HeuristicPrefix
.Name
);
1738 auto Pos
= offsetToPosition(Content
, Offset
);
1739 ReplacedRange
.start
= ReplacedRange
.end
= Pos
;
1740 ReplacedRange
.start
.character
-= HeuristicPrefix
.Name
.size();
1742 llvm::StringMap
<SourceParams
> ProxSources
;
1743 ProxSources
[FileName
].Cost
= 0;
1744 FileProximity
.emplace(ProxSources
);
1746 auto Style
= getFormatStyleForFile(FileName
, Content
, TFS
, false);
1747 // This will only insert verbatim headers.
1748 Inserter
.emplace(FileName
, Content
, Style
,
1749 /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr);
1751 auto Identifiers
= collectIdentifiers(Content
, Style
);
1752 std::vector
<RawIdentifier
> IdentifierResults
;
1753 for (const auto &IDAndCount
: Identifiers
) {
1755 ID
.Name
= IDAndCount
.first();
1756 ID
.References
= IDAndCount
.second
;
1757 // Avoid treating typed filter as an identifier.
1758 if (ID
.Name
== HeuristicPrefix
.Name
)
1760 if (ID
.References
> 0)
1761 IdentifierResults
.push_back(std::move(ID
));
1764 // Simplified version of getQueryScopes():
1765 // - accessible scopes are determined heuristically.
1766 // - all-scopes query if no qualifier was typed (and it's allowed).
1767 SpecifiedScope Scopes
;
1768 Scopes
.QueryScopes
= visibleNamespaces(
1769 Content
.take_front(Offset
), format::getFormattingLangOpts(Style
));
1770 for (std::string
&S
: Scopes
.QueryScopes
)
1772 S
.append("::"); // visibleNamespaces doesn't include trailing ::.
1773 if (HeuristicPrefix
.Qualifier
.empty())
1774 AllScopes
= Opts
.AllScopes
;
1775 else if (HeuristicPrefix
.Qualifier
.starts_with("::")) {
1776 Scopes
.QueryScopes
= {""};
1777 Scopes
.UnresolvedQualifier
=
1778 std::string(HeuristicPrefix
.Qualifier
.drop_front(2));
1780 Scopes
.UnresolvedQualifier
= std::string(HeuristicPrefix
.Qualifier
);
1781 // First scope is the (modified) enclosing scope.
1782 QueryScopes
= Scopes
.scopesForIndexQuery();
1783 AccessibleScopes
= QueryScopes
;
1784 ScopeProximity
.emplace(QueryScopes
);
1786 SymbolSlab IndexResults
= Opts
.Index
? queryIndex() : SymbolSlab();
1788 CodeCompleteResult Output
= toCodeCompleteResult(mergeResults(
1789 /*SemaResults=*/{}, IndexResults
, IdentifierResults
));
1790 Output
.RanParser
= false;
1791 logResults(Output
, Tracer
);
1796 void populateContextWords(llvm::StringRef Content
) {
1797 // Take last 3 lines before the completion point.
1798 unsigned RangeEnd
= HeuristicPrefix
.Qualifier
.begin() - Content
.data(),
1799 RangeBegin
= RangeEnd
;
1800 for (size_t I
= 0; I
< 3 && RangeBegin
> 0; ++I
) {
1801 auto PrevNL
= Content
.rfind('\n', RangeBegin
);
1802 if (PrevNL
== StringRef::npos
) {
1806 RangeBegin
= PrevNL
;
1809 ContextWords
= collectWords(Content
.slice(RangeBegin
, RangeEnd
));
1810 dlog("Completion context words: {0}",
1811 llvm::join(ContextWords
.keys(), ", "));
1814 // This is called by run() once Sema code completion is done, but before the
1815 // Sema data structures are torn down. It does all the real work.
1816 CodeCompleteResult
runWithSema() {
1817 const auto &CodeCompletionRange
= CharSourceRange::getCharRange(
1818 Recorder
->CCSema
->getPreprocessor().getCodeCompletionTokenRange());
1819 // When we are getting completions with an empty identifier, for example
1820 // std::vector<int> asdf;
1822 // Then the range will be invalid and we will be doing insertion, use
1823 // current cursor position in such cases as range.
1824 if (CodeCompletionRange
.isValid()) {
1825 ReplacedRange
= halfOpenToRange(Recorder
->CCSema
->getSourceManager(),
1826 CodeCompletionRange
);
1828 const auto &Pos
= sourceLocToPosition(
1829 Recorder
->CCSema
->getSourceManager(),
1830 Recorder
->CCSema
->getPreprocessor().getCodeCompletionLoc());
1831 ReplacedRange
.start
= ReplacedRange
.end
= Pos
;
1833 Filter
= FuzzyMatcher(
1834 Recorder
->CCSema
->getPreprocessor().getCodeCompletionFilter());
1835 auto SpecifiedScopes
= getQueryScopes(
1836 Recorder
->CCContext
, *Recorder
->CCSema
, HeuristicPrefix
, Opts
);
1838 QueryScopes
= SpecifiedScopes
.scopesForIndexQuery();
1839 AccessibleScopes
= SpecifiedScopes
.scopesForQualification();
1840 AllScopes
= SpecifiedScopes
.AllowAllScopes
;
1841 if (!QueryScopes
.empty())
1842 ScopeProximity
.emplace(QueryScopes
);
1844 OpaqueType::fromType(Recorder
->CCSema
->getASTContext(),
1845 Recorder
->CCContext
.getPreferredType());
1846 // Sema provides the needed context to query the index.
1847 // FIXME: in addition to querying for extra/overlapping symbols, we should
1848 // explicitly request symbols corresponding to Sema results.
1849 // We can use their signals even if the index can't suggest them.
1850 // We must copy index results to preserve them, but there are at most Limit.
1851 auto IndexResults
= (Opts
.Index
&& allowIndex(Recorder
->CCContext
))
1854 trace::Span
Tracer("Populate CodeCompleteResult");
1855 // Merge Sema and Index results, score them, and pick the winners.
1857 mergeResults(Recorder
->Results
, IndexResults
, /*Identifiers*/ {});
1858 return toCodeCompleteResult(Top
);
1862 toCodeCompleteResult(const std::vector
<ScoredBundle
> &Scored
) {
1863 CodeCompleteResult Output
;
1865 // Convert the results to final form, assembling the expensive strings.
1866 for (auto &C
: Scored
) {
1867 Output
.Completions
.push_back(toCodeCompletion(C
.first
));
1868 Output
.Completions
.back().Score
= C
.second
;
1869 Output
.Completions
.back().CompletionTokenRange
= ReplacedRange
;
1871 Output
.HasMore
= Incomplete
;
1872 Output
.Context
= CCContextKind
;
1873 Output
.CompletionRange
= ReplacedRange
;
1877 SymbolSlab
queryIndex() {
1878 trace::Span
Tracer("Query index");
1879 SPAN_ATTACH(Tracer
, "limit", int64_t(Opts
.Limit
));
1882 FuzzyFindRequest Req
;
1884 Req
.Limit
= Opts
.Limit
;
1885 Req
.Query
= std::string(Filter
->pattern());
1886 Req
.RestrictForCodeCompletion
= true;
1887 Req
.Scopes
= QueryScopes
;
1888 Req
.AnyScope
= AllScopes
;
1889 // FIXME: we should send multiple weighted paths here.
1890 Req
.ProximityPaths
.push_back(std::string(FileName
));
1892 Req
.PreferredTypes
.push_back(std::string(PreferredType
->raw()));
1893 vlog("Code complete: fuzzyFind({0:2})", toJSON(Req
));
1896 SpecFuzzyFind
->NewReq
= Req
;
1897 if (SpecFuzzyFind
&& SpecFuzzyFind
->Result
.valid() && (*SpecReq
== Req
)) {
1898 vlog("Code complete: speculative fuzzy request matches the actual index "
1899 "request. Waiting for the speculative index results.");
1900 SPAN_ATTACH(Tracer
, "Speculative results", true);
1902 trace::Span
WaitSpec("Wait speculative results");
1903 auto SpecRes
= SpecFuzzyFind
->Result
.get();
1904 Incomplete
|= SpecRes
.first
;
1905 return std::move(SpecRes
.second
);
1908 SPAN_ATTACH(Tracer
, "Speculative results", false);
1910 // Run the query against the index.
1911 SymbolSlab::Builder ResultsBuilder
;
1912 Incomplete
|= Opts
.Index
->fuzzyFind(
1913 Req
, [&](const Symbol
&Sym
) { ResultsBuilder
.insert(Sym
); });
1914 return std::move(ResultsBuilder
).build();
1917 // Merges Sema and Index results where possible, to form CompletionCandidates.
1918 // \p Identifiers is raw identifiers that can also be completion candidates.
1919 // Identifiers are not merged with results from index or sema.
1920 // Groups overloads if desired, to form CompletionCandidate::Bundles. The
1921 // bundles are scored and top results are returned, best to worst.
1922 std::vector
<ScoredBundle
>
1923 mergeResults(const std::vector
<CodeCompletionResult
> &SemaResults
,
1924 const SymbolSlab
&IndexResults
,
1925 const std::vector
<RawIdentifier
> &IdentifierResults
) {
1926 trace::Span
Tracer("Merge and score results");
1927 std::vector
<CompletionCandidate::Bundle
> Bundles
;
1928 llvm::DenseMap
<size_t, size_t> BundleLookup
;
1929 auto AddToBundles
= [&](const CodeCompletionResult
*SemaResult
,
1930 const Symbol
*IndexResult
,
1931 const RawIdentifier
*IdentifierResult
) {
1932 CompletionCandidate C
;
1933 C
.SemaResult
= SemaResult
;
1934 C
.IndexResult
= IndexResult
;
1935 C
.IdentifierResult
= IdentifierResult
;
1936 if (C
.IndexResult
) {
1937 C
.Name
= IndexResult
->Name
;
1938 C
.RankedIncludeHeaders
= getRankedIncludes(*C
.IndexResult
);
1939 } else if (C
.SemaResult
) {
1940 C
.Name
= Recorder
->getName(*SemaResult
);
1942 assert(IdentifierResult
);
1943 C
.Name
= IdentifierResult
->Name
;
1945 if (auto OverloadSet
= C
.overloadSet(
1946 Opts
, FileName
, Inserter
? &*Inserter
: nullptr, CCContextKind
)) {
1947 auto Ret
= BundleLookup
.try_emplace(OverloadSet
, Bundles
.size());
1949 Bundles
.emplace_back();
1950 Bundles
[Ret
.first
->second
].push_back(std::move(C
));
1952 Bundles
.emplace_back();
1953 Bundles
.back().push_back(std::move(C
));
1956 llvm::DenseSet
<const Symbol
*> UsedIndexResults
;
1957 auto CorrespondingIndexResult
=
1958 [&](const CodeCompletionResult
&SemaResult
) -> const Symbol
* {
1960 getSymbolID(SemaResult
, Recorder
->CCSema
->getSourceManager())) {
1961 auto I
= IndexResults
.find(SymID
);
1962 if (I
!= IndexResults
.end()) {
1963 UsedIndexResults
.insert(&*I
);
1969 // Emit all Sema results, merging them with Index results if possible.
1970 for (auto &SemaResult
: SemaResults
)
1971 AddToBundles(&SemaResult
, CorrespondingIndexResult(SemaResult
), nullptr);
1972 // Now emit any Index-only results.
1973 for (const auto &IndexResult
: IndexResults
) {
1974 if (UsedIndexResults
.count(&IndexResult
))
1976 if (!includeSymbolFromIndex(CCContextKind
, IndexResult
))
1978 AddToBundles(/*SemaResult=*/nullptr, &IndexResult
, nullptr);
1980 // Emit identifier results.
1981 for (const auto &Ident
: IdentifierResults
)
1982 AddToBundles(/*SemaResult=*/nullptr, /*IndexResult=*/nullptr, &Ident
);
1983 // We only keep the best N results at any time, in "native" format.
1984 TopN
<ScoredBundle
, ScoredBundleGreater
> Top(
1985 Opts
.Limit
== 0 ? std::numeric_limits
<size_t>::max() : Opts
.Limit
);
1986 for (auto &Bundle
: Bundles
)
1987 addCandidate(Top
, std::move(Bundle
));
1988 return std::move(Top
).items();
1991 std::optional
<float> fuzzyScore(const CompletionCandidate
&C
) {
1992 // Macros can be very spammy, so we only support prefix completion.
1993 if (((C
.SemaResult
&&
1994 C
.SemaResult
->Kind
== CodeCompletionResult::RK_Macro
) ||
1996 C
.IndexResult
->SymInfo
.Kind
== index::SymbolKind::Macro
)) &&
1997 !C
.Name
.starts_with_insensitive(Filter
->pattern()))
1998 return std::nullopt
;
1999 return Filter
->match(C
.Name
);
2002 CodeCompletion::Scores
2003 evaluateCompletion(const SymbolQualitySignals
&Quality
,
2004 const SymbolRelevanceSignals
&Relevance
) {
2005 using RM
= CodeCompleteOptions::CodeCompletionRankingModel
;
2006 CodeCompletion::Scores Scores
;
2007 switch (Opts
.RankingModel
) {
2008 case RM::Heuristics
:
2009 Scores
.Quality
= Quality
.evaluateHeuristics();
2010 Scores
.Relevance
= Relevance
.evaluateHeuristics();
2012 evaluateSymbolAndRelevance(Scores
.Quality
, Scores
.Relevance
);
2013 // NameMatch is in fact a multiplier on total score, so rescoring is
2015 Scores
.ExcludingName
=
2016 Relevance
.NameMatch
> std::numeric_limits
<float>::epsilon()
2017 ? Scores
.Total
/ Relevance
.NameMatch
2021 case RM::DecisionForest
:
2022 DecisionForestScores DFScores
= Opts
.DecisionForestScorer(
2023 Quality
, Relevance
, Opts
.DecisionForestBase
);
2024 Scores
.ExcludingName
= DFScores
.ExcludingName
;
2025 Scores
.Total
= DFScores
.Total
;
2028 llvm_unreachable("Unhandled CodeCompletion ranking model.");
2031 // Scores a candidate and adds it to the TopN structure.
2032 void addCandidate(TopN
<ScoredBundle
, ScoredBundleGreater
> &Candidates
,
2033 CompletionCandidate::Bundle Bundle
) {
2034 SymbolQualitySignals Quality
;
2035 SymbolRelevanceSignals Relevance
;
2036 Relevance
.Context
= CCContextKind
;
2037 Relevance
.Name
= Bundle
.front().Name
;
2038 Relevance
.FilterLength
= HeuristicPrefix
.Name
.size();
2039 Relevance
.Query
= SymbolRelevanceSignals::CodeComplete
;
2040 Relevance
.FileProximityMatch
= &*FileProximity
;
2042 Relevance
.ScopeProximityMatch
= &*ScopeProximity
;
2044 Relevance
.HadContextType
= true;
2045 Relevance
.ContextWords
= &ContextWords
;
2046 Relevance
.MainFileSignals
= Opts
.MainFileSignals
;
2048 auto &First
= Bundle
.front();
2049 if (auto FuzzyScore
= fuzzyScore(First
))
2050 Relevance
.NameMatch
= *FuzzyScore
;
2053 SymbolOrigin Origin
= SymbolOrigin::Unknown
;
2054 bool FromIndex
= false;
2055 for (const auto &Candidate
: Bundle
) {
2056 if (Candidate
.IndexResult
) {
2057 Quality
.merge(*Candidate
.IndexResult
);
2058 Relevance
.merge(*Candidate
.IndexResult
);
2059 Origin
|= Candidate
.IndexResult
->Origin
;
2061 if (!Candidate
.IndexResult
->Type
.empty())
2062 Relevance
.HadSymbolType
|= true;
2063 if (PreferredType
&&
2064 PreferredType
->raw() == Candidate
.IndexResult
->Type
) {
2065 Relevance
.TypeMatchesPreferred
= true;
2068 if (Candidate
.SemaResult
) {
2069 Quality
.merge(*Candidate
.SemaResult
);
2070 Relevance
.merge(*Candidate
.SemaResult
);
2071 if (PreferredType
) {
2072 if (auto CompletionType
= OpaqueType::fromCompletionResult(
2073 Recorder
->CCSema
->getASTContext(), *Candidate
.SemaResult
)) {
2074 Relevance
.HadSymbolType
|= true;
2075 if (PreferredType
== CompletionType
)
2076 Relevance
.TypeMatchesPreferred
= true;
2079 Origin
|= SymbolOrigin::AST
;
2081 if (Candidate
.IdentifierResult
) {
2082 Quality
.References
= Candidate
.IdentifierResult
->References
;
2083 Relevance
.Scope
= SymbolRelevanceSignals::FileScope
;
2084 Origin
|= SymbolOrigin::Identifier
;
2088 CodeCompletion::Scores Scores
= evaluateCompletion(Quality
, Relevance
);
2089 if (Opts
.RecordCCResult
)
2090 Opts
.RecordCCResult(toCodeCompletion(Bundle
), Quality
, Relevance
,
2093 dlog("CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First
.Name
,
2094 llvm::to_string(Origin
), Scores
.Total
, llvm::to_string(Quality
),
2095 llvm::to_string(Relevance
));
2097 NSema
+= bool(Origin
& SymbolOrigin::AST
);
2098 NIndex
+= FromIndex
;
2099 NSemaAndIndex
+= bool(Origin
& SymbolOrigin::AST
) && FromIndex
;
2100 NIdent
+= bool(Origin
& SymbolOrigin::Identifier
);
2101 if (Candidates
.push({std::move(Bundle
), Scores
}))
2105 CodeCompletion
toCodeCompletion(const CompletionCandidate::Bundle
&Bundle
) {
2106 std::optional
<CodeCompletionBuilder
> Builder
;
2107 for (const auto &Item
: Bundle
) {
2108 CodeCompletionString
*SemaCCS
=
2109 Item
.SemaResult
? Recorder
->codeCompletionString(*Item
.SemaResult
)
2112 Builder
.emplace(Recorder
? &Recorder
->CCSema
->getASTContext() : nullptr,
2113 Item
, SemaCCS
, AccessibleScopes
, *Inserter
, FileName
,
2114 CCContextKind
, Opts
, IsUsingDeclaration
, NextTokenKind
);
2116 Builder
->add(Item
, SemaCCS
, CCContextKind
);
2118 return Builder
->build();
2124 clang::CodeCompleteOptions
CodeCompleteOptions::getClangCompleteOpts() const {
2125 clang::CodeCompleteOptions Result
;
2126 Result
.IncludeCodePatterns
= EnableSnippets
;
2127 Result
.IncludeMacros
= true;
2128 Result
.IncludeGlobals
= true;
2129 // We choose to include full comments and not do doxygen parsing in
2131 // FIXME: ideally, we should support doxygen in some form, e.g. do markdown
2132 // formatting of the comments.
2133 Result
.IncludeBriefComments
= false;
2135 // When an is used, Sema is responsible for completing the main file,
2136 // the index can provide results from the preamble.
2137 // Tell Sema not to deserialize the preamble to look for results.
2138 Result
.LoadExternal
= ForceLoadPreamble
|| !Index
;
2139 Result
.IncludeFixIts
= IncludeFixIts
;
2144 CompletionPrefix
guessCompletionPrefix(llvm::StringRef Content
,
2146 assert(Offset
<= Content
.size());
2147 StringRef Rest
= Content
.take_front(Offset
);
2148 CompletionPrefix Result
;
2150 // Consume the unqualified name. We only handle ASCII characters.
2151 // isAsciiIdentifierContinue will let us match "0invalid", but we don't mind.
2152 while (!Rest
.empty() && isAsciiIdentifierContinue(Rest
.back()))
2153 Rest
= Rest
.drop_back();
2154 Result
.Name
= Content
.slice(Rest
.size(), Offset
);
2156 // Consume qualifiers.
2157 while (Rest
.consume_back("::") && !Rest
.ends_with(":")) // reject ::::
2158 while (!Rest
.empty() && isAsciiIdentifierContinue(Rest
.back()))
2159 Rest
= Rest
.drop_back();
2161 Content
.slice(Rest
.size(), Result
.Name
.begin() - Content
.begin());
2166 // Code complete the argument name on "/*" inside function call.
2167 // Offset should be pointing to the start of the comment, i.e.:
2168 // foo(^/*, rather than foo(/*^) where the cursor probably is.
2169 CodeCompleteResult
codeCompleteComment(PathRef FileName
, unsigned Offset
,
2170 llvm::StringRef Prefix
,
2171 const PreambleData
*Preamble
,
2172 const ParseInputs
&ParseInput
) {
2173 if (Preamble
== nullptr) // Can't run without Sema.
2174 return CodeCompleteResult();
2176 clang::CodeCompleteOptions Options
;
2177 Options
.IncludeGlobals
= false;
2178 Options
.IncludeMacros
= false;
2179 Options
.IncludeCodePatterns
= false;
2180 Options
.IncludeBriefComments
= false;
2181 std::set
<std::string
> ParamNames
;
2182 // We want to see signatures coming from newly introduced includes, hence a
2185 std::make_unique
<ParamNameCollector
>(Options
, ParamNames
), Options
,
2186 {FileName
, Offset
, *Preamble
,
2187 PreamblePatch::createFullPatch(FileName
, ParseInput
, *Preamble
),
2189 if (ParamNames
.empty())
2190 return CodeCompleteResult();
2192 CodeCompleteResult Result
;
2193 Range CompletionRange
;
2196 CompletionRange
.start
= offsetToPosition(ParseInput
.Contents
, Offset
);
2197 CompletionRange
.end
=
2198 offsetToPosition(ParseInput
.Contents
, Offset
+ Prefix
.size());
2199 Result
.CompletionRange
= CompletionRange
;
2200 Result
.Context
= CodeCompletionContext::CCC_NaturalLanguage
;
2201 for (llvm::StringRef Name
: ParamNames
) {
2202 if (!Name
.starts_with(Prefix
))
2204 CodeCompletion Item
;
2205 Item
.Name
= Name
.str() + "=*/";
2206 Item
.FilterText
= Item
.Name
;
2207 Item
.Kind
= CompletionItemKind::Text
;
2208 Item
.CompletionTokenRange
= CompletionRange
;
2209 Item
.Origin
= SymbolOrigin::AST
;
2210 Result
.Completions
.push_back(Item
);
2216 // If Offset is inside what looks like argument comment (e.g.
2217 // "/*^" or "/* foo^"), returns new offset pointing to the start of the /*
2218 // (place where semaCodeComplete should run).
2219 std::optional
<unsigned>
2220 maybeFunctionArgumentCommentStart(llvm::StringRef Content
) {
2221 while (!Content
.empty() && isAsciiIdentifierContinue(Content
.back()))
2222 Content
= Content
.drop_back();
2223 Content
= Content
.rtrim();
2224 if (Content
.ends_with("/*"))
2225 return Content
.size() - 2;
2226 return std::nullopt
;
2229 CodeCompleteResult
codeComplete(PathRef FileName
, Position Pos
,
2230 const PreambleData
*Preamble
,
2231 const ParseInputs
&ParseInput
,
2232 CodeCompleteOptions Opts
,
2233 SpeculativeFuzzyFind
*SpecFuzzyFind
) {
2234 auto Offset
= positionToOffset(ParseInput
.Contents
, Pos
);
2236 elog("Code completion position was invalid {0}", Offset
.takeError());
2237 return CodeCompleteResult();
2240 auto Content
= llvm::StringRef(ParseInput
.Contents
).take_front(*Offset
);
2241 if (auto OffsetBeforeComment
= maybeFunctionArgumentCommentStart(Content
)) {
2242 // We are doing code completion of a comment, where we currently only
2243 // support completing param names in function calls. To do this, we
2244 // require information from Sema, but Sema's comment completion stops at
2245 // parsing, so we must move back the position before running it, extract
2246 // information we need and construct completion items ourselves.
2247 auto CommentPrefix
= Content
.substr(*OffsetBeforeComment
+ 2).trim();
2248 return codeCompleteComment(FileName
, *OffsetBeforeComment
, CommentPrefix
,
2249 Preamble
, ParseInput
);
2252 auto Flow
= CodeCompleteFlow(
2253 FileName
, Preamble
? Preamble
->Includes
: IncludeStructure(),
2254 SpecFuzzyFind
, Opts
);
2255 return (!Preamble
|| Opts
.RunParser
== CodeCompleteOptions::NeverParse
)
2256 ? std::move(Flow
).runWithoutSema(ParseInput
.Contents
, *Offset
,
2258 : std::move(Flow
).run({FileName
, *Offset
, *Preamble
,
2260 PreamblePatch::createMacroPatch(
2261 FileName
, ParseInput
, *Preamble
),
2265 SignatureHelp
signatureHelp(PathRef FileName
, Position Pos
,
2266 const PreambleData
&Preamble
,
2267 const ParseInputs
&ParseInput
,
2268 MarkupKind DocumentationFormat
) {
2269 auto Offset
= positionToOffset(ParseInput
.Contents
, Pos
);
2271 elog("Signature help position was invalid {0}", Offset
.takeError());
2272 return SignatureHelp();
2274 SignatureHelp Result
;
2275 clang::CodeCompleteOptions Options
;
2276 Options
.IncludeGlobals
= false;
2277 Options
.IncludeMacros
= false;
2278 Options
.IncludeCodePatterns
= false;
2279 Options
.IncludeBriefComments
= false;
2281 std::make_unique
<SignatureHelpCollector
>(Options
, DocumentationFormat
,
2282 ParseInput
.Index
, Result
),
2284 {FileName
, *Offset
, Preamble
,
2285 PreamblePatch::createFullPatch(FileName
, ParseInput
, Preamble
),
2290 bool isIndexedForCodeCompletion(const NamedDecl
&ND
, ASTContext
&ASTCtx
) {
2291 auto InTopLevelScope
= [](const NamedDecl
&ND
) {
2292 switch (ND
.getDeclContext()->getDeclKind()) {
2293 case Decl::TranslationUnit
:
2294 case Decl::Namespace
:
2295 case Decl::LinkageSpec
:
2302 auto InClassScope
= [](const NamedDecl
&ND
) {
2303 return ND
.getDeclContext()->getDeclKind() == Decl::CXXRecord
;
2305 // We only complete symbol's name, which is the same as the name of the
2306 // *primary* template in case of template specializations.
2307 if (isExplicitTemplateSpecialization(&ND
))
2310 // Category decls are not useful on their own outside the interface or
2311 // implementation blocks. Moreover, sema already provides completion for
2312 // these, even if it requires preamble deserialization. So by excluding them
2313 // from the index, we reduce the noise in all the other completion scopes.
2314 if (llvm::isa
<ObjCCategoryDecl
>(&ND
) || llvm::isa
<ObjCCategoryImplDecl
>(&ND
))
2317 if (InTopLevelScope(ND
))
2320 // Always index enum constants, even if they're not in the top level scope:
2322 // --all-scopes-completion is set, we'll want to complete those as well.
2323 if (const auto *EnumDecl
= dyn_cast
<clang::EnumDecl
>(ND
.getDeclContext()))
2324 return (InTopLevelScope(*EnumDecl
) || InClassScope(*EnumDecl
));
2329 CompletionItem
CodeCompletion::render(const CodeCompleteOptions
&Opts
) const {
2331 const auto *InsertInclude
= Includes
.empty() ? nullptr : &Includes
[0];
2332 // We could move our indicators from label into labelDetails->description.
2333 // In VSCode there are rendering issues that prevent these being aligned.
2334 LSP
.label
= ((InsertInclude
&& InsertInclude
->Insertion
)
2335 ? Opts
.IncludeIndicator
.Insert
2336 : Opts
.IncludeIndicator
.NoInsert
) +
2337 (Opts
.ShowOrigins
? "[" + llvm::to_string(Origin
) + "]" : "") +
2338 RequiredQualifier
+ Name
;
2339 LSP
.labelDetails
.emplace();
2340 LSP
.labelDetails
->detail
= Signature
;
2343 LSP
.detail
= BundleSize
> 1
2344 ? std::string(llvm::formatv("[{0} overloads]", BundleSize
))
2346 LSP
.deprecated
= Deprecated
;
2347 // Combine header information and documentation in LSP `documentation` field.
2348 // This is not quite right semantically, but tends to display well in editors.
2349 if (InsertInclude
|| Documentation
) {
2350 markup::Document Doc
;
2352 Doc
.addParagraph().appendText("From ").appendCode(InsertInclude
->Header
);
2354 Doc
.append(*Documentation
);
2355 LSP
.documentation
= renderDoc(Doc
, Opts
.DocumentationFormat
);
2357 LSP
.sortText
= sortText(Score
.Total
, FilterText
);
2358 LSP
.filterText
= FilterText
;
2359 LSP
.textEdit
= {CompletionTokenRange
, RequiredQualifier
+ Name
, ""};
2360 // Merge continuous additionalTextEdits into main edit. The main motivation
2361 // behind this is to help LSP clients, it seems most of them are confused when
2362 // they are provided with additionalTextEdits that are consecutive to main
2364 // Note that we store additional text edits from back to front in a line. That
2365 // is mainly to help LSP clients again, so that changes do not effect each
2367 for (const auto &FixIt
: FixIts
) {
2368 if (FixIt
.range
.end
== LSP
.textEdit
->range
.start
) {
2369 LSP
.textEdit
->newText
= FixIt
.newText
+ LSP
.textEdit
->newText
;
2370 LSP
.textEdit
->range
.start
= FixIt
.range
.start
;
2372 LSP
.additionalTextEdits
.push_back(FixIt
);
2375 if (Opts
.EnableSnippets
)
2376 LSP
.textEdit
->newText
+= SnippetSuffix
;
2378 // FIXME(kadircet): Do not even fill insertText after making sure textEdit is
2379 // compatible with most of the editors.
2380 LSP
.insertText
= LSP
.textEdit
->newText
;
2381 // Some clients support snippets but work better with plaintext.
2382 // So if the snippet is trivial, let the client know.
2383 // https://github.com/clangd/clangd/issues/922
2384 LSP
.insertTextFormat
= (Opts
.EnableSnippets
&& !SnippetSuffix
.empty())
2385 ? InsertTextFormat::Snippet
2386 : InsertTextFormat::PlainText
;
2387 if (InsertInclude
&& InsertInclude
->Insertion
)
2388 LSP
.additionalTextEdits
.push_back(*InsertInclude
->Insertion
);
2390 LSP
.score
= Score
.ExcludingName
;
2395 llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
, const CodeCompletion
&C
) {
2396 // For now just lean on CompletionItem.
2397 return OS
<< C
.render(CodeCompleteOptions());
2400 llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
,
2401 const CodeCompleteResult
&R
) {
2402 OS
<< "CodeCompleteResult: " << R
.Completions
.size() << (R
.HasMore
? "+" : "")
2403 << " (" << getCompletionKindString(R
.Context
) << ")"
2405 for (const auto &C
: R
.Completions
)
2410 // Heuristically detect whether the `Line` is an unterminated include filename.
2411 bool isIncludeFile(llvm::StringRef Line
) {
2412 Line
= Line
.ltrim();
2413 if (!Line
.consume_front("#"))
2415 Line
= Line
.ltrim();
2416 if (!(Line
.consume_front("include_next") || Line
.consume_front("include") ||
2417 Line
.consume_front("import")))
2419 Line
= Line
.ltrim();
2420 if (Line
.consume_front("<"))
2421 return Line
.count('>') == 0;
2422 if (Line
.consume_front("\""))
2423 return Line
.count('"') == 0;
2427 bool allowImplicitCompletion(llvm::StringRef Content
, unsigned Offset
) {
2428 // Look at last line before completion point only.
2429 Content
= Content
.take_front(Offset
);
2430 auto Pos
= Content
.rfind('\n');
2431 if (Pos
!= llvm::StringRef::npos
)
2432 Content
= Content
.substr(Pos
+ 1);
2434 // Complete after scope operators.
2435 if (Content
.ends_with(".") || Content
.ends_with("->") ||
2436 Content
.ends_with("::") || Content
.ends_with("/*"))
2438 // Complete after `#include <` and #include `<foo/`.
2439 if ((Content
.ends_with("<") || Content
.ends_with("\"") ||
2440 Content
.ends_with("/")) &&
2441 isIncludeFile(Content
))
2444 // Complete words. Give non-ascii characters the benefit of the doubt.
2445 return !Content
.empty() && (isAsciiIdentifierContinue(Content
.back()) ||
2446 !llvm::isASCII(Content
.back()));
2449 } // namespace clangd
2450 } // namespace clang