Merge pull request #9502 from ethereum/yul-object-syntax-tests

Add syntax tests for Yul objects
This commit is contained in:
Alex Beregszaszi 2020-08-05 22:19:36 +01:00 committed by GitHub
commit d98eab24c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 316 additions and 179 deletions

View File

@ -217,15 +217,15 @@ def examine_id_coverage(top_dir, source_id_to_file_names, new_ids_only=False):
return False
old_source_only_ids = {
"1054", "1123", "1133", "1220", "1395", "1584", "1733", "1823", "1950", "1957",
"1988", "2418", "2461", "2512", "2592", "2657", "2800", "2842", "2856", "3069",
"3263", "3287", "3299", "3356", "3384", "3441", "3517", "3682", "3781", "3876",
"3893", "3947", "3997", "4010", "4110", "4294", "4802", "4805", "4828", "4846",
"1054", "1123", "1133", "1220", "1584", "1823", "1950", "1957",
"1988", "2418", "2461", "2512", "2592", "2657", "2800", "2842", "2856",
"3263", "3299", "3356", "3441", "3682", "3876",
"3893", "3997", "4010", "4110", "4802", "4805", "4828",
"4904", "4990", "5052", "5073", "5170", "5188", "5272", "5333", "5347", "5473",
"5622", "6041", "6052", "6272", "6708", "6792", "6931", "7110", "7128", "7186",
"7319", "7589", "7593", "7653", "7771", "7812", "7885", "8065", "8084", "8140",
"8143", "8261", "8311", "8312", "8452", "8592", "8758", "8794", "9005", "9011",
"9085", "9102", "9114", "9390", "9439", "9440", "9547", "9551", "9615", "9980"
"7319", "7589", "7593", "7653", "7812", "7885", "8065", "8084", "8140",
"8261", "8312", "8452", "8592", "8758", "9011",
"9085", "9102", "9390", "9439", "9440", "9547", "9551", "9615", "9980"
}
new_source_only_ids = source_only_ids - old_source_only_ids
if len(new_source_only_ids) != 0:

View File

@ -118,21 +118,6 @@ BOOST_AUTO_TEST_CASE(empty_code)
BOOST_CHECK(successParse("{ }"));
}
BOOST_AUTO_TEST_CASE(object_with_empty_code)
{
BOOST_CHECK(successParse("object \"a\" { code { } }"));
}
BOOST_AUTO_TEST_CASE(non_object)
{
CHECK_ERROR("code {}", ParserError, "Expected keyword \"object\"");
}
BOOST_AUTO_TEST_CASE(empty_name)
{
CHECK_ERROR("object \"\" { code {} }", ParserError, "Object name cannot be empty");
}
BOOST_AUTO_TEST_CASE(recursion_depth)
{
string input;
@ -144,77 +129,6 @@ BOOST_AUTO_TEST_CASE(recursion_depth)
CHECK_ERROR(input, ParserError, "recursion");
}
BOOST_AUTO_TEST_CASE(object_with_code)
{
BOOST_CHECK(successParse("object \"a\" { code { let x := mload(0) sstore(0, x) } }"));
}
BOOST_AUTO_TEST_CASE(object_with_code_and_data)
{
BOOST_CHECK(successParse("object \"a\" { code { let x := mload(0) sstore(0, x) } data \"b\" hex\"01010202\" }"));
}
BOOST_AUTO_TEST_CASE(object_with_non_code_at_start)
{
CHECK_ERROR("object \"a\" { data \"d\" hex\"0102\" code { } }", ParserError, "Expected keyword \"code\"");
}
BOOST_AUTO_TEST_CASE(nested_object)
{
string code = R"(
object "outer" {
code { let x := mload(0) }
data "x" "stringdata"
object "inner" {
code { mstore(0, 1) }
object "inner inner" { code {} }
data "innerx" "abc"
data "innery" "def"
}
}
)";
BOOST_CHECK(successParse(code));
}
BOOST_AUTO_TEST_CASE(incomplete)
{
CHECK_ERROR("object \"abc\" { code {} } object", ParserError, "Expected end of source");
}
BOOST_AUTO_TEST_CASE(reuse_object_name)
{
string code = R"(
object "outer" {
code { }
data "outer" "stringdata"
}
)";
CHECK_ERROR(code, ParserError, "Object name cannot be the same as the name of the containing object");
}
BOOST_AUTO_TEST_CASE(reuse_object_in_subobject)
{
string code = R"(
object "outer" {
code { }
object "outer" { code {} }
}
)";
CHECK_ERROR(code, ParserError, "Object name cannot be the same as the name of the containing object");
}
BOOST_AUTO_TEST_CASE(reuse_object_of_sibling)
{
string code = R"(
object "O" {
code { }
object "i" { code {} }
data "i" "abc"
}
)";
CHECK_ERROR(code, ParserError, "already exists inside the");
}
BOOST_AUTO_TEST_CASE(to_string)
{
string code = R"(
@ -248,69 +162,6 @@ BOOST_AUTO_TEST_CASE(to_string)
BOOST_CHECK_EQUAL(asmStack.print(), expectation);
}
BOOST_AUTO_TEST_CASE(arg_to_dataoffset_must_be_literal)
{
string code = R"(
object "outer" {
code { let x := "outer" let y := dataoffset(x) }
}
)";
CHECK_ERROR(code, TypeError, "Function expects direct literals as arguments.");
}
BOOST_AUTO_TEST_CASE(arg_to_dataoffset_must_be_string_literal)
{
string code = R"(
object "outer" {
code { let y := dataoffset(0) }
}
)";
CHECK_ERROR(code, TypeError, "Function expects string literal.");
}
BOOST_AUTO_TEST_CASE(arg_to_datasize_must_be_literal)
{
string code = R"(
object "outer" {
code { let x := "outer" let y := datasize(x) }
}
)";
CHECK_ERROR(code, TypeError, "Function expects direct literals as arguments.");
}
BOOST_AUTO_TEST_CASE(arg_to_datasize_must_be_string_literal)
{
string code = R"(
object "outer" {
code { let y := datasize(0) }
}
)";
CHECK_ERROR(code, TypeError, "Function expects string literal.");
}
BOOST_AUTO_TEST_CASE(args_to_datacopy_are_arbitrary)
{
string code = R"(
object "outer" {
code { let x := 0 let y := 2 let s := 3 datacopy(x, y, s) }
}
)";
BOOST_CHECK(successParse(code));
}
BOOST_AUTO_TEST_CASE(non_existing_objects)
{
BOOST_CHECK(successParse(
"object \"main\" { code { pop(datasize(\"main\")) } }"
));
CHECK_ERROR(
"object \"main\" { code { pop(datasize(\"abc\")) } }",
TypeError,
"Unknown data object"
);
}
BOOST_AUTO_TEST_SUITE_END()
}

View File

@ -26,6 +26,8 @@
#include <test/libyul/Common.h>
#include <test/libyul/SyntaxTest.h>
#include <test/libsolidity/util/SoltestErrors.h>
#include <test/Common.h>
using namespace std;
@ -43,17 +45,9 @@ void SyntaxTest::parseAndAnalyze()
string const& source = m_sources.begin()->second;
ErrorList errorList{};
ErrorReporter errorReporter{errorList};
auto scanner = make_shared<Scanner>(CharStream(source, name));
auto parserResult = yul::Parser(errorReporter, *m_dialect).parse(scanner, false);
if (parserResult)
{
yul::AsmAnalysisInfo analysisInfo;
yul::AsmAnalyzer(analysisInfo, errorReporter, *m_dialect).analyze(*parserResult);
}
soltestAssert(m_dialect, "");
// Silently ignoring the results.
yul::test::parse(source, *m_dialect, errorList);
for (auto const& error: errorList)
{
int locationStart = -1;

View File

@ -1,12 +0,0 @@
{
datacopy(0, 1, 2)
datasize("")
datasize(0) // This should not be valid.
dataoffset("")
dataoffset(0) // This should not be valid.
}
// ----
// TypeError 3517: (37-39): Unknown data object "".
// TypeError 5859: (54-55): Function expects string literal.
// TypeError 3517: (101-103): Unknown data object "".
// TypeError 5859: (120-121): Function expects string literal.

View File

@ -0,0 +1,7 @@
object "A" {
code {}
object "B" {
code {}
}
}

View File

@ -0,0 +1,18 @@
object "A" {
code {
function f() -> offset, len {
offset := dataoffset("A")
len := datasize("A")
}
let offset, len := f()
codecopy(0, offset, len)
}
data "hello" "hello world text"
object "hello" {
code {}
}
}
// ----
// ParserError 8794: (226-233): Object name "hello" already exists inside the containing object.

View File

@ -0,0 +1,4 @@
code {
}
// ----
// ParserError 4294: (0-4): Expected keyword "object".

View File

@ -0,0 +1,35 @@
object "A" {
code {}
data "1" hex"001122"
object "B" {
code {}
data "2" ""
object "B_C" {
code {}
object "B_C_1" {
code {}
}
object "B_C_2" {
code {}
}
}
}
object "C" {
code {}
}
object "D" {
code {}
object "D_1" {
code {}
}
object "D_2" {
code {}
}
}
}

View File

@ -0,0 +1,8 @@
object "A" {
code {}
data "B" ""
data "B" hex"00"
}
// ----
// ParserError 8794: (45-48): Object name "B" already exists inside the containing object.

View File

@ -0,0 +1,9 @@
object "A" {
code {}
object "A" {
code {}
}
}
// ----
// ParserError 8311: (33-36): Object name cannot be the same as the name of the containing object.

View File

@ -0,0 +1,10 @@
object "A" {
code {}
data "B" ""
object "B" {
code {}
}
}
// ----
// ParserError 8794: (47-50): Object name "B" already exists inside the containing object.

View File

@ -0,0 +1,12 @@
object "A" {
code {}
object "B" {
code {}
}
object "B" {
code {}
}
}
// ----
// ParserError 8794: (64-67): Object name "B" already exists inside the containing object.

View File

@ -0,0 +1,7 @@
object "A" {
code {}
data "A" ""
}
// ----
// ParserError 8311: (31-34): Object name cannot be the same as the name of the containing object.

View File

@ -0,0 +1,9 @@
object "A" {
code {
datacopy(0, dataoffset("3"), datasize("3"))
}
data "1" ""
data "2" hex"0011"
data "3" "hello world this is longer than 32 bytes and should still work"
}
// ----

View File

@ -0,0 +1,9 @@
object "A" {
code {
pop(dataoffset("B"))
pop(datasize("B"))
}
data "B" hex"00"
}
// ----

View File

@ -0,0 +1,6 @@
object "A" {
data "B" ""
code {}
}
// ----
// ParserError 4846: (15-19): Expected keyword "code".

View File

@ -0,0 +1,6 @@
object "A" {
code {}
data hex"11" ""
}
// ----
// ParserError 2314: (30-37): Expected 'StringLiteral' but got 'HexStringLiteral'

View File

@ -0,0 +1,8 @@
object "A" {
code {
}
data "1" hex"0"
data "1" hex"wronghexencoding"
}
// ----
// ParserError 2314: (37-41): Expected 'StringLiteral' but got 'ILLEGAL'

View File

@ -0,0 +1,8 @@
object "A" {
code {
}
data "1" hex"wronghexencoding"
data "2" hex"0"
}
// ----
// ParserError 2314: (37-41): Expected 'StringLiteral' but got 'ILLEGAL'

View File

@ -0,0 +1,8 @@
{
datacopy(0, 1, 2)
// datacopy also accepts pretty much anything which can be turned into a number
let x := 0
let s := ""
datacopy(x, "11", s)
}

View File

@ -0,0 +1,10 @@
object "A" {
code {
let x := "B"
pop(dataoffset(x))
}
data "B" hex"00"
}
// ----
// TypeError 9114: (47-57): Function expects direct literals as arguments.

View File

@ -0,0 +1,7 @@
object "A" {
code {
pop(dataoffset(0))
}
}
// ----
// TypeError 5859: (41-42): Function expects string literal.

View File

@ -0,0 +1,8 @@
object "A" {
code {
pop(dataoffset("C"))
}
data "B" ""
}
// ----
// TypeError 3517: (41-44): Unknown data object "C".

View File

@ -0,0 +1,10 @@
object "A" {
code {
let x := "B"
pop(datasize(x))
}
data "B" hex"00"
}
// ----
// TypeError 9114: (47-55): Function expects direct literals as arguments.

View File

@ -0,0 +1,7 @@
object "A" {
code {
pop(datasize(0))
}
}
// ----
// TypeError 5859: (39-40): Function expects string literal.

View File

@ -0,0 +1,8 @@
object "A" {
code {
pop(datasize("C"))
}
data "B" ""
}
// ----
// TypeError 3517: (39-42): Unknown data object "C".

View File

@ -0,0 +1,3 @@
object "A" {
code { }
}

View File

@ -0,0 +1,5 @@
object "A" {
data "tmp" ""
}
// ----
// ParserError 4846: (15-19): Expected keyword "code".

View File

@ -0,0 +1,4 @@
object "A" {
}
// ----
// ParserError 4846: (13-14): Expected keyword "code".

View File

@ -0,0 +1,5 @@
object "" {
}
// ----
// ParserError 3287: (7-9): Object name cannot be empty.
// ParserError 4846: (12-13): Expected keyword "code".

View File

@ -0,0 +1,3 @@
object {
// ----
// ParserError 2314: (7-8): Expected 'StringLiteral' but got '{'

View File

@ -0,0 +1,7 @@
object "A" {
code {}
}
object
// ----
// ParserError 2314: (26-32): Expected end of source but got identifier

View File

@ -0,0 +1,6 @@
object "A" {
code { }
code { }
}
// ----
// ParserError 8143: (26-30): Expected keyword "data" or "object" or "}".

View File

@ -0,0 +1,6 @@
object "A" {
code { }
data "one" ""
data "two" ""
}
// ----

View File

@ -0,0 +1,8 @@
object "A" {
code { }
}
object "B" {
code { }
}
// ----
// ParserError 2314: (26-32): Expected end of source but got identifier

View File

@ -0,0 +1,10 @@
object "outer" {
code { let x := mload(0) }
data "x" "stringdata"
object "inner" {
code { mstore(0, 1) }
object "inner inner" { code {} }
data "innerx" "abc"
data "innery" "def"
}
}

View File

@ -0,0 +1,5 @@
object hex"11" {
code {}
}
// ----
// ParserError 2314: (7-14): Expected 'StringLiteral' but got 'HexStringLiteral'

View File

@ -0,0 +1,11 @@
object "A" {
code {
pop(dataoffset("B"))
pop(datasize("B"))
}
object "B" {
code {}
}
}
// ----

View File

@ -0,0 +1,9 @@
object "A" {
object "B" {
code {}
}
code {}
}
// ----
// ParserError 4846: (15-21): Expected keyword "code".

View File

@ -0,0 +1,8 @@
object "A" {
code {}
object hex"11" {
code {}
}
}
// ----
// ParserError 2314: (32-39): Expected 'StringLiteral' but got 'HexStringLiteral'