Extract import-file remapping logic out of CompilerStack into ImportRemapper.

This commit is contained in:
Christian Parpart
2021-04-06 14:28:53 +02:00
parent 30588a90eb
commit 3490577140
10 changed files with 184 additions and 85 deletions
+6 -61
View File
@@ -24,6 +24,7 @@
#include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <libsolidity/analysis/ControlFlowAnalyzer.h>
#include <libsolidity/analysis/ControlFlowGraph.h>
@@ -196,33 +197,13 @@ void CompilerStack::findAndReportCyclicContractDependencies()
}
}
std::optional<CompilerStack::Remapping> CompilerStack::parseRemapping(string const& _remapping)
{
auto eq = find(_remapping.begin(), _remapping.end(), '=');
if (eq == _remapping.end())
return {};
auto colon = find(_remapping.begin(), eq, ':');
Remapping r;
r.context = colon == eq ? string() : string(_remapping.begin(), colon);
r.prefix = colon == eq ? string(_remapping.begin(), eq) : string(colon + 1, eq);
r.target = string(eq + 1, _remapping.end());
if (r.prefix.empty())
return {};
return r;
}
void CompilerStack::setRemappings(vector<Remapping> const& _remappings)
void CompilerStack::setRemappings(vector<ImportRemapper::Remapping> _remappings)
{
if (m_stackState >= ParsedAndImported)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Must set remappings before parsing."));
for (auto const& remapping: _remappings)
solAssert(!remapping.prefix.empty(), "");
m_remappings = _remappings;
m_importRemapper.setRemappings(move(_remappings));
}
void CompilerStack::setViaIR(bool _viaIR)
@@ -312,7 +293,7 @@ void CompilerStack::reset(bool _keepSettings)
m_unhandledSMTLib2Queries.clear();
if (!_keepSettings)
{
m_remappings.clear();
m_importRemapper.clear();
m_libraries.clear();
m_viaIR = false;
m_evmVersion = langutil::EVMVersion();
@@ -1170,43 +1151,7 @@ StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast, std::string
string CompilerStack::applyRemapping(string const& _path, string const& _context)
{
solAssert(m_stackState < ParsedAndImported, "");
// Try to find the longest prefix match in all remappings that are active in the current context.
auto isPrefixOf = [](string const& _a, string const& _b)
{
if (_a.length() > _b.length())
return false;
return std::equal(_a.begin(), _a.end(), _b.begin());
};
size_t longestPrefix = 0;
size_t longestContext = 0;
string bestMatchTarget;
for (auto const& redir: m_remappings)
{
string context = util::sanitizePath(redir.context);
string prefix = util::sanitizePath(redir.prefix);
// Skip if current context is closer
if (context.length() < longestContext)
continue;
// Skip if redir.context is not a prefix of _context
if (!isPrefixOf(context, _context))
continue;
// Skip if we already have a closer prefix match.
if (prefix.length() < longestPrefix && context.length() == longestContext)
continue;
// Skip if the prefix does not match.
if (!isPrefixOf(prefix, _path))
continue;
longestContext = context.length();
longestPrefix = prefix.length();
bestMatchTarget = util::sanitizePath(redir.target);
}
string path = bestMatchTarget;
path.append(_path.begin() + static_cast<string::difference_type>(longestPrefix), _path.end());
return path;
return m_importRemapper.apply(_path, _context);
}
void CompilerStack::resolveImports()
@@ -1584,7 +1529,7 @@ string CompilerStack::createMetadata(Contract const& _contract) const
meta["settings"]["remappings"] = Json::arrayValue;
set<string> remappings;
for (auto const& r: m_remappings)
for (auto const& r: m_importRemapper.remappings())
remappings.insert(r.context + ":" + r.prefix + "=" + r.target);
for (auto const& r: remappings)
meta["settings"]["remappings"].append(r);
+3 -14
View File
@@ -26,6 +26,7 @@
#include <libsolidity/analysis/FunctionCallGraph.h>
#include <libsolidity/interface/ReadFile.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <libsolidity/interface/OptimiserSettings.h>
#include <libsolidity/interface/Version.h>
#include <libsolidity/interface/DebugSettings.h>
@@ -111,13 +112,6 @@ public:
None
};
struct Remapping
{
std::string context;
std::string prefix;
std::string target;
};
/// Creates a new compiler stack.
/// @param _readFile callback used to read files for import statements. Must return
/// and must not emit exceptions.
@@ -139,12 +133,9 @@ public:
/// all settings are reset as well.
void reset(bool _keepSettings = false);
// Parses a remapping of the format "context:prefix=target".
static std::optional<Remapping> parseRemapping(std::string const& _remapping);
/// Sets path remappings.
/// Must be set before parsing.
void setRemappings(std::vector<Remapping> const& _remappings);
void setRemappings(std::vector<ImportRemapper::Remapping> _remappings);
/// Sets library addresses. Addresses are cleared iff @a _libraries is missing.
/// Must be set before parsing.
@@ -487,9 +478,7 @@ private:
bool m_generateIR = false;
bool m_generateEwasm = false;
std::map<std::string, util::h160> m_libraries;
/// list of path prefix remappings, e.g. mylibrary: github.com/ethereum = /usr/local/ethereum
/// "context:prefix=target"
std::vector<Remapping> m_remappings;
ImportRemapper m_importRemapper;
std::map<std::string const, Source> m_sources;
// if imported, store AST-JSONS for each filename
std::map<std::string, Json::Value> m_sourceJsons;
+100
View File
@@ -0,0 +1,100 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/interface/ImportRemapper.h>
#include <libsolutil/CommonIO.h>
#include <liblangutil/Exceptions.h>
using std::equal;
using std::move;
using std::optional;
using std::string;
using std::string;
using std::vector;
namespace solidity::frontend
{
void ImportRemapper::setRemappings(vector<Remapping> _remappings)
{
for (auto const& remapping: _remappings)
solAssert(!remapping.prefix.empty(), "");
m_remappings = move(_remappings);
}
SourceUnitName ImportRemapper::apply(ImportPath const& _path, string const& _context) const
{
// Try to find the longest prefix match in all remappings that are active in the current context.
auto isPrefixOf = [](string const& _a, string const& _b)
{
if (_a.length() > _b.length())
return false;
return equal(_a.begin(), _a.end(), _b.begin());
};
size_t longestPrefix = 0;
size_t longestContext = 0;
string bestMatchTarget;
for (auto const& redir: m_remappings)
{
string context = util::sanitizePath(redir.context);
string prefix = util::sanitizePath(redir.prefix);
// Skip if current context is closer
if (context.length() < longestContext)
continue;
// Skip if redir.context is not a prefix of _context
if (!isPrefixOf(context, _context))
continue;
// Skip if we already have a closer prefix match.
if (prefix.length() < longestPrefix && context.length() == longestContext)
continue;
// Skip if the prefix does not match.
if (!isPrefixOf(prefix, _path))
continue;
longestContext = context.length();
longestPrefix = prefix.length();
bestMatchTarget = util::sanitizePath(redir.target);
}
string path = bestMatchTarget;
path.append(_path.begin() + static_cast<string::difference_type>(longestPrefix), _path.end());
return path;
}
optional<ImportRemapper::Remapping> ImportRemapper::parseRemapping(string const& _remapping)
{
auto eq = find(_remapping.begin(), _remapping.end(), '=');
if (eq == _remapping.end())
return {};
auto colon = find(_remapping.begin(), eq, ':');
Remapping r;
r.context = colon == eq ? string() : string(_remapping.begin(), colon);
r.prefix = colon == eq ? string(_remapping.begin(), eq) : string(colon + 1, eq);
r.target = string(eq + 1, _remapping.end());
if (r.prefix.empty())
return {};
return r;
}
}
+59
View File
@@ -0,0 +1,59 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <optional>
#include <string>
#include <vector>
namespace solidity::frontend
{
// Some helper typedefs to make reading the signatures more self explaining.
using SourceUnitName = std::string;
using SourceCode = std::string;
using ImportPath = std::string;
/// The ImportRemapper is being used on imported file paths for being remapped to source unit IDs before being loaded.
class ImportRemapper
{
public:
struct Remapping
{
std::string context;
std::string prefix;
std::string target;
};
void clear() { m_remappings.clear(); }
void setRemappings(std::vector<Remapping> _remappings);
std::vector<Remapping> const& remappings() const noexcept { return m_remappings; }
SourceUnitName apply(ImportPath const& _path, std::string const& _context) const;
// Parses a remapping of the format "context:prefix=target".
static std::optional<Remapping> parseRemapping(std::string const& _remapping);
private:
/// list of path prefix remappings, e.g. mylibrary: github.com/ethereum = /usr/local/ethereum
/// "context:prefix=target"
std::vector<Remapping> m_remappings = {};
};
}
+3 -2
View File
@@ -22,6 +22,7 @@
*/
#include <libsolidity/interface/StandardCompiler.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <libsolidity/ast/ASTJsonConverter.h>
#include <libyul/AssemblyStack.h>
@@ -810,7 +811,7 @@ std::variant<StandardCompiler::InputsAndSettings, Json::Value> StandardCompiler:
{
if (!remapping.isString())
return formatFatalError("JSONError", "\"settings.remappings\" must be an array of strings");
if (auto r = CompilerStack::parseRemapping(remapping.asString()))
if (auto r = ImportRemapper::parseRemapping(remapping.asString()))
ret.remappings.emplace_back(std::move(*r));
else
return formatFatalError("JSONError", "Invalid remapping: \"" + remapping.asString() + "\"");
@@ -941,7 +942,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
compilerStack.setViaIR(_inputsAndSettings.viaIR);
compilerStack.setEVMVersion(_inputsAndSettings.evmVersion);
compilerStack.setParserErrorRecovery(_inputsAndSettings.parserErrorRecovery);
compilerStack.setRemappings(_inputsAndSettings.remappings);
compilerStack.setRemappings(move(_inputsAndSettings.remappings));
compilerStack.setOptimiserSettings(std::move(_inputsAndSettings.optimiserSettings));
compilerStack.setRevertStringBehaviour(_inputsAndSettings.revertStrings);
compilerStack.setLibraries(_inputsAndSettings.libraries);
+1 -1
View File
@@ -64,7 +64,7 @@ private:
std::map<std::string, std::string> sources;
std::map<util::h256, std::string> smtLib2Responses;
langutil::EVMVersion evmVersion;
std::vector<CompilerStack::Remapping> remappings;
std::vector<ImportRemapper::Remapping> remappings;
RevertStrings revertStrings = RevertStrings::Default;
OptimiserSettings optimiserSettings = OptimiserSettings::minimal();
std::map<std::string, util::h160> libraries;