Skip to content

Commit c8011d8

Browse files
Cleaning up helpers around errors
1 parent 1fbee82 commit c8011d8

File tree

21 files changed

+268
-228
lines changed

21 files changed

+268
-228
lines changed

liblangutil/Exceptions.cpp

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,6 @@ Error::Error(
3939
m_errorId(_errorId),
4040
m_type(_type)
4141
{
42-
switch (m_type)
43-
{
44-
case Type::CodeGenerationError:
45-
m_typeName = "CodeGenerationError";
46-
break;
47-
case Type::DeclarationError:
48-
m_typeName = "DeclarationError";
49-
break;
50-
case Type::DocstringParsingError:
51-
m_typeName = "DocstringParsingError";
52-
break;
53-
case Type::Info:
54-
m_typeName = "Info";
55-
break;
56-
case Type::ParserError:
57-
m_typeName = "ParserError";
58-
break;
59-
case Type::SyntaxError:
60-
m_typeName = "SyntaxError";
61-
break;
62-
case Type::TypeError:
63-
m_typeName = "TypeError";
64-
break;
65-
case Type::Warning:
66-
m_typeName = "Warning";
67-
break;
68-
}
69-
7042
if (_location.isValid())
7143
*this << errinfo_sourceLocation(_location);
7244
if (!_secondaryLocation.infos.empty())
@@ -84,15 +56,3 @@ SecondarySourceLocation const* Error::secondarySourceLocation() const noexcept
8456
{
8557
return boost::get_error_info<errinfo_secondarySourceLocation>(*this);
8658
}
87-
88-
optional<Error::Severity> Error::severityFromString(string _input)
89-
{
90-
boost::algorithm::to_lower(_input);
91-
boost::algorithm::trim(_input);
92-
93-
for (Severity severity: {Severity::Error, Severity::Warning, Severity::Info})
94-
if (_input == formatErrorSeverityLowercase(severity))
95-
return severity;
96-
97-
return nullopt;
98-
}

liblangutil/Exceptions.h

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <boost/preprocessor/cat.hpp>
3232
#include <boost/preprocessor/facilities/empty.hpp>
3333
#include <boost/preprocessor/facilities/overload.hpp>
34+
#include <boost/algorithm/string/case_conv.hpp>
3435

3536
#include <string>
3637
#include <utility>
@@ -174,6 +175,15 @@ class Error: virtual public util::Exception
174175
ParserError,
175176
TypeError,
176177
SyntaxError,
178+
IOError,
179+
FatalError,
180+
JSONError,
181+
InternalCompilerError,
182+
CompilerError,
183+
Exception,
184+
UnimplementedFeatureError,
185+
YulException,
186+
SMTLogicException,
177187
Warning,
178188
Info
179189
};
@@ -195,7 +205,6 @@ class Error: virtual public util::Exception
195205

196206
ErrorId errorId() const { return m_errorId; }
197207
Type type() const { return m_type; }
198-
std::string const& typeName() const { return m_typeName; }
199208

200209
SourceLocation const* sourceLocation() const noexcept;
201210
SecondarySourceLocation const* secondarySourceLocation() const noexcept;
@@ -211,11 +220,12 @@ class Error: virtual public util::Exception
211220

212221
static constexpr Severity errorSeverity(Type _type)
213222
{
214-
if (_type == Type::Info)
215-
return Severity::Info;
216-
if (_type == Type::Warning)
217-
return Severity::Warning;
218-
return Severity::Error;
223+
switch (_type)
224+
{
225+
case Type::Info: return Severity::Info;
226+
case Type::Warning: return Severity::Warning;
227+
default: return Severity::Error;
228+
}
219229
}
220230

221231
static bool isError(Severity _severity)
@@ -238,35 +248,50 @@ class Error: virtual public util::Exception
238248

239249
static std::string formatErrorSeverity(Severity _severity)
240250
{
241-
if (_severity == Severity::Info)
242-
return "Info";
243-
if (_severity == Severity::Warning)
244-
return "Warning";
245-
solAssert(isError(_severity), "");
246-
return "Error";
251+
switch (_severity)
252+
{
253+
case Severity::Info: return "Info";
254+
case Severity::Warning: return "Warning";
255+
case Severity::Error: return "Error";
256+
}
257+
util::unreachable();
247258
}
248259

249-
static std::string formatErrorSeverityLowercase(Severity _severity)
260+
static std::string formatErrorType(Type _type)
250261
{
251-
switch (_severity)
262+
switch (_type)
252263
{
253-
case Severity::Info:
254-
return "info";
255-
case Severity::Warning:
256-
return "warning";
257-
case Severity::Error:
258-
solAssert(isError(_severity), "");
259-
return "error";
264+
case Type::IOError: return "IOError";
265+
case Type::FatalError: return "FatalError";
266+
case Type::JSONError: return "JSONError";
267+
case Type::InternalCompilerError: return "InternalCompilerError";
268+
case Type::CompilerError: return "CompilerError";
269+
case Type::Exception: return "Exception";
270+
case Type::CodeGenerationError: return "CodeGenerationError";
271+
case Type::DeclarationError: return "DeclarationError";
272+
case Type::DocstringParsingError: return "DocstringParsingError";
273+
case Type::ParserError: return "ParserError";
274+
case Type::SyntaxError: return "SyntaxError";
275+
case Type::TypeError: return "TypeError";
276+
case Type::UnimplementedFeatureError: return "UnimplementedFeatureError";
277+
case Type::YulException: return "YulException";
278+
case Type::SMTLogicException: return "SMTLogicException";
279+
case Type::Warning: return "Warning";
280+
case Type::Info: return "Info";
260281
}
261-
solAssert(false, "");
282+
util::unreachable();
262283
}
263284

264-
static std::optional<Severity> severityFromString(std::string _input);
285+
static std::string formatErrorSeverityLowercase(Severity _severity)
286+
{
287+
std::string severityValue = formatErrorSeverity(_severity);
288+
boost::algorithm::to_lower(severityValue);
289+
return severityValue;
290+
}
265291

266292
private:
267293
ErrorId m_errorId;
268294
Type m_type;
269-
std::string m_typeName;
270295
};
271296

272297
}

liblangutil/SourceReferenceExtractor.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ using namespace solidity::langutil;
3030
SourceReferenceExtractor::Message SourceReferenceExtractor::extract(
3131
CharStreamProvider const& _charStreamProvider,
3232
util::Exception const& _exception,
33-
string _severity
33+
Error::Type _type
3434
)
3535
{
3636
SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
@@ -44,16 +44,15 @@ SourceReferenceExtractor::Message SourceReferenceExtractor::extract(
4444
for (auto const& info: secondaryLocation->infos)
4545
secondary.emplace_back(extract(_charStreamProvider, &info.second, info.first));
4646

47-
return Message{std::move(primary), _severity, std::move(secondary), nullopt};
47+
return Message{std::move(primary), _type, std::move(secondary), nullopt};
4848
}
4949

5050
SourceReferenceExtractor::Message SourceReferenceExtractor::extract(
5151
CharStreamProvider const& _charStreamProvider,
5252
Error const& _error
5353
)
5454
{
55-
string severity = Error::formatErrorSeverity(Error::errorSeverity(_error.type()));
56-
Message message = extract(_charStreamProvider, _error, severity);
55+
Message message = extract(_charStreamProvider, _error, _error.type());
5756
message.errorId = _error.errorId();
5857
return message;
5958
}

liblangutil/SourceReferenceExtractor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ namespace SourceReferenceExtractor
5555
struct Message
5656
{
5757
SourceReference primary;
58-
std::string severity; // "Error", "Warning", "Info", ...
58+
Error::Type type;
5959
std::vector<SourceReference> secondary;
6060
std::optional<ErrorId> errorId;
6161
};
6262

63-
Message extract(CharStreamProvider const& _charStreamProvider, util::Exception const& _exception, std::string _severity);
63+
Message extract(CharStreamProvider const& _charStreamProvider, util::Exception const& _exception, Error::Type _type);
6464
Message extract(CharStreamProvider const& _charStreamProvider, Error const& _error);
6565
SourceReference extract(CharStreamProvider const& _charStreamProvider, SourceLocation const* _location, std::string message = "");
6666
}

liblangutil/SourceReferenceFormatter.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,18 @@ AnsiColorized SourceReferenceFormatter::frameColored() const
6565
return AnsiColorized(m_stream, m_colored, {BOLD, BLUE});
6666
}
6767

68-
AnsiColorized SourceReferenceFormatter::errorColored(optional<Error::Severity> _severity) const
68+
AnsiColorized SourceReferenceFormatter::errorColored(Error::Severity _severity) const
6969
{
7070
// We used to color messages of any severity as errors so this seems like a good default
7171
// for cases where severity cannot be determined.
7272
char const* textColor = RED;
7373

74-
if (_severity.has_value())
75-
switch (_severity.value())
76-
{
77-
case Error::Severity::Error: textColor = RED; break;
78-
case Error::Severity::Warning: textColor = YELLOW; break;
79-
case Error::Severity::Info: textColor = WHITE; break;
80-
}
74+
switch (_severity)
75+
{
76+
case Error::Severity::Error: textColor = RED; break;
77+
case Error::Severity::Warning: textColor = YELLOW; break;
78+
case Error::Severity::Info: textColor = WHITE; break;
79+
}
8180

8281
return AnsiColorized(m_stream, m_colored, {BOLD, textColor});
8382
}
@@ -176,15 +175,18 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref)
176175
}
177176
}
178177

179-
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)
178+
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg, bool _printFullType)
180179
{
181-
// exception header line
182-
optional<Error::Severity> severity = Error::severityFromString(_msg.severity);
183-
errorColored(severity) << _msg.severity;
180+
errorColored(Error::errorSeverity(_msg.type)) << (
181+
_printFullType ?
182+
Error::formatErrorType(_msg.type) :
183+
Error::formatErrorSeverity(Error::errorSeverity(_msg.type))
184+
);
185+
184186
if (m_withErrorIds && _msg.errorId.has_value())
185-
errorColored(severity) << " (" << _msg.errorId.value().error << ")";
186-
messageColored() << ": " << _msg.primary.message << '\n';
187+
errorColored(Error::errorSeverity(_msg.type)) << " (" << _msg.errorId.value().error << ")";
187188

189+
messageColored() << ": " << _msg.primary.message << '\n';
188190
printSourceLocation(_msg.primary);
189191

190192
for (auto const& secondary: _msg.secondary)
@@ -197,9 +199,9 @@ void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtracto
197199
m_stream << '\n';
198200
}
199201

200-
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, std::string const& _severity)
202+
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, Error::Type _type, bool _printFullType)
201203
{
202-
printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _severity));
204+
printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _type), _printFullType);
203205
}
204206

205207
void SourceReferenceFormatter::printErrorInformation(ErrorList const& _errors)

liblangutil/SourceReferenceFormatter.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,23 @@ class SourceReferenceFormatter
5151

5252
/// Prints source location if it is given.
5353
void printSourceLocation(SourceReference const& _ref);
54-
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);
55-
void printExceptionInformation(util::Exception const& _exception, std::string const& _severity);
54+
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg, bool _printFullType=false);
55+
void printExceptionInformation(util::Exception const& _exception, Error::Type _type, bool _printFullType=false);
5656
void printErrorInformation(langutil::ErrorList const& _errors);
5757
void printErrorInformation(Error const& _error);
5858

5959
static std::string formatExceptionInformation(
6060
util::Exception const& _exception,
61-
std::string const& _name,
61+
Error::Type _type,
6262
CharStreamProvider const& _charStreamProvider,
6363
bool _colored = false,
64-
bool _withErrorIds = false
64+
bool _withErrorIds = false,
65+
bool _printFullType = false
6566
)
6667
{
6768
std::ostringstream errorOutput;
6869
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds);
69-
formatter.printExceptionInformation(_exception, _name);
70+
formatter.printExceptionInformation(_exception, _type, _printFullType);
7071
return errorOutput.str();
7172
}
7273

@@ -77,7 +78,7 @@ class SourceReferenceFormatter
7778
{
7879
return formatExceptionInformation(
7980
_error,
80-
Error::formatErrorSeverity(Error::errorSeverity(_error.type())),
81+
_error.type(),
8182
_charStreamProvider
8283
);
8384
}
@@ -87,7 +88,7 @@ class SourceReferenceFormatter
8788
private:
8889
util::AnsiColorized normalColored() const;
8990
util::AnsiColorized frameColored() const;
90-
util::AnsiColorized errorColored(std::optional<langutil::Error::Severity> _severity) const;
91+
util::AnsiColorized errorColored(langutil::Error::Severity _severity) const;
9192
util::AnsiColorized messageColored() const;
9293
util::AnsiColorized secondaryColored() const;
9394
util::AnsiColorized highlightColored() const;

0 commit comments

Comments
 (0)