Skip to content

Commit 690f97e

Browse files
committed
Add EmitSwitchArm
1 parent 88a6e91 commit 690f97e

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,7 +2647,22 @@ bool Converter::ConvertSwitchCaseCondition(clang::SwitchCase *stmt) {
26472647
return false;
26482648
}
26492649

2650+
void Converter::EmitSwitchArm(clang::CompoundStmt *body, clang::SwitchCase *sc,
2651+
bool is_default) {
2652+
if (is_default) {
2653+
StrCat("_ => {");
2654+
} else {
2655+
StrCat("v if v == ");
2656+
ConvertSwitchCaseCondition(sc);
2657+
}
2658+
for (auto *t : GetSwitchCaseBody(body, sc)) {
2659+
Convert(t);
2660+
}
2661+
StrCat("},");
2662+
}
2663+
26502664
bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
2665+
PushBreakTarget push(break_target_, BreakTarget::Switch);
26512666
auto *body = clang::dyn_cast<clang::CompoundStmt>(stmt->getBody());
26522667
assert(body);
26532668

@@ -2656,28 +2671,17 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
26562671
StrCat("match __match_cond");
26572672
StrCat("{");
26582673

2659-
PushBreakTarget push(break_target_, BreakTarget::Switch);
2660-
26612674
clang::SwitchCase *default_case = nullptr;
26622675
for (auto *sc : GetTopLevelSwitchCases(stmt)) {
26632676
if (SwitchCaseContainsDefault(sc)) {
26642677
default_case = sc;
26652678
continue;
26662679
}
2667-
StrCat("v if v == ");
2668-
ConvertSwitchCaseCondition(sc);
2669-
for (auto *t : GetSwitchCaseBody(body, sc)) {
2670-
Convert(t);
2671-
}
2672-
StrCat("},");
2680+
EmitSwitchArm(body, sc, /*is_default=*/false);
26732681
}
26742682

26752683
if (default_case) {
2676-
StrCat("_ => {");
2677-
for (auto *t : GetSwitchCaseBody(body, default_case)) {
2678-
Convert(t);
2679-
}
2680-
StrCat("},");
2684+
EmitSwitchArm(body, default_case, /*is_default=*/true);
26812685
} else {
26822686
StrCat(R"( _ => {})");
26832687
}

cpp2rust/converter/converter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
290290

291291
virtual bool VisitSwitchStmt(clang::SwitchStmt *stmt);
292292

293+
void EmitSwitchArm(clang::CompoundStmt *body, clang::SwitchCase *sc,
294+
bool is_default);
295+
293296
bool ConvertSwitchCaseCondition(clang::SwitchCase *stmt);
294297

295298
virtual bool VisitVAArgExpr(clang::VAArgExpr *expr);

0 commit comments

Comments
 (0)