Path: blob/main/contrib/llvm-project/llvm/tools/llvm-pdbutil/PrettyVariableDumper.cpp
35260 views
//===- PrettyVariableDumper.cpp ---------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "PrettyVariableDumper.h"910#include "PrettyBuiltinDumper.h"11#include "PrettyFunctionDumper.h"12#include "llvm-pdbutil.h"1314#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"15#include "llvm/DebugInfo/PDB/IPDBSession.h"16#include "llvm/DebugInfo/PDB/PDBSymbolData.h"17#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"18#include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"19#include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"20#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"21#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"22#include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"23#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"24#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"25#include "llvm/DebugInfo/PDB/PDBSymbolTypeVTable.h"26#include "llvm/DebugInfo/PDB/PDBSymbolTypeVTableShape.h"27#include "llvm/DebugInfo/PDB/PDBTypes.h"2829#include "llvm/Support/Format.h"3031using namespace llvm;32using namespace llvm::codeview;33using namespace llvm::pdb;3435VariableDumper::VariableDumper(LinePrinter &P)36: PDBSymDumper(true), Printer(P) {}3738void VariableDumper::start(const PDBSymbolData &Var, uint32_t Offset) {39if (Var.isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated)40return;41if (Printer.IsSymbolExcluded(Var.getName()))42return;4344auto VarType = Var.getType();4546uint64_t Length = VarType->getRawSymbol().getLength();4748switch (auto LocType = Var.getLocationType()) {49case PDB_LocType::Static:50Printer.NewLine();51Printer << "data [";52WithColor(Printer, PDB_ColorItem::Address).get()53<< format_hex(Var.getVirtualAddress(), 10);54Printer << ", sizeof=" << Length << "] ";55WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";56dumpSymbolTypeAndName(*VarType, Var.getName());57break;58case PDB_LocType::Constant:59if (isa<PDBSymbolTypeEnum>(*VarType))60break;61Printer.NewLine();62Printer << "data [sizeof=" << Length << "] ";63dumpSymbolTypeAndName(*VarType, Var.getName());64Printer << " = ";65WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();66break;67case PDB_LocType::ThisRel:68Printer.NewLine();69Printer << "data ";70WithColor(Printer, PDB_ColorItem::Offset).get()71<< "+" << format_hex(Offset + Var.getOffset(), 4)72<< " [sizeof=" << Length << "] ";73dumpSymbolTypeAndName(*VarType, Var.getName());74break;75case PDB_LocType::BitField:76Printer.NewLine();77Printer << "data ";78WithColor(Printer, PDB_ColorItem::Offset).get()79<< "+" << format_hex(Offset + Var.getOffset(), 4)80<< " [sizeof=" << Length << "] ";81dumpSymbolTypeAndName(*VarType, Var.getName());82Printer << " : ";83WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();84break;85default:86Printer.NewLine();87Printer << "data [sizeof=" << Length << "] ";88Printer << "unknown(" << LocType << ") ";89WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();90break;91}92}9394void VariableDumper::startVbptr(uint32_t Offset, uint32_t Size) {95Printer.NewLine();96Printer << "vbptr ";9798WithColor(Printer, PDB_ColorItem::Offset).get()99<< "+" << format_hex(Offset, 4) << " [sizeof=" << Size << "] ";100}101102void VariableDumper::start(const PDBSymbolTypeVTable &Var, uint32_t Offset) {103Printer.NewLine();104Printer << "vfptr ";105auto VTableType = cast<PDBSymbolTypePointer>(Var.getType());106uint32_t PointerSize = VTableType->getLength();107108WithColor(Printer, PDB_ColorItem::Offset).get()109<< "+" << format_hex(Offset + Var.getOffset(), 4)110<< " [sizeof=" << PointerSize << "] ";111}112113void VariableDumper::dump(const PDBSymbolTypeArray &Symbol) {114auto ElementType = Symbol.getElementType();115assert(ElementType);116if (!ElementType)117return;118ElementType->dump(*this);119}120121void VariableDumper::dumpRight(const PDBSymbolTypeArray &Symbol) {122auto ElementType = Symbol.getElementType();123assert(ElementType);124if (!ElementType)125return;126Printer << '[' << Symbol.getCount() << ']';127ElementType->dumpRight(*this);128}129130void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {131BuiltinDumper Dumper(Printer);132Dumper.start(Symbol);133}134135void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {136WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();137}138139void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {140auto ReturnType = Symbol.getReturnType();141ReturnType->dump(*this);142Printer << " ";143144uint32_t ClassParentId = Symbol.getClassParentId();145auto ClassParent =146Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(147ClassParentId);148149if (ClassParent) {150WithColor(Printer, PDB_ColorItem::Identifier).get()151<< ClassParent->getName();152Printer << "::";153}154}155156void VariableDumper::dumpRight(const PDBSymbolTypeFunctionSig &Symbol) {157Printer << "(";158if (auto Arguments = Symbol.getArguments()) {159uint32_t Index = 0;160while (auto Arg = Arguments->getNext()) {161Arg->dump(*this);162if (++Index < Arguments->getChildCount())163Printer << ", ";164}165}166Printer << ")";167168if (Symbol.isConstType())169WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";170if (Symbol.isVolatileType())171WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";172173if (Symbol.getRawSymbol().isRestrictedType())174WithColor(Printer, PDB_ColorItem::Keyword).get() << " __restrict";175}176177void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {178auto PointeeType = Symbol.getPointeeType();179if (!PointeeType)180return;181PointeeType->dump(*this);182if (auto FuncSig = unique_dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType)) {183// A hack to get the calling convention in the right spot.184Printer << " (";185PDB_CallingConv CC = FuncSig->getCallingConvention();186WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";187} else if (isa<PDBSymbolTypeArray>(PointeeType)) {188Printer << " (";189}190Printer << (Symbol.isReference() ? "&" : "*");191if (Symbol.isConstType())192WithColor(Printer, PDB_ColorItem::Keyword).get() << " const ";193if (Symbol.isVolatileType())194WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile ";195196if (Symbol.getRawSymbol().isRestrictedType())197WithColor(Printer, PDB_ColorItem::Keyword).get() << " __restrict ";198}199200void VariableDumper::dumpRight(const PDBSymbolTypePointer &Symbol) {201auto PointeeType = Symbol.getPointeeType();202assert(PointeeType);203if (!PointeeType)204return;205if (isa<PDBSymbolTypeFunctionSig>(PointeeType) ||206isa<PDBSymbolTypeArray>(PointeeType)) {207Printer << ")";208}209PointeeType->dumpRight(*this);210}211212void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {213WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";214WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();215}216217void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {218WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();219}220221void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,222StringRef Name) {223Type.dump(*this);224WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;225Type.dumpRight(*this);226}227228229