Skip to content

Commit eafd721

Browse files
refactored struct message to use std::variant for _typeOrSeverity
1 parent c8011d8 commit eafd721

File tree

12 files changed

+80
-73
lines changed

12 files changed

+80
-73
lines changed

liblangutil/Exceptions.h

+15
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <utility>
3838
#include <vector>
3939
#include <memory>
40+
#include <variant>
4041

4142
namespace solidity::langutil
4243
{
@@ -228,6 +229,13 @@ class Error: virtual public util::Exception
228229
}
229230
}
230231

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);
237+
}
238+
231239
static bool isError(Severity _severity)
232240
{
233241
return _severity == Severity::Error;
@@ -282,6 +290,13 @@ class Error: virtual public util::Exception
282290
util::unreachable();
283291
}
284292

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));
298+
}
299+
285300
static std::string formatErrorSeverityLowercase(Severity _severity)
286301
{
287302
std::string severityValue = formatErrorSeverity(_severity);

liblangutil/SourceReferenceExtractor.cpp

+6-4
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-
Error::Type _type
34+
std::variant<Error::Type, Error::Severity> _typeOrSeverity
3435
)
3536
{
3637
SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
@@ -44,15 +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), _type, 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-
Message message = extract(_charStreamProvider, _error, _error.type());
57+
Message message = extract(_charStreamProvider, static_cast<util::Exception>(_error), _typeOrSeverity);
5658
message.errorId = _error.errorId();
5759
return message;
5860
}

liblangutil/SourceReferenceExtractor.h

+12-2
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-
Error::Type type;
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, Error::Type _type);
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

+18-10
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;
@@ -175,16 +176,12 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref)
175176
}
176177
}
177178

178-
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg, bool _printFullType)
179+
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)
179180
{
180-
errorColored(Error::errorSeverity(_msg.type)) << (
181-
_printFullType ?
182-
Error::formatErrorType(_msg.type) :
183-
Error::formatErrorSeverity(Error::errorSeverity(_msg.type))
184-
);
181+
errorColored(Error::errorSeverityOrType(_msg._typeOrSeverity)) << Error::formatTypeOrSeverity(_msg._typeOrSeverity);
185182

186183
if (m_withErrorIds && _msg.errorId.has_value())
187-
errorColored(Error::errorSeverity(_msg.type)) << " (" << _msg.errorId.value().error << ")";
184+
errorColored(Error::errorSeverityOrType(_msg._typeOrSeverity)) << " (" << _msg.errorId.value().error << ")";
188185

189186
messageColored() << ": " << _msg.primary.message << '\n';
190187
printSourceLocation(_msg.primary);
@@ -199,9 +196,14 @@ void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtracto
199196
m_stream << '\n';
200197
}
201198

202-
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, Error::Type _type, bool _printFullType)
199+
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, Error::Type _type)
203200
{
204-
printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _type), _printFullType);
201+
printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _type));
202+
}
203+
204+
void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, Error::Severity _severity)
205+
{
206+
printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _severity));
205207
}
206208

207209
void SourceReferenceFormatter::printErrorInformation(ErrorList const& _errors)
@@ -212,5 +214,11 @@ void SourceReferenceFormatter::printErrorInformation(ErrorList const& _errors)
212214

213215
void SourceReferenceFormatter::printErrorInformation(Error const& _error)
214216
{
215-
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);
216224
}

liblangutil/SourceReferenceFormatter.h

+20-6
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ class SourceReferenceFormatter
5151

5252
/// Prints source location if it is given.
5353
void printSourceLocation(SourceReference const& _ref);
54-
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg, bool _printFullType=false);
55-
void printExceptionInformation(util::Exception const& _exception, Error::Type _type, bool _printFullType=false);
54+
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);
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

@@ -61,13 +62,26 @@ class SourceReferenceFormatter
6162
Error::Type _type,
6263
CharStreamProvider const& _charStreamProvider,
6364
bool _colored = false,
64-
bool _withErrorIds = false,
65-
bool _printFullType = false
65+
bool _withErrorIds = false
6666
)
6767
{
6868
std::ostringstream errorOutput;
6969
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds);
70-
formatter.printExceptionInformation(_exception, _type, _printFullType);
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);
7185
return errorOutput.str();
7286
}
7387

@@ -78,7 +92,7 @@ class SourceReferenceFormatter
7892
{
7993
return formatExceptionInformation(
8094
_error,
81-
_error.type(),
95+
Error::errorSeverity(_error.type()),
8296
_charStreamProvider
8397
);
8498
}

libsolidity/interface/StandardCompiler.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ Json::Value formatErrorWithException(
126126
_type,
127127
_charStreamProvider,
128128
false, // colored
129-
false, // _withErrorIds
130-
true // _printFullType
129+
false // _withErrorIds
131130
);
132131

133132
if (string const* description = _exception.comment())

solc/CommandLineInterface.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,10 @@ void CommandLineInterface::compile()
765765
catch (CompilerError const& _exception)
766766
{
767767
m_hasOutput = true;
768-
formatter.printExceptionInformation(_exception, Error::Type::CompilerError);
768+
formatter.printExceptionInformation(
769+
_exception,
770+
Error::errorSeverity(Error::Type::CompilerError)
771+
);
769772
solThrow(CommandLineExecutionError, "");
770773
}
771774
catch (Error const& _error)
@@ -778,7 +781,7 @@ void CommandLineInterface::compile()
778781
else
779782
{
780783
m_hasOutput = true;
781-
formatter.printExceptionInformation(_error, _error.type());
784+
formatter.printExceptionInformation(_error, Error::errorSeverity(_error.type()));
782785
solThrow(CommandLineExecutionError, "");
783786
}
784787
}

test/cmdlineTests/standard_urls_existing_and_missing/args

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,18 +1 @@
1-
{
2-
"errors":
3-
[
4-
{
5-
"component": "general",
6-
"formattedMessage": "Cannot import url (\"standard_urls_existing_and_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".",
7-
"message": "Cannot import url (\"standard_urls_existing_and_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".",
8-
"severity": "warning",
9-
"type": "Warning"
10-
}],
11-
"sources":
12-
{
13-
"url_not_found.sol":
14-
{
15-
"id": 0
16-
}
17-
}
18-
}
1+
{"errors":[{"component":"general","formattedMessage":"Cannot import url (\"standard_urls_existing_and_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".","message":"Cannot import url (\"standard_urls_existing_and_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".","severity":"warning","type":"Warning"}],"sources":{"url_not_found.sol":{"id":0}}}

test/cmdlineTests/standard_urls_missing/args

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,26 +1 @@
1-
{
2-
"errors":
3-
[
4-
{
5-
"component": "general",
6-
"formattedMessage": "Cannot import url (\"standard_urls_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".",
7-
"message": "Cannot import url (\"standard_urls_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".",
8-
"severity": "error",
9-
"type": "IOError"
10-
},
11-
{
12-
"component": "general",
13-
"formattedMessage": "Cannot import url (\"standard_urls_missing/non-existent-contract.sol\"): File not found. Searched the following locations: \"\".",
14-
"message": "Cannot import url (\"standard_urls_missing/non-existent-contract.sol\"): File not found. Searched the following locations: \"\".",
15-
"severity": "error",
16-
"type": "IOError"
17-
},
18-
{
19-
"component": "general",
20-
"formattedMessage": "Cannot import url (\"standard_urls_missing/non-existent-contract-2.sol\"): File not found. Searched the following locations: \"\".",
21-
"message": "Cannot import url (\"standard_urls_missing/non-existent-contract-2.sol\"): File not found. Searched the following locations: \"\".",
22-
"severity": "error",
23-
"type": "IOError"
24-
}],
25-
"sources": {}
26-
}
1+
{"errors":[{"component":"general","formattedMessage":"Cannot import url (\"standard_urls_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".","message":"Cannot import url (\"standard_urls_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".","severity":"error","type":"IOError"},{"component":"general","formattedMessage":"Cannot import url (\"standard_urls_missing/non-existent-contract.sol\"): File not found. Searched the following locations: \"\".","message":"Cannot import url (\"standard_urls_missing/non-existent-contract.sol\"): File not found. Searched the following locations: \"\".","severity":"error","type":"IOError"},{"component":"general","formattedMessage":"Cannot import url (\"standard_urls_missing/non-existent-contract-2.sol\"): File not found. Searched the following locations: \"\".","message":"Cannot import url (\"standard_urls_missing/non-existent-contract-2.sol\"): File not found. Searched the following locations: \"\".","severity":"error","type":"IOError"}],"sources":{}}

test/tools/ossfuzz/yulProto_diff_ossfuzz.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ DEFINE_PROTO_FUZZER(Program const& _input)
7979
SourceReferenceFormatter formatter(std::cout, stack, false, false);
8080

8181
for (auto const& error: stack.errors())
82-
formatter.printExceptionInformation(*error, error->type());
82+
formatter.printExceptionInformation(*error, Error::errorSeverity(error->type()));
8383
yulAssert(false, "Proto fuzzer generated malformed program");
8484
}
8585

0 commit comments

Comments
 (0)