[clang-tools-extra] Extend call hierarchy for field and non-local variables (PR #113900)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 28 05:44:03 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangd
Author: None (timon-ul)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/113900.diff
2 Files Affected:
- (modified) clang-tools-extra/clangd/XRefs.cpp (+4-1)
- (modified) clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp (+45)
``````````diff
diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index b34aba603b5306..53a0346a445d8e 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::Var &&
+ !cast<VarDecl>(Decl)->isLocalVarDecl()) &&
+ Decl->getKind() != Decl::Kind::FunctionTemplate &&
+ 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
``````````
</details>
https://github.com/llvm/llvm-project/pull/113900
More information about the cfe-commits
mailing list