2018-11-26 23:21:53 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-11-26 23:21:53 +00:00
|
|
|
/**
|
|
|
|
* 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>
|
2018-11-26 23:21:53 +00:00
|
|
|
#include <liblangutil/SourceReferenceExtractor.h>
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/AnsiColorized.h>
|
2018-11-26 23:21:53 +00:00
|
|
|
|
|
|
|
#include <ostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <functional>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::langutil
|
2018-11-26 23:21:53 +00:00
|
|
|
{
|
2021-08-03 13:20:00 +00:00
|
|
|
|
|
|
|
class CharStream;
|
|
|
|
class CharStreamProvider;
|
2020-11-04 00:15:01 +00:00
|
|
|
struct SourceLocation;
|
2018-11-26 23:21:53 +00:00
|
|
|
|
2020-12-01 13:22:15 +00:00
|
|
|
class SourceReferenceFormatter
|
2018-11-26 23:21:53 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-06-29 12:38:59 +00:00
|
|
|
SourceReferenceFormatter(
|
|
|
|
std::ostream& _stream,
|
|
|
|
CharStreamProvider const& _charStreamProvider,
|
|
|
|
bool _colored,
|
|
|
|
bool _withErrorIds
|
|
|
|
):
|
2023-08-08 14:11:57 +00:00
|
|
|
m_stream(_stream),
|
|
|
|
m_charStreamProvider(_charStreamProvider),
|
|
|
|
m_colored(_colored),
|
|
|
|
m_withErrorIds(_withErrorIds)
|
2018-11-26 23:21:53 +00:00
|
|
|
{}
|
|
|
|
|
2023-08-08 15:17:37 +00:00
|
|
|
// WARNING: Use the xyzErrorInformation() variants over xyzExceptionInformation() when you
|
|
|
|
// do have access to an Error instance. Error is implicitly convertible to util::Exception
|
|
|
|
// but the conversion loses the error ID.
|
|
|
|
|
2020-11-04 00:15:01 +00:00
|
|
|
/// Prints source location if it is given.
|
|
|
|
void printSourceLocation(SourceReference const& _ref);
|
2022-09-06 10:12:15 +00:00
|
|
|
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);
|
|
|
|
void printExceptionInformation(util::Exception const& _exception, Error::Type _type);
|
|
|
|
void printExceptionInformation(util::Exception const& _exception, Error::Severity _severity);
|
2021-06-29 12:38:59 +00:00
|
|
|
void printErrorInformation(langutil::ErrorList const& _errors);
|
2020-11-04 00:15:01 +00:00
|
|
|
void printErrorInformation(Error const& _error);
|
2018-11-26 23:21:53 +00:00
|
|
|
|
|
|
|
static std::string formatExceptionInformation(
|
2019-12-11 16:31:36 +00:00
|
|
|
util::Exception const& _exception,
|
2022-06-17 03:25:52 +00:00
|
|
|
Error::Type _type,
|
2021-06-29 12:38:59 +00:00
|
|
|
CharStreamProvider const& _charStreamProvider,
|
2023-08-08 15:54:05 +00:00
|
|
|
bool _colored = false
|
2018-11-26 23:21:53 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
std::ostringstream errorOutput;
|
2023-08-08 15:54:05 +00:00
|
|
|
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, false /* _withErrorIds */);
|
2022-09-06 10:12:15 +00:00
|
|
|
formatter.printExceptionInformation(_exception, _type);
|
|
|
|
return errorOutput.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string formatExceptionInformation(
|
|
|
|
util::Exception const& _exception,
|
|
|
|
Error::Severity _severity,
|
|
|
|
CharStreamProvider const& _charStreamProvider,
|
2023-08-08 15:54:05 +00:00
|
|
|
bool _colored = false
|
2022-09-06 10:12:15 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
std::ostringstream errorOutput;
|
2023-08-08 15:54:05 +00:00
|
|
|
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, false /* _withErrorIds */);
|
2022-09-06 10:12:15 +00:00
|
|
|
formatter.printExceptionInformation(_exception, _severity);
|
2018-11-26 23:21:53 +00:00
|
|
|
return errorOutput.str();
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:38:59 +00:00
|
|
|
static std::string formatErrorInformation(
|
|
|
|
Error const& _error,
|
2023-08-08 15:08:37 +00:00
|
|
|
CharStreamProvider const& _charStreamProvider,
|
|
|
|
bool _colored = false,
|
|
|
|
bool _withErrorIds = false
|
2021-06-29 12:38:59 +00:00
|
|
|
)
|
2020-11-02 14:29:33 +00:00
|
|
|
{
|
2023-08-08 15:08:37 +00:00
|
|
|
std::ostringstream errorOutput;
|
|
|
|
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds);
|
|
|
|
formatter.printErrorInformation(_error);
|
|
|
|
return errorOutput.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string formatErrorInformation(
|
|
|
|
langutil::ErrorList const& _errors,
|
|
|
|
CharStreamProvider const& _charStreamProvider,
|
|
|
|
bool _colored = false,
|
|
|
|
bool _withErrorIds = false
|
|
|
|
)
|
|
|
|
{
|
|
|
|
std::ostringstream errorOutput;
|
|
|
|
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds);
|
|
|
|
formatter.printErrorInformation(_errors);
|
|
|
|
return errorOutput.str();
|
2021-06-29 12:38:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 13:20:00 +00:00
|
|
|
static std::string formatErrorInformation(Error const& _error, CharStream const& _charStream);
|
2020-11-02 14:29:33 +00:00
|
|
|
|
2018-11-26 23:21:53 +00:00
|
|
|
private:
|
2019-12-11 16:31:36 +00:00
|
|
|
util::AnsiColorized normalColored() const;
|
|
|
|
util::AnsiColorized frameColored() const;
|
2022-06-17 03:25:52 +00:00
|
|
|
util::AnsiColorized errorColored(langutil::Error::Severity _severity) const;
|
2019-12-11 16:31:36 +00:00
|
|
|
util::AnsiColorized messageColored() const;
|
|
|
|
util::AnsiColorized secondaryColored() const;
|
|
|
|
util::AnsiColorized highlightColored() const;
|
|
|
|
util::AnsiColorized diagColored() const;
|
2018-11-26 23:21:53 +00:00
|
|
|
|
|
|
|
private:
|
2020-11-04 00:15:01 +00:00
|
|
|
std::ostream& m_stream;
|
2021-06-29 12:38:59 +00:00
|
|
|
CharStreamProvider const& m_charStreamProvider;
|
2018-11-26 23:21:53 +00:00
|
|
|
bool m_colored;
|
2020-06-04 01:19:47 +00:00
|
|
|
bool m_withErrorIds;
|
2018-11-26 23:21:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|