1 //===- llvm/unittest/DebugInfo/PDB/PDBApiTest.cpp -------------------------===// 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 10 #include <hash_map> 11 12 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" 13 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h" 14 #include "llvm/DebugInfo/PDB/PDBSymbol.h" 15 #include "llvm/DebugInfo/PDB/PDBSymbolAnnotation.h" 16 #include "llvm/DebugInfo/PDB/PDBSymbolBlock.h" 17 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" 18 #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" 19 #include "llvm/DebugInfo/PDB/PDBSymbolCompilandEnv.h" 20 #include "llvm/DebugInfo/PDB/PDBSymbolCustom.h" 21 #include "llvm/DebugInfo/PDB/PDBSymbolData.h" 22 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" 23 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" 24 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h" 25 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h" 26 #include "llvm/DebugInfo/PDB/PDBSymbolLabel.h" 27 #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h" 28 #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h" 29 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h" 30 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h" 31 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h" 32 #include "llvm/DebugInfo/PDB/PDBSymbolTypeCustom.h" 33 #include "llvm/DebugInfo/PDB/PDBSymbolTypeDimension.h" 34 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" 35 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFriend.h" 36 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h" 37 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h" 38 #include "llvm/DebugInfo/PDB/PDBSymbolTypeManaged.h" 39 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h" 40 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" 41 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" 42 #include "llvm/DebugInfo/PDB/PDBSymbolTypeVTable.h" 43 #include "llvm/DebugInfo/PDB/PDBSymbolTypeVTableShape.h" 44 #include "llvm/DebugInfo/PDB/PDBSymbolUnknown.h" 45 #include "llvm/DebugInfo/PDB/PDBSymbolUsingNamespace.h" 46 #include "llvm/DebugInfo/PDB/PDBTypes.h" 47 #include "gtest/gtest.h" 48 using namespace llvm; 49 50 namespace { 51 52 #define MOCK_SYMBOL_ACCESSOR(Func) \ 53 auto Func() const->decltype(((IPDBRawSymbol *)nullptr)->Func()) override { \ 54 typedef decltype(IPDBRawSymbol::Func()) ReturnType; \ 55 return ReturnType(); \ 56 } 57 58 class MockRawSymbol : public IPDBRawSymbol { 59 public: 60 MockRawSymbol(PDB_SymType SymType) : Type(SymType) {} 61 62 virtual void dump(llvm::raw_ostream &OS) const {} 63 64 virtual std::unique_ptr<IPDBEnumSymbols> 65 findChildren(PDB_SymType Type, StringRef Name, 66 PDB_NameSearchFlags Flags) const { 67 return nullptr; 68 } 69 virtual std::unique_ptr<IPDBEnumSymbols> 70 findChildrenByRVA(PDB_SymType Type, StringRef Name, PDB_NameSearchFlags Flags, 71 uint32_t RVA) const { 72 return nullptr; 73 } 74 virtual std::unique_ptr<IPDBEnumSymbols> 75 findInlineFramesByRVA(uint32_t RVA) const { 76 return nullptr; 77 } 78 79 virtual void getDataBytes(llvm::SmallVector<uint8_t, 32> &bytes) const {} 80 81 PDB_SymType getSymTag() const override { return Type; } 82 83 MOCK_SYMBOL_ACCESSOR(getAccess) 84 MOCK_SYMBOL_ACCESSOR(getAddressOffset) 85 MOCK_SYMBOL_ACCESSOR(getAddressSection) 86 MOCK_SYMBOL_ACCESSOR(getAge) 87 MOCK_SYMBOL_ACCESSOR(getArrayIndexTypeId) 88 MOCK_SYMBOL_ACCESSOR(getBackEndBuild) 89 MOCK_SYMBOL_ACCESSOR(getBackEndMajor) 90 MOCK_SYMBOL_ACCESSOR(getBackEndMinor) 91 MOCK_SYMBOL_ACCESSOR(getBaseDataOffset) 92 MOCK_SYMBOL_ACCESSOR(getBaseDataSlot) 93 MOCK_SYMBOL_ACCESSOR(getBaseSymbolId) 94 MOCK_SYMBOL_ACCESSOR(getBuiltinType) 95 MOCK_SYMBOL_ACCESSOR(getBitPosition) 96 MOCK_SYMBOL_ACCESSOR(getCallingConvention) 97 MOCK_SYMBOL_ACCESSOR(getClassParentId) 98 MOCK_SYMBOL_ACCESSOR(getCompilerName) 99 MOCK_SYMBOL_ACCESSOR(getCount) 100 MOCK_SYMBOL_ACCESSOR(getCountLiveRanges) 101 MOCK_SYMBOL_ACCESSOR(getFrontEndBuild) 102 MOCK_SYMBOL_ACCESSOR(getFrontEndMajor) 103 MOCK_SYMBOL_ACCESSOR(getFrontEndMinor) 104 MOCK_SYMBOL_ACCESSOR(getLanguage) 105 MOCK_SYMBOL_ACCESSOR(getLexicalParentId) 106 MOCK_SYMBOL_ACCESSOR(getLibraryName) 107 MOCK_SYMBOL_ACCESSOR(getLiveRangeStartAddressOffset) 108 MOCK_SYMBOL_ACCESSOR(getLiveRangeStartAddressSection) 109 MOCK_SYMBOL_ACCESSOR(getLiveRangeStartRelativeVirtualAddress) 110 MOCK_SYMBOL_ACCESSOR(getLocalBasePointerRegisterId) 111 MOCK_SYMBOL_ACCESSOR(getLowerBoundId) 112 MOCK_SYMBOL_ACCESSOR(getMemorySpaceKind) 113 MOCK_SYMBOL_ACCESSOR(getName) 114 MOCK_SYMBOL_ACCESSOR(getNumberOfAcceleratorPointerTags) 115 MOCK_SYMBOL_ACCESSOR(getNumberOfColumns) 116 MOCK_SYMBOL_ACCESSOR(getNumberOfModifiers) 117 MOCK_SYMBOL_ACCESSOR(getNumberOfRegisterIndices) 118 MOCK_SYMBOL_ACCESSOR(getNumberOfRows) 119 MOCK_SYMBOL_ACCESSOR(getObjectFileName) 120 MOCK_SYMBOL_ACCESSOR(getOemId) 121 MOCK_SYMBOL_ACCESSOR(getOemSymbolId) 122 MOCK_SYMBOL_ACCESSOR(getOffsetInUdt) 123 MOCK_SYMBOL_ACCESSOR(getPlatform) 124 MOCK_SYMBOL_ACCESSOR(getRank) 125 MOCK_SYMBOL_ACCESSOR(getRegisterId) 126 MOCK_SYMBOL_ACCESSOR(getRegisterType) 127 MOCK_SYMBOL_ACCESSOR(getRelativeVirtualAddress) 128 MOCK_SYMBOL_ACCESSOR(getSamplerSlot) 129 MOCK_SYMBOL_ACCESSOR(getSignature) 130 MOCK_SYMBOL_ACCESSOR(getSizeInUdt) 131 MOCK_SYMBOL_ACCESSOR(getSlot) 132 MOCK_SYMBOL_ACCESSOR(getSourceFileName) 133 MOCK_SYMBOL_ACCESSOR(getStride) 134 MOCK_SYMBOL_ACCESSOR(getSubTypeId) 135 MOCK_SYMBOL_ACCESSOR(getSymbolsFileName) 136 MOCK_SYMBOL_ACCESSOR(getSymIndexId) 137 MOCK_SYMBOL_ACCESSOR(getTargetOffset) 138 MOCK_SYMBOL_ACCESSOR(getTargetRelativeVirtualAddress) 139 MOCK_SYMBOL_ACCESSOR(getTargetVirtualAddress) 140 MOCK_SYMBOL_ACCESSOR(getTargetSection) 141 MOCK_SYMBOL_ACCESSOR(getTextureSlot) 142 MOCK_SYMBOL_ACCESSOR(getTimeStamp) 143 MOCK_SYMBOL_ACCESSOR(getToken) 144 MOCK_SYMBOL_ACCESSOR(getTypeId) 145 MOCK_SYMBOL_ACCESSOR(getUavSlot) 146 MOCK_SYMBOL_ACCESSOR(getUndecoratedName) 147 MOCK_SYMBOL_ACCESSOR(getUnmodifiedTypeId) 148 MOCK_SYMBOL_ACCESSOR(getUpperBoundId) 149 MOCK_SYMBOL_ACCESSOR(getVirtualBaseDispIndex) 150 MOCK_SYMBOL_ACCESSOR(getVirtualBaseOffset) 151 MOCK_SYMBOL_ACCESSOR(getVirtualTableShapeId) 152 MOCK_SYMBOL_ACCESSOR(getDataKind) 153 MOCK_SYMBOL_ACCESSOR(getGuid) 154 MOCK_SYMBOL_ACCESSOR(getOffset) 155 MOCK_SYMBOL_ACCESSOR(getThisAdjust) 156 MOCK_SYMBOL_ACCESSOR(getVirtualBasePointerOffset) 157 MOCK_SYMBOL_ACCESSOR(getLocationType) 158 MOCK_SYMBOL_ACCESSOR(getMachineType) 159 MOCK_SYMBOL_ACCESSOR(getThunkOrdinal) 160 MOCK_SYMBOL_ACCESSOR(getLength) 161 MOCK_SYMBOL_ACCESSOR(getLiveRangeLength) 162 MOCK_SYMBOL_ACCESSOR(getVirtualAddress) 163 MOCK_SYMBOL_ACCESSOR(getUdtKind) 164 MOCK_SYMBOL_ACCESSOR(hasConstructor) 165 MOCK_SYMBOL_ACCESSOR(hasCustomCallingConvention) 166 MOCK_SYMBOL_ACCESSOR(hasFarReturn) 167 MOCK_SYMBOL_ACCESSOR(isCode) 168 MOCK_SYMBOL_ACCESSOR(isCompilerGenerated) 169 MOCK_SYMBOL_ACCESSOR(isConstType) 170 MOCK_SYMBOL_ACCESSOR(isEditAndContinueEnabled) 171 MOCK_SYMBOL_ACCESSOR(isFunction) 172 MOCK_SYMBOL_ACCESSOR(getAddressTaken) 173 MOCK_SYMBOL_ACCESSOR(getNoStackOrdering) 174 MOCK_SYMBOL_ACCESSOR(hasAlloca) 175 MOCK_SYMBOL_ACCESSOR(hasAssignmentOperator) 176 MOCK_SYMBOL_ACCESSOR(hasCTypes) 177 MOCK_SYMBOL_ACCESSOR(hasCastOperator) 178 MOCK_SYMBOL_ACCESSOR(hasDebugInfo) 179 MOCK_SYMBOL_ACCESSOR(hasEH) 180 MOCK_SYMBOL_ACCESSOR(hasEHa) 181 MOCK_SYMBOL_ACCESSOR(hasInlAsm) 182 MOCK_SYMBOL_ACCESSOR(hasInlineAttribute) 183 MOCK_SYMBOL_ACCESSOR(hasInterruptReturn) 184 MOCK_SYMBOL_ACCESSOR(hasLongJump) 185 MOCK_SYMBOL_ACCESSOR(hasManagedCode) 186 MOCK_SYMBOL_ACCESSOR(hasNestedTypes) 187 MOCK_SYMBOL_ACCESSOR(hasNoInlineAttribute) 188 MOCK_SYMBOL_ACCESSOR(hasNoReturnAttribute) 189 MOCK_SYMBOL_ACCESSOR(hasOptimizedCodeDebugInfo) 190 MOCK_SYMBOL_ACCESSOR(hasOverloadedOperator) 191 MOCK_SYMBOL_ACCESSOR(hasSEH) 192 MOCK_SYMBOL_ACCESSOR(hasSecurityChecks) 193 MOCK_SYMBOL_ACCESSOR(hasSetJump) 194 MOCK_SYMBOL_ACCESSOR(hasStrictGSCheck) 195 MOCK_SYMBOL_ACCESSOR(isAcceleratorGroupSharedLocal) 196 MOCK_SYMBOL_ACCESSOR(isAcceleratorPointerTagLiveRange) 197 MOCK_SYMBOL_ACCESSOR(isAcceleratorStubFunction) 198 MOCK_SYMBOL_ACCESSOR(isAggregated) 199 MOCK_SYMBOL_ACCESSOR(isIntroVirtualFunction) 200 MOCK_SYMBOL_ACCESSOR(isCVTCIL) 201 MOCK_SYMBOL_ACCESSOR(isConstructorVirtualBase) 202 MOCK_SYMBOL_ACCESSOR(isCxxReturnUdt) 203 MOCK_SYMBOL_ACCESSOR(isDataAligned) 204 MOCK_SYMBOL_ACCESSOR(isHLSLData) 205 MOCK_SYMBOL_ACCESSOR(isHotpatchable) 206 MOCK_SYMBOL_ACCESSOR(isIndirectVirtualBaseClass) 207 MOCK_SYMBOL_ACCESSOR(isInterfaceUdt) 208 MOCK_SYMBOL_ACCESSOR(isIntrinsic) 209 MOCK_SYMBOL_ACCESSOR(isLTCG) 210 MOCK_SYMBOL_ACCESSOR(isLocationControlFlowDependent) 211 MOCK_SYMBOL_ACCESSOR(isMSILNetmodule) 212 MOCK_SYMBOL_ACCESSOR(isMatrixRowMajor) 213 MOCK_SYMBOL_ACCESSOR(isManagedCode) 214 MOCK_SYMBOL_ACCESSOR(isMSILCode) 215 MOCK_SYMBOL_ACCESSOR(isMultipleInheritance) 216 MOCK_SYMBOL_ACCESSOR(isNaked) 217 MOCK_SYMBOL_ACCESSOR(isNested) 218 MOCK_SYMBOL_ACCESSOR(isOptimizedAway) 219 MOCK_SYMBOL_ACCESSOR(isPacked) 220 MOCK_SYMBOL_ACCESSOR(isPointerBasedOnSymbolValue) 221 MOCK_SYMBOL_ACCESSOR(isPointerToDataMember) 222 MOCK_SYMBOL_ACCESSOR(isPointerToMemberFunction) 223 MOCK_SYMBOL_ACCESSOR(isPureVirtual) 224 MOCK_SYMBOL_ACCESSOR(isRValueReference) 225 MOCK_SYMBOL_ACCESSOR(isRefUdt) 226 MOCK_SYMBOL_ACCESSOR(isReference) 227 MOCK_SYMBOL_ACCESSOR(isRestrictedType) 228 MOCK_SYMBOL_ACCESSOR(isReturnValue) 229 MOCK_SYMBOL_ACCESSOR(isSafeBuffers) 230 MOCK_SYMBOL_ACCESSOR(isScoped) 231 MOCK_SYMBOL_ACCESSOR(isSdl) 232 MOCK_SYMBOL_ACCESSOR(isSingleInheritance) 233 MOCK_SYMBOL_ACCESSOR(isSplitted) 234 MOCK_SYMBOL_ACCESSOR(isStatic) 235 MOCK_SYMBOL_ACCESSOR(hasPrivateSymbols) 236 MOCK_SYMBOL_ACCESSOR(isUnalignedType) 237 MOCK_SYMBOL_ACCESSOR(isUnreached) 238 MOCK_SYMBOL_ACCESSOR(isValueUdt) 239 MOCK_SYMBOL_ACCESSOR(isVirtual) 240 MOCK_SYMBOL_ACCESSOR(isVirtualBaseClass) 241 MOCK_SYMBOL_ACCESSOR(isVirtualInheritance) 242 MOCK_SYMBOL_ACCESSOR(isVolatileType) 243 244 private: 245 PDB_SymType Type; 246 }; 247 248 class PDBApiTest : public testing::Test { 249 public: 250 std::hash_map<PDB_SymType, std::unique_ptr<PDBSymbol>> SymbolMap; 251 252 void SetUp() override { 253 InsertItemWithTag(PDB_SymType::None); 254 InsertItemWithTag(PDB_SymType::Exe); 255 InsertItemWithTag(PDB_SymType::Compiland); 256 InsertItemWithTag(PDB_SymType::CompilandDetails); 257 InsertItemWithTag(PDB_SymType::CompilandEnv); 258 InsertItemWithTag(PDB_SymType::Function); 259 InsertItemWithTag(PDB_SymType::Block); 260 InsertItemWithTag(PDB_SymType::Data); 261 InsertItemWithTag(PDB_SymType::Annotation); 262 InsertItemWithTag(PDB_SymType::Label); 263 InsertItemWithTag(PDB_SymType::PublicSymbol); 264 InsertItemWithTag(PDB_SymType::UDT); 265 InsertItemWithTag(PDB_SymType::Enum); 266 InsertItemWithTag(PDB_SymType::FunctionSig); 267 InsertItemWithTag(PDB_SymType::PointerType); 268 InsertItemWithTag(PDB_SymType::ArrayType); 269 InsertItemWithTag(PDB_SymType::BuiltinType); 270 InsertItemWithTag(PDB_SymType::Typedef); 271 InsertItemWithTag(PDB_SymType::BaseClass); 272 InsertItemWithTag(PDB_SymType::Friend); 273 InsertItemWithTag(PDB_SymType::FunctionArg); 274 InsertItemWithTag(PDB_SymType::FuncDebugStart); 275 InsertItemWithTag(PDB_SymType::FuncDebugEnd); 276 InsertItemWithTag(PDB_SymType::UsingNamespace); 277 InsertItemWithTag(PDB_SymType::VTableShape); 278 InsertItemWithTag(PDB_SymType::VTable); 279 InsertItemWithTag(PDB_SymType::Custom); 280 InsertItemWithTag(PDB_SymType::Thunk); 281 InsertItemWithTag(PDB_SymType::CustomType); 282 InsertItemWithTag(PDB_SymType::ManagedType); 283 InsertItemWithTag(PDB_SymType::Dimension); 284 InsertItemWithTag(PDB_SymType::Max); 285 } 286 287 private: 288 void InsertItemWithTag(PDB_SymType Tag) { 289 auto RawSymbol = std::unique_ptr<IPDBRawSymbol>(new MockRawSymbol(Tag)); 290 auto Symbol = PDBSymbol::create(std::move(RawSymbol)); 291 SymbolMap.insert(std::make_pair(Tag, std::move(Symbol))); 292 } 293 294 public: 295 template <class ExpectedType> void VerifyDyncast(PDB_SymType Tag) { 296 for (auto item = SymbolMap.begin(); item != SymbolMap.end(); ++item) { 297 EXPECT_EQ(item->first == Tag, llvm::isa<ExpectedType>(*item->second)); 298 } 299 } 300 301 void VerifyUnknownDyncasts() { 302 for (auto item = SymbolMap.begin(); item != SymbolMap.end(); ++item) { 303 bool should_match = false; 304 if (item->first == PDB_SymType::None || item->first >= PDB_SymType::Max) 305 should_match = true; 306 307 EXPECT_EQ(should_match, llvm::isa<PDBSymbolUnknown>(*item->second)); 308 } 309 } 310 }; 311 312 TEST_F(PDBApiTest, Dyncast) { 313 314 // Most of the types have a one-to-one mapping between Tag and concrete type. 315 VerifyDyncast<PDBSymbolExe>(PDB_SymType::Exe); 316 VerifyDyncast<PDBSymbolCompiland>(PDB_SymType::Compiland); 317 VerifyDyncast<PDBSymbolCompilandDetails>(PDB_SymType::CompilandDetails); 318 VerifyDyncast<PDBSymbolCompilandEnv>(PDB_SymType::CompilandEnv); 319 VerifyDyncast<PDBSymbolFunc>(PDB_SymType::Function); 320 VerifyDyncast<PDBSymbolBlock>(PDB_SymType::Block); 321 VerifyDyncast<PDBSymbolData>(PDB_SymType::Data); 322 VerifyDyncast<PDBSymbolAnnotation>(PDB_SymType::Annotation); 323 VerifyDyncast<PDBSymbolLabel>(PDB_SymType::Label); 324 VerifyDyncast<PDBSymbolPublicSymbol>(PDB_SymType::PublicSymbol); 325 VerifyDyncast<PDBSymbolTypeUDT>(PDB_SymType::UDT); 326 VerifyDyncast<PDBSymbolTypeEnum>(PDB_SymType::Enum); 327 VerifyDyncast<PDBSymbolTypeFunctionSig>(PDB_SymType::FunctionSig); 328 VerifyDyncast<PDBSymbolTypePointer>(PDB_SymType::PointerType); 329 VerifyDyncast<PDBSymbolTypeArray>(PDB_SymType::ArrayType); 330 VerifyDyncast<PDBSymbolTypeBuiltin>(PDB_SymType::BuiltinType); 331 VerifyDyncast<PDBSymbolTypeTypedef>(PDB_SymType::Typedef); 332 VerifyDyncast<PDBSymbolTypeBaseClass>(PDB_SymType::BaseClass); 333 VerifyDyncast<PDBSymbolTypeFriend>(PDB_SymType::Friend); 334 VerifyDyncast<PDBSymbolTypeFunctionArg>(PDB_SymType::FunctionArg); 335 VerifyDyncast<PDBSymbolFuncDebugStart>(PDB_SymType::FuncDebugStart); 336 VerifyDyncast<PDBSymbolFuncDebugEnd>(PDB_SymType::FuncDebugEnd); 337 VerifyDyncast<PDBSymbolUsingNamespace>(PDB_SymType::UsingNamespace); 338 VerifyDyncast<PDBSymbolTypeVTableShape>(PDB_SymType::VTableShape); 339 VerifyDyncast<PDBSymbolTypeVTable>(PDB_SymType::VTable); 340 VerifyDyncast<PDBSymbolCustom>(PDB_SymType::Custom); 341 VerifyDyncast<PDBSymbolThunk>(PDB_SymType::Thunk); 342 VerifyDyncast<PDBSymbolTypeCustom>(PDB_SymType::CustomType); 343 VerifyDyncast<PDBSymbolTypeManaged>(PDB_SymType::ManagedType); 344 VerifyDyncast<PDBSymbolTypeDimension>(PDB_SymType::Dimension); 345 346 VerifyUnknownDyncasts(); 347 } 348 349 } // end anonymous namespace 350