AddPadding: Insert padding at the beginning of structs if necessary

This is sometimes needed now that we're removing members before adding
padding.
This commit is contained in:
Alastair Robertson 2023-07-26 06:35:21 -07:00 committed by Alastair Robertson
parent d7cfcca30d
commit 7bb6791af9
2 changed files with 19 additions and 1 deletions

View File

@ -71,7 +71,9 @@ void AddPadding::visit(Class& c) {
std::vector<Member> paddedMembers; std::vector<Member> paddedMembers;
paddedMembers.reserve(c.members.size()); paddedMembers.reserve(c.members.size());
for (size_t i = 0; i < c.members.size(); i++) { for (size_t i = 0; i < c.members.size(); i++) {
if (i >= 1) { if (i == 0) {
addPadding(0, c.members[0].bitOffset, paddedMembers);
} else {
addPadding(c.members[i - 1], c.members[i].bitOffset, paddedMembers); addPadding(c.members[i - 1], c.members[i].bitOffset, paddedMembers);
} }

View File

@ -25,6 +25,22 @@ TEST(AddPaddingTest, BetweenMembers) {
)"); )");
} }
TEST(AddPaddingTest, AtBeginning) {
test(AddPadding::createPass(), R"(
[0] Struct: MyStruct (size: 16)
Member: n1 (offset: 8)
Primitive: int64_t
)",
R"(
[0] Struct: MyStruct (size: 16)
Member: __oi_padding (offset: 0)
[1] Array: (length: 8)
Primitive: int8_t
Member: n1 (offset: 8)
Primitive: int64_t
)");
}
TEST(AddPaddingTest, AtEnd) { TEST(AddPaddingTest, AtEnd) {
auto myclass = Class{0, Class::Kind::Struct, "MyStruct", 16}; auto myclass = Class{0, Class::Kind::Struct, "MyStruct", 16};
auto myint8 = Primitive{Primitive::Kind::Int8}; auto myint8 = Primitive{Primitive::Kind::Int8};