blob: 1b77be6192ddc5fac0ed08a0919e5e77e2f5aa2e [file] [log] [blame]
Eugene Zelenko28db7e62017-03-01 01:14:23 +00001//===- DWARFDebugMacro.cpp ------------------------------------------------===//
Dimitry Andric227b9282016-01-03 17:22:03 +00002//
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
Eugene Zelenko28db7e62017-03-01 01:14:23 +000010#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000011#include "SyntaxHighlighting.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000012#include "llvm/BinaryFormat/Dwarf.h"
Dimitry Andric227b9282016-01-03 17:22:03 +000013#include "llvm/Support/raw_ostream.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000014#include <cstdint>
Dimitry Andric227b9282016-01-03 17:22:03 +000015
16using namespace llvm;
17using namespace dwarf;
18using namespace syntax;
19
20void DWARFDebugMacro::dump(raw_ostream &OS) const {
21 unsigned IndLevel = 0;
22 for (const Entry &E : Macros) {
23 // There should not be DW_MACINFO_end_file when IndLevel is Zero. However,
24 // this check handles the case of corrupted ".debug_macinfo" section.
25 if (IndLevel > 0)
26 IndLevel -= (E.Type == DW_MACINFO_end_file);
27 // Print indentation.
28 for (unsigned I = 0; I < IndLevel; I++)
29 OS << " ";
30 IndLevel += (E.Type == DW_MACINFO_start_file);
31
32 WithColor(OS, syntax::Macro).get() << MacinfoString(E.Type);
33 switch (E.Type) {
34 default:
35 // Got a corrupted ".debug_macinfo" section (invalid macinfo type).
36 break;
37 case DW_MACINFO_define:
38 case DW_MACINFO_undef:
39 OS << " - lineno: " << E.Line;
40 OS << " macro: " << E.MacroStr;
41 break;
42 case DW_MACINFO_start_file:
43 OS << " - lineno: " << E.Line;
44 OS << " filenum: " << E.File;
45 break;
46 case DW_MACINFO_end_file:
47 break;
48 case DW_MACINFO_vendor_ext:
49 OS << " - constant: " << E.ExtConstant;
50 OS << " string: " << E.ExtStr;
51 break;
52 }
53 OS << "\n";
54 }
55}
56
57void DWARFDebugMacro::parse(DataExtractor data) {
58 uint32_t Offset = 0;
59 while (data.isValidOffset(Offset)) {
60 // A macro list entry consists of:
61 Entry E;
62 // 1. Macinfo type
63 E.Type = data.getULEB128(&Offset);
64
65 if (E.Type == 0) {
66 // Reached end of ".debug_macinfo" section.
67 return;
68 }
69
70 switch (E.Type) {
71 default:
72 // Got a corrupted ".debug_macinfo" section (invalid macinfo type).
73 // Push the corrupted entry to the list and halt parsing.
74 E.Type = DW_MACINFO_invalid;
75 Macros.push_back(E);
76 return;
77 case DW_MACINFO_define:
78 case DW_MACINFO_undef:
79 // 2. Source line
80 E.Line = data.getULEB128(&Offset);
81 // 3. Macro string
82 E.MacroStr = data.getCStr(&Offset);
83 break;
84 case DW_MACINFO_start_file:
85 // 2. Source line
86 E.Line = data.getULEB128(&Offset);
87 // 3. Source file id
88 E.File = data.getULEB128(&Offset);
89 break;
90 case DW_MACINFO_end_file:
91 break;
92 case DW_MACINFO_vendor_ext:
93 // 2. Vendor extension constant
94 E.ExtConstant = data.getULEB128(&Offset);
95 // 3. Vendor extension string
96 E.ExtStr = data.getCStr(&Offset);
97 break;
98 }
99
100 Macros.push_back(E);
101 }
102}