2018-03-12 12:33:37 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/libsolidity/SyntaxTest.h>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2018-03-12 12:33:37 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2020-01-14 14:55:31 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2018-03-12 12:33:37 +00:00
|
|
|
#include <boost/throw_exception.hpp>
|
|
|
|
#include <fstream>
|
2018-03-13 16:54:22 +00:00
|
|
|
#include <memory>
|
2018-03-12 12:33:37 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::util::formatting;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::frontend::test;
|
2018-03-12 12:33:37 +00:00
|
|
|
using namespace boost::unit_test;
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace fs = boost::filesystem;
|
2018-03-12 12:33:37 +00:00
|
|
|
|
2020-02-04 18:40:32 +00:00
|
|
|
SyntaxTest::SyntaxTest(string const& _filename, langutil::EVMVersion _evmVersion, bool _parserErrorRecovery): CommonSyntaxTest(_filename, _evmVersion)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
2019-05-27 12:01:53 +00:00
|
|
|
if (m_settings.count("optimize-yul"))
|
|
|
|
{
|
2019-11-26 15:41:58 +00:00
|
|
|
if (m_settings["optimize-yul"] == "true")
|
|
|
|
{
|
|
|
|
m_validatedSettings["optimize-yul"] = "true";
|
|
|
|
m_settings.erase("optimize-yul");
|
|
|
|
}
|
|
|
|
else if (m_settings["optimize-yul"] == "false")
|
|
|
|
{
|
|
|
|
m_validatedSettings["optimize-yul"] = "false";
|
|
|
|
m_settings.erase("optimize-yul");
|
|
|
|
m_optimiseYul = false;
|
|
|
|
}
|
2019-05-27 12:01:53 +00:00
|
|
|
}
|
2019-05-30 14:35:42 +00:00
|
|
|
m_parserErrorRecovery = _parserErrorRecovery;
|
2018-03-12 12:33:37 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 13:33:22 +00:00
|
|
|
TestCase::TestResult SyntaxTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
2019-12-03 15:50:04 +00:00
|
|
|
setupCompiler();
|
|
|
|
parseAndAnalyze();
|
|
|
|
filterObtainedErrors();
|
2018-04-03 10:05:26 +00:00
|
|
|
|
2019-05-07 13:33:22 +00:00
|
|
|
return printExpectationAndError(_stream, _linePrefix, _formatted) ? TestResult::Success : TestResult::Failure;
|
2018-11-15 15:47:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 15:50:04 +00:00
|
|
|
void SyntaxTest::setupCompiler()
|
|
|
|
{
|
|
|
|
string const versionPragma = "pragma solidity >=0.0;\n";
|
|
|
|
compiler().reset();
|
|
|
|
auto sourcesWithPragma = m_sources;
|
|
|
|
for (auto& source: sourcesWithPragma)
|
|
|
|
source.second = versionPragma + source.second;
|
|
|
|
compiler().setSources(sourcesWithPragma);
|
|
|
|
compiler().setEVMVersion(m_evmVersion);
|
|
|
|
compiler().setParserErrorRecovery(m_parserErrorRecovery);
|
|
|
|
compiler().setOptimiserSettings(
|
|
|
|
m_optimiseYul ?
|
|
|
|
OptimiserSettings::full() :
|
|
|
|
OptimiserSettings::minimal()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyntaxTest::parseAndAnalyze()
|
|
|
|
{
|
2019-12-03 15:50:28 +00:00
|
|
|
if (compiler().parse() && compiler().analyze())
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!compiler().compile())
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Compilation failed even though analysis was successful."));
|
|
|
|
}
|
|
|
|
catch (UnimplementedFeatureError const& _e)
|
|
|
|
{
|
|
|
|
m_errorList.emplace_back(SyntaxTestError{
|
|
|
|
"UnimplementedFeatureError",
|
|
|
|
errorMessage(_e),
|
|
|
|
"",
|
|
|
|
-1,
|
|
|
|
-1
|
|
|
|
});
|
|
|
|
}
|
2019-12-03 15:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SyntaxTest::filterObtainedErrors()
|
|
|
|
{
|
|
|
|
string const versionPragma = "pragma solidity >=0.0;\n";
|
|
|
|
for (auto const& currentError: filterErrors(compiler().errors(), true))
|
|
|
|
{
|
|
|
|
int locationStart = -1, locationEnd = -1;
|
|
|
|
string sourceName;
|
|
|
|
if (auto location = boost::get_error_info<errinfo_sourceLocation>(*currentError))
|
|
|
|
{
|
|
|
|
// ignore the version pragma inserted by the testing tool when calculating locations.
|
|
|
|
if (location->start >= static_cast<int>(versionPragma.size()))
|
|
|
|
locationStart = location->start - versionPragma.size();
|
|
|
|
if (location->end >= static_cast<int>(versionPragma.size()))
|
|
|
|
locationEnd = location->end - versionPragma.size();
|
|
|
|
if (location->source)
|
|
|
|
sourceName = location->source->name();
|
|
|
|
}
|
|
|
|
m_errorList.emplace_back(SyntaxTestError{
|
|
|
|
currentError->typeName(),
|
|
|
|
errorMessage(*currentError),
|
|
|
|
sourceName,
|
|
|
|
locationStart,
|
|
|
|
locationEnd
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|