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` could be
186     // found as a child of the global scope (PDB executable). Usually, such
187     // compilands contain `thunk` symbols in which we are not interested for
188     // now. However we still count them in the compiland list. If we perform
189     // any compiland related activity, like finding symbols through
190     // llvm::pdb::IPDBSession methods, such compilands will all be searched
191     // 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::Style::windows);
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. We
474     // 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
626       // this file unless the FileSpec matches. For inline functions, we don't
627       // have to match the FileSpec since they could be defined in headers
628       // 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::Style::windows);
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
655           // it.
656           assert(line && "Couldn't get all line entries!\n");
657 
658           // Current compiland does not have the requested line. Search next.
659           continue;
660         }
661 
662         if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
663           if (!has_line_table)
664             continue;
665 
666           auto *line_table = sc.comp_unit->GetLineTable();
667           lldbassert(line_table);
668 
669           uint32_t num_line_entries = line_table->GetSize();
670           // Skip the terminal line entry.
671           --num_line_entries;
672 
673           // If `line `!= 0, see if we can resolve function for each line entry
674           // in the line table.
675           for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
676                ++line_idx) {
677             if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
678               continue;
679 
680             auto file_vm_addr =
681                 sc.line_entry.range.GetBaseAddress().GetFileAddress();
682             if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
683               continue;
684 
685             auto symbol_up = m_session_up->findSymbolByAddress(
686                 file_vm_addr, PDB_SymType::Function);
687             if (symbol_up) {
688               auto func_uid = symbol_up->getSymIndexId();
689               sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
690               if (sc.function == nullptr) {
691                 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
692                 assert(pdb_func);
693                 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
694               }
695               if (sc.function && (resolve_scope & eSymbolContextBlock)) {
696                 Block &block = sc.function->GetBlock(true);
697                 sc.block = block.FindBlockByID(sc.function->GetID());
698               }
699             }
700             sc_list.Append(sc);
701           }
702         } else if (has_line_table) {
703           // We can parse line table for the compile unit. But no query to
704           // resolve function or block. We append `sc` to the list anyway.
705           sc_list.Append(sc);
706         }
707       } else {
708         // No query for line entry, function or block. But we have a valid
709         // compile unit, append `sc` to the list.
710         sc_list.Append(sc);
711       }
712     }
713   }
714   return sc_list.GetSize() - old_size;
715 }
716 
717 uint32_t SymbolFilePDB::FindGlobalVariables(
718     const lldb_private::ConstString &name,
719     const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
720     uint32_t max_matches, lldb_private::VariableList &variables) {
721   return uint32_t();
722 }
723 
724 uint32_t
725 SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
726                                    bool append, uint32_t max_matches,
727                                    lldb_private::VariableList &variables) {
728   return uint32_t();
729 }
730 
731 bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
732                                     bool include_inlines,
733                                     lldb_private::SymbolContextList &sc_list) {
734   lldb_private::SymbolContext sc;
735   sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
736   if (!sc.comp_unit)
737     return false;
738   sc.module_sp = sc.comp_unit->GetModule();
739   sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
740   if (!sc.function)
741     return false;
742 
743   sc_list.Append(sc);
744   return true;
745 }
746 
747 bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
748                                     lldb_private::SymbolContextList &sc_list) {
749   auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
750   if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
751     return false;
752   return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
753 }
754 
755 void SymbolFilePDB::CacheFunctionNames() {
756   if (!m_func_full_names.IsEmpty())
757     return;
758 
759   std::map<uint64_t, uint32_t> addr_ids;
760 
761   if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
762     while (auto pdb_func_up = results_up->getNext()) {
763       if (pdb_func_up->isCompilerGenerated())
764         continue;
765 
766       auto name = pdb_func_up->getName();
767       auto demangled_name = pdb_func_up->getUndecoratedName();
768       if (name.empty() && demangled_name.empty())
769         continue;
770 
771       auto uid = pdb_func_up->getSymIndexId();
772       if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
773         addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
774 
775       if (auto parent = pdb_func_up->getClassParent()) {
776 
777         // PDB have symbols for class/struct methods or static methods in Enum
778         // Class. We won't bother to check if the parent is UDT or Enum here.
779         m_func_method_names.Append(ConstString(name), uid);
780 
781         ConstString cstr_name(name);
782 
783         // To search a method name, like NS::Class:MemberFunc, LLDB searches
784         // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
785         // not have inforamtion of this, we extract base names and cache them
786         // by our own effort.
787         llvm::StringRef basename;
788         CPlusPlusLanguage::MethodName cpp_method(cstr_name);
789         if (cpp_method.IsValid()) {
790           llvm::StringRef context;
791           basename = cpp_method.GetBasename();
792           if (basename.empty())
793             CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(),
794                                                            context, basename);
795         }
796 
797         if (!basename.empty())
798           m_func_base_names.Append(ConstString(basename), uid);
799         else {
800           m_func_base_names.Append(ConstString(name), uid);
801         }
802 
803         if (!demangled_name.empty())
804           m_func_full_names.Append(ConstString(demangled_name), uid);
805 
806       } else {
807         // Handle not-method symbols.
808 
809         // The function name might contain namespace, or its lexical scope. It
810         // is not safe to get its base name by applying same scheme as we deal
811         // with the method names.
812         // FIXME: Remove namespace if function is static in a scope.
813         m_func_base_names.Append(ConstString(name), uid);
814 
815         if (name == "main") {
816           m_func_full_names.Append(ConstString(name), uid);
817 
818           if (!demangled_name.empty() && name != demangled_name) {
819             m_func_full_names.Append(ConstString(demangled_name), uid);
820             m_func_base_names.Append(ConstString(demangled_name), uid);
821           }
822         } else if (!demangled_name.empty()) {
823           m_func_full_names.Append(ConstString(demangled_name), uid);
824         } else {
825           m_func_full_names.Append(ConstString(name), uid);
826         }
827       }
828     }
829   }
830 
831   if (auto results_up =
832           m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
833     while (auto pub_sym_up = results_up->getNext()) {
834       if (!pub_sym_up->isFunction())
835         continue;
836       auto name = pub_sym_up->getName();
837       if (name.empty())
838         continue;
839 
840       if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
841         auto vm_addr = pub_sym_up->getVirtualAddress();
842 
843         // PDB public symbol has mangled name for its associated function.
844         if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
845           // Cache mangled name.
846           m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
847         }
848       }
849     }
850   }
851   // Sort them before value searching is working properly
852   m_func_full_names.Sort();
853   m_func_full_names.SizeToFit();
854   m_func_method_names.Sort();
855   m_func_method_names.SizeToFit();
856   m_func_base_names.Sort();
857   m_func_base_names.SizeToFit();
858 }
859 
860 uint32_t SymbolFilePDB::FindFunctions(
861     const lldb_private::ConstString &name,
862     const lldb_private::CompilerDeclContext *parent_decl_ctx,
863     uint32_t name_type_mask, bool include_inlines, bool append,
864     lldb_private::SymbolContextList &sc_list) {
865   if (!append)
866     sc_list.Clear();
867   lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
868 
869   if (name_type_mask == eFunctionNameTypeNone)
870     return 0;
871   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
872     return 0;
873   if (name.IsEmpty())
874     return 0;
875 
876   auto old_size = sc_list.GetSize();
877   if (name_type_mask & eFunctionNameTypeFull ||
878       name_type_mask & eFunctionNameTypeBase ||
879       name_type_mask & eFunctionNameTypeMethod) {
880     CacheFunctionNames();
881 
882     std::set<uint32_t> resolved_ids;
883     auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids,
884                       this](UniqueCStringMap<uint32_t> &Names) {
885       std::vector<uint32_t> ids;
886       if (Names.GetValues(name, ids)) {
887         for (auto id : ids) {
888           if (resolved_ids.find(id) == resolved_ids.end()) {
889             if (ResolveFunction(id, include_inlines, sc_list))
890               resolved_ids.insert(id);
891           }
892         }
893       }
894     };
895     if (name_type_mask & eFunctionNameTypeFull) {
896       ResolveFn(m_func_full_names);
897     }
898     if (name_type_mask & eFunctionNameTypeBase) {
899       ResolveFn(m_func_base_names);
900     }
901     if (name_type_mask & eFunctionNameTypeMethod) {
902       ResolveFn(m_func_method_names);
903     }
904   }
905   return sc_list.GetSize() - old_size;
906 }
907 
908 uint32_t
909 SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
910                              bool include_inlines, bool append,
911                              lldb_private::SymbolContextList &sc_list) {
912   if (!append)
913     sc_list.Clear();
914   if (!regex.IsValid())
915     return 0;
916 
917   auto old_size = sc_list.GetSize();
918   CacheFunctionNames();
919 
920   std::set<uint32_t> resolved_ids;
921   auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
922                     this](UniqueCStringMap<uint32_t> &Names) {
923     std::vector<uint32_t> ids;
924     if (Names.GetValues(regex, ids)) {
925       for (auto id : ids) {
926         if (resolved_ids.find(id) == resolved_ids.end())
927           if (ResolveFunction(id, include_inlines, sc_list))
928             resolved_ids.insert(id);
929       }
930     }
931   };
932   ResolveFn(m_func_full_names);
933   ResolveFn(m_func_base_names);
934 
935   return sc_list.GetSize() - old_size;
936 }
937 
938 void SymbolFilePDB::GetMangledNamesForFunction(
939     const std::string &scope_qualified_name,
940     std::vector<lldb_private::ConstString> &mangled_names) {}
941 
942 uint32_t SymbolFilePDB::FindTypes(
943     const lldb_private::SymbolContext &sc,
944     const lldb_private::ConstString &name,
945     const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
946     uint32_t max_matches,
947     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
948     lldb_private::TypeMap &types) {
949   if (!append)
950     types.Clear();
951   if (!name)
952     return 0;
953   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
954     return 0;
955 
956   searched_symbol_files.clear();
957   searched_symbol_files.insert(this);
958 
959   std::string name_str = name.AsCString();
960 
961   // There is an assumption 'name' is not a regex
962   FindTypesByName(name_str, max_matches, types);
963 
964   return types.GetSize();
965 }
966 
967 void SymbolFilePDB::FindTypesByRegex(
968     const lldb_private::RegularExpression &regex, uint32_t max_matches,
969     lldb_private::TypeMap &types) {
970   // When searching by regex, we need to go out of our way to limit the search
971   // space as much as possible since this searches EVERYTHING in the PDB,
972   // manually doing regex comparisons.  PDB library isn't optimized for regex
973   // searches or searches across multiple symbol types at the same time, so the
974   // best we can do is to search enums, then typedefs, then classes one by one,
975   // and do a regex comparison against each of them.
976   PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
977                                   PDB_SymType::UDT};
978   std::unique_ptr<IPDBEnumSymbols> results;
979 
980   uint32_t matches = 0;
981 
982   for (auto tag : tags_to_search) {
983     results = m_global_scope_up->findAllChildren(tag);
984     if (!results)
985       continue;
986 
987     while (auto result = results->getNext()) {
988       if (max_matches > 0 && matches >= max_matches)
989         break;
990 
991       std::string type_name;
992       if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
993         type_name = enum_type->getName();
994       else if (auto typedef_type =
995                    llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
996         type_name = typedef_type->getName();
997       else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
998         type_name = class_type->getName();
999       else {
1000         // We're looking only for types that have names.  Skip symbols, as well
1001         // as unnamed types such as arrays, pointers, etc.
1002         continue;
1003       }
1004 
1005       if (!regex.Execute(type_name))
1006         continue;
1007 
1008       // This should cause the type to get cached and stored in the `m_types`
1009       // lookup.
1010       if (!ResolveTypeUID(result->getSymIndexId()))
1011         continue;
1012 
1013       auto iter = m_types.find(result->getSymIndexId());
1014       if (iter == m_types.end())
1015         continue;
1016       types.Insert(iter->second);
1017       ++matches;
1018     }
1019   }
1020 }
1021 
1022 void SymbolFilePDB::FindTypesByName(const std::string &name,
1023                                     uint32_t max_matches,
1024                                     lldb_private::TypeMap &types) {
1025   std::unique_ptr<IPDBEnumSymbols> results;
1026   if (name.empty())
1027     return;
1028   results = m_global_scope_up->findChildren(PDB_SymType::None, name,
1029                                             PDB_NameSearchFlags::NS_Default);
1030   if (!results)
1031     return;
1032 
1033   uint32_t matches = 0;
1034 
1035   while (auto result = results->getNext()) {
1036     if (max_matches > 0 && matches >= max_matches)
1037       break;
1038     switch (result->getSymTag()) {
1039     case PDB_SymType::Enum:
1040     case PDB_SymType::UDT:
1041     case PDB_SymType::Typedef:
1042       break;
1043     default:
1044       // We're looking only for types that have names.  Skip symbols, as well
1045       // as unnamed types such as arrays, pointers, etc.
1046       continue;
1047     }
1048 
1049     // This should cause the type to get cached and stored in the `m_types`
1050     // lookup.
1051     if (!ResolveTypeUID(result->getSymIndexId()))
1052       continue;
1053 
1054     auto iter = m_types.find(result->getSymIndexId());
1055     if (iter == m_types.end())
1056       continue;
1057     types.Insert(iter->second);
1058     ++matches;
1059   }
1060 }
1061 
1062 size_t SymbolFilePDB::FindTypes(
1063     const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1064     lldb_private::TypeMap &types) {
1065   return 0;
1066 }
1067 
1068 lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1069   return m_obj_file->GetModule()->GetTypeList();
1070 }
1071 
1072 void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1073                                          uint32_t type_mask,
1074                                          TypeCollection &type_collection) {
1075   bool can_parse = false;
1076   switch (pdb_symbol.getSymTag()) {
1077   case PDB_SymType::ArrayType:
1078     can_parse = ((type_mask & eTypeClassArray) != 0);
1079     break;
1080   case PDB_SymType::BuiltinType:
1081     can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1082     break;
1083   case PDB_SymType::Enum:
1084     can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1085     break;
1086   case PDB_SymType::Function:
1087   case PDB_SymType::FunctionSig:
1088     can_parse = ((type_mask & eTypeClassFunction) != 0);
1089     break;
1090   case PDB_SymType::PointerType:
1091     can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1092                                eTypeClassMemberPointer)) != 0);
1093     break;
1094   case PDB_SymType::Typedef:
1095     can_parse = ((type_mask & eTypeClassTypedef) != 0);
1096     break;
1097   case PDB_SymType::UDT: {
1098     auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
1099     assert(udt);
1100     can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
1101                  ((type_mask & (eTypeClassClass | eTypeClassStruct |
1102                                 eTypeClassUnion)) != 0));
1103   } break;
1104   default:
1105     break;
1106   }
1107 
1108   if (can_parse) {
1109     if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
1110       auto result =
1111           std::find(type_collection.begin(), type_collection.end(), type);
1112       if (result == type_collection.end())
1113         type_collection.push_back(type);
1114     }
1115   }
1116 
1117   auto results_up = pdb_symbol.findAllChildren();
1118   while (auto symbol_up = results_up->getNext())
1119     GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
1120 }
1121 
1122 size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1123                                uint32_t type_mask,
1124                                lldb_private::TypeList &type_list) {
1125   TypeCollection type_collection;
1126   uint32_t old_size = type_list.GetSize();
1127   CompileUnit *cu =
1128       sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
1129   if (cu) {
1130     auto compiland_up = GetPDBCompilandByUID(cu->GetID());
1131     if (!compiland_up)
1132       return 0;
1133     GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
1134   } else {
1135     for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1136       auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
1137       if (cu_sp) {
1138         if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1139           GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
1140       }
1141     }
1142   }
1143 
1144   for (auto type : type_collection) {
1145     type->GetForwardCompilerType();
1146     type_list.Insert(type->shared_from_this());
1147   }
1148   return type_list.GetSize() - old_size;
1149 }
1150 
1151 lldb_private::TypeSystem *
1152 SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1153   auto type_system =
1154       m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1155   if (type_system)
1156     type_system->SetSymbolFile(this);
1157   return type_system;
1158 }
1159 
1160 lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1161     const lldb_private::SymbolContext &sc,
1162     const lldb_private::ConstString &name,
1163     const lldb_private::CompilerDeclContext *parent_decl_ctx) {
1164   return lldb_private::CompilerDeclContext();
1165 }
1166 
1167 lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1168   static ConstString g_name("pdb");
1169   return g_name;
1170 }
1171 
1172 uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1173 
1174 IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1175 
1176 const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1177   return *m_session_up;
1178 }
1179 
1180 lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1181                                                        uint32_t index) {
1182   auto found_cu = m_comp_units.find(id);
1183   if (found_cu != m_comp_units.end())
1184     return found_cu->second;
1185 
1186   auto compiland_up = GetPDBCompilandByUID(id);
1187   if (!compiland_up)
1188     return CompUnitSP();
1189 
1190   lldb::LanguageType lang;
1191   auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
1192   if (!details)
1193     lang = lldb::eLanguageTypeC_plus_plus;
1194   else
1195     lang = TranslateLanguage(details->getLanguage());
1196 
1197   if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1198     return CompUnitSP();
1199 
1200   std::string path = compiland_up->getSourceFileFullPath();
1201   if (path.empty())
1202     return CompUnitSP();
1203 
1204   // Don't support optimized code for now, DebugInfoPDB does not return this
1205   // information.
1206   LazyBool optimized = eLazyBoolNo;
1207   auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1208                                              path.c_str(), id, lang, optimized);
1209 
1210   if (!cu_sp)
1211     return CompUnitSP();
1212 
1213   m_comp_units.insert(std::make_pair(id, cu_sp));
1214   if (index == UINT32_MAX)
1215     GetCompileUnitIndex(*compiland_up, index);
1216   lldbassert(index != UINT32_MAX);
1217   m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
1218                                                                     cu_sp);
1219   return cu_sp;
1220 }
1221 
1222 bool SymbolFilePDB::ParseCompileUnitLineTable(
1223     const lldb_private::SymbolContext &sc, uint32_t match_line) {
1224   lldbassert(sc.comp_unit);
1225 
1226   auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1227   if (!compiland_up)
1228     return false;
1229 
1230   // LineEntry needs the *index* of the file into the list of support files
1231   // returned by ParseCompileUnitSupportFiles.  But the underlying SDK gives us
1232   // a globally unique idenfitifier in the namespace of the PDB.  So, we have
1233   // to do a mapping so that we can hand out indices.
1234   llvm::DenseMap<uint32_t, uint32_t> index_map;
1235   BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
1236   auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
1237 
1238   // Find contributions to `compiland` from all source and header files.
1239   std::string path = sc.comp_unit->GetPath();
1240   auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1241   if (!files)
1242     return false;
1243 
1244   // For each source and header file, create a LineSequence for contributions
1245   // to the compiland from that file, and add the sequence.
1246   while (auto file = files->getNext()) {
1247     std::unique_ptr<LineSequence> sequence(
1248         line_table->CreateLineSequenceContainer());
1249     auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1250     if (!lines)
1251       continue;
1252     int entry_count = lines->getChildCount();
1253 
1254     uint64_t prev_addr;
1255     uint32_t prev_length;
1256     uint32_t prev_line;
1257     uint32_t prev_source_idx;
1258 
1259     for (int i = 0; i < entry_count; ++i) {
1260       auto line = lines->getChildAtIndex(i);
1261 
1262       uint64_t lno = line->getLineNumber();
1263       uint64_t addr = line->getVirtualAddress();
1264       uint32_t length = line->getLength();
1265       uint32_t source_id = line->getSourceFileId();
1266       uint32_t col = line->getColumnNumber();
1267       uint32_t source_idx = index_map[source_id];
1268 
1269       // There was a gap between the current entry and the previous entry if
1270       // the addresses don't perfectly line up.
1271       bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
1272 
1273       // Before inserting the current entry, insert a terminal entry at the end
1274       // of the previous entry's address range if the current entry resulted in
1275       // a gap from the previous entry.
1276       if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1277         line_table->AppendLineEntryToSequence(
1278             sequence.get(), prev_addr + prev_length, prev_line, 0,
1279             prev_source_idx, false, false, false, false, true);
1280       }
1281 
1282       if (ShouldAddLine(match_line, lno, length)) {
1283         bool is_statement = line->isStatement();
1284         bool is_prologue = false;
1285         bool is_epilogue = false;
1286         auto func =
1287             m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1288         if (func) {
1289           auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
1290           if (prologue)
1291             is_prologue = (addr == prologue->getVirtualAddress());
1292 
1293           auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
1294           if (epilogue)
1295             is_epilogue = (addr == epilogue->getVirtualAddress());
1296         }
1297 
1298         line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1299                                               source_idx, is_statement, false,
1300                                               is_prologue, is_epilogue, false);
1301       }
1302 
1303       prev_addr = addr;
1304       prev_length = length;
1305       prev_line = lno;
1306       prev_source_idx = source_idx;
1307     }
1308 
1309     if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1310       // The end is always a terminal entry, so insert it regardless.
1311       line_table->AppendLineEntryToSequence(
1312           sequence.get(), prev_addr + prev_length, prev_line, 0,
1313           prev_source_idx, false, false, false, false, true);
1314     }
1315 
1316     line_table->InsertSequence(sequence.release());
1317   }
1318 
1319   if (line_table->GetSize()) {
1320     sc.comp_unit->SetLineTable(line_table.release());
1321     return true;
1322   }
1323   return false;
1324 }
1325 
1326 void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
1327     const PDBSymbolCompiland &compiland,
1328     llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
1329   // This is a hack, but we need to convert the source id into an index into
1330   // the support files array.  We don't want to do path comparisons to avoid
1331   // basename / full path issues that may or may not even be a problem, so we
1332   // use the globally unique source file identifiers.  Ideally we could use the
1333   // global identifiers everywhere, but LineEntry currently assumes indices.
1334   auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1335   if (!source_files)
1336     return;
1337   int index = 0;
1338 
1339   while (auto file = source_files->getNext()) {
1340     uint32_t source_id = file->getUniqueId();
1341     index_map[source_id] = index++;
1342   }
1343 }
1344 
1345 lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
1346     const lldb_private::Address &so_addr) {
1347   lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1348   if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
1349     return nullptr;
1350 
1351   // If it is a PDB function's vm addr, this is the first sure bet.
1352   if (auto lines =
1353           m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1354     if (auto first_line = lines->getNext())
1355       return ParseCompileUnitForUID(first_line->getCompilandId());
1356   }
1357 
1358   // Otherwise we resort to section contributions.
1359   if (auto sec_contribs = m_session_up->getSectionContribs()) {
1360     while (auto section = sec_contribs->getNext()) {
1361       auto va = section->getVirtualAddress();
1362       if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1363         return ParseCompileUnitForUID(section->getCompilandId());
1364     }
1365   }
1366   return nullptr;
1367 }
1368 
1369 Mangled
1370 SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
1371   Mangled mangled;
1372   auto func_name = pdb_func.getName();
1373   auto func_undecorated_name = pdb_func.getUndecoratedName();
1374   std::string func_decorated_name;
1375 
1376   // Seek from public symbols for non-static function's decorated name if any.
1377   // For static functions, they don't have undecorated names and aren't exposed
1378   // in Public Symbols either.
1379   if (!func_undecorated_name.empty()) {
1380     auto result_up = m_global_scope_up->findChildren(
1381         PDB_SymType::PublicSymbol, func_undecorated_name,
1382         PDB_NameSearchFlags::NS_UndecoratedName);
1383     if (result_up) {
1384       while (auto symbol_up = result_up->getNext()) {
1385         // For a public symbol, it is unique.
1386         lldbassert(result_up->getChildCount() == 1);
1387         if (auto *pdb_public_sym =
1388                 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1389                     symbol_up.get())) {
1390           if (pdb_public_sym->isFunction()) {
1391             func_decorated_name = pdb_public_sym->getName();
1392             break;
1393           }
1394         }
1395       }
1396     }
1397   }
1398   if (!func_decorated_name.empty()) {
1399     mangled.SetMangledName(ConstString(func_decorated_name));
1400 
1401     // For MSVC, format of C funciton's decorated name depends on calling
1402     // conventon. Unfortunately none of the format is recognized by current
1403     // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
1404     // `__purecall` is retrieved as both its decorated and undecorated name
1405     // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1406     // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1407     // Mangled::GetDemangledName method will fail internally and caches an
1408     // empty string as its undecorated name. So we will face a contradition
1409     // here for the same symbol:
1410     //   non-empty undecorated name from PDB
1411     //   empty undecorated name from LLDB
1412     if (!func_undecorated_name.empty() &&
1413         mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1414       mangled.SetDemangledName(ConstString(func_undecorated_name));
1415 
1416     // LLDB uses several flags to control how a C++ decorated name is
1417     // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1418     // yielded name could be different from what we retrieve from
1419     // PDB source unless we also apply same flags in getting undecorated
1420     // name through PDBSymbolFunc::getUndecoratedNameEx method.
1421     if (!func_undecorated_name.empty() &&
1422         mangled.GetDemangledName(mangled.GuessLanguage()) !=
1423             ConstString(func_undecorated_name))
1424       mangled.SetDemangledName(ConstString(func_undecorated_name));
1425   } else if (!func_undecorated_name.empty()) {
1426     mangled.SetDemangledName(ConstString(func_undecorated_name));
1427   } else if (!func_name.empty())
1428     mangled.SetValue(ConstString(func_name), false);
1429 
1430   return mangled;
1431 }
1432 
1433 bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1434     const lldb_private::CompilerDeclContext *decl_ctx) {
1435   if (decl_ctx == nullptr || !decl_ctx->IsValid())
1436     return true;
1437 
1438   TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1439   if (!decl_ctx_type_system)
1440     return false;
1441   TypeSystem *type_system = GetTypeSystemForLanguage(
1442       decl_ctx_type_system->GetMinimumLanguage(nullptr));
1443   if (decl_ctx_type_system == type_system)
1444     return true; // The type systems match, return true
1445 
1446   return false;
1447 }
1448