From f8228e8ab116799a1b28ea9cbb01fdd7342b395c Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 9 Dec 2015 17:35:20 +0100 Subject: Relative paths in import directives. --- libsolidity/interface/CompilerStack.cpp | 29 ++++++++++++++++++++++++----- libsolidity/interface/CompilerStack.h | 2 ++ 2 files changed, 26 insertions(+), 5 deletions(-) (limited to 'libsolidity/interface') diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index 38fe5956..7f097523 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -22,6 +22,7 @@ */ #include +#include #include #include #include @@ -367,7 +368,7 @@ void CompilerStack::resolveImports() vector sourceOrder; set sourcesSeen; - function toposort = [&](Source const* _source) + function toposort = [&](string const& _sourceName, Source const* _source) { if (sourcesSeen.count(_source)) return; @@ -375,26 +376,44 @@ void CompilerStack::resolveImports() for (ASTPointer const& node: _source->ast->nodes()) if (ImportDirective const* import = dynamic_cast(node.get())) { - string const& id = import->identifier(); - if (!m_sources.count(id)) + string path = absolutePath(import->identifier(), _sourceName); + import->annotation().absolutePath = path; + if (!m_sources.count(path)) BOOST_THROW_EXCEPTION( Error(Error::Type::ParserError) << errinfo_sourceLocation(import->location()) << errinfo_comment("Source not found.") ); - toposort(&m_sources[id]); + toposort(path, &m_sources[path]); } sourceOrder.push_back(_source); }; for (auto const& sourcePair: m_sources) if (!sourcePair.second.isLibrary) - toposort(&sourcePair.second); + toposort(sourcePair.first, &sourcePair.second); swap(m_sourceOrder, sourceOrder); } +string CompilerStack::absolutePath(string const& _path, string const& _reference) const +{ + // Anything that does not start with `.` is an absolute path. + if (_path.empty() || _path.front() != '.') + return _path; + using path = boost::filesystem::path; + path p(_path); + path result(_reference); + result.remove_filename(); + for (path::iterator it = p.begin(); it != p.end(); ++it) + if (*it == "..") + result = result.parent_path(); + else if (*it != ".") + result /= *it; + return result.string(); +} + void CompilerStack::compileContract( bool _optimize, unsigned _runs, diff --git a/libsolidity/interface/CompilerStack.h b/libsolidity/interface/CompilerStack.h index 0473d58b..3e6dc456 100644 --- a/libsolidity/interface/CompilerStack.h +++ b/libsolidity/interface/CompilerStack.h @@ -199,6 +199,8 @@ private: }; void resolveImports(); + /// @returns the absolute path corresponding to @a _path relative to @a _reference. + std::string absolutePath(std::string const& _path, std::string const& _reference) const; /// Compile a single contract and put the result in @a _compiledContracts. void compileContract( bool _optimize, -- cgit