Index: lldb/source/Commands/CommandObjectSettings.cpp =================================================================== --- lldb/source/Commands/CommandObjectSettings.cpp +++ lldb/source/Commands/CommandObjectSettings.cpp @@ -94,19 +94,18 @@ Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override { Status error; - const int short_option = m_getopt_table[option_idx].val; - switch (short_option) { - case 'f': + auto opt = toOptionEnumSettingsSet(m_getopt_table[option_idx].val, error); + if (!opt) + return error; + + switch (*opt) { + case OptionEnumSettingsSet::Force: m_force = true; break; - case 'g': + case OptionEnumSettingsSet::Global: m_global = true; break; - default: - error.SetErrorStringWithFormat("unrecognized options '%c'", - short_option); - break; } return error; Index: lldb/utils/TableGen/LLDBOptionDefEmitter.cpp =================================================================== --- lldb/utils/TableGen/LLDBOptionDefEmitter.cpp +++ lldb/utils/TableGen/LLDBOptionDefEmitter.cpp @@ -160,6 +160,44 @@ OS << "},\n"; } +static std::string Capitalize(llvm::StringRef s) { + return s.substr(0, 1).upper() + s.substr(1).str(); +} +static std::string ToCamelCase(llvm::StringRef In) { + llvm::SmallVector Parts; + llvm::SplitString(In, Parts, "-_ "); + std::string Result; + for (llvm::StringRef S : Parts) + Result.append(Capitalize(S)); + return Result; +} + +static void emitEnumDeclaration(llvm::StringRef EnumName, + const std::vector &Options, + raw_ostream &OS) { + OS << "namespace { enum class " << EnumName << " {\n"; + for (const CommandOption &CO : Options) + OS << " " << ToCamelCase(CO.FullName) << ",\n"; + OS << "}; }\n"; +} + +static void emitEnumSwitch(llvm::StringRef EnumName, + const std::vector &Options, + raw_ostream &OS) { + OS << "static llvm::Optional<" << EnumName << "> to" << EnumName + << "(char c, Status error) {\n"; + OS << " switch(c) {"; + for (const CommandOption &CO : Options) + OS << " case '" << CO.ShortName << "': return " << EnumName + << "::" << ToCamelCase(CO.FullName) << ";\n"; + OS << R"cpp( + default: + error.SetErrorStringWithFormat("unrecognized option '%c'", c); + return {}; +)cpp"; + OS << " }\n}\n"; +} + /// Emits all option initializers to the raw_ostream. static void emitOptions(std::string Command, std::vector Records, raw_ostream &OS) { @@ -169,6 +207,9 @@ std::string ID = Command; std::replace(ID.begin(), ID.end(), ' ', '_'); + + std::string CamelCaseID = ToCamelCase(Command); + // Generate the macro that the user needs to define before including the // *.inc file. std::string NeededMacro = "LLDB_OPTIONS_" + ID; @@ -180,8 +221,13 @@ OS << "constexpr static OptionDefinition g_" + ID + "_options[] = {\n"; for (CommandOption &CO : Options) emitOption(CO, OS); - // We undefine the macro for the user like Clang's include files are doing it. OS << "};\n"; + + std::string Enum = "OptionEnum" + CamelCaseID; + emitEnumDeclaration(Enum, Options, OS); + emitEnumSwitch(Enum, Options, OS); + + // We undefine the macro for the user like Clang's include files are doing it. OS << "#undef " << NeededMacro << "\n"; OS << "#endif // " << Command << " command\n\n"; }