diff --git a/test/tools/ossfuzz/Generators.h b/test/tools/ossfuzz/Generators.h index 50f80b488..4fd3026ea 100644 --- a/test/tools/ossfuzz/Generators.h +++ b/test/tools/ossfuzz/Generators.h @@ -47,6 +47,7 @@ MACRO(ExpressionStmtGenerator) SEP \ MACRO(FunctionCallGenerator) SEP \ MACRO(FunctionGenerator) SEP \ + MACRO(IfStmtGenerator) SEP \ MACRO(ImportGenerator) SEP \ MACRO(PragmaGenerator) SEP \ MACRO(SourceUnitGenerator) SEP \ diff --git a/test/tools/ossfuzz/SolidityGenerator.cpp b/test/tools/ossfuzz/SolidityGenerator.cpp index 0b04edcf7..e5a66b3a4 100644 --- a/test/tools/ossfuzz/SolidityGenerator.cpp +++ b/test/tools/ossfuzz/SolidityGenerator.cpp @@ -436,13 +436,45 @@ string ExpressionStmtGenerator::visit() return "\n"; } +void IfStmtGenerator::setup() +{ + set> dependsOn = { + {mutator->generator(), 1}, + }; + addGenerators(std::move(dependsOn)); +} + +string IfStmtGenerator::visit() +{ + ostringstream ifStmt; + ExpressionGenerator exprGen{state}; + auto boolType = make_shared(); + auto expression = exprGen.rOrLValueExpression({boolType, {}}); + if (expression.has_value()) + ifStmt << indentation() + << "if (" + << expression.value().second + << ")\n"; + else + return "\n"; + // Make sure block stmt generator does not output an unchecked block + mutator->generator()->unchecked(false); + ostringstream ifBlock; + ifBlock << visitChildren(); + if (ifBlock.str().empty()) + ifBlock << indentation() << "{ }\n"; + ifStmt << ifBlock.str(); + return ifStmt.str(); +} + void StatementGenerator::setup() { set> dependsOn = { {mutator->generator(), 1}, {mutator->generator(), 1}, {mutator->generator(), 1}, - {mutator->generator(), 1} + {mutator->generator(), 1}, + {mutator->generator(), 2}, }; addGenerators(std::move(dependsOn)); } @@ -488,7 +520,7 @@ void BlockStmtGenerator::setup() string BlockStmtGenerator::visit() { if (nestingTooDeep()) - return "\n"; + return indentation() + "{ }\n"; incrementNestingDepth(); ostringstream block; if (unchecked() && !m_inUnchecked) diff --git a/test/tools/ossfuzz/SolidityGenerator.h b/test/tools/ossfuzz/SolidityGenerator.h index 00cf754a4..07291ae0e 100644 --- a/test/tools/ossfuzz/SolidityGenerator.h +++ b/test/tools/ossfuzz/SolidityGenerator.h @@ -1068,6 +1068,17 @@ public: } }; +class IfStmtGenerator: public GeneratorBase +{ +public: + explicit IfStmtGenerator(SolidityGenerator* _mutator): + GeneratorBase(std::move(_mutator)) + {} + void setup() override; + std::string visit() override; + std::string name() override { return "If statement generator"; } +}; + class AssignmentStmtGenerator: public GeneratorBase { public: