NameGen: Override inputName for anonymous members

This commit is contained in:
Alastair Robertson 2023-12-14 06:44:13 -08:00 committed by Alastair Robertson
parent c874f72ae2
commit aa87c3f2d1
3 changed files with 34 additions and 3 deletions

View File

@ -77,8 +77,14 @@ void NameGen::visit(Class& c) {
// Deduplicate member names. Duplicates may be present after flattening.
for (size_t i = 0; i < c.members.size(); i++) {
if (c.members[i].name.empty())
c.members[i].name = AnonPrefix;
c.members[i].name += "_" + std::to_string(i);
if (c.members[i].inputName.empty())
c.members[i].inputName = c.members[i].name;
// GCC includes dots in vptr member names, e.g. "_vptr.MyClass"
// These aren't valid in C++, so we must replace them
std::replace(c.members[i].name.begin(), c.members[i].name.end(), '.', '$');

View File

@ -192,7 +192,6 @@ definitions = '''
}]'''
[cases.anon_union]
oil_skip = "anonymous unions are fully stubbed in the generated code" # https://github.com/facebookexperimental/object-introspection/issues/292
param_types = ["const AnonUnionContainer&"]
setup = 'return AnonUnionContainer{ .a = 3 };'
cli_options = ["-fchase-raw-pointers"]
@ -209,8 +208,8 @@ definitions = '''
"staticSize": 24,
"exclusiveSize": 10,
"members": [
{"name":"__anon_member_0", "staticSize":2, "exclusiveSize":2},
{"name":"__anon_member_1", "staticSize":8, "exclusiveSize":8},
{"name":"__oi_anon_0", "staticSize":2, "exclusiveSize":2},
{"name":"__oi_anon_2", "staticSize":8, "exclusiveSize":8},
{"name":"e", "staticSize":4, "exclusiveSize":4, "typeNames":["int32_t"]}
]
}]'''

View File

@ -475,3 +475,29 @@ TEST(NameGenTest, AnonymousTypes) {
EXPECT_EQ(myenum.inputName(), "__oi_anon_1");
EXPECT_EQ(mytypedef.inputName(), "");
}
TEST(NameGenTest, AnonymousMembers) {
auto myint = Primitive{Primitive::Kind::Int32};
auto myclass = Class{0, Class::Kind::Class, "C", 69};
auto myunion1 = Class{1, Class::Kind::Union, "", 4};
auto myunion2 = Class{2, Class::Kind::Union, "", 4};
myclass.members.push_back(Member{myunion1, "", 0});
myclass.members.push_back(Member{myunion2, "", 4});
NameGen nameGen;
nameGen.generateNames({myclass, myunion1, myunion2});
EXPECT_EQ(myclass.name(), "C_0");
EXPECT_EQ(myunion1.name(), "__oi_anon_1");
EXPECT_EQ(myunion2.name(), "__oi_anon_2");
EXPECT_EQ(myclass.members[0].name, "__oi_anon_0");
EXPECT_EQ(myclass.members[1].name, "__oi_anon_1");
EXPECT_EQ(myclass.inputName(), "C");
EXPECT_EQ(myunion1.inputName(), "__oi_anon_1");
EXPECT_EQ(myunion2.inputName(), "__oi_anon_2");
EXPECT_EQ(myclass.members[0].inputName, "__oi_anon_0");
EXPECT_EQ(myclass.members[1].inputName, "__oi_anon_1");
}