authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-03-06 09:17:17-08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-03-07 14:53:33+02:00
log874c63f89cb6174d2dba32f4d08c140b5eed6744
treed4f68a819de0b2545bd3a8d8156112c68e0358ad
parenta7c0234eee97c11b1dd6879a3cf313ac9a7bdbd9

translate-c: translate align attribute for block scoped variables


2 files changed, 18 insertions(+), 29 deletions(-)

src/translate_c.zig+12-29
......@@ -671,15 +671,6 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co
671671 break :blk null;
672672 };
673673
674 const alignment = blk: {
675 const alignment = var_decl.getAlignedAttribute(c.clang_context);
676 if (alignment != 0) {
677 // Clang reports the alignment in bits
678 break :blk alignment / 8;
679 }
680 break :blk null;
681 };
682
683674 const node = try Tag.var_decl.create(c.arena, .{
684675 .is_pub = is_pub,
685676 .is_const = is_const,
......@@ -687,7 +678,7 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co
687678 .is_export = is_export,
688679 .is_threadlocal = is_threadlocal,
689680 .linksection_string = linksection_string,
690 .alignment = alignment,
681 .alignment = zigAlignment(var_decl.getAlignedAttribute(c.clang_context)),
691682 .name = checked_name,
692683 .type = type_node,
693684 .init = init_node,
......@@ -833,14 +824,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_decl: *const clang.RecordD
833824 else => |e| return e,
834825 };
835826
836 const alignment = blk_2: {
837 const alignment = field_decl.getAlignedAttribute(c.clang_context);
838 if (alignment != 0) {
839 // Clang reports the alignment in bits
840 break :blk_2 alignment / 8;
841 }
842 break :blk_2 null;
843 };
827 const alignment = zigAlignment(field_decl.getAlignedAttribute(c.clang_context));
844828
845829 if (is_anon) {
846830 try c.decl_table.putNoClobber(c.gpa, @ptrToInt(field_decl.getCanonicalDecl()), field_name);
......@@ -1373,6 +1357,13 @@ fn transCStyleCastExprClass(
13731357 return maybeSuppressResult(c, scope, result_used, cast_node);
13741358}
13751359
1360/// Clang reports the alignment in bits, we use bytes
1361/// Clang uses 0 for "no alignment specified", we use null
1362fn zigAlignment(bit_alignment: c_uint) ?c_uint {
1363 if (bit_alignment == 0) return null;
1364 return bit_alignment / 8;
1365}
1366
13761367fn transDeclStmtOne(
13771368 c: *Context,
13781369 scope: *Scope,
......@@ -1412,6 +1403,7 @@ fn transDeclStmtOne(
14121403 if (!qualTypeIsBoolean(qual_type) and isBoolRes(init_node)) {
14131404 init_node = try Tag.bool_to_int.create(c.arena, init_node);
14141405 }
1406
14151407 const node = try Tag.var_decl.create(c.arena, .{
14161408 .is_pub = false,
14171409 .is_const = is_const,
......@@ -1419,7 +1411,7 @@ fn transDeclStmtOne(
14191411 .is_export = false,
14201412 .is_threadlocal = false,
14211413 .linksection_string = null,
1422 .alignment = null,
1414 .alignment = zigAlignment(var_decl.getAlignedAttribute(c.clang_context)),
14231415 .name = mangled_name,
14241416 .type = type_node,
14251417 .init = init_node,
......@@ -4111,16 +4103,7 @@ fn finishTransFnProto(
41114103 break :blk null;
41124104 };
41134105
4114 const alignment = blk: {
4115 if (fn_decl) |decl| {
4116 const alignment = decl.getAlignedAttribute(c.clang_context);
4117 if (alignment != 0) {
4118 // Clang reports the alignment in bits
4119 break :blk alignment / 8;
4120 }
4121 }
4122 break :blk null;
4123 };
4106 const alignment = if (fn_decl) |decl| zigAlignment(decl.getAlignedAttribute(c.clang_context)) else null;
41244107
41254108 const explicit_callconv = if ((is_export or is_extern) and cc == .C) null else cc;
41264109
test/translate_c.zig+6
......@@ -643,9 +643,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
643643 \\extern char my_array[16];
644644 \\__attribute__ ((aligned(128)))
645645 \\void my_fn(void) { }
646 \\void other_fn(void) {
647 \\ char ARR[16] __attribute__ ((aligned (16)));
648 \\}
646649 , &[_][]const u8{
647650 \\pub extern var my_array: [16]u8 align(128);
648651 \\pub export fn my_fn() align(128) void {}
652 \\pub export fn other_fn() void {
653 \\ var ARR: [16]u8 align(16) = undefined;
654 \\}
649655 });
650656
651657 cases.add("linksection() attribute",