mirror of
https://github.com/JakeHillion/object-introspection.git
synced 2024-11-09 21:24:14 +00:00
AddPadding: Pad classes with zero members
This commit is contained in:
parent
3aa52deb71
commit
a0acaaea65
@ -89,6 +89,9 @@ void AddPadding::visit(Class& c) {
|
|||||||
|
|
||||||
if (!c.members.empty()) {
|
if (!c.members.empty()) {
|
||||||
addPadding(c.members.back(), c.size() * 8, paddedMembers);
|
addPadding(c.members.back(), c.size() * 8, paddedMembers);
|
||||||
|
} else {
|
||||||
|
// Pad out empty classes
|
||||||
|
addPadding(0, c.size() * 8, paddedMembers);
|
||||||
}
|
}
|
||||||
|
|
||||||
c.members = std::move(paddedMembers);
|
c.members = std::move(paddedMembers);
|
||||||
@ -109,7 +112,14 @@ void AddPadding::addPadding(const Member& prevMember,
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint64_t prevMemberEndBits = prevMember.bitOffset + prevMemberSizeBits;
|
uint64_t prevMemberEndBits = prevMember.bitOffset + prevMemberSizeBits;
|
||||||
uint64_t paddingBits = paddingEndBits - prevMemberEndBits;
|
|
||||||
|
addPadding(prevMemberEndBits, paddingEndBits, paddedMembers);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AddPadding::addPadding(uint64_t paddingStartBits,
|
||||||
|
uint64_t paddingEndBits,
|
||||||
|
std::vector<Member>& paddedMembers) {
|
||||||
|
uint64_t paddingBits = paddingEndBits - paddingStartBits;
|
||||||
if (paddingBits == 0)
|
if (paddingBits == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -117,11 +127,11 @@ void AddPadding::addPadding(const Member& prevMember,
|
|||||||
// Pad with an array of bytes
|
// Pad with an array of bytes
|
||||||
auto& primitive = typeGraph_.makeType<Primitive>(Primitive::Kind::Int8);
|
auto& primitive = typeGraph_.makeType<Primitive>(Primitive::Kind::Int8);
|
||||||
auto& paddingArray = typeGraph_.makeType<Array>(primitive, paddingBits / 8);
|
auto& paddingArray = typeGraph_.makeType<Array>(primitive, paddingBits / 8);
|
||||||
paddedMembers.emplace_back(paddingArray, MemberPrefix, prevMemberEndBits);
|
paddedMembers.emplace_back(paddingArray, MemberPrefix, paddingStartBits);
|
||||||
} else {
|
} else {
|
||||||
// Pad with a bitfield
|
// Pad with a bitfield
|
||||||
auto& primitive = typeGraph_.makeType<Primitive>(Primitive::Kind::Int64);
|
auto& primitive = typeGraph_.makeType<Primitive>(Primitive::Kind::Int64);
|
||||||
paddedMembers.emplace_back(primitive, MemberPrefix, prevMemberEndBits,
|
paddedMembers.emplace_back(primitive, MemberPrefix, paddingStartBits,
|
||||||
paddingBits);
|
paddingBits);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,6 +58,9 @@ class AddPadding final : public RecursiveVisitor {
|
|||||||
void addPadding(const Member& prevMember,
|
void addPadding(const Member& prevMember,
|
||||||
uint64_t paddingEndBits,
|
uint64_t paddingEndBits,
|
||||||
std::vector<Member>& paddedMembers);
|
std::vector<Member>& paddedMembers);
|
||||||
|
void addPadding(uint64_t paddingStartBits,
|
||||||
|
uint64_t paddingEndBits,
|
||||||
|
std::vector<Member>& paddedMembers);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace type_graph
|
} // namespace type_graph
|
||||||
|
@ -176,6 +176,9 @@ TEST(AddPaddingTest, MemberlessClass) {
|
|||||||
)",
|
)",
|
||||||
R"(
|
R"(
|
||||||
[0] Class: MyClass (size: 12)
|
[0] Class: MyClass (size: 12)
|
||||||
|
Member: __oi_padding (offset: 0)
|
||||||
|
[1] Array: (length: 12)
|
||||||
|
Primitive: int8_t
|
||||||
)");
|
)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user