1 //===-- SymbolFilePDB.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 "SymbolFilePDB.h"
11 
12 #include "clang/Lex/Lexer.h"
13 
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/PluginManager.h"
16 #include "lldb/Symbol/ClangASTContext.h"
17 #include "lldb/Symbol/CompileUnit.h"
18 #include "lldb/Symbol/LineTable.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Symbol/SymbolVendor.h"
22 #include "lldb/Symbol/TypeList.h"
23 #include "lldb/Symbol/TypeMap.h"
24 #include "lldb/Symbol/Variable.h"
25 #include "lldb/Utility/RegularExpression.h"
26 
27 #include "llvm/DebugInfo/PDB/GenericError.h"
28 #include "llvm/DebugInfo/PDB/IPDBDataStream.h"
29 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
30 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
31 #include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
32 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
33 #include "llvm/DebugInfo/PDB/IPDBTable.h"
34 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
35 #include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
36 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
37 #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
38 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
39 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
40 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
41 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
42 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
43 #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
44 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
45 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
46 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
47 
48 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
49 #include "Plugins/SymbolFile/PDB/PDBASTParser.h"
50 
51 #include <regex>
52 
53 using namespace lldb;
54 using namespace lldb_private;
55 using namespace llvm::pdb;
56 
57 namespace {
58 lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
59   switch (lang) {
60   case PDB_Lang::Cpp:
61     return lldb::LanguageType::eLanguageTypeC_plus_plus;
62   case PDB_Lang::C:
63     return lldb::LanguageType::eLanguageTypeC;
64   default:
65     return lldb::LanguageType::eLanguageTypeUnknown;
66   }
67 }
68 
69 bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
70                    uint32_t addr_length) {
71   return ((requested_line == 0 || actual_line == requested_line) &&
72           addr_length > 0);
73 }
74 } // namespace
75 
76 void SymbolFilePDB::Initialize() {
77   PluginManager::RegisterPlugin(GetPluginNameStatic(),
78                                 GetPluginDescriptionStatic(), CreateInstance,
79                                 DebuggerInitialize);
80 }
81 
82 void SymbolFilePDB::Terminate() {
83   PluginManager::UnregisterPlugin(CreateInstance);
84 }
85 
86 void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
87 
88 lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
89   static ConstString g_name("pdb");
90   return g_name;
91 }
92 
93 const char *SymbolFilePDB::GetPluginDescriptionStatic() {
94   return "Microsoft PDB debug symbol file reader.";
95 }
96 
97 lldb_private::SymbolFile *
98 SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
99   return new SymbolFilePDB(obj_file);
100 }
101 
102 SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
103     : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
104       m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
105 
106 SymbolFilePDB::~SymbolFilePDB() {}
107 
108 uint32_t SymbolFilePDB::CalculateAbilities() {
109   uint32_t abilities = 0;
110   if (!m_obj_file)
111     return 0;
112 
113   if (!m_session_up) {
114     // Lazily load and match the PDB file, but only do this once.
115     std::string exePath = m_obj_file->GetFileSpec().GetPath();
116     auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
117                                 m_session_up);
118     if (error) {
119       llvm::consumeError(std::move(error));
120       auto module_sp = m_obj_file->GetModule();
121       if (!module_sp)
122         return 0;
123       // See if any symbol file is specified through `--symfile` option.
124       FileSpec symfile = module_sp->GetSymbolFileFileSpec();
125       if (!symfile)
126         return 0;
127       error = loadDataForPDB(PDB_ReaderType::DIA,
128                              llvm::StringRef(symfile.GetPath()), m_session_up);
129       if (error) {
130         llvm::consumeError(std::move(error));
131         return 0;
132       }
133     }
134   }
135   if (!m_session_up)
136     return 0;
137 
138   auto enum_tables_up = m_session_up->getEnumTables();
139   if (!enum_tables_up)
140     return 0;
141   while (auto table_up = enum_tables_up->getNext()) {
142     if (table_up->getItemCount() == 0)
143       continue;
144     auto type = table_up->getTableType();
145     switch (type) {
146     case PDB_TableType::Symbols:
147       // This table represents a store of symbols with types listed in
148       // PDBSym_Type
149       abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
150                     LocalVariables | VariableTypes);
151       break;
152     case PDB_TableType::LineNumbers:
153       abilities |= LineTables;
154       break;
155     default:
156       break;
157     }
158   }
159   return abilities;
160 }
161 
162 void SymbolFilePDB::InitializeObject() {
163   lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
164   lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
165   m_session_up->setLoadAddress(obj_load_address);
166   if (!m_global_scope_up)
167     m_global_scope_up = m_session_up->getGlobalScope();
168   lldbassert(m_global_scope_up.get());
169 
170   TypeSystem *type_system =
171       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
172   ClangASTContext *clang_type_system =
173       llvm::dyn_cast_or_null<ClangASTContext>(type_system);
174   lldbassert(clang_type_system);
175   m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
176       type_system, clang_type_system->GetTranslationUnitDecl());
177 }
178 
179 uint32_t SymbolFilePDB::GetNumCompileUnits() {
180   if (m_cached_compile_unit_count == 0) {
181     auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
182     if (!compilands)
183       return 0;
184 
185     // The linker could link *.dll (compiland language = LINK), or import
186     // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
187     // found as a child of the global scope (PDB executable). Usually, such
188     // compilands contain `thunk` symbols in which we are not interested for
189     // now. However we still count them in the compiland list. If we perform
190     // any compiland related activity, like finding symbols through
191     // llvm::pdb::IPDBSession methods, such compilands will all be searched
192     // automatically no matter whether we include them or not.
193     m_cached_compile_unit_count = compilands->getChildCount();
194 
195     // The linker can inject an additional "dummy" compilation unit into the
196     // PDB. Ignore this special compile unit for our purposes, if it is there.
197     // It is always the last one.
198     auto last_compiland_up =
199         compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
200     lldbassert(last_compiland_up.get());
201     std::string name = last_compiland_up->getName();
202     if (name == "* Linker *")
203       --m_cached_compile_unit_count;
204   }
205   return m_cached_compile_unit_count;
206 }
207 
208 void SymbolFilePDB::GetCompileUnitIndex(
209     const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
210   auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
211   if (!results_up)
212     return;
213   auto uid = pdb_compiland.getSymIndexId();
214   for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
215     auto compiland_up = results_up->getChildAtIndex(cu_idx);
216     if (!compiland_up)
217       continue;
218     if (compiland_up->getSymIndexId() == uid) {
219       index = cu_idx;
220       return;
221     }
222   }
223   index = UINT32_MAX;
224   return;
225 }
226 
227 std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
228 SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
229   return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
230 }
231 
232 lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
233   if (index >= GetNumCompileUnits())
234     return CompUnitSP();
235 
236   // Assuming we always retrieve same compilands listed in same order through
237   // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
238   // compile unit makes no sense.
239   auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
240   if (!results)
241     return CompUnitSP();
242   auto compiland_up = results->getChildAtIndex(index);
243   if (!compiland_up)
244     return CompUnitSP();
245   return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
246 }
247 
248 lldb::LanguageType
249 SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) {
250   // What fields should I expect to be filled out on the SymbolContext?  Is it
251   // safe to assume that `sc.comp_unit` is valid?
252   if (!sc.comp_unit)
253     return lldb::eLanguageTypeUnknown;
254 
255   auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
256   if (!compiland_up)
257     return lldb::eLanguageTypeUnknown;
258   auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
259   if (!details)
260     return lldb::eLanguageTypeUnknown;
261   return TranslateLanguage(details->getLanguage());
262 }
263 
264 lldb_private::Function *SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(
265     const PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc) {
266   lldbassert(sc.comp_unit && sc.module_sp.get());
267 
268   auto file_vm_addr = pdb_func.getVirtualAddress();
269   if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
270     return nullptr;
271 
272   auto func_length = pdb_func.getLength();
273   AddressRange func_range =
274       AddressRange(file_vm_addr, func_length, sc.module_sp->GetSectionList());
275   if (!func_range.GetBaseAddress().IsValid())
276     return nullptr;
277 
278   lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
279   if (!func_type)
280     return nullptr;
281 
282   user_id_t func_type_uid = pdb_func.getSignatureId();
283 
284   Mangled mangled = GetMangledForPDBFunc(pdb_func);
285 
286   FunctionSP func_sp =
287       std::make_shared<Function>(sc.comp_unit, pdb_func.getSymIndexId(),
288                                  func_type_uid, mangled, func_type, func_range);
289 
290   sc.comp_unit->AddFunction(func_sp);
291   return func_sp.get();
292 }
293 
294 size_t SymbolFilePDB::ParseCompileUnitFunctions(
295     const lldb_private::SymbolContext &sc) {
296   lldbassert(sc.comp_unit);
297   size_t func_added = 0;
298   auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
299   if (!compiland_up)
300     return 0;
301   auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
302   if (!results_up)
303     return 0;
304   while (auto pdb_func_up = results_up->getNext()) {
305     auto func_sp =
306         sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId());
307     if (!func_sp) {
308       if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc))
309         ++func_added;
310     }
311   }
312   return func_added;
313 }
314 
315 bool SymbolFilePDB::ParseCompileUnitLineTable(
316     const lldb_private::SymbolContext &sc) {
317   lldbassert(sc.comp_unit);
318   if (sc.comp_unit->GetLineTable())
319     return true;
320   return ParseCompileUnitLineTable(sc, 0);
321 }
322 
323 bool SymbolFilePDB::ParseCompileUnitDebugMacros(
324     const lldb_private::SymbolContext &sc) {
325   // PDB doesn't contain information about macros
326   return false;
327 }
328 
329 bool SymbolFilePDB::ParseCompileUnitSupportFiles(
330     const lldb_private::SymbolContext &sc,
331     lldb_private::FileSpecList &support_files) {
332   lldbassert(sc.comp_unit);
333 
334   // In theory this is unnecessary work for us, because all of this information
335   // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
336   // second time seems like a waste.  Unfortunately, there's no good way around
337   // this short of a moderate refactor since SymbolVendor depends on being able
338   // to cache this list.
339   auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
340   if (!compiland_up)
341     return false;
342   auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
343   if (!files || files->getChildCount() == 0)
344     return false;
345 
346   while (auto file = files->getNext()) {
347     FileSpec spec(file->getFileName(), false, FileSpec::Style::windows);
348     support_files.AppendIfUnique(spec);
349   }
350   return true;
351 }
352 
353 bool SymbolFilePDB::ParseImportedModules(
354     const lldb_private::SymbolContext &sc,
355     std::vector<lldb_private::ConstString> &imported_modules) {
356   // PDB does not yet support module debug info
357   return false;
358 }
359 
360 static size_t ParseFunctionBlocksForPDBSymbol(
361     const lldb_private::SymbolContext &sc, uint64_t func_file_vm_addr,
362     const llvm::pdb::PDBSymbol *pdb_symbol, lldb_private::Block *parent_block,
363     bool is_top_parent) {
364   assert(pdb_symbol && parent_block);
365 
366   size_t num_added = 0;
367   switch (pdb_symbol->getSymTag()) {
368   case PDB_SymType::Block:
369   case PDB_SymType::Function: {
370     Block *block = nullptr;
371     auto &raw_sym = pdb_symbol->getRawSymbol();
372     if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
373       if (pdb_func->hasNoInlineAttribute())
374         break;
375       if (is_top_parent)
376         block = parent_block;
377       else
378         break;
379     } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
380       auto uid = pdb_symbol->getSymIndexId();
381       if (parent_block->FindBlockByID(uid))
382         break;
383       if (raw_sym.getVirtualAddress() < func_file_vm_addr)
384         break;
385 
386       auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
387       parent_block->AddChild(block_sp);
388       block = block_sp.get();
389     } else
390       llvm_unreachable("Unexpected PDB symbol!");
391 
392     block->AddRange(Block::Range(
393         raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
394     block->FinalizeRanges();
395     ++num_added;
396 
397     auto results_up = pdb_symbol->findAllChildren();
398     if (!results_up)
399       break;
400     while (auto symbol_up = results_up->getNext()) {
401       num_added += ParseFunctionBlocksForPDBSymbol(
402           sc, func_file_vm_addr, symbol_up.get(), block, false);
403     }
404   } break;
405   default:
406     break;
407   }
408   return num_added;
409 }
410 
411 size_t
412 SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
413   lldbassert(sc.comp_unit && sc.function);
414   size_t num_added = 0;
415   auto uid = sc.function->GetID();
416   auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
417   if (!pdb_func_up)
418     return 0;
419   Block &parent_block = sc.function->GetBlock(false);
420   num_added =
421       ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(),
422                                       pdb_func_up.get(), &parent_block, true);
423   return num_added;
424 }
425 
426 size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
427   lldbassert(sc.module_sp.get());
428   if (!sc.comp_unit)
429     return 0;
430 
431   size_t num_added = 0;
432   auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
433   if (!compiland)
434     return 0;
435 
436   auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
437     std::unique_ptr<IPDBEnumSymbols> results;
438     PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
439                                     PDB_SymType::UDT};
440     for (auto tag : tags_to_search) {
441       results = raw_sym.findAllChildren(tag);
442       if (!results || results->getChildCount() == 0)
443         continue;
444       while (auto symbol = results->getNext()) {
445         switch (symbol->getSymTag()) {
446         case PDB_SymType::Enum:
447         case PDB_SymType::UDT:
448         case PDB_SymType::Typedef:
449           break;
450         default:
451           continue;
452         }
453 
454         // This should cause the type to get cached and stored in the `m_types`
455         // lookup.
456         if (!ResolveTypeUID(symbol->getSymIndexId()))
457           continue;
458 
459         ++num_added;
460       }
461     }
462   };
463 
464   if (sc.function) {
465     auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
466         sc.function->GetID());
467     if (!pdb_func)
468       return 0;
469     ParseTypesByTagFn(*pdb_func);
470   } else {
471     ParseTypesByTagFn(*compiland);
472 
473     // Also parse global types particularly coming from this compiland.
474     // Unfortunately, PDB has no compiland information for each global type. We
475     // have to parse them all. But ensure we only do this once.
476     static bool parse_all_global_types = false;
477     if (!parse_all_global_types) {
478       ParseTypesByTagFn(*m_global_scope_up);
479       parse_all_global_types = true;
480     }
481   }
482   return num_added;
483 }
484 
485 size_t
486 SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
487   if (!sc.comp_unit)
488     return 0;
489 
490   size_t num_added = 0;
491   if (sc.function) {
492     auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
493         sc.function->GetID());
494     if (!pdb_func)
495       return 0;
496 
497     num_added += ParseVariables(sc, *pdb_func);
498     sc.function->GetBlock(false).SetDidParseVariables(true, true);
499   } else if (sc.comp_unit) {
500     auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
501     if (!compiland)
502       return 0;
503 
504     if (sc.comp_unit->GetVariableList(false))
505       return 0;
506 
507     auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
508     if (results && results->getChildCount()) {
509       while (auto result = results->getNext()) {
510         auto cu_id = result->getCompilandId();
511         // FIXME: We are not able to determine variable's compile unit.
512         if (cu_id == 0)
513           continue;
514 
515         if (cu_id == sc.comp_unit->GetID())
516           num_added += ParseVariables(sc, *result);
517       }
518     }
519 
520     // FIXME: A `file static` or `global constant` variable appears both in
521     // compiland's children and global scope's children with unexpectedly
522     // different symbol's Id making it ambiguous.
523 
524     // FIXME: 'local constant', for example, const char var[] = "abc", declared
525     // in a function scope, can't be found in PDB.
526 
527     // Parse variables in this compiland.
528     num_added += ParseVariables(sc, *compiland);
529   }
530 
531   return num_added;
532 }
533 
534 lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
535   auto find_result = m_types.find(type_uid);
536   if (find_result != m_types.end())
537     return find_result->second.get();
538 
539   TypeSystem *type_system =
540       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
541   ClangASTContext *clang_type_system =
542       llvm::dyn_cast_or_null<ClangASTContext>(type_system);
543   if (!clang_type_system)
544     return nullptr;
545   PDBASTParser *pdb =
546       llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser());
547   if (!pdb)
548     return nullptr;
549 
550   auto pdb_type = m_session_up->getSymbolById(type_uid);
551   if (pdb_type == nullptr)
552     return nullptr;
553 
554   lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
555   if (result) {
556     m_types.insert(std::make_pair(type_uid, result));
557     auto type_list = GetTypeList();
558     if (type_list)
559       type_list->Insert(result);
560   }
561   return result.get();
562 }
563 
564 bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
565   // TODO: Implement this
566   return false;
567 }
568 
569 lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
570   return lldb_private::CompilerDecl();
571 }
572 
573 lldb_private::CompilerDeclContext
574 SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
575   // PDB always uses the translation unit decl context for everything.  We can
576   // improve this later but it's not easy because PDB doesn't provide a high
577   // enough level of type fidelity in this area.
578   return *m_tu_decl_ctx_up;
579 }
580 
581 lldb_private::CompilerDeclContext
582 SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
583   return *m_tu_decl_ctx_up;
584 }
585 
586 void SymbolFilePDB::ParseDeclsForContext(
587     lldb_private::CompilerDeclContext decl_ctx) {}
588 
589 uint32_t
590 SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
591                                     uint32_t resolve_scope,
592                                     lldb_private::SymbolContext &sc) {
593   uint32_t resolved_flags = 0;
594   if (resolve_scope & eSymbolContextCompUnit ||
595       resolve_scope & eSymbolContextVariable ||
596       resolve_scope & eSymbolContextFunction ||
597       resolve_scope & eSymbolContextBlock ||
598       resolve_scope & eSymbolContextLineEntry) {
599     addr_t file_vm_addr = so_addr.GetFileAddress();
600     auto symbol_up =
601         m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::None);
602     if (!symbol_up)
603       return 0;
604 
605     auto cu_sp = GetCompileUnitContainsAddress(so_addr);
606     if (!cu_sp) {
607       if (resolved_flags | eSymbolContextVariable) {
608         // TODO: Resolve variables
609       }
610       return 0;
611     }
612     sc.comp_unit = cu_sp.get();
613     resolved_flags |= eSymbolContextCompUnit;
614     lldbassert(sc.module_sp == cu_sp->GetModule());
615 
616     switch (symbol_up->getSymTag()) {
617     case PDB_SymType::Function:
618       if (resolve_scope & eSymbolContextFunction) {
619         auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
620         assert(pdb_func);
621         auto func_uid = pdb_func->getSymIndexId();
622         sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
623         if (sc.function == nullptr)
624           sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
625         if (sc.function) {
626           resolved_flags |= eSymbolContextFunction;
627           if (resolve_scope & eSymbolContextBlock) {
628             Block &block = sc.function->GetBlock(true);
629             sc.block = block.FindBlockByID(sc.function->GetID());
630             if (sc.block)
631               resolved_flags |= eSymbolContextBlock;
632           }
633         }
634       }
635       break;
636     default:
637       break;
638     }
639 
640     if (resolve_scope & eSymbolContextLineEntry) {
641       if (auto *line_table = sc.comp_unit->GetLineTable()) {
642         Address addr(so_addr);
643         if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
644           resolved_flags |= eSymbolContextLineEntry;
645       }
646     }
647   }
648   return resolved_flags;
649 }
650 
651 uint32_t SymbolFilePDB::ResolveSymbolContext(
652     const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
653     uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) {
654   const size_t old_size = sc_list.GetSize();
655   if (resolve_scope & lldb::eSymbolContextCompUnit) {
656     // Locate all compilation units with line numbers referencing the specified
657     // file.  For example, if `file_spec` is <vector>, then this should return
658     // all source files and header files that reference <vector>, either
659     // directly or indirectly.
660     auto compilands = m_session_up->findCompilandsForSourceFile(
661         file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
662 
663     if (!compilands)
664       return 0;
665 
666     // For each one, either find its previously parsed data or parse it afresh
667     // and add it to the symbol context list.
668     while (auto compiland = compilands->getNext()) {
669       // If we're not checking inlines, then don't add line information for
670       // this file unless the FileSpec matches. For inline functions, we don't
671       // have to match the FileSpec since they could be defined in headers
672       // other than file specified in FileSpec.
673       if (!check_inlines) {
674         std::string source_file = compiland->getSourceFileFullPath();
675         if (source_file.empty())
676           continue;
677         FileSpec this_spec(source_file, false, FileSpec::Style::windows);
678         bool need_full_match = !file_spec.GetDirectory().IsEmpty();
679         if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
680           continue;
681       }
682 
683       SymbolContext sc;
684       auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
685       if (!cu)
686         continue;
687       sc.comp_unit = cu.get();
688       sc.module_sp = cu->GetModule();
689 
690       // If we were asked to resolve line entries, add all entries to the line
691       // table that match the requested line (or all lines if `line` == 0).
692       if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
693                            eSymbolContextLineEntry)) {
694         bool has_line_table = ParseCompileUnitLineTable(sc, line);
695 
696         if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
697           // The query asks for line entries, but we can't get them for the
698           // compile unit. This is not normal for `line` = 0. So just assert
699           // it.
700           assert(line && "Couldn't get all line entries!\n");
701 
702           // Current compiland does not have the requested line. Search next.
703           continue;
704         }
705 
706         if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
707           if (!has_line_table)
708             continue;
709 
710           auto *line_table = sc.comp_unit->GetLineTable();
711           lldbassert(line_table);
712 
713           uint32_t num_line_entries = line_table->GetSize();
714           // Skip the terminal line entry.
715           --num_line_entries;
716 
717           // If `line `!= 0, see if we can resolve function for each line entry
718           // in the line table.
719           for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
720                ++line_idx) {
721             if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
722               continue;
723 
724             auto file_vm_addr =
725                 sc.line_entry.range.GetBaseAddress().GetFileAddress();
726             if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
727               continue;
728 
729             auto symbol_up = m_session_up->findSymbolByAddress(
730                 file_vm_addr, PDB_SymType::Function);
731             if (symbol_up) {
732               auto func_uid = symbol_up->getSymIndexId();
733               sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
734               if (sc.function == nullptr) {
735                 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
736                 assert(pdb_func);
737                 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
738               }
739               if (sc.function && (resolve_scope & eSymbolContextBlock)) {
740                 Block &block = sc.function->GetBlock(true);
741                 sc.block = block.FindBlockByID(sc.function->GetID());
742               }
743             }
744             sc_list.Append(sc);
745           }
746         } else if (has_line_table) {
747           // We can parse line table for the compile unit. But no query to
748           // resolve function or block. We append `sc` to the list anyway.
749           sc_list.Append(sc);
750         }
751       } else {
752         // No query for line entry, function or block. But we have a valid
753         // compile unit, append `sc` to the list.
754         sc_list.Append(sc);
755       }
756     }
757   }
758   return sc_list.GetSize() - old_size;
759 }
760 
761 std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
762   std::string decorated_name;
763   auto vm_addr = pdb_data.getVirtualAddress();
764   if (vm_addr != LLDB_INVALID_ADDRESS && vm_addr) {
765     auto result_up =
766         m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol);
767     if (result_up) {
768       while (auto symbol_up = result_up->getNext()) {
769         if (symbol_up->getRawSymbol().getVirtualAddress() == vm_addr) {
770           decorated_name = symbol_up->getRawSymbol().getName();
771           break;
772         }
773       }
774     }
775   }
776   if (!decorated_name.empty())
777     return decorated_name;
778 
779   return std::string();
780 }
781 
782 VariableSP SymbolFilePDB::ParseVariableForPDBData(
783     const lldb_private::SymbolContext &sc,
784     const llvm::pdb::PDBSymbolData &pdb_data) {
785   VariableSP var_sp;
786   uint32_t var_uid = pdb_data.getSymIndexId();
787   auto result = m_variables.find(var_uid);
788   if (result != m_variables.end())
789     return result->second;
790 
791   ValueType scope = eValueTypeInvalid;
792   bool is_static_member = false;
793   bool is_external = false;
794   bool is_artificial = false;
795 
796   switch (pdb_data.getDataKind()) {
797   case PDB_DataKind::Global:
798     scope = eValueTypeVariableGlobal;
799     is_external = true;
800     break;
801   case PDB_DataKind::Local:
802     scope = eValueTypeVariableLocal;
803     break;
804   case PDB_DataKind::FileStatic:
805     scope = eValueTypeVariableStatic;
806     break;
807   case PDB_DataKind::StaticMember:
808     is_static_member = true;
809     scope = eValueTypeVariableStatic;
810     break;
811   case PDB_DataKind::Member:
812     scope = eValueTypeVariableStatic;
813     break;
814   case PDB_DataKind::Param:
815     scope = eValueTypeVariableArgument;
816     break;
817   case PDB_DataKind::Constant:
818     scope = eValueTypeConstResult;
819     break;
820   default:
821     break;
822   }
823 
824   switch (pdb_data.getLocationType()) {
825   case PDB_LocType::TLS:
826     scope = eValueTypeVariableThreadLocal;
827     break;
828   case PDB_LocType::RegRel: {
829     // It is a `this` pointer.
830     if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
831       scope = eValueTypeVariableArgument;
832       is_artificial = true;
833     }
834   } break;
835   default:
836     break;
837   }
838 
839   Declaration decl;
840   if (!is_artificial && !pdb_data.isCompilerGenerated()) {
841     if (auto lines = pdb_data.getLineNumbers()) {
842       if (auto first_line = lines->getNext()) {
843         uint32_t src_file_id = first_line->getSourceFileId();
844         auto src_file = m_session_up->getSourceFileById(src_file_id);
845         if (src_file) {
846           FileSpec spec(src_file->getFileName(), /*resolve_path*/ false);
847           decl.SetFile(spec);
848           decl.SetColumn(first_line->getColumnNumber());
849           decl.SetLine(first_line->getLineNumber());
850         }
851       }
852     }
853   }
854 
855   Variable::RangeList ranges;
856   SymbolContextScope *context_scope = sc.comp_unit;
857   if (scope == eValueTypeVariableLocal) {
858     if (sc.function) {
859       context_scope = sc.function->GetBlock(true).FindBlockByID(
860           pdb_data.getClassParentId());
861       if (context_scope == nullptr)
862         context_scope = sc.function;
863     }
864   }
865 
866   SymbolFileTypeSP type_sp =
867       std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId());
868 
869   auto var_name = pdb_data.getName();
870   auto mangled = GetMangledForPDBData(pdb_data);
871   auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
872 
873   DWARFExpression location(nullptr);
874 
875   var_sp = std::make_shared<Variable>(
876       var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope,
877       ranges, &decl, location, is_external, is_artificial, is_static_member);
878 
879   m_variables.insert(std::make_pair(var_uid, var_sp));
880   return var_sp;
881 }
882 
883 size_t
884 SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
885                               const llvm::pdb::PDBSymbol &pdb_symbol,
886                               lldb_private::VariableList *variable_list) {
887   size_t num_added = 0;
888 
889   if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) {
890     VariableListSP local_variable_list_sp;
891 
892     auto result = m_variables.find(pdb_data->getSymIndexId());
893     if (result != m_variables.end()) {
894       if (variable_list)
895         variable_list->AddVariableIfUnique(result->second);
896     } else {
897       // Prepare right VariableList for this variable.
898       if (auto lexical_parent = pdb_data->getLexicalParent()) {
899         switch (lexical_parent->getSymTag()) {
900         case PDB_SymType::Exe:
901           assert(sc.comp_unit);
902           LLVM_FALLTHROUGH;
903         case PDB_SymType::Compiland: {
904           if (sc.comp_unit) {
905             local_variable_list_sp = sc.comp_unit->GetVariableList(false);
906             if (!local_variable_list_sp) {
907               local_variable_list_sp = std::make_shared<VariableList>();
908               sc.comp_unit->SetVariableList(local_variable_list_sp);
909             }
910           }
911         } break;
912         case PDB_SymType::Block:
913         case PDB_SymType::Function: {
914           if (sc.function) {
915             Block *block = sc.function->GetBlock(true).FindBlockByID(
916                 lexical_parent->getSymIndexId());
917             if (block) {
918               local_variable_list_sp = block->GetBlockVariableList(false);
919               if (!local_variable_list_sp) {
920                 local_variable_list_sp = std::make_shared<VariableList>();
921                 block->SetVariableList(local_variable_list_sp);
922               }
923             }
924           }
925         } break;
926         default:
927           break;
928         }
929       }
930 
931       if (local_variable_list_sp) {
932         if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) {
933           local_variable_list_sp->AddVariableIfUnique(var_sp);
934           if (variable_list)
935             variable_list->AddVariableIfUnique(var_sp);
936           ++num_added;
937         }
938       }
939     }
940   }
941 
942   if (auto results = pdb_symbol.findAllChildren()) {
943     while (auto result = results->getNext())
944       num_added += ParseVariables(sc, *result, variable_list);
945   }
946 
947   return num_added;
948 }
949 
950 uint32_t SymbolFilePDB::FindGlobalVariables(
951     const lldb_private::ConstString &name,
952     const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
953     uint32_t max_matches, lldb_private::VariableList &variables) {
954   if (!append)
955     variables.Clear();
956   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
957     return 0;
958   if (name.IsEmpty())
959     return 0;
960 
961   auto results =
962       m_global_scope_up->findChildren(PDB_SymType::Data, name.GetStringRef(),
963                                       PDB_NameSearchFlags::NS_CaseSensitive);
964   if (!results)
965     return 0;
966 
967   uint32_t matches = 0;
968   size_t old_size = variables.GetSize();
969   while (auto result = results->getNext()) {
970     auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
971     if (max_matches > 0 && matches >= max_matches)
972       break;
973 
974     SymbolContext sc;
975     sc.module_sp = m_obj_file->GetModule();
976     lldbassert(sc.module_sp.get());
977 
978     sc.comp_unit = ParseCompileUnitForUID(pdb_data->getCompilandId()).get();
979     // FIXME: We are not able to determine the compile unit.
980     if (sc.comp_unit == nullptr)
981       continue;
982 
983     ParseVariables(sc, *pdb_data, &variables);
984     matches = variables.GetSize() - old_size;
985   }
986 
987   return matches;
988 }
989 
990 uint32_t
991 SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
992                                    bool append, uint32_t max_matches,
993                                    lldb_private::VariableList &variables) {
994   if (!regex.IsValid())
995     return 0;
996   auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
997   if (!results)
998     return 0;
999 
1000   uint32_t matches = 0;
1001   size_t old_size = variables.GetSize();
1002   while (auto pdb_data = results->getNext()) {
1003     if (max_matches > 0 && matches >= max_matches)
1004       break;
1005 
1006     auto var_name = pdb_data->getName();
1007     if (var_name.empty())
1008       continue;
1009     if (!regex.Execute(var_name))
1010       continue;
1011     SymbolContext sc;
1012     sc.module_sp = m_obj_file->GetModule();
1013     lldbassert(sc.module_sp.get());
1014 
1015     sc.comp_unit = ParseCompileUnitForUID(pdb_data->getCompilandId()).get();
1016     // FIXME: We are not able to determine the compile unit.
1017     if (sc.comp_unit == nullptr)
1018       continue;
1019 
1020     ParseVariables(sc, *pdb_data, &variables);
1021     matches = variables.GetSize() - old_size;
1022   }
1023 
1024   return matches;
1025 }
1026 
1027 bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
1028                                     bool include_inlines,
1029                                     lldb_private::SymbolContextList &sc_list) {
1030   lldb_private::SymbolContext sc;
1031   sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
1032   if (!sc.comp_unit)
1033     return false;
1034   sc.module_sp = sc.comp_unit->GetModule();
1035   sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
1036   if (!sc.function)
1037     return false;
1038 
1039   sc_list.Append(sc);
1040   return true;
1041 }
1042 
1043 bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
1044                                     lldb_private::SymbolContextList &sc_list) {
1045   auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
1046   if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
1047     return false;
1048   return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
1049 }
1050 
1051 void SymbolFilePDB::CacheFunctionNames() {
1052   if (!m_func_full_names.IsEmpty())
1053     return;
1054 
1055   std::map<uint64_t, uint32_t> addr_ids;
1056 
1057   if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
1058     while (auto pdb_func_up = results_up->getNext()) {
1059       if (pdb_func_up->isCompilerGenerated())
1060         continue;
1061 
1062       auto name = pdb_func_up->getName();
1063       auto demangled_name = pdb_func_up->getUndecoratedName();
1064       if (name.empty() && demangled_name.empty())
1065         continue;
1066 
1067       auto uid = pdb_func_up->getSymIndexId();
1068       if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
1069         addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
1070 
1071       if (auto parent = pdb_func_up->getClassParent()) {
1072 
1073         // PDB have symbols for class/struct methods or static methods in Enum
1074         // Class. We won't bother to check if the parent is UDT or Enum here.
1075         m_func_method_names.Append(ConstString(name), uid);
1076 
1077         ConstString cstr_name(name);
1078 
1079         // To search a method name, like NS::Class:MemberFunc, LLDB searches
1080         // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
1081         // not have inforamtion of this, we extract base names and cache them
1082         // by our own effort.
1083         llvm::StringRef basename;
1084         CPlusPlusLanguage::MethodName cpp_method(cstr_name);
1085         if (cpp_method.IsValid()) {
1086           llvm::StringRef context;
1087           basename = cpp_method.GetBasename();
1088           if (basename.empty())
1089             CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(),
1090                                                            context, basename);
1091         }
1092 
1093         if (!basename.empty())
1094           m_func_base_names.Append(ConstString(basename), uid);
1095         else {
1096           m_func_base_names.Append(ConstString(name), uid);
1097         }
1098 
1099         if (!demangled_name.empty())
1100           m_func_full_names.Append(ConstString(demangled_name), uid);
1101 
1102       } else {
1103         // Handle not-method symbols.
1104 
1105         // The function name might contain namespace, or its lexical scope. It
1106         // is not safe to get its base name by applying same scheme as we deal
1107         // with the method names.
1108         // FIXME: Remove namespace if function is static in a scope.
1109         m_func_base_names.Append(ConstString(name), uid);
1110 
1111         if (name == "main") {
1112           m_func_full_names.Append(ConstString(name), uid);
1113 
1114           if (!demangled_name.empty() && name != demangled_name) {
1115             m_func_full_names.Append(ConstString(demangled_name), uid);
1116             m_func_base_names.Append(ConstString(demangled_name), uid);
1117           }
1118         } else if (!demangled_name.empty()) {
1119           m_func_full_names.Append(ConstString(demangled_name), uid);
1120         } else {
1121           m_func_full_names.Append(ConstString(name), uid);
1122         }
1123       }
1124     }
1125   }
1126 
1127   if (auto results_up =
1128           m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
1129     while (auto pub_sym_up = results_up->getNext()) {
1130       if (!pub_sym_up->isFunction())
1131         continue;
1132       auto name = pub_sym_up->getName();
1133       if (name.empty())
1134         continue;
1135 
1136       if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
1137         auto vm_addr = pub_sym_up->getVirtualAddress();
1138 
1139         // PDB public symbol has mangled name for its associated function.
1140         if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
1141           // Cache mangled name.
1142           m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
1143         }
1144       }
1145     }
1146   }
1147   // Sort them before value searching is working properly
1148   m_func_full_names.Sort();
1149   m_func_full_names.SizeToFit();
1150   m_func_method_names.Sort();
1151   m_func_method_names.SizeToFit();
1152   m_func_base_names.Sort();
1153   m_func_base_names.SizeToFit();
1154 }
1155 
1156 uint32_t SymbolFilePDB::FindFunctions(
1157     const lldb_private::ConstString &name,
1158     const lldb_private::CompilerDeclContext *parent_decl_ctx,
1159     uint32_t name_type_mask, bool include_inlines, bool append,
1160     lldb_private::SymbolContextList &sc_list) {
1161   if (!append)
1162     sc_list.Clear();
1163   lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
1164 
1165   if (name_type_mask == eFunctionNameTypeNone)
1166     return 0;
1167   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1168     return 0;
1169   if (name.IsEmpty())
1170     return 0;
1171 
1172   auto old_size = sc_list.GetSize();
1173   if (name_type_mask & eFunctionNameTypeFull ||
1174       name_type_mask & eFunctionNameTypeBase ||
1175       name_type_mask & eFunctionNameTypeMethod) {
1176     CacheFunctionNames();
1177 
1178     std::set<uint32_t> resolved_ids;
1179     auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids,
1180                       this](UniqueCStringMap<uint32_t> &Names) {
1181       std::vector<uint32_t> ids;
1182       if (Names.GetValues(name, ids)) {
1183         for (auto id : ids) {
1184           if (resolved_ids.find(id) == resolved_ids.end()) {
1185             if (ResolveFunction(id, include_inlines, sc_list))
1186               resolved_ids.insert(id);
1187           }
1188         }
1189       }
1190     };
1191     if (name_type_mask & eFunctionNameTypeFull) {
1192       ResolveFn(m_func_full_names);
1193     }
1194     if (name_type_mask & eFunctionNameTypeBase) {
1195       ResolveFn(m_func_base_names);
1196     }
1197     if (name_type_mask & eFunctionNameTypeMethod) {
1198       ResolveFn(m_func_method_names);
1199     }
1200   }
1201   return sc_list.GetSize() - old_size;
1202 }
1203 
1204 uint32_t
1205 SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1206                              bool include_inlines, bool append,
1207                              lldb_private::SymbolContextList &sc_list) {
1208   if (!append)
1209     sc_list.Clear();
1210   if (!regex.IsValid())
1211     return 0;
1212 
1213   auto old_size = sc_list.GetSize();
1214   CacheFunctionNames();
1215 
1216   std::set<uint32_t> resolved_ids;
1217   auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1218                     this](UniqueCStringMap<uint32_t> &Names) {
1219     std::vector<uint32_t> ids;
1220     if (Names.GetValues(regex, ids)) {
1221       for (auto id : ids) {
1222         if (resolved_ids.find(id) == resolved_ids.end())
1223           if (ResolveFunction(id, include_inlines, sc_list))
1224             resolved_ids.insert(id);
1225       }
1226     }
1227   };
1228   ResolveFn(m_func_full_names);
1229   ResolveFn(m_func_base_names);
1230 
1231   return sc_list.GetSize() - old_size;
1232 }
1233 
1234 void SymbolFilePDB::GetMangledNamesForFunction(
1235     const std::string &scope_qualified_name,
1236     std::vector<lldb_private::ConstString> &mangled_names) {}
1237 
1238 uint32_t SymbolFilePDB::FindTypes(
1239     const lldb_private::SymbolContext &sc,
1240     const lldb_private::ConstString &name,
1241     const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1242     uint32_t max_matches,
1243     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1244     lldb_private::TypeMap &types) {
1245   if (!append)
1246     types.Clear();
1247   if (!name)
1248     return 0;
1249   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1250     return 0;
1251 
1252   searched_symbol_files.clear();
1253   searched_symbol_files.insert(this);
1254 
1255   std::string name_str = name.AsCString();
1256 
1257   // There is an assumption 'name' is not a regex
1258   FindTypesByName(name_str, max_matches, types);
1259 
1260   return types.GetSize();
1261 }
1262 
1263 void SymbolFilePDB::FindTypesByRegex(
1264     const lldb_private::RegularExpression &regex, uint32_t max_matches,
1265     lldb_private::TypeMap &types) {
1266   // When searching by regex, we need to go out of our way to limit the search
1267   // space as much as possible since this searches EVERYTHING in the PDB,
1268   // manually doing regex comparisons.  PDB library isn't optimized for regex
1269   // searches or searches across multiple symbol types at the same time, so the
1270   // best we can do is to search enums, then typedefs, then classes one by one,
1271   // and do a regex comparison against each of them.
1272   PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1273                                   PDB_SymType::UDT};
1274   std::unique_ptr<IPDBEnumSymbols> results;
1275 
1276   uint32_t matches = 0;
1277 
1278   for (auto tag : tags_to_search) {
1279     results = m_global_scope_up->findAllChildren(tag);
1280     if (!results)
1281       continue;
1282 
1283     while (auto result = results->getNext()) {
1284       if (max_matches > 0 && matches >= max_matches)
1285         break;
1286 
1287       std::string type_name;
1288       if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1289         type_name = enum_type->getName();
1290       else if (auto typedef_type =
1291                    llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
1292         type_name = typedef_type->getName();
1293       else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1294         type_name = class_type->getName();
1295       else {
1296         // We're looking only for types that have names.  Skip symbols, as well
1297         // as unnamed types such as arrays, pointers, etc.
1298         continue;
1299       }
1300 
1301       if (!regex.Execute(type_name))
1302         continue;
1303 
1304       // This should cause the type to get cached and stored in the `m_types`
1305       // lookup.
1306       if (!ResolveTypeUID(result->getSymIndexId()))
1307         continue;
1308 
1309       auto iter = m_types.find(result->getSymIndexId());
1310       if (iter == m_types.end())
1311         continue;
1312       types.Insert(iter->second);
1313       ++matches;
1314     }
1315   }
1316 }
1317 
1318 void SymbolFilePDB::FindTypesByName(const std::string &name,
1319                                     uint32_t max_matches,
1320                                     lldb_private::TypeMap &types) {
1321   std::unique_ptr<IPDBEnumSymbols> results;
1322   if (name.empty())
1323     return;
1324   results = m_global_scope_up->findChildren(PDB_SymType::None, name,
1325                                             PDB_NameSearchFlags::NS_Default);
1326   if (!results)
1327     return;
1328 
1329   uint32_t matches = 0;
1330 
1331   while (auto result = results->getNext()) {
1332     if (max_matches > 0 && matches >= max_matches)
1333       break;
1334     switch (result->getSymTag()) {
1335     case PDB_SymType::Enum:
1336     case PDB_SymType::UDT:
1337     case PDB_SymType::Typedef:
1338       break;
1339     default:
1340       // We're looking only for types that have names.  Skip symbols, as well
1341       // as unnamed types such as arrays, pointers, etc.
1342       continue;
1343     }
1344 
1345     // This should cause the type to get cached and stored in the `m_types`
1346     // lookup.
1347     if (!ResolveTypeUID(result->getSymIndexId()))
1348       continue;
1349 
1350     auto iter = m_types.find(result->getSymIndexId());
1351     if (iter == m_types.end())
1352       continue;
1353     types.Insert(iter->second);
1354     ++matches;
1355   }
1356 }
1357 
1358 size_t SymbolFilePDB::FindTypes(
1359     const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1360     lldb_private::TypeMap &types) {
1361   return 0;
1362 }
1363 
1364 lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1365   return m_obj_file->GetModule()->GetTypeList();
1366 }
1367 
1368 void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1369                                          uint32_t type_mask,
1370                                          TypeCollection &type_collection) {
1371   bool can_parse = false;
1372   switch (pdb_symbol.getSymTag()) {
1373   case PDB_SymType::ArrayType:
1374     can_parse = ((type_mask & eTypeClassArray) != 0);
1375     break;
1376   case PDB_SymType::BuiltinType:
1377     can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1378     break;
1379   case PDB_SymType::Enum:
1380     can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1381     break;
1382   case PDB_SymType::Function:
1383   case PDB_SymType::FunctionSig:
1384     can_parse = ((type_mask & eTypeClassFunction) != 0);
1385     break;
1386   case PDB_SymType::PointerType:
1387     can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1388                                eTypeClassMemberPointer)) != 0);
1389     break;
1390   case PDB_SymType::Typedef:
1391     can_parse = ((type_mask & eTypeClassTypedef) != 0);
1392     break;
1393   case PDB_SymType::UDT: {
1394     auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
1395     assert(udt);
1396     can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
1397                  ((type_mask & (eTypeClassClass | eTypeClassStruct |
1398                                 eTypeClassUnion)) != 0));
1399   } break;
1400   default:
1401     break;
1402   }
1403 
1404   if (can_parse) {
1405     if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
1406       auto result =
1407           std::find(type_collection.begin(), type_collection.end(), type);
1408       if (result == type_collection.end())
1409         type_collection.push_back(type);
1410     }
1411   }
1412 
1413   auto results_up = pdb_symbol.findAllChildren();
1414   while (auto symbol_up = results_up->getNext())
1415     GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
1416 }
1417 
1418 size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1419                                uint32_t type_mask,
1420                                lldb_private::TypeList &type_list) {
1421   TypeCollection type_collection;
1422   uint32_t old_size = type_list.GetSize();
1423   CompileUnit *cu =
1424       sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
1425   if (cu) {
1426     auto compiland_up = GetPDBCompilandByUID(cu->GetID());
1427     if (!compiland_up)
1428       return 0;
1429     GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
1430   } else {
1431     for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1432       auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
1433       if (cu_sp) {
1434         if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1435           GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
1436       }
1437     }
1438   }
1439 
1440   for (auto type : type_collection) {
1441     type->GetForwardCompilerType();
1442     type_list.Insert(type->shared_from_this());
1443   }
1444   return type_list.GetSize() - old_size;
1445 }
1446 
1447 lldb_private::TypeSystem *
1448 SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1449   auto type_system =
1450       m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1451   if (type_system)
1452     type_system->SetSymbolFile(this);
1453   return type_system;
1454 }
1455 
1456 lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1457     const lldb_private::SymbolContext &sc,
1458     const lldb_private::ConstString &name,
1459     const lldb_private::CompilerDeclContext *parent_decl_ctx) {
1460   return lldb_private::CompilerDeclContext();
1461 }
1462 
1463 lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1464   static ConstString g_name("pdb");
1465   return g_name;
1466 }
1467 
1468 uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1469 
1470 IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1471 
1472 const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1473   return *m_session_up;
1474 }
1475 
1476 lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1477                                                        uint32_t index) {
1478   auto found_cu = m_comp_units.find(id);
1479   if (found_cu != m_comp_units.end())
1480     return found_cu->second;
1481 
1482   auto compiland_up = GetPDBCompilandByUID(id);
1483   if (!compiland_up)
1484     return CompUnitSP();
1485 
1486   lldb::LanguageType lang;
1487   auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
1488   if (!details)
1489     lang = lldb::eLanguageTypeC_plus_plus;
1490   else
1491     lang = TranslateLanguage(details->getLanguage());
1492 
1493   if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1494     return CompUnitSP();
1495 
1496   std::string path = compiland_up->getSourceFileFullPath();
1497   if (path.empty())
1498     return CompUnitSP();
1499 
1500   // Don't support optimized code for now, DebugInfoPDB does not return this
1501   // information.
1502   LazyBool optimized = eLazyBoolNo;
1503   auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1504                                              path.c_str(), id, lang, optimized);
1505 
1506   if (!cu_sp)
1507     return CompUnitSP();
1508 
1509   m_comp_units.insert(std::make_pair(id, cu_sp));
1510   if (index == UINT32_MAX)
1511     GetCompileUnitIndex(*compiland_up, index);
1512   lldbassert(index != UINT32_MAX);
1513   m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
1514                                                                     cu_sp);
1515   return cu_sp;
1516 }
1517 
1518 bool SymbolFilePDB::ParseCompileUnitLineTable(
1519     const lldb_private::SymbolContext &sc, uint32_t match_line) {
1520   lldbassert(sc.comp_unit);
1521 
1522   auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1523   if (!compiland_up)
1524     return false;
1525 
1526   // LineEntry needs the *index* of the file into the list of support files
1527   // returned by ParseCompileUnitSupportFiles.  But the underlying SDK gives us
1528   // a globally unique idenfitifier in the namespace of the PDB.  So, we have
1529   // to do a mapping so that we can hand out indices.
1530   llvm::DenseMap<uint32_t, uint32_t> index_map;
1531   BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
1532   auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
1533 
1534   // Find contributions to `compiland` from all source and header files.
1535   std::string path = sc.comp_unit->GetPath();
1536   auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1537   if (!files)
1538     return false;
1539 
1540   // For each source and header file, create a LineSequence for contributions
1541   // to the compiland from that file, and add the sequence.
1542   while (auto file = files->getNext()) {
1543     std::unique_ptr<LineSequence> sequence(
1544         line_table->CreateLineSequenceContainer());
1545     auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1546     if (!lines)
1547       continue;
1548     int entry_count = lines->getChildCount();
1549 
1550     uint64_t prev_addr;
1551     uint32_t prev_length;
1552     uint32_t prev_line;
1553     uint32_t prev_source_idx;
1554 
1555     for (int i = 0; i < entry_count; ++i) {
1556       auto line = lines->getChildAtIndex(i);
1557 
1558       uint64_t lno = line->getLineNumber();
1559       uint64_t addr = line->getVirtualAddress();
1560       uint32_t length = line->getLength();
1561       uint32_t source_id = line->getSourceFileId();
1562       uint32_t col = line->getColumnNumber();
1563       uint32_t source_idx = index_map[source_id];
1564 
1565       // There was a gap between the current entry and the previous entry if
1566       // the addresses don't perfectly line up.
1567       bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
1568 
1569       // Before inserting the current entry, insert a terminal entry at the end
1570       // of the previous entry's address range if the current entry resulted in
1571       // a gap from the previous entry.
1572       if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1573         line_table->AppendLineEntryToSequence(
1574             sequence.get(), prev_addr + prev_length, prev_line, 0,
1575             prev_source_idx, false, false, false, false, true);
1576       }
1577 
1578       if (ShouldAddLine(match_line, lno, length)) {
1579         bool is_statement = line->isStatement();
1580         bool is_prologue = false;
1581         bool is_epilogue = false;
1582         auto func =
1583             m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1584         if (func) {
1585           auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
1586           if (prologue)
1587             is_prologue = (addr == prologue->getVirtualAddress());
1588 
1589           auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
1590           if (epilogue)
1591             is_epilogue = (addr == epilogue->getVirtualAddress());
1592         }
1593 
1594         line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1595                                               source_idx, is_statement, false,
1596                                               is_prologue, is_epilogue, false);
1597       }
1598 
1599       prev_addr = addr;
1600       prev_length = length;
1601       prev_line = lno;
1602       prev_source_idx = source_idx;
1603     }
1604 
1605     if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1606       // The end is always a terminal entry, so insert it regardless.
1607       line_table->AppendLineEntryToSequence(
1608           sequence.get(), prev_addr + prev_length, prev_line, 0,
1609           prev_source_idx, false, false, false, false, true);
1610     }
1611 
1612     line_table->InsertSequence(sequence.release());
1613   }
1614 
1615   if (line_table->GetSize()) {
1616     sc.comp_unit->SetLineTable(line_table.release());
1617     return true;
1618   }
1619   return false;
1620 }
1621 
1622 void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
1623     const PDBSymbolCompiland &compiland,
1624     llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
1625   // This is a hack, but we need to convert the source id into an index into
1626   // the support files array.  We don't want to do path comparisons to avoid
1627   // basename / full path issues that may or may not even be a problem, so we
1628   // use the globally unique source file identifiers.  Ideally we could use the
1629   // global identifiers everywhere, but LineEntry currently assumes indices.
1630   auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1631   if (!source_files)
1632     return;
1633   int index = 0;
1634 
1635   while (auto file = source_files->getNext()) {
1636     uint32_t source_id = file->getUniqueId();
1637     index_map[source_id] = index++;
1638   }
1639 }
1640 
1641 lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
1642     const lldb_private::Address &so_addr) {
1643   lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1644   if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
1645     return nullptr;
1646 
1647   // If it is a PDB function's vm addr, this is the first sure bet.
1648   if (auto lines =
1649           m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1650     if (auto first_line = lines->getNext())
1651       return ParseCompileUnitForUID(first_line->getCompilandId());
1652   }
1653 
1654   // Otherwise we resort to section contributions.
1655   if (auto sec_contribs = m_session_up->getSectionContribs()) {
1656     while (auto section = sec_contribs->getNext()) {
1657       auto va = section->getVirtualAddress();
1658       if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1659         return ParseCompileUnitForUID(section->getCompilandId());
1660     }
1661   }
1662   return nullptr;
1663 }
1664 
1665 Mangled
1666 SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
1667   Mangled mangled;
1668   auto func_name = pdb_func.getName();
1669   auto func_undecorated_name = pdb_func.getUndecoratedName();
1670   std::string func_decorated_name;
1671 
1672   // Seek from public symbols for non-static function's decorated name if any.
1673   // For static functions, they don't have undecorated names and aren't exposed
1674   // in Public Symbols either.
1675   if (!func_undecorated_name.empty()) {
1676     auto result_up = m_global_scope_up->findChildren(
1677         PDB_SymType::PublicSymbol, func_undecorated_name,
1678         PDB_NameSearchFlags::NS_UndecoratedName);
1679     if (result_up) {
1680       while (auto symbol_up = result_up->getNext()) {
1681         // For a public symbol, it is unique.
1682         lldbassert(result_up->getChildCount() == 1);
1683         if (auto *pdb_public_sym =
1684                 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1685                     symbol_up.get())) {
1686           if (pdb_public_sym->isFunction()) {
1687             func_decorated_name = pdb_public_sym->getName();
1688             break;
1689           }
1690         }
1691       }
1692     }
1693   }
1694   if (!func_decorated_name.empty()) {
1695     mangled.SetMangledName(ConstString(func_decorated_name));
1696 
1697     // For MSVC, format of C funciton's decorated name depends on calling
1698     // conventon. Unfortunately none of the format is recognized by current
1699     // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
1700     // `__purecall` is retrieved as both its decorated and undecorated name
1701     // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1702     // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1703     // Mangled::GetDemangledName method will fail internally and caches an
1704     // empty string as its undecorated name. So we will face a contradition
1705     // here for the same symbol:
1706     //   non-empty undecorated name from PDB
1707     //   empty undecorated name from LLDB
1708     if (!func_undecorated_name.empty() &&
1709         mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1710       mangled.SetDemangledName(ConstString(func_undecorated_name));
1711 
1712     // LLDB uses several flags to control how a C++ decorated name is
1713     // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1714     // yielded name could be different from what we retrieve from
1715     // PDB source unless we also apply same flags in getting undecorated
1716     // name through PDBSymbolFunc::getUndecoratedNameEx method.
1717     if (!func_undecorated_name.empty() &&
1718         mangled.GetDemangledName(mangled.GuessLanguage()) !=
1719             ConstString(func_undecorated_name))
1720       mangled.SetDemangledName(ConstString(func_undecorated_name));
1721   } else if (!func_undecorated_name.empty()) {
1722     mangled.SetDemangledName(ConstString(func_undecorated_name));
1723   } else if (!func_name.empty())
1724     mangled.SetValue(ConstString(func_name), false);
1725 
1726   return mangled;
1727 }
1728 
1729 bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1730     const lldb_private::CompilerDeclContext *decl_ctx) {
1731   if (decl_ctx == nullptr || !decl_ctx->IsValid())
1732     return true;
1733 
1734   TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1735   if (!decl_ctx_type_system)
1736     return false;
1737   TypeSystem *type_system = GetTypeSystemForLanguage(
1738       decl_ctx_type_system->GetMinimumLanguage(nullptr));
1739   if (decl_ctx_type_system == type_system)
1740     return true; // The type systems match, return true
1741 
1742   return false;
1743 }
1744