dwarfdump: Added macro support to llvm-dwarfdump tool.
[oota-llvm.git] / lib / DebugInfo / DWARF / DWARFDebugMacro.cpp
1 //===-- DWARFDebugMacro.cpp -----------------------------------------------===//\r
2 //\r
3 //                     The LLVM Compiler Infrastructure\r
4 //\r
5 // This file is distributed under the University of Illinois Open Source\r
6 // License. See LICENSE.TXT for details.\r
7 //\r
8 //===----------------------------------------------------------------------===//\r
9 \r
10 #include "SyntaxHighlighting.h"\r
11 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"\r
12 #include "llvm/Support/Compiler.h"\r
13 #include "llvm/Support/Dwarf.h"\r
14 #include "llvm/Support/Format.h"\r
15 #include "llvm/Support/raw_ostream.h"\r
16 \r
17 using namespace llvm;\r
18 using namespace dwarf;\r
19 using namespace syntax;\r
20 \r
21 void DWARFDebugMacro::dump(raw_ostream &OS) const {\r
22   unsigned IndLevel = 0;\r
23   for (const Entry &E : Macros) {\r
24     // There should not be DW_MACINFO_end_file when IndLevel is Zero. However,\r
25     // this check handles the case of corrupted ".debug_macinfo" section.\r
26     if (IndLevel > 0)\r
27       IndLevel -= (E.Type == DW_MACINFO_end_file);\r
28     // Print indentation.\r
29     for (unsigned I = 0; I < IndLevel; I++)\r
30       OS << "  ";\r
31     IndLevel += (E.Type == DW_MACINFO_start_file);\r
32 \r
33     WithColor(OS, syntax::Macro).get() << MacinfoString(E.Type);\r
34     switch (E.Type) {\r
35     default:\r
36       // Got a corrupted ".debug_macinfo" section (invalid macinfo type).\r
37       break;\r
38     case DW_MACINFO_define:\r
39     case DW_MACINFO_undef:\r
40       OS << " - lineno: " << E.Line;\r
41       OS << " macro: " << E.MacroStr;\r
42       break;\r
43     case DW_MACINFO_start_file:\r
44       OS << " - lineno: " << E.Line;\r
45       OS << " filenum: " << E.File;\r
46       break;\r
47     case DW_MACINFO_end_file:\r
48       break;\r
49     case DW_MACINFO_vendor_ext:\r
50       OS << " - constant: " << E.ExtConstant;\r
51       OS << " string: " << E.ExtStr;\r
52       break;\r
53     }\r
54     OS << "\n";\r
55   }\r
56 }\r
57 \r
58 void DWARFDebugMacro::parse(DataExtractor data) {\r
59   uint32_t Offset = 0;\r
60   while (data.isValidOffset(Offset)) {\r
61     // A macro list entry consists of:\r
62     Entry E;\r
63     // 1. Macinfo type\r
64     E.Type = data.getULEB128(&Offset);\r
65 \r
66     if (E.Type == 0) {\r
67       // Reached end of ".debug_macinfo" section.\r
68       return;\r
69     }\r
70 \r
71     switch (E.Type) {\r
72     default:\r
73       // Got a corrupted ".debug_macinfo" section (invalid macinfo type).\r
74       // Push the corrupted entry to the list and halt parsing.\r
75       E.Type = DW_MACINFO_invalid;\r
76       Macros.push_back(E);\r
77       return;\r
78     case DW_MACINFO_define:\r
79     case DW_MACINFO_undef:\r
80       // 2. Source line\r
81       E.Line = data.getULEB128(&Offset);\r
82       // 3. Macro string\r
83       E.MacroStr = data.getCStr(&Offset);\r
84       break;\r
85     case DW_MACINFO_start_file:\r
86       // 2. Source line\r
87       E.Line = data.getULEB128(&Offset);\r
88       // 3. Source file id\r
89       E.File = data.getULEB128(&Offset);\r
90       break;\r
91     case DW_MACINFO_end_file:\r
92       break;\r
93     case DW_MACINFO_vendor_ext:\r
94       // 2. Vendor extension constant\r
95       E.ExtConstant = data.getULEB128(&Offset);\r
96       // 3. Vendor extension string\r
97       E.ExtStr = data.getCStr(&Offset);\r
98       break;\r
99     }\r
100 \r
101     Macros.push_back(E);\r
102   }\r
103 }\r