mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge 6c1ed8c2d0
into fe1f9c640e
This commit is contained in:
commit
01707cb182
@ -29,6 +29,26 @@
|
|||||||
using namespace solidity;
|
using namespace solidity;
|
||||||
using namespace solidity::langutil;
|
using namespace solidity::langutil;
|
||||||
|
|
||||||
|
std::map<Error::Type, std::string> const Error::m_errorTypeNames = {
|
||||||
|
{Error::Type::IOError, "IOError"},
|
||||||
|
{Error::Type::FatalError, "FatalError"},
|
||||||
|
{Error::Type::JSONError, "JSONError"},
|
||||||
|
{Error::Type::InternalCompilerError, "InternalCompilerError"},
|
||||||
|
{Error::Type::CompilerError, "CompilerError"},
|
||||||
|
{Error::Type::Exception, "Exception"},
|
||||||
|
{Error::Type::CodeGenerationError, "CodeGenerationError"},
|
||||||
|
{Error::Type::DeclarationError, "DeclarationError"},
|
||||||
|
{Error::Type::DocstringParsingError, "DocstringParsingError"},
|
||||||
|
{Error::Type::ParserError, "ParserError"},
|
||||||
|
{Error::Type::SyntaxError, "SyntaxError"},
|
||||||
|
{Error::Type::TypeError, "TypeError"},
|
||||||
|
{Error::Type::UnimplementedFeatureError, "UnimplementedFeatureError"},
|
||||||
|
{Error::Type::YulException, "YulException"},
|
||||||
|
{Error::Type::SMTLogicException, "SMTLogicException"},
|
||||||
|
{Error::Type::Warning, "Warning"},
|
||||||
|
{Error::Type::Info, "Info"},
|
||||||
|
};
|
||||||
|
|
||||||
Error::Error(
|
Error::Error(
|
||||||
ErrorId _errorId, Error::Type _type,
|
ErrorId _errorId, Error::Type _type,
|
||||||
std::string const& _description,
|
std::string const& _description,
|
||||||
|
@ -33,10 +33,11 @@
|
|||||||
#include <boost/preprocessor/facilities/overload.hpp>
|
#include <boost/preprocessor/facilities/overload.hpp>
|
||||||
#include <boost/algorithm/string/case_conv.hpp>
|
#include <boost/algorithm/string/case_conv.hpp>
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
|
||||||
namespace solidity::langutil
|
namespace solidity::langutil
|
||||||
@ -269,27 +270,17 @@ public:
|
|||||||
|
|
||||||
static std::string formatErrorType(Type _type)
|
static std::string formatErrorType(Type _type)
|
||||||
{
|
{
|
||||||
switch (_type)
|
return m_errorTypeNames.at(_type);
|
||||||
{
|
}
|
||||||
case Type::IOError: return "IOError";
|
|
||||||
case Type::FatalError: return "FatalError";
|
static std::optional<Type> parseErrorType(std::string _name)
|
||||||
case Type::JSONError: return "JSONError";
|
{
|
||||||
case Type::InternalCompilerError: return "InternalCompilerError";
|
static std::map<std::string, Error::Type> const m_errorTypesByName = util::invertMap(m_errorTypeNames);
|
||||||
case Type::CompilerError: return "CompilerError";
|
|
||||||
case Type::Exception: return "Exception";
|
if (m_errorTypesByName.count(_name) == 0)
|
||||||
case Type::CodeGenerationError: return "CodeGenerationError";
|
return std::nullopt;
|
||||||
case Type::DeclarationError: return "DeclarationError";
|
|
||||||
case Type::DocstringParsingError: return "DocstringParsingError";
|
return m_errorTypesByName.at(_name);
|
||||||
case Type::ParserError: return "ParserError";
|
|
||||||
case Type::SyntaxError: return "SyntaxError";
|
|
||||||
case Type::TypeError: return "TypeError";
|
|
||||||
case Type::UnimplementedFeatureError: return "UnimplementedFeatureError";
|
|
||||||
case Type::YulException: return "YulException";
|
|
||||||
case Type::SMTLogicException: return "SMTLogicException";
|
|
||||||
case Type::Warning: return "Warning";
|
|
||||||
case Type::Info: return "Info";
|
|
||||||
}
|
|
||||||
util::unreachable();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string formatTypeOrSeverity(std::variant<Error::Type, Error::Severity> _typeOrSeverity)
|
static std::string formatTypeOrSeverity(std::variant<Error::Type, Error::Severity> _typeOrSeverity)
|
||||||
@ -309,6 +300,8 @@ public:
|
|||||||
private:
|
private:
|
||||||
ErrorId m_errorId;
|
ErrorId m_errorId;
|
||||||
Type m_type;
|
Type m_type;
|
||||||
|
|
||||||
|
static std::map<Type, std::string> const m_errorTypeNames;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,28 @@ std::string SourceReferenceFormatter::formatErrorInformation(Error const& _error
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char const* SourceReferenceFormatter::errorTextColor(Error::Severity _severity)
|
||||||
|
{
|
||||||
|
switch (_severity)
|
||||||
|
{
|
||||||
|
case Error::Severity::Error: return RED;
|
||||||
|
case Error::Severity::Warning: return YELLOW;
|
||||||
|
case Error::Severity::Info: return WHITE;
|
||||||
|
}
|
||||||
|
util::unreachable();
|
||||||
|
}
|
||||||
|
|
||||||
|
char const* SourceReferenceFormatter::errorHighlightColor(Error::Severity _severity)
|
||||||
|
{
|
||||||
|
switch (_severity)
|
||||||
|
{
|
||||||
|
case Error::Severity::Error: return RED_BACKGROUND;
|
||||||
|
case Error::Severity::Warning: return ORANGE_BACKGROUND_256;
|
||||||
|
case Error::Severity::Info: return GRAY_BACKGROUND;
|
||||||
|
}
|
||||||
|
util::unreachable();
|
||||||
|
}
|
||||||
|
|
||||||
AnsiColorized SourceReferenceFormatter::normalColored() const
|
AnsiColorized SourceReferenceFormatter::normalColored() const
|
||||||
{
|
{
|
||||||
return AnsiColorized(m_stream, m_colored, {WHITE});
|
return AnsiColorized(m_stream, m_colored, {WHITE});
|
||||||
@ -67,18 +89,7 @@ AnsiColorized SourceReferenceFormatter::frameColored() const
|
|||||||
|
|
||||||
AnsiColorized SourceReferenceFormatter::errorColored(Error::Severity _severity) const
|
AnsiColorized SourceReferenceFormatter::errorColored(Error::Severity _severity) const
|
||||||
{
|
{
|
||||||
// We used to color messages of any severity as errors so this seems like a good default
|
return AnsiColorized(m_stream, m_colored, {BOLD, errorTextColor(_severity)});
|
||||||
// for cases where severity cannot be determined.
|
|
||||||
char const* textColor = RED;
|
|
||||||
|
|
||||||
switch (_severity)
|
|
||||||
{
|
|
||||||
case Error::Severity::Error: textColor = RED; break;
|
|
||||||
case Error::Severity::Warning: textColor = YELLOW; break;
|
|
||||||
case Error::Severity::Info: textColor = WHITE; break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return AnsiColorized(m_stream, m_colored, {BOLD, textColor});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiColorized SourceReferenceFormatter::messageColored() const
|
AnsiColorized SourceReferenceFormatter::messageColored() const
|
||||||
|
@ -118,6 +118,15 @@ public:
|
|||||||
|
|
||||||
static std::string formatErrorInformation(Error const& _error, CharStream const& _charStream);
|
static std::string formatErrorInformation(Error const& _error, CharStream const& _charStream);
|
||||||
|
|
||||||
|
/// The default text color for printing error messages of a given severity in the terminal.
|
||||||
|
/// Only suitable for color schemes with a dark background.
|
||||||
|
static char const* errorTextColor(Error::Severity _severity);
|
||||||
|
|
||||||
|
/// The default background color for highlighting source fragments corresponding to an error
|
||||||
|
/// of a given severity in the terminal.
|
||||||
|
/// Only suitable for color schemes with a light text.
|
||||||
|
static char const* errorHighlightColor(Error::Severity _severity);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
util::AnsiColorized normalColored() const;
|
util::AnsiColorized normalColored() const;
|
||||||
util::AnsiColorized frameColored() const;
|
util::AnsiColorized frameColored() const;
|
||||||
|
@ -52,6 +52,7 @@ static constexpr char const* BLUE_BACKGROUND = "\033[44m";
|
|||||||
static constexpr char const* MAGENTA_BACKGROUND = "\033[45m";
|
static constexpr char const* MAGENTA_BACKGROUND = "\033[45m";
|
||||||
static constexpr char const* CYAN_BACKGROUND = "\033[46m";
|
static constexpr char const* CYAN_BACKGROUND = "\033[46m";
|
||||||
static constexpr char const* WHITE_BACKGROUND = "\033[47m";
|
static constexpr char const* WHITE_BACKGROUND = "\033[47m";
|
||||||
|
static constexpr char const* GRAY_BACKGROUND = "\033[100m";
|
||||||
|
|
||||||
// 256-bit-colors (incomplete set)
|
// 256-bit-colors (incomplete set)
|
||||||
static constexpr char const* RED_BACKGROUND_256 = "\033[48;5;160m";
|
static constexpr char const* RED_BACKGROUND_256 = "\033[48;5;160m";
|
||||||
|
@ -19,11 +19,16 @@
|
|||||||
#include <test/CommonSyntaxTest.h>
|
#include <test/CommonSyntaxTest.h>
|
||||||
#include <test/Common.h>
|
#include <test/Common.h>
|
||||||
#include <test/TestCase.h>
|
#include <test/TestCase.h>
|
||||||
|
|
||||||
|
#include <liblangutil/SourceReferenceFormatter.h>
|
||||||
|
|
||||||
#include <libsolutil/CommonIO.h>
|
#include <libsolutil/CommonIO.h>
|
||||||
|
|
||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
#include <boost/algorithm/string/predicate.hpp>
|
#include <boost/algorithm/string/predicate.hpp>
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
#include <boost/throw_exception.hpp>
|
#include <boost/throw_exception.hpp>
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@ -114,15 +119,18 @@ void CommonSyntaxTest::printSource(ostream& _stream, string const& _linePrefix,
|
|||||||
{
|
{
|
||||||
assert(static_cast<size_t>(error.locationStart) <= source.length());
|
assert(static_cast<size_t>(error.locationStart) <= source.length());
|
||||||
assert(static_cast<size_t>(error.locationEnd) <= source.length());
|
assert(static_cast<size_t>(error.locationEnd) <= source.length());
|
||||||
bool isWarning = error.type == "Warning";
|
|
||||||
for (int i = error.locationStart; i < error.locationEnd; i++)
|
for (int i = error.locationStart; i < error.locationEnd; i++)
|
||||||
if (isWarning)
|
{
|
||||||
{
|
char const*& cellFormat = sourceFormatting[static_cast<size_t>(i)];
|
||||||
if (sourceFormatting[static_cast<size_t>(i)] == util::formatting::RESET)
|
char const* infoBgColor = SourceReferenceFormatter::errorHighlightColor(Error::Severity::Info);
|
||||||
sourceFormatting[static_cast<size_t>(i)] = util::formatting::ORANGE_BACKGROUND_256;
|
|
||||||
}
|
if (
|
||||||
else
|
(error.type != Error::Type::Warning && error.type != Error::Type::Info) ||
|
||||||
sourceFormatting[static_cast<size_t>(i)] = util::formatting::RED_BACKGROUND;
|
(error.type == Error::Type::Warning && (cellFormat == RESET || cellFormat == infoBgColor)) ||
|
||||||
|
(error.type == Error::Type::Info && cellFormat == RESET)
|
||||||
|
)
|
||||||
|
cellFormat = SourceReferenceFormatter::errorHighlightColor(Error::errorSeverity(error.type));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_stream << _linePrefix << sourceFormatting.front() << source.front();
|
_stream << _linePrefix << sourceFormatting.front() << source.front();
|
||||||
@ -192,8 +200,12 @@ void CommonSyntaxTest::printErrorList(
|
|||||||
for (auto const& error: _errorList)
|
for (auto const& error: _errorList)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
util::AnsiColorized scope(_stream, _formatted, {BOLD, (error.type == "Warning") ? YELLOW : RED});
|
util::AnsiColorized scope(
|
||||||
_stream << _linePrefix << error.type;
|
_stream,
|
||||||
|
_formatted,
|
||||||
|
{BOLD, SourceReferenceFormatter::errorTextColor(Error::errorSeverity(error.type))}
|
||||||
|
);
|
||||||
|
_stream << _linePrefix << Error::formatErrorType(error.type);
|
||||||
if (error.errorId.has_value())
|
if (error.errorId.has_value())
|
||||||
_stream << ' ' << error.errorId->error;
|
_stream << ' ' << error.errorId->error;
|
||||||
_stream << ": ";
|
_stream << ": ";
|
||||||
@ -246,7 +258,11 @@ vector<SyntaxTestError> CommonSyntaxTest::parseExpectations(istream& _stream)
|
|||||||
auto typeBegin = it;
|
auto typeBegin = it;
|
||||||
while (it != line.end() && isalpha(*it, locale::classic()))
|
while (it != line.end() && isalpha(*it, locale::classic()))
|
||||||
++it;
|
++it;
|
||||||
string errorType(typeBegin, it);
|
|
||||||
|
std::string errorTypeStr(typeBegin, it);
|
||||||
|
optional<Error::Type> errorType = Error::parseErrorType(errorTypeStr);
|
||||||
|
if (!errorType.has_value())
|
||||||
|
BOOST_THROW_EXCEPTION(runtime_error("Invalid error type: " + errorTypeStr));
|
||||||
|
|
||||||
skipWhitespace(it, line.end());
|
skipWhitespace(it, line.end());
|
||||||
|
|
||||||
@ -283,7 +299,7 @@ vector<SyntaxTestError> CommonSyntaxTest::parseExpectations(istream& _stream)
|
|||||||
|
|
||||||
string errorMessage(it, line.end());
|
string errorMessage(it, line.end());
|
||||||
expectations.emplace_back(SyntaxTestError{
|
expectations.emplace_back(SyntaxTestError{
|
||||||
std::move(errorType),
|
errorType.value(),
|
||||||
std::move(errorId),
|
std::move(errorId),
|
||||||
std::move(errorMessage),
|
std::move(errorMessage),
|
||||||
std::move(sourceName),
|
std::move(sourceName),
|
||||||
|
@ -34,7 +34,7 @@ namespace solidity::test
|
|||||||
|
|
||||||
struct SyntaxTestError
|
struct SyntaxTestError
|
||||||
{
|
{
|
||||||
std::string type;
|
langutil::Error::Type type;
|
||||||
std::optional<langutil::ErrorId> errorId;
|
std::optional<langutil::ErrorId> errorId;
|
||||||
std::string message;
|
std::string message;
|
||||||
std::string sourceName;
|
std::string sourceName;
|
||||||
|
@ -86,7 +86,7 @@ void SyntaxTest::parseAndAnalyze()
|
|||||||
catch (UnimplementedFeatureError const& _e)
|
catch (UnimplementedFeatureError const& _e)
|
||||||
{
|
{
|
||||||
m_errorList.emplace_back(SyntaxTestError{
|
m_errorList.emplace_back(SyntaxTestError{
|
||||||
"UnimplementedFeatureError",
|
Error::Type::UnimplementedFeatureError,
|
||||||
nullopt,
|
nullopt,
|
||||||
errorMessage(_e),
|
errorMessage(_e),
|
||||||
"",
|
"",
|
||||||
@ -135,7 +135,7 @@ void SyntaxTest::filterObtainedErrors()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_errorList.emplace_back(SyntaxTestError{
|
m_errorList.emplace_back(SyntaxTestError{
|
||||||
Error::formatErrorType(currentError->type()),
|
currentError->type(),
|
||||||
currentError->errorId(),
|
currentError->errorId(),
|
||||||
errorMessage(*currentError),
|
errorMessage(*currentError),
|
||||||
sourceName,
|
sourceName,
|
||||||
|
@ -61,7 +61,7 @@ void SyntaxTest::parseAndAnalyze()
|
|||||||
}
|
}
|
||||||
|
|
||||||
m_errorList.emplace_back(SyntaxTestError{
|
m_errorList.emplace_back(SyntaxTestError{
|
||||||
Error::formatErrorType(error->type()),
|
error->type(),
|
||||||
error->errorId(),
|
error->errorId(),
|
||||||
errorMessage(*error),
|
errorMessage(*error),
|
||||||
name,
|
name,
|
||||||
|
Loading…
Reference in New Issue
Block a user