Path: blob/main/contrib/llvm-project/lldb/utils/TableGen/LLDBOptionDefEmitter.cpp
39563 views
//===- LLDBOptionDefEmitter.cpp -------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// These tablegen backends emits LLDB's OptionDefinition values for different9// LLDB commands.10//11//===----------------------------------------------------------------------===//1213#include "LLDBTableGenBackends.h"14#include "LLDBTableGenUtils.h"15#include "llvm/ADT/StringExtras.h"16#include "llvm/TableGen/Record.h"17#include "llvm/TableGen/StringMatcher.h"18#include "llvm/TableGen/TableGenBackend.h"19#include <vector>2021using namespace llvm;22using namespace lldb_private;2324namespace {25struct CommandOption {26std::vector<std::string> GroupsArg;27bool Required = false;28std::string FullName;29std::string ShortName;30std::string ArgType;31bool OptionalArg = false;32std::string Validator;33std::vector<StringRef> Completions;34std::string Description;3536CommandOption() = default;37CommandOption(Record *Option) {38if (Option->getValue("Groups")) {39// The user specified a list of groups.40auto Groups = Option->getValueAsListOfInts("Groups");41for (int Group : Groups)42GroupsArg.push_back("LLDB_OPT_SET_" + std::to_string(Group));43} else if (Option->getValue("GroupStart")) {44// The user specified a range of groups (with potentially only one45// element).46int GroupStart = Option->getValueAsInt("GroupStart");47int GroupEnd = Option->getValueAsInt("GroupEnd");48for (int i = GroupStart; i <= GroupEnd; ++i)49GroupsArg.push_back("LLDB_OPT_SET_" + std::to_string(i));50}5152// Check if this option is required.53Required = Option->getValue("Required");5455// Add the full and short name for this option.56FullName = std::string(Option->getValueAsString("FullName"));57ShortName = std::string(Option->getValueAsString("ShortName"));5859if (auto A = Option->getValue("ArgType"))60ArgType = A->getValue()->getAsUnquotedString();61OptionalArg = Option->getValue("OptionalArg") != nullptr;6263if (Option->getValue("Validator"))64Validator = std::string(Option->getValueAsString("Validator"));6566if (Option->getValue("Completions"))67Completions = Option->getValueAsListOfStrings("Completions");6869if (auto D = Option->getValue("Description"))70Description = D->getValue()->getAsUnquotedString();71}72};73} // namespace7475static void emitOption(const CommandOption &O, raw_ostream &OS) {76OS << " {";7778// If we have any groups, we merge them. Otherwise we move this option into79// the all group.80if (O.GroupsArg.empty())81OS << "LLDB_OPT_SET_ALL";82else83OS << llvm::join(O.GroupsArg.begin(), O.GroupsArg.end(), " | ");8485OS << ", ";8687// Check if this option is required.88OS << (O.Required ? "true" : "false");8990// Add the full and short name for this option.91OS << ", \"" << O.FullName << "\", ";92OS << '\'' << O.ShortName << "'";9394// Decide if we have either an option, required or no argument for this95// option.96OS << ", OptionParser::";97if (!O.ArgType.empty()) {98if (O.OptionalArg)99OS << "eOptionalArgument";100else101OS << "eRequiredArgument";102} else103OS << "eNoArgument";104OS << ", ";105106if (!O.Validator.empty())107OS << O.Validator;108else109OS << "nullptr";110OS << ", ";111112if (!O.ArgType.empty())113OS << "g_argument_table[eArgType" << O.ArgType << "].enum_values";114else115OS << "{}";116OS << ", ";117118// Read the tab completions we offer for this option (if there are any)119if (!O.Completions.empty()) {120std::vector<std::string> CompletionArgs;121for (llvm::StringRef Completion : O.Completions)122CompletionArgs.push_back("e" + Completion.str() + "Completion");123124OS << llvm::join(CompletionArgs.begin(), CompletionArgs.end(), " | ");125} else126OS << "CompletionType::eNoCompletion";127128// Add the argument type.129OS << ", eArgType";130if (!O.ArgType.empty()) {131OS << O.ArgType;132} else133OS << "None";134OS << ", ";135136// Add the description if there is any.137if (!O.Description.empty()) {138OS << "\"";139llvm::printEscapedString(O.Description, OS);140OS << "\"";141} else142OS << "\"\"";143OS << "},\n";144}145146/// Emits all option initializers to the raw_ostream.147static void emitOptions(std::string Command, std::vector<Record *> Records,148raw_ostream &OS) {149std::vector<CommandOption> Options;150for (Record *R : Records)151Options.emplace_back(R);152153std::string ID = Command;154std::replace(ID.begin(), ID.end(), ' ', '_');155// Generate the macro that the user needs to define before including the156// *.inc file.157std::string NeededMacro = "LLDB_OPTIONS_" + ID;158159// All options are in one file, so we need put them behind macros and ask the160// user to define the macro for the options that are needed.161OS << "// Options for " << Command << "\n";162OS << "#ifdef " << NeededMacro << "\n";163OS << "constexpr static OptionDefinition g_" + ID + "_options[] = {\n";164for (CommandOption &CO : Options)165emitOption(CO, OS);166// We undefine the macro for the user like Clang's include files are doing it.167OS << "};\n";168OS << "#undef " << NeededMacro << "\n";169OS << "#endif // " << Command << " command\n\n";170}171172void lldb_private::EmitOptionDefs(RecordKeeper &Records, raw_ostream &OS) {173emitSourceFileHeader("Options for LLDB command line commands.", OS, Records);174175std::vector<Record *> Options = Records.getAllDerivedDefinitions("Option");176for (auto &CommandRecordPair : getRecordsByName(Options, "Command")) {177emitOptions(CommandRecordPair.first, CommandRecordPair.second, OS);178}179}180181182