Zachary Turner | a9054dd | 2017-01-11 00:35:43 +0000 | [diff] [blame] | 1 | //===- PrettyVariableDumper.cpp ---------------------------------*- C++ -*-===// |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 2 | // |
| 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 | |
Zachary Turner | a9054dd | 2017-01-11 00:35:43 +0000 | [diff] [blame] | 10 | #include "PrettyVariableDumper.h" |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 11 | |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 12 | #include "LinePrinter.h" |
Zachary Turner | a9054dd | 2017-01-11 00:35:43 +0000 | [diff] [blame] | 13 | #include "PrettyBuiltinDumper.h" |
| 14 | #include "PrettyFunctionDumper.h" |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 15 | #include "llvm-pdbdump.h" |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 16 | |
Adrian McCarthy | 08eb343 | 2017-04-10 16:43:09 +0000 | [diff] [blame^] | 17 | #include "llvm/DebugInfo/PDB/IPDBSession.h" |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/PDBSymbolData.h" |
| 19 | #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" |
| 20 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h" |
Zachary Turner | 6532365 | 2015-03-04 06:09:53 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" |
Zachary Turner | a9054dd | 2017-01-11 00:35:43 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" |
Zachary Turner | d270d22 | 2015-02-26 23:49:23 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h" |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h" |
| 25 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" |
Adrian McCarthy | 08eb343 | 2017-04-10 16:43:09 +0000 | [diff] [blame^] | 27 | #include "llvm/DebugInfo/PDB/PDBTypes.h" |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 28 | |
| 29 | #include "llvm/Support/Format.h" |
| 30 | |
| 31 | using namespace llvm; |
Adrian McCarthy | 08eb343 | 2017-04-10 16:43:09 +0000 | [diff] [blame^] | 32 | using namespace llvm::codeview; |
Zachary Turner | ec28fc3 | 2016-05-04 20:32:13 +0000 | [diff] [blame] | 33 | using namespace llvm::pdb; |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 34 | |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 35 | VariableDumper::VariableDumper(LinePrinter &P) |
| 36 | : PDBSymDumper(true), Printer(P) {} |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 37 | |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 38 | void VariableDumper::start(const PDBSymbolData &Var) { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 39 | if (Var.isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated) |
Zachary Turner | 7797c72 | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 40 | return; |
Zachary Turner | f5abda2 | 2015-03-01 06:49:49 +0000 | [diff] [blame] | 41 | if (Printer.IsSymbolExcluded(Var.getName())) |
| 42 | return; |
| 43 | |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 44 | auto VarType = Var.getType(); |
| 45 | |
| 46 | switch (auto LocType = Var.getLocationType()) { |
| 47 | case PDB_LocType::Static: |
Zachary Turner | 6532365 | 2015-03-04 06:09:53 +0000 | [diff] [blame] | 48 | Printer.NewLine(); |
| 49 | Printer << "data ["; |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 50 | WithColor(Printer, PDB_ColorItem::Address).get() |
Zachary Turner | e5cb269 | 2015-05-01 20:24:26 +0000 | [diff] [blame] | 51 | << format_hex(Var.getVirtualAddress(), 10); |
Zachary Turner | 7797c72 | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 52 | Printer << "] "; |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 53 | WithColor(Printer, PDB_ColorItem::Keyword).get() << "static "; |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 54 | dumpSymbolTypeAndName(*VarType, Var.getName()); |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 55 | break; |
| 56 | case PDB_LocType::Constant: |
Zachary Turner | 6532365 | 2015-03-04 06:09:53 +0000 | [diff] [blame] | 57 | if (isa<PDBSymbolTypeEnum>(*VarType)) |
| 58 | break; |
| 59 | Printer.NewLine(); |
| 60 | Printer << "data "; |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 61 | dumpSymbolTypeAndName(*VarType, Var.getName()); |
Zachary Turner | 7797c72 | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 62 | Printer << " = "; |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 63 | WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue(); |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 64 | break; |
Benjamin Kramer | d0be170 | 2015-02-23 11:33:54 +0000 | [diff] [blame] | 65 | case PDB_LocType::ThisRel: |
Zachary Turner | 6532365 | 2015-03-04 06:09:53 +0000 | [diff] [blame] | 66 | Printer.NewLine(); |
| 67 | Printer << "data "; |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 68 | WithColor(Printer, PDB_ColorItem::Offset).get() |
| 69 | << "+" << format_hex(Var.getOffset(), 4) << " "; |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 70 | dumpSymbolTypeAndName(*VarType, Var.getName()); |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 71 | break; |
Zachary Turner | 7797c72 | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 72 | case PDB_LocType::BitField: |
Zachary Turner | 6532365 | 2015-03-04 06:09:53 +0000 | [diff] [blame] | 73 | Printer.NewLine(); |
| 74 | Printer << "data "; |
Zachary Turner | 7797c72 | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 75 | WithColor(Printer, PDB_ColorItem::Offset).get() |
| 76 | << "+" << format_hex(Var.getOffset(), 4) << " "; |
| 77 | dumpSymbolTypeAndName(*VarType, Var.getName()); |
| 78 | Printer << " : "; |
| 79 | WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength(); |
| 80 | break; |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 81 | default: |
Zachary Turner | 6532365 | 2015-03-04 06:09:53 +0000 | [diff] [blame] | 82 | Printer.NewLine(); |
| 83 | Printer << "data "; |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 84 | Printer << "unknown(" << LocType << ") "; |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 85 | WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName(); |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 86 | break; |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Adrian McCarthy | 08eb343 | 2017-04-10 16:43:09 +0000 | [diff] [blame^] | 90 | void VariableDumper::dump(const PDBSymbolTypeArray &Symbol) { |
| 91 | auto ElementType = Symbol.getElementType(); |
| 92 | assert(ElementType); |
| 93 | if (!ElementType) |
| 94 | return; |
| 95 | ElementType->dump(*this); |
| 96 | } |
| 97 | |
| 98 | void VariableDumper::dumpRight(const PDBSymbolTypeArray &Symbol) { |
| 99 | auto ElementType = Symbol.getElementType(); |
| 100 | assert(ElementType); |
| 101 | if (!ElementType) |
| 102 | return; |
| 103 | Printer << '[' << Symbol.getCount() << ']'; |
| 104 | ElementType->dumpRight(*this); |
| 105 | } |
| 106 | |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 107 | void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) { |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 108 | BuiltinDumper Dumper(Printer); |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 109 | Dumper.start(Symbol); |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 112 | void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) { |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 113 | WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName(); |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Adrian McCarthy | 08eb343 | 2017-04-10 16:43:09 +0000 | [diff] [blame^] | 116 | void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) { |
| 117 | auto ReturnType = Symbol.getReturnType(); |
| 118 | ReturnType->dump(*this); |
| 119 | Printer << " "; |
| 120 | |
| 121 | uint32_t ClassParentId = Symbol.getClassParentId(); |
| 122 | auto ClassParent = |
| 123 | Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>( |
| 124 | ClassParentId); |
| 125 | |
| 126 | if (ClassParent) { |
| 127 | WithColor(Printer, PDB_ColorItem::Identifier).get() |
| 128 | << ClassParent->getName(); |
| 129 | Printer << "::"; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void VariableDumper::dumpRight(const PDBSymbolTypeFunctionSig &Symbol) { |
| 134 | Printer << "("; |
| 135 | if (auto Arguments = Symbol.getArguments()) { |
| 136 | uint32_t Index = 0; |
| 137 | while (auto Arg = Arguments->getNext()) { |
| 138 | Arg->dump(*this); |
| 139 | if (++Index < Arguments->getChildCount()) |
| 140 | Printer << ", "; |
| 141 | } |
| 142 | } |
| 143 | Printer << ")"; |
| 144 | |
| 145 | if (Symbol.isConstType()) |
| 146 | WithColor(Printer, PDB_ColorItem::Keyword).get() << " const"; |
| 147 | if (Symbol.isVolatileType()) |
| 148 | WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile"; |
| 149 | } |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 150 | |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 151 | void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) { |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 152 | auto PointeeType = Symbol.getPointeeType(); |
| 153 | if (!PointeeType) |
| 154 | return; |
Adrian McCarthy | 08eb343 | 2017-04-10 16:43:09 +0000 | [diff] [blame^] | 155 | PointeeType->dump(*this); |
| 156 | if (auto Func = PointeeType->cast<PDBSymbolTypeFunctionSig>()) { |
| 157 | // A hack to get the calling convention in the right spot. |
| 158 | Printer << " ("; |
| 159 | PDB_CallingConv CC = Func->getCallingConvention(); |
| 160 | WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " "; |
| 161 | } else if (isa<PDBSymbolTypeArray>(PointeeType.get())) { |
| 162 | Printer << " ("; |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 163 | } |
Adrian McCarthy | 08eb343 | 2017-04-10 16:43:09 +0000 | [diff] [blame^] | 164 | Printer << (Symbol.isReference() ? "&" : "*"); |
| 165 | if (Symbol.isConstType()) |
| 166 | WithColor(Printer, PDB_ColorItem::Keyword).get() << " const "; |
| 167 | if (Symbol.isVolatileType()) |
| 168 | WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile "; |
| 169 | } |
| 170 | |
| 171 | void VariableDumper::dumpRight(const PDBSymbolTypePointer &Symbol) { |
| 172 | auto PointeeType = Symbol.getPointeeType(); |
| 173 | assert(PointeeType); |
| 174 | if (!PointeeType) |
| 175 | return; |
| 176 | if (isa<PDBSymbolTypeFunctionSig>(PointeeType.get()) || |
| 177 | isa<PDBSymbolTypeArray>(PointeeType.get())) { |
| 178 | Printer << ")"; |
| 179 | } |
| 180 | PointeeType->dumpRight(*this); |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 183 | void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) { |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 184 | WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef "; |
| 185 | WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName(); |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 188 | void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) { |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 189 | WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName(); |
Zachary Turner | 29c6910 | 2015-02-23 05:58:34 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type, |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 193 | StringRef Name) { |
Adrian McCarthy | 08eb343 | 2017-04-10 16:43:09 +0000 | [diff] [blame^] | 194 | Type.dump(*this); |
| 195 | WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name; |
| 196 | Type.dumpRight(*this); |
Zachary Turner | d270d22 | 2015-02-26 23:49:23 +0000 | [diff] [blame] | 197 | } |