/src/solidity/liblangutil/SourceReferenceExtractor.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | #include <liblangutil/SourceReferenceExtractor.h> |
19 | | #include <liblangutil/Exceptions.h> |
20 | | #include <liblangutil/CharStreamProvider.h> |
21 | | #include <liblangutil/CharStream.h> |
22 | | |
23 | | #include <algorithm> |
24 | | #include <cmath> |
25 | | |
26 | | using namespace std; |
27 | | using namespace solidity; |
28 | | using namespace solidity::langutil; |
29 | | |
30 | | SourceReferenceExtractor::Message SourceReferenceExtractor::extract( |
31 | | CharStreamProvider const& _charStreamProvider, |
32 | | util::Exception const& _exception, |
33 | | string _severity |
34 | | ) |
35 | 0 | { |
36 | 0 | SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception); |
37 | |
|
38 | 0 | string const* message = boost::get_error_info<util::errinfo_comment>(_exception); |
39 | 0 | SourceReference primary = extract(_charStreamProvider, location, message ? *message : ""); |
40 | |
|
41 | 0 | std::vector<SourceReference> secondary; |
42 | 0 | auto secondaryLocation = boost::get_error_info<errinfo_secondarySourceLocation>(_exception); |
43 | 0 | if (secondaryLocation && !secondaryLocation->infos.empty()) |
44 | 0 | for (auto const& info: secondaryLocation->infos) |
45 | 0 | secondary.emplace_back(extract(_charStreamProvider, &info.second, info.first)); |
46 | |
|
47 | 0 | return Message{std::move(primary), _severity, std::move(secondary), nullopt}; |
48 | 0 | } |
49 | | |
50 | | SourceReferenceExtractor::Message SourceReferenceExtractor::extract( |
51 | | CharStreamProvider const& _charStreamProvider, |
52 | | Error const& _error |
53 | | ) |
54 | 0 | { |
55 | 0 | string severity = Error::formatErrorSeverity(Error::errorSeverity(_error.type())); |
56 | 0 | Message message = extract(_charStreamProvider, _error, severity); |
57 | 0 | message.errorId = _error.errorId(); |
58 | 0 | return message; |
59 | 0 | } |
60 | | |
61 | | SourceReference SourceReferenceExtractor::extract( |
62 | | CharStreamProvider const& _charStreamProvider, |
63 | | SourceLocation const* _location, |
64 | | std::string message |
65 | | ) |
66 | 0 | { |
67 | 0 | if (!_location || !_location->sourceName) // Nothing we can extract here |
68 | 0 | return SourceReference::MessageOnly(std::move(message)); |
69 | | |
70 | 0 | if (!_location->hasText()) // No source text, so we can only extract the source name |
71 | 0 | return SourceReference::MessageOnly(std::move(message), *_location->sourceName); |
72 | | |
73 | 0 | CharStream const& charStream = _charStreamProvider.charStream(*_location->sourceName); |
74 | |
|
75 | 0 | LineColumn const interest = charStream.translatePositionToLineColumn(_location->start); |
76 | 0 | LineColumn start = interest; |
77 | 0 | LineColumn end = charStream.translatePositionToLineColumn(_location->end); |
78 | 0 | bool const isMultiline = start.line != end.line; |
79 | |
|
80 | 0 | string line = charStream.lineAtPosition(_location->start); |
81 | |
|
82 | 0 | int locationLength = |
83 | 0 | isMultiline ? |
84 | 0 | int(line.length()) - start.column : |
85 | 0 | end.column - start.column; |
86 | |
|
87 | 0 | if (locationLength > 150) |
88 | 0 | { |
89 | 0 | auto const lhs = static_cast<size_t>(start.column) + 35; |
90 | 0 | string::size_type const rhs = (isMultiline ? line.length() : static_cast<size_t>(end.column)) - 35; |
91 | 0 | line = line.substr(0, lhs) + " ... " + line.substr(rhs); |
92 | 0 | end.column = start.column + 75; |
93 | 0 | locationLength = 75; |
94 | 0 | } |
95 | |
|
96 | 0 | if (line.length() > 150) |
97 | 0 | { |
98 | 0 | int const len = static_cast<int>(line.length()); |
99 | 0 | line = line.substr( |
100 | 0 | static_cast<size_t>(max(0, start.column - 35)), |
101 | 0 | static_cast<size_t>(min(start.column, 35)) + static_cast<size_t>( |
102 | 0 | min(locationLength + 35, len - start.column) |
103 | 0 | ) |
104 | 0 | ); |
105 | 0 | if (start.column + locationLength + 35 < len) |
106 | 0 | line += " ..."; |
107 | 0 | if (start.column > 35) |
108 | 0 | { |
109 | 0 | line = " ... " + line; |
110 | 0 | start.column = 40; |
111 | 0 | } |
112 | 0 | end.column = start.column + static_cast<int>(locationLength); |
113 | 0 | } |
114 | |
|
115 | 0 | return SourceReference{ |
116 | 0 | std::move(message), |
117 | 0 | *_location->sourceName, |
118 | 0 | interest, |
119 | 0 | isMultiline, |
120 | 0 | line, |
121 | 0 | min(start.column, static_cast<int>(line.length())), |
122 | 0 | min(end.column, static_cast<int>(line.length())) |
123 | 0 | }; |
124 | 0 | } |