1 //===- TableGen.cpp - Top-Level TableGen implementation for Clang ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // These tablegen backends emits LLDB's OptionDefinition values for different
10 // LLDB commands.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "LLDBTableGenBackends.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 <map>
20 #include <vector>
21 
22 using namespace llvm;
23 
24 /// Map of command names to their associated records. Also makes sure our
25 /// commands are sorted in a deterministic way.
26 typedef std::map<std::string, std::vector<Record *>> RecordsByCommand;
27 
28 /// Groups all records by their command.
29 static RecordsByCommand getCommandList(std::vector<Record *> Options) {
30   RecordsByCommand result;
31   for (Record *Option : Options)
32     result[Option->getValueAsString("Command").str()].push_back(Option);
33   return result;
34 }
35 
36 static void emitOption(Record *Option, raw_ostream &OS) {
37   OS << "  {";
38 
39   // List of option groups this option is in.
40   std::vector<std::string> GroupsArg;
41 
42   if (Option->getValue("Groups")) {
43     // The user specified a list of groups.
44     auto Groups = Option->getValueAsListOfInts("Groups");
45     for (int Group : Groups)
46       GroupsArg.push_back("LLDB_OPT_SET_" + std::to_string(Group));
47     OS << llvm::join(GroupsArg.begin(), GroupsArg.end(), " | ");
48   } else if (Option->getValue("GroupStart")) {
49     // The user specified a range of groups (with potentially only one element).
50     int GroupStart = Option->getValueAsInt("GroupStart");
51     int GroupEnd = Option->getValueAsInt("GroupEnd");
52     for (int i = GroupStart; i <= GroupEnd; ++i)
53       GroupsArg.push_back("LLDB_OPT_SET_" + std::to_string(i));
54   }
55 
56   // If we have any groups, we merge them. Otherwise we move this option into
57   // the all group.
58   if (GroupsArg.empty())
59     OS << "LLDB_OPT_SET_ALL";
60   else
61     OS << llvm::join(GroupsArg.begin(), GroupsArg.end(), " | ");
62 
63   OS << ", ";
64 
65   // Check if this option is required.
66   OS << (Option->getValue("Required") ? "true" : "false");
67 
68   // Add the full and short name for this option.
69   OS << ", \"" << Option->getValueAsString("FullName") << "\", ";
70   OS << '\'' << Option->getValueAsString("ShortName") << "'";
71 
72   auto ArgType = Option->getValue("ArgType");
73   bool IsOptionalArg = Option->getValue("OptionalArg") != nullptr;
74 
75   // Decide if we have either an option, required or no argument for this
76   // option.
77   OS << ", OptionParser::";
78   if (ArgType) {
79     if (IsOptionalArg)
80       OS << "eOptionalArgument";
81     else
82       OS << "eRequiredArgument";
83   } else
84     OS << "eNoArgument";
85   OS << ", nullptr, ";
86 
87   if (Option->getValue("ArgEnum"))
88     OS << Option->getValueAsString("ArgEnum");
89   else
90     OS << "{}";
91   OS << ", ";
92 
93   // Read the tab completions we offer for this option (if there are any)
94   if (Option->getValue("Completions")) {
95     auto Completions = Option->getValueAsListOfStrings("Completions");
96     std::vector<std::string> CompletionArgs;
97     for (llvm::StringRef Completion : Completions)
98       CompletionArgs.push_back("CommandCompletions::e" + Completion.str() +
99                                "Completion");
100 
101     OS << llvm::join(CompletionArgs.begin(), CompletionArgs.end(), " | ");
102   } else {
103     OS << "CommandCompletions::eNoCompletion";
104   }
105 
106   // Add the argument type.
107   OS << ", eArgType";
108   if (ArgType) {
109     OS << ArgType->getValue()->getAsUnquotedString();
110   } else
111     OS << "None";
112   OS << ", ";
113 
114   // Add the description if there is any.
115   if (auto D = Option->getValue("Description"))
116     OS << D->getValue()->getAsString();
117   else
118     OS << "\"\"";
119   OS << "},\n";
120 }
121 
122 /// Emits all option initializers to the raw_ostream.
123 static void emitOptions(std::string Command, std::vector<Record *> Option,
124                         raw_ostream &OS) {
125   // Generate the macro that the user needs to define before including the
126   // *.inc file.
127   std::string NeededMacro = "LLDB_OPTIONS_" + Command;
128   std::replace(NeededMacro.begin(), NeededMacro.end(), ' ', '_');
129 
130   // All options are in one file, so we need put them behind macros and ask the
131   // user to define the macro for the options that are needed.
132   OS << "// Options for " << Command << "\n";
133   OS << "#ifdef " << NeededMacro << "\n";
134   for (Record *R : Option)
135     emitOption(R, OS);
136   // We undefine the macro for the user like Clang's include files are doing it.
137   OS << "#undef " << NeededMacro << "\n";
138   OS << "#endif // " << Command << " command\n\n";
139 }
140 
141 void lldb_private::EmitOptionDefs(RecordKeeper &Records, raw_ostream &OS) {
142 
143   std::vector<Record *> Options = Records.getAllDerivedDefinitions("Option");
144 
145   emitSourceFileHeader("Options for LLDB command line commands.", OS);
146 
147   RecordsByCommand ByCommand = getCommandList(Options);
148 
149   for (auto &CommandRecordPair : ByCommand) {
150     emitOptions(CommandRecordPair.first, CommandRecordPair.second, OS);
151   }
152 }
153