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