forked from argotorg/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectParser.cpp
More file actions
167 lines (141 loc) · 3.82 KB
/
Copy pathObjectParser.cpp
File metadata and controls
167 lines (141 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @date 2018
* Unit tests for the Yul object parser.
*/
#include <test/Common.h>
#include <test/libsolidity/ErrorCheck.h>
#include <libyul/AssemblyStack.h>
#include <libsolidity/interface/OptimiserSettings.h>
#include <boost/algorithm/string/replace.hpp>
#include <boost/test/unit_test.hpp>
#include <memory>
#include <optional>
#include <string>
using namespace std;
using namespace solidity::frontend;
using namespace solidity::langutil;
namespace solidity::yul::test
{
namespace
{
std::pair<bool, ErrorList> parse(string const& _source)
{
try
{
AssemblyStack asmStack(
solidity::test::CommonOptions::get().evmVersion(),
AssemblyStack::Language::StrictAssembly,
solidity::frontend::OptimiserSettings::none()
);
bool success = asmStack.parseAndAnalyze("source", _source);
return {success, asmStack.errors()};
}
catch (FatalError const&)
{
BOOST_FAIL("Fatal error leaked.");
}
return {false, {}};
}
std::optional<Error> parseAndReturnFirstError(string const& _source, bool _allowWarnings = true)
{
bool success;
ErrorList errors;
tie(success, errors) = parse(_source);
if (!success)
{
BOOST_REQUIRE_EQUAL(errors.size(), 1);
return *errors.front();
}
else
{
// If success is true, there might still be an error in the assembly stage.
if (_allowWarnings && Error::containsOnlyWarnings(errors))
return {};
else if (!errors.empty())
{
if (!_allowWarnings)
BOOST_CHECK_EQUAL(errors.size(), 1);
return *errors.front();
}
}
return {};
}
bool successParse(std::string const& _source, bool _allowWarnings = true)
{
return !parseAndReturnFirstError(_source, _allowWarnings);
}
Error expectError(std::string const& _source, bool _allowWarnings = false)
{
auto error = parseAndReturnFirstError(_source, _allowWarnings);
BOOST_REQUIRE(error);
return *error;
}
}
#define CHECK_ERROR(text, typ, substring) \
do \
{ \
Error err = expectError((text), false); \
BOOST_CHECK(err.type() == (Error::Type::typ)); \
BOOST_CHECK(::solidity::frontend::test::searchErrorMessage(err, (substring))); \
} while(0)
BOOST_AUTO_TEST_SUITE(YulObjectParser)
BOOST_AUTO_TEST_CASE(empty_code)
{
BOOST_CHECK(successParse("{ }"));
}
BOOST_AUTO_TEST_CASE(recursion_depth)
{
string input;
for (size_t i = 0; i < 20000; i++)
input += "object \"a" + to_string(i) + "\" { code {} ";
for (size_t i = 0; i < 20000; i++)
input += "}";
CHECK_ERROR(input, ParserError, "recursion");
}
BOOST_AUTO_TEST_CASE(to_string)
{
string code = R"(
object "O" {
code { let x := mload(0) if x { sstore(0, 1) } }
object "i" { code {} data "j" "def" }
data "j" "abc"
data "k" hex"010203"
}
)";
string expectation = R"(object "O" {
code {
let x := mload(0)
if x { sstore(0, 1) }
}
object "i" {
code { }
data "j" hex"646566"
}
data "j" hex"616263"
data "k" hex"010203"
}
)";
expectation = boost::replace_all_copy(expectation, "\t", " ");
AssemblyStack asmStack(
solidity::test::CommonOptions::get().evmVersion(),
AssemblyStack::Language::StrictAssembly,
solidity::frontend::OptimiserSettings::none()
);
BOOST_REQUIRE(asmStack.parseAndAnalyze("source", code));
BOOST_CHECK_EQUAL(asmStack.print(), expectation);
}
BOOST_AUTO_TEST_SUITE_END()
}