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
|
/*
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/>.
*/
/**
* Framework for testing features from the analysis phase of compiler.
*/
#include <test/libsolidity/AnalysisFramework.h>
#include <test/Options.h>
#include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/interface/SourceReferenceFormatter.h>
#include <libsolidity/ast/AST.h>
#include <libsolidity/parsing/Scanner.h>
#include <libdevcore/SHA3.h>
#include <boost/test/unit_test.hpp>
using namespace std;
using namespace dev;
using namespace dev::solidity;
using namespace dev::solidity::test;
pair<SourceUnit const*, ErrorList>
AnalysisFramework::parseAnalyseAndReturnError(
string const& _source,
bool _reportWarnings,
bool _insertVersionPragma,
bool _allowMultipleErrors
)
{
m_compiler.reset();
m_compiler.addSource("", _insertVersionPragma ? "pragma solidity >=0.0;\n" + _source : _source);
m_compiler.setEVMVersion(dev::test::Options::get().evmVersion());
if (!m_compiler.parse())
{
BOOST_ERROR("Parsing contract failed in analysis test suite:" + formatErrors());
}
m_compiler.analyze();
ErrorList errors = filterErrors(m_compiler.errors(), _reportWarnings);
if (errors.size() > 1 && !_allowMultipleErrors)
BOOST_FAIL("Multiple errors found: " + formatErrors());
return make_pair(&m_compiler.ast(""), std::move(errors));
}
ErrorList AnalysisFramework::filterErrors(ErrorList const& _errorList, bool _includeWarnings) const
{
ErrorList errors;
for (auto const& currentError: _errorList)
{
solAssert(currentError->comment(), "");
if (currentError->type() == Error::Type::Warning)
{
if (!_includeWarnings)
continue;
bool ignoreWarning = false;
for (auto const& filter: m_warningsToFilter)
if (currentError->comment()->find(filter) == 0)
{
ignoreWarning = true;
break;
}
if (ignoreWarning)
continue;
}
errors.emplace_back(currentError);
}
return errors;
}
SourceUnit const* AnalysisFramework::parseAndAnalyse(string const& _source)
{
auto sourceAndError = parseAnalyseAndReturnError(_source);
BOOST_REQUIRE(!!sourceAndError.first);
string message;
if (!sourceAndError.second.empty())
message = "Unexpected error: " + formatErrors();
BOOST_REQUIRE_MESSAGE(sourceAndError.second.empty(), message);
return sourceAndError.first;
}
bool AnalysisFramework::success(string const& _source)
{
return parseAnalyseAndReturnError(_source).second.empty();
}
ErrorList AnalysisFramework::expectError(std::string const& _source, bool _warning, bool _allowMultiple)
{
auto sourceAndErrors = parseAnalyseAndReturnError(_source, _warning, true, _allowMultiple);
BOOST_REQUIRE(!sourceAndErrors.second.empty());
BOOST_REQUIRE_MESSAGE(!!sourceAndErrors.first, "Expected error, but no error happened.");
return sourceAndErrors.second;
}
string AnalysisFramework::formatErrors() const
{
string message;
for (auto const& error: m_compiler.errors())
message += formatError(*error);
return message;
}
string AnalysisFramework::formatError(Error const& _error) const
{
return SourceReferenceFormatter::formatExceptionInformation(
_error,
(_error.type() == Error::Type::Warning) ? "Warning" : "Error",
[&](std::string const& _sourceName) -> solidity::Scanner const& { return m_compiler.scanner(_sourceName); }
);
}
ContractDefinition const* AnalysisFramework::retrieveContractByName(SourceUnit const& _source, string const& _name)
{
ContractDefinition* contract = nullptr;
for (shared_ptr<ASTNode> const& node: _source.nodes())
if ((contract = dynamic_cast<ContractDefinition*>(node.get())) && contract->name() == _name)
return contract;
return nullptr;
}
FunctionTypePointer AnalysisFramework::retrieveFunctionBySignature(
ContractDefinition const& _contract,
std::string const& _signature
)
{
FixedHash<4> hash(dev::keccak256(_signature));
return _contract.interfaceFunctions()[hash];
}
|