Commit 79549e0ac1

Vexu <git@vexu.eu>
2020-11-06 11:48:28
translate-c: fix macro functions with no arguments
1 parent c955165
Changed files (2)
src/translate_c.zig
@@ -5437,9 +5437,8 @@ fn transMacroFnDefine(c: *Context, m: *MacroCtx) ParseError!void {
     defer fn_params.deinit();
 
     while (true) {
-        if (m.next().? != .Identifier) {
-            return m.fail(c, "unable to translate C expr: expected identifier", .{});
-        }
+        if (m.peek().? != .Identifier) break;
+        _ = m.next();
 
         const mangled_name = try block_scope.makeMangledName(c, m.slice());
         const param_name_tok = try appendIdentifier(c, mangled_name);
@@ -5459,8 +5458,7 @@ fn transMacroFnDefine(c: *Context, m: *MacroCtx) ParseError!void {
             .param_type = .{ .any_type = &any_type.base },
         };
 
-        if (m.peek().? != .Comma)
-            break;
+        if (m.peek().? != .Comma) break;
         _ = m.next();
         _ = try appendToken(c, .Comma, ",");
     }
test/translate_c.zig
@@ -1589,6 +1589,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\extern int c;
         \\#define BASIC(c) (c*2)
         \\#define FOO(L,b) (L + b)
+        \\#define BAR() (c*c)
     , &[_][]const u8{
         \\pub extern var c: c_int;
         ,
@@ -1599,6 +1600,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\pub inline fn FOO(L: anytype, b: anytype) @TypeOf(L + b) {
         \\    return L + b;
         \\}
+        ,
+        \\pub inline fn BAR() @TypeOf(c * c) {
+        \\    return c * c;
+        \\}
     });
 
     cases.add("macro defines string literal with hex",