[clang] acd22ee - [clang][bytecode][NFC] Add Record::findField to look up fields by offset (#212697)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 29 01:02:26 PDT 2026
Author: Timm Baeder
Date: 2026-07-29T10:02:20+02:00
New Revision: acd22eec9e79b69515aa2204dc96dcfa3ff0d387
URL: https://github.com/llvm/llvm-project/commit/acd22eec9e79b69515aa2204dc96dcfa3ff0d387
DIFF: https://github.com/llvm/llvm-project/commit/acd22eec9e79b69515aa2204dc96dcfa3ff0d387.diff
LOG: [clang][bytecode][NFC] Add Record::findField to look up fields by offset (#212697)
Added:
Modified:
clang/lib/AST/ByteCode/Interp.cpp
clang/lib/AST/ByteCode/Pointer.cpp
clang/lib/AST/ByteCode/Record.cpp
clang/lib/AST/ByteCode/Record.h
Removed:
################################################################################
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