[clang] [clang-tools-extra] [clangd] Collect references in array designators (PR #140356)
via cfe-commits
cfe-commits at lists.llvm.org
Fri May 16 22:13:33 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangd
Author: Aleksandr Platonov (ArcsinX)
<details>
<summary>Changes</summary>
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
};
```
---
Full diff: https://github.com/llvm/llvm-project/pull/140356.diff
2 Files Affected:
- (modified) clang-tools-extra/clangd/unittests/XRefsTests.cpp (+8)
- (modified) clang/lib/Index/IndexBody.cpp (+7)
``````````diff
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/140356
More information about the cfe-commits
mailing list