authorgravatar for 50495725+Banacial@users.noreply.github.comBanacial <50495725+Banacial@users.noreply.github.com> 2023-09-18 12:27:32+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-09-18 13:27:32+03:00
logd2a937838e26ad0bb380b843dee9781a96a01ef5
tree6ae1a3695f1fa01b126c05e5b2a7a1cce96708c1
parentada02ac6fe502589575d07c37b13f05bf890f54b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.zig.c_translation.zig: fix L suffix with sized ints

Closes #15066

3 files changed, 42 insertions(+), 7 deletions(-)

lib/std/zig/c_translation.zig+33-6
...@@ -384,16 +384,16 @@ pub const Macros = struct {...@@ -384,16 +384,16 @@ pub const Macros = struct {
384 }384 }
385385
386 fn L_SUFFIX_ReturnType(comptime number: anytype) type {386 fn L_SUFFIX_ReturnType(comptime number: anytype) type {
387 switch (@TypeOf(number)) {387 switch (@typeInfo(@TypeOf(number))) {
388 comptime_int => return @TypeOf(promoteIntLiteral(c_long, number, .decimal)),388 .Int, .ComptimeInt => return @TypeOf(promoteIntLiteral(c_long, number, .decimal)),
389 comptime_float => return c_longdouble,389 .Float, .ComptimeFloat => return c_longdouble,
390 else => @compileError("Invalid value for L suffix"),390 else => @compileError("Invalid value for L suffix"),
391 }391 }
392 }392 }
393 pub fn L_SUFFIX(comptime number: anytype) L_SUFFIX_ReturnType(number) {393 pub fn L_SUFFIX(comptime number: anytype) L_SUFFIX_ReturnType(number) {
394 switch (@TypeOf(number)) {394 switch (@typeInfo(@TypeOf(number))) {
395 comptime_int => return promoteIntLiteral(c_long, number, .decimal),395 .Int, .ComptimeInt => return promoteIntLiteral(c_long, number, .decimal),
396 comptime_float => @compileError("TODO: c_longdouble initialization from comptime_float not supported"),396 .Float, .ComptimeFloat => @compileError("TODO: c_longdouble initialization from comptime_float not supported"),
397 else => @compileError("Invalid value for L suffix"),397 else => @compileError("Invalid value for L suffix"),
398 }398 }
399 }399 }
...@@ -644,3 +644,30 @@ test "CAST_OR_CALL calling" {...@@ -644,3 +644,30 @@ test "CAST_OR_CALL calling" {
644644
645 try testing.expectEqual(Helper.identity(@as(c_uint, 100)), Macros.CAST_OR_CALL(Helper.identity, @as(c_uint, 100)));645 try testing.expectEqual(Helper.identity(@as(c_uint, 100)), Macros.CAST_OR_CALL(Helper.identity, @as(c_uint, 100)));
646}646}
647
648test "Extended C ABI casting" {
649 if (math.maxInt(c_long) > math.maxInt(c_char)) {
650 try testing.expect(@TypeOf(Macros.L_SUFFIX(@as(c_char, math.maxInt(c_char) - 1))) == c_long); // c_char
651 }
652 if (math.maxInt(c_long) > math.maxInt(c_short)) {
653 try testing.expect(@TypeOf(Macros.L_SUFFIX(@as(c_short, math.maxInt(c_short) - 1))) == c_long); // c_short
654 }
655
656 if (math.maxInt(c_long) > math.maxInt(c_ushort)) {
657 try testing.expect(@TypeOf(Macros.L_SUFFIX(@as(c_ushort, math.maxInt(c_ushort) - 1))) == c_long); //c_ushort
658 }
659
660 if (math.maxInt(c_long) > math.maxInt(c_int)) {
661 try testing.expect(@TypeOf(Macros.L_SUFFIX(@as(c_int, math.maxInt(c_int) - 1))) == c_long); // c_int
662 }
663
664 if (math.maxInt(c_long) > math.maxInt(c_uint)) {
665 try testing.expect(@TypeOf(Macros.L_SUFFIX(@as(c_uint, math.maxInt(c_uint) - 1))) == c_long); // c_uint
666 try testing.expect(@TypeOf(Macros.L_SUFFIX(math.maxInt(c_uint) + 1)) == c_long); // comptime_int -> c_long
667 }
668
669 if (math.maxInt(c_longlong) > math.maxInt(c_long)) {
670 try testing.expect(@TypeOf(Macros.L_SUFFIX(@as(c_long, math.maxInt(c_long) - 1))) == c_long); // c_long
671 try testing.expect(@TypeOf(Macros.L_SUFFIX(math.maxInt(c_long) + 1)) == c_longlong); // comptime_int -> c_longlong
672 }
673}
test/behavior/translate_c_macros.h+4-1
...@@ -58,4 +58,7 @@ typedef _Bool uintptr_t;...@@ -58,4 +58,7 @@ typedef _Bool uintptr_t;
58#define DIVIDE_ARGS(A, B) (A / B)58#define DIVIDE_ARGS(A, B) (A / B)
5959
60#define REMAINDER_CONSTANT(version) (version % 10000)60#define REMAINDER_CONSTANT(version) (version % 10000)
61#define REMAINDER_ARGS(A, B) (A % B)
\ No newline at end of file
61#define REMAINDER_ARGS(A, B) (A % B)
62
63#define LONG(x) x##L
64#define X LONG(10)
test/behavior/translate_c_macros.zig+5
...@@ -231,3 +231,8 @@ test "@typeInfo on @cImport result" {...@@ -231,3 +231,8 @@ test "@typeInfo on @cImport result" {
231231
232 try expect(@typeInfo(h).Struct.decls.len > 1);232 try expect(@typeInfo(h).Struct.decls.len > 1);
233}233}
234
235test "Macro that uses Long type concatenation casting" {
236 try expect((@TypeOf(h.X)) == c_long);
237 try expectEqual(h.X, @as(c_long, 10));
238}