Remove CharStream from SourceLocation.

This commit is contained in:
chriseth
2021-07-14 15:12:07 +02:00
parent 57d32ca252
commit f75b55071e
73 changed files with 613 additions and 560 deletions
+4 -6
View File
@@ -141,9 +141,8 @@ TestCase::TestResult ASTJSONTest::run(ostream& _stream, string const& _linePrefi
if (!c.compile(CompilerStack::State::Parsed))
{
SourceReferenceFormatter formatter(_stream, _formatted, false);
for (auto const& error: c.errors())
formatter.printErrorInformation(*error);
SourceReferenceFormatter formatter(_stream, c, _formatted, false);
formatter.printErrorInformation(c.errors());
return TestResult::FatalError;
}
@@ -167,9 +166,8 @@ TestCase::TestResult ASTJSONTest::run(ostream& _stream, string const& _linePrefi
if (m_expectation.empty())
return resultsMatch ? TestResult::Success : TestResult::Failure;
SourceReferenceFormatter formatter(_stream, _formatted, false);
for (auto const& error: c.errors())
formatter.printErrorInformation(*error);
SourceReferenceFormatter{_stream, c, _formatted, false}
.printErrorInformation(c.errors());
return TestResult::FatalError;
}
+1 -1
View File
@@ -151,7 +151,7 @@ string AnalysisFramework::formatErrors() const
string AnalysisFramework::formatError(Error const& _error) const
{
return SourceReferenceFormatter::formatErrorInformation(_error);
return SourceReferenceFormatter::formatErrorInformation(_error, *m_compiler);
}
ContractDefinition const* AnalysisFramework::retrieveContractByName(SourceUnit const& _source, string const& _name)
+15 -14
View File
@@ -109,7 +109,7 @@ void printAssemblyLocations(AssemblyItems const& _items)
", " <<
_loc.end <<
", make_shared<string>(\"" <<
_loc.source->name() <<
*_loc.sourceName <<
"\")}) +" << endl;
};
@@ -157,15 +157,16 @@ BOOST_AUTO_TEST_SUITE(Assembly)
BOOST_AUTO_TEST_CASE(location_test)
{
auto sourceCode = make_shared<CharStream>(R"(
string sourceCode = R"(
pragma abicoder v1;
contract test {
function f() public returns (uint256 a) {
return 16;
}
}
)", "");
AssemblyItems items = compileContract(sourceCode);
)";
AssemblyItems items = compileContract(make_shared<CharStream>(sourceCode, ""));
shared_ptr<string> sourceName = make_shared<string>();
bool hasShifts = solidity::test::CommonOptions::get().evmVersion().hasBitwiseShifting();
auto codegenCharStream = make_shared<CharStream>("", "--CODEGEN--");
@@ -173,18 +174,18 @@ BOOST_AUTO_TEST_CASE(location_test)
vector<SourceLocation> locations;
if (solidity::test::CommonOptions::get().optimize)
locations =
vector<SourceLocation>(31, SourceLocation{23, 103, sourceCode}) +
vector<SourceLocation>(1, SourceLocation{41, 100, sourceCode}) +
vector<SourceLocation>(1, SourceLocation{93, 95, sourceCode}) +
vector<SourceLocation>(15, SourceLocation{41, 100, sourceCode});
vector<SourceLocation>(31, SourceLocation{23, 103, sourceName}) +
vector<SourceLocation>(1, SourceLocation{41, 100, sourceName}) +
vector<SourceLocation>(1, SourceLocation{93, 95, sourceName}) +
vector<SourceLocation>(15, SourceLocation{41, 100, sourceName});
else
locations =
vector<SourceLocation>(hasShifts ? 31 : 32, SourceLocation{23, 103, sourceCode}) +
vector<SourceLocation>(24, SourceLocation{41, 100, sourceCode}) +
vector<SourceLocation>(1, SourceLocation{70, 79, sourceCode}) +
vector<SourceLocation>(1, SourceLocation{93, 95, sourceCode}) +
vector<SourceLocation>(2, SourceLocation{86, 95, sourceCode}) +
vector<SourceLocation>(2, SourceLocation{41, 100, sourceCode});
vector<SourceLocation>(hasShifts ? 31 : 32, SourceLocation{23, 103, sourceName}) +
vector<SourceLocation>(24, SourceLocation{41, 100, sourceName}) +
vector<SourceLocation>(1, SourceLocation{70, 79, sourceName}) +
vector<SourceLocation>(1, SourceLocation{93, 95, sourceName}) +
vector<SourceLocation>(2, SourceLocation{86, 95, sourceName}) +
vector<SourceLocation>(2, SourceLocation{41, 100, sourceName});
checkAssemblyLocations(items, locations);
}
+2 -3
View File
@@ -118,9 +118,8 @@ TestCase::TestResult GasTest::run(ostream& _stream, string const& _linePrefix, b
if (!compiler().parseAndAnalyze() || !compiler().compile())
{
SourceReferenceFormatter formatter(_stream, _formatted, false);
for (auto const& error: compiler().errors())
formatter.printErrorInformation(*error);
SourceReferenceFormatter{_stream, compiler(), _formatted, false}
.printErrorInformation(compiler().errors());
return TestResult::FatalError;
}
+1 -1
View File
@@ -81,7 +81,7 @@ std::optional<Error> parseAndReturnFirstError(
{
string errors;
for (auto const& err: stack.errors())
errors += SourceReferenceFormatter::formatErrorInformation(*err);
errors += SourceReferenceFormatter::formatErrorInformation(*err, stack);
BOOST_FAIL("Found more than one error:\n" + errors);
}
error = e;
@@ -65,10 +65,8 @@ bytes SolidityExecutionFramework::multiSourceCompileContract(
for (auto const& error: m_compiler.errors())
if (error->type() == langutil::Error::Type::CodeGenerationError)
BOOST_THROW_EXCEPTION(*error);
langutil::SourceReferenceFormatter formatter(std::cerr, true, false);
for (auto const& error: m_compiler.errors())
formatter.printErrorInformation(*error);
langutil::SourceReferenceFormatter{std::cerr, m_compiler, true, false}
.printErrorInformation(m_compiler.errors());
BOOST_ERROR("Compiling contract failed");
}
string contractName(_contractName.empty() ? m_compiler.lastContractName(_mainSourceName) : _contractName);
+4 -2
View File
@@ -591,19 +591,21 @@ BOOST_AUTO_TEST_CASE(inline_asm_end_location)
class CheckInlineAsmLocation: public ASTConstVisitor
{
public:
explicit CheckInlineAsmLocation(string _sourceCode): m_sourceCode(_sourceCode) {}
bool visited = false;
bool visit(InlineAssembly const& _inlineAsm) override
{
auto loc = _inlineAsm.location();
auto asmStr = loc.source->source().substr(static_cast<size_t>(loc.start), static_cast<size_t>(loc.end - loc.start));
auto asmStr = m_sourceCode.substr(static_cast<size_t>(loc.start), static_cast<size_t>(loc.end - loc.start));
BOOST_CHECK_EQUAL(asmStr, "assembly { a := 0x12345678 }");
visited = true;
return false;
}
string m_sourceCode;
};
CheckInlineAsmLocation visitor;
CheckInlineAsmLocation visitor{sourceCode};
contract->accept(visitor);
BOOST_CHECK_MESSAGE(visitor.visited, "No inline asm block found?!");
+7 -4
View File
@@ -17,6 +17,7 @@
// SPDX-License-Identifier: GPL-3.0
#include <test/libsolidity/SyntaxTest.h>
#include <test/Common.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
@@ -119,11 +120,13 @@ void SyntaxTest::filterObtainedErrors()
string sourceName;
if (auto location = boost::get_error_info<errinfo_sourceLocation>(*currentError))
{
solAssert(location->source, "");
sourceName = location->source->name();
solAssert(location->sourceName, "");
sourceName = *location->sourceName;
solAssert(m_sources.sources.count(sourceName) == 1, "");
int preambleSize = static_cast<int>(location->source->source().size()) - static_cast<int>(m_sources.sources[sourceName].size());
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.