| author | |
| committer | |
| log | c7884af063791211544c6595a4900bbfcd5d96b6 |
| tree | a8e234e9ad48519bd39f3957cdbeed7df0ee84c8 |
| parent | 0f61d1f0df887081d60558256e10944633eb868f |
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 | 1166 | }); |
| 1167 | 1167 | } |
| 1168 | 1168 | |
| 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 | 1173 | const record_payload = try c.arena.create(ast.Payload.Record); |
| 1170 | 1174 | record_payload.* = .{ |
| 1171 | 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 | 250 | \\} |
| 251 | 251 | , ""); |
| 252 | 252 | |
| 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 | } | |
| 265 | 267 | |
| 266 | 268 | cases.add("cast signed array index to unsigned", |
| 267 | 269 | \\#include <stdlib.h> |
test/translate_c.zig+16-14| ... | ... | @@ -728,20 +728,22 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 728 | 728 | \\} |
| 729 | 729 | }); |
| 730 | 730 | |
| 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 | } | |
| 745 | 747 | |
| 746 | 748 | // Test case temporarily disabled: |
| 747 | 749 | // https://github.com/ziglang/zig/issues/12055 |