AddPadding: Pad unions when necessary

This commit is contained in:
Alastair Robertson 2023-07-18 06:15:59 -07:00 committed by Alastair Robertson
parent a0acaaea65
commit ec9f98f0e1
2 changed files with 14 additions and 1 deletions

View File

@ -61,7 +61,14 @@ void AddPadding::visit(Class& c) {
}
if (c.kind() == Class::Kind::Union) {
// Don't pad unions
// Only apply padding to the full size of the union, not between members
for (const auto& member : c.members) {
if (member.bitsize == c.size() * 8 || member.type().size() == c.size())
return; // Don't add padding to unions which don't need it
}
// This union's members aren't big enough to make up its size, so add a
// single padding member
addPadding(0, c.size() * 8, c.members);
return;
}

View File

@ -85,6 +85,9 @@ TEST(AddPaddingTest, UnionAtEnd) {
Primitive: int64_t
Member: n2 (offset: 0)
Primitive: int8_t
Member: __oi_padding (offset: 0)
[1] Array: (length: 16)
Primitive: int8_t
)");
}
@ -188,6 +191,9 @@ TEST(AddPaddingTest, MemberlessUnion) {
)",
R"(
[0] Union: MyUnion (size: 16)
Member: __oi_padding (offset: 0)
[1] Array: (length: 16)
Primitive: int8_t
)");
}