From 8773a7ea0f7be837a9c13aeb2419fcdb7f3b3f6f Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Tue, 9 Dec 2014 12:05:32 +0100 Subject: [PATCH] Style improvements and succinctness in solc main.cpp --- solc/main.cpp | 132 ++++++++++++++++++++++++++++---------------------- 1 file changed, 73 insertions(+), 59 deletions(-) diff --git a/solc/main.cpp b/solc/main.cpp index 8650d429c..9500eb97f 100644 --- a/solc/main.cpp +++ b/solc/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; +} + +static inline bool outputToStdout(OutputType type) +{ + return type == OutputType::STDOUT || type == OutputType::BOTH; +} + -std::istream& operator>>(std::istream& _in, OutputType& io_output) +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;