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 #include "llvm/Testing/Support/Error.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   llvm::DenseSet<SymbolFile *> searched_files;
370   TypeMap results;
371   EXPECT_EQ(1u, symfile->FindTypes(ConstString("Class"), nullptr, false, 0,
372                                    searched_files, results));
373   EXPECT_EQ(1u, results.GetSize());
374   lldb::TypeSP udt_type = results.GetTypeAtIndex(0);
375   EXPECT_EQ(ConstString("Class"), udt_type->GetName());
376   CompilerType compiler_type = udt_type->GetForwardCompilerType();
377   EXPECT_TRUE(ClangASTContext::IsClassType(compiler_type.GetOpaqueQualType()));
378   EXPECT_EQ(GetGlobalConstantInteger(session, "sizeof_Class"),
379             udt_type->GetByteSize());
380 }
381 
382 TEST_F(SymbolFilePDBTests, TestNestedClassTypes) {
383   FileSpec fspec(m_types_test_exe);
384   ArchSpec aspec("i686-pc-windows");
385   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
386 
387   SymbolVendor *plugin = module->GetSymbolVendor();
388   SymbolFilePDB *symfile =
389       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
390   llvm::pdb::IPDBSession &session = symfile->GetPDBSession();
391   llvm::DenseSet<SymbolFile *> searched_files;
392   TypeMap results;
393 
394   auto clang_ast_ctx_or_err =
395       symfile->GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
396   ASSERT_THAT_EXPECTED(clang_ast_ctx_or_err, llvm::Succeeded());
397 
398   auto clang_ast_ctx =
399       llvm::dyn_cast_or_null<ClangASTContext>(&clang_ast_ctx_or_err.get());
400   EXPECT_NE(nullptr, clang_ast_ctx);
401 
402   EXPECT_EQ(1u, symfile->FindTypes(ConstString("Class"), nullptr, false, 0,
403                                    searched_files, results));
404   EXPECT_EQ(1u, results.GetSize());
405 
406   auto Class = results.GetTypeAtIndex(0);
407   EXPECT_TRUE(Class);
408   EXPECT_TRUE(Class->IsValidType());
409 
410   auto ClassCompilerType = Class->GetFullCompilerType();
411   EXPECT_TRUE(ClassCompilerType.IsValid());
412 
413   auto ClassDeclCtx = clang_ast_ctx->GetDeclContextForType(ClassCompilerType);
414   EXPECT_NE(nullptr, ClassDeclCtx);
415 
416   // There are two symbols for nested classes: one belonging to enclosing class
417   // and one is global. We process correctly this case and create the same
418   // compiler type for both, but `FindTypes` may return more than one type
419   // (with the same compiler type) because the symbols have different IDs.
420   auto ClassCompilerDeclCtx = CompilerDeclContext(clang_ast_ctx, ClassDeclCtx);
421   EXPECT_LE(1u, symfile->FindTypes(ConstString("NestedClass"),
422                                    &ClassCompilerDeclCtx, false, 0,
423                                    searched_files, results));
424   EXPECT_LE(1u, results.GetSize());
425 
426   lldb::TypeSP udt_type = results.GetTypeAtIndex(0);
427   EXPECT_EQ(ConstString("NestedClass"), udt_type->GetName());
428 
429   CompilerType compiler_type = udt_type->GetForwardCompilerType();
430   EXPECT_TRUE(ClangASTContext::IsClassType(compiler_type.GetOpaqueQualType()));
431 
432   EXPECT_EQ(GetGlobalConstantInteger(session, "sizeof_NestedClass"),
433             udt_type->GetByteSize());
434 }
435 
436 TEST_F(SymbolFilePDBTests, TestClassInNamespace) {
437   FileSpec fspec(m_types_test_exe);
438   ArchSpec aspec("i686-pc-windows");
439   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
440 
441   SymbolVendor *plugin = module->GetSymbolVendor();
442   SymbolFilePDB *symfile =
443       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
444   llvm::pdb::IPDBSession &session = symfile->GetPDBSession();
445   llvm::DenseSet<SymbolFile *> searched_files;
446   TypeMap results;
447 
448   auto clang_ast_ctx_or_err =
449       symfile->GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
450   ASSERT_THAT_EXPECTED(clang_ast_ctx_or_err, llvm::Succeeded());
451 
452   auto clang_ast_ctx =
453       llvm::dyn_cast_or_null<ClangASTContext>(&clang_ast_ctx_or_err.get());
454   EXPECT_NE(nullptr, clang_ast_ctx);
455 
456   auto ast_ctx = clang_ast_ctx->getASTContext();
457   EXPECT_NE(nullptr, ast_ctx);
458 
459   auto tu = ast_ctx->getTranslationUnitDecl();
460   EXPECT_NE(nullptr, tu);
461 
462   symfile->ParseDeclsForContext(CompilerDeclContext(
463       clang_ast_ctx, static_cast<clang::DeclContext *>(tu)));
464 
465   auto ns_namespace = symfile->FindNamespace(ConstString("NS"), nullptr);
466   EXPECT_TRUE(ns_namespace.IsValid());
467 
468   EXPECT_EQ(1u, symfile->FindTypes(ConstString("NSClass"), &ns_namespace, false,
469                                    0, searched_files, results));
470   EXPECT_EQ(1u, results.GetSize());
471 
472   lldb::TypeSP udt_type = results.GetTypeAtIndex(0);
473   EXPECT_EQ(ConstString("NSClass"), udt_type->GetName());
474 
475   CompilerType compiler_type = udt_type->GetForwardCompilerType();
476   EXPECT_TRUE(ClangASTContext::IsClassType(compiler_type.GetOpaqueQualType()));
477 
478   EXPECT_EQ(GetGlobalConstantInteger(session, "sizeof_NSClass"),
479             udt_type->GetByteSize());
480 }
481 
482 TEST_F(SymbolFilePDBTests, TestEnumTypes) {
483   FileSpec fspec(m_types_test_exe);
484   ArchSpec aspec("i686-pc-windows");
485   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
486 
487   SymbolVendor *plugin = module->GetSymbolVendor();
488   SymbolFilePDB *symfile =
489       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
490   llvm::pdb::IPDBSession &session = symfile->GetPDBSession();
491   llvm::DenseSet<SymbolFile *> searched_files;
492   const char *EnumsToCheck[] = {"Enum", "ShortEnum"};
493   for (auto Enum : EnumsToCheck) {
494     TypeMap results;
495     EXPECT_EQ(1u, symfile->FindTypes(ConstString(Enum), nullptr, false, 0,
496                                      searched_files, results));
497     EXPECT_EQ(1u, results.GetSize());
498     lldb::TypeSP enum_type = results.GetTypeAtIndex(0);
499     EXPECT_EQ(ConstString(Enum), enum_type->GetName());
500     CompilerType compiler_type = enum_type->GetFullCompilerType();
501     EXPECT_TRUE(ClangASTContext::IsEnumType(compiler_type.GetOpaqueQualType()));
502     clang::EnumDecl *enum_decl = ClangASTContext::GetAsEnumDecl(compiler_type);
503     EXPECT_NE(nullptr, enum_decl);
504     EXPECT_EQ(2, std::distance(enum_decl->enumerator_begin(),
505                                enum_decl->enumerator_end()));
506 
507     std::string sizeof_var = "sizeof_";
508     sizeof_var.append(Enum);
509     EXPECT_EQ(GetGlobalConstantInteger(session, sizeof_var),
510               enum_type->GetByteSize());
511   }
512 }
513 
514 TEST_F(SymbolFilePDBTests, TestArrayTypes) {
515   // In order to get this test working, we need to support lookup by symbol
516   // name.  Because array
517   // types themselves do not have names, only the symbols have names (i.e. the
518   // name of the array).
519 }
520 
521 TEST_F(SymbolFilePDBTests, TestFunctionTypes) {
522   // In order to get this test working, we need to support lookup by symbol
523   // name.  Because array
524   // types themselves do not have names, only the symbols have names (i.e. the
525   // name of the array).
526 }
527 
528 TEST_F(SymbolFilePDBTests, TestTypedefs) {
529   FileSpec fspec(m_types_test_exe);
530   ArchSpec aspec("i686-pc-windows");
531   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
532 
533   SymbolVendor *plugin = module->GetSymbolVendor();
534   SymbolFilePDB *symfile =
535       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
536   llvm::pdb::IPDBSession &session = symfile->GetPDBSession();
537   llvm::DenseSet<SymbolFile *> searched_files;
538   TypeMap results;
539 
540   const char *TypedefsToCheck[] = {"ClassTypedef", "NSClassTypedef",
541                                    "FuncPointerTypedef",
542                                    "VariadicFuncPointerTypedef"};
543   for (auto Typedef : TypedefsToCheck) {
544     TypeMap results;
545     EXPECT_EQ(1u, symfile->FindTypes(ConstString(Typedef), nullptr, false, 0,
546                                      searched_files, results));
547     EXPECT_EQ(1u, results.GetSize());
548     lldb::TypeSP typedef_type = results.GetTypeAtIndex(0);
549     EXPECT_EQ(ConstString(Typedef), typedef_type->GetName());
550     CompilerType compiler_type = typedef_type->GetFullCompilerType();
551     ClangASTContext *clang_type_system =
552         llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem());
553     EXPECT_TRUE(
554         clang_type_system->IsTypedefType(compiler_type.GetOpaqueQualType()));
555 
556     std::string sizeof_var = "sizeof_";
557     sizeof_var.append(Typedef);
558     EXPECT_EQ(GetGlobalConstantInteger(session, sizeof_var),
559               typedef_type->GetByteSize());
560   }
561 }
562 
563 TEST_F(SymbolFilePDBTests, TestRegexNameMatch) {
564   FileSpec fspec(m_types_test_exe);
565   ArchSpec aspec("i686-pc-windows");
566   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
567 
568   SymbolVendor *plugin = module->GetSymbolVendor();
569   SymbolFilePDB *symfile =
570       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
571   TypeMap results;
572 
573   symfile->FindTypesByRegex(RegularExpression(".*"), 0, results);
574   EXPECT_GT(results.GetSize(), 1u);
575 
576   // We expect no exception thrown if the given regex can't be compiled
577   results.Clear();
578   symfile->FindTypesByRegex(RegularExpression("**"), 0, results);
579   EXPECT_EQ(0u, results.GetSize());
580 }
581 
582 TEST_F(SymbolFilePDBTests, TestMaxMatches) {
583   FileSpec fspec(m_types_test_exe);
584   ArchSpec aspec("i686-pc-windows");
585   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
586 
587   SymbolVendor *plugin = module->GetSymbolVendor();
588   SymbolFilePDB *symfile =
589       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
590   llvm::DenseSet<SymbolFile *> searched_files;
591   TypeMap results;
592   const ConstString name("ClassTypedef");
593   uint32_t num_results =
594       symfile->FindTypes(name, nullptr, false, 0, searched_files, results);
595   // Try to limit ourselves from 1 to 10 results, otherwise we could be doing
596   // this thousands of times.
597   // The idea is just to make sure that for a variety of values, the number of
598   // limited results always
599   // comes out to the number we are expecting.
600   uint32_t iterations = std::min(num_results, 10u);
601   for (uint32_t i = 1; i <= iterations; ++i) {
602     uint32_t num_limited_results =
603         symfile->FindTypes(name, nullptr, false, i, searched_files, results);
604     EXPECT_EQ(i, num_limited_results);
605     EXPECT_EQ(num_limited_results, results.GetSize());
606   }
607 }
608 
609 TEST_F(SymbolFilePDBTests, TestNullName) {
610   FileSpec fspec(m_types_test_exe);
611   ArchSpec aspec("i686-pc-windows");
612   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
613 
614   SymbolVendor *plugin = module->GetSymbolVendor();
615   SymbolFilePDB *symfile =
616       static_cast<SymbolFilePDB *>(plugin->GetSymbolFile());
617   llvm::DenseSet<SymbolFile *> searched_files;
618   TypeMap results;
619   uint32_t num_results = symfile->FindTypes(ConstString(), nullptr, false, 0,
620                                             searched_files, results);
621   EXPECT_EQ(0u, num_results);
622   EXPECT_EQ(0u, results.GetSize());
623 }
624 
625 TEST_F(SymbolFilePDBTests, TestFindSymbolsWithNameAndType) {
626   FileSpec fspec(m_pdb_test_exe.c_str());
627   ArchSpec aspec("i686-pc-windows");
628   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
629 
630   SymbolContextList sc_list;
631   EXPECT_EQ(1u,
632             module->FindSymbolsWithNameAndType(ConstString("?foo@@YAHH@Z"),
633                                                lldb::eSymbolTypeAny, sc_list));
634   EXPECT_EQ(1u, sc_list.GetSize());
635 
636   SymbolContext sc;
637   EXPECT_TRUE(sc_list.GetContextAtIndex(0, sc));
638   EXPECT_STREQ("int foo(int)",
639                sc.GetFunctionName(Mangled::ePreferDemangled).AsCString());
640 }
641