[clang-tools-extra] [clangd] Add go-to-definition support for fields in offsetof expressions (PR #192953)

via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 20 04:57:06 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Jakob Linke (schuay)

<details>
<summary>Changes</summary>

clangd had no handling for OffsetOfExpr in FindTarget.cpp, so go-to-definition and find-references on field names inside __builtin_offsetof(Type, field) produced no results.

Fix this by adding VisitOffsetOfExpr to both visitors:
- The TargetFinder visitor in add(const Stmt*) so that allTargetDecls() resolves the referenced FieldDecl(s).
- The refInStmt visitor so that findExplicitReferences() emits a ReferenceLoc per field component, enabling find-references and rename to locate each field at its source position.

Each OffsetOfNode of kind Field is visited; Identifier nodes (unresolved dependent-type designators) and Base nodes (implicit base-class indirections) are intentionally skipped.

Repro:
```                                                                                                                                                                                                                                             
  struct Foo { int bar; };                               
  int x = offsetof(Foo, bar);  // go-to-def on `bar` previously did nothing
```

---
Full diff: https://github.com/llvm/llvm-project/pull/192953.diff


2 Files Affected:

- (modified) clang-tools-extra/clangd/FindTarget.cpp (+18) 
- (modified) clang-tools-extra/clangd/unittests/FindTargetTests.cpp (+26) 


``````````diff
diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp
index 064e0db3b5b14..af54beb0a4e66 100644
--- a/clang-tools-extra/clangd/FindTarget.cpp
+++ b/clang-tools-extra/clangd/FindTarget.cpp
@@ -294,6 +294,13 @@ struct TargetFinder {
             break;
           }
       }
+      void VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
+        for (unsigned I = 0, N = OOE->getNumComponents(); I < N; ++I) {
+          const OffsetOfNode &Component = OOE->getComponent(I);
+          if (Component.getKind() == OffsetOfNode::Field)
+            Outer.add(Component.getField(), Flags);
+        }
+      }
       void VisitGotoStmt(const GotoStmt *Goto) {
         if (auto *LabelDecl = Goto->getLabel())
           Outer.add(LabelDecl, Flags);
@@ -815,6 +822,17 @@ llvm::SmallVector<ReferenceLoc> refInStmt(const Stmt *S,
       }
     }
 
+    void VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
+      for (unsigned I = 0, N = OOE->getNumComponents(); I < N; ++I) {
+        const OffsetOfNode &Component = OOE->getComponent(I);
+        if (Component.getKind() == OffsetOfNode::Field)
+          Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
+                                      Component.getEndLoc(),
+                                      /*IsDecl=*/false,
+                                      {Component.getField()}});
+      }
+    }
+
     void VisitGotoStmt(const GotoStmt *GS) {
       Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
                                   GS->getLabelLoc(),
diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index 6aee310208e5c..d4e4e37eadfa2 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -342,6 +342,20 @@ TEST_F(TargetDeclTest, DesignatedInit) {
   EXPECT_DECLS("DesignatedInitExpr", "int a");
 }
 
+TEST_F(TargetDeclTest, OffsetOf) {
+  Code = R"cpp(
+    struct Foo { int bar; };
+    int x = __builtin_offsetof(Foo, [[bar]]);
+  )cpp";
+  EXPECT_DECLS("OffsetOfExpr", "int bar");
+
+  Code = R"cpp(
+    struct Foo { int bar; int baz; };
+    int x = __builtin_offsetof(Foo, [[baz]]);
+  )cpp";
+  EXPECT_DECLS("OffsetOfExpr", "int baz");
+}
+
 TEST_F(TargetDeclTest, NestedNameSpecifier) {
   Code = R"cpp(
     namespace a { namespace b { int c; } }
@@ -2076,6 +2090,18 @@ TEST_F(FindExplicitReferencesTest, AllRefsInFoo) {
         "6: targets = {bar}, decl\n"
         "7: targets = {foo()::Bar::Foo}\n"
         "8: targets = {foo()::Baz::Field}\n"},
+       // offsetof
+       {R"cpp(
+            void foo() {
+              struct $0^Foo { int $1^bar; };
+              int $2^x = __builtin_offsetof($3^Foo, $4^bar);
+            }
+        )cpp",
+        "0: targets = {Foo}, decl\n"
+        "1: targets = {foo()::Foo::bar}, decl\n"
+        "2: targets = {x}, decl\n"
+        "3: targets = {Foo}\n"
+        "4: targets = {foo()::Foo::bar}\n"},
        {R"cpp(
            template<typename T>
            void crash(T);

``````````

</details>


https://github.com/llvm/llvm-project/pull/192953


More information about the cfe-commits mailing list