mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #470 from chriseth/redundancy
Remove code duplication in source references formatter.
This commit is contained in:
commit
c492d9be00
@ -33,19 +33,22 @@ namespace solidity
|
|||||||
|
|
||||||
void SourceReferenceFormatter::printSourceLocation(
|
void SourceReferenceFormatter::printSourceLocation(
|
||||||
ostream& _stream,
|
ostream& _stream,
|
||||||
SourceLocation const& _location,
|
SourceLocation const* _location,
|
||||||
Scanner const& _scanner
|
function<Scanner const&(string const&)> const& _scannerFromSourceName
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
if (!_location || !_location->sourceName)
|
||||||
|
return; // Nothing we can print here
|
||||||
|
auto const& scanner = _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);
|
||||||
int endLine;
|
int endLine;
|
||||||
int endColumn;
|
int endColumn;
|
||||||
tie(endLine, endColumn) = _scanner.translatePositionToLineColumn(_location.end);
|
tie(endLine, endColumn) = scanner.translatePositionToLineColumn(_location->end);
|
||||||
if (startLine == endLine)
|
if (startLine == endLine)
|
||||||
{
|
{
|
||||||
string line = _scanner.lineAtPosition(_location.start);
|
string line = scanner.lineAtPosition(_location->start);
|
||||||
_stream << line << endl;
|
_stream << line << endl;
|
||||||
for_each(
|
for_each(
|
||||||
line.cbegin(),
|
line.cbegin(),
|
||||||
@ -61,7 +64,7 @@ void SourceReferenceFormatter::printSourceLocation(
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
_stream <<
|
_stream <<
|
||||||
_scanner.lineAtPosition(_location.start) <<
|
scanner.lineAtPosition(_location->start) <<
|
||||||
endl <<
|
endl <<
|
||||||
string(startColumn, ' ') <<
|
string(startColumn, ' ') <<
|
||||||
"^\n" <<
|
"^\n" <<
|
||||||
@ -70,14 +73,17 @@ void SourceReferenceFormatter::printSourceLocation(
|
|||||||
|
|
||||||
void SourceReferenceFormatter::printSourceName(
|
void SourceReferenceFormatter::printSourceName(
|
||||||
ostream& _stream,
|
ostream& _stream,
|
||||||
SourceLocation const& _location,
|
SourceLocation const* _location,
|
||||||
Scanner const& _scanner
|
function<Scanner const&(string const&)> const& _scannerFromSourceName
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
if (!_location || !_location->sourceName)
|
||||||
|
return; // Nothing we can print here
|
||||||
|
auto const& scanner = _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) << ": ";
|
_stream << *_location->sourceName << ":" << (startLine + 1) << ":" << (startColumn + 1) << ": ";
|
||||||
}
|
}
|
||||||
|
|
||||||
void SourceReferenceFormatter::printExceptionInformation(
|
void SourceReferenceFormatter::printExceptionInformation(
|
||||||
@ -89,38 +95,23 @@ void SourceReferenceFormatter::printExceptionInformation(
|
|||||||
{
|
{
|
||||||
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);
|
||||||
Scanner const* scannerPtr = nullptr;
|
|
||||||
|
|
||||||
if (location && location->sourceName)
|
printSourceName(_stream, location, _scannerFromSourceName);
|
||||||
{
|
|
||||||
scannerPtr = &_scannerFromSourceName(*location->sourceName);
|
|
||||||
printSourceName(_stream, *location, *scannerPtr);
|
|
||||||
}
|
|
||||||
|
|
||||||
_stream << _name;
|
_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;
|
_stream << ": " << *description << endl;
|
||||||
|
|
||||||
if (location && location->sourceName)
|
printSourceLocation(_stream, location, _scannerFromSourceName);
|
||||||
{
|
|
||||||
scannerPtr = &_scannerFromSourceName(*location->sourceName);
|
|
||||||
printSourceLocation(_stream, *location, *scannerPtr);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (secondarylocation && !secondarylocation->infos.empty())
|
if (secondarylocation && !secondarylocation->infos.empty())
|
||||||
{
|
{
|
||||||
for (auto info: secondarylocation->infos)
|
for (auto info: secondarylocation->infos)
|
||||||
{
|
{
|
||||||
_stream << info.first << " ";
|
_stream << info.first << " ";
|
||||||
if (!info.second.sourceName)
|
printSourceName(_stream, &info.second, _scannerFromSourceName);
|
||||||
{
|
|
||||||
_stream << endl;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
scannerPtr = &_scannerFromSourceName(*info.second.sourceName);
|
|
||||||
printSourceName(_stream, info.second, *scannerPtr);
|
|
||||||
_stream << endl;
|
_stream << endl;
|
||||||
printSourceLocation(_stream, info.second, *scannerPtr);
|
printSourceLocation(_stream, &info.second, _scannerFromSourceName);
|
||||||
}
|
}
|
||||||
_stream << endl;
|
_stream << endl;
|
||||||
}
|
}
|
||||||
|
@ -40,15 +40,26 @@ class CompilerStack; // forward
|
|||||||
struct SourceReferenceFormatter
|
struct SourceReferenceFormatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void printSourceLocation(std::ostream& _stream, SourceLocation const& _location, Scanner const& _scanner);
|
using ScannerFromSourceNameFun = std::function<Scanner const&(std::string const&)>;
|
||||||
|
/// Prints source location if it is given.
|
||||||
|
static void printSourceLocation(
|
||||||
|
std::ostream& _stream,
|
||||||
|
SourceLocation const* _location,
|
||||||
|
ScannerFromSourceNameFun const& _scannerFromSourceName
|
||||||
|
);
|
||||||
static void printExceptionInformation(
|
static void printExceptionInformation(
|
||||||
std::ostream& _stream,
|
std::ostream& _stream,
|
||||||
Exception const& _exception,
|
Exception const& _exception,
|
||||||
std::string const& _name,
|
std::string const& _name,
|
||||||
std::function<Scanner const&(std::string const&)> const& _scannerFromSourceName
|
ScannerFromSourceNameFun const& _scannerFromSourceName
|
||||||
);
|
);
|
||||||
private:
|
private:
|
||||||
static void printSourceName(std::ostream& _stream, SourceLocation const& _location, Scanner const& _scanner);
|
/// Prints source name if location is given.
|
||||||
|
static void printSourceName(
|
||||||
|
std::ostream& _stream,
|
||||||
|
SourceLocation const* _location,
|
||||||
|
ScannerFromSourceNameFun const& _scannerFromSourceName
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -120,8 +120,9 @@ BOOST_AUTO_TEST_CASE(non_overlapping_filtered_costs)
|
|||||||
if (first->first->location().intersects(second->first->location()))
|
if (first->first->location().intersects(second->first->location()))
|
||||||
{
|
{
|
||||||
BOOST_CHECK_MESSAGE(false, "Source locations should not overlap!");
|
BOOST_CHECK_MESSAGE(false, "Source locations should not overlap!");
|
||||||
SourceReferenceFormatter::printSourceLocation(cout, first->first->location(), m_compiler.scanner());
|
auto scannerFromSource = [&](string const&) -> Scanner const& { return m_compiler.scanner(); };
|
||||||
SourceReferenceFormatter::printSourceLocation(cout, second->first->location(), m_compiler.scanner());
|
SourceReferenceFormatter::printSourceLocation(cout, &first->first->location(), scannerFromSource);
|
||||||
|
SourceReferenceFormatter::printSourceLocation(cout, &second->first->location(), scannerFromSource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user