Merge pull request #3135 from federicobond/formatter-instance

Convert static SourceReferenceFormatter functions to member ones
This commit is contained in:
chriseth 2018-02-19 17:34:07 +01:00 committed by GitHub
commit abc23ac608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 70 deletions

View File

@ -31,15 +31,11 @@ namespace dev
namespace solidity
{
void SourceReferenceFormatter::printSourceLocation(
ostream& _stream,
SourceLocation const* _location,
function<Scanner const&(string const&)> const& _scannerFromSourceName
)
void SourceReferenceFormatter::printSourceLocation(SourceLocation const* _location)
{
if (!_location || !_location->sourceName)
return; // Nothing we can print here
auto const& scanner = _scannerFromSourceName(*_location->sourceName);
auto const& scanner = m_scannerFromSourceName(*_location->sourceName);
int startLine;
int startColumn;
tie(startLine, startColumn) = scanner.translatePositionToLineColumn(_location->start);
@ -64,21 +60,22 @@ void SourceReferenceFormatter::printSourceLocation(
endColumn = startColumn + locationLength;
}
_stream << line << endl;
m_stream << line << endl;
for_each(
line.cbegin(),
line.cbegin() + startColumn,
[&_stream](char const& ch) { _stream << (ch == '\t' ? '\t' : ' '); }
[this](char const& ch) { m_stream << (ch == '\t' ? '\t' : ' '); }
);
_stream << "^";
m_stream << "^";
if (endColumn > startColumn + 2)
_stream << string(endColumn - startColumn - 2, '-');
m_stream << string(endColumn - startColumn - 2, '-');
if (endColumn > startColumn + 1)
_stream << "^";
_stream << endl;
m_stream << "^";
m_stream << endl;
}
else
_stream <<
m_stream <<
scanner.lineAtPosition(_location->start) <<
endl <<
string(startColumn, ' ') <<
@ -86,50 +83,44 @@ void SourceReferenceFormatter::printSourceLocation(
"Spanning multiple lines.\n";
}
void SourceReferenceFormatter::printSourceName(
ostream& _stream,
SourceLocation const* _location,
function<Scanner const&(string const&)> const& _scannerFromSourceName
)
void SourceReferenceFormatter::printSourceName(SourceLocation const* _location)
{
if (!_location || !_location->sourceName)
return; // Nothing we can print here
auto const& scanner = _scannerFromSourceName(*_location->sourceName);
auto const& scanner = m_scannerFromSourceName(*_location->sourceName);
int startLine;
int startColumn;
tie(startLine, startColumn) = scanner.translatePositionToLineColumn(_location->start);
_stream << *_location->sourceName << ":" << (startLine + 1) << ":" << (startColumn + 1) << ": ";
m_stream << *_location->sourceName << ":" << (startLine + 1) << ":" << (startColumn + 1) << ": ";
}
void SourceReferenceFormatter::printExceptionInformation(
ostream& _stream,
Exception const& _exception,
string const& _name,
function<Scanner const&(string const&)> const& _scannerFromSourceName
string const& _name
)
{
SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
auto secondarylocation = boost::get_error_info<errinfo_secondarySourceLocation>(_exception);
printSourceName(_stream, location, _scannerFromSourceName);
printSourceName(location);
_stream << _name;
m_stream << _name;
if (string const* description = boost::get_error_info<errinfo_comment>(_exception))
_stream << ": " << *description << endl;
m_stream << ": " << *description << endl;
else
_stream << endl;
m_stream << endl;
printSourceLocation(_stream, location, _scannerFromSourceName);
printSourceLocation(location);
if (secondarylocation && !secondarylocation->infos.empty())
{
for (auto info: secondarylocation->infos)
{
printSourceName(_stream, &info.second, _scannerFromSourceName);
_stream << info.first << endl;
printSourceLocation(_stream, &info.second, _scannerFromSourceName);
printSourceName(&info.second);
m_stream << info.first << endl;
printSourceLocation(&info.second);
}
_stream << endl;
m_stream << endl;
}
}

View File

@ -38,22 +38,23 @@ namespace solidity
class Scanner; // forward
class CompilerStack; // forward
struct SourceReferenceFormatter
class SourceReferenceFormatter
{
public:
using ScannerFromSourceNameFun = std::function<Scanner const&(std::string const&)>;
explicit SourceReferenceFormatter(
std::ostream& _stream,
ScannerFromSourceNameFun _scannerFromSourceName
):
m_stream(_stream),
m_scannerFromSourceName(std::move(_scannerFromSourceName))
{}
/// Prints source location if it is given.
static void printSourceLocation(
std::ostream& _stream,
SourceLocation const* _location,
ScannerFromSourceNameFun const& _scannerFromSourceName
);
static void printExceptionInformation(
std::ostream& _stream,
Exception const& _exception,
std::string const& _name,
ScannerFromSourceNameFun const& _scannerFromSourceName
);
void printSourceLocation(SourceLocation const* _location);
void printExceptionInformation(Exception const& _exception, std::string const& _name);
static std::string formatExceptionInformation(
Exception const& _exception,
std::string const& _name,
@ -61,16 +62,17 @@ public:
)
{
std::ostringstream errorOutput;
printExceptionInformation(errorOutput, _exception, _name, _scannerFromSourceName);
SourceReferenceFormatter formatter(errorOutput, _scannerFromSourceName);
formatter.printExceptionInformation(_exception, _name);
return errorOutput.str();
}
private:
/// Prints source name if location is given.
static void printSourceName(
std::ostream& _stream,
SourceLocation const* _location,
ScannerFromSourceNameFun const& _scannerFromSourceName
);
void printSourceName(SourceLocation const* _location);
std::ostream& m_stream;
ScannerFromSourceNameFun m_scannerFromSourceName;
};
}

View File

@ -777,7 +777,10 @@ bool CommandLineInterface::processInput()
}
m_compiler.reset(new CompilerStack(fileReader));
auto scannerFromSourceName = [&](string const& _sourceName) -> solidity::Scanner const& { return m_compiler->scanner(_sourceName); };
SourceReferenceFormatter formatter(cerr, scannerFromSourceName);
try
{
if (m_args.count(g_argMetadataLiteral) > 0)
@ -796,11 +799,9 @@ bool CommandLineInterface::processInput()
bool successful = m_compiler->compile();
for (auto const& error: m_compiler->errors())
SourceReferenceFormatter::printExceptionInformation(
cerr,
formatter.printExceptionInformation(
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error",
scannerFromSourceName
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
if (!successful)
@ -808,7 +809,7 @@ bool CommandLineInterface::processInput()
}
catch (CompilerError const& _exception)
{
SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Compiler error", scannerFromSourceName);
formatter.printExceptionInformation(_exception, "Compiler error");
return false;
}
catch (InternalCompilerError const& _exception)
@ -828,7 +829,7 @@ bool CommandLineInterface::processInput()
if (_error.type() == Error::Type::DocstringParsingError)
cerr << "Documentation parsing error: " << *boost::get_error_info<errinfo_comment>(_error) << endl;
else
SourceReferenceFormatter::printExceptionInformation(cerr, _error, _error.typeName(), scannerFromSourceName);
formatter.printExceptionInformation(_error, _error.typeName());
return false;
}
@ -1086,15 +1087,17 @@ bool CommandLineInterface::assemble(
return false;
}
}
for (auto const& sourceAndStack: assemblyStacks)
{
auto const& stack = sourceAndStack.second;
auto scannerFromSourceName = [&](string const&) -> Scanner const& { return stack.scanner(); };
SourceReferenceFormatter formatter(cerr, scannerFromSourceName);
for (auto const& error: stack.errors())
SourceReferenceFormatter::printExceptionInformation(
cerr,
formatter.printExceptionInformation(
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error",
[&](string const&) -> Scanner const& { return stack.scanner(); }
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
if (!Error::containsOnlyWarnings(stack.errors()))
successful = false;

View File

@ -40,12 +40,12 @@ using namespace dev::solidity;
void dev::julia::test::printErrors(ErrorList const& _errors, Scanner const& _scanner)
{
SourceReferenceFormatter formatter(cout, [&](std::string const&) -> Scanner const& { return _scanner; });
for (auto const& error: _errors)
SourceReferenceFormatter::printExceptionInformation(
cout,
formatter.printExceptionInformation(
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error",
[&](std::string const&) -> Scanner const& { return _scanner; }
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
}

View File

@ -136,8 +136,10 @@ BOOST_AUTO_TEST_CASE(non_overlapping_filtered_costs)
{
BOOST_CHECK_MESSAGE(false, "Source locations should not overlap!");
auto scannerFromSource = [&](string const& _sourceName) -> Scanner const& { return m_compiler.scanner(_sourceName); };
SourceReferenceFormatter::printSourceLocation(cout, &first->first->location(), scannerFromSource);
SourceReferenceFormatter::printSourceLocation(cout, &second->first->location(), scannerFromSource);
SourceReferenceFormatter formatter(cout, scannerFromSource);
formatter.printSourceLocation(&first->first->location());
formatter.printSourceLocation(&second->first->location());
}
}
}

View File

@ -60,12 +60,13 @@ public:
m_compiler.setOptimiserSettings(m_optimize, m_optimizeRuns);
if (!m_compiler.compile())
{
auto scannerFromSourceName = [&](std::string const& _sourceName) -> solidity::Scanner const& { return m_compiler.scanner(_sourceName); };
SourceReferenceFormatter formatter(std::cerr, scannerFromSourceName);
for (auto const& error: m_compiler.errors())
SourceReferenceFormatter::printExceptionInformation(
std::cerr,
formatter.printExceptionInformation(
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error",
[&](std::string const& _sourceName) -> solidity::Scanner const& { return m_compiler.scanner(_sourceName); }
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
BOOST_ERROR("Compiling contract failed");
}