diff options
author | Christian Parpart <christian@ethereum.org> | 2018-11-30 21:34:08 +0800 |
---|---|---|
committer | chriseth <chris@ethereum.org> | 2018-12-06 21:01:01 +0800 |
commit | 073b03d90c8f0648ba135f0b30d8e72fd871478f (patch) | |
tree | b38bf2ef623bb1ac85bf9ca929ba61f76c05bd0d /liblangutil | |
parent | 6efe2a526691f42e83b11cf670ec3e7f51927b3e (diff) | |
download | dexon-solidity-073b03d90c8f0648ba135f0b30d8e72fd871478f.tar.gz dexon-solidity-073b03d90c8f0648ba135f0b30d8e72fd871478f.tar.zst dexon-solidity-073b03d90c8f0648ba135f0b30d8e72fd871478f.zip |
liblangutil: refactor SourceReferenceFormatter, splitting out retrieval and making use of new SourceLocation's CharStream knowledge
Diffstat (limited to 'liblangutil')
-rw-r--r-- | liblangutil/CMakeLists.txt | 1 | ||||
-rw-r--r-- | liblangutil/SourceReferenceExtractor.cpp | 89 | ||||
-rw-r--r-- | liblangutil/SourceReferenceExtractor.h | 74 | ||||
-rw-r--r-- | liblangutil/SourceReferenceFormatter.cpp | 103 | ||||
-rw-r--r-- | liblangutil/SourceReferenceFormatter.h | 26 |
5 files changed, 208 insertions, 85 deletions
diff --git a/liblangutil/CMakeLists.txt b/liblangutil/CMakeLists.txt index dfcccfce..5a82f3ff 100644 --- a/liblangutil/CMakeLists.txt +++ b/liblangutil/CMakeLists.txt @@ -5,6 +5,7 @@ set(sources Exceptions.cpp ParserBase.cpp Scanner.cpp + SourceReferenceExtractor.cpp SourceReferenceFormatter.cpp Token.cpp ) diff --git a/liblangutil/SourceReferenceExtractor.cpp b/liblangutil/SourceReferenceExtractor.cpp new file mode 100644 index 00000000..4502bb23 --- /dev/null +++ b/liblangutil/SourceReferenceExtractor.cpp @@ -0,0 +1,89 @@ +/* + 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/>. +*/ +#include <liblangutil/SourceReferenceExtractor.h> +#include <liblangutil/CharStream.h> +#include <liblangutil/Exceptions.h> + +#include <cmath> +#include <iomanip> + +using namespace std; +using namespace dev; +using namespace langutil; + +SourceReferenceExtractor::Message SourceReferenceExtractor::extract(Exception const& _exception, string _category) +{ + SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception); + + string const* message = boost::get_error_info<errinfo_comment>(_exception); + SourceReference primary = extract(location, message ? *message : ""); + + std::vector<SourceReference> secondary; + auto secondaryLocation = boost::get_error_info<errinfo_secondarySourceLocation>(_exception); + if (secondaryLocation && !secondaryLocation->infos.empty()) + for (auto const& info: secondaryLocation->infos) + secondary.emplace_back(extract(&info.second, info.first)); + + return Message{std::move(primary), _category, std::move(secondary)}; +} + +SourceReference SourceReferenceExtractor::extract(SourceLocation const* _location, std::string message) +{ + if (!_location || !_location->source.get()) // Nothing we can extract here + return SourceReference::MessageOnly(std::move(message)); + + shared_ptr<CharStream> const& source = _location->source; + + LineColumn const interest = source->translatePositionToLineColumn(_location->start); + LineColumn start = interest; + LineColumn end = source->translatePositionToLineColumn(_location->end); + bool const isMultiline = start.line != end.line; + + string line = source->lineAtPosition(_location->start); + + int locationLength = isMultiline ? line.length() - start.column : end.column - start.column; + if (locationLength > 150) + { + line = line.substr(0, start.column + 35) + " ... " + line.substr(end.column - 35); + end.column = start.column + 75; + locationLength = 75; + } + + if (line.length() > 150) + { + int const len = line.length(); + line = line.substr(max(0, start.column - 35), min(start.column, 35) + min(locationLength + 35, len - start.column)); + if (start.column + locationLength + 35 < len) + line += " ..."; + if (start.column > 35) + { + line = " ... " + line; + start.column = 40; + } + end.column = start.column + locationLength; + } + + return SourceReference{ + std::move(message), + source->name(), + interest, + isMultiline, + line, + start.column, + end.column, + }; +} diff --git a/liblangutil/SourceReferenceExtractor.h b/liblangutil/SourceReferenceExtractor.h new file mode 100644 index 00000000..0be7e9d8 --- /dev/null +++ b/liblangutil/SourceReferenceExtractor.h @@ -0,0 +1,74 @@ +/* + 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 <iosfwd> +#include <string> +#include <tuple> +#include <vector> + +namespace dev +{ +struct Exception; +} + +namespace langutil +{ + +struct LineColumn +{ + int line; + int column; + + LineColumn(std::tuple<int, int> const& _t): line{std::get<0>(_t)}, column{std::get<1>(_t)} {} + LineColumn(int _line, int _column): line{_line}, column{_column} {} + LineColumn(): line{-1}, column{-1} {} +}; + +struct SourceReference +{ + std::string message; ///< A message that relates to this source reference (such as a warning or an error message). + std::string sourceName; ///< Underlying source name (for example the filename). + LineColumn position; ///< Actual (error) position this source reference is surrounding. + bool multiline; ///< Indicates whether the actual SourceReference is truncated to one line. + std::string text; ///< Extracted source code text (potentially truncated if multiline or too long). + int startColumn; ///< Highlighting range-start of text field. + int endColumn; ///< Highlighting range-end of text field. + + /// Constructs a SourceReference containing a message only. + static SourceReference MessageOnly(std::string _msg) + { + return SourceReference{std::move(_msg), "", LineColumn{-1, -1}, false, "", -1, -1}; + } +}; + +struct SourceLocation; + +namespace SourceReferenceExtractor +{ + struct Message + { + SourceReference primary; + std::string category; // "Error", "Warning", ... + std::vector<SourceReference> secondary; + }; + + Message extract(dev::Exception const& _exception, std::string _category); + SourceReference extract(SourceLocation const* _location, std::string message = ""); +} + +} diff --git a/liblangutil/SourceReferenceFormatter.cpp b/liblangutil/SourceReferenceFormatter.cpp index 8ac05b4e..4bc47a65 100644 --- a/liblangutil/SourceReferenceFormatter.cpp +++ b/liblangutil/SourceReferenceFormatter.cpp @@ -30,100 +30,63 @@ using namespace langutil; void SourceReferenceFormatter::printSourceLocation(SourceLocation const* _location) { - if (!_location || !_location->source) - return; // Nothing we can print here - auto const& scanner = m_scannerFromSourceName(_location->source->name()); - int startLine; - int startColumn; - tie(startLine, startColumn) = scanner.translatePositionToLineColumn(_location->start); - int endLine; - int endColumn; - tie(endLine, endColumn) = scanner.translatePositionToLineColumn(_location->end); - if (startLine == endLine) - { - string line = scanner.lineAtPosition(_location->start); + printSourceLocation(SourceReferenceExtractor::extract(_location)); +} - int locationLength = endColumn - startColumn; - if (locationLength > 150) - { - line = line.substr(0, startColumn + 35) + " ... " + line.substr(endColumn - 35); - endColumn = startColumn + 75; - locationLength = 75; - } - if (line.length() > 150) - { - int len = line.length(); - line = line.substr(max(0, startColumn - 35), min(startColumn, 35) + min(locationLength + 35, len - startColumn)); - if (startColumn + locationLength + 35 < len) - line += " ..."; - if (startColumn > 35) - { - line = " ... " + line; - startColumn = 40; - } - endColumn = startColumn + locationLength; - } +void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref) +{ + if (_ref.position.line < 0) + return; // Nothing we can print here - m_stream << line << endl; + if (!_ref.multiline) + { + m_stream << _ref.text << endl; + // mark the text-range like this: ^-----^ for_each( - line.cbegin(), - line.cbegin() + startColumn, - [this](char const& ch) { m_stream << (ch == '\t' ? '\t' : ' '); } + _ref.text.cbegin(), + _ref.text.cbegin() + _ref.startColumn, + [this](char ch) { m_stream << (ch == '\t' ? '\t' : ' '); } ); m_stream << "^"; - if (endColumn > startColumn + 2) - m_stream << string(endColumn - startColumn - 2, '-'); - if (endColumn > startColumn + 1) + if (_ref.endColumn > _ref.startColumn + 2) + m_stream << string(_ref.endColumn - _ref.startColumn - 2, '-'); + if (_ref.endColumn > _ref.startColumn + 1) m_stream << "^"; m_stream << endl; } else m_stream << - scanner.lineAtPosition(_location->start) << + _ref.text << endl << - string(startColumn, ' ') << + string(_ref.startColumn, ' ') << "^ (Relevant source part starts here and spans across multiple lines)." << endl; } -void SourceReferenceFormatter::printSourceName(SourceLocation const* _location) +void SourceReferenceFormatter::printSourceName(SourceReference const& _ref) { - if (!_location || !_location->source) - return; // Nothing we can print here - auto const& scanner = m_scannerFromSourceName(_location->source->name()); - int startLine; - int startColumn; - tie(startLine, startColumn) = scanner.translatePositionToLineColumn(_location->start); - m_stream << _location->source->name() << ":" << (startLine + 1) << ":" << (startColumn + 1) << ": "; + if (_ref.position.line != -1) + m_stream << _ref.sourceName << ":" << (_ref.position.line + 1) << ":" << (_ref.position.column + 1) << ": "; } -void SourceReferenceFormatter::printExceptionInformation( - dev::Exception const& _exception, - string const& _name -) +void SourceReferenceFormatter::printExceptionInformation(dev::Exception const& _error, std::string const& _category) { - SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception); - auto secondarylocation = boost::get_error_info<errinfo_secondarySourceLocation>(_exception); + printExceptionInformation(SourceReferenceExtractor::extract(_error, _category)); +} - printSourceName(location); +void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg) +{ + printSourceName(_msg.primary); - m_stream << _name; - if (string const* description = boost::get_error_info<errinfo_comment>(_exception)) - m_stream << ": " << *description << endl; - else - m_stream << endl; + m_stream << _msg.category << ": " << _msg.primary.message << endl; - printSourceLocation(location); + printSourceLocation(_msg.primary); - if (secondarylocation && !secondarylocation->infos.empty()) + for (auto const& ref: _msg.secondary) { - for (auto info: secondarylocation->infos) - { - printSourceName(&info.second); - m_stream << info.first << endl; - printSourceLocation(&info.second); - } - m_stream << endl; + printSourceName(ref); + m_stream << ref.message << endl; + printSourceLocation(ref); } } diff --git a/liblangutil/SourceReferenceFormatter.h b/liblangutil/SourceReferenceFormatter.h index 0ef3ca00..9f05f430 100644 --- a/liblangutil/SourceReferenceFormatter.h +++ b/liblangutil/SourceReferenceFormatter.h @@ -25,6 +25,7 @@ #include <ostream> #include <sstream> #include <functional> +#include <liblangutil/SourceReferenceExtractor.h> namespace dev { @@ -39,38 +40,33 @@ class Scanner; class SourceReferenceFormatter { public: - using ScannerFromSourceNameFun = std::function<langutil::Scanner const&(std::string const&)>; - - explicit SourceReferenceFormatter( - std::ostream& _stream, - ScannerFromSourceNameFun _scannerFromSourceName - ): - m_stream(_stream), - m_scannerFromSourceName(std::move(_scannerFromSourceName)) + explicit SourceReferenceFormatter(std::ostream& _stream): + m_stream(_stream) {} /// Prints source location if it is given. - void printSourceLocation(langutil::SourceLocation const* _location); - void printExceptionInformation(dev::Exception const& _exception, std::string const& _name); + 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); static std::string formatExceptionInformation( dev::Exception const& _exception, - std::string const& _name, - ScannerFromSourceNameFun const& _scannerFromSourceName + std::string const& _name ) { std::ostringstream errorOutput; - SourceReferenceFormatter formatter(errorOutput, _scannerFromSourceName); + SourceReferenceFormatter formatter(errorOutput); formatter.printExceptionInformation(_exception, _name); return errorOutput.str(); } + private: /// Prints source name if location is given. - void printSourceName(langutil::SourceLocation const* _location); + void printSourceName(SourceReference const& _ref); std::ostream& m_stream; - ScannerFromSourceNameFun m_scannerFromSourceName; }; } |