Skip to content

Commit d518ed4

Browse files
committed
Handle aliasing a non-top-level command.
This didn't work previously because we had to check whether the incoming command was an alias command, but if it wasn't we still used the result of that lookup - which was by the command's node name. That fails for non-top-level commands. In this case, the resolution is pretty simple since we already have the node's CommandObject, so all we needed to do was turn it into a shared pointer, for which I added enable_shared_from_this to the CommandObject. Differential Revision: https://reviews.llvm.org/D137662
1 parent b565e7f commit d518ed4

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
@@ -485,29 +485,31 @@ rather than using a positional placeholder:"
485485
OptionArgVectorSP(new OptionArgVector);
486486

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

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

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)