[clang] [clang][bytecode] Add Record::findBase() (PR #212952)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 30 01:26:12 PDT 2026


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/212952

Similar to findField().

>From 4521884fde0d14b7a003311745cc76d35842d172 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 30 Jul 2026 10:24:53 +0200
Subject: [PATCH] [clang][bytecode] Add Record::findBase()

Similar to findField().
---
 clang/lib/AST/ByteCode/Pointer.cpp | 12 ++++--------
 clang/lib/AST/ByteCode/Record.cpp  |  9 +++++++++
 clang/lib/AST/ByteCode/Record.h    |  1 +
 3 files changed, 14 insertions(+), 8 deletions(-)

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 {



More information about the cfe-commits mailing list