From 7bb6791af92aceecccce4452de272cbb956beb22 Mon Sep 17 00:00:00 2001 From: Alastair Robertson Date: Wed, 26 Jul 2023 06:35:21 -0700 Subject: [PATCH] AddPadding: Insert padding at the beginning of structs if necessary This is sometimes needed now that we're removing members before adding padding. --- oi/type_graph/AddPadding.cpp | 4 +++- test/test_add_padding.cpp | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/oi/type_graph/AddPadding.cpp b/oi/type_graph/AddPadding.cpp index 2c4a7c8..1feb09a 100644 --- a/oi/type_graph/AddPadding.cpp +++ b/oi/type_graph/AddPadding.cpp @@ -71,7 +71,9 @@ void AddPadding::visit(Class& c) { std::vector paddedMembers; paddedMembers.reserve(c.members.size()); 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); } diff --git a/test/test_add_padding.cpp b/test/test_add_padding.cpp index cd96265..757b1ce 100644 --- a/test/test_add_padding.cpp +++ b/test/test_add_padding.cpp @@ -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) { auto myclass = Class{0, Class::Kind::Struct, "MyStruct", 16}; auto myint8 = Primitive{Primitive::Kind::Int8};