authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-30 19:11:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-30 19:11:31-07:00
logea4ef832e364d48b4670c71d948782df12e5e4ca
treeb1c395eb2cd6b22d0c5feb55c5d73323dc7aa967
parentfb81ba87620467de11db6b904741b4aafc7cb756

fix llvm 15 translate-c regressions

by inserting explicit casts. closes #12682

2 files changed, 65 insertions(+), 74 deletions(-)

test/run_translated_c.zig+12-17
......@@ -1868,21 +1868,16 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
18681868 \\}
18691869 , "");
18701870
1871 // Regressed with LLVM 15:
1872 // https://github.com/ziglang/zig/issues/12682
1873 if (false) {
1874 // The C standard does not require function pointers to be convertible to any integer type.
1875 // However, POSIX requires that function pointers have the same representation as `void *`
1876 // so that dlsym() can work
1877 cases.add("Function to integral",
1878 \\#include <stdint.h>
1879 \\int main(void) {
1880 \\#if defined(__UINTPTR_MAX__) && __has_include(<unistd.h>)
1881 \\ uintptr_t x = main;
1882 \\ x = (uintptr_t)main;
1883 \\#endif
1884 \\ return 0;
1885 \\}
1886 , "");
1887 }
1871 // The C standard does not require function pointers to be convertible to any integer type.
1872 // However, POSIX requires that function pointers have the same representation as `void *`
1873 // so that dlsym() can work
1874 cases.add("Function to integral",
1875 \\#include <stdint.h>
1876 \\int main(void) {
1877 \\#if defined(__UINTPTR_MAX__) && __has_include(<unistd.h>)
1878 \\ uintptr_t x = (uintptr_t)main;
1879 \\#endif
1880 \\ return 0;
1881 \\}
1882 , "");
18881883}
test/translate_c.zig+53-57
......@@ -3173,63 +3173,59 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31733173 \\}
31743174 });
31753175
3176 // Regressed with LLVM 15:
3177 // https://github.com/ziglang/zig/issues/12682
3178 if (false) {
3179 if (builtin.zig_backend != .stage1) {
3180 cases.add("implicit casts",
3181 \\#include <stdbool.h>
3182 \\
3183 \\void fn_int(int x);
3184 \\void fn_f32(float x);
3185 \\void fn_f64(double x);
3186 \\void fn_char(char x);
3187 \\void fn_bool(bool x);
3188 \\void fn_ptr(void *x);
3189 \\
3190 \\void call() {
3191 \\ fn_int(3.0f);
3192 \\ fn_int(3.0);
3193 \\ fn_int('ABCD');
3194 \\ fn_f32(3);
3195 \\ fn_f64(3);
3196 \\ fn_char('3');
3197 \\ fn_char('\x1');
3198 \\ fn_char(0);
3199 \\ fn_f32(3.0f);
3200 \\ fn_f64(3.0);
3201 \\ fn_bool(123);
3202 \\ fn_bool(0);
3203 \\ fn_bool(&fn_int);
3204 \\ fn_int(&fn_int);
3205 \\ fn_ptr(42);
3206 \\}
3207 , &[_][]const u8{
3208 \\pub extern fn fn_int(x: c_int) void;
3209 \\pub extern fn fn_f32(x: f32) void;
3210 \\pub extern fn fn_f64(x: f64) void;
3211 \\pub extern fn fn_char(x: u8) void;
3212 \\pub extern fn fn_bool(x: bool) void;
3213 \\pub extern fn fn_ptr(x: ?*anyopaque) void;
3214 \\pub export fn call() void {
3215 \\ fn_int(@floatToInt(c_int, 3.0));
3216 \\ fn_int(@floatToInt(c_int, 3.0));
3217 \\ fn_int(@as(c_int, 1094861636));
3218 \\ fn_f32(@intToFloat(f32, @as(c_int, 3)));
3219 \\ fn_f64(@intToFloat(f64, @as(c_int, 3)));
3220 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));
3221 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));
3222 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));
3223 \\ fn_f32(3.0);
3224 \\ fn_f64(3.0);
3225 \\ fn_bool(@as(c_int, 123) != 0);
3226 \\ fn_bool(@as(c_int, 0) != 0);
3227 \\ fn_bool(@ptrToInt(&fn_int) != 0);
3228 \\ fn_int(@intCast(c_int, @ptrToInt(&fn_int)));
3229 \\ fn_ptr(@intToPtr(?*anyopaque, @as(c_int, 42)));
3230 \\}
3231 });
3232 }
3176 if (builtin.zig_backend != .stage1) {
3177 cases.add("implicit casts",
3178 \\#include <stdbool.h>
3179 \\
3180 \\void fn_int(int x);
3181 \\void fn_f32(float x);
3182 \\void fn_f64(double x);
3183 \\void fn_char(char x);
3184 \\void fn_bool(bool x);
3185 \\void fn_ptr(void *x);
3186 \\
3187 \\void call() {
3188 \\ fn_int(3.0f);
3189 \\ fn_int(3.0);
3190 \\ fn_int('ABCD');
3191 \\ fn_f32(3);
3192 \\ fn_f64(3);
3193 \\ fn_char('3');
3194 \\ fn_char('\x1');
3195 \\ fn_char(0);
3196 \\ fn_f32(3.0f);
3197 \\ fn_f64(3.0);
3198 \\ fn_bool(123);
3199 \\ fn_bool(0);
3200 \\ fn_bool(&fn_int);
3201 \\ fn_int((int)&fn_int);
3202 \\ fn_ptr((void *)42);
3203 \\}
3204 , &[_][]const u8{
3205 \\pub extern fn fn_int(x: c_int) void;
3206 \\pub extern fn fn_f32(x: f32) void;
3207 \\pub extern fn fn_f64(x: f64) void;
3208 \\pub extern fn fn_char(x: u8) void;
3209 \\pub extern fn fn_bool(x: bool) void;
3210 \\pub extern fn fn_ptr(x: ?*anyopaque) void;
3211 \\pub export fn call() void {
3212 \\ fn_int(@floatToInt(c_int, 3.0));
3213 \\ fn_int(@floatToInt(c_int, 3.0));
3214 \\ fn_int(@as(c_int, 1094861636));
3215 \\ fn_f32(@intToFloat(f32, @as(c_int, 3)));
3216 \\ fn_f64(@intToFloat(f64, @as(c_int, 3)));
3217 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));
3218 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));
3219 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));
3220 \\ fn_f32(3.0);
3221 \\ fn_f64(3.0);
3222 \\ fn_bool(@as(c_int, 123) != 0);
3223 \\ fn_bool(@as(c_int, 0) != 0);
3224 \\ fn_bool(@ptrToInt(&fn_int) != 0);
3225 \\ fn_int(@intCast(c_int, @ptrToInt(&fn_int)));
3226 \\ fn_ptr(@intToPtr(?*anyopaque, @as(c_int, 42)));
3227 \\}
3228 });
32333229 }
32343230
32353231 if (builtin.zig_backend != .stage1) {