Skip to content

Commit cda95f4

Browse files
committed
When the type-id or new-type-id of a C++ "new" expression is a typedef
of an array type, use the outermost array bound as the number of elements to allocate. Fixes PR7147. llvm-svn: 103908
1 parent 5ce10a6 commit cda95f4

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -680,10 +680,19 @@ Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal,
680680
if (CheckAllocatedType(AllocType, TypeLoc, TypeRange))
681681
return ExprError();
682682

683-
QualType ResultType = Context.getPointerType(AllocType);
683+
// Per C++0x [expr.new]p5, the type being constructed may be a
684+
// typedef of an array type.
685+
if (!ArraySizeE.get()) {
686+
if (const ConstantArrayType *Array
687+
= Context.getAsConstantArrayType(AllocType)) {
688+
ArraySizeE = Owned(new (Context) IntegerLiteral(Array->getSize(),
689+
Context.getSizeType(),
690+
TypeRange.getEnd()));
691+
AllocType = Array->getElementType();
692+
}
693+
}
684694

685-
// That every array dimension except the first is constant was already
686-
// checked by the type check above.
695+
QualType ResultType = Context.getPointerType(AllocType);
687696

688697
// C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral
689698
// or enumeration type with a non-negative value."

clang/test/SemaCXX/new-delete.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ void* operator new(size_t, int*); // expected-note 3 {{candidate}}
2424
void* operator new(size_t, float*); // expected-note 3 {{candidate}}
2525
void* operator new(size_t, S); // expected-note 2 {{candidate}}
2626

27+
struct foo { };
28+
2729
void good_news()
2830
{
2931
int *pi = new int;
@@ -43,6 +45,14 @@ void good_news()
4345
pi = new (S(1.0f, 2)) int;
4446

4547
(void)new int[true];
48+
49+
// PR7147
50+
typedef int a[2];
51+
foo* f1 = new foo;
52+
foo* f2 = new foo[2];
53+
typedef foo x[2];
54+
typedef foo y[2][2];
55+
x* f3 = new y;
4656
}
4757

4858
struct abstract {

0 commit comments

Comments
 (0)