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