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 "SyntaxHighlighting.h"
11 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
12 #include "llvm/Support/Compiler.h"
13 #include "llvm/Support/Dwarf.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/raw_ostream.h"
18 using namespace dwarf;
19 using namespace syntax;
21 void DWARFDebugMacro::dump(raw_ostream &OS) const {
22 unsigned IndLevel = 0;
23 for (const Entry &E : Macros) {
24 // There should not be DW_MACINFO_end_file when IndLevel is Zero. However,
25 // this check handles the case of corrupted ".debug_macinfo" section.
27 IndLevel -= (E.Type == DW_MACINFO_end_file);
29 for (unsigned I = 0; I < IndLevel; I++)
31 IndLevel += (E.Type == DW_MACINFO_start_file);
33 WithColor(OS, syntax::Macro).get() << MacinfoString(E.Type);
36 // Got a corrupted ".debug_macinfo" section (invalid macinfo type).
38 case DW_MACINFO_define:
39 case DW_MACINFO_undef:
40 OS << " - lineno: " << E.Line;
41 OS << " macro: " << E.MacroStr;
43 case DW_MACINFO_start_file:
44 OS << " - lineno: " << E.Line;
45 OS << " filenum: " << E.File;
47 case DW_MACINFO_end_file:
49 case DW_MACINFO_vendor_ext:
50 OS << " - constant: " << E.ExtConstant;
51 OS << " string: " << E.ExtStr;
58 void DWARFDebugMacro::parse(DataExtractor data) {
60 while (data.isValidOffset(Offset)) {
61 // A macro list entry consists of:
64 E.Type = data.getULEB128(&Offset);
67 // Reached end of ".debug_macinfo" section.
73 // Got a corrupted ".debug_macinfo" section (invalid macinfo type).
74 // Push the corrupted entry to the list and halt parsing.
75 E.Type = DW_MACINFO_invalid;
78 case DW_MACINFO_define:
79 case DW_MACINFO_undef:
81 E.Line = data.getULEB128(&Offset);
83 E.MacroStr = data.getCStr(&Offset);
85 case DW_MACINFO_start_file:
87 E.Line = data.getULEB128(&Offset);
89 E.File = data.getULEB128(&Offset);
91 case DW_MACINFO_end_file:
93 case DW_MACINFO_vendor_ext:
94 // 2. Vendor extension constant
95 E.ExtConstant = data.getULEB128(&Offset);
96 // 3. Vendor extension string
97 E.ExtStr = data.getCStr(&Offset);