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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-11-26 23:21:53 +00:00
|
|
|
/**
|
|
|
|
* Formatting functions for errors referencing positions and locations in the source.
|
|
|
|
*/
|
|
|
|
|
2020-12-01 13:22:15 +00:00
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
2018-11-26 23:21:53 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
2021-08-03 13:20:00 +00:00
|
|
|
#include <liblangutil/CharStream.h>
|
|
|
|
#include <liblangutil/CharStreamProvider.h>
|
2020-05-25 10:22:11 +00:00
|
|
|
#include <libsolutil/UTF8.h>
|
2018-11-26 23:21:53 +00:00
|
|
|
#include <iomanip>
|
2020-05-31 03:08:37 +00:00
|
|
|
#include <string_view>
|
2022-09-06 10:12:15 +00:00
|
|
|
#include <variant>
|
2018-11-26 23:21:53 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-05-31 03:08:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string replaceNonTabs(std::string_view _utf8Input, char _filler)
|
|
|
|
{
|
|
|
|
std::string output;
|
|
|
|
for (char const c: _utf8Input)
|
|
|
|
if ((c & 0xc0) != 0x80)
|
|
|
|
output.push_back(c == '\t' ? '\t' : _filler);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-03 13:20:00 +00:00
|
|
|
std::string SourceReferenceFormatter::formatErrorInformation(Error const& _error, CharStream const& _charStream)
|
|
|
|
{
|
|
|
|
return formatErrorInformation(
|
|
|
|
_error,
|
|
|
|
SingletonCharStreamProvider(_charStream)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:22:15 +00:00
|
|
|
AnsiColorized SourceReferenceFormatter::normalColored() const
|
2018-11-26 23:21:53 +00:00
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {WHITE});
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:22:15 +00:00
|
|
|
AnsiColorized SourceReferenceFormatter::frameColored() const
|
2018-11-26 23:21:53 +00:00
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {BOLD, BLUE});
|
|
|
|
}
|
|
|
|
|
2022-06-17 03:25:52 +00:00
|
|
|
AnsiColorized SourceReferenceFormatter::errorColored(Error::Severity _severity) const
|
2018-11-26 23:21:53 +00:00
|
|
|
{
|
2021-09-24 04:25:20 +00:00
|
|
|
// We used to color messages of any severity as errors so this seems like a good default
|
|
|
|
// for cases where severity cannot be determined.
|
|
|
|
char const* textColor = RED;
|
|
|
|
|
2022-06-17 03:25:52 +00:00
|
|
|
switch (_severity)
|
|
|
|
{
|
|
|
|
case Error::Severity::Error: textColor = RED; break;
|
|
|
|
case Error::Severity::Warning: textColor = YELLOW; break;
|
|
|
|
case Error::Severity::Info: textColor = WHITE; break;
|
|
|
|
}
|
2021-09-24 04:25:20 +00:00
|
|
|
|
|
|
|
return AnsiColorized(m_stream, m_colored, {BOLD, textColor});
|
2018-11-26 23:21:53 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:22:15 +00:00
|
|
|
AnsiColorized SourceReferenceFormatter::messageColored() const
|
2018-11-26 23:21:53 +00:00
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {BOLD, WHITE});
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:22:15 +00:00
|
|
|
AnsiColorized SourceReferenceFormatter::secondaryColored() const
|
2018-11-26 23:21:53 +00:00
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {BOLD, CYAN});
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:22:15 +00:00
|
|
|
AnsiColorized SourceReferenceFormatter::highlightColored() const
|
2018-11-26 23:21:53 +00:00
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {YELLOW});
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:22:15 +00:00
|
|
|
AnsiColorized SourceReferenceFormatter::diagColored() const
|
2018-11-26 23:21:53 +00:00
|
|
|
{
|
|
|
|
return AnsiColorized(m_stream, m_colored, {BOLD, YELLOW});
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:22:15 +00:00
|
|
|
void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref)
|
2018-11-26 23:21:53 +00:00
|
|
|
{
|
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
|
|
|
|
2020-05-31 03:08:37 +00:00
|
|
|
string_view text = _ref.text;
|
|
|
|
|
2022-07-04 22:01:30 +00:00
|
|
|
if (m_charStreamProvider.charStream(_ref.sourceName).isImportedFromAST())
|
|
|
|
return;
|
|
|
|
|
2018-11-26 23:21:53 +00:00
|
|
|
if (!_ref.multiline)
|
|
|
|
{
|
2020-09-30 20:12:02 +00:00
|
|
|
size_t const locationLength = static_cast<size_t>(_ref.endColumn - _ref.startColumn);
|
2018-11-26 23:21:53 +00:00
|
|
|
|
|
|
|
// 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 << " |";
|
2020-06-02 13:45:03 +00:00
|
|
|
|
|
|
|
m_stream << ' ' << text.substr(0, static_cast<size_t>(_ref.startColumn));
|
|
|
|
highlightColored() << text.substr(static_cast<size_t>(_ref.startColumn), locationLength);
|
|
|
|
m_stream << text.substr(static_cast<size_t>(_ref.endColumn)) << '\n';
|
2018-11-26 23:21:53 +00:00
|
|
|
|
|
|
|
// line 3:
|
2020-04-23 22:51:15 +00:00
|
|
|
m_stream << leftpad << ' ';
|
|
|
|
frameColored() << '|';
|
2020-06-02 13:45:03 +00:00
|
|
|
|
|
|
|
m_stream << ' ' << replaceNonTabs(text.substr(0, static_cast<size_t>(_ref.startColumn)), ' ');
|
2020-09-30 20:12:02 +00:00
|
|
|
diagColored() << (
|
|
|
|
locationLength == 0 ?
|
|
|
|
"^" :
|
|
|
|
replaceNonTabs(text.substr(static_cast<size_t>(_ref.startColumn), locationLength), '^')
|
|
|
|
);
|
2020-04-23 22:51:15 +00:00
|
|
|
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 << " |";
|
2020-06-02 13:45:03 +00:00
|
|
|
m_stream << ' ' << text.substr(0, static_cast<size_t>(_ref.startColumn));
|
|
|
|
highlightColored() << text.substr(static_cast<size_t>(_ref.startColumn)) << '\n';
|
2018-11-26 23:21:53 +00:00
|
|
|
|
|
|
|
// line 3:
|
2020-04-23 22:51:15 +00:00
|
|
|
m_stream << leftpad << ' ';
|
|
|
|
frameColored() << '|';
|
2020-06-02 13:45:03 +00:00
|
|
|
m_stream << ' ' << replaceNonTabs(text.substr(0, static_cast<size_t>(_ref.startColumn)), ' ');
|
2020-04-23 22:51:15 +00:00
|
|
|
diagColored() << "^ (Relevant source part starts here and spans across multiple lines).";
|
|
|
|
m_stream << '\n';
|
2018-11-26 23:21:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 10:12:15 +00:00
|
|
|
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)
|
2018-11-26 23:21:53 +00:00
|
|
|
{
|
2022-09-06 10:12:15 +00:00
|
|
|
errorColored(Error::errorSeverityOrType(_msg._typeOrSeverity)) << Error::formatTypeOrSeverity(_msg._typeOrSeverity);
|
2022-06-17 03:25:52 +00:00
|
|
|
|
2020-06-04 01:19:47 +00:00
|
|
|
if (m_withErrorIds && _msg.errorId.has_value())
|
2022-09-06 10:12:15 +00:00
|
|
|
errorColored(Error::errorSeverityOrType(_msg._typeOrSeverity)) << " (" << _msg.errorId.value().error << ")";
|
2018-11-26 23:21:53 +00:00
|
|
|
|
2022-06-17 03:25:52 +00:00
|
|
|
messageColored() << ": " << _msg.primary.message << '\n';
|
2018-11-26 23:21:53 +00:00
|
|
|
printSourceLocation(_msg.primary);
|
|
|
|
|
|
|
|
for (auto const& secondary: _msg.secondary)
|
|
|
|
{
|
|
|
|
secondaryColored() << "Note";
|
2020-10-14 15:44:40 +00:00
|
|
|
messageColored() << ":" << (secondary.message.empty() ? "" : (" " + secondary.message)) << '\n';
|
2018-11-26 23:21:53 +00:00
|
|
|
printSourceLocation(secondary);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_stream << '\n';
|
|
|
|
}
|
2020-11-04 00:15:01 +00:00
|
|
|
|
2022-09-06 10:12:15 +00:00
|
|
|
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, Error::Type _type)
|
2020-11-04 00:15:01 +00:00
|
|
|
{
|
2022-09-06 10:12:15 +00:00
|
|
|
printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _type));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, Error::Severity _severity)
|
|
|
|
{
|
|
|
|
printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _severity));
|
2021-06-29 12:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SourceReferenceFormatter::printErrorInformation(ErrorList const& _errors)
|
|
|
|
{
|
|
|
|
for (auto const& error: _errors)
|
|
|
|
printErrorInformation(*error);
|
2020-11-04 00:15:01 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:22:15 +00:00
|
|
|
void SourceReferenceFormatter::printErrorInformation(Error const& _error)
|
2020-11-04 00:15:01 +00:00
|
|
|
{
|
2022-09-06 10:12:15 +00:00
|
|
|
SourceReferenceExtractor::Message message =
|
|
|
|
SourceReferenceExtractor::extract(
|
|
|
|
m_charStreamProvider,
|
|
|
|
_error,
|
|
|
|
Error::errorSeverity(_error.type())
|
|
|
|
);
|
|
|
|
printExceptionInformation(message);
|
2020-11-04 00:15:01 +00:00
|
|
|
}
|