fix llvm 15 translate-c regressions

by inserting explicit casts.

closes #12682
This commit is contained in:
Andrew Kelley 2022-08-30 19:11:31 -07:00
parent fb81ba8762
commit ea4ef832e3
2 changed files with 65 additions and 74 deletions

View File

@ -1868,21 +1868,16 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\}
, "");
// Regressed with LLVM 15:
// https://github.com/ziglang/zig/issues/12682
if (false) {
// The C standard does not require function pointers to be convertible to any integer type.
// However, POSIX requires that function pointers have the same representation as `void *`
// so that dlsym() can work
cases.add("Function to integral",
\\#include <stdint.h>
\\int main(void) {
\\#if defined(__UINTPTR_MAX__) && __has_include(<unistd.h>)
\\ uintptr_t x = main;
\\ x = (uintptr_t)main;
\\#endif
\\ return 0;
\\}
, "");
}
// The C standard does not require function pointers to be convertible to any integer type.
// However, POSIX requires that function pointers have the same representation as `void *`
// so that dlsym() can work
cases.add("Function to integral",
\\#include <stdint.h>
\\int main(void) {
\\#if defined(__UINTPTR_MAX__) && __has_include(<unistd.h>)
\\ uintptr_t x = (uintptr_t)main;
\\#endif
\\ return 0;
\\}
, "");
}

View File

@ -3173,63 +3173,59 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\}
});
// Regressed with LLVM 15:
// https://github.com/ziglang/zig/issues/12682
if (false) {
if (builtin.zig_backend != .stage1) {
cases.add("implicit casts",
\\#include <stdbool.h>
\\
\\void fn_int(int x);
\\void fn_f32(float x);
\\void fn_f64(double x);
\\void fn_char(char x);
\\void fn_bool(bool x);
\\void fn_ptr(void *x);
\\
\\void call() {
\\ fn_int(3.0f);
\\ fn_int(3.0);
\\ fn_int('ABCD');
\\ fn_f32(3);
\\ fn_f64(3);
\\ fn_char('3');
\\ fn_char('\x1');
\\ fn_char(0);
\\ fn_f32(3.0f);
\\ fn_f64(3.0);
\\ fn_bool(123);
\\ fn_bool(0);
\\ fn_bool(&fn_int);
\\ fn_int(&fn_int);
\\ fn_ptr(42);
\\}
, &[_][]const u8{
\\pub extern fn fn_int(x: c_int) void;
\\pub extern fn fn_f32(x: f32) void;
\\pub extern fn fn_f64(x: f64) void;
\\pub extern fn fn_char(x: u8) void;
\\pub extern fn fn_bool(x: bool) void;
\\pub extern fn fn_ptr(x: ?*anyopaque) void;
\\pub export fn call() void {
\\ fn_int(@floatToInt(c_int, 3.0));
\\ fn_int(@floatToInt(c_int, 3.0));
\\ fn_int(@as(c_int, 1094861636));
\\ fn_f32(@intToFloat(f32, @as(c_int, 3)));
\\ fn_f64(@intToFloat(f64, @as(c_int, 3)));
\\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));
\\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));
\\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));
\\ fn_f32(3.0);
\\ fn_f64(3.0);
\\ fn_bool(@as(c_int, 123) != 0);
\\ fn_bool(@as(c_int, 0) != 0);
\\ fn_bool(@ptrToInt(&fn_int) != 0);
\\ fn_int(@intCast(c_int, @ptrToInt(&fn_int)));
\\ fn_ptr(@intToPtr(?*anyopaque, @as(c_int, 42)));
\\}
});
}
if (builtin.zig_backend != .stage1) {
cases.add("implicit casts",
\\#include <stdbool.h>
\\
\\void fn_int(int x);
\\void fn_f32(float x);
\\void fn_f64(double x);
\\void fn_char(char x);
\\void fn_bool(bool x);
\\void fn_ptr(void *x);
\\
\\void call() {
\\ fn_int(3.0f);
\\ fn_int(3.0);
\\ fn_int('ABCD');
\\ fn_f32(3);
\\ fn_f64(3);
\\ fn_char('3');
\\ fn_char('\x1');
\\ fn_char(0);
\\ fn_f32(3.0f);
\\ fn_f64(3.0);
\\ fn_bool(123);
\\ fn_bool(0);
\\ fn_bool(&fn_int);
\\ fn_int((int)&fn_int);
\\ fn_ptr((void *)42);
\\}
, &[_][]const u8{
\\pub extern fn fn_int(x: c_int) void;
\\pub extern fn fn_f32(x: f32) void;
\\pub extern fn fn_f64(x: f64) void;
\\pub extern fn fn_char(x: u8) void;
\\pub extern fn fn_bool(x: bool) void;
\\pub extern fn fn_ptr(x: ?*anyopaque) void;
\\pub export fn call() void {
\\ fn_int(@floatToInt(c_int, 3.0));
\\ fn_int(@floatToInt(c_int, 3.0));
\\ fn_int(@as(c_int, 1094861636));
\\ fn_f32(@intToFloat(f32, @as(c_int, 3)));
\\ fn_f64(@intToFloat(f64, @as(c_int, 3)));
\\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));
\\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));
\\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));
\\ fn_f32(3.0);
\\ fn_f64(3.0);
\\ fn_bool(@as(c_int, 123) != 0);
\\ fn_bool(@as(c_int, 0) != 0);
\\ fn_bool(@ptrToInt(&fn_int) != 0);
\\ fn_int(@intCast(c_int, @ptrToInt(&fn_int)));
\\ fn_ptr(@intToPtr(?*anyopaque, @as(c_int, 42)));
\\}
});
}
if (builtin.zig_backend != .stage1) {