diff --git a/lldb/include/lldb/Interpreter/CommandInterpreter.h b/lldb/include/lldb/Interpreter/CommandInterpreter.h --- a/lldb/include/lldb/Interpreter/CommandInterpreter.h +++ b/lldb/include/lldb/Interpreter/CommandInterpreter.h @@ -549,6 +549,8 @@ void SetEchoCommentCommands(bool enable); bool GetRepeatPreviousCommand() const; + + bool GetRequireCommandOverwrite() const; const CommandObject::CommandMap &GetUserCommands() const { return m_user_dict; diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp --- a/lldb/source/Commands/CommandObjectCommands.cpp +++ b/lldb/source/Commands/CommandObjectCommands.cpp @@ -1445,7 +1445,7 @@ m_short_help = std::string(option_arg); break; case 'o': - m_overwrite = true; + m_overwrite_lazy = eLazyBoolYes; break; case 's': m_synchronicity = @@ -1467,7 +1467,7 @@ m_class_name.clear(); m_funct_name.clear(); m_short_help.clear(); - m_overwrite = false; + m_overwrite_lazy = eLazyBoolCalculate; m_synchronicity = eScriptedCommandSynchronicitySynchronous; } @@ -1480,7 +1480,7 @@ std::string m_class_name; std::string m_funct_name; std::string m_short_help; - bool m_overwrite = false; + LazyBool m_overwrite_lazy; ScriptedCommandSynchronicity m_synchronicity = eScriptedCommandSynchronicitySynchronous; }; @@ -1499,7 +1499,6 @@ ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter(); if (interpreter) { - StringList lines; lines.SplitIntoLines(data); if (lines.GetSize() > 0) { @@ -1562,8 +1561,19 @@ result.AppendError("'command script add' requires at least one argument"); return false; } - // Store the options in case we get multi-line input - m_overwrite = m_options.m_overwrite; + // Store the options in case we get multi-line input, also figure out the + // default if not user supplied: + switch (m_options.m_overwrite_lazy) { + case eLazyBoolCalculate: + m_overwrite = !GetDebugger().GetCommandInterpreter().GetRequireCommandOverwrite(); + break; + case eLazyBoolYes: + m_overwrite = true; + break; + case eLazyBoolNo: + m_overwrite = false; + } + Status path_error; m_container = GetCommandInterpreter().VerifyUserMultiwordCmdPath( command, true, path_error); @@ -1637,7 +1647,7 @@ std::string m_cmd_name; CommandObjectMultiword *m_container = nullptr; std::string m_short_help; - bool m_overwrite = false; + bool m_overwrite = eLazyBoolCalculate; ScriptedCommandSynchronicity m_synchronicity = eScriptedCommandSynchronicitySynchronous; }; diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -244,6 +244,12 @@ nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0); } +bool CommandInterpreter::GetRequireCommandOverwrite() const { + const uint32_t idx = ePropertyRequireCommandOverwrite; + return m_collection_sp->GetPropertyAtIndexAsBoolean( + nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0); +} + void CommandInterpreter::Initialize() { LLDB_SCOPED_TIMER(); diff --git a/lldb/source/Interpreter/InterpreterProperties.td b/lldb/source/Interpreter/InterpreterProperties.td --- a/lldb/source/Interpreter/InterpreterProperties.td +++ b/lldb/source/Interpreter/InterpreterProperties.td @@ -36,4 +36,8 @@ Global, DefaultTrue, Desc<"If true, LLDB will repeat the previous command if no command was passed to the interpreter. If false, LLDB won't repeat the previous command but only return a new prompt.">; + def RequireCommandOverwrite: Property<"require-overwrite", "Boolean">, + Global, + DefaultTrue, + Desc<"If true, require --overwrite in 'command script add' before overwriting existing user commands.">; } diff --git a/lldb/test/API/commands/command/container/TestContainerCommands.py b/lldb/test/API/commands/command/container/TestContainerCommands.py --- a/lldb/test/API/commands/command/container/TestContainerCommands.py +++ b/lldb/test/API/commands/command/container/TestContainerCommands.py @@ -57,8 +57,9 @@ self.expect("test-multi test-multi-sub welcome friend", "Test command works", substrs=["Hello friend, welcome to LLDB"]) - # Make sure overwriting works, first the leaf command: - # We should not be able to remove extant commands by default: + # Make sure overwriting works on the leaf command. First using the + # explicit option so we should not be able to remove extant commands by default: + self.expect("command script add -c welcome.WelcomeCommand2 test-multi test-multi-sub welcome", "overwrite command w/o -o", substrs=["cannot add command: sub-command already exists"], error=True) @@ -67,9 +68,15 @@ # Make sure we really did overwrite: self.expect("test-multi test-multi-sub welcome friend", "Used the new command class", substrs=["Hello friend, welcome again to LLDB"]) - self.expect("apropos welcome", "welcome should show up in apropos", substrs=["A docstring for the second Welcome"]) + # Now switch the default and make sure we can now delete w/o the overwrite option: + self.runCmd("settings set interpreter.require-overwrite 0") + self.runCmd("command script add -c welcome.WelcomeCommand test-multi test-multi-sub welcome") + # Make sure we really did overwrite: + self.expect("test-multi test-multi-sub welcome friend", "Used the new command class", + substrs=["Hello friend, welcome to LLDB"]) + # Make sure we give good errors when the input is wrong: self.expect("command script delete test-mult test-multi-sub welcome", "Delete script command - wrong first path component", substrs=["'test-mult' not found"], error=True)