2014-10-23 17:22:30 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2014-10-23 17:22:30 +00:00
|
|
|
|
2016-11-18 23:13:20 +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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2018-11-24 11:33:36 +00:00
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
2018-11-14 13:59:30 +00:00
|
|
|
#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;
|
2018-11-24 11:33:36 +00:00
|
|
|
using namespace dev;
|
2018-11-14 16:11:55 +00:00
|
|
|
using namespace langutil;
|
2014-10-24 17:06:30 +00:00
|
|
|
|
2017-10-26 20:56:00 +00:00
|
|
|
void SourceReferenceFormatter::printSourceLocation(SourceLocation const* _location)
|
2014-10-23 17:22:30 +00:00
|
|
|
{
|
2018-11-30 13:34:08 +00:00
|
|
|
printSourceLocation(SourceReferenceExtractor::extract(_location));
|
|
|
|
}
|
2017-10-17 17:59:15 +00:00
|
|
|
|
2018-11-30 13:34:08 +00:00
|
|
|
void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref)
|
|
|
|
{
|
|
|
|
if (_ref.position.line < 0)
|
|
|
|
return; // Nothing we can print here
|
2017-10-17 17:59:15 +00:00
|
|
|
|
2018-11-30 13:34:08 +00:00
|
|
|
if (!_ref.multiline)
|
|
|
|
{
|
|
|
|
m_stream << _ref.text << endl;
|
2017-10-26 20:56:00 +00:00
|
|
|
|
2018-11-30 13:34:08 +00:00
|
|
|
// mark the text-range like this: ^-----^
|
2015-05-04 15:18:01 +00:00
|
|
|
for_each(
|
2018-11-30 13:34:08 +00:00
|
|
|
_ref.text.cbegin(),
|
|
|
|
_ref.text.cbegin() + _ref.startColumn,
|
|
|
|
[this](char ch) { m_stream << (ch == '\t' ? '\t' : ' '); }
|
2015-05-04 15:18:01 +00:00
|
|
|
);
|
2017-10-26 20:56:00 +00:00
|
|
|
m_stream << "^";
|
2018-11-30 13:34:08 +00:00
|
|
|
if (_ref.endColumn > _ref.startColumn + 2)
|
|
|
|
m_stream << string(_ref.endColumn - _ref.startColumn - 2, '-');
|
|
|
|
if (_ref.endColumn > _ref.startColumn + 1)
|
2017-10-26 20:56:00 +00:00
|
|
|
m_stream << "^";
|
|
|
|
m_stream << endl;
|
2014-10-23 17:22:30 +00:00
|
|
|
}
|
|
|
|
else
|
2017-10-26 20:56:00 +00:00
|
|
|
m_stream <<
|
2018-11-30 13:34:08 +00:00
|
|
|
_ref.text <<
|
2015-05-04 15:18:01 +00:00
|
|
|
endl <<
|
2018-11-30 13:34:08 +00:00
|
|
|
string(_ref.startColumn, ' ') <<
|
2018-03-02 21:30:03 +00:00
|
|
|
"^ (Relevant source part starts here and spans across multiple lines)." <<
|
|
|
|
endl;
|
2014-10-23 17:22:30 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:34:08 +00:00
|
|
|
void SourceReferenceFormatter::printSourceName(SourceReference const& _ref)
|
2015-04-30 16:06:04 +00:00
|
|
|
{
|
2018-11-30 13:34:08 +00:00
|
|
|
if (_ref.position.line != -1)
|
|
|
|
m_stream << _ref.sourceName << ":" << (_ref.position.line + 1) << ":" << (_ref.position.column + 1) << ": ";
|
2015-04-30 16:06:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:34:08 +00:00
|
|
|
void SourceReferenceFormatter::printExceptionInformation(dev::Exception const& _error, std::string const& _category)
|
2014-10-23 17:22:30 +00:00
|
|
|
{
|
2018-11-30 13:34:08 +00:00
|
|
|
printExceptionInformation(SourceReferenceExtractor::extract(_error, _category));
|
|
|
|
}
|
2014-10-23 17:22:30 +00:00
|
|
|
|
2018-11-30 13:34:08 +00:00
|
|
|
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)
|
|
|
|
{
|
|
|
|
printSourceName(_msg.primary);
|
2015-04-30 16:06:04 +00:00
|
|
|
|
2018-11-30 13:34:08 +00:00
|
|
|
m_stream << _msg.category << ": " << _msg.primary.message << endl;
|
2014-12-03 17:52:28 +00:00
|
|
|
|
2018-11-30 13:34:08 +00:00
|
|
|
printSourceLocation(_msg.primary);
|
2015-04-30 16:06:04 +00:00
|
|
|
|
2018-11-30 13:34:08 +00:00
|
|
|
for (auto const& ref: _msg.secondary)
|
2015-04-30 16:06:04 +00:00
|
|
|
{
|
2018-11-30 13:34:08 +00:00
|
|
|
printSourceName(ref);
|
|
|
|
m_stream << ref.message << endl;
|
|
|
|
printSourceLocation(ref);
|
2015-04-30 16:06:04 +00:00
|
|
|
}
|
2014-10-23 17:22:30 +00:00
|
|
|
}
|