43#include "clang/AST/Decl.h"
44#include "clang/AST/DeclBase.h"
45#include "clang/Basic/CharInfo.h"
46#include "clang/Basic/LangOptions.h"
47#include "clang/Basic/SourceLocation.h"
48#include "clang/Basic/TokenKinds.h"
49#include "clang/Format/Format.h"
50#include "clang/Frontend/CompilerInstance.h"
51#include "clang/Frontend/FrontendActions.h"
52#include "clang/Lex/ExternalPreprocessorSource.h"
53#include "clang/Lex/Lexer.h"
54#include "clang/Lex/Preprocessor.h"
55#include "clang/Lex/PreprocessorOptions.h"
56#include "clang/Sema/CodeCompleteConsumer.h"
57#include "clang/Sema/DeclSpec.h"
58#include "clang/Sema/Sema.h"
59#include "llvm/ADT/ArrayRef.h"
60#include "llvm/ADT/SmallVector.h"
61#include "llvm/ADT/StringExtras.h"
62#include "llvm/ADT/StringRef.h"
63#include "llvm/Support/Casting.h"
64#include "llvm/Support/Compiler.h"
65#include "llvm/Support/Debug.h"
66#include "llvm/Support/Error.h"
67#include "llvm/Support/FormatVariadic.h"
68#include "llvm/Support/ScopedPrinter.h"
76#define DEBUG_TYPE "CodeComplete"
81#if CLANGD_DECISION_FOREST
93 using SK = index::SymbolKind;
99 case SK::NamespaceAlias:
123 case SK::ConversionFunction:
127 case SK::NonTypeTemplateParm:
131 case SK::EnumConstant:
133 case SK::InstanceMethod:
134 case SK::ClassMethod:
135 case SK::StaticMethod:
138 case SK::InstanceProperty:
139 case SK::ClassProperty:
140 case SK::StaticProperty:
142 case SK::Constructor:
144 case SK::TemplateTypeParm:
145 case SK::TemplateTemplateParm:
150 llvm_unreachable(
"Unhandled clang::index::SymbolKind.");
154 CodeCompletionContext::Kind CtxKind) {
156 return toCompletionItemKind(index::getSymbolInfo(Res.Declaration).Kind);
157 if (CtxKind == CodeCompletionContext::CCC_IncludedFile)
160 case CodeCompletionResult::RK_Declaration:
161 llvm_unreachable(
"RK_Declaration without Decl");
162 case CodeCompletionResult::RK_Keyword:
164 case CodeCompletionResult::RK_Macro:
168 return Res.MacroDefInfo && Res.MacroDefInfo->isFunctionLike()
171 case CodeCompletionResult::RK_Pattern:
174 llvm_unreachable(
"Unhandled CodeCompletionResult::ResultKind.");
178MarkupContent renderDoc(
const markup::Document &Doc,
MarkupKind Kind) {
179 MarkupContent Result;
183 Result.value.append(Doc.asPlainText());
186 Result.value.append(Doc.asMarkdown());
199struct RawIdentifier {
206struct CompletionCandidate {
207 llvm::StringRef
Name;
216 size_t overloadSet(
const CodeCompleteOptions &Opts, llvm::StringRef
FileName,
217 IncludeInserter *Inserter,
218 CodeCompletionContext::Kind CCContextKind)
const {
219 if (!Opts.BundleOverloads.value_or(
false))
225 std::string HeaderForHash;
227 if (
auto Header = headerToInsertIfAllowed(Opts, CCContextKind)) {
230 Inserter->calculateIncludePath(*HeaderFile,
FileName))
233 vlog(
"Code completion header path manipulation failed {0}",
234 HeaderFile.takeError());
239 llvm::SmallString<256> Scratch;
242 case index::SymbolKind::ClassMethod:
243 case index::SymbolKind::InstanceMethod:
244 case index::SymbolKind::StaticMethod:
246 llvm_unreachable(
"Don't expect members from index in code completion");
250 case index::SymbolKind::Function:
253 return llvm::hash_combine(
263 if (!D || !D->isFunctionOrFunctionTemplate())
266 llvm::raw_svector_ostream
OS(Scratch);
267 D->printQualifiedName(
OS);
269 return llvm::hash_combine(Scratch, HeaderForHash);
275 bool contextAllowsHeaderInsertion(CodeCompletionContext::Kind
Kind)
const {
278 if (
Kind == CodeCompletionContext::CCC_ObjCClassForwardDecl)
284 std::optional<llvm::StringRef>
285 headerToInsertIfAllowed(
const CodeCompleteOptions &Opts,
286 CodeCompletionContext::Kind ContextKind)
const {
289 !contextAllowsHeaderInsertion(ContextKind))
294 auto &SM =
SemaResult->Declaration->getASTContext().getSourceManager();
296 if (SM.isInMainFile(SM.getExpansionLoc(RD->getBeginLoc())))
300 for (
const auto &Inc : RankedIncludeHeaders)
306 using Bundle = llvm::SmallVector<CompletionCandidate, 4>;
309 std::pair<CompletionCandidate::Bundle, CodeCompletion::Scores>;
310struct ScoredBundleGreater {
311 bool operator()(
const ScoredBundle &L,
const ScoredBundle &R) {
312 if (L.second.Total != R.second.Total)
313 return L.second.Total > R.second.Total;
314 return L.first.front().Name <
315 R.first.front().Name;
321std::string removeFirstTemplateArg(llvm::StringRef
Signature) {
325 return (
"<" + Rest.ltrim()).str();
335struct CodeCompletionBuilder {
336 CodeCompletionBuilder(ASTContext *ASTCtx,
const CompletionCandidate &
C,
337 CodeCompletionString *SemaCCS,
339 const IncludeInserter &Includes,
341 CodeCompletionContext::Kind ContextKind,
342 const CodeCompleteOptions &Opts,
343 bool IsUsingDeclaration, tok::TokenKind NextTokenKind)
345 EnableFunctionArgSnippets(Opts.EnableFunctionArgSnippets),
346 IsUsingDeclaration(IsUsingDeclaration), NextTokenKind(NextTokenKind) {
347 Completion.Deprecated =
true;
348 add(
C, SemaCCS, ContextKind);
352 Completion.Name = std::string(llvm::StringRef(SemaCCS->getTypedText()));
353 Completion.FilterText = SemaCCS->getAllTypedText();
354 if (Completion.Scope.empty()) {
355 if ((
C.SemaResult->Kind == CodeCompletionResult::RK_Declaration) ||
356 (
C.SemaResult->Kind == CodeCompletionResult::RK_Pattern))
357 if (
const auto *D =
C.SemaResult->getDeclaration())
358 if (
const auto *ND = dyn_cast<NamedDecl>(D))
359 Completion.Scope = std::string(
362 Completion.Kind = toCompletionItemKind(*
C.SemaResult, ContextKind);
366 Completion.Name.back() ==
'/')
368 for (
const auto &
FixIt :
C.SemaResult->FixIts) {
370 FixIt, ASTCtx->getSourceManager(), ASTCtx->getLangOpts()));
372 llvm::sort(Completion.FixIts, [](
const TextEdit &
X,
const TextEdit &Y) {
373 return std::tie(X.range.start.line, X.range.start.character) <
374 std::tie(Y.range.start.line, Y.range.start.character);
378 Completion.Origin |=
C.IndexResult->Origin;
379 if (Completion.Scope.empty())
380 Completion.Scope = std::string(
C.IndexResult->Scope);
382 Completion.Kind = toCompletionItemKind(
C.IndexResult->SymInfo.Kind);
383 if (Completion.Name.empty())
384 Completion.Name = std::string(
C.IndexResult->Name);
385 if (Completion.FilterText.empty())
386 Completion.FilterText = Completion.Name;
389 if (Completion.RequiredQualifier.empty() && !
C.SemaResult) {
390 llvm::StringRef ShortestQualifier =
C.IndexResult->Scope;
392 llvm::StringRef Qualifier =
C.IndexResult->Scope;
393 if (Qualifier.consume_front(Scope) &&
394 Qualifier.size() < ShortestQualifier.size())
395 ShortestQualifier = Qualifier;
397 Completion.RequiredQualifier = std::string(ShortestQualifier);
400 if (
C.IdentifierResult) {
403 Completion.Name = std::string(
C.IdentifierResult->Name);
404 Completion.FilterText = Completion.Name;
408 auto Inserted = [&](llvm::StringRef Header)
409 -> llvm::Expected<std::pair<std::string, bool>> {
410 auto ResolvedDeclaring =
412 if (!ResolvedDeclaring)
413 return ResolvedDeclaring.takeError();
415 if (!ResolvedInserted)
416 return ResolvedInserted.takeError();
417 auto Spelled = Includes.calculateIncludePath(*ResolvedInserted,
FileName);
419 return error(
"Header not on include path");
420 return std::make_pair(
422 Includes.shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
425 C.headerToInsertIfAllowed(Opts, ContextKind).has_value();
428 for (
const auto &Inc :
C.RankedIncludeHeaders) {
432 if (
auto ToInclude = Inserted(Inc.Header)) {
433 CodeCompletion::IncludeCandidate Include;
434 Include.Header = ToInclude->first;
435 if (ToInclude->second && ShouldInsert)
436 Include.Insertion = Includes.insert(
438 ? tooling::IncludeDirective::Import
439 : tooling::IncludeDirective::Include);
440 Completion.Includes.push_back(std::move(Include));
442 log(
"Failed to generate include insertion edits for adding header "
443 "(FileURI='{0}', IncludeHeader='{1}') into {2}: {3}",
444 C.IndexResult->CanonicalDeclaration.FileURI, Inc.Header,
FileName,
445 ToInclude.takeError());
448 std::stable_partition(Completion.Includes.begin(),
449 Completion.Includes.end(),
450 [](
const CodeCompletion::IncludeCandidate &I) {
451 return !I.Insertion.has_value();
455 void add(
const CompletionCandidate &
C, CodeCompletionString *SemaCCS,
456 CodeCompletionContext::Kind ContextKind) {
457 assert(
bool(
C.SemaResult) ==
bool(SemaCCS));
458 Bundled.emplace_back();
459 BundledEntry &S = Bundled.back();
460 bool IsConcept =
false;
462 getSignature(*SemaCCS, &S.Signature, &S.SnippetSuffix,
C.SemaResult->Kind,
463 C.SemaResult->CursorKind,
464 C.SemaResult->FunctionCanBeCall,
465 &Completion.RequiredQualifier);
467 if (
C.SemaResult->Kind == CodeCompletionResult::RK_Declaration)
468 if (
const auto *D =
C.SemaResult->getDeclaration())
469 if (isa<ConceptDecl>(D))
471 }
else if (
C.IndexResult) {
472 S.Signature = std::string(
C.IndexResult->Signature);
473 S.SnippetSuffix = std::string(
C.IndexResult->CompletionSnippetSuffix);
474 S.ReturnType = std::string(
C.IndexResult->ReturnType);
475 if (
C.IndexResult->SymInfo.Kind == index::SymbolKind::Concept)
482 if (IsConcept && ContextKind == CodeCompletionContext::CCC_TopLevel) {
483 S.Signature = removeFirstTemplateArg(S.Signature);
486 S.SnippetSuffix = removeFirstTemplateArg(S.SnippetSuffix);
489 if (!Completion.Documentation) {
490 auto SetDoc = [&](llvm::StringRef Doc) {
492 Completion.Documentation.emplace();
497 SetDoc(
C.IndexResult->Documentation);
498 }
else if (
C.SemaResult) {
504 if (Completion.Deprecated) {
506 Completion.Deprecated &=
507 C.SemaResult->Availability == CXAvailability_Deprecated;
509 Completion.Deprecated &=
514 CodeCompletion build() {
515 Completion.ReturnType = summarizeReturnType();
516 Completion.Signature = summarizeSignature();
517 Completion.SnippetSuffix = summarizeSnippet();
518 Completion.BundleSize = Bundled.size();
519 return std::move(Completion);
523 struct BundledEntry {
530 template <std::
string BundledEntry::*Member>
531 const std::string *onlyValue()
const {
532 auto B = Bundled.begin(),
E = Bundled.end();
533 for (
auto *I =
B + 1; I !=
E; ++I)
534 if (I->*Member !=
B->*Member)
536 return &(
B->*Member);
539 template <
bool BundledEntry::*Member>
const bool *onlyValue()
const {
540 auto B = Bundled.begin(),
E = Bundled.end();
541 for (
auto *I = B + 1; I !=
E; ++I)
542 if (I->*Member !=
B->*Member)
544 return &(
B->*Member);
547 std::string summarizeReturnType()
const {
548 if (
auto *RT = onlyValue<&BundledEntry::ReturnType>())
553 std::string summarizeSnippet()
const {
554 if (IsUsingDeclaration)
556 auto *
Snippet = onlyValue<&BundledEntry::SnippetSuffix>();
574 if (MayHaveArgList) {
578 if (NextTokenKind == tok::less &&
Snippet->front() ==
'<')
581 if (NextTokenKind == tok::l_paren) {
591 else if (
Snippet->at(I) ==
'<')
594 }
while (Balance > 0);
600 if (EnableFunctionArgSnippets)
604 if (MayHaveArgList) {
613 bool EmptyArgs = llvm::StringRef(*Snippet).endswith(
"()");
615 return EmptyArgs ?
"<$1>()$0" :
"<$1>($0)";
617 return EmptyArgs ?
"()" :
"($0)";
628 if (llvm::StringRef(*Snippet).endswith(
"<>"))
635 std::string summarizeSignature()
const {
636 if (
auto *
Signature = onlyValue<&BundledEntry::Signature>())
644 CodeCompletion Completion;
645 llvm::SmallVector<BundledEntry, 1> Bundled;
646 bool EnableFunctionArgSnippets;
649 bool IsUsingDeclaration;
650 tok::TokenKind NextTokenKind;
656 case CodeCompletionResult::RK_Declaration:
657 case CodeCompletionResult::RK_Pattern: {
663 case CodeCompletionResult::RK_Macro:
665 case CodeCompletionResult::RK_Keyword:
668 llvm_unreachable(
"unknown CodeCompletionResult kind");
673struct SpecifiedScope {
710 std::vector<std::string> scopesForQualification() {
720 std::vector<std::string> scopesForIndexQuery() {
722 std::vector<std::string> EnclosingAtFront;
724 EnclosingAtFront.push_back(*EnclosingNamespace);
725 std::set<std::string> Deduplicated;
726 for (llvm::StringRef S : QueryScopes)
727 if (S != EnclosingNamespace)
730 EnclosingAtFront.reserve(EnclosingAtFront.size() + Deduplicated.size());
731 llvm::copy(Deduplicated, std::back_inserter(EnclosingAtFront));
733 return EnclosingAtFront;
740SpecifiedScope getQueryScopes(CodeCompletionContext &
CCContext,
742 const CompletionPrefix &HeuristicPrefix,
743 const CodeCompleteOptions &Opts) {
744 SpecifiedScope Scopes;
745 for (
auto *Context :
CCContext.getVisitedContexts()) {
746 if (isa<TranslationUnitDecl>(Context)) {
747 Scopes.QueryScopes.push_back(
"");
748 Scopes.AccessibleScopes.push_back(
"");
749 }
else if (
const auto *ND = dyn_cast<NamespaceDecl>(Context)) {
755 const CXXScopeSpec *SemaSpecifier =
756 CCContext.getCXXScopeSpecifier().value_or(
nullptr);
758 if (!SemaSpecifier) {
761 if (!HeuristicPrefix.
Qualifier.empty()) {
762 vlog(
"Sema said no scope specifier, but we saw {0} in the source code",
764 StringRef SpelledSpecifier = HeuristicPrefix.
Qualifier;
765 if (SpelledSpecifier.consume_front(
"::")) {
766 Scopes.AccessibleScopes = {
""};
767 Scopes.QueryScopes = {
""};
769 Scopes.UnresolvedQualifier = std::string(SpelledSpecifier);
781 if (SemaSpecifier && SemaSpecifier->isValid())
785 Scopes.QueryScopes.push_back(
"");
786 llvm::StringRef SpelledSpecifier = Lexer::getSourceText(
787 CharSourceRange::getCharRange(SemaSpecifier->getRange()),
788 CCSema.SourceMgr, clang::LangOptions());
789 if (SpelledSpecifier.consume_front(
"::"))
790 Scopes.QueryScopes = {
""};
791 Scopes.UnresolvedQualifier = std::string(SpelledSpecifier);
793 if (!Scopes.UnresolvedQualifier->empty())
794 *Scopes.UnresolvedQualifier +=
"::";
796 Scopes.AccessibleScopes = Scopes.QueryScopes;
803bool contextAllowsIndex(
enum CodeCompletionContext::Kind
K) {
805 case CodeCompletionContext::CCC_TopLevel:
806 case CodeCompletionContext::CCC_ObjCInterface:
807 case CodeCompletionContext::CCC_ObjCImplementation:
808 case CodeCompletionContext::CCC_ObjCIvarList:
809 case CodeCompletionContext::CCC_ClassStructUnion:
810 case CodeCompletionContext::CCC_Statement:
811 case CodeCompletionContext::CCC_Expression:
812 case CodeCompletionContext::CCC_ObjCMessageReceiver:
813 case CodeCompletionContext::CCC_EnumTag:
814 case CodeCompletionContext::CCC_UnionTag:
815 case CodeCompletionContext::CCC_ClassOrStructTag:
816 case CodeCompletionContext::CCC_ObjCProtocolName:
817 case CodeCompletionContext::CCC_Namespace:
818 case CodeCompletionContext::CCC_Type:
819 case CodeCompletionContext::CCC_ParenthesizedExpression:
820 case CodeCompletionContext::CCC_ObjCInterfaceName:
821 case CodeCompletionContext::CCC_Symbol:
822 case CodeCompletionContext::CCC_SymbolOrNewName:
823 case CodeCompletionContext::CCC_ObjCClassForwardDecl:
824 case CodeCompletionContext::CCC_TopLevelOrExpression:
826 case CodeCompletionContext::CCC_OtherWithMacros:
827 case CodeCompletionContext::CCC_DotMemberAccess:
828 case CodeCompletionContext::CCC_ArrowMemberAccess:
829 case CodeCompletionContext::CCC_ObjCCategoryName:
830 case CodeCompletionContext::CCC_ObjCPropertyAccess:
831 case CodeCompletionContext::CCC_MacroName:
832 case CodeCompletionContext::CCC_MacroNameUse:
833 case CodeCompletionContext::CCC_PreprocessorExpression:
834 case CodeCompletionContext::CCC_PreprocessorDirective:
835 case CodeCompletionContext::CCC_SelectorName:
836 case CodeCompletionContext::CCC_TypeQualifiers:
837 case CodeCompletionContext::CCC_ObjCInstanceMessage:
838 case CodeCompletionContext::CCC_ObjCClassMessage:
839 case CodeCompletionContext::CCC_IncludedFile:
840 case CodeCompletionContext::CCC_Attribute:
842 case CodeCompletionContext::CCC_Other:
843 case CodeCompletionContext::CCC_NaturalLanguage:
844 case CodeCompletionContext::CCC_Recovery:
845 case CodeCompletionContext::CCC_NewName:
848 llvm_unreachable(
"unknown code completion context");
851static bool isInjectedClass(
const NamedDecl &D) {
852 if (
auto *R = dyn_cast_or_null<RecordDecl>(&D))
853 if (R->isInjectedClassName())
859static bool isExcludedMember(
const NamedDecl &D) {
862 if (D.getKind() == Decl::CXXDestructor)
865 if (isInjectedClass(D))
868 auto NameKind = D.getDeclName().getNameKind();
869 if (NameKind == DeclarationName::CXXOperatorName ||
870 NameKind == DeclarationName::CXXLiteralOperatorName ||
871 NameKind == DeclarationName::CXXConversionFunctionName)
882struct CompletionRecorder :
public CodeCompleteConsumer {
883 CompletionRecorder(
const CodeCompleteOptions &Opts,
884 llvm::unique_function<
void()> ResultsCallback)
885 : CodeCompleteConsumer(Opts.getClangCompleteOpts()),
886 CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
887 CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
888 CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) {
889 assert(this->ResultsCallback);
897 void ProcessCodeCompleteResults(
class Sema &S, CodeCompletionContext Context,
898 CodeCompletionResult *InResults,
899 unsigned NumResults)
final {
908 if (Context.getKind() == CodeCompletionContext::CCC_Recovery) {
909 log(
"Code complete: Ignoring sema code complete callback with Recovery "
916 if (NumResults == 0 && !contextAllowsIndex(Context.getKind()))
919 log(
"Multiple code complete callbacks (parser backtracked?). "
920 "Dropping results from context {0}, keeping results from {1}.",
921 getCompletionKindString(Context.getKind()),
922 getCompletionKindString(this->CCContext.getKind()));
930 for (
unsigned I = 0; I < NumResults; ++I) {
931 auto &Result = InResults[I];
933 if (Result.Hidden && Result.Declaration &&
934 Result.Declaration->isCXXClassMember())
936 if (!Opts.IncludeIneligibleResults &&
937 (Result.Availability == CXAvailability_NotAvailable ||
938 Result.Availability == CXAvailability_NotAccessible))
940 if (Result.Declaration &&
941 !Context.getBaseType().isNull()
942 && isExcludedMember(*Result.Declaration))
946 if (Result.Declaration && !Context.getCXXScopeSpecifier() &&
947 isInjectedClass(*Result.Declaration))
950 Result.StartsNestedNameSpecifier =
false;
956 CodeCompletionAllocator &getAllocator()
override {
return *CCAllocator; }
957 CodeCompletionTUInfo &getCodeCompletionTUInfo()
override {
return CCTUInfo; }
961 llvm::StringRef getName(
const CodeCompletionResult &Result) {
962 switch (Result.Kind) {
963 case CodeCompletionResult::RK_Declaration:
964 if (
auto *
ID = Result.Declaration->getIdentifier())
965 return ID->getName();
967 case CodeCompletionResult::RK_Keyword:
968 return Result.Keyword;
969 case CodeCompletionResult::RK_Macro:
970 return Result.Macro->getName();
971 case CodeCompletionResult::RK_Pattern:
974 auto *CCS = codeCompletionString(Result);
975 const CodeCompletionString::Chunk *OnlyText =
nullptr;
976 for (
auto &
C : *CCS) {
977 if (
C.Kind != CodeCompletionString::CK_TypedText)
980 return CCAllocator->CopyString(CCS->getAllTypedText());
983 return OnlyText ? OnlyText->Text : llvm::StringRef();
988 CodeCompletionString *codeCompletionString(
const CodeCompletionResult &R) {
990 return const_cast<CodeCompletionResult &
>(R).CreateCodeCompletionString(
991 *CCSema, CCContext, *CCAllocator, CCTUInfo,
996 CodeCompleteOptions Opts;
997 std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
998 CodeCompletionTUInfo CCTUInfo;
999 llvm::unique_function<void()> ResultsCallback;
1002struct ScoredSignature {
1015int paramIndexForArg(
const CodeCompleteConsumer::OverloadCandidate &
Candidate,
1017 int NumParams =
Candidate.getNumParams();
1018 if (
auto *T =
Candidate.getFunctionType()) {
1019 if (
auto *Proto = T->getAs<FunctionProtoType>()) {
1020 if (Proto->isVariadic())
1024 return std::min(Arg, std::max(NumParams - 1, 0));
1027class SignatureHelpCollector final :
public CodeCompleteConsumer {
1029 SignatureHelpCollector(
const clang::CodeCompleteOptions &CodeCompleteOpts,
1031 const SymbolIndex *Index, SignatureHelp &SigHelp)
1032 : CodeCompleteConsumer(CodeCompleteOpts), SigHelp(SigHelp),
1033 Allocator(std::make_shared<
clang::GlobalCodeCompletionAllocator>()),
1034 CCTUInfo(Allocator), Index(Index),
1035 DocumentationFormat(DocumentationFormat) {}
1037 void ProcessOverloadCandidates(Sema &S,
unsigned CurrentArg,
1038 OverloadCandidate *Candidates,
1039 unsigned NumCandidates,
1040 SourceLocation OpenParLoc,
1041 bool Braced)
override {
1042 assert(!OpenParLoc.isInvalid());
1043 SourceManager &SrcMgr = S.getSourceManager();
1044 OpenParLoc = SrcMgr.getFileLoc(OpenParLoc);
1045 if (SrcMgr.isInMainFile(OpenParLoc))
1048 elog(
"Location oustide main file in signature help: {0}",
1049 OpenParLoc.printToString(SrcMgr));
1051 std::vector<ScoredSignature> ScoredSignatures;
1052 SigHelp.signatures.reserve(NumCandidates);
1053 ScoredSignatures.reserve(NumCandidates);
1057 SigHelp.activeSignature = 0;
1058 assert(CurrentArg <= (
unsigned)std::numeric_limits<int>::max() &&
1059 "too many arguments");
1061 SigHelp.activeParameter =
static_cast<int>(CurrentArg);
1063 for (
unsigned I = 0; I < NumCandidates; ++I) {
1064 OverloadCandidate
Candidate = Candidates[I];
1068 if (
auto *Func =
Candidate.getFunction()) {
1069 if (
auto *Pattern = Func->getTemplateInstantiationPattern())
1072 if (
static_cast<int>(I) == SigHelp.activeSignature) {
1077 SigHelp.activeParameter =
1078 paramIndexForArg(
Candidate, SigHelp.activeParameter);
1081 const auto *CCS =
Candidate.CreateSignatureString(
1082 CurrentArg, S, *Allocator, CCTUInfo,
1084 assert(CCS &&
"Expected the CodeCompletionString to be non-null");
1085 ScoredSignatures.push_back(processOverloadCandidate(
1094 llvm::DenseMap<SymbolID, std::string> FetchedDocs;
1096 LookupRequest IndexRequest;
1097 for (
const auto &S : ScoredSignatures) {
1100 IndexRequest.IDs.insert(S.IDForDoc);
1102 Index->lookup(IndexRequest, [&](
const Symbol &S) {
1103 if (!S.Documentation.empty())
1104 FetchedDocs[S.ID] = std::string(S.Documentation);
1106 vlog(
"SigHelp: requested docs for {0} symbols from the index, got {1} "
1107 "symbols with non-empty docs in the response",
1108 IndexRequest.IDs.size(), FetchedDocs.size());
1111 llvm::sort(ScoredSignatures, [](
const ScoredSignature &L,
1112 const ScoredSignature &R) {
1119 if (L.Quality.NumberOfParameters != R.Quality.NumberOfParameters)
1120 return L.Quality.NumberOfParameters < R.Quality.NumberOfParameters;
1121 if (L.Quality.NumberOfOptionalParameters !=
1122 R.Quality.NumberOfOptionalParameters)
1123 return L.Quality.NumberOfOptionalParameters <
1124 R.Quality.NumberOfOptionalParameters;
1125 if (L.Quality.Kind != R.Quality.Kind) {
1126 using OC = CodeCompleteConsumer::OverloadCandidate;
1127 auto KindPriority = [&](OC::CandidateKind K) {
1129 case OC::CK_Aggregate:
1131 case OC::CK_Function:
1133 case OC::CK_FunctionType:
1135 case OC::CK_FunctionProtoTypeLoc:
1137 case OC::CK_FunctionTemplate:
1139 case OC::CK_Template:
1142 llvm_unreachable(
"Unknown overload candidate type.");
1144 return KindPriority(L.Quality.Kind) < KindPriority(R.Quality.Kind);
1146 if (L.Signature.label.size() != R.Signature.label.size())
1147 return L.Signature.label.size() < R.Signature.label.size();
1148 return L.Signature.label < R.Signature.label;
1151 for (
auto &SS : ScoredSignatures) {
1153 SS.IDForDoc ? FetchedDocs.find(SS.IDForDoc) : FetchedDocs.end();
1154 if (IndexDocIt != FetchedDocs.end()) {
1155 markup::Document SignatureComment;
1157 SS.Signature.documentation =
1158 renderDoc(SignatureComment, DocumentationFormat);
1161 SigHelp.signatures.push_back(std::move(SS.Signature));
1165 GlobalCodeCompletionAllocator &getAllocator()
override {
return *Allocator; }
1167 CodeCompletionTUInfo &getCodeCompletionTUInfo()
override {
return CCTUInfo; }
1170 void processParameterChunk(llvm::StringRef ChunkText,
1171 SignatureInformation &
Signature)
const {
1174 unsigned ParamEndOffset = ParamStartOffset +
lspLength(ChunkText);
1179 ParameterInformation
Info;
1180 Info.labelOffsets.emplace(ParamStartOffset, ParamEndOffset);
1182 Info.labelString = std::string(ChunkText);
1187 void processOptionalChunk(
const CodeCompletionString &CCS,
1189 SignatureQualitySignals &Signal)
const {
1190 for (
const auto &Chunk : CCS) {
1191 switch (Chunk.Kind) {
1192 case CodeCompletionString::CK_Optional:
1193 assert(Chunk.Optional &&
1194 "Expected the optional code completion string to be non-null.");
1195 processOptionalChunk(*Chunk.Optional,
Signature, Signal);
1197 case CodeCompletionString::CK_VerticalSpace:
1199 case CodeCompletionString::CK_CurrentParameter:
1200 case CodeCompletionString::CK_Placeholder:
1201 processParameterChunk(Chunk.Text,
Signature);
1202 Signal.NumberOfOptionalParameters++;
1213 ScoredSignature processOverloadCandidate(
const OverloadCandidate &
Candidate,
1214 const CodeCompletionString &CCS,
1215 llvm::StringRef DocComment)
const {
1217 SignatureQualitySignals Signal;
1220 markup::Document OverloadComment;
1222 Signature.documentation = renderDoc(OverloadComment, DocumentationFormat);
1225 for (
const auto &Chunk : CCS) {
1226 switch (Chunk.Kind) {
1227 case CodeCompletionString::CK_ResultType:
1230 assert(!
ReturnType &&
"Unexpected CK_ResultType");
1233 case CodeCompletionString::CK_CurrentParameter:
1234 case CodeCompletionString::CK_Placeholder:
1235 processParameterChunk(Chunk.Text,
Signature);
1236 Signal.NumberOfParameters++;
1238 case CodeCompletionString::CK_Optional: {
1240 assert(Chunk.Optional &&
1241 "Expected the optional code completion string to be non-null.");
1242 processOptionalChunk(*Chunk.Optional,
Signature, Signal);
1245 case CodeCompletionString::CK_VerticalSpace:
1257 ScoredSignature Result;
1258 Result.Signature = std::move(
Signature);
1259 Result.Quality = Signal;
1260 const FunctionDecl *Func =
Candidate.getFunction();
1261 if (Func && Result.Signature.documentation.value.empty()) {
1269 SignatureHelp &SigHelp;
1270 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
1271 CodeCompletionTUInfo CCTUInfo;
1272 const SymbolIndex *Index;
1278class ParamNameCollector final :
public CodeCompleteConsumer {
1280 ParamNameCollector(
const clang::CodeCompleteOptions &CodeCompleteOpts,
1281 std::set<std::string> &ParamNames)
1282 : CodeCompleteConsumer(CodeCompleteOpts),
1283 Allocator(std::make_shared<
clang::GlobalCodeCompletionAllocator>()),
1284 CCTUInfo(Allocator), ParamNames(ParamNames) {}
1286 void ProcessOverloadCandidates(Sema &S,
unsigned CurrentArg,
1287 OverloadCandidate *Candidates,
1288 unsigned NumCandidates,
1289 SourceLocation OpenParLoc,
1290 bool Braced)
override {
1291 assert(CurrentArg <= (
unsigned)std::numeric_limits<int>::max() &&
1292 "too many arguments");
1294 for (
unsigned I = 0; I < NumCandidates; ++I) {
1295 if (
const NamedDecl *ND = Candidates[I].getParamDecl(CurrentArg))
1296 if (
const auto *II = ND->getIdentifier())
1297 ParamNames.emplace(II->getName());
1302 GlobalCodeCompletionAllocator &getAllocator()
override {
return *Allocator; }
1304 CodeCompletionTUInfo &getCodeCompletionTUInfo()
override {
return CCTUInfo; }
1306 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
1307 CodeCompletionTUInfo CCTUInfo;
1308 std::set<std::string> &ParamNames;
1311struct SemaCompleteInput {
1315 const std::optional<PreamblePatch>
Patch;
1319void loadMainFilePreambleMacros(
const Preprocessor &PP,
1324 ExternalPreprocessorSource *PreambleMacros = PP.getExternalSource();
1327 const auto &ITable = PP.getIdentifierTable();
1328 IdentifierInfoLookup *PreambleIdentifiers =
1329 ITable.getExternalIdentifierLookup();
1331 if (!PreambleIdentifiers || !PreambleMacros)
1334 if (ITable.find(
MacroName.getKey()) != ITable.end())
1336 if (
auto *II = PreambleIdentifiers->get(
MacroName.getKey()))
1337 if (II->isOutOfDate())
1338 PreambleMacros->updateOutOfDateIdentifier(*II);
1344bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
1345 const clang::CodeCompleteOptions &Options,
1346 const SemaCompleteInput &Input,
1347 IncludeStructure *Includes =
nullptr) {
1348 trace::Span Tracer(
"Sema completion");
1353 elog(
"Couldn't create CompilerInvocation");
1356 auto &FrontendOpts =
CI->getFrontendOpts();
1357 FrontendOpts.SkipFunctionBodies =
true;
1359 CI->getLangOpts().SpellChecking =
false;
1363 CI->getLangOpts().DelayedTemplateParsing =
false;
1365 FrontendOpts.CodeCompleteOpts = Options;
1366 FrontendOpts.CodeCompletionAt.FileName = std::string(Input.FileName);
1367 std::tie(FrontendOpts.CodeCompletionAt.Line,
1368 FrontendOpts.CodeCompletionAt.Column) =
1371 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1372 llvm::MemoryBuffer::getMemBuffer(Input.ParseInput.Contents,
1375 CI->getDiagnosticOpts().IgnoreWarnings =
true;
1382 PreambleBounds PreambleRegion =
1383 ComputePreambleBounds(
CI->getLangOpts(), *ContentsBuffer, 0);
1384 bool CompletingInPreamble = Input.Offset < PreambleRegion.Size ||
1385 (!PreambleRegion.PreambleEndsAtStartOfLine &&
1386 Input.Offset == PreambleRegion.Size);
1388 Input.Patch->apply(*
CI);
1391 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
1392 Input.ParseInput.TFS->view(Input.ParseInput.CompileCommand.Directory);
1393 if (Input.Preamble.StatCache)
1394 VFS = Input.Preamble.StatCache->getConsumingFS(std::move(VFS));
1396 std::move(
CI), !CompletingInPreamble ? &Input.Preamble.Preamble :
nullptr,
1397 std::move(ContentsBuffer), std::move(VFS),
IgnoreDiags);
1398 Clang->getPreprocessorOpts().SingleFileParseMode = CompletingInPreamble;
1399 Clang->setCodeCompletionConsumer(Consumer.release());
1403 log(
"BeginSourceFile() failed when running codeComplete for {0}",
1413 loadMainFilePreambleMacros(
Clang->getPreprocessor(), Input.Preamble);
1416 if (llvm::Error Err =
Action.Execute()) {
1417 log(
"Execute() failed when running codeComplete for {0}: {1}",
1418 Input.FileName,
toString(std::move(Err)));
1427bool allowIndex(CodeCompletionContext &
CC) {
1428 if (!contextAllowsIndex(
CC.getKind()))
1431 auto Scope =
CC.getCXXScopeSpecifier();
1434 NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
1439 switch (NameSpec->getKind()) {
1440 case NestedNameSpecifier::Global:
1441 case NestedNameSpecifier::Namespace:
1442 case NestedNameSpecifier::NamespaceAlias:
1444 case NestedNameSpecifier::Super:
1445 case NestedNameSpecifier::TypeSpec:
1446 case NestedNameSpecifier::TypeSpecWithTemplate:
1448 case NestedNameSpecifier::Identifier:
1451 llvm_unreachable(
"invalid NestedNameSpecifier kind");
1456bool includeSymbolFromIndex(CodeCompletionContext::Kind
Kind,
1457 const Symbol &Sym) {
1461 if (Sym.SymInfo.Kind == index::SymbolKind::Protocol &&
1462 Sym.SymInfo.Lang == index::SymbolLanguage::ObjC)
1463 return Kind == CodeCompletionContext::CCC_ObjCProtocolName;
1464 else if (
Kind == CodeCompletionContext::CCC_ObjCProtocolName)
1468 if (
Kind == CodeCompletionContext::CCC_ObjCClassForwardDecl)
1469 return Sym.SymInfo.Kind == index::SymbolKind::Class &&
1470 Sym.SymInfo.Lang == index::SymbolLanguage::ObjC;
1474std::future<std::pair<bool, SymbolSlab>>
1475startAsyncFuzzyFind(
const SymbolIndex &Index,
const FuzzyFindRequest &Req) {
1476 return runAsync<std::pair<bool, SymbolSlab>>([&Index, Req]() {
1477 trace::Span Tracer(
"Async fuzzyFind");
1478 SymbolSlab::Builder Syms;
1480 Index.
fuzzyFind(Req, [&Syms](
const Symbol &Sym) { Syms.insert(Sym); });
1481 return std::make_pair(Incomplete, std::move(Syms).build());
1488FuzzyFindRequest speculativeFuzzyFindRequestForCompletion(
1489 FuzzyFindRequest CachedReq,
const CompletionPrefix &HeuristicPrefix) {
1490 CachedReq.Query = std::string(HeuristicPrefix.
Name);
1498findTokenAfterCompletionPoint(SourceLocation CompletionPoint,
1499 const SourceManager &SM,
1500 const LangOptions &LangOpts) {
1501 SourceLocation
Loc = CompletionPoint;
1502 if (
Loc.isMacroID()) {
1503 if (!Lexer::isAtEndOfMacroExpansion(
Loc, SM, LangOpts, &
Loc))
1504 return std::nullopt;
1512 Loc =
Loc.getLocWithOffset(1);
1515 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(
Loc);
1518 bool InvalidTemp =
false;
1519 StringRef
File = SM.getBufferData(LocInfo.first, &InvalidTemp);
1521 return std::nullopt;
1523 const char *TokenBegin =
File.data() + LocInfo.second;
1526 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts,
File.begin(),
1527 TokenBegin,
File.end());
1530 TheLexer.LexFromRawLexer(Tok);
1563class CodeCompleteFlow {
1565 IncludeStructure Includes;
1566 SpeculativeFuzzyFind *SpecFuzzyFind;
1567 const CodeCompleteOptions &Opts;
1570 CompletionRecorder *Recorder =
nullptr;
1571 CodeCompletionContext::Kind CCContextKind = CodeCompletionContext::CCC_Other;
1572 bool IsUsingDeclaration =
false;
1576 tok::TokenKind NextTokenKind = tok::eof;
1578 int NSema = 0, NIndex = 0, NSemaAndIndex = 0, NIdent = 0;
1579 bool Incomplete =
false;
1580 CompletionPrefix HeuristicPrefix;
1581 std::optional<FuzzyMatcher> Filter;
1582 Range ReplacedRange;
1586 std::optional<ScopeDistance> ScopeProximity;
1587 std::optional<OpaqueType> PreferredType;
1589 bool AllScopes =
false;
1590 llvm::StringSet<> ContextWords;
1593 std::optional<IncludeInserter> Inserter;
1594 std::optional<URIDistance> FileProximity;
1599 std::optional<FuzzyFindRequest> SpecReq;
1603 CodeCompleteFlow(
PathRef FileName,
const IncludeStructure &Includes,
1604 SpeculativeFuzzyFind *SpecFuzzyFind,
1605 const CodeCompleteOptions &Opts)
1609 CodeCompleteResult
run(
const SemaCompleteInput &SemaCCInput) && {
1610 trace::Span Tracer(
"CodeCompleteFlow");
1612 SemaCCInput.Offset);
1613 populateContextWords(SemaCCInput.ParseInput.Contents);
1614 if (Opts.Index && SpecFuzzyFind && SpecFuzzyFind->CachedReq) {
1615 assert(!SpecFuzzyFind->Result.valid());
1616 SpecReq = speculativeFuzzyFindRequestForCompletion(
1617 *SpecFuzzyFind->CachedReq, HeuristicPrefix);
1618 SpecFuzzyFind->Result = startAsyncFuzzyFind(*Opts.Index, *SpecReq);
1624 CodeCompleteResult
Output;
1625 auto RecorderOwner = std::make_unique<CompletionRecorder>(Opts, [&]() {
1626 assert(Recorder &&
"Recorder is not set");
1627 CCContextKind = Recorder->CCContext.getKind();
1628 IsUsingDeclaration = Recorder->CCContext.isUsingDeclaration();
1630 SemaCCInput.ParseInput.Contents,
1631 *SemaCCInput.ParseInput.TFS);
1632 const auto NextToken = findTokenAfterCompletionPoint(
1633 Recorder->CCSema->getPreprocessor().getCodeCompletionLoc(),
1634 Recorder->CCSema->getSourceManager(), Recorder->CCSema->LangOpts);
1636 NextTokenKind = NextToken->getKind();
1640 SemaCCInput.FileName, SemaCCInput.ParseInput.Contents, Style,
1641 SemaCCInput.ParseInput.CompileCommand.Directory,
1642 &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo());
1643 for (
const auto &Inc : Includes.MainFileIncludes)
1644 Inserter->addExisting(Inc);
1650 FileDistanceOptions ProxOpts{};
1651 const auto &SM = Recorder->CCSema->getSourceManager();
1652 llvm::StringMap<SourceParams> ProxSources;
1654 Includes.getID(SM.getFileEntryForID(SM.getMainFileID()));
1656 for (
auto &HeaderIDAndDepth : Includes.includeDepth(*MainFileID)) {
1658 ProxSources[Includes.getRealPath(HeaderIDAndDepth.getFirst())];
1659 Source.Cost = HeaderIDAndDepth.getSecond() * ProxOpts.IncludeCost;
1663 if (HeaderIDAndDepth.getSecond() > 0)
1664 Source.MaxUpTraversals = 1;
1666 FileProximity.emplace(ProxSources, ProxOpts);
1671 getCompletionKindString(CCContextKind));
1672 log(
"Code complete: sema context {0}, query scopes [{1}] (AnyScope={2}), "
1673 "expected type {3}{4}",
1674 getCompletionKindString(CCContextKind),
1676 PreferredType ? Recorder->CCContext.getPreferredType().getAsString()
1678 IsUsingDeclaration ?
", inside using declaration" :
"");
1681 Recorder = RecorderOwner.get();
1683 semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
1684 SemaCCInput, &Includes);
1685 logResults(
Output, Tracer);
1689 void logResults(
const CodeCompleteResult &
Output,
const trace::Span &Tracer) {
1692 SPAN_ATTACH(Tracer,
"merged_results", NSemaAndIndex);
1693 SPAN_ATTACH(Tracer,
"identifier_results", NIdent);
1696 log(
"Code complete: {0} results from Sema, {1} from Index, "
1697 "{2} matched, {3} from identifiers, {4} returned{5}.",
1698 NSema, NIndex, NSemaAndIndex, NIdent,
Output.Completions.size(),
1699 Output.HasMore ?
" (incomplete)" :
"");
1700 assert(!Opts.Limit ||
Output.Completions.size() <= Opts.Limit);
1705 CodeCompleteResult runWithoutSema(llvm::StringRef Content,
size_t Offset,
1706 const ThreadsafeFS &TFS) && {
1707 trace::Span Tracer(
"CodeCompleteWithoutSema");
1710 populateContextWords(Content);
1711 CCContextKind = CodeCompletionContext::CCC_Recovery;
1712 IsUsingDeclaration =
false;
1713 Filter = FuzzyMatcher(HeuristicPrefix.Name);
1715 ReplacedRange.start = ReplacedRange.end =
Pos;
1716 ReplacedRange.start.character -= HeuristicPrefix.Name.size();
1718 llvm::StringMap<SourceParams> ProxSources;
1720 FileProximity.emplace(ProxSources);
1724 Inserter.emplace(FileName, Content, Style,
1728 std::vector<RawIdentifier> IdentifierResults;
1729 for (
const auto &IDAndCount : Identifiers) {
1731 ID.Name = IDAndCount.first();
1732 ID.References = IDAndCount.second;
1734 if (
ID.Name == HeuristicPrefix.Name)
1736 if (
ID.References > 0)
1737 IdentifierResults.push_back(std::move(
ID));
1743 SpecifiedScope Scopes;
1745 Content.take_front(
Offset), format::getFormattingLangOpts(Style));
1746 for (std::string &S : Scopes.QueryScopes)
1749 if (HeuristicPrefix.Qualifier.empty())
1750 AllScopes = Opts.AllScopes;
1751 else if (HeuristicPrefix.Qualifier.startswith(
"::")) {
1752 Scopes.QueryScopes = {
""};
1753 Scopes.UnresolvedQualifier =
1754 std::string(HeuristicPrefix.Qualifier.drop_front(2));
1756 Scopes.UnresolvedQualifier = std::string(HeuristicPrefix.Qualifier);
1760 ScopeProximity.emplace(QueryScopes);
1762 SymbolSlab IndexResults = Opts.Index ? queryIndex() : SymbolSlab();
1764 CodeCompleteResult
Output = toCodeCompleteResult(mergeResults(
1765 {}, IndexResults, IdentifierResults));
1766 Output.RanParser =
false;
1767 logResults(
Output, Tracer);
1772 void populateContextWords(llvm::StringRef Content) {
1774 unsigned RangeEnd = HeuristicPrefix.Qualifier.begin() - Content.data(),
1775 RangeBegin = RangeEnd;
1776 for (
size_t I = 0; I < 3 && RangeBegin > 0; ++I) {
1777 auto PrevNL = Content.rfind(
'\n', RangeBegin);
1778 if (PrevNL == StringRef::npos) {
1782 RangeBegin = PrevNL;
1785 ContextWords =
collectWords(Content.slice(RangeBegin, RangeEnd));
1786 dlog(
"Completion context words: {0}",
1787 llvm::join(ContextWords.keys(),
", "));
1792 CodeCompleteResult runWithSema() {
1793 const auto &CodeCompletionRange = CharSourceRange::getCharRange(
1794 Recorder->CCSema->getPreprocessor().getCodeCompletionTokenRange());
1800 if (CodeCompletionRange.isValid()) {
1802 CodeCompletionRange);
1805 Recorder->CCSema->getSourceManager(),
1806 Recorder->CCSema->getPreprocessor().getCodeCompletionLoc());
1807 ReplacedRange.start = ReplacedRange.end =
Pos;
1809 Filter = FuzzyMatcher(
1810 Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
1811 auto SpecifiedScopes = getQueryScopes(
1812 Recorder->CCContext, *Recorder->CCSema, HeuristicPrefix, Opts);
1814 QueryScopes = SpecifiedScopes.scopesForIndexQuery();
1816 AllScopes = SpecifiedScopes.AllowAllScopes;
1818 ScopeProximity.emplace(QueryScopes);
1821 Recorder->CCContext.getPreferredType());
1827 auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
1830 trace::Span Tracer(
"Populate CodeCompleteResult");
1833 mergeResults(Recorder->Results, IndexResults, {});
1834 return toCodeCompleteResult(Top);
1838 toCodeCompleteResult(
const std::vector<ScoredBundle> &Scored) {
1839 CodeCompleteResult
Output;
1842 for (
auto &
C : Scored) {
1843 Output.Completions.push_back(toCodeCompletion(
C.first));
1844 Output.Completions.back().Score =
C.second;
1845 Output.Completions.back().CompletionTokenRange = ReplacedRange;
1847 Output.HasMore = Incomplete;
1848 Output.Context = CCContextKind;
1849 Output.CompletionRange = ReplacedRange;
1853 SymbolSlab queryIndex() {
1854 trace::Span Tracer(
"Query index");
1855 SPAN_ATTACH(Tracer,
"limit", int64_t(Opts.Limit));
1858 FuzzyFindRequest Req;
1860 Req.Limit = Opts.Limit;
1861 Req.Query = std::string(Filter->pattern());
1862 Req.RestrictForCodeCompletion =
true;
1864 Req.AnyScope = AllScopes;
1866 Req.ProximityPaths.push_back(std::string(FileName));
1868 Req.PreferredTypes.push_back(std::string(PreferredType->raw()));
1869 vlog(
"Code complete: fuzzyFind({0:2})",
toJSON(Req));
1872 SpecFuzzyFind->NewReq = Req;
1873 if (SpecFuzzyFind && SpecFuzzyFind->Result.valid() && (*SpecReq == Req)) {
1874 vlog(
"Code complete: speculative fuzzy request matches the actual index "
1875 "request. Waiting for the speculative index results.");
1878 trace::Span WaitSpec(
"Wait speculative results");
1879 auto SpecRes = SpecFuzzyFind->Result.get();
1880 Incomplete |= SpecRes.first;
1881 return std::move(SpecRes.second);
1884 SPAN_ATTACH(Tracer,
"Speculative results",
false);
1887 SymbolSlab::Builder ResultsBuilder;
1888 Incomplete |= Opts.Index->fuzzyFind(
1889 Req, [&](
const Symbol &Sym) { ResultsBuilder.insert(Sym); });
1890 return std::move(ResultsBuilder).build();
1898 std::vector<ScoredBundle>
1899 mergeResults(
const std::vector<CodeCompletionResult> &SemaResults,
1900 const SymbolSlab &IndexResults,
1901 const std::vector<RawIdentifier> &IdentifierResults) {
1902 trace::Span Tracer(
"Merge and score results");
1903 std::vector<CompletionCandidate::Bundle> Bundles;
1904 llvm::DenseMap<size_t, size_t> BundleLookup;
1905 auto AddToBundles = [&](
const CodeCompletionResult *
SemaResult,
1908 CompletionCandidate
C;
1912 if (
C.IndexResult) {
1915 }
else if (
C.SemaResult) {
1921 if (
auto OverloadSet =
C.overloadSet(
1922 Opts, FileName, Inserter ? &*Inserter :
nullptr, CCContextKind)) {
1923 auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1925 Bundles.emplace_back();
1926 Bundles[Ret.first->second].push_back(std::move(
C));
1928 Bundles.emplace_back();
1929 Bundles.back().push_back(std::move(
C));
1932 llvm::DenseSet<const Symbol *> UsedIndexResults;
1933 auto CorrespondingIndexResult =
1934 [&](
const CodeCompletionResult &
SemaResult) ->
const Symbol * {
1937 auto I = IndexResults.find(SymID);
1938 if (I != IndexResults.end()) {
1939 UsedIndexResults.insert(&*I);
1952 if (!includeSymbolFromIndex(CCContextKind,
IndexResult))
1957 for (
const auto &Ident : IdentifierResults)
1958 AddToBundles(
nullptr,
nullptr, &Ident);
1960 TopN<ScoredBundle, ScoredBundleGreater> Top(
1961 Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1962 for (
auto &Bundle : Bundles)
1963 addCandidate(Top, std::move(Bundle));
1964 return std::move(Top).items();
1967 std::optional<float> fuzzyScore(
const CompletionCandidate &
C) {
1969 if (((
C.SemaResult &&
1970 C.SemaResult->Kind == CodeCompletionResult::RK_Macro) ||
1972 C.IndexResult->SymInfo.Kind == index::SymbolKind::Macro)) &&
1973 !
C.Name.starts_with_insensitive(Filter->pattern()))
1974 return std::nullopt;
1975 return Filter->match(
C.Name);
1978 CodeCompletion::Scores
1979 evaluateCompletion(
const SymbolQualitySignals &
Quality,
1980 const SymbolRelevanceSignals &Relevance) {
1982 CodeCompletion::Scores Scores;
1983 switch (Opts.RankingModel) {
1984 case RM::Heuristics:
1985 Scores.Quality =
Quality.evaluateHeuristics();
1986 Scores.Relevance = Relevance.evaluateHeuristics();
1991 Scores.ExcludingName =
1992 Relevance.NameMatch > std::numeric_limits<float>::epsilon()
1993 ? Scores.Total / Relevance.NameMatch
1997 case RM::DecisionForest:
1998 DecisionForestScores DFScores = Opts.DecisionForestScorer(
1999 Quality, Relevance, Opts.DecisionForestBase);
2000 Scores.ExcludingName = DFScores.ExcludingName;
2001 Scores.Total = DFScores.Total;
2004 llvm_unreachable(
"Unhandled CodeCompletion ranking model.");
2008 void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
2009 CompletionCandidate::Bundle Bundle) {
2011 SymbolRelevanceSignals Relevance;
2012 Relevance.Context = CCContextKind;
2013 Relevance.Name = Bundle.front().Name;
2014 Relevance.FilterLength = HeuristicPrefix.Name.size();
2016 Relevance.FileProximityMatch = &*FileProximity;
2018 Relevance.ScopeProximityMatch = &*ScopeProximity;
2020 Relevance.HadContextType =
true;
2021 Relevance.ContextWords = &ContextWords;
2022 Relevance.MainFileSignals = Opts.MainFileSignals;
2024 auto &First = Bundle.front();
2025 if (
auto FuzzyScore = fuzzyScore(First))
2026 Relevance.NameMatch = *FuzzyScore;
2030 bool FromIndex =
false;
2034 Relevance.merge(*
Candidate.IndexResult);
2035 Origin |=
Candidate.IndexResult->Origin;
2037 if (!
Candidate.IndexResult->Type.empty())
2038 Relevance.HadSymbolType |=
true;
2039 if (PreferredType &&
2040 PreferredType->raw() ==
Candidate.IndexResult->Type) {
2041 Relevance.TypeMatchesPreferred =
true;
2047 if (PreferredType) {
2049 Recorder->CCSema->getASTContext(), *
Candidate.SemaResult)) {
2050 Relevance.HadSymbolType |=
true;
2051 if (PreferredType == CompletionType)
2052 Relevance.TypeMatchesPreferred =
true;
2064 CodeCompletion::Scores Scores = evaluateCompletion(
Quality, Relevance);
2065 if (Opts.RecordCCResult)
2066 Opts.RecordCCResult(toCodeCompletion(Bundle),
Quality, Relevance,
2069 dlog(
"CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First.Name,
2070 llvm::to_string(Origin), Scores.Total, llvm::to_string(
Quality),
2071 llvm::to_string(Relevance));
2074 NIndex += FromIndex;
2077 if (Candidates.push({std::move(Bundle), Scores}))
2081 CodeCompletion toCodeCompletion(
const CompletionCandidate::Bundle &Bundle) {
2082 std::optional<CodeCompletionBuilder>
Builder;
2083 for (
const auto &Item : Bundle) {
2084 CodeCompletionString *SemaCCS =
2085 Item.SemaResult ? Recorder->codeCompletionString(*Item.SemaResult)
2088 Builder.emplace(Recorder ? &Recorder->CCSema->getASTContext() :
nullptr,
2089 Item, SemaCCS, AccessibleScopes, *Inserter, FileName,
2090 CCContextKind, Opts, IsUsingDeclaration, NextTokenKind);
2092 Builder->add(Item, SemaCCS, CCContextKind);
2100clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts()
const {
2101 clang::CodeCompleteOptions Result;
2102 Result.IncludeCodePatterns = EnableSnippets;
2103 Result.IncludeMacros =
true;
2104 Result.IncludeGlobals =
true;
2109 Result.IncludeBriefComments =
false;
2114 Result.LoadExternal = !Index;
2115 Result.IncludeFixIts = IncludeFixIts;
2122 assert(
Offset <= Content.size());
2123 StringRef Rest = Content.take_front(
Offset);
2128 while (!Rest.empty() && isAsciiIdentifierContinue(Rest.back()))
2129 Rest = Rest.drop_back();
2130 Result.Name = Content.slice(Rest.size(),
Offset);
2133 while (Rest.consume_back(
"::") && !Rest.endswith(
":"))
2134 while (!Rest.empty() && isAsciiIdentifierContinue(Rest.back()))
2135 Rest = Rest.drop_back();
2137 Content.slice(Rest.size(), Result.Name.begin() - Content.begin());
2146 llvm::StringRef Prefix,
2152 clang::CodeCompleteOptions Options;
2153 Options.IncludeGlobals =
false;
2154 Options.IncludeMacros =
false;
2155 Options.IncludeCodePatterns =
false;
2156 Options.IncludeBriefComments =
false;
2157 std::set<std::string> ParamNames;
2161 std::make_unique<ParamNameCollector>(Options, ParamNames), Options,
2165 if (ParamNames.empty())
2169 Range CompletionRange;
2173 CompletionRange.
end =
2175 Result.CompletionRange = CompletionRange;
2176 Result.Context = CodeCompletionContext::CCC_NaturalLanguage;
2177 for (llvm::StringRef
Name : ParamNames) {
2178 if (!
Name.startswith(Prefix))
2183 Item.
Kind = CompletionItemKind::Text;
2185 Item.
Origin = SymbolOrigin::AST;
2186 Result.Completions.push_back(Item);
2195std::optional<unsigned>
2197 while (!Content.empty() && isAsciiIdentifierContinue(Content.back()))
2198 Content = Content.drop_back();
2199 Content = Content.rtrim();
2200 if (Content.endswith(
"/*"))
2201 return Content.size() - 2;
2202 return std::nullopt;
2212 elog(
"Code completion position was invalid {0}",
Offset.takeError());
2223 auto CommentPrefix = Content.substr(*OffsetBeforeComment + 2).trim();
2228 auto Flow = CodeCompleteFlow(
2230 SpecFuzzyFind, Opts);
2231 return (!
Preamble || Opts.RunParser == CodeCompleteOptions::NeverParse)
2236 PreamblePatch::createMacroPatch(
2247 elog(
"Signature help position was invalid {0}",
Offset.takeError());
2251 clang::CodeCompleteOptions Options;
2252 Options.IncludeGlobals =
false;
2253 Options.IncludeMacros =
false;
2254 Options.IncludeCodePatterns =
false;
2255 Options.IncludeBriefComments =
false;
2257 std::make_unique<SignatureHelpCollector>(Options, DocumentationFormat,
2260 {FileName, *Offset, Preamble,
2261 PreamblePatch::createFullPatch(FileName, ParseInput, Preamble),
2267 auto InTopLevelScope = [](
const NamedDecl &ND) {
2268 switch (ND.getDeclContext()->getDeclKind()) {
2269 case Decl::TranslationUnit:
2270 case Decl::Namespace:
2271 case Decl::LinkageSpec:
2278 auto InClassScope = [](
const NamedDecl &ND) {
2279 return ND.getDeclContext()->getDeclKind() == Decl::CXXRecord;
2290 if (llvm::isa<ObjCCategoryDecl>(&ND) || llvm::isa<ObjCCategoryImplDecl>(&ND))
2293 if (InTopLevelScope(ND))
2299 if (
const auto *EnumDecl = dyn_cast<clang::EnumDecl>(ND.getDeclContext()))
2300 return (InTopLevelScope(*EnumDecl) || InClassScope(*EnumDecl));
2307 const auto *InsertInclude = Includes.empty() ? nullptr : &Includes[0];
2310 LSP.
label = ((InsertInclude && InsertInclude->Insertion)
2311 ? Opts.IncludeIndicator.Insert
2312 : Opts.IncludeIndicator.NoInsert) +
2313 (Opts.ShowOrigins ?
"[" + llvm::to_string(Origin) +
"]" :
"") +
2314 RequiredQualifier +
Name;
2319 LSP.
detail = BundleSize > 1
2320 ? std::string(llvm::formatv(
"[{0} overloads]", BundleSize))
2325 if (InsertInclude || Documentation) {
2330 Doc.
append(*Documentation);
2331 LSP.
documentation = renderDoc(Doc, Opts.DocumentationFormat);
2335 LSP.
textEdit = {CompletionTokenRange, RequiredQualifier +
Name,
""};
2343 for (
const auto &
FixIt : FixIts) {
2351 if (Opts.EnableSnippets)
2361 ? InsertTextFormat::Snippet
2362 : InsertTextFormat::PlainText;
2363 if (InsertInclude && InsertInclude->Insertion)
2379 <<
" (" << getCompletionKindString(R.
Context) <<
")"
2389 if (!
Line.consume_front(
"#"))
2392 if (!(
Line.consume_front(
"include_next") ||
Line.consume_front(
"include") ||
2393 Line.consume_front(
"import")))
2396 if (
Line.consume_front(
"<"))
2397 return Line.count(
'>') == 0;
2398 if (
Line.consume_front(
"\""))
2399 return Line.count(
'"') == 0;
2405 Content = Content.take_front(
Offset);
2406 auto Pos = Content.rfind(
'\n');
2407 if (
Pos != llvm::StringRef::npos)
2408 Content = Content.substr(
Pos + 1);
2411 if (Content.endswith(
".") || Content.endswith(
"->") ||
2412 Content.endswith(
"::") || Content.endswith(
"/*"))
2415 if ((Content.endswith(
"<") || Content.endswith(
"\"") ||
2416 Content.endswith(
"/")) &&
2421 return !Content.empty() && (isAsciiIdentifierContinue(Content.back()) ||
2422 !llvm::isASCII(Content.back()));
const FunctionDecl * Decl
const ParseInputs & ParseInput
std::vector< std::string > AccessibleScopes
std::optional< std::string > UnresolvedQualifier
CodeCompletionContext CCContext
llvm::SmallVector< SymbolInclude, 1 > RankedIncludeHeaders
const RawIdentifier * IdentifierResult
const Symbol * IndexResult
std::vector< std::string > QueryScopes
std::vector< CodeCompletionResult > Results
std::optional< std::string > EnclosingNamespace
const std::optional< PreamblePatch > Patch
std::string SnippetSuffix
SignatureQualitySignals Quality
const PreambleData & Preamble
const CodeCompletionResult * SemaResult
CodeCompletionBuilder Builder
std::optional< float > Score
CharSourceRange Range
SourceRange for the file name.
const MacroDirective * Directive
std::unique_ptr< CompilerInvocation > CI
llvm::raw_string_ostream OS
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
std::optional< FixItHint > FixIt
void collect(const CompilerInstance &CI)
static std::optional< OpaqueType > fromCompletionResult(ASTContext &Ctx, const CodeCompletionResult &R)
Create a type from a code completion result.
static std::optional< OpaqueType > fromType(ASTContext &Ctx, QualType Type)
Construct an instance from a clang::QualType.
virtual bool fuzzyFind(const FuzzyFindRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const =0
Matches symbols in the index fuzzily and applies Callback on each matched symbol before returning.
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
A format-agnostic representation for structured text.
Paragraph & addParagraph()
Adds a semantical block that will be separate from others.
void append(Document Other)
Paragraph & appendText(llvm::StringRef Text)
Append plain text to the end of the string.
Paragraph & appendCode(llvm::StringRef Code, bool Preserve=false)
Append inline code, this translates to the ` block in markdown.
std::pair< StringRef, StringRef > splitQualifiedName(StringRef QName)
@ Info
An information message.
SymbolID getSymbolID(const Decl *D)
Gets the symbol ID for a declaration. Returned SymbolID might be null.
std::string formatDocumentation(const CodeCompletionString &CCS, llvm::StringRef DocComment)
Assembles formatted documentation for a completion result.
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
std::string sortText(float Score, llvm::StringRef Name)
Returns a string that sorts in the same order as (-Score, Tiebreak), for LSP.
std::string getDeclComment(const ASTContext &Ctx, const NamedDecl &Decl)
Similar to getDocComment, but returns the comment for a NamedDecl.
bool isIncludeFile(llvm::StringRef Line)
TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M, const LangOptions &L)
Position offsetToPosition(llvm::StringRef Code, size_t Offset)
Turn an offset in Code into a [line, column] pair.
size_t lspLength(llvm::StringRef Code)
CompletionPrefix guessCompletionPrefix(llvm::StringRef Content, unsigned Offset)
std::unique_ptr< CompilerInvocation > buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, std::vector< std::string > *CC1Args)
Builds compiler invocation that could be used to build AST or preamble.
bool isExplicitTemplateSpecialization(const NamedDecl *D)
Indicates if D is an explicit template specialization, e.g.
format::FormatStyle getFormatStyleForFile(llvm::StringRef File, llvm::StringRef Content, const ThreadsafeFS &TFS)
Choose the clang-format style we should apply to a certain file.
CompletionItemKind
The kind of a completion entry.
bool allowImplicitCompletion(llvm::StringRef Content, unsigned Offset)
void vlog(const char *Fmt, Ts &&... Vals)
static const char * toString(OffsetEncoding OE)
CodeCompleteResult codeCompleteComment(PathRef FileName, unsigned Offset, llvm::StringRef Prefix, const PreambleData *Preamble, const ParseInputs &ParseInput)
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
std::string getReturnType(const CodeCompletionString &CCS)
Gets detail to be used as the detail field in an LSP completion item.
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
llvm::StringMap< unsigned > collectIdentifiers(llvm::StringRef Content, const format::FormatStyle &Style)
Collects identifiers with counts in the source code.
bool hasUnstableLinkage(const Decl *D)
Whether we must avoid computing linkage for D during code completion.
llvm::json::Value toJSON(const FuzzyFindRequest &Request)
std::vector< std::string > visibleNamespaces(llvm::StringRef Code, const LangOptions &LangOpts)
Heuristically determine namespaces visible at a point, without parsing Code.
llvm::Expected< HeaderFile > toHeaderFile(llvm::StringRef Header, llvm::StringRef HintPath)
Creates a HeaderFile from Header which can be either a URI or a literal include.
std::unique_ptr< CompilerInstance > prepareCompilerInstance(std::unique_ptr< clang::CompilerInvocation > CI, const PrecompiledPreamble *Preamble, std::unique_ptr< llvm::MemoryBuffer > Buffer, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, DiagnosticConsumer &DiagsClient)
void log(const char *Fmt, Ts &&... Vals)
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength)
Turn a [line, column] pair into an offset in Code.
std::optional< unsigned > maybeFunctionArgumentCommentStart(llvm::StringRef Content)
llvm::StringSet collectWords(llvm::StringRef Content)
Collects words from the source code.
void getSignature(const CodeCompletionString &CCS, std::string *Signature, std::string *Snippet, CodeCompletionResult::ResultKind ResultKind, CXCursorKind CursorKind, bool IncludeFunctionArguments, std::string *RequiredQualifiers)
Formats the signature for an item, as a display string and snippet.
llvm::SmallVector< SymbolInclude, 1 > getRankedIncludes(const Symbol &Sym)
std::pair< size_t, size_t > offsetToClangLineColumn(llvm::StringRef Code, size_t Offset)
@ Deprecated
Deprecated or obsolete code.
void parseDocumentation(llvm::StringRef Input, markup::Document &Output)
float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance)
Combine symbol quality and relevance into a single score.
CodeCompleteResult codeComplete(PathRef FileName, Position Pos, const PreambleData *Preamble, const ParseInputs &ParseInput, CodeCompleteOptions Opts, SpeculativeFuzzyFind *SpecFuzzyFind)
Gets code completions at a specified Pos in FileName.
std::string printQualifiedName(const NamedDecl &ND)
Returns the qualified name of ND.
llvm::StringRef PathRef
A typedef to represent a ref to file path.
SignatureHelp signatureHelp(PathRef FileName, Position Pos, const PreambleData &Preamble, const ParseInputs &ParseInput, MarkupKind DocumentationFormat)
Get signature help at a specified Pos in FileName.
void elog(const char *Fmt, Ts &&... Vals)
std::string getDocComment(const ASTContext &Ctx, const CodeCompletionResult &Result, bool CommentsFromHeaders)
Gets a minimally formatted documentation comment of Result, with comment markers stripped.
std::string printNamespaceScope(const DeclContext &DC)
Returns the first enclosing namespace scope starting from DC.
bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx)
std::array< uint8_t, 20 > SymbolID
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Symbol::IncludeDirective InsertionDirective
Preferred preprocessor directive to use for inclusions by the file.
static const CodeCompletionRankingModel DefaultRankingModel
CodeCompletionRankingModel
Model to use for ranking code completion candidates.
const ASTSignals * MainFileSignals
bool ImportInsertions
Whether include insertions for Objective-C code should use #import instead of #include.
bool AllScopes
Whether to include index symbols that are not defined in the scopes visible from the code completion ...
std::vector< CodeCompletion > Completions
CodeCompletionContext::Kind Context
Range CompletionTokenRange
Holds the range of the token we are going to replace with this completion.
std::string sortText
A string that should be used when comparing this item with other items.
std::optional< TextEdit > textEdit
An edit which is applied to a document when selecting this completion.
std::string filterText
A string that should be used when filtering a set of completion items.
std::string detail
A human-readable string with additional information about this item, like type or symbol information.
InsertTextFormat insertTextFormat
The format of the insert text.
CompletionItemKind kind
The kind of this completion item.
std::vector< TextEdit > additionalTextEdits
An optional array of additional text edits that are applied when selecting this completion.
std::optional< MarkupContent > documentation
A human-readable string that represents a doc-comment.
std::string insertText
A string that should be inserted to a document when selecting this completion.
bool deprecated
Indicates if this item is deprecated.
std::optional< CompletionItemLabelDetails > labelDetails
Additional details for the label.
float score
The score that clangd calculates to rank the returned completions.
std::string label
The label of this completion item.
llvm::StringRef Qualifier
The parsed preamble and associated data.
Position start
The range's start position.
Position end
The range's end position.
Represents the signature of a callable.
A speculative and asynchronous fuzzy find index request (based on cached request) that can be sent be...
@ Deprecated
Indicates if the symbol is deprecated.
@ Include
#include "header.h"
@ Import
#import "header.h"