Fix leftpad in SourceReferenceFormatterHuman

This commit is contained in:
a3d4 2020-04-23 08:23:01 +02:00
parent 6d98b907ef
commit 8717c073a6
10 changed files with 173 additions and 19 deletions

View File

@ -70,35 +70,36 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _
if (_ref.sourceName.empty()) if (_ref.sourceName.empty())
return; // Nothing we can print here return; // Nothing we can print here
int const leftpad = static_cast<int>(log10(max(_ref.position.line, 1))) + 1;
// line 0: source name
frameColored() << string(leftpad, ' ') << "--> ";
if (_ref.position.line < 0) if (_ref.position.line < 0)
{ {
frameColored() << "--> ";
m_stream << _ref.sourceName << "\n"; m_stream << _ref.sourceName << "\n";
return; // No line available, nothing else to print return; // No line available, nothing else to print
} }
m_stream << _ref.sourceName << ":" << (_ref.position.line + 1) << ":" << (_ref.position.column + 1) << ":" << '\n'; string line = std::to_string(_ref.position.line + 1); // one-based line number as string
string leftpad = string(line.size(), ' ');
// line 0: source name
frameColored() << leftpad << "--> ";
m_stream << _ref.sourceName << ":" << line << ":" << (_ref.position.column + 1) << ":" << '\n';
if (!_ref.multiline) if (!_ref.multiline)
{ {
int const locationLength = _ref.endColumn - _ref.startColumn; int const locationLength = _ref.endColumn - _ref.startColumn;
// line 1: // line 1:
m_stream << string(leftpad, ' '); m_stream << leftpad;
frameColored() << " |" << '\n'; frameColored() << " |" << '\n';
// line 2: // line 2:
frameColored() << (_ref.position.line + 1) << " | "; frameColored() << line << " | ";
m_stream << _ref.text.substr(0, _ref.startColumn); m_stream << _ref.text.substr(0, _ref.startColumn);
highlightColored() << _ref.text.substr(_ref.startColumn, locationLength); highlightColored() << _ref.text.substr(_ref.startColumn, locationLength);
m_stream << _ref.text.substr(_ref.endColumn) << '\n'; m_stream << _ref.text.substr(_ref.endColumn) << '\n';
// line 3: // line 3:
m_stream << string(leftpad, ' '); m_stream << leftpad;
frameColored() << " | "; frameColored() << " | ";
for_each( for_each(
_ref.text.cbegin(), _ref.text.cbegin(),
@ -110,16 +111,16 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _
else else
{ {
// line 1: // line 1:
m_stream << string(leftpad, ' '); m_stream << leftpad;
frameColored() << " |" << '\n'; frameColored() << " |" << '\n';
// line 2: // line 2:
frameColored() << (_ref.position.line + 1) << " | "; frameColored() << line << " | ";
m_stream << _ref.text.substr(0, _ref.startColumn); m_stream << _ref.text.substr(0, _ref.startColumn);
highlightColored() << _ref.text.substr(_ref.startColumn) << '\n'; highlightColored() << _ref.text.substr(_ref.startColumn) << '\n';
// line 3: // line 3:
frameColored() << string(leftpad, ' ') << " | "; frameColored() << leftpad << " | ";
m_stream << string(_ref.startColumn, ' '); m_stream << string(_ref.startColumn, ' ');
diagColored() << "^ (Relevant source part starts here and spans across multiple lines).\n"; diagColored() << "^ (Relevant source part starts here and spans across multiple lines).\n";
} }

View File

@ -0,0 +1,50 @@
Warning: Source file does not specify required compiler version!
--> message_format/input.sol
Warning: Unused local variable.
--> message_format/input.sol:9:27:
|
9 | function f() public { int x; }
| ^^^^^
Warning: Unused local variable.
--> message_format/input.sol:10:27:
|
10 | function g() public { int x; }
| ^^^^^
Warning: Unused local variable.
--> message_format/input.sol:99:14:
|
99 | /**/ int a; /**/
| ^^^^^
Warning: Unused local variable.
--> message_format/input.sol:100:14:
|
100 | /**/ int b; /**/
| ^^^^^
Warning: Unused local variable.
--> message_format/input.sol:101:14:
|
101 | /**/ int c; /**/
| ^^^^^
Warning: Function state mutability can be restricted to pure
--> message_format/input.sol:9:5:
|
9 | function f() public { int x; }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning: Function state mutability can be restricted to pure
--> message_format/input.sol:10:5:
|
10 | function g() public { int x; }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning: Function state mutability can be restricted to pure
--> message_format/input.sol:11:5:
|
11 | function h() public {
| ^ (Relevant source part starts here and spans across multiple lines).

View File

@ -0,0 +1,103 @@
// checks that error messages around power-or-10 lines are formatted correctly
contract C {
function f() public { int x; }
function g() public { int x; }
function h() public {
/**/ int a; /**/
/**/ int b; /**/
/**/ int c; /**/
}
}

View File

@ -1,5 +1,5 @@
Warning: Source file does not specify required compiler version! Warning: Source file does not specify required compiler version!
--> too_long_line/input.sol --> too_long_line/input.sol
Error: Identifier not found or not unique. Error: Identifier not found or not unique.
--> too_long_line/input.sol:2:164: --> too_long_line/input.sol:2:164:

View File

@ -1,5 +1,5 @@
Warning: Source file does not specify required compiler version! Warning: Source file does not specify required compiler version!
--> too_long_line_both_sides_short/input.sol --> too_long_line_both_sides_short/input.sol
Error: Identifier not found or not unique. Error: Identifier not found or not unique.
--> too_long_line_both_sides_short/input.sol:2:15: --> too_long_line_both_sides_short/input.sol:2:15:

View File

@ -1,5 +1,5 @@
Warning: Source file does not specify required compiler version! Warning: Source file does not specify required compiler version!
--> too_long_line_edge_in/input.sol --> too_long_line_edge_in/input.sol
Error: Identifier not found or not unique. Error: Identifier not found or not unique.
--> too_long_line_edge_in/input.sol:2:36: --> too_long_line_edge_in/input.sol:2:36:

View File

@ -1,5 +1,5 @@
Warning: Source file does not specify required compiler version! Warning: Source file does not specify required compiler version!
--> too_long_line_edge_out/input.sol --> too_long_line_edge_out/input.sol
Error: Identifier not found or not unique. Error: Identifier not found or not unique.
--> too_long_line_edge_out/input.sol:2:37: --> too_long_line_edge_out/input.sol:2:37:

View File

@ -1,5 +1,5 @@
Warning: Source file does not specify required compiler version! Warning: Source file does not specify required compiler version!
--> too_long_line_left_short/input.sol --> too_long_line_left_short/input.sol
Error: Identifier not found or not unique. Error: Identifier not found or not unique.
--> too_long_line_left_short/input.sol:2:15: --> too_long_line_left_short/input.sol:2:15:

View File

@ -5,4 +5,4 @@ Error: No visibility specified. Did you intend to add "public"?
| ^ (Relevant source part starts here and spans across multiple lines). | ^ (Relevant source part starts here and spans across multiple lines).
Warning: Source file does not specify required compiler version! Warning: Source file does not specify required compiler version!
--> too_long_line_multiline/input.sol --> too_long_line_multiline/input.sol

View File

@ -1,5 +1,5 @@
Warning: Source file does not specify required compiler version! Warning: Source file does not specify required compiler version!
--> too_long_line_right_short/input.sol --> too_long_line_right_short/input.sol
Error: Identifier not found or not unique. Error: Identifier not found or not unique.
--> too_long_line_right_short/input.sol:2:164: --> too_long_line_right_short/input.sol:2:164: