diff options
author | Anders Carlsson <andersca@mac.com> | 2009-05-31 20:56:36 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2009-05-31 20:56:36 +0000 |
commit | 6d0ffad181215fc4ec0fca37c55eae82df6e0531 (patch) | |
tree | 9f415d7cb73b6ed6477b3c013f027455650fc7c6 | |
parent | d958389d52170be1c6dee93031d34b13809b786b (diff) |
Better support for scalar POD types in 'new' expressions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72674 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/CodeGen/CGCXX.cpp | 31 | ||||
-rw-r--r-- | test/CodeGenCXX/new.cpp | 4 |
2 files changed, 24 insertions, 11 deletions
diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp index cf09bf62a0..4b8e733f4d 100644 --- a/lib/CodeGen/CGCXX.cpp +++ b/lib/CodeGen/CGCXX.cpp @@ -285,20 +285,29 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { CGM.GetAddrOfFunction(GlobalDecl(NewFD)), NewArgs, NewFD); - llvm::Value *V = Builder.CreateBitCast(RV.getScalarVal(), - ConvertType(E->getType())); + llvm::Value *NewPtr = Builder.CreateBitCast(RV.getScalarVal(), + ConvertType(E->getType())); - if (E->hasInitializer()) { - ErrorUnsupported(E, "new expression with initializer"); - return llvm::UndefValue::get(ConvertType(E->getType())); + if (AllocType->isPODType()) { + if (E->getNumConstructorArgs() != 0) { + assert(E->getNumConstructorArgs() == 1 && + "Can only have one argument to initializer of POD type."); + + const Expr *Init = E->getConstructorArg(0); + + if (!hasAggregateLLVMType(AllocType)) { + Builder.CreateStore(EmitScalarExpr(Init), NewPtr); + } else { + ErrorUnsupported(E, "new expression"); + return llvm::UndefValue::get(ConvertType(E->getType())); + } + } + + return NewPtr; } - if (!AllocType->isPODType()) { - ErrorUnsupported(E, "new expression with non-POD type"); - return llvm::UndefValue::get(ConvertType(E->getType())); - } - - return V; + ErrorUnsupported(E, "new expression with non-POD type"); + return llvm::UndefValue::get(ConvertType(E->getType())); } static bool canGenerateCXXstructor(const CXXRecordDecl *RD, diff --git a/test/CodeGenCXX/new.cpp b/test/CodeGenCXX/new.cpp index d07466db67..a1786e8181 100644 --- a/test/CodeGenCXX/new.cpp +++ b/test/CodeGenCXX/new.cpp @@ -10,3 +10,7 @@ void* operator new(unsigned long, void*) throw(); void t2(int* a) { int* b = new (a) int; } + +void t3() { + int *a = new int(10); +} |