1 //===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "SymbolFilePDB.h"
11 
12 #include "clang/Lex/Lexer.h"
13 
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/PluginManager.h"
16 #include "lldb/Symbol/ClangASTContext.h"
17 #include "lldb/Symbol/CompileUnit.h"
18 #include "lldb/Symbol/LineTable.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Symbol/SymbolVendor.h"
22 #include "lldb/Symbol/TypeMap.h"
23 #include "lldb/Symbol/TypeList.h"
24 #include "lldb/Utility/RegularExpression.h"
25 
26 #include "llvm/DebugInfo/PDB/GenericError.h"
27 #include "llvm/DebugInfo/PDB/IPDBDataStream.h"
28 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
29 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
30 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
31 #include "llvm/DebugInfo/PDB/IPDBTable.h"
32 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
33 #include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
34 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
35 #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
36 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
37 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
38 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
39 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
40 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
41 #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
42 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
43 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
44 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
45 
46 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
47 #include "Plugins/SymbolFile/PDB/PDBASTParser.h"
48 
49 #include <regex>
50 
51 using namespace lldb;
52 using namespace lldb_private;
53 using namespace llvm::pdb;
54 
55 namespace {
56 lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
57   switch (lang) {
58   case PDB_Lang::Cpp:
59     return lldb::LanguageType::eLanguageTypeC_plus_plus;
60   case PDB_Lang::C:
61     return lldb::LanguageType::eLanguageTypeC;
62   default:
63     return lldb::LanguageType::eLanguageTypeUnknown;
64   }
65 }
66 
67 bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
68                    uint32_t addr_length) {
69   return ((requested_line == 0 || actual_line == requested_line) &&
70           addr_length > 0);
71 }
72 }
73 
74 void SymbolFilePDB::Initialize() {
75   PluginManager::RegisterPlugin(GetPluginNameStatic(),
76                                 GetPluginDescriptionStatic(), CreateInstance,
77                                 DebuggerInitialize);
78 }
79 
80 void SymbolFilePDB::Terminate() {
81   PluginManager::UnregisterPlugin(CreateInstance);
82 }
83 
84 void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
85 
86 lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
87   static ConstString g_name("pdb");
88   return g_name;
89 }
90 
91 const char *SymbolFilePDB::GetPluginDescriptionStatic() {
92   return "Microsoft PDB debug symbol file reader.";
93 }
94 
95 lldb_private::SymbolFile *
96 SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
97   return new SymbolFilePDB(obj_file);
98 }
99 
100 SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
101     : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
102       m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
103 
104 SymbolFilePDB::~SymbolFilePDB() {}
105 
106 uint32_t SymbolFilePDB::CalculateAbilities() {
107   uint32_t abilities = 0;
108   if (!m_obj_file)
109     return 0;
110 
111   if (!m_session_up) {
112     // Lazily load and match the PDB file, but only do this once.
113     std::string exePath = m_obj_file->GetFileSpec().GetPath();
114     auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
115                                 m_session_up);
116     if (error) {
117       llvm::consumeError(std::move(error));
118       auto module_sp = m_obj_file->GetModule();
119       if (!module_sp)
120         return 0;
121       // See if any symbol file is specified through `--symfile` option.
122       FileSpec symfile = module_sp->GetSymbolFileFileSpec();
123       if (!symfile)
124         return 0;
125       error = loadDataForPDB(PDB_ReaderType::DIA,
126                              llvm::StringRef(symfile.GetPath()),
127                              m_session_up);
128       if (error) {
129         llvm::consumeError(std::move(error));
130         return 0;
131       }
132     }
133   }
134   if (!m_session_up.get())
135     return 0;
136 
137   auto enum_tables_up = m_session_up->getEnumTables();
138   if (!enum_tables_up)
139     return 0;
140   while (auto table_up = enum_tables_up->getNext()) {
141     if (table_up->getItemCount() == 0)
142       continue;
143     auto type = table_up->getTableType();
144     switch (type) {
145     case PDB_TableType::Symbols:
146       // This table represents a store of symbols with types listed in
147       // PDBSym_Type
148       abilities |= (CompileUnits | Functions | Blocks |
149                     GlobalVariables | LocalVariables | VariableTypes);
150       break;
151     case PDB_TableType::LineNumbers:
152       abilities |= LineTables;
153       break;
154     default: break;
155     }
156   }
157   return abilities;
158 }
159 
160 void SymbolFilePDB::InitializeObject() {
161   lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
162   lldbassert(obj_load_address &&
163              obj_load_address != LLDB_INVALID_ADDRESS);
164   m_session_up->setLoadAddress(obj_load_address);
165   if (!m_global_scope_up)
166     m_global_scope_up = m_session_up->getGlobalScope();
167   lldbassert(m_global_scope_up.get());
168 
169   TypeSystem *type_system =
170       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
171   ClangASTContext *clang_type_system =
172       llvm::dyn_cast_or_null<ClangASTContext>(type_system);
173   lldbassert(clang_type_system);
174   m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
175       type_system, clang_type_system->GetTranslationUnitDecl());
176 }
177 
178 uint32_t SymbolFilePDB::GetNumCompileUnits() {
179   if (m_cached_compile_unit_count == 0) {
180     auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
181     if (!compilands)
182       return 0;
183 
184     // The linker could link *.dll (compiland language = LINK), or import
185     // *.dll. For example, a compiland with name `Import:KERNEL32.dll`
186     // could be found as a child of the global scope (PDB executable).
187     // Usually, such compilands contain `thunk` symbols in which we are not
188     // interested for now. However we still count them in the compiland list.
189     // If we perform any compiland related activity, like finding symbols
190     // through llvm::pdb::IPDBSession methods, such compilands will all be
191     // searched automatically no matter whether we include them or not.
192     m_cached_compile_unit_count = compilands->getChildCount();
193 
194     // The linker can inject an additional "dummy" compilation unit into the
195     // PDB. Ignore this special compile unit for our purposes, if it is there.
196     // It is always the last one.
197     auto last_compiland_up =
198         compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
199     lldbassert(last_compiland_up.get());
200     std::string name = last_compiland_up->getName();
201     if (name == "* Linker *")
202       --m_cached_compile_unit_count;
203   }
204   return m_cached_compile_unit_count;
205 }
206 
207 void SymbolFilePDB::GetCompileUnitIndex(
208     const llvm::pdb::PDBSymbolCompiland *pdb_compiland,
209     uint32_t &index) {
210   if (!pdb_compiland)
211     return;
212 
213   auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
214   if (!results_up)
215     return;
216   auto uid = pdb_compiland->getSymIndexId();
217   for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
218     auto compiland_up = results_up->getChildAtIndex(cu_idx);
219     if (!compiland_up)
220       continue;
221     if (compiland_up->getSymIndexId() == uid) {
222       index = cu_idx;
223       return;
224     }
225   }
226   index = UINT32_MAX;
227   return;
228 }
229 
230 std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
231 SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
232   return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
233 }
234 
235 lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
236   if (index >= GetNumCompileUnits())
237     return CompUnitSP();
238 
239   // Assuming we always retrieve same compilands listed in same order through
240   // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
241   // compile unit makes no sense.
242   auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
243   if (!results)
244     return CompUnitSP();
245   auto compiland_up = results->getChildAtIndex(index);
246   if (!compiland_up)
247     return CompUnitSP();
248   return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
249 }
250 
251 lldb::LanguageType
252 SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) {
253   // What fields should I expect to be filled out on the SymbolContext?  Is it
254   // safe to assume that `sc.comp_unit` is valid?
255   if (!sc.comp_unit)
256     return lldb::eLanguageTypeUnknown;
257 
258   auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
259   if (!compiland_up)
260     return lldb::eLanguageTypeUnknown;
261   auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
262   if (!details)
263     return lldb::eLanguageTypeUnknown;
264   return TranslateLanguage(details->getLanguage());
265 }
266 
267 lldb_private::Function *
268 SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(
269     const PDBSymbolFunc *pdb_func,
270     const lldb_private::SymbolContext &sc) {
271   assert(pdb_func != nullptr);
272   lldbassert(sc.comp_unit && sc.module_sp.get());
273 
274   auto file_vm_addr = pdb_func->getVirtualAddress();
275   if (file_vm_addr == LLDB_INVALID_ADDRESS)
276     return nullptr;
277 
278   auto func_length = pdb_func->getLength();
279   AddressRange func_range = AddressRange(file_vm_addr,
280                                          func_length,
281                                          sc.module_sp->GetSectionList());
282   if (!func_range.GetBaseAddress().IsValid())
283     return nullptr;
284 
285   lldb_private::Type* func_type = ResolveTypeUID(pdb_func->getSymIndexId());
286   if (!func_type)
287     return nullptr;
288 
289   user_id_t func_type_uid = pdb_func->getSignatureId();
290 
291   Mangled mangled = GetMangledForPDBFunc(pdb_func);
292 
293   FunctionSP func_sp = std::make_shared<Function>(sc.comp_unit,
294                                                   pdb_func->getSymIndexId(),
295                                                   func_type_uid,
296                                                   mangled,
297                                                   func_type,
298                                                   func_range);
299 
300   sc.comp_unit->AddFunction(func_sp);
301   return func_sp.get();
302 }
303 
304 size_t SymbolFilePDB::ParseCompileUnitFunctions(
305     const lldb_private::SymbolContext &sc) {
306   lldbassert(sc.comp_unit);
307   size_t func_added = 0;
308   auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
309   if (!compiland_up)
310     return 0;
311   auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
312   if (!results_up)
313     return 0;
314   while (auto pdb_func_up = results_up->getNext()) {
315     auto func_sp =
316         sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId());
317     if (!func_sp) {
318       if (ParseCompileUnitFunctionForPDBFunc(pdb_func_up.get(), sc))
319         ++func_added;
320     }
321   }
322   return func_added;
323 }
324 
325 bool SymbolFilePDB::ParseCompileUnitLineTable(
326     const lldb_private::SymbolContext &sc) {
327   lldbassert(sc.comp_unit);
328   if (sc.comp_unit->GetLineTable())
329     return true;
330   return ParseCompileUnitLineTable(sc, 0);
331 }
332 
333 bool SymbolFilePDB::ParseCompileUnitDebugMacros(
334     const lldb_private::SymbolContext &sc) {
335   // PDB doesn't contain information about macros
336   return false;
337 }
338 
339 bool SymbolFilePDB::ParseCompileUnitSupportFiles(
340     const lldb_private::SymbolContext &sc,
341     lldb_private::FileSpecList &support_files) {
342   lldbassert(sc.comp_unit);
343 
344   // In theory this is unnecessary work for us, because all of this information
345   // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
346   // second time seems like a waste.  Unfortunately, there's no good way around
347   // this short of a moderate refactor since SymbolVendor depends on being able
348   // to cache this list.
349   auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
350   if (!compiland_up)
351     return false;
352   auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
353   if (!files || files->getChildCount() == 0)
354     return false;
355 
356   while (auto file = files->getNext()) {
357     FileSpec spec(file->getFileName(), false, FileSpec::ePathSyntaxWindows);
358     support_files.AppendIfUnique(spec);
359   }
360   return true;
361 }
362 
363 bool SymbolFilePDB::ParseImportedModules(
364     const lldb_private::SymbolContext &sc,
365     std::vector<lldb_private::ConstString> &imported_modules) {
366   // PDB does not yet support module debug info
367   return false;
368 }
369 
370 static size_t
371 ParseFunctionBlocksForPDBSymbol(const lldb_private::SymbolContext &sc,
372                                 uint64_t func_file_vm_addr,
373                                 const llvm::pdb::PDBSymbol *pdb_symbol,
374                                 lldb_private::Block *parent_block,
375                                 bool is_top_parent) {
376   assert(pdb_symbol && parent_block);
377 
378   size_t num_added = 0;
379   switch (pdb_symbol->getSymTag()) {
380   case PDB_SymType::Block:
381   case PDB_SymType::Function: {
382     Block *block = nullptr;
383     auto &raw_sym = pdb_symbol->getRawSymbol();
384     if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
385       if (pdb_func->hasNoInlineAttribute())
386         break;
387       if (is_top_parent)
388         block = parent_block;
389       else
390         break;
391     } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
392       auto uid = pdb_symbol->getSymIndexId();
393       if (parent_block->FindBlockByID(uid))
394         break;
395       if (raw_sym.getVirtualAddress() < func_file_vm_addr)
396         break;
397 
398       auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
399       parent_block->AddChild(block_sp);
400       block = block_sp.get();
401     } else
402       llvm_unreachable("Unexpected PDB symbol!");
403 
404     block->AddRange(
405         Block::Range(raw_sym.getVirtualAddress() - func_file_vm_addr,
406                      raw_sym.getLength()));
407     block->FinalizeRanges();
408     ++num_added;
409 
410     auto results_up = pdb_symbol->findAllChildren();
411     if (!results_up)
412       break;
413     while (auto symbol_up = results_up->getNext()) {
414       num_added += ParseFunctionBlocksForPDBSymbol(sc, func_file_vm_addr,
415                                                    symbol_up.get(),
416                                                    block, false);
417     }
418   } break;
419   default: break;
420   }
421   return num_added;
422 }
423 
424 size_t
425 SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
426   lldbassert(sc.comp_unit && sc.function);
427   size_t num_added = 0;
428   auto uid = sc.function->GetID();
429   auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
430   if (!pdb_func_up)
431     return 0;
432   Block &parent_block = sc.function->GetBlock(false);
433   num_added =
434       ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(),
435                                       pdb_func_up.get(), &parent_block, true);
436   return num_added;
437 }
438 
439 size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
440   lldbassert(sc.module_sp.get());
441   size_t num_added = 0;
442   auto results_up = m_session_up->getGlobalScope()->findAllChildren();
443   if (!results_up)
444     return 0;
445   while (auto symbol_up = results_up->getNext()) {
446     switch (symbol_up->getSymTag()) {
447     case PDB_SymType::Enum:
448     case PDB_SymType::UDT:
449     case PDB_SymType::Typedef:
450       break;
451     default:
452       continue;
453     }
454 
455     auto type_uid = symbol_up->getSymIndexId();
456     if (m_types.find(type_uid) != m_types.end())
457       continue;
458 
459     // This should cause the type to get cached and stored in the `m_types`
460     // lookup.
461     if (!ResolveTypeUID(symbol_up->getSymIndexId()))
462       continue;
463 
464     ++num_added;
465   }
466   return num_added;
467 }
468 
469 size_t
470 SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
471   // TODO: Implement this
472   return size_t();
473 }
474 
475 lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
476   auto find_result = m_types.find(type_uid);
477   if (find_result != m_types.end())
478     return find_result->second.get();
479 
480   TypeSystem *type_system =
481       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
482   ClangASTContext *clang_type_system =
483       llvm::dyn_cast_or_null<ClangASTContext>(type_system);
484   if (!clang_type_system)
485     return nullptr;
486   PDBASTParser *pdb =
487       llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser());
488   if (!pdb)
489     return nullptr;
490 
491   auto pdb_type = m_session_up->getSymbolById(type_uid);
492   if (pdb_type == nullptr)
493     return nullptr;
494 
495   lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
496   if (result.get()) {
497     m_types.insert(std::make_pair(type_uid, result));
498     auto type_list = GetTypeList();
499     if (type_list)
500       type_list->Insert(result);
501   }
502   return result.get();
503 }
504 
505 bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
506   // TODO: Implement this
507   return false;
508 }
509 
510 lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
511   return lldb_private::CompilerDecl();
512 }
513 
514 lldb_private::CompilerDeclContext
515 SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
516   // PDB always uses the translation unit decl context for everything.  We can
517   // improve this later but it's not easy because PDB doesn't provide a high
518   // enough level of type fidelity in this area.
519   return *m_tu_decl_ctx_up;
520 }
521 
522 lldb_private::CompilerDeclContext
523 SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
524   return *m_tu_decl_ctx_up;
525 }
526 
527 void SymbolFilePDB::ParseDeclsForContext(
528     lldb_private::CompilerDeclContext decl_ctx) {}
529 
530 uint32_t
531 SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
532                                     uint32_t resolve_scope,
533                                     lldb_private::SymbolContext &sc) {
534   uint32_t resolved_flags = 0;
535   if (resolve_scope & eSymbolContextCompUnit ||
536       resolve_scope & eSymbolContextVariable ||
537       resolve_scope & eSymbolContextFunction ||
538       resolve_scope & eSymbolContextBlock ||
539       resolve_scope & eSymbolContextLineEntry) {
540     addr_t file_vm_addr = so_addr.GetFileAddress();
541     auto symbol_up =
542         m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::None);
543     if (!symbol_up)
544       return 0;
545 
546     auto cu_sp = GetCompileUnitContainsAddress(so_addr);
547     if (!cu_sp) {
548       if (resolved_flags | eSymbolContextVariable) {
549         // TODO: Resolve variables
550       }
551       return 0;
552     }
553     sc.comp_unit = cu_sp.get();
554     resolved_flags |= eSymbolContextCompUnit;
555     lldbassert(sc.module_sp == cu_sp->GetModule());
556 
557     switch (symbol_up->getSymTag()) {
558     case PDB_SymType::Function:
559       if (resolve_scope & eSymbolContextFunction) {
560         auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
561         assert(pdb_func);
562         auto func_uid = pdb_func->getSymIndexId();
563         sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
564         if (sc.function == nullptr)
565           sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
566         if (sc.function) {
567           resolved_flags |= eSymbolContextFunction;
568           if (resolve_scope & eSymbolContextBlock) {
569             Block &block = sc.function->GetBlock(true);
570             sc.block = block.FindBlockByID(sc.function->GetID());
571             if (sc.block)
572               resolved_flags |= eSymbolContextBlock;
573           }
574         }
575       }
576       break;
577     default:
578       break;
579     }
580 
581     if (resolve_scope & eSymbolContextLineEntry) {
582       if (auto *line_table = sc.comp_unit->GetLineTable()) {
583         Address addr(so_addr);
584         if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
585           resolved_flags |= eSymbolContextLineEntry;
586       }
587     }
588   }
589   return resolved_flags;
590 }
591 
592 std::string SymbolFilePDB::GetSourceFileNameForPDBCompiland(
593     const PDBSymbolCompiland *pdb_compiland) {
594   if (!pdb_compiland)
595     return std::string();
596 
597   std::string source_file_name;
598   // `getSourceFileName` returns the basename of the original source file
599   // used to generate this compiland.  It does not return the full path.
600   // Currently the only way to get that is to do a basename lookup to get the
601   // IPDBSourceFile, but this is ambiguous in the case of two source files
602   // with the same name contributing to the same compiland. This is an edge
603   // case that we ignore for now, although we need to a long-term solution.
604   std::string file_name = pdb_compiland->getSourceFileName();
605   if (!file_name.empty()) {
606     auto one_src_file_up =
607         m_session_up->findOneSourceFile(pdb_compiland, file_name,
608                                         PDB_NameSearchFlags::NS_CaseInsensitive);
609     if (one_src_file_up)
610       source_file_name = one_src_file_up->getFileName();
611   }
612   // For some reason, source file name could be empty, so we will walk through
613   // all source files of this compiland, and determine the right source file
614   // if any that is used to generate this compiland based on language
615   // indicated in compilanddetails language field.
616   if (!source_file_name.empty())
617     return source_file_name;
618 
619   auto details_up = pdb_compiland->findOneChild<PDBSymbolCompilandDetails>();
620   PDB_Lang pdb_lang = details_up ? details_up->getLanguage() : PDB_Lang::Cpp;
621   auto src_files_up =
622       m_session_up->getSourceFilesForCompiland(*pdb_compiland);
623   if (src_files_up) {
624     while (auto file_up = src_files_up->getNext()) {
625       FileSpec file_spec(file_up->getFileName(), false,
626                          FileSpec::ePathSyntaxWindows);
627       auto file_extension = file_spec.GetFileNameExtension();
628       if (pdb_lang == PDB_Lang::Cpp || pdb_lang == PDB_Lang::C) {
629         static const char* exts[] = { "cpp", "c", "cc", "cxx" };
630         if (llvm::is_contained(exts, file_extension.GetStringRef().lower()))
631           source_file_name = file_up->getFileName();
632         break;
633       } else if (pdb_lang == PDB_Lang::Masm &&
634                  ConstString::Compare(file_extension, ConstString("ASM"),
635                                       false) == 0) {
636         source_file_name = file_up->getFileName();
637         break;
638       }
639     }
640   }
641   return source_file_name;
642 }
643 
644 uint32_t SymbolFilePDB::ResolveSymbolContext(
645     const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
646     uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) {
647   const size_t old_size = sc_list.GetSize();
648   if (resolve_scope & lldb::eSymbolContextCompUnit) {
649     // Locate all compilation units with line numbers referencing the specified
650     // file.  For example, if `file_spec` is <vector>, then this should return
651     // all source files and header files that reference <vector>, either
652     // directly or indirectly.
653     auto compilands = m_session_up->findCompilandsForSourceFile(
654         file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
655 
656     if (!compilands)
657       return 0;
658 
659     // For each one, either find its previously parsed data or parse it afresh
660     // and add it to the symbol context list.
661     while (auto compiland = compilands->getNext()) {
662       // If we're not checking inlines, then don't add line information for this
663       // file unless the FileSpec matches.
664       // For inline functions, we don't have to match the FileSpec since they
665       // could be defined in headers other than file specified in FileSpec.
666       if (!check_inlines) {
667         // `getSourceFileName` returns the basename of the original source file
668         // used to generate this compiland.  It does not return the full path.
669         // Currently the only way to get that is to do a basename lookup to get
670         // the IPDBSourceFile, but this is ambiguous in the case of two source
671         // files with the same name contributing to the same compiland.  This is
672         // a moderately extreme edge case, so we consider this OK for now,
673         // although we need to find a long-term solution.
674         std::string source_file =
675             GetSourceFileNameForPDBCompiland(compiland.get());
676         if (source_file.empty())
677           continue;
678         FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows);
679         bool need_full_match = !file_spec.GetDirectory().IsEmpty();
680         if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
681           continue;
682       }
683 
684       SymbolContext sc;
685       auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
686       if (!cu.get())
687         continue;
688       sc.comp_unit = cu.get();
689       sc.module_sp = cu->GetModule();
690 
691       // If we were asked to resolve line entries, add all entries to the line
692       // table that match the requested line (or all lines if `line` == 0).
693       if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
694                            eSymbolContextLineEntry)) {
695         bool has_line_table = ParseCompileUnitLineTable(sc, line);
696 
697         if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
698           // The query asks for line entries, but we can't get them for the
699           // compile unit. This is not normal for `line` = 0. So just assert it.
700           assert(line && "Couldn't get all line entries!\n");
701 
702           // Current compiland does not have the requested line. Search next.
703           continue;
704         }
705 
706         if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
707           if (!has_line_table)
708             continue;
709 
710           auto *line_table = sc.comp_unit->GetLineTable();
711           lldbassert(line_table);
712 
713           uint32_t num_line_entries = line_table->GetSize();
714           // Skip the terminal line entry.
715           --num_line_entries;
716 
717           // If `line `!= 0, see if we can resolve function for each line
718           // entry in the line table.
719           for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
720                ++line_idx) {
721             if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
722               continue;
723 
724             auto file_vm_addr =
725                 sc.line_entry.range.GetBaseAddress().GetFileAddress();
726             if (file_vm_addr == LLDB_INVALID_ADDRESS)
727               continue;
728 
729             auto symbol_up =
730                 m_session_up->findSymbolByAddress(file_vm_addr,
731                                                   PDB_SymType::Function);
732             if (symbol_up) {
733               auto func_uid = symbol_up->getSymIndexId();
734               sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
735               if (sc.function == nullptr) {
736                 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
737                 assert(pdb_func);
738                 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
739               }
740               if (sc.function && (resolve_scope & eSymbolContextBlock)) {
741                 Block &block = sc.function->GetBlock(true);
742                 sc.block = block.FindBlockByID(sc.function->GetID());
743               }
744             }
745             sc_list.Append(sc);
746           }
747         } else if (has_line_table) {
748           // We can parse line table for the compile unit. But no query to
749           // resolve function or block. We append `sc` to the list anyway.
750           sc_list.Append(sc);
751         }
752       } else {
753         // No query for line entry, function or block. But we have a valid
754         // compile unit, append `sc` to the list.
755         sc_list.Append(sc);
756       }
757     }
758   }
759   return sc_list.GetSize() - old_size;
760 }
761 
762 uint32_t SymbolFilePDB::FindGlobalVariables(
763     const lldb_private::ConstString &name,
764     const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
765     uint32_t max_matches, lldb_private::VariableList &variables) {
766   return uint32_t();
767 }
768 
769 uint32_t
770 SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
771                                    bool append, uint32_t max_matches,
772                                    lldb_private::VariableList &variables) {
773   return uint32_t();
774 }
775 
776 bool SymbolFilePDB::ResolveFunction(llvm::pdb::PDBSymbolFunc *pdb_func,
777                                     bool include_inlines,
778                                     lldb_private::SymbolContextList &sc_list) {
779   if (!pdb_func)
780     return false;
781   lldb_private::SymbolContext sc;
782   auto file_vm_addr = pdb_func->getVirtualAddress();
783   if (file_vm_addr == LLDB_INVALID_ADDRESS)
784     return false;
785 
786   Address so_addr(file_vm_addr);
787   sc.comp_unit = GetCompileUnitContainsAddress(so_addr).get();
788   if (!sc.comp_unit)
789     return false;
790   sc.module_sp = sc.comp_unit->GetModule();
791   auto symbol_up =
792       m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
793   if (!symbol_up)
794     return false;
795 
796   auto *func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
797   assert(func);
798   sc.function = ParseCompileUnitFunctionForPDBFunc(func, sc);
799   if (!sc.function)
800     return false;
801 
802   sc_list.Append(sc);
803   return true;
804 }
805 
806 bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
807                                     lldb_private::SymbolContextList &sc_list) {
808   auto pdb_func_up =
809       m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
810   if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
811     return false;
812   return ResolveFunction(pdb_func_up.get(), include_inlines, sc_list);
813 }
814 
815 void SymbolFilePDB::CacheFunctionNames() {
816   if (!m_func_full_names.IsEmpty())
817     return;
818 
819   std::map<uint64_t, uint32_t> addr_ids;
820 
821   if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
822     while (auto pdb_func_up = results_up->getNext()) {
823       if (pdb_func_up->isCompilerGenerated())
824         continue;
825 
826       auto name = pdb_func_up->getName();
827       auto demangled_name = pdb_func_up->getUndecoratedName();
828       if (name.empty() && demangled_name.empty())
829         continue;
830 
831       auto uid = pdb_func_up->getSymIndexId();
832       if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
833         addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
834 
835       if (auto parent = pdb_func_up->getClassParent()) {
836 
837         // PDB have symbols for class/struct methods or static methods in Enum
838         // Class. We won't bother to check if the parent is UDT or Enum here.
839         m_func_method_names.Append(ConstString(name), uid);
840 
841         ConstString cstr_name(name);
842 
843         // To search a method name, like NS::Class:MemberFunc, LLDB searches its
844         // base name, i.e. MemberFunc by default. Since PDBSymbolFunc does not
845         // have inforamtion of this, we extract base names and cache them by our
846         // own effort.
847         llvm::StringRef basename;
848         CPlusPlusLanguage::MethodName cpp_method(cstr_name);
849         if (cpp_method.IsValid()) {
850           llvm::StringRef context;
851           basename = cpp_method.GetBasename();
852           if (basename.empty())
853             CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(),
854                                                            context, basename);
855         }
856 
857         if (!basename.empty())
858           m_func_base_names.Append(ConstString(basename), uid);
859         else {
860           m_func_base_names.Append(ConstString(name), uid);
861         }
862 
863         if (!demangled_name.empty())
864           m_func_full_names.Append(ConstString(demangled_name), uid);
865 
866       } else {
867         // Handle not-method symbols.
868 
869         // The function name might contain namespace, or its lexical scope. It
870         // is not safe to get its base name by applying same scheme as we deal
871         // with the method names.
872         // FIXME: Remove namespace if function is static in a scope.
873         m_func_base_names.Append(ConstString(name), uid);
874 
875         if (name == "main") {
876           m_func_full_names.Append(ConstString(name), uid);
877 
878           if (!demangled_name.empty() && name != demangled_name) {
879             m_func_full_names.Append(ConstString(demangled_name), uid);
880             m_func_base_names.Append(ConstString(demangled_name), uid);
881           }
882         } else if (!demangled_name.empty()) {
883           m_func_full_names.Append(ConstString(demangled_name), uid);
884         } else {
885           m_func_full_names.Append(ConstString(name), uid);
886         }
887       }
888     }
889   }
890 
891   if (auto results_up =
892       m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
893     while (auto pub_sym_up = results_up->getNext()) {
894       if (!pub_sym_up->isFunction())
895         continue;
896       auto name = pub_sym_up->getName();
897       if (name.empty())
898         continue;
899 
900       if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
901         auto vm_addr = pub_sym_up->getVirtualAddress();
902 
903         // PDB public symbol has mangled name for its associated function.
904         if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
905           // Cache mangled name.
906           m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
907         }
908       }
909     }
910   }
911   // Sort them before value searching is working properly
912   m_func_full_names.Sort();
913   m_func_full_names.SizeToFit();
914   m_func_method_names.Sort();
915   m_func_method_names.SizeToFit();
916   m_func_base_names.Sort();
917   m_func_base_names.SizeToFit();
918 }
919 
920 uint32_t SymbolFilePDB::FindFunctions(
921     const lldb_private::ConstString &name,
922     const lldb_private::CompilerDeclContext *parent_decl_ctx,
923     uint32_t name_type_mask, bool include_inlines, bool append,
924     lldb_private::SymbolContextList &sc_list) {
925   if (!append)
926     sc_list.Clear();
927   lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
928 
929   if (name_type_mask == eFunctionNameTypeNone)
930     return 0;
931   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
932     return 0;
933   if (name.IsEmpty())
934     return 0;
935 
936   auto old_size = sc_list.GetSize();
937   if (name_type_mask & eFunctionNameTypeFull ||
938       name_type_mask & eFunctionNameTypeBase ||
939       name_type_mask & eFunctionNameTypeMethod) {
940     CacheFunctionNames();
941 
942     std::set<uint32_t> resolved_ids;
943     auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids, this] (
944         UniqueCStringMap<uint32_t> &Names)
945     {
946       std::vector<uint32_t> ids;
947       if (Names.GetValues(name, ids)) {
948         for (auto id : ids) {
949           if (resolved_ids.find(id) == resolved_ids.end()) {
950             if (ResolveFunction(id, include_inlines, sc_list))
951               resolved_ids.insert(id);
952           }
953         }
954       }
955     };
956     if (name_type_mask & eFunctionNameTypeFull) {
957       ResolveFn(m_func_full_names);
958     }
959     if (name_type_mask & eFunctionNameTypeBase) {
960       ResolveFn(m_func_base_names);
961     }
962     if (name_type_mask & eFunctionNameTypeMethod) {
963       ResolveFn(m_func_method_names);
964     }
965   }
966   return sc_list.GetSize() - old_size;
967 }
968 
969 uint32_t
970 SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
971                              bool include_inlines, bool append,
972                              lldb_private::SymbolContextList &sc_list) {
973   if (!append)
974     sc_list.Clear();
975   if (!regex.IsValid())
976     return 0;
977 
978   auto old_size = sc_list.GetSize();
979   CacheFunctionNames();
980 
981   std::set<uint32_t> resolved_ids;
982   auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids, this] (
983       UniqueCStringMap<uint32_t> &Names)
984   {
985     std::vector<uint32_t> ids;
986     if (Names.GetValues(regex, ids)) {
987       for (auto id : ids) {
988         if (resolved_ids.find(id) == resolved_ids.end())
989           if (ResolveFunction(id, include_inlines, sc_list))
990             resolved_ids.insert(id);
991       }
992     }
993   };
994   ResolveFn(m_func_full_names);
995   ResolveFn(m_func_base_names);
996 
997   return sc_list.GetSize() - old_size;
998 }
999 
1000 void SymbolFilePDB::GetMangledNamesForFunction(
1001     const std::string &scope_qualified_name,
1002     std::vector<lldb_private::ConstString> &mangled_names) {}
1003 
1004 uint32_t SymbolFilePDB::FindTypes(
1005     const lldb_private::SymbolContext &sc,
1006     const lldb_private::ConstString &name,
1007     const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1008     uint32_t max_matches,
1009     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1010     lldb_private::TypeMap &types) {
1011   if (!append)
1012     types.Clear();
1013   if (!name)
1014     return 0;
1015   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1016     return 0;
1017 
1018   searched_symbol_files.clear();
1019   searched_symbol_files.insert(this);
1020 
1021   std::string name_str = name.AsCString();
1022 
1023   // There is an assumption 'name' is not a regex
1024   FindTypesByName(name_str, max_matches, types);
1025 
1026   return types.GetSize();
1027 }
1028 
1029 void
1030 SymbolFilePDB::FindTypesByRegex(const lldb_private::RegularExpression &regex,
1031                                 uint32_t max_matches,
1032                                 lldb_private::TypeMap &types) {
1033   // When searching by regex, we need to go out of our way to limit the search
1034   // space as much as possible since this searches EVERYTHING in the PDB,
1035   // manually doing regex comparisons.  PDB library isn't optimized for regex
1036   // searches or searches across multiple symbol types at the same time, so the
1037   // best we can do is to search enums, then typedefs, then classes one by one,
1038   // and do a regex comparison against each of them.
1039   PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1040                                   PDB_SymType::UDT};
1041   std::unique_ptr<IPDBEnumSymbols> results;
1042 
1043   uint32_t matches = 0;
1044 
1045   for (auto tag : tags_to_search) {
1046     results = m_global_scope_up->findAllChildren(tag);
1047     if (!results)
1048       continue;
1049 
1050     while (auto result = results->getNext()) {
1051       if (max_matches > 0 && matches >= max_matches)
1052         break;
1053 
1054       std::string type_name;
1055       if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1056         type_name = enum_type->getName();
1057       else if (auto typedef_type =
1058                llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
1059         type_name = typedef_type->getName();
1060       else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1061         type_name = class_type->getName();
1062       else {
1063         // We're looking only for types that have names.  Skip symbols, as well
1064         // as unnamed types such as arrays, pointers, etc.
1065         continue;
1066       }
1067 
1068       if (!regex.Execute(type_name))
1069         continue;
1070 
1071       // This should cause the type to get cached and stored in the `m_types`
1072       // lookup.
1073       if (!ResolveTypeUID(result->getSymIndexId()))
1074         continue;
1075 
1076       auto iter = m_types.find(result->getSymIndexId());
1077       if (iter == m_types.end())
1078         continue;
1079       types.Insert(iter->second);
1080       ++matches;
1081     }
1082   }
1083 }
1084 
1085 void SymbolFilePDB::FindTypesByName(const std::string &name,
1086                                     uint32_t max_matches,
1087                                     lldb_private::TypeMap &types) {
1088   std::unique_ptr<IPDBEnumSymbols> results;
1089   if (name.empty())
1090     return;
1091   results = m_global_scope_up->findChildren(PDB_SymType::None, name,
1092                                             PDB_NameSearchFlags::NS_Default);
1093   if (!results)
1094     return;
1095 
1096   uint32_t matches = 0;
1097 
1098   while (auto result = results->getNext()) {
1099     if (max_matches > 0 && matches >= max_matches)
1100       break;
1101     switch (result->getSymTag()) {
1102     case PDB_SymType::Enum:
1103     case PDB_SymType::UDT:
1104     case PDB_SymType::Typedef:
1105       break;
1106     default:
1107       // We're looking only for types that have names.  Skip symbols, as well as
1108       // unnamed types such as arrays, pointers, etc.
1109       continue;
1110     }
1111 
1112     // This should cause the type to get cached and stored in the `m_types`
1113     // lookup.
1114     if (!ResolveTypeUID(result->getSymIndexId()))
1115       continue;
1116 
1117     auto iter = m_types.find(result->getSymIndexId());
1118     if (iter == m_types.end())
1119       continue;
1120     types.Insert(iter->second);
1121     ++matches;
1122   }
1123 }
1124 
1125 size_t SymbolFilePDB::FindTypes(
1126     const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1127     lldb_private::TypeMap &types) {
1128   return 0;
1129 }
1130 
1131 lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1132   return m_obj_file->GetModule()->GetTypeList();
1133 }
1134 
1135 void
1136 SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol *pdb_symbol,
1137                                     uint32_t type_mask,
1138                                     TypeCollection &type_collection) {
1139   if (!pdb_symbol)
1140     return;
1141 
1142   bool can_parse = false;
1143   switch (pdb_symbol->getSymTag()) {
1144   case PDB_SymType::ArrayType:
1145     can_parse = ((type_mask & eTypeClassArray) != 0);
1146     break;
1147   case PDB_SymType::BuiltinType:
1148     can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1149     break;
1150   case PDB_SymType::Enum:
1151     can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1152     break;
1153   case PDB_SymType::Function:
1154   case PDB_SymType::FunctionSig:
1155     can_parse = ((type_mask & eTypeClassFunction) != 0);
1156     break;
1157   case PDB_SymType::PointerType:
1158     can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1159                                eTypeClassMemberPointer)) != 0);
1160     break;
1161   case PDB_SymType::Typedef:
1162     can_parse = ((type_mask & eTypeClassTypedef) != 0);
1163     break;
1164   case PDB_SymType::UDT: {
1165     auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(pdb_symbol);
1166     assert(udt);
1167     can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
1168         ((type_mask & (eTypeClassClass | eTypeClassStruct |
1169                        eTypeClassUnion)) != 0));
1170   } break;
1171   default:break;
1172   }
1173 
1174   if (can_parse) {
1175     if (auto *type = ResolveTypeUID(pdb_symbol->getSymIndexId())) {
1176       auto result =
1177           std::find(type_collection.begin(), type_collection.end(), type);
1178       if (result == type_collection.end())
1179         type_collection.push_back(type);
1180     }
1181   }
1182 
1183   auto results_up = pdb_symbol->findAllChildren();
1184   while (auto symbol_up = results_up->getNext())
1185     GetTypesForPDBSymbol(symbol_up.get(), type_mask, type_collection);
1186 }
1187 
1188 size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1189                                uint32_t type_mask,
1190                                lldb_private::TypeList &type_list) {
1191   TypeCollection type_collection;
1192   uint32_t old_size = type_list.GetSize();
1193   CompileUnit *cu = sc_scope ?
1194       sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
1195   if (cu) {
1196     auto compiland_up = GetPDBCompilandByUID(cu->GetID());
1197     GetTypesForPDBSymbol(compiland_up.get(), type_mask, type_collection);
1198   } else {
1199     for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1200       auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
1201       if (cu_sp.get()) {
1202         auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID());
1203         GetTypesForPDBSymbol(compiland_up.get(), type_mask, type_collection);
1204       }
1205     }
1206   }
1207 
1208   for (auto type : type_collection) {
1209     type->GetForwardCompilerType();
1210     type_list.Insert(type->shared_from_this());
1211   }
1212   return type_list.GetSize() - old_size;
1213 }
1214 
1215 lldb_private::TypeSystem *
1216 SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1217   auto type_system =
1218       m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1219   if (type_system)
1220     type_system->SetSymbolFile(this);
1221   return type_system;
1222 }
1223 
1224 lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1225     const lldb_private::SymbolContext &sc,
1226     const lldb_private::ConstString &name,
1227     const lldb_private::CompilerDeclContext *parent_decl_ctx) {
1228   return lldb_private::CompilerDeclContext();
1229 }
1230 
1231 lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1232   static ConstString g_name("pdb");
1233   return g_name;
1234 }
1235 
1236 uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1237 
1238 IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1239 
1240 const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1241   return *m_session_up;
1242 }
1243 
1244 lldb::CompUnitSP
1245 SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, uint32_t index) {
1246   auto found_cu = m_comp_units.find(id);
1247   if (found_cu != m_comp_units.end())
1248     return found_cu->second;
1249 
1250   auto compiland_up = GetPDBCompilandByUID(id);
1251   if (!compiland_up)
1252     return CompUnitSP();
1253 
1254   lldb::LanguageType lang;
1255   auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
1256   if (!details)
1257     lang = lldb::eLanguageTypeC_plus_plus;
1258   else
1259     lang = TranslateLanguage(details->getLanguage());
1260 
1261   if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1262     return CompUnitSP();
1263 
1264   std::string path = GetSourceFileNameForPDBCompiland(compiland_up.get());
1265   if (path.empty())
1266     return CompUnitSP();
1267 
1268   // Don't support optimized code for now, DebugInfoPDB does not return this
1269   // information.
1270   LazyBool optimized = eLazyBoolNo;
1271   auto cu_sp = std::make_shared<CompileUnit>(
1272       m_obj_file->GetModule(), nullptr, path.c_str(), id, lang, optimized);
1273 
1274   if (!cu_sp)
1275     return CompUnitSP();
1276 
1277   m_comp_units.insert(std::make_pair(id, cu_sp));
1278   if (index == UINT32_MAX)
1279     GetCompileUnitIndex(compiland_up.get(), index);
1280   lldbassert(index != UINT32_MAX);
1281   m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
1282       index, cu_sp);
1283   return cu_sp;
1284 }
1285 
1286 bool SymbolFilePDB::ParseCompileUnitLineTable(
1287     const lldb_private::SymbolContext &sc, uint32_t match_line) {
1288   lldbassert(sc.comp_unit);
1289 
1290   auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1291   if (!compiland_up)
1292     return false;
1293 
1294   // LineEntry needs the *index* of the file into the list of support files
1295   // returned by ParseCompileUnitSupportFiles.  But the underlying SDK gives us
1296   // a globally unique idenfitifier in the namespace of the PDB.  So, we have to
1297   // do a mapping so that we can hand out indices.
1298   llvm::DenseMap<uint32_t, uint32_t> index_map;
1299   BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
1300   auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
1301 
1302   // Find contributions to `compiland` from all source and header files.
1303   std::string path = sc.comp_unit->GetPath();
1304   auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1305   if (!files)
1306     return false;
1307 
1308   // For each source and header file, create a LineSequence for contributions to
1309   // the compiland from that file, and add the sequence.
1310   while (auto file = files->getNext()) {
1311     std::unique_ptr<LineSequence> sequence(
1312         line_table->CreateLineSequenceContainer());
1313     auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1314     if (!lines)
1315       continue;
1316     int entry_count = lines->getChildCount();
1317 
1318     uint64_t prev_addr;
1319     uint32_t prev_length;
1320     uint32_t prev_line;
1321     uint32_t prev_source_idx;
1322 
1323     for (int i = 0; i < entry_count; ++i) {
1324       auto line = lines->getChildAtIndex(i);
1325 
1326       uint64_t lno = line->getLineNumber();
1327       uint64_t addr = line->getVirtualAddress();
1328       uint32_t length = line->getLength();
1329       uint32_t source_id = line->getSourceFileId();
1330       uint32_t col = line->getColumnNumber();
1331       uint32_t source_idx = index_map[source_id];
1332 
1333       // There was a gap between the current entry and the previous entry if the
1334       // addresses don't perfectly line up.
1335       bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
1336 
1337       // Before inserting the current entry, insert a terminal entry at the end
1338       // of the previous entry's address range if the current entry resulted in
1339       // a gap from the previous entry.
1340       if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1341         line_table->AppendLineEntryToSequence(
1342             sequence.get(), prev_addr + prev_length, prev_line, 0,
1343             prev_source_idx, false, false, false, false, true);
1344       }
1345 
1346       if (ShouldAddLine(match_line, lno, length)) {
1347         bool is_statement = line->isStatement();
1348         bool is_prologue = false;
1349         bool is_epilogue = false;
1350         auto func =
1351             m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1352         if (func) {
1353           auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
1354           if (prologue)
1355             is_prologue = (addr == prologue->getVirtualAddress());
1356 
1357           auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
1358           if (epilogue)
1359             is_epilogue = (addr == epilogue->getVirtualAddress());
1360         }
1361 
1362         line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1363                                               source_idx, is_statement, false,
1364                                               is_prologue, is_epilogue, false);
1365       }
1366 
1367       prev_addr = addr;
1368       prev_length = length;
1369       prev_line = lno;
1370       prev_source_idx = source_idx;
1371     }
1372 
1373     if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1374       // The end is always a terminal entry, so insert it regardless.
1375       line_table->AppendLineEntryToSequence(
1376           sequence.get(), prev_addr + prev_length, prev_line, 0,
1377           prev_source_idx, false, false, false, false, true);
1378     }
1379 
1380     line_table->InsertSequence(sequence.release());
1381   }
1382 
1383   if (line_table->GetSize()) {
1384     sc.comp_unit->SetLineTable(line_table.release());
1385     return true;
1386   }
1387   return false;
1388 }
1389 
1390 void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
1391     const PDBSymbolCompiland &compiland,
1392     llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
1393   // This is a hack, but we need to convert the source id into an index into the
1394   // support files array.  We don't want to do path comparisons to avoid
1395   // basename / full path issues that may or may not even be a problem, so we
1396   // use the globally unique source file identifiers.  Ideally we could use the
1397   // global identifiers everywhere, but LineEntry currently assumes indices.
1398   auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1399   if (!source_files)
1400     return;
1401   int index = 0;
1402 
1403   while (auto file = source_files->getNext()) {
1404     uint32_t source_id = file->getUniqueId();
1405     index_map[source_id] = index++;
1406   }
1407 }
1408 
1409 lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
1410      const lldb_private::Address &so_addr) {
1411   lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1412   if (file_vm_addr == LLDB_INVALID_ADDRESS)
1413     return nullptr;
1414 
1415   auto lines_up =
1416       m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/200);
1417   if (!lines_up)
1418     return nullptr;
1419 
1420   auto first_line_up = lines_up->getNext();
1421   if (!first_line_up)
1422     return nullptr;
1423   auto compiland_up = GetPDBCompilandByUID(first_line_up->getCompilandId());
1424   if (compiland_up) {
1425     return ParseCompileUnitForUID(compiland_up->getSymIndexId());
1426   }
1427 
1428   return nullptr;
1429 }
1430 
1431 Mangled
1432 SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc *pdb_func) {
1433   Mangled mangled;
1434   if (!pdb_func)
1435     return mangled;
1436 
1437   auto func_name = pdb_func->getName();
1438   auto func_undecorated_name = pdb_func->getUndecoratedName();
1439   std::string func_decorated_name;
1440 
1441   // Seek from public symbols for non-static function's decorated name if any.
1442   // For static functions, they don't have undecorated names and aren't exposed
1443   // in Public Symbols either.
1444   if (!func_undecorated_name.empty()) {
1445     auto result_up =
1446         m_global_scope_up->findChildren(PDB_SymType::PublicSymbol,
1447                                         func_undecorated_name,
1448                                         PDB_NameSearchFlags::NS_UndecoratedName);
1449     if (result_up) {
1450       while (auto symbol_up = result_up->getNext()) {
1451         // For a public symbol, it is unique.
1452         lldbassert(result_up->getChildCount() == 1);
1453         if (auto *pdb_public_sym =
1454             llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(symbol_up.get())) {
1455           if (pdb_public_sym->isFunction()) {
1456             func_decorated_name = pdb_public_sym->getName();
1457             break;
1458           }
1459         }
1460       }
1461     }
1462   }
1463   if (!func_decorated_name.empty()) {
1464     mangled.SetMangledName(ConstString(func_decorated_name));
1465 
1466     // For MSVC, format of C funciton's decorated name depends on calling
1467     // conventon. Unfortunately none of the format is recognized by current
1468     // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
1469     // `__purecall` is retrieved as both its decorated and
1470     // undecorated name (using PDBSymbolFunc::getUndecoratedName method).
1471     // However `__purecall` string is not treated as mangled in LLDB
1472     // (neither `?` nor `_Z` prefix). Mangled::GetDemangledName method
1473     // will fail internally and caches an empty string as its undecorated
1474     // name. So we will face a contradition here for the same symbol:
1475     //   non-empty undecorated name from PDB
1476     //   empty undecorated name from LLDB
1477     if (!func_undecorated_name.empty() &&
1478         mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1479       mangled.SetDemangledName(ConstString(func_undecorated_name));
1480 
1481     // LLDB uses several flags to control how a C++ decorated name is
1482     // undecorated for MSVC. See `safeUndecorateName` in Class Mangled.
1483     // So the yielded name could be different from what we retrieve from
1484     // PDB source unless we also apply same flags in getting undecorated
1485     // name through PDBSymbolFunc::getUndecoratedNameEx method.
1486     if (!func_undecorated_name.empty() &&
1487         mangled.GetDemangledName(mangled.GuessLanguage()) !=
1488             ConstString(func_undecorated_name))
1489       mangled.SetDemangledName(ConstString(func_undecorated_name));
1490   } else if (!func_undecorated_name.empty()) {
1491     mangled.SetDemangledName(ConstString(func_undecorated_name));
1492   } else if (!func_name.empty())
1493     mangled.SetValue(ConstString(func_name), false);
1494 
1495   return mangled;
1496 }
1497 
1498 bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1499     const lldb_private::CompilerDeclContext *decl_ctx) {
1500   if (decl_ctx == nullptr || !decl_ctx->IsValid())
1501     return true;
1502 
1503   TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1504   if (!decl_ctx_type_system)
1505     return false;
1506   TypeSystem *type_system = GetTypeSystemForLanguage(
1507       decl_ctx_type_system->GetMinimumLanguage(nullptr));
1508   if (decl_ctx_type_system == type_system)
1509     return true; // The type systems match, return true
1510 
1511   return false;
1512 }
1513