llvm/tools/llvm-pdbdump/PrettyVariableDumper.cpp
Zachary Turner e98c913509 [llvm-pdbdump] Display padding bytes on record layout
When dumping classes, show where padding occurs, and at the end of the
class print statistics about how many bytes total of padding exist in a
class.

Since PDB doesn't specifically contain information about padding, we have
to mimic this by sort of reversing a small portion of the record layout
algorithm (e.g. looking at offsets and sizes and trying to determine
whether something is part of the same field or a new field).

Differential Revision: https://reviews.llvm.org/D31800

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@299869 91177308-0d34-0410-b5e6-96231b3b80d8
2017-04-10 19:33:29 +00:00

202 lines
6.4 KiB
C++

//===- PrettyVariableDumper.cpp ---------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "PrettyVariableDumper.h"
#include "LinePrinter.h"
#include "PrettyBuiltinDumper.h"
#include "PrettyFunctionDumper.h"
#include "llvm-pdbdump.h"
#include "llvm/DebugInfo/PDB/IPDBSession.h"
#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
#include "llvm/DebugInfo/PDB/PDBTypes.h"
#include "llvm/Support/Format.h"
using namespace llvm;
using namespace llvm::codeview;
using namespace llvm::pdb;
VariableDumper::VariableDumper(LinePrinter &P)
: PDBSymDumper(true), Printer(P) {}
void VariableDumper::start(const PDBSymbolData &Var) {
if (Var.isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated)
return;
if (Printer.IsSymbolExcluded(Var.getName()))
return;
auto VarType = Var.getType();
uint64_t Length = VarType->getRawSymbol().getLength();
switch (auto LocType = Var.getLocationType()) {
case PDB_LocType::Static:
Printer.NewLine();
Printer << "data [";
WithColor(Printer, PDB_ColorItem::Address).get()
<< format_hex(Var.getVirtualAddress(), 10);
Printer << ", sizeof=" << Length << "] ";
WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
dumpSymbolTypeAndName(*VarType, Var.getName());
break;
case PDB_LocType::Constant:
if (isa<PDBSymbolTypeEnum>(*VarType))
break;
Printer.NewLine();
Printer << "data [sizeof=" << Length << "] ";
dumpSymbolTypeAndName(*VarType, Var.getName());
Printer << " = ";
WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
break;
case PDB_LocType::ThisRel:
Printer.NewLine();
Printer << "data ";
WithColor(Printer, PDB_ColorItem::Offset).get()
<< "+" << format_hex(Var.getOffset(), 4) << " [sizeof=" << Length
<< "] ";
dumpSymbolTypeAndName(*VarType, Var.getName());
break;
case PDB_LocType::BitField:
Printer.NewLine();
Printer << "data ";
WithColor(Printer, PDB_ColorItem::Offset).get()
<< "+" << format_hex(Var.getOffset(), 4) << " [sizeof=" << Length
<< "] ";
dumpSymbolTypeAndName(*VarType, Var.getName());
Printer << " : ";
WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
break;
default:
Printer.NewLine();
Printer << "data [sizeof=" << Length << "] ";
Printer << "unknown(" << LocType << ") ";
WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
break;
}
}
void VariableDumper::dump(const PDBSymbolTypeArray &Symbol) {
auto ElementType = Symbol.getElementType();
assert(ElementType);
if (!ElementType)
return;
ElementType->dump(*this);
}
void VariableDumper::dumpRight(const PDBSymbolTypeArray &Symbol) {
auto ElementType = Symbol.getElementType();
assert(ElementType);
if (!ElementType)
return;
Printer << '[' << Symbol.getCount() << ']';
ElementType->dumpRight(*this);
}
void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
BuiltinDumper Dumper(Printer);
Dumper.start(Symbol);
}
void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
}
void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {
auto ReturnType = Symbol.getReturnType();
ReturnType->dump(*this);
Printer << " ";
uint32_t ClassParentId = Symbol.getClassParentId();
auto ClassParent =
Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
ClassParentId);
if (ClassParent) {
WithColor(Printer, PDB_ColorItem::Identifier).get()
<< ClassParent->getName();
Printer << "::";
}
}
void VariableDumper::dumpRight(const PDBSymbolTypeFunctionSig &Symbol) {
Printer << "(";
if (auto Arguments = Symbol.getArguments()) {
uint32_t Index = 0;
while (auto Arg = Arguments->getNext()) {
Arg->dump(*this);
if (++Index < Arguments->getChildCount())
Printer << ", ";
}
}
Printer << ")";
if (Symbol.isConstType())
WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
if (Symbol.isVolatileType())
WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
}
void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
auto PointeeType = Symbol.getPointeeType();
if (!PointeeType)
return;
PointeeType->dump(*this);
if (auto Func = PointeeType->cast<PDBSymbolTypeFunctionSig>()) {
// A hack to get the calling convention in the right spot.
Printer << " (";
PDB_CallingConv CC = Func->getCallingConvention();
WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
} else if (isa<PDBSymbolTypeArray>(PointeeType.get())) {
Printer << " (";
}
Printer << (Symbol.isReference() ? "&" : "*");
if (Symbol.isConstType())
WithColor(Printer, PDB_ColorItem::Keyword).get() << " const ";
if (Symbol.isVolatileType())
WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile ";
}
void VariableDumper::dumpRight(const PDBSymbolTypePointer &Symbol) {
auto PointeeType = Symbol.getPointeeType();
assert(PointeeType);
if (!PointeeType)
return;
if (isa<PDBSymbolTypeFunctionSig>(PointeeType.get()) ||
isa<PDBSymbolTypeArray>(PointeeType.get())) {
Printer << ")";
}
PointeeType->dumpRight(*this);
}
void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
}
void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
}
void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
StringRef Name) {
Type.dump(*this);
WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
Type.dumpRight(*this);
}