Commit b4ef6fa09d

xackus <14938807+xackus@users.noreply.github.com>
2021-03-05 19:42:21
fix test-translate-c
1 parent 5f53b77
Changed files (1)
test/translate_c.zig
@@ -232,12 +232,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\                             | (*((unsigned char *)(p) + 1) << 8)  \
         \\                             | (*((unsigned char *)(p) + 2) << 16))
     , &[_][]const u8{
-        \\pub const FOO = (foo + 2).*;
+        \\pub const FOO = (foo + @as(c_int, 2)).*;
         ,
-        \\pub const VALUE = ((((1 + (2 * 3)) + (4 * 5)) + 6) << 7) | @boolToInt(8 == 9);
+        \\pub const VALUE = ((((@as(c_int, 1) + (@as(c_int, 2) * @as(c_int, 3))) + (@as(c_int, 4) * @as(c_int, 5))) + @as(c_int, 6)) << @as(c_int, 7)) | @boolToInt(@as(c_int, 8) == @as(c_int, 9));
         ,
-        \\pub fn _AL_READ3BYTES(p: anytype) callconv(.Inline) @TypeOf((@import("std").meta.cast([*c]u8, p).* | ((@import("std").meta.cast([*c]u8, p) + 1).* << 8)) | ((@import("std").meta.cast([*c]u8, p) + 2).* << 16)) {
-        \\    return (@import("std").meta.cast([*c]u8, p).* | ((@import("std").meta.cast([*c]u8, p) + 1).* << 8)) | ((@import("std").meta.cast([*c]u8, p) + 2).* << 16);
+        \\pub fn _AL_READ3BYTES(p: anytype) callconv(.Inline) @TypeOf((@import("std").meta.cast([*c]u8, p).* | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16))) {
+        \\    return (@import("std").meta.cast([*c]u8, p).* | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16));
         \\}
     });
 
@@ -312,14 +312,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\    return type_1;
         \\}
         ,
-        \\pub const LIGHTGRAY = @import("std").mem.zeroInit(CLITERAL(Color), .{ 200, 200, 200, 255 });
+        \\pub const LIGHTGRAY = @import("std").mem.zeroInit(CLITERAL(Color), .{ @as(c_int, 200), @as(c_int, 200), @as(c_int, 200), @as(c_int, 255) });
         ,
         \\pub const struct_boom_t = extern struct {
         \\    i1: c_int,
         \\};
         \\pub const boom_t = struct_boom_t;
         ,
-        \\pub const FOO = @import("std").mem.zeroInit(boom_t, .{1});
+        \\pub const FOO = @import("std").mem.zeroInit(boom_t, .{@as(c_int, 1)});
     });
 
     cases.add("complex switch",
@@ -343,8 +343,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
     cases.add("correct semicolon after infixop",
         \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
     , &[_][]const u8{
-        \\pub fn __ferror_unlocked_body(_fp: anytype) callconv(.Inline) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != 0) {
-        \\    return (_fp.*._flags & _IO_ERR_SEEN) != 0;
+        \\pub fn __ferror_unlocked_body(_fp: anytype) callconv(.Inline) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) {
+        \\    return (_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0);
         \\}
     });
 
@@ -352,11 +352,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\#define FOO(x) ((x >= 0) + (x >= 0))
         \\#define BAR 1 && 2 > 4
     , &[_][]const u8{
-        \\pub fn FOO(x: anytype) callconv(.Inline) @TypeOf(@boolToInt(x >= 0) + @boolToInt(x >= 0)) {
-        \\    return @boolToInt(x >= 0) + @boolToInt(x >= 0);
+        \\pub fn FOO(x: anytype) callconv(.Inline) @TypeOf(@boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0))) {
+        \\    return @boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0));
         \\}
         ,
-        \\pub const BAR = (1 != 0) and (2 > 4);
+        \\pub const BAR = (@as(c_int, 1) != 0) and (@as(c_int, 2) > @as(c_int, 4));
     });
 
     cases.add("struct with aligned fields",
@@ -401,15 +401,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\    break :blk bar;
         \\};
         ,
-        \\pub fn bar(x: anytype) callconv(.Inline) @TypeOf(baz(1, 2)) {
+        \\pub fn bar(x: anytype) callconv(.Inline) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) {
         \\    return blk: {
         \\        _ = &x;
-        \\        _ = 3;
-        \\        _ = 4 == 4;
-        \\        _ = 5 * 6;
-        \\        _ = baz(1, 2);
-        \\        _ = 2 % 2;
-        \\        break :blk baz(1, 2);
+        \\        _ = @as(c_int, 3);
+        \\        _ = @as(c_int, 4) == @as(c_int, 4);
+        \\        _ = @as(c_int, 5) * @as(c_int, 6);
+        \\        _ = baz(@as(c_int, 1), @as(c_int, 2));
+        \\        _ = @as(c_int, 2) % @as(c_int, 2);
+        \\        break :blk baz(@as(c_int, 1), @as(c_int, 2));
         \\    };
         \\}
     });
@@ -418,9 +418,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\#define foo 1
         \\#define inline 2
     , &[_][]const u8{
-        \\pub const foo = 1;
+        \\pub const foo = @as(c_int, 1);
         ,
-        \\pub const @"inline" = 2;
+        \\pub const @"inline" = @as(c_int, 2);
     });
 
     cases.add("macro line continuation",
@@ -507,7 +507,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
     cases.add("#define hex literal with capital X",
         \\#define VAL 0XF00D
     , &[_][]const u8{
-        \\pub const VAL = 0xF00D;
+        \\pub const VAL = @import("std").meta.promoteIntLiteral(c_int, 0xF00D, .hexadecimal);
     });
 
     cases.add("anonymous struct & unions",
@@ -872,7 +872,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
     cases.add("macro with left shift",
         \\#define REDISMODULE_READ (1<<0)
     , &[_][]const u8{
-        \\pub const REDISMODULE_READ = 1 << 0;
+        \\pub const REDISMODULE_READ = @as(c_int, 1) << @as(c_int, 0);
     });
 
     cases.add("macro with right shift",
@@ -881,7 +881,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
     , &[_][]const u8{
         \\pub const FLASH_SIZE = @as(c_ulong, 0x200000);
         ,
-        \\pub const FLASH_BANK_SIZE = FLASH_SIZE >> 1;
+        \\pub const FLASH_BANK_SIZE = FLASH_SIZE >> @as(c_int, 1);
     });
 
     cases.add("double define struct",
@@ -949,14 +949,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
     cases.add("#define an unsigned integer literal",
         \\#define CHANNEL_COUNT 24
     , &[_][]const u8{
-        \\pub const CHANNEL_COUNT = 24;
+        \\pub const CHANNEL_COUNT = @as(c_int, 24);
     });
 
     cases.add("#define referencing another #define",
         \\#define THING2 THING1
         \\#define THING1 1234
     , &[_][]const u8{
-        \\pub const THING1 = 1234;
+        \\pub const THING1 = @as(c_int, 1234);
         ,
         \\pub const THING2 = THING1;
     });
@@ -1002,7 +1002,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
     cases.add("macro with parens around negative number",
         \\#define LUA_GLOBALSINDEX        (-10002)
     , &[_][]const u8{
-        \\pub const LUA_GLOBALSINDEX = -10002;
+        \\pub const LUA_GLOBALSINDEX = -@as(c_int, 10002);
     });
 
     cases.add(
@@ -1085,8 +1085,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\#define foo 1 //foo
         \\#define bar /* bar */ 2
     , &[_][]const u8{
-        "pub const foo = 1;",
-        "pub const bar = 2;",
+        "pub const foo = @as(c_int, 1);",
+        "pub const bar = @as(c_int, 2);",
     });
 
     cases.add("string prefix",
@@ -1716,7 +1716,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
     cases.add("comment after integer literal",
         \\#define SDL_INIT_VIDEO 0x00000020  /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
     , &[_][]const u8{
-        \\pub const SDL_INIT_VIDEO = 0x00000020;
+        \\pub const SDL_INIT_VIDEO = @as(c_int, 0x00000020);
     });
 
     cases.add("u integer suffix after hex literal",
@@ -1830,8 +1830,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
     , &[_][]const u8{
         \\pub extern var c: c_int;
         ,
-        \\pub fn BASIC(c_1: anytype) callconv(.Inline) @TypeOf(c_1 * 2) {
-        \\    return c_1 * 2;
+        \\pub fn BASIC(c_1: anytype) callconv(.Inline) @TypeOf(c_1 * @as(c_int, 2)) {
+        \\    return c_1 * @as(c_int, 2);
         \\}
         ,
         \\pub fn FOO(L: anytype, b: anytype) callconv(.Inline) @TypeOf(L + b) {
@@ -2475,7 +2475,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\    return array[@intCast(c_uint, index)];
         \\}
         ,
-        \\pub const ACCESS = array[2];
+        \\pub const ACCESS = array[@as(c_int, 2)];
     });
 
     cases.add("cast signed array index to unsigned",
@@ -3091,7 +3091,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         ,
         \\pub const BAR = @import("std").meta.cast(?*c_void, a);
         ,
-        \\pub const BAZ = @import("std").meta.cast(u32, 2);
+        \\pub const BAZ = @import("std").meta.cast(u32, @as(c_int, 2));
     });
 
     cases.add("macro with cast to unsigned short, long, and long long",
@@ -3099,9 +3099,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\#define CURLAUTH_BASIC ((unsigned long) 1)
         \\#define CURLAUTH_BASIC_BUT_ULONGLONG ((unsigned long long) 1)
     , &[_][]const u8{
-        \\pub const CURLAUTH_BASIC_BUT_USHORT = @import("std").meta.cast(c_ushort, 1);
-        \\pub const CURLAUTH_BASIC = @import("std").meta.cast(c_ulong, 1);
-        \\pub const CURLAUTH_BASIC_BUT_ULONGLONG = @import("std").meta.cast(c_ulonglong, 1);
+        \\pub const CURLAUTH_BASIC_BUT_USHORT = @import("std").meta.cast(c_ushort, @as(c_int, 1));
+        \\pub const CURLAUTH_BASIC = @import("std").meta.cast(c_ulong, @as(c_int, 1));
+        \\pub const CURLAUTH_BASIC_BUT_ULONGLONG = @import("std").meta.cast(c_ulonglong, @as(c_int, 1));
     });
 
     cases.add("macro conditional operator",
@@ -3196,7 +3196,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\    bar_1 = 2;
         \\}
         ,
-        \\pub const bar = 4;
+        \\pub const bar = @as(c_int, 4);
     });
 
     cases.add("don't export inline functions",
@@ -3325,9 +3325,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\#define NULL ((void*)0)
         \\#define FOO ((int)0x8000)
     , &[_][]const u8{
-        \\pub const NULL = @import("std").meta.cast(?*c_void, 0);
+        \\pub const NULL = @import("std").meta.cast(?*c_void, @as(c_int, 0));
         ,
-        \\pub const FOO = @import("std").meta.cast(c_int, 0x8000);
+        \\pub const FOO = @import("std").meta.cast(c_int, @import("std").meta.promoteIntLiteral(c_int, 0x8000, .hexadecimal));
     });
 
     if (std.Target.current.abi == .msvc) {