[clang-tools-extra] 70d583a - [clangd] Have template template arguments target their referenced template decl
Nathan Ridge via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 10 10:27:44 PDT 2020
Author: Nathan Ridge
Date: 2020-08-10T13:27:23-04:00
New Revision: 70d583ad12872ef8714b15f1d1e982f1db6d9a15
URL: https://github.com/llvm/llvm-project/commit/70d583ad12872ef8714b15f1d1e982f1db6d9a15
DIFF: https://github.com/llvm/llvm-project/commit/70d583ad12872ef8714b15f1d1e982f1db6d9a15.diff
LOG: [clangd] Have template template arguments target their referenced template decl
Fixes https://github.com/clangd/clangd/issues/473
Differential Revision: https://reviews.llvm.org/D85503
Added:
Modified:
clang-tools-extra/clangd/FindTarget.cpp
clang-tools-extra/clangd/Selection.cpp
clang-tools-extra/clangd/unittests/FindTargetTests.cpp
clang-tools-extra/clangd/unittests/SelectionTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp
index e4d2dddb4b5d..734b8432c838 100644
--- a/clang-tools-extra/clangd/FindTarget.cpp
+++ b/clang-tools-extra/clangd/FindTarget.cpp
@@ -596,6 +596,19 @@ struct TargetFinder {
add(CCI->getAnyMember(), Flags);
// Constructor calls contain a TypeLoc node, so we don't handle them here.
}
+
+ void add(const TemplateArgument &Arg, RelSet Flags) {
+ // Only used for template template arguments.
+ // For type and non-type template arguments, SelectionTree
+ // will hit a more specific node (e.g. a TypeLoc or a
+ // DeclRefExpr).
+ if (Arg.getKind() == TemplateArgument::Template ||
+ Arg.getKind() == TemplateArgument::TemplateExpansion) {
+ if (TemplateDecl *TD = Arg.getAsTemplate().getAsTemplateDecl()) {
+ report(TD, Flags);
+ }
+ }
+ }
};
} // namespace
@@ -619,6 +632,8 @@ allTargetDecls(const ast_type_traits::DynTypedNode &N) {
Finder.add(*QT, Flags);
else if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>())
Finder.add(CCI, Flags);
+ else if (const TemplateArgumentLoc *TAL = N.get<TemplateArgumentLoc>())
+ Finder.add(TAL->getArgument(), Flags);
return Finder.takeDecls();
}
diff --git a/clang-tools-extra/clangd/Selection.cpp b/clang-tools-extra/clangd/Selection.cpp
index 2e3c491d561e..5c08485d8b92 100644
--- a/clang-tools-extra/clangd/Selection.cpp
+++ b/clang-tools-extra/clangd/Selection.cpp
@@ -479,6 +479,10 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
bool TraverseTypeLoc(TypeLoc X) {
return traverseNode(&X, [&] { return Base::TraverseTypeLoc(X); });
}
+ bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &X) {
+ return traverseNode(&X,
+ [&] { return Base::TraverseTemplateArgumentLoc(X); });
+ }
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc X) {
return traverseNode(
&X, [&] { return Base::TraverseNestedNameSpecifierLoc(X); });
diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index 3421b9cec2d3..838150815f5e 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -376,6 +376,15 @@ TEST_F(TargetDeclTest, ClassTemplate) {
{"template<> class Foo<int *>", Rel::TemplateInstantiation},
{"template <typename T> class Foo<T *>", Rel::TemplatePattern});
+ Code = R"cpp(
+ // Template template argument.
+ template<typename T> struct Vector {};
+ template <template <typename> class Container>
+ struct A {};
+ A<[[Vector]]> a;
+ )cpp";
+ EXPECT_DECLS("TemplateArgumentLoc", {"template <typename T> struct Vector"});
+
Flags.push_back("-std=c++17"); // for CTAD tests
Code = R"cpp(
diff --git a/clang-tools-extra/clangd/unittests/SelectionTests.cpp b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
index 6a9f587a7768..2026292bc6f3 100644
--- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -407,8 +407,16 @@ TEST(SelectionTest, CommonAncestor) {
s2[[-^>]]f();
}
)cpp",
- "DeclRefExpr"} // DeclRefExpr to the "operator->" method.
- };
+ "DeclRefExpr"}, // DeclRefExpr to the "operator->" method.
+
+ // Template template argument.
+ {R"cpp(
+ template <typename> class Vector {};
+ template <template <typename> class Container> class A {};
+ A<[[V^ector]]> a;
+ )cpp",
+ "TemplateArgumentLoc"}};
+
for (const Case &C : Cases) {
trace::TestTracer Tracer;
Annotations Test(C.Code);
More information about the cfe-commits
mailing list