authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-21 22:49:58-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-21 22:51:17-07:00
log046a4d084bbeaaaf5bfe99ee9179a43f89977369
tree129f313c52150191b3c15522fcb3cbdf87dc4121
parent31149783894e294322e25b2821fa41b22d8988c5

update test-translate-c cases to stage2


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

test/translate_c.zig+240-218
......@@ -549,25 +549,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
549549 \\};
550550 });
551551
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 = ?fn () callconv(.C) void;
563 \\pub const fnptr_attr_ty = ?fn () callconv(.C) void;
564 \\pub const struct_foo = extern struct {
565 \\ foo: ?fn () callconv(.C) void,
566 \\ bar: ?fn () callconv(.C) void,
567 \\ baz: fnptr_ty,
568 \\ qux: fnptr_attr_ty,
569 \\};
570 });
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 }
571573
572574 cases.add("function prototype with parenthesis",
573575 \\void (f0) (void *L);
......@@ -895,20 +897,22 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
895897 \\pub const baz = c_int;
896898 });
897899
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: ?fn () callconv(.C) void = @intToPtr(?fn () callconv(.C) void, @intCast(c_ulong, @ptrToInt(func_ptr)));
909 \\ _ = typed_func_ptr;
910 \\}
911 });
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 \\ _ = typed_func_ptr;
913 \\}
914 });
915 }
912916
913917 cases.add("noreturn attribute",
914918 \\void foo(void) __attribute__((noreturn));
......@@ -968,19 +972,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
968972 \\}
969973 });
970974
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: ?fn () callconv(.C) void,
981 \\ callback_http: ?lws_callback_function,
982 \\};
983 });
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 }
984990
985991 cases.add("pointer to struct demoted to opaque due to bit fields",
986992 \\struct Foo {
......@@ -1051,17 +1057,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
10511057 \\pub const Foo = struct_Foo;
10521058 });
10531059
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: ?fn ([*c]struct_Foo) callconv(.C) void,
1061 \\};
1062 ,
1063 \\pub const Foo = struct_Foo;
1064 });
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 }
10651073
10661074 cases.add("struct prototype used in func",
10671075 \\struct Foo;
......@@ -1327,11 +1335,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13271335 \\pub extern fn func(array: [*c]c_int) void;
13281336 });
13291337
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: ?fn () callconv(.C) void) void;
1334 });
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 }
13351345
13361346 cases.add("void cast",
13371347 \\void foo() {
......@@ -1674,13 +1684,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
16741684 \\pub extern var my_enum: enum_enum_ty;
16751685 });
16761686
1677 cases.add("Parameterless function pointers",
1678 \\typedef void (*fn0)();
1679 \\typedef void (*fn1)(char);
1680 , &[_][]const u8{
1681 \\pub const fn0 = ?fn (...) callconv(.C) void;
1682 \\pub const fn1 = ?fn (u8) callconv(.C) void;
1683 });
1687 if (builtin.zig_backend != .stage1) {
1688 cases.add("Parameterless function pointers",
1689 \\typedef void (*fn0)();
1690 \\typedef void (*fn1)(char);
1691 , &[_][]const u8{
1692 \\pub const fn0 = ?*const fn (...) callconv(.C) void;
1693 \\pub const fn1 = ?*const fn (u8) callconv(.C) void;
1694 });
1695 }
16841696
16851697 cases.addWithTarget("Calling convention", .{
16861698 .cpu_arch = .i386,
......@@ -1880,60 +1892,64 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
18801892 \\pub const SDL_INIT_VIDEO = @as(c_ulonglong, 0x00000020);
18811893 });
18821894
1883 cases.add("generate inline func for #define global extern fn",
1884 \\extern void (*fn_ptr)(void);
1885 \\#define foo fn_ptr
1886 \\
1887 \\extern char (*fn_ptr2)(int, float);
1888 \\#define bar fn_ptr2
1889 , &[_][]const u8{
1890 \\pub extern var fn_ptr: ?fn () callconv(.C) void;
1891 ,
1892 \\pub inline fn foo() void {
1893 \\ return fn_ptr.?();
1894 \\}
1895 ,
1896 \\pub extern var fn_ptr2: ?fn (c_int, f32) callconv(.C) u8;
1897 ,
1898 \\pub inline fn bar(arg_1: c_int, arg_2: f32) u8 {
1899 \\ return fn_ptr2.?(arg_1, arg_2);
1900 \\}
1901 });
1895 if (builtin.zig_backend != .stage1) {
1896 cases.add("generate inline func for #define global extern fn",
1897 \\extern void (*fn_ptr)(void);
1898 \\#define foo fn_ptr
1899 \\
1900 \\extern char (*fn_ptr2)(int, float);
1901 \\#define bar fn_ptr2
1902 , &[_][]const u8{
1903 \\pub extern var fn_ptr: ?*const fn () callconv(.C) void;
1904 ,
1905 \\pub inline fn foo() void {
1906 \\ return fn_ptr.?();
1907 \\}
1908 ,
1909 \\pub extern var fn_ptr2: ?*const fn (c_int, f32) callconv(.C) u8;
1910 ,
1911 \\pub inline fn bar(arg_1: c_int, arg_2: f32) u8 {
1912 \\ return fn_ptr2.?(arg_1, arg_2);
1913 \\}
1914 });
1915 }
19021916
1903 cases.add("macros with field targets",
1904 \\typedef unsigned int GLbitfield;
1905 \\typedef void (*PFNGLCLEARPROC) (GLbitfield mask);
1906 \\typedef void(*OpenGLProc)(void);
1907 \\union OpenGLProcs {
1908 \\ OpenGLProc ptr[1];
1909 \\ struct {
1910 \\ PFNGLCLEARPROC Clear;
1911 \\ } gl;
1912 \\};
1913 \\extern union OpenGLProcs glProcs;
1914 \\#define glClearUnion glProcs.gl.Clear
1915 \\#define glClearPFN PFNGLCLEARPROC
1916 , &[_][]const u8{
1917 \\pub const GLbitfield = c_uint;
1918 \\pub const PFNGLCLEARPROC = ?fn (GLbitfield) callconv(.C) void;
1919 \\pub const OpenGLProc = ?fn () callconv(.C) void;
1920 \\const struct_unnamed_1 = extern struct {
1921 \\ Clear: PFNGLCLEARPROC,
1922 \\};
1923 \\pub const union_OpenGLProcs = extern union {
1924 \\ ptr: [1]OpenGLProc,
1925 \\ gl: struct_unnamed_1,
1926 \\};
1927 \\pub extern var glProcs: union_OpenGLProcs;
1928 ,
1929 \\pub const glClearPFN = PFNGLCLEARPROC;
1930 ,
1931 \\pub inline fn glClearUnion(arg_2: GLbitfield) void {
1932 \\ return glProcs.gl.Clear.?(arg_2);
1933 \\}
1934 ,
1935 \\pub const OpenGLProcs = union_OpenGLProcs;
1936 });
1917 if (builtin.zig_backend != .stage1) {
1918 cases.add("macros with field targets",
1919 \\typedef unsigned int GLbitfield;
1920 \\typedef void (*PFNGLCLEARPROC) (GLbitfield mask);
1921 \\typedef void(*OpenGLProc)(void);
1922 \\union OpenGLProcs {
1923 \\ OpenGLProc ptr[1];
1924 \\ struct {
1925 \\ PFNGLCLEARPROC Clear;
1926 \\ } gl;
1927 \\};
1928 \\extern union OpenGLProcs glProcs;
1929 \\#define glClearUnion glProcs.gl.Clear
1930 \\#define glClearPFN PFNGLCLEARPROC
1931 , &[_][]const u8{
1932 \\pub const GLbitfield = c_uint;
1933 \\pub const PFNGLCLEARPROC = ?*const fn (GLbitfield) callconv(.C) void;
1934 \\pub const OpenGLProc = ?*const fn () callconv(.C) void;
1935 \\const struct_unnamed_1 = extern struct {
1936 \\ Clear: PFNGLCLEARPROC,
1937 \\};
1938 \\pub const union_OpenGLProcs = extern union {
1939 \\ ptr: [1]OpenGLProc,
1940 \\ gl: struct_unnamed_1,
1941 \\};
1942 \\pub extern var glProcs: union_OpenGLProcs;
1943 ,
1944 \\pub const glClearPFN = PFNGLCLEARPROC;
1945 ,
1946 \\pub inline fn glClearUnion(arg_2: GLbitfield) void {
1947 \\ return glProcs.gl.Clear.?(arg_2);
1948 \\}
1949 ,
1950 \\pub const OpenGLProcs = union_OpenGLProcs;
1951 });
1952 }
19371953
19381954 cases.add("macro pointer cast",
19391955 \\#define NRF_GPIO_BASE 0
......@@ -2840,35 +2856,37 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
28402856 \\}
28412857 });
28422858
2843 cases.add("deref function pointer",
2844 \\void foo(void) {}
2845 \\int baz(void) { return 0; }
2846 \\void bar(void) {
2847 \\ void(*f)(void) = foo;
2848 \\ int(*b)(void) = baz;
2849 \\ f();
2850 \\ (*(f))();
2851 \\ foo();
2852 \\ b();
2853 \\ (*(b))();
2854 \\ baz();
2855 \\}
2856 , &[_][]const u8{
2857 \\pub export fn foo() void {}
2858 \\pub export fn baz() c_int {
2859 \\ return 0;
2860 \\}
2861 \\pub export fn bar() void {
2862 \\ var f: ?fn () callconv(.C) void = foo;
2863 \\ var b: ?fn () callconv(.C) c_int = baz;
2864 \\ f.?();
2865 \\ f.?();
2866 \\ foo();
2867 \\ _ = b.?();
2868 \\ _ = b.?();
2869 \\ _ = baz();
2870 \\}
2871 });
2859 if (builtin.zig_backend != .stage1) {
2860 cases.add("deref function pointer",
2861 \\void foo(void) {}
2862 \\int baz(void) { return 0; }
2863 \\void bar(void) {
2864 \\ void(*f)(void) = foo;
2865 \\ int(*b)(void) = baz;
2866 \\ f();
2867 \\ (*(f))();
2868 \\ foo();
2869 \\ b();
2870 \\ (*(b))();
2871 \\ baz();
2872 \\}
2873 , &[_][]const u8{
2874 \\pub export fn foo() void {}
2875 \\pub export fn baz() c_int {
2876 \\ return 0;
2877 \\}
2878 \\pub export fn bar() void {
2879 \\ var f: ?*const fn () callconv(.C) void = foo;
2880 \\ var b: ?*const fn () callconv(.C) c_int = baz;
2881 \\ f.?();
2882 \\ f.?();
2883 \\ foo();
2884 \\ _ = b.?();
2885 \\ _ = b.?();
2886 \\ _ = baz();
2887 \\}
2888 });
2889 }
28722890
28732891 cases.add("pre increment/decrement",
28742892 \\void foo(void) {
......@@ -3143,73 +3161,77 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31433161 \\}
31443162 });
31453163
3146 cases.add("implicit casts",
3147 \\#include <stdbool.h>
3148 \\
3149 \\void fn_int(int x);
3150 \\void fn_f32(float x);
3151 \\void fn_f64(double x);
3152 \\void fn_char(char x);
3153 \\void fn_bool(bool x);
3154 \\void fn_ptr(void *x);
3155 \\
3156 \\void call() {
3157 \\ fn_int(3.0f);
3158 \\ fn_int(3.0);
3159 \\ fn_int('ABCD');
3160 \\ fn_f32(3);
3161 \\ fn_f64(3);
3162 \\ fn_char('3');
3163 \\ fn_char('\x1');
3164 \\ fn_char(0);
3165 \\ fn_f32(3.0f);
3166 \\ fn_f64(3.0);
3167 \\ fn_bool(123);
3168 \\ fn_bool(0);
3169 \\ fn_bool(&fn_int);
3170 \\ fn_int(&fn_int);
3171 \\ fn_ptr(42);
3172 \\}
3173 , &[_][]const u8{
3174 \\pub extern fn fn_int(x: c_int) void;
3175 \\pub extern fn fn_f32(x: f32) void;
3176 \\pub extern fn fn_f64(x: f64) void;
3177 \\pub extern fn fn_char(x: u8) void;
3178 \\pub extern fn fn_bool(x: bool) void;
3179 \\pub extern fn fn_ptr(x: ?*anyopaque) void;
3180 \\pub export fn call() void {
3181 \\ fn_int(@floatToInt(c_int, 3.0));
3182 \\ fn_int(@floatToInt(c_int, 3.0));
3183 \\ fn_int(@as(c_int, 1094861636));
3184 \\ fn_f32(@intToFloat(f32, @as(c_int, 3)));
3185 \\ fn_f64(@intToFloat(f64, @as(c_int, 3)));
3186 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));
3187 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));
3188 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));
3189 \\ fn_f32(3.0);
3190 \\ fn_f64(3.0);
3191 \\ fn_bool(@as(c_int, 123) != 0);
3192 \\ fn_bool(@as(c_int, 0) != 0);
3193 \\ fn_bool(@ptrToInt(fn_int) != 0);
3194 \\ fn_int(@intCast(c_int, @ptrToInt(fn_int)));
3195 \\ fn_ptr(@intToPtr(?*anyopaque, @as(c_int, 42)));
3196 \\}
3197 });
3198
3199 cases.add("function call",
3200 \\static void bar(void) { }
3201 \\void foo(int *(baz)(void)) {
3202 \\ bar();
3203 \\ baz();
3204 \\}
3205 , &[_][]const u8{
3206 \\pub fn bar() callconv(.C) void {}
3207 \\pub export fn foo(arg_baz: ?fn () callconv(.C) [*c]c_int) void {
3208 \\ var baz = arg_baz;
3209 \\ bar();
3210 \\ _ = baz.?();
3211 \\}
3212 });
3164 if (builtin.zig_backend != .stage1) {
3165 cases.add("implicit casts",
3166 \\#include <stdbool.h>
3167 \\
3168 \\void fn_int(int x);
3169 \\void fn_f32(float x);
3170 \\void fn_f64(double x);
3171 \\void fn_char(char x);
3172 \\void fn_bool(bool x);
3173 \\void fn_ptr(void *x);
3174 \\
3175 \\void call() {
3176 \\ fn_int(3.0f);
3177 \\ fn_int(3.0);
3178 \\ fn_int('ABCD');
3179 \\ fn_f32(3);
3180 \\ fn_f64(3);
3181 \\ fn_char('3');
3182 \\ fn_char('\x1');
3183 \\ fn_char(0);
3184 \\ fn_f32(3.0f);
3185 \\ fn_f64(3.0);
3186 \\ fn_bool(123);
3187 \\ fn_bool(0);
3188 \\ fn_bool(&fn_int);
3189 \\ fn_int(&fn_int);
3190 \\ fn_ptr(42);
3191 \\}
3192 , &[_][]const u8{
3193 \\pub extern fn fn_int(x: c_int) void;
3194 \\pub extern fn fn_f32(x: f32) void;
3195 \\pub extern fn fn_f64(x: f64) void;
3196 \\pub extern fn fn_char(x: u8) void;
3197 \\pub extern fn fn_bool(x: bool) void;
3198 \\pub extern fn fn_ptr(x: ?*anyopaque) void;
3199 \\pub export fn call() void {
3200 \\ fn_int(@floatToInt(c_int, 3.0));
3201 \\ fn_int(@floatToInt(c_int, 3.0));
3202 \\ fn_int(@as(c_int, 1094861636));
3203 \\ fn_f32(@intToFloat(f32, @as(c_int, 3)));
3204 \\ fn_f64(@intToFloat(f64, @as(c_int, 3)));
3205 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));
3206 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));
3207 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));
3208 \\ fn_f32(3.0);
3209 \\ fn_f64(3.0);
3210 \\ fn_bool(@as(c_int, 123) != 0);
3211 \\ fn_bool(@as(c_int, 0) != 0);
3212 \\ fn_bool(@ptrToInt(&fn_int) != 0);
3213 \\ fn_int(@intCast(c_int, @ptrToInt(&fn_int)));
3214 \\ fn_ptr(@intToPtr(?*anyopaque, @as(c_int, 42)));
3215 \\}
3216 });
3217 }
3218
3219 if (builtin.zig_backend != .stage1) {
3220 cases.add("function call",
3221 \\static void bar(void) { }
3222 \\void foo(int *(baz)(void)) {
3223 \\ bar();
3224 \\ baz();
3225 \\}
3226 , &[_][]const u8{
3227 \\pub fn bar() callconv(.C) void {}
3228 \\pub export fn foo(arg_baz: ?*const fn () callconv(.C) [*c]c_int) void {
3229 \\ var baz = arg_baz;
3230 \\ bar();
3231 \\ _ = baz.?();
3232 \\}
3233 });
3234 }
32133235
32143236 cases.add("macro defines string literal with octal",
32153237 \\#define FOO "aoeu\023 derp"