From f9cd90837bdced66c933349b402c322f7198ddfe Mon Sep 17 00:00:00 2001 From: Igor Kudrin Date: Wed, 11 Mar 2020 21:54:51 +0700 Subject: [PATCH] [llvm-dwp] Refactor handling of section identifiers. NFCI. There is a number of places in llvm-dwp.cpp where a section identifier is translated into an index of an internal array of section contributions, and another place where the index is converted to an on-disk value. All these places use direct expressions like " - DW_SECT_INFO" or " + DW_SECT_INFO", exploiting the fact that DW_SECT_INFO is the minimum valid value of that kind. The patch adds distinct functions for that translation. The goal is to make the code more readable and to prepare it to support index sections of new versions, where the numeric scheme of section indexes is changed. Differential Revision: https://reviews.llvm.org/D76067 --- llvm/tools/llvm-dwp/llvm-dwp.cpp | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/llvm/tools/llvm-dwp/llvm-dwp.cpp b/llvm/tools/llvm-dwp/llvm-dwp.cpp index 2a17819..cd7048b 100644 --- a/llvm/tools/llvm-dwp/llvm-dwp.cpp +++ b/llvm/tools/llvm-dwp/llvm-dwp.cpp @@ -216,6 +216,19 @@ struct UnitIndexEntry { StringRef DWPName; }; +// Convert a section identifier into the index to use with +// UnitIndexEntry::Contributions. +static unsigned getContributionIndex(DWARFSectionKind Kind) { + assert(Kind >= DW_SECT_INFO); + return Kind - DW_SECT_INFO; +} + +// Convert a UnitIndexEntry::Contributions index to the corresponding on-disk +// value of the section identifier. +static unsigned getOnDiskSectionId(unsigned Index) { + return Index + DW_SECT_INFO; +} + static StringRef getSubsection(StringRef Section, const DWARFUnitIndex::Entry &Entry, DWARFSectionKind Kind) { @@ -241,15 +254,15 @@ static void addAllTypesFromDWP( // Zero out the debug_info contribution Entry.Contributions[0] = {}; for (auto Kind : TUIndex.getColumnKinds()) { - auto &C = Entry.Contributions[Kind - DW_SECT_INFO]; + auto &C = Entry.Contributions[getContributionIndex(Kind)]; C.Offset += I->Offset; C.Length = I->Length; ++I; } - auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO]; + unsigned TypesIndex = getContributionIndex(DW_SECT_TYPES); + auto &C = Entry.Contributions[TypesIndex]; Out.emitBytes(Types.substr( - C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset, - C.Length)); + C.Offset - TUEntry.Contributions[TypesIndex].Offset, C.Length)); C.Offset = TypesOffset; TypesOffset += C.Length; } @@ -268,7 +281,7 @@ static void addAllTypes(MCStreamer &Out, UnitIndexEntry Entry = CUEntry; // Zero out the debug_info contribution Entry.Contributions[0] = {}; - auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO]; + auto &C = Entry.Contributions[getContributionIndex(DW_SECT_TYPES)]; C.Offset = TypesOffset; auto PrevOffset = Offset; // Length of the unit, including the 4 byte length field. @@ -345,7 +358,7 @@ writeIndex(MCStreamer &Out, MCSection *Section, // Write the column headers (which sections will appear in the table) for (size_t i = 0; i != ContributionOffsets.size(); ++i) if (ContributionOffsets[i]) - Out.emitIntValue(i + DW_SECT_INFO, 4); + Out.emitIntValue(getOnDiskSectionId(i), 4); // Write the offsets. writeIndexTable(Out, ContributionOffsets, IndexEntries, @@ -438,7 +451,7 @@ static Error handleSection( return Error::success(); if (DWARFSectionKind Kind = SectionPair->second.second) { - auto Index = Kind - DW_SECT_INFO; + auto Index = getContributionIndex(Kind); if (Kind != DW_SECT_TYPES) { CurEntry.Contributions[Index].Offset = ContributionOffsets[Index]; ContributionOffsets[Index] += @@ -589,7 +602,8 @@ static Error write(MCStreamer &Out, ArrayRef Inputs) { P.first->second.Name = ID.Name; P.first->second.DWOName = ID.DWOName; addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection, - CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]); + CurEntry, + ContributionOffsets[getContributionIndex(DW_SECT_TYPES)]); continue; } @@ -618,7 +632,7 @@ static Error write(MCStreamer &Out, ArrayRef Inputs) { NewEntry.DWOName = ID.DWOName; NewEntry.DWPName = Input; for (auto Kind : CUIndex.getColumnKinds()) { - auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO]; + auto &C = NewEntry.Contributions[getContributionIndex(Kind)]; C.Offset += I->Offset; C.Length = I->Length; ++I; @@ -632,9 +646,9 @@ static Error write(MCStreamer &Out, ArrayRef Inputs) { DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0); if (!TUIndex.parse(TUIndexData)) return make_error("failed to parse tu_index"); - addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection, - CurTypesSection.front(), CurEntry, - ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]); + addAllTypesFromDWP( + Out, TypeIndexEntries, TUIndex, TypesSection, CurTypesSection.front(), + CurEntry, ContributionOffsets[getContributionIndex(DW_SECT_TYPES)]); } } @@ -645,7 +659,7 @@ static Error write(MCStreamer &Out, ArrayRef Inputs) { TypeIndexEntries); // Lie about the type contribution - ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0; + ContributionOffsets[getContributionIndex(DW_SECT_TYPES)] = 0; // Unlie about the info contribution ContributionOffsets[0] = 1; -- 2.7.4