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