diff --git a/libsolidity/CompilerStack.h b/libsolidity/CompilerStack.h index cae0f4e27..7ad3405e1 100644 --- a/libsolidity/CompilerStack.h +++ b/libsolidity/CompilerStack.h @@ -60,7 +60,7 @@ class CompilerStack: boost::noncopyable { public: /// Creates a new compiler stack. Adds standard sources if @a _addStandardSources. - explicit CompilerStack(bool _addStandardSources = true); + explicit CompilerStack(bool _addStandardSources = false); /// Adds a source object (e.g. file) to the parser. After this, parse has to be called again. /// @returns true if a source object by the name already existed and was replaced. diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 87edb24b0..aa651eb41 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -61,6 +61,7 @@ static string const g_argBinaryStr = "binary"; static string const g_argOpcodesStr = "opcodes"; static string const g_argNatspecDevStr = "natspec-dev"; static string const g_argNatspecUserStr = "natspec-user"; +static string const g_argAddStandard = "add-std"; static void version() { @@ -116,13 +117,13 @@ void CommandLineInterface::handleBinary(string const& _contract) if (outputToStdout(choice)) { cout << "Binary: " << endl; - cout << toHex(m_compiler.getBytecode(_contract)) << endl; + cout << toHex(m_compiler->getBytecode(_contract)) << endl; } if (outputToFile(choice)) { ofstream outFile(_contract + ".binary"); - outFile << toHex(m_compiler.getBytecode(_contract)); + outFile << toHex(m_compiler->getBytecode(_contract)); outFile.close(); } } @@ -133,14 +134,14 @@ void CommandLineInterface::handleOpcode(string const& _contract) if (outputToStdout(choice)) { cout << "Opcodes: " << endl; - cout << eth::disassemble(m_compiler.getBytecode(_contract)); + cout << eth::disassemble(m_compiler->getBytecode(_contract)); cout << endl; } if (outputToFile(choice)) { ofstream outFile(_contract + ".opcode"); - outFile << eth::disassemble(m_compiler.getBytecode(_contract)); + outFile << eth::disassemble(m_compiler->getBytecode(_contract)); outFile.close(); } } @@ -191,13 +192,13 @@ void CommandLineInterface::handleMeta(DocumentationType _type, string const& _co if (outputToStdout(choice)) { cout << title << endl; - cout << m_compiler.getMetadata(_contract, _type) << endl; + cout << m_compiler->getMetadata(_contract, _type) << endl; } if (outputToFile(choice)) { ofstream outFile(_contract + suffix); - outFile << m_compiler.getMetadata(_contract, _type); + outFile << m_compiler->getMetadata(_contract, _type); outFile.close(); } } @@ -211,6 +212,7 @@ bool CommandLineInterface::parseArguments(int argc, char** argv) ("help", "Show help message and exit") ("version", "Show version and exit") ("optimize", po::value()->default_value(false), "Optimize bytecode for size") + ("add-std", po::value()->default_value(false), "Add standard contracts") ("input-file", po::value>(), "input file") (g_argAstStr.c_str(), po::value()->value_name("stdout|file|both"), "Request to output the AST of the contract.") @@ -292,31 +294,32 @@ bool CommandLineInterface::processInput() m_sourceCodes[infile] = asString(dev::contents(infile)); } + m_compiler.reset(new CompilerStack(m_args["add-std"].as())); try { for (auto const& sourceCode: m_sourceCodes) - m_compiler.addSource(sourceCode.first, sourceCode.second); + m_compiler->addSource(sourceCode.first, sourceCode.second); // TODO: Perhaps we should not compile unless requested - m_compiler.compile(m_args["optimize"].as()); + m_compiler->compile(m_args["optimize"].as()); } catch (ParserError const& _exception) { - SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Parser error", m_compiler); + SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Parser error", *m_compiler); return false; } catch (DeclarationError const& _exception) { - SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Declaration error", m_compiler); + SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Declaration error", *m_compiler); return false; } catch (TypeError const& _exception) { - SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Type error", m_compiler); + SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Type error", *m_compiler); return false; } catch (CompilerError const& _exception) { - SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Compiler error", m_compiler); + SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Compiler error", *m_compiler); return false; } catch (InternalCompilerError const& _exception) @@ -369,12 +372,12 @@ void CommandLineInterface::handleAst(string const& _argStr) cout << endl << "======= " << sourceCode.first << " =======" << endl; if (_argStr == g_argAstStr) { - ASTPrinter printer(m_compiler.getAST(sourceCode.first), sourceCode.second); + ASTPrinter printer(m_compiler->getAST(sourceCode.first), sourceCode.second); printer.print(cout); } else { - ASTJsonConverter converter(m_compiler.getAST(sourceCode.first)); + ASTJsonConverter converter(m_compiler->getAST(sourceCode.first)); converter.print(cout); } } @@ -388,12 +391,12 @@ void CommandLineInterface::handleAst(string const& _argStr) ofstream outFile(p.stem().string() + ".ast"); if (_argStr == g_argAstStr) { - ASTPrinter printer(m_compiler.getAST(sourceCode.first), sourceCode.second); + ASTPrinter printer(m_compiler->getAST(sourceCode.first), sourceCode.second); printer.print(outFile); } else { - ASTJsonConverter converter(m_compiler.getAST(sourceCode.first)); + ASTJsonConverter converter(m_compiler->getAST(sourceCode.first)); converter.print(outFile); } outFile.close(); @@ -408,7 +411,7 @@ void CommandLineInterface::actOnInput() handleAst(g_argAstStr); handleAst(g_argAstJson); - vector contracts = m_compiler.getContractNames(); + vector contracts = m_compiler->getContractNames(); for (string const& contract: contracts) { if (needStdout(m_args)) @@ -421,13 +424,13 @@ void CommandLineInterface::actOnInput() if (outputToStdout(choice)) { cout << "EVM assembly:" << endl; - m_compiler.streamAssembly(cout, contract); + m_compiler->streamAssembly(cout, contract); } if (outputToFile(choice)) { ofstream outFile(contract + ".evm"); - m_compiler.streamAssembly(outFile, contract); + m_compiler->streamAssembly(outFile, contract); outFile.close(); } } diff --git a/solc/CommandLineInterface.h b/solc/CommandLineInterface.h index 2862773ba..79029f9d1 100644 --- a/solc/CommandLineInterface.h +++ b/solc/CommandLineInterface.h @@ -21,9 +21,9 @@ */ #pragma once -#include - #include +#include +#include namespace dev { @@ -65,7 +65,7 @@ private: /// map of input files to source code strings std::map m_sourceCodes; /// Solidity compiler stack - dev::solidity::CompilerStack m_compiler; + std::unique_ptr m_compiler; }; } diff --git a/test/SolidityEndToEndTest.cpp b/test/SolidityEndToEndTest.cpp index 1ddb22731..5d726f0d0 100644 --- a/test/SolidityEndToEndTest.cpp +++ b/test/SolidityEndToEndTest.cpp @@ -1905,6 +1905,7 @@ BOOST_AUTO_TEST_CASE(use_std_lib) import "mortal"; contract Icarus is mortal { } )"; + m_addStandardSources = true; u256 amount(130); u160 address(23); compileAndRun(sourceCode, amount, "Icarus"); diff --git a/test/solidityExecutionFramework.h b/test/solidityExecutionFramework.h index 208e9ae80..7dad9ad44 100644 --- a/test/solidityExecutionFramework.h +++ b/test/solidityExecutionFramework.h @@ -45,8 +45,7 @@ public: bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") { - // add standard sources only if contract name is given - dev::solidity::CompilerStack compiler(!_contractName.empty()); + dev::solidity::CompilerStack compiler(m_addStandardSources); try { compiler.addSource("", _sourceCode); @@ -175,6 +174,7 @@ private: protected: bool m_optimize = false; + bool m_addStandardSources = false; Address m_sender; Address m_contractAddress; eth::State m_state;