Merge pull request #8753 from a3d4/fix-sourcereferenceformathuman-leftpad

Fix leftpad in SourceReferenceFormatterHuman
This commit is contained in:
chriseth 2020-04-27 11:22:14 +02:00 committed by GitHub
commit 5b92dedeed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 189 additions and 29 deletions

View File

@ -21,7 +21,6 @@
#include <liblangutil/SourceReferenceFormatterHuman.h> #include <liblangutil/SourceReferenceFormatterHuman.h>
#include <liblangutil/Scanner.h> #include <liblangutil/Scanner.h>
#include <liblangutil/Exceptions.h> #include <liblangutil/Exceptions.h>
#include <cmath>
#include <iomanip> #include <iomanip>
using namespace std; using namespace std;
@ -70,58 +69,66 @@ 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)
{ {
m_stream << _ref.sourceName << "\n"; frameColored() << "-->";
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
m_stream << leftpad;
frameColored() << "-->";
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() << '|';
m_stream << '\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() << '|';
m_stream << ' ';
for_each( for_each(
_ref.text.cbegin(), _ref.text.cbegin(),
_ref.text.cbegin() + _ref.startColumn, _ref.text.cbegin() + _ref.startColumn,
[this](char ch) { m_stream << (ch == '\t' ? '\t' : ' '); } [this](char ch) { m_stream << (ch == '\t' ? '\t' : ' '); }
); );
diagColored() << string(locationLength, '^') << '\n'; diagColored() << string(locationLength, '^');
m_stream << '\n';
} }
else else
{ {
// line 1: // line 1:
m_stream << string(leftpad, ' '); m_stream << leftpad << ' ';
frameColored() << " |" << '\n'; frameColored() << '|';
m_stream << '\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, ' ') << " | "; m_stream << leftpad << ' ';
m_stream << string(_ref.startColumn, ' '); frameColored() << '|';
diagColored() << "^ (Relevant source part starts here and spans across multiple lines).\n"; m_stream << ' ' << string(_ref.startColumn, ' ');
diagColored() << "^ (Relevant source part starts here and spans across multiple lines).";
m_stream << '\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; /**/
}
}