Convert static SourceReferenceFormatter functions to member ones

This commit is contained in:
Federico Bond 2017-10-26 17:56:00 -03:00 committed by chriseth
parent 3f7e82d00b
commit d7532f7b9c
6 changed files with 69 additions and 70 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -136,8 +136,10 @@ BOOST_AUTO_TEST_CASE(non_overlapping_filtered_costs)
{ {
BOOST_CHECK_MESSAGE(false, "Source locations should not overlap!"); BOOST_CHECK_MESSAGE(false, "Source locations should not overlap!");
auto scannerFromSource = [&](string const& _sourceName) -> Scanner const& { return m_compiler.scanner(_sourceName); }; auto scannerFromSource = [&](string const& _sourceName) -> Scanner const& { return m_compiler.scanner(_sourceName); };
SourceReferenceFormatter::printSourceLocation(cout, &first->first->location(), scannerFromSource); SourceReferenceFormatter formatter(cout, scannerFromSource);
SourceReferenceFormatter::printSourceLocation(cout, &second->first->location(), 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); m_compiler.setOptimiserSettings(m_optimize, m_optimizeRuns);
if (!m_compiler.compile()) 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()) for (auto const& error: m_compiler.errors())
SourceReferenceFormatter::printExceptionInformation( formatter.printExceptionInformation(
std::cerr,
*error, *error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error", (error->type() == Error::Type::Warning) ? "Warning" : "Error"
[&](std::string const& _sourceName) -> solidity::Scanner const& { return m_compiler.scanner(_sourceName); }
); );
BOOST_ERROR("Compiling contract failed"); BOOST_ERROR("Compiling contract failed");
} }