1 //===- DWARFDebugMacro.cpp ------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
11 #include "llvm/BinaryFormat/Dwarf.h"
12 #include "llvm/Support/WithColor.h"
13 #include "llvm/Support/raw_ostream.h"
17 using namespace dwarf
;
19 void DWARFDebugMacro::dump(raw_ostream
&OS
) const {
20 unsigned IndLevel
= 0;
21 for (const Entry
&E
: Macros
) {
22 // There should not be DW_MACINFO_end_file when IndLevel is Zero. However,
23 // this check handles the case of corrupted ".debug_macinfo" section.
25 IndLevel
-= (E
.Type
== DW_MACINFO_end_file
);
27 for (unsigned I
= 0; I
< IndLevel
; I
++)
29 IndLevel
+= (E
.Type
== DW_MACINFO_start_file
);
31 WithColor(OS
, HighlightColor::Macro
).get() << MacinfoString(E
.Type
);
34 // Got a corrupted ".debug_macinfo" section (invalid macinfo type).
36 case DW_MACINFO_define
:
37 case DW_MACINFO_undef
:
38 OS
<< " - lineno: " << E
.Line
;
39 OS
<< " macro: " << E
.MacroStr
;
41 case DW_MACINFO_start_file
:
42 OS
<< " - lineno: " << E
.Line
;
43 OS
<< " filenum: " << E
.File
;
45 case DW_MACINFO_end_file
:
47 case DW_MACINFO_vendor_ext
:
48 OS
<< " - constant: " << E
.ExtConstant
;
49 OS
<< " string: " << E
.ExtStr
;
56 void DWARFDebugMacro::parse(DataExtractor data
) {
58 while (data
.isValidOffset(Offset
)) {
59 // A macro list entry consists of:
62 E
.Type
= data
.getULEB128(&Offset
);
65 // Reached end of ".debug_macinfo" section.
71 // Got a corrupted ".debug_macinfo" section (invalid macinfo type).
72 // Push the corrupted entry to the list and halt parsing.
73 E
.Type
= DW_MACINFO_invalid
;
76 case DW_MACINFO_define
:
77 case DW_MACINFO_undef
:
79 E
.Line
= data
.getULEB128(&Offset
);
81 E
.MacroStr
= data
.getCStr(&Offset
);
83 case DW_MACINFO_start_file
:
85 E
.Line
= data
.getULEB128(&Offset
);
87 E
.File
= data
.getULEB128(&Offset
);
89 case DW_MACINFO_end_file
:
91 case DW_MACINFO_vendor_ext
:
92 // 2. Vendor extension constant
93 E
.ExtConstant
= data
.getULEB128(&Offset
);
94 // 3. Vendor extension string
95 E
.ExtStr
= data
.getCStr(&Offset
);