174e08ca0SZachary Turner //===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===//
274e08ca0SZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
674e08ca0SZachary Turner //
774e08ca0SZachary Turner //===----------------------------------------------------------------------===//
874e08ca0SZachary Turner 
974e08ca0SZachary Turner #include "SymbolFilePDB.h"
1074e08ca0SZachary Turner 
11c1e530eeSAleksandr Urakov #include "PDBASTParser.h"
12c1e530eeSAleksandr Urakov #include "PDBLocationToDWARFExpression.h"
13c1e530eeSAleksandr Urakov 
1442dff790SZachary Turner #include "clang/Lex/Lexer.h"
1542dff790SZachary Turner 
1674e08ca0SZachary Turner #include "lldb/Core/Module.h"
1774e08ca0SZachary Turner #include "lldb/Core/PluginManager.h"
1842dff790SZachary Turner #include "lldb/Symbol/ClangASTContext.h"
1974e08ca0SZachary Turner #include "lldb/Symbol/CompileUnit.h"
2074e08ca0SZachary Turner #include "lldb/Symbol/LineTable.h"
2174e08ca0SZachary Turner #include "lldb/Symbol/ObjectFile.h"
2274e08ca0SZachary Turner #include "lldb/Symbol/SymbolContext.h"
2310a02577SAaron Smith #include "lldb/Symbol/SymbolVendor.h"
24ec40f818SAaron Smith #include "lldb/Symbol/TypeList.h"
25308e39caSAaron Smith #include "lldb/Symbol/TypeMap.h"
26cab0d23fSAaron Smith #include "lldb/Symbol/Variable.h"
270e252e38SAlex Langford #include "lldb/Utility/Log.h"
2886e9434dSAaron Smith #include "lldb/Utility/RegularExpression.h"
2974e08ca0SZachary Turner 
30b8d8c62bSPavel Labath #include "llvm/DebugInfo/PDB/GenericError.h"
311f8552abSAaron Smith #include "llvm/DebugInfo/PDB/IPDBDataStream.h"
3274e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
3374e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
34308e39caSAaron Smith #include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
3574e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
361f8552abSAaron Smith #include "llvm/DebugInfo/PDB/IPDBTable.h"
3774e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbol.h"
387ac1c780SAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
3974e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
4074e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
411f8552abSAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
4274e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
4374e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
4474e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
4574e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
467ac1c780SAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
4742dff790SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
4842dff790SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
4942dff790SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
5042dff790SZachary Turner 
51672d2c12SJonas Devlieghere #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
52c1e530eeSAleksandr Urakov #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
53307f5ae8SZachary Turner #include "Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h"
5442dff790SZachary Turner 
5542dff790SZachary Turner #include <regex>
5674e08ca0SZachary Turner 
5710a02577SAaron Smith using namespace lldb;
5874e08ca0SZachary Turner using namespace lldb_private;
5954fd7ff6SZachary Turner using namespace llvm::pdb;
6074e08ca0SZachary Turner 
61b9c1b51eSKate Stone namespace {
62b9c1b51eSKate Stone lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
63b9c1b51eSKate Stone   switch (lang) {
6454fd7ff6SZachary Turner   case PDB_Lang::Cpp:
6574e08ca0SZachary Turner     return lldb::LanguageType::eLanguageTypeC_plus_plus;
6654fd7ff6SZachary Turner   case PDB_Lang::C:
6774e08ca0SZachary Turner     return lldb::LanguageType::eLanguageTypeC;
680561be6cSNathan Lanza   case PDB_Lang::Swift:
690561be6cSNathan Lanza     return lldb::LanguageType::eLanguageTypeSwift;
7074e08ca0SZachary Turner   default:
7174e08ca0SZachary Turner     return lldb::LanguageType::eLanguageTypeUnknown;
7274e08ca0SZachary Turner   }
7374e08ca0SZachary Turner }
747e8c7beaSZachary Turner 
75b9c1b51eSKate Stone bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
76b9c1b51eSKate Stone                    uint32_t addr_length) {
77b9c1b51eSKate Stone   return ((requested_line == 0 || actual_line == requested_line) &&
78b9c1b51eSKate Stone           addr_length > 0);
797e8c7beaSZachary Turner }
80c8316ed2SAaron Smith } // namespace
8174e08ca0SZachary Turner 
82307f5ae8SZachary Turner static bool ShouldUseNativeReader() {
8371970b72SGreg Clayton #if defined(_WIN32)
84307f5ae8SZachary Turner   llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER");
85307f5ae8SZachary Turner   return use_native.equals_lower("on") || use_native.equals_lower("yes") ||
86307f5ae8SZachary Turner          use_native.equals_lower("1") || use_native.equals_lower("true");
8771970b72SGreg Clayton #else
8871970b72SGreg Clayton   return true;
8971970b72SGreg Clayton #endif
90307f5ae8SZachary Turner }
91307f5ae8SZachary Turner 
92b9c1b51eSKate Stone void SymbolFilePDB::Initialize() {
93307f5ae8SZachary Turner   if (ShouldUseNativeReader()) {
94307f5ae8SZachary Turner     npdb::SymbolFileNativePDB::Initialize();
95307f5ae8SZachary Turner   } else {
96b9c1b51eSKate Stone     PluginManager::RegisterPlugin(GetPluginNameStatic(),
97b9c1b51eSKate Stone                                   GetPluginDescriptionStatic(), CreateInstance,
9874e08ca0SZachary Turner                                   DebuggerInitialize);
9974e08ca0SZachary Turner   }
100307f5ae8SZachary Turner }
10174e08ca0SZachary Turner 
102b9c1b51eSKate Stone void SymbolFilePDB::Terminate() {
103307f5ae8SZachary Turner   if (ShouldUseNativeReader()) {
104307f5ae8SZachary Turner     npdb::SymbolFileNativePDB::Terminate();
105307f5ae8SZachary Turner   } else {
10674e08ca0SZachary Turner     PluginManager::UnregisterPlugin(CreateInstance);
10774e08ca0SZachary Turner   }
108307f5ae8SZachary Turner }
10974e08ca0SZachary Turner 
110b9c1b51eSKate Stone void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
11174e08ca0SZachary Turner 
112b9c1b51eSKate Stone lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
11374e08ca0SZachary Turner   static ConstString g_name("pdb");
11474e08ca0SZachary Turner   return g_name;
11574e08ca0SZachary Turner }
11674e08ca0SZachary Turner 
117b9c1b51eSKate Stone const char *SymbolFilePDB::GetPluginDescriptionStatic() {
11874e08ca0SZachary Turner   return "Microsoft PDB debug symbol file reader.";
11974e08ca0SZachary Turner }
12074e08ca0SZachary Turner 
12174e08ca0SZachary Turner lldb_private::SymbolFile *
122*d2deeb44SPavel Labath SymbolFilePDB::CreateInstance(ObjectFileSP objfile_sp) {
123*d2deeb44SPavel Labath   return new SymbolFilePDB(std::move(objfile_sp));
12474e08ca0SZachary Turner }
12574e08ca0SZachary Turner 
126*d2deeb44SPavel Labath SymbolFilePDB::SymbolFilePDB(lldb::ObjectFileSP objfile_sp)
127*d2deeb44SPavel Labath     : SymbolFile(std::move(objfile_sp)), m_session_up(), m_global_scope_up() {}
12874e08ca0SZachary Turner 
129b9c1b51eSKate Stone SymbolFilePDB::~SymbolFilePDB() {}
13074e08ca0SZachary Turner 
131b9c1b51eSKate Stone uint32_t SymbolFilePDB::CalculateAbilities() {
1321f8552abSAaron Smith   uint32_t abilities = 0;
133*d2deeb44SPavel Labath   if (!m_objfile_sp)
1341f8552abSAaron Smith     return 0;
1351f8552abSAaron Smith 
136b9c1b51eSKate Stone   if (!m_session_up) {
13774e08ca0SZachary Turner     // Lazily load and match the PDB file, but only do this once.
138*d2deeb44SPavel Labath     std::string exePath = m_objfile_sp->GetFileSpec().GetPath();
139b9c1b51eSKate Stone     auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
140b9c1b51eSKate Stone                                 m_session_up);
141b9c1b51eSKate Stone     if (error) {
1424fd6a960SZachary Turner       llvm::consumeError(std::move(error));
143*d2deeb44SPavel Labath       auto module_sp = m_objfile_sp->GetModule();
1441f8552abSAaron Smith       if (!module_sp)
1451f8552abSAaron Smith         return 0;
1461f8552abSAaron Smith       // See if any symbol file is specified through `--symfile` option.
1471f8552abSAaron Smith       FileSpec symfile = module_sp->GetSymbolFileFileSpec();
1481f8552abSAaron Smith       if (!symfile)
1491f8552abSAaron Smith         return 0;
1501f8552abSAaron Smith       error = loadDataForPDB(PDB_ReaderType::DIA,
151c8316ed2SAaron Smith                              llvm::StringRef(symfile.GetPath()), m_session_up);
1521f8552abSAaron Smith       if (error) {
1531f8552abSAaron Smith         llvm::consumeError(std::move(error));
15474e08ca0SZachary Turner         return 0;
15574e08ca0SZachary Turner       }
156b8d8c62bSPavel Labath     }
1571f8552abSAaron Smith   }
158d5a925f4SAaron Smith   if (!m_session_up)
1591f8552abSAaron Smith     return 0;
1601f8552abSAaron Smith 
1611f8552abSAaron Smith   auto enum_tables_up = m_session_up->getEnumTables();
1621f8552abSAaron Smith   if (!enum_tables_up)
1631f8552abSAaron Smith     return 0;
1641f8552abSAaron Smith   while (auto table_up = enum_tables_up->getNext()) {
1651f8552abSAaron Smith     if (table_up->getItemCount() == 0)
1661f8552abSAaron Smith       continue;
1671f8552abSAaron Smith     auto type = table_up->getTableType();
1681f8552abSAaron Smith     switch (type) {
1691f8552abSAaron Smith     case PDB_TableType::Symbols:
1701f8552abSAaron Smith       // This table represents a store of symbols with types listed in
1711f8552abSAaron Smith       // PDBSym_Type
172c8316ed2SAaron Smith       abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
173c8316ed2SAaron Smith                     LocalVariables | VariableTypes);
1741f8552abSAaron Smith       break;
1751f8552abSAaron Smith     case PDB_TableType::LineNumbers:
1761f8552abSAaron Smith       abilities |= LineTables;
1771f8552abSAaron Smith       break;
178c8316ed2SAaron Smith     default:
179c8316ed2SAaron Smith       break;
1801f8552abSAaron Smith     }
1811f8552abSAaron Smith   }
1821f8552abSAaron Smith   return abilities;
18374e08ca0SZachary Turner }
18474e08ca0SZachary Turner 
185b9c1b51eSKate Stone void SymbolFilePDB::InitializeObject() {
186*d2deeb44SPavel Labath   lldb::addr_t obj_load_address =
187*d2deeb44SPavel Labath       m_objfile_sp->GetBaseAddress().GetFileAddress();
188c8316ed2SAaron Smith   lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
18974e08ca0SZachary Turner   m_session_up->setLoadAddress(obj_load_address);
19010a02577SAaron Smith   if (!m_global_scope_up)
19110a02577SAaron Smith     m_global_scope_up = m_session_up->getGlobalScope();
19210a02577SAaron Smith   lldbassert(m_global_scope_up.get());
19374e08ca0SZachary Turner }
19474e08ca0SZachary Turner 
195e0119909SPavel Labath uint32_t SymbolFilePDB::CalculateNumCompileUnits() {
19610a02577SAaron Smith   auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
19710a02577SAaron Smith   if (!compilands)
19810a02577SAaron Smith     return 0;
19910a02577SAaron Smith 
20010a02577SAaron Smith   // The linker could link *.dll (compiland language = LINK), or import
20105097246SAdrian Prantl   // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
20205097246SAdrian Prantl   // found as a child of the global scope (PDB executable). Usually, such
20305097246SAdrian Prantl   // compilands contain `thunk` symbols in which we are not interested for
20405097246SAdrian Prantl   // now. However we still count them in the compiland list. If we perform
20505097246SAdrian Prantl   // any compiland related activity, like finding symbols through
20605097246SAdrian Prantl   // llvm::pdb::IPDBSession methods, such compilands will all be searched
20705097246SAdrian Prantl   // automatically no matter whether we include them or not.
208e0119909SPavel Labath   uint32_t compile_unit_count = compilands->getChildCount();
20974e08ca0SZachary Turner 
210b9c1b51eSKate Stone   // The linker can inject an additional "dummy" compilation unit into the
2119d0eb996SAdrian McCarthy   // PDB. Ignore this special compile unit for our purposes, if it is there.
2129d0eb996SAdrian McCarthy   // It is always the last one.
213e0119909SPavel Labath   auto last_compiland_up = compilands->getChildAtIndex(compile_unit_count - 1);
21410a02577SAaron Smith   lldbassert(last_compiland_up.get());
21510a02577SAaron Smith   std::string name = last_compiland_up->getName();
21674e08ca0SZachary Turner   if (name == "* Linker *")
217e0119909SPavel Labath     --compile_unit_count;
218e0119909SPavel Labath   return compile_unit_count;
21974e08ca0SZachary Turner }
22074e08ca0SZachary Turner 
22110a02577SAaron Smith void SymbolFilePDB::GetCompileUnitIndex(
222c8316ed2SAaron Smith     const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
22310a02577SAaron Smith   auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
22410a02577SAaron Smith   if (!results_up)
22510a02577SAaron Smith     return;
226e664b5dcSAaron Smith   auto uid = pdb_compiland.getSymIndexId();
227fbdf0b93SRaphael Isemann   for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
22810a02577SAaron Smith     auto compiland_up = results_up->getChildAtIndex(cu_idx);
22910a02577SAaron Smith     if (!compiland_up)
23010a02577SAaron Smith       continue;
23110a02577SAaron Smith     if (compiland_up->getSymIndexId() == uid) {
23210a02577SAaron Smith       index = cu_idx;
23310a02577SAaron Smith       return;
23410a02577SAaron Smith     }
23510a02577SAaron Smith   }
23610a02577SAaron Smith   index = UINT32_MAX;
23710a02577SAaron Smith   return;
23810a02577SAaron Smith }
23910a02577SAaron Smith 
24010a02577SAaron Smith std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
24110a02577SAaron Smith SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
24210a02577SAaron Smith   return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
24310a02577SAaron Smith }
24410a02577SAaron Smith 
245b9c1b51eSKate Stone lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
24610a02577SAaron Smith   if (index >= GetNumCompileUnits())
24710a02577SAaron Smith     return CompUnitSP();
24874e08ca0SZachary Turner 
24910a02577SAaron Smith   // Assuming we always retrieve same compilands listed in same order through
25010a02577SAaron Smith   // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
25110a02577SAaron Smith   // compile unit makes no sense.
25210a02577SAaron Smith   auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
25310a02577SAaron Smith   if (!results)
25410a02577SAaron Smith     return CompUnitSP();
25510a02577SAaron Smith   auto compiland_up = results->getChildAtIndex(index);
25610a02577SAaron Smith   if (!compiland_up)
25710a02577SAaron Smith     return CompUnitSP();
25810a02577SAaron Smith   return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
25974e08ca0SZachary Turner }
26074e08ca0SZachary Turner 
261863f8c18SZachary Turner lldb::LanguageType SymbolFilePDB::ParseLanguage(CompileUnit &comp_unit) {
262656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
263863f8c18SZachary Turner   auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
26410a02577SAaron Smith   if (!compiland_up)
26574e08ca0SZachary Turner     return lldb::eLanguageTypeUnknown;
26610a02577SAaron Smith   auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
26774e08ca0SZachary Turner   if (!details)
26874e08ca0SZachary Turner     return lldb::eLanguageTypeUnknown;
26974e08ca0SZachary Turner   return TranslateLanguage(details->getLanguage());
27074e08ca0SZachary Turner }
27174e08ca0SZachary Turner 
272863f8c18SZachary Turner lldb_private::Function *
273863f8c18SZachary Turner SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(const PDBSymbolFunc &pdb_func,
274863f8c18SZachary Turner                                                   CompileUnit &comp_unit) {
275863f8c18SZachary Turner   if (FunctionSP result = comp_unit.FindFunctionByUID(pdb_func.getSymIndexId()))
276a5235af9SAleksandr Urakov     return result.get();
277a5235af9SAleksandr Urakov 
278e664b5dcSAaron Smith   auto file_vm_addr = pdb_func.getVirtualAddress();
279308e39caSAaron Smith   if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
2807ac1c780SAaron Smith     return nullptr;
2817ac1c780SAaron Smith 
282e664b5dcSAaron Smith   auto func_length = pdb_func.getLength();
283c8316ed2SAaron Smith   AddressRange func_range =
284863f8c18SZachary Turner       AddressRange(file_vm_addr, func_length,
285863f8c18SZachary Turner                    GetObjectFile()->GetModule()->GetSectionList());
2867ac1c780SAaron Smith   if (!func_range.GetBaseAddress().IsValid())
2877ac1c780SAaron Smith     return nullptr;
2887ac1c780SAaron Smith 
289e664b5dcSAaron Smith   lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
2907ac1c780SAaron Smith   if (!func_type)
2917ac1c780SAaron Smith     return nullptr;
2927ac1c780SAaron Smith 
293e664b5dcSAaron Smith   user_id_t func_type_uid = pdb_func.getSignatureId();
294f76fe682SAaron Smith 
2957ac1c780SAaron Smith   Mangled mangled = GetMangledForPDBFunc(pdb_func);
2967ac1c780SAaron Smith 
297c8316ed2SAaron Smith   FunctionSP func_sp =
298863f8c18SZachary Turner       std::make_shared<Function>(&comp_unit, pdb_func.getSymIndexId(),
299c8316ed2SAaron Smith                                  func_type_uid, mangled, func_type, func_range);
3007ac1c780SAaron Smith 
301863f8c18SZachary Turner   comp_unit.AddFunction(func_sp);
302c68925abSZachary Turner 
303d0050d1bSNathan Lanza   LanguageType lang = ParseLanguage(comp_unit);
3040e252e38SAlex Langford   auto type_system_or_err = GetTypeSystemForLanguage(lang);
3050e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
3060e252e38SAlex Langford     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
3070e252e38SAlex Langford                    std::move(err), "Unable to parse PDBFunc");
308c68925abSZachary Turner     return nullptr;
3090e252e38SAlex Langford   }
3100e252e38SAlex Langford 
311c68925abSZachary Turner   ClangASTContext *clang_type_system =
3120e252e38SAlex Langford     llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
313c68925abSZachary Turner   if (!clang_type_system)
314c68925abSZachary Turner     return nullptr;
315c68925abSZachary Turner   clang_type_system->GetPDBParser()->GetDeclForSymbol(pdb_func);
316c68925abSZachary Turner 
3177ac1c780SAaron Smith   return func_sp.get();
3187ac1c780SAaron Smith }
3197ac1c780SAaron Smith 
320863f8c18SZachary Turner size_t SymbolFilePDB::ParseFunctions(CompileUnit &comp_unit) {
321656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
3227ac1c780SAaron Smith   size_t func_added = 0;
323863f8c18SZachary Turner   auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
3247ac1c780SAaron Smith   if (!compiland_up)
3257ac1c780SAaron Smith     return 0;
3267ac1c780SAaron Smith   auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
3277ac1c780SAaron Smith   if (!results_up)
3287ac1c780SAaron Smith     return 0;
3297ac1c780SAaron Smith   while (auto pdb_func_up = results_up->getNext()) {
330863f8c18SZachary Turner     auto func_sp = comp_unit.FindFunctionByUID(pdb_func_up->getSymIndexId());
3317ac1c780SAaron Smith     if (!func_sp) {
332863f8c18SZachary Turner       if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, comp_unit))
3337ac1c780SAaron Smith         ++func_added;
3347ac1c780SAaron Smith     }
3357ac1c780SAaron Smith   }
3367ac1c780SAaron Smith   return func_added;
33774e08ca0SZachary Turner }
33874e08ca0SZachary Turner 
339863f8c18SZachary Turner bool SymbolFilePDB::ParseLineTable(CompileUnit &comp_unit) {
340656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
341863f8c18SZachary Turner   if (comp_unit.GetLineTable())
34210a02577SAaron Smith     return true;
343863f8c18SZachary Turner   return ParseCompileUnitLineTable(comp_unit, 0);
34474e08ca0SZachary Turner }
34574e08ca0SZachary Turner 
346863f8c18SZachary Turner bool SymbolFilePDB::ParseDebugMacros(CompileUnit &comp_unit) {
34774e08ca0SZachary Turner   // PDB doesn't contain information about macros
34874e08ca0SZachary Turner   return false;
34974e08ca0SZachary Turner }
35074e08ca0SZachary Turner 
351863f8c18SZachary Turner bool SymbolFilePDB::ParseSupportFiles(
352863f8c18SZachary Turner     CompileUnit &comp_unit, lldb_private::FileSpecList &support_files) {
35374e08ca0SZachary Turner 
354b9c1b51eSKate Stone   // In theory this is unnecessary work for us, because all of this information
3559d0eb996SAdrian McCarthy   // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
3569d0eb996SAdrian McCarthy   // second time seems like a waste.  Unfortunately, there's no good way around
3579d0eb996SAdrian McCarthy   // this short of a moderate refactor since SymbolVendor depends on being able
3589d0eb996SAdrian McCarthy   // to cache this list.
359656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
360863f8c18SZachary Turner   auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
36110a02577SAaron Smith   if (!compiland_up)
36274e08ca0SZachary Turner     return false;
36310a02577SAaron Smith   auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
36474e08ca0SZachary Turner   if (!files || files->getChildCount() == 0)
36574e08ca0SZachary Turner     return false;
36674e08ca0SZachary Turner 
367b9c1b51eSKate Stone   while (auto file = files->getNext()) {
3688f3be7a3SJonas Devlieghere     FileSpec spec(file->getFileName(), FileSpec::Style::windows);
36910a02577SAaron Smith     support_files.AppendIfUnique(spec);
37074e08ca0SZachary Turner   }
3719ea80d25SPavel Labath 
3729ea80d25SPavel Labath   // LLDB uses the DWARF-like file numeration (one based),
3739ea80d25SPavel Labath   // the zeroth file is the compile unit itself
374863f8c18SZachary Turner   support_files.Insert(0, comp_unit);
3759ea80d25SPavel Labath 
37674e08ca0SZachary Turner   return true;
37774e08ca0SZachary Turner }
37874e08ca0SZachary Turner 
379b9c1b51eSKate Stone bool SymbolFilePDB::ParseImportedModules(
380b9c1b51eSKate Stone     const lldb_private::SymbolContext &sc,
3810f30a3b6SAdrian Prantl     std::vector<SourceModule> &imported_modules) {
38274e08ca0SZachary Turner   // PDB does not yet support module debug info
38374e08ca0SZachary Turner   return false;
38474e08ca0SZachary Turner }
38574e08ca0SZachary Turner 
386c8316ed2SAaron Smith static size_t ParseFunctionBlocksForPDBSymbol(
387ffc1b8fdSZachary Turner     uint64_t func_file_vm_addr, const llvm::pdb::PDBSymbol *pdb_symbol,
388ffc1b8fdSZachary Turner     lldb_private::Block *parent_block, bool is_top_parent) {
3897ac1c780SAaron Smith   assert(pdb_symbol && parent_block);
3907ac1c780SAaron Smith 
3917ac1c780SAaron Smith   size_t num_added = 0;
3927ac1c780SAaron Smith   switch (pdb_symbol->getSymTag()) {
3937ac1c780SAaron Smith   case PDB_SymType::Block:
3947ac1c780SAaron Smith   case PDB_SymType::Function: {
3957ac1c780SAaron Smith     Block *block = nullptr;
3967ac1c780SAaron Smith     auto &raw_sym = pdb_symbol->getRawSymbol();
3977ac1c780SAaron Smith     if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
3987ac1c780SAaron Smith       if (pdb_func->hasNoInlineAttribute())
3997ac1c780SAaron Smith         break;
4007ac1c780SAaron Smith       if (is_top_parent)
4017ac1c780SAaron Smith         block = parent_block;
4027ac1c780SAaron Smith       else
4037ac1c780SAaron Smith         break;
4047ac1c780SAaron Smith     } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
4057ac1c780SAaron Smith       auto uid = pdb_symbol->getSymIndexId();
4067ac1c780SAaron Smith       if (parent_block->FindBlockByID(uid))
4077ac1c780SAaron Smith         break;
4087ac1c780SAaron Smith       if (raw_sym.getVirtualAddress() < func_file_vm_addr)
4097ac1c780SAaron Smith         break;
4107ac1c780SAaron Smith 
4117ac1c780SAaron Smith       auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
4127ac1c780SAaron Smith       parent_block->AddChild(block_sp);
4137ac1c780SAaron Smith       block = block_sp.get();
4147ac1c780SAaron Smith     } else
4157ac1c780SAaron Smith       llvm_unreachable("Unexpected PDB symbol!");
4167ac1c780SAaron Smith 
417c8316ed2SAaron Smith     block->AddRange(Block::Range(
418c8316ed2SAaron Smith         raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
4197ac1c780SAaron Smith     block->FinalizeRanges();
4207ac1c780SAaron Smith     ++num_added;
4217ac1c780SAaron Smith 
4227ac1c780SAaron Smith     auto results_up = pdb_symbol->findAllChildren();
4237ac1c780SAaron Smith     if (!results_up)
4247ac1c780SAaron Smith       break;
4257ac1c780SAaron Smith     while (auto symbol_up = results_up->getNext()) {
426c8316ed2SAaron Smith       num_added += ParseFunctionBlocksForPDBSymbol(
427ffc1b8fdSZachary Turner           func_file_vm_addr, symbol_up.get(), block, false);
4287ac1c780SAaron Smith     }
4297ac1c780SAaron Smith   } break;
430c8316ed2SAaron Smith   default:
431c8316ed2SAaron Smith     break;
4327ac1c780SAaron Smith   }
4337ac1c780SAaron Smith   return num_added;
4347ac1c780SAaron Smith }
4357ac1c780SAaron Smith 
436ffc1b8fdSZachary Turner size_t SymbolFilePDB::ParseBlocksRecursive(Function &func) {
437656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
4387ac1c780SAaron Smith   size_t num_added = 0;
439ffc1b8fdSZachary Turner   auto uid = func.GetID();
4407ac1c780SAaron Smith   auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
4417ac1c780SAaron Smith   if (!pdb_func_up)
4427ac1c780SAaron Smith     return 0;
443ffc1b8fdSZachary Turner   Block &parent_block = func.GetBlock(false);
444ffc1b8fdSZachary Turner   num_added = ParseFunctionBlocksForPDBSymbol(
445ffc1b8fdSZachary Turner       pdb_func_up->getVirtualAddress(), pdb_func_up.get(), &parent_block, true);
4467ac1c780SAaron Smith   return num_added;
447b9c1b51eSKate Stone }
448b9c1b51eSKate Stone 
449863f8c18SZachary Turner size_t SymbolFilePDB::ParseTypes(CompileUnit &comp_unit) {
450656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
45166b84079SAaron Smith 
45266b84079SAaron Smith   size_t num_added = 0;
453ac0d41c7SZachary Turner   auto compiland = GetPDBCompilandByUID(comp_unit.GetID());
45466b84079SAaron Smith   if (!compiland)
45566b84079SAaron Smith     return 0;
45666b84079SAaron Smith 
45766b84079SAaron Smith   auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
45866b84079SAaron Smith     std::unique_ptr<IPDBEnumSymbols> results;
45966b84079SAaron Smith     PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
46066b84079SAaron Smith                                     PDB_SymType::UDT};
46166b84079SAaron Smith     for (auto tag : tags_to_search) {
46266b84079SAaron Smith       results = raw_sym.findAllChildren(tag);
46366b84079SAaron Smith       if (!results || results->getChildCount() == 0)
46466b84079SAaron Smith         continue;
46566b84079SAaron Smith       while (auto symbol = results->getNext()) {
46666b84079SAaron Smith         switch (symbol->getSymTag()) {
467ec40f818SAaron Smith         case PDB_SymType::Enum:
468ec40f818SAaron Smith         case PDB_SymType::UDT:
469ec40f818SAaron Smith         case PDB_SymType::Typedef:
470ec40f818SAaron Smith           break;
471ec40f818SAaron Smith         default:
472ec40f818SAaron Smith           continue;
473ec40f818SAaron Smith         }
474ec40f818SAaron Smith 
475ec40f818SAaron Smith         // This should cause the type to get cached and stored in the `m_types`
476ec40f818SAaron Smith         // lookup.
4777d2a74fcSAleksandr Urakov         if (auto type = ResolveTypeUID(symbol->getSymIndexId())) {
4787d2a74fcSAleksandr Urakov           // Resolve the type completely to avoid a completion
4797d2a74fcSAleksandr Urakov           // (and so a list change, which causes an iterators invalidation)
4807d2a74fcSAleksandr Urakov           // during a TypeList dumping
4817d2a74fcSAleksandr Urakov           type->GetFullCompilerType();
482ec40f818SAaron Smith           ++num_added;
483ec40f818SAaron Smith         }
48466b84079SAaron Smith       }
4857d2a74fcSAleksandr Urakov     }
48666b84079SAaron Smith   };
48766b84079SAaron Smith 
48866b84079SAaron Smith   ParseTypesByTagFn(*compiland);
48966b84079SAaron Smith 
49066b84079SAaron Smith   // Also parse global types particularly coming from this compiland.
49105097246SAdrian Prantl   // Unfortunately, PDB has no compiland information for each global type. We
49205097246SAdrian Prantl   // have to parse them all. But ensure we only do this once.
49366b84079SAaron Smith   static bool parse_all_global_types = false;
49466b84079SAaron Smith   if (!parse_all_global_types) {
49566b84079SAaron Smith     ParseTypesByTagFn(*m_global_scope_up);
49666b84079SAaron Smith     parse_all_global_types = true;
49766b84079SAaron Smith   }
498ec40f818SAaron Smith   return num_added;
49974e08ca0SZachary Turner }
50074e08ca0SZachary Turner 
50174e08ca0SZachary Turner size_t
502b9c1b51eSKate Stone SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
503656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
504cab0d23fSAaron Smith   if (!sc.comp_unit)
505cab0d23fSAaron Smith     return 0;
506cab0d23fSAaron Smith 
507cab0d23fSAaron Smith   size_t num_added = 0;
508cab0d23fSAaron Smith   if (sc.function) {
509cab0d23fSAaron Smith     auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
510cab0d23fSAaron Smith         sc.function->GetID());
511cab0d23fSAaron Smith     if (!pdb_func)
512cab0d23fSAaron Smith       return 0;
513cab0d23fSAaron Smith 
514cab0d23fSAaron Smith     num_added += ParseVariables(sc, *pdb_func);
515cab0d23fSAaron Smith     sc.function->GetBlock(false).SetDidParseVariables(true, true);
516cab0d23fSAaron Smith   } else if (sc.comp_unit) {
517cab0d23fSAaron Smith     auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
518cab0d23fSAaron Smith     if (!compiland)
519cab0d23fSAaron Smith       return 0;
520cab0d23fSAaron Smith 
521cab0d23fSAaron Smith     if (sc.comp_unit->GetVariableList(false))
522cab0d23fSAaron Smith       return 0;
523cab0d23fSAaron Smith 
524cab0d23fSAaron Smith     auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
525cab0d23fSAaron Smith     if (results && results->getChildCount()) {
526cab0d23fSAaron Smith       while (auto result = results->getNext()) {
527356aa4a9SAleksandr Urakov         auto cu_id = GetCompilandId(*result);
528cab0d23fSAaron Smith         // FIXME: We are not able to determine variable's compile unit.
529cab0d23fSAaron Smith         if (cu_id == 0)
530cab0d23fSAaron Smith           continue;
531cab0d23fSAaron Smith 
532cab0d23fSAaron Smith         if (cu_id == sc.comp_unit->GetID())
533cab0d23fSAaron Smith           num_added += ParseVariables(sc, *result);
534cab0d23fSAaron Smith       }
535cab0d23fSAaron Smith     }
536cab0d23fSAaron Smith 
537cab0d23fSAaron Smith     // FIXME: A `file static` or `global constant` variable appears both in
538cab0d23fSAaron Smith     // compiland's children and global scope's children with unexpectedly
539cab0d23fSAaron Smith     // different symbol's Id making it ambiguous.
540cab0d23fSAaron Smith 
541cab0d23fSAaron Smith     // FIXME: 'local constant', for example, const char var[] = "abc", declared
542cab0d23fSAaron Smith     // in a function scope, can't be found in PDB.
543cab0d23fSAaron Smith 
544cab0d23fSAaron Smith     // Parse variables in this compiland.
545cab0d23fSAaron Smith     num_added += ParseVariables(sc, *compiland);
546cab0d23fSAaron Smith   }
547cab0d23fSAaron Smith 
548cab0d23fSAaron Smith   return num_added;
54974e08ca0SZachary Turner }
55074e08ca0SZachary Turner 
551b9c1b51eSKate Stone lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
552656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
55342dff790SZachary Turner   auto find_result = m_types.find(type_uid);
55442dff790SZachary Turner   if (find_result != m_types.end())
55542dff790SZachary Turner     return find_result->second.get();
55642dff790SZachary Turner 
5570e252e38SAlex Langford   auto type_system_or_err =
558b9c1b51eSKate Stone       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
5590e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
5600e252e38SAlex Langford     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
5610e252e38SAlex Langford                    std::move(err), "Unable to ResolveTypeUID");
5620e252e38SAlex Langford     return nullptr;
5630e252e38SAlex Langford   }
5640e252e38SAlex Langford 
565b9c1b51eSKate Stone   ClangASTContext *clang_type_system =
5660e252e38SAlex Langford       llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
56742dff790SZachary Turner   if (!clang_type_system)
56874e08ca0SZachary Turner     return nullptr;
569709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_type_system->GetPDBParser();
57042dff790SZachary Turner   if (!pdb)
57142dff790SZachary Turner     return nullptr;
57242dff790SZachary Turner 
57342dff790SZachary Turner   auto pdb_type = m_session_up->getSymbolById(type_uid);
57442dff790SZachary Turner   if (pdb_type == nullptr)
57542dff790SZachary Turner     return nullptr;
57642dff790SZachary Turner 
57742dff790SZachary Turner   lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
578d5a925f4SAaron Smith   if (result) {
57942dff790SZachary Turner     m_types.insert(std::make_pair(type_uid, result));
580f46e8974SPavel Labath     GetTypeList().Insert(result);
581ec40f818SAaron Smith   }
58242dff790SZachary Turner   return result.get();
58374e08ca0SZachary Turner }
58474e08ca0SZachary Turner 
585eca07c59SAdrian Prantl llvm::Optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID(
586eca07c59SAdrian Prantl     lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
587eca07c59SAdrian Prantl   return llvm::None;
588eca07c59SAdrian Prantl }
589eca07c59SAdrian Prantl 
590b9c1b51eSKate Stone bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
5917d2a74fcSAleksandr Urakov   std::lock_guard<std::recursive_mutex> guard(
5927d2a74fcSAleksandr Urakov       GetObjectFile()->GetModule()->GetMutex());
5937d2a74fcSAleksandr Urakov 
5940e252e38SAlex Langford   auto type_system_or_err =
5950e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
5960e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
5970e252e38SAlex Langford     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
5980e252e38SAlex Langford                    std::move(err), "Unable to get dynamic array info for UID");
5990e252e38SAlex Langford     return false;
6000e252e38SAlex Langford   }
6010e252e38SAlex Langford 
6020e252e38SAlex Langford   ClangASTContext *clang_ast_ctx =
6030e252e38SAlex Langford       llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
6040e252e38SAlex Langford 
6057d2a74fcSAleksandr Urakov   if (!clang_ast_ctx)
60674e08ca0SZachary Turner     return false;
6077d2a74fcSAleksandr Urakov 
608709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
6097d2a74fcSAleksandr Urakov   if (!pdb)
6107d2a74fcSAleksandr Urakov     return false;
6117d2a74fcSAleksandr Urakov 
6127d2a74fcSAleksandr Urakov   return pdb->CompleteTypeFromPDB(compiler_type);
61374e08ca0SZachary Turner }
61474e08ca0SZachary Turner 
615b9c1b51eSKate Stone lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
6160e252e38SAlex Langford   auto type_system_or_err =
6170e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
6180e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
6190e252e38SAlex Langford     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
6200e252e38SAlex Langford                    std::move(err), "Unable to get decl for UID");
6210e252e38SAlex Langford     return CompilerDecl();
6220e252e38SAlex Langford   }
6230e252e38SAlex Langford 
6240e252e38SAlex Langford   ClangASTContext *clang_ast_ctx =
6250e252e38SAlex Langford       llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
626709426b3SAleksandr Urakov   if (!clang_ast_ctx)
627709426b3SAleksandr Urakov     return CompilerDecl();
628709426b3SAleksandr Urakov 
629709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
630709426b3SAleksandr Urakov   if (!pdb)
631709426b3SAleksandr Urakov     return CompilerDecl();
632709426b3SAleksandr Urakov 
633709426b3SAleksandr Urakov   auto symbol = m_session_up->getSymbolById(uid);
634709426b3SAleksandr Urakov   if (!symbol)
635709426b3SAleksandr Urakov     return CompilerDecl();
636709426b3SAleksandr Urakov 
637709426b3SAleksandr Urakov   auto decl = pdb->GetDeclForSymbol(*symbol);
638709426b3SAleksandr Urakov   if (!decl)
639709426b3SAleksandr Urakov     return CompilerDecl();
640709426b3SAleksandr Urakov 
641709426b3SAleksandr Urakov   return CompilerDecl(clang_ast_ctx, decl);
64274e08ca0SZachary Turner }
64374e08ca0SZachary Turner 
64474e08ca0SZachary Turner lldb_private::CompilerDeclContext
645b9c1b51eSKate Stone SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
6460e252e38SAlex Langford   auto type_system_or_err =
6470e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
6480e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
6490e252e38SAlex Langford     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
6500e252e38SAlex Langford                    std::move(err), "Unable to get DeclContext for UID");
6510e252e38SAlex Langford     return CompilerDeclContext();
6520e252e38SAlex Langford   }
6530e252e38SAlex Langford 
6540e252e38SAlex Langford   ClangASTContext *clang_ast_ctx =
6550e252e38SAlex Langford       llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
656709426b3SAleksandr Urakov   if (!clang_ast_ctx)
657709426b3SAleksandr Urakov     return CompilerDeclContext();
658709426b3SAleksandr Urakov 
659709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
660709426b3SAleksandr Urakov   if (!pdb)
661709426b3SAleksandr Urakov     return CompilerDeclContext();
662709426b3SAleksandr Urakov 
663709426b3SAleksandr Urakov   auto symbol = m_session_up->getSymbolById(uid);
664709426b3SAleksandr Urakov   if (!symbol)
665709426b3SAleksandr Urakov     return CompilerDeclContext();
666709426b3SAleksandr Urakov 
667709426b3SAleksandr Urakov   auto decl_context = pdb->GetDeclContextForSymbol(*symbol);
668709426b3SAleksandr Urakov   if (!decl_context)
669709426b3SAleksandr Urakov     return GetDeclContextContainingUID(uid);
670709426b3SAleksandr Urakov 
671709426b3SAleksandr Urakov   return CompilerDeclContext(clang_ast_ctx, decl_context);
67274e08ca0SZachary Turner }
67374e08ca0SZachary Turner 
67474e08ca0SZachary Turner lldb_private::CompilerDeclContext
675b9c1b51eSKate Stone SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
6760e252e38SAlex Langford   auto type_system_or_err =
6770e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
6780e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
6790e252e38SAlex Langford     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
6800e252e38SAlex Langford                    std::move(err), "Unable to get DeclContext containing UID");
6810e252e38SAlex Langford     return CompilerDeclContext();
6820e252e38SAlex Langford   }
6830e252e38SAlex Langford 
6840e252e38SAlex Langford   ClangASTContext *clang_ast_ctx =
6850e252e38SAlex Langford       llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
686709426b3SAleksandr Urakov   if (!clang_ast_ctx)
687709426b3SAleksandr Urakov     return CompilerDeclContext();
688709426b3SAleksandr Urakov 
689709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
690709426b3SAleksandr Urakov   if (!pdb)
691709426b3SAleksandr Urakov     return CompilerDeclContext();
692709426b3SAleksandr Urakov 
693709426b3SAleksandr Urakov   auto symbol = m_session_up->getSymbolById(uid);
694709426b3SAleksandr Urakov   if (!symbol)
695709426b3SAleksandr Urakov     return CompilerDeclContext();
696709426b3SAleksandr Urakov 
697709426b3SAleksandr Urakov   auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol);
698709426b3SAleksandr Urakov   assert(decl_context);
699709426b3SAleksandr Urakov 
700709426b3SAleksandr Urakov   return CompilerDeclContext(clang_ast_ctx, decl_context);
70174e08ca0SZachary Turner }
70274e08ca0SZachary Turner 
703b9c1b51eSKate Stone void SymbolFilePDB::ParseDeclsForContext(
704709426b3SAleksandr Urakov     lldb_private::CompilerDeclContext decl_ctx) {
7050e252e38SAlex Langford   auto type_system_or_err =
7060e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
7070e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
7080e252e38SAlex Langford     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
7090e252e38SAlex Langford                    std::move(err), "Unable to parse decls for context");
7100e252e38SAlex Langford     return;
7110e252e38SAlex Langford   }
7120e252e38SAlex Langford 
7130e252e38SAlex Langford   ClangASTContext *clang_ast_ctx =
7140e252e38SAlex Langford       llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
715709426b3SAleksandr Urakov   if (!clang_ast_ctx)
716709426b3SAleksandr Urakov     return;
717709426b3SAleksandr Urakov 
718709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
719709426b3SAleksandr Urakov   if (!pdb)
720709426b3SAleksandr Urakov     return;
721709426b3SAleksandr Urakov 
722709426b3SAleksandr Urakov   pdb->ParseDeclsForDeclContext(
723709426b3SAleksandr Urakov       static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext()));
724709426b3SAleksandr Urakov }
72574e08ca0SZachary Turner 
72674e08ca0SZachary Turner uint32_t
727b9c1b51eSKate Stone SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
728991e4453SZachary Turner                                     SymbolContextItem resolve_scope,
729b9c1b51eSKate Stone                                     lldb_private::SymbolContext &sc) {
730656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
7317ac1c780SAaron Smith   uint32_t resolved_flags = 0;
7324d4d63eeSPavel Labath   if (resolve_scope & eSymbolContextCompUnit ||
7334d4d63eeSPavel Labath       resolve_scope & eSymbolContextVariable ||
7344d4d63eeSPavel Labath       resolve_scope & eSymbolContextFunction ||
7354d4d63eeSPavel Labath       resolve_scope & eSymbolContextBlock ||
7367ac1c780SAaron Smith       resolve_scope & eSymbolContextLineEntry) {
7377ac1c780SAaron Smith     auto cu_sp = GetCompileUnitContainsAddress(so_addr);
7387ac1c780SAaron Smith     if (!cu_sp) {
73933cdbff2SRichard Trieu       if (resolved_flags & eSymbolContextVariable) {
7407ac1c780SAaron Smith         // TODO: Resolve variables
7417ac1c780SAaron Smith       }
7427ac1c780SAaron Smith       return 0;
7437ac1c780SAaron Smith     }
7447ac1c780SAaron Smith     sc.comp_unit = cu_sp.get();
7457ac1c780SAaron Smith     resolved_flags |= eSymbolContextCompUnit;
7467ac1c780SAaron Smith     lldbassert(sc.module_sp == cu_sp->GetModule());
7479ea80d25SPavel Labath   }
7487ac1c780SAaron Smith 
749398f81b3SAleksandr Urakov   if (resolve_scope & eSymbolContextFunction ||
750398f81b3SAleksandr Urakov       resolve_scope & eSymbolContextBlock) {
7519ea80d25SPavel Labath     addr_t file_vm_addr = so_addr.GetFileAddress();
7529ea80d25SPavel Labath     auto symbol_up =
7539ea80d25SPavel Labath         m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
7549ea80d25SPavel Labath     if (symbol_up) {
7557ac1c780SAaron Smith       auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
7567ac1c780SAaron Smith       assert(pdb_func);
7577ac1c780SAaron Smith       auto func_uid = pdb_func->getSymIndexId();
7587ac1c780SAaron Smith       sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
7597ac1c780SAaron Smith       if (sc.function == nullptr)
760863f8c18SZachary Turner         sc.function =
761863f8c18SZachary Turner             ParseCompileUnitFunctionForPDBFunc(*pdb_func, *sc.comp_unit);
7627ac1c780SAaron Smith       if (sc.function) {
7637ac1c780SAaron Smith         resolved_flags |= eSymbolContextFunction;
7647ac1c780SAaron Smith         if (resolve_scope & eSymbolContextBlock) {
765398f81b3SAleksandr Urakov           auto block_symbol = m_session_up->findSymbolByAddress(
766398f81b3SAleksandr Urakov               file_vm_addr, PDB_SymType::Block);
767398f81b3SAleksandr Urakov           auto block_id = block_symbol ? block_symbol->getSymIndexId()
768398f81b3SAleksandr Urakov                                        : sc.function->GetID();
769398f81b3SAleksandr Urakov           sc.block = sc.function->GetBlock(true).FindBlockByID(block_id);
7707ac1c780SAaron Smith           if (sc.block)
7717ac1c780SAaron Smith             resolved_flags |= eSymbolContextBlock;
7727ac1c780SAaron Smith         }
7737ac1c780SAaron Smith       }
7747ac1c780SAaron Smith     }
7757ac1c780SAaron Smith   }
7767ac1c780SAaron Smith 
7777ac1c780SAaron Smith   if (resolve_scope & eSymbolContextLineEntry) {
7787ac1c780SAaron Smith     if (auto *line_table = sc.comp_unit->GetLineTable()) {
7797ac1c780SAaron Smith       Address addr(so_addr);
7807ac1c780SAaron Smith       if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
7817ac1c780SAaron Smith         resolved_flags |= eSymbolContextLineEntry;
7827ac1c780SAaron Smith     }
7837ac1c780SAaron Smith   }
7849ea80d25SPavel Labath 
7857ac1c780SAaron Smith   return resolved_flags;
78674e08ca0SZachary Turner }
78774e08ca0SZachary Turner 
788b9c1b51eSKate Stone uint32_t SymbolFilePDB::ResolveSymbolContext(
789b9c1b51eSKate Stone     const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
790991e4453SZachary Turner     SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) {
791656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
79210a02577SAaron Smith   const size_t old_size = sc_list.GetSize();
793b9c1b51eSKate Stone   if (resolve_scope & lldb::eSymbolContextCompUnit) {
794b9c1b51eSKate Stone     // Locate all compilation units with line numbers referencing the specified
7959d0eb996SAdrian McCarthy     // file.  For example, if `file_spec` is <vector>, then this should return
7969d0eb996SAdrian McCarthy     // all source files and header files that reference <vector>, either
7979d0eb996SAdrian McCarthy     // directly or indirectly.
798b9c1b51eSKate Stone     auto compilands = m_session_up->findCompilandsForSourceFile(
799b9c1b51eSKate Stone         file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
80074e08ca0SZachary Turner 
80110a02577SAaron Smith     if (!compilands)
80210a02577SAaron Smith       return 0;
80310a02577SAaron Smith 
8049d0eb996SAdrian McCarthy     // For each one, either find its previously parsed data or parse it afresh
8059d0eb996SAdrian McCarthy     // and add it to the symbol context list.
806b9c1b51eSKate Stone     while (auto compiland = compilands->getNext()) {
80705097246SAdrian Prantl       // If we're not checking inlines, then don't add line information for
80805097246SAdrian Prantl       // this file unless the FileSpec matches. For inline functions, we don't
80905097246SAdrian Prantl       // have to match the FileSpec since they could be defined in headers
81005097246SAdrian Prantl       // other than file specified in FileSpec.
811b9c1b51eSKate Stone       if (!check_inlines) {
812487b0c6bSAaron Smith         std::string source_file = compiland->getSourceFileFullPath();
81310a02577SAaron Smith         if (source_file.empty())
81410a02577SAaron Smith           continue;
8158f3be7a3SJonas Devlieghere         FileSpec this_spec(source_file, FileSpec::Style::windows);
81610a02577SAaron Smith         bool need_full_match = !file_spec.GetDirectory().IsEmpty();
81710a02577SAaron Smith         if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
81874e08ca0SZachary Turner           continue;
81974e08ca0SZachary Turner       }
82074e08ca0SZachary Turner 
82174e08ca0SZachary Turner       SymbolContext sc;
82210a02577SAaron Smith       auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
823d5a925f4SAaron Smith       if (!cu)
82410a02577SAaron Smith         continue;
82574e08ca0SZachary Turner       sc.comp_unit = cu.get();
82674e08ca0SZachary Turner       sc.module_sp = cu->GetModule();
82774e08ca0SZachary Turner 
828b9c1b51eSKate Stone       // If we were asked to resolve line entries, add all entries to the line
8299d0eb996SAdrian McCarthy       // table that match the requested line (or all lines if `line` == 0).
8307ac1c780SAaron Smith       if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
8317ac1c780SAaron Smith                            eSymbolContextLineEntry)) {
832863f8c18SZachary Turner         bool has_line_table = ParseCompileUnitLineTable(*sc.comp_unit, line);
8337ac1c780SAaron Smith 
8347ac1c780SAaron Smith         if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
8357ac1c780SAaron Smith           // The query asks for line entries, but we can't get them for the
83605097246SAdrian Prantl           // compile unit. This is not normal for `line` = 0. So just assert
83705097246SAdrian Prantl           // it.
838f76fe682SAaron Smith           assert(line && "Couldn't get all line entries!\n");
8397ac1c780SAaron Smith 
8407ac1c780SAaron Smith           // Current compiland does not have the requested line. Search next.
8417ac1c780SAaron Smith           continue;
8427ac1c780SAaron Smith         }
8437ac1c780SAaron Smith 
8447ac1c780SAaron Smith         if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
8457ac1c780SAaron Smith           if (!has_line_table)
8467ac1c780SAaron Smith             continue;
8477ac1c780SAaron Smith 
8487ac1c780SAaron Smith           auto *line_table = sc.comp_unit->GetLineTable();
8497ac1c780SAaron Smith           lldbassert(line_table);
8507ac1c780SAaron Smith 
8517ac1c780SAaron Smith           uint32_t num_line_entries = line_table->GetSize();
8527ac1c780SAaron Smith           // Skip the terminal line entry.
8537ac1c780SAaron Smith           --num_line_entries;
8547ac1c780SAaron Smith 
85505097246SAdrian Prantl           // If `line `!= 0, see if we can resolve function for each line entry
85605097246SAdrian Prantl           // in the line table.
8577ac1c780SAaron Smith           for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
8587ac1c780SAaron Smith                ++line_idx) {
8597ac1c780SAaron Smith             if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
8607ac1c780SAaron Smith               continue;
8617ac1c780SAaron Smith 
8627ac1c780SAaron Smith             auto file_vm_addr =
8637ac1c780SAaron Smith                 sc.line_entry.range.GetBaseAddress().GetFileAddress();
864308e39caSAaron Smith             if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
8657ac1c780SAaron Smith               continue;
8667ac1c780SAaron Smith 
867c8316ed2SAaron Smith             auto symbol_up = m_session_up->findSymbolByAddress(
868c8316ed2SAaron Smith                 file_vm_addr, PDB_SymType::Function);
8697ac1c780SAaron Smith             if (symbol_up) {
8707ac1c780SAaron Smith               auto func_uid = symbol_up->getSymIndexId();
8717ac1c780SAaron Smith               sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
8727ac1c780SAaron Smith               if (sc.function == nullptr) {
8737ac1c780SAaron Smith                 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
8747ac1c780SAaron Smith                 assert(pdb_func);
875863f8c18SZachary Turner                 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func,
876863f8c18SZachary Turner                                                                  *sc.comp_unit);
8777ac1c780SAaron Smith               }
8787ac1c780SAaron Smith               if (sc.function && (resolve_scope & eSymbolContextBlock)) {
8797ac1c780SAaron Smith                 Block &block = sc.function->GetBlock(true);
8807ac1c780SAaron Smith                 sc.block = block.FindBlockByID(sc.function->GetID());
8817ac1c780SAaron Smith               }
8827ac1c780SAaron Smith             }
8837ac1c780SAaron Smith             sc_list.Append(sc);
8847ac1c780SAaron Smith           }
8857ac1c780SAaron Smith         } else if (has_line_table) {
8867ac1c780SAaron Smith           // We can parse line table for the compile unit. But no query to
8877ac1c780SAaron Smith           // resolve function or block. We append `sc` to the list anyway.
8887ac1c780SAaron Smith           sc_list.Append(sc);
8897ac1c780SAaron Smith         }
8907ac1c780SAaron Smith       } else {
8917ac1c780SAaron Smith         // No query for line entry, function or block. But we have a valid
8927ac1c780SAaron Smith         // compile unit, append `sc` to the list.
8937ac1c780SAaron Smith         sc_list.Append(sc);
8947ac1c780SAaron Smith       }
89574e08ca0SZachary Turner     }
89674e08ca0SZachary Turner   }
89710a02577SAaron Smith   return sc_list.GetSize() - old_size;
89874e08ca0SZachary Turner }
89974e08ca0SZachary Turner 
900cab0d23fSAaron Smith std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
901356aa4a9SAleksandr Urakov   // Cache public names at first
902356aa4a9SAleksandr Urakov   if (m_public_names.empty())
903356aa4a9SAleksandr Urakov     if (auto result_up =
904356aa4a9SAleksandr Urakov             m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol))
905356aa4a9SAleksandr Urakov       while (auto symbol_up = result_up->getNext())
906356aa4a9SAleksandr Urakov         if (auto addr = symbol_up->getRawSymbol().getVirtualAddress())
907356aa4a9SAleksandr Urakov           m_public_names[addr] = symbol_up->getRawSymbol().getName();
908cab0d23fSAaron Smith 
909356aa4a9SAleksandr Urakov   // Look up the name in the cache
910356aa4a9SAleksandr Urakov   return m_public_names.lookup(pdb_data.getVirtualAddress());
911cab0d23fSAaron Smith }
912cab0d23fSAaron Smith 
913cab0d23fSAaron Smith VariableSP SymbolFilePDB::ParseVariableForPDBData(
914cab0d23fSAaron Smith     const lldb_private::SymbolContext &sc,
915cab0d23fSAaron Smith     const llvm::pdb::PDBSymbolData &pdb_data) {
916cab0d23fSAaron Smith   VariableSP var_sp;
917cab0d23fSAaron Smith   uint32_t var_uid = pdb_data.getSymIndexId();
918cab0d23fSAaron Smith   auto result = m_variables.find(var_uid);
919cab0d23fSAaron Smith   if (result != m_variables.end())
920cab0d23fSAaron Smith     return result->second;
921cab0d23fSAaron Smith 
922cab0d23fSAaron Smith   ValueType scope = eValueTypeInvalid;
923cab0d23fSAaron Smith   bool is_static_member = false;
924cab0d23fSAaron Smith   bool is_external = false;
925cab0d23fSAaron Smith   bool is_artificial = false;
926cab0d23fSAaron Smith 
927cab0d23fSAaron Smith   switch (pdb_data.getDataKind()) {
928cab0d23fSAaron Smith   case PDB_DataKind::Global:
929cab0d23fSAaron Smith     scope = eValueTypeVariableGlobal;
930cab0d23fSAaron Smith     is_external = true;
931cab0d23fSAaron Smith     break;
932cab0d23fSAaron Smith   case PDB_DataKind::Local:
933cab0d23fSAaron Smith     scope = eValueTypeVariableLocal;
934cab0d23fSAaron Smith     break;
935cab0d23fSAaron Smith   case PDB_DataKind::FileStatic:
936cab0d23fSAaron Smith     scope = eValueTypeVariableStatic;
937cab0d23fSAaron Smith     break;
938cab0d23fSAaron Smith   case PDB_DataKind::StaticMember:
939cab0d23fSAaron Smith     is_static_member = true;
940cab0d23fSAaron Smith     scope = eValueTypeVariableStatic;
941cab0d23fSAaron Smith     break;
942cab0d23fSAaron Smith   case PDB_DataKind::Member:
943cab0d23fSAaron Smith     scope = eValueTypeVariableStatic;
944cab0d23fSAaron Smith     break;
945cab0d23fSAaron Smith   case PDB_DataKind::Param:
946cab0d23fSAaron Smith     scope = eValueTypeVariableArgument;
947cab0d23fSAaron Smith     break;
948cab0d23fSAaron Smith   case PDB_DataKind::Constant:
949cab0d23fSAaron Smith     scope = eValueTypeConstResult;
950cab0d23fSAaron Smith     break;
951cab0d23fSAaron Smith   default:
952cab0d23fSAaron Smith     break;
953cab0d23fSAaron Smith   }
954cab0d23fSAaron Smith 
955cab0d23fSAaron Smith   switch (pdb_data.getLocationType()) {
956cab0d23fSAaron Smith   case PDB_LocType::TLS:
957cab0d23fSAaron Smith     scope = eValueTypeVariableThreadLocal;
958cab0d23fSAaron Smith     break;
959cab0d23fSAaron Smith   case PDB_LocType::RegRel: {
960cab0d23fSAaron Smith     // It is a `this` pointer.
961cab0d23fSAaron Smith     if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
962cab0d23fSAaron Smith       scope = eValueTypeVariableArgument;
963cab0d23fSAaron Smith       is_artificial = true;
964cab0d23fSAaron Smith     }
965cab0d23fSAaron Smith   } break;
966cab0d23fSAaron Smith   default:
967cab0d23fSAaron Smith     break;
968cab0d23fSAaron Smith   }
969cab0d23fSAaron Smith 
970cab0d23fSAaron Smith   Declaration decl;
971cab0d23fSAaron Smith   if (!is_artificial && !pdb_data.isCompilerGenerated()) {
972cab0d23fSAaron Smith     if (auto lines = pdb_data.getLineNumbers()) {
973cab0d23fSAaron Smith       if (auto first_line = lines->getNext()) {
974cab0d23fSAaron Smith         uint32_t src_file_id = first_line->getSourceFileId();
975cab0d23fSAaron Smith         auto src_file = m_session_up->getSourceFileById(src_file_id);
976cab0d23fSAaron Smith         if (src_file) {
9778f3be7a3SJonas Devlieghere           FileSpec spec(src_file->getFileName());
978cab0d23fSAaron Smith           decl.SetFile(spec);
979cab0d23fSAaron Smith           decl.SetColumn(first_line->getColumnNumber());
980cab0d23fSAaron Smith           decl.SetLine(first_line->getLineNumber());
981cab0d23fSAaron Smith         }
982cab0d23fSAaron Smith       }
983cab0d23fSAaron Smith     }
984cab0d23fSAaron Smith   }
985cab0d23fSAaron Smith 
986cab0d23fSAaron Smith   Variable::RangeList ranges;
987cab0d23fSAaron Smith   SymbolContextScope *context_scope = sc.comp_unit;
988758657e5SAleksandr Urakov   if (scope == eValueTypeVariableLocal || scope == eValueTypeVariableArgument) {
989cab0d23fSAaron Smith     if (sc.function) {
990758657e5SAleksandr Urakov       Block &function_block = sc.function->GetBlock(true);
991758657e5SAleksandr Urakov       Block *block =
992758657e5SAleksandr Urakov           function_block.FindBlockByID(pdb_data.getLexicalParentId());
993758657e5SAleksandr Urakov       if (!block)
994758657e5SAleksandr Urakov         block = &function_block;
995758657e5SAleksandr Urakov 
996758657e5SAleksandr Urakov       context_scope = block;
997758657e5SAleksandr Urakov 
998758657e5SAleksandr Urakov       for (size_t i = 0, num_ranges = block->GetNumRanges(); i < num_ranges;
999758657e5SAleksandr Urakov            ++i) {
1000758657e5SAleksandr Urakov         AddressRange range;
1001758657e5SAleksandr Urakov         if (!block->GetRangeAtIndex(i, range))
1002758657e5SAleksandr Urakov           continue;
1003758657e5SAleksandr Urakov 
1004758657e5SAleksandr Urakov         ranges.Append(range.GetBaseAddress().GetFileAddress(),
1005758657e5SAleksandr Urakov                       range.GetByteSize());
1006758657e5SAleksandr Urakov       }
1007cab0d23fSAaron Smith     }
1008cab0d23fSAaron Smith   }
1009cab0d23fSAaron Smith 
1010cab0d23fSAaron Smith   SymbolFileTypeSP type_sp =
1011cab0d23fSAaron Smith       std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId());
1012cab0d23fSAaron Smith 
1013cab0d23fSAaron Smith   auto var_name = pdb_data.getName();
1014cab0d23fSAaron Smith   auto mangled = GetMangledForPDBData(pdb_data);
1015cab0d23fSAaron Smith   auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
1016cab0d23fSAaron Smith 
1017924d5608SJonas Devlieghere   bool is_constant;
1018924d5608SJonas Devlieghere   DWARFExpression location = ConvertPDBLocationToDWARFExpression(
1019758657e5SAleksandr Urakov       GetObjectFile()->GetModule(), pdb_data, ranges, is_constant);
1020cab0d23fSAaron Smith 
1021cab0d23fSAaron Smith   var_sp = std::make_shared<Variable>(
1022cab0d23fSAaron Smith       var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope,
1023cab0d23fSAaron Smith       ranges, &decl, location, is_external, is_artificial, is_static_member);
1024924d5608SJonas Devlieghere   var_sp->SetLocationIsConstantValueData(is_constant);
1025cab0d23fSAaron Smith 
1026cab0d23fSAaron Smith   m_variables.insert(std::make_pair(var_uid, var_sp));
1027cab0d23fSAaron Smith   return var_sp;
1028cab0d23fSAaron Smith }
1029cab0d23fSAaron Smith 
1030cab0d23fSAaron Smith size_t
1031cab0d23fSAaron Smith SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
1032cab0d23fSAaron Smith                               const llvm::pdb::PDBSymbol &pdb_symbol,
1033cab0d23fSAaron Smith                               lldb_private::VariableList *variable_list) {
1034cab0d23fSAaron Smith   size_t num_added = 0;
1035cab0d23fSAaron Smith 
1036cab0d23fSAaron Smith   if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) {
1037cab0d23fSAaron Smith     VariableListSP local_variable_list_sp;
1038cab0d23fSAaron Smith 
1039cab0d23fSAaron Smith     auto result = m_variables.find(pdb_data->getSymIndexId());
1040cab0d23fSAaron Smith     if (result != m_variables.end()) {
1041cab0d23fSAaron Smith       if (variable_list)
1042cab0d23fSAaron Smith         variable_list->AddVariableIfUnique(result->second);
1043cab0d23fSAaron Smith     } else {
1044cab0d23fSAaron Smith       // Prepare right VariableList for this variable.
1045cab0d23fSAaron Smith       if (auto lexical_parent = pdb_data->getLexicalParent()) {
1046cab0d23fSAaron Smith         switch (lexical_parent->getSymTag()) {
1047cab0d23fSAaron Smith         case PDB_SymType::Exe:
1048cab0d23fSAaron Smith           assert(sc.comp_unit);
1049cab0d23fSAaron Smith           LLVM_FALLTHROUGH;
1050cab0d23fSAaron Smith         case PDB_SymType::Compiland: {
1051cab0d23fSAaron Smith           if (sc.comp_unit) {
1052cab0d23fSAaron Smith             local_variable_list_sp = sc.comp_unit->GetVariableList(false);
1053cab0d23fSAaron Smith             if (!local_variable_list_sp) {
1054cab0d23fSAaron Smith               local_variable_list_sp = std::make_shared<VariableList>();
1055cab0d23fSAaron Smith               sc.comp_unit->SetVariableList(local_variable_list_sp);
1056cab0d23fSAaron Smith             }
1057cab0d23fSAaron Smith           }
1058cab0d23fSAaron Smith         } break;
1059cab0d23fSAaron Smith         case PDB_SymType::Block:
1060cab0d23fSAaron Smith         case PDB_SymType::Function: {
1061cab0d23fSAaron Smith           if (sc.function) {
1062cab0d23fSAaron Smith             Block *block = sc.function->GetBlock(true).FindBlockByID(
1063cab0d23fSAaron Smith                 lexical_parent->getSymIndexId());
1064cab0d23fSAaron Smith             if (block) {
1065cab0d23fSAaron Smith               local_variable_list_sp = block->GetBlockVariableList(false);
1066cab0d23fSAaron Smith               if (!local_variable_list_sp) {
1067cab0d23fSAaron Smith                 local_variable_list_sp = std::make_shared<VariableList>();
1068cab0d23fSAaron Smith                 block->SetVariableList(local_variable_list_sp);
1069cab0d23fSAaron Smith               }
1070cab0d23fSAaron Smith             }
1071cab0d23fSAaron Smith           }
1072cab0d23fSAaron Smith         } break;
1073cab0d23fSAaron Smith         default:
1074cab0d23fSAaron Smith           break;
1075cab0d23fSAaron Smith         }
1076cab0d23fSAaron Smith       }
1077cab0d23fSAaron Smith 
1078cab0d23fSAaron Smith       if (local_variable_list_sp) {
1079cab0d23fSAaron Smith         if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) {
1080cab0d23fSAaron Smith           local_variable_list_sp->AddVariableIfUnique(var_sp);
1081cab0d23fSAaron Smith           if (variable_list)
1082cab0d23fSAaron Smith             variable_list->AddVariableIfUnique(var_sp);
1083cab0d23fSAaron Smith           ++num_added;
1084c68925abSZachary Turner           PDBASTParser *ast = GetPDBAstParser();
1085c68925abSZachary Turner           if (ast)
1086c68925abSZachary Turner             ast->GetDeclForSymbol(*pdb_data);
1087cab0d23fSAaron Smith         }
1088cab0d23fSAaron Smith       }
1089cab0d23fSAaron Smith     }
1090cab0d23fSAaron Smith   }
1091cab0d23fSAaron Smith 
1092cab0d23fSAaron Smith   if (auto results = pdb_symbol.findAllChildren()) {
1093cab0d23fSAaron Smith     while (auto result = results->getNext())
1094cab0d23fSAaron Smith       num_added += ParseVariables(sc, *result, variable_list);
1095cab0d23fSAaron Smith   }
1096cab0d23fSAaron Smith 
1097cab0d23fSAaron Smith   return num_added;
1098cab0d23fSAaron Smith }
1099cab0d23fSAaron Smith 
1100b9c1b51eSKate Stone uint32_t SymbolFilePDB::FindGlobalVariables(
11010e4c4821SAdrian Prantl     lldb_private::ConstString name,
110234cda14bSPavel Labath     const lldb_private::CompilerDeclContext *parent_decl_ctx,
1103b9c1b51eSKate Stone     uint32_t max_matches, lldb_private::VariableList &variables) {
1104656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1105cab0d23fSAaron Smith   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1106cab0d23fSAaron Smith     return 0;
1107cab0d23fSAaron Smith   if (name.IsEmpty())
1108cab0d23fSAaron Smith     return 0;
1109cab0d23fSAaron Smith 
1110709426b3SAleksandr Urakov   auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
1111cab0d23fSAaron Smith   if (!results)
1112cab0d23fSAaron Smith     return 0;
1113cab0d23fSAaron Smith 
1114cab0d23fSAaron Smith   uint32_t matches = 0;
1115cab0d23fSAaron Smith   size_t old_size = variables.GetSize();
1116cab0d23fSAaron Smith   while (auto result = results->getNext()) {
1117cab0d23fSAaron Smith     auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
1118cab0d23fSAaron Smith     if (max_matches > 0 && matches >= max_matches)
1119cab0d23fSAaron Smith       break;
1120cab0d23fSAaron Smith 
1121cab0d23fSAaron Smith     SymbolContext sc;
1122*d2deeb44SPavel Labath     sc.module_sp = m_objfile_sp->GetModule();
1123cab0d23fSAaron Smith     lldbassert(sc.module_sp.get());
1124cab0d23fSAaron Smith 
1125709426b3SAleksandr Urakov     if (!name.GetStringRef().equals(
1126c1e530eeSAleksandr Urakov             MSVCUndecoratedNameParser::DropScope(pdb_data->getName())))
1127709426b3SAleksandr Urakov       continue;
1128709426b3SAleksandr Urakov 
1129356aa4a9SAleksandr Urakov     sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
1130356aa4a9SAleksandr Urakov     // FIXME: We are not able to determine the compile unit.
1131356aa4a9SAleksandr Urakov     if (sc.comp_unit == nullptr)
1132356aa4a9SAleksandr Urakov       continue;
1133356aa4a9SAleksandr Urakov 
1134a5235af9SAleksandr Urakov     if (parent_decl_ctx && GetDeclContextContainingUID(
1135a5235af9SAleksandr Urakov                                result->getSymIndexId()) != *parent_decl_ctx)
1136709426b3SAleksandr Urakov       continue;
1137709426b3SAleksandr Urakov 
1138cab0d23fSAaron Smith     ParseVariables(sc, *pdb_data, &variables);
1139cab0d23fSAaron Smith     matches = variables.GetSize() - old_size;
1140cab0d23fSAaron Smith   }
1141cab0d23fSAaron Smith 
1142cab0d23fSAaron Smith   return matches;
114374e08ca0SZachary Turner }
114474e08ca0SZachary Turner 
114574e08ca0SZachary Turner uint32_t
1146b9c1b51eSKate Stone SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
114734cda14bSPavel Labath                                    uint32_t max_matches,
1148b9c1b51eSKate Stone                                    lldb_private::VariableList &variables) {
1149656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1150cab0d23fSAaron Smith   if (!regex.IsValid())
1151cab0d23fSAaron Smith     return 0;
1152cab0d23fSAaron Smith   auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
1153cab0d23fSAaron Smith   if (!results)
1154cab0d23fSAaron Smith     return 0;
1155cab0d23fSAaron Smith 
1156cab0d23fSAaron Smith   uint32_t matches = 0;
1157cab0d23fSAaron Smith   size_t old_size = variables.GetSize();
1158cab0d23fSAaron Smith   while (auto pdb_data = results->getNext()) {
1159cab0d23fSAaron Smith     if (max_matches > 0 && matches >= max_matches)
1160cab0d23fSAaron Smith       break;
1161cab0d23fSAaron Smith 
1162cab0d23fSAaron Smith     auto var_name = pdb_data->getName();
1163cab0d23fSAaron Smith     if (var_name.empty())
1164cab0d23fSAaron Smith       continue;
1165cab0d23fSAaron Smith     if (!regex.Execute(var_name))
1166cab0d23fSAaron Smith       continue;
1167cab0d23fSAaron Smith     SymbolContext sc;
1168*d2deeb44SPavel Labath     sc.module_sp = m_objfile_sp->GetModule();
1169cab0d23fSAaron Smith     lldbassert(sc.module_sp.get());
1170cab0d23fSAaron Smith 
1171356aa4a9SAleksandr Urakov     sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
1172cab0d23fSAaron Smith     // FIXME: We are not able to determine the compile unit.
1173cab0d23fSAaron Smith     if (sc.comp_unit == nullptr)
1174cab0d23fSAaron Smith       continue;
1175cab0d23fSAaron Smith 
1176cab0d23fSAaron Smith     ParseVariables(sc, *pdb_data, &variables);
1177cab0d23fSAaron Smith     matches = variables.GetSize() - old_size;
1178cab0d23fSAaron Smith   }
1179cab0d23fSAaron Smith 
1180cab0d23fSAaron Smith   return matches;
1181b9c1b51eSKate Stone }
1182b9c1b51eSKate Stone 
1183e664b5dcSAaron Smith bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
11847ac1c780SAaron Smith                                     bool include_inlines,
11857ac1c780SAaron Smith                                     lldb_private::SymbolContextList &sc_list) {
11867ac1c780SAaron Smith   lldb_private::SymbolContext sc;
1187a3a8cc80SAaron Smith   sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
11887ac1c780SAaron Smith   if (!sc.comp_unit)
11897ac1c780SAaron Smith     return false;
11907ac1c780SAaron Smith   sc.module_sp = sc.comp_unit->GetModule();
1191863f8c18SZachary Turner   sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, *sc.comp_unit);
11927ac1c780SAaron Smith   if (!sc.function)
11937ac1c780SAaron Smith     return false;
11947ac1c780SAaron Smith 
11957ac1c780SAaron Smith   sc_list.Append(sc);
11967ac1c780SAaron Smith   return true;
11977ac1c780SAaron Smith }
11987ac1c780SAaron Smith 
11997ac1c780SAaron Smith bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
12007ac1c780SAaron Smith                                     lldb_private::SymbolContextList &sc_list) {
1201c8316ed2SAaron Smith   auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
12027ac1c780SAaron Smith   if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
12037ac1c780SAaron Smith     return false;
1204e664b5dcSAaron Smith   return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
12057ac1c780SAaron Smith }
12067ac1c780SAaron Smith 
12077ac1c780SAaron Smith void SymbolFilePDB::CacheFunctionNames() {
12087ac1c780SAaron Smith   if (!m_func_full_names.IsEmpty())
12097ac1c780SAaron Smith     return;
12107ac1c780SAaron Smith 
12117ac1c780SAaron Smith   std::map<uint64_t, uint32_t> addr_ids;
12127ac1c780SAaron Smith 
12137ac1c780SAaron Smith   if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
12147ac1c780SAaron Smith     while (auto pdb_func_up = results_up->getNext()) {
1215f76fe682SAaron Smith       if (pdb_func_up->isCompilerGenerated())
1216f76fe682SAaron Smith         continue;
1217f76fe682SAaron Smith 
12187ac1c780SAaron Smith       auto name = pdb_func_up->getName();
12197ac1c780SAaron Smith       auto demangled_name = pdb_func_up->getUndecoratedName();
12207ac1c780SAaron Smith       if (name.empty() && demangled_name.empty())
12217ac1c780SAaron Smith         continue;
12227ac1c780SAaron Smith 
1223f76fe682SAaron Smith       auto uid = pdb_func_up->getSymIndexId();
12247ac1c780SAaron Smith       if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
12257ac1c780SAaron Smith         addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
12267ac1c780SAaron Smith 
12277ac1c780SAaron Smith       if (auto parent = pdb_func_up->getClassParent()) {
12287ac1c780SAaron Smith 
12297ac1c780SAaron Smith         // PDB have symbols for class/struct methods or static methods in Enum
12307ac1c780SAaron Smith         // Class. We won't bother to check if the parent is UDT or Enum here.
12317ac1c780SAaron Smith         m_func_method_names.Append(ConstString(name), uid);
12327ac1c780SAaron Smith 
123305097246SAdrian Prantl         // To search a method name, like NS::Class:MemberFunc, LLDB searches
123405097246SAdrian Prantl         // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
123505097246SAdrian Prantl         // not have inforamtion of this, we extract base names and cache them
123605097246SAdrian Prantl         // by our own effort.
1237c1e530eeSAleksandr Urakov         llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
12387ac1c780SAaron Smith         if (!basename.empty())
12397ac1c780SAaron Smith           m_func_base_names.Append(ConstString(basename), uid);
12407ac1c780SAaron Smith         else {
12417ac1c780SAaron Smith           m_func_base_names.Append(ConstString(name), uid);
12427ac1c780SAaron Smith         }
12437ac1c780SAaron Smith 
12447ac1c780SAaron Smith         if (!demangled_name.empty())
12457ac1c780SAaron Smith           m_func_full_names.Append(ConstString(demangled_name), uid);
12467ac1c780SAaron Smith 
12477ac1c780SAaron Smith       } else {
12487ac1c780SAaron Smith         // Handle not-method symbols.
12497ac1c780SAaron Smith 
1250c1e530eeSAleksandr Urakov         // The function name might contain namespace, or its lexical scope.
1251c1e530eeSAleksandr Urakov         llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
1252c1e530eeSAleksandr Urakov         if (!basename.empty())
1253c1e530eeSAleksandr Urakov           m_func_base_names.Append(ConstString(basename), uid);
1254c1e530eeSAleksandr Urakov         else
12557ac1c780SAaron Smith           m_func_base_names.Append(ConstString(name), uid);
12567ac1c780SAaron Smith 
12577ac1c780SAaron Smith         if (name == "main") {
12587ac1c780SAaron Smith           m_func_full_names.Append(ConstString(name), uid);
12597ac1c780SAaron Smith 
12607ac1c780SAaron Smith           if (!demangled_name.empty() && name != demangled_name) {
12617ac1c780SAaron Smith             m_func_full_names.Append(ConstString(demangled_name), uid);
12627ac1c780SAaron Smith             m_func_base_names.Append(ConstString(demangled_name), uid);
12637ac1c780SAaron Smith           }
12647ac1c780SAaron Smith         } else if (!demangled_name.empty()) {
12657ac1c780SAaron Smith           m_func_full_names.Append(ConstString(demangled_name), uid);
12667ac1c780SAaron Smith         } else {
12677ac1c780SAaron Smith           m_func_full_names.Append(ConstString(name), uid);
12687ac1c780SAaron Smith         }
12697ac1c780SAaron Smith       }
12707ac1c780SAaron Smith     }
12717ac1c780SAaron Smith   }
12727ac1c780SAaron Smith 
12737ac1c780SAaron Smith   if (auto results_up =
12747ac1c780SAaron Smith           m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
12757ac1c780SAaron Smith     while (auto pub_sym_up = results_up->getNext()) {
12767ac1c780SAaron Smith       if (!pub_sym_up->isFunction())
12777ac1c780SAaron Smith         continue;
12787ac1c780SAaron Smith       auto name = pub_sym_up->getName();
12797ac1c780SAaron Smith       if (name.empty())
12807ac1c780SAaron Smith         continue;
12817ac1c780SAaron Smith 
12827ac1c780SAaron Smith       if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
12837ac1c780SAaron Smith         auto vm_addr = pub_sym_up->getVirtualAddress();
12847ac1c780SAaron Smith 
12857ac1c780SAaron Smith         // PDB public symbol has mangled name for its associated function.
12867ac1c780SAaron Smith         if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
12877ac1c780SAaron Smith           // Cache mangled name.
12887ac1c780SAaron Smith           m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
12897ac1c780SAaron Smith         }
12907ac1c780SAaron Smith       }
12917ac1c780SAaron Smith     }
12927ac1c780SAaron Smith   }
12937ac1c780SAaron Smith   // Sort them before value searching is working properly
12947ac1c780SAaron Smith   m_func_full_names.Sort();
12957ac1c780SAaron Smith   m_func_full_names.SizeToFit();
12967ac1c780SAaron Smith   m_func_method_names.Sort();
12977ac1c780SAaron Smith   m_func_method_names.SizeToFit();
12987ac1c780SAaron Smith   m_func_base_names.Sort();
12997ac1c780SAaron Smith   m_func_base_names.SizeToFit();
13007ac1c780SAaron Smith }
13017ac1c780SAaron Smith 
1302b9c1b51eSKate Stone uint32_t SymbolFilePDB::FindFunctions(
13030e4c4821SAdrian Prantl     lldb_private::ConstString name,
1304b9c1b51eSKate Stone     const lldb_private::CompilerDeclContext *parent_decl_ctx,
1305117b1fa1SZachary Turner     FunctionNameType name_type_mask, bool include_inlines, bool append,
1306b9c1b51eSKate Stone     lldb_private::SymbolContextList &sc_list) {
1307656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
13087ac1c780SAaron Smith   if (!append)
13097ac1c780SAaron Smith     sc_list.Clear();
13107ac1c780SAaron Smith   lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
13117ac1c780SAaron Smith 
13127ac1c780SAaron Smith   if (name_type_mask == eFunctionNameTypeNone)
13137ac1c780SAaron Smith     return 0;
13147ac1c780SAaron Smith   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
13157ac1c780SAaron Smith     return 0;
13167ac1c780SAaron Smith   if (name.IsEmpty())
13177ac1c780SAaron Smith     return 0;
13187ac1c780SAaron Smith 
13197ac1c780SAaron Smith   auto old_size = sc_list.GetSize();
13204d4d63eeSPavel Labath   if (name_type_mask & eFunctionNameTypeFull ||
13214d4d63eeSPavel Labath       name_type_mask & eFunctionNameTypeBase ||
13227ac1c780SAaron Smith       name_type_mask & eFunctionNameTypeMethod) {
13237ac1c780SAaron Smith     CacheFunctionNames();
13247ac1c780SAaron Smith 
13257ac1c780SAaron Smith     std::set<uint32_t> resolved_ids;
1326a5235af9SAleksandr Urakov     auto ResolveFn = [this, &name, parent_decl_ctx, include_inlines, &sc_list,
1327a5235af9SAleksandr Urakov                       &resolved_ids](UniqueCStringMap<uint32_t> &Names) {
13287ac1c780SAaron Smith       std::vector<uint32_t> ids;
1329a5235af9SAleksandr Urakov       if (!Names.GetValues(name, ids))
1330a5235af9SAleksandr Urakov         return;
1331a5235af9SAleksandr Urakov 
1332a5235af9SAleksandr Urakov       for (uint32_t id : ids) {
1333a5235af9SAleksandr Urakov         if (resolved_ids.find(id) != resolved_ids.end())
1334a5235af9SAleksandr Urakov           continue;
1335a5235af9SAleksandr Urakov 
1336a5235af9SAleksandr Urakov         if (parent_decl_ctx &&
1337a5235af9SAleksandr Urakov             GetDeclContextContainingUID(id) != *parent_decl_ctx)
1338a5235af9SAleksandr Urakov           continue;
1339a5235af9SAleksandr Urakov 
13407ac1c780SAaron Smith         if (ResolveFunction(id, include_inlines, sc_list))
13417ac1c780SAaron Smith           resolved_ids.insert(id);
13427ac1c780SAaron Smith       }
13437ac1c780SAaron Smith     };
13447ac1c780SAaron Smith     if (name_type_mask & eFunctionNameTypeFull) {
13457ac1c780SAaron Smith       ResolveFn(m_func_full_names);
1346a5235af9SAleksandr Urakov       ResolveFn(m_func_base_names);
1347a5235af9SAleksandr Urakov       ResolveFn(m_func_method_names);
13487ac1c780SAaron Smith     }
13497ac1c780SAaron Smith     if (name_type_mask & eFunctionNameTypeBase) {
13507ac1c780SAaron Smith       ResolveFn(m_func_base_names);
13517ac1c780SAaron Smith     }
13527ac1c780SAaron Smith     if (name_type_mask & eFunctionNameTypeMethod) {
13537ac1c780SAaron Smith       ResolveFn(m_func_method_names);
13547ac1c780SAaron Smith     }
13557ac1c780SAaron Smith   }
13567ac1c780SAaron Smith   return sc_list.GetSize() - old_size;
135774e08ca0SZachary Turner }
135874e08ca0SZachary Turner 
135974e08ca0SZachary Turner uint32_t
1360b9c1b51eSKate Stone SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1361b9c1b51eSKate Stone                              bool include_inlines, bool append,
1362b9c1b51eSKate Stone                              lldb_private::SymbolContextList &sc_list) {
1363656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
13647ac1c780SAaron Smith   if (!append)
13657ac1c780SAaron Smith     sc_list.Clear();
13667ac1c780SAaron Smith   if (!regex.IsValid())
13677ac1c780SAaron Smith     return 0;
13687ac1c780SAaron Smith 
13697ac1c780SAaron Smith   auto old_size = sc_list.GetSize();
13707ac1c780SAaron Smith   CacheFunctionNames();
13717ac1c780SAaron Smith 
13727ac1c780SAaron Smith   std::set<uint32_t> resolved_ids;
1373c8316ed2SAaron Smith   auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1374c8316ed2SAaron Smith                     this](UniqueCStringMap<uint32_t> &Names) {
13757ac1c780SAaron Smith     std::vector<uint32_t> ids;
13767ac1c780SAaron Smith     if (Names.GetValues(regex, ids)) {
13777ac1c780SAaron Smith       for (auto id : ids) {
13787ac1c780SAaron Smith         if (resolved_ids.find(id) == resolved_ids.end())
13797ac1c780SAaron Smith           if (ResolveFunction(id, include_inlines, sc_list))
13807ac1c780SAaron Smith             resolved_ids.insert(id);
13817ac1c780SAaron Smith       }
13827ac1c780SAaron Smith     }
13837ac1c780SAaron Smith   };
13847ac1c780SAaron Smith   ResolveFn(m_func_full_names);
13857ac1c780SAaron Smith   ResolveFn(m_func_base_names);
13867ac1c780SAaron Smith 
13877ac1c780SAaron Smith   return sc_list.GetSize() - old_size;
138874e08ca0SZachary Turner }
138974e08ca0SZachary Turner 
1390b9c1b51eSKate Stone void SymbolFilePDB::GetMangledNamesForFunction(
1391b9c1b51eSKate Stone     const std::string &scope_qualified_name,
1392b9c1b51eSKate Stone     std::vector<lldb_private::ConstString> &mangled_names) {}
139374e08ca0SZachary Turner 
13948cfb12b9SAleksandr Urakov void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) {
13958cfb12b9SAleksandr Urakov   std::set<lldb::addr_t> sym_addresses;
13968cfb12b9SAleksandr Urakov   for (size_t i = 0; i < symtab.GetNumSymbols(); i++)
13978cfb12b9SAleksandr Urakov     sym_addresses.insert(symtab.SymbolAtIndex(i)->GetFileAddress());
13988cfb12b9SAleksandr Urakov 
13998cfb12b9SAleksandr Urakov   auto results = m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>();
14008cfb12b9SAleksandr Urakov   if (!results)
14018cfb12b9SAleksandr Urakov     return;
14028cfb12b9SAleksandr Urakov 
1403*d2deeb44SPavel Labath   auto section_list = m_objfile_sp->GetSectionList();
14048cfb12b9SAleksandr Urakov   if (!section_list)
14058cfb12b9SAleksandr Urakov     return;
14068cfb12b9SAleksandr Urakov 
14078cfb12b9SAleksandr Urakov   while (auto pub_symbol = results->getNext()) {
14087db8b5c4SPavel Labath     auto section_id = pub_symbol->getAddressSection();
14098cfb12b9SAleksandr Urakov 
14107db8b5c4SPavel Labath     auto section = section_list->FindSectionByID(section_id);
14118cfb12b9SAleksandr Urakov     if (!section)
14128cfb12b9SAleksandr Urakov       continue;
14138cfb12b9SAleksandr Urakov 
14148cfb12b9SAleksandr Urakov     auto offset = pub_symbol->getAddressOffset();
14158cfb12b9SAleksandr Urakov 
14168cfb12b9SAleksandr Urakov     auto file_addr = section->GetFileAddress() + offset;
14178cfb12b9SAleksandr Urakov     if (sym_addresses.find(file_addr) != sym_addresses.end())
14188cfb12b9SAleksandr Urakov       continue;
14198cfb12b9SAleksandr Urakov     sym_addresses.insert(file_addr);
14208cfb12b9SAleksandr Urakov 
14218cfb12b9SAleksandr Urakov     auto size = pub_symbol->getLength();
14228cfb12b9SAleksandr Urakov     symtab.AddSymbol(
14238cfb12b9SAleksandr Urakov         Symbol(pub_symbol->getSymIndexId(),   // symID
14248cfb12b9SAleksandr Urakov                pub_symbol->getName().c_str(), // name
14258cfb12b9SAleksandr Urakov                true,                          // name_is_mangled
14268cfb12b9SAleksandr Urakov                pub_symbol->isCode() ? eSymbolTypeCode : eSymbolTypeData, // type
14278cfb12b9SAleksandr Urakov                true,      // external
14288cfb12b9SAleksandr Urakov                false,     // is_debug
14298cfb12b9SAleksandr Urakov                false,     // is_trampoline
14308cfb12b9SAleksandr Urakov                false,     // is_artificial
14318cfb12b9SAleksandr Urakov                section,   // section_sp
14328cfb12b9SAleksandr Urakov                offset,    // value
14338cfb12b9SAleksandr Urakov                size,      // size
14348cfb12b9SAleksandr Urakov                size != 0, // size_is_valid
14358cfb12b9SAleksandr Urakov                false,     // contains_linker_annotations
14368cfb12b9SAleksandr Urakov                0          // flags
14378cfb12b9SAleksandr Urakov                ));
14388cfb12b9SAleksandr Urakov   }
14398cfb12b9SAleksandr Urakov 
14408cfb12b9SAleksandr Urakov   symtab.CalculateSymbolSizes();
14418cfb12b9SAleksandr Urakov   symtab.Finalize();
14428cfb12b9SAleksandr Urakov }
14438cfb12b9SAleksandr Urakov 
1444b9c1b51eSKate Stone uint32_t SymbolFilePDB::FindTypes(
14450e4c4821SAdrian Prantl     lldb_private::ConstString name,
1446b9c1b51eSKate Stone     const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1447b9c1b51eSKate Stone     uint32_t max_matches,
144874e08ca0SZachary Turner     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1449b9c1b51eSKate Stone     lldb_private::TypeMap &types) {
1450656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
145142dff790SZachary Turner   if (!append)
145242dff790SZachary Turner     types.Clear();
145342dff790SZachary Turner   if (!name)
145442dff790SZachary Turner     return 0;
14557ac1c780SAaron Smith   if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
14567ac1c780SAaron Smith     return 0;
145742dff790SZachary Turner 
145842dff790SZachary Turner   searched_symbol_files.clear();
145942dff790SZachary Turner   searched_symbol_files.insert(this);
146042dff790SZachary Turner 
146186e9434dSAaron Smith   // There is an assumption 'name' is not a regex
1462c1e530eeSAleksandr Urakov   FindTypesByName(name.GetStringRef(), parent_decl_ctx, max_matches, types);
146386e9434dSAaron Smith 
146442dff790SZachary Turner   return types.GetSize();
146542dff790SZachary Turner }
146642dff790SZachary Turner 
14674911023fSZachary Turner void SymbolFilePDB::DumpClangAST(Stream &s) {
14680e252e38SAlex Langford   auto type_system_or_err =
14690e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
14700e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
14710e252e38SAlex Langford     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
14720e252e38SAlex Langford                    std::move(err), "Unable to dump ClangAST");
14734911023fSZachary Turner     return;
14740e252e38SAlex Langford   }
14750e252e38SAlex Langford 
14760e252e38SAlex Langford   auto *clang_type_system =
14770e252e38SAlex Langford       llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
14780e252e38SAlex Langford   if (!clang_type_system)
14790e252e38SAlex Langford     return;
14800e252e38SAlex Langford   clang_type_system->Dump(s);
14814911023fSZachary Turner }
14824911023fSZachary Turner 
1483c8316ed2SAaron Smith void SymbolFilePDB::FindTypesByRegex(
1484c8316ed2SAaron Smith     const lldb_private::RegularExpression &regex, uint32_t max_matches,
1485b9c1b51eSKate Stone     lldb_private::TypeMap &types) {
1486b9c1b51eSKate Stone   // When searching by regex, we need to go out of our way to limit the search
14879d0eb996SAdrian McCarthy   // space as much as possible since this searches EVERYTHING in the PDB,
14889d0eb996SAdrian McCarthy   // manually doing regex comparisons.  PDB library isn't optimized for regex
14899d0eb996SAdrian McCarthy   // searches or searches across multiple symbol types at the same time, so the
1490b9c1b51eSKate Stone   // best we can do is to search enums, then typedefs, then classes one by one,
14919d0eb996SAdrian McCarthy   // and do a regex comparison against each of them.
1492b9c1b51eSKate Stone   PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1493b9c1b51eSKate Stone                                   PDB_SymType::UDT};
149454fd7ff6SZachary Turner   std::unique_ptr<IPDBEnumSymbols> results;
149542dff790SZachary Turner 
149642dff790SZachary Turner   uint32_t matches = 0;
149742dff790SZachary Turner 
1498b9c1b51eSKate Stone   for (auto tag : tags_to_search) {
149910a02577SAaron Smith     results = m_global_scope_up->findAllChildren(tag);
150010a02577SAaron Smith     if (!results)
150110a02577SAaron Smith       continue;
150210a02577SAaron Smith 
1503b9c1b51eSKate Stone     while (auto result = results->getNext()) {
150442dff790SZachary Turner       if (max_matches > 0 && matches >= max_matches)
150542dff790SZachary Turner         break;
150642dff790SZachary Turner 
150742dff790SZachary Turner       std::string type_name;
150854fd7ff6SZachary Turner       if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
150942dff790SZachary Turner         type_name = enum_type->getName();
1510b9c1b51eSKate Stone       else if (auto typedef_type =
1511b9c1b51eSKate Stone                    llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
151242dff790SZachary Turner         type_name = typedef_type->getName();
151354fd7ff6SZachary Turner       else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
151442dff790SZachary Turner         type_name = class_type->getName();
1515b9c1b51eSKate Stone       else {
15169d0eb996SAdrian McCarthy         // We're looking only for types that have names.  Skip symbols, as well
15179d0eb996SAdrian McCarthy         // as unnamed types such as arrays, pointers, etc.
151842dff790SZachary Turner         continue;
151942dff790SZachary Turner       }
152042dff790SZachary Turner 
152186e9434dSAaron Smith       if (!regex.Execute(type_name))
152242dff790SZachary Turner         continue;
152342dff790SZachary Turner 
1524b9c1b51eSKate Stone       // This should cause the type to get cached and stored in the `m_types`
1525b9c1b51eSKate Stone       // lookup.
152642dff790SZachary Turner       if (!ResolveTypeUID(result->getSymIndexId()))
152742dff790SZachary Turner         continue;
152842dff790SZachary Turner 
152942dff790SZachary Turner       auto iter = m_types.find(result->getSymIndexId());
153042dff790SZachary Turner       if (iter == m_types.end())
153142dff790SZachary Turner         continue;
153242dff790SZachary Turner       types.Insert(iter->second);
153342dff790SZachary Turner       ++matches;
153442dff790SZachary Turner     }
153542dff790SZachary Turner   }
153642dff790SZachary Turner }
153742dff790SZachary Turner 
1538709426b3SAleksandr Urakov void SymbolFilePDB::FindTypesByName(
1539c1e530eeSAleksandr Urakov     llvm::StringRef name,
1540709426b3SAleksandr Urakov     const lldb_private::CompilerDeclContext *parent_decl_ctx,
1541709426b3SAleksandr Urakov     uint32_t max_matches, lldb_private::TypeMap &types) {
154254fd7ff6SZachary Turner   std::unique_ptr<IPDBEnumSymbols> results;
1543f76fe682SAaron Smith   if (name.empty())
1544f76fe682SAaron Smith     return;
1545709426b3SAleksandr Urakov   results = m_global_scope_up->findAllChildren(PDB_SymType::None);
154610a02577SAaron Smith   if (!results)
154710a02577SAaron Smith     return;
154842dff790SZachary Turner 
154942dff790SZachary Turner   uint32_t matches = 0;
155042dff790SZachary Turner 
1551b9c1b51eSKate Stone   while (auto result = results->getNext()) {
155242dff790SZachary Turner     if (max_matches > 0 && matches >= max_matches)
155342dff790SZachary Turner       break;
1554709426b3SAleksandr Urakov 
1555c1e530eeSAleksandr Urakov     if (MSVCUndecoratedNameParser::DropScope(
1556c1e530eeSAleksandr Urakov             result->getRawSymbol().getName()) != name)
1557709426b3SAleksandr Urakov       continue;
1558709426b3SAleksandr Urakov 
1559b9c1b51eSKate Stone     switch (result->getSymTag()) {
156054fd7ff6SZachary Turner     case PDB_SymType::Enum:
156154fd7ff6SZachary Turner     case PDB_SymType::UDT:
156254fd7ff6SZachary Turner     case PDB_SymType::Typedef:
156342dff790SZachary Turner       break;
156442dff790SZachary Turner     default:
156505097246SAdrian Prantl       // We're looking only for types that have names.  Skip symbols, as well
156605097246SAdrian Prantl       // as unnamed types such as arrays, pointers, etc.
156742dff790SZachary Turner       continue;
156842dff790SZachary Turner     }
156942dff790SZachary Turner 
1570b9c1b51eSKate Stone     // This should cause the type to get cached and stored in the `m_types`
1571b9c1b51eSKate Stone     // lookup.
157242dff790SZachary Turner     if (!ResolveTypeUID(result->getSymIndexId()))
157342dff790SZachary Turner       continue;
157442dff790SZachary Turner 
1575a5235af9SAleksandr Urakov     if (parent_decl_ctx && GetDeclContextContainingUID(
1576a5235af9SAleksandr Urakov                                result->getSymIndexId()) != *parent_decl_ctx)
1577709426b3SAleksandr Urakov       continue;
1578709426b3SAleksandr Urakov 
157942dff790SZachary Turner     auto iter = m_types.find(result->getSymIndexId());
158042dff790SZachary Turner     if (iter == m_types.end())
158142dff790SZachary Turner       continue;
158242dff790SZachary Turner     types.Insert(iter->second);
158342dff790SZachary Turner     ++matches;
158442dff790SZachary Turner   }
158574e08ca0SZachary Turner }
158674e08ca0SZachary Turner 
1587b9c1b51eSKate Stone size_t SymbolFilePDB::FindTypes(
1588b9c1b51eSKate Stone     const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1589b9c1b51eSKate Stone     lldb_private::TypeMap &types) {
159042dff790SZachary Turner   return 0;
159174e08ca0SZachary Turner }
159274e08ca0SZachary Turner 
1593c8316ed2SAaron Smith void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
15947ac1c780SAaron Smith                                          uint32_t type_mask,
15957ac1c780SAaron Smith                                          TypeCollection &type_collection) {
15967ac1c780SAaron Smith   bool can_parse = false;
1597e664b5dcSAaron Smith   switch (pdb_symbol.getSymTag()) {
15987ac1c780SAaron Smith   case PDB_SymType::ArrayType:
15997ac1c780SAaron Smith     can_parse = ((type_mask & eTypeClassArray) != 0);
16007ac1c780SAaron Smith     break;
16017ac1c780SAaron Smith   case PDB_SymType::BuiltinType:
16027ac1c780SAaron Smith     can_parse = ((type_mask & eTypeClassBuiltin) != 0);
16037ac1c780SAaron Smith     break;
16047ac1c780SAaron Smith   case PDB_SymType::Enum:
16057ac1c780SAaron Smith     can_parse = ((type_mask & eTypeClassEnumeration) != 0);
16067ac1c780SAaron Smith     break;
16077ac1c780SAaron Smith   case PDB_SymType::Function:
16087ac1c780SAaron Smith   case PDB_SymType::FunctionSig:
16097ac1c780SAaron Smith     can_parse = ((type_mask & eTypeClassFunction) != 0);
16107ac1c780SAaron Smith     break;
16117ac1c780SAaron Smith   case PDB_SymType::PointerType:
16127ac1c780SAaron Smith     can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
16137ac1c780SAaron Smith                                eTypeClassMemberPointer)) != 0);
16147ac1c780SAaron Smith     break;
16157ac1c780SAaron Smith   case PDB_SymType::Typedef:
16167ac1c780SAaron Smith     can_parse = ((type_mask & eTypeClassTypedef) != 0);
16177ac1c780SAaron Smith     break;
16187ac1c780SAaron Smith   case PDB_SymType::UDT: {
1619e664b5dcSAaron Smith     auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
16207ac1c780SAaron Smith     assert(udt);
16217ac1c780SAaron Smith     can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
16227ac1c780SAaron Smith                  ((type_mask & (eTypeClassClass | eTypeClassStruct |
16237ac1c780SAaron Smith                                 eTypeClassUnion)) != 0));
16247ac1c780SAaron Smith   } break;
1625c8316ed2SAaron Smith   default:
1626c8316ed2SAaron Smith     break;
16277ac1c780SAaron Smith   }
16287ac1c780SAaron Smith 
16297ac1c780SAaron Smith   if (can_parse) {
1630e664b5dcSAaron Smith     if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
16317ac1c780SAaron Smith       auto result =
16327ac1c780SAaron Smith           std::find(type_collection.begin(), type_collection.end(), type);
16337ac1c780SAaron Smith       if (result == type_collection.end())
16347ac1c780SAaron Smith         type_collection.push_back(type);
16357ac1c780SAaron Smith     }
16367ac1c780SAaron Smith   }
16377ac1c780SAaron Smith 
1638e664b5dcSAaron Smith   auto results_up = pdb_symbol.findAllChildren();
16397ac1c780SAaron Smith   while (auto symbol_up = results_up->getNext())
1640e664b5dcSAaron Smith     GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
16417ac1c780SAaron Smith }
16427ac1c780SAaron Smith 
1643b9c1b51eSKate Stone size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1644117b1fa1SZachary Turner                                TypeClass type_mask,
1645b9c1b51eSKate Stone                                lldb_private::TypeList &type_list) {
1646656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
16477ac1c780SAaron Smith   TypeCollection type_collection;
16487ac1c780SAaron Smith   uint32_t old_size = type_list.GetSize();
1649c8316ed2SAaron Smith   CompileUnit *cu =
1650c8316ed2SAaron Smith       sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
16517ac1c780SAaron Smith   if (cu) {
16527ac1c780SAaron Smith     auto compiland_up = GetPDBCompilandByUID(cu->GetID());
1653e664b5dcSAaron Smith     if (!compiland_up)
1654e664b5dcSAaron Smith       return 0;
1655e664b5dcSAaron Smith     GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
16567ac1c780SAaron Smith   } else {
16577ac1c780SAaron Smith     for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
16587ac1c780SAaron Smith       auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
1659d5a925f4SAaron Smith       if (cu_sp) {
1660e664b5dcSAaron Smith         if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1661e664b5dcSAaron Smith           GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
16627ac1c780SAaron Smith       }
16637ac1c780SAaron Smith     }
16647ac1c780SAaron Smith   }
16657ac1c780SAaron Smith 
16667ac1c780SAaron Smith   for (auto type : type_collection) {
16677ac1c780SAaron Smith     type->GetForwardCompilerType();
16687ac1c780SAaron Smith     type_list.Insert(type->shared_from_this());
16697ac1c780SAaron Smith   }
16707ac1c780SAaron Smith   return type_list.GetSize() - old_size;
167174e08ca0SZachary Turner }
167274e08ca0SZachary Turner 
16730e252e38SAlex Langford llvm::Expected<lldb_private::TypeSystem &>
1674b9c1b51eSKate Stone SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
16750e252e38SAlex Langford   auto type_system_or_err =
1676*d2deeb44SPavel Labath       m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);
16770e252e38SAlex Langford   if (type_system_or_err) {
16780e252e38SAlex Langford     type_system_or_err->SetSymbolFile(this);
16790e252e38SAlex Langford   }
16800e252e38SAlex Langford   return type_system_or_err;
168174e08ca0SZachary Turner }
168274e08ca0SZachary Turner 
1683c68925abSZachary Turner PDBASTParser *SymbolFilePDB::GetPDBAstParser() {
16840e252e38SAlex Langford   auto type_system_or_err =
16850e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
16860e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
16870e252e38SAlex Langford     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
16880e252e38SAlex Langford                    std::move(err), "Unable to get PDB AST parser");
16890e252e38SAlex Langford     return nullptr;
16900e252e38SAlex Langford   }
16910e252e38SAlex Langford 
16920e252e38SAlex Langford   auto *clang_type_system =
16930e252e38SAlex Langford       llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
1694c68925abSZachary Turner   if (!clang_type_system)
1695c68925abSZachary Turner     return nullptr;
1696c68925abSZachary Turner 
1697c68925abSZachary Turner   return clang_type_system->GetPDBParser();
1698c68925abSZachary Turner }
1699c68925abSZachary Turner 
1700c68925abSZachary Turner 
1701b9c1b51eSKate Stone lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
17020e4c4821SAdrian Prantl     lldb_private::ConstString name,
1703b9c1b51eSKate Stone     const lldb_private::CompilerDeclContext *parent_decl_ctx) {
1704656ddeb2SPavel Labath   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
17050e252e38SAlex Langford   auto type_system_or_err =
17060e252e38SAlex Langford       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
17070e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
17080e252e38SAlex Langford     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
17090e252e38SAlex Langford                    std::move(err), "Unable to find namespace {}",
17100e252e38SAlex Langford                    name.AsCString());
17110e252e38SAlex Langford     return CompilerDeclContext();
17120e252e38SAlex Langford   }
17130e252e38SAlex Langford 
17140e252e38SAlex Langford   auto *clang_type_system =
17150e252e38SAlex Langford       llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get());
1716709426b3SAleksandr Urakov   if (!clang_type_system)
1717709426b3SAleksandr Urakov     return CompilerDeclContext();
1718709426b3SAleksandr Urakov 
1719709426b3SAleksandr Urakov   PDBASTParser *pdb = clang_type_system->GetPDBParser();
1720709426b3SAleksandr Urakov   if (!pdb)
1721709426b3SAleksandr Urakov     return CompilerDeclContext();
1722709426b3SAleksandr Urakov 
1723709426b3SAleksandr Urakov   clang::DeclContext *decl_context = nullptr;
1724709426b3SAleksandr Urakov   if (parent_decl_ctx)
1725709426b3SAleksandr Urakov     decl_context = static_cast<clang::DeclContext *>(
1726709426b3SAleksandr Urakov         parent_decl_ctx->GetOpaqueDeclContext());
1727709426b3SAleksandr Urakov 
1728709426b3SAleksandr Urakov   auto namespace_decl =
1729709426b3SAleksandr Urakov       pdb->FindNamespaceDecl(decl_context, name.GetStringRef());
1730709426b3SAleksandr Urakov   if (!namespace_decl)
1731709426b3SAleksandr Urakov     return CompilerDeclContext();
1732709426b3SAleksandr Urakov 
17330e252e38SAlex Langford   return CompilerDeclContext(clang_type_system,
1734709426b3SAleksandr Urakov                              static_cast<clang::DeclContext *>(namespace_decl));
173574e08ca0SZachary Turner }
173674e08ca0SZachary Turner 
1737b9c1b51eSKate Stone lldb_private::ConstString SymbolFilePDB::GetPluginName() {
173874e08ca0SZachary Turner   static ConstString g_name("pdb");
173974e08ca0SZachary Turner   return g_name;
174074e08ca0SZachary Turner }
174174e08ca0SZachary Turner 
1742b9c1b51eSKate Stone uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
174374e08ca0SZachary Turner 
1744b9c1b51eSKate Stone IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1745b9c1b51eSKate Stone 
1746b9c1b51eSKate Stone const IPDBSession &SymbolFilePDB::GetPDBSession() const {
174742dff790SZachary Turner   return *m_session_up;
174842dff790SZachary Turner }
174942dff790SZachary Turner 
1750c8316ed2SAaron Smith lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1751c8316ed2SAaron Smith                                                        uint32_t index) {
175274e08ca0SZachary Turner   auto found_cu = m_comp_units.find(id);
175374e08ca0SZachary Turner   if (found_cu != m_comp_units.end())
175474e08ca0SZachary Turner     return found_cu->second;
175574e08ca0SZachary Turner 
175610a02577SAaron Smith   auto compiland_up = GetPDBCompilandByUID(id);
175710a02577SAaron Smith   if (!compiland_up)
175810a02577SAaron Smith     return CompUnitSP();
175974e08ca0SZachary Turner 
176074e08ca0SZachary Turner   lldb::LanguageType lang;
176110a02577SAaron Smith   auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
176274e08ca0SZachary Turner   if (!details)
176374e08ca0SZachary Turner     lang = lldb::eLanguageTypeC_plus_plus;
176474e08ca0SZachary Turner   else
176574e08ca0SZachary Turner     lang = TranslateLanguage(details->getLanguage());
176674e08ca0SZachary Turner 
1767f76fe682SAaron Smith   if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1768f76fe682SAaron Smith     return CompUnitSP();
1769f76fe682SAaron Smith 
1770487b0c6bSAaron Smith   std::string path = compiland_up->getSourceFileFullPath();
1771f76fe682SAaron Smith   if (path.empty())
1772f76fe682SAaron Smith     return CompUnitSP();
1773f76fe682SAaron Smith 
1774b9c1b51eSKate Stone   // Don't support optimized code for now, DebugInfoPDB does not return this
1775b9c1b51eSKate Stone   // information.
1776ad2b63cbSGreg Clayton   LazyBool optimized = eLazyBoolNo;
1777*d2deeb44SPavel Labath   auto cu_sp = std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr,
1778c8316ed2SAaron Smith                                              path.c_str(), id, lang, optimized);
177910a02577SAaron Smith 
178010a02577SAaron Smith   if (!cu_sp)
178110a02577SAaron Smith     return CompUnitSP();
178210a02577SAaron Smith 
178310a02577SAaron Smith   m_comp_units.insert(std::make_pair(id, cu_sp));
178410a02577SAaron Smith   if (index == UINT32_MAX)
1785e664b5dcSAaron Smith     GetCompileUnitIndex(*compiland_up, index);
178610a02577SAaron Smith   lldbassert(index != UINT32_MAX);
1787e0119909SPavel Labath   SetCompileUnitAtIndex(index, cu_sp);
178810a02577SAaron Smith   return cu_sp;
178974e08ca0SZachary Turner }
179074e08ca0SZachary Turner 
1791863f8c18SZachary Turner bool SymbolFilePDB::ParseCompileUnitLineTable(CompileUnit &comp_unit,
1792863f8c18SZachary Turner                                               uint32_t match_line) {
1793863f8c18SZachary Turner   auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID());
179410a02577SAaron Smith   if (!compiland_up)
179510a02577SAaron Smith     return false;
179674e08ca0SZachary Turner 
1797b9c1b51eSKate Stone   // LineEntry needs the *index* of the file into the list of support files
17989d0eb996SAdrian McCarthy   // returned by ParseCompileUnitSupportFiles.  But the underlying SDK gives us
179905097246SAdrian Prantl   // a globally unique idenfitifier in the namespace of the PDB.  So, we have
180005097246SAdrian Prantl   // to do a mapping so that we can hand out indices.
180142dff790SZachary Turner   llvm::DenseMap<uint32_t, uint32_t> index_map;
180210a02577SAaron Smith   BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
1803863f8c18SZachary Turner   auto line_table = llvm::make_unique<LineTable>(&comp_unit);
180474e08ca0SZachary Turner 
180510a02577SAaron Smith   // Find contributions to `compiland` from all source and header files.
1806863f8c18SZachary Turner   std::string path = comp_unit.GetPath();
180710a02577SAaron Smith   auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
180810a02577SAaron Smith   if (!files)
180910a02577SAaron Smith     return false;
181074e08ca0SZachary Turner 
181105097246SAdrian Prantl   // For each source and header file, create a LineSequence for contributions
181205097246SAdrian Prantl   // to the compiland from that file, and add the sequence.
1813b9c1b51eSKate Stone   while (auto file = files->getNext()) {
1814b9c1b51eSKate Stone     std::unique_ptr<LineSequence> sequence(
1815b9c1b51eSKate Stone         line_table->CreateLineSequenceContainer());
181610a02577SAaron Smith     auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
181710a02577SAaron Smith     if (!lines)
181810a02577SAaron Smith       continue;
181974e08ca0SZachary Turner     int entry_count = lines->getChildCount();
182074e08ca0SZachary Turner 
18217e8c7beaSZachary Turner     uint64_t prev_addr;
18227e8c7beaSZachary Turner     uint32_t prev_length;
18237e8c7beaSZachary Turner     uint32_t prev_line;
18247e8c7beaSZachary Turner     uint32_t prev_source_idx;
18257e8c7beaSZachary Turner 
1826b9c1b51eSKate Stone     for (int i = 0; i < entry_count; ++i) {
182774e08ca0SZachary Turner       auto line = lines->getChildAtIndex(i);
182874e08ca0SZachary Turner 
18297e8c7beaSZachary Turner       uint64_t lno = line->getLineNumber();
18307e8c7beaSZachary Turner       uint64_t addr = line->getVirtualAddress();
18317e8c7beaSZachary Turner       uint32_t length = line->getLength();
183274e08ca0SZachary Turner       uint32_t source_id = line->getSourceFileId();
18337e8c7beaSZachary Turner       uint32_t col = line->getColumnNumber();
183474e08ca0SZachary Turner       uint32_t source_idx = index_map[source_id];
183574e08ca0SZachary Turner 
183605097246SAdrian Prantl       // There was a gap between the current entry and the previous entry if
183705097246SAdrian Prantl       // the addresses don't perfectly line up.
18387e8c7beaSZachary Turner       bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
18397e8c7beaSZachary Turner 
1840b9c1b51eSKate Stone       // Before inserting the current entry, insert a terminal entry at the end
18419d0eb996SAdrian McCarthy       // of the previous entry's address range if the current entry resulted in
18429d0eb996SAdrian McCarthy       // a gap from the previous entry.
1843b9c1b51eSKate Stone       if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1844b9c1b51eSKate Stone         line_table->AppendLineEntryToSequence(
1845b9c1b51eSKate Stone             sequence.get(), prev_addr + prev_length, prev_line, 0,
18467e8c7beaSZachary Turner             prev_source_idx, false, false, false, false, true);
1847010edd37SAaron Smith 
1848010edd37SAaron Smith         line_table->InsertSequence(sequence.release());
1849010edd37SAaron Smith         sequence.reset(line_table->CreateLineSequenceContainer());
18507e8c7beaSZachary Turner       }
18517e8c7beaSZachary Turner 
1852b9c1b51eSKate Stone       if (ShouldAddLine(match_line, lno, length)) {
18537e8c7beaSZachary Turner         bool is_statement = line->isStatement();
185474e08ca0SZachary Turner         bool is_prologue = false;
185574e08ca0SZachary Turner         bool is_epilogue = false;
1856b9c1b51eSKate Stone         auto func =
1857b9c1b51eSKate Stone             m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1858b9c1b51eSKate Stone         if (func) {
185954fd7ff6SZachary Turner           auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
186010a02577SAaron Smith           if (prologue)
18617e8c7beaSZachary Turner             is_prologue = (addr == prologue->getVirtualAddress());
186274e08ca0SZachary Turner 
186354fd7ff6SZachary Turner           auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
186410a02577SAaron Smith           if (epilogue)
18657e8c7beaSZachary Turner             is_epilogue = (addr == epilogue->getVirtualAddress());
18667e8c7beaSZachary Turner         }
186774e08ca0SZachary Turner 
1868b9c1b51eSKate Stone         line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1869b9c1b51eSKate Stone                                               source_idx, is_statement, false,
18707e8c7beaSZachary Turner                                               is_prologue, is_epilogue, false);
18717e8c7beaSZachary Turner       }
18727e8c7beaSZachary Turner 
18737e8c7beaSZachary Turner       prev_addr = addr;
18747e8c7beaSZachary Turner       prev_length = length;
18757e8c7beaSZachary Turner       prev_line = lno;
18767e8c7beaSZachary Turner       prev_source_idx = source_idx;
18777e8c7beaSZachary Turner     }
18787e8c7beaSZachary Turner 
1879b9c1b51eSKate Stone     if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
18807e8c7beaSZachary Turner       // The end is always a terminal entry, so insert it regardless.
1881b9c1b51eSKate Stone       line_table->AppendLineEntryToSequence(
1882b9c1b51eSKate Stone           sequence.get(), prev_addr + prev_length, prev_line, 0,
18837e8c7beaSZachary Turner           prev_source_idx, false, false, false, false, true);
188474e08ca0SZachary Turner     }
188574e08ca0SZachary Turner 
18867e8c7beaSZachary Turner     line_table->InsertSequence(sequence.release());
188774e08ca0SZachary Turner   }
188874e08ca0SZachary Turner 
188910a02577SAaron Smith   if (line_table->GetSize()) {
1890863f8c18SZachary Turner     comp_unit.SetLineTable(line_table.release());
189174e08ca0SZachary Turner     return true;
189274e08ca0SZachary Turner   }
189310a02577SAaron Smith   return false;
189410a02577SAaron Smith }
189574e08ca0SZachary Turner 
1896b9c1b51eSKate Stone void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
189710a02577SAaron Smith     const PDBSymbolCompiland &compiland,
1898b9c1b51eSKate Stone     llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
189905097246SAdrian Prantl   // This is a hack, but we need to convert the source id into an index into
190005097246SAdrian Prantl   // the support files array.  We don't want to do path comparisons to avoid
19019d0eb996SAdrian McCarthy   // basename / full path issues that may or may not even be a problem, so we
19029d0eb996SAdrian McCarthy   // use the globally unique source file identifiers.  Ideally we could use the
19039d0eb996SAdrian McCarthy   // global identifiers everywhere, but LineEntry currently assumes indices.
190410a02577SAaron Smith   auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
190510a02577SAaron Smith   if (!source_files)
190610a02577SAaron Smith     return;
19079ea80d25SPavel Labath 
19089ea80d25SPavel Labath   // LLDB uses the DWARF-like file numeration (one based)
19099ea80d25SPavel Labath   int index = 1;
191074e08ca0SZachary Turner 
1911b9c1b51eSKate Stone   while (auto file = source_files->getNext()) {
191274e08ca0SZachary Turner     uint32_t source_id = file->getUniqueId();
191374e08ca0SZachary Turner     index_map[source_id] = index++;
191474e08ca0SZachary Turner   }
191574e08ca0SZachary Turner }
19167ac1c780SAaron Smith 
19177ac1c780SAaron Smith lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
19187ac1c780SAaron Smith     const lldb_private::Address &so_addr) {
19197ac1c780SAaron Smith   lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1920308e39caSAaron Smith   if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
19217ac1c780SAaron Smith     return nullptr;
19227ac1c780SAaron Smith 
1923308e39caSAaron Smith   // If it is a PDB function's vm addr, this is the first sure bet.
1924308e39caSAaron Smith   if (auto lines =
1925308e39caSAaron Smith           m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1926308e39caSAaron Smith     if (auto first_line = lines->getNext())
1927308e39caSAaron Smith       return ParseCompileUnitForUID(first_line->getCompilandId());
19287ac1c780SAaron Smith   }
19297ac1c780SAaron Smith 
1930308e39caSAaron Smith   // Otherwise we resort to section contributions.
1931308e39caSAaron Smith   if (auto sec_contribs = m_session_up->getSectionContribs()) {
1932308e39caSAaron Smith     while (auto section = sec_contribs->getNext()) {
1933308e39caSAaron Smith       auto va = section->getVirtualAddress();
1934308e39caSAaron Smith       if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1935308e39caSAaron Smith         return ParseCompileUnitForUID(section->getCompilandId());
1936308e39caSAaron Smith     }
1937308e39caSAaron Smith   }
19387ac1c780SAaron Smith   return nullptr;
19397ac1c780SAaron Smith }
19407ac1c780SAaron Smith 
19417ac1c780SAaron Smith Mangled
1942e664b5dcSAaron Smith SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
19437ac1c780SAaron Smith   Mangled mangled;
1944e664b5dcSAaron Smith   auto func_name = pdb_func.getName();
1945e664b5dcSAaron Smith   auto func_undecorated_name = pdb_func.getUndecoratedName();
19467ac1c780SAaron Smith   std::string func_decorated_name;
19477ac1c780SAaron Smith 
19487ac1c780SAaron Smith   // Seek from public symbols for non-static function's decorated name if any.
19497ac1c780SAaron Smith   // For static functions, they don't have undecorated names and aren't exposed
19507ac1c780SAaron Smith   // in Public Symbols either.
19517ac1c780SAaron Smith   if (!func_undecorated_name.empty()) {
1952c8316ed2SAaron Smith     auto result_up = m_global_scope_up->findChildren(
1953c8316ed2SAaron Smith         PDB_SymType::PublicSymbol, func_undecorated_name,
19547ac1c780SAaron Smith         PDB_NameSearchFlags::NS_UndecoratedName);
19557ac1c780SAaron Smith     if (result_up) {
19567ac1c780SAaron Smith       while (auto symbol_up = result_up->getNext()) {
19577ac1c780SAaron Smith         // For a public symbol, it is unique.
19587ac1c780SAaron Smith         lldbassert(result_up->getChildCount() == 1);
19597ac1c780SAaron Smith         if (auto *pdb_public_sym =
1960c8316ed2SAaron Smith                 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1961c8316ed2SAaron Smith                     symbol_up.get())) {
19627ac1c780SAaron Smith           if (pdb_public_sym->isFunction()) {
19637ac1c780SAaron Smith             func_decorated_name = pdb_public_sym->getName();
1964f76fe682SAaron Smith             break;
19657ac1c780SAaron Smith           }
19667ac1c780SAaron Smith         }
19677ac1c780SAaron Smith       }
19687ac1c780SAaron Smith     }
19697ac1c780SAaron Smith   }
19707ac1c780SAaron Smith   if (!func_decorated_name.empty()) {
19717ac1c780SAaron Smith     mangled.SetMangledName(ConstString(func_decorated_name));
19727ac1c780SAaron Smith 
19737ac1c780SAaron Smith     // For MSVC, format of C funciton's decorated name depends on calling
19747ac1c780SAaron Smith     // conventon. Unfortunately none of the format is recognized by current
19757ac1c780SAaron Smith     // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
197605097246SAdrian Prantl     // `__purecall` is retrieved as both its decorated and undecorated name
197705097246SAdrian Prantl     // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
197805097246SAdrian Prantl     // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
197905097246SAdrian Prantl     // Mangled::GetDemangledName method will fail internally and caches an
198005097246SAdrian Prantl     // empty string as its undecorated name. So we will face a contradition
198105097246SAdrian Prantl     // here for the same symbol:
19827ac1c780SAaron Smith     //   non-empty undecorated name from PDB
19837ac1c780SAaron Smith     //   empty undecorated name from LLDB
19847ac1c780SAaron Smith     if (!func_undecorated_name.empty() &&
19857ac1c780SAaron Smith         mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
19867ac1c780SAaron Smith       mangled.SetDemangledName(ConstString(func_undecorated_name));
19877ac1c780SAaron Smith 
19887ac1c780SAaron Smith     // LLDB uses several flags to control how a C++ decorated name is
198905097246SAdrian Prantl     // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
199005097246SAdrian Prantl     // yielded name could be different from what we retrieve from
19917ac1c780SAaron Smith     // PDB source unless we also apply same flags in getting undecorated
19927ac1c780SAaron Smith     // name through PDBSymbolFunc::getUndecoratedNameEx method.
19937ac1c780SAaron Smith     if (!func_undecorated_name.empty() &&
19947ac1c780SAaron Smith         mangled.GetDemangledName(mangled.GuessLanguage()) !=
19957ac1c780SAaron Smith             ConstString(func_undecorated_name))
19967ac1c780SAaron Smith       mangled.SetDemangledName(ConstString(func_undecorated_name));
19977ac1c780SAaron Smith   } else if (!func_undecorated_name.empty()) {
19987ac1c780SAaron Smith     mangled.SetDemangledName(ConstString(func_undecorated_name));
19997ac1c780SAaron Smith   } else if (!func_name.empty())
20007ac1c780SAaron Smith     mangled.SetValue(ConstString(func_name), false);
20017ac1c780SAaron Smith 
20027ac1c780SAaron Smith   return mangled;
20037ac1c780SAaron Smith }
20047ac1c780SAaron Smith 
20057ac1c780SAaron Smith bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
20067ac1c780SAaron Smith     const lldb_private::CompilerDeclContext *decl_ctx) {
20077ac1c780SAaron Smith   if (decl_ctx == nullptr || !decl_ctx->IsValid())
20087ac1c780SAaron Smith     return true;
20097ac1c780SAaron Smith 
20107ac1c780SAaron Smith   TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
20117ac1c780SAaron Smith   if (!decl_ctx_type_system)
20127ac1c780SAaron Smith     return false;
20130e252e38SAlex Langford   auto type_system_or_err = GetTypeSystemForLanguage(
20147ac1c780SAaron Smith       decl_ctx_type_system->GetMinimumLanguage(nullptr));
20150e252e38SAlex Langford   if (auto err = type_system_or_err.takeError()) {
20160e252e38SAlex Langford     LLDB_LOG_ERROR(
20170e252e38SAlex Langford         lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS),
20180e252e38SAlex Langford         std::move(err),
20190e252e38SAlex Langford         "Unable to determine if DeclContext matches this symbol file");
20200e252e38SAlex Langford     return false;
20210e252e38SAlex Langford   }
20220e252e38SAlex Langford 
20230e252e38SAlex Langford   if (decl_ctx_type_system == &type_system_or_err.get())
20247ac1c780SAaron Smith     return true; // The type systems match, return true
20257ac1c780SAaron Smith 
20267ac1c780SAaron Smith   return false;
20277ac1c780SAaron Smith }
2028356aa4a9SAleksandr Urakov 
2029356aa4a9SAleksandr Urakov uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) {
2030356aa4a9SAleksandr Urakov   static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) {
2031356aa4a9SAleksandr Urakov     return lhs < rhs.Offset;
2032356aa4a9SAleksandr Urakov   };
2033356aa4a9SAleksandr Urakov 
2034356aa4a9SAleksandr Urakov   // Cache section contributions
2035356aa4a9SAleksandr Urakov   if (m_sec_contribs.empty()) {
2036356aa4a9SAleksandr Urakov     if (auto SecContribs = m_session_up->getSectionContribs()) {
2037356aa4a9SAleksandr Urakov       while (auto SectionContrib = SecContribs->getNext()) {
2038356aa4a9SAleksandr Urakov         auto comp_id = SectionContrib->getCompilandId();
2039356aa4a9SAleksandr Urakov         if (!comp_id)
2040356aa4a9SAleksandr Urakov           continue;
2041356aa4a9SAleksandr Urakov 
2042356aa4a9SAleksandr Urakov         auto sec = SectionContrib->getAddressSection();
2043356aa4a9SAleksandr Urakov         auto &sec_cs = m_sec_contribs[sec];
2044356aa4a9SAleksandr Urakov 
2045356aa4a9SAleksandr Urakov         auto offset = SectionContrib->getAddressOffset();
2046356aa4a9SAleksandr Urakov         auto it =
2047356aa4a9SAleksandr Urakov             std::upper_bound(sec_cs.begin(), sec_cs.end(), offset, pred_upper);
2048356aa4a9SAleksandr Urakov 
2049356aa4a9SAleksandr Urakov         auto size = SectionContrib->getLength();
2050356aa4a9SAleksandr Urakov         sec_cs.insert(it, {offset, size, comp_id});
2051356aa4a9SAleksandr Urakov       }
2052356aa4a9SAleksandr Urakov     }
2053356aa4a9SAleksandr Urakov   }
2054356aa4a9SAleksandr Urakov 
2055356aa4a9SAleksandr Urakov   // Check by line number
2056356aa4a9SAleksandr Urakov   if (auto Lines = data.getLineNumbers()) {
2057356aa4a9SAleksandr Urakov     if (auto FirstLine = Lines->getNext())
2058356aa4a9SAleksandr Urakov       return FirstLine->getCompilandId();
2059356aa4a9SAleksandr Urakov   }
2060356aa4a9SAleksandr Urakov 
2061356aa4a9SAleksandr Urakov   // Retrieve section + offset
2062356aa4a9SAleksandr Urakov   uint32_t DataSection = data.getAddressSection();
2063356aa4a9SAleksandr Urakov   uint32_t DataOffset = data.getAddressOffset();
2064356aa4a9SAleksandr Urakov   if (DataSection == 0) {
2065356aa4a9SAleksandr Urakov     if (auto RVA = data.getRelativeVirtualAddress())
2066356aa4a9SAleksandr Urakov       m_session_up->addressForRVA(RVA, DataSection, DataOffset);
2067356aa4a9SAleksandr Urakov   }
2068356aa4a9SAleksandr Urakov 
2069356aa4a9SAleksandr Urakov   if (DataSection) {
2070356aa4a9SAleksandr Urakov     // Search by section contributions
2071356aa4a9SAleksandr Urakov     auto &sec_cs = m_sec_contribs[DataSection];
2072356aa4a9SAleksandr Urakov     auto it =
2073356aa4a9SAleksandr Urakov         std::upper_bound(sec_cs.begin(), sec_cs.end(), DataOffset, pred_upper);
2074356aa4a9SAleksandr Urakov     if (it != sec_cs.begin()) {
2075356aa4a9SAleksandr Urakov       --it;
2076356aa4a9SAleksandr Urakov       if (DataOffset < it->Offset + it->Size)
2077356aa4a9SAleksandr Urakov         return it->CompilandId;
2078356aa4a9SAleksandr Urakov     }
2079356aa4a9SAleksandr Urakov   } else {
2080356aa4a9SAleksandr Urakov     // Search in lexical tree
2081356aa4a9SAleksandr Urakov     auto LexParentId = data.getLexicalParentId();
2082356aa4a9SAleksandr Urakov     while (auto LexParent = m_session_up->getSymbolById(LexParentId)) {
2083356aa4a9SAleksandr Urakov       if (LexParent->getSymTag() == PDB_SymType::Exe)
2084356aa4a9SAleksandr Urakov         break;
2085356aa4a9SAleksandr Urakov       if (LexParent->getSymTag() == PDB_SymType::Compiland)
2086356aa4a9SAleksandr Urakov         return LexParentId;
2087356aa4a9SAleksandr Urakov       LexParentId = LexParent->getRawSymbol().getLexicalParentId();
2088356aa4a9SAleksandr Urakov     }
2089356aa4a9SAleksandr Urakov   }
2090356aa4a9SAleksandr Urakov 
2091356aa4a9SAleksandr Urakov   return 0;
2092356aa4a9SAleksandr Urakov }
2093