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
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp b/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp
index 2424ea4..2ddbc50 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp
@@ -154,9 +154,8 @@
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 @@
// 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 @@
}
// 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;
}