[clang-tools-extra] Extend call hierarchy for field and non-local variables (PR #113900)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 1 09:35:30 PDT 2024
https://github.com/timon-ul updated https://github.com/llvm/llvm-project/pull/113900
>From 96c759835b798b8f002adb4835db08b8de5da97a Mon Sep 17 00:00:00 2001
From: Timon Ulrich <timon.ulrich at advantest.com>
Date: Mon, 28 Oct 2024 11:03:10 +0100
Subject: [PATCH 1/3] Added call hierarchy for Field and non-local variables
---
clang-tools-extra/clangd/XRefs.cpp | 5 ++-
.../clangd/unittests/CallHierarchyTests.cpp | 45 +++++++++++++++++++
2 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index b34aba603b5306..b18a33b8ca7e8e 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..4ddb8d486bdfad 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 fields.
+ 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
>From 0db63a5f3a86ee844bbb499a7fe188a59f9fca45 Mon Sep 17 00:00:00 2001
From: Timon Ulrich <timon.ulrich at advantest.com>
Date: Mon, 28 Oct 2024 13:38:58 +0100
Subject: [PATCH 2/3] Formatting changes
---
clang-tools-extra/clangd/XRefs.cpp | 4 ++--
clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp | 6 +++---
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index b18a33b8ca7e8e..53a0346a445d8e 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -2238,8 +2238,8 @@ 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::Var &&
- !cast<VarDecl>(Decl)->isLocalVarDecl())) &&
+ !(Decl->getKind() == Decl::Kind::Var &&
+ !cast<VarDecl>(Decl)->isLocalVarDecl()) &&
Decl->getKind() != Decl::Kind::FunctionTemplate &&
Decl->getKind() != Decl::Kind::Field)
continue;
diff --git a/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp b/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp
index 4ddb8d486bdfad..b2278ff12735dc 100644
--- a/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp
@@ -446,7 +446,7 @@ TEST(CallHierarchy, CallInLocalVarDecl) {
AllOf(from(withName("caller3")), fromRanges(Source.range("call3")))));
}
-TEST(CallHierarchy, HierarchyOnField){
+TEST(CallHierarchy, HierarchyOnField) {
// Tests that the call hierarchy works on fields.
Annotations Source(R"cpp(
struct Vars {
@@ -470,8 +470,8 @@ TEST(CallHierarchy, HierarchyOnField){
fromRanges(Source.range("Callee")))));
}
-TEST(CallHierarchy, HierarchyOnVar){
- // Tests that the call hierarchy works on fields.
+TEST(CallHierarchy, HierarchyOnVar) {
+ // Tests that the call hierarchy works on non-local variables.
Annotations Source(R"cpp(
int v^ar = 1;
void caller() {
>From 7fa211e9bc26bf42fa386c6fa1385bf3d658f815 Mon Sep 17 00:00:00 2001
From: Timon Ulrich <timon.ulrich at advantest.com>
Date: Fri, 1 Nov 2024 17:33:23 +0100
Subject: [PATCH 3/3] Rearranged if condition
---
clang-tools-extra/clangd/XRefs.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index 53a0346a445d8e..4fd11307857ff8 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -2238,9 +2238,9 @@ 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()))
More information about the cfe-commits
mailing list