DWARFAbbreviationDeclaration.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
  10. #include "llvm/Support/Dwarf.h"
  11. #include "llvm/Support/Format.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. using namespace llvm;
  14. using namespace dwarf;
  15. void DWARFAbbreviationDeclaration::clear() {
  16. Code = 0;
  17. Tag = 0;
  18. HasChildren = false;
  19. AttributeSpecs.clear();
  20. }
  21. DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration() {
  22. clear();
  23. }
  24. bool
  25. DWARFAbbreviationDeclaration::extract(DataExtractor Data, uint32_t* OffsetPtr) {
  26. clear();
  27. Code = Data.getULEB128(OffsetPtr);
  28. if (Code == 0) {
  29. return false;
  30. }
  31. Tag = Data.getULEB128(OffsetPtr);
  32. uint8_t ChildrenByte = Data.getU8(OffsetPtr);
  33. HasChildren = (ChildrenByte == DW_CHILDREN_yes);
  34. while (true) {
  35. uint32_t CurOffset = *OffsetPtr;
  36. uint16_t Attr = Data.getULEB128(OffsetPtr);
  37. if (CurOffset == *OffsetPtr) {
  38. clear();
  39. return false;
  40. }
  41. CurOffset = *OffsetPtr;
  42. uint16_t Form = Data.getULEB128(OffsetPtr);
  43. if (CurOffset == *OffsetPtr) {
  44. clear();
  45. return false;
  46. }
  47. if (Attr == 0 && Form == 0)
  48. break;
  49. AttributeSpecs.push_back(AttributeSpec(Attr, Form));
  50. }
  51. if (Tag == 0) {
  52. clear();
  53. return false;
  54. }
  55. return true;
  56. }
  57. void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const {
  58. const char *tagString = TagString(getTag());
  59. OS << '[' << getCode() << "] ";
  60. if (tagString)
  61. OS << tagString;
  62. else
  63. OS << format("DW_TAG_Unknown_%x", getTag());
  64. OS << "\tDW_CHILDREN_" << (hasChildren() ? "yes" : "no") << '\n';
  65. for (const AttributeSpec &Spec : AttributeSpecs) {
  66. OS << '\t';
  67. const char *attrString = AttributeString(Spec.Attr);
  68. if (attrString)
  69. OS << attrString;
  70. else
  71. OS << format("DW_AT_Unknown_%x", Spec.Attr);
  72. OS << '\t';
  73. const char *formString = FormEncodingString(Spec.Form);
  74. if (formString)
  75. OS << formString;
  76. else
  77. OS << format("DW_FORM_Unknown_%x", Spec.Form);
  78. OS << '\n';
  79. }
  80. OS << '\n';
  81. }
  82. uint32_t
  83. DWARFAbbreviationDeclaration::findAttributeIndex(uint16_t attr) const {
  84. for (uint32_t i = 0, e = AttributeSpecs.size(); i != e; ++i) {
  85. if (AttributeSpecs[i].Attr == attr)
  86. return i;
  87. }
  88. return -1U;
  89. }