| author | |
| committer | |
| log | ab6dbfe1a314e7004fea4625c191d57d4c67f049 |
| tree | e02f06bd9330c6f4dccbb4596f3620f846c6df6e |
| parent | 0795e2b2ef96c75b8d5e68ed1b8ac23ced28bfce |
3 files changed, 27 insertions(+), 5 deletions(-)
src/translate_c.zig+4-3| ... | ... | @@ -7,10 +7,10 @@ const meta = std.meta; |
| 7 | 7 | const clang = @import("clang.zig"); |
| 8 | 8 | const aro = @import("aro"); |
| 9 | 9 | const CToken = aro.Tokenizer.Token; |
| 10 | const Node = ast.Node; | |
| 11 | const Tag = Node.Tag; | |
| 12 | 10 | const common = @import("aro_translate_c"); |
| 13 | 11 | const ast = common.ast; |
| 12 | const Node = ast.Node; | |
| 13 | const Tag = Node.Tag; | |
| 14 | 14 | const Error = common.Error; |
| 15 | 15 | const MacroProcessingError = common.MacroProcessingError; |
| 16 | 16 | const TypeError = common.TypeError; |
| ... | ... | @@ -5985,10 +5985,11 @@ fn parseCCondExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { |
| 5985 | 5985 | } |
| 5986 | 5986 | _ = m.next(); |
| 5987 | 5987 | |
| 5988 | const cond_body = try macroIntToBool(c, node); | |
| 5988 | 5989 | const then_body = try parseCOrExpr(c, m, scope); |
| 5989 | 5990 | try m.skip(c, .colon); |
| 5990 | 5991 | const else_body = try parseCCondExpr(c, m, scope); |
| 5991 | return Tag.@"if".create(c.arena, .{ .cond = node, .then = then_body, .@"else" = else_body }); | |
| 5992 | return Tag.@"if".create(c.arena, .{ .cond = cond_body, .then = then_body, .@"else" = else_body }); | |
| 5992 | 5993 | } |
| 5993 | 5994 | |
| 5994 | 5995 | fn parseCOrExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { |
test/cases/translate_c/tenary_in_macro.c created+21| ... | ... | @@ -0,0 +1,21 @@ |
| 1 | #define TERNARY_CHECK(i) check(i) | |
| 2 | #define TERNARY_CALL(i) (TERNARY_CHECK(i) ? (i+1) : (i-1)) | |
| 3 | ||
| 4 | static inline int check(int obj) { | |
| 5 | return obj % 2; | |
| 6 | } | |
| 7 | int target_func(int a) { | |
| 8 | return TERNARY_CALL(a); | |
| 9 | } | |
| 10 | ||
| 11 | // translate-c | |
| 12 | // c_frontend=clang | |
| 13 | // | |
| 14 | // pub inline fn TERNARY_CHECK(i: anytype) @TypeOf(check(i)) { | |
| 15 | // _ = &i; | |
| 16 | // return check(i); | |
| 17 | // } | |
| 18 | // pub inline fn TERNARY_CALL(i: anytype) @TypeOf(if (TERNARY_CHECK(i) != 0) i + @as(c_int, 1) else i - @as(c_int, 1)) { | |
| 19 | // _ = &i; | |
| 20 | // return if (TERNARY_CHECK(i) != 0) i + @as(c_int, 1) else i - @as(c_int, 1); | |
| 21 | // } | |
| \ No newline at end of file |
test/translate_c.zig+2-2| ... | ... | @@ -3101,8 +3101,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3101 | 3101 | \\ int a, b, c; |
| 3102 | 3102 | \\#define FOO a ? b : c |
| 3103 | 3103 | , &[_][]const u8{ |
| 3104 | \\pub inline fn FOO() @TypeOf(if (a) b else c) { | |
| 3105 | \\ return if (a) b else c; | |
| 3104 | \\pub inline fn FOO() @TypeOf(if (a != 0) b else c) { | |
| 3105 | \\ return if (a != 0) b else c; | |
| 3106 | 3106 | \\} |
| 3107 | 3107 | }); |
| 3108 | 3108 |