2019-12-09 16:01:31 +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
|
2019-12-09 16:01:31 +00:00
|
|
|
#include <tools/solidityUpgrade/UpgradeChange.h>
|
|
|
|
|
|
|
|
#include <liblangutil/SourceReferenceExtractor.h>
|
2020-12-01 13:22:15 +00:00
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
2021-09-16 14:33:28 +00:00
|
|
|
#include <libsolutil/Numeric.h>
|
2019-12-09 16:01:31 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::tools;
|
|
|
|
|
2021-06-29 12:38:59 +00:00
|
|
|
string UpgradeChange::apply(string _source) const
|
2019-12-09 16:01:31 +00:00
|
|
|
{
|
2021-06-29 12:38:59 +00:00
|
|
|
_source.replace(
|
2020-06-03 08:27:23 +00:00
|
|
|
static_cast<size_t>(m_location.start),
|
2021-06-29 12:38:59 +00:00
|
|
|
static_cast<size_t>(m_location.end - m_location.start),
|
|
|
|
m_patch
|
2020-06-03 08:27:23 +00:00
|
|
|
);
|
2021-06-29 12:38:59 +00:00
|
|
|
return _source;
|
2019-12-09 16:01:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:38:59 +00:00
|
|
|
void UpgradeChange::log(CharStreamProvider const& _charStreamProvider, bool const _shorten) const
|
2019-12-09 16:01:31 +00:00
|
|
|
{
|
|
|
|
stringstream os;
|
2021-06-29 12:38:59 +00:00
|
|
|
SourceReferenceFormatter formatter{os, _charStreamProvider, true, false};
|
2019-12-09 16:01:31 +00:00
|
|
|
|
|
|
|
string start = to_string(m_location.start);
|
|
|
|
string end = to_string(m_location.end);
|
|
|
|
|
|
|
|
auto color = m_level == Level::Unsafe ? formatting::MAGENTA : formatting::CYAN;
|
|
|
|
auto level = m_level == Level::Unsafe ? "unsafe" : "safe";
|
|
|
|
|
|
|
|
os << endl;
|
|
|
|
AnsiColorized(os, true, {formatting::BOLD, color}) << "Upgrade change (" << level << ")" << endl;
|
|
|
|
os << "=======================" << endl;
|
2021-06-29 12:38:59 +00:00
|
|
|
formatter.printSourceLocation(SourceReferenceExtractor::extract(_charStreamProvider, &m_location));
|
2019-12-09 16:01:31 +00:00
|
|
|
os << endl;
|
|
|
|
|
2021-06-29 12:38:59 +00:00
|
|
|
LineColumn lineEnd = _charStreamProvider.charStream(*m_location.sourceName).translatePositionToLineColumn(m_location.end);
|
2019-12-09 16:01:31 +00:00
|
|
|
int const leftpad = static_cast<int>(log10(max(lineEnd.line, 1))) + 2;
|
|
|
|
|
|
|
|
stringstream output;
|
|
|
|
output << (_shorten ? shortenSource(m_patch) : m_patch);
|
|
|
|
|
|
|
|
string line;
|
|
|
|
while (getline(output, line))
|
|
|
|
{
|
2020-06-03 08:27:23 +00:00
|
|
|
os << string(static_cast<size_t>(leftpad), ' ');
|
2019-12-09 16:01:31 +00:00
|
|
|
AnsiColorized(os, true, {formatting::BOLD, formatting::BLUE}) << "| ";
|
|
|
|
AnsiColorized(os, true, {}) << line << endl;
|
|
|
|
}
|
|
|
|
cout << os.str();
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
string UpgradeChange::shortenSource(string const& _source)
|
|
|
|
{
|
|
|
|
size_t constexpr maxSourceLength = 1000;
|
|
|
|
if (_source.size() > maxSourceLength)
|
|
|
|
return _source.substr(0, min(_source.size(), maxSourceLength)) + "...";
|
|
|
|
return _source;
|
|
|
|
}
|