1 //===-- DWARFDebugAbbrev.cpp ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DWARFDebugAbbrev.h"
11 #include "llvm/Support/Format.h"
12 #include "llvm/Support/raw_ostream.h"
15 DWARFAbbreviationDeclarationSet::DWARFAbbreviationDeclarationSet() {
19 void DWARFAbbreviationDeclarationSet::clear() {
25 bool DWARFAbbreviationDeclarationSet::extract(DataExtractor Data,
26 uint32_t *OffsetPtr) {
28 const uint32_t BeginOffset = *OffsetPtr;
30 DWARFAbbreviationDeclaration AbbrDecl;
31 uint32_t PrevAbbrCode = 0;
32 while (AbbrDecl.extract(Data, OffsetPtr)) {
33 Decls.push_back(AbbrDecl);
34 if (FirstAbbrCode == 0) {
35 FirstAbbrCode = AbbrDecl.getCode();
37 if (PrevAbbrCode + 1 != AbbrDecl.getCode()) {
38 // Codes are not consecutive, can't do O(1) lookups.
39 FirstAbbrCode = UINT32_MAX;
42 PrevAbbrCode = AbbrDecl.getCode();
44 return BeginOffset != *OffsetPtr;
47 void DWARFAbbreviationDeclarationSet::dump(raw_ostream &OS) const {
48 for (const auto &Decl : Decls)
52 const DWARFAbbreviationDeclaration *
53 DWARFAbbreviationDeclarationSet::getAbbreviationDeclaration(
54 uint32_t AbbrCode) const {
55 if (FirstAbbrCode == UINT32_MAX) {
56 for (const auto &Decl : Decls) {
57 if (Decl.getCode() == AbbrCode)
62 if (AbbrCode < FirstAbbrCode || AbbrCode >= FirstAbbrCode + Decls.size())
64 return &Decls[AbbrCode - FirstAbbrCode];
67 DWARFDebugAbbrev::DWARFDebugAbbrev() {
71 void DWARFDebugAbbrev::clear() {
73 PrevAbbrOffsetPos = AbbrDeclSets.end();
76 void DWARFDebugAbbrev::extract(DataExtractor Data) {
80 DWARFAbbreviationDeclarationSet AbbrDecls;
81 while (Data.isValidOffset(Offset)) {
82 uint32_t CUAbbrOffset = Offset;
83 if (!AbbrDecls.extract(Data, &Offset))
85 AbbrDeclSets[CUAbbrOffset] = AbbrDecls;
89 void DWARFDebugAbbrev::dump(raw_ostream &OS) const {
90 if (AbbrDeclSets.empty()) {
95 for (const auto &I : AbbrDeclSets) {
96 OS << format("Abbrev table for offset: 0x%8.8" PRIx64 "\n", I.first);
101 const DWARFAbbreviationDeclarationSet*
102 DWARFDebugAbbrev::getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const {
103 const auto End = AbbrDeclSets.end();
104 if (PrevAbbrOffsetPos != End && PrevAbbrOffsetPos->first == CUAbbrOffset) {
105 return &(PrevAbbrOffsetPos->second);
108 const auto Pos = AbbrDeclSets.find(CUAbbrOffset);
110 PrevAbbrOffsetPos = Pos;
111 return &(Pos->second);