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>
|
2018-04-03 10:05:26 +00:00
|
|
|
#include <test/Options.h>
|
2018-03-12 12:33:37 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#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>
|
|
|
|
|
2018-11-14 16:11:55 +00:00
|
|
|
using namespace langutil;
|
2019-02-11 15:00:24 +00:00
|
|
|
using namespace dev::solidity;
|
2018-03-12 12:33:37 +00:00
|
|
|
using namespace dev::solidity::test;
|
2019-02-11 15:00:24 +00:00
|
|
|
using namespace dev::formatting;
|
|
|
|
using namespace dev;
|
2018-03-12 12:33:37 +00:00
|
|
|
using namespace std;
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
using namespace boost::unit_test;
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
namespace
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
2018-04-06 16:37:01 +00:00
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
int parseUnsignedInteger(string::iterator& _it, string::iterator _end)
|
2018-04-06 16:37:01 +00:00
|
|
|
{
|
|
|
|
if (_it == _end || !isdigit(*_it))
|
|
|
|
throw runtime_error("Invalid test expectation. Source location expected.");
|
|
|
|
int result = 0;
|
|
|
|
while (_it != _end && isdigit(*_it))
|
|
|
|
{
|
|
|
|
result *= 10;
|
|
|
|
result += *_it - '0';
|
|
|
|
++_it;
|
|
|
|
}
|
|
|
|
return result;
|
2018-03-12 12:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 14:35:42 +00:00
|
|
|
SyntaxTest::SyntaxTest(string const& _filename, langutil::EVMVersion _evmVersion, bool _parserErrorRecovery): m_evmVersion(_evmVersion)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
|
|
|
ifstream file(_filename);
|
|
|
|
if (!file)
|
2018-03-13 11:30:56 +00:00
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Cannot open test contract: \"" + _filename + "\"."));
|
2018-03-12 12:33:37 +00:00
|
|
|
file.exceptions(ios::badbit);
|
|
|
|
|
2019-08-16 12:50:31 +00:00
|
|
|
m_sources = parseSourcesAndSettings(file);
|
|
|
|
|
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
|
|
|
}
|
2018-03-12 12:33:37 +00:00
|
|
|
m_expectations = parseExpectations(file);
|
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-03-18 13:08:56 +00:00
|
|
|
bool SyntaxTest::printExpectationAndError(ostream& _stream, string const& _linePrefix, bool _formatted)
|
2018-11-15 15:47:52 +00:00
|
|
|
{
|
2018-04-03 10:05:26 +00:00
|
|
|
if (m_expectations != m_errorList)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
2018-04-03 10:05:26 +00:00
|
|
|
string nextIndentLevel = _linePrefix + " ";
|
2019-02-11 15:00:24 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Expected result:" << endl;
|
2018-04-03 10:05:26 +00:00
|
|
|
printErrorList(_stream, m_expectations, nextIndentLevel, _formatted);
|
2019-02-11 15:00:24 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Obtained result:" << endl;
|
2018-04-03 10:05:26 +00:00
|
|
|
printErrorList(_stream, m_errorList, nextIndentLevel, _formatted);
|
2018-03-12 12:33:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-18 13:08:56 +00:00
|
|
|
void SyntaxTest::printSource(ostream& _stream, string const& _linePrefix, bool _formatted) const
|
2018-06-08 12:17:50 +00:00
|
|
|
{
|
|
|
|
|
2019-08-16 12:50:31 +00:00
|
|
|
if (m_sources.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool outputSourceNames = true;
|
|
|
|
if (m_sources.size() == 1 && m_sources.begin()->first.empty())
|
|
|
|
outputSourceNames = false;
|
2018-06-08 12:17:50 +00:00
|
|
|
|
2019-08-16 12:50:31 +00:00
|
|
|
if (_formatted)
|
|
|
|
{
|
|
|
|
for (auto const& [name, source]: m_sources)
|
2018-06-08 12:17:50 +00:00
|
|
|
{
|
2019-08-16 12:50:31 +00:00
|
|
|
if (outputSourceNames)
|
|
|
|
_stream << _linePrefix << formatting::CYAN << "==== Source: " << name << " ====" << formatting::RESET << endl;
|
|
|
|
vector<char const*> sourceFormatting(source.length(), formatting::RESET);
|
|
|
|
for (auto const& error: m_errorList)
|
|
|
|
if (error.sourceName == name && error.locationStart >= 0 && error.locationEnd >= 0)
|
|
|
|
{
|
|
|
|
assert(static_cast<size_t>(error.locationStart) <= source.length());
|
|
|
|
assert(static_cast<size_t>(error.locationEnd) <= source.length());
|
|
|
|
bool isWarning = error.type == "Warning";
|
|
|
|
for (int i = error.locationStart; i < error.locationEnd; i++)
|
|
|
|
if (isWarning)
|
|
|
|
{
|
|
|
|
if (sourceFormatting[i] == formatting::RESET)
|
|
|
|
sourceFormatting[i] = formatting::ORANGE_BACKGROUND_256;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sourceFormatting[i] = formatting::RED_BACKGROUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
_stream << _linePrefix << sourceFormatting.front() << source.front();
|
|
|
|
for (size_t i = 1; i < source.length(); i++)
|
2018-06-08 12:17:50 +00:00
|
|
|
{
|
2019-08-16 12:50:31 +00:00
|
|
|
if (sourceFormatting[i] != sourceFormatting[i - 1])
|
|
|
|
_stream << sourceFormatting[i];
|
|
|
|
if (source[i] != '\n')
|
|
|
|
_stream << source[i];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_stream << formatting::RESET << endl;
|
|
|
|
if (i + 1 < source.length())
|
|
|
|
_stream << _linePrefix << sourceFormatting[i];
|
|
|
|
}
|
2018-06-08 12:17:50 +00:00
|
|
|
}
|
2019-08-16 12:50:31 +00:00
|
|
|
_stream << formatting::RESET;
|
2018-06-08 12:17:50 +00:00
|
|
|
}
|
2019-08-16 12:50:31 +00:00
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
}
|
|
|
|
else
|
2019-08-16 12:50:31 +00:00
|
|
|
for (auto const& [name, source]: m_sources)
|
|
|
|
{
|
|
|
|
if (outputSourceNames)
|
|
|
|
_stream << _linePrefix << "==== Source: " + name << " ====" << endl;
|
|
|
|
stringstream stream(source);
|
|
|
|
string line;
|
|
|
|
while (getline(stream, line))
|
|
|
|
_stream << _linePrefix << line << endl;
|
|
|
|
}
|
2018-06-08 12:17:50 +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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-12 12:33:37 +00:00
|
|
|
void SyntaxTest::printErrorList(
|
|
|
|
ostream& _stream,
|
2018-04-03 10:05:26 +00:00
|
|
|
vector<SyntaxTestError> const& _errorList,
|
2018-03-12 12:57:48 +00:00
|
|
|
string const& _linePrefix,
|
2019-03-18 13:08:56 +00:00
|
|
|
bool _formatted
|
2018-04-03 10:05:26 +00:00
|
|
|
)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
|
|
|
if (_errorList.empty())
|
2019-02-11 15:00:24 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, GREEN}) << _linePrefix << "Success" << endl;
|
2018-03-12 12:33:37 +00:00
|
|
|
else
|
|
|
|
for (auto const& error: _errorList)
|
2018-03-12 12:57:48 +00:00
|
|
|
{
|
|
|
|
{
|
2019-02-11 15:00:24 +00:00
|
|
|
AnsiColorized scope(_stream, _formatted, {BOLD, (error.type == "Warning") ? YELLOW : RED});
|
2018-03-15 15:27:34 +00:00
|
|
|
_stream << _linePrefix;
|
2018-04-03 10:05:26 +00:00
|
|
|
_stream << error.type << ": ";
|
2018-03-12 12:57:48 +00:00
|
|
|
}
|
2019-08-16 12:50:31 +00:00
|
|
|
if (!error.sourceName.empty() || error.locationStart >= 0 || error.locationEnd >= 0)
|
2018-04-06 16:37:01 +00:00
|
|
|
{
|
|
|
|
_stream << "(";
|
2019-08-16 12:50:31 +00:00
|
|
|
if (!error.sourceName.empty())
|
|
|
|
_stream << error.sourceName << ":";
|
2018-04-06 16:37:01 +00:00
|
|
|
if (error.locationStart >= 0)
|
|
|
|
_stream << error.locationStart;
|
|
|
|
_stream << "-";
|
|
|
|
if (error.locationEnd >= 0)
|
|
|
|
_stream << error.locationEnd;
|
|
|
|
_stream << "): ";
|
|
|
|
}
|
2018-04-03 10:05:26 +00:00
|
|
|
_stream << error.message << endl;
|
2018-03-12 12:57:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 10:05:26 +00:00
|
|
|
string SyntaxTest::errorMessage(Exception const& _e)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
2018-04-03 10:05:26 +00:00
|
|
|
if (_e.comment() && !_e.comment()->empty())
|
2018-03-12 12:33:37 +00:00
|
|
|
return boost::replace_all_copy(*_e.comment(), "\n", "\\n");
|
|
|
|
else
|
|
|
|
return "NONE";
|
|
|
|
}
|
|
|
|
|
2018-04-03 10:05:26 +00:00
|
|
|
vector<SyntaxTestError> SyntaxTest::parseExpectations(istream& _stream)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
2018-04-03 10:05:26 +00:00
|
|
|
vector<SyntaxTestError> expectations;
|
2018-03-12 12:33:37 +00:00
|
|
|
string line;
|
|
|
|
while (getline(_stream, line))
|
|
|
|
{
|
|
|
|
auto it = line.begin();
|
|
|
|
|
|
|
|
skipSlashes(it, line.end());
|
|
|
|
skipWhitespace(it, line.end());
|
|
|
|
|
|
|
|
if (it == line.end()) continue;
|
|
|
|
|
|
|
|
auto typeBegin = it;
|
|
|
|
while (it != line.end() && *it != ':')
|
|
|
|
++it;
|
|
|
|
string errorType(typeBegin, it);
|
|
|
|
|
|
|
|
// skip colon
|
|
|
|
if (it != line.end()) it++;
|
|
|
|
|
|
|
|
skipWhitespace(it, line.end());
|
|
|
|
|
2018-04-06 16:37:01 +00:00
|
|
|
int locationStart = -1;
|
|
|
|
int locationEnd = -1;
|
2019-08-16 12:50:31 +00:00
|
|
|
std::string sourceName;
|
2018-04-06 16:37:01 +00:00
|
|
|
|
|
|
|
if (it != line.end() && *it == '(')
|
|
|
|
{
|
|
|
|
++it;
|
2019-08-16 12:50:31 +00:00
|
|
|
if (it != line.end() && !isdigit(*it))
|
|
|
|
{
|
|
|
|
auto sourceNameStart = it;
|
|
|
|
while (it != line.end() && *it != ':')
|
|
|
|
++it;
|
|
|
|
sourceName = std::string(sourceNameStart, it);
|
|
|
|
expect(it, line.end(), ':');
|
|
|
|
}
|
2018-04-06 16:37:01 +00:00
|
|
|
locationStart = parseUnsignedInteger(it, line.end());
|
|
|
|
expect(it, line.end(), '-');
|
|
|
|
locationEnd = parseUnsignedInteger(it, line.end());
|
|
|
|
expect(it, line.end(), ')');
|
|
|
|
expect(it, line.end(), ':');
|
|
|
|
}
|
|
|
|
|
|
|
|
skipWhitespace(it, line.end());
|
|
|
|
|
2018-03-12 12:33:37 +00:00
|
|
|
string errorMessage(it, line.end());
|
2018-04-06 16:37:01 +00:00
|
|
|
expectations.emplace_back(SyntaxTestError{
|
|
|
|
move(errorType),
|
|
|
|
move(errorMessage),
|
2019-08-16 12:50:31 +00:00
|
|
|
move(sourceName),
|
2018-04-06 16:37:01 +00:00
|
|
|
locationStart,
|
|
|
|
locationEnd
|
|
|
|
});
|
2018-03-12 12:33:37 +00:00
|
|
|
}
|
|
|
|
return expectations;
|
|
|
|
}
|