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 -4
View File
@@ -57,11 +57,11 @@ BOOST_AUTO_TEST_CASE(all_assembly_items)
{ "sub.asm", 1 }
};
Assembly _assembly;
auto root_asm = make_shared<CharStream>("lorem ipsum", "root.asm");
auto root_asm = make_shared<string>("root.asm");
_assembly.setSourceLocation({1, 3, root_asm});
Assembly _subAsm;
auto sub_asm = make_shared<CharStream>("lorem ipsum", "sub.asm");
auto sub_asm = make_shared<string>("sub.asm");
_subAsm.setSourceLocation({6, 8, sub_asm});
// PushImmutable
_subAsm.appendImmutable("someImmutable");
@@ -172,11 +172,11 @@ BOOST_AUTO_TEST_CASE(immutable)
{ "sub.asm", 1 }
};
Assembly _assembly;
auto root_asm = make_shared<CharStream>("lorem ipsum", "root.asm");
auto root_asm = make_shared<string>("root.asm");
_assembly.setSourceLocation({1, 3, root_asm});
Assembly _subAsm;
auto sub_asm = make_shared<CharStream>("lorem ipsum", "sub.asm");
auto sub_asm = make_shared<string>("sub.asm");
_subAsm.setSourceLocation({6, 8, sub_asm});
_subAsm.appendImmutable("someImmutable");
_subAsm.appendImmutable("someOtherImmutable");
+3 -3
View File
@@ -34,9 +34,9 @@ BOOST_AUTO_TEST_SUITE(SourceLocationTest)
BOOST_AUTO_TEST_CASE(test_fail)
{
auto const source = std::make_shared<CharStream>("lorem ipsum", "source");
auto const sourceA = std::make_shared<CharStream>("lorem ipsum", "sourceA");
auto const sourceB = std::make_shared<CharStream>("lorem ipsum", "sourceB");
auto const source = std::make_shared<std::string>("source");
auto const sourceA = std::make_shared<std::string>("sourceA");
auto const sourceB = std::make_shared<std::string>("sourceB");
BOOST_CHECK(SourceLocation{} == SourceLocation{});
BOOST_CHECK((SourceLocation{0, 3, sourceA} != SourceLocation{0, 3, sourceB}));
+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.
-9
View File
@@ -53,15 +53,6 @@ Dialect const& defaultDialect(bool _yul)
}
}
void yul::test::printErrors(ErrorList const& _errors)
{
SourceReferenceFormatter formatter(cout, true, false);
for (auto const& error: _errors)
formatter.printErrorInformation(*error);
}
pair<shared_ptr<Block>, shared_ptr<yul::AsmAnalysisInfo>> yul::test::parse(string const& _source, bool _yul)
{
AssemblyStack stack(
-2
View File
@@ -44,8 +44,6 @@ struct Dialect;
namespace solidity::yul::test
{
void printErrors(langutil::ErrorList const& _errors);
std::pair<std::shared_ptr<Block>, std::shared_ptr<AsmAnalysisInfo>>
parse(std::string const& _source, bool _yul = true);
+2 -3
View File
@@ -51,9 +51,8 @@ TestCase::TestResult EVMCodeTransformTest::run(ostream& _stream, string const& _
if (!stack.parseAndAnalyze("", m_source))
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
SourceReferenceFormatter formatter(_stream, true, false);
for (auto const& error: stack.errors())
formatter.printErrorInformation(*error);
SourceReferenceFormatter{_stream, stack, true, false}
.printErrorInformation(stack.errors());
return TestResult::FatalError;
}
+7 -13
View File
@@ -63,7 +63,8 @@ TestCase::TestResult EwasmTranslationTest::run(ostream& _stream, string const& _
return TestResult::FatalError;
*m_object = EVMToEwasmTranslator(
EVMDialect::strictAssemblyForEVMObjects(solidity::test::CommonOptions::get().evmVersion())
EVMDialect::strictAssemblyForEVMObjects(solidity::test::CommonOptions::get().evmVersion()),
m_stack
).run(*m_object);
// Add call to "main()".
@@ -78,20 +79,21 @@ TestCase::TestResult EwasmTranslationTest::run(ostream& _stream, string const& _
bool EwasmTranslationTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)
{
AssemblyStack stack(
m_stack = AssemblyStack(
solidity::test::CommonOptions::get().evmVersion(),
AssemblyStack::Language::StrictAssembly,
solidity::frontend::OptimiserSettings::none()
);
if (stack.parseAndAnalyze("", m_source))
if (m_stack.parseAndAnalyze("", m_source))
{
m_object = stack.parserResult();
m_object = m_stack.parserResult();
return true;
}
else
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
printErrors(_stream, stack.errors());
SourceReferenceFormatter{_stream, m_stack, true, false}
.printErrorInformation(m_stack.errors());
return false;
}
}
@@ -114,11 +116,3 @@ string EwasmTranslationTest::interpret()
state.dumpTraceAndState(result);
return result.str();
}
void EwasmTranslationTest::printErrors(ostream& _stream, ErrorList const& _errors)
{
SourceReferenceFormatter formatter(_stream, true, false);
for (auto const& error: _errors)
formatter.printErrorInformation(*error);
}
+3 -8
View File
@@ -20,16 +20,12 @@
#include <test/TestCase.h>
namespace solidity::langutil
{
class Scanner;
class Error;
using ErrorList = std::vector<std::shared_ptr<Error const>>;
}
#include <libyul/AssemblyStack.h>
namespace solidity::yul
{
struct Object;
class AssemblyStack;
}
namespace solidity::yul::test
@@ -51,9 +47,8 @@ private:
bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted);
std::string interpret();
static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors);
std::shared_ptr<Object> m_object;
AssemblyStack m_stack;
};
}
+2 -9
View File
@@ -69,7 +69,8 @@ TestCase::TestResult ObjectCompilerTest::run(ostream& _stream, string const& _li
if (!stack.parseAndAnalyze("source", m_source))
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
printErrors(_stream, stack.errors());
SourceReferenceFormatter{_stream, stack, true, false}
.printErrorInformation(stack.errors());
return TestResult::FatalError;
}
stack.optimize();
@@ -104,11 +105,3 @@ TestCase::TestResult ObjectCompilerTest::run(ostream& _stream, string const& _li
return checkResult(_stream, _linePrefix, _formatted);
}
void ObjectCompilerTest::printErrors(ostream& _stream, ErrorList const& _errors)
{
SourceReferenceFormatter formatter(_stream, true, false);
for (auto const& error: _errors)
formatter.printErrorInformation(*error);
}
-2
View File
@@ -54,8 +54,6 @@ private:
bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted);
void disambiguate();
static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors);
frontend::OptimisationPreset m_optimisationPreset;
bool m_wasm = false;
};
+42 -46
View File
@@ -51,24 +51,19 @@ namespace solidity::yul::test
namespace
{
string_view constexpr g_strAlternateSourceText = "{}";
shared_ptr<Block> parse(string const& _source, Dialect const& _dialect, ErrorReporter& errorReporter)
{
try
{
auto scanner = make_shared<Scanner>(CharStream(_source, ""));
map<unsigned, shared_ptr<CharStream>> indicesToCharStreams;
indicesToCharStreams[0] = scanner->charStream();
indicesToCharStreams[1] = make_shared<CharStream>(
string(g_strAlternateSourceText.data(), g_strAlternateSourceText.size()),
"alternate.sol"
);
map<unsigned, shared_ptr<string const>> indicesToSourceNames;
indicesToSourceNames[0] = make_shared<string const>("source0");
indicesToSourceNames[1] = make_shared<string const>("source1");
auto parserResult = yul::Parser(
errorReporter,
_dialect,
move(indicesToCharStreams)
move(indicesToSourceNames)
).parse(scanner, false);
if (parserResult)
{
@@ -200,9 +195,9 @@ BOOST_AUTO_TEST_CASE(default_types_set)
);
}
#define CHECK_LOCATION(_actual, _sourceText, _start, _end) \
#define CHECK_LOCATION(_actual, _sourceName, _start, _end) \
do { \
BOOST_CHECK_EQUAL((_sourceText), ((_actual).source ? (_actual).source->source() : "")); \
BOOST_CHECK_EQUAL((_sourceName), ((_actual).sourceName ? *(_actual).sourceName : "")); \
BOOST_CHECK_EQUAL((_start), (_actual).start); \
BOOST_CHECK_EQUAL((_end), (_actual).end); \
} while (0)
@@ -217,7 +212,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_empty_block)
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 234, 543);
CHECK_LOCATION(result->debugData->location, "source0", 234, 543);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_block_with_children)
@@ -235,12 +230,12 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_with_children)
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 234, 543);
CHECK_LOCATION(result->debugData->location, "source0", 234, 543);
BOOST_REQUIRE_EQUAL(3, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), sourceText, 234, 543);
CHECK_LOCATION(locationOf(result->statements.at(1)), sourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 234, 543);
CHECK_LOCATION(locationOf(result->statements.at(1)), "source0", 123, 432);
// [2] is inherited source location
CHECK_LOCATION(locationOf(result->statements.at(2)), sourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(2)), "source0", 123, 432);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_block_different_sources)
@@ -258,12 +253,12 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_different_sources)
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 234, 543);
CHECK_LOCATION(result->debugData->location, "source0", 234, 543);
BOOST_REQUIRE_EQUAL(3, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), sourceText, 234, 543);
CHECK_LOCATION(locationOf(result->statements.at(1)), g_strAlternateSourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 234, 543);
CHECK_LOCATION(locationOf(result->statements.at(1)), "source1", 123, 432);
// [2] is inherited source location
CHECK_LOCATION(locationOf(result->statements.at(2)), g_strAlternateSourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(2)), "source1", 123, 432);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_block_nested)
@@ -280,9 +275,9 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_nested)
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 234, 543);
CHECK_LOCATION(result->debugData->location, "source0", 234, 543);
BOOST_REQUIRE_EQUAL(2, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(1)), sourceText, 343, 434);
CHECK_LOCATION(locationOf(result->statements.at(1)), "source0", 343, 434);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_block_switch_case)
@@ -304,19 +299,19 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_switch_case)
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 234, 543);
CHECK_LOCATION(result->debugData->location, "source0", 234, 543);
BOOST_REQUIRE_EQUAL(2, result->statements.size());
BOOST_REQUIRE(holds_alternative<Switch>(result->statements.at(1)));
auto const& switchStmt = get<Switch>(result->statements.at(1));
CHECK_LOCATION(switchStmt.debugData->location, sourceText, 343, 434);
CHECK_LOCATION(switchStmt.debugData->location, "source0", 343, 434);
BOOST_REQUIRE_EQUAL(1, switchStmt.cases.size());
CHECK_LOCATION(switchStmt.cases.at(0).debugData->location, sourceText, 3141, 59265);
CHECK_LOCATION(switchStmt.cases.at(0).debugData->location, "source0", 3141, 59265);
auto const& caseBody = switchStmt.cases.at(0).body;
BOOST_REQUIRE_EQUAL(1, caseBody.statements.size());
CHECK_LOCATION(locationOf(caseBody.statements.at(0)), sourceText, 271, 828);
CHECK_LOCATION(locationOf(caseBody.statements.at(0)), "source0", 271, 828);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_inherit_into_outer_scope)
@@ -337,19 +332,19 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_inherit_into_outer_scope)
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 1, 100);
CHECK_LOCATION(result->debugData->location, "source0", 1, 100);
BOOST_REQUIRE_EQUAL(3, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), sourceText, 1, 100);
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 1, 100);
// First child element must be a block itself with one statement.
BOOST_REQUIRE(holds_alternative<Block>(result->statements.at(0)));
BOOST_REQUIRE_EQUAL(get<Block>(result->statements.at(0)).statements.size(), 1);
CHECK_LOCATION(locationOf(get<Block>(result->statements.at(0)).statements.at(0)), sourceText, 123, 432);
CHECK_LOCATION(locationOf(get<Block>(result->statements.at(0)).statements.at(0)), "source0", 123, 432);
// The next two elements have an inherited source location from the prior inner scope.
CHECK_LOCATION(locationOf(result->statements.at(1)), sourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(2)), sourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(1)), "source0", 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(2)), "source0", 123, 432);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_assign_empty)
@@ -368,8 +363,8 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_assign_empty)
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); // should still parse
BOOST_REQUIRE_EQUAL(2, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), sourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(1)), g_strAlternateSourceText, 1, 10);
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(1)), "source1", 1, 10);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_source_index)
@@ -407,10 +402,10 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_1)
BOOST_REQUIRE(!!result);
BOOST_REQUIRE_EQUAL(1, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), sourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 123, 432);
BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0)));
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
CHECK_LOCATION(locationOf(*varDecl.value), sourceText, 234, 2026);
CHECK_LOCATION(locationOf(*varDecl.value), "source0", 234, 2026);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_2)
@@ -429,20 +424,20 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_2)
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
BOOST_REQUIRE_EQUAL(1, result->statements.size());
CHECK_LOCATION(result->debugData->location, sourceText, 0, 5);
CHECK_LOCATION(result->debugData->location, "source0", 0, 5);
// `let x := add(1, `
BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0)));
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
CHECK_LOCATION(varDecl.debugData->location, sourceText, 0, 5);
CHECK_LOCATION(varDecl.debugData->location, "source0", 0, 5);
BOOST_REQUIRE(!!varDecl.value);
BOOST_REQUIRE(holds_alternative<FunctionCall>(*varDecl.value));
FunctionCall const& call = get<FunctionCall>(*varDecl.value);
CHECK_LOCATION(call.debugData->location, g_strAlternateSourceText, 2, 3);
CHECK_LOCATION(call.debugData->location, "source1", 2, 3);
// `2`
BOOST_REQUIRE_EQUAL(2, call.arguments.size());
CHECK_LOCATION(locationOf(call.arguments.at(1)), sourceText, 4, 8);
CHECK_LOCATION(locationOf(call.arguments.at(1)), "source0", 4, 8);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_3)
@@ -463,24 +458,24 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_3)
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
BOOST_REQUIRE_EQUAL(2, result->statements.size());
CHECK_LOCATION(result->debugData->location, g_strAlternateSourceText, 23, 45);
CHECK_LOCATION(result->debugData->location, "source1", 23, 45);
BOOST_REQUIRE(holds_alternative<Block>(result->statements.at(0)));
Block const& innerBlock = get<Block>(result->statements.at(0));
CHECK_LOCATION(innerBlock.debugData->location, g_strAlternateSourceText, 23, 45);
CHECK_LOCATION(innerBlock.debugData->location, "source1", 23, 45);
BOOST_REQUIRE_EQUAL(1, innerBlock.statements.size());
BOOST_REQUIRE(holds_alternative<ExpressionStatement>(result->statements.at(1)));
ExpressionStatement const& sstoreStmt = get<ExpressionStatement>(innerBlock.statements.at(0));
BOOST_REQUIRE(holds_alternative<FunctionCall>(sstoreStmt.expression));
FunctionCall const& sstoreCall = get<FunctionCall>(sstoreStmt.expression);
CHECK_LOCATION(sstoreCall.debugData->location, g_strAlternateSourceText, 23, 45);
CHECK_LOCATION(sstoreCall.debugData->location, "source1", 23, 45);
BOOST_REQUIRE(holds_alternative<ExpressionStatement>(result->statements.at(1)));
ExpressionStatement mstoreStmt = get<ExpressionStatement>(result->statements.at(1));
BOOST_REQUIRE(holds_alternative<FunctionCall>(mstoreStmt.expression));
FunctionCall const& mstoreCall = get<FunctionCall>(mstoreStmt.expression);
CHECK_LOCATION(mstoreCall.debugData->location, sourceText, 420, 680);
CHECK_LOCATION(mstoreCall.debugData->location, "source0", 420, 680);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_comments_after_valid)
@@ -499,11 +494,11 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_comments_after_valid)
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
BOOST_REQUIRE_EQUAL(1, result->statements.size());
CHECK_LOCATION(result->debugData->location, g_strAlternateSourceText, 23, 45);
CHECK_LOCATION(result->debugData->location, "source1", 23, 45);
BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0)));
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
CHECK_LOCATION(varDecl.debugData->location, sourceText, 420, 680);
CHECK_LOCATION(varDecl.debugData->location, "source0", 420, 680);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_suffix)
@@ -553,7 +548,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_ensure_last_match)
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
// Ensure the latest @src per documentation-comment is used (0:30:40).
CHECK_LOCATION(varDecl.debugData->location, sourceText, 30, 40);
CHECK_LOCATION(varDecl.debugData->location, "source0", 30, 40);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_reference_original_sloc)
@@ -573,6 +568,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_reference_original_sloc)
BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0)));
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
// -1 points to original source code, which in this case is `"source0"` (which is also
CHECK_LOCATION(varDecl.debugData->location, "", 10, 20);
}
+2 -9
View File
@@ -78,7 +78,8 @@ bool YulInterpreterTest::parse(ostream& _stream, string const& _linePrefix, bool
else
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
printErrors(_stream, stack.errors());
SourceReferenceFormatter{_stream, stack, true, false}
.printErrorInformation(stack.errors());
return false;
}
}
@@ -101,11 +102,3 @@ string YulInterpreterTest::interpret()
state.dumpTraceAndState(result);
return result.str();
}
void YulInterpreterTest::printErrors(ostream& _stream, ErrorList const& _errors)
{
SourceReferenceFormatter formatter(_stream, true, false);
for (auto const& error: _errors)
formatter.printErrorInformation(*error);
}
-9
View File
@@ -20,13 +20,6 @@
#include <test/TestCase.h>
namespace solidity::langutil
{
class Scanner;
class Error;
using ErrorList = std::vector<std::shared_ptr<Error const>>;
}
namespace solidity::yul
{
struct AsmAnalysisInfo;
@@ -52,8 +45,6 @@ private:
bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted);
std::string interpret();
static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors);
std::shared_ptr<Block> m_ast;
std::shared_ptr<AsmAnalysisInfo> m_analysisInfo;
};
+4 -9
View File
@@ -28,6 +28,7 @@
#include <libyul/AsmPrinter.h>
#include <liblangutil/SourceReferenceFormatter.h>
#include <liblangutil/Scanner.h>
#include <libsolutil/AnsiColorized.h>
@@ -115,16 +116,10 @@ std::pair<std::shared_ptr<Object>, std::shared_ptr<AsmAnalysisInfo>> YulOptimize
if (!object || !analysisInfo || !Error::containsOnlyWarnings(errors))
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
printErrors(_stream, errors);
CharStream charStream(_source, "");
SourceReferenceFormatter{_stream, SingletonCharStreamProvider(charStream), true, false}
.printErrorInformation(errors);
return {};
}
return {std::move(object), std::move(analysisInfo)};
}
void YulOptimizerTest::printErrors(ostream& _stream, ErrorList const& _errors)
{
SourceReferenceFormatter formatter(_stream, true, false);
for (auto const& error: _errors)
formatter.printErrorInformation(*error);
}
-1
View File
@@ -51,7 +51,6 @@ private:
std::pair<std::shared_ptr<Object>, std::shared_ptr<AsmAnalysisInfo>> parse(
std::ostream& _stream, std::string const& _linePrefix, bool const _formatted, std::string const& _source
);
static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors);
std::string m_optimizerStep;
@@ -41,13 +41,11 @@ optional<CompilerOutput> SolidityCompilationFramework::compileContract()
{
if (m_compilerInput.debugFailure)
{
SourceReferenceFormatter formatter(cerr, false, false);
cerr << "Compiling contract failed" << endl;
for (auto const& error: m_compiler.errors())
formatter.printExceptionInformation(
cerr << SourceReferenceFormatter::formatErrorInformation(
*error,
formatter.formatErrorInformation(*error)
m_compiler
);
}
return {};
+7 -15
View File
@@ -43,20 +43,6 @@ using namespace solidity::yul;
using namespace solidity::yul::test;
using namespace solidity::yul::test::yul_fuzzer;
namespace
{
void printErrors(ostream& _stream, ErrorList const& _errors)
{
SourceReferenceFormatter formatter(_stream, false, false);
for (auto const& error: _errors)
formatter.printExceptionInformation(
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
}
}
DEFINE_PROTO_FUZZER(Program const& _input)
{
ProtoConverter converter;
@@ -88,7 +74,13 @@ DEFINE_PROTO_FUZZER(Program const& _input)
!Error::containsOnlyWarnings(stack.errors())
)
{
printErrors(std::cout, stack.errors());
SourceReferenceFormatter formatter(std::cout, stack, false, false);
for (auto const& error: stack.errors())
formatter.printExceptionInformation(
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
yulAssert(false, "Proto fuzzer generated malformed program");
}
+11 -6
View File
@@ -44,6 +44,8 @@
#include <libsolutil/JSON.h>
#include <libsolidity/interface/OptimiserSettings.h>
#include <liblangutil/CharStreamProvider.h>
#include <liblangutil/Scanner.h>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/join.hpp>
@@ -78,17 +80,19 @@ class YulOpti
public:
void printErrors()
{
SourceReferenceFormatter formatter(cerr, true, false);
for (auto const& error: m_errors)
formatter.printErrorInformation(*error);
SourceReferenceFormatter{
cerr,
SingletonCharStreamProvider(*m_scanner->charStream()),
true,
false
}.printErrorInformation(m_errors);
}
bool parse(string const& _input)
{
ErrorReporter errorReporter(m_errors);
shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(_input, ""));
m_ast = yul::Parser(errorReporter, m_dialect).parse(scanner, false);
m_scanner = make_shared<Scanner>(CharStream(_input, ""));
m_ast = yul::Parser(errorReporter, m_dialect).parse(m_scanner, false);
if (!m_ast || !errorReporter.errors().empty())
{
cerr << "Error parsing source." << endl;
@@ -230,6 +234,7 @@ public:
private:
ErrorList m_errors;
shared_ptr<Scanner> m_scanner;
shared_ptr<yul::Block> m_ast;
Dialect const& m_dialect{EVMDialect::strictAssemblyForEVMObjects(EVMVersion{})};
shared_ptr<AsmAnalysisInfo> m_analysisInfo;
+1 -7
View File
@@ -53,12 +53,6 @@ namespace po = boost::program_options;
namespace
{
void printErrors(ErrorList const& _errors)
{
for (auto const& error: _errors)
SourceReferenceFormatter(cout, true, false).printErrorInformation(*error);
}
pair<shared_ptr<Block>, shared_ptr<AsmAnalysisInfo>> parse(string const& _source)
{
AssemblyStack stack(
@@ -73,7 +67,7 @@ pair<shared_ptr<Block>, shared_ptr<AsmAnalysisInfo>> parse(string const& _source
}
else
{
printErrors(stack.errors());
SourceReferenceFormatter(cout, stack, true, false).printErrorInformation(stack.errors());
return {};
}
}