2023-05-26 13:52:09 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "oi/type_graph/AddPadding.h"
|
|
|
|
#include "oi/type_graph/Types.h"
|
2023-05-31 14:26:46 +01:00
|
|
|
#include "test/type_graph_utils.h"
|
2023-05-26 13:52:09 +01:00
|
|
|
|
|
|
|
using namespace type_graph;
|
2023-07-03 13:18:25 +01:00
|
|
|
using ObjectIntrospection::Feature;
|
|
|
|
using ObjectIntrospection::FeatureSet;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void test(std::vector<std::reference_wrapper<type_graph::Type>> rootTypes,
|
|
|
|
std::string_view expectedAfter) {
|
|
|
|
FeatureSet features;
|
|
|
|
features[Feature::TreeBuilderV2] = true;
|
|
|
|
::test(AddPadding::createPass({}), rootTypes, expectedAfter);
|
|
|
|
}
|
|
|
|
} // namespace
|
2023-05-26 13:52:09 +01:00
|
|
|
|
|
|
|
TEST(AddPaddingTest, BetweenMembers) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myclass = Class{0, Class::Kind::Class, "MyClass", 16};
|
2023-07-04 16:55:10 +01:00
|
|
|
auto myint8 = Primitive{Primitive::Kind::Int8};
|
|
|
|
auto myint64 = Primitive{Primitive::Kind::Int64};
|
2023-07-05 10:33:44 +01:00
|
|
|
myclass.members.push_back(Member{myint8, "n1", 0});
|
|
|
|
myclass.members.push_back(Member{myint64, "n2", 8 * 8});
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-03 13:18:25 +01:00
|
|
|
test({myclass}, R"(
|
2023-05-26 13:52:09 +01:00
|
|
|
[0] Class: MyClass (size: 16)
|
|
|
|
Member: n1 (offset: 0)
|
|
|
|
Primitive: int8_t
|
2023-06-29 09:15:03 +01:00
|
|
|
Member: __oi_padding (offset: 1)
|
2023-05-26 13:52:09 +01:00
|
|
|
[1] Array: (length: 7)
|
|
|
|
Primitive: int8_t
|
|
|
|
Member: n2 (offset: 8)
|
|
|
|
Primitive: int64_t
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AddPaddingTest, AtEnd) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myclass = Class{0, Class::Kind::Struct, "MyStruct", 16};
|
2023-07-04 16:55:10 +01:00
|
|
|
auto myint8 = Primitive{Primitive::Kind::Int8};
|
|
|
|
auto myint64 = Primitive{Primitive::Kind::Int64};
|
2023-07-05 10:33:44 +01:00
|
|
|
myclass.members.push_back(Member{myint64, "n1", 0});
|
|
|
|
myclass.members.push_back(Member{myint8, "n2", 8 * 8});
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-03 13:18:25 +01:00
|
|
|
test({myclass}, R"(
|
2023-05-26 13:52:09 +01:00
|
|
|
[0] Struct: MyStruct (size: 16)
|
|
|
|
Member: n1 (offset: 0)
|
|
|
|
Primitive: int64_t
|
|
|
|
Member: n2 (offset: 8)
|
|
|
|
Primitive: int8_t
|
2023-06-29 09:15:03 +01:00
|
|
|
Member: __oi_padding (offset: 9)
|
2023-05-26 13:52:09 +01:00
|
|
|
[1] Array: (length: 7)
|
|
|
|
Primitive: int8_t
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2023-07-18 13:54:52 +01:00
|
|
|
TEST(AddPaddingTest, UnionBetweenMembers) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myclass = Class{0, Class::Kind::Union, "MyUnion", 8};
|
2023-07-04 16:55:10 +01:00
|
|
|
auto myint8 = Primitive{Primitive::Kind::Int8};
|
|
|
|
auto myint64 = Primitive{Primitive::Kind::Int64};
|
2023-07-05 10:33:44 +01:00
|
|
|
myclass.members.push_back(Member{myint64, "n1", 0});
|
|
|
|
myclass.members.push_back(Member{myint8, "n2", 0});
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-03 13:18:25 +01:00
|
|
|
test({myclass}, R"(
|
2023-05-26 13:52:09 +01:00
|
|
|
[0] Union: MyUnion (size: 8)
|
|
|
|
Member: n1 (offset: 0)
|
|
|
|
Primitive: int64_t
|
|
|
|
Member: n2 (offset: 0)
|
|
|
|
Primitive: int8_t
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2023-07-18 13:54:52 +01:00
|
|
|
TEST(AddPaddingTest, UnionAtEnd) {
|
|
|
|
test(AddPadding::createPass({}), R"(
|
|
|
|
[0] Union: MyUnion (size: 16)
|
|
|
|
Member: n1 (offset: 0)
|
|
|
|
Primitive: int64_t
|
|
|
|
Member: n2 (offset: 0)
|
|
|
|
Primitive: int8_t
|
|
|
|
)",
|
|
|
|
R"(
|
|
|
|
[0] Union: MyUnion (size: 16)
|
|
|
|
Member: n1 (offset: 0)
|
|
|
|
Primitive: int64_t
|
|
|
|
Member: n2 (offset: 0)
|
|
|
|
Primitive: int8_t
|
2023-07-18 14:15:59 +01:00
|
|
|
Member: __oi_padding (offset: 0)
|
|
|
|
[1] Array: (length: 16)
|
|
|
|
Primitive: int8_t
|
2023-07-18 13:54:52 +01:00
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2023-06-29 09:15:03 +01:00
|
|
|
TEST(AddPaddingTest, Bitfields) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myclass = Class{0, Class::Kind::Class, "MyClass", 16};
|
2023-06-29 09:15:03 +01:00
|
|
|
auto myint64 = Primitive{Primitive::Kind::Int64};
|
|
|
|
auto myint16 = Primitive{Primitive::Kind::Int16};
|
|
|
|
auto myint8 = Primitive{Primitive::Kind::Int8};
|
|
|
|
|
2023-07-05 10:33:44 +01:00
|
|
|
Member b1{myint64, "b1", 0};
|
2023-06-29 09:15:03 +01:00
|
|
|
b1.bitsize = 3;
|
2023-07-05 10:33:44 +01:00
|
|
|
Member b2{myint8, "b2", 3};
|
2023-06-29 09:15:03 +01:00
|
|
|
b2.bitsize = 2;
|
|
|
|
// There may be a 0-sized bitfield between these two that does not appear
|
|
|
|
// in the DWARF. This would add padding and push b3 to the next byte.
|
2023-07-05 10:33:44 +01:00
|
|
|
Member b3{myint8, "b3", 8};
|
2023-06-29 09:15:03 +01:00
|
|
|
b3.bitsize = 1;
|
|
|
|
|
2023-07-05 10:33:44 +01:00
|
|
|
Member b4{myint64, "b4", 8 * 8};
|
2023-06-29 09:15:03 +01:00
|
|
|
b4.bitsize = 24;
|
|
|
|
|
2023-07-05 10:33:44 +01:00
|
|
|
Member n{myint16, "n", 12 * 8};
|
2023-06-29 09:15:03 +01:00
|
|
|
|
|
|
|
myclass.members.push_back(b1);
|
|
|
|
myclass.members.push_back(b2);
|
|
|
|
myclass.members.push_back(b3);
|
|
|
|
myclass.members.push_back(b4);
|
|
|
|
myclass.members.push_back(n);
|
|
|
|
|
2023-07-03 13:18:25 +01:00
|
|
|
test({myclass}, R"(
|
2023-06-29 09:15:03 +01:00
|
|
|
[0] Class: MyClass (size: 16)
|
|
|
|
Member: b1 (offset: 0, bitsize: 3)
|
|
|
|
Primitive: int64_t
|
|
|
|
Member: b2 (offset: 0.375, bitsize: 2)
|
|
|
|
Primitive: int8_t
|
|
|
|
Member: __oi_padding (offset: 0.625, bitsize: 3)
|
2023-07-19 16:12:45 +01:00
|
|
|
Primitive: int8_t
|
2023-06-29 09:15:03 +01:00
|
|
|
Member: b3 (offset: 1, bitsize: 1)
|
|
|
|
Primitive: int8_t
|
2023-07-19 16:12:45 +01:00
|
|
|
Member: __oi_padding (offset: 1.125, bitsize: 7)
|
|
|
|
Primitive: int8_t
|
|
|
|
Member: __oi_padding (offset: 2)
|
|
|
|
[1] Array: (length: 6)
|
|
|
|
Primitive: int8_t
|
2023-06-29 09:15:03 +01:00
|
|
|
Member: b4 (offset: 8, bitsize: 24)
|
|
|
|
Primitive: int64_t
|
|
|
|
Member: __oi_padding (offset: 11)
|
2023-07-19 16:12:45 +01:00
|
|
|
[2] Array: (length: 1)
|
2023-06-29 09:15:03 +01:00
|
|
|
Primitive: int8_t
|
|
|
|
Member: n (offset: 12)
|
|
|
|
Primitive: int16_t
|
|
|
|
Member: __oi_padding (offset: 14)
|
2023-07-19 16:12:45 +01:00
|
|
|
[3] Array: (length: 2)
|
2023-06-29 09:15:03 +01:00
|
|
|
Primitive: int8_t
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2023-07-03 13:18:25 +01:00
|
|
|
TEST(AddPaddingTest, CodeGenCompatibility) {
|
|
|
|
auto myint = Primitive{Primitive::Kind::Int32};
|
|
|
|
auto vector = getVector(1);
|
|
|
|
vector.templateParams.push_back(TemplateParam{myint});
|
|
|
|
|
|
|
|
auto myclass = Class{0, Class::Kind::Class, "MyClass", 24};
|
|
|
|
myclass.members.push_back(Member{vector, "__oi_parent", 0});
|
|
|
|
|
|
|
|
FeatureSet features;
|
|
|
|
features[Feature::TreeBuilderV2] = false;
|
|
|
|
test(AddPadding::createPass(features), {myclass}, R"(
|
|
|
|
[0] Class: MyClass (size: 24)
|
|
|
|
Member: __oi_parent (offset: 0)
|
|
|
|
[1] Container: std::vector (size: 24)
|
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
)",
|
|
|
|
R"(
|
|
|
|
[0] Class: MyClass (size: 24)
|
|
|
|
Member: __oi_padding (offset: 0)
|
|
|
|
[1] Array: (length: 24)
|
|
|
|
Primitive: int8_t
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2023-07-18 13:54:52 +01:00
|
|
|
TEST(AddPaddingTest, EmptyClass) {
|
|
|
|
testNoChange(AddPadding::createPass({}), R"(
|
|
|
|
[0] Class: MyClass (size: 0)
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AddPaddingTest, MemberlessClass) {
|
|
|
|
test(AddPadding::createPass({}), R"(
|
|
|
|
[0] Class: MyClass (size: 12)
|
|
|
|
)",
|
|
|
|
R"(
|
|
|
|
[0] Class: MyClass (size: 12)
|
2023-07-18 13:38:47 +01:00
|
|
|
Member: __oi_padding (offset: 0)
|
|
|
|
[1] Array: (length: 12)
|
|
|
|
Primitive: int8_t
|
2023-07-18 13:54:52 +01:00
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AddPaddingTest, MemberlessUnion) {
|
|
|
|
test(AddPadding::createPass({}), R"(
|
|
|
|
[0] Union: MyUnion (size: 16)
|
|
|
|
)",
|
|
|
|
R"(
|
|
|
|
[0] Union: MyUnion (size: 16)
|
2023-07-18 14:15:59 +01:00
|
|
|
Member: __oi_padding (offset: 0)
|
|
|
|
[1] Array: (length: 16)
|
|
|
|
Primitive: int8_t
|
2023-07-18 13:54:52 +01:00
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2023-05-26 13:52:09 +01:00
|
|
|
// TODO test we follow class members, templates, children
|