@@ -435,25 +435,17 @@ void Converter::ConvertGotoBlock(clang::CompoundStmt *body) {
435435}
436436
437437void Converter::ConvertFunctionBody (clang::FunctionDecl *decl) {
438- if (auto compound = clang::dyn_cast<clang::CompoundStmt>(decl->getBody ())) {
439- if (CompoundHasTopLevelLabel (compound)) {
440- ConvertGotoBlock (compound);
441- if (!decl->getReturnType ()->isVoidType ()) {
442- StrCat (R"( panic!("ub: non-void function does not return a value"))" );
443- }
444- return ;
445- }
438+ ConvertBodyStmts (decl->getBody ());
439+ if (decl->getReturnType ()->isVoidType ()) {
440+ return ;
446441 }
447-
448- Convert (decl->getBody ());
449- if (!decl->getReturnType ()->isVoidType ()) {
450- if (auto compound = clang::dyn_cast<clang::CompoundStmt>(decl->getBody ())) {
451- if (!compound->body_empty ()) {
452- if (!clang::isa<clang::ReturnStmt>(compound->body_back ())) {
453- StrCat (R"( panic!("ub: non-void function does not return a value"))" );
454- }
455- }
456- }
442+ auto compound = clang::dyn_cast<clang::CompoundStmt>(decl->getBody ());
443+ if (!compound || compound->body_empty ()) {
444+ return ;
445+ }
446+ if (CompoundHasTopLevelLabel (compound) ||
447+ !clang::isa<clang::ReturnStmt>(compound->body_back ())) {
448+ StrCat (R"( panic!("ub: non-void function does not return a value"))" );
457449 }
458450}
459451
@@ -1057,7 +1049,7 @@ void Converter::ConvertCXXConstructorBody(clang::CXXConstructorDecl *decl) {
10571049 }
10581050
10591051 StrCat (token::kSemiColon );
1060- Convert (decl->getBody ());
1052+ ConvertBodyStmts (decl->getBody ());
10611053 StrCat (" this" );
10621054}
10631055
@@ -1127,14 +1119,28 @@ bool Converter::Convert(clang::Stmt *stmt) {
11271119 return exited_visit;
11281120}
11291121
1130- bool Converter::VisitCompoundStmt (clang::CompoundStmt *stmt) {
1131- if (CompoundHasTopLevelLabel (stmt)) {
1132- ConvertGotoBlock (stmt);
1133- return false ;
1122+ void Converter::ConvertBody (clang::Stmt *body) {
1123+ PushBrace brace (*this );
1124+ ConvertBodyStmts (body);
1125+ }
1126+
1127+ void Converter::ConvertBodyStmts (clang::Stmt *body) {
1128+ auto *compound = clang::dyn_cast_or_null<clang::CompoundStmt>(body);
1129+ if (!compound) {
1130+ Convert (body);
1131+ return ;
11341132 }
1135- for (auto *child : stmt->body ()) {
1133+ if (CompoundHasTopLevelLabel (compound)) {
1134+ ConvertGotoBlock (compound);
1135+ return ;
1136+ }
1137+ for (auto *child : compound->body ()) {
11361138 Convert (child);
11371139 }
1140+ }
1141+
1142+ bool Converter::VisitCompoundStmt (clang::CompoundStmt *stmt) {
1143+ ConvertBody (stmt);
11381144 return false ;
11391145}
11401146
@@ -1172,17 +1178,13 @@ void Converter::ConvertCondition(clang::Expr *cond) {
11721178bool Converter::VisitIfStmt (clang::IfStmt *stmt) {
11731179 StrCat (keyword::kIf );
11741180 ConvertCondition (stmt->getCond ());
1175- {
1176- PushBrace brace (*this );
1177- Convert (stmt->getThen ());
1178- }
1181+ ConvertBody (stmt->getThen ());
11791182 if (stmt->hasElseStorage ()) {
11801183 StrCat (keyword::kElse );
11811184 if (clang::isa<clang::IfStmt>(stmt->getElse ())) {
11821185 Convert (stmt->getElse ());
11831186 } else {
1184- PushBrace brace (*this );
1185- Convert (stmt->getElse ());
1187+ ConvertBody (stmt->getElse ());
11861188 }
11871189 }
11881190 return false ;
@@ -1193,12 +1195,9 @@ bool Converter::VisitWhileStmt(clang::WhileStmt *stmt) {
11931195 StrCat (" 'loop_:" );
11941196 StrCat (keyword::kWhile );
11951197 ConvertCondition (stmt->getCond ());
1196- {
1197- PushBrace brace (*this );
1198- curr_for_inc_.emplace_back (nullptr );
1199- Convert (stmt->getBody ());
1200- curr_for_inc_.pop_back ();
1201- }
1198+ curr_for_inc_.emplace_back (nullptr );
1199+ ConvertBody (stmt->getBody ());
1200+ curr_for_inc_.pop_back ();
12021201 return false ;
12031202}
12041203
@@ -1216,7 +1215,7 @@ bool Converter::VisitDoStmt(clang::DoStmt *stmt) {
12161215 PushBrace loop_brace (*this );
12171216 StrCat (control_var, token::kAssign , keyword::kFalse , token::kSemiColon );
12181217 curr_for_inc_.emplace_back (nullptr );
1219- Convert (stmt->getBody ());
1218+ ConvertBodyStmts (stmt->getBody ());
12201219 curr_for_inc_.pop_back ();
12211220 }
12221221 return false ;
@@ -1235,7 +1234,7 @@ bool Converter::VisitForStmt(clang::ForStmt *stmt) {
12351234 {
12361235 PushBrace brace (*this );
12371236 curr_for_inc_.emplace_back (stmt->getInc ());
1238- Convert (stmt->getBody ());
1237+ ConvertBodyStmts (stmt->getBody ());
12391238 curr_for_inc_.pop_back ();
12401239 Convert (stmt->getInc ());
12411240 StrCat (token::kSemiColon );
@@ -1273,7 +1272,7 @@ void Converter::ConvertForRangeBody(clang::CXXForRangeStmt *stmt,
12731272 if (map_iter_decl)
12741273 skip.emplace (*this , map_iter_decl);
12751274 curr_for_inc_.emplace_back (nullptr );
1276- Convert (stmt->getBody ());
1275+ ConvertBodyStmts (stmt->getBody ());
12771276 curr_for_inc_.pop_back ();
12781277}
12791278
0 commit comments