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