Skip to content

Commit 37597f9

Browse files
Merge pull request #13162 from ethereum/cleanup_helpers_around_errors
cleaning up helpers around errors
2 parents db571ad + eafd721 commit 37597f9

File tree

19 files changed

+273
-226
lines changed

19 files changed

+273
-226
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: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
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>
3738
#include <vector>
3839
#include <memory>
40+
#include <variant>
3941

4042
namespace solidity::langutil
4143
{
@@ -174,6 +176,15 @@ class Error: virtual public util::Exception
174176
ParserError,
175177
TypeError,
176178
SyntaxError,
179+
IOError,
180+
FatalError,
181+
JSONError,
182+
InternalCompilerError,
183+
CompilerError,
184+
Exception,
185+
UnimplementedFeatureError,
186+
YulException,
187+
SMTLogicException,
177188
Warning,
178189
Info
179190
};
@@ -195,7 +206,6 @@ class Error: virtual public util::Exception
195206

196207
ErrorId errorId() const { return m_errorId; }
197208
Type type() const { return m_type; }
198-
std::string const& typeName() const { return m_typeName; }
199209

200210
SourceLocation const* sourceLocation() const noexcept;
201211
SecondarySourceLocation const* secondarySourceLocation() const noexcept;
@@ -211,11 +221,19 @@ class Error: virtual public util::Exception
211221

212222
static constexpr Severity errorSeverity(Type _type)
213223
{
214-
if (_type == Type::Info)
215-
return Severity::Info;
216-
if (_type == Type::Warning)
217-
return Severity::Warning;
218-
return Severity::Error;
224+
switch (_type)
225+
{
226+
case Type::Info: return Severity::Info;
227+
case Type::Warning: return Severity::Warning;
228+
default: return Severity::Error;
229+
}
230+
}
231+
232+
static constexpr Severity errorSeverityOrType(std::variant<Error::Type, Error::Severity> _typeOrSeverity)
233+
{
234+
if (std::holds_alternative<Error::Type>(_typeOrSeverity))
235+
return errorSeverity(std::get<Error::Type>(_typeOrSeverity));
236+
return std::get<Error::Severity>(_typeOrSeverity);
219237
}
220238

221239
static bool isError(Severity _severity)
@@ -238,35 +256,57 @@ class Error: virtual public util::Exception
238256

239257
static std::string formatErrorSeverity(Severity _severity)
240258
{
241-
if (_severity == Severity::Info)
242-
return "Info";
243-
if (_severity == Severity::Warning)
244-
return "Warning";
245-
solAssert(isError(_severity), "");
246-
return "Error";
259+
switch (_severity)
260+
{
261+
case Severity::Info: return "Info";
262+
case Severity::Warning: return "Warning";
263+
case Severity::Error: return "Error";
264+
}
265+
util::unreachable();
247266
}
248267

249-
static std::string formatErrorSeverityLowercase(Severity _severity)
268+
static std::string formatErrorType(Type _type)
250269
{
251-
switch (_severity)
270+
switch (_type)
252271
{
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";
272+
case Type::IOError: return "IOError";
273+
case Type::FatalError: return "FatalError";
274+
case Type::JSONError: return "JSONError";
275+
case Type::InternalCompilerError: return "InternalCompilerError";
276+
case Type::CompilerError: return "CompilerError";
277+
case Type::Exception: return "Exception";
278+
case Type::CodeGenerationError: return "CodeGenerationError";
279+
case Type::DeclarationError: return "DeclarationError";
280+
case Type::DocstringParsingError: return "DocstringParsingError";
281+
case Type::ParserError: return "ParserError";
282+
case Type::SyntaxError: return "SyntaxError";
283+
case Type::TypeError: return "TypeError";
284+
case Type::UnimplementedFeatureError: return "UnimplementedFeatureError";
285+
case Type::YulException: return "YulException";
286+
case Type::SMTLogicException: return "SMTLogicException";
287+
case Type::Warning: return "Warning";
288+
case Type::Info: return "Info";
260289
}
261-
solAssert(false, "");
290+
util::unreachable();
291+
}
292+
293+
static std::string formatTypeOrSeverity(std::variant<Error::Type, Error::Severity> _typeOrSeverity)
294+
{
295+
if (std::holds_alternative<Error::Type>(_typeOrSeverity))
296+
return formatErrorType(std::get<Error::Type>(_typeOrSeverity));
297+
return formatErrorSeverity(std::get<Error::Severity>(_typeOrSeverity));
262298
}
263299

264-
static std::optional<Severity> severityFromString(std::string _input);
300+
static std::string formatErrorSeverityLowercase(Severity _severity)
301+
{
302+
std::string severityValue = formatErrorSeverity(_severity);
303+
boost::algorithm::to_lower(severityValue);
304+
return severityValue;
305+
}
265306

266307
private:
267308
ErrorId m_errorId;
268309
Type m_type;
269-
std::string m_typeName;
270310
};
271311

272312
}

liblangutil/SourceReferenceExtractor.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <algorithm>
2424
#include <cmath>
25+
#include <variant>
2526

2627
using namespace std;
2728
using namespace solidity;
@@ -30,7 +31,7 @@ using namespace solidity::langutil;
3031
SourceReferenceExtractor::Message SourceReferenceExtractor::extract(
3132
CharStreamProvider const& _charStreamProvider,
3233
util::Exception const& _exception,
33-
string _severity
34+
std::variant<Error::Type, Error::Severity> _typeOrSeverity
3435
)
3536
{
3637
SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
@@ -44,16 +45,16 @@ SourceReferenceExtractor::Message SourceReferenceExtractor::extract(
4445
for (auto const& info: secondaryLocation->infos)
4546
secondary.emplace_back(extract(_charStreamProvider, &info.second, info.first));
4647

47-
return Message{std::move(primary), _severity, std::move(secondary), nullopt};
48+
return Message{std::move(primary), _typeOrSeverity, std::move(secondary), nullopt};
4849
}
4950

5051
SourceReferenceExtractor::Message SourceReferenceExtractor::extract(
5152
CharStreamProvider const& _charStreamProvider,
52-
Error const& _error
53+
Error const& _error,
54+
std::variant<Error::Type, Error::Severity> _typeOrSeverity
5355
)
5456
{
55-
string severity = Error::formatErrorSeverity(Error::errorSeverity(_error.type()));
56-
Message message = extract(_charStreamProvider, _error, severity);
57+
Message message = extract(_charStreamProvider, static_cast<util::Exception>(_error), _typeOrSeverity);
5758
message.errorId = _error.errorId();
5859
return message;
5960
}

liblangutil/SourceReferenceExtractor.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string>
2525
#include <tuple>
2626
#include <vector>
27+
#include <variant>
2728

2829
namespace solidity::langutil
2930
{
@@ -55,12 +56,21 @@ namespace SourceReferenceExtractor
5556
struct Message
5657
{
5758
SourceReference primary;
58-
std::string severity; // "Error", "Warning", "Info", ...
59+
std::variant<Error::Type, Error::Severity> _typeOrSeverity;
5960
std::vector<SourceReference> secondary;
6061
std::optional<ErrorId> errorId;
6162
};
6263

63-
Message extract(CharStreamProvider const& _charStreamProvider, util::Exception const& _exception, std::string _severity);
64+
Message extract(
65+
CharStreamProvider const& _charStreamProvider,
66+
util::Exception const& _exception,
67+
std::variant<Error::Type, Error::Severity> _typeOrSeverity
68+
);
69+
Message extract(
70+
CharStreamProvider const& _charStreamProvider,
71+
Error const& _error,
72+
std::variant<Error::Type, Error::Severity> _typeOrSeverity
73+
);
6474
Message extract(CharStreamProvider const& _charStreamProvider, Error const& _error);
6575
SourceReference extract(CharStreamProvider const& _charStreamProvider, SourceLocation const* _location, std::string message = "");
6676
}

liblangutil/SourceReferenceFormatter.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <libsolutil/UTF8.h>
2727
#include <iomanip>
2828
#include <string_view>
29+
#include <variant>
2930

3031
using namespace std;
3132
using namespace solidity;
@@ -65,19 +66,18 @@ AnsiColorized SourceReferenceFormatter::frameColored() const
6566
return AnsiColorized(m_stream, m_colored, {BOLD, BLUE});
6667
}
6768

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

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-
}
75+
switch (_severity)
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+
}
8181

8282
return AnsiColorized(m_stream, m_colored, {BOLD, textColor});
8383
}
@@ -178,13 +178,12 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref)
178178

179179
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)
180180
{
181-
// exception header line
182-
optional<Error::Severity> severity = Error::severityFromString(_msg.severity);
183-
errorColored(severity) << _msg.severity;
181+
errorColored(Error::errorSeverityOrType(_msg._typeOrSeverity)) << Error::formatTypeOrSeverity(_msg._typeOrSeverity);
182+
184183
if (m_withErrorIds && _msg.errorId.has_value())
185-
errorColored(severity) << " (" << _msg.errorId.value().error << ")";
186-
messageColored() << ": " << _msg.primary.message << '\n';
184+
errorColored(Error::errorSeverityOrType(_msg._typeOrSeverity)) << " (" << _msg.errorId.value().error << ")";
187185

186+
messageColored() << ": " << _msg.primary.message << '\n';
188187
printSourceLocation(_msg.primary);
189188

190189
for (auto const& secondary: _msg.secondary)
@@ -197,7 +196,12 @@ void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtracto
197196
m_stream << '\n';
198197
}
199198

200-
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, std::string const& _severity)
199+
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, Error::Type _type)
200+
{
201+
printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _type));
202+
}
203+
204+
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, Error::Severity _severity)
201205
{
202206
printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _severity));
203207
}
@@ -210,5 +214,11 @@ void SourceReferenceFormatter::printErrorInformation(ErrorList const& _errors)
210214

211215
void SourceReferenceFormatter::printErrorInformation(Error const& _error)
212216
{
213-
printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _error));
217+
SourceReferenceExtractor::Message message =
218+
SourceReferenceExtractor::extract(
219+
m_charStreamProvider,
220+
_error,
221+
Error::errorSeverity(_error.type())
222+
);
223+
printExceptionInformation(message);
214224
}

liblangutil/SourceReferenceFormatter.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,36 @@ class SourceReferenceFormatter
5252
/// Prints source location if it is given.
5353
void printSourceLocation(SourceReference const& _ref);
5454
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);
55-
void printExceptionInformation(util::Exception const& _exception, std::string const& _severity);
55+
void printExceptionInformation(util::Exception const& _exception, Error::Type _type);
56+
void printExceptionInformation(util::Exception const& _exception, Error::Severity _severity);
5657
void printErrorInformation(langutil::ErrorList const& _errors);
5758
void printErrorInformation(Error const& _error);
5859

5960
static std::string formatExceptionInformation(
6061
util::Exception const& _exception,
61-
std::string const& _name,
62+
Error::Type _type,
6263
CharStreamProvider const& _charStreamProvider,
6364
bool _colored = false,
6465
bool _withErrorIds = false
6566
)
6667
{
6768
std::ostringstream errorOutput;
6869
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds);
69-
formatter.printExceptionInformation(_exception, _name);
70+
formatter.printExceptionInformation(_exception, _type);
71+
return errorOutput.str();
72+
}
73+
74+
static std::string formatExceptionInformation(
75+
util::Exception const& _exception,
76+
Error::Severity _severity,
77+
CharStreamProvider const& _charStreamProvider,
78+
bool _colored = false,
79+
bool _withErrorIds = false
80+
)
81+
{
82+
std::ostringstream errorOutput;
83+
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds);
84+
formatter.printExceptionInformation(_exception, _severity);
7085
return errorOutput.str();
7186
}
7287

@@ -77,7 +92,7 @@ class SourceReferenceFormatter
7792
{
7893
return formatExceptionInformation(
7994
_error,
80-
Error::formatErrorSeverity(Error::errorSeverity(_error.type())),
95+
Error::errorSeverity(_error.type()),
8196
_charStreamProvider
8297
);
8398
}
@@ -87,7 +102,7 @@ class SourceReferenceFormatter
87102
private:
88103
util::AnsiColorized normalColored() const;
89104
util::AnsiColorized frameColored() const;
90-
util::AnsiColorized errorColored(std::optional<langutil::Error::Severity> _severity) const;
105+
util::AnsiColorized errorColored(langutil::Error::Severity _severity) const;
91106
util::AnsiColorized messageColored() const;
92107
util::AnsiColorized secondaryColored() const;
93108
util::AnsiColorized highlightColored() const;

0 commit comments

Comments
 (0)