Skip to content

Commit 116013b

Browse files
committed
[NFC][TableGen] Refactor JSON and detailed record emitter
- Fix JSON and detailed record emitters to use const reference and pointers. - Fix code to use C++ structured bindings and range based loops, include reverse() range for locations. - Eliminate `NL` define for "\n". - Change JSON emitter to populate `instance_list` in an earlier loop over superclasses instead of a separate loop.
1 parent 4e6ff75 commit 116013b

File tree

2 files changed

+58
-78
lines changed

2 files changed

+58
-78
lines changed

llvm/lib/TableGen/DetailedRecordsBackend.cpp

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,27 @@
2626
#include <string>
2727
#include <utility>
2828

29-
#define DEBUG_TYPE "detailed-records-backend"
30-
31-
#define NL "\n"
32-
3329
using namespace llvm;
3430

3531
namespace {
3632

3733
class DetailedRecordsEmitter {
3834
private:
39-
RecordKeeper &Records;
35+
const RecordKeeper &Records;
4036

4137
public:
42-
DetailedRecordsEmitter(RecordKeeper &RK) : Records(RK) {}
38+
DetailedRecordsEmitter(const RecordKeeper &RK) : Records(RK) {}
4339

4440
void run(raw_ostream &OS);
4541
void printReportHeading(raw_ostream &OS);
42+
void printSectionHeading(StringRef Title, int Count, raw_ostream &OS);
4643
void printVariables(raw_ostream &OS);
4744
void printClasses(raw_ostream &OS);
4845
void printRecords(raw_ostream &OS);
49-
void printSectionHeading(StringRef Title, int Count, raw_ostream &OS);
50-
void printDefms(Record *Rec, raw_ostream &OS);
51-
void printTemplateArgs(Record *Rec, raw_ostream &OS);
52-
void printSuperclasses(Record *Rec, raw_ostream &OS);
53-
void printFields(Record *Rec, raw_ostream &OS);
46+
void printDefms(const Record *Rec, raw_ostream &OS);
47+
void printTemplateArgs(const Record *Rec, raw_ostream &OS);
48+
void printSuperclasses(const Record *Rec, raw_ostream &OS);
49+
void printFields(const Record *Rec, raw_ostream &OS);
5450
}; // emitter class
5551

5652
} // anonymous namespace
@@ -68,14 +64,21 @@ void DetailedRecordsEmitter::printReportHeading(raw_ostream &OS) {
6864
OS << formatv("DETAILED RECORDS for file {0}\n", Records.getInputFilename());
6965
}
7066

67+
// Print a section heading with the name of the section and
68+
// the item count.
69+
void DetailedRecordsEmitter::printSectionHeading(StringRef Title, int Count,
70+
raw_ostream &OS) {
71+
OS << formatv("\n{0} {1} ({2}) {0}\n", "--------------------", Title, Count);
72+
}
73+
7174
// Print the global variables.
7275
void DetailedRecordsEmitter::printVariables(raw_ostream &OS) {
7376
const auto GlobalList = Records.getGlobals();
7477
printSectionHeading("Global Variables", GlobalList.size(), OS);
7578

76-
OS << NL;
79+
OS << "\n";
7780
for (const auto &Var : GlobalList) {
78-
OS << Var.first << " = " << Var.second->getAsString() << NL;
81+
OS << Var.first << " = " << Var.second->getAsString() << "\n";
7982
}
8083
}
8184

@@ -85,13 +88,12 @@ void DetailedRecordsEmitter::printClasses(raw_ostream &OS) {
8588
const auto &ClassList = Records.getClasses();
8689
printSectionHeading("Classes", ClassList.size(), OS);
8790

88-
for (const auto &ClassPair : ClassList) {
89-
auto *const Class = ClassPair.second.get();
91+
for (const auto &[Name, Class] : ClassList) {
9092
OS << formatv("\n{0} |{1}|\n", Class->getNameInitAsString(),
9193
SrcMgr.getFormattedLocationNoOffset(Class->getLoc().front()));
92-
printTemplateArgs(Class, OS);
93-
printSuperclasses(Class, OS);
94-
printFields(Class, OS);
94+
printTemplateArgs(Class.get(), OS);
95+
printSuperclasses(Class.get(), OS);
96+
printFields(Class.get(), OS);
9597
}
9698
}
9799

@@ -101,40 +103,31 @@ void DetailedRecordsEmitter::printRecords(raw_ostream &OS) {
101103
const auto &RecordList = Records.getDefs();
102104
printSectionHeading("Records", RecordList.size(), OS);
103105

104-
for (const auto &RecPair : RecordList) {
105-
auto *const Rec = RecPair.second.get();
106+
for (const auto &[DefName, Rec] : RecordList) {
106107
std::string Name = Rec->getNameInitAsString();
107108
OS << formatv("\n{0} |{1}|\n", Name.empty() ? "\"\"" : Name,
108109
SrcMgr.getFormattedLocationNoOffset(Rec->getLoc().front()));
109-
printDefms(Rec, OS);
110-
printSuperclasses(Rec, OS);
111-
printFields(Rec, OS);
110+
printDefms(Rec.get(), OS);
111+
printSuperclasses(Rec.get(), OS);
112+
printFields(Rec.get(), OS);
112113
}
113114
}
114115

115-
// Print a section heading with the name of the section and
116-
// the item count.
117-
void DetailedRecordsEmitter::printSectionHeading(StringRef Title, int Count,
118-
raw_ostream &OS) {
119-
OS << formatv("\n{0} {1} ({2}) {0}\n", "--------------------", Title, Count);
120-
}
121-
122116
// Print the record's defm source locations, if any. Note that they
123117
// are stored in the reverse order of their invocation.
124-
void DetailedRecordsEmitter::printDefms(Record *Rec, raw_ostream &OS) {
118+
void DetailedRecordsEmitter::printDefms(const Record *Rec, raw_ostream &OS) {
125119
const auto &LocList = Rec->getLoc();
126120
if (LocList.size() < 2)
127121
return;
128122

129123
OS << " Defm sequence:";
130-
for (unsigned I = LocList.size() - 1; I >= 1; --I) {
131-
OS << formatv(" |{0}|", SrcMgr.getFormattedLocationNoOffset(LocList[I]));
132-
}
133-
OS << NL;
124+
for (const SMLoc Loc : reverse(LocList))
125+
OS << formatv(" |{0}|", SrcMgr.getFormattedLocationNoOffset(Loc));
126+
OS << "\n";
134127
}
135128

136129
// Print the template arguments of a class.
137-
void DetailedRecordsEmitter::printTemplateArgs(Record *Rec,
130+
void DetailedRecordsEmitter::printTemplateArgs(const Record *Rec,
138131
raw_ostream &OS) {
139132
ArrayRef<Init *> Args = Rec->getTemplateArgs();
140133
if (Args.empty()) {
@@ -149,32 +142,32 @@ void DetailedRecordsEmitter::printTemplateArgs(Record *Rec,
149142
OS << " ";
150143
Value->print(OS, false);
151144
OS << formatv(" |{0}|", SrcMgr.getFormattedLocationNoOffset(Value->getLoc()));
152-
OS << NL;
145+
OS << "\n";
153146
}
154147
}
155148

156149
// Print the superclasses of a class or record. Indirect superclasses
157150
// are enclosed in parentheses.
158-
void DetailedRecordsEmitter::printSuperclasses(Record *Rec, raw_ostream &OS) {
151+
void DetailedRecordsEmitter::printSuperclasses(const Record *Rec,
152+
raw_ostream &OS) {
159153
ArrayRef<std::pair<Record *, SMRange>> Superclasses = Rec->getSuperClasses();
160154
if (Superclasses.empty()) {
161155
OS << " Superclasses: (none)\n";
162156
return;
163157
}
164158

165159
OS << " Superclasses:";
166-
for (const auto &SuperclassPair : Superclasses) {
167-
auto *ClassRec = SuperclassPair.first;
160+
for (const auto &[ClassRec, Loc] : Superclasses) {
168161
if (Rec->hasDirectSuperClass(ClassRec))
169162
OS << formatv(" {0}", ClassRec->getNameInitAsString());
170163
else
171164
OS << formatv(" ({0})", ClassRec->getNameInitAsString());
172165
}
173-
OS << NL;
166+
OS << "\n";
174167
}
175168

176169
// Print the fields of a class or record, including their source locations.
177-
void DetailedRecordsEmitter::printFields(Record *Rec, raw_ostream &OS) {
170+
void DetailedRecordsEmitter::printFields(const Record *Rec, raw_ostream &OS) {
178171
const auto &ValueList = Rec->getValues();
179172
if (ValueList.empty()) {
180173
OS << " Fields: (none)\n";
@@ -191,13 +184,8 @@ void DetailedRecordsEmitter::printFields(Record *Rec, raw_ostream &OS) {
191184
}
192185
}
193186

194-
namespace llvm {
195-
196187
// This function is called by TableGen after parsing the files.
197-
198-
void EmitDetailedRecords(RecordKeeper &RK, raw_ostream &OS) {
188+
void llvm::EmitDetailedRecords(RecordKeeper &RK, raw_ostream &OS) {
199189
// Instantiate the emitter class and invoke run().
200190
DetailedRecordsEmitter(RK).run(OS);
201191
}
202-
203-
} // namespace llvm

llvm/lib/TableGen/JSONBackend.cpp

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,19 @@ namespace {
2626

2727
class JSONEmitter {
2828
private:
29-
RecordKeeper &Records;
29+
const RecordKeeper &Records;
3030

3131
json::Value translateInit(const Init &I);
3232

3333
public:
34-
JSONEmitter(RecordKeeper &R);
34+
JSONEmitter(const RecordKeeper &R) : Records(R) {}
3535

3636
void run(raw_ostream &OS);
3737
};
3838

3939
} // end anonymous namespace
4040

41-
JSONEmitter::JSONEmitter(RecordKeeper &R) : Records(R) {}
42-
4341
json::Value JSONEmitter::translateInit(const Init &I) {
44-
4542
// Init subclasses that we return as JSON primitive values of one
4643
// kind or another.
4744

@@ -128,21 +125,18 @@ void JSONEmitter::run(raw_ostream &OS) {
128125
// over the classes, invoking std::map::operator[] to default-
129126
// construct the array for each one.
130127
std::map<std::string, json::Array> instance_lists;
131-
for (const auto &C : Records.getClasses()) {
132-
const auto Name = C.second->getNameInitAsString();
133-
(void)instance_lists[Name];
134-
}
128+
for (const auto &[Name, ClassRec] : Records.getClasses())
129+
instance_lists.emplace(ClassRec->getNameInitAsString(), json::Array());
135130

136131
// Main iteration over the defs.
137-
for (const auto &D : Records.getDefs()) {
138-
const auto Name = D.second->getNameInitAsString();
139-
auto &Def = *D.second;
132+
for (const auto &[MapName, Def] : Records.getDefs()) {
133+
const std::string Name = Def->getNameInitAsString();
140134

141135
json::Object obj;
142136
json::Array fields;
143137

144-
for (const RecordVal &RV : Def.getValues()) {
145-
if (!Def.isTemplateArg(RV.getNameInit())) {
138+
for (const RecordVal &RV : Def->getValues()) {
139+
if (!Def->isTemplateArg(RV.getNameInit())) {
146140
auto Name = RV.getNameInitAsString();
147141
if (RV.isNonconcreteOK())
148142
fields.push_back(Name);
@@ -153,38 +147,36 @@ void JSONEmitter::run(raw_ostream &OS) {
153147
obj["!fields"] = std::move(fields);
154148

155149
json::Array superclasses;
156-
for (const auto &SuperPair : Def.getSuperClasses())
157-
superclasses.push_back(SuperPair.first->getNameInitAsString());
150+
// Add this def to the instance list for each of its superclasses.
151+
for (const auto &[SuperClass, Loc] : Def->getSuperClasses()) {
152+
std::string SuperName = SuperClass->getNameInitAsString();
153+
superclasses.push_back(SuperName);
154+
instance_lists[SuperName].push_back(Name);
155+
}
156+
158157
obj["!superclasses"] = std::move(superclasses);
159158

160159
obj["!name"] = Name;
161-
obj["!anonymous"] = Def.isAnonymous();
160+
obj["!anonymous"] = Def->isAnonymous();
162161

163162
json::Array locs;
164-
for (const SMLoc Loc : Def.getLoc())
163+
for (const SMLoc Loc : Def->getLoc())
165164
locs.push_back(SrcMgr.getFormattedLocationNoOffset(Loc));
166165
obj["!locs"] = std::move(locs);
167166

168167
root[Name] = std::move(obj);
169-
170-
// Add this def to the instance list for each of its superclasses.
171-
for (const auto &SuperPair : Def.getSuperClasses()) {
172-
auto SuperName = SuperPair.first->getNameInitAsString();
173-
instance_lists[SuperName].push_back(Name);
174-
}
175168
}
176169

177170
// Make a JSON object from the std::map of instance lists.
178171
json::Object instanceof;
179-
for (auto kv: instance_lists)
180-
instanceof[kv.first] = std::move(kv.second);
172+
for (auto &[ClassName, Instances] : instance_lists)
173+
instanceof [ ClassName ] = std::move(Instances);
181174
root["!instanceof"] = std::move(instanceof);
182175

183176
// Done. Write the output.
184177
OS << json::Value(std::move(root)) << "\n";
185178
}
186179

187-
namespace llvm {
188-
189-
void EmitJSON(RecordKeeper &RK, raw_ostream &OS) { JSONEmitter(RK).run(OS); }
190-
} // end namespace llvm
180+
void llvm::EmitJSON(RecordKeeper &RK, raw_ostream &OS) {
181+
JSONEmitter(RK).run(OS);
182+
}

0 commit comments

Comments
 (0)