Skip to content

Commit 78233d9

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 (cherry picked from commit 1f7b58f)
1 parent 6e60a3d commit 78233d9

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

lldb/include/lldb/Interpreter/CommandInterpreter.h

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

550550
bool GetRepeatPreviousCommand() const;
551+
552+
bool GetRequireCommandOverwrite() const;
551553

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

lldb/source/Commands/CommandObjectCommands.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
14501450
m_short_help = std::string(option_arg);
14511451
break;
14521452
case 'o':
1453-
m_overwrite = true;
1453+
m_overwrite_lazy = eLazyBoolYes;
14541454
break;
14551455
case 's':
14561456
m_synchronicity =
@@ -1472,7 +1472,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
14721472
m_class_name.clear();
14731473
m_funct_name.clear();
14741474
m_short_help.clear();
1475-
m_overwrite = false;
1475+
m_overwrite_lazy = eLazyBoolCalculate;
14761476
m_synchronicity = eScriptedCommandSynchronicitySynchronous;
14771477
}
14781478

@@ -1485,7 +1485,7 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
14851485
std::string m_class_name;
14861486
std::string m_funct_name;
14871487
std::string m_short_help;
1488-
bool m_overwrite;
1488+
LazyBool m_overwrite_lazy;
14891489
ScriptedCommandSynchronicity m_synchronicity =
14901490
eScriptedCommandSynchronicitySynchronous;
14911491
};
@@ -1504,7 +1504,6 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
15041504

15051505
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
15061506
if (interpreter) {
1507-
15081507
StringList lines;
15091508
lines.SplitIntoLines(data);
15101509
if (lines.GetSize() > 0) {
@@ -1567,8 +1566,19 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
15671566
result.AppendError("'command script add' requires at least one argument");
15681567
return false;
15691568
}
1570-
// Store the options in case we get multi-line input
1571-
m_overwrite = m_options.m_overwrite;
1569+
// Store the options in case we get multi-line input, also figure out the
1570+
// default if not user supplied:
1571+
switch (m_options.m_overwrite_lazy) {
1572+
case eLazyBoolCalculate:
1573+
m_overwrite = !GetDebugger().GetCommandInterpreter().GetRequireCommandOverwrite();
1574+
break;
1575+
case eLazyBoolYes:
1576+
m_overwrite = true;
1577+
break;
1578+
case eLazyBoolNo:
1579+
m_overwrite = false;
1580+
}
1581+
15721582
Status path_error;
15731583
m_container = GetCommandInterpreter().VerifyUserMultiwordCmdPath(
15741584
command, true, path_error);
@@ -1642,8 +1652,9 @@ class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
16421652
std::string m_cmd_name;
16431653
CommandObjectMultiword *m_container = nullptr;
16441654
std::string m_short_help;
1645-
bool m_overwrite;
1646-
ScriptedCommandSynchronicity m_synchronicity;
1655+
bool m_overwrite = eLazyBoolCalculate;
1656+
ScriptedCommandSynchronicity m_synchronicity =
1657+
eScriptedCommandSynchronicitySynchronous;
16471658
};
16481659

16491660
// CommandObjectCommandsScriptList

lldb/source/Interpreter/CommandInterpreter.cpp

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

244+
bool CommandInterpreter::GetRequireCommandOverwrite() const {
245+
const uint32_t idx = ePropertyRequireCommandOverwrite;
246+
return m_collection_sp->GetPropertyAtIndexAsBoolean(
247+
nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
248+
}
249+
244250
void CommandInterpreter::Initialize() {
245251
LLDB_SCOPED_TIMER();
246252

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: 11 additions & 4 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,8 +68,14 @@ 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-
71-
self.expect("apropos welcome", "welcome should show up in apropos", substrs=["Just a docstring for the second Welcome"])
71+
self.expect("apropos welcome", "welcome should show up in apropos", substrs=["A docstring for the second Welcome"])
72+
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"])
7279

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",

0 commit comments

Comments
 (0)