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

This commit is contained in:
Christian Parpart 2021-03-29 14:46:19 +02:00
parent 30588a90eb
commit 3490577140
10 changed files with 184 additions and 85 deletions

View File

@ -133,6 +133,8 @@ set(sources
interface/CompilerStack.cpp interface/CompilerStack.cpp
interface/CompilerStack.h interface/CompilerStack.h
interface/DebugSettings.h interface/DebugSettings.h
interface/ImportRemapper.cpp
interface/ImportRemapper.h
interface/GasEstimator.cpp interface/GasEstimator.cpp
interface/GasEstimator.h interface/GasEstimator.h
interface/Natspec.cpp interface/Natspec.cpp

View File

@ -24,6 +24,7 @@
#include <libsolidity/interface/CompilerStack.h> #include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <libsolidity/analysis/ControlFlowAnalyzer.h> #include <libsolidity/analysis/ControlFlowAnalyzer.h>
#include <libsolidity/analysis/ControlFlowGraph.h> #include <libsolidity/analysis/ControlFlowGraph.h>
@ -196,33 +197,13 @@ void CompilerStack::findAndReportCyclicContractDependencies()
} }
} }
std::optional<CompilerStack::Remapping> CompilerStack::parseRemapping(string const& _remapping) void CompilerStack::setRemappings(vector<ImportRemapper::Remapping> _remappings)
{
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)
{ {
if (m_stackState >= ParsedAndImported) if (m_stackState >= ParsedAndImported)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Must set remappings before parsing.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Must set remappings before parsing."));
for (auto const& remapping: _remappings) for (auto const& remapping: _remappings)
solAssert(!remapping.prefix.empty(), ""); solAssert(!remapping.prefix.empty(), "");
m_remappings = _remappings; m_importRemapper.setRemappings(move(_remappings));
} }
void CompilerStack::setViaIR(bool _viaIR) void CompilerStack::setViaIR(bool _viaIR)
@ -312,7 +293,7 @@ void CompilerStack::reset(bool _keepSettings)
m_unhandledSMTLib2Queries.clear(); m_unhandledSMTLib2Queries.clear();
if (!_keepSettings) if (!_keepSettings)
{ {
m_remappings.clear(); m_importRemapper.clear();
m_libraries.clear(); m_libraries.clear();
m_viaIR = false; m_viaIR = false;
m_evmVersion = langutil::EVMVersion(); 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) string CompilerStack::applyRemapping(string const& _path, string const& _context)
{ {
solAssert(m_stackState < ParsedAndImported, ""); solAssert(m_stackState < ParsedAndImported, "");
// Try to find the longest prefix match in all remappings that are active in the current context. return m_importRemapper.apply(_path, _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;
} }
void CompilerStack::resolveImports() void CompilerStack::resolveImports()
@ -1584,7 +1529,7 @@ string CompilerStack::createMetadata(Contract const& _contract) const
meta["settings"]["remappings"] = Json::arrayValue; meta["settings"]["remappings"] = Json::arrayValue;
set<string> remappings; set<string> remappings;
for (auto const& r: m_remappings) for (auto const& r: m_importRemapper.remappings())
remappings.insert(r.context + ":" + r.prefix + "=" + r.target); remappings.insert(r.context + ":" + r.prefix + "=" + r.target);
for (auto const& r: remappings) for (auto const& r: remappings)
meta["settings"]["remappings"].append(r); meta["settings"]["remappings"].append(r);

View File

@ -26,6 +26,7 @@
#include <libsolidity/analysis/FunctionCallGraph.h> #include <libsolidity/analysis/FunctionCallGraph.h>
#include <libsolidity/interface/ReadFile.h> #include <libsolidity/interface/ReadFile.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <libsolidity/interface/OptimiserSettings.h> #include <libsolidity/interface/OptimiserSettings.h>
#include <libsolidity/interface/Version.h> #include <libsolidity/interface/Version.h>
#include <libsolidity/interface/DebugSettings.h> #include <libsolidity/interface/DebugSettings.h>
@ -111,13 +112,6 @@ public:
None None
}; };
struct Remapping
{
std::string context;
std::string prefix;
std::string target;
};
/// Creates a new compiler stack. /// Creates a new compiler stack.
/// @param _readFile callback used to read files for import statements. Must return /// @param _readFile callback used to read files for import statements. Must return
/// and must not emit exceptions. /// and must not emit exceptions.
@ -139,12 +133,9 @@ public:
/// all settings are reset as well. /// all settings are reset as well.
void reset(bool _keepSettings = false); 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. /// Sets path remappings.
/// Must be set before parsing. /// 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. /// Sets library addresses. Addresses are cleared iff @a _libraries is missing.
/// Must be set before parsing. /// Must be set before parsing.
@ -487,9 +478,7 @@ private:
bool m_generateIR = false; bool m_generateIR = false;
bool m_generateEwasm = false; bool m_generateEwasm = false;
std::map<std::string, util::h160> m_libraries; std::map<std::string, util::h160> m_libraries;
/// list of path prefix remappings, e.g. mylibrary: github.com/ethereum = /usr/local/ethereum ImportRemapper m_importRemapper;
/// "context:prefix=target"
std::vector<Remapping> m_remappings;
std::map<std::string const, Source> m_sources; std::map<std::string const, Source> m_sources;
// if imported, store AST-JSONS for each filename // if imported, store AST-JSONS for each filename
std::map<std::string, Json::Value> m_sourceJsons; std::map<std::string, Json::Value> m_sourceJsons;

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;
}
}

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 = {};
};
}

View File

@ -22,6 +22,7 @@
*/ */
#include <libsolidity/interface/StandardCompiler.h> #include <libsolidity/interface/StandardCompiler.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <libsolidity/ast/ASTJsonConverter.h> #include <libsolidity/ast/ASTJsonConverter.h>
#include <libyul/AssemblyStack.h> #include <libyul/AssemblyStack.h>
@ -810,7 +811,7 @@ std::variant<StandardCompiler::InputsAndSettings, Json::Value> StandardCompiler:
{ {
if (!remapping.isString()) if (!remapping.isString())
return formatFatalError("JSONError", "\"settings.remappings\" must be an array of strings"); 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)); ret.remappings.emplace_back(std::move(*r));
else else
return formatFatalError("JSONError", "Invalid remapping: \"" + remapping.asString() + "\""); return formatFatalError("JSONError", "Invalid remapping: \"" + remapping.asString() + "\"");
@ -941,7 +942,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
compilerStack.setViaIR(_inputsAndSettings.viaIR); compilerStack.setViaIR(_inputsAndSettings.viaIR);
compilerStack.setEVMVersion(_inputsAndSettings.evmVersion); compilerStack.setEVMVersion(_inputsAndSettings.evmVersion);
compilerStack.setParserErrorRecovery(_inputsAndSettings.parserErrorRecovery); compilerStack.setParserErrorRecovery(_inputsAndSettings.parserErrorRecovery);
compilerStack.setRemappings(_inputsAndSettings.remappings); compilerStack.setRemappings(move(_inputsAndSettings.remappings));
compilerStack.setOptimiserSettings(std::move(_inputsAndSettings.optimiserSettings)); compilerStack.setOptimiserSettings(std::move(_inputsAndSettings.optimiserSettings));
compilerStack.setRevertStringBehaviour(_inputsAndSettings.revertStrings); compilerStack.setRevertStringBehaviour(_inputsAndSettings.revertStrings);
compilerStack.setLibraries(_inputsAndSettings.libraries); compilerStack.setLibraries(_inputsAndSettings.libraries);

View File

@ -64,7 +64,7 @@ private:
std::map<std::string, std::string> sources; std::map<std::string, std::string> sources;
std::map<util::h256, std::string> smtLib2Responses; std::map<util::h256, std::string> smtLib2Responses;
langutil::EVMVersion evmVersion; langutil::EVMVersion evmVersion;
std::vector<CompilerStack::Remapping> remappings; std::vector<ImportRemapper::Remapping> remappings;
RevertStrings revertStrings = RevertStrings::Default; RevertStrings revertStrings = RevertStrings::Default;
OptimiserSettings optimiserSettings = OptimiserSettings::minimal(); OptimiserSettings optimiserSettings = OptimiserSettings::minimal();
std::map<std::string, util::h160> libraries; std::map<std::string, util::h160> libraries;

View File

@ -35,6 +35,7 @@
#include <libsolidity/interface/StandardCompiler.h> #include <libsolidity/interface/StandardCompiler.h>
#include <libsolidity/interface/GasEstimator.h> #include <libsolidity/interface/GasEstimator.h>
#include <libsolidity/interface/DebugSettings.h> #include <libsolidity/interface/DebugSettings.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <libsolidity/interface/StorageLayout.h> #include <libsolidity/interface/StorageLayout.h>
#include <libyul/AssemblyStack.h> #include <libyul/AssemblyStack.h>
@ -579,7 +580,7 @@ bool CommandLineInterface::readInputFilesAndConfigureRemappings()
auto eq = find(path.begin(), path.end(), '='); auto eq = find(path.begin(), path.end(), '=');
if (eq != path.end()) if (eq != path.end())
{ {
if (auto r = CompilerStack::parseRemapping(path)) if (auto r = ImportRemapper::parseRemapping(path))
{ {
m_remappings.emplace_back(std::move(*r)); m_remappings.emplace_back(std::move(*r));
path = string(eq + 1, path.end()); path = string(eq + 1, path.end());

View File

@ -24,6 +24,7 @@
#include <libsolidity/interface/CompilerStack.h> #include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/interface/DebugSettings.h> #include <libsolidity/interface/DebugSettings.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <libyul/AssemblyStack.h> #include <libyul/AssemblyStack.h>
#include <liblangutil/EVMVersion.h> #include <liblangutil/EVMVersion.h>
@ -117,7 +118,7 @@ private:
/// map of input files to source code strings /// map of input files to source code strings
std::map<std::string, std::string> m_sourceCodes; std::map<std::string, std::string> m_sourceCodes;
/// list of remappings /// list of remappings
std::vector<frontend::CompilerStack::Remapping> m_remappings; std::vector<ImportRemapper::Remapping> m_remappings;
/// list of allowed directories to read files from /// list of allowed directories to read files from
std::vector<boost::filesystem::path> m_allowedDirectories; std::vector<boost::filesystem::path> m_allowedDirectories;
/// Base path, used for resolving relative paths in imports. /// Base path, used for resolving relative paths in imports.

View File

@ -26,6 +26,7 @@
#include <liblangutil/Exceptions.h> #include <liblangutil/Exceptions.h>
#include <libsolidity/interface/CompilerStack.h> #include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
@ -41,7 +42,7 @@ BOOST_AUTO_TEST_SUITE(SolidityImports)
BOOST_AUTO_TEST_CASE(remappings) BOOST_AUTO_TEST_CASE(remappings)
{ {
CompilerStack c; CompilerStack c;
c.setRemappings(vector<CompilerStack::Remapping>{{"", "s", "s_1.4.6"},{"", "t", "Tee"}}); c.setRemappings(vector<ImportRemapper::Remapping>{{"", "s", "s_1.4.6"},{"", "t", "Tee"}});
c.setSources({ c.setSources({
{"a", "import \"s/s.sol\"; contract A is S {} pragma solidity >=0.0;"}, {"a", "import \"s/s.sol\"; contract A is S {} pragma solidity >=0.0;"},
{"b", "import \"t/tee.sol\"; contract A is Tee {} pragma solidity >=0.0;"}, {"b", "import \"t/tee.sol\"; contract A is Tee {} pragma solidity >=0.0;"},
@ -55,7 +56,7 @@ BOOST_AUTO_TEST_CASE(remappings)
BOOST_AUTO_TEST_CASE(context_dependent_remappings) BOOST_AUTO_TEST_CASE(context_dependent_remappings)
{ {
CompilerStack c; CompilerStack c;
c.setRemappings(vector<CompilerStack::Remapping>{{"a", "s", "s_1.4.6"}, {"b", "s", "s_1.4.7"}}); c.setRemappings(vector<ImportRemapper::Remapping>{{"a", "s", "s_1.4.6"}, {"b", "s", "s_1.4.7"}});
c.setSources({ c.setSources({
{"a/a.sol", "import \"s/s.sol\"; contract A is SSix {} pragma solidity >=0.0;"}, {"a/a.sol", "import \"s/s.sol\"; contract A is SSix {} pragma solidity >=0.0;"},
{"b/b.sol", "import \"s/s.sol\"; contract B is SSeven {} pragma solidity >=0.0;"}, {"b/b.sol", "import \"s/s.sol\"; contract B is SSeven {} pragma solidity >=0.0;"},
@ -69,7 +70,7 @@ BOOST_AUTO_TEST_CASE(context_dependent_remappings)
BOOST_AUTO_TEST_CASE(context_dependent_remappings_ensure_default_and_module_preserved) BOOST_AUTO_TEST_CASE(context_dependent_remappings_ensure_default_and_module_preserved)
{ {
CompilerStack c; CompilerStack c;
c.setRemappings(vector<CompilerStack::Remapping>{ c.setRemappings(vector<ImportRemapper::Remapping>{
{"", "foo", "vendor/foo_2.0.0"}, {"", "foo", "vendor/foo_2.0.0"},
{"vendor/bar", "foo", "vendor/foo_1.0.0"}, {"vendor/bar", "foo", "vendor/foo_1.0.0"},
{"", "bar", "vendor/bar"} {"", "bar", "vendor/bar"}
@ -87,7 +88,7 @@ BOOST_AUTO_TEST_CASE(context_dependent_remappings_ensure_default_and_module_pres
BOOST_AUTO_TEST_CASE(context_dependent_remappings_order_independent_1) BOOST_AUTO_TEST_CASE(context_dependent_remappings_order_independent_1)
{ {
CompilerStack c; CompilerStack c;
c.setRemappings(vector<CompilerStack::Remapping>{{"a", "x/y/z", "d"}, {"a/b", "x", "e"}}); c.setRemappings(vector<ImportRemapper::Remapping>{{"a", "x/y/z", "d"}, {"a/b", "x", "e"}});
c.setSources({ c.setSources({
{"a/main.sol", "import \"x/y/z/z.sol\"; contract Main is D {} pragma solidity >=0.0;"}, {"a/main.sol", "import \"x/y/z/z.sol\"; contract Main is D {} pragma solidity >=0.0;"},
{"a/b/main.sol", "import \"x/y/z/z.sol\"; contract Main is E {} pragma solidity >=0.0;"}, {"a/b/main.sol", "import \"x/y/z/z.sol\"; contract Main is E {} pragma solidity >=0.0;"},
@ -101,7 +102,7 @@ BOOST_AUTO_TEST_CASE(context_dependent_remappings_order_independent_1)
BOOST_AUTO_TEST_CASE(context_dependent_remappings_order_independent_2) BOOST_AUTO_TEST_CASE(context_dependent_remappings_order_independent_2)
{ {
CompilerStack c; CompilerStack c;
c.setRemappings(vector<CompilerStack::Remapping>{{"a/b", "x", "e"}, {"a", "x/y/z", "d"}}); c.setRemappings(vector<ImportRemapper::Remapping>{{"a/b", "x", "e"}, {"a", "x/y/z", "d"}});
c.setSources({ c.setSources({
{"a/main.sol", "import \"x/y/z/z.sol\"; contract Main is D {} pragma solidity >=0.0;"}, {"a/main.sol", "import \"x/y/z/z.sol\"; contract Main is D {} pragma solidity >=0.0;"},
{"a/b/main.sol", "import \"x/y/z/z.sol\"; contract Main is E {} pragma solidity >=0.0;"}, {"a/b/main.sol", "import \"x/y/z/z.sol\"; contract Main is E {} pragma solidity >=0.0;"},