#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdint>
using namespace llvm;
using namespace dwarf;
void DWARFDebugMacro::dump(raw_ostream &OS) const {
unsigned IndLevel = 0;
for (const auto &Macros : MacroLists) {
for (const Entry &E : Macros) {
if (IndLevel > 0)
IndLevel -= (E.Type == DW_MACINFO_end_file);
for (unsigned I = 0; I < IndLevel; I++)
OS << " ";
IndLevel += (E.Type == DW_MACINFO_start_file);
WithColor(OS, HighlightColor::Macro).get() << MacinfoString(E.Type);
switch (E.Type) {
default:
break;
case DW_MACINFO_define:
case DW_MACINFO_undef:
OS << " - lineno: " << E.Line;
OS << " macro: " << E.MacroStr;
break;
case DW_MACINFO_start_file:
OS << " - lineno: " << E.Line;
OS << " filenum: " << E.File;
break;
case DW_MACINFO_end_file:
break;
case DW_MACINFO_vendor_ext:
OS << " - constant: " << E.ExtConstant;
OS << " string: " << E.ExtStr;
break;
}
OS << "\n";
}
OS << "\n";
}
}
void DWARFDebugMacro::parse(DataExtractor data) {
uint64_t Offset = 0;
MacroList *M = nullptr;
while (data.isValidOffset(Offset)) {
if (!M) {
MacroLists.emplace_back();
M = &MacroLists.back();
}
M->emplace_back();
Entry &E = M->back();
E.Type = data.getULEB128(&Offset);
if (E.Type == 0) {
continue;
}
switch (E.Type) {
default:
E.Type = DW_MACINFO_invalid;
return;
case DW_MACINFO_define:
case DW_MACINFO_undef:
E.Line = data.getULEB128(&Offset);
E.MacroStr = data.getCStr(&Offset);
break;
case DW_MACINFO_start_file:
E.Line = data.getULEB128(&Offset);
E.File = data.getULEB128(&Offset);
break;
case DW_MACINFO_end_file:
break;
case DW_MACINFO_vendor_ext:
E.ExtConstant = data.getULEB128(&Offset);
E.ExtStr = data.getCStr(&Offset);
break;
}
}
}