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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2017-08-28 18:40:38 +00:00
|
|
|
/**
|
|
|
|
* Framework for testing features from the analysis phase of compiler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/libsolidity/AnalysisFramework.h>
|
|
|
|
|
2023-08-04 16:54:06 +00:00
|
|
|
#include <test/libsolidity/util/Common.h>
|
2023-08-07 11:00:08 +00:00
|
|
|
#include <test/libsolidity/util/SoltestErrors.h>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2018-02-23 18:29:20 +00:00
|
|
|
|
2017-08-28 18:40:38 +00:00
|
|
|
#include <libsolidity/interface/CompilerStack.h>
|
2018-11-24 11:33:36 +00:00
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
2017-08-28 18:40:38 +00:00
|
|
|
|
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/Scanner.h>
|
2017-09-28 08:59:15 +00:00
|
|
|
|
2022-09-27 12:13:44 +00:00
|
|
|
#include <libsolutil/FunctionSelector.h>
|
2017-08-28 18:40:38 +00:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::frontend::test;
|
2017-08-28 18:40:38 +00:00
|
|
|
|
2023-08-04 17:44:59 +00:00
|
|
|
std::pair<SourceUnit const*, ErrorList> AnalysisFramework::runAnalysisAndExpectNoParsingErrors(
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string const& _source,
|
2023-08-04 17:44:59 +00:00
|
|
|
bool _includeWarningsAndInfos,
|
|
|
|
bool _addPreamble,
|
|
|
|
bool _allowMultiple
|
2017-08-28 18:40:38 +00:00
|
|
|
)
|
|
|
|
{
|
2023-08-04 17:44:59 +00:00
|
|
|
runFramework(_addPreamble ? withPreamble(_source) : _source, PipelineStage::Analysis);
|
2017-08-28 18:40:38 +00:00
|
|
|
|
2023-08-04 17:44:59 +00:00
|
|
|
if (!stageSuccessful(PipelineStage::Parsing))
|
|
|
|
BOOST_FAIL("Parsing contract failed in analysis test suite:" + formatErrors(m_compiler->errors()));
|
2017-08-28 18:40:38 +00:00
|
|
|
|
2023-08-04 17:44:59 +00:00
|
|
|
ErrorList errors = filteredErrors(_includeWarningsAndInfos);
|
|
|
|
if (errors.size() > 1 && !_allowMultiple)
|
2023-08-04 13:27:27 +00:00
|
|
|
BOOST_FAIL("Multiple errors found: " + formatErrors(errors));
|
2018-04-03 10:05:26 +00:00
|
|
|
|
2019-04-15 13:27:51 +00:00
|
|
|
return make_pair(&compiler().ast(""), std::move(errors));
|
2018-04-03 10:05:26 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 11:00:08 +00:00
|
|
|
bool AnalysisFramework::runFramework(StringMap _sources, PipelineStage _targetStage)
|
|
|
|
{
|
|
|
|
resetFramework();
|
|
|
|
m_targetStage = _targetStage;
|
|
|
|
soltestAssert(m_compiler);
|
|
|
|
|
|
|
|
m_compiler->setSources(std::move(_sources));
|
|
|
|
setupCompiler(*m_compiler);
|
|
|
|
executeCompilationPipeline();
|
|
|
|
return pipelineSuccessful();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalysisFramework::resetFramework()
|
|
|
|
{
|
|
|
|
compiler().reset();
|
|
|
|
m_targetStage = PipelineStage::Compilation;
|
|
|
|
}
|
|
|
|
|
2023-08-04 14:25:33 +00:00
|
|
|
std::unique_ptr<CompilerStack> AnalysisFramework::createStack() const
|
|
|
|
{
|
|
|
|
return std::make_unique<CompilerStack>();
|
|
|
|
}
|
|
|
|
|
2023-08-07 11:00:08 +00:00
|
|
|
void AnalysisFramework::setupCompiler(CompilerStack& _compiler)
|
|
|
|
{
|
2023-08-07 10:26:44 +00:00
|
|
|
// These are just defaults based on the (global) CLI options.
|
|
|
|
// Technically, every TestCase should override these with values passed to it in TestCase::Config.
|
|
|
|
// In practice TestCase::Config always matches global config so most test cases don't care.
|
2023-08-07 11:00:08 +00:00
|
|
|
_compiler.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
|
2023-08-07 10:26:44 +00:00
|
|
|
_compiler.setEOFVersion(solidity::test::CommonOptions::get().eofVersion());
|
|
|
|
_compiler.setOptimiserSettings(solidity::test::CommonOptions::get().optimize);
|
2023-08-07 11:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnalysisFramework::executeCompilationPipeline()
|
|
|
|
{
|
|
|
|
soltestAssert(m_compiler);
|
|
|
|
|
|
|
|
// If you add a new stage, remember to handle it below.
|
|
|
|
soltestAssert(
|
|
|
|
m_targetStage == PipelineStage::Parsing ||
|
|
|
|
m_targetStage == PipelineStage::Analysis ||
|
|
|
|
m_targetStage == PipelineStage::Compilation
|
|
|
|
);
|
|
|
|
|
|
|
|
bool parsingSuccessful = m_compiler->parse();
|
|
|
|
soltestAssert(parsingSuccessful || !filteredErrors(false /* _includeWarningsAndInfos */).empty());
|
|
|
|
if (!parsingSuccessful || stageSuccessful(m_targetStage))
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool analysisSuccessful = m_compiler->analyze();
|
|
|
|
soltestAssert(analysisSuccessful || !filteredErrors(false /* _includeWarningsAndInfos */).empty());
|
|
|
|
if (!analysisSuccessful || stageSuccessful(m_targetStage))
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool compilationSuccessful = m_compiler->compile();
|
|
|
|
soltestAssert(compilationSuccessful || !filteredErrors(false /* _includeWarningsAndInfos */).empty());
|
|
|
|
soltestAssert(stageSuccessful(m_targetStage) == compilationSuccessful);
|
|
|
|
}
|
|
|
|
|
2021-06-30 12:48:45 +00:00
|
|
|
ErrorList AnalysisFramework::filterErrors(ErrorList const& _errorList, bool _includeWarningsAndInfos) const
|
2018-04-03 10:05:26 +00:00
|
|
|
{
|
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(), "");
|
2021-06-30 12:48:45 +00:00
|
|
|
if (!Error::isError(currentError->type()))
|
2017-09-28 08:59:02 +00:00
|
|
|
{
|
2021-06-30 12:48:45 +00:00
|
|
|
if (!_includeWarningsAndInfos)
|
2018-04-03 10:05:26 +00:00
|
|
|
continue;
|
2021-06-30 12:48:45 +00:00
|
|
|
bool ignoreWarningsAndInfos = false;
|
2017-09-28 08:59:02 +00:00
|
|
|
for (auto const& filter: m_warningsToFilter)
|
|
|
|
if (currentError->comment()->find(filter) == 0)
|
|
|
|
{
|
2021-06-30 12:48:45 +00:00
|
|
|
ignoreWarningsAndInfos = true;
|
2017-09-28 08:59:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-06-30 12:48:45 +00:00
|
|
|
if (ignoreWarningsAndInfos)
|
2017-09-28 08:59:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-08-28 18:40:38 +00:00
|
|
|
|
2020-09-28 15:39:40 +00:00
|
|
|
std::shared_ptr<Error const> newError = currentError;
|
|
|
|
for (auto const& messagePrefix: m_messagesToCut)
|
|
|
|
if (currentError->comment()->find(messagePrefix) == 0)
|
|
|
|
{
|
2021-12-01 11:30:36 +00:00
|
|
|
SourceLocation const* location = currentError->sourceLocation();
|
2020-09-28 15:39:40 +00:00
|
|
|
// sufficient for now, but in future we might clone the error completely, including the secondary location
|
2023-08-30 00:08:06 +00:00
|
|
|
newError = std::make_shared<Error>(
|
2020-09-28 15:39:40 +00:00
|
|
|
currentError->errorId(),
|
|
|
|
currentError->type(),
|
2021-04-23 22:55:22 +00:00
|
|
|
messagePrefix + " ....",
|
|
|
|
location ? *location : SourceLocation()
|
2020-09-28 15:39:40 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
errors.emplace_back(newError);
|
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
|
|
|
}
|
|
|
|
|
2023-08-07 11:00:08 +00:00
|
|
|
bool AnalysisFramework::stageSuccessful(PipelineStage _stage) const
|
|
|
|
{
|
|
|
|
switch (_stage) {
|
|
|
|
case PipelineStage::Parsing: return compiler().state() >= CompilerStack::Parsed;
|
|
|
|
case PipelineStage::Analysis: return compiler().state() >= CompilerStack::AnalysisSuccessful;
|
|
|
|
case PipelineStage::Compilation: return compiler().state() >= CompilerStack::CompilationSuccessful;
|
|
|
|
}
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
2023-08-04 17:44:59 +00:00
|
|
|
ErrorList AnalysisFramework::runAnalysisAndExpectError(
|
|
|
|
std::string const& _source,
|
|
|
|
bool _includeWarningsAndInfos,
|
|
|
|
bool _allowMultiple
|
|
|
|
)
|
2017-08-28 18:40:38 +00:00
|
|
|
{
|
2023-08-04 17:44:59 +00:00
|
|
|
auto [ast, errors] = runAnalysisAndExpectNoParsingErrors(_source, _includeWarningsAndInfos, true, _allowMultiple);
|
|
|
|
BOOST_REQUIRE(!errors.empty());
|
|
|
|
BOOST_REQUIRE_MESSAGE(ast, "Expected error, but no error happened.");
|
|
|
|
return errors;
|
2017-08-28 18:40:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string AnalysisFramework::formatErrors(
|
2023-08-08 15:19:43 +00:00
|
|
|
langutil::ErrorList const& _errors,
|
2023-08-04 11:32:32 +00:00
|
|
|
bool _colored,
|
|
|
|
bool _withErrorIds
|
|
|
|
) const
|
2017-08-28 18:40:38 +00:00
|
|
|
{
|
2023-08-08 15:19:43 +00:00
|
|
|
return SourceReferenceFormatter::formatErrorInformation(
|
|
|
|
_errors,
|
|
|
|
*m_compiler,
|
|
|
|
_colored,
|
|
|
|
_withErrorIds
|
|
|
|
);
|
2017-09-28 08:59:15 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string AnalysisFramework::formatError(
|
2023-08-04 11:32:32 +00:00
|
|
|
Error const& _error,
|
|
|
|
bool _colored,
|
|
|
|
bool _withErrorIds
|
|
|
|
) const
|
2017-09-28 08:59:15 +00:00
|
|
|
{
|
2023-08-08 15:19:43 +00:00
|
|
|
return SourceReferenceFormatter::formatErrorInformation(
|
2023-08-04 11:32:32 +00:00
|
|
|
_error,
|
|
|
|
*m_compiler,
|
|
|
|
_colored,
|
|
|
|
_withErrorIds
|
|
|
|
);
|
2017-08-28 18:40:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
ContractDefinition const* AnalysisFramework::retrieveContractByName(SourceUnit const& _source, std::string const& _name)
|
2017-08-28 18:40:38 +00:00
|
|
|
{
|
|
|
|
ContractDefinition* contract = nullptr;
|
2017-09-06 14:05:35 +00:00
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
for (std::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
|
|
|
|
)
|
|
|
|
{
|
2022-09-27 12:13:44 +00:00
|
|
|
return _contract.interfaceFunctions()[util::selectorFromSignatureH32(_signature)];
|
2017-08-28 18:40:38 +00:00
|
|
|
}
|