LLVM  10.0.0
DWARFDebugMacro.cpp
Go to the documentation of this file.
1 //===- DWARFDebugMacro.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 #include "llvm/Support/WithColor.h"
13 #include <cstdint>
14 
15 using namespace llvm;
16 using namespace dwarf;
17 
19  unsigned IndLevel = 0;
20  for (const auto &Macros : MacroLists) {
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.
24  if (IndLevel > 0)
25  IndLevel -= (E.Type == DW_MACINFO_end_file);
26  // Print indentation.
27  for (unsigned I = 0; I < IndLevel; I++)
28  OS << " ";
29  IndLevel += (E.Type == DW_MACINFO_start_file);
30 
32  switch (E.Type) {
33  default:
34  // Got a corrupted ".debug_macinfo" section (invalid macinfo type).
35  break;
36  case DW_MACINFO_define:
37  case DW_MACINFO_undef:
38  OS << " - lineno: " << E.Line;
39  OS << " macro: " << E.MacroStr;
40  break;
42  OS << " - lineno: " << E.Line;
43  OS << " filenum: " << E.File;
44  break;
46  break;
48  OS << " - constant: " << E.ExtConstant;
49  OS << " string: " << E.ExtStr;
50  break;
51  }
52  OS << "\n";
53  }
54  OS << "\n";
55  }
56 }
57 
59  uint64_t Offset = 0;
60  MacroList *M = nullptr;
61  while (data.isValidOffset(Offset)) {
62  if (!M) {
63  MacroLists.emplace_back();
64  M = &MacroLists.back();
65  }
66  // A macro list entry consists of:
67  M->emplace_back();
68  Entry &E = M->back();
69  // 1. Macinfo type
70  E.Type = data.getULEB128(&Offset);
71 
72  if (E.Type == 0) {
73  // Reached end of a ".debug_macinfo" section contribution.
74  continue;
75  }
76 
77  switch (E.Type) {
78  default:
79  // Got a corrupted ".debug_macinfo" section (invalid macinfo type).
80  // Push the corrupted entry to the list and halt parsing.
81  E.Type = DW_MACINFO_invalid;
82  return;
83  case DW_MACINFO_define:
84  case DW_MACINFO_undef:
85  // 2. Source line
86  E.Line = data.getULEB128(&Offset);
87  // 3. Macro string
88  E.MacroStr = data.getCStr(&Offset);
89  break;
91  // 2. Source line
92  E.Line = data.getULEB128(&Offset);
93  // 3. Source file id
94  E.File = data.getULEB128(&Offset);
95  break;
97  break;
99  // 2. Vendor extension constant
100  E.ExtConstant = data.getULEB128(&Offset);
101  // 3. Vendor extension string
102  E.ExtStr = data.getCStr(&Offset);
103  break;
104  }
105  }
106 }
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:641
This class represents lattice values for constants.
Definition: AllocatorList.h:23
An RAII object that temporarily switches an output stream to a specific color.
Definition: WithColor.h:37
const char * getCStr(uint64_t *offset_ptr) const
Extract a C string from *offset_ptr.
raw_ostream & get()
Definition: WithColor.h:64
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
void parse(DataExtractor data)
Parse the debug_macinfo section accessible via the &#39;data&#39; parameter.
void dump(raw_ostream &OS) const
Print the macro list found within the debug_macinfo section.
bool isValidOffset(uint64_t offset) const
Test the validity of offset.
This file contains constants used for implementing Dwarf debug support.
#define I(x, y, z)
Definition: MD5.cpp:58
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef MacinfoString(unsigned Encoding)
Definition: Dwarf.cpp:451