https://github.com/timon-ul created 
https://github.com/llvm/llvm-project/pull/147042

Implementation for https://github.com/clangd/clangd/issues/2203 

>From 464db70ca20e0924d37ebf6c5147721e382d5063 Mon Sep 17 00:00:00 2001
From: Timon Ulrich <timon.ulr...@advantest.com>
Date: Wed, 2 Jul 2025 15:23:46 +0200
Subject: [PATCH 1/2] Added call hierarchy for enum constants

---
 clang-tools-extra/clangd/XRefs.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clangd/XRefs.cpp 
b/clang-tools-extra/clangd/XRefs.cpp
index 089f8158c9aa5..5bbc681cf04e2 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -2287,7 +2287,8 @@ prepareCallHierarchy(ParsedAST &AST, Position Pos, 
PathRef TUPath) {
         Decl->getKind() != Decl::Kind::FunctionTemplate &&
         !(Decl->getKind() == Decl::Kind::Var &&
           !cast<VarDecl>(Decl)->isLocalVarDecl()) &&
-        Decl->getKind() != Decl::Kind::Field)
+        Decl->getKind() != Decl::Kind::Field &&
+        Decl->getKind() != Decl::Kind::EnumConstant)
       continue;
     if (auto CHI = declToCallHierarchyItem(*Decl, AST.tuPath()))
       Result.emplace_back(std::move(*CHI));

>From bfc167d2da892dfd9624eff651cece2b3328cee4 Mon Sep 17 00:00:00 2001
From: Timon Ulrich <timon.ulr...@advantest.com>
Date: Thu, 3 Jul 2025 13:54:43 +0200
Subject: [PATCH 2/2] Added test for CallHierarchy on enum constants

---
 .../clangd/unittests/CallHierarchyTests.cpp   | 29 +++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp 
b/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp
index eb852ef5ee00b..08cc80ff8981e 100644
--- a/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp
@@ -633,6 +633,35 @@ TEST(CallHierarchy, HierarchyOnVar) {
                                 iFromRanges(Source.range("Callee")))));
 }
 
+TEST(CallHierarchy, HierarchyOnEnumConstant) {
+  // Tests that the call hierarchy works on enum constants.
+  Annotations Source(R"cpp(
+    enum class Coin { heads$Heads^ , tai$Tails^ls };
+    void caller() {
+      Coin::$CallerH[[heads]];
+      Coin::$CallerT[[tails]];
+    }
+  )cpp");
+  TestTU TU = TestTU::withCode(Source.code());
+  auto AST = TU.build();
+  auto Index = TU.index();
+
+  std::vector<CallHierarchyItem> Items =
+      prepareCallHierarchy(AST, Source.point("Heads"), testPath(TU.Filename));
+  ASSERT_THAT(Items, ElementsAre(withName("heads")));
+  auto IncomingLevel1 = incomingCalls(Items[0], Index.get());
+  ASSERT_THAT(IncomingLevel1,
+              ElementsAre(AllOf(from(withName("caller")),
+                                iFromRanges(Source.range("CallerH")))));
+  Items =
+      prepareCallHierarchy(AST, Source.point("Tails"), testPath(TU.Filename));
+  ASSERT_THAT(Items, ElementsAre(withName("tails")));
+  IncomingLevel1 = incomingCalls(Items[0], Index.get());
+  ASSERT_THAT(IncomingLevel1,
+              ElementsAre(AllOf(from(withName("caller")),
+                                iFromRanges(Source.range("CallerT")))));
+}
+
 TEST(CallHierarchy, CallInDifferentFileThanCaller) {
   Annotations Header(R"cpp(
     #define WALDO void caller() {

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to