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
11661166 });
11671167 }
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
11691173 const record_payload = try c.arena.create(ast.Payload.Record);
11701174 record_payload.* = .{
11711175 .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 {
250250 \\}
251251 , "");
252252
253 cases.add("struct initializer - packed",
254 \\#define _NO_CRT_STDIO_INLINE 1
255 \\#include <stdint.h>
256 \\#include <stdlib.h>
257 \\struct s {uint8_t x,y;
258 \\ uint32_t z;} __attribute__((packed)) s0 = {1, 2};
259 \\int main() {
260 \\ /* sizeof nor offsetof currently supported */
261 \\ if (((intptr_t)&s0.z - (intptr_t)&s0.x) != 2) abort();
262 \\ return 0;
263 \\}
264 , "");
253 if (@import("builtin").zig_backend == .stage1) {
254 cases.add("struct initializer - packed",
255 \\#define _NO_CRT_STDIO_INLINE 1
256 \\#include <stdint.h>
257 \\#include <stdlib.h>
258 \\struct s {uint8_t x,y;
259 \\ uint32_t z;} __attribute__((packed)) s0 = {1, 2};
260 \\int main() {
261 \\ /* sizeof nor offsetof currently supported */
262 \\ if (((intptr_t)&s0.z - (intptr_t)&s0.x) != 2) abort();
263 \\ return 0;
264 \\}
265 , "");
266 }
265267
266268 cases.add("cast signed array index to unsigned",
267269 \\#include <stdlib.h>
test/translate_c.zig+16-14
......@@ -728,20 +728,22 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
728728 \\}
729729 });
730730
731 cases.add("struct initializer - packed",
732 \\struct {int x,y,z;} __attribute__((packed)) s0 = {1, 2};
733 , &[_][]const u8{
734 \\const struct_unnamed_1 = packed struct {
735 \\ x: c_int,
736 \\ y: c_int,
737 \\ z: c_int,
738 \\};
739 \\pub export var s0: struct_unnamed_1 = struct_unnamed_1{
740 \\ .x = @as(c_int, 1),
741 \\ .y = @as(c_int, 2),
742 \\ .z = 0,
743 \\};
744 });
731 if (builtin.zig_backend == .stage1) {
732 cases.add("struct initializer - packed",
733 \\struct {int x,y,z;} __attribute__((packed)) s0 = {1, 2};
734 , &[_][]const u8{
735 \\const struct_unnamed_1 = packed struct {
736 \\ x: c_int,
737 \\ y: c_int,
738 \\ z: c_int,
739 \\};
740 \\pub export var s0: struct_unnamed_1 = struct_unnamed_1{
741 \\ .x = @as(c_int, 1),
742 \\ .y = @as(c_int, 2),
743 \\ .z = 0,
744 \\};
745 });
746 }
745747
746748 // Test case temporarily disabled:
747749 // https://github.com/ziglang/zig/issues/12055