Use shared_ptr refs where possible.

This commit is contained in:
Bhargava Shastry 2021-05-17 16:49:08 +02:00
parent 5b7f413bb2
commit f234c8e53f
3 changed files with 24 additions and 40 deletions

View File

@ -53,9 +53,10 @@ SolidityCustomMutatorInterface::SolidityCustomMutatorInterface(
):
data(_data),
size(_size),
maxMutantSize(_maxSize),
generator(make_shared<SolidityGenerator>(_seed))
{}
maxMutantSize(_maxSize)
{
generator = make_shared<SolidityGenerator>(_seed);
}
size_t SolidityCustomMutatorInterface::generate()
{

View File

@ -33,10 +33,9 @@ using namespace solidity::test::fuzzer::mutator;
using namespace solidity::util;
using namespace std;
GeneratorBase::GeneratorBase(SolidityGenerator* _mutator)
GeneratorBase::GeneratorBase(SolidityGenerator* _mutator): state(_mutator->testState())
{
mutator = _mutator;
state = mutator->testState();
}
string GeneratorBase::visitChildren()

View File

@ -347,10 +347,10 @@ struct FunctionState;
struct SourceState
{
explicit SourceState(
std::shared_ptr<UniformRandomDistribution> _urd,
std::shared_ptr<UniformRandomDistribution>& _urd,
std::string _sourceName
):
uRandDist(std::move(_urd)),
uRandDist(_urd),
importedSources({}),
sourceName(_sourceName)
{}
@ -417,7 +417,7 @@ struct SourceState
}
/// Prints source state to @param _os.
void print(std::ostream& _os) const;
std::shared_ptr<UniformRandomDistribution> uRandDist;
std::shared_ptr<UniformRandomDistribution>& uRandDist;
std::set<std::string> importedSources;
std::map<SolidityTypePtr, std::string> exports;
std::set<std::shared_ptr<FunctionState>> freeFunctions;
@ -485,10 +485,10 @@ struct FunctionState
struct ContractState
{
explicit ContractState(
std::shared_ptr<UniformRandomDistribution> _urd,
std::shared_ptr<UniformRandomDistribution>& _urd,
std::string _contractName
):
uRandDist(std::move(_urd)),
uRandDist(_urd),
name(_contractName)
{}
~ContractState()
@ -502,19 +502,19 @@ struct ContractState
}
std::set<std::shared_ptr<FunctionState>> functions;
std::shared_ptr<UniformRandomDistribution> uRandDist;
std::shared_ptr<UniformRandomDistribution>& uRandDist;
std::string name;
};
struct TestState
{
explicit TestState(std::shared_ptr<UniformRandomDistribution> _urd):
explicit TestState(std::shared_ptr<UniformRandomDistribution>& _urd):
sourceUnitState({}),
contractState({}),
currentSourceUnitPath({}),
currentContract({}),
currentFunction({}),
uRandDist(std::move(_urd)),
uRandDist(_urd),
numSourceUnits(0),
numContracts(0),
numFunctions(0),
@ -673,7 +673,7 @@ struct TestState
/// Current function
std::string currentFunction;
/// Uniform random distribution.
std::shared_ptr<UniformRandomDistribution> uRandDist;
std::shared_ptr<UniformRandomDistribution>& uRandDist;
/// Number of source units in test input
size_t numSourceUnits;
/// Number of contracts in test input
@ -694,14 +694,9 @@ struct TestState
struct TypeProvider
{
TypeProvider(std::shared_ptr<TestState> _state): state(std::move(_state))
TypeProvider(std::shared_ptr<TestState>& _state): state(_state)
{}
~TypeProvider()
{
state.reset();
}
enum class Type: size_t
{
INTEGER = 1,
@ -722,7 +717,7 @@ struct TypeProvider
return static_cast<Type>(state->uRandDist->distributionOneToN(static_cast<size_t>(Type::TYPEMAX) - 1));
}
std::shared_ptr<TestState> state;
std::shared_ptr<TestState>& state;
};
struct TypeComparator
@ -743,14 +738,9 @@ struct TypeComparator
struct LiteralGenerator
{
explicit LiteralGenerator(std::shared_ptr<TestState> _state): state(std::move(_state))
explicit LiteralGenerator(std::shared_ptr<TestState>& _state): state(_state)
{}
~LiteralGenerator()
{
state.reset();
}
std::string operator()(std::shared_ptr<AddressType> const& _type);
std::string operator()(std::shared_ptr<BoolType> const& _type);
std::string operator()(std::shared_ptr<BytesType> const& _type);
@ -759,19 +749,14 @@ struct LiteralGenerator
std::string operator()(std::shared_ptr<FunctionType> const& _type);
std::string operator()(std::shared_ptr<IntegerType> const& _type);
std::shared_ptr<TestState> state;
std::shared_ptr<TestState>& state;
};
struct ExpressionGenerator
{
ExpressionGenerator(std::shared_ptr<TestState> _state): state(std::move(_state))
ExpressionGenerator(std::shared_ptr<TestState>& _state): state(_state)
{}
~ExpressionGenerator()
{
state.reset();
}
enum class RLValueExpr: size_t
{
VARREF = 1,
@ -847,7 +832,7 @@ struct ExpressionGenerator
{
return nestingDepth > s_maxNestingDepth;
}
std::shared_ptr<TestState> state;
std::shared_ptr<TestState>& state;
unsigned nestingDepth;
static constexpr unsigned s_maxNestingDepth = 30;
};
@ -869,14 +854,14 @@ public:
std::shared_ptr<T> generator();
/// @returns a shared ptr to underlying random
/// number distribution.
std::shared_ptr<UniformRandomDistribution> uniformRandomDist()
std::shared_ptr<UniformRandomDistribution>& uniformRandomDist()
{
return m_urd;
}
/// @returns a pseudo randomly generated test case.
std::string generateTestProgram();
/// @returns shared ptr to global test state.
std::shared_ptr<TestState> testState()
std::shared_ptr<TestState>& testState()
{
return m_state;
}
@ -946,9 +931,8 @@ struct GeneratorBase
virtual ~GeneratorBase()
{
generators.clear();
state.reset();
}
std::shared_ptr<UniformRandomDistribution> uRandDist()
std::shared_ptr<UniformRandomDistribution>& uRandDist()
{
return mutator->uniformRandomDist();
}
@ -958,7 +942,7 @@ struct GeneratorBase
/// Set of generators used by this generator.
std::set<std::pair<GeneratorPtr, unsigned>> generators;
/// Shared ptr to global test state.
std::shared_ptr<TestState> state;
std::shared_ptr<TestState>& state;
};
class TestCaseGenerator: public GeneratorBase