[clang] [clang-tools-extra] [clangd] Collect references in array designators (PR #140356)

Aleksandr Platonov via cfe-commits cfe-commits at lists.llvm.org
Fri May 16 22:13:09 PDT 2025


https://github.com/ArcsinX created https://github.com/llvm/llvm-project/pull/140356

Without this patch clangd doesn't collect references in array designators. E.g. Find All References for symbol `Foo` in the following code gives only 1 result (in definition):
```c
  const int Foo = 0;
  int Bar[] = {
    [Foo...Foo + 1] = 0,
    [Foo + 2] = 1
  };
```

>From 382ac0f0147cfefef9ef1e9b4745178c0b001e00 Mon Sep 17 00:00:00 2001
From: Aleksandr Platonov <platonov.aleksandr at huawei.com>
Date: Sat, 17 May 2025 08:05:21 +0300
Subject: [PATCH] [clangd] Collect references in array designators

---
 clang-tools-extra/clangd/unittests/XRefsTests.cpp | 8 ++++++++
 clang/lib/Index/IndexBody.cpp                     | 7 +++++++
 2 files changed, 15 insertions(+)

diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
index 1892f87c8e82a..b04d6431f89f9 100644
--- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -2311,6 +2311,14 @@ TEST(FindReferences, WithinAST) {
             $(S::deleteObject)[[de^lete]] S;
           }
         };
+      )cpp",
+      // Array designators
+      R"cpp(
+        const int $def[[F^oo]] = 0;
+        int Bar[] = {
+          [$(Bar)[[F^oo]]...$(Bar)[[Fo^o]] + 1] = 0,
+          [$(Bar)[[^Foo]] + 2] = 1
+        };
       )cpp"};
   for (const char *Test : Tests)
     checkFindRefs(Test);
diff --git a/clang/lib/Index/IndexBody.cpp b/clang/lib/Index/IndexBody.cpp
index 2ed20df22bda0..98ce6f73ec849 100644
--- a/clang/lib/Index/IndexBody.cpp
+++ b/clang/lib/Index/IndexBody.cpp
@@ -435,6 +435,13 @@ class BodyIndexer : public RecursiveASTVisitor<BodyIndexer> {
                                             ParentDC, SymbolRoleSet(),
                                             /*Relations=*/{}, E);
           }
+        } else {
+          if (D.isArrayDesignator())
+            TraverseStmt(E->getArrayIndex(D));
+          else if (D.isArrayRangeDesignator()) {
+            TraverseStmt(E->getArrayRangeStart(D));
+            TraverseStmt(E->getArrayRangeEnd(D));
+          }
         }
       }
       return true;



More information about the cfe-commits mailing list