Skip to content

Commit 517134e

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

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ Bug Fixes to C++ Support
285285
templates when determining the primary template of an explicit specialization.
286286
- Fixed a crash in Microsoft compatibility mode where unqualified dependent base class
287287
lookup searches the bases of an incomplete class.
288+
- placement new initializes typedef array with correct size
289+
(`#41441 <https://github.com/llvm/llvm-project/issues/41441>`_)
288290

289291
Bug Fixes to AST Handling
290292
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/TreeTransform.h

Lines changed: 13 additions & 1 deletion
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
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang -S -fno-discard-value-names -emit-llvm -o - %s | FileCheck %s
2+
// Issue no: 41441
3+
#include <new>
4+
5+
// CHECK: call void @llvm.memset.p0.i64(ptr align 1 %x, i8 0, i64 8, i1 false)
6+
// CHECK: call void @llvm.memset.p0.i64(ptr align 16 %x, i8 0, i64 32, i1 false)
7+
template <typename TYPE>
8+
void f()
9+
{
10+
typedef TYPE TArray[8];
11+
12+
TArray x;
13+
new(&x) TArray();
14+
}
15+
16+
int main()
17+
{
18+
f<char>();
19+
f<int>();
20+
}

0 commit comments

Comments
 (0)