aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-09-03 01:55:01 +0300
committerVeikka Tuominen <git@vexu.eu>2022-09-03 03:42:42 +0300
commitc7884af063791211544c6595a4900bbfcd5d96b6 (patch)
treea8e234e9ad48519bd39f3957cdbeed7df0ee84c8
parent0f61d1f0df887081d60558256e10944633eb868f (diff)
downloadzig-c7884af063791211544c6595a4900bbfcd5d96b6.tar.gz
zig-c7884af063791211544c6595a4900bbfcd5d96b6.zip
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.
-rw-r--r--src/translate_c.zig4
-rw-r--r--test/run_translated_c.zig26
-rw-r--r--test/translate_c.zig30
3 files changed, 34 insertions, 26 deletions
diff --git a/src/translate_c.zig b/src/translate_c.zig
index b0fae81475..faa8a456f5 100644
--- a/src/translate_c.zig
+++ b/src/translate_c.zig
@@ -1166,6 +1166,10 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_decl: *const clang.RecordD
});
}
+ if (!c.zig_is_stage1 and is_packed) {
+ return failDecl(c, record_loc, bare_name, "cannot translate packed record union", .{});
+ }
+
const record_payload = try c.arena.create(ast.Payload.Record);
record_payload.* = .{
.base = .{ .tag = ([2]Tag{ .@"struct", .@"union" })[@boolToInt(is_union)] },
diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig
index 4345625dc1..5aa72e2d1f 100644
--- a/test/run_translated_c.zig
+++ b/test/run_translated_c.zig
@@ -250,18 +250,20 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\}
, "");
- cases.add("struct initializer - packed",
- \\#define _NO_CRT_STDIO_INLINE 1
- \\#include <stdint.h>
- \\#include <stdlib.h>
- \\struct s {uint8_t x,y;
- \\ uint32_t z;} __attribute__((packed)) s0 = {1, 2};
- \\int main() {
- \\ /* sizeof nor offsetof currently supported */
- \\ if (((intptr_t)&s0.z - (intptr_t)&s0.x) != 2) abort();
- \\ return 0;
- \\}
- , "");
+ if (@import("builtin").zig_backend == .stage1) {
+ cases.add("struct initializer - packed",
+ \\#define _NO_CRT_STDIO_INLINE 1
+ \\#include <stdint.h>
+ \\#include <stdlib.h>
+ \\struct s {uint8_t x,y;
+ \\ uint32_t z;} __attribute__((packed)) s0 = {1, 2};
+ \\int main() {
+ \\ /* sizeof nor offsetof currently supported */
+ \\ if (((intptr_t)&s0.z - (intptr_t)&s0.x) != 2) abort();
+ \\ return 0;
+ \\}
+ , "");
+ }
cases.add("cast signed array index to unsigned",
\\#include <stdlib.h>
diff --git a/test/translate_c.zig b/test/translate_c.zig
index 637d491f49..54b4ad6081 100644
--- a/test/translate_c.zig
+++ b/test/translate_c.zig
@@ -728,20 +728,22 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\}
});
- cases.add("struct initializer - packed",
- \\struct {int x,y,z;} __attribute__((packed)) s0 = {1, 2};
- , &[_][]const u8{
- \\const struct_unnamed_1 = packed struct {
- \\ x: c_int,
- \\ y: c_int,
- \\ z: c_int,
- \\};
- \\pub export var s0: struct_unnamed_1 = struct_unnamed_1{
- \\ .x = @as(c_int, 1),
- \\ .y = @as(c_int, 2),
- \\ .z = 0,
- \\};
- });
+ if (builtin.zig_backend == .stage1) {
+ cases.add("struct initializer - packed",
+ \\struct {int x,y,z;} __attribute__((packed)) s0 = {1, 2};
+ , &[_][]const u8{
+ \\const struct_unnamed_1 = packed struct {
+ \\ x: c_int,
+ \\ y: c_int,
+ \\ z: c_int,
+ \\};
+ \\pub export var s0: struct_unnamed_1 = struct_unnamed_1{
+ \\ .x = @as(c_int, 1),
+ \\ .y = @as(c_int, 2),
+ \\ .z = 0,
+ \\};
+ });
+ }
// Test case temporarily disabled:
// https://github.com/ziglang/zig/issues/12055