2014-10-16 15:56:10 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2014-10-16 15:56:10 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2014-10-16 15:56:10 +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-16 15:56:10 +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-16 15:56:10 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2014-10-16 15:56:10 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2014
|
2014-10-23 17:22:30 +00:00
|
|
|
* Formatting functions for errors referencing positions and locations in the source.
|
2014-10-16 15:56:10 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-23 17:22:30 +00:00
|
|
|
#pragma once
|
2014-10-16 15:56:10 +00:00
|
|
|
|
2014-10-23 17:22:30 +00:00
|
|
|
#include <ostream>
|
2017-03-29 21:40:10 +00:00
|
|
|
#include <sstream>
|
2016-02-22 01:13:41 +00:00
|
|
|
#include <functional>
|
2019-04-05 15:49:39 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
2018-11-30 13:34:08 +00:00
|
|
|
#include <liblangutil/SourceReferenceExtractor.h>
|
2018-11-14 16:11:55 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::util
|
2014-10-16 15:56:10 +00:00
|
|
|
{
|
2015-01-13 12:20:07 +00:00
|
|
|
struct Exception; // forward
|
2018-11-24 11:33:36 +00:00
|
|
|
}
|
2014-10-16 15:56:10 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::langutil
|
2014-10-16 15:56:10 +00:00
|
|
|
{
|
2018-11-24 11:33:36 +00:00
|
|
|
struct SourceLocation;
|
|
|
|
class Scanner;
|
2014-10-23 17:22:30 +00:00
|
|
|
|
2017-10-26 20:56:00 +00:00
|
|
|
class SourceReferenceFormatter
|
2014-10-16 15:56:10 +00:00
|
|
|
{
|
2014-10-23 17:22:30 +00:00
|
|
|
public:
|
2018-11-30 13:34:08 +00:00
|
|
|
explicit SourceReferenceFormatter(std::ostream& _stream):
|
|
|
|
m_stream(_stream)
|
2017-10-26 20:56:00 +00:00
|
|
|
{}
|
|
|
|
|
2018-11-26 23:21:53 +00:00
|
|
|
virtual ~SourceReferenceFormatter() = default;
|
|
|
|
|
2017-10-26 20:56:00 +00:00
|
|
|
/// Prints source location if it is given.
|
2018-11-26 23:21:53 +00:00
|
|
|
virtual void printSourceLocation(SourceReference const& _ref);
|
|
|
|
virtual void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);
|
|
|
|
|
|
|
|
virtual void printSourceLocation(SourceLocation const* _location);
|
2019-12-11 16:31:36 +00:00
|
|
|
virtual void printExceptionInformation(util::Exception const& _exception, std::string const& _category);
|
2019-04-05 15:49:39 +00:00
|
|
|
virtual void printErrorInformation(Error const& _error);
|
|
|
|
|
|
|
|
static std::string formatErrorInformation(Error const& _error)
|
|
|
|
{
|
|
|
|
return formatExceptionInformation(
|
|
|
|
_error,
|
|
|
|
(_error.type() == Error::Type::Warning) ? "Warning" : "Error"
|
|
|
|
);
|
|
|
|
}
|
2017-10-26 20:56:00 +00:00
|
|
|
|
2017-03-29 21:40:10 +00:00
|
|
|
static std::string formatExceptionInformation(
|
2019-12-11 16:31:36 +00:00
|
|
|
util::Exception const& _exception,
|
2018-11-30 13:34:08 +00:00
|
|
|
std::string const& _name
|
2017-03-29 21:40:10 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
std::ostringstream errorOutput;
|
2017-10-26 20:56:00 +00:00
|
|
|
|
2018-11-30 13:34:08 +00:00
|
|
|
SourceReferenceFormatter formatter(errorOutput);
|
2017-10-26 20:56:00 +00:00
|
|
|
formatter.printExceptionInformation(_exception, _name);
|
2017-03-29 21:40:10 +00:00
|
|
|
return errorOutput.str();
|
|
|
|
}
|
2018-11-30 13:34:08 +00:00
|
|
|
|
2018-11-26 23:21:53 +00:00
|
|
|
protected:
|
2016-03-31 22:54:07 +00:00
|
|
|
/// Prints source name if location is given.
|
2018-11-30 13:34:08 +00:00
|
|
|
void printSourceName(SourceReference const& _ref);
|
2017-10-26 20:56:00 +00:00
|
|
|
|
|
|
|
std::ostream& m_stream;
|
2014-10-23 17:22:30 +00:00
|
|
|
};
|
2014-10-16 15:56:10 +00:00
|
|
|
|
|
|
|
}
|