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