diff options
| author | LemonBoy <thatlemon@gmail.com> | 2021-04-25 16:50:41 +0200 |
|---|---|---|
| committer | LemonBoy <thatlemon@gmail.com> | 2021-04-25 16:50:41 +0200 |
| commit | ae15022406e5d59787195130cfd5261c6336f41c (patch) | |
| tree | ff5184e7f432c08496bcbee186f5c5856c6efca1 /src | |
| parent | 19cec0db1e8a9fefd8295c81edb03d631c624e62 (diff) | |
| download | zig-ae15022406e5d59787195130cfd5261c6336f41c.tar.gz zig-ae15022406e5d59787195130cfd5261c6336f41c.zip | |
translate-c: Fix casting of function pointers
The @ptrCast(X, @alignCast(@alignOf(T), Y)) pattern is only correct if T
is not a function type or a pointer, in that case the @alignOf refers to
the pointer itself and not to the pointee type.
Diffstat (limited to 'src')
| -rw-r--r-- | src/translate_c.zig | 2 | ||||
| -rw-r--r-- | src/translate_c/ast.zig | 12 |
2 files changed, 12 insertions, 2 deletions
diff --git a/src/translate_c.zig b/src/translate_c.zig index 60b3961e6e..0a722ab3b4 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -3539,7 +3539,7 @@ fn transCPtrCast( expr else blk: { const child_type_node = try transQualType(c, scope, child_type, loc); - const alignof = try Tag.alignof.create(c.arena, child_type_node); + const alignof = try Tag.std_meta_alignment.create(c.arena, child_type_node); const align_cast = try Tag.align_cast.create(c.arena, .{ .lhs = alignof, .rhs = expr }); break :blk align_cast; }; diff --git a/src/translate_c/ast.zig b/src/translate_c/ast.zig index 61d28bb22d..2e75bf1182 100644 --- a/src/translate_c/ast.zig +++ b/src/translate_c/ast.zig @@ -120,8 +120,11 @@ pub const Node = extern union { std_math_Log2Int, /// @intCast(lhs, rhs) int_cast, - /// @rem(lhs, rhs) + /// @import("std").meta.promoteIntLiteral(value, type, radix) std_meta_promoteIntLiteral, + /// @import("std").meta.alignment(value) + std_meta_alignment, + /// @rem(lhs, rhs) rem, /// @divTrunc(lhs, rhs) div_trunc, @@ -260,6 +263,7 @@ pub const Node = extern union { .switch_else, .block_single, .std_meta_sizeof, + .std_meta_alignment, .bool_to_int, .sizeof, .alignof, @@ -876,6 +880,11 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex { const import_node = try renderStdImport(c, "meta", "promoteIntLiteral"); return renderCall(c, import_node, &.{ payload.type, payload.value, payload.radix }); }, + .std_meta_alignment => { + const payload = node.castTag(.std_meta_alignment).?.data; + const import_node = try renderStdImport(c, "meta", "alignment"); + return renderCall(c, import_node, &.{payload}); + }, .std_meta_sizeof => { const payload = node.castTag(.std_meta_sizeof).?.data; const import_node = try renderStdImport(c, "meta", "sizeof"); @@ -2144,6 +2153,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex { .typeof, .typeinfo, .std_meta_sizeof, + .std_meta_alignment, .std_meta_cast, .std_meta_promoteIntLiteral, .std_meta_vector, |
