LLVM API Documentation

DWARFAbbreviationDeclaration.cpp
Go to the documentation of this file.
00001 //===-- DWARFAbbreviationDeclaration.cpp ----------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "DWARFAbbreviationDeclaration.h"
00011 #include "llvm/Support/Dwarf.h"
00012 #include "llvm/Support/Format.h"
00013 #include "llvm/Support/raw_ostream.h"
00014 using namespace llvm;
00015 using namespace dwarf;
00016 
00017 void DWARFAbbreviationDeclaration::clear() {
00018   Code = 0;
00019   Tag = 0;
00020   HasChildren = false;
00021   AttributeSpecs.clear();
00022 }
00023 
00024 DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration() {
00025   clear();
00026 }
00027 
00028 bool
00029 DWARFAbbreviationDeclaration::extract(DataExtractor Data, uint32_t* OffsetPtr) {
00030   clear();
00031   Code = Data.getULEB128(OffsetPtr);
00032   if (Code == 0) {
00033     return false;
00034   }
00035   Tag = Data.getULEB128(OffsetPtr);
00036   uint8_t ChildrenByte = Data.getU8(OffsetPtr);
00037   HasChildren = (ChildrenByte == DW_CHILDREN_yes);
00038 
00039   while (true) {
00040     uint32_t CurOffset = *OffsetPtr;
00041     uint16_t Attr = Data.getULEB128(OffsetPtr);
00042     if (CurOffset == *OffsetPtr) {
00043       clear();
00044       return false;
00045     }
00046     CurOffset = *OffsetPtr;
00047     uint16_t Form = Data.getULEB128(OffsetPtr);
00048     if (CurOffset == *OffsetPtr) {
00049       clear();
00050       return false;
00051     }
00052     if (Attr == 0 && Form == 0)
00053       break;
00054     AttributeSpecs.push_back(AttributeSpec(Attr, Form));
00055   }
00056 
00057   if (Tag == 0) {
00058     clear();
00059     return false;
00060   }
00061   return true;
00062 }
00063 
00064 void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const {
00065   const char *tagString = TagString(getTag());
00066   OS << '[' << getCode() << "] ";
00067   if (tagString)
00068     OS << tagString;
00069   else
00070     OS << format("DW_TAG_Unknown_%x", getTag());
00071   OS << "\tDW_CHILDREN_" << (hasChildren() ? "yes" : "no") << '\n';
00072   for (const AttributeSpec &Spec : AttributeSpecs) {
00073     OS << '\t';
00074     const char *attrString = AttributeString(Spec.Attr);
00075     if (attrString)
00076       OS << attrString;
00077     else
00078       OS << format("DW_AT_Unknown_%x", Spec.Attr);
00079     OS << '\t';
00080     const char *formString = FormEncodingString(Spec.Form);
00081     if (formString)
00082       OS << formString;
00083     else
00084       OS << format("DW_FORM_Unknown_%x", Spec.Form);
00085     OS << '\n';
00086   }
00087   OS << '\n';
00088 }
00089 
00090 uint32_t
00091 DWARFAbbreviationDeclaration::findAttributeIndex(uint16_t attr) const {
00092   for (uint32_t i = 0, e = AttributeSpecs.size(); i != e; ++i) {
00093     if (AttributeSpecs[i].Attr == attr)
00094       return i;
00095   }
00096   return -1U;
00097 }