Skip to content

[clangd] Collect references in array designators #140356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ArcsinX
Copy link
Contributor

@ArcsinX ArcsinX commented May 17, 2025

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):

  const int Foo = 0;
  int Bar[] = {
    [Foo...Foo + 1] = 0,
    [Foo + 2] = 1
  };

@ArcsinX ArcsinX added the clangd label May 17, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang-tools-extra labels May 17, 2025
@llvmbot
Copy link
Member

llvmbot commented May 17, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clangd

Author: Aleksandr Platonov (ArcsinX)

Changes

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):

  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 --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;

@ArcsinX ArcsinX requested a review from llvm-beanz May 20, 2025 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category clang-tools-extra clangd
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants