Skip to content

Commit 991ca33

Browse files
committed
[Clang][Sema] placement new initializes typedef array with correct size
1 parent ca0560d commit 991ca33

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

clang/lib/Sema/TreeTransform.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -12669,6 +12669,19 @@ TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E) {
1266912669
ArraySize = NewArraySize.get();
1267012670
}
1267112671

12672+
// Per C++0x [expr.new]p5, the type being constructed may be a
12673+
// typedef of an array type.
12674+
QualType AllocType = AllocTypeInfo->getType();
12675+
if (ArraySize) {
12676+
if (const ConstantArrayType *Array =
12677+
SemaRef.Context.getAsConstantArrayType(AllocType)) {
12678+
ArraySize = IntegerLiteral::Create(SemaRef.Context, Array->getSize(),
12679+
SemaRef.Context.getSizeType(),
12680+
E->getBeginLoc());
12681+
AllocType = Array->getElementType();
12682+
}
12683+
}
12684+
1267212685
// Transform the placement arguments (if any).
1267312686
bool ArgumentChanged = false;
1267412687
SmallVector<Expr*, 8> PlacementArgs;
@@ -12730,7 +12743,6 @@ TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E) {
1273012743
return E;
1273112744
}
1273212745

12733-
QualType AllocType = AllocTypeInfo->getType();
1273412746
if (!ArraySize) {
1273512747
// If no array size was specified, but the new expression was
1273612748
// instantiated with an array type (e.g., "new T" where T is
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang -S -fno-discard-value-names -emit-llvm -o - %s | FileCheck %s
2+
#include <new>
3+
4+
// CHECK: call void @llvm.memset.p0.i64(ptr align 1 %x, i8 0, i64 8, i1 false)
5+
// CHECK: call void @llvm.memset.p0.i64(ptr align 16 %x, i8 0, i64 32, i1 false)
6+
template <typename TYPE>
7+
void f()
8+
{
9+
typedef TYPE TArray[8];
10+
11+
TArray x;
12+
new(&x) TArray();
13+
}
14+
15+
int main()
16+
{
17+
f<char>();
18+
f<int>();
19+
}

0 commit comments

Comments
 (0)