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