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