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