dwarfdump: Added macro support to llvm-dwarfdump tool.
[oota-llvm.git] / lib / DebugInfo / DWARF / DWARFDebugMacro.cpp
diff --git a/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp b/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp
new file mode 100644 (file)
index 0000000..b6555fa
--- /dev/null
@@ -0,0 +1,103 @@
+//===-- DWARFDebugMacro.cpp -----------------------------------------------===//\r
+//\r
+//                     The LLVM Compiler Infrastructure\r
+//\r
+// This file is distributed under the University of Illinois Open Source\r
+// License. See LICENSE.TXT for details.\r
+//\r
+//===----------------------------------------------------------------------===//\r
+\r
+#include "SyntaxHighlighting.h"\r
+#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"\r
+#include "llvm/Support/Compiler.h"\r
+#include "llvm/Support/Dwarf.h"\r
+#include "llvm/Support/Format.h"\r
+#include "llvm/Support/raw_ostream.h"\r
+\r
+using namespace llvm;\r
+using namespace dwarf;\r
+using namespace syntax;\r
+\r
+void DWARFDebugMacro::dump(raw_ostream &OS) const {\r
+  unsigned IndLevel = 0;\r
+  for (const Entry &E : Macros) {\r
+    // There should not be DW_MACINFO_end_file when IndLevel is Zero. However,\r
+    // this check handles the case of corrupted ".debug_macinfo" section.\r
+    if (IndLevel > 0)\r
+      IndLevel -= (E.Type == DW_MACINFO_end_file);\r
+    // Print indentation.\r
+    for (unsigned I = 0; I < IndLevel; I++)\r
+      OS << "  ";\r
+    IndLevel += (E.Type == DW_MACINFO_start_file);\r
+\r
+    WithColor(OS, syntax::Macro).get() << MacinfoString(E.Type);\r
+    switch (E.Type) {\r
+    default:\r
+      // Got a corrupted ".debug_macinfo" section (invalid macinfo type).\r
+      break;\r
+    case DW_MACINFO_define:\r
+    case DW_MACINFO_undef:\r
+      OS << " - lineno: " << E.Line;\r
+      OS << " macro: " << E.MacroStr;\r
+      break;\r
+    case DW_MACINFO_start_file:\r
+      OS << " - lineno: " << E.Line;\r
+      OS << " filenum: " << E.File;\r
+      break;\r
+    case DW_MACINFO_end_file:\r
+      break;\r
+    case DW_MACINFO_vendor_ext:\r
+      OS << " - constant: " << E.ExtConstant;\r
+      OS << " string: " << E.ExtStr;\r
+      break;\r
+    }\r
+    OS << "\n";\r
+  }\r
+}\r
+\r
+void DWARFDebugMacro::parse(DataExtractor data) {\r
+  uint32_t Offset = 0;\r
+  while (data.isValidOffset(Offset)) {\r
+    // A macro list entry consists of:\r
+    Entry E;\r
+    // 1. Macinfo type\r
+    E.Type = data.getULEB128(&Offset);\r
+\r
+    if (E.Type == 0) {\r
+      // Reached end of ".debug_macinfo" section.\r
+      return;\r
+    }\r
+\r
+    switch (E.Type) {\r
+    default:\r
+      // Got a corrupted ".debug_macinfo" section (invalid macinfo type).\r
+      // Push the corrupted entry to the list and halt parsing.\r
+      E.Type = DW_MACINFO_invalid;\r
+      Macros.push_back(E);\r
+      return;\r
+    case DW_MACINFO_define:\r
+    case DW_MACINFO_undef:\r
+      // 2. Source line\r
+      E.Line = data.getULEB128(&Offset);\r
+      // 3. Macro string\r
+      E.MacroStr = data.getCStr(&Offset);\r
+      break;\r
+    case DW_MACINFO_start_file:\r
+      // 2. Source line\r
+      E.Line = data.getULEB128(&Offset);\r
+      // 3. Source file id\r
+      E.File = data.getULEB128(&Offset);\r
+      break;\r
+    case DW_MACINFO_end_file:\r
+      break;\r
+    case DW_MACINFO_vendor_ext:\r
+      // 2. Vendor extension constant\r
+      E.ExtConstant = data.getULEB128(&Offset);\r
+      // 3. Vendor extension string\r
+      E.ExtStr = data.getCStr(&Offset);\r
+      break;\r
+    }\r
+\r
+    Macros.push_back(E);\r
+  }\r
+}\r