authorgravatar for 3405586+schmee@users.noreply.github.comJohn Schmidt <3405586+schmee@users.noreply.github.com> 2023-02-10 14:46:29+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-02-10 15:46:29+02:00
loga5d25fabdaf7e68f375874b9bda402acaeb9545d
treeeaa62bf7c9919efbd5b8ba8d3bd467ad2462df62
parentd24ebf1d12cf66665b52136a2807f97ff021d78d
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

translate_c: fix typedeffed pointer subtraction

Closes #14560.

2 files changed, 34 insertions(+), 1 deletions(-)

src/translate_c.zig+2-1
...@@ -1748,7 +1748,8 @@ fn transBinaryOperator(...@@ -1748,7 +1748,8 @@ fn transBinaryOperator(
1748 const lhs_expr = stmt.getLHS();1748 const lhs_expr = stmt.getLHS();
1749 const lhs_qt = getExprQualType(c, lhs_expr);1749 const lhs_qt = getExprQualType(c, lhs_expr);
1750 const lhs_qt_translated = try transQualType(c, scope, lhs_qt, lhs_expr.getBeginLoc());1750 const lhs_qt_translated = try transQualType(c, scope, lhs_qt, lhs_expr.getBeginLoc());
1751 const elem_type = lhs_qt_translated.castTag(.c_pointer).?.data.elem_type;1751 const c_pointer = getContainer(c, lhs_qt_translated).?;
1752 const elem_type = c_pointer.castTag(.c_pointer).?.data.elem_type;
1752 const sizeof = try Tag.sizeof.create(c.arena, elem_type);1753 const sizeof = try Tag.sizeof.create(c.arena, elem_type);
17531754
1754 const bitcast = try Tag.bit_cast.create(c.arena, .{ .lhs = ptrdiff_type, .rhs = infixOpNode });1755 const bitcast = try Tag.bit_cast.create(c.arena, .{ .lhs = ptrdiff_type, .rhs = infixOpNode });
test/translate_c.zig+32
...@@ -3916,4 +3916,36 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3916,4 +3916,36 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3916 \\ }) != 0) {}3916 \\ }) != 0) {}
3917 \\}3917 \\}
3918 });3918 });
3919
3920 if (builtin.os.tag == .windows) {
3921 cases.add("Pointer subtraction with typedef",
3922 \\typedef char* S;
3923 \\void foo() {
3924 \\ S a, b;
3925 \\ long long c = a - b;
3926 \\}
3927 , &[_][]const u8{
3928 \\pub export fn foo() void {
3929 \\ var a: S = undefined;
3930 \\ var b: S = undefined;
3931 \\ var c: c_longlong = @divExact(@bitCast(c_longlong, @ptrToInt(a) -% @ptrToInt(b)), @sizeOf(u8));
3932 \\ _ = @TypeOf(c);
3933 \\}
3934 });
3935 } else {
3936 cases.add("Pointer subtraction with typedef",
3937 \\typedef char* S;
3938 \\void foo() {
3939 \\ S a, b;
3940 \\ long c = a - b;
3941 \\}
3942 , &[_][]const u8{
3943 \\pub export fn foo() void {
3944 \\ var a: S = undefined;
3945 \\ var b: S = undefined;
3946 \\ var c: c_long = @divExact(@bitCast(c_long, @ptrToInt(a) -% @ptrToInt(b)), @sizeOf(u8));
3947 \\ _ = @TypeOf(c);
3948 \\}
3949 });
3950 }
3919}3951}