diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp --- a/clang-tools-extra/clangd/FindTarget.cpp +++ b/clang-tools-extra/clangd/FindTarget.cpp @@ -52,6 +52,40 @@ return S; } +// Given a dependent type and a member name, heuristically resolve the +// name to one or more declarations. +// The current heuristic is simply to look up the name in the primary +// template. This is a heuristic because the template could potentially +// have specializations that declare different members. +// Multiple declarations could be returned if the name is overloaded +// (e.g. an overloaded method in the primary template). +// This heuristic will give the desired answer in many cases, e.g. +// for a call to vector::size(). +std::vector +getMembersReferencedViaDependentName(const Type *T, const DeclarationName &Name, + bool IsNonstaticMember) { + if (!T) + return {}; + if (auto *ICNT = T->getAs()) { + T = ICNT->getInjectedSpecializationType().getTypePtrOrNull(); + } + auto *TST = T->getAs(); + if (!TST) + return {}; + const ClassTemplateDecl *TD = dyn_cast_or_null( + TST->getTemplateName().getAsTemplateDecl()); + if (!TD) + return {}; + CXXRecordDecl *RD = TD->getTemplatedDecl(); + if (!RD->hasDefinition()) + return {}; + RD = RD->getDefinition(); + return RD->lookupDependentName(Name, [=](const NamedDecl *D) { + return IsNonstaticMember ? D->isCXXInstanceMember() + : !D->isCXXInstanceMember(); + }); +} + // TargetFinder locates the entities that an AST node refers to. // // Typically this is (possibly) one declaration and (possibly) one type, but @@ -79,9 +113,9 @@ // formally size() is unresolved, but the primary template is a good guess. // This affects: // - DependentTemplateSpecializationType, -// - DependentScopeMemberExpr -// - DependentScopeDeclRefExpr // - DependentNameType +// - UnresolvedUsingValueDecl +// - UnresolvedUsingTypenameDecl struct TargetFinder { using RelSet = DeclRelationSet; using Rel = DeclRelation; @@ -212,6 +246,32 @@ break; } } + void + VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) { + const Type *BaseType = E->getBaseType().getTypePtrOrNull(); + if (E->isArrow()) { + // FIXME: Handle smart pointer types by looking up operator-> + // in the primary template. + if (!BaseType || !BaseType->isPointerType()) { + return; + } + BaseType = BaseType->getAs() + ->getPointeeType() + .getTypePtrOrNull(); + } + for (const NamedDecl *D : + getMembersReferencedViaDependentName(BaseType, E->getMember(), + /*IsNonstaticMember=*/true)) { + Outer.add(D, Flags); + } + } + void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) { + for (const NamedDecl *D : getMembersReferencedViaDependentName( + E->getQualifier()->getAsType(), E->getDeclName(), + /*IsNonstaticMember=*/false)) { + Outer.add(D, Flags); + } + } void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *OIRE) { Outer.add(OIRE->getDecl(), Flags); } diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -384,8 +384,8 @@ // different kinds, deduplicate them. std::vector Result; for (const auto &Ref : References) { - if (auto Range = getTokenRange(AST.getSourceManager(), - AST.getLangOpts(), Ref.Loc)) { + if (auto Range = + getTokenRange(AST.getSourceManager(), AST.getLangOpts(), Ref.Loc)) { DocumentHighlight DH; DH.range = *Range; if (Ref.Role & index::SymbolRoleSet(index::SymbolRole::Write)) diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp --- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -465,6 +465,51 @@ template struct Fo^o {}; + )cpp", + + R"cpp(// Heuristic resolution of dependent method + template + struct S { + void [[bar]]() {} + }; + + template + void foo(S arg) { + arg.ba^r(); + } + )cpp", + + R"cpp(// Heuristic resolution of dependent method via this-> + template + struct S { + void [[foo]]() { + this->fo^o(); + } + }; + )cpp", + + R"cpp(// Heuristic resolution of dependent static method + template + struct S { + static void [[bar]]() {} + }; + + template + void foo() { + S::ba^r(); + } + )cpp", + + R"cpp(// FIXME: Heuristic resolution of dependent method + // invoked via smart pointer + template struct S { void foo(); }; + template struct unique_ptr { + T* operator->(); + }; + template + void test(unique_ptr>& V) { + V->fo^o(); + } )cpp"}; for (const char *Test : Tests) { Annotations T(Test); @@ -525,6 +570,21 @@ Foo abcde$10^("asdf"); Foo foox2 = Foo$11^("asdf"); } + + template + struct S { + void $NonstaticOverload1[[bar]](int); + void $NonstaticOverload2[[bar]](float); + + static void $StaticOverload1[[baz]](int); + static void $StaticOverload2[[baz]](float); + }; + + template + void dependent_call(S s, U u) { + s.ba$12^r(u); + S::ba$13^z(u); + } )cpp"); auto AST = TestTU::withCode(T.code()).build(); // Ordered assertions are deliberate: we expect a predictable order. @@ -544,6 +604,15 @@ ElementsAre(Sym("Foo", T.range("ConstructorLoc")))); EXPECT_THAT(locateSymbolAt(AST, T.point("11")), ElementsAre(Sym("Foo", T.range("ConstructorLoc")))); + // These assertions are unordered because the order comes from + // CXXRecordDecl::lookupDependentName() which doesn't appear to provide + // an order guarantee. + EXPECT_THAT(locateSymbolAt(AST, T.point("12")), + UnorderedElementsAre(Sym("bar", T.range("NonstaticOverload1")), + Sym("bar", T.range("NonstaticOverload2")))); + EXPECT_THAT(locateSymbolAt(AST, T.point("13")), + UnorderedElementsAre(Sym("baz", T.range("StaticOverload1")), + Sym("baz", T.range("StaticOverload2")))); } TEST(LocateSymbol, TemplateTypedefs) {