From 623b8eb107a97861e3e7e0c13acee39c8d5f4075 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 10 Apr 2017 12:48:41 +0100 Subject: [PATCH] Pull out ReadFile from CompilerStack --- libsolidity/interface/CompilerStack.cpp | 4 +-- libsolidity/interface/CompilerStack.h | 14 ++------ libsolidity/interface/ReadFile.h | 45 +++++++++++++++++++++++++ solc/CommandLineInterface.cpp | 10 +++--- solc/jsonCompiler.cpp | 4 +-- 5 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 libsolidity/interface/ReadFile.h diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index effc83093..9ad285733 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -55,7 +55,7 @@ using namespace std; using namespace dev; using namespace dev::solidity; -CompilerStack::CompilerStack(ReadFileCallback const& _readFile): +CompilerStack::CompilerStack(ReadFile::Callback const& _readFile): m_readFile(_readFile), m_parseSuccessful(false) {} void CompilerStack::setRemappings(vector const& _remappings) @@ -522,7 +522,7 @@ StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast, std::string if (m_sources.count(importPath) || newSources.count(importPath)) continue; - ReadFileResult result{false, string("File not supplied initially.")}; + ReadFile::Result result{false, string("File not supplied initially.")}; if (m_readFile) result = m_readFile(importPath); diff --git a/libsolidity/interface/CompilerStack.h b/libsolidity/interface/CompilerStack.h index 65850683e..9c37eead0 100644 --- a/libsolidity/interface/CompilerStack.h +++ b/libsolidity/interface/CompilerStack.h @@ -36,6 +36,7 @@ #include #include #include +#include namespace dev { @@ -77,18 +78,9 @@ enum class DocumentationType: uint8_t class CompilerStack: boost::noncopyable { public: - struct ReadFileResult - { - bool success; - std::string contentsOrErrorMessage; - }; - - /// File reading callback. - using ReadFileCallback = std::function; - /// Creates a new compiler stack. /// @param _readFile callback to used to read files for import statements. Should return - explicit CompilerStack(ReadFileCallback const& _readFile = ReadFileCallback()); + explicit CompilerStack(ReadFile::Callback const& _readFile = ReadFile::Callback()); /// Sets path remappings in the format "context:prefix=target" void setRemappings(std::vector const& _remappings); @@ -263,7 +255,7 @@ private: std::string target; }; - ReadFileCallback m_readFile; + ReadFile::Callback m_readFile; bool m_optimize = false; unsigned m_optimizeRuns = 200; std::map m_libraries; diff --git a/libsolidity/interface/ReadFile.h b/libsolidity/interface/ReadFile.h new file mode 100644 index 000000000..2e8a6bd83 --- /dev/null +++ b/libsolidity/interface/ReadFile.h @@ -0,0 +1,45 @@ +/* + 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 . +*/ + +#pragma once + +#include +#include +#include + +namespace dev +{ + +namespace solidity +{ + +class ReadFile: boost::noncopyable +{ +public: + /// File reading result. + struct Result + { + bool success; + std::string contentsOrErrorMessage; + }; + + /// File reading callback. + using Callback = std::function; +}; + +} +} diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 31f70272a..f0b731524 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -630,11 +630,11 @@ bool CommandLineInterface::processInput() return link(); } - CompilerStack::ReadFileCallback fileReader = [this](string const& _path) + ReadFile::Callback fileReader = [this](string const& _path) { auto path = boost::filesystem::path(_path); if (!boost::filesystem::exists(path)) - return CompilerStack::ReadFileResult{false, "File not found."}; + return ReadFile::Result{false, "File not found."}; auto canonicalPath = boost::filesystem::canonical(path); bool isAllowed = false; for (auto const& allowedDir: m_allowedDirectories) @@ -650,14 +650,14 @@ bool CommandLineInterface::processInput() } } if (!isAllowed) - return CompilerStack::ReadFileResult{false, "File outside of allowed directories."}; + return ReadFile::Result{false, "File outside of allowed directories."}; else if (!boost::filesystem::is_regular_file(canonicalPath)) - return CompilerStack::ReadFileResult{false, "Not a valid file."}; + return ReadFile::Result{false, "Not a valid file."}; else { auto contents = dev::contentsString(canonicalPath.string()); m_sourceCodes[path.string()] = contents; - return CompilerStack::ReadFileResult{true, contents}; + return ReadFile::Result{true, contents}; } }; diff --git a/solc/jsonCompiler.cpp b/solc/jsonCompiler.cpp index 453221177..74d6a901e 100644 --- a/solc/jsonCompiler.cpp +++ b/solc/jsonCompiler.cpp @@ -130,7 +130,7 @@ string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback { Json::Value output(Json::objectValue); Json::Value errors(Json::arrayValue); - CompilerStack::ReadFileCallback readCallback; + ReadFile::Callback readCallback; if (_readCallback) { readCallback = [=](string const& _path) @@ -138,7 +138,7 @@ string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback char* contents_c = nullptr; char* error_c = nullptr; _readCallback(_path.c_str(), &contents_c, &error_c); - CompilerStack::ReadFileResult result; + ReadFile::Result result; result.success = true; if (!contents_c && !error_c) {