Skip to content

Commit 4c601da

Browse files
committed
Move visited_cases in class scope
Rename visited_cases to visited_switch_cases_ and move it in class scope. VisitSwitchStmt always resets the set of visited switch cases. The old implementation was buggy because it saved reused SwitchCase pointers across translation units, making contain() return true for switches declared in other TUs.
1 parent ff93441 commit 4c601da

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,13 +2579,11 @@ bool Converter::VisitImplicitValueInitExpr(clang::ImplicitValueInitExpr *expr) {
25792579
return false;
25802580
}
25812581

2582-
static std::unordered_set<clang::SwitchCase *> visited_cases;
2583-
25842582
bool Converter::VisitSwitchCase(clang::SwitchCase *stmt) {
2585-
if (visited_cases.contains(stmt)) {
2583+
if (visited_switch_cases_.contains(stmt)) {
25862584
return false;
25872585
}
2588-
visited_cases.insert(stmt);
2586+
visited_switch_cases_.insert(stmt);
25892587

25902588
if (auto case_stmt = clang::dyn_cast<clang::CaseStmt>(stmt)) {
25912589
Convert(case_stmt->getLHS());
@@ -2615,6 +2613,8 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
26152613
auto body = llvm::cast<clang::CompoundStmt>(stmt->getBody());
26162614
assert(body);
26172615

2616+
visited_switch_cases_ = {};
2617+
26182618
break_with_explicit_label_ = true;
26192619
for (auto it = body->body_begin(), end = body->body_end(); it != end;) {
26202620
if (auto switch_case = clang::dyn_cast<clang::SwitchCase>(*it)) {

cpp2rust/converter/converter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
462462
bool break_with_explicit_label_ = false;
463463
std::stack<clang::Expr *> curr_for_inc_;
464464
std::stack<clang::QualType> curr_init_type_;
465+
std::unordered_set<clang::SwitchCase *> visited_switch_cases_;
465466

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

0 commit comments

Comments
 (0)