Resolve imported symbols

This commit is contained in:
Bhargava Shastry 2021-04-26 12:17:19 +02:00
parent bc53705e06
commit 99adff305c
2 changed files with 42 additions and 2 deletions

View File

@ -182,6 +182,9 @@ string ImportGenerator::visit()
<< importPath << importPath
<< "\";\n"; << "\";\n";
state->sourceUnitState[state->currentPath()]->addImportedSourcePath(importPath); state->sourceUnitState[state->currentPath()]->addImportedSourcePath(importPath);
state->sourceUnitState[state->currentPath()]->resolveImports(
state->sourceUnitState[importPath]->exports
);
} }
return os.str(); return os.str();
} }
@ -204,9 +207,15 @@ string ContractGenerator::visit()
mutator->generator<FunctionGenerator>()->scope(false); mutator->generator<FunctionGenerator>()->scope(false);
}; };
ostringstream os; ostringstream os;
string inheritance;
if (state->sourceUnitState[state->currentPath()]->contractType())
inheritance = state->sourceUnitState[state->currentPath()]->randomContract();
string name = state->newContract(); string name = state->newContract();
state->updateContract(name); state->updateContract(name);
os << "contract " << name << " {" << endl; os << "contract " << name;
if (!inheritance.empty())
os << " is " << inheritance;
os << " {" << endl;
set(); set();
os << visitChildren(); os << visitChildren();
os << "}" << endl; os << "}" << endl;

View File

@ -30,7 +30,11 @@
#include <random> #include <random>
#include <set> #include <set>
#include <variant> #include <variant>
#include <range/v3/action/transform.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/filter.hpp> #include <range/v3/view/filter.hpp>
#include <range/v3/view/transform.hpp>
namespace solidity::test::fuzzer::mutator namespace solidity::test::fuzzer::mutator
{ {
@ -113,12 +117,16 @@ struct ContractState
std::shared_ptr<UniformRandomDistribution> uRandDist; std::shared_ptr<UniformRandomDistribution> uRandDist;
}; };
struct Type {}; struct Type
{
virtual ~Type() {}
};
struct FunctionType: Type struct FunctionType: Type
{ {
std::vector<Type> inputs; std::vector<Type> inputs;
std::vector<Type> outputs; std::vector<Type> outputs;
}; };
struct ContractType: Type {};
struct SourceState struct SourceState
{ {
@ -134,10 +142,32 @@ struct SourceState
{ {
return !(exports | ranges::views::filter([&_functionName](auto& _p) { return _p.second == _functionName; })).empty(); return !(exports | ranges::views::filter([&_functionName](auto& _p) { return _p.second == _functionName; })).empty();
} }
bool contractType()
{
return !(exports | ranges::views::filter([](auto& _i) {
return bool(std::dynamic_pointer_cast<ContractType>(_i.first));
})).empty();
}
std::string randomContract()
{
auto contracts = exports |
ranges::views::filter([](auto& _item) -> bool {
return bool(std::dynamic_pointer_cast<ContractType>(_item.first));
}) |
ranges::views::transform([](auto& _item) -> std::string {
return _item.second;
}) | ranges::to<std::vector<std::string>>;
return contracts[uRandDist->distributionOneToN(contracts.size()) - 1];
}
void addImportedSourcePath(std::string& _sourcePath) void addImportedSourcePath(std::string& _sourcePath)
{ {
importedSources.emplace(_sourcePath); importedSources.emplace(_sourcePath);
} }
void resolveImports(std::map<std::shared_ptr<Type>, std::string> _imports)
{
for (auto const& item: _imports)
exports.emplace(item);
}
[[nodiscard]] bool sourcePathImported(std::string const& _sourcePath) const [[nodiscard]] bool sourcePathImported(std::string const& _sourcePath) const
{ {
return importedSources.count(_sourcePath); return importedSources.count(_sourcePath);
@ -181,6 +211,7 @@ struct TestState
void addContract(std::string const& _name) void addContract(std::string const& _name)
{ {
contractState.emplace(_name, std::make_shared<ContractState>(uRandDist)); contractState.emplace(_name, std::make_shared<ContractState>(uRandDist));
sourceUnitState[currentSourceUnitPath]->exports[std::make_shared<ContractType>()] = _name;
currentContract = _name; currentContract = _name;
} }
void addFunction(std::string const& _name) void addFunction(std::string const& _name)