Skip to content

[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

Merged
merged 2 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Language Features:

Compiler Features:
* C API (``libsolc`` / raw ``soljson.js``): Introduce ``solidity_free`` method which releases all internal buffers to save memory.

* Commandline interface: Adds new option ``--new-reporter`` for improved diagnostics formatting
along with ``--color`` and ``--no-color`` for colorized output to be forced (or explicitly disabled).

Bugfixes:

Expand Down
91 changes: 91 additions & 0 deletions libdevcore/AnsiColorized.h
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;
};

}
1 change: 1 addition & 0 deletions libdevcore/CMakeLists.txt
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
Expand Down
2 changes: 2 additions & 0 deletions liblangutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ set(sources
SourceReferenceExtractor.h
SourceReferenceFormatter.cpp
SourceReferenceFormatter.h
SourceReferenceFormatterHuman.cpp
SourceReferenceFormatterHuman.h
Token.cpp
Token.h
UndefMacros.h
Expand Down
13 changes: 8 additions & 5 deletions liblangutil/SourceReferenceFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ class SourceReferenceFormatter
m_stream(_stream)
{}

virtual ~SourceReferenceFormatter() = default;

/// Prints source location if it is given.
void printSourceLocation(SourceLocation const* _location);
void printSourceLocation(SourceReference const& _ref);
void printExceptionInformation(dev::Exception const& _error, std::string const& _category);
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);
virtual void printSourceLocation(SourceReference const& _ref);
virtual void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);

virtual void printSourceLocation(SourceLocation const* _location);
virtual void printExceptionInformation(dev::Exception const& _error, std::string const& _category);

static std::string formatExceptionInformation(
dev::Exception const& _exception,
Expand All @@ -62,7 +65,7 @@ class SourceReferenceFormatter
return errorOutput.str();
}

private:
protected:
/// Prints source name if location is given.
void printSourceName(SourceReference const& _ref);

Expand Down
136 changes: 136 additions & 0 deletions liblangutil/SourceReferenceFormatterHuman.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' : ' '); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! We might even have a bug about this.

Copy link
Member Author

Choose a reason for hiding this comment

The 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';
}
80 changes: 80 additions & 0 deletions liblangutil/SourceReferenceFormatterHuman.h
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();
}

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;
};

}
Loading