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