diff options
| author | Jonas Devlieghere <jonas@devlieghere.com> | 2019-02-07 21:51:20 +0000 |
|---|---|---|
| committer | Jonas Devlieghere <jonas@devlieghere.com> | 2019-02-07 21:51:20 +0000 |
| commit | 166c262f231e05665032fc6ed480f8c3422e0f45 (patch) | |
| tree | 7b26e602f074bb5ff2d53a0631deff461e694a3a /lldb/source/Interpreter/CommandInterpreter.cpp | |
| parent | 96f54de8ff5d357fadcf474823f70288561923bf (diff) | |
| download | bcm5719-llvm-166c262f231e05665032fc6ed480f8c3422e0f45.tar.gz bcm5719-llvm-166c262f231e05665032fc6ed480f8c3422e0f45.zip | |
[CommandInterpreter] Early return on error (NFC)
We save two levels of indentation by returning early if the given file
doesn't exists or cannot be opened.
llvm-svn: 353472
Diffstat (limited to 'lldb/source/Interpreter/CommandInterpreter.cpp')
| -rw-r--r-- | lldb/source/Interpreter/CommandInterpreter.cpp | 246 |
1 files changed, 121 insertions, 125 deletions
diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp index 573a3ce51c7..45fb3a05ade 100644 --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -2364,138 +2364,134 @@ enum { void CommandInterpreter::HandleCommandsFromFile( FileSpec &cmd_file, ExecutionContext *context, CommandInterpreterRunOptions &options, CommandReturnObject &result) { - if (FileSystem::Instance().Exists(cmd_file)) { - StreamFileSP input_file_sp(new StreamFile()); - - std::string cmd_file_path = cmd_file.GetPath(); - Status error = FileSystem::Instance().Open(input_file_sp->GetFile(), - cmd_file, File::eOpenOptionRead); - if (error.Success()) { - Debugger &debugger = GetDebugger(); - - uint32_t flags = 0; - - if (options.m_stop_on_continue == eLazyBoolCalculate) { - if (m_command_source_flags.empty()) { - // Stop on continue by default - flags |= eHandleCommandFlagStopOnContinue; - } else if (m_command_source_flags.back() & - eHandleCommandFlagStopOnContinue) { - flags |= eHandleCommandFlagStopOnContinue; - } - } else if (options.m_stop_on_continue == eLazyBoolYes) { - flags |= eHandleCommandFlagStopOnContinue; - } - - if (options.m_stop_on_error == eLazyBoolCalculate) { - if (m_command_source_flags.empty()) { - if (GetStopCmdSourceOnError()) - flags |= eHandleCommandFlagStopOnError; - } else if (m_command_source_flags.back() & - eHandleCommandFlagStopOnError) { - flags |= eHandleCommandFlagStopOnError; - } - } else if (options.m_stop_on_error == eLazyBoolYes) { - flags |= eHandleCommandFlagStopOnError; - } - - // stop-on-crash can only be set, if it is present in all levels of - // pushed flag sets. - if (options.GetStopOnCrash()) { - if (m_command_source_flags.empty()) { - flags |= eHandleCommandFlagStopOnCrash; - } else if (m_command_source_flags.back() & - eHandleCommandFlagStopOnCrash) { - flags |= eHandleCommandFlagStopOnCrash; - } - } - - if (options.m_echo_commands == eLazyBoolCalculate) { - if (m_command_source_flags.empty()) { - // Echo command by default - flags |= eHandleCommandFlagEchoCommand; - } else if (m_command_source_flags.back() & - eHandleCommandFlagEchoCommand) { - flags |= eHandleCommandFlagEchoCommand; - } - } else if (options.m_echo_commands == eLazyBoolYes) { - flags |= eHandleCommandFlagEchoCommand; - } - - // We will only ever ask for this flag, if we echo commands in general. - if (options.m_echo_comment_commands == eLazyBoolCalculate) { - if (m_command_source_flags.empty()) { - // Echo comments by default - flags |= eHandleCommandFlagEchoCommentCommand; - } else if (m_command_source_flags.back() & - eHandleCommandFlagEchoCommentCommand) { - flags |= eHandleCommandFlagEchoCommentCommand; - } - } else if (options.m_echo_comment_commands == eLazyBoolYes) { - flags |= eHandleCommandFlagEchoCommentCommand; - } + if (!FileSystem::Instance().Exists(cmd_file)) { + result.AppendErrorWithFormat( + "Error reading commands from file %s - file not found.\n", + cmd_file.GetFilename().AsCString("<Unknown>")); + result.SetStatus(eReturnStatusFailed); + return; + } - if (options.m_print_results == eLazyBoolCalculate) { - if (m_command_source_flags.empty()) { - // Print output by default - flags |= eHandleCommandFlagPrintResult; - } else if (m_command_source_flags.back() & - eHandleCommandFlagPrintResult) { - flags |= eHandleCommandFlagPrintResult; - } - } else if (options.m_print_results == eLazyBoolYes) { - flags |= eHandleCommandFlagPrintResult; - } + StreamFileSP input_file_sp(new StreamFile()); + std::string cmd_file_path = cmd_file.GetPath(); + Status error = FileSystem::Instance().Open(input_file_sp->GetFile(), cmd_file, + File::eOpenOptionRead); - if (flags & eHandleCommandFlagPrintResult) { - debugger.GetOutputFile()->Printf("Executing commands in '%s'.\n", - cmd_file_path.c_str()); - } + if (error.Fail()) { + result.AppendErrorWithFormat( + "error: an error occurred read file '%s': %s\n", cmd_file_path.c_str(), + error.AsCString()); + result.SetStatus(eReturnStatusFailed); + return; + } - // Used for inheriting the right settings when "command source" might - // have nested "command source" commands - lldb::StreamFileSP empty_stream_sp; - m_command_source_flags.push_back(flags); - IOHandlerSP io_handler_sp(new IOHandlerEditline( - debugger, IOHandler::Type::CommandInterpreter, input_file_sp, - empty_stream_sp, // Pass in an empty stream so we inherit the top - // input reader output stream - empty_stream_sp, // Pass in an empty stream so we inherit the top - // input reader error stream - flags, - nullptr, // Pass in NULL for "editline_name" so no history is saved, - // or written - debugger.GetPrompt(), llvm::StringRef(), - false, // Not multi-line - debugger.GetUseColor(), 0, *this)); - const bool old_async_execution = debugger.GetAsyncExecution(); - - // Set synchronous execution if we are not stopping on continue - if ((flags & eHandleCommandFlagStopOnContinue) == 0) - debugger.SetAsyncExecution(false); + Debugger &debugger = GetDebugger(); - m_command_source_depth++; + uint32_t flags = 0; - debugger.RunIOHandler(io_handler_sp); - if (!m_command_source_flags.empty()) - m_command_source_flags.pop_back(); - m_command_source_depth--; - result.SetStatus(eReturnStatusSuccessFinishNoResult); - debugger.SetAsyncExecution(old_async_execution); - } else { - result.AppendErrorWithFormat( - "error: an error occurred read file '%s': %s\n", - cmd_file_path.c_str(), error.AsCString()); - result.SetStatus(eReturnStatusFailed); + if (options.m_stop_on_continue == eLazyBoolCalculate) { + if (m_command_source_flags.empty()) { + // Stop on continue by default + flags |= eHandleCommandFlagStopOnContinue; + } else if (m_command_source_flags.back() & + eHandleCommandFlagStopOnContinue) { + flags |= eHandleCommandFlagStopOnContinue; } - - } else { - result.AppendErrorWithFormat( - "Error reading commands from file %s - file not found.\n", - cmd_file.GetFilename().AsCString("<Unknown>")); - result.SetStatus(eReturnStatusFailed); - return; + } else if (options.m_stop_on_continue == eLazyBoolYes) { + flags |= eHandleCommandFlagStopOnContinue; } + + if (options.m_stop_on_error == eLazyBoolCalculate) { + if (m_command_source_flags.empty()) { + if (GetStopCmdSourceOnError()) + flags |= eHandleCommandFlagStopOnError; + } else if (m_command_source_flags.back() & eHandleCommandFlagStopOnError) { + flags |= eHandleCommandFlagStopOnError; + } + } else if (options.m_stop_on_error == eLazyBoolYes) { + flags |= eHandleCommandFlagStopOnError; + } + + // stop-on-crash can only be set, if it is present in all levels of + // pushed flag sets. + if (options.GetStopOnCrash()) { + if (m_command_source_flags.empty()) { + flags |= eHandleCommandFlagStopOnCrash; + } else if (m_command_source_flags.back() & eHandleCommandFlagStopOnCrash) { + flags |= eHandleCommandFlagStopOnCrash; + } + } + + if (options.m_echo_commands == eLazyBoolCalculate) { + if (m_command_source_flags.empty()) { + // Echo command by default + flags |= eHandleCommandFlagEchoCommand; + } else if (m_command_source_flags.back() & eHandleCommandFlagEchoCommand) { + flags |= eHandleCommandFlagEchoCommand; + } + } else if (options.m_echo_commands == eLazyBoolYes) { + flags |= eHandleCommandFlagEchoCommand; + } + + // We will only ever ask for this flag, if we echo commands in general. + if (options.m_echo_comment_commands == eLazyBoolCalculate) { + if (m_command_source_flags.empty()) { + // Echo comments by default + flags |= eHandleCommandFlagEchoCommentCommand; + } else if (m_command_source_flags.back() & + eHandleCommandFlagEchoCommentCommand) { + flags |= eHandleCommandFlagEchoCommentCommand; + } + } else if (options.m_echo_comment_commands == eLazyBoolYes) { + flags |= eHandleCommandFlagEchoCommentCommand; + } + + if (options.m_print_results == eLazyBoolCalculate) { + if (m_command_source_flags.empty()) { + // Print output by default + flags |= eHandleCommandFlagPrintResult; + } else if (m_command_source_flags.back() & eHandleCommandFlagPrintResult) { + flags |= eHandleCommandFlagPrintResult; + } + } else if (options.m_print_results == eLazyBoolYes) { + flags |= eHandleCommandFlagPrintResult; + } + + if (flags & eHandleCommandFlagPrintResult) { + debugger.GetOutputFile()->Printf("Executing commands in '%s'.\n", + cmd_file_path.c_str()); + } + + // Used for inheriting the right settings when "command source" might + // have nested "command source" commands + lldb::StreamFileSP empty_stream_sp; + m_command_source_flags.push_back(flags); + IOHandlerSP io_handler_sp(new IOHandlerEditline( + debugger, IOHandler::Type::CommandInterpreter, input_file_sp, + empty_stream_sp, // Pass in an empty stream so we inherit the top + // input reader output stream + empty_stream_sp, // Pass in an empty stream so we inherit the top + // input reader error stream + flags, + nullptr, // Pass in NULL for "editline_name" so no history is saved, + // or written + debugger.GetPrompt(), llvm::StringRef(), + false, // Not multi-line + debugger.GetUseColor(), 0, *this)); + const bool old_async_execution = debugger.GetAsyncExecution(); + + // Set synchronous execution if we are not stopping on continue + if ((flags & eHandleCommandFlagStopOnContinue) == 0) + debugger.SetAsyncExecution(false); + + m_command_source_depth++; + + debugger.RunIOHandler(io_handler_sp); + if (!m_command_source_flags.empty()) + m_command_source_flags.pop_back(); + m_command_source_depth--; + result.SetStatus(eReturnStatusSuccessFinishNoResult); + debugger.SetAsyncExecution(old_async_execution); } ScriptInterpreter *CommandInterpreter::GetScriptInterpreter(bool can_create) { |

