2018-11-30 13:34:08 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-11-30 13:34:08 +00:00
|
|
|
#include <liblangutil/SourceReferenceExtractor.h>
|
|
|
|
#include <liblangutil/Exceptions.h>
|
2021-06-29 12:38:59 +00:00
|
|
|
#include <liblangutil/CharStreamProvider.h>
|
|
|
|
#include <liblangutil/CharStream.h>
|
2018-11-30 13:34:08 +00:00
|
|
|
|
2021-03-09 10:34:01 +00:00
|
|
|
#include <algorithm>
|
2018-11-30 13:34:08 +00:00
|
|
|
#include <cmath>
|
2022-09-06 10:12:15 +00:00
|
|
|
#include <variant>
|
2018-11-30 13:34:08 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::langutil;
|
2018-11-30 13:34:08 +00:00
|
|
|
|
2021-06-29 12:38:59 +00:00
|
|
|
SourceReferenceExtractor::Message SourceReferenceExtractor::extract(
|
|
|
|
CharStreamProvider const& _charStreamProvider,
|
|
|
|
util::Exception const& _exception,
|
2022-09-06 10:12:15 +00:00
|
|
|
std::variant<Error::Type, Error::Severity> _typeOrSeverity
|
2021-06-29 12:38:59 +00:00
|
|
|
)
|
2018-11-30 13:34:08 +00:00
|
|
|
{
|
|
|
|
SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
|
|
|
|
|
2023-07-12 08:05:47 +00:00
|
|
|
std::string const* message = boost::get_error_info<util::errinfo_comment>(_exception);
|
2021-06-29 12:38:59 +00:00
|
|
|
SourceReference primary = extract(_charStreamProvider, location, message ? *message : "");
|
2018-11-30 13:34:08 +00:00
|
|
|
|
|
|
|
std::vector<SourceReference> secondary;
|
|
|
|
auto secondaryLocation = boost::get_error_info<errinfo_secondarySourceLocation>(_exception);
|
|
|
|
if (secondaryLocation && !secondaryLocation->infos.empty())
|
|
|
|
for (auto const& info: secondaryLocation->infos)
|
2021-06-29 12:38:59 +00:00
|
|
|
secondary.emplace_back(extract(_charStreamProvider, &info.second, info.first));
|
2018-11-30 13:34:08 +00:00
|
|
|
|
2023-07-12 08:05:47 +00:00
|
|
|
return Message{std::move(primary), _typeOrSeverity, std::move(secondary), std::nullopt};
|
2020-06-04 01:19:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:38:59 +00:00
|
|
|
SourceReferenceExtractor::Message SourceReferenceExtractor::extract(
|
|
|
|
CharStreamProvider const& _charStreamProvider,
|
2022-09-06 10:12:15 +00:00
|
|
|
Error const& _error,
|
|
|
|
std::variant<Error::Type, Error::Severity> _typeOrSeverity
|
2021-06-29 12:38:59 +00:00
|
|
|
)
|
2020-06-04 01:19:47 +00:00
|
|
|
{
|
2022-09-06 10:12:15 +00:00
|
|
|
Message message = extract(_charStreamProvider, static_cast<util::Exception>(_error), _typeOrSeverity);
|
2020-06-04 01:19:47 +00:00
|
|
|
message.errorId = _error.errorId();
|
|
|
|
return message;
|
2018-11-30 13:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:38:59 +00:00
|
|
|
SourceReference SourceReferenceExtractor::extract(
|
|
|
|
CharStreamProvider const& _charStreamProvider,
|
|
|
|
SourceLocation const* _location,
|
|
|
|
std::string message
|
|
|
|
)
|
2018-11-30 13:34:08 +00:00
|
|
|
{
|
2021-06-29 12:38:59 +00:00
|
|
|
if (!_location || !_location->sourceName) // Nothing we can extract here
|
2018-11-30 13:34:08 +00:00
|
|
|
return SourceReference::MessageOnly(std::move(message));
|
|
|
|
|
2020-02-07 01:36:51 +00:00
|
|
|
if (!_location->hasText()) // No source text, so we can only extract the source name
|
2021-06-29 12:38:59 +00:00
|
|
|
return SourceReference::MessageOnly(std::move(message), *_location->sourceName);
|
2020-01-09 13:18:54 +00:00
|
|
|
|
2021-06-29 12:38:59 +00:00
|
|
|
CharStream const& charStream = _charStreamProvider.charStream(*_location->sourceName);
|
2018-11-30 13:34:08 +00:00
|
|
|
|
2021-06-29 12:38:59 +00:00
|
|
|
LineColumn const interest = charStream.translatePositionToLineColumn(_location->start);
|
2018-11-30 13:34:08 +00:00
|
|
|
LineColumn start = interest;
|
2021-06-29 12:38:59 +00:00
|
|
|
LineColumn end = charStream.translatePositionToLineColumn(_location->end);
|
2018-11-30 13:34:08 +00:00
|
|
|
bool const isMultiline = start.line != end.line;
|
|
|
|
|
2023-07-12 08:05:47 +00:00
|
|
|
std::string line = charStream.lineAtPosition(_location->start);
|
2018-11-30 13:34:08 +00:00
|
|
|
|
2020-06-02 13:45:03 +00:00
|
|
|
int locationLength =
|
|
|
|
isMultiline ?
|
|
|
|
int(line.length()) - start.column :
|
|
|
|
end.column - start.column;
|
|
|
|
|
2018-11-30 13:34:08 +00:00
|
|
|
if (locationLength > 150)
|
|
|
|
{
|
2020-06-02 13:45:03 +00:00
|
|
|
auto const lhs = static_cast<size_t>(start.column) + 35;
|
2023-07-12 08:05:47 +00:00
|
|
|
std::string::size_type const rhs = (isMultiline ? line.length() : static_cast<size_t>(end.column)) - 35;
|
2019-01-07 10:33:14 +00:00
|
|
|
line = line.substr(0, lhs) + " ... " + line.substr(rhs);
|
2018-11-30 13:34:08 +00:00
|
|
|
end.column = start.column + 75;
|
|
|
|
locationLength = 75;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.length() > 150)
|
|
|
|
{
|
2020-06-02 13:45:03 +00:00
|
|
|
int const len = static_cast<int>(line.length());
|
|
|
|
line = line.substr(
|
2023-07-12 08:05:47 +00:00
|
|
|
static_cast<size_t>(std::max(0, start.column - 35)),
|
|
|
|
static_cast<size_t>(std::min(start.column, 35)) + static_cast<size_t>(
|
|
|
|
std::min(locationLength + 35, len - start.column)
|
2020-06-02 13:45:03 +00:00
|
|
|
)
|
|
|
|
);
|
2018-11-30 13:34:08 +00:00
|
|
|
if (start.column + locationLength + 35 < len)
|
|
|
|
line += " ...";
|
|
|
|
if (start.column > 35)
|
|
|
|
{
|
|
|
|
line = " ... " + line;
|
|
|
|
start.column = 40;
|
|
|
|
}
|
2020-06-02 13:45:03 +00:00
|
|
|
end.column = start.column + static_cast<int>(locationLength);
|
2018-11-30 13:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SourceReference{
|
|
|
|
std::move(message),
|
2021-06-29 12:38:59 +00:00
|
|
|
*_location->sourceName,
|
2018-11-30 13:34:08 +00:00
|
|
|
interest,
|
|
|
|
isMultiline,
|
|
|
|
line,
|
2023-07-12 08:05:47 +00:00
|
|
|
std::min(start.column, static_cast<int>(line.length())),
|
|
|
|
std::min(end.column, static_cast<int>(line.length()))
|
2018-11-30 13:34:08 +00:00
|
|
|
};
|
|
|
|
}
|