2017-08-28 18:40:38 +00:00
|
|
|
/*
|
|
|
|
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>
|
|
|
|
|
2018-03-14 11:04:04 +00:00
|
|
|
#include <test/Options.h>
|
2018-02-23 18:29:20 +00:00
|
|
|
|
2017-08-28 18:40:38 +00:00
|
|
|
#include <libsolidity/interface/CompilerStack.h>
|
|
|
|
#include <libsolidity/interface/SourceReferenceFormatter.h>
|
|
|
|
|
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
|
2017-09-28 08:59:15 +00:00
|
|
|
#include <libsolidity/parsing/Scanner.h>
|
|
|
|
|
2018-10-18 11:35:20 +00:00
|
|
|
#include <libdevcore/Keccak256.h>
|
2017-08-28 18:40:38 +00:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity;
|
|
|
|
using namespace dev::solidity::test;
|
|
|
|
|
2018-02-09 22:54:05 +00:00
|
|
|
pair<SourceUnit const*, ErrorList>
|
2017-08-28 18:40:38 +00:00
|
|
|
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);
|
2018-02-23 18:29:20 +00:00
|
|
|
m_compiler.setEVMVersion(dev::test::Options::get().evmVersion());
|
2017-08-28 18:40:38 +00:00
|
|
|
if (!m_compiler.parse())
|
|
|
|
{
|
2017-09-28 08:59:15 +00:00
|
|
|
BOOST_ERROR("Parsing contract failed in analysis test suite:" + formatErrors());
|
2017-08-28 18:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_compiler.analyze();
|
|
|
|
|
2018-04-03 10:05:26 +00:00
|
|
|
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
|
|
|
|
{
|
2018-02-09 22:54:05 +00:00
|
|
|
ErrorList errors;
|
2018-04-03 10:05:26 +00:00
|
|
|
for (auto const& currentError: _errorList)
|
2017-08-28 18:40:38 +00:00
|
|
|
{
|
|
|
|
solAssert(currentError->comment(), "");
|
2017-09-28 08:59:02 +00:00
|
|
|
if (currentError->type() == Error::Type::Warning)
|
|
|
|
{
|
2018-04-03 10:05:26 +00:00
|
|
|
if (!_includeWarnings)
|
|
|
|
continue;
|
2017-09-28 08:59:02 +00:00
|
|
|
bool ignoreWarning = false;
|
|
|
|
for (auto const& filter: m_warningsToFilter)
|
|
|
|
if (currentError->comment()->find(filter) == 0)
|
|
|
|
{
|
|
|
|
ignoreWarning = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ignoreWarning)
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-28 18:40:38 +00:00
|
|
|
|
2018-04-03 10:05:26 +00:00
|
|
|
errors.emplace_back(currentError);
|
2017-08-28 18:40:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 10:05:26 +00:00
|
|
|
return errors;
|
2017-08-28 18:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SourceUnit const* AnalysisFramework::parseAndAnalyse(string const& _source)
|
|
|
|
{
|
|
|
|
auto sourceAndError = parseAnalyseAndReturnError(_source);
|
|
|
|
BOOST_REQUIRE(!!sourceAndError.first);
|
2017-09-28 08:59:15 +00:00
|
|
|
string message;
|
2018-02-09 22:54:05 +00:00
|
|
|
if (!sourceAndError.second.empty())
|
|
|
|
message = "Unexpected error: " + formatErrors();
|
|
|
|
BOOST_REQUIRE_MESSAGE(sourceAndError.second.empty(), message);
|
2017-08-28 18:40:38 +00:00
|
|
|
return sourceAndError.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AnalysisFramework::success(string const& _source)
|
|
|
|
{
|
2018-02-09 22:54:05 +00:00
|
|
|
return parseAnalyseAndReturnError(_source).second.empty();
|
2017-08-28 18:40:38 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 22:54:05 +00:00
|
|
|
ErrorList AnalysisFramework::expectError(std::string const& _source, bool _warning, bool _allowMultiple)
|
2017-08-28 18:40:38 +00:00
|
|
|
{
|
2018-02-09 22:54:05 +00:00
|
|
|
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;
|
2017-08-28 18:40:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 10:05:26 +00:00
|
|
|
string AnalysisFramework::formatErrors() const
|
2017-08-28 18:40:38 +00:00
|
|
|
{
|
2017-09-28 08:59:15 +00:00
|
|
|
string message;
|
2017-08-28 18:40:38 +00:00
|
|
|
for (auto const& error: m_compiler.errors())
|
2017-09-28 08:59:15 +00:00
|
|
|
message += formatError(*error);
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2018-04-03 10:05:26 +00:00
|
|
|
string AnalysisFramework::formatError(Error const& _error) const
|
2017-09-28 08:59:15 +00:00
|
|
|
{
|
|
|
|
return SourceReferenceFormatter::formatExceptionInformation(
|
|
|
|
_error,
|
|
|
|
(_error.type() == Error::Type::Warning) ? "Warning" : "Error",
|
2017-08-28 18:40:38 +00:00
|
|
|
[&](std::string const& _sourceName) -> solidity::Scanner const& { return m_compiler.scanner(_sourceName); }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-06 14:05:35 +00:00
|
|
|
ContractDefinition const* AnalysisFramework::retrieveContractByName(SourceUnit const& _source, string const& _name)
|
2017-08-28 18:40:38 +00:00
|
|
|
{
|
|
|
|
ContractDefinition* contract = nullptr;
|
2017-09-06 14:05:35 +00:00
|
|
|
|
2017-08-28 18:40:38 +00:00
|
|
|
for (shared_ptr<ASTNode> const& node: _source.nodes())
|
2017-09-06 14:05:35 +00:00
|
|
|
if ((contract = dynamic_cast<ContractDefinition*>(node.get())) && contract->name() == _name)
|
2017-08-28 18:40:38 +00:00
|
|
|
return contract;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionTypePointer AnalysisFramework::retrieveFunctionBySignature(
|
|
|
|
ContractDefinition const& _contract,
|
|
|
|
std::string const& _signature
|
|
|
|
)
|
|
|
|
{
|
|
|
|
FixedHash<4> hash(dev::keccak256(_signature));
|
|
|
|
return _contract.interfaceFunctions()[hash];
|
|
|
|
}
|