Implement block nesting depth properly

This commit is contained in:
Bhargava Shastry 2021-05-01 15:32:07 +02:00
parent 638ab0bb68
commit 1c43ecf9c8
2 changed files with 27 additions and 8 deletions

View File

@ -301,7 +301,6 @@ string BlockStmtGenerator::visit()
{ {
if (nestingTooDeep()) if (nestingTooDeep())
return "\n"; return "\n";
incrementNestingDepth(); incrementNestingDepth();
ostringstream block; ostringstream block;
block << indentation() + "{\n"; block << indentation() + "{\n";
@ -345,11 +344,21 @@ string FunctionGenerator::visit()
if (!state->currentFunctionState()->outputs.empty()) if (!state->currentFunctionState()->outputs.empty())
function << " returns" function << " returns"
<< state->currentFunctionState()->params(FunctionState::Params::OUTPUT); << state->currentFunctionState()->params(FunctionState::Params::OUTPUT);
function << "\n" << generator<BlockStmtGenerator>()->visit(); ostringstream block;
generator<BlockStmtGenerator>()->endVisit(); // Since visitChildren() may not visit block stmt, we default to an empty
// block.
block << visitChildren();
if (block.str().empty())
block << indentation() << "{ }\n";
function << "\n" << block.str();
return function.str(); return function.str();
} }
void FunctionGenerator::endVisit()
{
mutator->generator<BlockStmtGenerator>()->resetNestingDepth();
}
pair<SolidityTypePtr, string> ExpressionGenerator::randomLValueExpression() pair<SolidityTypePtr, string> ExpressionGenerator::randomLValueExpression()
{ {
LValueExpr exprType = static_cast<LValueExpr>( LValueExpr exprType = static_cast<LValueExpr>(

View File

@ -288,13 +288,13 @@ public:
{ {
return name(); return name();
} }
std::string name() std::string name() const
{ {
return contractName; return contractName;
} }
bool operator==(ContractType const&) bool operator==(ContractType const& _rhs)
{ {
return true; return _rhs.name() == this->name();
} }
std::string contractName; std::string contractName;
}; };
@ -530,7 +530,8 @@ struct TestState
} }
void addSource() void addSource()
{ {
updateSourcePath(newPath()); std::string path = newPath();
updateSourcePath(path);
} }
/// Increments indentation level globally. /// Increments indentation level globally.
void indent() void indent()
@ -806,6 +807,7 @@ public:
{} {}
std::string visit() override; std::string visit() override;
std::string name() override { return "Function generator"; } std::string name() override { return "Function generator"; }
void endVisit() override;
void setup() override; void setup() override;
/// Sets @name m_freeFunction to @param _freeFunction. /// Sets @name m_freeFunction to @param _freeFunction.
void scope(bool _freeFunction) void scope(bool _freeFunction)
@ -849,12 +851,20 @@ public:
{} {}
void endVisit() override void endVisit() override
{ {
m_nestingDepth = 0; decrementNestingDepth();
} }
void incrementNestingDepth() void incrementNestingDepth()
{ {
++m_nestingDepth; ++m_nestingDepth;
} }
void decrementNestingDepth()
{
--m_nestingDepth;
}
void resetNestingDepth()
{
m_nestingDepth = 0;
}
bool nestingTooDeep() bool nestingTooDeep()
{ {
return m_nestingDepth > s_maxNestingDepth; return m_nestingDepth > s_maxNestingDepth;