Skip to content

[WIP][clangd] Resolve the dependent type from its single instantiation. Take 1 #71279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 194 additions & 1 deletion clang-tools-extra/clangd/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/DeclVisitor.h"
#include "clang/AST/DeclarationName.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/NestedNameSpecifier.h"
#include "clang/AST/PrettyPrinter.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/AST/TemplateBase.h"
#include "clang/AST/TypeLoc.h"
#include "clang/Basic/Builtins.h"
Expand Down Expand Up @@ -636,7 +638,7 @@ static NamedDecl *getOnlyInstantiationImpl(TemplateDeclTy *TD) {
return Only;
}

NamedDecl *getOnlyInstantiation(NamedDecl *TemplatedDecl) {
NamedDecl *getOnlyInstantiation(const NamedDecl *TemplatedDecl) {
if (TemplateDecl *TD = TemplatedDecl->getDescribedTemplate()) {
if (auto *CTD = llvm::dyn_cast<ClassTemplateDecl>(TD))
return getOnlyInstantiationImpl(CTD);
Expand All @@ -648,6 +650,197 @@ NamedDecl *getOnlyInstantiation(NamedDecl *TemplatedDecl) {
return nullptr;
}

NamedDecl *getOnlyInstantiatedDecls(const NamedDecl *DependentDecl) {
if (auto *Instantiation = getOnlyInstantiation(DependentDecl))
return Instantiation;
NamedDecl *OuterTemplate = nullptr;
for (auto *DC = DependentDecl->getDeclContext(); isa<CXXRecordDecl>(DC);
DC = DC->getParent()) {
auto *RD = cast<CXXRecordDecl>(DC);
if (auto *I = getOnlyInstantiation(RD)) {
OuterTemplate = I;
break;
}
}

if (!OuterTemplate)
return nullptr;

struct Visitor : DeclVisitor<Visitor, NamedDecl *> {
const NamedDecl *TemplatedDecl;
Visitor(const NamedDecl *TemplatedDecl) : TemplatedDecl(TemplatedDecl) {}

NamedDecl *VisitCXXRecordDecl(CXXRecordDecl *RD) {
if (RD->getTemplateInstantiationPattern() == TemplatedDecl)
return RD;
for (auto *F : RD->decls()) {
if (auto *Injected = llvm::dyn_cast<CXXRecordDecl>(F);
Injected && Injected->isInjectedClassName())
continue;
if (NamedDecl *ND = Visit(F))
return ND;
}
return nullptr;
}

NamedDecl *VisitClassTemplateDecl(ClassTemplateDecl *CTD) {
unsigned Size = llvm::range_size(CTD->specializations());
if (Size != 1)
return nullptr;
return Visit(*CTD->spec_begin());
}

NamedDecl *VisitFunctionTemplateDecl(FunctionTemplateDecl *FTD) {
unsigned Size = llvm::range_size(FTD->specializations());
if (Size != 1)
return nullptr;
return Visit(*FTD->spec_begin());
}

NamedDecl *VisitFunctionDecl(FunctionDecl *FD) {
if (FD->getTemplateInstantiationPattern() == TemplatedDecl)
return FD;
return nullptr;
}

NamedDecl *VisitVarDecl(VarDecl *VD) {
if (VD->getCanonicalDecl()->getSourceRange() ==
TemplatedDecl->getCanonicalDecl()->getSourceRange())
return VD;
return nullptr;
}

NamedDecl *VisitFieldDecl(FieldDecl *FD) {
if (FD->getCanonicalDecl()->getSourceRange() ==
TemplatedDecl->getCanonicalDecl()->getSourceRange())
return FD;
return nullptr;
}
};
return Visitor(DependentDecl).Visit(OuterTemplate);
}

std::optional<DynTypedNode>
getOnlyInstantiatedNode(const DeclContext *StartingPoint,
const DynTypedNode &DependentNode) {
if (auto *CTD = DependentNode.get<ClassTemplateDecl>())
return getOnlyInstantiatedNode(
StartingPoint, DynTypedNode::create(*CTD->getTemplatedDecl()));
if (auto *FTD = DependentNode.get<FunctionTemplateDecl>())
return getOnlyInstantiatedNode(
StartingPoint, DynTypedNode::create(*FTD->getTemplatedDecl()));

if (auto *FD = DependentNode.get<FunctionDecl>()) {
auto *ID = getOnlyInstantiatedDecls(FD);
if (!ID)
return std::nullopt;
return DynTypedNode::create(*ID);
}
if (auto *RD = DependentNode.get<CXXRecordDecl>()) {
auto *ID = getOnlyInstantiatedDecls(RD);
if (!ID)
return std::nullopt;
return DynTypedNode::create(*ID);
}

NamedDecl *InstantiatedEnclosingDecl = nullptr;
for (auto *DC = StartingPoint; DC;
DC = DC->getParent()) {
auto *ND = llvm::dyn_cast<NamedDecl>(DC);
if (!ND)
continue;
InstantiatedEnclosingDecl = getOnlyInstantiatedDecls(ND);
if (InstantiatedEnclosingDecl)
break;
}

if (!InstantiatedEnclosingDecl)
return std::nullopt;

auto *InstantiatedFunctionDecl =
llvm::dyn_cast<FunctionDecl>(InstantiatedEnclosingDecl);
if (!InstantiatedFunctionDecl)
return std::nullopt;

struct FullExprVisitor : RecursiveASTVisitor<FullExprVisitor> {
const DynTypedNode &DependentNode;
Stmt *Result;
FullExprVisitor(const DynTypedNode &DependentNode)
: DependentNode(DependentNode), Result(nullptr) {}

bool shouldVisitTemplateInstantiations() const { return true; }

bool shouldVisitImplicitCode() const { return true; }

bool VisitStmt(Stmt *S) {
if (S->getSourceRange() == DependentNode.getSourceRange()) {
Result = S;
return false;
}
return true;
}
};

FullExprVisitor Visitor(DependentNode);
Visitor.TraverseFunctionDecl(InstantiatedFunctionDecl);
if (Visitor.Result)
return DynTypedNode::create(*Visitor.Result);
return std::nullopt;
}

NamedDecl *
getOnlyInstantiationForMemberFunction(const CXXMethodDecl *TemplatedDecl) {
if (auto *MemberInstantiation = getOnlyInstantiation(TemplatedDecl))
return MemberInstantiation;
NamedDecl *OuterTemplate = nullptr;
for (auto *DC = TemplatedDecl->getDeclContext(); isa<CXXRecordDecl>(DC);
DC = DC->getParent()) {
auto *RD = cast<CXXRecordDecl>(DC);
if (auto *I = getOnlyInstantiation(RD)) {
OuterTemplate = I;
break;
}
}
if (!OuterTemplate)
return nullptr;
struct Visitor : DeclVisitor<Visitor, NamedDecl *> {
const CXXMethodDecl *TD;
Visitor(const CXXMethodDecl *TemplatedDecl) : TD(TemplatedDecl) {}
NamedDecl *VisitCXXRecordDecl(CXXRecordDecl *RD) {
for (auto *F : RD->decls()) {
if (!isa<NamedDecl>(F))
continue;
if (NamedDecl *ND = Visit(F))
return ND;
}
return nullptr;
}

NamedDecl *VisitClassTemplateDecl(ClassTemplateDecl *CTD) {
unsigned Size = llvm::range_size(CTD->specializations());
if (Size != 1)
return nullptr;
return Visit(*CTD->spec_begin());
}

NamedDecl *VisitFunctionTemplateDecl(FunctionTemplateDecl *FTD) {
unsigned Size = llvm::range_size(FTD->specializations());
if (Size != 1)
return nullptr;
return Visit(*FTD->spec_begin());
}

NamedDecl *VisitCXXMethodDecl(CXXMethodDecl *MD) {
auto *Pattern = MD->getTemplateInstantiationPattern();
if (Pattern == TD)
return MD;
return nullptr;
}

};
return Visitor(TemplatedDecl).Visit(OuterTemplate);
}

std::vector<const Attr *> getAttributes(const DynTypedNode &N) {
std::vector<const Attr *> Result;
if (const auto *TL = N.get<TypeLoc>()) {
Expand Down
9 changes: 8 additions & 1 deletion clang-tools-extra/clangd/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,14 @@ TemplateTypeParmTypeLoc getContainedAutoParamType(TypeLoc TL);

// If TemplatedDecl is the generic body of a template, and the template has
// exactly one visible instantiation, return the instantiated body.
NamedDecl *getOnlyInstantiation(NamedDecl *TemplatedDecl);
NamedDecl *getOnlyInstantiation(const NamedDecl *TemplatedDecl);

NamedDecl *
getOnlyInstantiationForMemberFunction(const CXXMethodDecl *TemplatedDecl);

std::optional<DynTypedNode>
getOnlyInstantiatedNode(const DeclContext *StartingPoint,
const DynTypedNode &DependentNode);

/// Return attributes attached directly to a node.
std::vector<const Attr *> getAttributes(const DynTypedNode &);
Expand Down
Loading