Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
9b26449
Move visited_cases in class scope
lucic71 Apr 21, 2026
c33fbee
Handle default as middle case
lucic71 Apr 21, 2026
0c143fe
Add switch tests
lucic71 Apr 21, 2026
614e78e
Fix nested switches
lucic71 Apr 21, 2026
c4880c0
Move helper switch/case functions in converter_lib
lucic71 Apr 22, 2026
2aa8cc8
Emit switch! macro on switch stmt with fallthrough
lucic71 Apr 22, 2026
5455888
Update tests
lucic71 Apr 22, 2026
ac4bb8b
Add impl for switch and goto_block macros
lucic71 Apr 22, 2026
2c48f4a
clang-format
lucic71 Apr 22, 2026
e184ee7
Add switch_break
lucic71 Apr 23, 2026
9f9ee3b
Add SwitchBreakRewriter and LoopControlForbidden
lucic71 Apr 23, 2026
41bf5a3
Replace break with switch_break in tests
lucic71 Apr 23, 2026
caf0176
Import switch_break in libcc2rs
lucic71 Apr 23, 2026
cb7c711
Add negative tests
lucic71 Apr 23, 2026
228f855
Add banner
lucic71 Apr 23, 2026
6558323
Add BreakTarget
lucic71 Apr 23, 2026
c7416fc
Delete switch_break
lucic71 Apr 23, 2026
88ffb77
Update tests
lucic71 Apr 23, 2026
2f6a163
Add GotoStateMachine and SwitchStateMachine
lucic71 Apr 23, 2026
d40aeea
Describe behavior of switch and goto_block
lucic71 Apr 23, 2026
f84dbd4
Rename any to should_emit
lucic71 Apr 23, 2026
10acf9d
Add switch interleaved with loops tests
lucic71 Apr 23, 2026
c90fb38
Fix naming
lucic71 Apr 23, 2026
b0b483c
Drop explicit fallthrough from test
lucic71 Apr 23, 2026
cf8d4f0
Delete libcc2rs-macros
lucic71 Apr 23, 2026
214a0b5
Delete libcc2rs-macros references
lucic71 Apr 23, 2026
0749c51
Drop support for reducible default case chains
lucic71 Apr 23, 2026
23765a0
Delete unused functions
lucic71 Apr 23, 2026
033337f
Rename to break_target
lucic71 Apr 23, 2026
88a6e91
Rename VisitSwitchCase to ConvertSwitchCaseCondition
lucic71 Apr 23, 2026
690f97e
Add EmitSwitchArm
lucic71 Apr 23, 2026
436fc75
Simplify PushBreakTarget
lucic71 Apr 23, 2026
f6b377f
Track current break target using a stack
lucic71 Apr 24, 2026
7bc6f88
Update tests
lucic71 Apr 24, 2026
5caae54
Merge branch 'break-target' into default-case-in-the-middle
lucic71 Apr 24, 2026
cc986ec
Delete the global switch test
lucic71 Apr 24, 2026
77a9bb7
Re-add body declaration
lucic71 Apr 24, 2026
e6a66fd
Update tests
lucic71 Apr 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 48 additions & 41 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ bool Converter::VisitIfStmt(clang::IfStmt *stmt) {
}

bool Converter::VisitWhileStmt(clang::WhileStmt *stmt) {
PushBreakTarget push(break_target_, BreakTarget::Loop);
StrCat("'loop_:");
StrCat(keyword::kWhile);
ConvertCondition(stmt->getCond());
Expand All @@ -1002,6 +1003,7 @@ bool Converter::VisitWhileStmt(clang::WhileStmt *stmt) {
}

bool Converter::VisitDoStmt(clang::DoStmt *stmt) {
PushBreakTarget push(break_target_, BreakTarget::Loop);
StrCat("'loop_:");
StrCat(keyword::kLoop, token::kOpenCurlyBracket);
curr_for_inc_.emplace(nullptr);
Expand All @@ -1016,6 +1018,7 @@ bool Converter::VisitDoStmt(clang::DoStmt *stmt) {
}

bool Converter::VisitForStmt(clang::ForStmt *stmt) {
PushBreakTarget push(break_target_, BreakTarget::Loop);
Convert(stmt->getInit());
StrCat("'loop_:");
StrCat(keyword::kWhile);
Expand Down Expand Up @@ -1055,6 +1058,7 @@ void Converter::ConvertLoopVariable(clang::VarDecl *decl,

void Converter::ConvertForRangeBody(clang::CXXForRangeStmt *stmt,
const clang::VarDecl *map_iter_decl) {
PushBreakTarget push(break_target_, BreakTarget::Loop);
std::optional<ScopedMapIterDecl> skip;
if (map_iter_decl)
skip.emplace(*this, map_iter_decl);
Expand Down Expand Up @@ -1137,7 +1141,7 @@ bool Converter::VisitCXXForRangeStmtIndexBased(clang::CXXForRangeStmt *stmt,

bool Converter::VisitBreakStmt([[maybe_unused]] clang::BreakStmt *stmt) {
StrCat(keyword::kBreak);
if (break_with_explicit_label_) {
if (isSwitchBreak()) {
StrCat("'switch");
}
return false;
Expand Down Expand Up @@ -2617,66 +2621,69 @@ bool Converter::VisitImplicitValueInitExpr(clang::ImplicitValueInitExpr *expr) {
return false;
}

static std::unordered_set<clang::SwitchCase *> visited_cases;
bool Converter::ConvertSwitchCaseCondition(clang::SwitchCase *stmt) {
clang::Stmt *cur = stmt;
clang::SwitchCase *last = nullptr;
bool first = true;

bool Converter::VisitSwitchCase(clang::SwitchCase *stmt) {
if (visited_cases.contains(stmt)) {
return false;
while (auto *sc = clang::dyn_cast<clang::SwitchCase>(cur)) {
if (auto *case_stmt = clang::dyn_cast<clang::CaseStmt>(sc)) {
if (!first) {
StrCat("|| v == ");
}
Convert(case_stmt->getLHS());
}
last = sc;
first = false;
cur = sc->getSubStmt();
}
visited_cases.insert(stmt);

if (auto case_stmt = clang::dyn_cast<clang::CaseStmt>(stmt)) {
Convert(case_stmt->getLHS());
if (clang::isa<clang::CaseStmt>(last)) {
StrCat(" => {");
} else /* DefaultStmt */ {
StrCat("_ => {");
}
return false;
}

if (clang::isa<clang::CaseStmt>(stmt->getSubStmt())) {
StrCat("|| v == ");
void Converter::EmitSwitchArm(clang::CompoundStmt *body, clang::SwitchCase *sc,
bool is_default) {
if (is_default) {
StrCat("_ => {");
} else {
if (clang::isa<clang::CaseStmt>(stmt)) {
StrCat(" => {");
} else {
StrCat("_ => {");
}
StrCat("v if v == ");
ConvertSwitchCaseCondition(sc);
}

Convert(stmt->getSubStmt());
return false;
for (auto *t : GetSwitchCaseBody(body, sc)) {
Convert(t);
}
StrCat("},");
}

bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
PushBreakTarget push(break_target_, BreakTarget::Switch);
auto *body = clang::dyn_cast<clang::CompoundStmt>(stmt->getBody());
assert(body);

StrCat("'switch: {");
StrCat(std::format("let __match_cond = {};", ToString(stmt->getCond())));
StrCat("match __match_cond");
StrCat("{");

bool has_default_case = false;
auto body = llvm::cast<clang::CompoundStmt>(stmt->getBody());
assert(body);

break_with_explicit_label_ = true;
for (auto it = body->body_begin(), end = body->body_end(); it != end;) {
if (auto switch_case = clang::dyn_cast<clang::SwitchCase>(*it)) {
if (clang::isa<clang::CaseStmt>(switch_case)) {
StrCat("v if v == ");
} else {
has_default_case = true;
}
VisitSwitchCase(switch_case);
++it;
}

while (it != end && !clang::isa<clang::SwitchCase>(*it)) {
Convert(*it);
++it;
clang::SwitchCase *default_case = nullptr;
for (auto *sc : GetTopLevelSwitchCases(stmt)) {
if (SwitchCaseContainsDefault(sc)) {
default_case = sc;
continue;
}

StrCat("},");
EmitSwitchArm(body, sc, /*is_default=*/false);
}

if (!has_default_case) {
if (default_case) {
EmitSwitchArm(body, default_case, /*is_default=*/true);
} else {
StrCat(R"( _ => {})");
}
break_with_explicit_label_ = false;

StrCat("}");
StrCat("}");
Expand Down
27 changes: 25 additions & 2 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

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

virtual bool VisitSwitchCase(clang::SwitchCase *stmt);
void EmitSwitchArm(clang::CompoundStmt *body, clang::SwitchCase *sc,
bool is_default);

bool ConvertSwitchCaseCondition(clang::SwitchCase *stmt);

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

Expand Down Expand Up @@ -463,10 +466,30 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
clang::ASTContext &ctx_;
clang::FunctionDecl *curr_function_ = nullptr;
bool in_function_formals_ = false;
bool break_with_explicit_label_ = false;
std::stack<clang::Expr *> curr_for_inc_;
std::stack<clang::QualType> curr_init_type_;

enum class BreakTarget { Loop, Switch };
std::stack<BreakTarget> break_target_;

bool isSwitchBreak() const {
return !break_target_.empty() && break_target_.top() == BreakTarget::Switch;
}

class PushBreakTarget {
public:
PushBreakTarget(std::stack<BreakTarget> &stack, BreakTarget target)
: stack_(stack) {
stack_.push(target);
}
~PushBreakTarget() { stack_.pop(); }
PushBreakTarget(const PushBreakTarget &) = delete;
PushBreakTarget &operator=(const PushBreakTarget &) = delete;

private:
std::stack<BreakTarget> &stack_;
};

std::unordered_set<const clang::VarDecl *> map_iter_decls_;

struct ScopedMapIterDecl {
Expand Down
52 changes: 52 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,4 +660,56 @@ clang::Expr *CreateConversionToBool(clang::Expr *expr, clang::ASTContext &ctx) {
/*BasePath=*/nullptr, clang::VK_PRValue, clang::FPOptionsOverride());
}

std::vector<clang::SwitchCase *>
GetTopLevelSwitchCases(clang::SwitchStmt *stmt) {
std::vector<clang::SwitchCase *> cases;
if (auto *body = llvm::dyn_cast<clang::CompoundStmt>(stmt->getBody())) {
for (auto *s : body->body()) {
if (auto *sc = clang::dyn_cast<clang::SwitchCase>(s)) {
cases.push_back(sc);
}
}
}
return cases;
}

bool SwitchCaseContainsDefault(clang::SwitchCase *c) {
for (clang::Stmt *cur = c;;) {
if (clang::isa<clang::DefaultStmt>(cur)) {
return true;
}
auto *sc = clang::dyn_cast<clang::SwitchCase>(cur);
if (!sc) {
return false;
}
cur = sc->getSubStmt();
}
return false;
}

static clang::Stmt *GetLastStmtOfSwitchCase(clang::SwitchCase *c) {
clang::Stmt *cur = c->getSubStmt();
while (auto *sc = clang::dyn_cast<clang::SwitchCase>(cur)) {
cur = sc->getSubStmt();
}
return cur;
}

std::vector<clang::Stmt *> GetSwitchCaseBody(clang::CompoundStmt *body,
clang::SwitchCase *head) {
std::vector<clang::Stmt *> out;
out.push_back(GetLastStmtOfSwitchCase(head));
auto it = body->body_begin(), end = body->body_end();
while (it != end && *it != head) {
++it;
}
assert(it != end);
++it;
while (it != end && !clang::isa<clang::SwitchCase>(*it)) {
out.push_back(*it);
++it;
}
return out;
}

} // namespace cpp2rust
8 changes: 8 additions & 0 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,12 @@ bool ContainsVAArgExpr(const clang::Stmt *stmt);

clang::Expr *CreateConversionToBool(clang::Expr *expr, clang::ASTContext &ctx);

std::vector<clang::SwitchCase *>
GetTopLevelSwitchCases(clang::SwitchStmt *stmt);

bool SwitchCaseContainsDefault(clang::SwitchCase *c);

std::vector<clang::Stmt *> GetSwitchCaseBody(clang::CompoundStmt *body,
clang::SwitchCase *head);

} // namespace cpp2rust
8 changes: 4 additions & 4 deletions tests/unit/out/refcount/switch_default_first.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ pub fn default_first_0(x: i32) -> i32 {
'switch: {
let __match_cond = (*x.borrow());
match __match_cond {
_ => {
(*r.borrow_mut()) = 7;
break 'switch;
}
v if v == 1 => {
(*r.borrow_mut()) = 1;
break 'switch;
Expand All @@ -24,6 +20,10 @@ pub fn default_first_0(x: i32) -> i32 {
(*r.borrow_mut()) = 2;
break 'switch;
}
_ => {
(*r.borrow_mut()) = 7;
break 'switch;
}
}
};
return (*r.borrow());
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/out/refcount/switch_default_middle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ pub fn default_middle_0(x: i32) -> i32 {
(*r.borrow_mut()) = 1;
break 'switch;
}
_ => {
(*r.borrow_mut()) = 99;
break 'switch;
}
v if v == 2 => {
(*r.borrow_mut()) = 2;
break 'switch;
}
_ => {
(*r.borrow_mut()) = 99;
break 'switch;
}
}
};
return (*r.borrow());
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/switch_for_in_switch_break.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn for_in_switch_break_0(n: i32) -> i32 {
let i: Value<i32> = Rc::new(RefCell::new(0));
'loop_: while ((*i.borrow()) < 10) {
if ((*i.borrow()) == 3) {
break 'switch;
break;
}
(*r.borrow_mut()) += (*i.borrow());
(*i.borrow_mut()).prefix_inc();
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/switch_for_switch_for_break.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn for_switch_for_break_0(n: i32) -> i32 {
let j: Value<i32> = Rc::new(RefCell::new(0));
'loop_: while ((*j.borrow()) < 10) {
if ((*j.borrow()) == 2) {
break 'switch;
break;
}
(*r.borrow_mut()) += 1;
(*j.borrow_mut()).prefix_inc();
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/out/refcount/switch_nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ pub fn nested_0(a: i32, b: i32) -> i32 {
}
};
(*r.borrow_mut()) += 1;
break;
break 'switch;
}
v if v == 2 => {
(*r.borrow_mut()) = 2;
break;
break 'switch;
}
_ => {
(*r.borrow_mut()) = -1_i32;
break;
break 'switch;
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/switch_while_in_switch_break.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn while_in_switch_break_0(n: i32) -> i32 {
let i: Value<i32> = Rc::new(RefCell::new(0));
'loop_: while ((*i.borrow()) < 10) {
if ((*i.borrow()) == 4) {
break 'switch;
break;
}
(*r.borrow_mut()) += (*i.borrow());
(*i.borrow_mut()).prefix_inc();
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/out/unsafe/switch_default_first.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ pub unsafe fn default_first_0(mut x: i32) -> i32 {
'switch: {
let __match_cond = x;
match __match_cond {
_ => {
r = 7;
break 'switch;
}
v if v == 1 => {
r = 1;
break 'switch;
Expand All @@ -23,6 +19,10 @@ pub unsafe fn default_first_0(mut x: i32) -> i32 {
r = 2;
break 'switch;
}
_ => {
r = 7;
break 'switch;
}
}
};
return r;
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/out/unsafe/switch_default_middle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ pub unsafe fn default_middle_0(mut x: i32) -> i32 {
r = 1;
break 'switch;
}
_ => {
r = 99;
break 'switch;
}
v if v == 2 => {
r = 2;
break 'switch;
}
_ => {
r = 99;
break 'switch;
}
}
};
return r;
Expand Down
Loading
Loading