From 9390e8b84883331757d3ce15cfca89279aceb090 Mon Sep 17 00:00:00 2001 From: via Date: Tue, 7 Jan 2020 02:36:07 -0500 Subject: [PATCH] Preserve packed attribute in C translated struct (#4085) * Preserve packed attribute in C translated struct * Add tests for packed C struct --- src-self-hosted/clang.zig | 1 + src-self-hosted/translate_c.zig | 7 +++++-- src/zig_clang.cpp | 8 ++++++++ src/zig_clang.h | 1 + test/run_translated_c.zig | 13 +++++++++++++ test/translate_c.zig | 15 +++++++++++++++ 6 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src-self-hosted/clang.zig b/src-self-hosted/clang.zig index 3ff59c2281..6ca263cdd6 100644 --- a/src-self-hosted/clang.zig +++ b/src-self-hosted/clang.zig @@ -768,6 +768,7 @@ pub extern fn ZigClangVarDecl_getCanonicalDecl(self: ?*const struct_ZigClangVarD pub extern fn ZigClangVarDecl_getSectionAttribute(self: *const ZigClangVarDecl, len: *usize) ?[*]const u8; pub extern fn ZigClangFunctionDecl_getAlignedAttribute(self: *const ZigClangFunctionDecl, *const ZigClangASTContext) c_uint; pub extern fn ZigClangVarDecl_getAlignedAttribute(self: *const ZigClangVarDecl, *const ZigClangASTContext) c_uint; +pub extern fn ZigClangRecordDecl_getPackedAttribute(self: ?*const struct_ZigClangRecordDecl) bool; pub extern fn ZigClangRecordDecl_getDefinition(self: ?*const struct_ZigClangRecordDecl) ?*const struct_ZigClangRecordDecl; pub extern fn ZigClangEnumDecl_getDefinition(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangEnumDecl; pub extern fn ZigClangRecordDecl_getLocation(self: ?*const struct_ZigClangRecordDecl) struct_ZigClangSourceLocation; diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index 64ea8fa62f..8961ffb680 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -733,13 +733,16 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?* break :blk opaque; }; - const extern_tok = try appendToken(c, .Keyword_extern, "extern"); + const layout_tok = try if (ZigClangRecordDecl_getPackedAttribute(record_decl)) + appendToken(c, .Keyword_packed, "packed") + else + appendToken(c, .Keyword_extern, "extern"); const container_tok = try appendToken(c, container_kind, container_kind_name); const lbrace_token = try appendToken(c, .LBrace, "{"); const container_node = try c.a().create(ast.Node.ContainerDecl); container_node.* = .{ - .layout_token = extern_tok, + .layout_token = layout_tok, .kind_token = container_tok, .init_arg_expr = .None, .fields_and_decls = ast.Node.ContainerDecl.DeclList.init(c.a()), diff --git a/src/zig_clang.cpp b/src/zig_clang.cpp index aaf4ae40a5..925d719d64 100644 --- a/src/zig_clang.cpp +++ b/src/zig_clang.cpp @@ -1597,6 +1597,14 @@ const char* ZigClangVarDecl_getSectionAttribute(const struct ZigClangVarDecl *se return nullptr; } +bool ZigClangRecordDecl_getPackedAttribute(const ZigClangRecordDecl *zig_record_decl) { + const clang::RecordDecl *record_decl = reinterpret_cast(zig_record_decl); + if (record_decl->getAttr()) { + return true; + } + return false; +} + unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx) { auto casted_self = reinterpret_cast(self); auto casted_ctx = const_cast(reinterpret_cast(ctx)); diff --git a/src/zig_clang.h b/src/zig_clang.h index 0066eb51f4..0d89d3c258 100644 --- a/src/zig_clang.h +++ b/src/zig_clang.h @@ -863,6 +863,7 @@ ZIG_EXTERN_C const char* ZigClangVarDecl_getSectionAttribute(const struct ZigCla ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx); ZIG_EXTERN_C unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx); +ZIG_EXTERN_C bool ZigClangRecordDecl_getPackedAttribute(const struct ZigClangRecordDecl *); ZIG_EXTERN_C const struct ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const struct ZigClangRecordDecl *); ZIG_EXTERN_C const struct ZigClangEnumDecl *ZigClangEnumDecl_getDefinition(const struct ZigClangEnumDecl *); diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig index 0b11018118..a9b9a8d6c2 100644 --- a/test/run_translated_c.zig +++ b/test/run_translated_c.zig @@ -83,4 +83,17 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void { \\ return 0; \\} , ""); + + cases.add("struct initializer - packed", + \\#define _NO_CRT_STDIO_INLINE 1 + \\#include + \\#include + \\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; + \\} + , ""); } diff --git a/test/translate_c.zig b/test/translate_c.zig index af9c961c97..6557c98963 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -121,6 +121,21 @@ 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, + \\}; + }); + cases.add("align() attribute", \\__attribute__ ((aligned(128))) \\extern char my_array[16];