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 @@ -100,7 +100,7 @@ std::vector getMembersReferencedViaDependentName( const Type *T, llvm::function_ref NameFactory, - bool IsNonstaticMember) { + llvm::function_ref Filter) { if (!T) return {}; if (auto *ET = T->getAs()) { @@ -113,17 +113,22 @@ return {}; RD = RD->getDefinition(); DeclarationName Name = NameFactory(RD->getASTContext()); - return RD->lookupDependentName(Name, [=](const NamedDecl *D) { - return IsNonstaticMember ? D->isCXXInstanceMember() - : !D->isCXXInstanceMember(); - }); + return RD->lookupDependentName(Name, Filter); } return {}; } -// Given the type T of a dependent expression that appears of the LHS of a "->", -// heuristically find a corresponding pointee type in whose scope we could look -// up the name appearing on the RHS. +const auto NonStaticFilter = [](const NamedDecl *D) { + return D->isCXXInstanceMember(); +}; +const auto StaticFilter = [](const NamedDecl *D) { + return !D->isCXXInstanceMember(); +}; +const auto ValueFilter = [](const NamedDecl *D) { return isa(D); }; + +// Given the type T of a dependent expression that appears of the LHS of a +// "->", heuristically find a corresponding pointee type in whose scope we +// could look up the name appearing on the RHS. const Type *getPointeeType(const Type *T) { if (!T) return nullptr; @@ -141,7 +146,7 @@ [](ASTContext &Ctx) { return Ctx.DeclarationNames.getCXXOperatorName(OO_Arrow); }, - /*IsNonStaticMember=*/true); + NonStaticFilter); if (ArrowOps.empty()) return nullptr; @@ -187,13 +192,12 @@ } return getMembersReferencedViaDependentName( BaseType, [ME](ASTContext &) { return ME->getMember(); }, - /*IsNonstaticMember=*/true); + NonStaticFilter); } if (const auto *RE = dyn_cast(E)) { return getMembersReferencedViaDependentName( RE->getQualifier()->getAsType(), - [RE](ASTContext &) { return RE->getDeclName(); }, - /*IsNonstaticMember=*/false); + [RE](ASTContext &) { return RE->getDeclName(); }, StaticFilter); } if (const auto *CE = dyn_cast(E)) { const auto *CalleeType = resolveExprToType(CE->getCallee()); @@ -291,7 +295,6 @@ // CXXDependentScopeMemberExpr, but some other constructs remain to be handled: // - DependentTemplateSpecializationType, // - DependentNameType -// - UnresolvedUsingValueDecl // - UnresolvedUsingTypenameDecl struct TargetFinder { using RelSet = DeclRelationSet; @@ -345,6 +348,15 @@ } else if (const auto *NAD = dyn_cast(D)) { add(NAD->getUnderlyingDecl(), Flags | Rel::Underlying); Flags |= Rel::Alias; // continue with the alias + } else if (const UnresolvedUsingValueDecl *UUVD = + dyn_cast(D)) { + for (const NamedDecl *Target : getMembersReferencedViaDependentName( + UUVD->getQualifier()->getAsType(), + [UUVD](ASTContext &) { return UUVD->getNameInfo().getName(); }, + ValueFilter)) { + add(Target, Flags | Rel::Underlying); + } + Flags |= Rel::Alias; // continue with the alias } else if (const UsingShadowDecl *USD = dyn_cast(D)) { // Include the using decl, but don't traverse it. This may end up // including *all* shadows, which we don't want. 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 @@ -345,7 +345,7 @@ // Give the underlying decl if navigation is triggered on a non-renaming // alias. - if (llvm::isa(D)) { + if (llvm::isa(D) || llvm::isa(D)) { // FIXME: address more complicated cases. TargetDecl(... Underlying) gives // all overload candidates, we only want the targeted one if the cursor is // on an using-alias usage, workround it with getDeclAtPosition. diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp --- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp @@ -207,6 +207,19 @@ )cpp"; EXPECT_DECLS("MemberExpr", {"using X::foo", Rel::Alias}, {"int foo()", Rel::Underlying}); + + Code = R"cpp( + template + struct Base { + void waldo() {} + }; + template + struct Derived : Base { + using Base::[[waldo]]; + }; + )cpp"; + EXPECT_DECLS("UnresolvedUsingValueDecl", {"using Base::waldo", Rel::Alias}, + {"void waldo()", Rel::Underlying}); } TEST_F(TargetDeclTest, ConstructorInitList) { 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 @@ -1087,66 +1087,78 @@ TEST(LocateSymbol, Alias) { const char *Tests[] = { - R"cpp( + R"cpp( template struct function {}; template using [[callback]] = function; c^allback foo; )cpp", - // triggered on non-definition of a renaming alias: should not give any - // underlying decls. - R"cpp( + // triggered on non-definition of a renaming alias: should not give any + // underlying decls. + R"cpp( class Foo {}; typedef Foo [[Bar]]; B^ar b; )cpp", - R"cpp( + R"cpp( class Foo {}; using [[Bar]] = Foo; // definition Ba^r b; )cpp", - // triggered on the underlying decl of a renaming alias. - R"cpp( + // triggered on the underlying decl of a renaming alias. + R"cpp( class [[Foo]]; using Bar = Fo^o; )cpp", - // triggered on definition of a non-renaming alias: should give underlying - // decls. - R"cpp( + // triggered on definition of a non-renaming alias: should give underlying + // decls. + R"cpp( namespace ns { class [[Foo]] {}; } using ns::F^oo; )cpp", - R"cpp( + R"cpp( namespace ns { int [[x]](char); int [[x]](double); } using ns::^x; )cpp", - R"cpp( + R"cpp( namespace ns { int [[x]](char); int x(double); } using ns::x; int y = ^x('a'); )cpp", - R"cpp( + R"cpp( namespace ns { class [[Foo]] {}; } using ns::Foo; F^oo f; )cpp", - // other cases that don't matter much. - R"cpp( + // other cases that don't matter much. + R"cpp( class Foo {}; typedef Foo [[Ba^r]]; )cpp", - R"cpp( + R"cpp( class Foo {}; using [[B^ar]] = Foo; )cpp", + + // Member of dependent base + R"cpp( + template + struct Base { + void [[waldo]]() {} + }; + template + struct Derived : Base { + using Base::w^aldo; + }; + )cpp", }; for (const auto* Case : Tests) {