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