From 8717c073a67d1a7f44cb61a78df15e6be5de1927 Mon Sep 17 00:00:00 2001 From: a3d4 Date: Thu, 23 Apr 2020 08:23:01 +0200 Subject: [PATCH] Fix leftpad in SourceReferenceFormatterHuman --- liblangutil/SourceReferenceFormatterHuman.cpp | 25 +++-- test/cmdlineTests/message_format/err | 50 +++++++++ test/cmdlineTests/message_format/input.sol | 103 ++++++++++++++++++ test/cmdlineTests/too_long_line/err | 2 +- .../too_long_line_both_sides_short/err | 2 +- test/cmdlineTests/too_long_line_edge_in/err | 2 +- test/cmdlineTests/too_long_line_edge_out/err | 2 +- .../cmdlineTests/too_long_line_left_short/err | 2 +- test/cmdlineTests/too_long_line_multiline/err | 2 +- .../too_long_line_right_short/err | 2 +- 10 files changed, 173 insertions(+), 19 deletions(-) create mode 100644 test/cmdlineTests/message_format/err create mode 100644 test/cmdlineTests/message_format/input.sol diff --git a/liblangutil/SourceReferenceFormatterHuman.cpp b/liblangutil/SourceReferenceFormatterHuman.cpp index 669fa2007..430883996 100644 --- a/liblangutil/SourceReferenceFormatterHuman.cpp +++ b/liblangutil/SourceReferenceFormatterHuman.cpp @@ -70,35 +70,36 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _ if (_ref.sourceName.empty()) return; // Nothing we can print here - int const leftpad = static_cast(log10(max(_ref.position.line, 1))) + 1; - - // line 0: source name - frameColored() << string(leftpad, ' ') << "--> "; - if (_ref.position.line < 0) { + frameColored() << "--> "; m_stream << _ref.sourceName << "\n"; 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) { int const locationLength = _ref.endColumn - _ref.startColumn; // line 1: - m_stream << string(leftpad, ' '); + m_stream << leftpad; frameColored() << " |" << '\n'; // line 2: - frameColored() << (_ref.position.line + 1) << " | "; + frameColored() << line << " | "; m_stream << _ref.text.substr(0, _ref.startColumn); highlightColored() << _ref.text.substr(_ref.startColumn, locationLength); m_stream << _ref.text.substr(_ref.endColumn) << '\n'; // line 3: - m_stream << string(leftpad, ' '); + m_stream << leftpad; frameColored() << " | "; for_each( _ref.text.cbegin(), @@ -110,16 +111,16 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _ else { // line 1: - m_stream << string(leftpad, ' '); + m_stream << leftpad; frameColored() << " |" << '\n'; // line 2: - frameColored() << (_ref.position.line + 1) << " | "; + frameColored() << line << " | "; m_stream << _ref.text.substr(0, _ref.startColumn); highlightColored() << _ref.text.substr(_ref.startColumn) << '\n'; // line 3: - frameColored() << string(leftpad, ' ') << " | "; + frameColored() << leftpad << " | "; m_stream << string(_ref.startColumn, ' '); diagColored() << "^ (Relevant source part starts here and spans across multiple lines).\n"; } diff --git a/test/cmdlineTests/message_format/err b/test/cmdlineTests/message_format/err new file mode 100644 index 000000000..eee8267e2 --- /dev/null +++ b/test/cmdlineTests/message_format/err @@ -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). diff --git a/test/cmdlineTests/message_format/input.sol b/test/cmdlineTests/message_format/input.sol new file mode 100644 index 000000000..6f854f96a --- /dev/null +++ b/test/cmdlineTests/message_format/input.sol @@ -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; /**/ + } +} diff --git a/test/cmdlineTests/too_long_line/err b/test/cmdlineTests/too_long_line/err index 87612fe59..6e5119037 100644 --- a/test/cmdlineTests/too_long_line/err +++ b/test/cmdlineTests/too_long_line/err @@ -1,5 +1,5 @@ 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. --> too_long_line/input.sol:2:164: diff --git a/test/cmdlineTests/too_long_line_both_sides_short/err b/test/cmdlineTests/too_long_line_both_sides_short/err index 81f44930f..34bd25d28 100644 --- a/test/cmdlineTests/too_long_line_both_sides_short/err +++ b/test/cmdlineTests/too_long_line_both_sides_short/err @@ -1,5 +1,5 @@ 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. --> too_long_line_both_sides_short/input.sol:2:15: diff --git a/test/cmdlineTests/too_long_line_edge_in/err b/test/cmdlineTests/too_long_line_edge_in/err index b00bec7a1..2988bc44e 100644 --- a/test/cmdlineTests/too_long_line_edge_in/err +++ b/test/cmdlineTests/too_long_line_edge_in/err @@ -1,5 +1,5 @@ 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. --> too_long_line_edge_in/input.sol:2:36: diff --git a/test/cmdlineTests/too_long_line_edge_out/err b/test/cmdlineTests/too_long_line_edge_out/err index 3de913b7d..a85faa44f 100644 --- a/test/cmdlineTests/too_long_line_edge_out/err +++ b/test/cmdlineTests/too_long_line_edge_out/err @@ -1,5 +1,5 @@ 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. --> too_long_line_edge_out/input.sol:2:37: diff --git a/test/cmdlineTests/too_long_line_left_short/err b/test/cmdlineTests/too_long_line_left_short/err index a6ddfca81..aa36b1068 100644 --- a/test/cmdlineTests/too_long_line_left_short/err +++ b/test/cmdlineTests/too_long_line_left_short/err @@ -1,5 +1,5 @@ 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. --> too_long_line_left_short/input.sol:2:15: diff --git a/test/cmdlineTests/too_long_line_multiline/err b/test/cmdlineTests/too_long_line_multiline/err index 5a2655174..2aa801623 100644 --- a/test/cmdlineTests/too_long_line_multiline/err +++ b/test/cmdlineTests/too_long_line_multiline/err @@ -5,4 +5,4 @@ Error: No visibility specified. Did you intend to add "public"? | ^ (Relevant source part starts here and spans across multiple lines). Warning: Source file does not specify required compiler version! - --> too_long_line_multiline/input.sol +--> too_long_line_multiline/input.sol diff --git a/test/cmdlineTests/too_long_line_right_short/err b/test/cmdlineTests/too_long_line_right_short/err index f5801e616..d2d8f3980 100644 --- a/test/cmdlineTests/too_long_line_right_short/err +++ b/test/cmdlineTests/too_long_line_right_short/err @@ -1,5 +1,5 @@ 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. --> too_long_line_right_short/input.sol:2:164: