aboutsummaryrefslogtreecommitdiffstats
path: root/liblangutil/SourceReferenceExtractor.cpp
diff options
context:
space:
mode:
authorChristian Parpart <christian@ethereum.org>2018-11-30 21:34:08 +0800
committerchriseth <chris@ethereum.org>2018-12-06 21:01:01 +0800
commit073b03d90c8f0648ba135f0b30d8e72fd871478f (patch)
treeb38bf2ef623bb1ac85bf9ca929ba61f76c05bd0d /liblangutil/SourceReferenceExtractor.cpp
parent6efe2a526691f42e83b11cf670ec3e7f51927b3e (diff)
downloaddexon-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/SourceReferenceExtractor.cpp')
-rw-r--r--liblangutil/SourceReferenceExtractor.cpp89
1 files changed, 89 insertions, 0 deletions
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,
+ };
+}