This is an archive of the discontinued LLVM Phabricator instance.

Add the ability to get attribute values as Optional<T>
ClosedPublic

Authored by clayborg on Dec 14 2016, 1:51 PM.

Details

Summary

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
}

Diff Detail

Event Timeline

clayborg updated this revision to Diff 81460.Dec 14 2016, 1:51 PM
clayborg retitled this revision from to Add the ability to get attribute values as Optional<T>.
clayborg updated this object.
aprantl accepted this revision.Dec 14 2016, 2:20 PM
aprantl edited edge metadata.

Thanks, this is much cleaner than using special ~1ULL return values!
LGTM, and if we can get the Optional-default-value patch in later, this will be even better.

lib/DebugInfo/DWARF/DWARFDie.cpp
255

formatting nit: Comment should go above the statement.

This revision is now accepted and ready to land.Dec 14 2016, 2:20 PM
This revision was automatically updated to reflect the committed changes.