[clang-tools-extra] e102338 - Support call hierarchy for fields and non-local variables (#113900)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 2 23:38:28 PDT 2024
Author: timon-ul
Date: 2024-11-03T01:38:25-05:00
New Revision: e102338b6e2f9ec1a882bdfb91eb4e364c7b6478
URL: https://github.com/llvm/llvm-project/commit/e102338b6e2f9ec1a882bdfb91eb4e364c7b6478
DIFF: https://github.com/llvm/llvm-project/commit/e102338b6e2f9ec1a882bdfb91eb4e364c7b6478.diff
LOG: Support call hierarchy for fields and non-local variables (#113900)
Fixes https://github.com/clangd/clangd/issues/1308
Added:
Modified:
clang-tools-extra/clangd/XRefs.cpp
clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index b34aba603b5306..4fd11307857ff8 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -2238,7 +2238,10 @@ prepareCallHierarchy(ParsedAST &AST, Position Pos, PathRef TUPath) {
for (const NamedDecl *Decl : getDeclAtPosition(AST, *Loc, {})) {
if (!(isa<DeclContext>(Decl) &&
cast<DeclContext>(Decl)->isFunctionOrMethod()) &&
- Decl->getKind() != Decl::Kind::FunctionTemplate)
+ Decl->getKind() != Decl::Kind::FunctionTemplate &&
+ !(Decl->getKind() == Decl::Kind::Var &&
+ !cast<VarDecl>(Decl)->isLocalVarDecl()) &&
+ Decl->getKind() != Decl::Kind::Field)
continue;
if (auto CHI = declToCallHierarchyItem(*Decl, AST.tuPath()))
Result.emplace_back(std::move(*CHI));
diff --git a/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp b/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp
index 6fa76aa6094bf2..b2278ff12735dc 100644
--- a/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp
@@ -446,6 +446,51 @@ TEST(CallHierarchy, CallInLocalVarDecl) {
AllOf(from(withName("caller3")), fromRanges(Source.range("call3")))));
}
+TEST(CallHierarchy, HierarchyOnField) {
+ // Tests that the call hierarchy works on fields.
+ Annotations Source(R"cpp(
+ struct Vars {
+ int v^ar1 = 1;
+ };
+ void caller() {
+ Vars values;
+ values.$Callee[[var1]];
+ }
+ )cpp");
+ TestTU TU = TestTU::withCode(Source.code());
+ auto AST = TU.build();
+ auto Index = TU.index();
+
+ std::vector<CallHierarchyItem> Items =
+ prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
+ ASSERT_THAT(Items, ElementsAre(withName("var1")));
+ auto IncomingLevel1 = incomingCalls(Items[0], Index.get());
+ ASSERT_THAT(IncomingLevel1,
+ ElementsAre(AllOf(from(withName("caller")),
+ fromRanges(Source.range("Callee")))));
+}
+
+TEST(CallHierarchy, HierarchyOnVar) {
+ // Tests that the call hierarchy works on non-local variables.
+ Annotations Source(R"cpp(
+ int v^ar = 1;
+ void caller() {
+ $Callee[[var]];
+ }
+ )cpp");
+ TestTU TU = TestTU::withCode(Source.code());
+ auto AST = TU.build();
+ auto Index = TU.index();
+
+ std::vector<CallHierarchyItem> Items =
+ prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
+ ASSERT_THAT(Items, ElementsAre(withName("var")));
+ auto IncomingLevel1 = incomingCalls(Items[0], Index.get());
+ ASSERT_THAT(IncomingLevel1,
+ ElementsAre(AllOf(from(withName("caller")),
+ fromRanges(Source.range("Callee")))));
+}
+
} // namespace
} // namespace clangd
} // namespace clang
More information about the cfe-commits
mailing list