Skip to content

Commit

Permalink
Convert static SourceReferenceFormatter functions to member ones
Browse files Browse the repository at this point in the history
  • Loading branch information
federicobond authored and chriseth committed Feb 19, 2018
1 parent 3f7e82d commit d7532f7
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 69 deletions.
55 changes: 23 additions & 32 deletions libsolidity/interface/SourceReferenceFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -64,72 +60,67 @@ 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, ' ') <<
"^\n" <<
"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;
}
}

Expand Down
36 changes: 19 additions & 17 deletions libsolidity/interface/SourceReferenceFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,41 @@ namespace solidity
class Scanner; // forward
class CompilerStack; // forward

struct SourceReferenceFormatter
class SourceReferenceFormatter
{
public:
using ScannerFromSourceNameFun = std::function<Scanner const&(std::string const&)>;
/// Prints source location if it is given.
static void printSourceLocation(

explicit SourceReferenceFormatter(
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
);
):
m_stream(_stream),
m_scannerFromSourceName(_scannerFromSourceName)
{}

/// Prints source location if it is given.
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,
ScannerFromSourceNameFun const& _scannerFromSourceName
)
{
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 const& m_scannerFromSourceName;
};

}
Expand Down
23 changes: 13 additions & 10 deletions solc/CommandLineInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -796,19 +799,17 @@ 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)
return false;
}
catch (CompilerError const& _exception)
{
SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Compiler error", scannerFromSourceName);
formatter.printExceptionInformation(_exception, "Compiler error");
return false;
}
catch (InternalCompilerError const& _exception)
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions test/libjulia/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}

Expand Down
6 changes: 4 additions & 2 deletions test/libsolidity/GasMeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions test/libsolidity/SolidityExecutionFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ class SolidityExecutionFramework: public dev::test::ExecutionFramework
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");
}
Expand Down

0 comments on commit d7532f7

Please sign in to comment.