c: don't generate while(condition) loops due to UB in C spec

See https://www.iso-9899.info/n1570.html#6.8.5p6

Fix #885
This commit is contained in:
Alexey Andreev 2024-02-19 20:35:21 +01:00
parent c95af17eae
commit 393cd7f807

View File

@ -1245,13 +1245,15 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
int statementId = registerIdentifiedStatement(statement); int statementId = registerIdentifiedStatement(statement);
writer.print("while ("); writer.println("while (1) {").indent();
// This can't be moved to 'while', since C11 standard allows removing infinite loops
// See https://www.iso-9899.info/n1570.html#6.8.5p6
if (statement.getCondition() != null) { if (statement.getCondition() != null) {
writer.print("if (!");
statement.getCondition().acceptVisitor(this); statement.getCondition().acceptVisitor(this);
} else { writer.println(") break;");
writer.print("1");
} }
writer.println(") {").indent();
boolean oldEnd = end; boolean oldEnd = end;
for (Statement part : statement.getBody()) { for (Statement part : statement.getBody()) {