Skip to content

Commit 1da57d5

Browse files
authored
Merge pull request #11970 from shikharvashistha/colors
Added different colors for warning & error
2 parents 8460a65 + cf00536 commit 1da57d5

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Language Features:
77
Compiler Features:
88
* Commandline Interface: Accept nested brackets in step sequences passed to ``--yul-optimizations``.
99
* Commandline Interface: Add ``--debug-info`` option for selecting how much extra debug information should be included in the produced EVM assembly and Yul code.
10+
* Commandline Interface: Use different colors when printing errors, warnings and infos.
1011
* SMTChecker: Output values for ``block.*``, ``msg.*`` and ``tx.*`` variables that are present in the called functions.
1112
* Standard JSON: Accept nested brackets in step sequences passed to ``settings.optimizer.details.yulDetails.optimizerSteps``.
1213
* Standard JSON: Add ``settings.debug.debugInfo`` option for selecting how much extra debug information should be included in the produced EVM assembly and Yul code.

liblangutil/Exceptions.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
#include <liblangutil/Exceptions.h>
2525

26+
#include <boost/algorithm/string/case_conv.hpp>
27+
#include <boost/algorithm/string/trim.hpp>
28+
2629
using namespace std;
2730
using namespace solidity;
2831
using namespace solidity::langutil;
@@ -71,3 +74,15 @@ Error::Error(
7174
if (!_description.empty())
7275
*this << util::errinfo_comment(_description);
7376
}
77+
78+
optional<Error::Severity> Error::severityFromString(string _input)
79+
{
80+
boost::algorithm::to_lower(_input);
81+
boost::algorithm::trim(_input);
82+
83+
for (Severity severity: {Severity::Error, Severity::Warning, Severity::Info})
84+
if (_input == formatErrorSeverityLowercase(severity))
85+
return severity;
86+
87+
return nullopt;
88+
}

liblangutil/Exceptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ class Error: virtual public util::Exception
258258
solAssert(false, "");
259259
}
260260

261+
static std::optional<Severity> severityFromString(std::string _input);
262+
261263
private:
262264
ErrorId m_errorId;
263265
Type m_type;

liblangutil/SourceReferenceFormatter.cpp

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

68-
AnsiColorized SourceReferenceFormatter::errorColored() const
68+
AnsiColorized SourceReferenceFormatter::errorColored(optional<Error::Severity> _severity) const
6969
{
70-
return AnsiColorized(m_stream, m_colored, {BOLD, RED});
70+
// We used to color messages of any severity as errors so this seems like a good default
71+
// for cases where severity cannot be determined.
72+
char const* textColor = RED;
73+
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+
}
81+
82+
return AnsiColorized(m_stream, m_colored, {BOLD, textColor});
7183
}
7284

7385
AnsiColorized SourceReferenceFormatter::messageColored() const
@@ -164,9 +176,10 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref)
164176
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)
165177
{
166178
// exception header line
167-
errorColored() << _msg.severity;
179+
optional<Error::Severity> severity = Error::severityFromString(_msg.severity);
180+
errorColored(severity) << _msg.severity;
168181
if (m_withErrorIds && _msg.errorId.has_value())
169-
errorColored() << " (" << _msg.errorId.value().error << ")";
182+
errorColored(severity) << " (" << _msg.errorId.value().error << ")";
170183
messageColored() << ": " << _msg.primary.message << '\n';
171184

172185
printSourceLocation(_msg.primary);

liblangutil/SourceReferenceFormatter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class SourceReferenceFormatter
8787
private:
8888
util::AnsiColorized normalColored() const;
8989
util::AnsiColorized frameColored() const;
90-
util::AnsiColorized errorColored() const;
90+
util::AnsiColorized errorColored(std::optional<langutil::Error::Severity> _severity) const;
9191
util::AnsiColorized messageColored() const;
9292
util::AnsiColorized secondaryColored() const;
9393
util::AnsiColorized highlightColored() const;

0 commit comments

Comments
 (0)