From e13dc5084d9556d22c8c63fdb4aead1d31a03c70 Mon Sep 17 00:00:00 2001 From: Saurabh Sharma Date: Mon, 9 Aug 2021 15:06:29 +0530 Subject: [PATCH] Move exception handlers from CommandLineInterface to main() --- solc/CommandLineInterface.cpp | 119 +++------------------------------- solc/main.cpp | 61 +++++++++++++---- 2 files changed, 55 insertions(+), 125 deletions(-) diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index dfe5cfc5c..a825dd2a4 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -699,30 +699,6 @@ bool CommandLineInterface::compile() formatter.printExceptionInformation(_exception, "Compiler error"); return false; } - catch (InternalCompilerError const& _exception) - { - serr() << - "Internal compiler error during compilation:" << - endl << - boost::diagnostic_information(_exception); - return false; - } - catch (UnimplementedFeatureError const& _exception) - { - serr() << - "Unimplemented feature:" << - endl << - boost::diagnostic_information(_exception); - return false; - } - catch (smtutil::SMTLogicError const& _exception) - { - serr() << - "SMT logic error during analysis:" << - endl << - boost::diagnostic_information(_exception); - return false; - } catch (Error const& _error) { if (_error.type() == Error::Type::DocstringParsingError) @@ -735,23 +711,6 @@ bool CommandLineInterface::compile() return false; } - catch (Exception const& _exception) - { - serr() << "Exception during compilation: " << boost::diagnostic_information(_exception) << endl; - return false; - } - catch (std::exception const& _e) - { - serr() << "Unknown exception during compilation" << ( - _e.what() ? ": " + string(_e.what()) : "." - ) << endl; - return false; - } - catch (...) - { - serr() << "Unknown exception during compilation." << endl; - return false; - } return true; } @@ -1016,31 +975,10 @@ bool CommandLineInterface::assemble(yul::AssemblyStack::Language _language, yul: m_options.optimiserSettings() ); - try - { - if (!stack.parseAndAnalyze(src.first, src.second)) - successful = false; - else - stack.optimize(); - } - catch (Exception const& _exception) - { - serr() << "Exception in assembler: " << boost::diagnostic_information(_exception) << endl; - return false; - } - catch (std::exception const& _e) - { - serr() << - "Unknown exception during compilation" << - (_e.what() ? ": " + string(_e.what()) : ".") << - endl; - return false; - } - catch (...) - { - serr() << "Unknown exception in assembler." << endl; - return false; - } + if (!stack.parseAndAnalyze(src.first, src.second)) + successful = false; + else + stack.optimize(); } for (auto const& sourceAndStack: assemblyStacks) @@ -1074,29 +1012,8 @@ bool CommandLineInterface::assemble(yul::AssemblyStack::Language _language, yul: if (_language != yul::AssemblyStack::Language::Ewasm && _targetMachine == yul::AssemblyStack::Machine::Ewasm) { - try - { - stack.translate(yul::AssemblyStack::Language::Ewasm); - stack.optimize(); - } - catch (Exception const& _exception) - { - serr() << "Exception in assembler: " << boost::diagnostic_information(_exception) << endl; - return false; - } - catch (std::exception const& _e) - { - serr() << - "Unknown exception during compilation" << - (_e.what() ? ": " + string(_e.what()) : ".") << - endl; - return false; - } - catch (...) - { - serr() << "Unknown exception in assembler." << endl; - return false; - } + stack.translate(yul::AssemblyStack::Language::Ewasm); + stack.optimize(); sout() << endl << "==========================" << endl; sout() << endl << "Translated source:" << endl; @@ -1104,28 +1021,8 @@ bool CommandLineInterface::assemble(yul::AssemblyStack::Language _language, yul: } yul::MachineAssemblyObject object; - try - { - object = stack.assemble(_targetMachine); - object.bytecode->link(m_options.linker.libraries); - } - catch (Exception const& _exception) - { - serr() << "Exception while assembling: " << boost::diagnostic_information(_exception) << endl; - return false; - } - catch (std::exception const& _e) - { - serr() << "Unknown exception during compilation" << ( - _e.what() ? ": " + string(_e.what()) : "." - ) << endl; - return false; - } - catch (...) - { - serr() << "Unknown exception while assembling." << endl; - return false; - } + object = stack.assemble(_targetMachine); + object.bytecode->link(m_options.linker.libraries); sout() << endl << "Binary representation:" << endl; if (object.bytecode) diff --git a/solc/main.cpp b/solc/main.cpp index 9eefc1afd..1299e2564 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -22,11 +22,16 @@ */ #include + +#include + #include + #include #include using namespace std; +using namespace solidity; /* The equivalent of setlocale(LC_ALL, "C") is called before any user code is run. @@ -53,24 +58,52 @@ static void setDefaultOrCLocale() int main(int argc, char** argv) { - setDefaultOrCLocale(); - solidity::frontend::CommandLineInterface cli(cin, cout, cerr); - if (!cli.parseArguments(argc, argv)) - return 1; - if (!cli.readInputFiles()) - return 1; - if (!cli.processInput()) - return 1; - bool success = false; try { - success = cli.actOnInput(); + setDefaultOrCLocale(); + solidity::frontend::CommandLineInterface cli(cin, cout, cerr); + bool success = + cli.parseArguments(argc, argv) && + cli.readInputFiles() && + cli.processInput() && + cli.actOnInput(); + + return success ? 0 : 1; + } + catch (smtutil::SMTLogicError const& _exception) + { + cerr << "SMT logic error:" << endl; + cerr << boost::diagnostic_information(_exception); + return 1; + } + catch (langutil::UnimplementedFeatureError const& _exception) + { + cerr << "Unimplemented feature:" << endl; + cerr << boost::diagnostic_information(_exception); + return 1; + } + catch (langutil::InternalCompilerError const& _exception) + { + cerr << "Internal compiler error:" << endl; + cerr << boost::diagnostic_information(_exception); + return 1; } catch (boost::exception const& _exception) { - cerr << "Exception during output generation: " << boost::diagnostic_information(_exception) << endl; - success = false; + cerr << "Uncaught exception:" << endl; + cerr << boost::diagnostic_information(_exception) << endl; + return 1; + } + catch (std::exception const& _exception) + { + cerr << "Uncaught exception:" << endl; + cerr << boost::diagnostic_information(_exception) << endl; + return 1; + } + catch (...) + { + cerr << "Uncaught exception" << endl; + cerr << boost::current_exception_diagnostic_information() << endl; + return 1; } - - return success ? 0 : 1; }