Skip to content

Commit 32cef07

Browse files
authored
[LLDB][TableGen] Migrate lldb-tblgen to use const RecordKeeper (#107536)
Migrate LLDB TableGen backend to use const RecordKeeper. This is a part of effort to have better const correctness in TableGen backends: https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089
1 parent cca54e3 commit 32cef07

6 files changed

+26
-25
lines changed

lldb/utils/TableGen/LLDBOptionDefEmitter.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct CommandOption {
3535
std::string Description;
3636

3737
CommandOption() = default;
38-
CommandOption(Record *Option) {
38+
CommandOption(const Record *Option) {
3939
if (Option->getValue("Groups")) {
4040
// The user specified a list of groups.
4141
auto Groups = Option->getValueAsListOfInts("Groups");
@@ -145,11 +145,9 @@ static void emitOption(const CommandOption &O, raw_ostream &OS) {
145145
}
146146

147147
/// Emits all option initializers to the raw_ostream.
148-
static void emitOptions(std::string Command, std::vector<Record *> Records,
148+
static void emitOptions(std::string Command, ArrayRef<const Record *> Records,
149149
raw_ostream &OS) {
150-
std::vector<CommandOption> Options;
151-
for (Record *R : Records)
152-
Options.emplace_back(R);
150+
std::vector<CommandOption> Options(Records.begin(), Records.end());
153151

154152
std::string ID = Command;
155153
std::replace(ID.begin(), ID.end(), ' ', '_');
@@ -170,10 +168,11 @@ static void emitOptions(std::string Command, std::vector<Record *> Records,
170168
OS << "#endif // " << Command << " command\n\n";
171169
}
172170

173-
void lldb_private::EmitOptionDefs(RecordKeeper &Records, raw_ostream &OS) {
171+
void lldb_private::EmitOptionDefs(const RecordKeeper &Records,
172+
raw_ostream &OS) {
174173
emitSourceFileHeader("Options for LLDB command line commands.", OS, Records);
175174

176-
std::vector<Record *> Options = Records.getAllDerivedDefinitions("Option");
175+
ArrayRef<const Record *> Options = Records.getAllDerivedDefinitions("Option");
177176
for (auto &CommandRecordPair : getRecordsByName(Options, "Command")) {
178177
emitOptions(CommandRecordPair.first, CommandRecordPair.second, OS);
179178
}

lldb/utils/TableGen/LLDBPropertyDefEmitter.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
using namespace llvm;
2222
using namespace lldb_private;
2323

24-
static void emitPropertyEnum(Record *Property, raw_ostream &OS) {
24+
static void emitPropertyEnum(const Record *Property, raw_ostream &OS) {
2525
OS << "eProperty";
2626
OS << Property->getName();
2727
OS << ",\n";
2828
}
2929

30-
static void emitProperty(Record *Property, raw_ostream &OS) {
30+
static void emitProperty(const Record *Property, raw_ostream &OS) {
3131
OS << " {";
3232

3333
// Emit the property name.
@@ -126,7 +126,7 @@ static void emitProperty(Record *Property, raw_ostream &OS) {
126126

127127
/// Emits all property initializers to the raw_ostream.
128128
static void emityProperties(std::string PropertyName,
129-
std::vector<Record *> PropertyRecords,
129+
const std::vector<const Record *> &PropertyRecords,
130130
raw_ostream &OS) {
131131
// Generate the macro that the user needs to define before including the
132132
// *.inc file.
@@ -139,7 +139,7 @@ static void emityProperties(std::string PropertyName,
139139
OS << "#ifdef " << NeededMacro << "\n";
140140
OS << "static constexpr PropertyDefinition g_" << PropertyName
141141
<< "_properties[] = {\n";
142-
for (Record *R : PropertyRecords)
142+
for (const Record *R : PropertyRecords)
143143
emitProperty(R, OS);
144144
OS << "};\n";
145145
// We undefine the macro for the user like Clang's include files are doing it.
@@ -149,7 +149,7 @@ static void emityProperties(std::string PropertyName,
149149

150150
/// Emits all property initializers to the raw_ostream.
151151
static void emitPropertyEnum(std::string PropertyName,
152-
std::vector<Record *> PropertyRecords,
152+
ArrayRef<const Record *> PropertyRecords,
153153
raw_ostream &OS) {
154154
// Generate the macro that the user needs to define before including the
155155
// *.inc file.
@@ -160,28 +160,29 @@ static void emitPropertyEnum(std::string PropertyName,
160160
// user to define the macro for the options that are needed.
161161
OS << "// Property enum cases for " << PropertyName << "\n";
162162
OS << "#ifdef " << NeededMacro << "\n";
163-
for (Record *R : PropertyRecords)
163+
for (const Record *R : PropertyRecords)
164164
emitPropertyEnum(R, OS);
165165
// We undefine the macro for the user like Clang's include files are doing it.
166166
OS << "#undef " << NeededMacro << "\n";
167167
OS << "#endif // " << PropertyName << " Property\n\n";
168168
}
169169

170-
void lldb_private::EmitPropertyDefs(RecordKeeper &Records, raw_ostream &OS) {
170+
void lldb_private::EmitPropertyDefs(const RecordKeeper &Records,
171+
raw_ostream &OS) {
171172
emitSourceFileHeader("Property definitions for LLDB.", OS, Records);
172173

173-
std::vector<Record *> Properties =
174+
ArrayRef<const Record *> Properties =
174175
Records.getAllDerivedDefinitions("Property");
175176
for (auto &PropertyRecordPair : getRecordsByName(Properties, "Definition")) {
176177
emityProperties(PropertyRecordPair.first, PropertyRecordPair.second, OS);
177178
}
178179
}
179180

180-
void lldb_private::EmitPropertyEnumDefs(RecordKeeper &Records,
181+
void lldb_private::EmitPropertyEnumDefs(const RecordKeeper &Records,
181182
raw_ostream &OS) {
182183
emitSourceFileHeader("Property definition enum for LLDB.", OS, Records);
183184

184-
std::vector<Record *> Properties =
185+
ArrayRef<const Record *> Properties =
185186
Records.getAllDerivedDefinitions("Property");
186187
for (auto &PropertyRecordPair : getRecordsByName(Properties, "Definition")) {
187188
emitPropertyEnum(PropertyRecordPair.first, PropertyRecordPair.second, OS);

lldb/utils/TableGen/LLDBTableGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static cl::opt<ActionType> Action(
4343
clEnumValN(GenPropertyEnumDefs, "gen-lldb-property-enum-defs",
4444
"Generate lldb property enum definitions")));
4545

46-
static bool LLDBTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
46+
static bool LLDBTableGenMain(raw_ostream &OS, const RecordKeeper &Records) {
4747
switch (Action) {
4848
case PrintRecords:
4949
OS << Records; // No argument, dump all contents

lldb/utils/TableGen/LLDBTableGenBackends.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ using llvm::RecordKeeper;
2929

3030
namespace lldb_private {
3131

32-
void EmitOptionDefs(RecordKeeper &RK, raw_ostream &OS);
33-
void EmitPropertyDefs(RecordKeeper &RK, raw_ostream &OS);
34-
void EmitPropertyEnumDefs(RecordKeeper &RK, raw_ostream &OS);
32+
void EmitOptionDefs(const RecordKeeper &RK, raw_ostream &OS);
33+
void EmitPropertyDefs(const RecordKeeper &RK, raw_ostream &OS);
34+
void EmitPropertyEnumDefs(const RecordKeeper &RK, raw_ostream &OS);
3535
int EmitSBAPIDWARFEnum(int argc, char **argv);
3636

3737
} // namespace lldb_private

lldb/utils/TableGen/LLDBTableGenUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
using namespace llvm;
1313
using namespace lldb_private;
1414

15-
RecordsByName lldb_private::getRecordsByName(std::vector<Record *> Records,
15+
RecordsByName lldb_private::getRecordsByName(ArrayRef<const Record *> Records,
1616
StringRef Name) {
1717
RecordsByName Result;
18-
for (Record *R : Records)
18+
for (const Record *R : Records)
1919
Result[R->getValueAsString(Name).str()].push_back(R);
2020
return Result;
2121
}

lldb/utils/TableGen/LLDBTableGenUtils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLDB_UTILS_TABLEGEN_LLDBTABLEGENUTILS_H
1010
#define LLDB_UTILS_TABLEGEN_LLDBTABLEGENUTILS_H
1111

12+
#include "llvm/ADT/ArrayRef.h"
1213
#include "llvm/ADT/StringRef.h"
1314
#include <map>
1415
#include <string>
@@ -23,10 +24,10 @@ namespace lldb_private {
2324

2425
/// Map of names to their associated records. This map also ensures that our
2526
/// records are sorted in a deterministic way.
26-
typedef std::map<std::string, std::vector<llvm::Record *>> RecordsByName;
27+
typedef std::map<std::string, std::vector<const llvm::Record *>> RecordsByName;
2728

2829
/// Return records grouped by name.
29-
RecordsByName getRecordsByName(std::vector<llvm::Record *> Records,
30+
RecordsByName getRecordsByName(llvm::ArrayRef<const llvm::Record *> Records,
3031
llvm::StringRef);
3132

3233
} // namespace lldb_private

0 commit comments

Comments
 (0)