Merge pull request #5531 from Vexu/translate-c

Translate-c don't crash on complex switches
master
Veikka Tuominen 2020-06-05 10:41:56 +03:00 committed by GitHub
commit 499df9680c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -2625,6 +2625,9 @@ fn transSwitch(
const else_prong = try transCreateNodeSwitchCase(rp.c, try transCreateNodeSwitchElse(rp.c)); const else_prong = try transCreateNodeSwitchCase(rp.c, try transCreateNodeSwitchElse(rp.c));
else_prong.expr = &(try transCreateNodeBreak(rp.c, "__switch")).base; else_prong.expr = &(try transCreateNodeBreak(rp.c, "__switch")).base;
_ = try appendToken(rp.c, .Comma, ","); _ = try appendToken(rp.c, .Comma, ",");
if (switch_scope.case_index >= switch_scope.cases.len)
return revertAndWarn(rp, error.UnsupportedTranslation, ZigClangStmt_getBeginLoc(@ptrCast(*const ZigClangStmt, stmt)), "TODO complex switch cases", .{});
switch_scope.cases[switch_scope.case_index] = &else_prong.base; switch_scope.cases[switch_scope.case_index] = &else_prong.base;
switch_scope.case_index += 1; switch_scope.case_index += 1;
} }
@ -2666,6 +2669,9 @@ fn transCase(
const switch_prong = try transCreateNodeSwitchCase(rp.c, expr); const switch_prong = try transCreateNodeSwitchCase(rp.c, expr);
switch_prong.expr = &(try transCreateNodeBreak(rp.c, label)).base; switch_prong.expr = &(try transCreateNodeBreak(rp.c, label)).base;
_ = try appendToken(rp.c, .Comma, ","); _ = try appendToken(rp.c, .Comma, ",");
if (switch_scope.case_index >= switch_scope.cases.len)
return revertAndWarn(rp, error.UnsupportedTranslation, ZigClangStmt_getBeginLoc(@ptrCast(*const ZigClangStmt, stmt)), "TODO complex switch cases", .{});
switch_scope.cases[switch_scope.case_index] = &switch_prong.base; switch_scope.cases[switch_scope.case_index] = &switch_prong.base;
switch_scope.case_index += 1; switch_scope.case_index += 1;
@ -2699,6 +2705,9 @@ fn transDefault(
const else_prong = try transCreateNodeSwitchCase(rp.c, try transCreateNodeSwitchElse(rp.c)); const else_prong = try transCreateNodeSwitchCase(rp.c, try transCreateNodeSwitchElse(rp.c));
else_prong.expr = &(try transCreateNodeBreak(rp.c, label)).base; else_prong.expr = &(try transCreateNodeBreak(rp.c, label)).base;
_ = try appendToken(rp.c, .Comma, ","); _ = try appendToken(rp.c, .Comma, ",");
if (switch_scope.case_index >= switch_scope.cases.len)
return revertAndWarn(rp, error.UnsupportedTranslation, ZigClangStmt_getBeginLoc(@ptrCast(*const ZigClangStmt, stmt)), "TODO complex switch cases", .{});
switch_scope.cases[switch_scope.case_index] = &else_prong.base; switch_scope.cases[switch_scope.case_index] = &else_prong.base;
switch_scope.case_index += 1; switch_scope.case_index += 1;

View File

@ -3,6 +3,21 @@ const std = @import("std");
const CrossTarget = std.zig.CrossTarget; const CrossTarget = std.zig.CrossTarget;
pub fn addCases(cases: *tests.TranslateCContext) void { pub fn addCases(cases: *tests.TranslateCContext) void {
cases.add("complex switch",
\\int main() {
\\ int i = 2;
\\ switch (i) {
\\ case 0: {
\\ case 2:{
\\ i += 2;}
\\ i += 1;
\\ }
\\ }
\\}
, &[_][]const u8{ // TODO properly translate this
\\pub const main = @compileError("unable to translate function");
});
cases.add("correct semicolon after infixop", cases.add("correct semicolon after infixop",
\\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0) \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
, &[_][]const u8{ , &[_][]const u8{