Skip to content

Commit 00a2038

Browse files
committed
[clang][initlist] handle incomplete array type in Constant Expr Calculation
In #65918, support of incomplete array type is added in TryReferenceListInitialization. It causes the crash in Constant Expr Calculation since it only considers the case where it is ConstantArrayType. This patch wants to add support for incomplete array type also.
1 parent 428ffbd commit 00a2038

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4030,9 +4030,13 @@ findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,
40304030
if (ObjType->isArrayType()) {
40314031
// Next subobject is an array element.
40324032
const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType);
4033-
assert(CAT && "vla in literal type?");
4033+
const IncompleteArrayType *IAT =
4034+
Info.Ctx.getAsIncompleteArrayType(ObjType);
4035+
const ArrayType *AT = CAT ? static_cast<const ArrayType *>(CAT)
4036+
: static_cast<const ArrayType *>(IAT);
4037+
assert(AT && "vla in literal type?");
40344038
uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4035-
if (CAT->getSize().ule(Index)) {
4039+
if (CAT && CAT->getSize().ule(Index)) {
40364040
// Note, it should not be possible to form a pointer with a valid
40374041
// designator which points more than one past the end of the array.
40384042
if (Info.getLangOpts().CPlusPlus11)
@@ -4043,12 +4047,12 @@ findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,
40434047
return handler.failed();
40444048
}
40454049

4046-
ObjType = CAT->getElementType();
4050+
ObjType = AT->getElementType();
40474051

40484052
if (O->getArrayInitializedElts() > Index)
40494053
O = &O->getArrayInitializedElt(Index);
40504054
else if (!isRead(handler.AccessKind)) {
4051-
if (!CheckArraySize(Info, CAT, E->getExprLoc()))
4055+
if (CAT && !CheckArraySize(Info, CAT, E->getExprLoc()))
40524056
return handler.failed();
40534057

40544058
expandArray(*O, Index);

clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,19 @@ void foo(int a) {
3232
f({a});
3333
}
3434

35+
constexpr int gh151716() {
36+
int(&&g)[]{0,1,2};
37+
return g[2];
38+
}
39+
// CHECK-LABEL: @_ZN3One10gh151716_fEv
40+
// CHECK-NEXT: entry:
41+
// CHECK-NEXT: %v = alloca i32, align 4
42+
// CHECK-NEXT: call void @llvm.lifetime.start.p0(ptr nonnull %v)
43+
// CHECK-NEXT: store volatile i32 2, ptr %v, align 4
44+
// CHECK-NEXT: call void @llvm.lifetime.end.p0(ptr nonnull %v)
45+
// CHECK-NEXT: ret void
46+
void gh151716_f() {
47+
volatile const int v = gh151716();
48+
}
49+
3550
} // namespace One

0 commit comments

Comments
 (0)