nridge created this revision. nridge added a reviewer: sammccall. Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, jkorous, MaskRay, ilya-biryukov. Herald added a project: clang.
Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D71090 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 @@ -450,7 +450,22 @@ +^+x; } )cpp", - }; + + R"cpp(// Declaration of explicit template specialization + template <typename T> + struct $decl[[Foo]] {}; + + template <> + struct Fo^o<int> {}; + )cpp", + + R"cpp(// Declaration of partial template specialization + template <typename T> + struct $decl[[Foo]] {}; + + template <typename T> + struct Fo^o<T*> {}; + )cpp"}; for (const char *Test : Tests) { Annotations T(Test); llvm::Optional<Range> WantDecl; Index: clang-tools-extra/clangd/XRefs.cpp =================================================================== --- clang-tools-extra/clangd/XRefs.cpp +++ clang-tools-extra/clangd/XRefs.cpp @@ -191,10 +191,10 @@ // Macros are simple: there's no declaration/definition distinction. // As a consequence, there's no need to look them up in the index either. - SourceLocation MaybeMacroLocation = SM.getMacroArgExpandedLocation( + SourceLocation IdentStartLoc = SM.getMacroArgExpandedLocation( getBeginningOfIdentifier(Pos, AST.getSourceManager(), AST.getLangOpts())); std::vector<LocatedSymbol> Result; - if (auto M = locateMacroAt(MaybeMacroLocation, AST.getPreprocessor())) { + if (auto M = locateMacroAt(IdentStartLoc, AST.getPreprocessor())) { if (auto Loc = makeLocation(AST.getASTContext(), M->Info->getDefinitionLoc(), *MainFilePath)) { LocatedSymbol Macro; @@ -234,6 +234,17 @@ for (const Decl *D : getDeclAtPosition(AST, SourceLoc, Relations)) { const Decl *Def = getDefinition(D); const Decl *Preferred = Def ? Def : D; + + // If we're at the point of declaration of a template specialization, + // it's more useful to navigate to the template declaration. + if (Preferred->getLocation() == IdentStartLoc) { + if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Preferred)) { + D = CTSD->getSpecializedTemplate(); + Def = getDefinition(D); + Preferred = Def ? Def : D; + } + } + auto Loc = makeLocation(AST.getASTContext(), spellingLocIfSpelled(findName(Preferred), SM), *MainFilePath);
Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -450,7 +450,22 @@ +^+x; } )cpp", - }; + + R"cpp(// Declaration of explicit template specialization + template <typename T> + struct $decl[[Foo]] {}; + + template <> + struct Fo^o<int> {}; + )cpp", + + R"cpp(// Declaration of partial template specialization + template <typename T> + struct $decl[[Foo]] {}; + + template <typename T> + struct Fo^o<T*> {}; + )cpp"}; for (const char *Test : Tests) { Annotations T(Test); llvm::Optional<Range> WantDecl; Index: clang-tools-extra/clangd/XRefs.cpp =================================================================== --- clang-tools-extra/clangd/XRefs.cpp +++ clang-tools-extra/clangd/XRefs.cpp @@ -191,10 +191,10 @@ // Macros are simple: there's no declaration/definition distinction. // As a consequence, there's no need to look them up in the index either. - SourceLocation MaybeMacroLocation = SM.getMacroArgExpandedLocation( + SourceLocation IdentStartLoc = SM.getMacroArgExpandedLocation( getBeginningOfIdentifier(Pos, AST.getSourceManager(), AST.getLangOpts())); std::vector<LocatedSymbol> Result; - if (auto M = locateMacroAt(MaybeMacroLocation, AST.getPreprocessor())) { + if (auto M = locateMacroAt(IdentStartLoc, AST.getPreprocessor())) { if (auto Loc = makeLocation(AST.getASTContext(), M->Info->getDefinitionLoc(), *MainFilePath)) { LocatedSymbol Macro; @@ -234,6 +234,17 @@ for (const Decl *D : getDeclAtPosition(AST, SourceLoc, Relations)) { const Decl *Def = getDefinition(D); const Decl *Preferred = Def ? Def : D; + + // If we're at the point of declaration of a template specialization, + // it's more useful to navigate to the template declaration. + if (Preferred->getLocation() == IdentStartLoc) { + if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Preferred)) { + D = CTSD->getSpecializedTemplate(); + Def = getDefinition(D); + Preferred = Def ? Def : D; + } + } + auto Loc = makeLocation(AST.getASTContext(), spellingLocIfSpelled(findName(Preferred), SM), *MainFilePath);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits