[clang] [clang][bytecode][NFC] Add Record::findField to look up fields by offset (PR #212697)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 29 00:12:41 PDT 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/212697
None
>From 45a559b8b41ee8191b0ef76d96bc64033f06c8d5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 29 Jul 2026 09:06:59 +0200
Subject: [PATCH] [clang][bytecode][NFC] Add Record::findField to look up
fields by offset
---
clang/lib/AST/ByteCode/Interp.cpp | 5 +----
clang/lib/AST/ByteCode/Pointer.cpp | 8 +-------
clang/lib/AST/ByteCode/Record.cpp | 9 +++++++++
clang/lib/AST/ByteCode/Record.h | 3 +++
4 files changed, 14 insertions(+), 11 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index f59485ec306e4..23bdc5ea28c03 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1572,10 +1572,7 @@ static bool diagnoseTypeIdField(InterpState &S, CodePtr OpPC,
Ptr.asTypeidPointer().TypeInfoType->getAsRecordDecl());
if (!R)
return false;
- const Record::Field *Field =
- llvm::find_if(R->fields(), [=](const Record::Field &F) -> bool {
- return F.Offset == Offset;
- });
+ const Record::Field *Field = R->findField(Offset);
if (!Field)
return false;
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 243a2c9ea4b8f..972dbfbbbd15d 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -1149,13 +1149,7 @@ std::optional<IntPointer> IntPointer::atOffset(const interp::Context &Ctx,
if (!R)
return *this;
- const Record::Field *F = nullptr;
- for (auto &It : R->fields()) {
- if (It.Offset == Offset) {
- F = &It;
- break;
- }
- }
+ const Record::Field *F = R->findField(Offset);
if (!F)
return *this;
diff --git a/clang/lib/AST/ByteCode/Record.cpp b/clang/lib/AST/ByteCode/Record.cpp
index 2d217e7594a18..47f7dea656f32 100644
--- a/clang/lib/AST/ByteCode/Record.cpp
+++ b/clang/lib/AST/ByteCode/Record.cpp
@@ -49,6 +49,15 @@ bool Record::hasTrivialDtor() const {
return !Dtor || Dtor->isTrivial();
}
+const Record::Field *Record::findField(unsigned Offset) const {
+ if (auto It = llvm::find_if(
+ Fields,
+ [=](const Record::Field &F) -> bool { return F.Offset == Offset; });
+ It != Fields.end())
+ return &*It;
+ return nullptr;
+}
+
const Record::Base *Record::getBase(const RecordDecl *RD) const {
auto It = BaseMap.find(RD);
assert(It != BaseMap.end() && "Missing base");
diff --git a/clang/lib/AST/ByteCode/Record.h b/clang/lib/AST/ByteCode/Record.h
index 9fbed79a241be..955f816e52c1e 100644
--- a/clang/lib/AST/ByteCode/Record.h
+++ b/clang/lib/AST/ByteCode/Record.h
@@ -93,6 +93,9 @@ class Record final {
unsigned getNumFields() const { return Fields.size(); }
const Field *getField(unsigned I) const { return &Fields[I]; }
+ /// Find a field with the given offset.
+ /// This does a linear search, so use sparingly.
+ const Field *findField(unsigned Offset) const;
/// Returns a field.
const Field *getField(const FieldDecl *FD) const {
return &Fields[FD->getFieldIndex()];
More information about the cfe-commits
mailing list