[clang] a15adf5 - [clang][bytecode][NFC] Add Pointer::stripBaseCasts() helper (#176875)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 20 02:21:27 PST 2026
Author: Timm Baeder
Date: 2026-01-20T11:21:22+01:00
New Revision: a15adf5b9be33d557c45a5a1cb381f82782e5199
URL: https://github.com/llvm/llvm-project/commit/a15adf5b9be33d557c45a5a1cb381f82782e5199
DIFF: https://github.com/llvm/llvm-project/commit/a15adf5b9be33d557c45a5a1cb381f82782e5199.diff
LOG: [clang][bytecode][NFC] Add Pointer::stripBaseCasts() helper (#176875)
We do this is in a few places, so add a helper function for it.
Added:
Modified:
clang/lib/AST/ByteCode/Interp.cpp
clang/lib/AST/ByteCode/Interp.h
clang/lib/AST/ByteCode/InterpBuiltin.cpp
clang/lib/AST/ByteCode/Pointer.h
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 0205d840fd71e..768e24c038cda 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1320,8 +1320,7 @@ bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
// Remove base casts.
QualType InitialType = Ptr.getType();
- while (Ptr.isBaseClass())
- Ptr = Ptr.getBase();
+ Ptr = Ptr.stripBaseCasts();
Source = Ptr.getDeclDesc()->asExpr();
BlockToDelete = Ptr.block();
@@ -1709,8 +1708,7 @@ bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
static bool GetDynamicDecl(InterpState &S, CodePtr OpPC, Pointer TypePtr,
const CXXRecordDecl *&DynamicDecl) {
- while (TypePtr.isBaseClass())
- TypePtr = TypePtr.getBase();
+ TypePtr = TypePtr.stripBaseCasts();
QualType DynamicType = TypePtr.getType();
if (TypePtr.isStatic() || TypePtr.isConst()) {
@@ -1794,8 +1792,7 @@ bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
// If the function we call is further DOWN the hierarchy than the
// FieldDesc of our pointer, just go up the hierarchy of this field
// the furthest we can go.
- while (ThisPtr.isBaseClass())
- ThisPtr = ThisPtr.getBase();
+ ThisPtr = ThisPtr.stripBaseCasts();
}
}
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 0eae7848b4fe0..75961126fbce9 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -1908,10 +1908,7 @@ inline bool CheckNull(InterpState &S, CodePtr OpPC) {
inline bool VirtBaseHelper(InterpState &S, CodePtr OpPC, const RecordDecl *Decl,
const Pointer &Ptr) {
- Pointer Base = Ptr;
- while (Base.isBaseClass())
- Base = Base.getBase();
-
+ Pointer Base = Ptr.stripBaseCasts();
const Record::Base *VirtBase = Base.getRecord()->getVirtualBase(Decl);
S.Stk.push<Pointer>(Base.atField(VirtBase->Offset));
return true;
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 1b651413f43d7..5d3b6aa77685b 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1894,13 +1894,8 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
// Check for overlapping memory regions.
if (!Move && Pointer::pointToSameBlock(SrcPtr, DestPtr)) {
// Remove base casts.
- Pointer SrcP = SrcPtr;
- while (SrcP.isBaseClass())
- SrcP = SrcP.getBase();
-
- Pointer DestP = DestPtr;
- while (DestP.isBaseClass())
- DestP = DestP.getBase();
+ Pointer SrcP = SrcPtr.stripBaseCasts();
+ Pointer DestP = DestPtr.stripBaseCasts();
unsigned SrcIndex = SrcP.expand().getIndex() * SrcP.elemSize();
unsigned DstIndex = DestP.expand().getIndex() * DestP.elemSize();
@@ -2334,10 +2329,8 @@ static bool interp__builtin_object_size(InterpState &S, CodePtr OpPC,
// The "closest surrounding subobject" is NOT a base class,
// so strip the base class casts.
- if (UseFieldDesc && Ptr.isBaseClass()) {
- while (Ptr.isBaseClass())
- Ptr = Ptr.getBase();
- }
+ if (UseFieldDesc && Ptr.isBaseClass())
+ Ptr = Ptr.stripBaseCasts();
const Descriptor *Desc = UseFieldDesc ? Ptr.getFieldDesc() : DeclDesc;
assert(Desc);
diff --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h
index 202508cb71e5a..32e1c3268484e 100644
--- a/clang/lib/AST/ByteCode/Pointer.h
+++ b/clang/lib/AST/ByteCode/Pointer.h
@@ -751,6 +751,16 @@ class Pointer {
getInlineDesc()->LifeState = Lifetime::Started;
}
+ /// Strip base casts from this Pointer.
+ /// The result is either a root pointer or something
+ /// that isn't a base class anymore.
+ [[nodiscard]] Pointer stripBaseCasts() const {
+ Pointer P = *this;
+ while (P.isBaseClass())
+ P = P.getBase();
+ return P;
+ }
+
/// Compare two pointers.
ComparisonCategoryResult compare(const Pointer &Other) const {
if (!hasSameBase(*this, Other))
More information about the cfe-commits
mailing list