solidity/liblangutil/SourceReferenceFormatter.cpp

101 lines
3.0 KiB
C++
Raw Normal View History

2014-10-23 17:22:30 +00:00
/*
This file is part of solidity.
2014-10-23 17:22:30 +00:00
solidity is free software: you can redistribute it and/or modify
2014-10-23 17:22:30 +00:00
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,
2014-10-23 17:22:30 +00:00
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/>.
2014-10-23 17:22:30 +00:00
*/
/**
* @author Christian <c@ethdev.com>
* @date 2014
* Formatting functions for errors referencing positions and locations in the source.
*/
#include <liblangutil/SourceReferenceFormatter.h>
#include <liblangutil/Scanner.h>
#include <liblangutil/Exceptions.h>
2014-10-23 17:22:30 +00:00
2014-10-24 17:06:30 +00:00
using namespace std;
2019-12-11 16:31:36 +00:00
using namespace solidity;
using namespace solidity::util;
using namespace solidity::langutil;
2014-10-24 17:06:30 +00:00
void SourceReferenceFormatter::printSourceLocation(SourceLocation const* _location)
2014-10-23 17:22:30 +00:00
{
printSourceLocation(SourceReferenceExtractor::extract(_location));
}
void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref)
{
if (_ref.position.line < 0)
return; // Nothing we can print here
if (!_ref.multiline)
{
m_stream << _ref.text << endl;
// mark the text-range like this: ^-----^
2015-05-04 15:18:01 +00:00
for_each(
_ref.text.cbegin(),
_ref.text.cbegin() + _ref.startColumn,
[this](char ch) { m_stream << (ch == '\t' ? '\t' : ' '); }
2015-05-04 15:18:01 +00:00
);
m_stream << "^";
if (_ref.endColumn > _ref.startColumn + 2)
m_stream << string(static_cast<size_t>(_ref.endColumn - _ref.startColumn - 2), '-');
if (_ref.endColumn > _ref.startColumn + 1)
m_stream << "^";
m_stream << endl;
2014-10-23 17:22:30 +00:00
}
else
m_stream <<
_ref.text <<
2015-05-04 15:18:01 +00:00
endl <<
string(static_cast<size_t>(_ref.startColumn), ' ') <<
"^ (Relevant source part starts here and spans across multiple lines)." <<
endl;
2014-10-23 17:22:30 +00:00
}
void SourceReferenceFormatter::printSourceName(SourceReference const& _ref)
{
if (_ref.position.line != -1)
m_stream << _ref.sourceName << ":" << (_ref.position.line + 1) << ":" << (_ref.position.column + 1) << ": ";
else if (!_ref.sourceName.empty())
m_stream << _ref.sourceName << ": ";
}
2019-12-11 16:31:36 +00:00
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, std::string const& _category)
2014-10-23 17:22:30 +00:00
{
printExceptionInformation(SourceReferenceExtractor::extract(_exception, _category));
}
void SourceReferenceFormatter::printErrorInformation(Error const& _error)
{
2020-06-04 01:19:47 +00:00
printExceptionInformation(SourceReferenceExtractor::extract(_error));
}
2014-10-23 17:22:30 +00:00
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)
{
printSourceName(_msg.primary);
m_stream << _msg.category << ": " << _msg.primary.message << endl;
printSourceLocation(_msg.primary);
for (auto const& ref: _msg.secondary)
{
printSourceName(ref);
m_stream << ref.message << endl;
printSourceLocation(ref);
}
2014-10-23 17:22:30 +00:00
}