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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-03-12 12:33:37 +00:00
|
|
|
|
|
|
|
#include <test/libsolidity/SyntaxTest.h>
|
2021-06-29 12:38:59 +00:00
|
|
|
|
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
|
|
|
{
|
2020-03-06 00:22:51 +00:00
|
|
|
m_optimiseYul = m_reader.boolSetting("optimize-yul", true);
|
2019-05-30 14:35:42 +00:00
|
|
|
m_parserErrorRecovery = _parserErrorRecovery;
|
2018-03-12 12:33:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 14:51:30 +00:00
|
|
|
string SyntaxTest::addPreamble(string const& _sourceCode)
|
|
|
|
{
|
|
|
|
// Silence compiler version warning
|
|
|
|
string preamble = "pragma solidity >=0.0;\n";
|
|
|
|
// NOTE: this check is intentionally loose to match weird cases.
|
|
|
|
// We can manually adjust a test case where this causes problem.
|
|
|
|
if (_sourceCode.find("SPDX-License-Identifier:") == string::npos)
|
|
|
|
preamble += "// SPDX-License-Identifier: GPL-3.0\n";
|
|
|
|
return preamble + _sourceCode;
|
|
|
|
}
|
|
|
|
|
2019-12-03 15:50:04 +00:00
|
|
|
void SyntaxTest::setupCompiler()
|
|
|
|
{
|
|
|
|
compiler().reset();
|
2021-03-15 22:52:24 +00:00
|
|
|
auto sourcesWithPragma = m_sources.sources;
|
2019-12-03 15:50:04 +00:00
|
|
|
for (auto& source: sourcesWithPragma)
|
2020-12-18 14:51:30 +00:00
|
|
|
source.second = addPreamble(source.second);
|
2019-12-03 15:50:04 +00:00
|
|
|
compiler().setSources(sourcesWithPragma);
|
|
|
|
compiler().setEVMVersion(m_evmVersion);
|
|
|
|
compiler().setParserErrorRecovery(m_parserErrorRecovery);
|
|
|
|
compiler().setOptimiserSettings(
|
|
|
|
m_optimiseYul ?
|
|
|
|
OptimiserSettings::full() :
|
|
|
|
OptimiserSettings::minimal()
|
|
|
|
);
|
2021-10-27 17:52:36 +00:00
|
|
|
compiler().setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata);
|
|
|
|
compiler().setMetadataHash(CompilerStack::MetadataHash::None);
|
2019-12-03 15:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SyntaxTest::parseAndAnalyze()
|
|
|
|
{
|
2023-08-04 15:42:22 +00:00
|
|
|
setupCompiler();
|
|
|
|
|
2019-12-03 15:50:28 +00:00
|
|
|
if (compiler().parse() && compiler().analyze())
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!compiler().compile())
|
2020-07-21 13:20:05 +00:00
|
|
|
{
|
|
|
|
ErrorList const& errors = compiler().errors();
|
|
|
|
auto codeGeneretionErrorCount = count_if(errors.cbegin(), errors.cend(), [](auto const& error) {
|
|
|
|
return error->type() == Error::Type::CodeGenerationError;
|
|
|
|
});
|
|
|
|
auto errorCount = count_if(errors.cbegin(), errors.cend(), [](auto const& error) {
|
2021-06-30 12:48:45 +00:00
|
|
|
return Error::isError(error->type());
|
2020-07-21 13:20:05 +00:00
|
|
|
});
|
|
|
|
// failing compilation after successful analysis is a rare case,
|
|
|
|
// it assumes that errors contain exactly one error, and the error is of type Error::Type::CodeGenerationError
|
|
|
|
if (codeGeneretionErrorCount != 1 || errorCount != 1)
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Compilation failed even though analysis was successful."));
|
|
|
|
}
|
2019-12-03 15:50:28 +00:00
|
|
|
}
|
|
|
|
catch (UnimplementedFeatureError const& _e)
|
|
|
|
{
|
|
|
|
m_errorList.emplace_back(SyntaxTestError{
|
|
|
|
"UnimplementedFeatureError",
|
2020-06-22 14:53:03 +00:00
|
|
|
nullopt,
|
2019-12-03 15:50:28 +00:00
|
|
|
errorMessage(_e),
|
|
|
|
"",
|
|
|
|
-1,
|
|
|
|
-1
|
|
|
|
});
|
|
|
|
}
|
2023-08-04 15:42:22 +00:00
|
|
|
|
|
|
|
filterObtainedErrors();
|
2019-12-03 15:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SyntaxTest::filterObtainedErrors()
|
|
|
|
{
|
2023-08-04 18:38:15 +00:00
|
|
|
for (auto const& currentError: filteredErrors())
|
2019-12-03 15:50:04 +00:00
|
|
|
{
|
2021-12-01 11:30:36 +00:00
|
|
|
int locationStart = -1;
|
|
|
|
int locationEnd = -1;
|
2019-12-03 15:50:04 +00:00
|
|
|
string sourceName;
|
2021-12-01 11:30:36 +00:00
|
|
|
if (SourceLocation const* location = currentError->sourceLocation())
|
2019-12-03 15:50:04 +00:00
|
|
|
{
|
2023-05-18 13:22:09 +00:00
|
|
|
locationStart = location->start;
|
|
|
|
locationEnd = location->end;
|
2021-06-29 12:38:59 +00:00
|
|
|
solAssert(location->sourceName, "");
|
|
|
|
sourceName = *location->sourceName;
|
2023-05-18 13:22:09 +00:00
|
|
|
if(m_sources.sources.count(sourceName) == 1)
|
2021-01-15 13:45:53 +00:00
|
|
|
{
|
2023-05-18 13:22:09 +00:00
|
|
|
int preambleSize =
|
|
|
|
static_cast<int>(compiler().charStream(sourceName).size()) -
|
|
|
|
static_cast<int>(m_sources.sources[sourceName].size());
|
|
|
|
solAssert(preambleSize >= 0, "");
|
|
|
|
|
|
|
|
// ignore the version & license pragma inserted by the testing tool when calculating locations.
|
|
|
|
if (location->start != -1)
|
|
|
|
{
|
|
|
|
solAssert(location->start >= preambleSize, "");
|
|
|
|
locationStart = location->start - preambleSize;
|
|
|
|
}
|
|
|
|
if (location->end != -1)
|
|
|
|
{
|
|
|
|
solAssert(location->end >= preambleSize, "");
|
|
|
|
locationEnd = location->end - preambleSize;
|
|
|
|
}
|
2021-01-15 13:45:53 +00:00
|
|
|
}
|
2019-12-03 15:50:04 +00:00
|
|
|
}
|
|
|
|
m_errorList.emplace_back(SyntaxTestError{
|
2022-06-17 03:25:52 +00:00
|
|
|
Error::formatErrorType(currentError->type()),
|
2020-06-22 14:53:03 +00:00
|
|
|
currentError->errorId(),
|
2019-12-03 15:50:04 +00:00
|
|
|
errorMessage(*currentError),
|
|
|
|
sourceName,
|
|
|
|
locationStart,
|
|
|
|
locationEnd
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|