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

Merged
merged 1 commit into from
Jun 13, 2025

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
@ArcsinX
Copy link
Contributor Author

ArcsinX commented May 26, 2025

Friendly ping

@ArcsinX
Copy link
Contributor Author

ArcsinX commented Jun 3, 2025

Maybe someone can take a look at this? The diff is very small.

@ArcsinX
Copy link
Contributor Author

ArcsinX commented Jun 10, 2025

Ping

Copy link
Member

@kadircet kadircet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the late response and thanks a lot!

@ArcsinX ArcsinX force-pushed the array-designators branch from ab701eb to 516384e Compare June 13, 2025 11:24
@ArcsinX ArcsinX force-pushed the array-designators branch from 516384e to c443180 Compare June 13, 2025 13:47
@ArcsinX
Copy link
Contributor Author

ArcsinX commented Jun 13, 2025

sorry for the late response and thanks a lot!

Thank you for review!

@ArcsinX ArcsinX merged commit ca50409 into llvm:main Jun 13, 2025
7 checks passed
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
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.

3 participants