authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-05 13:36:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-06 12:15:05-07:00
log57995c210064e954913d1f4ee16f315e78aa1a46
treef77adff4a46a6eb028afb3c44eab77a7392e1251
parentec1334d2d41c3e1ca57ad1c3a213d4cc8fc8dd5e

translate-c: remove stage1 conditions from tests


1 files changed, 218 insertions(+), 240 deletions(-)

test/translate_c.zig+218-240
......@@ -549,27 +549,25 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
549549 \\};
550550 });
551551
552 if (builtin.zig_backend != .stage1) {
553 cases.add("function prototype translated as optional",
554 \\typedef void (*fnptr_ty)(void);
555 \\typedef __attribute__((cdecl)) void (*fnptr_attr_ty)(void);
556 \\struct foo {
557 \\ __attribute__((cdecl)) void (*foo)(void);
558 \\ void (*bar)(void);
559 \\ fnptr_ty baz;
560 \\ fnptr_attr_ty qux;
561 \\};
562 , &[_][]const u8{
563 \\pub const fnptr_ty = ?*const fn () callconv(.C) void;
564 \\pub const fnptr_attr_ty = ?*const fn () callconv(.C) void;
565 \\pub const struct_foo = extern struct {
566 \\ foo: ?*const fn () callconv(.C) void,
567 \\ bar: ?*const fn () callconv(.C) void,
568 \\ baz: fnptr_ty,
569 \\ qux: fnptr_attr_ty,
570 \\};
571 });
572 }
552 cases.add("function prototype translated as optional",
553 \\typedef void (*fnptr_ty)(void);
554 \\typedef __attribute__((cdecl)) void (*fnptr_attr_ty)(void);
555 \\struct foo {
556 \\ __attribute__((cdecl)) void (*foo)(void);
557 \\ void (*bar)(void);
558 \\ fnptr_ty baz;
559 \\ fnptr_attr_ty qux;
560 \\};
561 , &[_][]const u8{
562 \\pub const fnptr_ty = ?*const fn () callconv(.C) void;
563 \\pub const fnptr_attr_ty = ?*const fn () callconv(.C) void;
564 \\pub const struct_foo = extern struct {
565 \\ foo: ?*const fn () callconv(.C) void,
566 \\ bar: ?*const fn () callconv(.C) void,
567 \\ baz: fnptr_ty,
568 \\ qux: fnptr_attr_ty,
569 \\};
570 });
573571
574572 cases.add("function prototype with parenthesis",
575573 \\void (f0) (void *L);
......@@ -897,22 +895,20 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
897895 \\pub const baz = c_int;
898896 });
899897
900 if (builtin.zig_backend != .stage1) {
901 cases.add("casting pointers to ints and ints to pointers",
902 \\void foo(void);
903 \\void bar(void) {
904 \\ void *func_ptr = foo;
905 \\ void (*typed_func_ptr)(void) = (void (*)(void)) (unsigned long) func_ptr;
906 \\}
907 , &[_][]const u8{
908 \\pub extern fn foo() void;
909 \\pub export fn bar() void {
910 \\ var func_ptr: ?*anyopaque = @ptrCast(?*anyopaque, &foo);
911 \\ var typed_func_ptr: ?*const fn () callconv(.C) void = @intToPtr(?*const fn () callconv(.C) void, @intCast(c_ulong, @ptrToInt(func_ptr)));
912 \\ _ = @TypeOf(typed_func_ptr);
913 \\}
914 });
915 }
898 cases.add("casting pointers to ints and ints to pointers",
899 \\void foo(void);
900 \\void bar(void) {
901 \\ void *func_ptr = foo;
902 \\ void (*typed_func_ptr)(void) = (void (*)(void)) (unsigned long) func_ptr;
903 \\}
904 , &[_][]const u8{
905 \\pub extern fn foo() void;
906 \\pub export fn bar() void {
907 \\ var func_ptr: ?*anyopaque = @ptrCast(?*anyopaque, &foo);
908 \\ var typed_func_ptr: ?*const fn () callconv(.C) void = @intToPtr(?*const fn () callconv(.C) void, @intCast(c_ulong, @ptrToInt(func_ptr)));
909 \\ _ = @TypeOf(typed_func_ptr);
910 \\}
911 });
916912
917913 cases.add("noreturn attribute",
918914 \\void foo(void) __attribute__((noreturn));
......@@ -972,21 +968,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
972968 \\}
973969 });
974970
975 if (builtin.zig_backend != .stage1) {
976 cases.add("typedef of function in struct field",
977 \\typedef void lws_callback_function(void);
978 \\struct Foo {
979 \\ void (*func)(void);
980 \\ lws_callback_function *callback_http;
981 \\};
982 , &[_][]const u8{
983 \\pub const lws_callback_function = fn () callconv(.C) void;
984 \\pub const struct_Foo = extern struct {
985 \\ func: ?*const fn () callconv(.C) void,
986 \\ callback_http: ?*const lws_callback_function,
987 \\};
988 });
989 }
971 cases.add("typedef of function in struct field",
972 \\typedef void lws_callback_function(void);
973 \\struct Foo {
974 \\ void (*func)(void);
975 \\ lws_callback_function *callback_http;
976 \\};
977 , &[_][]const u8{
978 \\pub const lws_callback_function = fn () callconv(.C) void;
979 \\pub const struct_Foo = extern struct {
980 \\ func: ?*const fn () callconv(.C) void,
981 \\ callback_http: ?*const lws_callback_function,
982 \\};
983 });
990984
991985 cases.add("pointer to struct demoted to opaque due to bit fields",
992986 \\struct Foo {
......@@ -1057,19 +1051,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
10571051 \\pub const Foo = struct_Foo;
10581052 });
10591053
1060 if (builtin.zig_backend != .stage1) {
1061 cases.add("self referential struct with function pointer",
1062 \\struct Foo {
1063 \\ void (*derp)(struct Foo *foo);
1064 \\};
1065 , &[_][]const u8{
1066 \\pub const struct_Foo = extern struct {
1067 \\ derp: ?*const fn ([*c]struct_Foo) callconv(.C) void,
1068 \\};
1069 ,
1070 \\pub const Foo = struct_Foo;
1071 });
1072 }
1054 cases.add("self referential struct with function pointer",
1055 \\struct Foo {
1056 \\ void (*derp)(struct Foo *foo);
1057 \\};
1058 , &[_][]const u8{
1059 \\pub const struct_Foo = extern struct {
1060 \\ derp: ?*const fn ([*c]struct_Foo) callconv(.C) void,
1061 \\};
1062 ,
1063 \\pub const Foo = struct_Foo;
1064 });
10731065
10741066 cases.add("struct prototype used in func",
10751067 \\struct Foo;
......@@ -1335,13 +1327,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13351327 \\pub extern fn func(array: [*c]c_int) void;
13361328 });
13371329
1338 if (builtin.zig_backend != .stage1) {
1339 cases.add("__cdecl doesn't mess up function pointers",
1340 \\void foo(void (__cdecl *fn_ptr)(void));
1341 , &[_][]const u8{
1342 \\pub extern fn foo(fn_ptr: ?*const fn () callconv(.C) void) void;
1343 });
1344 }
1330 cases.add("__cdecl doesn't mess up function pointers",
1331 \\void foo(void (__cdecl *fn_ptr)(void));
1332 , &[_][]const u8{
1333 \\pub extern fn foo(fn_ptr: ?*const fn () callconv(.C) void) void;
1334 });
13451335
13461336 cases.add("void cast",
13471337 \\void foo() {
......@@ -1764,15 +1754,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
17641754 \\pub extern var my_enum: enum_enum_ty;
17651755 });
17661756
1767 if (builtin.zig_backend != .stage1) {
1768 cases.add("Parameterless function pointers",
1769 \\typedef void (*fn0)();
1770 \\typedef void (*fn1)(char);
1771 , &[_][]const u8{
1772 \\pub const fn0 = ?*const fn (...) callconv(.C) void;
1773 \\pub const fn1 = ?*const fn (u8) callconv(.C) void;
1774 });
1775 }
1757 cases.add("Parameterless function pointers",
1758 \\typedef void (*fn0)();
1759 \\typedef void (*fn1)(char);
1760 , &[_][]const u8{
1761 \\pub const fn0 = ?*const fn (...) callconv(.C) void;
1762 \\pub const fn1 = ?*const fn (u8) callconv(.C) void;
1763 });
17761764
17771765 cases.addWithTarget("Calling convention", .{
17781766 .cpu_arch = .x86,
......@@ -1972,64 +1960,60 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
19721960 \\pub const SDL_INIT_VIDEO = @as(c_ulonglong, 0x00000020);
19731961 });
19741962
1975 if (builtin.zig_backend != .stage1) {
1976 cases.add("generate inline func for #define global extern fn",
1977 \\extern void (*fn_ptr)(void);
1978 \\#define foo fn_ptr
1979 \\
1980 \\extern char (*fn_ptr2)(int, float);
1981 \\#define bar fn_ptr2
1982 , &[_][]const u8{
1983 \\pub extern var fn_ptr: ?*const fn () callconv(.C) void;
1984 ,
1985 \\pub inline fn foo() void {
1986 \\ return fn_ptr.?();
1987 \\}
1988 ,
1989 \\pub extern var fn_ptr2: ?*const fn (c_int, f32) callconv(.C) u8;
1990 ,
1991 \\pub inline fn bar(arg_1: c_int, arg_2: f32) u8 {
1992 \\ return fn_ptr2.?(arg_1, arg_2);
1993 \\}
1994 });
1995 }
1963 cases.add("generate inline func for #define global extern fn",
1964 \\extern void (*fn_ptr)(void);
1965 \\#define foo fn_ptr
1966 \\
1967 \\extern char (*fn_ptr2)(int, float);
1968 \\#define bar fn_ptr2
1969 , &[_][]const u8{
1970 \\pub extern var fn_ptr: ?*const fn () callconv(.C) void;
1971 ,
1972 \\pub inline fn foo() void {
1973 \\ return fn_ptr.?();
1974 \\}
1975 ,
1976 \\pub extern var fn_ptr2: ?*const fn (c_int, f32) callconv(.C) u8;
1977 ,
1978 \\pub inline fn bar(arg_1: c_int, arg_2: f32) u8 {
1979 \\ return fn_ptr2.?(arg_1, arg_2);
1980 \\}
1981 });
19961982
1997 if (builtin.zig_backend != .stage1) {
1998 cases.add("macros with field targets",
1999 \\typedef unsigned int GLbitfield;
2000 \\typedef void (*PFNGLCLEARPROC) (GLbitfield mask);
2001 \\typedef void(*OpenGLProc)(void);
2002 \\union OpenGLProcs {
2003 \\ OpenGLProc ptr[1];
2004 \\ struct {
2005 \\ PFNGLCLEARPROC Clear;
2006 \\ } gl;
2007 \\};
2008 \\extern union OpenGLProcs glProcs;
2009 \\#define glClearUnion glProcs.gl.Clear
2010 \\#define glClearPFN PFNGLCLEARPROC
2011 , &[_][]const u8{
2012 \\pub const GLbitfield = c_uint;
2013 \\pub const PFNGLCLEARPROC = ?*const fn (GLbitfield) callconv(.C) void;
2014 \\pub const OpenGLProc = ?*const fn () callconv(.C) void;
2015 \\const struct_unnamed_1 = extern struct {
2016 \\ Clear: PFNGLCLEARPROC,
2017 \\};
2018 \\pub const union_OpenGLProcs = extern union {
2019 \\ ptr: [1]OpenGLProc,
2020 \\ gl: struct_unnamed_1,
2021 \\};
2022 \\pub extern var glProcs: union_OpenGLProcs;
2023 ,
2024 \\pub const glClearPFN = PFNGLCLEARPROC;
2025 ,
2026 \\pub inline fn glClearUnion(arg_2: GLbitfield) void {
2027 \\ return glProcs.gl.Clear.?(arg_2);
2028 \\}
2029 ,
2030 \\pub const OpenGLProcs = union_OpenGLProcs;
2031 });
2032 }
1983 cases.add("macros with field targets",
1984 \\typedef unsigned int GLbitfield;
1985 \\typedef void (*PFNGLCLEARPROC) (GLbitfield mask);
1986 \\typedef void(*OpenGLProc)(void);
1987 \\union OpenGLProcs {
1988 \\ OpenGLProc ptr[1];
1989 \\ struct {
1990 \\ PFNGLCLEARPROC Clear;
1991 \\ } gl;
1992 \\};
1993 \\extern union OpenGLProcs glProcs;
1994 \\#define glClearUnion glProcs.gl.Clear
1995 \\#define glClearPFN PFNGLCLEARPROC
1996 , &[_][]const u8{
1997 \\pub const GLbitfield = c_uint;
1998 \\pub const PFNGLCLEARPROC = ?*const fn (GLbitfield) callconv(.C) void;
1999 \\pub const OpenGLProc = ?*const fn () callconv(.C) void;
2000 \\const struct_unnamed_1 = extern struct {
2001 \\ Clear: PFNGLCLEARPROC,
2002 \\};
2003 \\pub const union_OpenGLProcs = extern union {
2004 \\ ptr: [1]OpenGLProc,
2005 \\ gl: struct_unnamed_1,
2006 \\};
2007 \\pub extern var glProcs: union_OpenGLProcs;
2008 ,
2009 \\pub const glClearPFN = PFNGLCLEARPROC;
2010 ,
2011 \\pub inline fn glClearUnion(arg_2: GLbitfield) void {
2012 \\ return glProcs.gl.Clear.?(arg_2);
2013 \\}
2014 ,
2015 \\pub const OpenGLProcs = union_OpenGLProcs;
2016 });
20332017
20342018 cases.add("macro pointer cast",
20352019 \\#define NRF_GPIO_BASE 0
......@@ -2936,37 +2920,35 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
29362920 \\}
29372921 });
29382922
2939 if (builtin.zig_backend != .stage1) {
2940 cases.add("deref function pointer",
2941 \\void foo(void) {}
2942 \\int baz(void) { return 0; }
2943 \\void bar(void) {
2944 \\ void(*f)(void) = foo;
2945 \\ int(*b)(void) = baz;
2946 \\ f();
2947 \\ (*(f))();
2948 \\ foo();
2949 \\ b();
2950 \\ (*(b))();
2951 \\ baz();
2952 \\}
2953 , &[_][]const u8{
2954 \\pub export fn foo() void {}
2955 \\pub export fn baz() c_int {
2956 \\ return 0;
2957 \\}
2958 \\pub export fn bar() void {
2959 \\ var f: ?*const fn () callconv(.C) void = &foo;
2960 \\ var b: ?*const fn () callconv(.C) c_int = &baz;
2961 \\ f.?();
2962 \\ f.?();
2963 \\ foo();
2964 \\ _ = b.?();
2965 \\ _ = b.?();
2966 \\ _ = baz();
2967 \\}
2968 });
2969 }
2923 cases.add("deref function pointer",
2924 \\void foo(void) {}
2925 \\int baz(void) { return 0; }
2926 \\void bar(void) {
2927 \\ void(*f)(void) = foo;
2928 \\ int(*b)(void) = baz;
2929 \\ f();
2930 \\ (*(f))();
2931 \\ foo();
2932 \\ b();
2933 \\ (*(b))();
2934 \\ baz();
2935 \\}
2936 , &[_][]const u8{
2937 \\pub export fn foo() void {}
2938 \\pub export fn baz() c_int {
2939 \\ return 0;
2940 \\}
2941 \\pub export fn bar() void {
2942 \\ var f: ?*const fn () callconv(.C) void = &foo;
2943 \\ var b: ?*const fn () callconv(.C) c_int = &baz;
2944 \\ f.?();
2945 \\ f.?();
2946 \\ foo();
2947 \\ _ = b.?();
2948 \\ _ = b.?();
2949 \\ _ = baz();
2950 \\}
2951 });
29702952
29712953 cases.add("pre increment/decrement",
29722954 \\void foo(void) {
......@@ -3241,77 +3223,73 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
32413223 \\}
32423224 });
32433225
3244 if (builtin.zig_backend != .stage1) {
3245 cases.add("implicit casts",
3246 \\#include <stdbool.h>
3247 \\
3248 \\void fn_int(int x);
3249 \\void fn_f32(float x);
3250 \\void fn_f64(double x);
3251 \\void fn_char(char x);
3252 \\void fn_bool(bool x);
3253 \\void fn_ptr(void *x);
3254 \\
3255 \\void call() {
3256 \\ fn_int(3.0f);
3257 \\ fn_int(3.0);
3258 \\ fn_int('ABCD');
3259 \\ fn_f32(3);
3260 \\ fn_f64(3);
3261 \\ fn_char('3');
3262 \\ fn_char('\x1');
3263 \\ fn_char(0);
3264 \\ fn_f32(3.0f);
3265 \\ fn_f64(3.0);
3266 \\ fn_bool(123);
3267 \\ fn_bool(0);
3268 \\ fn_bool(&fn_int);
3269 \\ fn_int((int)&fn_int);
3270 \\ fn_ptr((void *)42);
3271 \\}
3272 , &[_][]const u8{
3273 \\pub extern fn fn_int(x: c_int) void;
3274 \\pub extern fn fn_f32(x: f32) void;
3275 \\pub extern fn fn_f64(x: f64) void;
3276 \\pub extern fn fn_char(x: u8) void;
3277 \\pub extern fn fn_bool(x: bool) void;
3278 \\pub extern fn fn_ptr(x: ?*anyopaque) void;
3279 \\pub export fn call() void {
3280 \\ fn_int(@floatToInt(c_int, 3.0));
3281 \\ fn_int(@floatToInt(c_int, 3.0));
3282 \\ fn_int(@as(c_int, 1094861636));
3283 \\ fn_f32(@intToFloat(f32, @as(c_int, 3)));
3284 \\ fn_f64(@intToFloat(f64, @as(c_int, 3)));
3285 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));
3286 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));
3287 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));
3288 \\ fn_f32(3.0);
3289 \\ fn_f64(3.0);
3290 \\ fn_bool(@as(c_int, 123) != 0);
3291 \\ fn_bool(@as(c_int, 0) != 0);
3292 \\ fn_bool(@ptrToInt(&fn_int) != 0);
3293 \\ fn_int(@intCast(c_int, @ptrToInt(&fn_int)));
3294 \\ fn_ptr(@intToPtr(?*anyopaque, @as(c_int, 42)));
3295 \\}
3296 });
3297 }
3298
3299 if (builtin.zig_backend != .stage1) {
3300 cases.add("function call",
3301 \\static void bar(void) { }
3302 \\void foo(int *(baz)(void)) {
3303 \\ bar();
3304 \\ baz();
3305 \\}
3306 , &[_][]const u8{
3307 \\pub fn bar() callconv(.C) void {}
3308 \\pub export fn foo(arg_baz: ?*const fn () callconv(.C) [*c]c_int) void {
3309 \\ var baz = arg_baz;
3310 \\ bar();
3311 \\ _ = baz.?();
3312 \\}
3313 });
3314 }
3226 cases.add("implicit casts",
3227 \\#include <stdbool.h>
3228 \\
3229 \\void fn_int(int x);
3230 \\void fn_f32(float x);
3231 \\void fn_f64(double x);
3232 \\void fn_char(char x);
3233 \\void fn_bool(bool x);
3234 \\void fn_ptr(void *x);
3235 \\
3236 \\void call() {
3237 \\ fn_int(3.0f);
3238 \\ fn_int(3.0);
3239 \\ fn_int('ABCD');
3240 \\ fn_f32(3);
3241 \\ fn_f64(3);
3242 \\ fn_char('3');
3243 \\ fn_char('\x1');
3244 \\ fn_char(0);
3245 \\ fn_f32(3.0f);
3246 \\ fn_f64(3.0);
3247 \\ fn_bool(123);
3248 \\ fn_bool(0);
3249 \\ fn_bool(&fn_int);
3250 \\ fn_int((int)&fn_int);
3251 \\ fn_ptr((void *)42);
3252 \\}
3253 , &[_][]const u8{
3254 \\pub extern fn fn_int(x: c_int) void;
3255 \\pub extern fn fn_f32(x: f32) void;
3256 \\pub extern fn fn_f64(x: f64) void;
3257 \\pub extern fn fn_char(x: u8) void;
3258 \\pub extern fn fn_bool(x: bool) void;
3259 \\pub extern fn fn_ptr(x: ?*anyopaque) void;
3260 \\pub export fn call() void {
3261 \\ fn_int(@floatToInt(c_int, 3.0));
3262 \\ fn_int(@floatToInt(c_int, 3.0));
3263 \\ fn_int(@as(c_int, 1094861636));
3264 \\ fn_f32(@intToFloat(f32, @as(c_int, 3)));
3265 \\ fn_f64(@intToFloat(f64, @as(c_int, 3)));
3266 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));
3267 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));
3268 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));
3269 \\ fn_f32(3.0);
3270 \\ fn_f64(3.0);
3271 \\ fn_bool(@as(c_int, 123) != 0);
3272 \\ fn_bool(@as(c_int, 0) != 0);
3273 \\ fn_bool(@ptrToInt(&fn_int) != 0);
3274 \\ fn_int(@intCast(c_int, @ptrToInt(&fn_int)));
3275 \\ fn_ptr(@intToPtr(?*anyopaque, @as(c_int, 42)));
3276 \\}
3277 });
3278
3279 cases.add("function call",
3280 \\static void bar(void) { }
3281 \\void foo(int *(baz)(void)) {
3282 \\ bar();
3283 \\ baz();
3284 \\}
3285 , &[_][]const u8{
3286 \\pub fn bar() callconv(.C) void {}
3287 \\pub export fn foo(arg_baz: ?*const fn () callconv(.C) [*c]c_int) void {
3288 \\ var baz = arg_baz;
3289 \\ bar();
3290 \\ _ = baz.?();
3291 \\}
3292 });
33153293
33163294 cases.add("macro defines string literal with octal",
33173295 \\#define FOO "aoeu\023 derp"