Adds support for OSC-8 in error diagnostics, that is, you can click the file names in the error reports.

This is by default auto-detected, can be actively disabled (including auto-detection), or forcefully enabled.

A non-supporting terminal will silently ignore the generated hyperlink
anchor and just print the file name.
This commit is contained in:
Christian Parpart 2020-06-29 14:55:25 +02:00
parent 8fde9fd1c3
commit b1d8a1791b
10 changed files with 112 additions and 12 deletions

View File

@ -6,6 +6,7 @@ Language Features:
Compiler Features:
* NatSpec: Add fields "kind" and "version" to the JSON output.
* Commandline Interface: Prevent some incompatible commandline options from being used together.
* Commandline Interface: Adds new option ``--hyperlinks`` and ``--no-hyperlinks`` for clickable file names in diagnostics.
Bugfixes:

View File

@ -23,8 +23,10 @@
#include <liblangutil/Exceptions.h>
#include <libsolutil/UTF8.h>
#include <iomanip>
#include <iostream>
#include <string_view>
using namespace std;
using namespace solidity;
using namespace solidity::langutil;
@ -88,7 +90,7 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _
if (_ref.position.line < 0)
{
frameColored() << "-->";
m_stream << ' ' << _ref.sourceName << '\n';
m_stream << ' ' << m_hyperlink(_ref.sourceName) << '\n';
return; // No line available, nothing else to print
}
@ -98,7 +100,7 @@ void SourceReferenceFormatterHuman::printSourceLocation(SourceReference const& _
// line 0: source name
m_stream << leftpad;
frameColored() << "-->";
m_stream << ' ' << _ref.sourceName << ':' << line << ':' << (_ref.position.column + 1) << ":\n";
m_stream << ' ' << m_hyperlink(_ref.sourceName) << ':' << line << ':' << (_ref.position.column + 1) << ":\n";
string_view text = _ref.text;

View File

@ -24,6 +24,7 @@
#include <liblangutil/SourceReferenceFormatter.h> // SourceReferenceFormatterBase
#include <libsolutil/AnsiColorized.h>
#include <libsolutil/hyperlink.h>
#include <ostream>
#include <sstream>
@ -35,8 +36,8 @@ namespace solidity::langutil
class SourceReferenceFormatterHuman: public SourceReferenceFormatter
{
public:
SourceReferenceFormatterHuman(std::ostream& _stream, bool _colored, bool _withErrorIds):
SourceReferenceFormatter{_stream}, m_colored{_colored}, m_withErrorIds(_withErrorIds)
SourceReferenceFormatterHuman(std::ostream& _stream, bool _colored, bool _withErrorIds, bool _hyperlinks):
SourceReferenceFormatter{_stream}, m_colored{_colored}, m_withErrorIds(_withErrorIds), m_hyperlink{_hyperlinks}
{}
void printSourceLocation(SourceReference const& _ref) override;
@ -47,12 +48,13 @@ public:
util::Exception const& _exception,
std::string const& _name,
bool _colored = false,
bool _withErrorIds = false
bool _withErrorIds = false,
bool _hyperlinks = false
)
{
std::ostringstream errorOutput;
SourceReferenceFormatterHuman formatter(errorOutput, _colored, _withErrorIds);
SourceReferenceFormatterHuman formatter(errorOutput, _colored, _withErrorIds, _hyperlinks);
formatter.printExceptionInformation(_exception, _name);
return errorOutput.str();
}
@ -69,6 +71,7 @@ private:
private:
bool m_colored;
bool m_withErrorIds;
util::Hyperlink m_hyperlink;
};
}

72
libsolutil/hyperlink.h Normal file
View File

@ -0,0 +1,72 @@
/*
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 <boost/filesystem.hpp>
#include <iostream>
#include <string>
// Required for gethostname()
#if defined(_WIN32)
#include <Winsock2.h>
#else
#include <unistd.h>
#endif
namespace solidity::util
{
struct Hyperlink
{
bool enabled = false;
struct Ref { bool enabled; std::string text; };
Ref operator()(std::string const& _ref) { return Ref{enabled, _ref}; }
};
inline std::ostream& operator<<(std::ostream& _os, Hyperlink::Ref const& _hyperlink)
{
auto const path = boost::filesystem::path{_hyperlink.text};
bool const candidate = boost::filesystem::is_regular_file(path) && (&_os == &std::cout || &_os == &std::cerr);
if (_hyperlink.enabled && candidate)
{
static std::string const hostname = []() -> std::string {
char hostname[80] = {0};
if (gethostname(hostname, sizeof(hostname)) < 0)
return std::string{};
return std::string{hostname};
}();
auto const abspath = boost::filesystem::canonical(boost::filesystem::absolute(path));
auto constexpr OSC8 = "\033]8;;";
auto constexpr ST = "\033\\";
_os << OSC8 << "file://" << hostname << abspath.generic_string() << ST;
_os << _hyperlink.text;
_os << OSC8 << ST;
}
else
_os << _hyperlink.text;
return _os;
}
} // end namespace

View File

@ -178,6 +178,8 @@ static string const g_strVersion = "version";
static string const g_strIgnoreMissingFiles = "ignore-missing";
static string const g_strColor = "color";
static string const g_strNoColor = "no-color";
static string const g_strHyperlinks = "hyperlinks";
static string const g_strNoHyperlinks = "no-hyperlinks";
static string const g_strErrorIds = "error-codes";
static string const g_strOldReporter = "old-reporter";
@ -224,6 +226,8 @@ static string const g_stdinFileName = g_stdinFileNameStr;
static string const g_argIgnoreMissingFiles = g_strIgnoreMissingFiles;
static string const g_argColor = g_strColor;
static string const g_argNoColor = g_strNoColor;
static string const g_argHyperlinks = g_strHyperlinks;
static string const g_argNoHyperlinks = g_strNoHyperlinks;
static string const g_argErrorIds = g_strErrorIds;
static string const g_argOldReporter = g_strOldReporter;
@ -879,6 +883,14 @@ General Information)").c_str(),
g_argNoColor.c_str(),
"Explicitly disable colored output, disabling terminal auto-detection."
)
(
g_argHyperlinks.c_str(),
"Force hyperlink output. This will enable file names to be clickable in error diagnostics."
)
(
g_argNoHyperlinks.c_str(),
"Explicitly disable hyperlink output, disabling terminal auto-detection."
)
(
g_argErrorIds.c_str(),
"Output error codes."
@ -994,8 +1006,16 @@ General Information)").c_str(),
return false;
}
if (m_args.count(g_argHyperlinks) && m_args.count(g_argNoHyperlinks))
{
serr() << "Option " << g_argHyperlinks << " and " << g_argNoHyperlinks << " are mutualy exclusive." << endl;
return false;
}
m_coloredOutput = !m_args.count(g_argNoColor) && (isatty(STDERR_FILENO) || m_args.count(g_argColor));
m_hyperlinks = !m_args.count(g_argNoHyperlinks) && (isatty(STDERR_FILENO) || m_args.count(g_argHyperlinks));
m_withErrorIds = m_args.count(g_argErrorIds);
if (m_args.count(g_argHelp) || (isatty(fileno(stdin)) && _argc == 1))
@ -1331,7 +1351,7 @@ bool CommandLineInterface::processInput()
if (m_args.count(g_argOldReporter))
formatter = make_unique<SourceReferenceFormatter>(serr(false));
else
formatter = make_unique<SourceReferenceFormatterHuman>(serr(false), m_coloredOutput, m_withErrorIds);
formatter = make_unique<SourceReferenceFormatterHuman>(serr(false), m_coloredOutput, m_withErrorIds, m_hyperlinks);
try
{
@ -1769,7 +1789,7 @@ bool CommandLineInterface::assemble(
if (m_args.count(g_argOldReporter))
formatter = make_unique<SourceReferenceFormatter>(serr(false));
else
formatter = make_unique<SourceReferenceFormatterHuman>(serr(false), m_coloredOutput, m_withErrorIds);
formatter = make_unique<SourceReferenceFormatterHuman>(serr(false), m_coloredOutput, m_withErrorIds, m_hyperlinks);
for (auto const& error: stack.errors())
{

View File

@ -136,6 +136,8 @@ private:
bool m_coloredOutput = true;
/// Whether or not to output error IDs.
bool m_withErrorIds = false;
/// Whether or not error diagnostics may construct text with hyperlink anchors.
bool m_hyperlinks = false;
};
}

View File

@ -134,7 +134,7 @@ TestCase::TestResult ASTJSONTest::run(ostream& _stream, string const& _linePrefi
c.analyze();
else
{
SourceReferenceFormatterHuman formatter(_stream, _formatted, false);
SourceReferenceFormatterHuman formatter(_stream, _formatted, false, false);
for (auto const& error: c.errors())
formatter.printErrorInformation(*error);
return TestResult::FatalError;

View File

@ -118,7 +118,7 @@ TestCase::TestResult GasTest::run(ostream& _stream, string const& _linePrefix, b
if (!compiler().parseAndAnalyze() || !compiler().compile())
{
SourceReferenceFormatterHuman formatter(_stream, _formatted, false);
SourceReferenceFormatterHuman formatter(_stream, _formatted, false, false);
for (auto const& error: compiler().errors())
formatter.printErrorInformation(*error);
return TestResult::FatalError;

View File

@ -390,7 +390,7 @@ void SourceUpgrade::applyChange(
void SourceUpgrade::printErrors() const
{
auto formatter = make_unique<langutil::SourceReferenceFormatterHuman>(cout, true, false);
auto formatter = make_unique<langutil::SourceReferenceFormatterHuman>(cout, true, false, false);
for (auto const& error: m_compiler->errors())
if (error->type() != langutil::Error::Type::Warning)

View File

@ -36,7 +36,7 @@ void UpgradeChange::apply()
void UpgradeChange::log(bool const _shorten) const
{
stringstream os;
SourceReferenceFormatterHuman formatter{os, true, false};
SourceReferenceFormatterHuman formatter{os, true, false, false};
string start = to_string(m_location.start);
string end = to_string(m_location.end);