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