| 1 | //===-- DIERef.cpp --------------------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "DIERef.h" |
| 10 | #include "lldb/Utility/DataEncoder.h" |
| 11 | #include "lldb/Utility/DataExtractor.h" |
| 12 | #include "llvm/Support/Format.h" |
| 13 | #include <optional> |
| 14 | |
| 15 | using namespace lldb; |
| 16 | using namespace lldb_private; |
| 17 | using namespace lldb_private::plugin::dwarf; |
| 18 | |
| 19 | void llvm::format_provider<DIERef>::format(const DIERef &ref, raw_ostream &OS, |
| 20 | StringRef Style) { |
| 21 | if (ref.file_index()) |
| 22 | OS << format_hex_no_prefix(N: *ref.file_index(), Width: 8) << "/"; |
| 23 | OS << (ref.section() == DIERef::DebugInfo ? "INFO": "TYPE"); |
| 24 | OS << "/"<< format_hex_no_prefix(N: ref.die_offset(), Width: 8); |
| 25 | } |
| 26 | |
| 27 | std::optional<DIERef> DIERef::Decode(const DataExtractor &data, |
| 28 | lldb::offset_t *offset_ptr) { |
| 29 | DIERef die_ref(data.GetU64(offset_ptr)); |
| 30 | |
| 31 | // DIE offsets can't be zero and if we fail to decode something from data, |
| 32 | // it will return 0 |
| 33 | if (!die_ref.die_offset()) |
| 34 | return std::nullopt; |
| 35 | |
| 36 | return die_ref; |
| 37 | } |
| 38 | |
| 39 | void DIERef::Encode(DataEncoder &encoder) const { encoder.AppendU64(value: get_id()); } |
| 40 |
