[clang] f3bc580 - [clang][bytecode] Add `Pointer::getRootExpr()` (#216706)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 17 21:56:29 PDT 2026
Author: Timm Baeder
Date: 2026-08-18T06:56:24+02:00
New Revision: f3bc5801e20ce00feb55313735d7ee6b4d62bb23
URL: https://github.com/llvm/llvm-project/commit/f3bc5801e20ce00feb55313735d7ee6b4d62bb23
DIFF: https://github.com/llvm/llvm-project/commit/f3bc5801e20ce00feb55313735d7ee6b4d62bb23.diff
LOG: [clang][bytecode] Add `Pointer::getRootExpr()` (#216706)
Similarly to `getRootVarDecl()`, returns the expression of the base of
the pointer, if there is such an expression.
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.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 1f1db99973f2b..43020f5ad84c1 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -316,7 +316,7 @@ bool CheckBCPResult(InterpState &S, const Pointer &Ptr) {
if (Ptr.getType()->isAnyComplexType())
return true;
- if (const Expr *Base = Ptr.getDeclDesc()->asExpr())
+ if (const Expr *Base = Ptr.getRootExpr())
return isa<StringLiteral>(Base) && Ptr.getIndex() == 0;
return false;
}
@@ -1437,7 +1437,7 @@ bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
QualType InitialType = Ptr.getType();
Ptr = Ptr.expand().stripBaseCasts();
- Source = Ptr.getDeclDesc()->asExpr();
+ Source = Ptr.getRootExpr();
BlockToDelete = Ptr.block();
// Check that new[]/delete[] or new/delete were used, not a mixture.
@@ -2856,8 +2856,8 @@ bool arePotentiallyOverlappingStringLiterals(const Pointer &LHS,
unsigned LHSOffset = LHS.isOnePastEnd() ? LHS.getNumElems() : LHS.getIndex();
unsigned RHSOffset = RHS.isOnePastEnd() ? RHS.getNumElems() : RHS.getIndex();
- const auto *LHSLit = cast<StringLiteral>(LHS.getDeclDesc()->asExpr());
- const auto *RHSLit = cast<StringLiteral>(RHS.getDeclDesc()->asExpr());
+ const auto *LHSLit = cast<StringLiteral>(LHS.getRootExpr());
+ const auto *RHSLit = cast<StringLiteral>(RHS.getRootExpr());
StringRef LHSStr(LHSLit->getBytes());
unsigned LHSLength = LHSStr.size();
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 054fba2c87c45..9c90321da1c36 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -1402,7 +1402,7 @@ inline bool CmpHelperEQ<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
if (P.isZero())
continue;
if (P.pointsToLiteral()) {
- const Expr *E = P.getDeclDesc()->asExpr();
+ const Expr *E = P.getRootExpr();
if (isa<StringLiteral>(E)) {
const SourceInfo &Loc = S.Current->getSource(OpPC);
S.FFDiag(Loc, diag::note_constexpr_literal_comparison);
@@ -1833,9 +1833,8 @@ bool InitGlobalTemp(InterpState &S, uint32_t I,
assert(Temp);
const Pointer &Ptr = S.P.getGlobal(I);
- assert(Ptr.getDeclDesc()->asExpr());
- S.SeenGlobalTemporaries.push_back(
- std::make_pair(Ptr.getDeclDesc()->asExpr(), Temp));
+ assert(Ptr.getRootExpr());
+ S.SeenGlobalTemporaries.push_back(std::make_pair(Ptr.getRootExpr(), Temp));
Ptr.deref<T>() = S.Stk.pop<T>();
Ptr.initialize();
@@ -1852,8 +1851,7 @@ inline bool InitGlobalTempComp(InterpState &S,
assert(Temp);
const Pointer &Ptr = S.Stk.peek<Pointer>();
- S.SeenGlobalTemporaries.push_back(
- std::make_pair(Ptr.getDeclDesc()->asExpr(), Temp));
+ S.SeenGlobalTemporaries.push_back(std::make_pair(Ptr.getRootExpr(), Temp));
return true;
}
@@ -3007,7 +3005,7 @@ bool CastPointerIntegral(InterpState &S, CodePtr OpPC) {
IntegralKind Kind = IntegralKind::Address;
const void *PtrVal;
if (Ptr.isDummy()) {
- if (const Expr *E = Ptr.getDeclDesc()->asExpr()) {
+ if (const Expr *E = Ptr.getRootExpr()) {
PtrVal = E;
if (isa<AddrLabelExpr>(E))
Kind = IntegralKind::LabelAddress;
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index a61451f22631d..9386a12ac13c2 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1381,7 +1381,7 @@ static bool interp__builtin_assume_aligned(InterpState &S, CodePtr OpPC,
CharUnits BaseAlignment;
if (const auto *VD = Ptr.getDeclDesc()->asValueDecl())
BaseAlignment = S.getASTContext().getDeclAlign(VD);
- else if (const auto *E = Ptr.getDeclDesc()->asExpr())
+ else if (const auto *E = Ptr.getRootExpr())
BaseAlignment = GetAlignOfExpr(S.getASTContext(), E, UETT_AlignOf);
if (BaseAlignment < Align) {
@@ -1664,7 +1664,7 @@ static bool interp__builtin_operator_delete(InterpState &S, CodePtr OpPC,
return true;
}
- Source = Ptr.getDeclDesc()->asExpr();
+ Source = Ptr.getRootExpr();
BlockToDelete = Ptr.block();
if (!BlockToDelete->isDynamic()) {
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 38314312c7e79..55903c4cff70c 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -1158,6 +1158,12 @@ const VarDecl *Pointer::getRootVarDecl() const {
return nullptr;
}
+const Expr *Pointer::getRootExpr() const {
+ if (isBlockPointer())
+ return getDeclDesc()->asExpr();
+ return nullptr;
+}
+
std::optional<IntPointer> IntPointer::atOffset(const interp::Context &Ctx,
unsigned Offset) const {
QualType CurType = getPointeeType();
diff --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h
index a11031fb7a240..fa16c28712ac5 100644
--- a/clang/lib/AST/ByteCode/Pointer.h
+++ b/clang/lib/AST/ByteCode/Pointer.h
@@ -586,6 +586,7 @@ class Pointer {
}
const VarDecl *getRootVarDecl() const;
+ const Expr *getRootExpr() const;
[[nodiscard]] Pointer getDeclPtr() const { return Pointer(BS.Pointee); }
More information about the cfe-commits
mailing list