Skip to content

Commit 6f4fb4e

Browse files
committed
[lldb] Let table gen create command option initializers.
Summary: We currently have man large arrays containing initializers for our command options. These tables are tricky maintain as we don't have any good place to check them for consistency and it's also hard to read (`nullptr, {}, 0` is not very descriptive). This patch fixes this by letting table gen generate those tables. This way we can have a more readable syntax for this (especially for all the default arguments) and we can let TableCheck check them for consistency (e.g. an option with an optional argument can't have `eArgTypeNone`, naming of flags', etc.). Also refactoring the related data structures can now be done without changing the hundred of option initializers. For example, this line: ``` {LLDB_OPT_SET_ALL, false, "hide-aliases", 'a', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Hide aliases in the command list."}, ``` becomes this: ``` def hide_aliases : Option<"hide-aliases", "a">, Desc<"Hide aliases in the command list.">; ``` For now I just moved a few initializers to the new format to demonstrate the change. I'll slowly migrate the other option initializers tables in separate patches. Reviewers: JDevlieghere, davide, sgraenitz Reviewed By: JDevlieghere Subscribers: jingham, xiaobai, labath, mgorny, abidh, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D64365 llvm-svn: 365908
1 parent 38cd364 commit 6f4fb4e

13 files changed

+436
-29
lines changed

lldb/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ endif()
3333
if (NOT LLDB_DISABLE_PYTHON)
3434
add_subdirectory(scripts)
3535
endif ()
36+
37+
add_subdirectory(utils/TableGen)
3638
add_subdirectory(source)
3739
add_subdirectory(tools)
3840
add_subdirectory(docs)

lldb/cmake/modules/AddLLDB.cmake

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,37 @@
1+
function(lldb_tablegen)
2+
# Syntax:
3+
# lldb_tablegen output-file [tablegen-arg ...] SOURCE source-file
4+
# [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
5+
#
6+
# Generates a custom command for invoking tblgen as
7+
#
8+
# tblgen source-file -o=output-file tablegen-arg ...
9+
#
10+
# and, if cmake-target-name is provided, creates a custom target for
11+
# executing the custom command depending on output-file. It is
12+
# possible to list more files to depend after DEPENDS.
13+
14+
cmake_parse_arguments(LTG "" "SOURCE;TARGET" "" ${ARGN})
15+
16+
if(NOT LTG_SOURCE)
17+
message(FATAL_ERROR "SOURCE source-file required by lldb_tablegen")
18+
endif()
19+
20+
set(LLVM_TARGET_DEFINITIONS ${LTG_SOURCE})
21+
tablegen(LLDB ${LTG_UNPARSED_ARGUMENTS})
22+
23+
if(LTG_TARGET)
24+
add_public_tablegen_target(${LTG_TARGET})
25+
set_target_properties( ${LTG_TARGET} PROPERTIES FOLDER "LLDB tablegenning")
26+
set_property(GLOBAL APPEND PROPERTY LLDB_TABLEGEN_TARGETS ${LTG_TARGET})
27+
endif()
28+
endfunction(lldb_tablegen)
29+
130
function(add_lldb_library name)
31+
include_directories(BEFORE
32+
${CMAKE_CURRENT_BINARY_DIR}
33+
)
34+
235
# only supported parameters to this macro are the optional
336
# MODULE;SHARED;STATIC library type and source files
437
cmake_parse_arguments(PARAM
@@ -241,4 +274,4 @@ function(lldb_setup_rpaths name)
241274
BUILD_RPATH "${LIST_BUILD_RPATH}"
242275
INSTALL_RPATH "${LIST_INSTALL_RPATH}"
243276
)
244-
endfunction()
277+
endfunction()

lldb/source/Commands/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
lldb_tablegen(Options.inc -gen-lldb-option-defs
2+
SOURCE Options.td
3+
TARGET LLDBOptionsGen)
4+
15
add_lldb_library(lldbCommands
26
CommandCompletions.cpp
37
CommandObjectApropos.cpp
@@ -45,3 +49,5 @@ add_lldb_library(lldbCommands
4549
LINK_COMPONENTS
4650
Support
4751
)
52+
53+
add_dependencies(lldbCommands LLDBOptionsGen)

lldb/source/Commands/CommandObjectBreakpoint.cpp

+4-9
Original file line numberDiff line numberDiff line change
@@ -1246,15 +1246,10 @@ the second re-enables the first location.");
12461246

12471247
#pragma mark List::CommandOptions
12481248
static constexpr OptionDefinition g_breakpoint_list_options[] = {
1249-
// clang-format off
1250-
{ LLDB_OPT_SET_ALL, false, "internal", 'i', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Show debugger internal breakpoints" },
1251-
{ LLDB_OPT_SET_1, false, "brief", 'b', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Give a brief description of the breakpoint (no location info)." },
1252-
// FIXME: We need to add an "internal" command, and then add this sort of thing to it.
1253-
// But I need to see it for now, and don't want to wait.
1254-
{ LLDB_OPT_SET_2, false, "full", 'f', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Give a full description of the breakpoint and its locations." },
1255-
{ LLDB_OPT_SET_3, false, "verbose", 'v', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Explain everything we know about the breakpoint (for debugging debugger bugs)." },
1256-
{ LLDB_OPT_SET_ALL, false, "dummy-breakpoints", 'D', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "List Dummy breakpoints - i.e. breakpoints set before a file is provided, which prime new targets." },
1257-
// clang-format on
1249+
// FIXME: We need to add an "internal" command, and then add this sort of
1250+
// thing to it. But I need to see it for now, and don't want to wait.
1251+
#define LLDB_OPTIONS_breakpoint_list
1252+
#include "Options.inc"
12581253
};
12591254

12601255
#pragma mark List

lldb/source/Commands/CommandObjectHelp.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,8 @@ CommandObjectHelp::CommandObjectHelp(CommandInterpreter &interpreter)
6666
CommandObjectHelp::~CommandObjectHelp() = default;
6767

6868
static constexpr OptionDefinition g_help_options[] = {
69-
// clang-format off
70-
{LLDB_OPT_SET_ALL, false, "hide-aliases", 'a', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Hide aliases in the command list."},
71-
{LLDB_OPT_SET_ALL, false, "hide-user-commands", 'u', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Hide user-defined commands from the list."},
72-
{LLDB_OPT_SET_ALL, false, "show-hidden-commands", 'h', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Include commands prefixed with an underscore."},
73-
// clang-format on
69+
#define LLDB_OPTIONS_help
70+
#include "Options.inc"
7471
};
7572

7673
llvm::ArrayRef<OptionDefinition>

lldb/source/Commands/CommandObjectSettings.cpp

+6-11
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ using namespace lldb_private;
2222
// CommandObjectSettingsSet
2323

2424
static constexpr OptionDefinition g_settings_set_options[] = {
25-
// clang-format off
26-
{ LLDB_OPT_SET_2, false, "global", 'g', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Apply the new value to the global default value." },
27-
{ LLDB_OPT_SET_2, false, "force", 'f', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Force an empty value to be accepted as the default." }
28-
// clang-format on
25+
#define LLDB_OPTIONS_settings_set
26+
#include "Options.inc"
2927
};
3028

3129
class CommandObjectSettingsSet : public CommandObjectRaw {
@@ -313,10 +311,8 @@ class CommandObjectSettingsShow : public CommandObjectParsed {
313311
// CommandObjectSettingsWrite -- Write settings to file
314312

315313
static constexpr OptionDefinition g_settings_write_options[] = {
316-
// clang-format off
317-
{ LLDB_OPT_SET_ALL, true, "file", 'f', OptionParser::eRequiredArgument, nullptr, {}, CommandCompletions::eDiskFileCompletion, eArgTypeFilename, "The file into which to write the settings." },
318-
{ LLDB_OPT_SET_ALL, false, "append",'a', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Append to saved settings file if it exists."},
319-
// clang-format on
314+
#define LLDB_OPTIONS_settings_write
315+
#include "Options.inc"
320316
};
321317

322318
class CommandObjectSettingsWrite : public CommandObjectParsed {
@@ -438,9 +434,8 @@ class CommandObjectSettingsWrite : public CommandObjectParsed {
438434
// CommandObjectSettingsRead -- Read settings from file
439435

440436
static constexpr OptionDefinition g_settings_read_options[] = {
441-
// clang-format off
442-
{LLDB_OPT_SET_ALL, true, "file",'f', OptionParser::eRequiredArgument, nullptr, {}, CommandCompletions::eDiskFileCompletion, eArgTypeFilename, "The file from which to read the breakpoints." },
443-
// clang-format on
437+
#define LLDB_OPTIONS_settings_read
438+
#include "Options.inc"
444439
};
445440

446441
class CommandObjectSettingsRead : public CommandObjectParsed {

lldb/source/Commands/CommandObjectTarget.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1967,9 +1967,8 @@ static constexpr OptionEnumValueElement g_sort_option_enumeration[] = {
19671967
{eSortOrderByName, "name", "Sort output by symbol name."} };
19681968

19691969
static constexpr OptionDefinition g_target_modules_dump_symtab_options[] = {
1970-
// clang-format off
1971-
{ LLDB_OPT_SET_1, false, "sort", 's', OptionParser::eRequiredArgument, nullptr, OptionEnumValues(g_sort_option_enumeration), 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table." }
1972-
// clang-format on
1970+
#define LLDB_OPTIONS_target_modules_dump_symtab
1971+
#include "Options.inc"
19731972
};
19741973

19751974
class CommandObjectTargetModulesDumpSymtab

lldb/source/Commands/Options.td

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
include "OptionsBase.td"
2+
3+
let Command = "target modules dump symtab" in {
4+
def tm_sort : Option<"sort", "s">, Group<1>,
5+
Desc<"Supply a sort order when dumping the symbol table.">,
6+
EnumArg<"SortOrder", "OptionEnumValues(g_sort_option_enumeration)">;
7+
}
8+
9+
let Command = "help" in {
10+
def help_hide_aliases : Option<"hide-aliases", "a">,
11+
Desc<"Hide aliases in the command list.">;
12+
def help_hide_user : Option<"hide-user-commands", "u">,
13+
Desc<"Hide user-defined commands from the list.">;
14+
def help_show_hidden : Option<"show-hidden-commands", "h">,
15+
Desc<"Include commands prefixed with an underscore.">;
16+
}
17+
18+
let Command = "settings set" in {
19+
def setset_global : Option<"global", "g">, Arg<"Filename">,
20+
Completion<"DiskFile">,
21+
Desc<"Apply the new value to the global default value.">;
22+
def setset_force : Option<"force", "f">,
23+
Desc<"Force an empty value to be accepted as the default.">;
24+
}
25+
26+
let Command = "settings write" in {
27+
def setwrite_file : Option<"file", "f">, Required, Arg<"Filename">,
28+
Completion<"DiskFile">,
29+
Desc<"The file into which to write the settings.">;
30+
def setwrite_append : Option<"append", "a">,
31+
Desc<"Append to saved settings file if it exists.">;
32+
}
33+
34+
let Command = "settings read" in {
35+
def setread_file : Option<"file", "f">, Required, Arg<"Filename">,
36+
Completion<"DiskFile">,
37+
Desc<"The file from which to read the settings.">;
38+
}
39+
40+
let Command = "breakpoint list" in {
41+
def blist_internal : Option<"internal", "i">,
42+
Desc<"Show debugger internal breakpoints">;
43+
def blist_brief : Option<"brief", "b">, Group<1>,
44+
Desc<"Give a brief description of the breakpoint (no location info).">;
45+
def blist_full : Option<"full", "f">, Group<2>,
46+
Desc<"Give a full description of the breakpoint and its locations.">;
47+
def blist_verbose : Option<"verbose", "v">, Group<3>,
48+
Desc<"Explain everything we know about the breakpoint (for debugging "
49+
"debugger bugs).">;
50+
def blist_dummy_bp : Option<"dummy-breakpoints", "D">,
51+
Desc<"List Dummy breakpoints - i.e. breakpoints set before a file is "
52+
"provided, which prime new targets.">;
53+
}

lldb/source/Commands/OptionsBase.td

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Base class for all options.
2+
class Option<string fullname, string shortname> {
3+
string FullName = fullname;
4+
string ShortName = shortname;
5+
// The full associated command/subcommand such as "settings set".
6+
string Command;
7+
}
8+
9+
// Moves the option into a list of option groups.
10+
class Groups<list<int> groups> {
11+
list<int> Groups = groups;
12+
}
13+
14+
// Moves the option in all option groups in a range.
15+
// Start and end values are inclusive.
16+
class GroupRange<int start, int end> {
17+
int GroupStart = start;
18+
int GroupEnd = end;
19+
}
20+
// Moves the option in a single option group.
21+
class Group<int group> {
22+
int GroupStart = group;
23+
int GroupEnd = group;
24+
}
25+
26+
// Sets the description for the option that should be
27+
// displayed to the user.
28+
class Desc<string description> {
29+
string Description = description;
30+
}
31+
32+
// Marks the option as required when calling the
33+
// associated command.
34+
class Required {
35+
bit Required = 1;
36+
}
37+
38+
// Gives the option an optional argument.
39+
class OptionalArg<string type> {
40+
string ArgType = type;
41+
bit OptionalArg = 1;
42+
}
43+
44+
// Gives the option an required argument.
45+
class Arg<string type> {
46+
string ArgType = type;
47+
}
48+
49+
// Gives the option an required argument.
50+
class EnumArg<string type, string enum> {
51+
string ArgType = type;
52+
string ArgEnum = enum;
53+
}
54+
55+
// Sets the available completions for the given option.
56+
class Completions<list<string> completions> {
57+
list<string> Completions = completions;
58+
}
59+
// Sets a single completion for the given option.
60+
class Completion<string completion> {
61+
list<string> Completions = [completion];
62+
}

lldb/utils/TableGen/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
set(LLVM_LINK_COMPONENTS Support)
2+
3+
add_tablegen(lldb-tblgen LLDB
4+
LLDBOptionDefEmitter.cpp
5+
LLDBTableGen.cpp
6+
)
7+
set_target_properties(lldb-tblgen PROPERTIES FOLDER "LLDB tablegenning")
8+

0 commit comments

Comments
 (0)