Fix bug in break/continue placement and introduce do-while loops.

This commit is contained in:
Bhargava Shastry 2021-06-08 14:17:56 +02:00
parent 67644c0763
commit c07d24c66d
2 changed files with 20 additions and 10 deletions

View File

@ -518,10 +518,15 @@ string WhileStmtGenerator::visit()
pair<SolidityTypePtr, string> boolTypeName = {boolType, {}};
auto expression = exprGen.rLValueOrLiteral(boolTypeName);
solAssert(expression.has_value(), "");
whileStmt << indentation()
<< "while ("
<< expression.value().second
<< ")\n";
bool doWhile = uRandDist()->probable(2);
if (doWhile)
whileStmt << indentation()
<< "do\n";
else
whileStmt << indentation()
<< "while ("
<< expression.value().second
<< ")\n";
// Make sure block stmt generator does not output an unchecked block
mutator->generator<BlockStmtGenerator>()->unchecked(false);
ostringstream whileBlock;
@ -529,6 +534,11 @@ string WhileStmtGenerator::visit()
if (whileBlock.str().empty())
whileBlock << indentation() << "{ }\n";
whileStmt << whileBlock.str();
if (doWhile)
whileStmt << indentation()
<< "while ("
<< expression.value().second
<< ")\n";
return whileStmt.str();
}

View File

@ -30,6 +30,7 @@
#include <memory>
#include <random>
#include <set>
#include <stack>
#include <variant>
#include <range/v3/algorithm/all_of.hpp>
@ -525,7 +526,7 @@ struct TestState
numFunctions(0),
indentationLevel(0),
insideContract(false),
loopState(false, false)
loopState({false})
{}
/// Adds @param _path to @name sourceUnitPaths updates
/// @name currentSourceUnitPath.
@ -652,16 +653,15 @@ struct TestState
}
void enterLoop()
{
loopState.wasLoop = loopState.inLoop;
loopState.inLoop = true;
loopState.push(true);
}
void exitLoop()
{
loopState.inLoop = loopState.wasLoop;
loopState.pop();
}
bool inLoop()
{
return loopState.inLoop;
return loopState.top();
}
~TestState()
{
@ -703,7 +703,7 @@ struct TestState
/// Contract scope
bool insideContract;
/// Loop state
LoopState loopState;
std::stack<bool> loopState;
/// Source name prefix
std::string const sourceUnitNamePrefix = "su";
/// Contract name prefix