diff options
Diffstat (limited to 'utils/TableGen')
-rw-r--r-- | utils/TableGen/LLDBOptionDefEmitter.cpp | 162 | ||||
-rw-r--r-- | utils/TableGen/LLDBPropertyDefEmitter.cpp | 165 | ||||
-rw-r--r-- | utils/TableGen/LLDBTableGen.cpp | 32 | ||||
-rw-r--r-- | utils/TableGen/LLDBTableGenBackends.h | 12 | ||||
-rw-r--r-- | utils/TableGen/LLDBTableGenUtils.cpp | 21 | ||||
-rw-r--r-- | utils/TableGen/LLDBTableGenUtils.h | 34 |
6 files changed, 348 insertions, 78 deletions
diff --git a/utils/TableGen/LLDBOptionDefEmitter.cpp b/utils/TableGen/LLDBOptionDefEmitter.cpp index 4e62197d3989..6e73d0c53de3 100644 --- a/utils/TableGen/LLDBOptionDefEmitter.cpp +++ b/utils/TableGen/LLDBOptionDefEmitter.cpp @@ -1,4 +1,4 @@ -//===- TableGen.cpp - Top-Level TableGen implementation for Clang ---------===// +//===- LLDBOptionDefEmitter.cpp -------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -12,140 +12,174 @@ //===----------------------------------------------------------------------===// #include "LLDBTableGenBackends.h" +#include "LLDBTableGenUtils.h" #include "llvm/ADT/StringExtras.h" #include "llvm/TableGen/Record.h" #include "llvm/TableGen/StringMatcher.h" #include "llvm/TableGen/TableGenBackend.h" -#include <map> #include <vector> using namespace llvm; +using namespace lldb_private; -/// Map of command names to their associated records. Also makes sure our -/// commands are sorted in a deterministic way. -typedef std::map<std::string, std::vector<Record *>> RecordsByCommand; - -/// Groups all records by their command. -static RecordsByCommand getCommandList(std::vector<Record *> Options) { - RecordsByCommand result; - for (Record *Option : Options) - result[Option->getValueAsString("Command").str()].push_back(Option); - return result; -} - -static void emitOption(Record *Option, raw_ostream &OS) { - OS << " {"; - - // List of option groups this option is in. +namespace { +struct CommandOption { std::vector<std::string> GroupsArg; - - if (Option->getValue("Groups")) { - // The user specified a list of groups. - auto Groups = Option->getValueAsListOfInts("Groups"); - for (int Group : Groups) - GroupsArg.push_back("LLDB_OPT_SET_" + std::to_string(Group)); - } else if (Option->getValue("GroupStart")) { - // The user specified a range of groups (with potentially only one element). - int GroupStart = Option->getValueAsInt("GroupStart"); - int GroupEnd = Option->getValueAsInt("GroupEnd"); - for (int i = GroupStart; i <= GroupEnd; ++i) - GroupsArg.push_back("LLDB_OPT_SET_" + std::to_string(i)); + bool Required = false; + std::string FullName; + std::string ShortName; + std::string ArgType; + bool OptionalArg = false; + std::string Validator; + std::string ArgEnum; + std::vector<StringRef> Completions; + std::string Description; + + CommandOption() = default; + CommandOption(Record *Option) { + if (Option->getValue("Groups")) { + // The user specified a list of groups. + auto Groups = Option->getValueAsListOfInts("Groups"); + for (int Group : Groups) + GroupsArg.push_back("LLDB_OPT_SET_" + std::to_string(Group)); + } else if (Option->getValue("GroupStart")) { + // The user specified a range of groups (with potentially only one + // element). + int GroupStart = Option->getValueAsInt("GroupStart"); + int GroupEnd = Option->getValueAsInt("GroupEnd"); + for (int i = GroupStart; i <= GroupEnd; ++i) + GroupsArg.push_back("LLDB_OPT_SET_" + std::to_string(i)); + } + + // Check if this option is required. + Required = Option->getValue("Required"); + + // Add the full and short name for this option. + FullName = Option->getValueAsString("FullName"); + ShortName = Option->getValueAsString("ShortName"); + + if (auto A = Option->getValue("ArgType")) + ArgType = A->getValue()->getAsUnquotedString(); + OptionalArg = Option->getValue("OptionalArg") != nullptr; + + if (Option->getValue("Validator")) + Validator = Option->getValueAsString("Validator"); + + if (Option->getValue("ArgEnum")) + ArgEnum = Option->getValueAsString("ArgEnum"); + + if (Option->getValue("Completions")) + Completions = Option->getValueAsListOfStrings("Completions"); + + if (auto D = Option->getValue("Description")) + Description = D->getValue()->getAsUnquotedString(); } +}; +} // namespace + +static void emitOption(const CommandOption &O, raw_ostream &OS) { + OS << " {"; // If we have any groups, we merge them. Otherwise we move this option into // the all group. - if (GroupsArg.empty()) + if (O.GroupsArg.empty()) OS << "LLDB_OPT_SET_ALL"; else - OS << llvm::join(GroupsArg.begin(), GroupsArg.end(), " | "); + OS << llvm::join(O.GroupsArg.begin(), O.GroupsArg.end(), " | "); OS << ", "; // Check if this option is required. - OS << (Option->getValue("Required") ? "true" : "false"); + OS << (O.Required ? "true" : "false"); // Add the full and short name for this option. - OS << ", \"" << Option->getValueAsString("FullName") << "\", "; - OS << '\'' << Option->getValueAsString("ShortName") << "'"; - - auto ArgType = Option->getValue("ArgType"); - bool IsOptionalArg = Option->getValue("OptionalArg") != nullptr; + OS << ", \"" << O.FullName << "\", "; + OS << '\'' << O.ShortName << "'"; // Decide if we have either an option, required or no argument for this // option. OS << ", OptionParser::"; - if (ArgType) { - if (IsOptionalArg) + if (!O.ArgType.empty()) { + if (O.OptionalArg) OS << "eOptionalArgument"; else OS << "eRequiredArgument"; } else OS << "eNoArgument"; - OS << ", nullptr, "; + OS << ", "; - if (Option->getValue("ArgEnum")) - OS << Option->getValueAsString("ArgEnum"); + if (!O.Validator.empty()) + OS << O.Validator; + else + OS << "nullptr"; + OS << ", "; + + if (!O.ArgEnum.empty()) + OS << O.ArgEnum; else OS << "{}"; OS << ", "; // Read the tab completions we offer for this option (if there are any) - if (Option->getValue("Completions")) { - auto Completions = Option->getValueAsListOfStrings("Completions"); + if (!O.Completions.empty()) { std::vector<std::string> CompletionArgs; - for (llvm::StringRef Completion : Completions) + for (llvm::StringRef Completion : O.Completions) CompletionArgs.push_back("CommandCompletions::e" + Completion.str() + "Completion"); OS << llvm::join(CompletionArgs.begin(), CompletionArgs.end(), " | "); - } else { + } else OS << "CommandCompletions::eNoCompletion"; - } // Add the argument type. OS << ", eArgType"; - if (ArgType) { - OS << ArgType->getValue()->getAsUnquotedString(); + if (!O.ArgType.empty()) { + OS << O.ArgType; } else OS << "None"; OS << ", "; // Add the description if there is any. - if (auto D = Option->getValue("Description")) - OS << D->getValue()->getAsString(); - else + if (!O.Description.empty()) { + OS << "\""; + llvm::printEscapedString(O.Description, OS); + OS << "\""; + } else OS << "\"\""; OS << "},\n"; } /// Emits all option initializers to the raw_ostream. -static void emitOptions(std::string Command, std::vector<Record *> Option, +static void emitOptions(std::string Command, std::vector<Record *> Records, raw_ostream &OS) { + std::vector<CommandOption> Options; + for (Record *R : Records) + Options.emplace_back(R); + + std::string ID = Command; + std::replace(ID.begin(), ID.end(), ' ', '_'); // Generate the macro that the user needs to define before including the // *.inc file. - std::string NeededMacro = "LLDB_OPTIONS_" + Command; - std::replace(NeededMacro.begin(), NeededMacro.end(), ' ', '_'); + std::string NeededMacro = "LLDB_OPTIONS_" + ID; // All options are in one file, so we need put them behind macros and ask the // user to define the macro for the options that are needed. OS << "// Options for " << Command << "\n"; OS << "#ifdef " << NeededMacro << "\n"; - for (Record *R : Option) - emitOption(R, OS); + 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"; OS << "#undef " << NeededMacro << "\n"; OS << "#endif // " << Command << " command\n\n"; } void lldb_private::EmitOptionDefs(RecordKeeper &Records, raw_ostream &OS) { - - std::vector<Record *> Options = Records.getAllDerivedDefinitions("Option"); - emitSourceFileHeader("Options for LLDB command line commands.", OS); - RecordsByCommand ByCommand = getCommandList(Options); - - for (auto &CommandRecordPair : ByCommand) { + std::vector<Record *> Options = Records.getAllDerivedDefinitions("Option"); + for (auto &CommandRecordPair : getRecordsByName(Options, "Command")) { emitOptions(CommandRecordPair.first, CommandRecordPair.second, OS); } } diff --git a/utils/TableGen/LLDBPropertyDefEmitter.cpp b/utils/TableGen/LLDBPropertyDefEmitter.cpp new file mode 100644 index 000000000000..f49c05c9a585 --- /dev/null +++ b/utils/TableGen/LLDBPropertyDefEmitter.cpp @@ -0,0 +1,165 @@ +//===- LLDBPropertyDefEmitter.cpp -----------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// These tablegen backends emits LLDB's PropertyDefinition values. +// +//===----------------------------------------------------------------------===// + +#include "LLDBTableGenBackends.h" +#include "LLDBTableGenUtils.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/TableGen/Record.h" +#include "llvm/TableGen/StringMatcher.h" +#include "llvm/TableGen/TableGenBackend.h" +#include <vector> + +using namespace llvm; +using namespace lldb_private; + +static void emitPropertyEnum(Record *Property, raw_ostream &OS) { + OS << "eProperty"; + OS << Property->getName(); + OS << ",\n"; +} + +static void emitProperty(Record *Property, raw_ostream &OS) { + OS << " {"; + + // Emit the property name. + OS << "\"" << Property->getValueAsString("Name") << "\""; + OS << ", "; + + // Emit the property type. + OS << "OptionValue::eType"; + OS << Property->getValueAsString("Type"); + OS << ", "; + + // Emit the property's global value. + OS << (Property->getValue("Global") ? "true" : "false"); + OS << ", "; + + bool hasDefaultUnsignedValue = Property->getValue("HasDefaultUnsignedValue"); + bool hasDefaultEnumValue = Property->getValue("HasDefaultEnumValue"); + bool hasDefaultStringValue = Property->getValue("HasDefaultStringValue"); + + // Guarantee that every property has a default value. + assert((hasDefaultUnsignedValue || hasDefaultEnumValue || + hasDefaultStringValue) && + "Property must have a default value"); + + // Guarantee that no property has both a default unsigned value and a default + // enum value, since they're bothed stored in the same field. + assert(!(hasDefaultUnsignedValue && hasDefaultEnumValue) && + "Property cannot have both a unsigned and enum default value."); + + // Emit the default uint value. + if (hasDefaultUnsignedValue) { + OS << std::to_string(Property->getValueAsInt("DefaultUnsignedValue")); + } else if (hasDefaultEnumValue) { + OS << Property->getValueAsString("DefaultEnumValue"); + } else { + OS << "0"; + } + OS << ", "; + + // Emit the default string value. + if (hasDefaultStringValue) { + if (auto D = Property->getValue("DefaultStringValue")) { + OS << "\""; + OS << D->getValue()->getAsUnquotedString(); + OS << "\""; + } else { + OS << "\"\""; + } + } else { + OS << "nullptr"; + } + OS << ", "; + + // Emit the enum values value. + if (Property->getValue("EnumValues")) + OS << Property->getValueAsString("EnumValues"); + else + OS << "{}"; + OS << ", "; + + // Emit the property description. + if (auto D = Property->getValue("Description")) { + OS << "\""; + OS << D->getValue()->getAsUnquotedString(); + OS << "\""; + } else { + OS << "\"\""; + } + + OS << "},\n"; +} + +/// Emits all property initializers to the raw_ostream. +static void emityProperties(std::string PropertyName, + std::vector<Record *> PropertyRecords, + raw_ostream &OS) { + // Generate the macro that the user needs to define before including the + // *.inc file. + std::string NeededMacro = "LLDB_PROPERTIES_" + PropertyName; + std::replace(NeededMacro.begin(), NeededMacro.end(), ' ', '_'); + + // All options are in one file, so we need put them behind macros and ask the + // user to define the macro for the options that are needed. + OS << "// Property definitions for " << PropertyName << "\n"; + OS << "#ifdef " << NeededMacro << "\n"; + OS << "static constexpr PropertyDefinition g_" << PropertyName + << "_properties[] = {\n"; + for (Record *R : PropertyRecords) + emitProperty(R, OS); + OS << "};\n"; + // We undefine the macro for the user like Clang's include files are doing it. + OS << "#undef " << NeededMacro << "\n"; + OS << "#endif // " << PropertyName << " Property\n\n"; +} + +/// Emits all property initializers to the raw_ostream. +static void emitPropertyEnum(std::string PropertyName, + std::vector<Record *> PropertyRecords, + raw_ostream &OS) { + // Generate the macro that the user needs to define before including the + // *.inc file. + std::string NeededMacro = "LLDB_PROPERTIES_" + PropertyName; + std::replace(NeededMacro.begin(), NeededMacro.end(), ' ', '_'); + + // All options are in one file, so we need put them behind macros and ask the + // user to define the macro for the options that are needed. + OS << "// Property enum cases for " << PropertyName << "\n"; + OS << "#ifdef " << NeededMacro << "\n"; + for (Record *R : PropertyRecords) + emitPropertyEnum(R, OS); + // We undefine the macro for the user like Clang's include files are doing it. + OS << "#undef " << NeededMacro << "\n"; + OS << "#endif // " << PropertyName << " Property\n\n"; +} + +void lldb_private::EmitPropertyDefs(RecordKeeper &Records, raw_ostream &OS) { + emitSourceFileHeader("Property definitions for LLDB.", OS); + + std::vector<Record *> Properties = + Records.getAllDerivedDefinitions("Property"); + for (auto &PropertyRecordPair : getRecordsByName(Properties, "Definition")) { + emityProperties(PropertyRecordPair.first, PropertyRecordPair.second, OS); + } +} + +void lldb_private::EmitPropertyEnumDefs(RecordKeeper &Records, + raw_ostream &OS) { + emitSourceFileHeader("Property definition enum for LLDB.", OS); + + std::vector<Record *> Properties = + Records.getAllDerivedDefinitions("Property"); + for (auto &PropertyRecordPair : getRecordsByName(Properties, "Definition")) { + emitPropertyEnum(PropertyRecordPair.first, PropertyRecordPair.second, OS); + } +} diff --git a/utils/TableGen/LLDBTableGen.cpp b/utils/TableGen/LLDBTableGen.cpp index 9325fe038561..abb6589f0ca6 100644 --- a/utils/TableGen/LLDBTableGen.cpp +++ b/utils/TableGen/LLDBTableGen.cpp @@ -1,4 +1,4 @@ -//===- TableGen.cpp - Top-Level TableGen implementation for Clang ---------===// +//===- LLDBTableGen.cpp - Top-Level TableGen implementation for LLDB ------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// // -// This file contains the main function for Clang's TableGen. +// This file contains the main function for LLDB's TableGen. // //===----------------------------------------------------------------------===// @@ -25,16 +25,22 @@ enum ActionType { PrintRecords, DumpJSON, GenOptionDefs, + GenPropertyDefs, + GenPropertyEnumDefs, }; -static cl::opt<ActionType> - Action(cl::desc("Action to perform:"), - cl::values(clEnumValN(PrintRecords, "print-records", - "Print all records to stdout (default)"), - clEnumValN(DumpJSON, "dump-json", - "Dump all records as machine-readable JSON"), - clEnumValN(GenOptionDefs, "gen-lldb-option-defs", - "Generate clang attribute clases"))); +static cl::opt<ActionType> Action( + cl::desc("Action to perform:"), + cl::values(clEnumValN(PrintRecords, "print-records", + "Print all records to stdout (default)"), + clEnumValN(DumpJSON, "dump-json", + "Dump all records as machine-readable JSON"), + clEnumValN(GenOptionDefs, "gen-lldb-option-defs", + "Generate lldb option definitions"), + clEnumValN(GenPropertyDefs, "gen-lldb-property-defs", + "Generate lldb property definitions"), + clEnumValN(GenPropertyEnumDefs, "gen-lldb-property-enum-defs", + "Generate lldb property enum definitions"))); static bool LLDBTableGenMain(raw_ostream &OS, RecordKeeper &Records) { switch (Action) { @@ -47,6 +53,12 @@ static bool LLDBTableGenMain(raw_ostream &OS, RecordKeeper &Records) { case GenOptionDefs: EmitOptionDefs(Records, OS); break; + case GenPropertyDefs: + EmitPropertyDefs(Records, OS); + break; + case GenPropertyEnumDefs: + EmitPropertyEnumDefs(Records, OS); + break; } return false; } diff --git a/utils/TableGen/LLDBTableGenBackends.h b/utils/TableGen/LLDBTableGenBackends.h index eb14d80c5627..b424abfce9a7 100644 --- a/utils/TableGen/LLDBTableGenBackends.h +++ b/utils/TableGen/LLDBTableGenBackends.h @@ -1,4 +1,4 @@ -//===- TableGen.cpp - Top-Level TableGen implementation for Clang ---------===// +//===- LLDBTableGenBackends.h -----------------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -7,19 +7,21 @@ //===----------------------------------------------------------------------===// // // This file contains the declarations for all of the LLDB TableGen -// backends. A "TableGen backend" is just a function. See -// "$LLVM_ROOT/utils/TableGen/TableGenBackends.h" for more info. +// backends. A "TableGen backend" is just a function. +// +// See "$LLVM_ROOT/utils/TableGen/TableGenBackends.h" for more info. // //===----------------------------------------------------------------------===// #ifndef LLVM_LLDB_UTILS_TABLEGEN_TABLEGENBACKENDS_H #define LLVM_LLDB_UTILS_TABLEGEN_TABLEGENBACKENDS_H -#include <string> +#include "llvm/ADT/StringRef.h" namespace llvm { class raw_ostream; class RecordKeeper; +class Record; } // namespace llvm using llvm::raw_ostream; @@ -28,6 +30,8 @@ using llvm::RecordKeeper; namespace lldb_private { void EmitOptionDefs(RecordKeeper &RK, raw_ostream &OS); +void EmitPropertyDefs(RecordKeeper &RK, raw_ostream &OS); +void EmitPropertyEnumDefs(RecordKeeper &RK, raw_ostream &OS); } // namespace lldb_private diff --git a/utils/TableGen/LLDBTableGenUtils.cpp b/utils/TableGen/LLDBTableGenUtils.cpp new file mode 100644 index 000000000000..8b4c7581c98b --- /dev/null +++ b/utils/TableGen/LLDBTableGenUtils.cpp @@ -0,0 +1,21 @@ +//===- LLDBTableGenUtils.cpp ----------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "LLDBTableGenUtils.h" +#include "llvm/TableGen/Record.h" + +using namespace llvm; +using namespace lldb_private; + +RecordsByName lldb_private::getRecordsByName(std::vector<Record *> Records, + StringRef Name) { + RecordsByName Result; + for (Record *R : Records) + Result[R->getValueAsString(Name).str()].push_back(R); + return Result; +} diff --git a/utils/TableGen/LLDBTableGenUtils.h b/utils/TableGen/LLDBTableGenUtils.h new file mode 100644 index 000000000000..5553cecafb12 --- /dev/null +++ b/utils/TableGen/LLDBTableGenUtils.h @@ -0,0 +1,34 @@ +//===- LLDBTableGenUtils.h --------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LLDB_UTILS_TABLEGEN_TABLEGENUTILS_H +#define LLVM_LLDB_UTILS_TABLEGEN_TABLEGENUTILS_H + +#include "llvm/ADT/StringRef.h" +#include <map> +#include <string> +#include <vector> + +namespace llvm { +class RecordKeeper; +class Record; +} // namespace llvm + +namespace lldb_private { + +/// Map of names to their associated records. This map also ensures that our +/// records are sorted in a deterministic way. +typedef std::map<std::string, std::vector<llvm::Record *>> RecordsByName; + +/// Return records grouped by name. +RecordsByName getRecordsByName(std::vector<llvm::Record *> Records, + llvm::StringRef); + +} // namespace lldb_private + +#endif |