Merge pull request #6309 from ethereum/proto-add-str-hex-lit

yul proto: Add support for generating string and hex literals.
This commit is contained in:
Alex Beregszaszi 2019-03-21 21:47:58 +00:00 committed by GitHub
commit 7d94d3af20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 2 deletions

View File

@ -19,10 +19,40 @@
#include <ostream>
#include <sstream>
#include <boost/range/algorithm_ext/erase.hpp>
using namespace std;
using namespace yul::test::yul_fuzzer;
std::string yul::test::yul_fuzzer::createHex(std::string const& _hexBytes)
{
std::string tmp{_hexBytes};
if (!tmp.empty())
{
boost::range::remove_erase_if(tmp, [=](char c) -> bool {
return !std::isxdigit(c);
});
tmp = tmp.substr(0, 64);
}
// We need this awkward if case hex literals cannot be empty.
if (tmp.empty())
tmp = "1";
return tmp;
}
std::string yul::test::yul_fuzzer::createAlphaNum(std::string const& _strBytes)
{
std::string tmp{_strBytes};
if (!tmp.empty())
{
boost::range::remove_erase_if(tmp, [=](char c) -> bool {
return !(std::isalpha(c) || std::isdigit(c));
});
tmp = tmp.substr(0, 32);
}
return tmp;
}
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, Literal const& _x)
{
switch (_x.literal_oneof_case())
@ -30,6 +60,12 @@ ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, Literal const& _x)
case Literal::kIntval:
_os << _x.intval();
break;
case Literal::kHexval:
_os << "0x" << createHex(_x.hexval());
break;
case Literal::kStrval:
_os << "\"" << createAlphaNum(_x.strval()) << "\"";
break;
case Literal::LITERAL_ONEOF_NOT_SET:
_os << "1";
break;
@ -301,9 +337,7 @@ ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, Block const& _x)
_os << "}\n";
}
else
{
_os << "{}\n";
}
return _os;
}

View File

@ -31,6 +31,8 @@ class Function;
std::string functionToString(Function const& input);
std::string protoToYul(uint8_t const* data, size_t size);
std::string createHex(std::string const& _hexBytes);
std::string createAlphaNum(std::string const& _strBytes);
std::ostream& operator<<(std::ostream& _os, BinaryOp const& _x);
std::ostream& operator<<(std::ostream& _os, Block const& _x);
std::ostream& operator<<(std::ostream& _os, Literal const& _x);

View File

@ -48,6 +48,8 @@ message VarRef {
message Literal {
oneof literal_oneof {
uint64 intval = 1;
string hexval = 2;
string strval = 3;
}
}