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