Author: Jim Ingham Date: 2022-03-31T14:15:14-07:00 New Revision: 1f7b58f2a50461493f083b2ed807b25e036286f6
URL: https://github.com/llvm/llvm-project/commit/1f7b58f2a50461493f083b2ed807b25e036286f6 DIFF: https://github.com/llvm/llvm-project/commit/1f7b58f2a50461493f083b2ed807b25e036286f6.diff LOG: Add a setting to not require --overwrite to overwrite commands. Protecting against accidental overwriting of commands is good, but having to pass a flag to overwrite the command when developing your commands is pretty annoying. This adds a setting to defeat the protection so you can do this once at the start of your session and not have to worry about it again. Differential Revision: https://reviews.llvm.org/D122680 Added: Modified: lldb/include/lldb/Interpreter/CommandInterpreter.h lldb/source/Commands/CommandObjectCommands.cpp lldb/source/Interpreter/CommandInterpreter.cpp lldb/source/Interpreter/InterpreterProperties.td lldb/test/API/commands/command/container/TestContainerCommands.py Removed: ################################################################################ diff --git a/lldb/include/lldb/Interpreter/CommandInterpreter.h b/lldb/include/lldb/Interpreter/CommandInterpreter.h index 787dfdbcb21f5..641e651e18909 100644 --- a/lldb/include/lldb/Interpreter/CommandInterpreter.h +++ b/lldb/include/lldb/Interpreter/CommandInterpreter.h @@ -549,6 +549,8 @@ class CommandInterpreter : public Broadcaster, 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 index 51385e0d8e58a..1d4687b0650a9 100644 --- a/lldb/source/Commands/CommandObjectCommands.cpp +++ b/lldb/source/Commands/CommandObjectCommands.cpp @@ -1445,7 +1445,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed, 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 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed, 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 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed, 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 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed, ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter(); if (interpreter) { - StringList lines; lines.SplitIntoLines(data); if (lines.GetSize() > 0) { @@ -1562,8 +1561,19 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed, 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 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed, 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 index a6b7f0e480fc2..f9ba5c3d2a2ea 100644 --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -244,6 +244,12 @@ bool CommandInterpreter::GetRepeatPreviousCommand() const { 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 index 1c6f0206c489d..c0acc044fb7fe 100644 --- a/lldb/source/Interpreter/InterpreterProperties.td +++ b/lldb/source/Interpreter/InterpreterProperties.td @@ -36,4 +36,8 @@ let Definition = "interpreter" in { 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 index 0ef31a0da25dc..3bfb3ec62ad63 100644 --- a/lldb/test/API/commands/command/container/TestContainerCommands.py +++ b/lldb/test/API/commands/command/container/TestContainerCommands.py @@ -57,8 +57,9 @@ def container_add(self): 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 @@ def container_add(self): # 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) _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits