Skip to content

Commit

Permalink
[BOLT][NFC] Use std::optional for findAttributeInfo
Browse files Browse the repository at this point in the history
LLVM started switching from `llvm::Optional` to `std::optional`:
https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716/11

Reviewed By: #bolt, maksfb

Differential Revision: https://reviews.llvm.org/D139259
  • Loading branch information
aaupov committed Dec 6, 2022
1 parent 59ae452 commit 370e476
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
5 changes: 3 additions & 2 deletions bolt/include/bolt/Core/DebugData.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct AttrInfo {
/// \param DIE die to look up in.
/// \param AbbrevDecl abbrev declaration for the die.
/// \param Index an index in Abbrev declaration entry.
Optional<AttrInfo>
std::optional<AttrInfo>
findAttributeInfo(const DWARFDie DIE,
const DWARFAbbreviationDeclaration *AbbrevDecl,
uint32_t Index);
Expand All @@ -56,7 +56,8 @@ findAttributeInfo(const DWARFDie DIE,
/// \param DIE die to look up in.
/// \param Attr the attribute to extract.
/// \return an optional AttrInfo with DWARFFormValue and Offset.
Optional<AttrInfo> findAttributeInfo(const DWARFDie DIE, dwarf::Attribute Attr);
std::optional<AttrInfo> findAttributeInfo(const DWARFDie DIE,
dwarf::Attribute Attr);

// DWARF5 Header in order of encoding.
// Types represent encodnig sizes.
Expand Down
10 changes: 5 additions & 5 deletions bolt/lib/Core/DebugData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MCSymbol;

namespace bolt {

Optional<AttrInfo>
std::optional<AttrInfo>
findAttributeInfo(const DWARFDie DIE,
const DWARFAbbreviationDeclaration *AbbrevDecl,
uint32_t Index) {
Expand Down Expand Up @@ -73,8 +73,8 @@ findAttributeInfo(const DWARFDie DIE,
return AttrInfo{*Value, DIE.getAbbreviationDeclarationPtr(), Offset, ValSize};
}

Optional<AttrInfo> findAttributeInfo(const DWARFDie DIE,
dwarf::Attribute Attr) {
std::optional<AttrInfo> findAttributeInfo(const DWARFDie DIE,
dwarf::Attribute Attr) {
if (!DIE.isValid())
return std::nullopt;
const DWARFAbbreviationDeclaration *AbbrevDecl =
Expand Down Expand Up @@ -666,7 +666,7 @@ void DebugLoclistWriter::finalizeDWARF5(
*LocStream << *LocBodyBuffer;

if (!isSplitDwarf()) {
if (Optional<AttrInfo> AttrInfoVal =
if (std::optional<AttrInfo> AttrInfoVal =
findAttributeInfo(CU.getUnitDIE(), dwarf::DW_AT_loclists_base))
DebugInfoPatcher.addLE32Patch(AttrInfoVal->Offset,
LoclistBaseOffset +
Expand Down Expand Up @@ -733,7 +733,7 @@ void DebugInfoBinaryPatcher::insertNewEntry(const DWARFDie &DIE,
uint32_t Offset = DIE.getOffset() + 1;
size_t NumOfAttributes = AbbrevDecl->getNumAttributes();
if (NumOfAttributes) {
Optional<AttrInfo> Val =
std::optional<AttrInfo> Val =
findAttributeInfo(DIE, AbbrevDecl, NumOfAttributes - 1);
assert(Val && "Invalid Value.");

Expand Down
45 changes: 23 additions & 22 deletions bolt/lib/Rewrite/DWARFRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ namespace bolt {
/// \param DIE die to look up in.
/// \param Attrs finds the first attribute that matches and extracts it.
/// \return an optional AttrInfo with DWARFFormValue and Offset.
Optional<AttrInfo> findAttributeInfo(const DWARFDie DIE,
std::vector<dwarf::Attribute> Attrs) {
std::optional<AttrInfo> findAttributeInfo(const DWARFDie DIE,
std::vector<dwarf::Attribute> Attrs) {
for (dwarf::Attribute &Attr : Attrs)
if (Optional<AttrInfo> Info = findAttributeInfo(DIE, Attr))
if (std::optional<AttrInfo> Info = findAttributeInfo(DIE, Attr))
return Info;
return std::nullopt;
}
Expand Down Expand Up @@ -230,7 +230,7 @@ void DWARFRewriter::updateDebugInfo() {

auto updateDWONameCompDir = [&](DWARFUnit &Unit) -> void {
const DWARFDie &DIE = Unit.getUnitDIE();
Optional<AttrInfo> AttrInfoVal = findAttributeInfo(
std::optional<AttrInfo> AttrInfoVal = findAttributeInfo(
DIE, {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name});
(void)AttrInfoVal;
assert(AttrInfoVal && "Skeleton CU doesn't have dwo_name.");
Expand Down Expand Up @@ -514,11 +514,11 @@ void DWARFRewriter::updateUnitDebugInfo(
}
};

if (Optional<AttrInfo> AttrVal =
if (std::optional<AttrInfo> AttrVal =
findAttributeInfo(DIE, dwarf::DW_AT_call_pc))
patchPC(*AttrVal, "DW_AT_call_pc");

if (Optional<AttrInfo> AttrVal =
if (std::optional<AttrInfo> AttrVal =
findAttributeInfo(DIE, dwarf::DW_AT_call_return_pc))
patchPC(*AttrVal, "DW_AT_call_return_pc");

Expand All @@ -528,7 +528,7 @@ void DWARFRewriter::updateUnitDebugInfo(
// Handle any tag that can have DW_AT_location attribute.
DWARFFormValue Value;
uint64_t AttrOffset;
if (Optional<AttrInfo> AttrVal =
if (std::optional<AttrInfo> AttrVal =
findAttributeInfo(DIE, dwarf::DW_AT_location)) {
AttrOffset = AttrVal->Offset;
Value = AttrVal->V;
Expand Down Expand Up @@ -690,7 +690,7 @@ void DWARFRewriter::updateUnitDebugInfo(
SizeDiff + CurrEndOffset, 1);
}
}
} else if (Optional<AttrInfo> AttrVal =
} else if (std::optional<AttrInfo> AttrVal =
findAttributeInfo(DIE, dwarf::DW_AT_low_pc)) {
AttrOffset = AttrVal->Offset;
Value = AttrVal->V;
Expand Down Expand Up @@ -736,7 +736,7 @@ void DWARFRewriter::updateUnitDebugInfo(
}
} else if (IsDWP && Unit.isDWOUnit()) {
// Not a common path so don't want to search all DIEs all the time.
Optional<AttrInfo> SignatureAttrVal =
std::optional<AttrInfo> SignatureAttrVal =
findAttributeInfo(DIE, dwarf::DW_AT_signature);
if (!SignatureAttrVal)
continue;
Expand Down Expand Up @@ -771,7 +771,8 @@ void DWARFRewriter::updateUnitDebugInfo(
case dwarf::DW_FORM_ref8:
case dwarf::DW_FORM_ref_udata:
case dwarf::DW_FORM_ref_addr: {
Optional<AttrInfo> AttrVal = findAttributeInfo(DIE, AbbrevDecl, Index);
std::optional<AttrInfo> AttrVal =
findAttributeInfo(DIE, AbbrevDecl, Index);
uint32_t DestinationAddress =
AttrVal->V.getRawUValue() +
(Decl.Form == dwarf::DW_FORM_ref_addr ? 0 : Unit.getOffset());
Expand Down Expand Up @@ -813,7 +814,7 @@ void DWARFRewriter::updateDWARFObjectAddressRanges(
if (RangesBase) {
// If DW_AT_GNU_ranges_base is present, update it. No further modifications
// are needed for ranges base.
Optional<AttrInfo> RangesBaseAttrInfo =
std::optional<AttrInfo> RangesBaseAttrInfo =
findAttributeInfo(DIE, dwarf::DW_AT_GNU_ranges_base);
if (!RangesBaseAttrInfo)
RangesBaseAttrInfo = findAttributeInfo(DIE, dwarf::DW_AT_rnglists_base);
Expand All @@ -826,9 +827,9 @@ void DWARFRewriter::updateDWARFObjectAddressRanges(
}
}

Optional<AttrInfo> LowPCAttrInfo =
std::optional<AttrInfo> LowPCAttrInfo =
findAttributeInfo(DIE, dwarf::DW_AT_low_pc);
if (Optional<AttrInfo> AttrVal =
if (std::optional<AttrInfo> AttrVal =
findAttributeInfo(DIE, dwarf::DW_AT_ranges)) {
// Case 1: The object was already non-contiguous and had DW_AT_ranges.
// In this case we simply need to update the value of DW_AT_ranges
Expand Down Expand Up @@ -881,7 +882,7 @@ void DWARFRewriter::updateDWARFObjectAddressRanges(

// Case 2: The object has both DW_AT_low_pc and DW_AT_high_pc emitted back
// to back. Replace with new attributes and patch the DIE.
Optional<AttrInfo> HighPCAttrInfo =
std::optional<AttrInfo> HighPCAttrInfo =
findAttributeInfo(DIE, dwarf::DW_AT_high_pc);
if (LowPCAttrInfo && HighPCAttrInfo) {
convertToRangesPatchAbbrev(*DIE.getDwarfUnit(), AbbreviationDecl,
Expand Down Expand Up @@ -937,7 +938,7 @@ void DWARFRewriter::updateLineTableOffsets(const MCAsmLayout &Layout) {
if (!Label)
continue;

Optional<AttrInfo> AttrVal =
std::optional<AttrInfo> AttrVal =
findAttributeInfo(CU.get()->getUnitDIE(), dwarf::DW_AT_stmt_list);
if (!AttrVal)
continue;
Expand All @@ -951,7 +952,7 @@ void DWARFRewriter::updateLineTableOffsets(const MCAsmLayout &Layout) {

for (const std::unique_ptr<DWARFUnit> &TU : BC.DwCtx->types_section_units()) {
DWARFUnit *Unit = TU.get();
Optional<AttrInfo> AttrVal =
std::optional<AttrInfo> AttrVal =
findAttributeInfo(TU.get()->getUnitDIE(), dwarf::DW_AT_stmt_list);
if (!AttrVal)
continue;
Expand Down Expand Up @@ -1039,9 +1040,9 @@ DWARFRewriter::finalizeDebugSections(DebugInfoBinaryPatcher &DebugInfoPatcher) {
uint64_t Offset = 0;
uint64_t AttrOffset = 0;
uint32_t Size = 0;
Optional<AttrInfo> AttrValGnu =
std::optional<AttrInfo> AttrValGnu =
findAttributeInfo(DIE, dwarf::DW_AT_GNU_addr_base);
Optional<AttrInfo> AttrVal =
std::optional<AttrInfo> AttrVal =
findAttributeInfo(DIE, dwarf::DW_AT_addr_base);

// For cases where Skeleton CU does not have DW_AT_GNU_addr_base
Expand Down Expand Up @@ -1827,8 +1828,8 @@ std::unique_ptr<DebugBufferVector> DWARFRewriter::makeFinalLocListsSection(

namespace {

void getRangeAttrData(DWARFDie DIE, Optional<AttrInfo> &LowPCVal,
Optional<AttrInfo> &HighPCVal) {
void getRangeAttrData(DWARFDie DIE, std::optional<AttrInfo> &LowPCVal,
std::optional<AttrInfo> &HighPCVal) {
LowPCVal = findAttributeInfo(DIE, dwarf::DW_AT_low_pc);
HighPCVal = findAttributeInfo(DIE, dwarf::DW_AT_high_pc);
uint64_t LowPCOffset = LowPCVal->Offset;
Expand Down Expand Up @@ -1894,8 +1895,8 @@ void DWARFRewriter::convertToRangesPatchDebugInfo(
DWARFDie DIE, uint64_t RangesSectionOffset,
SimpleBinaryPatcher &DebugInfoPatcher, uint64_t LowPCToUse,
Optional<uint64_t> RangesBase) {
Optional<AttrInfo> LowPCVal;
Optional<AttrInfo> HighPCVal;
std::optional<AttrInfo> LowPCVal;
std::optional<AttrInfo> HighPCVal;
getRangeAttrData(DIE, LowPCVal, HighPCVal);
uint64_t LowPCOffset = LowPCVal->Offset;
uint64_t HighPCOffset = HighPCVal->Offset;
Expand Down

0 comments on commit 370e476

Please sign in to comment.