1 //===-- PythonDataObjectsTests.cpp ------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "gtest/gtest.h"
10 
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
13 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/Path.h"
16 
17 #include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h"
18 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
19 #include "Plugins/SymbolFile/PDB/SymbolFilePDB.h"
20 #include "TestingSupport/TestUtilities.h"
21 #include "lldb/Core/Address.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/ModuleSpec.h"
24 #include "lldb/Host/FileSystem.h"
25 #include "lldb/Host/HostInfo.h"
26 #include "lldb/Symbol/ClangASTContext.h"
27 #include "lldb/Symbol/CompileUnit.h"
28 #include "lldb/Symbol/LineTable.h"
29 #include "lldb/Symbol/SymbolVendor.h"
30 #include "lldb/Utility/ArchSpec.h"
31 #include "lldb/Utility/FileSpec.h"
32 
33 #if defined(_MSC_VER)
34 #include "lldb/Host/windows/windows.h"
35 #include <objbase.h>
36 #endif
37 
38 #include <algorithm>
39 
40 using namespace lldb_private;
41 
42 class SymbolFilePDBTests : public testing::Test {
43 public:
44   void SetUp() override {
45 // Initialize and TearDown the plugin every time, so we get a brand new
46 // AST every time so that modifications to the AST from each test don't
47 // leak into the next test.
48 #if defined(_MSC_VER)
49     ::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
50 #endif
51 
52     FileSystem::Initialize();
53     HostInfo::Initialize();
54     ObjectFilePECOFF::Initialize();
55     SymbolFileDWARF::Initialize();
56     ClangASTContext::Initialize();
57     SymbolFilePDB::Initialize();
58 
59     m_pdb_test_exe = GetInputFilePath("test-pdb.exe");
60     m_types_test_exe = GetInputFilePath("test-pdb-types.exe");
61   }
62 
63   void TearDown() override {
64     SymbolFilePDB::Terminate();
65     ClangASTContext::Initialize();
66     SymbolFileDWARF::Terminate();
67     ObjectFilePECOFF::Terminate();
68     HostInfo::Terminate();
69     FileSystem::Terminate();
70 
71 #if defined(_MSC_VER)
72     ::CoUninitialize();
73 #endif
74   }
75 
76 protected:
77   std::string m_pdb_test_exe;
78   std::string m_types_test_exe;
79 
80   bool FileSpecMatchesAsBaseOrFull(const FileSpec &left,
81                                    const FileSpec &right) const {
82     // If the filenames don't match, the paths can't be equal
83     if (!left.FileEquals(right))
84       return false;
85     // If BOTH have a directory, also compare the directories.
86     if (left.GetDirectory() && right.GetDirectory())
87       return left.DirectoryEquals(right);
88 
89     // If one has a directory but not the other, they match.
90     return true;
91   }
92 
93   void VerifyLineEntry(lldb::ModuleSP module, const SymbolContext &sc,
94                        const FileSpec &spec, LineTable &lt, uint32_t line,
95                        lldb::addr_t addr) {
96     LineEntry entry;
97     Address address;
98     EXPECT_TRUE(module->ResolveFileAddress(addr, address));
99 
100     EXPECT_TRUE(lt.FindLineEntryByAddress(address, entry));
101     EXPECT_EQ(line, entry.line);
102     EXPECT_EQ(address, entry.range.GetBaseAddress());
103 
104     EXPECT_TRUE(FileSpecMatchesAsBaseOrFull(spec, entry.file));
105   }
106 
107   bool ContainsCompileUnit(const SymbolContextList &sc_list,
108                            const FileSpec &spec) const {
109     for (size_t i = 0; i < sc_list.GetSize(); ++i) {
110       const SymbolContext &sc = sc_list[i];
111       if (FileSpecMatchesAsBaseOrFull(*sc.comp_unit, spec))
112         return true;
113     }
114     return false;
115   }
116 
117   uint64_t GetGlobalConstantInteger(llvm::pdb::IPDBSession &session,
118                                     llvm::StringRef var) const {
119     auto global = session.getGlobalScope();
120     auto results =
121         global->findChildren(llvm::pdb::PDB_SymType::Data, var,
122                              llvm::pdb::PDB_NameSearchFlags::NS_Default);
123     uint32_t count = results->getChildCount();
124     if (count == 0)
125       return -1;
126 
127     auto item = results->getChildAtIndex(0);
128     auto symbol = llvm::dyn_cast<llvm::pdb::PDBSymbolData>(item.get());
129     if (!symbol)
130       return -1;
131     llvm::pdb::Variant value = symbol->getValue();
132     switch (value.Type) {
133     case llvm::pdb::PDB_VariantType::Int16:
134       return value.Value.Int16;
135     case llvm::pdb::PDB_VariantType::Int32:
136       return value.Value.Int32;
137     case llvm::pdb::PDB_VariantType::UInt16:
138       return value.Value.UInt16;
139     case llvm::pdb::PDB_VariantType::UInt32:
140       return value.Value.UInt32;
141     default:
142       return 0;
143     }
144   }
145 };
146 
147 TEST_F(SymbolFilePDBTests, TestAbilitiesForPDB) {
148   // Test that when we have PDB debug info, SymbolFilePDB is used.
149   FileSpec fspec(m_pdb_test_exe);
150   ArchSpec aspec("i686-pc-windows");
151   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
152 
153   SymbolVendor *plugin = module->GetSymbolVendor();
154   EXPECT_NE(nullptr, plugin);
155   SymbolFile *symfile = plugin->GetSymbolFile();
156   EXPECT_NE(nullptr, symfile);
157   EXPECT_EQ(symfile->GetPluginName(), SymbolFilePDB::GetPluginNameStatic());
158 
159   uint32_t expected_abilities = SymbolFile::kAllAbilities;
160   EXPECT_EQ(expected_abilities, symfile->CalculateAbilities());
161 }
162 
163 TEST_F(SymbolFilePDBTests, TestResolveSymbolContextBasename) {
164   // Test that attempting to call ResolveSymbolContext with only a basename
165   // finds all full paths
166   // with the same basename
167   FileSpec fspec(m_pdb_test_exe);
168   ArchSpec aspec("i686-pc-windows");
169   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
170 
171   SymbolVendor *plugin = module->GetSymbolVendor();
172   EXPECT_NE(nullptr, plugin);
173   SymbolFile *symfile = plugin->GetSymbolFile();
174 
175   FileSpec header_spec("test-pdb.cpp");
176   SymbolContextList sc_list;
177   uint32_t result_count = symfile->ResolveSymbolContext(
178       header_spec, 0, false, lldb::eSymbolContextCompUnit, sc_list);
179   EXPECT_EQ(1u, result_count);
180   EXPECT_TRUE(ContainsCompileUnit(sc_list, header_spec));
181 }
182 
183 TEST_F(SymbolFilePDBTests, TestResolveSymbolContextFullPath) {
184   // Test that attempting to call ResolveSymbolContext with a full path only
185   // finds the one source
186   // file that matches the full path.
187   FileSpec fspec(m_pdb_test_exe);
188   ArchSpec aspec("i686-pc-windows");
189   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
190 
191   SymbolVendor *plugin = module->GetSymbolVendor();
192   EXPECT_NE(nullptr, plugin);
193   SymbolFile *symfile = plugin->GetSymbolFile();
194 
195   FileSpec header_spec(
196       R"spec(D:\src\llvm\tools\lldb\unittests\SymbolFile\PDB\Inputs\test-pdb.cpp)spec");
197   SymbolContextList sc_list;
198   uint32_t result_count = symfile->ResolveSymbolContext(
199       header_spec, 0, false, lldb::eSymbolContextCompUnit, sc_list);
200   EXPECT_GE(1u, result_count);
201   EXPECT_TRUE(ContainsCompileUnit(sc_list, header_spec));
202 }
203 
204 TEST_F(SymbolFilePDBTests, TestLookupOfHeaderFileWithInlines) {
205   // Test that when looking up a header file via ResolveSymbolContext (i.e. a
206   // file that was not by itself
207   // compiled, but only contributes to the combined code of other source files),
208   // a SymbolContext is returned
209   // for each compiland which has line contributions from the requested header.
210   FileSpec fspec(m_pdb_test_exe);
211   ArchSpec aspec("i686-pc-windows");
212   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
213 
214   SymbolVendor *plugin = module->GetSymbolVendor();
215   EXPECT_NE(nullptr, plugin);
216   SymbolFile *symfile = plugin->GetSymbolFile();
217 
218   FileSpec header_specs[] = {FileSpec("test-pdb.h"),
219                              FileSpec("test-pdb-nested.h")};
220   FileSpec main_cpp_spec("test-pdb.cpp");
221   FileSpec alt_cpp_spec("test-pdb-alt.cpp");
222   for (const auto &hspec : header_specs) {
223     SymbolContextList sc_list;
224     uint32_t result_count = symfile->ResolveSymbolContext(
225         hspec, 0, true, lldb::eSymbolContextCompUnit, sc_list);
226     EXPECT_EQ(2u, result_count);
227     EXPECT_TRUE(ContainsCompileUnit(sc_list, main_cpp_spec));
228     EXPECT_TRUE(ContainsCompileUnit(sc_list, alt_cpp_spec));
229   }
230 }
231 
232 TEST_F(SymbolFilePDBTests, TestLookupOfHeaderFileWithNoInlines) {
233   // Test that when looking up a header file via ResolveSymbolContext (i.e. a
234   // file that was not by itself
235   // compiled, but only contributes to the combined code of other source files),
236   // that if check_inlines
237   // is false, no SymbolContexts are returned.
238   FileSpec fspec(m_pdb_test_exe);
239   ArchSpec aspec("i686-pc-windows");
240   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
241 
242   SymbolVendor *plugin = module->GetSymbolVendor();
243   EXPECT_NE(nullptr, plugin);
244   SymbolFile *symfile = plugin->GetSymbolFile();
245 
246   FileSpec header_specs[] = {FileSpec("test-pdb.h"),
247                              FileSpec("test-pdb-nested.h")};
248   for (const auto &hspec : header_specs) {
249     SymbolContextList sc_list;
250     uint32_t result_count = symfile->ResolveSymbolContext(
251         hspec, 0, false, lldb::eSymbolContextCompUnit, sc_list);
252     EXPECT_EQ(0u, result_count);
253   }
254 }
255 
256 TEST_F(SymbolFilePDBTests, TestLineTablesMatchAll) {
257   // Test that when calling ResolveSymbolContext with a line number of 0, all
258   // line entries from
259   // the specified files are returned.
260   FileSpec fspec(m_pdb_test_exe);
261   ArchSpec aspec("i686-pc-windows");
262   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
263 
264   SymbolVendor *plugin = module->GetSymbolVendor();
265   SymbolFile *symfile = plugin->GetSymbolFile();
266 
267   FileSpec source_file("test-pdb.cpp");
268   FileSpec header1("test-pdb.h");
269   FileSpec header2("test-pdb-nested.h");
270   uint32_t cus = symfile->GetNumCompileUnits();
271   EXPECT_EQ(2u, cus);
272 
273   SymbolContextList sc_list;
274   lldb::SymbolContextItem scope =
275       lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry;
276 
277   uint32_t count =
278       symfile->ResolveSymbolContext(source_file, 0, true, scope, sc_list);
279   EXPECT_EQ(1u, count);
280   SymbolContext sc;
281   EXPECT_TRUE(sc_list.GetContextAtIndex(0, sc));
282 
283   LineTable *lt = sc.comp_unit->GetLineTable();
284   EXPECT_NE(nullptr, lt);
285   count = lt->GetSize();
286   // We expect one extra entry for termination (per function)
287   EXPECT_EQ(16u, count);
288 
289   VerifyLineEntry(module, sc, source_file, *lt, 7, 0x401040);
290   VerifyLineEntry(module, sc, source_file, *lt, 8, 0x401043);
291   VerifyLineEntry(module, sc, source_file, *lt, 9, 0x401045);
292 
293   VerifyLineEntry(module, sc, source_file, *lt, 13, 0x401050);
294   VerifyLineEntry(module, sc, source_file, *lt, 14, 0x401054);
295   VerifyLineEntry(module, sc, source_file, *lt, 15, 0x401070);
296 
297   VerifyLineEntry(module, sc, header1, *lt, 9, 0x401090);
298   VerifyLineEntry(module, sc, header1, *lt, 10, 0x401093);
299   VerifyLineEntry(module, sc, header1, *lt, 11, 0x4010a2);
300 
301   VerifyLineEntry(module, sc, header2, *lt, 5, 0x401080);
302   VerifyLineEntry(module, sc, header2, *lt, 6, 0x401083);
303   VerifyLineEntry(module, sc, header2, *lt, 7, 0x401089);
304 }
305 
306 TEST_F(SymbolFilePDBTests, TestLineTablesMatchSpecific) {
307   // Test that when calling ResolveSymbolContext with a specific line number,
308   // only line entries
309   // which match the requested line are returned.
310   FileSpec fspec(m_pdb_test_exe);
311   ArchSpec aspec("i686-pc-windows");
312   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
313 
314   SymbolVendor *plugin = module->GetSymbolVendor();
315   SymbolFile *symfile = plugin->GetSymbolFile();
316 
317   FileSpec source_file("test-pdb.cpp");
318   FileSpec header1("test-pdb.h");
319   FileSpec header2("test-pdb-nested.h");
320   uint32_t cus = symfile->GetNumCompileUnits();
321   EXPECT_EQ(2u, cus);
322 
323   SymbolContextList sc_list;
324   lldb::SymbolContextItem scope =
325       lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry;
326 
327   // First test with line 7, and verify that only line 7 entries are added.
328   uint32_t count =
329       symfile->ResolveSymbolContext(source_file, 7, true, scope, sc_list);
330   EXPECT_EQ(1u, count);
331   SymbolContext sc;
332   EXPECT_TRUE(sc_list.GetContextAtIndex(0, sc));
333 
334   LineTable *lt = sc.comp_unit->GetLineTable();
335   EXPECT_NE(nullptr, lt);
336   count = lt->GetSize();
337   // We expect one extra entry for termination
338   EXPECT_EQ(3u, count);
339 
340   VerifyLineEntry(module, sc, source_file, *lt, 7, 0x401040);
341   VerifyLineEntry(module, sc, header2, *lt, 7, 0x401089);
342 
343   sc_list.Clear();
344   // Then test with line 9, and verify that only line 9 entries are added.
345   count = symfile->ResolveSymbolContext(source_file, 9, true, scope, sc_list);
346   EXPECT_EQ(1u, count);
347   EXPECT_TRUE(sc_list.GetContextAtIndex(0, sc));
348 
349   lt = sc.comp_unit->GetLineTable();
350   EXPECT_NE(nullptr, lt);
351   count = lt->GetSize();
352   // We expect one extra entry for termination
353   EXPECT_EQ(3u, count);
354 
355   VerifyLineEntry(module, sc, source_file, *lt, 9, 0x401045);
356   VerifyLineEntry(module, sc, header1, *lt, 9, 0x401090);
357 }
358 
359 TEST_F(SymbolFilePDBTests, TestSimpleClassTypes) {
360   FileSpec fspec(m_types_test_exe);
361   ArchSpec aspec("i686-pc-windows");
362   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
363 
364   SymbolVendor *plugin = module->GetSymbolVendor();
365   SymbolFilePDB *symfile =
366       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
367   llvm::pdb::IPDBSession &session = symfile->GetPDBSession();
368   llvm::DenseSet<SymbolFile *> searched_files;
369   TypeMap results;
370   EXPECT_EQ(1u, symfile->FindTypes(ConstString("Class"), nullptr, false, 0,
371                                    searched_files, results));
372   EXPECT_EQ(1u, results.GetSize());
373   lldb::TypeSP udt_type = results.GetTypeAtIndex(0);
374   EXPECT_EQ(ConstString("Class"), udt_type->GetName());
375   CompilerType compiler_type = udt_type->GetForwardCompilerType();
376   EXPECT_TRUE(ClangASTContext::IsClassType(compiler_type.GetOpaqueQualType()));
377   EXPECT_EQ(GetGlobalConstantInteger(session, "sizeof_Class"),
378             udt_type->GetByteSize());
379 }
380 
381 TEST_F(SymbolFilePDBTests, TestNestedClassTypes) {
382   FileSpec fspec(m_types_test_exe);
383   ArchSpec aspec("i686-pc-windows");
384   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
385 
386   SymbolVendor *plugin = module->GetSymbolVendor();
387   SymbolFilePDB *symfile =
388       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
389   llvm::pdb::IPDBSession &session = symfile->GetPDBSession();
390   llvm::DenseSet<SymbolFile *> searched_files;
391   TypeMap results;
392 
393   auto clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
394       symfile->GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
395   EXPECT_NE(nullptr, clang_ast_ctx);
396 
397   EXPECT_EQ(1u, symfile->FindTypes(ConstString("Class"), nullptr, false, 0,
398                                    searched_files, results));
399   EXPECT_EQ(1u, results.GetSize());
400 
401   auto Class = results.GetTypeAtIndex(0);
402   EXPECT_TRUE(Class);
403   EXPECT_TRUE(Class->IsValidType());
404 
405   auto ClassCompilerType = Class->GetFullCompilerType();
406   EXPECT_TRUE(ClassCompilerType.IsValid());
407 
408   auto ClassDeclCtx = clang_ast_ctx->GetDeclContextForType(ClassCompilerType);
409   EXPECT_NE(nullptr, ClassDeclCtx);
410 
411   // There are two symbols for nested classes: one belonging to enclosing class
412   // and one is global. We process correctly this case and create the same
413   // compiler type for both, but `FindTypes` may return more than one type
414   // (with the same compiler type) because the symbols have different IDs.
415   auto ClassCompilerDeclCtx = CompilerDeclContext(clang_ast_ctx, ClassDeclCtx);
416   EXPECT_LE(1u, symfile->FindTypes(ConstString("NestedClass"),
417                                    &ClassCompilerDeclCtx, false, 0,
418                                    searched_files, results));
419   EXPECT_LE(1u, results.GetSize());
420 
421   lldb::TypeSP udt_type = results.GetTypeAtIndex(0);
422   EXPECT_EQ(ConstString("NestedClass"), udt_type->GetName());
423 
424   CompilerType compiler_type = udt_type->GetForwardCompilerType();
425   EXPECT_TRUE(ClangASTContext::IsClassType(compiler_type.GetOpaqueQualType()));
426 
427   EXPECT_EQ(GetGlobalConstantInteger(session, "sizeof_NestedClass"),
428             udt_type->GetByteSize());
429 }
430 
431 TEST_F(SymbolFilePDBTests, TestClassInNamespace) {
432   FileSpec fspec(m_types_test_exe);
433   ArchSpec aspec("i686-pc-windows");
434   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
435 
436   SymbolVendor *plugin = module->GetSymbolVendor();
437   SymbolFilePDB *symfile =
438       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
439   llvm::pdb::IPDBSession &session = symfile->GetPDBSession();
440   llvm::DenseSet<SymbolFile *> searched_files;
441   TypeMap results;
442 
443   auto clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
444       symfile->GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
445   EXPECT_NE(nullptr, clang_ast_ctx);
446 
447   auto ast_ctx = clang_ast_ctx->getASTContext();
448   EXPECT_NE(nullptr, ast_ctx);
449 
450   auto tu = ast_ctx->getTranslationUnitDecl();
451   EXPECT_NE(nullptr, tu);
452 
453   symfile->ParseDeclsForContext(CompilerDeclContext(
454       clang_ast_ctx, static_cast<clang::DeclContext *>(tu)));
455 
456   auto ns_namespace = symfile->FindNamespace(ConstString("NS"), nullptr);
457   EXPECT_TRUE(ns_namespace.IsValid());
458 
459   EXPECT_EQ(1u, symfile->FindTypes(ConstString("NSClass"), &ns_namespace, false,
460                                    0, searched_files, results));
461   EXPECT_EQ(1u, results.GetSize());
462 
463   lldb::TypeSP udt_type = results.GetTypeAtIndex(0);
464   EXPECT_EQ(ConstString("NSClass"), udt_type->GetName());
465 
466   CompilerType compiler_type = udt_type->GetForwardCompilerType();
467   EXPECT_TRUE(ClangASTContext::IsClassType(compiler_type.GetOpaqueQualType()));
468 
469   EXPECT_EQ(GetGlobalConstantInteger(session, "sizeof_NSClass"),
470             udt_type->GetByteSize());
471 }
472 
473 TEST_F(SymbolFilePDBTests, TestEnumTypes) {
474   FileSpec fspec(m_types_test_exe);
475   ArchSpec aspec("i686-pc-windows");
476   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
477 
478   SymbolVendor *plugin = module->GetSymbolVendor();
479   SymbolFilePDB *symfile =
480       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
481   llvm::pdb::IPDBSession &session = symfile->GetPDBSession();
482   llvm::DenseSet<SymbolFile *> searched_files;
483   const char *EnumsToCheck[] = {"Enum", "ShortEnum"};
484   for (auto Enum : EnumsToCheck) {
485     TypeMap results;
486     EXPECT_EQ(1u, symfile->FindTypes(ConstString(Enum), nullptr, false, 0,
487                                      searched_files, results));
488     EXPECT_EQ(1u, results.GetSize());
489     lldb::TypeSP enum_type = results.GetTypeAtIndex(0);
490     EXPECT_EQ(ConstString(Enum), enum_type->GetName());
491     CompilerType compiler_type = enum_type->GetFullCompilerType();
492     EXPECT_TRUE(ClangASTContext::IsEnumType(compiler_type.GetOpaqueQualType()));
493     clang::EnumDecl *enum_decl = ClangASTContext::GetAsEnumDecl(compiler_type);
494     EXPECT_NE(nullptr, enum_decl);
495     EXPECT_EQ(2, std::distance(enum_decl->enumerator_begin(),
496                                enum_decl->enumerator_end()));
497 
498     std::string sizeof_var = "sizeof_";
499     sizeof_var.append(Enum);
500     EXPECT_EQ(GetGlobalConstantInteger(session, sizeof_var),
501               enum_type->GetByteSize());
502   }
503 }
504 
505 TEST_F(SymbolFilePDBTests, TestArrayTypes) {
506   // In order to get this test working, we need to support lookup by symbol
507   // name.  Because array
508   // types themselves do not have names, only the symbols have names (i.e. the
509   // name of the array).
510 }
511 
512 TEST_F(SymbolFilePDBTests, TestFunctionTypes) {
513   // In order to get this test working, we need to support lookup by symbol
514   // name.  Because array
515   // types themselves do not have names, only the symbols have names (i.e. the
516   // name of the array).
517 }
518 
519 TEST_F(SymbolFilePDBTests, TestTypedefs) {
520   FileSpec fspec(m_types_test_exe);
521   ArchSpec aspec("i686-pc-windows");
522   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
523 
524   SymbolVendor *plugin = module->GetSymbolVendor();
525   SymbolFilePDB *symfile =
526       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
527   llvm::pdb::IPDBSession &session = symfile->GetPDBSession();
528   llvm::DenseSet<SymbolFile *> searched_files;
529   TypeMap results;
530 
531   const char *TypedefsToCheck[] = {"ClassTypedef", "NSClassTypedef",
532                                    "FuncPointerTypedef",
533                                    "VariadicFuncPointerTypedef"};
534   for (auto Typedef : TypedefsToCheck) {
535     TypeMap results;
536     EXPECT_EQ(1u, symfile->FindTypes(ConstString(Typedef), nullptr, false, 0,
537                                      searched_files, results));
538     EXPECT_EQ(1u, results.GetSize());
539     lldb::TypeSP typedef_type = results.GetTypeAtIndex(0);
540     EXPECT_EQ(ConstString(Typedef), typedef_type->GetName());
541     CompilerType compiler_type = typedef_type->GetFullCompilerType();
542     ClangASTContext *clang_type_system =
543         llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
544     EXPECT_TRUE(
545         clang_type_system->IsTypedefType(compiler_type.GetOpaqueQualType()));
546 
547     std::string sizeof_var = "sizeof_";
548     sizeof_var.append(Typedef);
549     EXPECT_EQ(GetGlobalConstantInteger(session, sizeof_var),
550               typedef_type->GetByteSize());
551   }
552 }
553 
554 TEST_F(SymbolFilePDBTests, TestRegexNameMatch) {
555   FileSpec fspec(m_types_test_exe);
556   ArchSpec aspec("i686-pc-windows");
557   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
558 
559   SymbolVendor *plugin = module->GetSymbolVendor();
560   SymbolFilePDB *symfile =
561       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
562   TypeMap results;
563 
564   symfile->FindTypesByRegex(RegularExpression(".*"), 0, results);
565   EXPECT_GT(results.GetSize(), 1u);
566 
567   // We expect no exception thrown if the given regex can't be compiled
568   results.Clear();
569   symfile->FindTypesByRegex(RegularExpression("**"), 0, results);
570   EXPECT_EQ(0u, results.GetSize());
571 }
572 
573 TEST_F(SymbolFilePDBTests, TestMaxMatches) {
574   FileSpec fspec(m_types_test_exe);
575   ArchSpec aspec("i686-pc-windows");
576   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
577 
578   SymbolVendor *plugin = module->GetSymbolVendor();
579   SymbolFilePDB *symfile =
580       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
581   llvm::DenseSet<SymbolFile *> searched_files;
582   TypeMap results;
583   const ConstString name("ClassTypedef");
584   uint32_t num_results =
585       symfile->FindTypes(name, nullptr, false, 0, searched_files, results);
586   // Try to limit ourselves from 1 to 10 results, otherwise we could be doing
587   // this thousands of times.
588   // The idea is just to make sure that for a variety of values, the number of
589   // limited results always
590   // comes out to the number we are expecting.
591   uint32_t iterations = std::min(num_results, 10u);
592   for (uint32_t i = 1; i <= iterations; ++i) {
593     uint32_t num_limited_results =
594         symfile->FindTypes(name, nullptr, false, i, searched_files, results);
595     EXPECT_EQ(i, num_limited_results);
596     EXPECT_EQ(num_limited_results, results.GetSize());
597   }
598 }
599 
600 TEST_F(SymbolFilePDBTests, TestNullName) {
601   FileSpec fspec(m_types_test_exe);
602   ArchSpec aspec("i686-pc-windows");
603   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
604 
605   SymbolVendor *plugin = module->GetSymbolVendor();
606   SymbolFilePDB *symfile =
607       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
608   llvm::DenseSet<SymbolFile *> searched_files;
609   TypeMap results;
610   uint32_t num_results = symfile->FindTypes(ConstString(), nullptr, false, 0,
611                                             searched_files, results);
612   EXPECT_EQ(0u, num_results);
613   EXPECT_EQ(0u, results.GetSize());
614 }
615 
616 TEST_F(SymbolFilePDBTests, TestFindSymbolsWithNameAndType) {
617   FileSpec fspec(m_pdb_test_exe.c_str());
618   ArchSpec aspec("i686-pc-windows");
619   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
620 
621   SymbolContextList sc_list;
622   EXPECT_EQ(1u,
623             module->FindSymbolsWithNameAndType(ConstString("?foo@@YAHH@Z"),
624                                                lldb::eSymbolTypeAny, sc_list));
625   EXPECT_EQ(1u, sc_list.GetSize());
626 
627   SymbolContext sc;
628   EXPECT_TRUE(sc_list.GetContextAtIndex(0, sc));
629   EXPECT_STREQ("int foo(int)",
630                sc.GetFunctionName(Mangled::ePreferDemangled).AsCString());
631 }
632