diff options
author | Greg Clayton <gclayton@apple.com> | 2016-12-14 22:38:08 +0000 |
---|---|---|
committer | Greg Clayton <gclayton@apple.com> | 2016-12-14 22:38:08 +0000 |
commit | 52fe1f68c8f405dc5799a640ff275c6afd74ebb1 (patch) | |
tree | 2cd938401d902651f7ab6469db90baab2d9cf81d /llvm/lib | |
parent | e7bbf7fde3f6513ef0311eda82b6f8f37b644fa9 (diff) | |
download | bcm5719-llvm-52fe1f68c8f405dc5799a640ff275c6afd74ebb1.tar.gz bcm5719-llvm-52fe1f68c8f405dc5799a640ff275c6afd74ebb1.zip |
Add the ability to get attribute values as Optional<T>
When getting attributes it is sometimes nicer to use Optional<T> some of the time instead of magic values. I tried to cut over to only using the Optional values but it made many of the call sites very messy, so it makes sense the leave in the calls that can return a default value. Otherwise code that looks like this:
uint64_t CallColumn = Die.getAttributeValueAsAddress(DW_AT_call_line, 0);
Has to be turned into:
uint64_t CallColumn = 0;
if (auto CallColumnValue = Die.getAttributeValueAsAddress(DW_AT_call_line))
CallColumn = *CallColumnValue;
The first snippet of code looks much better. But in cases where you want an offset that may or may not be there, the following code looks better:
if (auto StmtOffset = Die.getAttributeValueAsSectionOffset(DW_AT_stmt_list)) {
// Use StmtOffset
}
Differential Revision: https://reviews.llvm.org/D27772
llvm-svn: 289731
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/DebugInfo/DWARF/DWARFContext.cpp | 15 | ||||
-rw-r--r-- | llvm/lib/DebugInfo/DWARF/DWARFDie.cpp | 126 | ||||
-rw-r--r-- | llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp | 18 |
3 files changed, 94 insertions, 65 deletions
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp index 5982fbbab24..0f2a48bd509 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -159,13 +159,13 @@ void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH, auto CUDIE = CU->getUnitDIE(); if (!CUDIE) continue; - unsigned stmtOffset = CUDIE.getAttributeValueAsSectionOffset( - DW_AT_stmt_list, -1U); - if (stmtOffset != -1U) { + if (auto StmtOffset = + CUDIE.getAttributeValueAsSectionOffset(DW_AT_stmt_list)) { DataExtractor lineData(getLineSection().Data, isLittleEndian(), savedAddressByteSize); DWARFDebugLine::LineTable LineTable; - LineTable.parse(lineData, &getLineSection().Relocs, &stmtOffset); + uint32_t Offset = *StmtOffset; + LineTable.parse(lineData, &getLineSection().Relocs, &Offset); LineTable.dump(OS); } } @@ -416,12 +416,11 @@ DWARFContext::getLineTableForUnit(DWARFUnit *U) { if (!UnitDIE) return nullptr; - unsigned stmtOffset = - UnitDIE.getAttributeValueAsSectionOffset(DW_AT_stmt_list, -1U); - if (stmtOffset == -1U) + auto Offset = UnitDIE.getAttributeValueAsSectionOffset(DW_AT_stmt_list); + if (!Offset) return nullptr; // No line table for this compile unit. - stmtOffset += U->getLineTableOffset(); + uint32_t stmtOffset = *Offset + U->getLineTableOffset(); // See if the line table is cached. if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset)) return lt; diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp index f52cc112f7f..981f33c1f4f 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp @@ -154,82 +154,112 @@ const char *DWARFDie::getAttributeValueAsString(dwarf::Attribute Attr, uint64_t DWARFDie::getAttributeValueAsAddress(dwarf::Attribute Attr, uint64_t FailValue) const { - auto FormValue = getAttributeValue(Attr); - if (!FormValue) - return FailValue; - Optional<uint64_t> Result = FormValue->getAsAddress(); - return Result.hasValue() ? Result.getValue() : FailValue; + if (auto Value = getAttributeValueAsAddress(Attr)) + return *Value; + return FailValue; +} + +Optional<uint64_t> +DWARFDie::getAttributeValueAsAddress(dwarf::Attribute Attr) const { + if (auto FormValue = getAttributeValue(Attr)) + return FormValue->getAsAddress(); + return None; } int64_t DWARFDie::getAttributeValueAsSignedConstant(dwarf::Attribute Attr, int64_t FailValue) const { - auto FormValue = getAttributeValue(Attr); - if (!FormValue) - return FailValue; - Optional<int64_t> Result = FormValue->getAsSignedConstant(); - return Result.hasValue() ? Result.getValue() : FailValue; + if (auto Value = getAttributeValueAsSignedConstant(Attr)) + return *Value; + return FailValue; +} + +Optional<int64_t> +DWARFDie::getAttributeValueAsSignedConstant(dwarf::Attribute Attr) const { + if (auto FormValue = getAttributeValue(Attr)) + return FormValue->getAsSignedConstant(); + return None; } uint64_t DWARFDie::getAttributeValueAsUnsignedConstant(dwarf::Attribute Attr, uint64_t FailValue) const { - auto FormValue = getAttributeValue(Attr); - if (!FormValue) - return FailValue; - Optional<uint64_t> Result = FormValue->getAsUnsignedConstant(); - return Result.hasValue() ? Result.getValue() : FailValue; + if (auto Value = getAttributeValueAsUnsignedConstant(Attr)) + return *Value; + return FailValue; +} + + +Optional<uint64_t> +DWARFDie::getAttributeValueAsUnsignedConstant(dwarf::Attribute Attr) const { + if (auto FormValue = getAttributeValue(Attr)) + return FormValue->getAsUnsignedConstant(); + return None; } uint64_t DWARFDie::getAttributeValueAsReference(dwarf::Attribute Attr, uint64_t FailValue) const { - auto FormValue = getAttributeValue(Attr); - if (!FormValue) - return FailValue; - Optional<uint64_t> Result = FormValue->getAsReference(); - return Result.hasValue() ? Result.getValue() : FailValue; + if (auto Value = getAttributeValueAsReference(Attr)) + return *Value; + return FailValue; +} + + +Optional<uint64_t> +DWARFDie::getAttributeValueAsReference(dwarf::Attribute Attr) const { + if (auto FormValue = getAttributeValue(Attr)) + return FormValue->getAsReference(); + return None; } uint64_t DWARFDie::getAttributeValueAsSectionOffset(dwarf::Attribute Attr, uint64_t FailValue) const { - auto FormValue = getAttributeValue(Attr); - if (!FormValue) - return FailValue; - Optional<uint64_t> Result = FormValue->getAsSectionOffset(); - return Result.hasValue() ? Result.getValue() : FailValue; + if (auto Value = getAttributeValueAsSectionOffset(Attr)) + return *Value; + return FailValue; } +Optional<uint64_t> +DWARFDie::getAttributeValueAsSectionOffset(dwarf::Attribute Attr) const { + if (auto FormValue = getAttributeValue(Attr)) + return FormValue->getAsSectionOffset(); + return None; +} + + DWARFDie DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const { - uint32_t SpecRef = getAttributeValueAsReference(Attr, -1U); - if (SpecRef != -1U) { - auto SpecUnit = U->getUnitSection().getUnitForOffset(SpecRef); + auto SpecRef = getAttributeValueAsReference(Attr); + if (SpecRef) { + auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef); if (SpecUnit) - return SpecUnit->getDIEForOffset(SpecRef); + return SpecUnit->getDIEForOffset(*SpecRef); } return DWARFDie(); } -uint64_t -DWARFDie::getRangesBaseAttribute(uint64_t FailValue) const { - auto Result = getAttributeValueAsSectionOffset(DW_AT_rnglists_base, -1ULL); - if (Result != -1ULL) +Optional<uint64_t> +DWARFDie::getRangesBaseAttribute() const { + auto Result = getAttributeValueAsSectionOffset(DW_AT_rnglists_base); + if (Result) return Result; - return getAttributeValueAsSectionOffset(DW_AT_GNU_ranges_base, FailValue); + return getAttributeValueAsSectionOffset(DW_AT_GNU_ranges_base); } bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC) const { - LowPC = getAttributeValueAsAddress(DW_AT_low_pc, -1ULL); - if (LowPC == -1ULL) + if (auto LowPCVal = getAttributeValueAsAddress(DW_AT_low_pc)) + LowPC = *LowPCVal; + else return false; - HighPC = getAttributeValueAsAddress(DW_AT_high_pc, -1ULL); - if (HighPC == -1ULL) { - // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case - // it represents function size. - HighPC = getAttributeValueAsUnsignedConstant(DW_AT_high_pc, -1ULL); - if (HighPC != -1ULL) - HighPC += LowPC; - } - return (HighPC != -1ULL); + + if (auto HighPCVal = getAttributeValueAsAddress(DW_AT_high_pc)) { + // High PC is an address. + HighPC = *HighPCVal; + } else if (auto Offset = getAttributeValueAsUnsignedConstant(DW_AT_high_pc)) { + // High PC is an offset from LowPC. + HighPC = LowPC + *Offset; + } else + return false; + return true; } DWARFAddressRangesVector @@ -242,10 +272,10 @@ DWARFDie::getAddressRanges() const { return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC)); } // Multiple ranges from .debug_ranges section. - uint32_t RangesOffset = getAttributeValueAsSectionOffset(DW_AT_ranges, -1U); - if (RangesOffset != -1U) { + auto RangesOffset = getAttributeValueAsSectionOffset(DW_AT_ranges); + if (RangesOffset) { DWARFDebugRangeList RangeList; - if (U->extractRangeList(RangesOffset, RangeList)) + if (U->extractRangeList(*RangesOffset, RangeList)) return RangeList.getAbsoluteRanges(U->getBaseAddress()); } return DWARFAddressRangesVector(); diff --git a/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp b/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp index 2424ea44ca8..2ddbc507ff6 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp @@ -154,9 +154,8 @@ const char *DWARFUnit::getCompilationDir() { return getUnitDIE().getAttributeValueAsString(DW_AT_comp_dir, nullptr); } -uint64_t DWARFUnit::getDWOId() { - return getUnitDIE().getAttributeValueAsUnsignedConstant(DW_AT_GNU_dwo_id, - -1ULL); +Optional<uint64_t> DWARFUnit::getDWOId() { + return getUnitDIE().getAttributeValueAsUnsignedConstant(DW_AT_GNU_dwo_id); } void DWARFUnit::setDIERelations() { @@ -254,10 +253,11 @@ size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) { // If CU DIE was just parsed, copy several attribute values from it. if (!HasCUDie) { DWARFDie UnitDie = getUnitDIE(); - uint64_t BaseAddr = UnitDie.getAttributeValueAsAddress(DW_AT_low_pc, -1ULL); - if (BaseAddr == -1ULL) - BaseAddr = UnitDie.getAttributeValueAsAddress(DW_AT_entry_pc, 0); - setBaseAddress(BaseAddr); + auto BaseAddr = UnitDie.getAttributeValueAsAddress(DW_AT_low_pc); + if (!BaseAddr) + BaseAddr = UnitDie.getAttributeValueAsAddress(DW_AT_entry_pc); + if (BaseAddr) + setBaseAddress(*BaseAddr); AddrOffsetSectionBase = UnitDie.getAttributeValueAsSectionOffset( DW_AT_GNU_addr_base, 0); RangeSectionBase = UnitDie.getAttributeValueAsSectionOffset( @@ -313,8 +313,8 @@ bool DWARFUnit::parseDWO() { } // Share .debug_addr and .debug_ranges section with compile unit in .dwo DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase); - uint32_t DWORangesBase = UnitDie.getRangesBaseAttribute(0); - DWOCU->setRangesSection(RangeSection, DWORangesBase); + auto DWORangesBase = UnitDie.getRangesBaseAttribute(); + DWOCU->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0); return true; } |