[clang] [clang][bytecode] Add Record::findBase() (PR #212952)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 30 01:26:55 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
Similar to findField().
---
Full diff: https://github.com/llvm/llvm-project/pull/212952.diff
3 Files Affected:
- (modified) clang/lib/AST/ByteCode/Pointer.cpp (+4-8)
- (modified) clang/lib/AST/ByteCode/Record.cpp (+9)
- (modified) clang/lib/AST/ByteCode/Record.h (+1)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 972dbfbbbd15d..b987b350f9537 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -1177,18 +1177,14 @@ IntPointer IntPointer::baseCast(const interp::Context &Ctx,
return *this;
const Record *R = Ctx.getRecord(CurType->getAsRecordDecl());
- const Descriptor *BaseDesc = nullptr;
// This iterates over bases and checks for the proper offset. That's
// potentially slow but this case really shouldn't happen a lot.
- for (const Record::Base &B : R->bases()) {
- if (B.Offset == BaseOffset) {
- BaseDesc = B.Desc;
- break;
- }
- }
- assert(BaseDesc);
+ const Record::Base *B = R->findBase(BaseOffset);
+ if (!B)
+ return *this;
+ const Descriptor *BaseDesc = B->Desc;
// Adjust the offset value based on the information from the record layout.
const ASTContext &ASTCtx = Ctx.getASTContext();
const ASTRecordLayout &Layout = ASTCtx.getASTRecordLayout(R->getDecl());
diff --git a/clang/lib/AST/ByteCode/Record.cpp b/clang/lib/AST/ByteCode/Record.cpp
index 47f7dea656f32..790061baaa560 100644
--- a/clang/lib/AST/ByteCode/Record.cpp
+++ b/clang/lib/AST/ByteCode/Record.cpp
@@ -74,6 +74,15 @@ const Record::Base *Record::getBase(QualType T) const {
return nullptr;
}
+const Record::Base *Record::findBase(unsigned Offset) const {
+ if (auto It = llvm::find_if(
+ Bases,
+ [=](const Record::Base &B) -> bool { return B.Offset == Offset; });
+ It != Bases.end())
+ return &*It;
+ return nullptr;
+}
+
const Record::Base *Record::getVirtualBase(const RecordDecl *FD) const {
auto It = VirtualBaseMap.find(FD);
if (It == VirtualBaseMap.end())
diff --git a/clang/lib/AST/ByteCode/Record.h b/clang/lib/AST/ByteCode/Record.h
index 955f816e52c1e..e49f31a1d6a52 100644
--- a/clang/lib/AST/ByteCode/Record.h
+++ b/clang/lib/AST/ByteCode/Record.h
@@ -116,6 +116,7 @@ class Record final {
/// Returns a base descriptor.
const Base *getBase(const RecordDecl *RD) const;
const Base *getBaseOrNull(const RecordDecl *RD) const;
+ const Base *findBase(unsigned Offset) const;
using const_virtual_iter = VirtualBaseList::const_iterator;
llvm::iterator_range<const_virtual_iter> virtual_bases() const {
``````````
</details>
https://github.com/llvm/llvm-project/pull/212952
More information about the cfe-commits
mailing list