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