authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-03 01:55:01+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-03 03:42:42+03:00
logc7884af063791211544c6595a4900bbfcd5d96b6
treea8e234e9ad48519bd39f3957cdbeed7df0ee84c8
parent0f61d1f0df887081d60558256e10944633eb868f

translate-c: do not translate packed C structs as packed Zig structs in stage2

Zig's integer backed packed structs are not compatible with C's packed structs.

3 files changed, 34 insertions(+), 26 deletions(-)

src/translate_c.zig+4
...@@ -1166,6 +1166,10 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_decl: *const clang.RecordD...@@ -1166,6 +1166,10 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_decl: *const clang.RecordD
1166 });1166 });
1167 }1167 }
11681168
1169 if (!c.zig_is_stage1 and is_packed) {
1170 return failDecl(c, record_loc, bare_name, "cannot translate packed record union", .{});
1171 }
1172
1169 const record_payload = try c.arena.create(ast.Payload.Record);1173 const record_payload = try c.arena.create(ast.Payload.Record);
1170 record_payload.* = .{1174 record_payload.* = .{
1171 .base = .{ .tag = ([2]Tag{ .@"struct", .@"union" })[@boolToInt(is_union)] },1175 .base = .{ .tag = ([2]Tag{ .@"struct", .@"union" })[@boolToInt(is_union)] },
test/run_translated_c.zig+14-12
...@@ -250,18 +250,20 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {...@@ -250,18 +250,20 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
250 \\}250 \\}
251 , "");251 , "");
252252
253 cases.add("struct initializer - packed",253 if (@import("builtin").zig_backend == .stage1) {
254 \\#define _NO_CRT_STDIO_INLINE 1254 cases.add("struct initializer - packed",
255 \\#include <stdint.h>255 \\#define _NO_CRT_STDIO_INLINE 1
256 \\#include <stdlib.h>256 \\#include <stdint.h>
257 \\struct s {uint8_t x,y;257 \\#include <stdlib.h>
258 \\ uint32_t z;} __attribute__((packed)) s0 = {1, 2};258 \\struct s {uint8_t x,y;
259 \\int main() {259 \\ uint32_t z;} __attribute__((packed)) s0 = {1, 2};
260 \\ /* sizeof nor offsetof currently supported */260 \\int main() {
261 \\ if (((intptr_t)&s0.z - (intptr_t)&s0.x) != 2) abort();261 \\ /* sizeof nor offsetof currently supported */
262 \\ return 0;262 \\ if (((intptr_t)&s0.z - (intptr_t)&s0.x) != 2) abort();
263 \\}263 \\ return 0;
264 , "");264 \\}
265 , "");
266 }
265267
266 cases.add("cast signed array index to unsigned",268 cases.add("cast signed array index to unsigned",
267 \\#include <stdlib.h>269 \\#include <stdlib.h>
test/translate_c.zig+16-14
...@@ -728,20 +728,22 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -728,20 +728,22 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
728 \\}728 \\}
729 });729 });
730730
731 cases.add("struct initializer - packed",731 if (builtin.zig_backend == .stage1) {
732 \\struct {int x,y,z;} __attribute__((packed)) s0 = {1, 2};732 cases.add("struct initializer - packed",
733 , &[_][]const u8{733 \\struct {int x,y,z;} __attribute__((packed)) s0 = {1, 2};
734 \\const struct_unnamed_1 = packed struct {734 , &[_][]const u8{
735 \\ x: c_int,735 \\const struct_unnamed_1 = packed struct {
736 \\ y: c_int,736 \\ x: c_int,
737 \\ z: c_int,737 \\ y: c_int,
738 \\};738 \\ z: c_int,
739 \\pub export var s0: struct_unnamed_1 = struct_unnamed_1{739 \\};
740 \\ .x = @as(c_int, 1),740 \\pub export var s0: struct_unnamed_1 = struct_unnamed_1{
741 \\ .y = @as(c_int, 2),741 \\ .x = @as(c_int, 1),
742 \\ .z = 0,742 \\ .y = @as(c_int, 2),
743 \\};743 \\ .z = 0,
744 });744 \\};
745 });
746 }
745747
746 // Test case temporarily disabled:748 // Test case temporarily disabled:
747 // https://github.com/ziglang/zig/issues/12055749 // https://github.com/ziglang/zig/issues/12055