Skip to content

Commit

Permalink
[lldb/Commands] Add support to auto-completion for user commands
Browse files Browse the repository at this point in the history
This patch should allow the user to set specific auto-completion type
for their custom commands.

To do so, we had to hoist the `CompletionType` enum so the user can
access it and add a new completion type flag to the CommandScriptAdd
Command Object.

So now, the user can specify which completion type will be used with
their custom command, when they register it.

This also makes the `crashlog` custom commands use disk-file completion
type, to browse through the user file system and load the report.

Differential Revision: https://reviews.llvm.org/D152011

Signed-off-by: Med Ismail Bennani <[email protected]>
  • Loading branch information
medismailben committed Jun 6, 2023
1 parent 1e82b20 commit 6a9c3e6
Show file tree
Hide file tree
Showing 37 changed files with 495 additions and 396 deletions.
1 change: 1 addition & 0 deletions lldb/docs/python_api_enums.rst
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ CommandArgumentType
.. py:data:: eArgTypeColumnNum
.. py:data:: eArgTypeModuleUUID
.. py:data:: eArgTypeLastArg
.. py:data:: eArgTypeCompletionType
.. _SymbolType:

Expand Down
4 changes: 2 additions & 2 deletions lldb/examples/python/crashlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1666,10 +1666,10 @@ def should_run_in_interactive_mode(options, ci):

def __lldb_init_module(debugger, internal_dict):
debugger.HandleCommand(
"command script add -o -c lldb.macosx.crashlog.Symbolicate crashlog"
"command script add -o -c lldb.macosx.crashlog.Symbolicate -C disk-file crashlog"
)
debugger.HandleCommand(
"command script add -o -f lldb.macosx.crashlog.save_crashlog save_crashlog"
"command script add -o -f lldb.macosx.crashlog.save_crashlog -C disk-file save_crashlog"
)
print(
'"crashlog" and "save_crashlog" commands have been installed, use '
Expand Down
33 changes: 0 additions & 33 deletions lldb/include/lldb/Interpreter/CommandCompletions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,6 @@ namespace lldb_private {
class TildeExpressionResolver;
class CommandCompletions {
public:
enum CommonCompletionTypes {
eNoCompletion = 0u,
eSourceFileCompletion = (1u << 0),
eDiskFileCompletion = (1u << 1),
eDiskDirectoryCompletion = (1u << 2),
eSymbolCompletion = (1u << 3),
eModuleCompletion = (1u << 4),
eSettingsNameCompletion = (1u << 5),
ePlatformPluginCompletion = (1u << 6),
eArchitectureCompletion = (1u << 7),
eVariablePathCompletion = (1u << 8),
eRegisterCompletion = (1u << 9),
eBreakpointCompletion = (1u << 10),
eProcessPluginCompletion = (1u << 11),
eDisassemblyFlavorCompletion = (1u << 12),
eTypeLanguageCompletion = (1u << 13),
eFrameIndexCompletion = (1u << 14),
eModuleUUIDCompletion = (1u << 15),
eStopHookIDCompletion = (1u << 16),
eThreadIndexCompletion = (1u << 17),
eWatchPointIDCompletion = (1u << 18),
eBreakpointNameCompletion = (1u << 19),
eProcessIDCompletion = (1u << 20),
eProcessNameCompletion = (1u << 21),
eRemoteDiskFileCompletion = (1u << 22),
eRemoteDiskDirectoryCompletion = (1u << 23),
eTypeCategoryNameCompletion = (1u << 24),
// This item serves two purposes. It is the last element in the enum, so
// you can add custom enums starting from here in your Option class. Also
// if you & in this bit the base code will not process the option.
eCustomCompletion = (1u << 24)
};

static bool InvokeCommonCompletionCallbacks(
CommandInterpreter &interpreter, uint32_t completion_mask,
lldb_private::CompletionRequest &request, SearchFilter *searcher);
Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Interpreter/CommandObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class CommandObject : public std::enable_shared_from_this<CommandObject> {
struct ArgumentTableEntry {
lldb::CommandArgumentType arg_type;
const char *arg_name;
CommandCompletions::CommonCompletionTypes completion_type;
lldb::CompletionType completion_type;
OptionEnumValues enum_values;
ArgumentHelpCallback help_function;
const char *help_text;
Expand Down
236 changes: 141 additions & 95 deletions lldb/include/lldb/Interpreter/CommandOptionArgumentTable.h

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lldb/include/lldb/Interpreter/OptionValueFileColonLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class OptionValueFileColonLine :
FileSpec m_file_spec;
uint32_t m_line_number = LLDB_INVALID_LINE_NUMBER;
uint32_t m_column_number = LLDB_INVALID_COLUMN_NUMBER;
uint32_t m_completion_mask = CommandCompletions::eSourceFileCompletion;
uint32_t m_completion_mask = lldb::eSourceFileCompletion;
};

} // namespace lldb_private
Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Interpreter/OptionValueFileSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class OptionValueFileSpec : public Cloneable<OptionValueFileSpec, OptionValue> {
FileSpec m_default_value;
lldb::DataBufferSP m_data_sp;
llvm::sys::TimePoint<> m_data_mod_time;
uint32_t m_completion_mask = CommandCompletions::eDiskFileCompletion;
uint32_t m_completion_mask = lldb::eDiskFileCompletion;
bool m_resolve;
};

Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Utility/OptionDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct OptionDefinition {
/// If not empty, an array of enum values.
OptionEnumValues enum_values;
/// The kind of completion for this option.
/// Contains values of the CommandCompletions::CommonCompletionTypes enum.
/// Contains values of the lldb::CompletionType enum.
uint32_t completion_type;
/// Type of argument this option takes.
lldb::CommandArgumentType argument_type;
Expand Down
34 changes: 34 additions & 0 deletions lldb/include/lldb/lldb-enumerations.h
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ enum CommandArgumentType {
eArgTypeConnectURL,
eArgTypeTargetID,
eArgTypeStopHookID,
eArgTypeCompletionType,
eArgTypeLastArg // Always keep this entry as the last entry in this
// enumeration!!
};
Expand Down Expand Up @@ -1245,6 +1246,39 @@ enum WatchpointValueKind {
eWatchPointValueKindExpression = 2,
};

enum CompletionType {
eNoCompletion = 0u,
eSourceFileCompletion = (1u << 0),
eDiskFileCompletion = (1u << 1),
eDiskDirectoryCompletion = (1u << 2),
eSymbolCompletion = (1u << 3),
eModuleCompletion = (1u << 4),
eSettingsNameCompletion = (1u << 5),
ePlatformPluginCompletion = (1u << 6),
eArchitectureCompletion = (1u << 7),
eVariablePathCompletion = (1u << 8),
eRegisterCompletion = (1u << 9),
eBreakpointCompletion = (1u << 10),
eProcessPluginCompletion = (1u << 11),
eDisassemblyFlavorCompletion = (1u << 12),
eTypeLanguageCompletion = (1u << 13),
eFrameIndexCompletion = (1u << 14),
eModuleUUIDCompletion = (1u << 15),
eStopHookIDCompletion = (1u << 16),
eThreadIndexCompletion = (1u << 17),
eWatchpointIDCompletion = (1u << 18),
eBreakpointNameCompletion = (1u << 19),
eProcessIDCompletion = (1u << 20),
eProcessNameCompletion = (1u << 21),
eRemoteDiskFileCompletion = (1u << 22),
eRemoteDiskDirectoryCompletion = (1u << 23),
eTypeCategoryNameCompletion = (1u << 24),
// This item serves two purposes. It is the last element in the enum, so
// you can add custom enums starting from here in your Option class. Also
// if you & in this bit the base code will not process the option.
eCustomCompletion = (1u << 25)
};

} // namespace lldb

#endif // LLDB_LLDB_ENUMERATIONS_H
58 changes: 31 additions & 27 deletions lldb/source/Commands/CommandCompletions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,41 @@ bool CommandCompletions::InvokeCommonCompletionCallbacks(
bool handled = false;

const CommonCompletionElement common_completions[] = {
{eSourceFileCompletion, CommandCompletions::SourceFiles},
{eDiskFileCompletion, CommandCompletions::DiskFiles},
{eDiskDirectoryCompletion, CommandCompletions::DiskDirectories},
{eSymbolCompletion, CommandCompletions::Symbols},
{eModuleCompletion, CommandCompletions::Modules},
{eModuleUUIDCompletion, CommandCompletions::ModuleUUIDs},
{eSettingsNameCompletion, CommandCompletions::SettingsNames},
{ePlatformPluginCompletion, CommandCompletions::PlatformPluginNames},
{eArchitectureCompletion, CommandCompletions::ArchitectureNames},
{eVariablePathCompletion, CommandCompletions::VariablePath},
{eRegisterCompletion, CommandCompletions::Registers},
{eBreakpointCompletion, CommandCompletions::Breakpoints},
{eProcessPluginCompletion, CommandCompletions::ProcessPluginNames},
{eDisassemblyFlavorCompletion, CommandCompletions::DisassemblyFlavors},
{eTypeLanguageCompletion, CommandCompletions::TypeLanguages},
{eFrameIndexCompletion, CommandCompletions::FrameIndexes},
{eStopHookIDCompletion, CommandCompletions::StopHookIDs},
{eThreadIndexCompletion, CommandCompletions::ThreadIndexes},
{eWatchPointIDCompletion, CommandCompletions::WatchPointIDs},
{eBreakpointNameCompletion, CommandCompletions::BreakpointNames},
{eProcessIDCompletion, CommandCompletions::ProcessIDs},
{eProcessNameCompletion, CommandCompletions::ProcessNames},
{eRemoteDiskFileCompletion, CommandCompletions::RemoteDiskFiles},
{eRemoteDiskDirectoryCompletion,
{lldb::eSourceFileCompletion, CommandCompletions::SourceFiles},
{lldb::eDiskFileCompletion, CommandCompletions::DiskFiles},
{lldb::eDiskDirectoryCompletion, CommandCompletions::DiskDirectories},
{lldb::eSymbolCompletion, CommandCompletions::Symbols},
{lldb::eModuleCompletion, CommandCompletions::Modules},
{lldb::eModuleUUIDCompletion, CommandCompletions::ModuleUUIDs},
{lldb::eSettingsNameCompletion, CommandCompletions::SettingsNames},
{lldb::ePlatformPluginCompletion,
CommandCompletions::PlatformPluginNames},
{lldb::eArchitectureCompletion, CommandCompletions::ArchitectureNames},
{lldb::eVariablePathCompletion, CommandCompletions::VariablePath},
{lldb::eRegisterCompletion, CommandCompletions::Registers},
{lldb::eBreakpointCompletion, CommandCompletions::Breakpoints},
{lldb::eProcessPluginCompletion, CommandCompletions::ProcessPluginNames},
{lldb::eDisassemblyFlavorCompletion,
CommandCompletions::DisassemblyFlavors},
{lldb::eTypeLanguageCompletion, CommandCompletions::TypeLanguages},
{lldb::eFrameIndexCompletion, CommandCompletions::FrameIndexes},
{lldb::eStopHookIDCompletion, CommandCompletions::StopHookIDs},
{lldb::eThreadIndexCompletion, CommandCompletions::ThreadIndexes},
{lldb::eWatchpointIDCompletion, CommandCompletions::WatchPointIDs},
{lldb::eBreakpointNameCompletion, CommandCompletions::BreakpointNames},
{lldb::eProcessIDCompletion, CommandCompletions::ProcessIDs},
{lldb::eProcessNameCompletion, CommandCompletions::ProcessNames},
{lldb::eRemoteDiskFileCompletion, CommandCompletions::RemoteDiskFiles},
{lldb::eRemoteDiskDirectoryCompletion,
CommandCompletions::RemoteDiskDirectories},
{eTypeCategoryNameCompletion, CommandCompletions::TypeCategoryNames},
{eNoCompletion, nullptr} // This one has to be last in the list.
{lldb::eTypeCategoryNameCompletion,
CommandCompletions::TypeCategoryNames},
{lldb::CompletionType::eNoCompletion,
nullptr} // This one has to be last in the list.
};

for (int i = 0;; i++) {
if (common_completions[i].type == eNoCompletion)
if (common_completions[i].type == lldb::eNoCompletion)
break;
else if ((common_completions[i].type & completion_mask) ==
common_completions[i].type &&
Expand Down
40 changes: 16 additions & 24 deletions lldb/source/Commands/CommandObjectBreakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,8 @@ class CommandObjectBreakpointModify : public CommandObjectParsed {
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), CommandCompletions::eBreakpointCompletion,
request, nullptr);
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), lldb::eBreakpointCompletion, request, nullptr);
}

Options *GetOptions() override { return &m_options; }
Expand Down Expand Up @@ -902,9 +901,8 @@ class CommandObjectBreakpointEnable : public CommandObjectParsed {
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), CommandCompletions::eBreakpointCompletion,
request, nullptr);
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), lldb::eBreakpointCompletion, request, nullptr);
}

protected:
Expand Down Expand Up @@ -1017,9 +1015,8 @@ the second re-enables the first location.");
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), CommandCompletions::eBreakpointCompletion,
request, nullptr);
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), lldb::eBreakpointCompletion, request, nullptr);
}

protected:
Expand Down Expand Up @@ -1394,9 +1391,8 @@ class CommandObjectBreakpointDelete : public CommandObjectParsed {
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), CommandCompletions::eBreakpointCompletion,
request, nullptr);
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), lldb::eBreakpointCompletion, request, nullptr);
}

Options *GetOptions() override { return &m_options; }
Expand Down Expand Up @@ -1803,9 +1799,8 @@ class CommandObjectBreakpointNameAdd : public CommandObjectParsed {
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), CommandCompletions::eBreakpointCompletion,
request, nullptr);
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), lldb::eBreakpointCompletion, request, nullptr);
}

Options *GetOptions() override { return &m_option_group; }
Expand Down Expand Up @@ -1887,9 +1882,8 @@ class CommandObjectBreakpointNameDelete : public CommandObjectParsed {
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), CommandCompletions::eBreakpointCompletion,
request, nullptr);
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), lldb::eBreakpointCompletion, request, nullptr);
}

Options *GetOptions() override { return &m_option_group; }
Expand Down Expand Up @@ -2203,9 +2197,8 @@ class CommandObjectBreakpointRead : public CommandObjectParsed {

switch (GetDefinitions()[opt_defs_index].short_option) {
case 'f':
CommandCompletions::InvokeCommonCompletionCallbacks(
interpreter, CommandCompletions::eDiskFileCompletion, request,
nullptr);
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
interpreter, lldb::eDiskFileCompletion, request, nullptr);
break;

case 'N':
Expand Down Expand Up @@ -2343,9 +2336,8 @@ class CommandObjectBreakpointWrite : public CommandObjectParsed {
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), CommandCompletions::eBreakpointCompletion,
request, nullptr);
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), lldb::eBreakpointCompletion, request, nullptr);
}

Options *GetOptions() override { return &m_options; }
Expand Down
Loading

0 comments on commit 6a9c3e6

Please sign in to comment.