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