[libyul] ObjectParser: Enables the use of custom source mapping via @use-src.

This commit is contained in:
Christian Parpart
2021-07-27 16:46:47 +02:00
parent e3184c737a
commit 3755210b7b
7 changed files with 263 additions and 9 deletions
+117 -4
View File
@@ -24,16 +24,23 @@
#include <test/libsolidity/ErrorCheck.h>
#include <libyul/AssemblyStack.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libsolidity/interface/OptimiserSettings.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/test/unit_test.hpp>
#include <range/v3/view/iota.hpp>
#include <memory>
#include <optional>
#include <string>
#include <sstream>
using namespace ranges;
using namespace std;
using namespace solidity::frontend;
using namespace solidity::langutil;
@@ -44,7 +51,7 @@ namespace solidity::yul::test
namespace
{
std::pair<bool, ErrorList> parse(string const& _source)
pair<bool, ErrorList> parse(string const& _source)
{
try
{
@@ -63,7 +70,7 @@ std::pair<bool, ErrorList> parse(string const& _source)
return {false, {}};
}
std::optional<Error> parseAndReturnFirstError(string const& _source, bool _allowWarnings = true)
optional<Error> parseAndReturnFirstError(string const& _source, bool _allowWarnings = true)
{
bool success;
ErrorList errors;
@@ -88,12 +95,12 @@ std::optional<Error> parseAndReturnFirstError(string const& _source, bool _allow
return {};
}
bool successParse(std::string const& _source, bool _allowWarnings = true)
bool successParse(string const& _source, bool _allowWarnings = true)
{
return !parseAndReturnFirstError(_source, _allowWarnings);
}
Error expectError(std::string const& _source, bool _allowWarnings = false)
Error expectError(string const& _source, bool _allowWarnings = false)
{
auto error = parseAndReturnFirstError(_source, _allowWarnings);
@@ -101,6 +108,20 @@ Error expectError(std::string const& _source, bool _allowWarnings = false)
return *error;
}
tuple<optional<ObjectParser::SourceNameMap>, ErrorList> tryGetSourceLocationMapping(string _source)
{
vector<string> lines;
boost::split(lines, _source, boost::is_any_of("\n"));
string source = util::joinHumanReadablePrefixed(lines, "\n///") + "\n{}\n";
ErrorList errors;
ErrorReporter reporter(errors);
Dialect const& dialect = yul::EVMDialect::strictAssemblyForEVM(EVMVersion::berlin());
ObjectParser objectParser{reporter, dialect};
objectParser.parse(make_shared<Scanner>(CharStream(move(source), "")), false);
return {objectParser.sourceNameMapping(), std::move(errors)};
}
}
#define CHECK_ERROR(text, typ, substring) \
@@ -162,6 +183,98 @@ BOOST_AUTO_TEST_CASE(to_string)
BOOST_CHECK_EQUAL(asmStack.print(), expectation);
}
BOOST_AUTO_TEST_CASE(use_src_empty)
{
auto const [mapping, _] = tryGetSourceLocationMapping("");
BOOST_REQUIRE(!mapping);
}
BOOST_AUTO_TEST_CASE(use_src_simple)
{
auto const [mapping, _] = tryGetSourceLocationMapping(R"(@use-src 0:"contract.sol")");
BOOST_REQUIRE(mapping.has_value());
BOOST_REQUIRE_EQUAL(mapping->size(), 1);
BOOST_REQUIRE_EQUAL(*mapping->at(0), "contract.sol");
}
BOOST_AUTO_TEST_CASE(use_src_multiple)
{
auto const [mapping, _] = tryGetSourceLocationMapping(R"(@use-src 0:"contract.sol", 1:"misc.yul")");
BOOST_REQUIRE(mapping);
BOOST_REQUIRE_EQUAL(mapping->size(), 2);
BOOST_REQUIRE_EQUAL(*mapping->at(0), "contract.sol");
BOOST_REQUIRE_EQUAL(*mapping->at(1), "misc.yul");
}
BOOST_AUTO_TEST_CASE(use_src_escaped_filenames)
{
auto const [mapping, _] = tryGetSourceLocationMapping(
R"(@use-src 42:"con\"tract@\".sol")"
);
BOOST_REQUIRE(mapping);
BOOST_REQUIRE_EQUAL(mapping->size(), 1);
BOOST_REQUIRE(mapping->count(42));
BOOST_REQUIRE_EQUAL(*mapping->at(42), "con\"tract@\".sol");
}
BOOST_AUTO_TEST_CASE(use_src_invalid_syntax_malformed_param_1)
{
// open quote arg, missing closing quote
auto const [mapping, errors] = tryGetSourceLocationMapping(R"(@use-src 42_"con")");
BOOST_REQUIRE_EQUAL(errors.size(), 1);
BOOST_CHECK_EQUAL(errors.front()->errorId().error, 9804);
}
BOOST_AUTO_TEST_CASE(use_src_invalid_syntax_malformed_param_2)
{
// open quote arg, missing closing quote
auto const [mapping, errors] = tryGetSourceLocationMapping(R"(@use-src 42:"con)");
BOOST_REQUIRE_EQUAL(errors.size(), 1);
BOOST_CHECK_EQUAL(errors.front()->errorId().error, 9804);
}
BOOST_AUTO_TEST_CASE(use_src_error_unexpected_trailing_tokens)
{
auto const [mapping, errors] = tryGetSourceLocationMapping(
R"(@use-src 1:"file.sol" @use-src 2:"foo.sol")"
);
BOOST_REQUIRE_EQUAL(errors.size(), 1);
BOOST_CHECK_EQUAL(errors.front()->errorId().error, 9804);
}
BOOST_AUTO_TEST_CASE(use_src_multiline)
{
auto const [mapping, _] = tryGetSourceLocationMapping(
" @use-src \n 0:\"contract.sol\" \n , \n 1:\"misc.yul\""
);
BOOST_REQUIRE(mapping);
BOOST_REQUIRE_EQUAL(mapping->size(), 2);
BOOST_REQUIRE_EQUAL(*mapping->at(0), "contract.sol");
BOOST_REQUIRE_EQUAL(*mapping->at(1), "misc.yul");
}
BOOST_AUTO_TEST_CASE(use_src_empty_body)
{
auto const [mapping, _] = tryGetSourceLocationMapping("@use-src");
BOOST_REQUIRE(mapping);
BOOST_REQUIRE_EQUAL(mapping->size(), 0);
}
BOOST_AUTO_TEST_CASE(use_src_leading_text)
{
auto const [mapping, _] = tryGetSourceLocationMapping(
"@something else @use-src 0:\"contract.sol\", 1:\"misc.sol\""s
);
BOOST_REQUIRE(mapping);
BOOST_REQUIRE_EQUAL(mapping->size(), 2);
BOOST_REQUIRE(mapping->find(0) != mapping->end());
BOOST_REQUIRE_EQUAL(*mapping->at(0), "contract.sol");
BOOST_REQUIRE_EQUAL(*mapping->at(1), "misc.sol");
}
BOOST_AUTO_TEST_SUITE_END()
}