Skip to content

Commit 1f7b58f

Browse files
committed
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
1 parent fe528e7 commit 1f7b58f

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

lldb/include/lldb/Interpreter/CommandInterpreter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ class CommandInterpreter : public Broadcaster,
549549
void SetEchoCommentCommands(bool enable);
550550

551551
bool GetRepeatPreviousCommand() const;
552+
553+
bool GetRequireCommandOverwrite() const;
552554

553555
const CommandObject::CommandMap &GetUserCommands() const {
554556
return m_user_dict;

lldb/source/Commands/CommandObjectCommands.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
14451445
m_short_help = std::string(option_arg);
14461446
break;
14471447
case 'o':
1448-
m_overwrite = true;
1448+
m_overwrite_lazy = eLazyBoolYes;
14491449
break;
14501450
case 's':
14511451
m_synchronicity =
@@ -1467,7 +1467,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
14671467
m_class_name.clear();
14681468
m_funct_name.clear();
14691469
m_short_help.clear();
1470-
m_overwrite = false;
1470+
m_overwrite_lazy = eLazyBoolCalculate;
14711471
m_synchronicity = eScriptedCommandSynchronicitySynchronous;
14721472
}
14731473

@@ -1480,7 +1480,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
14801480
std::string m_class_name;
14811481
std::string m_funct_name;
14821482
std::string m_short_help;
1483-
bool m_overwrite = false;
1483+
LazyBool m_overwrite_lazy;
14841484
ScriptedCommandSynchronicity m_synchronicity =
14851485
eScriptedCommandSynchronicitySynchronous;
14861486
};
@@ -1499,7 +1499,6 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
14991499

15001500
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
15011501
if (interpreter) {
1502-
15031502
StringList lines;
15041503
lines.SplitIntoLines(data);
15051504
if (lines.GetSize() > 0) {
@@ -1562,8 +1561,19 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
15621561
result.AppendError("'command script add' requires at least one argument");
15631562
return false;
15641563
}
1565-
// Store the options in case we get multi-line input
1566-
m_overwrite = m_options.m_overwrite;
1564+
// Store the options in case we get multi-line input, also figure out the
1565+
// default if not user supplied:
1566+
switch (m_options.m_overwrite_lazy) {
1567+
case eLazyBoolCalculate:
1568+
m_overwrite = !GetDebugger().GetCommandInterpreter().GetRequireCommandOverwrite();
1569+
break;
1570+
case eLazyBoolYes:
1571+
m_overwrite = true;
1572+
break;
1573+
case eLazyBoolNo:
1574+
m_overwrite = false;
1575+
}
1576+
15671577
Status path_error;
15681578
m_container = GetCommandInterpreter().VerifyUserMultiwordCmdPath(
15691579
command, true, path_error);
@@ -1637,7 +1647,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
16371647
std::string m_cmd_name;
16381648
CommandObjectMultiword *m_container = nullptr;
16391649
std::string m_short_help;
1640-
bool m_overwrite = false;
1650+
bool m_overwrite = eLazyBoolCalculate;
16411651
ScriptedCommandSynchronicity m_synchronicity =
16421652
eScriptedCommandSynchronicitySynchronous;
16431653
};

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ bool CommandInterpreter::GetRepeatPreviousCommand() const {
244244
nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
245245
}
246246

247+
bool CommandInterpreter::GetRequireCommandOverwrite() const {
248+
const uint32_t idx = ePropertyRequireCommandOverwrite;
249+
return m_collection_sp->GetPropertyAtIndexAsBoolean(
250+
nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
251+
}
252+
247253
void CommandInterpreter::Initialize() {
248254
LLDB_SCOPED_TIMER();
249255

lldb/source/Interpreter/InterpreterProperties.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ let Definition = "interpreter" in {
3636
Global,
3737
DefaultTrue,
3838
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.">;
39+
def RequireCommandOverwrite: Property<"require-overwrite", "Boolean">,
40+
Global,
41+
DefaultTrue,
42+
Desc<"If true, require --overwrite in 'command script add' before overwriting existing user commands.">;
3943
}

lldb/test/API/commands/command/container/TestContainerCommands.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ def container_add(self):
5757
self.expect("test-multi test-multi-sub welcome friend", "Test command works",
5858
substrs=["Hello friend, welcome to LLDB"])
5959

60-
# Make sure overwriting works, first the leaf command:
61-
# We should not be able to remove extant commands by default:
60+
# Make sure overwriting works on the leaf command. First using the
61+
# explicit option so we should not be able to remove extant commands by default:
62+
6263
self.expect("command script add -c welcome.WelcomeCommand2 test-multi test-multi-sub welcome",
6364
"overwrite command w/o -o",
6465
substrs=["cannot add command: sub-command already exists"], error=True)
@@ -67,9 +68,15 @@ def container_add(self):
6768
# Make sure we really did overwrite:
6869
self.expect("test-multi test-multi-sub welcome friend", "Used the new command class",
6970
substrs=["Hello friend, welcome again to LLDB"])
70-
7171
self.expect("apropos welcome", "welcome should show up in apropos", substrs=["A docstring for the second Welcome"])
7272

73+
# Now switch the default and make sure we can now delete w/o the overwrite option:
74+
self.runCmd("settings set interpreter.require-overwrite 0")
75+
self.runCmd("command script add -c welcome.WelcomeCommand test-multi test-multi-sub welcome")
76+
# Make sure we really did overwrite:
77+
self.expect("test-multi test-multi-sub welcome friend", "Used the new command class",
78+
substrs=["Hello friend, welcome to LLDB"])
79+
7380
# Make sure we give good errors when the input is wrong:
7481
self.expect("command script delete test-mult test-multi-sub welcome", "Delete script command - wrong first path component",
7582
substrs=["'test-mult' not found"], error=True)

0 commit comments

Comments
 (0)