aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSid Nayyar <sidnayyar@google.com>2024-03-08 13:54:00 +0000
committerGiuliano Procida <gprocida@google.com>2024-03-08 14:24:09 +0000
commitc8070fe63aa2eac4387057b3e3417c927e0a8d77 (patch)
tree5f306e6d79f765519734b2fcd9be94d0ea887609
parent24083aea98c508ee534a709f2a26c8e99a614def (diff)
downloadstg-c8070fe63aa2eac4387057b3e3417c927e0a8d77.tar.gz
DWARF wrappers: add DWARF getter for mandatory attributes
It is a common pattern in DWARF processor to get an optional value and check for its presence. This helper checks for the presence and directly returns the underlying value, eliminating the need to check for presence check by the user. PiperOrigin-RevId: 613907941 Change-Id: Idddd9694f665935fc2b12fce161d85ab3250e324
-rw-r--r--dwarf_wrappers.cc9
-rw-r--r--dwarf_wrappers.h1
2 files changed, 10 insertions, 0 deletions
diff --git a/dwarf_wrappers.cc b/dwarf_wrappers.cc
index d13d7b8..04e5897 100644
--- a/dwarf_wrappers.cc
+++ b/dwarf_wrappers.cc
@@ -274,6 +274,15 @@ std::optional<uint64_t> Entry::MaybeGetUnsignedConstant(uint32_t attribute) {
return value;
}
+uint64_t Entry::MustGetUnsignedConstant(uint32_t attribute) {
+ auto maybe_constant = MaybeGetUnsignedConstant(attribute);
+ if (!maybe_constant) {
+ Die() << "DWARF entry <" << Hex(GetOffset()) << "> with tag " << GetTag()
+ << " is missing attribute " << Hex(attribute);
+ }
+ return maybe_constant.value();
+}
+
bool Entry::GetFlag(uint32_t attribute) {
bool result = false;
auto dwarf_attribute = (attribute == DW_AT_declaration)
diff --git a/dwarf_wrappers.h b/dwarf_wrappers.h
index d0daa5c..c584345 100644
--- a/dwarf_wrappers.h
+++ b/dwarf_wrappers.h
@@ -71,6 +71,7 @@ struct Entry {
std::optional<std::string> MaybeGetString(uint32_t attribute);
std::optional<std::string> MaybeGetDirectString(uint32_t attribute);
std::optional<uint64_t> MaybeGetUnsignedConstant(uint32_t attribute);
+ uint64_t MustGetUnsignedConstant(uint32_t attribute);
bool GetFlag(uint32_t attribute);
std::optional<Entry> MaybeGetReference(uint32_t attribute);
std::optional<Address> MaybeGetAddress(uint32_t attribute);