Home | History | Annotate | Download | only in DebugInfo
      1 //===-- DWARFAbbreviationDeclaration.cpp ----------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "DWARFAbbreviationDeclaration.h"
     11 #include "llvm/Support/Dwarf.h"
     12 #include "llvm/Support/Format.h"
     13 #include "llvm/Support/raw_ostream.h"
     14 using namespace llvm;
     15 using namespace dwarf;
     16 
     17 bool
     18 DWARFAbbreviationDeclaration::extract(DataExtractor data, uint32_t* offset_ptr){
     19   return extract(data, offset_ptr, data.getULEB128(offset_ptr));
     20 }
     21 
     22 bool
     23 DWARFAbbreviationDeclaration::extract(DataExtractor data, uint32_t* offset_ptr,
     24                                       uint32_t code) {
     25   Code = code;
     26   Attributes.clear();
     27   if (Code) {
     28     Tag = data.getULEB128(offset_ptr);
     29     HasChildren = data.getU8(offset_ptr);
     30 
     31     while (data.isValidOffset(*offset_ptr)) {
     32       uint16_t attr = data.getULEB128(offset_ptr);
     33       uint16_t form = data.getULEB128(offset_ptr);
     34 
     35       if (attr && form)
     36         Attributes.push_back(DWARFAttribute(attr, form));
     37       else
     38         break;
     39     }
     40 
     41     return Tag != 0;
     42   } else {
     43     Tag = 0;
     44     HasChildren = false;
     45   }
     46 
     47   return false;
     48 }
     49 
     50 void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const {
     51   const char *tagString = TagString(getTag());
     52   OS << '[' << getCode() << "] ";
     53   if (tagString)
     54     OS << tagString;
     55   else
     56     OS << format("DW_TAG_Unknown_%x", getTag());
     57   OS << "\tDW_CHILDREN_" << (hasChildren() ? "yes" : "no") << '\n';
     58   for (unsigned i = 0, e = Attributes.size(); i != e; ++i) {
     59     OS << '\t';
     60     const char *attrString = AttributeString(Attributes[i].getAttribute());
     61     if (attrString)
     62       OS << attrString;
     63     else
     64       OS << format("DW_AT_Unknown_%x", Attributes[i].getAttribute());
     65     OS << '\t';
     66     const char *formString = FormEncodingString(Attributes[i].getForm());
     67     if (formString)
     68       OS << formString;
     69     else
     70       OS << format("DW_FORM_Unknown_%x", Attributes[i].getForm());
     71     OS << '\n';
     72   }
     73   OS << '\n';
     74 }
     75 
     76 uint32_t
     77 DWARFAbbreviationDeclaration::findAttributeIndex(uint16_t attr) const {
     78   for (uint32_t i = 0, e = Attributes.size(); i != e; ++i) {
     79     if (Attributes[i].getAttribute() == attr)
     80       return i;
     81   }
     82   return -1U;
     83 }
     84