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 {...@@ -549,25 +549,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
549 \\};549 \\};
550 });550 });
551551
552 cases.add("function prototype translated as optional",552 if (builtin.zig_backend != .stage1) {
553 \\typedef void (*fnptr_ty)(void);553 cases.add("function prototype translated as optional",
554 \\typedef __attribute__((cdecl)) void (*fnptr_attr_ty)(void);554 \\typedef void (*fnptr_ty)(void);
555 \\struct foo {555 \\typedef __attribute__((cdecl)) void (*fnptr_attr_ty)(void);
556 \\ __attribute__((cdecl)) void (*foo)(void);556 \\struct foo {
557 \\ void (*bar)(void);557 \\ __attribute__((cdecl)) void (*foo)(void);
558 \\ fnptr_ty baz;558 \\ void (*bar)(void);
559 \\ fnptr_attr_ty qux;559 \\ fnptr_ty baz;
560 \\};560 \\ fnptr_attr_ty qux;
561 , &[_][]const u8{561 \\};
562 \\pub const fnptr_ty = ?fn () callconv(.C) void;562 , &[_][]const u8{
563 \\pub const fnptr_attr_ty = ?fn () callconv(.C) void;563 \\pub const fnptr_ty = ?*const fn () callconv(.C) void;
564 \\pub const struct_foo = extern struct {564 \\pub const fnptr_attr_ty = ?*const fn () callconv(.C) void;
565 \\ foo: ?fn () callconv(.C) void,565 \\pub const struct_foo = extern struct {
566 \\ bar: ?fn () callconv(.C) void,566 \\ foo: ?*const fn () callconv(.C) void,
567 \\ baz: fnptr_ty,567 \\ bar: ?*const fn () callconv(.C) void,
568 \\ qux: fnptr_attr_ty,568 \\ baz: fnptr_ty,
569 \\};569 \\ qux: fnptr_attr_ty,
570 });570 \\};
571 });
572 }
571573
572 cases.add("function prototype with parenthesis",574 cases.add("function prototype with parenthesis",
573 \\void (f0) (void *L);575 \\void (f0) (void *L);
...@@ -895,20 +897,22 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -895,20 +897,22 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
895 \\pub const baz = c_int;897 \\pub const baz = c_int;
896 });898 });
897899
898 cases.add("casting pointers to ints and ints to pointers",900 if (builtin.zig_backend != .stage1) {
899 \\void foo(void);901 cases.add("casting pointers to ints and ints to pointers",
900 \\void bar(void) {902 \\void foo(void);
901 \\ void *func_ptr = foo;903 \\void bar(void) {
902 \\ void (*typed_func_ptr)(void) = (void (*)(void)) (unsigned long) func_ptr;904 \\ void *func_ptr = foo;
903 \\}905 \\ void (*typed_func_ptr)(void) = (void (*)(void)) (unsigned long) func_ptr;
904 , &[_][]const u8{906 \\}
905 \\pub extern fn foo() void;907 , &[_][]const u8{
906 \\pub export fn bar() void {908 \\pub extern fn foo() void;
907 \\ var func_ptr: ?*anyopaque = @ptrCast(?*anyopaque, foo);909 \\pub export fn bar() void {
908 \\ var typed_func_ptr: ?fn () callconv(.C) void = @intToPtr(?fn () callconv(.C) void, @intCast(c_ulong, @ptrToInt(func_ptr)));910 \\ var func_ptr: ?*anyopaque = @ptrCast(?*anyopaque, foo);
909 \\ _ = typed_func_ptr;911 \\ var typed_func_ptr: ?*const fn () callconv(.C) void = @intToPtr(?*const fn () callconv(.C) void, @intCast(c_ulong, @ptrToInt(func_ptr)));
910 \\}912 \\ _ = typed_func_ptr;
911 });913 \\}
914 });
915 }
912916
913 cases.add("noreturn attribute",917 cases.add("noreturn attribute",
914 \\void foo(void) __attribute__((noreturn));918 \\void foo(void) __attribute__((noreturn));
...@@ -968,19 +972,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -968,19 +972,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
968 \\}972 \\}
969 });973 });
970974
971 cases.add("typedef of function in struct field",975 if (builtin.zig_backend != .stage1) {
972 \\typedef void lws_callback_function(void);976 cases.add("typedef of function in struct field",
973 \\struct Foo {977 \\typedef void lws_callback_function(void);
974 \\ void (*func)(void);978 \\struct Foo {
975 \\ lws_callback_function *callback_http;979 \\ void (*func)(void);
976 \\};980 \\ lws_callback_function *callback_http;
977 , &[_][]const u8{981 \\};
978 \\pub const lws_callback_function = fn () callconv(.C) void;982 , &[_][]const u8{
979 \\pub const struct_Foo = extern struct {983 \\pub const lws_callback_function = fn () callconv(.C) void;
980 \\ func: ?fn () callconv(.C) void,984 \\pub const struct_Foo = extern struct {
981 \\ callback_http: ?lws_callback_function,985 \\ func: ?*const fn () callconv(.C) void,
982 \\};986 \\ callback_http: ?*const lws_callback_function,
983 });987 \\};
988 });
989 }
984990
985 cases.add("pointer to struct demoted to opaque due to bit fields",991 cases.add("pointer to struct demoted to opaque due to bit fields",
986 \\struct Foo {992 \\struct Foo {
...@@ -1051,17 +1057,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1051,17 +1057,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1051 \\pub const Foo = struct_Foo;1057 \\pub const Foo = struct_Foo;
1052 });1058 });
10531059
1054 cases.add("self referential struct with function pointer",1060 if (builtin.zig_backend != .stage1) {
1055 \\struct Foo {1061 cases.add("self referential struct with function pointer",
1056 \\ void (*derp)(struct Foo *foo);1062 \\struct Foo {
1057 \\};1063 \\ void (*derp)(struct Foo *foo);
1058 , &[_][]const u8{1064 \\};
1059 \\pub const struct_Foo = extern struct {1065 , &[_][]const u8{
1060 \\ derp: ?fn ([*c]struct_Foo) callconv(.C) void,1066 \\pub const struct_Foo = extern struct {
1061 \\};1067 \\ derp: ?*const fn ([*c]struct_Foo) callconv(.C) void,
1062 ,1068 \\};
1063 \\pub const Foo = struct_Foo;1069 ,
1064 });1070 \\pub const Foo = struct_Foo;
1071 });
1072 }
10651073
1066 cases.add("struct prototype used in func",1074 cases.add("struct prototype used in func",
1067 \\struct Foo;1075 \\struct Foo;
...@@ -1327,11 +1335,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1327,11 +1335,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1327 \\pub extern fn func(array: [*c]c_int) void;1335 \\pub extern fn func(array: [*c]c_int) void;
1328 });1336 });
13291337
1330 cases.add("__cdecl doesn't mess up function pointers",1338 if (builtin.zig_backend != .stage1) {
1331 \\void foo(void (__cdecl *fn_ptr)(void));1339 cases.add("__cdecl doesn't mess up function pointers",
1332 , &[_][]const u8{1340 \\void foo(void (__cdecl *fn_ptr)(void));
1333 \\pub extern fn foo(fn_ptr: ?fn () callconv(.C) void) void;1341 , &[_][]const u8{
1334 });1342 \\pub extern fn foo(fn_ptr: ?*const fn () callconv(.C) void) void;
1343 });
1344 }
13351345
1336 cases.add("void cast",1346 cases.add("void cast",
1337 \\void foo() {1347 \\void foo() {
...@@ -1674,13 +1684,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1674,13 +1684,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1674 \\pub extern var my_enum: enum_enum_ty;1684 \\pub extern var my_enum: enum_enum_ty;
1675 });1685 });
16761686
1677 cases.add("Parameterless function pointers",1687 if (builtin.zig_backend != .stage1) {
1678 \\typedef void (*fn0)();1688 cases.add("Parameterless function pointers",
1679 \\typedef void (*fn1)(char);1689 \\typedef void (*fn0)();
1680 , &[_][]const u8{1690 \\typedef void (*fn1)(char);
1681 \\pub const fn0 = ?fn (...) callconv(.C) void;1691 , &[_][]const u8{
1682 \\pub const fn1 = ?fn (u8) callconv(.C) void;1692 \\pub const fn0 = ?*const fn (...) callconv(.C) void;
1683 });1693 \\pub const fn1 = ?*const fn (u8) callconv(.C) void;
1694 });
1695 }
16841696
1685 cases.addWithTarget("Calling convention", .{1697 cases.addWithTarget("Calling convention", .{
1686 .cpu_arch = .i386,1698 .cpu_arch = .i386,
...@@ -1880,60 +1892,64 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1880,60 +1892,64 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1880 \\pub const SDL_INIT_VIDEO = @as(c_ulonglong, 0x00000020);1892 \\pub const SDL_INIT_VIDEO = @as(c_ulonglong, 0x00000020);
1881 });1893 });
18821894
1883 cases.add("generate inline func for #define global extern fn",1895 if (builtin.zig_backend != .stage1) {
1884 \\extern void (*fn_ptr)(void);1896 cases.add("generate inline func for #define global extern fn",
1885 \\#define foo fn_ptr1897 \\extern void (*fn_ptr)(void);
1886 \\1898 \\#define foo fn_ptr
1887 \\extern char (*fn_ptr2)(int, float);1899 \\
1888 \\#define bar fn_ptr21900 \\extern char (*fn_ptr2)(int, float);
1889 , &[_][]const u8{1901 \\#define bar fn_ptr2
1890 \\pub extern var fn_ptr: ?fn () callconv(.C) void;1902 , &[_][]const u8{
1891 ,1903 \\pub extern var fn_ptr: ?*const fn () callconv(.C) void;
1892 \\pub inline fn foo() void {1904 ,
1893 \\ return fn_ptr.?();1905 \\pub inline fn foo() void {
1894 \\}1906 \\ return fn_ptr.?();
1895 ,1907 \\}
1896 \\pub extern var fn_ptr2: ?fn (c_int, f32) callconv(.C) u8;1908 ,
1897 ,1909 \\pub extern var fn_ptr2: ?*const fn (c_int, f32) callconv(.C) u8;
1898 \\pub inline fn bar(arg_1: c_int, arg_2: f32) u8 {1910 ,
1899 \\ return fn_ptr2.?(arg_1, arg_2);1911 \\pub inline fn bar(arg_1: c_int, arg_2: f32) u8 {
1900 \\}1912 \\ return fn_ptr2.?(arg_1, arg_2);
1901 });1913 \\}
1914 });
1915 }
19021916
1903 cases.add("macros with field targets",1917 if (builtin.zig_backend != .stage1) {
1904 \\typedef unsigned int GLbitfield;1918 cases.add("macros with field targets",
1905 \\typedef void (*PFNGLCLEARPROC) (GLbitfield mask);1919 \\typedef unsigned int GLbitfield;
1906 \\typedef void(*OpenGLProc)(void);1920 \\typedef void (*PFNGLCLEARPROC) (GLbitfield mask);
1907 \\union OpenGLProcs {1921 \\typedef void(*OpenGLProc)(void);
1908 \\ OpenGLProc ptr[1];1922 \\union OpenGLProcs {
1909 \\ struct {1923 \\ OpenGLProc ptr[1];
1910 \\ PFNGLCLEARPROC Clear;1924 \\ struct {
1911 \\ } gl;1925 \\ PFNGLCLEARPROC Clear;
1912 \\};1926 \\ } gl;
1913 \\extern union OpenGLProcs glProcs;1927 \\};
1914 \\#define glClearUnion glProcs.gl.Clear1928 \\extern union OpenGLProcs glProcs;
1915 \\#define glClearPFN PFNGLCLEARPROC1929 \\#define glClearUnion glProcs.gl.Clear
1916 , &[_][]const u8{1930 \\#define glClearPFN PFNGLCLEARPROC
1917 \\pub const GLbitfield = c_uint;1931 , &[_][]const u8{
1918 \\pub const PFNGLCLEARPROC = ?fn (GLbitfield) callconv(.C) void;1932 \\pub const GLbitfield = c_uint;
1919 \\pub const OpenGLProc = ?fn () callconv(.C) void;1933 \\pub const PFNGLCLEARPROC = ?*const fn (GLbitfield) callconv(.C) void;
1920 \\const struct_unnamed_1 = extern struct {1934 \\pub const OpenGLProc = ?*const fn () callconv(.C) void;
1921 \\ Clear: PFNGLCLEARPROC,1935 \\const struct_unnamed_1 = extern struct {
1922 \\};1936 \\ Clear: PFNGLCLEARPROC,
1923 \\pub const union_OpenGLProcs = extern union {1937 \\};
1924 \\ ptr: [1]OpenGLProc,1938 \\pub const union_OpenGLProcs = extern union {
1925 \\ gl: struct_unnamed_1,1939 \\ ptr: [1]OpenGLProc,
1926 \\};1940 \\ gl: struct_unnamed_1,
1927 \\pub extern var glProcs: union_OpenGLProcs;1941 \\};
1928 ,1942 \\pub extern var glProcs: union_OpenGLProcs;
1929 \\pub const glClearPFN = PFNGLCLEARPROC;1943 ,
1930 ,1944 \\pub const glClearPFN = PFNGLCLEARPROC;
1931 \\pub inline fn glClearUnion(arg_2: GLbitfield) void {1945 ,
1932 \\ return glProcs.gl.Clear.?(arg_2);1946 \\pub inline fn glClearUnion(arg_2: GLbitfield) void {
1933 \\}1947 \\ return glProcs.gl.Clear.?(arg_2);
1934 ,1948 \\}
1935 \\pub const OpenGLProcs = union_OpenGLProcs;1949 ,
1936 });1950 \\pub const OpenGLProcs = union_OpenGLProcs;
1951 });
1952 }
19371953
1938 cases.add("macro pointer cast",1954 cases.add("macro pointer cast",
1939 \\#define NRF_GPIO_BASE 01955 \\#define NRF_GPIO_BASE 0
...@@ -2840,35 +2856,37 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2840,35 +2856,37 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2840 \\}2856 \\}
2841 });2857 });
28422858
2843 cases.add("deref function pointer",2859 if (builtin.zig_backend != .stage1) {
2844 \\void foo(void) {}2860 cases.add("deref function pointer",
2845 \\int baz(void) { return 0; }2861 \\void foo(void) {}
2846 \\void bar(void) {2862 \\int baz(void) { return 0; }
2847 \\ void(*f)(void) = foo;2863 \\void bar(void) {
2848 \\ int(*b)(void) = baz;2864 \\ void(*f)(void) = foo;
2849 \\ f();2865 \\ int(*b)(void) = baz;
2850 \\ (*(f))();2866 \\ f();
2851 \\ foo();2867 \\ (*(f))();
2852 \\ b();2868 \\ foo();
2853 \\ (*(b))();2869 \\ b();
2854 \\ baz();2870 \\ (*(b))();
2855 \\}2871 \\ baz();
2856 , &[_][]const u8{2872 \\}
2857 \\pub export fn foo() void {}2873 , &[_][]const u8{
2858 \\pub export fn baz() c_int {2874 \\pub export fn foo() void {}
2859 \\ return 0;2875 \\pub export fn baz() c_int {
2860 \\}2876 \\ return 0;
2861 \\pub export fn bar() void {2877 \\}
2862 \\ var f: ?fn () callconv(.C) void = foo;2878 \\pub export fn bar() void {
2863 \\ var b: ?fn () callconv(.C) c_int = baz;2879 \\ var f: ?*const fn () callconv(.C) void = foo;
2864 \\ f.?();2880 \\ var b: ?*const fn () callconv(.C) c_int = baz;
2865 \\ f.?();2881 \\ f.?();
2866 \\ foo();2882 \\ f.?();
2867 \\ _ = b.?();2883 \\ foo();
2868 \\ _ = b.?();2884 \\ _ = b.?();
2869 \\ _ = baz();2885 \\ _ = b.?();
2870 \\}2886 \\ _ = baz();
2871 });2887 \\}
2888 });
2889 }
28722890
2873 cases.add("pre increment/decrement",2891 cases.add("pre increment/decrement",
2874 \\void foo(void) {2892 \\void foo(void) {
...@@ -3143,73 +3161,77 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3143,73 +3161,77 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3143 \\}3161 \\}
3144 });3162 });
31453163
3146 cases.add("implicit casts",3164 if (builtin.zig_backend != .stage1) {
3147 \\#include <stdbool.h>3165 cases.add("implicit casts",
3148 \\3166 \\#include <stdbool.h>
3149 \\void fn_int(int x);3167 \\
3150 \\void fn_f32(float x);3168 \\void fn_int(int x);
3151 \\void fn_f64(double x);3169 \\void fn_f32(float x);
3152 \\void fn_char(char x);3170 \\void fn_f64(double x);
3153 \\void fn_bool(bool x);3171 \\void fn_char(char x);
3154 \\void fn_ptr(void *x);3172 \\void fn_bool(bool x);
3155 \\3173 \\void fn_ptr(void *x);
3156 \\void call() {3174 \\
3157 \\ fn_int(3.0f);3175 \\void call() {
3158 \\ fn_int(3.0);3176 \\ fn_int(3.0f);
3159 \\ fn_int('ABCD');3177 \\ fn_int(3.0);
3160 \\ fn_f32(3);3178 \\ fn_int('ABCD');
3161 \\ fn_f64(3);3179 \\ fn_f32(3);
3162 \\ fn_char('3');3180 \\ fn_f64(3);
3163 \\ fn_char('\x1');3181 \\ fn_char('3');
3164 \\ fn_char(0);3182 \\ fn_char('\x1');
3165 \\ fn_f32(3.0f);3183 \\ fn_char(0);
3166 \\ fn_f64(3.0);3184 \\ fn_f32(3.0f);
3167 \\ fn_bool(123);3185 \\ fn_f64(3.0);
3168 \\ fn_bool(0);3186 \\ fn_bool(123);
3169 \\ fn_bool(&fn_int);3187 \\ fn_bool(0);
3170 \\ fn_int(&fn_int);3188 \\ fn_bool(&fn_int);
3171 \\ fn_ptr(42);3189 \\ fn_int(&fn_int);
3172 \\}3190 \\ fn_ptr(42);
3173 , &[_][]const u8{3191 \\}
3174 \\pub extern fn fn_int(x: c_int) void;3192 , &[_][]const u8{
3175 \\pub extern fn fn_f32(x: f32) void;3193 \\pub extern fn fn_int(x: c_int) void;
3176 \\pub extern fn fn_f64(x: f64) void;3194 \\pub extern fn fn_f32(x: f32) void;
3177 \\pub extern fn fn_char(x: u8) void;3195 \\pub extern fn fn_f64(x: f64) void;
3178 \\pub extern fn fn_bool(x: bool) void;3196 \\pub extern fn fn_char(x: u8) void;
3179 \\pub extern fn fn_ptr(x: ?*anyopaque) void;3197 \\pub extern fn fn_bool(x: bool) void;
3180 \\pub export fn call() void {3198 \\pub extern fn fn_ptr(x: ?*anyopaque) void;
3181 \\ fn_int(@floatToInt(c_int, 3.0));3199 \\pub export fn call() void {
3182 \\ fn_int(@floatToInt(c_int, 3.0));3200 \\ fn_int(@floatToInt(c_int, 3.0));
3183 \\ fn_int(@as(c_int, 1094861636));3201 \\ fn_int(@floatToInt(c_int, 3.0));
3184 \\ fn_f32(@intToFloat(f32, @as(c_int, 3)));3202 \\ fn_int(@as(c_int, 1094861636));
3185 \\ fn_f64(@intToFloat(f64, @as(c_int, 3)));3203 \\ fn_f32(@intToFloat(f32, @as(c_int, 3)));
3186 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));3204 \\ fn_f64(@intToFloat(f64, @as(c_int, 3)));
3187 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));3205 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));
3188 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));3206 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));
3189 \\ fn_f32(3.0);3207 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));
3190 \\ fn_f64(3.0);3208 \\ fn_f32(3.0);
3191 \\ fn_bool(@as(c_int, 123) != 0);3209 \\ fn_f64(3.0);
3192 \\ fn_bool(@as(c_int, 0) != 0);3210 \\ fn_bool(@as(c_int, 123) != 0);
3193 \\ fn_bool(@ptrToInt(fn_int) != 0);3211 \\ fn_bool(@as(c_int, 0) != 0);
3194 \\ fn_int(@intCast(c_int, @ptrToInt(fn_int)));3212 \\ fn_bool(@ptrToInt(&fn_int) != 0);
3195 \\ fn_ptr(@intToPtr(?*anyopaque, @as(c_int, 42)));3213 \\ fn_int(@intCast(c_int, @ptrToInt(&fn_int)));
3196 \\}3214 \\ fn_ptr(@intToPtr(?*anyopaque, @as(c_int, 42)));
3197 });3215 \\}
31983216 });
3199 cases.add("function call",3217 }
3200 \\static void bar(void) { }3218
3201 \\void foo(int *(baz)(void)) {3219 if (builtin.zig_backend != .stage1) {
3202 \\ bar();3220 cases.add("function call",
3203 \\ baz();3221 \\static void bar(void) { }
3204 \\}3222 \\void foo(int *(baz)(void)) {
3205 , &[_][]const u8{3223 \\ bar();
3206 \\pub fn bar() callconv(.C) void {}3224 \\ baz();
3207 \\pub export fn foo(arg_baz: ?fn () callconv(.C) [*c]c_int) void {3225 \\}
3208 \\ var baz = arg_baz;3226 , &[_][]const u8{
3209 \\ bar();3227 \\pub fn bar() callconv(.C) void {}
3210 \\ _ = baz.?();3228 \\pub export fn foo(arg_baz: ?*const fn () callconv(.C) [*c]c_int) void {
3211 \\}3229 \\ var baz = arg_baz;
3212 });3230 \\ bar();
3231 \\ _ = baz.?();
3232 \\}
3233 });
3234 }
32133235
3214 cases.add("macro defines string literal with octal",3236 cases.add("macro defines string literal with octal",
3215 \\#define FOO "aoeu\023 derp"3237 \\#define FOO "aoeu\023 derp"