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