Skip to content

Commit 2aa8cc8

Browse files
committed
Emit switch! macro on switch stmt with fallthrough
1 parent c4880c0 commit 2aa8cc8

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,12 +2646,20 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
26462646
auto *body = clang::dyn_cast<clang::CompoundStmt>(stmt->getBody());
26472647
assert(body);
26482648

2649-
StrCat("'switch: {");
2650-
StrCat(std::format("let __match_cond = {};", ToString(stmt->getCond())));
2651-
StrCat("match __match_cond");
2652-
StrCat("{");
2649+
bool has_fallthrough = SwitchHasFallthrough(stmt);
26532650

2654-
++switch_depth_;
2651+
if (has_fallthrough) {
2652+
StrCat("switch!(match ", ToString(stmt->getCond()), " {");
2653+
} else {
2654+
StrCat("'switch: {");
2655+
StrCat(std::format("let __match_cond = {};", ToString(stmt->getCond())));
2656+
StrCat("match __match_cond");
2657+
StrCat("{");
2658+
}
2659+
2660+
if (!has_fallthrough) {
2661+
++switch_depth_;
2662+
}
26552663

26562664
clang::SwitchCase *default_case = nullptr;
26572665
for (auto *sc : GetTopLevelSwitchCases(stmt)) {
@@ -2677,10 +2685,16 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
26772685
StrCat(R"( _ => {})");
26782686
}
26792687

2680-
--switch_depth_;
2688+
if (!has_fallthrough) {
2689+
--switch_depth_;
2690+
}
26812691

2682-
StrCat("}");
2683-
StrCat("}");
2692+
if (has_fallthrough) {
2693+
StrCat("})");
2694+
} else {
2695+
StrCat("}");
2696+
StrCat("}");
2697+
}
26842698
return false;
26852699
}
26862700

0 commit comments

Comments
 (0)