solidity/liblangutil/SourceReferenceFormatterHuman.h

87 lines
2.5 KiB
C
Raw Normal View History

/*
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
/**
* Formatting functions for errors referencing positions and locations in the source.
*/
#pragma once
2020-11-04 00:15:01 +00:00
#include <liblangutil/Exceptions.h>
#include <liblangutil/SourceReferenceExtractor.h>
#include <libsolutil/AnsiColorized.h>
#include <ostream>
#include <sstream>
#include <functional>
2019-12-11 16:31:36 +00:00
namespace solidity::langutil
{
2020-11-04 00:15:01 +00:00
struct SourceLocation;
2020-11-04 00:15:01 +00:00
class SourceReferenceFormatterHuman
{
public:
2020-06-04 01:19:47 +00:00
SourceReferenceFormatterHuman(std::ostream& _stream, bool _colored, bool _withErrorIds):
2020-11-04 00:15:01 +00:00
m_stream(_stream), m_colored(_colored), m_withErrorIds(_withErrorIds)
{}
2020-11-04 00:15:01 +00:00
/// 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(
2019-12-11 16:31:36 +00:00
util::Exception const& _exception,
std::string const& _name,
2020-06-04 01:19:47 +00:00
bool _colored = false,
bool _withErrorIds = false
)
{
std::ostringstream errorOutput;
2020-06-04 01:19:47 +00:00
SourceReferenceFormatterHuman formatter(errorOutput, _colored, _withErrorIds);
formatter.printExceptionInformation(_exception, _name);
return errorOutput.str();
}
2020-11-02 14:29:33 +00:00
static std::string formatErrorInformation(Error const& _error)
{
return formatExceptionInformation(
_error,
(_error.type() == Error::Type::Warning) ? "Warning" : "Error"
);
}
private:
2019-12-11 16:31:36 +00:00
util::AnsiColorized normalColored() const;
util::AnsiColorized frameColored() const;
util::AnsiColorized errorColored() const;
util::AnsiColorized messageColored() const;
util::AnsiColorized secondaryColored() const;
util::AnsiColorized highlightColored() const;
util::AnsiColorized diagColored() const;
private:
2020-11-04 00:15:01 +00:00
std::ostream& m_stream;
bool m_colored;
2020-06-04 01:19:47 +00:00
bool m_withErrorIds;
};
}