Skip to content

Commit 3f12ecd

Browse files
committed
[NFC][TableGen] Minor code cleanup in SearchableTableEmitter
- Add braces around if/else bodies per LLVM coding standards. - Use range for loops. - use auto for variables initialized with `dyn_cast`.
1 parent 9293b65 commit 3f12ecd

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

llvm/utils/TableGen/SearchableTableEmitter.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ using namespace llvm;
3131
#define DEBUG_TYPE "searchable-table-emitter"
3232

3333
static int64_t getAsInt(const Init *B) {
34-
if (const BitsInit *BI = dyn_cast<BitsInit>(B))
34+
if (const auto *BI = dyn_cast<BitsInit>(B))
3535
return *BI->convertInitializerToInt();
36-
if (const IntInit *II = dyn_cast<IntInit>(B))
36+
if (const auto *II = dyn_cast<IntInit>(B))
3737
return II->getValue();
3838
llvm_unreachable("Unexpected initializer");
3939
}
@@ -115,20 +115,20 @@ class SearchableTableEmitter {
115115

116116
std::string primaryRepresentation(SMLoc Loc, const GenericField &Field,
117117
const Init *I) {
118-
if (const StringInit *SI = dyn_cast<StringInit>(I)) {
118+
if (const auto *SI = dyn_cast<StringInit>(I)) {
119119
if (Field.IsCode || SI->hasCodeFormat())
120120
return SI->getValue().str();
121121
else
122122
return SI->getAsString();
123-
} else if (const BitsInit *BI = dyn_cast<BitsInit>(I))
123+
} else if (const auto *BI = dyn_cast<BitsInit>(I)) {
124124
return "0x" + utohexstr(getAsInt(BI));
125-
else if (const BitInit *BI = dyn_cast<BitInit>(I))
125+
} else if (const auto *BI = dyn_cast<BitInit>(I)) {
126126
return BI->getValue() ? "true" : "false";
127-
else if (Field.IsIntrinsic)
127+
} else if (Field.IsIntrinsic) {
128128
return "Intrinsic::" + getIntrinsic(I).EnumName.str();
129-
else if (Field.IsInstruction)
129+
} else if (Field.IsInstruction) {
130130
return I->getAsString();
131-
else if (Field.Enum) {
131+
} else if (Field.Enum) {
132132
auto *Entry = Field.Enum->EntryMap[cast<DefInit>(I)->getDef()];
133133
if (!Entry)
134134
PrintFatalError(Loc,
@@ -140,7 +140,7 @@ class SearchableTableEmitter {
140140
}
141141

142142
bool isIntrinsic(const Init *I) {
143-
if (const DefInit *DI = dyn_cast<DefInit>(I))
143+
if (const auto *DI = dyn_cast<DefInit>(I))
144144
return DI->getDef()->isSubClassOf("Intrinsic");
145145
return false;
146146
}
@@ -162,7 +162,7 @@ class SearchableTableEmitter {
162162
if (Ctx == TypeInTempStruct)
163163
return "std::string";
164164
return "StringRef";
165-
} else if (const BitsRecTy *BI = dyn_cast<BitsRecTy>(Field.RecType)) {
165+
} else if (const auto *BI = dyn_cast<BitsRecTy>(Field.RecType)) {
166166
unsigned NumBits = BI->getNumBits();
167167
if (NumBits <= 8)
168168
return "uint8_t";
@@ -178,8 +178,9 @@ class SearchableTableEmitter {
178178
"' of type bits is too large");
179179
} else if (isa<BitRecTy>(Field.RecType)) {
180180
return "bool";
181-
} else if (Field.Enum || Field.IsIntrinsic || Field.IsInstruction)
181+
} else if (Field.Enum || Field.IsIntrinsic || Field.IsInstruction) {
182182
return "unsigned";
183+
}
183184
PrintFatalError(Index.Loc,
184185
Twine("In table '") + Table.Name + "' lookup method '" +
185186
Index.Name + "', key field '" + Field.Name +
@@ -346,8 +347,8 @@ void SearchableTableEmitter::emitLookupFunction(const GenericTable &Table,
346347

347348
std::vector<std::pair<const Record *, unsigned>> Entries;
348349
Entries.reserve(Table.Entries.size());
349-
for (unsigned i = 0; i < Table.Entries.size(); ++i)
350-
Entries.emplace_back(Table.Entries[i], i);
350+
for (auto [Idx, TblEntry] : enumerate(Table.Entries))
351+
Entries.emplace_back(TblEntry, Idx);
351352

352353
llvm::stable_sort(Entries,
353354
[&](const std::pair<const Record *, unsigned> &LHS,
@@ -356,19 +357,19 @@ void SearchableTableEmitter::emitLookupFunction(const GenericTable &Table,
356357
});
357358

358359
IndexRowsStorage.reserve(Entries.size());
359-
for (const auto &Entry : Entries) {
360-
IndexRowsStorage.push_back(Entry.first);
360+
for (const auto &[EntryRec, EntryIndex] : Entries) {
361+
IndexRowsStorage.push_back(EntryRec);
361362

362363
OS << " { ";
363364
ListSeparator LS;
364365
for (const auto &Field : Index.Fields) {
365366
std::string Repr = primaryRepresentation(
366-
Index.Loc, Field, Entry.first->getValueInit(Field.Name));
367+
Index.Loc, Field, EntryRec->getValueInit(Field.Name));
367368
if (isa<StringRecTy>(Field.RecType))
368369
Repr = StringRef(Repr).upper();
369370
OS << LS << Repr;
370371
}
371-
OS << ", " << Entry.second << " },\n";
372+
OS << ", " << EntryIndex << " },\n";
372373
}
373374

374375
OS << " };\n\n";
@@ -385,8 +386,8 @@ void SearchableTableEmitter::emitLookupFunction(const GenericTable &Table,
385386
Index.Fields[0].IsInstruction)) {
386387
int64_t FirstKeyVal = getNumericKey(Index, IndexRows[0]);
387388
IsContiguous = true;
388-
for (unsigned i = 0; i < IndexRows.size(); ++i) {
389-
if (getNumericKey(Index, IndexRows[i]) != (FirstKeyVal + i)) {
389+
for (const auto &[Idx, IndexRow] : enumerate(IndexRows)) {
390+
if (getNumericKey(Index, IndexRow) != FirstKeyVal + (int64_t)Idx) {
390391
IsContiguous = false;
391392
break;
392393
}
@@ -496,9 +497,9 @@ void SearchableTableEmitter::emitLookupFunction(const GenericTable &Table,
496497
OS << " ||\n Key." << Field.Name << " != Idx->" << Field.Name;
497498
}
498499

499-
if (ShouldReturnRange)
500+
if (ShouldReturnRange) {
500501
OS << " return llvm::make_range(It.first, It.second);\n";
501-
else if (IsPrimary) {
502+
} else if (IsPrimary) {
502503
OS << ")\n return nullptr;\n\n";
503504
OS << " return &*Idx;\n";
504505
} else {
@@ -544,8 +545,7 @@ void SearchableTableEmitter::emitGenericTable(const GenericTable &Table,
544545

545546
// The primary data table contains all the fields defined for this map.
546547
OS << "constexpr " << Table.CppTypeName << " " << Table.Name << "[] = {\n";
547-
for (unsigned i = 0; i < Table.Entries.size(); ++i) {
548-
const Record *Entry = Table.Entries[i];
548+
for (const auto &[Idx, Entry] : enumerate(Table.Entries)) {
549549
OS << " { ";
550550

551551
ListSeparator LS;
@@ -554,7 +554,7 @@ void SearchableTableEmitter::emitGenericTable(const GenericTable &Table,
554554
<< primaryRepresentation(Table.Locs[0], Field,
555555
Entry->getValueInit(Field.Name));
556556

557-
OS << " }, // " << i << "\n";
557+
OS << " }, // " << Idx << "\n";
558558
}
559559
OS << " };\n";
560560

0 commit comments

Comments
 (0)