Merge pull request #4973 from SuperAuguste/nameless-fields

Nameless fields
This commit is contained in:
Vexu 2020-04-10 21:31:32 +03:00 committed by GitHub
commit 1b1cbd9358
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 2 deletions

View File

@ -811,7 +811,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
var is_anon = false;
var raw_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, field_decl)));
if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl)) {
if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl) or raw_name.len == 0) {
raw_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
is_anon = true;
}

View File

@ -2879,7 +2879,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
, &[_][]const u8{
\\pub const FOO = 0x61626364;
});
cases.add("Make sure casts are grouped",
\\typedef struct
\\{
@ -2910,4 +2910,49 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
,
\\pub const GPIO_2_MEM_MAP = @intToPtr([*c]c_uint, 0x8008);
});
if (std.Target.current.abi == .msvc) {
cases.add("nameless struct fields",
\\typedef struct NAMED
\\{
\\ long name;
\\} NAMED;
\\
\\typedef struct ONENAMEWITHSTRUCT
\\{
\\ NAMED;
\\ long b;
\\} ONENAMEWITHSTRUCT;
, &[_][]const u8{
\\pub const struct_NAMED = extern struct {
\\ name: c_long,
\\};
\\pub const NAMED = struct_NAMED;
\\pub const struct_ONENAMEWITHSTRUCT = extern struct {
\\ unnamed_1: struct_NAMED,
\\ b: c_long,
\\};
});
} else {
cases.add("nameless struct fields",
\\typedef struct NAMED
\\{
\\ long name;
\\} NAMED;
\\
\\typedef struct ONENAMEWITHSTRUCT
\\{
\\ NAMED;
\\ long b;
\\} ONENAMEWITHSTRUCT;
, &[_][]const u8{
\\pub const struct_NAMED = extern struct {
\\ name: c_long,
\\};
\\pub const NAMED = struct_NAMED;
\\pub const struct_ONENAMEWITHSTRUCT = extern struct {
\\ b: c_long,
\\};
});
}
}