-
Notifications
You must be signed in to change notification settings - Fork 6.1k
[solc] colorized diagnostics output #5511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
This file is part of solidity. | ||
|
||
solidity is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
solidity is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with solidity. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <ostream> | ||
#include <vector> | ||
|
||
namespace dev | ||
{ | ||
|
||
namespace formatting | ||
{ | ||
|
||
// control codes | ||
static constexpr char const* RESET = "\033[0m"; | ||
static constexpr char const* INVERSE = "\033[7m"; | ||
static constexpr char const* BOLD = "\033[1m"; | ||
static constexpr char const* BRIGHT = BOLD; | ||
|
||
// standard foreground colors | ||
static constexpr char const* BLACK = "\033[30m"; | ||
static constexpr char const* RED = "\033[31m"; | ||
static constexpr char const* GREEN = "\033[32m"; | ||
static constexpr char const* YELLOW = "\033[33m"; | ||
static constexpr char const* BLUE = "\033[34m"; | ||
static constexpr char const* MAGENTA = "\033[35m"; | ||
static constexpr char const* CYAN = "\033[36m"; | ||
static constexpr char const* WHITE = "\033[37m"; | ||
|
||
// standard background colors | ||
static constexpr char const* BLACK_BACKGROUND = "\033[40m"; | ||
static constexpr char const* RED_BACKGROUND = "\033[41m"; | ||
static constexpr char const* GREEN_BACKGROUND = "\033[42m"; | ||
static constexpr char const* YELLOW_BACKGROUND = "\033[43m"; | ||
static constexpr char const* BLUE_BACKGROUND = "\033[44m"; | ||
static constexpr char const* MAGENTA_BACKGROUND = "\033[45m"; | ||
static constexpr char const* CYAN_BACKGROUND = "\033[46m"; | ||
static constexpr char const* WHITE_BACKGROUND = "\033[47m"; | ||
|
||
// 256-bit-colors (incomplete set) | ||
static constexpr char const* RED_BACKGROUND_256 = "\033[48;5;160m"; | ||
static constexpr char const* ORANGE_BACKGROUND_256 = "\033[48;5;166m"; | ||
|
||
} | ||
|
||
/// AnsiColorized provides a convenience helper to colorize ostream with formatting-reset assured. | ||
class AnsiColorized | ||
{ | ||
public: | ||
AnsiColorized(std::ostream& _os, bool const _enabled, std::vector<char const*>&& _formatting): | ||
m_stream{_os}, m_enabled{_enabled}, m_codes{std::move(_formatting)} | ||
{ | ||
if (m_enabled) | ||
for (auto const& code: m_codes) | ||
m_stream << code; | ||
} | ||
|
||
~AnsiColorized() | ||
{ | ||
if (m_enabled) | ||
m_stream << formatting::RESET; | ||
} | ||
|
||
template <typename T> | ||
std::ostream& operator<<(T&& _t) | ||
{ | ||
return m_stream << std::forward<T>(_t); | ||
} | ||
|
||
private: | ||
std::ostream& m_stream; | ||
bool m_enabled; | ||
std::vector<char const*> m_codes; | ||
}; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
set(sources | ||
Algorithms.h | ||
AnsiColorized.h | ||
Assertions.h | ||
Common.h | ||
CommonData.cpp | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
This file is part of solidity. | ||
|
||
solidity is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
solidity is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with solidity. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/** | ||
* Formatting functions for errors referencing positions and locations in the source. | ||
*/ | ||
|
||
#include <liblangutil/SourceReferenceFormatterHuman.h> | ||
#include <liblangutil/Scanner.h> | ||
#include <liblangutil/Exceptions.h> | ||
#include <cmath> | ||
#include <iomanip> | ||
|
||
using namespace std; | ||
using namespace dev; | ||
using namespace dev::formatting; | ||
using namespace langutil; | ||
|
||
AnsiColorized SourceReferenceFormatterHuman::normalColored() const | ||
{ | ||
return AnsiColorized(m_stream, m_colored, {WHITE}); | ||
} | ||
|
||
AnsiColorized SourceReferenceFormatterHuman::frameColored() const | ||
{ | ||
return AnsiColorized(m_stream, m_colored, {BOLD, BLUE}); | ||
} | ||
|
||
AnsiColorized SourceReferenceFormatterHuman::errorColored() const | ||
{ | ||
return AnsiColorized(m_stream, m_colored, {BOLD, RED}); | ||
} | ||
|
||
AnsiColorized SourceReferenceFormatterHuman::messageColored() const | ||
{ | ||
return AnsiColorized(m_stream, m_colored, {BOLD, WHITE}); | ||
} | ||
|
||
AnsiColorized SourceReferenceFormatterHuman::secondaryColored() const | ||
{ | ||
return AnsiColorized(m_stream, m_colored, {BOLD, CYAN}); | ||
} | ||
|
||
AnsiColorized SourceReferenceFormatterHuman::highlightColored() const | ||
{ | ||
return AnsiColorized(m_stream, m_colored, {YELLOW}); | ||
} | ||
|
||
AnsiColorized SourceReferenceFormatterHuman::diagColored() const | ||
{ | ||
return AnsiColorized(m_stream, m_colored, {BOLD, YELLOW}); | ||
} | ||
|
||
void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _ref) | ||
{ | ||
if (_ref.position.line < 0) | ||
return; // Nothing we can print here | ||
|
||
int const leftpad = static_cast<int>(log10(max(_ref.position.line, 1))) + 1; | ||
|
||
// line 0: source name | ||
frameColored() << string(leftpad, ' ') << "--> "; | ||
m_stream << _ref.sourceName << ":" << (_ref.position.line + 1) << ":" << (_ref.position.column + 1) << ": " << '\n'; | ||
|
||
if (!_ref.multiline) | ||
{ | ||
int const locationLength = _ref.endColumn - _ref.startColumn; | ||
|
||
// line 1: | ||
m_stream << string(leftpad, ' '); | ||
frameColored() << " |" << '\n'; | ||
|
||
// line 2: | ||
frameColored() << (_ref.position.line + 1) << " | "; | ||
m_stream << _ref.text.substr(0, _ref.startColumn); | ||
highlightColored() << _ref.text.substr(_ref.startColumn, locationLength); | ||
m_stream << _ref.text.substr(_ref.endColumn) << '\n'; | ||
|
||
// line 3: | ||
m_stream << string(leftpad, ' '); | ||
frameColored() << " | "; | ||
for_each( | ||
_ref.text.cbegin(), | ||
_ref.text.cbegin() + _ref.startColumn, | ||
[this](char ch) { m_stream << (ch == '\t' ? '\t' : ' '); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! We might even have a bug about this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I too actually held back for a moment on that line, but said if that's a bug, then it was there before already, and I'd prefer to address it in a separate PR (as the base class is affected then, too). |
||
); | ||
diagColored() << string(locationLength, '^') << '\n'; | ||
} | ||
else | ||
{ | ||
// line 1: | ||
m_stream << string(leftpad, ' '); | ||
frameColored() << " |" << '\n'; | ||
|
||
// line 2: | ||
frameColored() << (_ref.position.line + 1) << " | "; | ||
m_stream << _ref.text.substr(0, _ref.startColumn); | ||
highlightColored() << _ref.text.substr(_ref.startColumn) << '\n'; | ||
|
||
// line 3: | ||
frameColored() << string(leftpad, ' ') << " | "; | ||
m_stream << string(_ref.startColumn, ' '); | ||
diagColored() << "^ (Relevant source part starts here and spans across multiple lines).\n"; | ||
} | ||
} | ||
|
||
void SourceReferenceFormatterHuman::printExceptionInformation(SourceReferenceExtractor::Message const& _msg) | ||
{ | ||
// exception header line | ||
errorColored() << _msg.category; | ||
messageColored() << ": " << _msg.primary.message << '\n'; | ||
|
||
printSourceLocation(_msg.primary); | ||
|
||
for (auto const& secondary: _msg.secondary) | ||
{ | ||
secondaryColored() << "Note"; | ||
messageColored() << ": " << secondary.message << '\n'; | ||
printSourceLocation(secondary); | ||
} | ||
|
||
m_stream << '\n'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
This file is part of solidity. | ||
|
||
solidity is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
solidity is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with solidity. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/** | ||
* Formatting functions for errors referencing positions and locations in the source. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <liblangutil/SourceReferenceExtractor.h> | ||
#include <liblangutil/SourceReferenceFormatter.h> // SourceReferenceFormatterBase | ||
|
||
#include <libdevcore/AnsiColorized.h> | ||
|
||
#include <ostream> | ||
#include <sstream> | ||
#include <functional> | ||
|
||
namespace dev | ||
{ | ||
struct Exception; // forward | ||
} | ||
|
||
namespace langutil | ||
{ | ||
|
||
struct SourceLocation; | ||
struct SourceReference; | ||
|
||
class SourceReferenceFormatterHuman: public SourceReferenceFormatter | ||
{ | ||
public: | ||
SourceReferenceFormatterHuman(std::ostream& _stream, bool colored): | ||
SourceReferenceFormatter{_stream}, m_colored{colored} | ||
{} | ||
|
||
void printSourceLocation(SourceReference const& _ref) override; | ||
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg) override; | ||
using SourceReferenceFormatter::printExceptionInformation; | ||
|
||
static std::string formatExceptionInformation( | ||
dev::Exception const& _exception, | ||
std::string const& _name, | ||
bool colored = false | ||
) | ||
{ | ||
std::ostringstream errorOutput; | ||
|
||
SourceReferenceFormatterHuman formatter(errorOutput, colored); | ||
formatter.printExceptionInformation(_exception, _name); | ||
return errorOutput.str(); | ||
christianparpart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private: | ||
dev::AnsiColorized normalColored() const; | ||
dev::AnsiColorized frameColored() const; | ||
dev::AnsiColorized errorColored() const; | ||
dev::AnsiColorized messageColored() const; | ||
dev::AnsiColorized secondaryColored() const; | ||
dev::AnsiColorized highlightColored() const; | ||
dev::AnsiColorized diagColored() const; | ||
|
||
private: | ||
bool m_colored; | ||
}; | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.