2018-11-26 23:21:53 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Formatting functions for errors referencing positions and locations in the source.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <liblangutil/SourceReferenceFormatterHuman.h>
|
|
|
|
#include <liblangutil/Scanner.h>
|
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::util::formatting;
|
2018-11-26 23:21:53 +00:00
|
|
|
|
|
|
|
AnsiColorized SourceReferenceFormatterHuman::normalColored() const
|
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {WHITE});
|
|
|
|
}
|
|
|
|
|
|
|
|
AnsiColorized SourceReferenceFormatterHuman::frameColored() const
|
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {BOLD, BLUE});
|
|
|
|
}
|
|
|
|
|
|
|
|
AnsiColorized SourceReferenceFormatterHuman::errorColored() const
|
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {BOLD, RED});
|
|
|
|
}
|
|
|
|
|
|
|
|
AnsiColorized SourceReferenceFormatterHuman::messageColored() const
|
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {BOLD, WHITE});
|
|
|
|
}
|
|
|
|
|
|
|
|
AnsiColorized SourceReferenceFormatterHuman::secondaryColored() const
|
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {BOLD, CYAN});
|
|
|
|
}
|
|
|
|
|
|
|
|
AnsiColorized SourceReferenceFormatterHuman::highlightColored() const
|
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {YELLOW});
|
|
|
|
}
|
|
|
|
|
|
|
|
AnsiColorized SourceReferenceFormatterHuman::diagColored() const
|
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {BOLD, YELLOW});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _ref)
|
|
|
|
{
|
2020-02-07 01:36:51 +00:00
|
|
|
if (_ref.sourceName.empty())
|
2018-11-26 23:21:53 +00:00
|
|
|
return; // Nothing we can print here
|
|
|
|
|
2020-02-07 01:36:51 +00:00
|
|
|
if (_ref.position.line < 0)
|
|
|
|
{
|
2020-04-23 22:51:15 +00:00
|
|
|
frameColored() << "-->";
|
|
|
|
m_stream << ' ' << _ref.sourceName << '\n';
|
2020-02-07 01:36:51 +00:00
|
|
|
return; // No line available, nothing else to print
|
|
|
|
}
|
|
|
|
|
2020-04-23 06:23:01 +00:00
|
|
|
string line = std::to_string(_ref.position.line + 1); // one-based line number as string
|
|
|
|
string leftpad = string(line.size(), ' ');
|
|
|
|
|
|
|
|
// line 0: source name
|
2020-04-23 22:51:15 +00:00
|
|
|
m_stream << leftpad;
|
|
|
|
frameColored() << "-->";
|
|
|
|
m_stream << ' ' << _ref.sourceName << ':' << line << ':' << (_ref.position.column + 1) << ":\n";
|
2018-11-26 23:21:53 +00:00
|
|
|
|
|
|
|
if (!_ref.multiline)
|
|
|
|
{
|
|
|
|
int const locationLength = _ref.endColumn - _ref.startColumn;
|
|
|
|
|
|
|
|
// line 1:
|
2020-04-23 22:51:15 +00:00
|
|
|
m_stream << leftpad << ' ';
|
|
|
|
frameColored() << '|';
|
|
|
|
m_stream << '\n';
|
2018-11-26 23:21:53 +00:00
|
|
|
|
|
|
|
// line 2:
|
2020-04-23 22:51:15 +00:00
|
|
|
frameColored() << line << " |";
|
|
|
|
m_stream << ' ' << _ref.text.substr(0, _ref.startColumn);
|
2018-11-26 23:21:53 +00:00
|
|
|
highlightColored() << _ref.text.substr(_ref.startColumn, locationLength);
|
|
|
|
m_stream << _ref.text.substr(_ref.endColumn) << '\n';
|
|
|
|
|
|
|
|
// line 3:
|
2020-04-23 22:51:15 +00:00
|
|
|
m_stream << leftpad << ' ';
|
|
|
|
frameColored() << '|';
|
|
|
|
m_stream << ' ';
|
2018-11-26 23:21:53 +00:00
|
|
|
for_each(
|
|
|
|
_ref.text.cbegin(),
|
|
|
|
_ref.text.cbegin() + _ref.startColumn,
|
|
|
|
[this](char ch) { m_stream << (ch == '\t' ? '\t' : ' '); }
|
|
|
|
);
|
2020-04-23 22:51:15 +00:00
|
|
|
diagColored() << string(locationLength, '^');
|
|
|
|
m_stream << '\n';
|
2018-11-26 23:21:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// line 1:
|
2020-04-23 22:51:15 +00:00
|
|
|
m_stream << leftpad << ' ';
|
|
|
|
frameColored() << '|';
|
|
|
|
m_stream << '\n';
|
2018-11-26 23:21:53 +00:00
|
|
|
|
|
|
|
// line 2:
|
2020-04-23 22:51:15 +00:00
|
|
|
frameColored() << line << " |";
|
|
|
|
m_stream << ' ' << _ref.text.substr(0, _ref.startColumn);
|
2018-11-26 23:21:53 +00:00
|
|
|
highlightColored() << _ref.text.substr(_ref.startColumn) << '\n';
|
|
|
|
|
|
|
|
// line 3:
|
2020-04-23 22:51:15 +00:00
|
|
|
m_stream << leftpad << ' ';
|
|
|
|
frameColored() << '|';
|
|
|
|
m_stream << ' ' << string(_ref.startColumn, ' ');
|
|
|
|
diagColored() << "^ (Relevant source part starts here and spans across multiple lines).";
|
|
|
|
m_stream << '\n';
|
2018-11-26 23:21:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceReferenceFormatterHuman::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)
|
|
|
|
{
|
|
|
|
// exception header line
|
|
|
|
errorColored() << _msg.category;
|
|
|
|
messageColored() << ": " << _msg.primary.message << '\n';
|
|
|
|
|
|
|
|
printSourceLocation(_msg.primary);
|
|
|
|
|
|
|
|
for (auto const& secondary: _msg.secondary)
|
|
|
|
{
|
|
|
|
secondaryColored() << "Note";
|
|
|
|
messageColored() << ": " << secondary.message << '\n';
|
|
|
|
printSourceLocation(secondary);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_stream << '\n';
|
|
|
|
}
|