Skip to content

Commit e72c716

Browse files
[AccelTable][nfc] Add helper function to cast AccelTableData (#77100)
Specializations of AccelTableBase are always interested in accessing the derived versions of their data classes (e.g. DWARF5AccelTableData). They do so by sprinkling `static_casts` all over the code. This commit adds a helper function to simplify this process, reducinng the number of casts that have to be made in the middle of code, making it easier to read.
1 parent eea627e commit e72c716

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

llvm/include/llvm/CodeGen/AccelTable.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ class AccelTableBase {
143143
std::vector<AccelTableData *> Values;
144144
MCSymbol *Sym;
145145

146+
/// Get all AccelTableData cast as a `T`.
147+
template <typename T = AccelTableData *> auto getValues() const {
148+
static_assert(std::is_pointer<T>());
149+
static_assert(
150+
std::is_base_of<AccelTableData, std::remove_pointer_t<T>>());
151+
return map_range(
152+
Values, [](AccelTableData *Data) { return static_cast<T>(Data); });
153+
}
154+
146155
#ifndef NDEBUG
147156
void print(raw_ostream &OS) const;
148157
void dump() const { print(dbgs()); }
@@ -319,8 +328,7 @@ class DWARF5AccelTable : public AccelTable<DWARF5AccelTableData> {
319328
/// Needs to be called after DIE offsets are computed.
320329
void convertDieToOffset() {
321330
for (auto &Entry : Entries) {
322-
for (AccelTableData *Value : Entry.second.Values) {
323-
DWARF5AccelTableData *Data = static_cast<DWARF5AccelTableData *>(Value);
331+
for (auto *Data : Entry.second.getValues<DWARF5AccelTableData *>()) {
324332
// For TU we normalize as each Unit is emitted.
325333
// So when this is invoked after CU construction we will be in mixed
326334
// state.
@@ -332,8 +340,7 @@ class DWARF5AccelTable : public AccelTable<DWARF5AccelTableData> {
332340

333341
void addTypeEntries(DWARF5AccelTable &Table) {
334342
for (auto &Entry : Table.getEntries()) {
335-
for (AccelTableData *Value : Entry.second.Values) {
336-
DWARF5AccelTableData *Data = static_cast<DWARF5AccelTableData *>(Value);
343+
for (auto *Data : Entry.second.getValues<DWARF5AccelTableData *>()) {
337344
addName(Entry.second.Name, Data->getDieOffset(), Data->getDieTag(),
338345
Data->getUnitID(), true);
339346
}

llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ void AppleAccelTableWriter::emitData() const {
342342
Asm->emitDwarfStringOffset(Hash->Name);
343343
Asm->OutStreamer->AddComment("Num DIEs");
344344
Asm->emitInt32(Hash->Values.size());
345-
for (const auto *V : Hash->Values)
346-
static_cast<const AppleAccelTableData *>(V)->emit(Asm);
345+
for (const auto *V : Hash->getValues<const AppleAccelTableData *>())
346+
V->emit(Asm);
347347
PrevHash = Hash->HashValue;
348348
}
349349
// Emit the final end marker for the bucket.
@@ -415,11 +415,10 @@ static uint32_t constructAbbreviationTag(
415415
void Dwarf5AccelTableWriter::populateAbbrevsMap() {
416416
for (auto &Bucket : Contents.getBuckets()) {
417417
for (auto *Hash : Bucket) {
418-
for (auto *Value : Hash->Values) {
418+
for (auto *Value : Hash->getValues<DWARF5AccelTableData *>()) {
419419
std::optional<DWARF5AccelTable::UnitIndexAndEncoding> EntryRet =
420-
getIndexForEntry(*static_cast<const DWARF5AccelTableData *>(Value));
421-
unsigned Tag =
422-
static_cast<const DWARF5AccelTableData *>(Value)->getDieTag();
420+
getIndexForEntry(*Value);
421+
unsigned Tag = Value->getDieTag();
423422
uint32_t AbbrvTag = constructAbbreviationTag(Tag, EntryRet);
424423
if (Abbreviations.count(AbbrvTag) == 0) {
425424
SmallVector<DWARF5AccelTableData::AttributeEncoding, 2> UA;

0 commit comments

Comments
 (0)