Skip to content

Commit 18bb458

Browse files
committed
Use RAII delimiters
1 parent 9ed258c commit 18bb458

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,21 +2704,32 @@ void Converter::EmitSwitchArm(clang::CompoundStmt *body, clang::SwitchCase *sc,
27042704
}
27052705

27062706
bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
2707+
bool has_fallthrough = SwitchHasFallthrough(stmt);
27072708
PushBreakTarget push(break_target_, has_fallthrough
27082709
? BreakTarget::FallthroughSwitch
27092710
: BreakTarget::Switch);
27102711
auto *body = clang::dyn_cast<clang::CompoundStmt>(stmt->getBody());
27112712
assert(body);
27122713

27132714
if (has_fallthrough) {
2714-
StrCat("switch!(match ", ToString(stmt->getCond()), " {");
2715+
// Use the switch-with-fallthrough macro
2716+
StrCat("switch!");
2717+
} else {
2718+
StrCat("'switch:");
2719+
}
2720+
2721+
PushParen switch_macro_paren(*this, has_fallthrough);
2722+
PushBrace switch_label_brace(*this, !has_fallthrough);
2723+
2724+
if (has_fallthrough) {
2725+
StrCat("match", ToString(stmt->getCond()));
27152726
} else {
2716-
StrCat("'switch: {");
27172727
StrCat(std::format("let __match_cond = {};", ToString(stmt->getCond())));
27182728
StrCat("match __match_cond");
2719-
StrCat("{");
27202729
}
27212730

2731+
PushBrace match_brace(*this);
2732+
27222733
clang::SwitchCase *default_case = nullptr;
27232734
for (auto *sc : GetTopLevelSwitchCases(stmt)) {
27242735
if (SwitchCaseContainsDefault(sc)) {
@@ -2734,12 +2745,6 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
27342745
StrCat(R"( _ => {})");
27352746
}
27362747

2737-
if (has_fallthrough) {
2738-
StrCat("})");
2739-
} else {
2740-
StrCat("}");
2741-
StrCat("}");
2742-
}
27432748
return false;
27442749
}
27452750

0 commit comments

Comments
 (0)