Skip to content

Handle aliasing a non-top-level command. #5585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lldb/include/lldb/Interpreter/CommandObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLDB_INTERPRETER_COMMANDOBJECT_H

#include <map>
#include <memory>
#include <string>
#include <vector>

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

class CommandObject {
class CommandObject : public std::enable_shared_from_this<CommandObject> {
public:
typedef llvm::StringRef(ArgumentHelpCallbackFunction)();

Expand Down
40 changes: 21 additions & 19 deletions lldb/source/Commands/CommandObjectCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,29 +484,31 @@ rather than using a positional placeholder:"
OptionArgVectorSP(new OptionArgVector);

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

if (m_interpreter.AliasExists(alias_command) ||
m_interpreter.UserCommandExists(alias_command)) {
result.AppendWarningWithFormat(
"Overwriting existing definition for '%s'.\n",
alias_command.str().c_str());
}
if (CommandAlias *alias = m_interpreter.AddAlias(
alias_command, cmd_obj_sp, raw_command_string)) {
if (m_command_options.m_help.OptionWasSet())
alias->SetHelp(m_command_options.m_help.GetCurrentValue());
if (m_command_options.m_long_help.OptionWasSet())
alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
result.SetStatus(eReturnStatusSuccessFinishNoResult);
} else {
result.AppendError("Unable to create requested alias.\n");
}

return result.Succeeded();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ def container_add(self):
self.expect("test-multi test-multi-sub welcome friend", "Test command works",
substrs=["Hello friend, welcome to LLDB"])

# Make sure we can make an alias to this:
self.runCmd("command alias my-welcome test-multi test-multi-sub welcome", "We can make an alias to multi-word")
self.expect("my-welcome friend", "Test command works",
substrs=["Hello friend, welcome to LLDB"])
self.runCmd("command unalias my-welcome")

# 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:

Expand Down