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)
const {
218 if (!Opts.BundleOverloads.value_or(
false))
224 std::string HeaderForHash;
226 if (
auto Header = headerToInsertIfAllowed(Opts)) {
229 Inserter->calculateIncludePath(*HeaderFile,
FileName))
232 vlog(
"Code completion header path manipulation failed {0}",
233 HeaderFile.takeError());
238 llvm::SmallString<256> Scratch;
241 case index::SymbolKind::ClassMethod:
242 case index::SymbolKind::InstanceMethod:
243 case index::SymbolKind::StaticMethod:
245 llvm_unreachable(
"Don't expect members from index in code completion");
249 case index::SymbolKind::Function:
252 return llvm::hash_combine(
262 if (!D || !D->isFunctionOrFunctionTemplate())
265 llvm::raw_svector_ostream
OS(Scratch);
266 D->printQualifiedName(
OS);
268 return llvm::hash_combine(Scratch, HeaderForHash);
275 std::optional<llvm::StringRef>
276 headerToInsertIfAllowed(
const CodeCompleteOptions &Opts)
const {
283 auto &SM =
SemaResult->Declaration->getASTContext().getSourceManager();
285 if (SM.isInMainFile(SM.getExpansionLoc(RD->getBeginLoc())))
289 for (
const auto &Inc : RankedIncludeHeaders)
295 using Bundle = llvm::SmallVector<CompletionCandidate, 4>;
298 std::pair<CompletionCandidate::Bundle, CodeCompletion::Scores>;
299struct ScoredBundleGreater {
300 bool operator()(
const ScoredBundle &L,
const ScoredBundle &R) {
301 if (L.second.Total != R.second.Total)
302 return L.second.Total > R.second.Total;
303 return L.first.front().Name <
304 R.first.front().Name;
315struct CodeCompletionBuilder {
316 CodeCompletionBuilder(ASTContext *ASTCtx,
const CompletionCandidate &
C,
317 CodeCompletionString *SemaCCS,
319 const IncludeInserter &Includes,
321 CodeCompletionContext::Kind ContextKind,
322 const CodeCompleteOptions &Opts,
323 bool IsUsingDeclaration, tok::TokenKind NextTokenKind)
325 EnableFunctionArgSnippets(Opts.EnableFunctionArgSnippets),
326 IsUsingDeclaration(IsUsingDeclaration), NextTokenKind(NextTokenKind) {
327 Completion.Deprecated =
true;
332 Completion.Name = std::string(llvm::StringRef(SemaCCS->getTypedText()));
333 Completion.FilterText = SemaCCS->getAllTypedText();
334 if (Completion.Scope.empty()) {
335 if ((
C.SemaResult->Kind == CodeCompletionResult::RK_Declaration) ||
336 (
C.SemaResult->Kind == CodeCompletionResult::RK_Pattern))
337 if (
const auto *D =
C.SemaResult->getDeclaration())
338 if (
const auto *ND = dyn_cast<NamedDecl>(D))
339 Completion.Scope = std::string(
342 Completion.Kind = toCompletionItemKind(*
C.SemaResult, ContextKind);
346 Completion.Name.back() ==
'/')
348 for (
const auto &
FixIt :
C.SemaResult->FixIts) {
350 FixIt, ASTCtx->getSourceManager(), ASTCtx->getLangOpts()));
352 llvm::sort(Completion.FixIts, [](
const TextEdit &
X,
const TextEdit &Y) {
353 return std::tie(X.range.start.line, X.range.start.character) <
354 std::tie(Y.range.start.line, Y.range.start.character);
358 Completion.Origin |=
C.IndexResult->Origin;
359 if (Completion.Scope.empty())
360 Completion.Scope = std::string(
C.IndexResult->Scope);
362 Completion.Kind = toCompletionItemKind(
C.IndexResult->SymInfo.Kind);
363 if (Completion.Name.empty())
364 Completion.Name = std::string(
C.IndexResult->Name);
365 if (Completion.FilterText.empty())
366 Completion.FilterText = Completion.Name;
369 if (Completion.RequiredQualifier.empty() && !
C.SemaResult) {
370 llvm::StringRef ShortestQualifier =
C.IndexResult->Scope;
372 llvm::StringRef Qualifier =
C.IndexResult->Scope;
373 if (Qualifier.consume_front(Scope) &&
374 Qualifier.size() < ShortestQualifier.size())
375 ShortestQualifier = Qualifier;
377 Completion.RequiredQualifier = std::string(ShortestQualifier);
380 if (
C.IdentifierResult) {
383 Completion.Name = std::string(
C.IdentifierResult->Name);
384 Completion.FilterText = Completion.Name;
388 auto Inserted = [&](llvm::StringRef Header)
389 -> llvm::Expected<std::pair<std::string, bool>> {
390 auto ResolvedDeclaring =
392 if (!ResolvedDeclaring)
393 return ResolvedDeclaring.takeError();
395 if (!ResolvedInserted)
396 return ResolvedInserted.takeError();
397 auto Spelled = Includes.calculateIncludePath(*ResolvedInserted,
FileName);
399 return error(
"Header not on include path");
400 return std::make_pair(
402 Includes.shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
404 bool ShouldInsert =
C.headerToInsertIfAllowed(Opts).has_value();
407 for (
const auto &Inc :
C.RankedIncludeHeaders) {
411 if (
auto ToInclude = Inserted(Inc.Header)) {
412 CodeCompletion::IncludeCandidate Include;
413 Include.Header = ToInclude->first;
414 if (ToInclude->second && ShouldInsert)
415 Include.Insertion = Includes.insert(
417 ? tooling::IncludeDirective::Import
418 : tooling::IncludeDirective::Include);
419 Completion.Includes.push_back(std::move(Include));
421 log(
"Failed to generate include insertion edits for adding header "
422 "(FileURI='{0}', IncludeHeader='{1}') into {2}: {3}",
423 C.IndexResult->CanonicalDeclaration.FileURI, Inc.Header,
FileName,
424 ToInclude.takeError());
427 std::stable_partition(Completion.Includes.begin(),
428 Completion.Includes.end(),
429 [](
const CodeCompletion::IncludeCandidate &I) {
430 return !I.Insertion.has_value();
434 void add(
const CompletionCandidate &
C, CodeCompletionString *SemaCCS) {
435 assert(
bool(
C.SemaResult) ==
bool(SemaCCS));
436 Bundled.emplace_back();
437 BundledEntry &S = Bundled.back();
439 getSignature(*SemaCCS, &S.Signature, &S.SnippetSuffix,
C.SemaResult->Kind,
440 C.SemaResult->CursorKind, &Completion.RequiredQualifier);
441 if (!
C.SemaResult->FunctionCanBeCall)
442 S.SnippetSuffix.clear();
444 }
else if (
C.IndexResult) {
445 S.Signature = std::string(
C.IndexResult->Signature);
446 S.SnippetSuffix = std::string(
C.IndexResult->CompletionSnippetSuffix);
447 S.ReturnType = std::string(
C.IndexResult->ReturnType);
449 if (!Completion.Documentation) {
450 auto SetDoc = [&](llvm::StringRef Doc) {
452 Completion.Documentation.emplace();
457 SetDoc(
C.IndexResult->Documentation);
458 }
else if (
C.SemaResult) {
464 if (Completion.Deprecated) {
466 Completion.Deprecated &=
467 C.SemaResult->Availability == CXAvailability_Deprecated;
469 Completion.Deprecated &=
474 CodeCompletion build() {
475 Completion.ReturnType = summarizeReturnType();
476 Completion.Signature = summarizeSignature();
477 Completion.SnippetSuffix = summarizeSnippet();
478 Completion.BundleSize = Bundled.size();
479 return std::move(Completion);
483 struct BundledEntry {
490 template <std::
string BundledEntry::*Member>
491 const std::string *onlyValue()
const {
492 auto B = Bundled.begin(),
E = Bundled.end();
493 for (
auto *I =
B + 1; I !=
E; ++I)
494 if (I->*Member !=
B->*Member)
496 return &(
B->*Member);
499 template <
bool BundledEntry::*Member>
const bool *onlyValue()
const {
500 auto B = Bundled.begin(),
E = Bundled.end();
501 for (
auto *I = B + 1; I !=
E; ++I)
502 if (I->*Member !=
B->*Member)
504 return &(
B->*Member);
507 std::string summarizeReturnType()
const {
508 if (
auto *RT = onlyValue<&BundledEntry::ReturnType>())
513 std::string summarizeSnippet()
const {
514 if (IsUsingDeclaration)
516 auto *
Snippet = onlyValue<&BundledEntry::SnippetSuffix>();
534 if (MayHaveArgList) {
538 if (NextTokenKind == tok::less &&
Snippet->front() ==
'<')
541 if (NextTokenKind == tok::l_paren) {
551 else if (
Snippet->at(I) ==
'<')
554 }
while (Balance > 0);
560 if (EnableFunctionArgSnippets)
564 if (MayHaveArgList) {
573 bool EmptyArgs = llvm::StringRef(*Snippet).endswith(
"()");
575 return EmptyArgs ?
"<$1>()$0" :
"<$1>($0)";
577 return EmptyArgs ?
"()" :
"($0)";
588 if (llvm::StringRef(*Snippet).endswith(
"<>"))
595 std::string summarizeSignature()
const {
596 if (
auto *
Signature = onlyValue<&BundledEntry::Signature>())
604 CodeCompletion Completion;
605 llvm::SmallVector<BundledEntry, 1> Bundled;
606 bool EnableFunctionArgSnippets;
609 bool IsUsingDeclaration;
610 tok::TokenKind NextTokenKind;
616 case CodeCompletionResult::RK_Declaration:
617 case CodeCompletionResult::RK_Pattern: {
623 case CodeCompletionResult::RK_Macro:
625 case CodeCompletionResult::RK_Keyword:
628 llvm_unreachable(
"unknown CodeCompletionResult kind");
633struct SpecifiedScope {
670 std::vector<std::string> scopesForQualification() {
680 std::vector<std::string> scopesForIndexQuery() {
682 std::vector<std::string> EnclosingAtFront;
684 EnclosingAtFront.push_back(*EnclosingNamespace);
685 std::set<std::string> Deduplicated;
686 for (llvm::StringRef S : QueryScopes)
687 if (S != EnclosingNamespace)
690 EnclosingAtFront.reserve(EnclosingAtFront.size() + Deduplicated.size());
691 llvm::copy(Deduplicated, std::back_inserter(EnclosingAtFront));
693 return EnclosingAtFront;
700SpecifiedScope getQueryScopes(CodeCompletionContext &
CCContext,
702 const CompletionPrefix &HeuristicPrefix,
703 const CodeCompleteOptions &Opts) {
704 SpecifiedScope Scopes;
705 for (
auto *Context :
CCContext.getVisitedContexts()) {
706 if (isa<TranslationUnitDecl>(Context)) {
707 Scopes.QueryScopes.push_back(
"");
708 Scopes.AccessibleScopes.push_back(
"");
709 }
else if (
const auto *ND = dyn_cast<NamespaceDecl>(Context)) {
715 const CXXScopeSpec *SemaSpecifier =
716 CCContext.getCXXScopeSpecifier().value_or(
nullptr);
718 if (!SemaSpecifier) {
721 if (!HeuristicPrefix.
Qualifier.empty()) {
722 vlog(
"Sema said no scope specifier, but we saw {0} in the source code",
724 StringRef SpelledSpecifier = HeuristicPrefix.
Qualifier;
725 if (SpelledSpecifier.consume_front(
"::")) {
726 Scopes.AccessibleScopes = {
""};
727 Scopes.QueryScopes = {
""};
729 Scopes.UnresolvedQualifier = std::string(SpelledSpecifier);
741 if (SemaSpecifier && SemaSpecifier->isValid())
745 Scopes.QueryScopes.push_back(
"");
746 llvm::StringRef SpelledSpecifier = Lexer::getSourceText(
747 CharSourceRange::getCharRange(SemaSpecifier->getRange()),
748 CCSema.SourceMgr, clang::LangOptions());
749 if (SpelledSpecifier.consume_front(
"::"))
750 Scopes.QueryScopes = {
""};
751 Scopes.UnresolvedQualifier = std::string(SpelledSpecifier);
753 if (!Scopes.UnresolvedQualifier->empty())
754 *Scopes.UnresolvedQualifier +=
"::";
756 Scopes.AccessibleScopes = Scopes.QueryScopes;
763bool contextAllowsIndex(
enum CodeCompletionContext::Kind
K) {
765 case CodeCompletionContext::CCC_TopLevel:
766 case CodeCompletionContext::CCC_ObjCInterface:
767 case CodeCompletionContext::CCC_ObjCImplementation:
768 case CodeCompletionContext::CCC_ObjCIvarList:
769 case CodeCompletionContext::CCC_ClassStructUnion:
770 case CodeCompletionContext::CCC_Statement:
771 case CodeCompletionContext::CCC_Expression:
772 case CodeCompletionContext::CCC_ObjCMessageReceiver:
773 case CodeCompletionContext::CCC_EnumTag:
774 case CodeCompletionContext::CCC_UnionTag:
775 case CodeCompletionContext::CCC_ClassOrStructTag:
776 case CodeCompletionContext::CCC_ObjCProtocolName:
777 case CodeCompletionContext::CCC_Namespace:
778 case CodeCompletionContext::CCC_Type:
779 case CodeCompletionContext::CCC_ParenthesizedExpression:
780 case CodeCompletionContext::CCC_ObjCInterfaceName:
781 case CodeCompletionContext::CCC_Symbol:
782 case CodeCompletionContext::CCC_SymbolOrNewName:
784 case CodeCompletionContext::CCC_OtherWithMacros:
785 case CodeCompletionContext::CCC_DotMemberAccess:
786 case CodeCompletionContext::CCC_ArrowMemberAccess:
787 case CodeCompletionContext::CCC_ObjCCategoryName:
788 case CodeCompletionContext::CCC_ObjCPropertyAccess:
789 case CodeCompletionContext::CCC_MacroName:
790 case CodeCompletionContext::CCC_MacroNameUse:
791 case CodeCompletionContext::CCC_PreprocessorExpression:
792 case CodeCompletionContext::CCC_PreprocessorDirective:
793 case CodeCompletionContext::CCC_SelectorName:
794 case CodeCompletionContext::CCC_TypeQualifiers:
795 case CodeCompletionContext::CCC_ObjCInstanceMessage:
796 case CodeCompletionContext::CCC_ObjCClassMessage:
797 case CodeCompletionContext::CCC_IncludedFile:
798 case CodeCompletionContext::CCC_Attribute:
800 case CodeCompletionContext::CCC_Other:
801 case CodeCompletionContext::CCC_NaturalLanguage:
802 case CodeCompletionContext::CCC_Recovery:
803 case CodeCompletionContext::CCC_NewName:
806 llvm_unreachable(
"unknown code completion context");
809static bool isInjectedClass(
const NamedDecl &D) {
810 if (
auto *R = dyn_cast_or_null<RecordDecl>(&D))
811 if (R->isInjectedClassName())
817static bool isExcludedMember(
const NamedDecl &D) {
820 if (
D.getKind() == Decl::CXXDestructor)
823 if (isInjectedClass(D))
826 auto NameKind =
D.getDeclName().getNameKind();
827 if (NameKind == DeclarationName::CXXOperatorName ||
828 NameKind == DeclarationName::CXXLiteralOperatorName ||
829 NameKind == DeclarationName::CXXConversionFunctionName)
840struct CompletionRecorder :
public CodeCompleteConsumer {
841 CompletionRecorder(
const CodeCompleteOptions &Opts,
842 llvm::unique_function<
void()> ResultsCallback)
843 : CodeCompleteConsumer(Opts.getClangCompleteOpts()),
844 CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
845 CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
846 CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) {
847 assert(this->ResultsCallback);
855 void ProcessCodeCompleteResults(
class Sema &S, CodeCompletionContext Context,
856 CodeCompletionResult *InResults,
857 unsigned NumResults)
final {
866 if (Context.getKind() == CodeCompletionContext::CCC_Recovery) {
867 log(
"Code complete: Ignoring sema code complete callback with Recovery "
874 if (NumResults == 0 && !contextAllowsIndex(Context.getKind()))
877 log(
"Multiple code complete callbacks (parser backtracked?). "
878 "Dropping results from context {0}, keeping results from {1}.",
879 getCompletionKindString(Context.getKind()),
880 getCompletionKindString(this->CCContext.getKind()));
888 for (
unsigned I = 0; I < NumResults; ++I) {
889 auto &Result = InResults[I];
891 if (Result.Hidden && Result.Declaration &&
892 Result.Declaration->isCXXClassMember())
894 if (!Opts.IncludeIneligibleResults &&
895 (Result.Availability == CXAvailability_NotAvailable ||
896 Result.Availability == CXAvailability_NotAccessible))
898 if (Result.Declaration &&
899 !Context.getBaseType().isNull()
900 && isExcludedMember(*Result.Declaration))
904 if (Result.Declaration && !Context.getCXXScopeSpecifier() &&
905 isInjectedClass(*Result.Declaration))
908 Result.StartsNestedNameSpecifier =
false;
914 CodeCompletionAllocator &getAllocator()
override {
return *CCAllocator; }
915 CodeCompletionTUInfo &getCodeCompletionTUInfo()
override {
return CCTUInfo; }
919 llvm::StringRef getName(
const CodeCompletionResult &Result) {
920 switch (Result.Kind) {
921 case CodeCompletionResult::RK_Declaration:
922 if (
auto *ID = Result.Declaration->getIdentifier())
923 return ID->getName();
925 case CodeCompletionResult::RK_Keyword:
926 return Result.Keyword;
927 case CodeCompletionResult::RK_Macro:
928 return Result.Macro->getName();
929 case CodeCompletionResult::RK_Pattern:
932 auto *CCS = codeCompletionString(Result);
933 const CodeCompletionString::Chunk *OnlyText =
nullptr;
934 for (
auto &
C : *CCS) {
935 if (
C.Kind != CodeCompletionString::CK_TypedText)
938 return CCAllocator->CopyString(CCS->getAllTypedText());
941 return OnlyText ? OnlyText->Text : llvm::StringRef();
946 CodeCompletionString *codeCompletionString(
const CodeCompletionResult &R) {
948 return const_cast<CodeCompletionResult &
>(R).CreateCodeCompletionString(
949 *CCSema, CCContext, *CCAllocator, CCTUInfo,
954 CodeCompleteOptions Opts;
955 std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
956 CodeCompletionTUInfo CCTUInfo;
957 llvm::unique_function<void()> ResultsCallback;
960struct ScoredSignature {
973int paramIndexForArg(
const CodeCompleteConsumer::OverloadCandidate &
Candidate,
975 int NumParams =
Candidate.getNumParams();
976 if (
auto *T =
Candidate.getFunctionType()) {
977 if (
auto *Proto = T->getAs<FunctionProtoType>()) {
978 if (Proto->isVariadic())
982 return std::min(Arg, std::max(NumParams - 1, 0));
985class SignatureHelpCollector final :
public CodeCompleteConsumer {
987 SignatureHelpCollector(
const clang::CodeCompleteOptions &CodeCompleteOpts,
989 const SymbolIndex *Index, SignatureHelp &SigHelp)
990 : CodeCompleteConsumer(CodeCompleteOpts), SigHelp(SigHelp),
991 Allocator(std::make_shared<
clang::GlobalCodeCompletionAllocator>()),
992 CCTUInfo(Allocator), Index(Index),
993 DocumentationFormat(DocumentationFormat) {}
995 void ProcessOverloadCandidates(Sema &S,
unsigned CurrentArg,
996 OverloadCandidate *Candidates,
997 unsigned NumCandidates,
998 SourceLocation OpenParLoc,
999 bool Braced)
override {
1000 assert(!OpenParLoc.isInvalid());
1001 SourceManager &SrcMgr = S.getSourceManager();
1002 OpenParLoc = SrcMgr.getFileLoc(OpenParLoc);
1003 if (SrcMgr.isInMainFile(OpenParLoc))
1006 elog(
"Location oustide main file in signature help: {0}",
1007 OpenParLoc.printToString(SrcMgr));
1009 std::vector<ScoredSignature> ScoredSignatures;
1010 SigHelp.signatures.reserve(NumCandidates);
1011 ScoredSignatures.reserve(NumCandidates);
1015 SigHelp.activeSignature = 0;
1016 assert(CurrentArg <= (
unsigned)std::numeric_limits<int>::max() &&
1017 "too many arguments");
1019 SigHelp.activeParameter =
static_cast<int>(CurrentArg);
1021 for (
unsigned I = 0; I < NumCandidates; ++I) {
1022 OverloadCandidate
Candidate = Candidates[I];
1026 if (
auto *Func =
Candidate.getFunction()) {
1027 if (
auto *Pattern = Func->getTemplateInstantiationPattern())
1030 if (
static_cast<int>(I) == SigHelp.activeSignature) {
1035 SigHelp.activeParameter =
1036 paramIndexForArg(
Candidate, SigHelp.activeParameter);
1039 const auto *CCS =
Candidate.CreateSignatureString(
1040 CurrentArg, S, *Allocator, CCTUInfo,
1042 assert(CCS &&
"Expected the CodeCompletionString to be non-null");
1043 ScoredSignatures.push_back(processOverloadCandidate(
1052 llvm::DenseMap<SymbolID, std::string> FetchedDocs;
1054 LookupRequest IndexRequest;
1055 for (
const auto &S : ScoredSignatures) {
1058 IndexRequest.IDs.insert(S.IDForDoc);
1060 Index->lookup(IndexRequest, [&](
const Symbol &S) {
1061 if (!S.Documentation.empty())
1062 FetchedDocs[S.ID] = std::string(S.Documentation);
1064 vlog(
"SigHelp: requested docs for {0} symbols from the index, got {1} "
1065 "symbols with non-empty docs in the response",
1066 IndexRequest.IDs.size(), FetchedDocs.size());
1069 llvm::sort(ScoredSignatures, [](
const ScoredSignature &L,
1070 const ScoredSignature &R) {
1077 if (L.Quality.NumberOfParameters != R.Quality.NumberOfParameters)
1078 return L.Quality.NumberOfParameters < R.Quality.NumberOfParameters;
1079 if (L.Quality.NumberOfOptionalParameters !=
1080 R.Quality.NumberOfOptionalParameters)
1081 return L.Quality.NumberOfOptionalParameters <
1082 R.Quality.NumberOfOptionalParameters;
1083 if (L.Quality.Kind != R.Quality.Kind) {
1084 using OC = CodeCompleteConsumer::OverloadCandidate;
1085 auto KindPriority = [&](OC::CandidateKind K) {
1087 case OC::CK_Aggregate:
1089 case OC::CK_Function:
1091 case OC::CK_FunctionType:
1093 case OC::CK_FunctionProtoTypeLoc:
1095 case OC::CK_FunctionTemplate:
1097 case OC::CK_Template:
1100 llvm_unreachable(
"Unknown overload candidate type.");
1102 return KindPriority(L.Quality.Kind) < KindPriority(R.Quality.Kind);
1104 if (L.Signature.label.size() != R.Signature.label.size())
1105 return L.Signature.label.size() < R.Signature.label.size();
1106 return L.Signature.label < R.Signature.label;
1109 for (
auto &SS : ScoredSignatures) {
1111 SS.IDForDoc ? FetchedDocs.find(SS.IDForDoc) : FetchedDocs.end();
1112 if (IndexDocIt != FetchedDocs.end()) {
1113 markup::Document SignatureComment;
1115 SS.Signature.documentation =
1116 renderDoc(SignatureComment, DocumentationFormat);
1119 SigHelp.signatures.push_back(std::move(SS.Signature));
1123 GlobalCodeCompletionAllocator &getAllocator()
override {
return *Allocator; }
1125 CodeCompletionTUInfo &getCodeCompletionTUInfo()
override {
return CCTUInfo; }
1128 void processParameterChunk(llvm::StringRef ChunkText,
1129 SignatureInformation &
Signature)
const {
1132 unsigned ParamEndOffset = ParamStartOffset +
lspLength(ChunkText);
1137 ParameterInformation
Info;
1138 Info.labelOffsets.emplace(ParamStartOffset, ParamEndOffset);
1140 Info.labelString = std::string(ChunkText);
1145 void processOptionalChunk(
const CodeCompletionString &CCS,
1147 SignatureQualitySignals &Signal)
const {
1148 for (
const auto &Chunk : CCS) {
1149 switch (Chunk.Kind) {
1150 case CodeCompletionString::CK_Optional:
1151 assert(Chunk.Optional &&
1152 "Expected the optional code completion string to be non-null.");
1153 processOptionalChunk(*Chunk.Optional,
Signature, Signal);
1155 case CodeCompletionString::CK_VerticalSpace:
1157 case CodeCompletionString::CK_CurrentParameter:
1158 case CodeCompletionString::CK_Placeholder:
1159 processParameterChunk(Chunk.Text,
Signature);
1160 Signal.NumberOfOptionalParameters++;
1171 ScoredSignature processOverloadCandidate(
const OverloadCandidate &
Candidate,
1172 const CodeCompletionString &CCS,
1173 llvm::StringRef DocComment)
const {
1175 SignatureQualitySignals Signal;
1178 markup::Document OverloadComment;
1180 Signature.documentation = renderDoc(OverloadComment, DocumentationFormat);
1183 for (
const auto &Chunk : CCS) {
1184 switch (Chunk.Kind) {
1185 case CodeCompletionString::CK_ResultType:
1188 assert(!
ReturnType &&
"Unexpected CK_ResultType");
1191 case CodeCompletionString::CK_CurrentParameter:
1192 case CodeCompletionString::CK_Placeholder:
1193 processParameterChunk(Chunk.Text,
Signature);
1194 Signal.NumberOfParameters++;
1196 case CodeCompletionString::CK_Optional: {
1198 assert(Chunk.Optional &&
1199 "Expected the optional code completion string to be non-null.");
1200 processOptionalChunk(*Chunk.Optional,
Signature, Signal);
1203 case CodeCompletionString::CK_VerticalSpace:
1215 ScoredSignature Result;
1216 Result.Signature = std::move(
Signature);
1217 Result.Quality = Signal;
1218 const FunctionDecl *Func =
Candidate.getFunction();
1219 if (Func && Result.Signature.documentation.value.empty()) {
1227 SignatureHelp &SigHelp;
1228 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
1229 CodeCompletionTUInfo CCTUInfo;
1230 const SymbolIndex *Index;
1236class ParamNameCollector final :
public CodeCompleteConsumer {
1238 ParamNameCollector(
const clang::CodeCompleteOptions &CodeCompleteOpts,
1239 std::set<std::string> &ParamNames)
1240 : CodeCompleteConsumer(CodeCompleteOpts),
1241 Allocator(std::make_shared<
clang::GlobalCodeCompletionAllocator>()),
1242 CCTUInfo(Allocator), ParamNames(ParamNames) {}
1244 void ProcessOverloadCandidates(Sema &S,
unsigned CurrentArg,
1245 OverloadCandidate *Candidates,
1246 unsigned NumCandidates,
1247 SourceLocation OpenParLoc,
1248 bool Braced)
override {
1249 assert(CurrentArg <= (
unsigned)std::numeric_limits<int>::max() &&
1250 "too many arguments");
1252 for (
unsigned I = 0; I < NumCandidates; ++I) {
1253 if (
const NamedDecl *ND = Candidates[I].getParamDecl(CurrentArg))
1254 if (
const auto *II = ND->getIdentifier())
1255 ParamNames.emplace(II->getName());
1260 GlobalCodeCompletionAllocator &getAllocator()
override {
return *Allocator; }
1262 CodeCompletionTUInfo &getCodeCompletionTUInfo()
override {
return CCTUInfo; }
1264 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
1265 CodeCompletionTUInfo CCTUInfo;
1266 std::set<std::string> &ParamNames;
1269struct SemaCompleteInput {
1273 const std::optional<PreamblePatch>
Patch;
1277void loadMainFilePreambleMacros(
const Preprocessor &PP,
1282 ExternalPreprocessorSource *PreambleMacros = PP.getExternalSource();
1285 const auto &ITable = PP.getIdentifierTable();
1286 IdentifierInfoLookup *PreambleIdentifiers =
1287 ITable.getExternalIdentifierLookup();
1289 if (!PreambleIdentifiers || !PreambleMacros)
1292 if (ITable.find(
MacroName.getKey()) != ITable.end())
1294 if (
auto *II = PreambleIdentifiers->get(
MacroName.getKey()))
1295 if (II->isOutOfDate())
1296 PreambleMacros->updateOutOfDateIdentifier(*II);
1302bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
1303 const clang::CodeCompleteOptions &Options,
1304 const SemaCompleteInput &Input,
1305 IncludeStructure *Includes =
nullptr) {
1306 trace::Span Tracer(
"Sema completion");
1311 elog(
"Couldn't create CompilerInvocation");
1314 auto &FrontendOpts =
CI->getFrontendOpts();
1315 FrontendOpts.SkipFunctionBodies =
true;
1317 CI->getLangOpts()->SpellChecking =
false;
1321 CI->getLangOpts()->DelayedTemplateParsing =
false;
1323 FrontendOpts.CodeCompleteOpts = Options;
1324 FrontendOpts.CodeCompletionAt.FileName = std::string(Input.FileName);
1325 std::tie(FrontendOpts.CodeCompletionAt.Line,
1326 FrontendOpts.CodeCompletionAt.Column) =
1329 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1330 llvm::MemoryBuffer::getMemBuffer(Input.ParseInput.Contents,
1333 CI->getDiagnosticOpts().IgnoreWarnings =
true;
1340 PreambleBounds PreambleRegion =
1341 ComputePreambleBounds(*
CI->getLangOpts(), *ContentsBuffer, 0);
1342 bool CompletingInPreamble = Input.Offset < PreambleRegion.Size ||
1343 (!PreambleRegion.PreambleEndsAtStartOfLine &&
1344 Input.Offset == PreambleRegion.Size);
1346 Input.Patch->apply(*
CI);
1349 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
1350 Input.ParseInput.TFS->view(Input.ParseInput.CompileCommand.Directory);
1351 if (Input.Preamble.StatCache)
1352 VFS = Input.Preamble.StatCache->getConsumingFS(std::move(VFS));
1354 std::move(
CI), !CompletingInPreamble ? &Input.Preamble.Preamble :
nullptr,
1355 std::move(ContentsBuffer), std::move(VFS),
IgnoreDiags);
1356 Clang->getPreprocessorOpts().SingleFileParseMode = CompletingInPreamble;
1357 Clang->setCodeCompletionConsumer(Consumer.release());
1361 log(
"BeginSourceFile() failed when running codeComplete for {0}",
1371 loadMainFilePreambleMacros(
Clang->getPreprocessor(), Input.Preamble);
1374 if (llvm::Error Err =
Action.Execute()) {
1375 log(
"Execute() failed when running codeComplete for {0}: {1}",
1376 Input.FileName,
toString(std::move(Err)));
1385bool allowIndex(CodeCompletionContext &
CC) {
1386 if (!contextAllowsIndex(
CC.getKind()))
1389 auto Scope =
CC.getCXXScopeSpecifier();
1392 NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
1397 switch (NameSpec->getKind()) {
1398 case NestedNameSpecifier::Global:
1399 case NestedNameSpecifier::Namespace:
1400 case NestedNameSpecifier::NamespaceAlias:
1402 case NestedNameSpecifier::Super:
1403 case NestedNameSpecifier::TypeSpec:
1404 case NestedNameSpecifier::TypeSpecWithTemplate:
1406 case NestedNameSpecifier::Identifier:
1409 llvm_unreachable(
"invalid NestedNameSpecifier kind");
1414bool includeSymbolFromIndex(CodeCompletionContext::Kind
Kind,
1415 const Symbol &Sym) {
1419 if (Sym.SymInfo.Kind == index::SymbolKind::Protocol &&
1420 Sym.SymInfo.Lang == index::SymbolLanguage::ObjC)
1421 return Kind == CodeCompletionContext::CCC_ObjCProtocolName;
1422 else if (
Kind == CodeCompletionContext::CCC_ObjCProtocolName)
1428std::future<std::pair<bool, SymbolSlab>>
1429startAsyncFuzzyFind(
const SymbolIndex &Index,
const FuzzyFindRequest &Req) {
1430 return runAsync<std::pair<bool, SymbolSlab>>([&Index, Req]() {
1431 trace::Span Tracer(
"Async fuzzyFind");
1432 SymbolSlab::Builder Syms;
1434 Index.
fuzzyFind(Req, [&Syms](
const Symbol &Sym) { Syms.insert(Sym); });
1435 return std::make_pair(Incomplete, std::move(Syms).build());
1442FuzzyFindRequest speculativeFuzzyFindRequestForCompletion(
1443 FuzzyFindRequest CachedReq,
const CompletionPrefix &HeuristicPrefix) {
1444 CachedReq.Query = std::string(HeuristicPrefix.
Name);
1477class CodeCompleteFlow {
1479 IncludeStructure Includes;
1480 SpeculativeFuzzyFind *SpecFuzzyFind;
1481 const CodeCompleteOptions &Opts;
1484 CompletionRecorder *Recorder =
nullptr;
1485 CodeCompletionContext::Kind CCContextKind = CodeCompletionContext::CCC_Other;
1486 bool IsUsingDeclaration =
false;
1490 tok::TokenKind NextTokenKind = tok::eof;
1492 int NSema = 0, NIndex = 0, NSemaAndIndex = 0, NIdent = 0;
1493 bool Incomplete =
false;
1494 CompletionPrefix HeuristicPrefix;
1495 std::optional<FuzzyMatcher> Filter;
1496 Range ReplacedRange;
1500 std::optional<ScopeDistance> ScopeProximity;
1501 std::optional<OpaqueType> PreferredType;
1503 bool AllScopes =
false;
1504 llvm::StringSet<> ContextWords;
1507 std::optional<IncludeInserter> Inserter;
1508 std::optional<URIDistance> FileProximity;
1513 std::optional<FuzzyFindRequest> SpecReq;
1517 CodeCompleteFlow(
PathRef FileName,
const IncludeStructure &Includes,
1518 SpeculativeFuzzyFind *SpecFuzzyFind,
1519 const CodeCompleteOptions &Opts)
1523 CodeCompleteResult
run(
const SemaCompleteInput &SemaCCInput) && {
1524 trace::Span Tracer(
"CodeCompleteFlow");
1526 SemaCCInput.Offset);
1527 populateContextWords(SemaCCInput.ParseInput.Contents);
1528 if (Opts.Index && SpecFuzzyFind && SpecFuzzyFind->CachedReq) {
1529 assert(!SpecFuzzyFind->Result.valid());
1530 SpecReq = speculativeFuzzyFindRequestForCompletion(
1531 *SpecFuzzyFind->CachedReq, HeuristicPrefix);
1532 SpecFuzzyFind->Result = startAsyncFuzzyFind(*Opts.Index, *SpecReq);
1538 CodeCompleteResult
Output;
1539 auto RecorderOwner = std::make_unique<CompletionRecorder>(Opts, [&]() {
1540 assert(Recorder &&
"Recorder is not set");
1541 CCContextKind = Recorder->CCContext.getKind();
1542 IsUsingDeclaration = Recorder->CCContext.isUsingDeclaration();
1544 SemaCCInput.ParseInput.Contents,
1545 *SemaCCInput.ParseInput.TFS);
1546 const auto NextToken = Lexer::findNextToken(
1547 Recorder->CCSema->getPreprocessor().getCodeCompletionLoc(),
1548 Recorder->CCSema->getSourceManager(), Recorder->CCSema->LangOpts);
1550 NextTokenKind = NextToken->getKind();
1554 SemaCCInput.FileName, SemaCCInput.ParseInput.Contents, Style,
1555 SemaCCInput.ParseInput.CompileCommand.Directory,
1556 &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo());
1557 for (
const auto &Inc : Includes.MainFileIncludes)
1558 Inserter->addExisting(Inc);
1564 FileDistanceOptions ProxOpts{};
1565 const auto &SM = Recorder->CCSema->getSourceManager();
1566 llvm::StringMap<SourceParams> ProxSources;
1568 Includes.getID(SM.getFileEntryForID(SM.getMainFileID()));
1570 for (
auto &HeaderIDAndDepth : Includes.includeDepth(*MainFileID)) {
1572 ProxSources[Includes.getRealPath(HeaderIDAndDepth.getFirst())];
1573 Source.Cost = HeaderIDAndDepth.getSecond() * ProxOpts.IncludeCost;
1577 if (HeaderIDAndDepth.getSecond() > 0)
1578 Source.MaxUpTraversals = 1;
1580 FileProximity.emplace(ProxSources, ProxOpts);
1585 getCompletionKindString(CCContextKind));
1586 log(
"Code complete: sema context {0}, query scopes [{1}] (AnyScope={2}), "
1587 "expected type {3}{4}",
1588 getCompletionKindString(CCContextKind),
1590 PreferredType ? Recorder->CCContext.getPreferredType().getAsString()
1592 IsUsingDeclaration ?
", inside using declaration" :
"");
1595 Recorder = RecorderOwner.get();
1597 semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
1598 SemaCCInput, &Includes);
1599 logResults(
Output, Tracer);
1603 void logResults(
const CodeCompleteResult &
Output,
const trace::Span &Tracer) {
1606 SPAN_ATTACH(Tracer,
"merged_results", NSemaAndIndex);
1607 SPAN_ATTACH(Tracer,
"identifier_results", NIdent);
1610 log(
"Code complete: {0} results from Sema, {1} from Index, "
1611 "{2} matched, {3} from identifiers, {4} returned{5}.",
1612 NSema, NIndex, NSemaAndIndex, NIdent,
Output.Completions.size(),
1613 Output.HasMore ?
" (incomplete)" :
"");
1614 assert(!Opts.Limit ||
Output.Completions.size() <= Opts.Limit);
1619 CodeCompleteResult runWithoutSema(llvm::StringRef Content,
size_t Offset,
1620 const ThreadsafeFS &TFS) && {
1621 trace::Span Tracer(
"CodeCompleteWithoutSema");
1624 populateContextWords(Content);
1625 CCContextKind = CodeCompletionContext::CCC_Recovery;
1626 IsUsingDeclaration =
false;
1627 Filter = FuzzyMatcher(HeuristicPrefix.Name);
1629 ReplacedRange.start = ReplacedRange.end =
Pos;
1630 ReplacedRange.start.character -= HeuristicPrefix.Name.size();
1632 llvm::StringMap<SourceParams> ProxSources;
1634 FileProximity.emplace(ProxSources);
1638 Inserter.emplace(FileName, Content, Style,
1642 std::vector<RawIdentifier> IdentifierResults;
1643 for (
const auto &IDAndCount : Identifiers) {
1645 ID.Name = IDAndCount.first();
1646 ID.References = IDAndCount.second;
1648 if (ID.Name == HeuristicPrefix.Name)
1650 if (ID.References > 0)
1651 IdentifierResults.push_back(std::move(ID));
1657 SpecifiedScope Scopes;
1659 Content.take_front(
Offset), format::getFormattingLangOpts(Style));
1660 for (std::string &S : Scopes.QueryScopes)
1663 if (HeuristicPrefix.Qualifier.empty())
1664 AllScopes = Opts.AllScopes;
1665 else if (HeuristicPrefix.Qualifier.startswith(
"::")) {
1666 Scopes.QueryScopes = {
""};
1667 Scopes.UnresolvedQualifier =
1668 std::string(HeuristicPrefix.Qualifier.drop_front(2));
1670 Scopes.UnresolvedQualifier = std::string(HeuristicPrefix.Qualifier);
1674 ScopeProximity.emplace(QueryScopes);
1676 SymbolSlab IndexResults = Opts.Index ? queryIndex() : SymbolSlab();
1678 CodeCompleteResult
Output = toCodeCompleteResult(mergeResults(
1679 {}, IndexResults, IdentifierResults));
1680 Output.RanParser =
false;
1681 logResults(
Output, Tracer);
1686 void populateContextWords(llvm::StringRef Content) {
1688 unsigned RangeEnd = HeuristicPrefix.Qualifier.begin() - Content.data(),
1689 RangeBegin = RangeEnd;
1690 for (
size_t I = 0; I < 3 && RangeBegin > 0; ++I) {
1691 auto PrevNL = Content.rfind(
'\n', RangeBegin);
1692 if (PrevNL == StringRef::npos) {
1696 RangeBegin = PrevNL;
1699 ContextWords =
collectWords(Content.slice(RangeBegin, RangeEnd));
1700 dlog(
"Completion context words: {0}",
1701 llvm::join(ContextWords.keys(),
", "));
1706 CodeCompleteResult runWithSema() {
1707 const auto &CodeCompletionRange = CharSourceRange::getCharRange(
1708 Recorder->CCSema->getPreprocessor().getCodeCompletionTokenRange());
1714 if (CodeCompletionRange.isValid()) {
1716 CodeCompletionRange);
1719 Recorder->CCSema->getSourceManager(),
1720 Recorder->CCSema->getPreprocessor().getCodeCompletionLoc());
1721 ReplacedRange.start = ReplacedRange.end =
Pos;
1723 Filter = FuzzyMatcher(
1724 Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
1725 auto SpecifiedScopes = getQueryScopes(
1726 Recorder->CCContext, *Recorder->CCSema, HeuristicPrefix, Opts);
1728 QueryScopes = SpecifiedScopes.scopesForIndexQuery();
1730 AllScopes = SpecifiedScopes.AllowAllScopes;
1732 ScopeProximity.emplace(QueryScopes);
1735 Recorder->CCContext.getPreferredType());
1741 auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
1744 trace::Span Tracer(
"Populate CodeCompleteResult");
1747 mergeResults(Recorder->Results, IndexResults, {});
1748 return toCodeCompleteResult(Top);
1752 toCodeCompleteResult(
const std::vector<ScoredBundle> &Scored) {
1753 CodeCompleteResult
Output;
1756 for (
auto &
C : Scored) {
1757 Output.Completions.push_back(toCodeCompletion(
C.first));
1758 Output.Completions.back().Score =
C.second;
1759 Output.Completions.back().CompletionTokenRange = ReplacedRange;
1761 Output.HasMore = Incomplete;
1762 Output.Context = CCContextKind;
1763 Output.CompletionRange = ReplacedRange;
1767 SymbolSlab queryIndex() {
1768 trace::Span Tracer(
"Query index");
1769 SPAN_ATTACH(Tracer,
"limit", int64_t(Opts.Limit));
1772 FuzzyFindRequest Req;
1774 Req.Limit = Opts.Limit;
1775 Req.Query = std::string(Filter->pattern());
1776 Req.RestrictForCodeCompletion =
true;
1778 Req.AnyScope = AllScopes;
1780 Req.ProximityPaths.push_back(std::string(FileName));
1782 Req.PreferredTypes.push_back(std::string(PreferredType->raw()));
1783 vlog(
"Code complete: fuzzyFind({0:2})",
toJSON(Req));
1786 SpecFuzzyFind->NewReq = Req;
1787 if (SpecFuzzyFind && SpecFuzzyFind->Result.valid() && (*SpecReq == Req)) {
1788 vlog(
"Code complete: speculative fuzzy request matches the actual index "
1789 "request. Waiting for the speculative index results.");
1792 trace::Span WaitSpec(
"Wait speculative results");
1793 auto SpecRes = SpecFuzzyFind->Result.get();
1794 Incomplete |= SpecRes.first;
1795 return std::move(SpecRes.second);
1798 SPAN_ATTACH(Tracer,
"Speculative results",
false);
1801 SymbolSlab::Builder ResultsBuilder;
1802 Incomplete |= Opts.Index->fuzzyFind(
1803 Req, [&](
const Symbol &Sym) { ResultsBuilder.insert(Sym); });
1804 return std::move(ResultsBuilder).build();
1812 std::vector<ScoredBundle>
1813 mergeResults(
const std::vector<CodeCompletionResult> &SemaResults,
1814 const SymbolSlab &IndexResults,
1815 const std::vector<RawIdentifier> &IdentifierResults) {
1816 trace::Span Tracer(
"Merge and score results");
1817 std::vector<CompletionCandidate::Bundle> Bundles;
1818 llvm::DenseMap<size_t, size_t> BundleLookup;
1819 auto AddToBundles = [&](
const CodeCompletionResult *
SemaResult,
1822 CompletionCandidate
C;
1826 if (
C.IndexResult) {
1829 }
else if (
C.SemaResult) {
1835 if (
auto OverloadSet =
1836 C.overloadSet(Opts, FileName, Inserter ? &*Inserter :
nullptr)) {
1837 auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1839 Bundles.emplace_back();
1840 Bundles[Ret.first->second].push_back(std::move(
C));
1842 Bundles.emplace_back();
1843 Bundles.back().push_back(std::move(
C));
1846 llvm::DenseSet<const Symbol *> UsedIndexResults;
1847 auto CorrespondingIndexResult =
1848 [&](
const CodeCompletionResult &
SemaResult) ->
const Symbol * {
1851 auto I = IndexResults.find(SymID);
1852 if (I != IndexResults.end()) {
1853 UsedIndexResults.insert(&*I);
1866 if (!includeSymbolFromIndex(CCContextKind,
IndexResult))
1871 for (
const auto &Ident : IdentifierResults)
1872 AddToBundles(
nullptr,
nullptr, &Ident);
1874 TopN<ScoredBundle, ScoredBundleGreater> Top(
1875 Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1876 for (
auto &Bundle : Bundles)
1877 addCandidate(Top, std::move(Bundle));
1878 return std::move(Top).items();
1881 std::optional<float> fuzzyScore(
const CompletionCandidate &
C) {
1883 if (((
C.SemaResult &&
1884 C.SemaResult->Kind == CodeCompletionResult::RK_Macro) ||
1886 C.IndexResult->SymInfo.Kind == index::SymbolKind::Macro)) &&
1887 !
C.Name.starts_with_insensitive(Filter->pattern()))
1888 return std::nullopt;
1889 return Filter->match(
C.Name);
1892 CodeCompletion::Scores
1893 evaluateCompletion(
const SymbolQualitySignals &
Quality,
1894 const SymbolRelevanceSignals &Relevance) {
1896 CodeCompletion::Scores Scores;
1897 switch (Opts.RankingModel) {
1898 case RM::Heuristics:
1899 Scores.Quality =
Quality.evaluateHeuristics();
1900 Scores.Relevance = Relevance.evaluateHeuristics();
1905 Scores.ExcludingName =
1906 Relevance.NameMatch > std::numeric_limits<float>::epsilon()
1907 ? Scores.Total / Relevance.NameMatch
1911 case RM::DecisionForest:
1912 DecisionForestScores DFScores = Opts.DecisionForestScorer(
1913 Quality, Relevance, Opts.DecisionForestBase);
1914 Scores.ExcludingName = DFScores.ExcludingName;
1915 Scores.Total = DFScores.Total;
1918 llvm_unreachable(
"Unhandled CodeCompletion ranking model.");
1922 void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
1923 CompletionCandidate::Bundle Bundle) {
1925 SymbolRelevanceSignals Relevance;
1926 Relevance.Context = CCContextKind;
1927 Relevance.Name = Bundle.front().Name;
1928 Relevance.FilterLength = HeuristicPrefix.Name.size();
1930 Relevance.FileProximityMatch = &*FileProximity;
1932 Relevance.ScopeProximityMatch = &*ScopeProximity;
1934 Relevance.HadContextType =
true;
1935 Relevance.ContextWords = &ContextWords;
1936 Relevance.MainFileSignals = Opts.MainFileSignals;
1938 auto &First = Bundle.front();
1939 if (
auto FuzzyScore = fuzzyScore(First))
1940 Relevance.NameMatch = *FuzzyScore;
1944 bool FromIndex =
false;
1948 Relevance.merge(*
Candidate.IndexResult);
1949 Origin |=
Candidate.IndexResult->Origin;
1951 if (!
Candidate.IndexResult->Type.empty())
1952 Relevance.HadSymbolType |=
true;
1953 if (PreferredType &&
1954 PreferredType->raw() ==
Candidate.IndexResult->Type) {
1955 Relevance.TypeMatchesPreferred =
true;
1961 if (PreferredType) {
1963 Recorder->CCSema->getASTContext(), *
Candidate.SemaResult)) {
1964 Relevance.HadSymbolType |=
true;
1965 if (PreferredType == CompletionType)
1966 Relevance.TypeMatchesPreferred =
true;
1978 CodeCompletion::Scores Scores = evaluateCompletion(
Quality, Relevance);
1979 if (Opts.RecordCCResult)
1980 Opts.RecordCCResult(toCodeCompletion(Bundle),
Quality, Relevance,
1983 dlog(
"CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First.Name,
1984 llvm::to_string(Origin), Scores.Total, llvm::to_string(
Quality),
1985 llvm::to_string(Relevance));
1988 NIndex += FromIndex;
1991 if (Candidates.push({std::move(Bundle), Scores}))
1995 CodeCompletion toCodeCompletion(
const CompletionCandidate::Bundle &Bundle) {
1996 std::optional<CodeCompletionBuilder>
Builder;
1997 for (
const auto &Item : Bundle) {
1998 CodeCompletionString *SemaCCS =
1999 Item.SemaResult ? Recorder->codeCompletionString(*Item.SemaResult)
2002 Builder.emplace(Recorder ? &Recorder->CCSema->getASTContext() :
nullptr,
2003 Item, SemaCCS, AccessibleScopes, *Inserter, FileName,
2004 CCContextKind, Opts, IsUsingDeclaration, NextTokenKind);
2014clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts()
const {
2015 clang::CodeCompleteOptions Result;
2016 Result.IncludeCodePatterns = EnableSnippets;
2017 Result.IncludeMacros =
true;
2018 Result.IncludeGlobals =
true;
2023 Result.IncludeBriefComments =
false;
2028 Result.LoadExternal = !Index;
2029 Result.IncludeFixIts = IncludeFixIts;
2036 assert(
Offset <= Content.size());
2037 StringRef Rest = Content.take_front(
Offset);
2042 while (!Rest.empty() && isAsciiIdentifierContinue(Rest.back()))
2043 Rest = Rest.drop_back();
2044 Result.Name = Content.slice(Rest.size(),
Offset);
2047 while (Rest.consume_back(
"::") && !Rest.endswith(
":"))
2048 while (!Rest.empty() && isAsciiIdentifierContinue(Rest.back()))
2049 Rest = Rest.drop_back();
2051 Content.slice(Rest.size(), Result.Name.begin() - Content.begin());
2060 llvm::StringRef Prefix,
2066 clang::CodeCompleteOptions Options;
2067 Options.IncludeGlobals =
false;
2068 Options.IncludeMacros =
false;
2069 Options.IncludeCodePatterns =
false;
2070 Options.IncludeBriefComments =
false;
2071 std::set<std::string> ParamNames;
2075 std::make_unique<ParamNameCollector>(Options, ParamNames), Options,
2079 if (ParamNames.empty())
2083 Range CompletionRange;
2087 CompletionRange.
end =
2089 Result.CompletionRange = CompletionRange;
2090 Result.Context = CodeCompletionContext::CCC_NaturalLanguage;
2091 for (llvm::StringRef
Name : ParamNames) {
2092 if (!
Name.startswith(Prefix))
2097 Item.
Kind = CompletionItemKind::Text;
2099 Item.
Origin = SymbolOrigin::AST;
2100 Result.Completions.push_back(Item);
2109std::optional<unsigned>
2111 while (!Content.empty() && isAsciiIdentifierContinue(Content.back()))
2112 Content = Content.drop_back();
2113 Content = Content.rtrim();
2114 if (Content.endswith(
"/*"))
2115 return Content.size() - 2;
2116 return std::nullopt;
2126 elog(
"Code completion position was invalid {0}",
Offset.takeError());
2137 auto CommentPrefix = Content.substr(*OffsetBeforeComment + 2).trim();
2142 auto Flow = CodeCompleteFlow(
2144 SpecFuzzyFind, Opts);
2145 return (!
Preamble || Opts.RunParser == CodeCompleteOptions::NeverParse)
2150 PreamblePatch::createMacroPatch(
2161 elog(
"Signature help position was invalid {0}",
Offset.takeError());
2165 clang::CodeCompleteOptions Options;
2166 Options.IncludeGlobals =
false;
2167 Options.IncludeMacros =
false;
2168 Options.IncludeCodePatterns =
false;
2169 Options.IncludeBriefComments =
false;
2171 std::make_unique<SignatureHelpCollector>(Options, DocumentationFormat,
2174 {FileName, *Offset, Preamble,
2175 PreamblePatch::createFullPatch(FileName, ParseInput, Preamble),
2181 auto InTopLevelScope = [](
const NamedDecl &ND) {
2182 switch (ND.getDeclContext()->getDeclKind()) {
2183 case Decl::TranslationUnit:
2184 case Decl::Namespace:
2185 case Decl::LinkageSpec:
2192 auto InClassScope = [](
const NamedDecl &ND) {
2193 return ND.getDeclContext()->getDeclKind() == Decl::CXXRecord;
2204 if (llvm::isa<ObjCCategoryDecl>(&ND) || llvm::isa<ObjCCategoryImplDecl>(&ND))
2207 if (InTopLevelScope(ND))
2213 if (
const auto *EnumDecl = dyn_cast<clang::EnumDecl>(ND.getDeclContext()))
2214 return (InTopLevelScope(*EnumDecl) || InClassScope(*EnumDecl));
2221 const auto *InsertInclude = Includes.empty() ? nullptr : &Includes[0];
2222 LSP.
label = ((InsertInclude && InsertInclude->Insertion)
2223 ? Opts.IncludeIndicator.Insert
2224 : Opts.IncludeIndicator.NoInsert) +
2225 (Opts.ShowOrigins ?
"[" + llvm::to_string(Origin) +
"]" :
"") +
2229 LSP.
detail = BundleSize > 1
2230 ? std::string(llvm::formatv(
"[{0} overloads]", BundleSize))
2235 if (InsertInclude || Documentation) {
2240 Doc.
append(*Documentation);
2241 LSP.
documentation = renderDoc(Doc, Opts.DocumentationFormat);
2245 LSP.
textEdit = {CompletionTokenRange, RequiredQualifier +
Name,
""};
2253 for (
const auto &
FixIt : FixIts) {
2261 if (Opts.EnableSnippets)
2271 ? InsertTextFormat::Snippet
2272 : InsertTextFormat::PlainText;
2273 if (InsertInclude && InsertInclude->Insertion)
2289 <<
" (" << getCompletionKindString(R.
Context) <<
")"
2299 if (!
Line.consume_front(
"#"))
2302 if (!(
Line.consume_front(
"include_next") ||
Line.consume_front(
"include") ||
2303 Line.consume_front(
"import")))
2306 if (
Line.consume_front(
"<"))
2307 return Line.count(
'>') == 0;
2308 if (
Line.consume_front(
"\""))
2309 return Line.count(
'"') == 0;
2315 Content = Content.take_front(
Offset);
2316 auto Pos = Content.rfind(
'\n');
2317 if (
Pos != llvm::StringRef::npos)
2318 Content = Content.substr(
Pos + 1);
2321 if (Content.endswith(
".") || Content.endswith(
"->") ||
2322 Content.endswith(
"::") || Content.endswith(
"/*"))
2325 if ((Content.endswith(
"<") || Content.endswith(
"\"") ||
2326 Content.endswith(
"/")) &&
2331 return !Content.empty() && (isAsciiIdentifierContinue(Content.back()) ||
2332 !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, 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.
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"