authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-08-02 22:44:20-07:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-08-06 09:10:50+03:00
log9fd3aeb8088cd9a3b0744d5f508ca256a2bbf19f
tree69422585eaf58ac2f4f1e7fcf30a4aab4b16f3eb
parentfdd97244fda948e0033034aba8042c8fdfb5d484

translate-c: handle macros that cast to cv void

Fixes #9507

4 files changed, 50 insertions(+), 0 deletions(-)

lib/std/zig/c_translation.zig+4
...@@ -404,6 +404,10 @@ pub const Macros = struct {...@@ -404,6 +404,10 @@ pub const Macros = struct {
404 else => unreachable, // return type will be a compile error otherwise404 else => unreachable, // return type will be a compile error otherwise
405 }405 }
406 }406 }
407
408 pub inline fn DISCARD(x: anytype) void {
409 _ = x;
410 }
407};411};
408412
409test "Macro suffix functions" {413test "Macro suffix functions" {
src/translate_c.zig+21
...@@ -4978,6 +4978,17 @@ const PatternList = struct {...@@ -4978,6 +4978,17 @@ const PatternList = struct {
4978 ,4978 ,
4979 "WL_CONTAINER_OF",4979 "WL_CONTAINER_OF",
4980 },4980 },
4981
4982 [2][]const u8{ "IGNORE_ME(X) ((void)(X))", "DISCARD" },
4983 [2][]const u8{ "IGNORE_ME(X) (void)(X)", "DISCARD" },
4984 [2][]const u8{ "IGNORE_ME(X) ((const void)(X))", "DISCARD" },
4985 [2][]const u8{ "IGNORE_ME(X) (const void)(X)", "DISCARD" },
4986 [2][]const u8{ "IGNORE_ME(X) ((volatile void)(X))", "DISCARD" },
4987 [2][]const u8{ "IGNORE_ME(X) (volatile void)(X)", "DISCARD" },
4988 [2][]const u8{ "IGNORE_ME(X) ((const volatile void)(X))", "DISCARD" },
4989 [2][]const u8{ "IGNORE_ME(X) (const volatile void)(X)", "DISCARD" },
4990 [2][]const u8{ "IGNORE_ME(X) ((volatile const void)(X))", "DISCARD" },
4991 [2][]const u8{ "IGNORE_ME(X) (volatile const void)(X)", "DISCARD" },
4981 };4992 };
49824993
4983 /// Assumes that `ms` represents a tokenized function-like macro.4994 /// Assumes that `ms` represents a tokenized function-like macro.
...@@ -5152,6 +5163,16 @@ test "Macro matching" {...@@ -5152,6 +5163,16 @@ test "Macro matching" {
51525163
5153 try helper.checkMacro(allocator, pattern_list, "NO_MATCH(X, Y) (X + Y)", null);5164 try helper.checkMacro(allocator, pattern_list, "NO_MATCH(X, Y) (X + Y)", null);
5154 try helper.checkMacro(allocator, pattern_list, "CAST_OR_CALL(X, Y) (X)(Y)", "CAST_OR_CALL");5165 try helper.checkMacro(allocator, pattern_list, "CAST_OR_CALL(X, Y) (X)(Y)", "CAST_OR_CALL");
5166 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (void)(X)", "DISCARD");
5167 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((void)(X))", "DISCARD");
5168 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (const void)(X)", "DISCARD");
5169 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((const void)(X))", "DISCARD");
5170 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (volatile void)(X)", "DISCARD");
5171 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((volatile void)(X))", "DISCARD");
5172 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (const volatile void)(X)", "DISCARD");
5173 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((const volatile void)(X))", "DISCARD");
5174 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (volatile const void)(X)", "DISCARD");
5175 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((volatile const void)(X))", "DISCARD");
5155}5176}
51565177
5157const MacroCtx = struct {5178const MacroCtx = struct {
test/behavior/translate_c_macros.h+12
...@@ -18,3 +18,15 @@ struct Foo {...@@ -18,3 +18,15 @@ struct Foo {
18#define SIZE_OF_FOO sizeof(struct Foo)18#define SIZE_OF_FOO sizeof(struct Foo)
1919
20#define MAP_FAILED ((void *) -1)20#define MAP_FAILED ((void *) -1)
21
22#define IGNORE_ME_1(x) ((void)(x))
23#define IGNORE_ME_2(x) ((const void)(x))
24#define IGNORE_ME_3(x) ((volatile void)(x))
25#define IGNORE_ME_4(x) ((const volatile void)(x))
26#define IGNORE_ME_5(x) ((volatile const void)(x))
27
28#define IGNORE_ME_6(x) (void)(x)
29#define IGNORE_ME_7(x) (const void)(x)
30#define IGNORE_ME_8(x) (volatile void)(x)
31#define IGNORE_ME_9(x) (const volatile void)(x)
32#define IGNORE_ME_10(x) (volatile const void)(x)
test/behavior/translate_c_macros.zig+13
...@@ -24,3 +24,16 @@ test "reference to a struct type" {...@@ -24,3 +24,16 @@ test "reference to a struct type" {
24test "cast negative integer to pointer" {24test "cast negative integer to pointer" {
25 try expectEqual(@intToPtr(?*c_void, @bitCast(usize, @as(isize, -1))), h.MAP_FAILED);25 try expectEqual(@intToPtr(?*c_void, @bitCast(usize, @as(isize, -1))), h.MAP_FAILED);
26}26}
27
28test "casting to void with a macro" {
29 h.IGNORE_ME_1(42);
30 h.IGNORE_ME_2(42);
31 h.IGNORE_ME_3(42);
32 h.IGNORE_ME_4(42);
33 h.IGNORE_ME_5(42);
34 h.IGNORE_ME_6(42);
35 h.IGNORE_ME_7(42);
36 h.IGNORE_ME_8(42);
37 h.IGNORE_ME_9(42);
38 h.IGNORE_ME_10(42);
39}