From 60d35413662ebdcd1d31e34a8a7c665eb6977f1e Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Sat, 20 Dec 2008 20:46:34 +0000 Subject: Change EmitVLASize to take a QualType that must be a variably modified type. Emit the size even if the declared type is a variably modified type. This lets us handle void f(int n) { int (*a)[n]; printf("size: %d\n", sizeof(*a)); } git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61285 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CodeGenFunction.cpp | 53 +++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 21 deletions(-) (limited to 'lib/CodeGen/CodeGenFunction.cpp') diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp index 4a3bb3f6ca..6f32553118 100644 --- a/lib/CodeGen/CodeGenFunction.cpp +++ b/lib/CodeGen/CodeGenFunction.cpp @@ -410,29 +410,40 @@ llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) return SizeEntry; } -llvm::Value *CodeGenFunction::EmitVLASize(const VariableArrayType *VAT) +llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) { - llvm::Value *&SizeEntry = VLASizeMap[VAT]; - - assert(!SizeEntry && "Must not emit the same VLA size more than once!"); - // Get the element size; - llvm::Value *ElemSize; + assert(Ty->isVariablyModifiedType() && + "Must pass variably modified type to EmitVLASizes!"); - QualType ElemTy = VAT->getElementType(); - - if (const VariableArrayType *ElemVAT = - getContext().getAsVariableArrayType(ElemTy)) - ElemSize = EmitVLASize(ElemVAT); + if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { + llvm::Value *&SizeEntry = VLASizeMap[VAT]; + + assert(!SizeEntry && "Must not emit the same VLA size more than once!"); + + // Get the element size; + llvm::Value *ElemSize; + + QualType ElemTy = VAT->getElementType(); + + if (ElemTy->isVariableArrayType()) + ElemSize = EmitVLASize(ElemTy); + else { + // FIXME: We use Int32Ty here because the alloca instruction takes a + // 32-bit integer. What should we do about overflow? + ElemSize = llvm::ConstantInt::get(llvm::Type::Int32Ty, + getContext().getTypeSize(ElemTy) / 8); + } + + llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); + + SizeEntry = Builder.CreateMul(ElemSize, NumElements); + + return SizeEntry; + } else if (const PointerType *PT = Ty->getAsPointerType()) + EmitVLASize(PT->getPointeeType()); else { - // FIXME: We use Int32Ty here because the alloca instruction takes a - // 32-bit integer. What should we do about overflow? - ElemSize = llvm::ConstantInt::get(llvm::Type::Int32Ty, - getContext().getTypeSize(ElemTy) / 8); + assert(0 && "unknown VM type!"); } - - llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); - - SizeEntry = Builder.CreateMul(ElemSize, NumElements); - - return SizeEntry; + + return 0; } -- cgit v1.2.3