diff options
| author | Amr Hesham <amr96@programmer.net> | 2025-10-23 11:04:09 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-23 09:04:09 +0000 |
| commit | 4f99111faf51a27f138f46f90bb1445a8962d13b (patch) | |
| tree | 16fc0ca4b42df270fd8c563fbff038f9ac025401 /clang/lib | |
| parent | f567367c7f1d543c2825f13aed83661dd4790e66 (diff) | |
| download | llvm-4f99111faf51a27f138f46f90bb1445a8962d13b.zip llvm-4f99111faf51a27f138f46f90bb1445a8962d13b.tar.gz llvm-4f99111faf51a27f138f46f90bb1445a8962d13b.tar.bz2 | |
[CIR] ConstRecordBuilder check if attribute present before casting (#164575)
Fix the crash because in `ConstRecordBuilder::build` we cast to
TypedAttr then we check if it null, but in case that the result from
emitter is nullptr, that cast crash, In this PR I fixed the order to
check first if it not null, then casting to the TypedAttr
Diffstat (limited to 'clang/lib')
| -rw-r--r-- | clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp b/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp index 8f05014..7de3dd0 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp @@ -627,10 +627,7 @@ bool ConstRecordBuilder::applyZeroInitPadding(const ASTRecordLayout &layout, } bool ConstRecordBuilder::build(InitListExpr *ile, bool allowOverwrite) { - RecordDecl *rd = ile->getType() - ->castAs<clang::RecordType>() - ->getDecl() - ->getDefinitionOrSelf(); + RecordDecl *rd = ile->getType()->castAsRecordDecl(); const ASTRecordLayout &layout = cgm.getASTContext().getASTRecordLayout(rd); // Bail out if we have base classes. We could support these, but they only @@ -686,17 +683,14 @@ bool ConstRecordBuilder::build(InitListExpr *ile, bool allowOverwrite) { return false; } - mlir::TypedAttr eltInit; - if (init) - eltInit = mlir::cast<mlir::TypedAttr>( - emitter.tryEmitPrivateForMemory(init, field->getType())); - else - eltInit = mlir::cast<mlir::TypedAttr>(emitter.emitNullForMemory( - cgm.getLoc(ile->getSourceRange()), field->getType())); - - if (!eltInit) + mlir::Attribute eltInitAttr = + init ? emitter.tryEmitPrivateForMemory(init, field->getType()) + : emitter.emitNullForMemory(cgm.getLoc(ile->getSourceRange()), + field->getType()); + if (!eltInitAttr) return false; + mlir::TypedAttr eltInit = mlir::cast<mlir::TypedAttr>(eltInitAttr); if (!field->isBitField()) { // Handle non-bitfield members. if (!appendField(field, layout.getFieldOffset(index), eltInit, |
