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 0d8800eec4
commit 55932cf5eb
2 changed files with 20 additions and 10 deletions

View File

@ -518,6 +518,11 @@ string WhileStmtGenerator::visit()
pair<SolidityTypePtr, string> boolTypeName = {boolType, {}}; pair<SolidityTypePtr, string> boolTypeName = {boolType, {}};
auto expression = exprGen.rLValueOrLiteral(boolTypeName); auto expression = exprGen.rLValueOrLiteral(boolTypeName);
solAssert(expression.has_value(), ""); solAssert(expression.has_value(), "");
bool doWhile = uRandDist()->probable(2);
if (doWhile)
whileStmt << indentation()
<< "do\n";
else
whileStmt << indentation() whileStmt << indentation()
<< "while (" << "while ("
<< expression.value().second << expression.value().second
@ -529,6 +534,11 @@ string WhileStmtGenerator::visit()
if (whileBlock.str().empty()) if (whileBlock.str().empty())
whileBlock << indentation() << "{ }\n"; whileBlock << indentation() << "{ }\n";
whileStmt << whileBlock.str(); whileStmt << whileBlock.str();
if (doWhile)
whileStmt << indentation()
<< "while ("
<< expression.value().second
<< ")\n";
return whileStmt.str(); return whileStmt.str();
} }

View File

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