aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/interface
diff options
context:
space:
mode:
authorchriseth <c@ethdev.com>2016-04-01 06:54:07 +0800
committerchriseth <c@ethdev.com>2016-04-01 06:54:12 +0800
commitea7325d2d2716703d458f520e3cc48a421730094 (patch)
tree429d6844def9c560e610d30a0ab69920cf5ac466 /libsolidity/interface
parentc67926cf2be95e9275eb2b7fe904131e7e8842ad (diff)
downloaddexon-solidity-ea7325d2d2716703d458f520e3cc48a421730094.tar
dexon-solidity-ea7325d2d2716703d458f520e3cc48a421730094.tar.gz
dexon-solidity-ea7325d2d2716703d458f520e3cc48a421730094.tar.bz2
dexon-solidity-ea7325d2d2716703d458f520e3cc48a421730094.tar.lz
dexon-solidity-ea7325d2d2716703d458f520e3cc48a421730094.tar.xz
dexon-solidity-ea7325d2d2716703d458f520e3cc48a421730094.tar.zst
dexon-solidity-ea7325d2d2716703d458f520e3cc48a421730094.zip
Remove code duplication in source references formatter.
Diffstat (limited to 'libsolidity/interface')
-rw-r--r--libsolidity/interface/SourceReferenceFormatter.cpp49
-rw-r--r--libsolidity/interface/SourceReferenceFormatter.h17
2 files changed, 34 insertions, 32 deletions
diff --git a/libsolidity/interface/SourceReferenceFormatter.cpp b/libsolidity/interface/SourceReferenceFormatter.cpp
index 7b91dd2c..f09d2d45 100644
--- a/libsolidity/interface/SourceReferenceFormatter.cpp
+++ b/libsolidity/interface/SourceReferenceFormatter.cpp
@@ -33,19 +33,22 @@ namespace solidity
void SourceReferenceFormatter::printSourceLocation(
ostream& _stream,
- SourceLocation const& _location,
- Scanner const& _scanner
+ SourceLocation const* _location,
+ 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 startColumn;
- tie(startLine, startColumn) = _scanner.translatePositionToLineColumn(_location.start);
+ tie(startLine, startColumn) = scanner.translatePositionToLineColumn(_location->start);
int endLine;
int endColumn;
- tie(endLine, endColumn) = _scanner.translatePositionToLineColumn(_location.end);
+ tie(endLine, endColumn) = scanner.translatePositionToLineColumn(_location->end);
if (startLine == endLine)
{
- string line = _scanner.lineAtPosition(_location.start);
+ string line = scanner.lineAtPosition(_location->start);
_stream << line << endl;
for_each(
line.cbegin(),
@@ -61,7 +64,7 @@ void SourceReferenceFormatter::printSourceLocation(
}
else
_stream <<
- _scanner.lineAtPosition(_location.start) <<
+ scanner.lineAtPosition(_location->start) <<
endl <<
string(startColumn, ' ') <<
"^\n" <<
@@ -70,14 +73,17 @@ void SourceReferenceFormatter::printSourceLocation(
void SourceReferenceFormatter::printSourceName(
ostream& _stream,
- SourceLocation const& _location,
- Scanner const& _scanner
+ SourceLocation const* _location,
+ 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 startColumn;
- tie(startLine, startColumn) = _scanner.translatePositionToLineColumn(_location.start);
- _stream << *_location.sourceName << ":" << (startLine + 1) << ":" << (startColumn + 1) << ": ";
+ tie(startLine, startColumn) = scanner.translatePositionToLineColumn(_location->start);
+ _stream << *_location->sourceName << ":" << (startLine + 1) << ":" << (startColumn + 1) << ": ";
}
void SourceReferenceFormatter::printExceptionInformation(
@@ -89,38 +95,23 @@ void SourceReferenceFormatter::printExceptionInformation(
{
SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
auto secondarylocation = boost::get_error_info<errinfo_secondarySourceLocation>(_exception);
- Scanner const* scannerPtr = nullptr;
- if (location && location->sourceName)
- {
- scannerPtr = &_scannerFromSourceName(*location->sourceName);
- printSourceName(_stream, *location, *scannerPtr);
- }
+ printSourceName(_stream, location, _scannerFromSourceName);
_stream << _name;
if (string const* description = boost::get_error_info<errinfo_comment>(_exception))
_stream << ": " << *description << endl;
- if (location && location->sourceName)
- {
- scannerPtr = &_scannerFromSourceName(*location->sourceName);
- printSourceLocation(_stream, *location, *scannerPtr);
- }
+ printSourceLocation(_stream, location, _scannerFromSourceName);
if (secondarylocation && !secondarylocation->infos.empty())
{
for (auto info: secondarylocation->infos)
{
_stream << info.first << " ";
- if (!info.second.sourceName)
- {
- _stream << endl;
- continue;
- }
- scannerPtr = &_scannerFromSourceName(*info.second.sourceName);
- printSourceName(_stream, info.second, *scannerPtr);
+ printSourceName(_stream, &info.second, _scannerFromSourceName);
_stream << endl;
- printSourceLocation(_stream, info.second, *scannerPtr);
+ printSourceLocation(_stream, &info.second, _scannerFromSourceName);
}
_stream << endl;
}
diff --git a/libsolidity/interface/SourceReferenceFormatter.h b/libsolidity/interface/SourceReferenceFormatter.h
index 1fc42753..2b908c46 100644
--- a/libsolidity/interface/SourceReferenceFormatter.h
+++ b/libsolidity/interface/SourceReferenceFormatter.h
@@ -40,15 +40,26 @@ class CompilerStack; // forward
struct SourceReferenceFormatter
{
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(
std::ostream& _stream,
Exception const& _exception,
std::string const& _name,
- std::function<Scanner const&(std::string const&)> const& _scannerFromSourceName
+ ScannerFromSourceNameFun const& _scannerFromSourceName
);
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
+ );
};
}