Skip to content

Commit be09589

Browse files
authored
Merge pull request #5585 from jimingham/multiword-alias-weed
Handle aliasing a non-top-level command.
2 parents 1438c5a + 23b5f47 commit be09589

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

lldb/include/lldb/Interpreter/CommandObject.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLDB_INTERPRETER_COMMANDOBJECT_H
1111

1212
#include <map>
13+
#include <memory>
1314
#include <string>
1415
#include <vector>
1516

@@ -64,7 +65,7 @@ size_t FindLongestCommandWord(std::map<std::string, ValueType> &dict) {
6465
return max_len;
6566
}
6667

67-
class CommandObject {
68+
class CommandObject : public std::enable_shared_from_this<CommandObject> {
6869
public:
6970
typedef llvm::StringRef(ArgumentHelpCallbackFunction)();
7071

lldb/source/Commands/CommandObjectCommands.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -484,29 +484,31 @@ rather than using a positional placeholder:"
484484
OptionArgVectorSP(new OptionArgVector);
485485

486486
const bool include_aliases = true;
487-
if (CommandObjectSP cmd_obj_sp = m_interpreter.GetCommandSPExact(
488-
cmd_obj.GetCommandName(), include_aliases)) {
489-
if (m_interpreter.AliasExists(alias_command) ||
490-
m_interpreter.UserCommandExists(alias_command)) {
491-
result.AppendWarningWithFormat(
492-
"Overwriting existing definition for '%s'.\n",
493-
alias_command.str().c_str());
494-
}
495-
if (CommandAlias *alias = m_interpreter.AddAlias(
496-
alias_command, cmd_obj_sp, raw_command_string)) {
497-
if (m_command_options.m_help.OptionWasSet())
498-
alias->SetHelp(m_command_options.m_help.GetCurrentValue());
499-
if (m_command_options.m_long_help.OptionWasSet())
500-
alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
501-
result.SetStatus(eReturnStatusSuccessFinishNoResult);
502-
} else {
503-
result.AppendError("Unable to create requested alias.\n");
504-
}
487+
// Look up the command using command's name first. This is to resolve
488+
// aliases when you are making nested aliases. But if you don't find
489+
// it that way, then it wasn't an alias and we can just use the object
490+
// we were passed in.
491+
CommandObjectSP cmd_obj_sp = m_interpreter.GetCommandSPExact(
492+
cmd_obj.GetCommandName(), include_aliases);
493+
if (!cmd_obj_sp)
494+
cmd_obj_sp = cmd_obj.shared_from_this();
505495

496+
if (m_interpreter.AliasExists(alias_command) ||
497+
m_interpreter.UserCommandExists(alias_command)) {
498+
result.AppendWarningWithFormat(
499+
"Overwriting existing definition for '%s'.\n",
500+
alias_command.str().c_str());
501+
}
502+
if (CommandAlias *alias = m_interpreter.AddAlias(
503+
alias_command, cmd_obj_sp, raw_command_string)) {
504+
if (m_command_options.m_help.OptionWasSet())
505+
alias->SetHelp(m_command_options.m_help.GetCurrentValue());
506+
if (m_command_options.m_long_help.OptionWasSet())
507+
alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
508+
result.SetStatus(eReturnStatusSuccessFinishNoResult);
506509
} else {
507510
result.AppendError("Unable to create requested alias.\n");
508511
}
509-
510512
return result.Succeeded();
511513
}
512514

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ def container_add(self):
5555
self.expect("test-multi test-multi-sub welcome friend", "Test command works",
5656
substrs=["Hello friend, welcome to LLDB"])
5757

58+
# Make sure we can make an alias to this:
59+
self.runCmd("command alias my-welcome test-multi test-multi-sub welcome", "We can make an alias to multi-word")
60+
self.expect("my-welcome friend", "Test command works",
61+
substrs=["Hello friend, welcome to LLDB"])
62+
self.runCmd("command unalias my-welcome")
63+
5864
# Make sure overwriting works on the leaf command. First using the
5965
# explicit option so we should not be able to remove extant commands by default:
6066

0 commit comments

Comments
 (0)