From d4d0b07c35123590230b30096fca249643b93221 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 12:42:29 +0100 Subject: [PATCH 01/11] using boost::program_options for argument parsing --- CMakeLists.txt | 1 + main.cpp | 59 +++++++++++++++++++++++++++----------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 386d4a1a8..689700a64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(EXECUTABLE solc) add_executable(${EXECUTABLE} ${SRC_LIST}) +target_link_libraries(${EXECUTABLE} boost_program_options) target_link_libraries(${EXECUTABLE} solidity) install( TARGETS ${EXECUTABLE} DESTINATION bin ) diff --git a/main.cpp b/main.cpp index 61f73b45d..1f8348746 100644 --- a/main.cpp +++ b/main.cpp @@ -23,6 +23,9 @@ #include #include +#include + +#include "BuildInfo.h" #include #include #include @@ -38,43 +41,45 @@ using namespace std; using namespace dev; using namespace solidity; - -void help() -{ - cout << "Usage solc [OPTIONS] " << endl - << "Options:" << endl - << " -o,--optimize Optimize the bytecode for size." << endl - << " -h,--help Show this help message and exit." << endl - << " -V,--version Show the version and exit." << endl; - exit(0); -} +namespace po = boost::program_options; void version() { cout << "solc, the solidity complier commandline interface " << dev::Version << endl - << " by Christian , (c) 2014." << endl + << " by Christian and Lefteris , (c) 2014." << endl << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl; exit(0); } int main(int argc, char** argv) { - vector infiles; - bool optimize = false; - for (int i = 1; i < argc; ++i) - { - string arg = argv[i]; - if (arg == "-o" || arg == "--optimize") - optimize = true; - else if (arg == "-h" || arg == "--help") - help(); - else if (arg == "-V" || arg == "--version") - version(); - else - infiles.push_back(argv[i]); + // Declare the supported options. + po::options_description desc("Allowed options"); + desc.add_options() + ("help", "Show help message and exit") + ("version", "Show version and exit") + ("optimize", po::value()->default_value(false), "Optimize bytecode for size") + ("input-file", po::value>(), "input file"); + + // All positional options should be interpreted as input files + po::positional_options_description p; + p.add("input-file", -1); + + po::variables_map vm; + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); + po::notify(vm); + + if (vm.count("help")) { + cout << desc; + return 0; + } + + if (vm.count("version")) { + version(); + return 0; } map sourceCodes; - if (infiles.empty()) + if (!vm.count("input-file")) { string s; while (!cin.eof()) @@ -84,7 +89,7 @@ int main(int argc, char** argv) } } else - for (string const& infile: infiles) + for (string const& infile: vm["input-file"].as>()) sourceCodes[infile] = asString(dev::contents(infile)); CompilerStack compiler; @@ -92,7 +97,7 @@ int main(int argc, char** argv) { for (auto const& sourceCode: sourceCodes) compiler.addSource(sourceCode.first, sourceCode.second); - compiler.compile(optimize); + compiler.compile( vm["optimize"].as()); } catch (ParserError const& exception) { From 7193ac2edc3ea63aa5712a10ce0b8738eb6ebf6a Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 13:30:55 +0100 Subject: [PATCH 02/11] Unknown solc arguments are now ignored --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 1f8348746..6b16c1249 100644 --- a/main.cpp +++ b/main.cpp @@ -66,7 +66,7 @@ int main(int argc, char** argv) p.add("input-file", -1); po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), vm); po::notify(vm); if (vm.count("help")) { From 3cb4562e5d6c58623a4868ea8d46cea65f9f4ca6 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 14:46:00 +0100 Subject: [PATCH 03/11] Solc cmdline option for ast outputting either to stdout or a file --- CMakeLists.txt | 1 + main.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 689700a64..84f87eb53 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(EXECUTABLE solc) add_executable(${EXECUTABLE} ${SRC_LIST}) +target_link_libraries(${EXECUTABLE} boost_filesystem) target_link_libraries(${EXECUTABLE} boost_program_options) target_link_libraries(${EXECUTABLE} solidity) diff --git a/main.cpp b/main.cpp index 6b16c1249..c79ba52a6 100644 --- a/main.cpp +++ b/main.cpp @@ -22,8 +22,10 @@ #include #include +#include #include +#include #include "BuildInfo.h" #include @@ -51,6 +53,28 @@ void version() exit(0); } +enum class AstOutput +{ + STDOUT, + FILE, + BOTH +}; + +std::istream& operator>>(std::istream& _in, AstOutput& io_output) +{ + std::string token; + _in >> token; + if (token == "stdout") + io_output = AstOutput::STDOUT; + else if (token == "file") + io_output = AstOutput::FILE; + else if (token == "both") + io_output = AstOutput::BOTH; + else + throw boost::program_options::invalid_option_value(token); + return _in; +} + int main(int argc, char** argv) { // Declare the supported options. @@ -59,14 +83,27 @@ int main(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") - ("input-file", po::value>(), "input file"); + ("input-file", po::value>(), "input file") + ("ast", po::value(), + "Request to output the AST of the contract. Legal values:\n" + "\tstdout: Print it to standar output\n" + "\tfile: Print it to a file with same name\n"); // All positional options should be interpreted as input files po::positional_options_description p; p.add("input-file", -1); + // parse the compiler arguments po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), vm); + try + { + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), vm); + } + catch (po::error const& exception) + { + cout << exception.what() << endl; + return -1; + } po::notify(vm); if (vm.count("help")) { @@ -135,13 +172,36 @@ int main(int argc, char** argv) return -1; } - cout << "Syntax trees:" << endl << endl; - for (auto const& sourceCode: sourceCodes) + + // do we need AST output? + if (vm.count("ast")) { - cout << endl << "======= " << sourceCode.first << " =======" << endl; - ASTPrinter printer(compiler.getAST(sourceCode.first), sourceCode.second); - printer.print(cout); + auto choice = vm["ast"].as(); + if (choice != AstOutput::FILE) + { + cout << "Syntax trees:" << endl << endl; + for (auto const& sourceCode: sourceCodes) + { + cout << endl << "======= " << sourceCode.first << " =======" << endl; + ASTPrinter printer(compiler.getAST(sourceCode.first), sourceCode.second); + printer.print(cout); + } + } + + if (choice != AstOutput::STDOUT) + { + for (auto const& sourceCode: sourceCodes) + { + boost::filesystem::path p(sourceCode.first); + ofstream outFile(p.stem().string() + ".ast"); + ASTPrinter printer(compiler.getAST(sourceCode.first), sourceCode.second); + printer.print(outFile); + outFile.close(); + } + } } + + vector contracts = compiler.getContractNames(); cout << endl << "Contracts:" << endl; for (string const& contract: contracts) From 501d6f4a2ccfa45782454a8229af3d6a2e727ad7 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 15:05:23 +0100 Subject: [PATCH 04/11] Solc evm assembly to either file or stdout option --- main.cpp | 56 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/main.cpp b/main.cpp index c79ba52a6..96e834ff1 100644 --- a/main.cpp +++ b/main.cpp @@ -53,23 +53,28 @@ void version() exit(0); } -enum class AstOutput +enum class OutputType { STDOUT, FILE, BOTH }; -std::istream& operator>>(std::istream& _in, AstOutput& io_output) +#define outputTypeStr "Legal values:\n"\ + "\tstdout: Print it to standard output\n"\ + "\tfile: Print it to a file with same name\n"\ + "\tboth: Print both to a file and the stdout\n" + +std::istream& operator>>(std::istream& _in, OutputType& io_output) { std::string token; _in >> token; if (token == "stdout") - io_output = AstOutput::STDOUT; + io_output = OutputType::STDOUT; else if (token == "file") - io_output = AstOutput::FILE; + io_output = OutputType::FILE; else if (token == "both") - io_output = AstOutput::BOTH; + io_output = OutputType::BOTH; else throw boost::program_options::invalid_option_value(token); return _in; @@ -84,10 +89,10 @@ int main(int argc, char** argv) ("version", "Show version and exit") ("optimize", po::value()->default_value(false), "Optimize bytecode for size") ("input-file", po::value>(), "input file") - ("ast", po::value(), - "Request to output the AST of the contract. Legal values:\n" - "\tstdout: Print it to standar output\n" - "\tfile: Print it to a file with same name\n"); + ("ast", po::value(), + "Request to output the AST of the contract. " outputTypeStr) + ("asm", po::value(), + "Request to output the EVM assembly of the contract. " outputTypeStr); // All positional options should be interpreted as input files po::positional_options_description p; @@ -176,8 +181,8 @@ int main(int argc, char** argv) // do we need AST output? if (vm.count("ast")) { - auto choice = vm["ast"].as(); - if (choice != AstOutput::FILE) + auto choice = vm["ast"].as(); + if (choice != OutputType::FILE) { cout << "Syntax trees:" << endl << endl; for (auto const& sourceCode: sourceCodes) @@ -188,7 +193,7 @@ int main(int argc, char** argv) } } - if (choice != AstOutput::STDOUT) + if (choice != OutputType::STDOUT) { for (auto const& sourceCode: sourceCodes) { @@ -201,14 +206,33 @@ int main(int argc, char** argv) } } - vector contracts = compiler.getContractNames(); + // do we need EVM assembly? + if (vm.count("asm")) + { + auto choice = vm["asm"].as(); + for (string const& contract: contracts) + { + if (choice != OutputType::FILE) + { + cout << endl << "======= " << contract << " =======" << endl + << "EVM assembly:" << endl; + compiler.streamAssembly(cout, contract); + } + + if (choice != OutputType::STDOUT) + { + ofstream outFile(contract + ".evm"); + compiler.streamAssembly(outFile, contract); + outFile.close(); + } + } + } + + cout << endl << "Contracts:" << endl; for (string const& contract: contracts) { - cout << endl << "======= " << contract << " =======" << endl - << "EVM assembly:" << endl; - compiler.streamAssembly(cout, contract); cout << "Opcodes:" << endl << eth::disassemble(compiler.getBytecode(contract)) << endl << "Binary: " << toHex(compiler.getBytecode(contract)) << endl From 5ccf5b5c9e884e4d5dc342a91b4f977c2986b39f Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 15:21:20 +0100 Subject: [PATCH 05/11] Solc option to output binary and opcode --- main.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index 96e834ff1..38295dcc3 100644 --- a/main.cpp +++ b/main.cpp @@ -92,7 +92,11 @@ int main(int argc, char** argv) ("ast", po::value(), "Request to output the AST of the contract. " outputTypeStr) ("asm", po::value(), - "Request to output the EVM assembly of the contract. " outputTypeStr); + "Request to output the EVM assembly of the contract. " outputTypeStr) + ("opcodes", po::value(), + "Request to output the Opcodes of the contract. " outputTypeStr) + ("binary", po::value(), + "Request to output the contract in binary (hexadecimal). " outputTypeStr); // All positional options should be interpreted as input files po::positional_options_description p; @@ -207,16 +211,17 @@ int main(int argc, char** argv) } vector contracts = compiler.getContractNames(); - // do we need EVM assembly? - if (vm.count("asm")) + for (string const& contract: contracts) { - auto choice = vm["asm"].as(); - for (string const& contract: contracts) + cout << endl << "======= " << contract << " =======" << endl; + + // do we need EVM assembly? + if (vm.count("asm")) { + auto choice = vm["asm"].as(); if (choice != OutputType::FILE) { - cout << endl << "======= " << contract << " =======" << endl - << "EVM assembly:" << endl; + cout << "EVM assembly:" << endl; compiler.streamAssembly(cout, contract); } @@ -227,16 +232,49 @@ int main(int argc, char** argv) outFile.close(); } } - } + // do we need opcodes? + if (vm.count("opcodes")) + { + auto choice = vm["opcodes"].as(); + if (choice != OutputType::FILE) + { + cout << "Opcodes:" << endl; + cout << eth::disassemble(compiler.getBytecode(contract)) << endl; + } + + if (choice != OutputType::STDOUT) + { + ofstream outFile(contract + ".opcodes"); + outFile << eth::disassemble(compiler.getBytecode(contract)); + outFile.close(); + } + } + + // do we need binary? + if (vm.count("binary")) + { + auto choice = vm["binary"].as(); + if (choice != OutputType::FILE) + { + cout << "Binary:" << endl; + cout << toHex(compiler.getBytecode(contract)) << endl; + } + + if (choice != OutputType::STDOUT) + { // TODO: Think, if we really want that? Could simply output to normal binary + ofstream outFile(contract + ".bin"); + outFile << toHex(compiler.getBytecode(contract)); + outFile.close(); + } + } + + } // end of contracts iteration cout << endl << "Contracts:" << endl; for (string const& contract: contracts) { - cout << "Opcodes:" << endl - << eth::disassemble(compiler.getBytecode(contract)) << endl - << "Binary: " << toHex(compiler.getBytecode(contract)) << endl - << "Interface specification: " << compiler.getJsonDocumentation(contract, DocumentationType::ABI_INTERFACE) << endl + cout << "Interface specification: " << compiler.getJsonDocumentation(contract, DocumentationType::ABI_INTERFACE) << endl << "Natspec user documentation: " << compiler.getJsonDocumentation(contract, DocumentationType::NATSPEC_USER) << endl << "Natspec developer documentation: " << compiler.getJsonDocumentation(contract, DocumentationType::NATSPEC_DEV) << endl; } From 88cbcdf98a1816b171fba2f56821a5a35474e0a2 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 16:42:56 +0100 Subject: [PATCH 06/11] Solc gets arguments for interface and documentation related output --- main.cpp | 155 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 109 insertions(+), 46 deletions(-) diff --git a/main.cpp b/main.cpp index 38295dcc3..8650d429c 100644 --- a/main.cpp +++ b/main.cpp @@ -80,6 +80,96 @@ std::istream& operator>>(std::istream& _in, OutputType& io_output) return _in; } +static void handleBytecode(po::variables_map const& vm, + char const* _argName, + string const& _title, + string const& _contract, + CompilerStack&_compiler, + string const& _suffix) +{ + if (vm.count(_argName)) + { + auto choice = vm[_argName].as(); + if (choice != OutputType::FILE) + { + cout << _title << endl; + if (_suffix == "opcodes") + cout << _compiler.getBytecode(_contract) << endl; + else + cout << toHex(_compiler.getBytecode(_contract)) << endl; + } + + if (choice != OutputType::STDOUT) + { + ofstream outFile(_contract + _suffix); + if (_suffix == "opcodes") + outFile << _compiler.getBytecode(_contract); + else + outFile << toHex(_compiler.getBytecode(_contract)); + outFile.close(); + } + } +} + +static void handleJson(po::variables_map const& _vm, + DocumentationType _type, + string const& _contract, + CompilerStack&_compiler) +{ + std::string argName; + std::string suffix; + std::string title; + switch(_type) + { + case DocumentationType::ABI_INTERFACE: + argName = "abi"; + suffix = ".abi"; + title = "Contract ABI"; + break; + case DocumentationType::NATSPEC_USER: + argName = "natspec-user"; + suffix = ".docuser"; + title = "User Documentation"; + break; + case DocumentationType::NATSPEC_DEV: + argName = "natspec-dev"; + suffix = ".docdev"; + title = "Developer Documentation"; + break; + default: + // should never happen + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation _type")); + } + + if (_vm.count(argName.c_str())) + { + auto choice = _vm[argName.c_str()].as(); + if (choice != OutputType::FILE) + { + cout << title << endl; + cout << _compiler.getJsonDocumentation(_contract, _type); + } + + if (choice != OutputType::STDOUT) + { + ofstream outFile(_contract + suffix); + outFile << _compiler.getJsonDocumentation(_contract, _type); + outFile.close(); + } + } +} + +static inline bool argToStdout(po::variables_map const& _vm, const char* _name) +{ + return _vm.count(_name) && _vm[_name].as() != OutputType::FILE; +} + +static bool needStdout(po::variables_map const& _vm) +{ + return argToStdout(_vm, "abi") || argToStdout(_vm, "natspec-user") || argToStdout(_vm, "natspec-dev") || + argToStdout(_vm, "asm") || argToStdout(_vm, "opcodes") || argToStdout(_vm, "binary"); +} + int main(int argc, char** argv) { // Declare the supported options. @@ -96,7 +186,13 @@ int main(int argc, char** argv) ("opcodes", po::value(), "Request to output the Opcodes of the contract. " outputTypeStr) ("binary", po::value(), - "Request to output the contract in binary (hexadecimal). " outputTypeStr); + "Request to output the contract in binary (hexadecimal). " outputTypeStr) + ("abi", po::value(), + "Request to output the contract's ABI interface. " outputTypeStr) + ("natspec-user", po::value(), + "Request to output the contract's Natspec user documentation. " outputTypeStr) + ("natspec-dev", po::value(), + "Request to output the contract's Natspec developer documentation. " outputTypeStr); // All positional options should be interpreted as input files po::positional_options_description p; @@ -124,6 +220,8 @@ int main(int argc, char** argv) version(); return 0; } + + // create a map of input files to source code strings map sourceCodes; if (!vm.count("input-file")) { @@ -138,6 +236,7 @@ int main(int argc, char** argv) for (string const& infile: vm["input-file"].as>()) sourceCodes[infile] = asString(dev::contents(infile)); + // parse the files CompilerStack compiler; try { @@ -182,6 +281,8 @@ int main(int argc, char** argv) } + /* -- act depending on the provided arguments -- */ + // do we need AST output? if (vm.count("ast")) { @@ -213,7 +314,8 @@ int main(int argc, char** argv) vector contracts = compiler.getContractNames(); for (string const& contract: contracts) { - cout << endl << "======= " << contract << " =======" << endl; + if (needStdout(vm)) + cout << endl << "======= " << contract << " =======" << endl; // do we need EVM assembly? if (vm.count("asm")) @@ -233,51 +335,12 @@ int main(int argc, char** argv) } } - // do we need opcodes? - if (vm.count("opcodes")) - { - auto choice = vm["opcodes"].as(); - if (choice != OutputType::FILE) - { - cout << "Opcodes:" << endl; - cout << eth::disassemble(compiler.getBytecode(contract)) << endl; - } - - if (choice != OutputType::STDOUT) - { - ofstream outFile(contract + ".opcodes"); - outFile << eth::disassemble(compiler.getBytecode(contract)); - outFile.close(); - } - } - - // do we need binary? - if (vm.count("binary")) - { - auto choice = vm["binary"].as(); - if (choice != OutputType::FILE) - { - cout << "Binary:" << endl; - cout << toHex(compiler.getBytecode(contract)) << endl; - } - - if (choice != OutputType::STDOUT) - { // TODO: Think, if we really want that? Could simply output to normal binary - ofstream outFile(contract + ".bin"); - outFile << toHex(compiler.getBytecode(contract)); - outFile.close(); - } - } - + handleBytecode(vm, "opcodes", "Opcodes:", contract, compiler, ".opcodes"); + handleBytecode(vm, "binary", "Binary:", contract, compiler, ".binary"); + handleJson(vm, DocumentationType::ABI_INTERFACE, contract, compiler); + handleJson(vm, DocumentationType::NATSPEC_DEV, contract, compiler); + handleJson(vm, DocumentationType::NATSPEC_USER, contract, compiler); } // end of contracts iteration - cout << endl << "Contracts:" << endl; - for (string const& contract: contracts) - { - cout << "Interface specification: " << compiler.getJsonDocumentation(contract, DocumentationType::ABI_INTERFACE) << endl - << "Natspec user documentation: " << compiler.getJsonDocumentation(contract, DocumentationType::NATSPEC_USER) << endl - << "Natspec developer documentation: " << compiler.getJsonDocumentation(contract, DocumentationType::NATSPEC_DEV) << endl; - } - return 0; } From 63e9b3940c76f36ec01000f58100b15fa708352a Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Tue, 9 Dec 2014 12:05:32 +0100 Subject: [PATCH 07/11] Style improvements and succinctness in solc main.cpp --- main.cpp | 132 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 73 insertions(+), 59 deletions(-) diff --git a/main.cpp b/main.cpp index 8650d429c..9500eb97f 100644 --- a/main.cpp +++ b/main.cpp @@ -60,12 +60,18 @@ enum class OutputType BOTH }; -#define outputTypeStr "Legal values:\n"\ - "\tstdout: Print it to standard output\n"\ - "\tfile: Print it to a file with same name\n"\ - "\tboth: Print both to a file and the stdout\n" +static inline bool outputToFile(OutputType type) +{ + return type == OutputType::FILE || type == OutputType::BOTH; +} -std::istream& operator>>(std::istream& _in, OutputType& io_output) +static inline bool outputToStdout(OutputType type) +{ + return type == OutputType::STDOUT || type == OutputType::BOTH; +} + + +static std::istream& operator>>(std::istream& _in, OutputType& io_output) { std::string token; _in >> token; @@ -81,16 +87,16 @@ std::istream& operator>>(std::istream& _in, OutputType& io_output) } static void handleBytecode(po::variables_map const& vm, - char const* _argName, + string const& _argName, string const& _title, string const& _contract, - CompilerStack&_compiler, + CompilerStack& _compiler, string const& _suffix) { if (vm.count(_argName)) { auto choice = vm[_argName].as(); - if (choice != OutputType::FILE) + if (outputToStdout(choice)) { cout << _title << endl; if (_suffix == "opcodes") @@ -99,7 +105,7 @@ static void handleBytecode(po::variables_map const& vm, cout << toHex(_compiler.getBytecode(_contract)) << endl; } - if (choice != OutputType::STDOUT) + if (outputToFile(choice)) { ofstream outFile(_contract + _suffix); if (_suffix == "opcodes") @@ -111,7 +117,7 @@ static void handleBytecode(po::variables_map const& vm, } } -static void handleJson(po::variables_map const& _vm, +static void handleJson(po::variables_map const& _args, DocumentationType _type, string const& _contract, CompilerStack&_compiler) @@ -141,16 +147,16 @@ static void handleJson(po::variables_map const& _vm, BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation _type")); } - if (_vm.count(argName.c_str())) + if (_args.count(argName)) { - auto choice = _vm[argName.c_str()].as(); - if (choice != OutputType::FILE) + auto choice = _args[argName].as(); + if (outputToStdout(choice)) { cout << title << endl; cout << _compiler.getJsonDocumentation(_contract, _type); } - if (choice != OutputType::STDOUT) + if (outputToFile(choice)) { ofstream outFile(_contract + suffix); outFile << _compiler.getJsonDocumentation(_contract, _type); @@ -159,71 +165,78 @@ static void handleJson(po::variables_map const& _vm, } } -static inline bool argToStdout(po::variables_map const& _vm, const char* _name) +static inline bool argToStdout(po::variables_map const& _args, const char* _name) { - return _vm.count(_name) && _vm[_name].as() != OutputType::FILE; + return _args.count(_name) && _args[_name].as() != OutputType::FILE; } -static bool needStdout(po::variables_map const& _vm) +static bool needStdout(po::variables_map const& _args) { - return argToStdout(_vm, "abi") || argToStdout(_vm, "natspec-user") || argToStdout(_vm, "natspec-dev") || - argToStdout(_vm, "asm") || argToStdout(_vm, "opcodes") || argToStdout(_vm, "binary"); + return argToStdout(_args, "abi") || argToStdout(_args, "natspec-user") || argToStdout(_args, "natspec-dev") || + argToStdout(_args, "asm") || argToStdout(_args, "opcodes") || argToStdout(_args, "binary"); } int main(int argc, char** argv) { +#define OUTPUT_TYPE_STR "Legal values:\n" \ + "\tstdout: Print it to standard output\n" \ + "\tfile: Print it to a file with same name\n" \ + "\tboth: Print both to a file and the stdout\n" // Declare the supported options. po::options_description desc("Allowed options"); desc.add_options() - ("help", "Show help message and exit") - ("version", "Show version and exit") - ("optimize", po::value()->default_value(false), "Optimize bytecode for size") - ("input-file", po::value>(), "input file") - ("ast", po::value(), - "Request to output the AST of the contract. " outputTypeStr) - ("asm", po::value(), - "Request to output the EVM assembly of the contract. " outputTypeStr) - ("opcodes", po::value(), - "Request to output the Opcodes of the contract. " outputTypeStr) - ("binary", po::value(), - "Request to output the contract in binary (hexadecimal). " outputTypeStr) - ("abi", po::value(), - "Request to output the contract's ABI interface. " outputTypeStr) - ("natspec-user", po::value(), - "Request to output the contract's Natspec user documentation. " outputTypeStr) - ("natspec-dev", po::value(), - "Request to output the contract's Natspec developer documentation. " outputTypeStr); + ("help", "Show help message and exit") + ("version", "Show version and exit") + ("optimize", po::value()->default_value(false), "Optimize bytecode for size") + ("input-file", po::value>(), "input file") + ("ast", po::value(), + "Request to output the AST of the contract. " OUTPUT_TYPE_STR) + ("asm", po::value(), + "Request to output the EVM assembly of the contract. " OUTPUT_TYPE_STR) + ("opcodes", po::value(), + "Request to output the Opcodes of the contract. " OUTPUT_TYPE_STR) + ("binary", po::value(), + "Request to output the contract in binary (hexadecimal). " OUTPUT_TYPE_STR) + ("abi", po::value(), + "Request to output the contract's ABI interface. " OUTPUT_TYPE_STR) + ("natspec-user", po::value(), + "Request to output the contract's Natspec user documentation. " OUTPUT_TYPE_STR) + ("natspec-dev", po::value(), + "Request to output the contract's Natspec developer documentation. " OUTPUT_TYPE_STR); +#undef OUTPUT_TYPE_STR // All positional options should be interpreted as input files po::positional_options_description p; p.add("input-file", -1); // parse the compiler arguments - po::variables_map vm; + po::variables_map args; try { - po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), vm); + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), args); } catch (po::error const& exception) { cout << exception.what() << endl; return -1; } - po::notify(vm); + po::notify(args); - if (vm.count("help")) { + if (args.count("help")) + { cout << desc; return 0; } - if (vm.count("version")) { + if (args.count("version")) + { version(); return 0; } // create a map of input files to source code strings map sourceCodes; - if (!vm.count("input-file")) + if (!args.count("input-file")) { string s; while (!cin.eof()) @@ -233,7 +246,7 @@ int main(int argc, char** argv) } } else - for (string const& infile: vm["input-file"].as>()) + for (string const& infile: args["input-file"].as>()) sourceCodes[infile] = asString(dev::contents(infile)); // parse the files @@ -242,7 +255,8 @@ int main(int argc, char** argv) { for (auto const& sourceCode: sourceCodes) compiler.addSource(sourceCode.first, sourceCode.second); - compiler.compile( vm["optimize"].as()); + // TODO: Perhaps we should not compile unless requested + compiler.compile(args["optimize"].as()); } catch (ParserError const& exception) { @@ -284,10 +298,10 @@ int main(int argc, char** argv) /* -- act depending on the provided arguments -- */ // do we need AST output? - if (vm.count("ast")) + if (args.count("ast")) { - auto choice = vm["ast"].as(); - if (choice != OutputType::FILE) + auto choice = args["ast"].as(); + if (outputToStdout(choice)) { cout << "Syntax trees:" << endl << endl; for (auto const& sourceCode: sourceCodes) @@ -298,7 +312,7 @@ int main(int argc, char** argv) } } - if (choice != OutputType::STDOUT) + if (outputToFile(choice)) { for (auto const& sourceCode: sourceCodes) { @@ -314,20 +328,20 @@ int main(int argc, char** argv) vector contracts = compiler.getContractNames(); for (string const& contract: contracts) { - if (needStdout(vm)) + if (needStdout(args)) cout << endl << "======= " << contract << " =======" << endl; // do we need EVM assembly? - if (vm.count("asm")) + if (args.count("asm")) { - auto choice = vm["asm"].as(); - if (choice != OutputType::FILE) + auto choice = args["asm"].as(); + if (outputToStdout(choice)) { cout << "EVM assembly:" << endl; compiler.streamAssembly(cout, contract); } - if (choice != OutputType::STDOUT) + if (outputToFile(choice)) { ofstream outFile(contract + ".evm"); compiler.streamAssembly(outFile, contract); @@ -335,11 +349,11 @@ int main(int argc, char** argv) } } - handleBytecode(vm, "opcodes", "Opcodes:", contract, compiler, ".opcodes"); - handleBytecode(vm, "binary", "Binary:", contract, compiler, ".binary"); - handleJson(vm, DocumentationType::ABI_INTERFACE, contract, compiler); - handleJson(vm, DocumentationType::NATSPEC_DEV, contract, compiler); - handleJson(vm, DocumentationType::NATSPEC_USER, contract, compiler); + handleBytecode(args, "opcodes", "Opcodes:", contract, compiler, ".opcodes"); + handleBytecode(args, "binary", "Binary:", contract, compiler, ".binary"); + handleJson(args, DocumentationType::ABI_INTERFACE, contract, compiler); + handleJson(args, DocumentationType::NATSPEC_DEV, contract, compiler); + handleJson(args, DocumentationType::NATSPEC_USER, contract, compiler); } // end of contracts iteration return 0; From 5ab37de94a0a5398e452a6839501a3262c847e61 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Tue, 9 Dec 2014 13:43:08 +0100 Subject: [PATCH 08/11] Moving most of the solc functionality to own class and splitting implementation in modular functions --- SolContext.cpp | 368 +++++++++++++++++++++++++++++++++++++++++++++++++ SolContext.h | 72 ++++++++++ main.cpp | 336 +------------------------------------------- 3 files changed, 447 insertions(+), 329 deletions(-) create mode 100644 SolContext.cpp create mode 100644 SolContext.h diff --git a/SolContext.cpp b/SolContext.cpp new file mode 100644 index 000000000..620ad112f --- /dev/null +++ b/SolContext.cpp @@ -0,0 +1,368 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum 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. + + cpp-ethereum 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 cpp-ethereum. If not, see . +*/ +/** + * @author Lefteris + * @date 2014 + * Solidity compiler context class. + */ +#include "SolContext.h" + +#include +#include +#include + +#include + +#include "BuildInfo.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +namespace po = boost::program_options; + +namespace dev +{ +namespace solidity +{ + +static void version() +{ + cout << "solc, the solidity complier commandline interface " << dev::Version << endl + << " by Christian and Lefteris , (c) 2014." << endl + << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl; + exit(0); +} + +static inline bool argToStdout(po::variables_map const& _args, const char* _name) +{ + return _args.count(_name) && _args[_name].as() != OutputType::FILE; +} + +static bool needStdout(po::variables_map const& _args) +{ + return argToStdout(_args, "abi") || argToStdout(_args, "natspec-user") || argToStdout(_args, "natspec-dev") || + argToStdout(_args, "asm") || argToStdout(_args, "opcodes") || argToStdout(_args, "binary"); +} + +static inline bool outputToFile(OutputType type) +{ + return type == OutputType::FILE || type == OutputType::BOTH; +} + +static inline bool outputToStdout(OutputType type) +{ + return type == OutputType::STDOUT || type == OutputType::BOTH; +} + + +static std::istream& operator>>(std::istream& _in, OutputType& io_output) +{ + std::string token; + _in >> token; + if (token == "stdout") + io_output = OutputType::STDOUT; + else if (token == "file") + io_output = OutputType::FILE; + else if (token == "both") + io_output = OutputType::BOTH; + else + throw boost::program_options::invalid_option_value(token); + return _in; +} + +void SolContext::handleBytecode(string const& _argName, + string const& _title, + string const& _contract, + string const& _suffix) +{ + if (m_args.count(_argName)) + { + auto choice = m_args[_argName].as(); + if (outputToStdout(choice)) + { + cout << _title << endl; + if (_suffix == "opcodes") + ; + // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes + // cout << m_compiler.getBytecode(_contract) << endl; + else + cout << toHex(m_compiler.getBytecode(_contract)) << endl; + } + + if (outputToFile(choice)) + { + ofstream outFile(_contract + _suffix); + if (_suffix == "opcodes") + ; + // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes + // outFile << m_compiler.getBytecode(_contract); + else + outFile << toHex(m_compiler.getBytecode(_contract)); + outFile.close(); + } + } +} + +void SolContext::handleJson(DocumentationType _type, + string const& _contract) +{ + std::string argName; + std::string suffix; + std::string title; + switch(_type) + { + case DocumentationType::ABI_INTERFACE: + argName = "abi"; + suffix = ".abi"; + title = "Contract ABI"; + break; + case DocumentationType::NATSPEC_USER: + argName = "natspec-user"; + suffix = ".docuser"; + title = "User Documentation"; + break; + case DocumentationType::NATSPEC_DEV: + argName = "natspec-dev"; + suffix = ".docdev"; + title = "Developer Documentation"; + break; + default: + // should never happen + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation _type")); + } + + if (m_args.count(argName)) + { + auto choice = m_args[argName].as(); + if (outputToStdout(choice)) + { + cout << title << endl; + cout << m_compiler.getJsonDocumentation(_contract, _type); + } + + if (outputToFile(choice)) + { + ofstream outFile(_contract + suffix); + outFile << m_compiler.getJsonDocumentation(_contract, _type); + outFile.close(); + } + } +} + + + + + + + + + + +bool SolContext::parseArguments(int argc, char** argv) +{ +#define OUTPUT_TYPE_STR "Legal values:\n" \ + "\tstdout: Print it to standard output\n" \ + "\tfile: Print it to a file with same name\n" \ + "\tboth: Print both to a file and the stdout\n" + // Declare the supported options. + po::options_description desc("Allowed options"); + desc.add_options() + ("help", "Show help message and exit") + ("version", "Show version and exit") + ("optimize", po::value()->default_value(false), "Optimize bytecode for size") + ("input-file", po::value>(), "input file") + ("ast", po::value(), + "Request to output the AST of the contract. " OUTPUT_TYPE_STR) + ("asm", po::value(), + "Request to output the EVM assembly of the contract. " OUTPUT_TYPE_STR) + ("opcodes", po::value(), + "Request to output the Opcodes of the contract. " OUTPUT_TYPE_STR) + ("binary", po::value(), + "Request to output the contract in binary (hexadecimal). " OUTPUT_TYPE_STR) + ("abi", po::value(), + "Request to output the contract's ABI interface. " OUTPUT_TYPE_STR) + ("natspec-user", po::value(), + "Request to output the contract's Natspec user documentation. " OUTPUT_TYPE_STR) + ("natspec-dev", po::value(), + "Request to output the contract's Natspec developer documentation. " OUTPUT_TYPE_STR); +#undef OUTPUT_TYPE_STR + + // All positional options should be interpreted as input files + po::positional_options_description p; + p.add("input-file", -1); + + // parse the compiler arguments + try + { + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), m_args); + } + catch (po::error const& exception) + { + cout << exception.what() << endl; + return false; + } + po::notify(m_args); + + if (m_args.count("help")) + { + cout << desc; + return false; + } + + if (m_args.count("version")) + { + version(); + return false; + } + + return true; +} + +bool SolContext::processInput() +{ + if (!m_args.count("input-file")) + { + string s; + while (!cin.eof()) + { + getline(cin, s); + m_sourceCodes[""].append(s); + } + } + else + for (string const& infile: m_args["input-file"].as>()) + m_sourceCodes[infile] = asString(dev::contents(infile)); + + try + { + for (auto const& sourceCode: m_sourceCodes) + m_compiler.addSource(sourceCode.first, sourceCode.second); + // TODO: Perhaps we should not compile unless requested + m_compiler.compile(m_args["optimize"].as()); + } + catch (ParserError const& exception) + { + SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Parser error", m_compiler); + return false; + } + catch (DeclarationError const& exception) + { + SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Declaration error", m_compiler); + return false; + } + catch (TypeError const& exception) + { + SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Type error", m_compiler); + return false; + } + catch (CompilerError const& exception) + { + SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Compiler error", m_compiler); + return false; + } + catch (InternalCompilerError const& exception) + { + SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Internal compiler error", m_compiler); + return false; + } + catch (Exception const& exception) + { + cerr << "Exception during compilation: " << boost::diagnostic_information(exception) << endl; + return false; + } + catch (...) + { + cerr << "Unknown exception during compilation." << endl; + return false; + } + + return true; +} + +void SolContext::actOnInput() +{ + // do we need AST output? + if (m_args.count("ast")) + { + auto choice = m_args["ast"].as(); + if (outputToStdout(choice)) + { + cout << "Syntax trees:" << endl << endl; + for (auto const& sourceCode: m_sourceCodes) + { + cout << endl << "======= " << sourceCode.first << " =======" << endl; + ASTPrinter printer(m_compiler.getAST(sourceCode.first), sourceCode.second); + printer.print(cout); + } + } + + if (outputToFile(choice)) + { + for (auto const& sourceCode: m_sourceCodes) + { + boost::filesystem::path p(sourceCode.first); + ofstream outFile(p.stem().string() + ".ast"); + ASTPrinter printer(m_compiler.getAST(sourceCode.first), sourceCode.second); + printer.print(outFile); + outFile.close(); + } + } + } + + vector contracts = m_compiler.getContractNames(); + for (string const& contract: contracts) + { + if (needStdout(m_args)) + cout << endl << "======= " << contract << " =======" << endl; + + // do we need EVM assembly? + if (m_args.count("asm")) + { + auto choice = m_args["asm"].as(); + if (outputToStdout(choice)) + { + cout << "EVM assembly:" << endl; + m_compiler.streamAssembly(cout, contract); + } + + if (outputToFile(choice)) + { + ofstream outFile(contract + ".evm"); + m_compiler.streamAssembly(outFile, contract); + outFile.close(); + } + } + + handleBytecode("opcodes", "Opcodes:", contract, ".opcodes"); + handleBytecode("binary", "Binary:", contract, ".binary"); + handleJson(DocumentationType::ABI_INTERFACE, contract); + handleJson(DocumentationType::NATSPEC_DEV, contract); + handleJson(DocumentationType::NATSPEC_USER, contract); + } // end of contracts iteration +} + +} +} diff --git a/SolContext.h b/SolContext.h new file mode 100644 index 000000000..922d471ab --- /dev/null +++ b/SolContext.h @@ -0,0 +1,72 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum 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. + + cpp-ethereum 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 cpp-ethereum. If not, see . +*/ +/** + * @author Lefteris + * @date 2014 + * Solidity compiler context class. + */ +#pragma once + +#include + +#include + +namespace dev +{ +namespace solidity +{ + +//forward declaration +enum class DocumentationType: uint8_t; + +enum class OutputType: uint8_t +{ + STDOUT, + FILE, + BOTH +}; + +class SolContext +{ +public: + SolContext() {} + + /// Parse command line arguments and return false if we should not continue + bool parseArguments(int argc, char** argv); + /// Parse the files and create source code objects + bool processInput(); + /// Perform actions on the input depending on provided compiler arguments + void actOnInput(); + +private: + void handleBytecode(std::string const& _argName, + std::string const& _title, + std::string const& _contract, + std::string const& _suffix); + void handleJson(DocumentationType _type, + std::string const& _contract); + + /// Compiler arguments variable map + boost::program_options::variables_map m_args; + /// map of input files to source code strings + std::map m_sourceCodes; + /// Solidity compiler stack + dev::solidity::CompilerStack m_compiler; +}; + +} +} diff --git a/main.cpp b/main.cpp index 9500eb97f..f692725d2 100644 --- a/main.cpp +++ b/main.cpp @@ -20,341 +20,19 @@ * Solidity commandline compiler. */ -#include -#include -#include -#include -#include - -#include "BuildInfo.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; -using namespace dev; -using namespace solidity; -namespace po = boost::program_options; - -void version() -{ - cout << "solc, the solidity complier commandline interface " << dev::Version << endl - << " by Christian and Lefteris , (c) 2014." << endl - << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl; - exit(0); -} - -enum class OutputType -{ - STDOUT, - FILE, - BOTH -}; - -static inline bool outputToFile(OutputType type) -{ - return type == OutputType::FILE || type == OutputType::BOTH; -} - -static inline bool outputToStdout(OutputType type) -{ - return type == OutputType::STDOUT || type == OutputType::BOTH; -} +#include "SolContext.h" -static std::istream& operator>>(std::istream& _in, OutputType& io_output) -{ - std::string token; - _in >> token; - if (token == "stdout") - io_output = OutputType::STDOUT; - else if (token == "file") - io_output = OutputType::FILE; - else if (token == "both") - io_output = OutputType::BOTH; - else - throw boost::program_options::invalid_option_value(token); - return _in; -} - -static void handleBytecode(po::variables_map const& vm, - string const& _argName, - string const& _title, - string const& _contract, - CompilerStack& _compiler, - string const& _suffix) -{ - if (vm.count(_argName)) - { - auto choice = vm[_argName].as(); - if (outputToStdout(choice)) - { - cout << _title << endl; - if (_suffix == "opcodes") - cout << _compiler.getBytecode(_contract) << endl; - else - cout << toHex(_compiler.getBytecode(_contract)) << endl; - } - - if (outputToFile(choice)) - { - ofstream outFile(_contract + _suffix); - if (_suffix == "opcodes") - outFile << _compiler.getBytecode(_contract); - else - outFile << toHex(_compiler.getBytecode(_contract)); - outFile.close(); - } - } -} - -static void handleJson(po::variables_map const& _args, - DocumentationType _type, - string const& _contract, - CompilerStack&_compiler) -{ - std::string argName; - std::string suffix; - std::string title; - switch(_type) - { - case DocumentationType::ABI_INTERFACE: - argName = "abi"; - suffix = ".abi"; - title = "Contract ABI"; - break; - case DocumentationType::NATSPEC_USER: - argName = "natspec-user"; - suffix = ".docuser"; - title = "User Documentation"; - break; - case DocumentationType::NATSPEC_DEV: - argName = "natspec-dev"; - suffix = ".docdev"; - title = "Developer Documentation"; - break; - default: - // should never happen - BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation _type")); - } - - if (_args.count(argName)) - { - auto choice = _args[argName].as(); - if (outputToStdout(choice)) - { - cout << title << endl; - cout << _compiler.getJsonDocumentation(_contract, _type); - } - - if (outputToFile(choice)) - { - ofstream outFile(_contract + suffix); - outFile << _compiler.getJsonDocumentation(_contract, _type); - outFile.close(); - } - } -} - -static inline bool argToStdout(po::variables_map const& _args, const char* _name) -{ - return _args.count(_name) && _args[_name].as() != OutputType::FILE; -} - -static bool needStdout(po::variables_map const& _args) -{ - return argToStdout(_args, "abi") || argToStdout(_args, "natspec-user") || argToStdout(_args, "natspec-dev") || - argToStdout(_args, "asm") || argToStdout(_args, "opcodes") || argToStdout(_args, "binary"); -} int main(int argc, char** argv) { -#define OUTPUT_TYPE_STR "Legal values:\n" \ - "\tstdout: Print it to standard output\n" \ - "\tfile: Print it to a file with same name\n" \ - "\tboth: Print both to a file and the stdout\n" - // Declare the supported options. - po::options_description desc("Allowed options"); - desc.add_options() - ("help", "Show help message and exit") - ("version", "Show version and exit") - ("optimize", po::value()->default_value(false), "Optimize bytecode for size") - ("input-file", po::value>(), "input file") - ("ast", po::value(), - "Request to output the AST of the contract. " OUTPUT_TYPE_STR) - ("asm", po::value(), - "Request to output the EVM assembly of the contract. " OUTPUT_TYPE_STR) - ("opcodes", po::value(), - "Request to output the Opcodes of the contract. " OUTPUT_TYPE_STR) - ("binary", po::value(), - "Request to output the contract in binary (hexadecimal). " OUTPUT_TYPE_STR) - ("abi", po::value(), - "Request to output the contract's ABI interface. " OUTPUT_TYPE_STR) - ("natspec-user", po::value(), - "Request to output the contract's Natspec user documentation. " OUTPUT_TYPE_STR) - ("natspec-dev", po::value(), - "Request to output the contract's Natspec developer documentation. " OUTPUT_TYPE_STR); -#undef OUTPUT_TYPE_STR - - // All positional options should be interpreted as input files - po::positional_options_description p; - p.add("input-file", -1); - - // parse the compiler arguments - po::variables_map args; - try - { - po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), args); - } - catch (po::error const& exception) - { - cout << exception.what() << endl; - return -1; - } - po::notify(args); - - if (args.count("help")) - { - cout << desc; - return 0; - } - - if (args.count("version")) - { - version(); - return 0; - } - - // create a map of input files to source code strings - map sourceCodes; - if (!args.count("input-file")) - { - string s; - while (!cin.eof()) - { - getline(cin, s); - sourceCodes[""].append(s); - } - } - else - for (string const& infile: args["input-file"].as>()) - sourceCodes[infile] = asString(dev::contents(infile)); - - // parse the files - CompilerStack compiler; - try - { - for (auto const& sourceCode: sourceCodes) - compiler.addSource(sourceCode.first, sourceCode.second); - // TODO: Perhaps we should not compile unless requested - compiler.compile(args["optimize"].as()); - } - catch (ParserError const& exception) - { - SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Parser error", compiler); - return -1; - } - catch (DeclarationError const& exception) - { - SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Declaration error", compiler); - return -1; - } - catch (TypeError const& exception) - { - SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Type error", compiler); - return -1; - } - catch (CompilerError const& exception) - { - SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Compiler error", compiler); - return -1; - } - catch (InternalCompilerError const& exception) - { - SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Internal compiler error", compiler); - return -1; - } - catch (Exception const& exception) - { - cerr << "Exception during compilation: " << boost::diagnostic_information(exception) << endl; - return -1; - } - catch (...) - { - cerr << "Unknown exception during compilation." << endl; - return -1; - } - - - /* -- act depending on the provided arguments -- */ - - // do we need AST output? - if (args.count("ast")) - { - auto choice = args["ast"].as(); - if (outputToStdout(choice)) - { - cout << "Syntax trees:" << endl << endl; - for (auto const& sourceCode: sourceCodes) - { - cout << endl << "======= " << sourceCode.first << " =======" << endl; - ASTPrinter printer(compiler.getAST(sourceCode.first), sourceCode.second); - printer.print(cout); - } - } - - if (outputToFile(choice)) - { - for (auto const& sourceCode: sourceCodes) - { - boost::filesystem::path p(sourceCode.first); - ofstream outFile(p.stem().string() + ".ast"); - ASTPrinter printer(compiler.getAST(sourceCode.first), sourceCode.second); - printer.print(outFile); - outFile.close(); - } - } - } - - vector contracts = compiler.getContractNames(); - for (string const& contract: contracts) - { - if (needStdout(args)) - cout << endl << "======= " << contract << " =======" << endl; - - // do we need EVM assembly? - if (args.count("asm")) - { - auto choice = args["asm"].as(); - if (outputToStdout(choice)) - { - cout << "EVM assembly:" << endl; - compiler.streamAssembly(cout, contract); - } - - if (outputToFile(choice)) - { - ofstream outFile(contract + ".evm"); - compiler.streamAssembly(outFile, contract); - outFile.close(); - } - } - - handleBytecode(args, "opcodes", "Opcodes:", contract, compiler, ".opcodes"); - handleBytecode(args, "binary", "Binary:", contract, compiler, ".binary"); - handleJson(args, DocumentationType::ABI_INTERFACE, contract, compiler); - handleJson(args, DocumentationType::NATSPEC_DEV, contract, compiler); - handleJson(args, DocumentationType::NATSPEC_USER, contract, compiler); - } // end of contracts iteration + dev::solidity::SolContext ctx; + if (!ctx.parseArguments(argc, argv)) + return 1; + if (!ctx.processInput()) + return 1; + ctx.actOnInput(); return 0; } From df82e26d5ad3eff2514c4461ff94ada8fa1ebd55 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Tue, 9 Dec 2014 17:39:34 +0100 Subject: [PATCH 09/11] Styling issues and new class name for the CLI --- SolContext.cpp => CommandLineInterface.cpp | 152 ++++++++++----------- SolContext.h => CommandLineInterface.h | 40 +++--- main.cpp | 17 +-- 3 files changed, 98 insertions(+), 111 deletions(-) rename SolContext.cpp => CommandLineInterface.cpp (69%) rename SolContext.h => CommandLineInterface.h (53%) diff --git a/SolContext.cpp b/CommandLineInterface.cpp similarity index 69% rename from SolContext.cpp rename to CommandLineInterface.cpp index 620ad112f..332dc771d 100644 --- a/SolContext.cpp +++ b/CommandLineInterface.cpp @@ -17,9 +17,9 @@ /** * @author Lefteris * @date 2014 - * Solidity compiler context class. + * Solidity command line interface. */ -#include "SolContext.h" +#include "CommandLineInterface.h" #include #include @@ -69,15 +69,14 @@ static bool needStdout(po::variables_map const& _args) static inline bool outputToFile(OutputType type) { - return type == OutputType::FILE || type == OutputType::BOTH; + return type == OutputType::FILE || type == OutputType::BOTH; } static inline bool outputToStdout(OutputType type) { - return type == OutputType::STDOUT || type == OutputType::BOTH; + return type == OutputType::STDOUT || type == OutputType::BOTH; } - static std::istream& operator>>(std::istream& _in, OutputType& io_output) { std::string token; @@ -93,10 +92,10 @@ static std::istream& operator>>(std::istream& _in, OutputType& io_output) return _in; } -void SolContext::handleBytecode(string const& _argName, - string const& _title, - string const& _contract, - string const& _suffix) +void CommandLineInterface::handleBytecode(string const& _argName, + string const& _title, + string const& _contract, + string const& _suffix) { if (m_args.count(_argName)) { @@ -105,8 +104,8 @@ void SolContext::handleBytecode(string const& _argName, { cout << _title << endl; if (_suffix == "opcodes") - ; - // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes + ; + // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes // cout << m_compiler.getBytecode(_contract) << endl; else cout << toHex(m_compiler.getBytecode(_contract)) << endl; @@ -116,8 +115,8 @@ void SolContext::handleBytecode(string const& _argName, { ofstream outFile(_contract + _suffix); if (_suffix == "opcodes") - ; - // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes + ; + // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes // outFile << m_compiler.getBytecode(_contract); else outFile << toHex(m_compiler.getBytecode(_contract)); @@ -126,8 +125,8 @@ void SolContext::handleBytecode(string const& _argName, } } -void SolContext::handleJson(DocumentationType _type, - string const& _contract) +void CommandLineInterface::handleJson(DocumentationType _type, + string const& _contract) { std::string argName; std::string suffix; @@ -172,76 +171,67 @@ void SolContext::handleJson(DocumentationType _type, } } - - - - - - - - - -bool SolContext::parseArguments(int argc, char** argv) +bool CommandLineInterface::parseArguments(int argc, char** argv) { -#define OUTPUT_TYPE_STR "Legal values:\n" \ - "\tstdout: Print it to standard output\n" \ - "\tfile: Print it to a file with same name\n" \ - "\tboth: Print both to a file and the stdout\n" - // Declare the supported options. - po::options_description desc("Allowed options"); - desc.add_options() - ("help", "Show help message and exit") - ("version", "Show version and exit") - ("optimize", po::value()->default_value(false), "Optimize bytecode for size") - ("input-file", po::value>(), "input file") - ("ast", po::value(), - "Request to output the AST of the contract. " OUTPUT_TYPE_STR) - ("asm", po::value(), - "Request to output the EVM assembly of the contract. " OUTPUT_TYPE_STR) - ("opcodes", po::value(), - "Request to output the Opcodes of the contract. " OUTPUT_TYPE_STR) - ("binary", po::value(), - "Request to output the contract in binary (hexadecimal). " OUTPUT_TYPE_STR) - ("abi", po::value(), - "Request to output the contract's ABI interface. " OUTPUT_TYPE_STR) - ("natspec-user", po::value(), - "Request to output the contract's Natspec user documentation. " OUTPUT_TYPE_STR) - ("natspec-dev", po::value(), - "Request to output the contract's Natspec developer documentation. " OUTPUT_TYPE_STR); +#define OUTPUT_TYPE_STR "Legal values:\n" \ + "\tstdout: Print it to standard output\n" \ + "\tfile: Print it to a file with same name\n" \ + "\tboth: Print both to a file and the stdout\n" + // Declare the supported options. + po::options_description desc("Allowed options"); + desc.add_options() + ("help", "Show help message and exit") + ("version", "Show version and exit") + ("optimize", po::value()->default_value(false), "Optimize bytecode for size") + ("input-file", po::value>(), "input file") + ("ast", po::value(), + "Request to output the AST of the contract. " OUTPUT_TYPE_STR) + ("asm", po::value(), + "Request to output the EVM assembly of the contract. " OUTPUT_TYPE_STR) + ("opcodes", po::value(), + "Request to output the Opcodes of the contract. " OUTPUT_TYPE_STR) + ("binary", po::value(), + "Request to output the contract in binary (hexadecimal). " OUTPUT_TYPE_STR) + ("abi", po::value(), + "Request to output the contract's ABI interface. " OUTPUT_TYPE_STR) + ("natspec-user", po::value(), + "Request to output the contract's Natspec user documentation. " OUTPUT_TYPE_STR) + ("natspec-dev", po::value(), + "Request to output the contract's Natspec developer documentation. " OUTPUT_TYPE_STR); #undef OUTPUT_TYPE_STR - // All positional options should be interpreted as input files - po::positional_options_description p; - p.add("input-file", -1); + // All positional options should be interpreted as input files + po::positional_options_description p; + p.add("input-file", -1); - // parse the compiler arguments - try - { - po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), m_args); - } - catch (po::error const& exception) - { - cout << exception.what() << endl; - return false; - } - po::notify(m_args); + // parse the compiler arguments + try + { + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), m_args); + } + catch (po::error const& exception) + { + cout << exception.what() << endl; + return false; + } + po::notify(m_args); - if (m_args.count("help")) - { - cout << desc; - return false; - } + if (m_args.count("help")) + { + cout << desc; + return false; + } - if (m_args.count("version")) - { - version(); - return false; - } + if (m_args.count("version")) + { + version(); + return false; + } - return true; + return true; } -bool SolContext::processInput() +bool CommandLineInterface::processInput() { if (!m_args.count("input-file")) { @@ -260,7 +250,7 @@ bool SolContext::processInput() { for (auto const& sourceCode: m_sourceCodes) m_compiler.addSource(sourceCode.first, sourceCode.second); - // TODO: Perhaps we should not compile unless requested + // TODO: Perhaps we should not compile unless requested m_compiler.compile(m_args["optimize"].as()); } catch (ParserError const& exception) @@ -299,10 +289,10 @@ bool SolContext::processInput() return false; } - return true; + return true; } -void SolContext::actOnInput() +void CommandLineInterface::actOnInput() { // do we need AST output? if (m_args.count("ast")) @@ -342,13 +332,13 @@ void SolContext::actOnInput() if (m_args.count("asm")) { auto choice = m_args["asm"].as(); - if (outputToStdout(choice)) + if (outputToStdout(choice)) { cout << "EVM assembly:" << endl; m_compiler.streamAssembly(cout, contract); } - if (outputToFile(choice)) + if (outputToFile(choice)) { ofstream outFile(contract + ".evm"); m_compiler.streamAssembly(outFile, contract); diff --git a/SolContext.h b/CommandLineInterface.h similarity index 53% rename from SolContext.h rename to CommandLineInterface.h index 922d471ab..8eb1fff3e 100644 --- a/SolContext.h +++ b/CommandLineInterface.h @@ -17,7 +17,7 @@ /** * @author Lefteris * @date 2014 - * Solidity compiler context class. + * Solidity command line interface. */ #pragma once @@ -40,32 +40,32 @@ enum class OutputType: uint8_t BOTH }; -class SolContext +class CommandLineInterface { public: - SolContext() {} + CommandLineInterface() {} - /// Parse command line arguments and return false if we should not continue - bool parseArguments(int argc, char** argv); - /// Parse the files and create source code objects - bool processInput(); - /// Perform actions on the input depending on provided compiler arguments - void actOnInput(); + /// Parse command line arguments and return false if we should not continue + bool parseArguments(int argc, char** argv); + /// Parse the files and create source code objects + bool processInput(); + /// Perform actions on the input depending on provided compiler arguments + void actOnInput(); private: - void handleBytecode(std::string const& _argName, - std::string const& _title, - std::string const& _contract, - std::string const& _suffix); - void handleJson(DocumentationType _type, - std::string const& _contract); + void handleBytecode(std::string const& _argName, + std::string const& _title, + std::string const& _contract, + std::string const& _suffix); + void handleJson(DocumentationType _type, + std::string const& _contract); - /// Compiler arguments variable map - boost::program_options::variables_map m_args; + /// Compiler arguments variable map + boost::program_options::variables_map m_args; /// map of input files to source code strings - std::map m_sourceCodes; - /// Solidity compiler stack - dev::solidity::CompilerStack m_compiler; + std::map m_sourceCodes; + /// Solidity compiler stack + dev::solidity::CompilerStack m_compiler; }; } diff --git a/main.cpp b/main.cpp index f692725d2..c5f72980d 100644 --- a/main.cpp +++ b/main.cpp @@ -20,19 +20,16 @@ * Solidity commandline compiler. */ - -#include "SolContext.h" - - +#include "CommandLineInterface.h" int main(int argc, char** argv) { - dev::solidity::SolContext ctx; - if (!ctx.parseArguments(argc, argv)) - return 1; - if (!ctx.processInput()) - return 1; - ctx.actOnInput(); + dev::solidity::CommandLineInterface cli; + if (!cli.parseArguments(argc, argv)) + return 1; + if (!cli.processInput()) + return 1; + cli.actOnInput(); return 0; } From e851d2173daea4f464d5f72910bb2934c0596a65 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Tue, 9 Dec 2014 18:17:54 +0100 Subject: [PATCH 10/11] Explicitly calling dev::operator<<() on two occassions due to mixup with boost --- CommandLineInterface.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/CommandLineInterface.cpp b/CommandLineInterface.cpp index 332dc771d..aef5512e3 100644 --- a/CommandLineInterface.cpp +++ b/CommandLineInterface.cpp @@ -104,9 +104,11 @@ void CommandLineInterface::handleBytecode(string const& _argName, { cout << _title << endl; if (_suffix == "opcodes") - ; - // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes - // cout << m_compiler.getBytecode(_contract) << endl; + { + // TODO: Figure out why the wrong operator << (from boost) is used here + dev::operator<<(cout, m_compiler.getBytecode(_contract)); + cout << endl; + } else cout << toHex(m_compiler.getBytecode(_contract)) << endl; } @@ -115,9 +117,9 @@ void CommandLineInterface::handleBytecode(string const& _argName, { ofstream outFile(_contract + _suffix); if (_suffix == "opcodes") - ; - // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes - // outFile << m_compiler.getBytecode(_contract); + // TODO: Figure out why the wrong operator << (from boost) is used here + dev::operator<<(outFile, m_compiler.getBytecode(_contract)); + else outFile << toHex(m_compiler.getBytecode(_contract)); outFile.close(); From d377ad3fb13dda6e8a629adfe7484359bda0706d Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Tue, 9 Dec 2014 20:29:29 +0100 Subject: [PATCH 11/11] Cleaner interface for Solc CLI bytecode handling --- CommandLineInterface.cpp | 72 ++++++++++++++++++++++------------------ CommandLineInterface.h | 7 ++-- 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/CommandLineInterface.cpp b/CommandLineInterface.cpp index aef5512e3..d3dd39459 100644 --- a/CommandLineInterface.cpp +++ b/CommandLineInterface.cpp @@ -92,39 +92,48 @@ static std::istream& operator>>(std::istream& _in, OutputType& io_output) return _in; } -void CommandLineInterface::handleBytecode(string const& _argName, - string const& _title, - string const& _contract, - string const& _suffix) +void CommandLineInterface::handleBinary(string const& _contract) { - if (m_args.count(_argName)) + auto choice = m_args["binary"].as(); + if (outputToStdout(choice)) { - auto choice = m_args[_argName].as(); - if (outputToStdout(choice)) - { - cout << _title << endl; - if (_suffix == "opcodes") - { - // TODO: Figure out why the wrong operator << (from boost) is used here - dev::operator<<(cout, m_compiler.getBytecode(_contract)); - cout << endl; - } - else - cout << toHex(m_compiler.getBytecode(_contract)) << endl; - } - - if (outputToFile(choice)) - { - ofstream outFile(_contract + _suffix); - if (_suffix == "opcodes") - // TODO: Figure out why the wrong operator << (from boost) is used here - dev::operator<<(outFile, m_compiler.getBytecode(_contract)); - - else - outFile << toHex(m_compiler.getBytecode(_contract)); - outFile.close(); - } + cout << "Binary: " << endl; + cout << toHex(m_compiler.getBytecode(_contract)) << endl; } + + if (outputToFile(choice)) + { + ofstream outFile(_contract + ".binary"); + outFile << toHex(m_compiler.getBytecode(_contract)); + outFile.close(); + } +} + +void CommandLineInterface::handleOpcode(string const& _contract) +{ + // TODO: Figure out why the wrong operator << (from boost) is used here + auto choice = m_args["opcode"].as(); + if (outputToStdout(choice)) + { + cout << "Opcodes: " << endl; + dev::operator<<(cout, m_compiler.getBytecode(_contract)); + cout << endl; + } + + if (outputToFile(choice)) + { + ofstream outFile(_contract + ".opcode"); + dev::operator<<(outFile, m_compiler.getBytecode(_contract)); + outFile.close(); + } +} + +void CommandLineInterface::handleBytecode(string const& _contract) +{ + if (m_args.count("opcodes")) + handleOpcode(_contract); + if (m_args.count("binary")) + handleBinary(_contract); } void CommandLineInterface::handleJson(DocumentationType _type, @@ -348,8 +357,7 @@ void CommandLineInterface::actOnInput() } } - handleBytecode("opcodes", "Opcodes:", contract, ".opcodes"); - handleBytecode("binary", "Binary:", contract, ".binary"); + handleBytecode(contract); handleJson(DocumentationType::ABI_INTERFACE, contract); handleJson(DocumentationType::NATSPEC_DEV, contract); handleJson(DocumentationType::NATSPEC_USER, contract); diff --git a/CommandLineInterface.h b/CommandLineInterface.h index 8eb1fff3e..7e3ad2502 100644 --- a/CommandLineInterface.h +++ b/CommandLineInterface.h @@ -53,10 +53,9 @@ public: void actOnInput(); private: - void handleBytecode(std::string const& _argName, - std::string const& _title, - std::string const& _contract, - std::string const& _suffix); + void handleBinary(std::string const& _contract); + void handleOpcode(std::string const& _contract); + void handleBytecode(std::string const& _contract); void handleJson(DocumentationType _type, std::string const& _contract);