mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[libyul] ObjectParser: Enables the use of custom source mapping via @use-src.
This commit is contained in:
@@ -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()
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
// something else
|
||||
/*-- another unrelated comment --*/
|
||||
/// @use-src 3: "abc.sol" , 2: "def.sol"
|
||||
object "a" {
|
||||
code {
|
||||
/// @src 3:0:2
|
||||
datacopy(0, dataoffset("sub"), datasize("sub"))
|
||||
return(0,
|
||||
/** @src 2:5:6 */
|
||||
datasize("sub")
|
||||
)
|
||||
}
|
||||
object "sub" {
|
||||
code {
|
||||
/// @src 2:70:72
|
||||
sstore(0, dataoffset("sub"))
|
||||
/**
|
||||
* @something else
|
||||
* @src 3:2:5
|
||||
*/
|
||||
mstore(
|
||||
0,
|
||||
datasize("data1")
|
||||
/// @src 3:90:2
|
||||
)
|
||||
}
|
||||
data "data1" "Hello, World!"
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// Assembly:
|
||||
// /* "abc.sol":0:2 */
|
||||
// codecopy(0x00, dataOffset(sub_0), dataSize(sub_0))
|
||||
// /* "def.sol":5:6 */
|
||||
// dataSize(sub_0)
|
||||
// /* "abc.sol":0:2 */
|
||||
// 0x00
|
||||
// return
|
||||
// stop
|
||||
//
|
||||
// sub_0: assembly {
|
||||
// /* "def.sol":70:72 */
|
||||
// 0x00
|
||||
// dup1
|
||||
// sstore
|
||||
// /* "abc.sol":2:5 */
|
||||
// mstore(0x00, 0x0d)
|
||||
// stop
|
||||
// data_acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f 48656c6c6f2c20576f726c6421
|
||||
// }
|
||||
// Bytecode: 600a600d600039600a6000f3fe60008055600d600052fe
|
||||
// Opcodes: PUSH1 0xA PUSH1 0xD PUSH1 0x0 CODECOPY PUSH1 0xA PUSH1 0x0 RETURN INVALID PUSH1 0x0 DUP1 SSTORE PUSH1 0xD PUSH1 0x0 MSTORE INVALID
|
||||
// SourceMappings: 0:2::-:0;;;;5:1;0:2;
|
||||
Reference in New Issue
Block a user