Skip to content

Commit bb0300c

Browse files
committed
[clang][Interp] Fix initializing array subobjects with This pointers
We need to select the right array element once we see the CXXThisExpr.
1 parent 8fd9624 commit bb0300c

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

clang/lib/AST/Interp/Compiler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ bool InitLink::emit(Compiler<Emitter> *Ctx, const Expr *E) const {
8989
return Ctx->emitGetPtrLocal(Offset, E);
9090
case K_Decl:
9191
return Ctx->visitDeclRef(D, E);
92+
case K_Elem:
93+
if (!Ctx->emitConstUint32(Offset, E))
94+
return false;
95+
return Ctx->emitArrayElemPtrUint32(E);
9296
default:
9397
llvm_unreachable("Unhandled InitLink kind");
9498
}
@@ -1556,6 +1560,7 @@ bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex,
15561560
return this->emitInitElem(*T, ElemIndex, Init);
15571561
}
15581562

1563+
InitLinkScope<Emitter> ILS(this, InitLink::Elem(ElemIndex));
15591564
// Advance the pointer currently on the stack to the given
15601565
// dimension.
15611566
if (!this->emitConstUint32(ElemIndex, Init))

clang/lib/AST/Interp/Compiler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct InitLink {
5050
K_Field = 1,
5151
K_Temp = 2,
5252
K_Decl = 3,
53+
K_Elem = 5,
5354
};
5455

5556
static InitLink This() { return InitLink{K_This}; }
@@ -68,6 +69,11 @@ struct InitLink {
6869
IL.D = D;
6970
return IL;
7071
}
72+
static InitLink Elem(unsigned Index) {
73+
InitLink IL{K_Elem};
74+
IL.Offset = Index;
75+
return IL;
76+
}
7177

7278
InitLink(uint8_t Kind) : Kind(Kind) {}
7379
template <class Emitter>

clang/test/AST/Interp/records.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,3 +1537,22 @@ namespace BitSet {
15371537
Bitset()
15381538
};
15391539
}
1540+
1541+
namespace ArrayInitChain {
1542+
struct StringLiteral {
1543+
const char *S;
1544+
};
1545+
1546+
struct CustomOperandVal {
1547+
StringLiteral Str;
1548+
unsigned Width;
1549+
unsigned Mask = Width + 1;
1550+
};
1551+
1552+
constexpr CustomOperandVal A[] = {
1553+
{},
1554+
};
1555+
static_assert(A[0].Str.S == nullptr, "");
1556+
static_assert(A[0].Width == 0, "");
1557+
static_assert(A[0].Mask == 1, "");
1558+
}

0 commit comments

Comments
 (0)