Drop old SourceReferenceFormatter

This commit is contained in:
Alex Beregszaszi 2020-11-04 00:15:01 +00:00 committed by chriseth
parent 1dedba8538
commit 4044817255
5 changed files with 20 additions and 196 deletions

View File

@ -19,8 +19,6 @@ set(sources
SourceLocation.cpp
SourceReferenceExtractor.cpp
SourceReferenceExtractor.h
SourceReferenceFormatter.cpp
SourceReferenceFormatter.h
SourceReferenceFormatterHuman.cpp
SourceReferenceFormatterHuman.h
Token.cpp

View File

@ -1,101 +0,0 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* @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>
using namespace std;
using namespace solidity;
using namespace solidity::util;
using namespace solidity::langutil;
void SourceReferenceFormatter::printSourceLocation(SourceLocation const* _location)
{
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: ^-----^
for_each(
_ref.text.cbegin(),
_ref.text.cbegin() + _ref.startColumn,
[this](char ch) { m_stream << (ch == '\t' ? '\t' : ' '); }
);
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;
}
else
m_stream <<
_ref.text <<
endl <<
string(static_cast<size_t>(_ref.startColumn), ' ') <<
"^ (Relevant source part starts here and spans across multiple lines)." <<
endl;
}
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 << ": ";
}
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, std::string const& _category)
{
printExceptionInformation(SourceReferenceExtractor::extract(_exception, _category));
}
void SourceReferenceFormatter::printErrorInformation(Error const& _error)
{
printExceptionInformation(SourceReferenceExtractor::extract(_error));
}
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);
}
}

View File

@ -1,86 +0,0 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* @author Christian <c@ethdev.com>
* @date 2014
* Formatting functions for errors referencing positions and locations in the source.
*/
#pragma once
#include <ostream>
#include <sstream>
#include <functional>
#include <liblangutil/Exceptions.h>
#include <liblangutil/SourceReferenceExtractor.h>
namespace solidity::util
{
struct Exception; // forward
}
namespace solidity::langutil
{
struct SourceLocation;
class Scanner;
class SourceReferenceFormatter
{
public:
explicit SourceReferenceFormatter(std::ostream& _stream):
m_stream(_stream)
{}
virtual ~SourceReferenceFormatter() = default;
/// Prints source location if it is given.
virtual void printSourceLocation(SourceReference const& _ref);
virtual void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);
virtual void printSourceLocation(SourceLocation const* _location);
virtual void printExceptionInformation(util::Exception const& _exception, std::string const& _category);
virtual void printErrorInformation(Error const& _error);
static std::string formatErrorInformation(Error const& _error)
{
return formatExceptionInformation(
_error,
(_error.type() == Error::Type::Warning) ? "Warning" : "Error"
);
}
static std::string formatExceptionInformation(
util::Exception const& _exception,
std::string const& _name
)
{
std::ostringstream errorOutput;
SourceReferenceFormatter formatter(errorOutput);
formatter.printExceptionInformation(_exception, _name);
return errorOutput.str();
}
protected:
/// Prints source name if location is given.
void printSourceName(SourceReference const& _ref);
std::ostream& m_stream;
};
}

View File

@ -171,3 +171,13 @@ void SourceReferenceFormatterHuman::printExceptionInformation(SourceReferenceExt
m_stream << '\n';
}
void SourceReferenceFormatterHuman::printExceptionInformation(util::Exception const& _exception, std::string const& _category)
{
printExceptionInformation(SourceReferenceExtractor::extract(_exception, _category));
}
void SourceReferenceFormatterHuman::printErrorInformation(Error const& _error)
{
printExceptionInformation(SourceReferenceExtractor::extract(_error));
}

View File

@ -21,8 +21,8 @@
#pragma once
#include <liblangutil/Exceptions.h>
#include <liblangutil/SourceReferenceExtractor.h>
#include <liblangutil/SourceReferenceFormatter.h> // SourceReferenceFormatterBase
#include <libsolutil/AnsiColorized.h>
@ -32,17 +32,20 @@
namespace solidity::langutil
{
struct SourceLocation;
class SourceReferenceFormatterHuman: public SourceReferenceFormatter
class SourceReferenceFormatterHuman
{
public:
SourceReferenceFormatterHuman(std::ostream& _stream, bool _colored, bool _withErrorIds):
SourceReferenceFormatter{_stream}, m_colored{_colored}, m_withErrorIds(_withErrorIds)
m_stream(_stream), m_colored(_colored), m_withErrorIds(_withErrorIds)
{}
void printSourceLocation(SourceReference const& _ref) override;
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg) override;
using SourceReferenceFormatter::printExceptionInformation;
/// Prints source location if it is given.
void printSourceLocation(SourceReference const& _ref);
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);
void printExceptionInformation(util::Exception const& _exception, std::string const& _category);
void printErrorInformation(Error const& _error);
static std::string formatExceptionInformation(
util::Exception const& _exception,
@ -52,7 +55,6 @@ public:
)
{
std::ostringstream errorOutput;
SourceReferenceFormatterHuman formatter(errorOutput, _colored, _withErrorIds);
formatter.printExceptionInformation(_exception, _name);
return errorOutput.str();
@ -76,6 +78,7 @@ private:
util::AnsiColorized diagColored() const;
private:
std::ostream& m_stream;
bool m_colored;
bool m_withErrorIds;
};