mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Introduce require/assert statements.
This commit is contained in:
parent
8cbdd1fc2f
commit
bec78e58bf
@ -52,6 +52,7 @@
|
|||||||
MACRO(FunctionGenerator) SEP \
|
MACRO(FunctionGenerator) SEP \
|
||||||
MACRO(IfStmtGenerator) SEP \
|
MACRO(IfStmtGenerator) SEP \
|
||||||
MACRO(ImportGenerator) SEP \
|
MACRO(ImportGenerator) SEP \
|
||||||
|
MACRO(MagicStmtGenerator) SEP \
|
||||||
MACRO(PragmaGenerator) SEP \
|
MACRO(PragmaGenerator) SEP \
|
||||||
MACRO(SourceUnitGenerator) SEP \
|
MACRO(SourceUnitGenerator) SEP \
|
||||||
MACRO(StatementGenerator) SEP \
|
MACRO(StatementGenerator) SEP \
|
||||||
|
|||||||
@ -658,7 +658,8 @@ void StatementGenerator::setup()
|
|||||||
{mutator->generator<BreakStmtGenerator>(), 1},
|
{mutator->generator<BreakStmtGenerator>(), 1},
|
||||||
{mutator->generator<ContinueStmtGenerator>(), 1},
|
{mutator->generator<ContinueStmtGenerator>(), 1},
|
||||||
{mutator->generator<VarDeclStmtGenerator>(), 1},
|
{mutator->generator<VarDeclStmtGenerator>(), 1},
|
||||||
{mutator->generator<ForStmtGenerator>(), 1}
|
{mutator->generator<ForStmtGenerator>(), 1},
|
||||||
|
{mutator->generator<MagicStmtGenerator>(), 1}
|
||||||
};
|
};
|
||||||
addGenerators(std::move(dependsOn));
|
addGenerators(std::move(dependsOn));
|
||||||
}
|
}
|
||||||
@ -1597,6 +1598,40 @@ string FunctionCallGenerator::visit()
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string MagicStmtGenerator::visit()
|
||||||
|
{
|
||||||
|
MagicId m = static_cast<MagicId>(uRandDist()->distributionOneToN(static_cast<size_t>(MagicId::MAGICMAX) - 1));
|
||||||
|
switch (m)
|
||||||
|
{
|
||||||
|
case MagicId::ASSERT:
|
||||||
|
{
|
||||||
|
ExpressionGenerator exprGen{state};
|
||||||
|
auto boolType = make_shared<BoolType>();
|
||||||
|
pair<SolidityTypePtr, string> boolTypeName = {boolType, {}};
|
||||||
|
auto expression = exprGen.rLValueOrLiteral(boolTypeName);
|
||||||
|
solAssert(expression.has_value(), "");
|
||||||
|
return indentation() +
|
||||||
|
"assert(" +
|
||||||
|
expression.value().second +
|
||||||
|
");\n";
|
||||||
|
}
|
||||||
|
case MagicId::REQUIRE:
|
||||||
|
{
|
||||||
|
ExpressionGenerator exprGen{state};
|
||||||
|
auto boolType = make_shared<BoolType>();
|
||||||
|
pair<SolidityTypePtr, string> boolTypeName = {boolType, {}};
|
||||||
|
auto expression = exprGen.rLValueOrLiteral(boolTypeName);
|
||||||
|
solAssert(expression.has_value(), "");
|
||||||
|
return indentation() +
|
||||||
|
"require(" +
|
||||||
|
expression.value().second +
|
||||||
|
");\n";
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
solAssert(false, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
shared_ptr<T> SolidityGenerator::generator()
|
shared_ptr<T> SolidityGenerator::generator()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1286,4 +1286,23 @@ private:
|
|||||||
std::optional<std::string> rhs(std::vector<std::pair<SolidityTypePtr, std::string>>& _functionInputTypeNames);
|
std::optional<std::string> rhs(std::vector<std::pair<SolidityTypePtr, std::string>>& _functionInputTypeNames);
|
||||||
std::string callStmt(std::shared_ptr<FunctionState> _callee);
|
std::string callStmt(std::shared_ptr<FunctionState> _callee);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class MagicStmtGenerator: public GeneratorBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum class MagicId: size_t
|
||||||
|
{
|
||||||
|
ASSERT = 1,
|
||||||
|
REQUIRE,
|
||||||
|
MAGICMAX
|
||||||
|
};
|
||||||
|
MagicStmtGenerator(SolidityGenerator* _mutator):
|
||||||
|
GeneratorBase(std::move(_mutator))
|
||||||
|
{}
|
||||||
|
std::string visit() override;
|
||||||
|
std::string name() override
|
||||||
|
{
|
||||||
|
return "Magic statement generator";
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user