commit 9a8545d5903ff60de16f0ddab2b9a4c4c1a798a7 (tree)
parent aa2ca3f02c11c133964d793847c925e3bd131b27
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Sun, 26 Nov 2017 16:03:56 -0500
translate-c: fix translation when no default switch case
Diffstat:
2 files changed, 39 insertions(+), 1 deletion(-)
diff --git a/src/translate_c.cpp b/src/translate_c.cpp
@@ -2344,7 +2344,7 @@ static AstNode *trans_switch_stmt(Context *c, TransScope *parent_scope, const Sw
if (!switch_scope->found_default && !stmt->isAllEnumCasesCovered()) {
AstNode *prong_node = trans_create_node(c, NodeTypeSwitchProng);
- prong_node->data.switch_prong.expr = trans_create_node(c, NodeTypeBreak);
+ prong_node->data.switch_prong.expr = trans_create_node_goto(c, end_label_name);
switch_scope->switch_node->data.switch_expr.prongs.append(prong_node);
}
diff --git a/test/translate_c.zig b/test/translate_c.zig
@@ -1042,6 +1042,44 @@ pub fn addCases(cases: &tests.TranslateCContext) {
\\ return x + 13;
\\}
);
+
+ cases.add("switch statement with no default",
+ \\int foo(int x) {
+ \\ switch (x) {
+ \\ case 1:
+ \\ x += 1;
+ \\ case 2:
+ \\ break;
+ \\ case 3:
+ \\ case 4:
+ \\ return x + 1;
+ \\ }
+ \\ return x + 13;
+ \\}
+ ,
+ \\fn foo(_arg_x: c_int) -> c_int {
+ \\ var x = _arg_x;
+ \\ {
+ \\ switch (x) {
+ \\ 1 => goto case_0,
+ \\ 2 => goto case_1,
+ \\ 3 => goto case_2,
+ \\ 4 => goto case_3,
+ \\ else => goto end,
+ \\ };
+ \\ case_0:
+ \\ x += 1;
+ \\ case_1:
+ \\ goto end;
+ \\ case_2:
+ \\ case_3:
+ \\ return x + 1;
+ \\ goto end;
+ \\ end:
+ \\ };
+ \\ return x + 13;
+ \\}
+ );
}