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