hokein created this revision. hokein added a reviewer: sammccall. Herald added subscribers: usaxena95, kadircet, arphaman, jkorous, MaskRay, ilya-biryukov. Herald added a project: clang.
Fixes https://github.com/clangd/clangd/issues/277 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D74054 Files: clang-tools-extra/clangd/XRefs.cpp clang-tools-extra/clangd/unittests/XRefsTests.cpp Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -45,6 +45,11 @@ MATCHER_P2(FileRange, File, Range, "") { return Location{URIForFile::canonicalize(File, testRoot()), Range} == arg; } +MATCHER(DeclRange, "") { + const LocatedSymbol &Sym = ::testing::get<0>(arg); + const Range &Range = ::testing::get<1>(arg); + return Sym.PreferredDeclaration.range == Range; +} // Extracts ranges from an annotated example, and constructs a matcher for a // highlight set. Ranges should be named $read/$write as appropriate. @@ -660,15 +665,38 @@ Sym("baz", T.range("StaticOverload2")))); } -TEST(LocateSymbol, TemplateTypedefs) { - auto T = Annotations(R"cpp( - template <class T> struct function {}; - template <class T> using callback = function<T()>; +TEST(LocateSymbol, Alias) { + const char *Tests[] = { + R"cpp( + template <class T> struct [[function]] {}; + template <class T> using [[callback]] = function<T()>; - c^allback<int> foo; - )cpp"); - auto AST = TestTU::withCode(T.code()).build(); - EXPECT_THAT(locateSymbolAt(AST, T.point()), ElementsAre(Sym("callback"))); + c^allback<int> foo; + )cpp", + + R"cpp( + class [[Foo]] {}; + typedef Foo [[Ba^r]]; + )cpp", + + R"cpp( + namespace ns { class [[Foo]] {}; } + using ns::[[F^oo]]; + )cpp", + + R"cpp( + class [[Foo]] {}; + using [[B^ar]] = Foo; + )cpp", + }; + + for (const auto* Case : Tests) { + SCOPED_TRACE(Case); + auto T = Annotations(Case); + auto AST = TestTU::withCode(T.code()).build(); + EXPECT_THAT(locateSymbolAt(AST, T.point()), + ::testing::UnorderedPointwise(DeclRange(), T.ranges())); + } } TEST(LocateSymbol, RelPathsInCompileCommand) { Index: clang-tools-extra/clangd/XRefs.cpp =================================================================== --- clang-tools-extra/clangd/XRefs.cpp +++ clang-tools-extra/clangd/XRefs.cpp @@ -273,8 +273,8 @@ }; // Emit all symbol locations (declaration or definition) from AST. - DeclRelationSet Relations = - DeclRelation::TemplatePattern | DeclRelation::Alias; + DeclRelationSet Relations = DeclRelation::TemplatePattern | + DeclRelation::Alias | DeclRelation::Underlying; for (const NamedDecl *D : getDeclAtPosition(AST, SourceLoc, Relations)) { // Special case: void foo() ^override: jump to the overridden method. if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D)) {
Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -45,6 +45,11 @@ MATCHER_P2(FileRange, File, Range, "") { return Location{URIForFile::canonicalize(File, testRoot()), Range} == arg; } +MATCHER(DeclRange, "") { + const LocatedSymbol &Sym = ::testing::get<0>(arg); + const Range &Range = ::testing::get<1>(arg); + return Sym.PreferredDeclaration.range == Range; +} // Extracts ranges from an annotated example, and constructs a matcher for a // highlight set. Ranges should be named $read/$write as appropriate. @@ -660,15 +665,38 @@ Sym("baz", T.range("StaticOverload2")))); } -TEST(LocateSymbol, TemplateTypedefs) { - auto T = Annotations(R"cpp( - template <class T> struct function {}; - template <class T> using callback = function<T()>; +TEST(LocateSymbol, Alias) { + const char *Tests[] = { + R"cpp( + template <class T> struct [[function]] {}; + template <class T> using [[callback]] = function<T()>; - c^allback<int> foo; - )cpp"); - auto AST = TestTU::withCode(T.code()).build(); - EXPECT_THAT(locateSymbolAt(AST, T.point()), ElementsAre(Sym("callback"))); + c^allback<int> foo; + )cpp", + + R"cpp( + class [[Foo]] {}; + typedef Foo [[Ba^r]]; + )cpp", + + R"cpp( + namespace ns { class [[Foo]] {}; } + using ns::[[F^oo]]; + )cpp", + + R"cpp( + class [[Foo]] {}; + using [[B^ar]] = Foo; + )cpp", + }; + + for (const auto* Case : Tests) { + SCOPED_TRACE(Case); + auto T = Annotations(Case); + auto AST = TestTU::withCode(T.code()).build(); + EXPECT_THAT(locateSymbolAt(AST, T.point()), + ::testing::UnorderedPointwise(DeclRange(), T.ranges())); + } } TEST(LocateSymbol, RelPathsInCompileCommand) { Index: clang-tools-extra/clangd/XRefs.cpp =================================================================== --- clang-tools-extra/clangd/XRefs.cpp +++ clang-tools-extra/clangd/XRefs.cpp @@ -273,8 +273,8 @@ }; // Emit all symbol locations (declaration or definition) from AST. - DeclRelationSet Relations = - DeclRelation::TemplatePattern | DeclRelation::Alias; + DeclRelationSet Relations = DeclRelation::TemplatePattern | + DeclRelation::Alias | DeclRelation::Underlying; for (const NamedDecl *D : getDeclAtPosition(AST, SourceLoc, Relations)) { // Special case: void foo() ^override: jump to the overridden method. if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D)) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits