Skip to content

Commit c4880c0

Browse files
committed
Move helper switch/case functions in converter_lib
1 parent 614e78e commit c4880c0

3 files changed

Lines changed: 88 additions & 52 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,58 +2617,6 @@ bool Converter::VisitImplicitValueInitExpr(clang::ImplicitValueInitExpr *expr) {
26172617
return false;
26182618
}
26192619

2620-
static std::vector<clang::SwitchCase *>
2621-
GetTopLevelSwitchCases(clang::SwitchStmt *stmt) {
2622-
std::vector<clang::SwitchCase *> cases;
2623-
if (auto *body = llvm::dyn_cast<clang::CompoundStmt>(stmt->getBody())) {
2624-
for (auto *s : body->body()) {
2625-
if (auto *sc = clang::dyn_cast<clang::SwitchCase>(s)) {
2626-
cases.push_back(sc);
2627-
}
2628-
}
2629-
}
2630-
return cases;
2631-
}
2632-
2633-
static bool ChainContainsDefault(clang::SwitchCase *c) {
2634-
for (clang::Stmt *cur = c;;) {
2635-
if (clang::isa<clang::DefaultStmt>(cur)) {
2636-
return true;
2637-
}
2638-
auto *sc = clang::dyn_cast<clang::SwitchCase>(cur);
2639-
if (!sc) {
2640-
return false;
2641-
}
2642-
cur = sc->getSubStmt();
2643-
}
2644-
return false;
2645-
}
2646-
2647-
static clang::Stmt *ChainLeafBody(clang::SwitchCase *c) {
2648-
clang::Stmt *cur = c->getSubStmt();
2649-
while (auto *sc = clang::dyn_cast<clang::SwitchCase>(cur)) {
2650-
cur = sc->getSubStmt();
2651-
}
2652-
return cur;
2653-
}
2654-
2655-
static std::vector<clang::Stmt *> GetSwitchArmBody(clang::CompoundStmt *body,
2656-
clang::SwitchCase *head) {
2657-
std::vector<clang::Stmt *> out;
2658-
out.push_back(ChainLeafBody(head));
2659-
auto it = body->body_begin(), end = body->body_end();
2660-
while (it != end && *it != head) {
2661-
++it;
2662-
}
2663-
assert(it != end);
2664-
++it;
2665-
while (it != end && !clang::isa<clang::SwitchCase>(*it)) {
2666-
out.push_back(*it);
2667-
++it;
2668-
}
2669-
return out;
2670-
}
2671-
26722620
bool Converter::VisitSwitchCase(clang::SwitchCase *stmt) {
26732621
clang::Stmt *cur = stmt;
26742622
clang::SwitchCase *last = nullptr;

cpp2rust/converter/converter_lib.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,4 +660,78 @@ clang::Expr *CreateConversionToBool(clang::Expr *expr, clang::ASTContext &ctx) {
660660
/*BasePath=*/nullptr, clang::VK_PRValue, clang::FPOptionsOverride());
661661
}
662662

663+
std::vector<clang::SwitchCase *>
664+
GetTopLevelSwitchCases(clang::SwitchStmt *stmt) {
665+
std::vector<clang::SwitchCase *> cases;
666+
if (auto *body = llvm::dyn_cast<clang::CompoundStmt>(stmt->getBody())) {
667+
for (auto *s : body->body()) {
668+
if (auto *sc = clang::dyn_cast<clang::SwitchCase>(s)) {
669+
cases.push_back(sc);
670+
}
671+
}
672+
}
673+
return cases;
674+
}
675+
676+
bool ChainContainsDefault(clang::SwitchCase *c) {
677+
for (clang::Stmt *cur = c;;) {
678+
if (clang::isa<clang::DefaultStmt>(cur)) {
679+
return true;
680+
}
681+
auto *sc = clang::dyn_cast<clang::SwitchCase>(cur);
682+
if (!sc) {
683+
return false;
684+
}
685+
cur = sc->getSubStmt();
686+
}
687+
return false;
688+
}
689+
690+
clang::Stmt *ChainLeafBody(clang::SwitchCase *c) {
691+
clang::Stmt *cur = c->getSubStmt();
692+
while (auto *sc = clang::dyn_cast<clang::SwitchCase>(cur)) {
693+
cur = sc->getSubStmt();
694+
}
695+
return cur;
696+
}
697+
698+
std::vector<clang::Stmt *> GetSwitchArmBody(clang::CompoundStmt *body,
699+
clang::SwitchCase *head) {
700+
std::vector<clang::Stmt *> out;
701+
out.push_back(ChainLeafBody(head));
702+
auto it = body->body_begin(), end = body->body_end();
703+
while (it != end && *it != head) {
704+
++it;
705+
}
706+
assert(it != end);
707+
++it;
708+
while (it != end && !clang::isa<clang::SwitchCase>(*it)) {
709+
out.push_back(*it);
710+
++it;
711+
}
712+
return out;
713+
}
714+
715+
bool SwitchArmHasFallthrough(clang::Stmt *stmt) {
716+
if (stmt) {
717+
if (clang::isa<clang::BreakStmt>(stmt) ||
718+
clang::isa<clang::ReturnStmt>(stmt)) {
719+
return false;
720+
}
721+
}
722+
return true;
723+
}
724+
725+
bool SwitchHasFallthrough(clang::SwitchStmt *stmt) {
726+
if (auto *body = clang::dyn_cast<clang::CompoundStmt>(stmt->getBody())) {
727+
for (auto top_level_case : GetTopLevelSwitchCases(stmt)) {
728+
auto arm = GetSwitchArmBody(body, top_level_case);
729+
if (arm.empty() || SwitchArmHasFallthrough(arm.back())) {
730+
return true;
731+
}
732+
}
733+
}
734+
return false;
735+
}
736+
663737
} // namespace cpp2rust

cpp2rust/converter/converter_lib.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,18 @@ bool ContainsVAArgExpr(const clang::Stmt *stmt);
154154

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

157+
std::vector<clang::SwitchCase *>
158+
GetTopLevelSwitchCases(clang::SwitchStmt *stmt);
159+
160+
bool ChainContainsDefault(clang::SwitchCase *c);
161+
162+
clang::Stmt *ChainLeafBody(clang::SwitchCase *c);
163+
164+
std::vector<clang::Stmt *> GetSwitchArmBody(clang::CompoundStmt *body,
165+
clang::SwitchCase *head);
166+
167+
bool SwitchArmHasFallthrough(clang::Stmt *stmt);
168+
169+
bool SwitchHasFallthrough(clang::SwitchStmt *stmt);
170+
157171
} // namespace cpp2rust

0 commit comments

Comments
 (0)