Skip to content

Commit

Permalink
Fixed #678: Second attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasfertig committed Jan 10, 2025
1 parent 9b66839 commit 2976ec7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ void CodeGenerator::InsertArg(const SwitchStmt* stmt)

void CodeGenerator::InsertArg(const WhileStmt* stmt)
{
const auto* conditionVar{stmt->getConditionVariable()};
auto* rwStmt = const_cast<WhileStmt*>(stmt);
auto* conditionVar{rwStmt->getConditionVariable()};

{
// We need to handle the case that a lambda is used in the init-statement of the for-loop.
Expand All @@ -571,7 +572,17 @@ void CodeGenerator::InsertArg(const WhileStmt* stmt)
WrapInParens([&]() { InsertArg(stmt->getCond()); }, AddSpaceAtTheEnd::Yes);
}

WrapInCompoundIfNeeded(stmt->getBody(), AddNewLineAfter::Yes);
if(not conditionVar) {
WrapInCompoundIfNeeded(stmt->getBody(), AddNewLineAfter::Yes);
} else {
const auto& ctx = GetGlobalAST();
StmtsContainer bodyStmts{};

bodyStmts.AddBodyStmts(rwStmt->getBody());
bodyStmts.AddBodyStmts(Assign(conditionVar, conditionVar->getInit()));

InsertArg(mkCompoundStmt(bodyStmts, stmt->getBeginLoc(), stmt->getEndLoc()));
}

if(conditionVar) {
mOutputFormatHelper.CloseScope();
Expand Down
2 changes: 1 addition & 1 deletion tests/Issue678.expect
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ int main()
break;
}

i = next();
}

}
return 0;
}

0 comments on commit 2976ec7

Please sign in to comment.