[clang] [clang][bytecode] Handle invalid temporary descriptors (PR #125033)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 29 22:04:33 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
This happens e.g. when a vector element type is not primitive.
---
Full diff: https://github.com/llvm/llvm-project/pull/125033.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+28-13)
- (modified) clang/lib/AST/ByteCode/Compiler.h (+1-1)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 4659d0e00784d9..f7f4f713c787f2 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -562,8 +562,10 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
// We're creating a complex value here, so we need to
// allocate storage for it.
if (!Initializing) {
- unsigned LocalIndex = allocateTemporary(CE);
- if (!this->emitGetPtrLocal(LocalIndex, CE))
+ std::optional<unsigned> LocalIndex = allocateTemporary(CE);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, CE))
return false;
}
@@ -679,8 +681,10 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
assert(CE->getType()->isVectorType());
if (!Initializing) {
- unsigned LocalIndex = allocateTemporary(CE);
- if (!this->emitGetPtrLocal(LocalIndex, CE))
+ std::optional<unsigned> LocalIndex = allocateTemporary(CE);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, CE))
return false;
}
unsigned ToSize = CE->getType()->getAs<VectorType>()->getNumElements();
@@ -759,8 +763,10 @@ bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
return true;
if (!Initializing) {
- unsigned LocalIndex = allocateTemporary(E);
- if (!this->emitGetPtrLocal(LocalIndex, E))
+ std::optional<unsigned> LocalIndex = allocateTemporary(E);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, E))
return false;
}
@@ -1118,8 +1124,10 @@ template <class Emitter>
bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
// Prepare storage for result.
if (!Initializing) {
- unsigned LocalIndex = allocateTemporary(E);
- if (!this->emitGetPtrLocal(LocalIndex, E))
+ std::optional<unsigned> LocalIndex = allocateTemporary(E);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, E))
return false;
}
@@ -1175,7 +1183,10 @@ bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
if (!LHSIsComplex) {
// This is using the RHS type for the fake-complex LHS.
- LHSOffset = allocateTemporary(RHS);
+ std::optional<unsigned> LocalIndex = allocateTemporary(RHS);
+ if (!LocalIndex)
+ return false;
+ LHSOffset = *LocalIndex;
if (!this->emitGetPtrLocal(LHSOffset, E))
return false;
@@ -1347,8 +1358,10 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
// Prepare storage for result.
if (!Initializing && !E->isCompoundAssignmentOp()) {
- unsigned LocalIndex = allocateTemporary(E);
- if (!this->emitGetPtrLocal(LocalIndex, E))
+ std::optional<unsigned> LocalIndex = allocateTemporary(E);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, E))
return false;
}
@@ -4170,14 +4183,16 @@ Compiler<Emitter>::allocateLocal(DeclTy &&Src, QualType Ty,
}
template <class Emitter>
-unsigned Compiler<Emitter>::allocateTemporary(const Expr *E) {
+std::optional<unsigned> Compiler<Emitter>::allocateTemporary(const Expr *E) {
QualType Ty = E->getType();
assert(!Ty->isRecordType());
Descriptor *D = P.createDescriptor(
E, Ty.getTypePtr(), Descriptor::InlineDescMD, Ty.isConstQualified(),
/*IsTemporary=*/true, /*IsMutable=*/false, /*Init=*/nullptr);
- assert(D);
+
+ if (!D)
+ return std::nullopt;
Scope::Local Local = this->createLocal(D);
VariableScope<Emitter> *S = VarScope;
diff --git a/clang/lib/AST/ByteCode/Compiler.h b/clang/lib/AST/ByteCode/Compiler.h
index f9a597a16ef4ae..5a02f38d78dec8 100644
--- a/clang/lib/AST/ByteCode/Compiler.h
+++ b/clang/lib/AST/ByteCode/Compiler.h
@@ -309,7 +309,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
std::optional<unsigned>
allocateLocal(DeclTy &&Decl, QualType Ty = QualType(),
const ValueDecl *ExtendingDecl = nullptr);
- unsigned allocateTemporary(const Expr *E);
+ std::optional<unsigned> allocateTemporary(const Expr *E);
private:
friend class VariableScope<Emitter>;
``````````
</details>
https://github.com/llvm/llvm-project/pull/125033
More information about the cfe-commits
mailing list