translate-c: don't crash on complex switches

master
Vexu 2020-06-04 13:53:29 +03:00
parent fd067fbe8b
commit c27a8bd6be
No known key found for this signature in database
GPG Key ID: 59AEB8936E16A6AC
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));
else_prong.expr = &(try transCreateNodeBreak(rp.c, "__switch")).base;
_ = 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.case_index += 1;
}
@ -2666,6 +2669,9 @@ fn transCase(
const switch_prong = try transCreateNodeSwitchCase(rp.c, expr);
switch_prong.expr = &(try transCreateNodeBreak(rp.c, label)).base;
_ = 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.case_index += 1;
@ -2699,6 +2705,9 @@ fn transDefault(
const else_prong = try transCreateNodeSwitchCase(rp.c, try transCreateNodeSwitchElse(rp.c));
else_prong.expr = &(try transCreateNodeBreak(rp.c, label)).base;
_ = 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.case_index += 1;

View File

@ -3,6 +3,21 @@ const std = @import("std");
const CrossTarget = std.zig.CrossTarget;
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",
\\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
, &[_][]const u8{